OpenVAS Scanner  7.0.1~git
bpf_share.c File Reference
#include <gvm/base/logging.h>
#include <pcap.h>
Include dependency graph for bpf_share.c:

Go to the source code of this file.

Macros

#define NUM_CLIENTS   128
 
#define G_LOG_DOMAIN   "lib misc"
 GLib logging domain. More...
 

Functions

void print_pcap_error (pcap_t *p, char *prefix)
 
int bpf_open_live (char *iface, char *filter)
 
u_char * bpf_next_tv (int bpf, int *caplen, struct timeval *tv)
 
u_char * bpf_next (int bpf, int *caplen)
 
int bpf_datalink (int bpf)
 
void bpf_close (int bpf)
 

Variables

static pcap_t * pcaps [NUM_CLIENTS]
 

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "lib misc"

GLib logging domain.

Definition at line 35 of file bpf_share.c.

◆ NUM_CLIENTS

#define NUM_CLIENTS   128
Todo:
There once was a BPF sharing feature with the same API as the methods below, but trying to share BPF among the daemon processes. What remains is a thin abstraction of the pcap API. Eventually it needs to be analysed whether this makes sense or can further be simplified.

Definition at line 29 of file bpf_share.c.

Referenced by bpf_open_live().

Function Documentation

◆ bpf_close()

void bpf_close ( int  bpf)

Definition at line 157 of file bpf_share.c.

References pcaps.

Referenced by nasl_pcap_next(), nasl_send_capture(), nasl_send_packet(), nasl_send_v6packet(), nasl_tcp_ping(), nasl_tcp_v6_ping(), scan(), sendpacket(), and v6_sendpacket().

158 {
159  pcap_close (pcaps[bpf]);
160  pcaps[bpf] = NULL;
161 }
static pcap_t * pcaps[NUM_CLIENTS]
Definition: bpf_share.c:38
Here is the caller graph for this function:

◆ bpf_datalink()

int bpf_datalink ( int  bpf)

Definition at line 151 of file bpf_share.c.

References pcaps.

Referenced by capture_next_packet(), capture_next_v6_packet(), nasl_pcap_next(), nasl_send_capture(), and scan().

152 {
153  return pcap_datalink (pcaps[bpf]);
154 }
static pcap_t * pcaps[NUM_CLIENTS]
Definition: bpf_share.c:38
Here is the caller graph for this function:

◆ bpf_next()

u_char* bpf_next ( int  bpf,
int *  caplen 
)

Definition at line 143 of file bpf_share.c.

References bpf_next_tv(), and timeval().

Referenced by capture_next_packet(), capture_next_v6_packet(), nasl_pcap_next(), nasl_send_capture(), and v6_sendpacket().

144 {
145  struct timeval tv = {0, 100000};
146 
147  return bpf_next_tv (bpf, caplen, &tv);
148 }
struct timeval timeval(unsigned long val)
u_char * bpf_next_tv(int bpf, int *caplen, struct timeval *tv)
Definition: bpf_share.c:112
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bpf_next_tv()

u_char* bpf_next_tv ( int  bpf,
int *  caplen,
struct timeval tv 
)

Definition at line 112 of file bpf_share.c.

References pcaps, and timeval().

Referenced by bpf_next(), nasl_tcp_ping(), nasl_tcp_v6_ping(), and sendpacket().

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 }
struct timeval timeval(unsigned long val)
static pcap_t * pcaps[NUM_CLIENTS]
Definition: bpf_share.c:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bpf_open_live()

int bpf_open_live ( char *  iface,
char *  filter 
)
Returns
-1 in case of error, index of the opened pcap_t in pcaps otherwise.

Definition at line 52 of file bpf_share.c.

References NUM_CLIENTS, pcaps, and print_pcap_error().

Referenced by init_capture_device(), init_v6_capture_device(), nasl_pcap_next(), nasl_send_capture(), openbpf(), and v6_openbpf().

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 }
#define NUM_CLIENTS
Definition: bpf_share.c:29
void print_pcap_error(pcap_t *p, char *prefix)
Definition: bpf_share.c:41
static pcap_t * pcaps[NUM_CLIENTS]
Definition: bpf_share.c:38
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_pcap_error()

void print_pcap_error ( pcap_t *  p,
char *  prefix 
)

Definition at line 41 of file bpf_share.c.

References prefix().

Referenced by bpf_open_live().

42 {
43  char *msg = pcap_geterr (p);
44  g_message ("%s : %s", prefix, msg);
45 }
static void prefix(int n, int i)
Definition: nasl_tree.c:233
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ pcaps

pcap_t* pcaps[NUM_CLIENTS]
static

Shared pcap_t's.

Definition at line 38 of file bpf_share.c.

Referenced by bpf_close(), bpf_datalink(), bpf_next_tv(), and bpf_open_live().