🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.os.linux.programowanie
1 messages
1 total messages Started by Jarek Fri, 23 Sep 2011 09:33
lpcap i 100% obciążenia procesora
#72345
Author: Jarek
Date: Fri, 23 Sep 2011 09:33
78 lines
3146 bytes
Witam

Chciałem napisać swojego "tcpdumpa" przy pomocy biblioteki lpcap ale co
bym nie robił (z filtrami lub bez) mam 100% obciążenia procesora.
Odpalony na tej samej maszynie tcpdump w postaci: "tcpdump -i eth1 -n"
bez dodatkowych filtrów daje 16% obciążenia, a z filtrem  "tcpdump -i
eth1 -n port 25 and tcp[13] == 2" 0%.

Poniżej okrojone do najważniejszego źródło programu, procedura
my_callback() okrojona do postaci jak poniżej i niestety wciąż mam 100%
zajętości procesora.

Komp to 4 rdzeniowy Intel(R) Xeon(R) CPU E5420  @ 2.50GHz, pracuje jako
router i forwarduje 300Mbps ruchu w obu kierunkach z natem i innymi
pierdołami - aczkolwiek nie powinno to mieć znaczenia skoro sam tcpdump
śmiga.

pozdrawiam
Jarek



void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,const
u_char* packet)
{
}

int main(int argc, char **argv)
{
    char *dev="eth1";
    char buff[80];
    char *filter=NULL;
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t* descr;
    struct bpf_program fp;      /* hold compiled program     */
    bpf_u_int32 maskp;          /* subnet mask               */
    bpf_u_int32 netp;           /* ip                        */
    u_char* args = NULL;
    int a=0, b, c, options;
    int pkt_count=1, output_count=MAXCOUNT;

....

    if(filter!=NULL)
      printf("Dumping %d packets from %s with filter
\"%s\"\n",pkt_count, dev,fi
    else
      printf("Dumping %d packets from %s\n",pkt_count, dev);

    pcap_lookupnet(dev,&netp,&maskp,errbuf);

    /* open device for reading. NOTE: defaulting to
     * promiscuous mode*/
    descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf);
    if(descr == NULL)
    {
      printf("pcap_open_live(): %s\n",errbuf); exit(1);
    }


...

    if(filter!=NULL)
    {
        /* Lets try and compile the program.. non-optimized */
        if(pcap_compile(descr,&fp,filter,0,netp) == -1)
        { fprintf(stderr,"Error calling pcap_compile\n"); exit(1); }

        /* set the compiled program as the filter */
        if(pcap_setfilter(descr,&fp) == -1)
        { fprintf(stderr,"Error setting filter\n"); exit(1); }
    }

    /* ... and loop */
    pcap_loop(descr,pkt_count,my_callback,NULL);

   return 0;
}
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads