Search

'Filtering the traffic'에 해당되는 글 1건

  1. 2016.03.07 Winpcap Test 06

Winpcap Test 06

Programming/C,CPP,CS 2016. 3. 7. 09:59 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Filtering the traffic


The functions used to filter packets are pcap_compile() and pcap_setfilter().


pcap_compile() takes a string containing a high-level Boolean (filter) expression and produces a low-level byte code that can be interpreted by the fileter engine in the packet driver. The syntax of the boolean expression can be found in the Filtering expression syntaxsection of this documentation.


pcap_setfilter() associates a filter with a capture session in the kernel driver. Once pcap_setfilter() is called, the associated filter will be applied to all the packets coming from the network, and all the conformant packets (i.e., packets for which the Boolean expression evaluates to true) will be actually copied to the application.



    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; 


compile the filter
    if (pcap_compile(adhandle, &fcode, "ip and tcp", 1, netmask) < 0)
    {
        fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }
    
set the filter
    if (pcap_setfilter(adhandle, &fcode) < 0)
    {
        fprintf(stderr,"\nError setting the filter.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }


'Programming > C,CPP,CS' 카테고리의 다른 글

Winpcap Test 08  (0) 2016.03.07
Winpcap Test 07  (0) 2016.03.07
Winpcap Test 05  (0) 2016.03.07
Winpcap Test 04  (0) 2016.03.07
Winpcap Test 03  (0) 2016.03.07