OpenVAS Libraries  9.0.3
bpf_share.c
Go to the documentation of this file.
1 /* Copyright (C) 2003 Renaud Deraison
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2,
5  * as published by the Free Software Foundation
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  */
17 
24 #include <pcap.h>
25 
26 #include "openvas_logging.h"
27 
28 #undef DEBUG
29 #undef DEBUG_HIGH
30 #define NUM_CLIENTS 128
31 
33 static pcap_t *pcaps[NUM_CLIENTS];
34 
39 int
40 bpf_open_live (char *iface, char *filter)
41 {
42  char errbuf[PCAP_ERRBUF_SIZE];
43  pcap_t *ret;
44  bpf_u_int32 netmask, network;
45  struct bpf_program filter_prog;
46  int i;
47 
48  for (i = 0; (i < (NUM_CLIENTS - 1)) && (pcaps[i]); i++)
49  ;
50 
51  if (pcaps[i])
52  {
53  log_legacy_write ("no free pcap");
54  return -1;
55  }
56 
57 
58  if (iface == NULL)
59  iface = pcap_lookupdev (errbuf);
60 
61  ret = pcap_open_live (iface, 1500, 0, 1, errbuf);
62  if (ret == NULL)
63  {
64  log_legacy_write ("%s", errbuf);
65  return -1;
66  }
67 
68  if (pcap_lookupnet (iface, &network, &netmask, 0) < 0)
69  {
70  log_legacy_write ("pcap_lookupnet failed");
71  pcap_close (ret);
72  return -1;
73  }
74 
75  if (pcap_compile (ret, &filter_prog, filter, 1, netmask) < 0)
76  {
77  pcap_perror (ret, "pcap_compile");
78  pcap_close (ret);
79  return -1;
80  }
81 
82  if (pcap_setnonblock (ret, 1, NULL) == -1)
83  {
84  pcap_perror (ret, "pcap_setnonblock");
86  ("call to pcap_setnonblock failed, some plugins/scripts will"
87  " hang/freeze. Upgrade your version of libcap!");
88  }
89 
90  if (pcap_setfilter (ret, &filter_prog) < 0)
91  {
92  pcap_perror (ret, "pcap_setfilter\n");
93  pcap_close (ret);
94  return -1;
95  }
96  pcaps[i] = ret;
97  pcap_freecode (&filter_prog);
98  return i;
99 }
100 
101 
102 
103 u_char *
104 bpf_next_tv (int bpf, int *caplen, struct timeval * tv)
105 {
106  u_char *p = NULL;
107  struct pcap_pkthdr head;
108  struct timeval timeout, now;
109 
110  gettimeofday (&timeout, NULL);
111  timeout.tv_sec += tv->tv_sec;
112  timeout.tv_usec += tv->tv_usec;
113  while (timeout.tv_usec >= 1000000)
114  {
115  timeout.tv_sec++;
116  timeout.tv_usec -= 1000000;
117  }
118 
119  do
120  {
121  p = (u_char *) pcap_next (pcaps[bpf], &head);
122  *caplen = head.caplen;
123  if (p != NULL)
124  break;
125  gettimeofday (&now, NULL);
126  }
127  while (!
128  ((now.tv_sec > timeout.tv_sec)
129  || (now.tv_sec == timeout.tv_sec && now.tv_usec >= timeout.tv_usec)));
130 
131 
132  return p;
133 }
134 
135 
136 u_char *
137 bpf_next (int bpf, int *caplen)
138 {
139  struct timeval tv = { 0, 100000 };
140 
141  return bpf_next_tv (bpf, caplen, &tv);
142 }
143 
144 
145 int
146 bpf_datalink (int bpf)
147 {
148  return pcap_datalink (pcaps[bpf]);
149 }
150 
151 
152 void
153 bpf_close (int bpf)
154 {
155  pcap_close (pcaps[bpf]);
156  pcaps[bpf] = NULL;
157 }
void log_legacy_write(const char *format,...)
Legacy function to write a log message.
#define NUM_CLIENTS
Definition: bpf_share.c:30
void bpf_close(int bpf)
Definition: bpf_share.c:153
int bpf_datalink(int bpf)
Definition: bpf_share.c:146
int bpf_open_live(char *iface, char *filter)
Definition: bpf_share.c:40
struct timeval timeval(unsigned long val)
u_char * bpf_next(int bpf, int *caplen)
Definition: bpf_share.c:137
u_char * bpf_next_tv(int bpf, int *caplen, struct timeval *tv)
Definition: bpf_share.c:104