1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| pcap_t * PacketAfdx::OpenDev(pcap_if_t *alldevs, int inum) //return -1 if error and return 0 else
{
pcap_t *adhandle;
pcap_if_t *d;
char errbuf[PCAP_ERRBUF_SIZE];
int i;
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
if ( (adhandle= pcap_open(d->name, // name of the device
65536, // portion of the packet to capture.
// 65536 guarantees that the whole packet will be captured on all the link layers
PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
1000, // read timeout
NULL, // authentication on the remote machine
errbuf // error buffer
) ) == NULL)
{
/* Free the device list */
//Console::WriteLine("errbuf:%s",errbuf);
cout <<"Le bloccage est ici";
pcap_freealldevs(alldevs);
}
cout <<"\nlistening on ...\n"<< d->description;
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
}
bpf_u_int32 netmask;
if (d->addresses != NULL)
/* Retrieve the mask of the first address of the interface */
netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
/* If the interface is without an address we suppose to be in a C class network */
netmask=0xffffff;
struct bpf_program *fcode = (bpf_program *) malloc(sizeof(struct bpf_program));
//compile the filter
if (pcap_compile(adhandle, fcode, "udp src port 57344", 1, netmask) < 0)
{
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* Free the device list */
}
//set the filter
if (pcap_setfilter(adhandle, fcode) < 0)
{
fprintf(stderr,"\nError setting the filter.\n");
/* Free the device list */
}
free(fcode);
/* At this point, we don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
return adhandle;
} |
Partager