/
capture.c
145 lines (122 loc) · 3.17 KB
/
capture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* nsntrace is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* nsntrace is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with nsntraces; if not, see <https://www.gnu.org/licenses/>.
*/
#include <pcap.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "capture.h"
/*
* We use libpcap to capture network data. It was originally developed by
* the tcpdump developers.
*
* Each time we get a packet, we count it and dump it to file.
* And we also check that the program we are supposed to trace is still
* alive.
*/
static pcap_dumper_t *pcap_dumper;
static pcap_t *handle;
static pthread_t capture_thread;
static unsigned long packet_count;
static void
_nsntrace_capture_callback(unsigned char *user_data,
struct pcap_pkthdr *header,
unsigned char *package)
{
packet_count++;
pcap_dump(user_data, header, package);
}
static void *
_nsntrace_capture_thread(void *data)
{
pcap_loop(handle, -1, (pcap_handler) _nsntrace_capture_callback,
(unsigned char *) pcap_dumper);
pcap_close(handle);
return NULL;
}
void
nsntrace_capture_stop()
{
pcap_breakloop(handle);
pthread_join(capture_thread, NULL);
}
int
nsntrace_capture_start(const char *iface,
const char *filter,
const char *outfile)
{
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
int ret;
if (!(handle = pcap_open_live(iface, BUFSIZ, 1, 1000, errbuf))) {
fprintf(stderr, "Couldn't open iface: %s\n", errbuf);
return EXIT_FAILURE;
}
if (filter) {
ret = pcap_compile(handle, &fp, filter,
0, PCAP_NETMASK_UNKNOWN);
if (ret < 0) {
fprintf(stderr, "Failed to set filter: %s\n", filter);
return EXIT_FAILURE;
} else {
pcap_setfilter(handle, &fp);
}
}
if (!(pcap_dumper = pcap_dump_open(handle, outfile))) {
fprintf(stderr, "Couldn't open output: %s: %s\n",
outfile, pcap_geterr(handle));
return EXIT_FAILURE;
}
ret = pthread_create(&capture_thread, NULL,
_nsntrace_capture_thread, NULL);
if (ret != 0) {
fprintf(stderr, "Failed to create capture thread\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
unsigned long
nsntrace_capture_packet_count()
{
return packet_count;
}
void
nsntrace_capture_flush()
{
pcap_dump_flush(pcap_dumper);
}
char *
nsntrace_capture_default_device()
{
return pcap_lookupdev(NULL);
}
int
nsntrace_capture_check_device(char *iface)
{
pcap_if_t *dev;
pcap_if_t *devList;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&devList, errbuf) == -1) {
fprintf(stderr, "pcap_findalldevs: %s\n", errbuf);
return EXIT_FAILURE;
}
for (dev = devList; dev != NULL; dev = dev->next) {
if (strcmp(dev->name, iface) == 0) {
return EXIT_SUCCESS;
}
}
fprintf(stderr, "Unknown interface: %s\n", iface);
return EXIT_FAILURE;
}