12 char errbuf
[PCAP_ERRBUF_SIZE
];
15 struct pcap_pkthdr
*header
;
16 const u_char
*pkt_data
;
20 /* Retrieve the device list on the local machine */
21 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING
, NULL
, &alldevs
, errbuf
) == -1)
23 fprintf(stderr
,"Error in pcap_findalldevs: %s\n", errbuf
);
28 for(d
=alldevs
; d
; d
=d
->next
)
30 printf("%d. %s", ++i
, d
->name
);
32 printf(" (%s)\n", d
->description
);
34 printf(" (No description available)\n");
39 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
43 printf("Enter the interface number (1-%d):",i
);
46 if(inum
< 1 || inum
> i
)
48 printf("\nInterface number out of range.\n");
49 /* Free the device list */
50 pcap_freealldevs(alldevs
);
54 /* Jump to the selected adapter */
55 for(d
=alldevs
, i
=0; i
< inum
-1 ;d
=d
->next
, i
++);
58 if ( (adhandle
= pcap_open(d
->name
, // name of the device
59 65536, // portion of the packet to capture.
60 // 65536 guarantees that the whole packet will be captured on all the link layers
61 PCAP_OPENFLAG_PROMISCUOUS
, // promiscuous mode
63 NULL
, // authentication on the remote machine
64 errbuf
// error buffer
67 fprintf(stderr
,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d
->name
);
68 /* Free the device list */
69 pcap_freealldevs(alldevs
);
73 printf("\nlistening on %s...\n", d
->description
);
75 /* At this point, we don't need any more the device list. Free it */
76 pcap_freealldevs(alldevs
);
78 /* Retrieve the packets */
79 while((res
= pcap_next_ex( adhandle
, &header
, &pkt_data
)) >= 0){
85 /* convert the timestamp to readable format */
86 local_tv_sec
= header
->ts
.tv_sec
;
87 ltime
=localtime(&local_tv_sec
);
88 strftime( timestr
, sizeof timestr
, "%H:%M:%S", ltime
);
90 printf("%s,%.6d len:%d\n", timestr
, header
->ts
.tv_usec
, header
->len
);
94 printf("Error reading the packets: %s\n", pcap_geterr(adhandle
));