File: open_pcap.c

package info (click to toggle)
ipgrab 0.5-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 268 kB
  • ctags: 169
  • sloc: sh: 1,507; ansic: 1,234; makefile: 56
file content (84 lines) | stat: -rw-r--r-- 2,015 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**************************************************************************** 
**
** File: open_pcap()
**
** Author: Mike Borella
**
** Comments: Set up pcap to sniff the packets we want.  Most of these 
** commands are listed in the pcap(3) man page.
**
*****************************************************************************/

#include <stdio.h>
#include <pcap.h>
#include "config.h"

extern struct arg_t *my_args;

/*----------------------------------------------------------------------------
 *
 * pcap_open()
 *
 *----------------------------------------------------------------------------
 */

void open_pcap(void)
{
  extern char *pcap_cmd;
  extern pcap_t *pd;
  extern int datalink;
  bpf_u_int32 localnet, netmask;
  struct bpf_program fcode;
  char errorbuf[PCAP_ERRBUF_SIZE];
  void ftlerr(char *, ...);

  /*
   * Look up the device and get a handle to it
   */

  if (my_args->i == NULL) 
    {
      my_args->i = pcap_lookupdev(errorbuf);
      if (my_args->i == NULL) 
	ftlerr("open_pcap: pcap_lookupdev() failed for %s: %s", my_args->i, 
	       errorbuf);
    }

  /*
   * Get a file descriptor to the device
   */

  pd = pcap_open_live(my_args->i, SNAPLEN, PROMISC, READ_TIMEOUT, errorbuf);
  if (pd == NULL) ftlerr("open_pcap: pcap_open_live() failed for %s: %s", 
			 my_args->i, errorbuf);

  /*
   * Determine local net and netmask
   */

  if (pcap_lookupnet(my_args->i, &localnet, &netmask, errorbuf) < 0)
    ftlerr("open_pcap: pcap_lookupnet() failed for %s: %s", my_args->i, 
	   errorbuf);
  
  /*
   * Compile command line filter spec info fcode FSM
   */

  if (pcap_compile(pd, &fcode, pcap_cmd, 0, netmask) < 0)
    ftlerr("pcap_compile: %s", pcap_geterr(pd));

  /*
   * Set the pcap filter with our fcode FSM.  That should do it...
   */

  if (pcap_setfilter(pd, &fcode) < 0)
    ftlerr("pcap_setfilter: %s", pcap_geterr(pd));

  /*
   * Get the data link type 
   */

  datalink = pcap_datalink(pd);
  if (datalink < 0) ftlerr("pcap_datalink: %s", pcap_geterr(pd));

}