CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: PCap Library

  1. #1
    Join Date
    Jun 2011
    Posts
    4

    PCap Library

    I'm able to do most everything in the PCap library successfully, except install this callback function. The line I bolded in the code below is where my program just freezes at. However, I tried using this same code in a console application, and it worked just fine. What's the deal?

    Code:
    void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
    {                   
    	return;
    }
    
    void RawPacket::SetupFilter(pcap_if_t* Device)
    {
    	char Error[256];
    	struct bpf_program fp;
    	char filter_exp[] = "src net 128.0.27.111";
    	bpf_u_int32 mask;
    	bpf_u_int32 net;
    
    	if (pcap_lookupnet(Device->name, &net, &mask, Error) == -1) {
    		fprintf(stderr, "Can't get netmask for device %s\n", Device->name);
    		net = 0;
    		mask = 0;
    	}
    
    	handle = pcap_open_live(Device->name, BUFSIZ, FALSE, 1000, Error);
    	if(!handle) {
    		fprintf(stderr, "Couldn't open device %s: %s\n", Device->name, Error);
    		return;
    	}
    	
    	if (pcap_datalink(handle) != DLT_EN10MB) {
    		fprintf(stderr, "%s is not an Ethernet\n", Device->name);
    		return;
    	}
    
    	if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
    		fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
    		return;
    	}
    	
    	if (pcap_setfilter(handle, &fp) == -1) {
    		fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
    		return;
    	}
    
    	if (pcap_loop(handle, 10, got_packet, NULL) == -1) {
    		fprintf(stderr, "Couldn't install callback: %s\n", pcap_geterr(handle));
    		return;
    	}
    
    	pcap_freecode(&fp);
    	pcap_close(handle);
    
    	filterSetup = true;
    }
    Stack trace:
    ntdll.dll!7d61c846()
    [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
    kernel32.dll!7d4d8c9e()
    kernel32.dll!7d4d8c0d()
    Packet.dll!00d23c3a()
    wpcap.dll!10016810()
    > UDP Packet Test.exe!RawPacket::SetupFilter(pcap_if * Device) Line 359 + 0x14 bytes C++

  2. #2
    Join Date
    Jun 2011
    Posts
    4

    Re: PCap Library

    So I just tried sending 10 packets, and it finally returned from that function.

    Which confuses me, because I thought what pcap_loop() was supposed to do was install an asynchronous receive callback function, and then return.

    Am I doing something wrong?

  3. #3
    Join Date
    Jun 2005
    Posts
    315

    Re: PCap Library

    After reading this, the operation you stated seems about right as you have specified 10 as the packet count to collect prior to returning.

    Code:
    pcap_loop(handle, 10, got_packet, NULL)

  4. #4
    Join Date
    Jun 2011
    Posts
    4

    Re: PCap Library

    Quote Originally Posted by jeron View Post
    After reading this, the operation you stated seems about right as you have specified 10 as the packet count to collect prior to returning.

    Code:
    pcap_loop(handle, 10, got_packet, NULL)
    Yea, I realized this yesterday. I misunderstood what pcap_loop() was supposed to do. Not sure what the point of pcap_next() is. I thought pcap_next() was synchronous receive, and pcap_loop() was asynchronous receive. Turns out they're both synchronous receive.

    Well whatever, I've found my solution. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured