dsniff  2.4b2
About: A collection of tools for network auditing
  Fossies Dox: dsniff-2.4b2.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

pcaputil.c
Go to the documentation of this file.
1 /*
2  * pcaputil.c
3  *
4  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
5  *
6  * $Id: pcaputil.c,v 1.2 2001/03/15 08:33:04 dugsong Exp $
7  */
8 
9 #include "config.h"
10 
11 #include <sys/param.h>
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <err.h>
19 #include <pcap.h>
20 #ifdef BSD
21 #include <pcap-int.h>
22 #endif
23 
24 #include "pcaputil.h"
25 
26 #ifdef BSD
27 static int
28 bpf_immediate(int fd, int on)
29 {
30  return (ioctl(fd, BIOCIMMEDIATE, &on));
31 }
32 #endif
33 
34 int
35 pcap_dloff(pcap_t *pd)
36 {
37  int offset = -1;
38 
39  switch (pcap_datalink(pd)) {
40  case DLT_EN10MB:
41  offset = 14;
42  break;
43  case DLT_IEEE802:
44  offset = 22;
45  break;
46  case DLT_FDDI:
47  offset = 21;
48  break;
49 #ifdef DLT_LOOP
50  case DLT_LOOP:
51 #endif
52  case DLT_NULL:
53  offset = 4;
54  break;
55  default:
56  warnx("unsupported datalink type");
57  break;
58  }
59  return (offset);
60 }
61 
62 pcap_t *
63 pcap_init(char *intf, char *filter, int snaplen)
64 {
65  pcap_t *pd;
66  u_int net, mask;
67  struct bpf_program fcode;
68  char ebuf[PCAP_ERRBUF_SIZE];
69 
70  if (intf == NULL && (intf = pcap_lookupdev(ebuf)) == NULL) {
71  warnx("%s", ebuf);
72  return (NULL);
73  }
74  if ((pd = pcap_open_live(intf, snaplen, 1, 512, ebuf)) == NULL) {
75  warnx("%s", ebuf);
76  return (NULL);
77  }
78  if (pcap_lookupnet(intf, &net, &mask, ebuf) == -1) {
79  warnx("%s", ebuf);
80  return (NULL);
81  }
82  if (pcap_compile(pd, &fcode, filter, 1, mask) < 0) {
83  pcap_perror(pd, "pcap_compile");
84  return (NULL);
85  }
86  if (pcap_setfilter(pd, &fcode) == -1) {
87  pcap_perror(pd, "pcap_compile");
88  return (NULL);
89  }
90 #ifdef BSD
91  if (bpf_immediate(pd->fd, 1) < 0) {
92  perror("ioctl");
93  return (NULL);
94  }
95 #endif
96  return (pd);
97 }
98 
99 /* from tcpdump util.c. */
100 char *
101 copy_argv(char **argv)
102 {
103  char **p, *buf, *src, *dst;
104  u_int len = 0;
105 
106  p = argv;
107  if (*p == 0)
108  return (0);
109 
110  while (*p)
111  len += strlen(*p++) + 1;
112 
113  if ((buf = (char *)malloc(len)) == NULL)
114  err(1, "copy_argv: malloc");
115 
116  p = argv;
117  dst = buf;
118 
119  while ((src = *p++) != NULL) {
120  while ((*dst++ = *src++) != '\0')
121  ;
122  dst[-1] = ' ';
123  }
124  dst[-1] = '\0';
125 
126  return (buf);
127 }
warnx
void warnx(const char *fmt,...)
Definition: err.c:89
pcap_init
pcap_t * pcap_init(char *intf, char *filter, int snaplen)
Definition: pcaputil.c:63
pcaputil.h
buf
static u_char buf[BUFSIZ]
Definition: filenamesnarf.c:29
intf
static char * intf
Definition: arpspoof.c:32
err.h
pcap_dloff
int pcap_dloff(pcap_t *pd)
Definition: pcaputil.c:35
buf
Definition: buf.h:14
err
void err(int eval, const char *fmt,...)
Definition: err.c:47
copy_argv
char * copy_argv(char **argv)
Definition: pcaputil.c:101
config.h