<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns:o = "urn:schemas-microsoft-com:office:office"><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2627" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>Hi,<BR><BR>I tried to implement&nbsp;packet capturing from multiple devices 
in Windows <BR>XP using pcap_geteven and WaitForMultipleEvents, but it does not 
work. I <BR>will really appreciate if you can take a look at my program and 
point out any errors.</DIV>
<DIV><FONT face=Arial size=2>I use WinPcap 3.0</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV>&nbsp;I already worked out a program which can capture packets from single 
device successfully. But when I <BR>tried multi-threaded method or&nbsp;use 
events without multi-threads to capture packets from multiple devices,</DIV>
<DIV>I could not make the code work.&nbsp; The program just hang there and no 
packet is captured. Attached is my program(the filter is set up</DIV>
<DIV>successfully when I run the program).<BR><BR>BTW, when I built this program 
and I get two warnings:<BR>c:\stun\wdcap\seqcap.c(85) : warning C4013: 
'pcap_getevent' undefined; <BR>assuming extern returning 
int<BR>c:\stun\wdcap\seqcap.c(85) : warning C4047: '=' : 'void *' differs in 
levels <BR>of indirection from 'int '<BR><BR>But actually get_getevent is 
well-defined. Why this happen?<BR><BR>Thanks a lot,<BR><BR>Liang<BR><BR><BR>int 
main()<BR>{<BR>&nbsp;capture_tcp_syn("dst host <A 
href="http://www.amazon.com">www.amazon.com</A>");<BR><BR>&nbsp;return 
0;<BR>}<BR><BR>int ifprint(pcap_if_t *d);<BR><BR>char *iptos(u_long 
in);<BR><BR>void RetrievePacket(pcap_t* pdev);<BR><BR>int ApplyFilter(pcap_t 
*hdev, pcap_if_t *devlist, char *filter);<BR><BR>struct packet 
*packetCap;<BR><BR>//*************************************************************<BR>struct 
packet * capture_tcp_type(char* sfiltere, char * tfilter);<BR><BR>struct packet 
* capture_packet(char *filter);<BR><BR>struct packet * capture_tcp_syn(char* 
sfilter)<BR>{<BR>&nbsp;return capture_tcp_type( sfilter, "tcp[13] = 0x02" 
);<BR>}<BR><BR>struct packet * capture_tcp_type(char* sfilter,&nbsp; char* 
tfilter )<BR>{<BR>&nbsp;char filter[512];<BR><BR>&nbsp;sprintf( filter, "ether 
proto <A href="file://\\ip">\\ip</A>" );<BR>&nbsp;sprintf( filter, "%s 
&amp;&amp; %s", filter, sfilter );<BR>&nbsp;sprintf( filter, "%s &amp;&amp; ip 
proto <A href="file://\\tcp">\\tcp</A>", filter );<BR>&nbsp;sprintf( filter, "%s 
&amp;&amp; %s", filter, tfilter );<BR><BR>&nbsp;return capture_packet( filter 
);<BR>}<BR><BR>struct packet *capture_packet(char *filter) 
{<BR><BR>&nbsp;pcap_if_t *alldevs;<BR>&nbsp;pcap_if_t *d;<BR>&nbsp;pcap_t* 
pdevArray[12];<BR>&nbsp;char errbuf[PCAP_ERRBUF_SIZE];<BR>&nbsp;HANDLE 
handleArray[12];<BR>&nbsp;DWORD&nbsp; WaitObj;<BR>&nbsp;int i, 
j;<BR><BR><BR>&nbsp;// Retrieve the device list<BR>&nbsp;if 
(pcap_findalldevs(&amp;alldevs, errbuf) == -1)<BR>&nbsp;{<BR>&nbsp; 
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);<BR>&nbsp; 
exit(1);<BR>&nbsp;}<BR><BR>&nbsp;i = 0;<BR>&nbsp;for(d=alldevs; d; 
d=d-&gt;next)<BR>&nbsp;{<BR>&nbsp; if (ifprint(d))<BR>&nbsp; {<BR>&nbsp;&nbsp; 
if ( (pdevArray[i]= pcap_open_live(d-&gt;name, 590, 0, -1, errbuf) ) == 
<BR>NULL)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; fprintf(stderr,"\nUnable to 
open the adapter. %s is not supported by <BR>WinPcap\n");<BR>&nbsp;&nbsp;&nbsp; 
return NULL;<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; handleArray[i] = 
pcap_getevent(pdevArray[i]);<BR>&nbsp;&nbsp; ApplyFilter(pdevArray[i++], 
alldevs, filter);<BR>&nbsp; }<BR>&nbsp;}<BR><BR>&nbsp;WaitObj = 
WaitForMultipleObjects(i, handleArray, 0, 
INFINITE);<BR>&nbsp;RetrievePacket(pdevArray[WaitObj - 
WAIT_OBJECT_0]);<BR><BR>&nbsp;for(j=0;j&lt;i;j++)<BR>&nbsp; 
CloseHandle(handleArray[j]);<BR><BR>&nbsp;&nbsp;&nbsp; // At this point, we 
don't need any more the device list. Free it<BR>&nbsp;&nbsp;&nbsp; 
pcap_freealldevs(alldevs);<BR><BR>&nbsp;return 
packetCap;<BR><BR>}<BR><BR><BR>void RetrievePacket(pcap_t* 
pdev)<BR>{<BR>&nbsp;struct pcap_pkthdr *pkt_header;<BR>&nbsp;int 
pkt_size;<BR>&nbsp;u_char *pkt_data;<BR>&nbsp;u_char *pdata;<BR>&nbsp;int 
res;<BR><BR>&nbsp;&nbsp;&nbsp; while((res = pcap_next_ex( pdev, &amp;pkt_header, 
&amp;pkt_data)) &gt;= 0){<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
if(res == 
0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 
Timeout 
elapsed<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
continue;<BR><BR>&nbsp; //check that caplen is equal to packet length<BR>&nbsp; 
if (pkt_header-&gt;caplen!=pkt_header-&gt;len)<BR>&nbsp; {<BR>&nbsp;&nbsp; 
printf("Inconsistent header: CapLen %d\t Len 
<BR>%d\n",pkt_header-&gt;caplen,pkt_header-&gt;len);<BR>&nbsp;&nbsp; 
return;<BR>&nbsp; }<BR><BR>&nbsp; pkt_size = pkt_header-&gt;caplen;<BR>&nbsp; if 
(!(pdata = (unsigned char *)malloc(pkt_size * sizeof(char))))<BR>&nbsp; 
{<BR>&nbsp;&nbsp; printf("Memory allocation error for captured 
packet\n");<BR>&nbsp;&nbsp; return;<BR>&nbsp; }<BR><BR>&nbsp; // copy the 
packet<BR>&nbsp; memcpy(pdata, pkt_data, pkt_size);<BR><BR>&nbsp; if 
(!(packetCap = (struct packet *)malloc( sizeof( struct packet ))))<BR>&nbsp; 
{<BR>&nbsp;&nbsp; printf("Memory allocation error for captured 
packet\n");<BR>&nbsp;&nbsp; return;<BR>&nbsp; }<BR><BR>&nbsp; packetCap-&gt;data 
= pdata;<BR>&nbsp; packetCap-&gt;length = pkt_size;<BR><BR>&nbsp; 
return;<BR>&nbsp;&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp;&nbsp; if(res == 
-1){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error reading the 
packets: %s\n", 
pcap_geterr(pdev));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
return;<BR>&nbsp;&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp;&nbsp; 
return;<BR>}<BR><BR><BR>int ApplyFilter(pcap_t *hdev, pcap_if_t *devlist, char 
*filter)<BR>{<BR>&nbsp;u_int netmask;<BR>&nbsp;int retvalue;<BR>&nbsp;struct 
bpf_program fcode;<BR><BR>&nbsp;if(devlist-&gt;addresses!=NULL)<BR>&nbsp; 
netmask=((struct sockaddr_in 
<BR>*)(devlist-&gt;addresses-&gt;netmask))-&gt;sin_addr.S_un.S_addr;<BR>&nbsp;else<BR>&nbsp; 
netmask=0xffffffff;<BR><BR>&nbsp;//compile the 
filter<BR>&nbsp;retvalue=pcap_compile(hdev,&amp;fcode,filter,1,netmask);<BR><BR>&nbsp;if(retvalue&lt;0)<BR>&nbsp;{<BR>&nbsp; 
printf("\n Unable to compile the filter\n");<BR>&nbsp; return 
0;<BR>&nbsp;}<BR><BR>&nbsp;//Set the 
filter<BR>&nbsp;retvalue=pcap_setfilter(hdev,&amp;fcode);<BR><BR>&nbsp;if(retvalue&lt;0)<BR>&nbsp;{<BR>&nbsp; 
printf("\n Unable to set the filter\n");<BR>&nbsp; return 
0;<BR>&nbsp;}<BR><BR><BR>&nbsp;printf("\n Filter applied 
successfully\n");<BR><BR>&nbsp;return 1;<BR><BR>}<BR><BR><BR>// Print all the 
available information on the given interface<BR>int ifprint(pcap_if_t 
*d)<BR>{<BR>&nbsp; pcap_addr_t *a;<BR>&nbsp; int phyDevice = 0;<BR><BR>&nbsp; // 
Name<BR>&nbsp; printf("%s\n",d-&gt;name);<BR><BR>&nbsp; // Description<BR>&nbsp; 
if (d-&gt;description)<BR>&nbsp;&nbsp;&nbsp; printf("\tDescription: 
%s\n",d-&gt;description);<BR>&nbsp; else<BR>&nbsp;printf(" (No description 
available)\n");<BR><BR><BR>&nbsp; // Loopback Address<BR>&nbsp; 
printf("\tLoopback: %s\n", (d-&gt;flags &amp; 
PCAP_IF_LOOPBACK)?"yes":"no");<BR><BR>&nbsp; // IP addresses<BR>&nbsp; 
for(a=d-&gt;addresses;a;a=a-&gt;next) {<BR>&nbsp;&nbsp;&nbsp; printf("\tAddress 
Family: #%d\n",a-&gt;addr-&gt;sa_family);<BR><BR>&nbsp;&nbsp;&nbsp; 
switch(a-&gt;addr-&gt;sa_family)<BR>&nbsp;&nbsp;&nbsp; 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 
AF_INET:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("\tAddress Family 
Name: AF_INET\n");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if 
(a-&gt;addr)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
printf("\tAddress: %s\n",iptos(((struct sockaddr_in 
<BR>*)a-&gt;addr)-&gt;sin_addr.s_addr));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
if (a-&gt;netmask)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
printf("\tNetmask: %s\n",iptos(((struct sockaddr_in 
<BR>*)a-&gt;netmask)-&gt;sin_addr.s_addr));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
if (a-&gt;broadaddr)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in 
<BR>*)a-&gt;broadaddr)-&gt;sin_addr.s_addr));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
if (a-&gt;dstaddr)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in 
<BR>*)a-&gt;dstaddr)-&gt;sin_addr.s_addr));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
default:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("\tAddress Family 
Name: Unknown\n");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
break;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;if (!(d-&gt;flags &amp; 
PCAP_IF_LOOPBACK))<BR>&nbsp; phyDevice = 1;<BR>&nbsp; }<BR>&nbsp; 
printf("\n");<BR><BR>&nbsp; return phyDevice;<BR>}<BR><BR>// From tcptraceroute, 
convert a numeric IP address to a string<BR>#define 
IPTOSBUFFERS&nbsp;&nbsp;&nbsp; 12<BR>char *iptos(u_long 
in)<BR>{<BR>&nbsp;&nbsp;&nbsp; static char 
output[IPTOSBUFFERS][3*4+3+1];<BR>&nbsp;&nbsp;&nbsp; static short 
which;<BR>&nbsp;&nbsp;&nbsp; u_char *p;<BR><BR>&nbsp;&nbsp;&nbsp; p = (u_char 
*)&amp;in;<BR>&nbsp;&nbsp;&nbsp; which = (which + 1 == IPTOSBUFFERS ? 0 : which 
+ 1);<BR>&nbsp;&nbsp;&nbsp; sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], 
p[2], p[3]);<BR>&nbsp;&nbsp;&nbsp; return 
output[which];<BR>}<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>----- Original 
Message ----- <BR>From: "Guy Harris" &lt;<A 
href="mailto:guy@alum.mit.edu">guy@alum.mit.edu</A>&gt;<BR>To: &lt;<A 
href="mailto:winpcap-users@winpcap.polito.it">winpcap-users@winpcap.polito.it</A>&gt;<BR>Sent: 
Monday, May 09, 2005 1:11 PM<BR>Subject: Re: [WinPcap-users] How to capture 
packet from "Any" device using <BR>WinPcap.<BR><BR><BR>&gt; Liang Yang 
wrote:<BR>&gt;<BR>&gt;&gt; In libpcap, we can use "any" as the device name to 
capture packets from <BR>&gt;&gt; any devices.<BR>&gt;<BR>&gt; In libpcap *ON 
LINUX* you can use "any" as the device name to capture from <BR>&gt; all 
devices.<BR>&gt;<BR>&gt; On other UN*Xes the "any" device isn't 
supported.<BR>&gt;<BR>&gt;&gt; But Windows XP does not support "Any" device and 
I do not want to ask the <BR>&gt;&gt; user to select which device to capture 
packets. How to do this using <BR>&gt;&gt; WinPCap?<BR>&gt;<BR>&gt; You'd have 
to capture on all of the devices separately.&nbsp; At least with <BR>&gt; 
WinPcap 3.1 beta (and perhaps 3.0), you could open several devices and <BR>&gt; 
capture on all of them with a single loop using WaitForMultipleEvents() or 
<BR>&gt; MsgWaitForMultipleEvents(), using handles you get from 
pcap_getevent().<BR>&gt;<BR>&gt;<BR>&gt; 
==================================================================<BR>&gt; This 
is the WinPcap users list. It is archived at<BR>&gt; <A 
href="http://www.mail-archive.com/winpcap-users@winpcap.polito.it/">http://www.mail-archive.com/winpcap-users@winpcap.polito.it/</A><BR>&gt;<BR>&gt; 
To unsubscribe use mailto: <BR>&gt; <A 
href="mailto:winpcap-users-request@winpcap.polito.it?body=unsubscribe">winpcap-users-request@winpcap.polito.it?body=unsubscribe</A><BR>&gt; 
==================================================================<BR>&gt; 
<BR></DIV></BODY></HTML>