2008-12-31 14:41:21 +02:00
|
|
|
#include "wrapper.h"
|
2009-01-06 18:14:51 +02:00
|
|
|
#include "storage.h"
|
2008-12-31 14:41:21 +02:00
|
|
|
|
2009-01-03 17:24:39 +02:00
|
|
|
struct s_mac_addr *mac; /* MAC address of the device */
|
2009-01-08 19:40:19 +02:00
|
|
|
char *dev; /* capture device name */
|
|
|
|
int dev_index; /* capture device index */
|
|
|
|
struct in_addr *dev_ip; /* IP address associated with the device */
|
2009-01-10 20:15:49 +02:00
|
|
|
struct in6_addr ip6addr_wrapsix; /* IPv6 prefix of WrapSix addresses */
|
2009-01-08 19:40:19 +02:00
|
|
|
struct in_addr ip4addr_wrapsix; /* IPv4 address for WrapSix */
|
2009-01-06 18:14:51 +02:00
|
|
|
|
|
|
|
/* storage trees */
|
2009-01-07 23:26:25 +02:00
|
|
|
jsw_rbtree_t *stg_conn_tcp;
|
2009-01-07 14:56:25 +02:00
|
|
|
jsw_rbtree_t *stg_conn_udp;
|
2009-01-06 18:14:51 +02:00
|
|
|
jsw_rbtree_t *stg_conn_icmp;
|
2009-01-03 17:24:39 +02:00
|
|
|
|
2008-12-31 14:41:21 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
|
|
|
char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */
|
|
|
|
pcap_t *handle; /* packet capture handle */
|
|
|
|
|
2009-01-07 23:26:25 +02:00
|
|
|
char filter_exp[] = ""; /* filter expression */
|
2008-12-31 14:41:21 +02:00
|
|
|
struct bpf_program fp; /* compiled filter program (expression) */
|
2009-01-03 17:24:39 +02:00
|
|
|
int num_packets = 0; /* number of packets to capture; 0 = infinite */
|
2008-12-31 14:41:21 +02:00
|
|
|
|
2009-01-06 18:14:51 +02:00
|
|
|
/* initialize the storage for connections */
|
2009-01-07 23:26:25 +02:00
|
|
|
stg_conn_tcp = jsw_rbnew(&stg_conn_tup_cmp, &stg_conn_tup_dup, &stg_conn_tup_rel);
|
|
|
|
stg_conn_udp = jsw_rbnew(&stg_conn_tup_cmp, &stg_conn_tup_dup, &stg_conn_tup_rel);
|
2009-01-06 18:14:51 +02:00
|
|
|
stg_conn_icmp = jsw_rbnew(&stg_conn_icmp_cmp, &stg_conn_icmp_dup, &stg_conn_icmp_rel);
|
|
|
|
|
2008-12-31 14:41:21 +02:00
|
|
|
/* find a capture device */
|
2009-01-03 17:24:39 +02:00
|
|
|
dev = NULL;
|
2008-12-31 14:41:21 +02:00
|
|
|
dev = pcap_lookupdev(errbuf);
|
|
|
|
if (dev == NULL) {
|
|
|
|
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print capture info */
|
2009-01-03 17:24:39 +02:00
|
|
|
printf("Device: %s\n", dev);
|
2008-12-31 14:41:21 +02:00
|
|
|
printf("Number of packets: %d\n", num_packets);
|
|
|
|
printf("Filter expression: %s\n", filter_exp);
|
|
|
|
|
|
|
|
/* open capture device */
|
|
|
|
handle = pcap_open_live(dev, SNAP_LEN, 1, 1, errbuf);
|
|
|
|
if (handle == NULL) {
|
|
|
|
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure we're capturing on an Ethernet device */
|
|
|
|
if (pcap_datalink(handle) != DLT_EN10MB) {
|
|
|
|
fprintf(stderr, "%s is not an Ethernet\n", dev);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2009-01-03 17:24:39 +02:00
|
|
|
/* obtain MAC address of the device */
|
|
|
|
mac = (struct s_mac_addr *) malloc(sizeof(struct s_mac_addr));
|
|
|
|
if (get_mac_addr(dev, mac) != 0) {
|
|
|
|
fprintf(stderr, "Couldn't get device MAC address\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2009-01-06 18:14:51 +02:00
|
|
|
/* obtain IP address of the device */
|
|
|
|
dev_ip = (struct in_addr *) malloc(sizeof(struct in_addr));
|
|
|
|
if (get_ip_addr(dev, dev_ip) != 0) {
|
|
|
|
fprintf(stderr, "Couldn't get device IP address\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2009-01-03 17:24:39 +02:00
|
|
|
/* get index of the device */
|
|
|
|
dev_index = get_dev_index(dev);
|
|
|
|
|
2009-01-10 20:15:49 +02:00
|
|
|
/* set the WrapSix addresses */
|
2009-01-08 19:40:19 +02:00
|
|
|
inet_aton("10.0.0.111", &ip4addr_wrapsix);
|
2009-01-10 20:15:49 +02:00
|
|
|
inet_pton(AF_INET6, "fc00:1::", &ip6addr_wrapsix);
|
2009-01-08 19:40:19 +02:00
|
|
|
|
2008-12-31 14:41:21 +02:00
|
|
|
/* compile the filter expression */
|
2009-01-03 17:24:39 +02:00
|
|
|
if (pcap_compile(handle, &fp, filter_exp, 0, 0) == -1) {
|
|
|
|
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
|
2008-12-31 14:41:21 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apply the compiled filter */
|
|
|
|
if (pcap_setfilter(handle, &fp) == -1) {
|
2009-01-03 17:24:39 +02:00
|
|
|
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
|
2008-12-31 14:41:21 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now we can set our callback function */
|
2009-01-06 18:14:51 +02:00
|
|
|
pcap_loop(handle, num_packets, process_packet, NULL);
|
2008-12-31 14:41:21 +02:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
pcap_freecode(&fp);
|
|
|
|
pcap_close(handle);
|
|
|
|
|
2009-01-07 23:26:25 +02:00
|
|
|
jsw_rbdelete(stg_conn_tcp);
|
|
|
|
jsw_rbdelete(stg_conn_udp);
|
2009-01-06 18:14:51 +02:00
|
|
|
jsw_rbdelete(stg_conn_icmp);
|
|
|
|
|
|
|
|
free(mac);
|
|
|
|
free(dev_ip);
|
|
|
|
|
2008-12-31 14:41:21 +02:00
|
|
|
printf("\nCapture complete.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|