Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I would like to use a simple timer that's occured every second, in an applicaton which use libpcap and the pcap_loop function. I am able to use a timer, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2004
    Posts
    2

    Using a timer in a pcap based application


    Hello,

    I would like to use a simple timer that's occured every second, in an applicaton which use libpcap and the pcap_loop function. I am able to use a timer, and the pcap_loop function, but the timers never occured if I put the timer and pcap_loop together.

    Does anybody can help me ?

    Thanks in advance,

    Jérémy

  2. #2
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    How do you implement your timer, i.e. what library routines do you use?
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  3. #3
    Just Joined!
    Join Date
    Dec 2004
    Posts
    2
    For the timer :

    Code:
    #include <signal.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/time.h>
    
    void timer_handler &#40;int signum&#41;
    &#123;
     static int count = 0;
     printf &#40;"timer expired %d times\n", ++count&#41;;
    &#125;
    
    int main &#40;&#41;
    &#123;
     struct sigaction sa;
     struct itimerval timer;
    
     /* Install timer_handler as the signal handler for SIGVTALRM. */
     memset &#40;&sa, 0, sizeof &#40;sa&#41;&#41;;
     sa.sa_handler = &timer_handler;
     sigaction &#40;SIGVTALRM, &sa, NULL&#41;;
    
     /* Configure the timer to expire after 250 msec... */
     timer.it_value.tv_sec = 0;
     timer.it_value.tv_usec = 1000000;
     /* ... and every 250 msec after that. */
     timer.it_interval.tv_sec = 0;
     timer.it_interval.tv_usec = 1000000;
     /* Start a virtual timer. It counts down whenever this process is
       executing. */
     setitimer &#40;ITIMER_VIRTUAL, &timer, NULL&#41;;
    
     /* Do busy work. */
     while &#40;1&#41;;
    &#125;
    And for using pcap :

    Code:
    #include <pcap.h>
    #include <stdio.h>
    
    void callback&#40;u_char *, const struct pcap_pkthdr *, const u_char *&#41;;
    
    int main&#40;&#41;
    &#123;
        
    pcap_t *handle;                 // Session handle
    char *dev;                      // The device to sniff on
    char errbuf&#91;PCAP_ERRBUF_SIZE&#93;;  // Error string
    struct bpf_program filter;      // The compiled filter
    char filter_app&#91;&#93; = "port 23";  // The filter expression
    bpf_u_int32 mask;               // Our netmask
    bpf_u_int32 net;                // Our IP
    struct pcap_pkthdr header;      // The header that pcap give us
    const u_char *packet;           // The actual packet
    pcap_t *desc_pqt;
    
    // Define the device
    //dev = pcap_lookupdev &#40;errbuf&#41;;
    dev="eth0";
    
    // Find the properties for the device
    pcap_lookupnet &#40;dev, &net, &mask, errbuf&#41;;
    
    // Open the session in promiscuous mode
    handle = pcap_open_live &#40;dev, BUFSIZ, 1, 0, errbuf&#41;;
    
    // Compile and apply the filter
    //pcap_compile &#40;handle, &filter, filter_app, 0, net&#41;;
    //pcap_setfilter &#40;handle, &filter&#41;;
    
    if &#40;pcap_loop&#40;handle, -1, callback, NULL&#41; < 0&#41;
    &#123;
        fprintf&#40;stderr,"Erreur pcap_loop&#58; %s\n",pcap_geterr&#40;desc_pqt&#41;&#41;;
        return -1;
    &#125;
    
    /*And close the session*/
    pcap_close &#40;handle&#41;;
    return &#40;0&#41;;
    &#125;
    
    void callback&#40;u_char *nom_param, const struct pcap_pkthdr *entete, const u_char *data&#41;
    &#123;
       
    &#125;    
    
    &#125;

  4. $spacer_open
    $spacer_close

Posting Permissions

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