OpenVAS Scanner 23.40.3
bpf_share.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 2003 Renaud Deraison
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7#include "bpf_share.h"
8
9#include "../nasl/nasl_debug.h" /* for nasl_*_filename */
10
11#include <gvm/base/logging.h>
12#include <pcap.h>
13
14#define NUM_CLIENTS 128
15
16#undef G_LOG_DOMAIN
20#define G_LOG_DOMAIN "lib misc"
21
23static pcap_t *pcaps[NUM_CLIENTS];
24
25static void
26print_pcap_error (pcap_t *p, char *prefix)
27{
28 char *msg = pcap_geterr (p);
29 g_message ("[%s] %s : %s",
31 prefix, msg);
32}
33
38int
39bpf_open_live (char *iface, char *filter)
40{
41 char errbuf[PCAP_ERRBUF_SIZE];
42 pcap_t *ret;
43 pcap_if_t *alldevsp = NULL; /* list of capture devices */
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 g_message ("no free pcap");
54 return -1;
55 }
56
57 if (iface == NULL)
58 {
59 if (pcap_findalldevs (&alldevsp, errbuf) < 0)
60 g_message ("Error for pcap_findalldevs(): %s", errbuf);
61 if (alldevsp != NULL)
62 iface = alldevsp->name;
63 }
64
65 ret = pcap_open_live (iface, 1500, 0, 1, errbuf);
66 if (ret == NULL)
67 {
68 g_message ("%s", errbuf);
69 if (alldevsp != NULL)
70 pcap_freealldevs (alldevsp);
71 return -1;
72 }
73
74 if (pcap_lookupnet (iface, &network, &netmask, errbuf) < 0)
75 {
76 g_message ("pcap_lookupnet failed: %s", errbuf);
77 if (alldevsp != NULL)
78 pcap_freealldevs (alldevsp);
79 pcap_close (ret);
80 return -1;
81 }
82
83 if (pcap_compile (ret, &filter_prog, filter, 1, netmask) < 0)
84 {
85 char buffer[2048];
86 snprintf (buffer, sizeof (buffer), "pcap_compile: Filter \"%s\"", filter);
87 print_pcap_error (ret, buffer);
88 if (alldevsp != NULL)
89 pcap_freealldevs (alldevsp);
90 pcap_close (ret);
91 return -1;
92 }
93
94 if (pcap_setnonblock (ret, 1, NULL) == -1)
95 {
96 print_pcap_error (ret, "pcap_setnonblock");
97 g_message ("call to pcap_setnonblock failed, some plugins/scripts will"
98 " hang/freeze. Upgrade your version of libcap!");
99 }
100
101 if (pcap_setfilter (ret, &filter_prog) < 0)
102 {
103 print_pcap_error (ret, "pcap_setfilter\n");
104 if (alldevsp != NULL)
105 pcap_freealldevs (alldevsp);
106 pcap_freecode (&filter_prog);
107 pcap_close (ret);
108 return -1;
109 }
110 pcaps[i] = ret;
111 pcap_freecode (&filter_prog);
112 if (alldevsp != NULL)
113 pcap_freealldevs (alldevsp);
114
115 return i;
116}
117
118u_char *
119bpf_next_tv (int bpf, int *caplen, struct timeval *tv)
120{
121 u_char *p = NULL;
122 struct pcap_pkthdr head;
123 struct timeval timeout, now;
124
125 gettimeofday (&timeout, NULL);
126 timeout.tv_sec += tv->tv_sec;
127 timeout.tv_usec += tv->tv_usec;
128 while (timeout.tv_usec >= 1000000)
129 {
130 timeout.tv_sec++;
131 timeout.tv_usec -= 1000000;
132 }
133
134 do
135 {
136 p = (u_char *) pcap_next (pcaps[bpf], &head);
137 *caplen = head.caplen;
138 if (p != NULL)
139 break;
140 gettimeofday (&now, NULL);
141 }
142 while (
143 !((now.tv_sec > timeout.tv_sec)
144 || (now.tv_sec == timeout.tv_sec && now.tv_usec >= timeout.tv_usec)));
145
146 return p;
147}
148
149u_char *
150bpf_next (int bpf, int *caplen)
151{
152 struct timeval tv = {0, 100000};
153
154 return bpf_next_tv (bpf, caplen, &tv);
155}
156
157int
159{
160 return pcap_datalink (pcaps[bpf]);
161}
162
163void
164bpf_close (int bpf)
165{
166 pcap_close (pcaps[bpf]);
167 pcaps[bpf] = NULL;
168}
int bpf_open_live(char *iface, char *filter)
Definition bpf_share.c:39
#define NUM_CLIENTS
Definition bpf_share.c:14
static pcap_t * pcaps[NUM_CLIENTS]
Definition bpf_share.c:23
static void print_pcap_error(pcap_t *p, char *prefix)
Definition bpf_share.c:26
int bpf_datalink(int bpf)
Definition bpf_share.c:158
u_char * bpf_next_tv(int bpf, int *caplen, struct timeval *tv)
Definition bpf_share.c:119
void bpf_close(int bpf)
Definition bpf_share.c:164
u_char * bpf_next(int bpf, int *caplen)
Definition bpf_share.c:150
Header file for module bpf_share.
static struct timeval timeval(unsigned long val)
const char * nasl_get_plugin_filename()
Get the current launched plugin filename.
Definition nasl_debug.c:36
static void prefix(int n, int i)
Definition nasl_tree.c:219