#include <netinet/in.h>
#include <netinet/ip6.h>
Go to the source code of this file.
|
| int | init_capture_device (struct in_addr, struct in_addr, char *) |
| | Set up the pcap filter, and select the correct interface. More...
|
| |
| struct ip * | capture_next_packet (int, int, int *) |
| |
| char * | capture_next_frame (int, int, int *, int) |
| | Capture a link layer frame. More...
|
| |
| int | init_v6_capture_device (struct in6_addr, struct in6_addr, char *) |
| |
| struct ip6_hdr * | capture_next_v6_packet (int, int, int *) |
| |
◆ capture_next_frame()
| char* capture_next_frame |
( |
int |
bpf, |
|
|
int |
timeout, |
|
|
int * |
sz, |
|
|
int |
dl_layer_only |
|
) |
| |
Capture a link layer frame.
- Parameters
-
| [in] | bpf | bpf handler |
| [in] | timeout | the timeout |
| [out] | sz | size of the frame, -1 if no frame was received. |
| [in] | dl_layer_only | If the answer should include the payload. |
- Returns
- the link layer frame.
Definition at line 96 of file capture_packet.c.
102 struct timeval past, now, then;
109 memset (&past,
'\0',
sizeof (past));
110 memset (&now,
'\0',
sizeof (now));
111 gettimeofday (&then, &tz);
114 memcpy (&past, &then,
sizeof (then));
118 gettimeofday (&now, &tz);
120 if (now.tv_usec < past.tv_usec)
123 now.tv_usec += 1000000;
128 if ((now.tv_sec - past.tv_sec) >= timeout)
137 if (dl_layer_only == 1)
139 ret = g_malloc0 (dl_len);
140 memcpy (ret, frame, dl_len);
146 ret = g_malloc0 (
len);
147 memcpy (ret, frame,
len);
References bpf_datalink(), bpf_next(), get_datalink_size(), len, and timeval().
Referenced by send_frame().
◆ capture_next_packet()
| struct ip* capture_next_packet |
( |
int |
, |
|
|
int |
, |
|
|
int * |
|
|
) |
| |
Definition at line 157 of file capture_packet.c.
163 struct timeval past, now, then;
170 memset (&past,
'\0',
sizeof (past));
171 memset (&now,
'\0',
sizeof (now));
172 gettimeofday (&then, &tz);
175 memcpy (&past, &then,
sizeof (then));
179 gettimeofday (&now, &tz);
181 if (now.tv_usec < past.tv_usec)
184 now.tv_usec += 1000000;
189 if ((now.tv_sec - past.tv_sec) >= timeout)
200 ip = (
struct ip *) (packet + dl_len);
201 #ifdef BSD_BYTE_ORDERING
202 ip->ip_len = ntohs (ip->ip_len);
203 ip->ip_off = ntohs (ip->ip_off);
205 ip->ip_id = ntohs (ip->ip_id);
206 ret = g_malloc0 (
len - dl_len);
207 memcpy (ret, ip,
len - dl_len);
211 return ((
struct ip *) ret);
References bpf_datalink(), bpf_next(), get_datalink_size(), len, and timeval().
Referenced by nasl_send_packet().
◆ capture_next_v6_packet()
| struct ip6_hdr* capture_next_v6_packet |
( |
int |
, |
|
|
int |
, |
|
|
int * |
|
|
) |
| |
Definition at line 271 of file capture_packet.c.
277 struct timeval past, now, then;
284 memset (&past,
'\0',
sizeof (past));
285 memset (&now,
'\0',
sizeof (now));
286 gettimeofday (&then, &tz);
290 memcpy (&past, &then,
sizeof (then));
296 gettimeofday (&now, &tz);
297 if (now.tv_usec < past.tv_usec)
300 now.tv_usec += 1000000;
305 if ((now.tv_sec - past.tv_sec) >= timeout)
315 ip6 = (
struct ip6_hdr *) (packet + dl_len);
316 #ifdef BSD_BYTE_ORDERING
317 ip6->ip6_plen = ntohs (ip6->ip6_plen);
319 ret = g_malloc0 (
len - dl_len);
320 memcpy (ret, ip6,
len - dl_len);
325 return ((
struct ip6_hdr *) ret);
References bpf_datalink(), bpf_next(), get_datalink_size(), len, and timeval().
Referenced by nasl_send_v6packet().
◆ init_capture_device()
| int init_capture_device |
( |
struct in_addr |
src, |
|
|
struct in_addr |
dest, |
|
|
char * |
filter |
|
) |
| |
Set up the pcap filter, and select the correct interface.
The filter will be changed only if this is necessary
Definition at line 31 of file capture_packet.c.
34 char *
interface = NULL;
36 char errbuf[PCAP_ERRBUF_SIZE];
38 pcap_if_t *alldevsp = NULL;
40 a_src = g_strdup (inet_ntoa (src));
41 a_dst = g_strdup (inet_ntoa (dest));
43 if ((filter == NULL) || (filter[0] ==
'\0') || (filter[0] ==
'0'))
45 filter = g_malloc0 (256);
48 snprintf (filter, 256,
"ip and (src host %s and dst host %s)", a_src,
54 filter = g_strdup (filter);
56 filter = g_malloc0 (1);
69 if (pcap_findalldevs (&alldevsp, errbuf) < 0)
70 g_message (
"Error for pcap_findalldevs(): %s", errbuf);
72 interface = alldevsp->
name;
80 pcap_freealldevs (alldevsp);
References bpf_open_live(), islocalhost(), name, and routethrough().
Referenced by nasl_send_packet(), nasl_tcp_ping(), and send_frame().
◆ init_v6_capture_device()
| int init_v6_capture_device |
( |
struct in6_addr |
, |
|
|
struct in6_addr |
, |
|
|
char * |
|
|
) |
| |
Definition at line 215 of file capture_packet.c.
218 char *
interface = NULL;
221 char name[INET6_ADDRSTRLEN];
222 char errbuf[PCAP_ERRBUF_SIZE];
223 pcap_if_t *alldevsp = NULL;
225 a_src = g_strdup (inet_ntop (AF_INET6, &src,
name, INET6_ADDRSTRLEN));
226 a_dst = g_strdup (inet_ntop (AF_INET6, &dest,
name, INET6_ADDRSTRLEN));
228 if ((filter == NULL) || (filter[0] ==
'\0') || (filter[0] ==
'0'))
230 filter = g_malloc0 (256);
233 snprintf (filter, 256,
"ip and (src host %s and dst host %s", a_src,
239 filter = g_strdup (filter);
241 filter = g_malloc0 (1);
254 if (pcap_findalldevs (&alldevsp, errbuf) < 0)
255 g_message (
"Error for pcap_findalldevs(): %s", errbuf);
256 if (alldevsp != NULL)
257 interface = alldevsp->
name;
261 if (free_filter != 0)
264 if (alldevsp != NULL)
265 pcap_freealldevs (alldevsp);
References bpf_open_live(), name, v6_islocalhost(), and v6_routethrough().
Referenced by nasl_send_v6packet(), nasl_tcp_v6_ping(), and send_frame().