using libpcap with timeout


 
Thread Tools Search this Thread
Top Forums Programming using libpcap with timeout
# 1  
Old 01-05-2011
using libpcap with timeout

I want to write a small application using Libpcap in C on Linux.

Currently, it starts to sniff and waits for the packets. But that's not what I need actually. I want it to wait for N seconds and then stop listening. (I think there's something wrong with my usage of 'pcap_open_live'...)


How can I achieve that?

Here's my code:
Code:
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
       printf("got packet\n);
}

int main()
{
 int ret = 0;
 char *dev = NULL;   /* capture device name */
 char errbuf[PCAP_ERRBUF_SIZE];  /* error buffer */
 pcap_t *handle;    /* packet capture handle */

 char filter_exp[] = "udp dst port 1500";  /* filter expression */
 struct bpf_program fp;   /* compiled filter program (expression) */
 bpf_u_int32 mask;   /* subnet mask */
 bpf_u_int32 net;   /* ip */
 int num_packets = 10;   /* number of packets to capture */


 /* get network number and mask associated with capture device */
 if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
  fprintf(stderr, "Couldn't get netmask for device %s: %s\n",
      dev, errbuf);
  net = 0;
  mask = 0;
 }


 /* print capture info */
 printf("Device: %s\n", dev);
 printf("Number of packets: %d\n", num_packets);
 printf("Filter expression: %s\n", filter_exp);




 /* open capture device */
 handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
 if (handle == NULL) {
  fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
  exit(EXIT_FAILURE);
 }



 /* compile the filter expression */
 if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
  fprintf(stderr, "Couldn't parse filter %s: %s\n",
      filter_exp, pcap_geterr(handle));
  exit(EXIT_FAILURE);
 }

 /* apply the compiled filter */
 if (pcap_setfilter(handle, &fp) == -1) {
  fprintf(stderr, "Couldn't install filter %s: %s\n",
      filter_exp, pcap_geterr(handle));
  exit(EXIT_FAILURE);
 }

 /* now we can set our callback function */
 pcap_loop(handle, num_packets, got_packet, NULL);

 /* cleanup */
 pcap_freecode(&fp);
 pcap_close(handle);
}

Man page says:

pcap_t *pcap_open_live(const char *device, int snaplen,
int promisc, int to_ms, char *errbuf);

DESCRIPTION
pcap_open_live() is used to obtain a packet capture handle to look at packets on the network. device is a string that specifies the network device to open; on Linux systems with 2.2
or later kernels, a device argument of "any" or NULL can be used to capture packets from all interfaces.

snaplen specifies the snapshot length to be set on the handle.

promisc specifies if the interface is to be put into promiscuous mode.

to_ms specifies the read timeout in milliseconds.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Setting a Timeout

Hi I'm writing a script which based on a condition, restarts a set of servers. The problem I'm facing is, say if one of the server is down, my script stops there and fails to proceed. How can I ensure to set a timeout value on that script, so when the server is not reachable, the script should... (2 Replies)
Discussion started by: mathbalaji
2 Replies

2. Programming

Extract payload with libpcap

hi! :) im having a problem while extracting payload from a tcp packet that is captured with libpcap. this is what ive got so far: const struct ethernet_header *ethernet; const struct ip_header *ip; const struct tcp_header *tcp; const char *payload; u_int size_ip; ... (0 Replies)
Discussion started by: shuwo
0 Replies

3. Programming

Libpcap: Set a filter. (C)

Hi I'm reading about the libpcap documentation. I see that with the function: pcap_setfilter ( pcap_t *p, struct bpf_program *fp) we can choose to take one packet instead another. I read that the struct bpf_program allow us to create a packet filter , ....so if I want just sample some... (2 Replies)
Discussion started by: Dedalus
2 Replies

4. Shell Programming and Scripting

how to set timeout?

When I run a script where the 1st parameter is ip address ftp -n -i -v $1 I hang here if the ip is wrong how to set a timeout something like if (20s not complete "ftp -n -i -v $1") then echo "error" fi Thanks a lot. (14 Replies)
Discussion started by: uativan
14 Replies

5. Solaris

About the Timeout

Hello everyone I am a new one,I want to know how to get the solaris force the loginer out if he do not in a time thanks (4 Replies)
Discussion started by: lyh003473
4 Replies

6. HP-UX

timeout

How can I kick a user out after being idle for a certain amount of time, would prefer not to use scripts, will TMOUT work on HP-UX? (5 Replies)
Discussion started by: csaunders
5 Replies
Login or Register to Ask a Question