Thread: libpcap: How to timeout pcap_loop() ?

  1. #1

    libpcap: How to timeout pcap_loop() ?

    I'm using the libpcap library to sniff packets on an interface. (My target OS is Linux, but I don't think that helps here.)

    I'm using pcap_loop() to capture packets, but pcap_loop() waits until a packet is captured before returning to the calling function. Is there any way to force it to only listen for a specified amount of time? I'd like to loop the main program while just checking each loop for new packets, rather than having to wait for a new packet to arrive before doing anything. I've looked around and am pretty sure there's no built-in feature for this (that works on Linux) so might there exist some type of work-around?
    "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably you want to look something in inet like
    http://www.tcpdump.org/lists/workers.../msg00178.html

    Haven't read it throughoutly but seems to me they are talking about same type of problems
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    From the link:
    If switching to pcap_dispatch() instead of pcap_loop() is not a bad option
    for you, one other way of doing what you want is use select for timing
    out. So you can access the pcap file descriptor and select on it for
    either reads or timeouts. When select returns, if read fd_set is set, you
    can call pcap_dispatch(), otherwise for timeout you can do your other
    tasks.
    What's this "select" he's talking about?

    (BTW, sorry for taking so long to return, holidays+travel+start of school kept me busy. :P )
    "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    select() is a function which allows you to wait on I/O. It would allow you to check if there is data to be read on a socket, and then time out if there is nothing to receive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TCP TimeOut
    By vishal_askqns in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2009, 07:02 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. how to setup C# TCPClient timeout
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-13-2007, 12:09 PM
  4. rsh timeout
    By jlai in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 07:18 AM
  5. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM