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