#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <netpacket/packet.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netinet/ip6.h>
#define PACKET_LENGTH 1480
int main(int argc, const char * argv[]) {
struct ip6_hdr *ip6hdr;
const char myaddr[]="fe80::226:c7ff:fe85:6758";
int socket_write,socket_read,slength=0,rlength=0,j=0,h,m;
struct ifreq dev,dev2;
struct sockaddr_ll address,address2;
void *buffer1=NULL;
void *ip6head;
struct ip6_hdr *ipr_hdr;
char *test;
//creaiamo il pacchetto da inviare
if(h!=1 || m!=1) {perror("inet_pton");}
char packet[PACKET_LENGTH],*data;
ip6hdr=(struct ip6_hdr*)packet;
memset(ip6hdr,0,sizeof(struct ip6_hdr));
h=inet_pton(AF_INET6,myaddr,&ip6hdr->ip6_src);
m=inet_pton(AF_INET6,myaddr,&ip6hdr->ip6_src);
if(h!=1 || m!=1) {perror("inet_pton");}
//Apriamo il primo socket in scrittura
socket_write=socket(AF_PACKET,SOCK_RAW,ETH_P_ALL);
if(socket_write==-1) { perror("socket"); }
memset(&dev, 0, sizeof(struct ifreq));
strncpy(dev.ifr_name, "wlan0", IFNAMSIZ);
if(ioctl(socket_write, SIOCGIFINDEX, &dev) == -1) {perror("ioctl");}
address.sll_family=AF_PACKET;
address.sll_protocol=htons(ETH_P_ALL);
address.sll_ifindex=dev.ifr_ifindex;
if(bind(socket_write,(const struct sockaddr *)&address, sizeof(struct sockaddr_ll))==-1) {
perror("bind");
}
//Apriamo il socket in lettura:
socket_read=socket(AF_PACKET,SOCK_RAW,ETH_P_ALL);
if(socket_read==-1) {
perror("socket");
exit(1);
}
memset(&dev2,0,sizeof(struct ifreq));
strncpy(dev2.ifr_name,"wlan0", IFNAMSIZ);
if((ioctl(socket_read, SIOCGIFINDEX, &dev2))==-1) {
perror("ioctl");
close(socket_read);
exit(1);
}
address2.sll_family=AF_PACKET;
address2.sll_protocol=htons(ETH_P_ALL);
address2.sll_ifindex=dev2.ifr_ifindex;
//bind my socket to address
if(bind(socket_read,(const struct sockaddr*)&address2, sizeof(struct sockaddr_ll))==-1) {
perror("bind");
}
//Inviamo e riceviamo i dati
data=(char*)(packet+sizeof(struct ip6_hdr));
strcpy(data,"Questo che leggete qui e' il payload del pacchetto ipv6 :)");
if (( buffer1 = (void*)malloc(PACKET_LENGTH))==NULL) {perror ("malloc");}
while(j<=6) {
slength=send(socket_write, packet,PACKET_LENGTH,MSG_DONTROUTE);
if(slength==-1) {perror("send\n");}
else {
printf("%d", (int)slength);
printf("\ninviato\n");
}
rlength=recv(socket_read,buffer1, PACKET_LENGTH,0);
if(rlength==-1) {perror("recv");}
else {
printf("%d", rlength);
printf("\nricevuto\n");
//Queste due righe creano segmentation fault
ip6head=&buffer1;
ipr_hdr=(struct ip6_hdr*)ip6head;
if(ipr_hdr!=NULL) {
char *buffer2=(char *)(buffer1+sizeof(struct ip6_hdr));
printf("%s", buffer2);
}
else { printf("c'è qualcosa che non va"); }
}
j++;
}
return 0;
}
Ho modificato il codice ma ora mi da segmentation fault quando cerco di leggere l'header...invece se non include le righe relative all'header ipv6 riesco tranquillamente a leggere il payload!