C programming question

Discussion in 'OT Technology' started by spoofy, Mar 24, 2005.

  1. spoofy

    spoofy Chemically Enhanced

    Joined:
    Aug 21, 2001
    Messages:
    5,014
    Likes Received:
    0
    Location:
    houston
    I have a function which sniffs packets on an interface.. when it gets a certain packet it returns a value..

    Now.. I want to time the function ..and if it doesnt return a value in a given amount of time... print an error... any suggestions on how i can do this?
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    If you're using Linux/Unix, then use signals and signal handlers.
    Use alarm() to start a timer that generates the SIGALRM signal when it times out.
    Use signal() specify a signal handling function.

    Code:
    int sniff_timeout = 0;
     
    void alarmhandler(int signo)
    
    
    {[indent]sniff_timeout = 1;
    
    
    [/indent]}
    
     
    int your_sniff_func()
    
    
    {[indent]int found_packet = 0;
    
    sniff_timeout = 0;
    signal(SIGALRM, alarmhandler);
    alarm(10); // set timeout to 10 seconds
    while (!found_packet && !sniff_timeout)
    
    
    {[indent]....
    
    
    [/indent]}
    
    alarm(0); // turn off the timer
    
    
    if (found_packet)[indent]return no_error;
    
    
    [/indent]else if (sniff_timeout)[indent]return timeout_error;
    
    
    [/indent]return other_error; 
    
    
    [/indent]} 
    
    
    The while loop will execute as long as the packet you're looking for isn't found. If nothing is found within 10 seconds, the SIGALRM signal will be generated, the alarmhandler function will be executed and the sniff_timeout flag will be set, stopping the while loop.

    I believe you can use setitimer() if you need more granularity in the timeout interval. If you're using Windows, you can achieve something similar using SetTimer().
     
    Last edited: Mar 25, 2005
  3. SL1200MK4

    SL1200MK4 New Member

    Joined:
    Sep 27, 2003
    Messages:
    1,552
    Likes Received:
    0
    use select()

    or

    fnctl()
     
  4. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    got to love C lol
     
  5. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    C is teh win for low-level stuff :coold:
     
  6. turbo91

    turbo91 New Member

    Joined:
    Jun 14, 2002
    Messages:
    2,521
    Likes Received:
    0
    Location:
    San Diego, CA
    An alternative would be WaitForSingleObject() (w/ mutexes or semaphores) if you are threading your app.
     

Share This Page