14#include <netinet/ip.h>
18#include <sys/socket.h>
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);
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);
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);
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);
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);
int bpf_open_live(char *iface, char *filter)
int bpf_datalink(int bpf)
u_char * bpf_next(int bpf, int *caplen)
Header file for module bpf_share.
struct ip6_hdr * capture_next_v6_packet(int bpf, int timeout, int *sz)
char * capture_next_frame(int bpf, int timeout, int *sz, int dl_layer_only)
Capture a link layer frame.
int islocalhost(struct in_addr *)
Tests whether a packet sent to IP is LIKELY to route through the kernel localhost interface.
int init_capture_device(struct in_addr src, struct in_addr dest, char *filter)
Set up the pcap filter, and select the correct interface.
struct ip * capture_next_packet(int bpf, int timeout, int *sz)
int init_v6_capture_device(struct in6_addr src, struct in6_addr dest, char *filter)
static struct timeval timeval(unsigned long val)
int islocalhost(struct in_addr *addr)
Tests whether a packet sent to IP is LIKELY to route through the kernel localhost interface.
char * routethrough(struct in_addr *dest, struct in_addr *source)
An awesome function to determine what interface a packet to a given destination should be routed thro...
int v6_islocalhost(struct in6_addr *addr)
Tests whether a packet sent to IP is LIKELY to route through the kernel localhost interface.
char * v6_routethrough(struct in6_addr *dest, struct in6_addr *source)
An awesome function to determine what interface a packet to a given destination should be routed thro...
int get_datalink_size(int datalink)
Header file for module pcap.