3 * a simple triggered libpcap-based capture agent
5 * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 static volatile int keep_going;
34 static pcap_t* listener;
35 static struct bpf_program stop_filter;
36 static int captured = 0;
37 static int debug_level = 0;
39 static void panic(int err, const char* fmt, ...) {
42 vfprintf(stderr,fmt,ap);
47 static void dprintf(int lev, const char* fmt, ...) {
50 if (lev <= debug_level) {
52 vfprintf(stderr,fmt,ap);
59 static void usage(int err) {
60 const char* usage_str = "usage:\n"
61 "trigcap -w outfile -b begin -e end [-f capture] [-i iface] [-s snaplen] [-p] [-q] [-d [-d [-d [-d]]]]\n"
63 " -b filter to start capturing\n"
64 " -e filter to stop capturing\n"
65 " -f capture filter\n"
66 " -p promiscuous mode\n"
67 " -s snapshot length\n"
69 " -d increase debug level\n"
70 " -h prints this message\n"
76 static void listener_handler(u_char* u, const struct pcap_pkthdr * ph, const u_char* buf) {
77 char errbuf[PCAP_ERRBUF_SIZE];
79 dprintf(2,"listener handler invoked dumping=%d\n",dumping);
82 dprintf(2,"last round\n");
85 if (pcap_setfilter(listener, &stop_filter) < 0) {
86 panic(23,"could not apply stop filter to listener: %s\n",pcap_geterr(listener));
89 dprintf(2,"apply stop filter to listener\n");
91 if (pcap_setnonblock(listener, 1, errbuf) < 0) {
92 panic(24,"could not set listener in non blocking mode: %s\n",errbuf);
95 dprintf(2,"listener -> non_blocking\n");
100 static void capture_handler(u_char* dumper, const struct pcap_pkthdr * ph, const u_char* buf) {
101 dprintf(4,"capture handler invoked dumping=%d\n",dumping);
104 pcap_dump(dumper, ph, buf);
108 static void sig_int(int sig) {
112 int main(int argc, char** argv) {
113 char errbuf[PCAP_ERRBUF_SIZE];
114 char* interface = NULL;
115 char* outfile = NULL;
116 guint snaplen = 65536;
117 char* start_filter_str = NULL;
118 char* stop_filter_str = NULL;
119 char* capture_filter_str = NULL;
122 struct bpf_program start_filter;
123 struct bpf_program capture_filter;
124 pcap_t* capturer = NULL;
125 pcap_dumper_t* dumper = NULL;
128 while ((opt = getopt(argc, argv, "i:w:s:b:e:f:phdq")) != -1) {
131 if (interface) panic(1,"interface already given");
132 interface = g_strdup(optarg);
135 if (outfile) panic(3,"output file already given");
136 outfile = g_strdup(optarg);
139 snaplen = strtoul(optarg,NULL,10);
141 panic(4,"invalid snaplen");
144 if (start_filter_str) panic(5,"start filter already given");
145 start_filter_str = g_strdup(optarg);
148 if (stop_filter_str) panic(6,"stop filter already given");
149 stop_filter_str = g_strdup(optarg);
152 if (capture_filter_str) panic(7,"capture filter already given");
153 capture_filter_str = g_strdup(optarg);
171 dprintf(1,"starting with:\n interface: %s\n snaplen: %d\n promisc: %d"
172 "\n outfile: %s\n capture filter: %s\n start: %s\n stop: %s\n debug level: %d\n",
173 interface ? interface : "to be chosen",
176 outfile ? outfile : "** missing **",
177 capture_filter_str ? capture_filter_str : "** none given **",
178 start_filter_str ? start_filter_str : "** missing **",
179 stop_filter_str ? stop_filter_str : "** missing **",
182 if (! ( start_filter_str && stop_filter_str && outfile ) ) {
187 interface = pcap_lookupdev(errbuf);
189 panic(11, "could not obtain an interface: %s\n",errbuf);
193 #ifdef HAVE_PCAP_OPEN
194 if ( ! ( capturer = pcap_open(interface, snaplen, promisc, 1, NULL, errbuf) )) {
196 if ( ! ( capturer = pcap_open_live(interface, snaplen, promisc, 1, errbuf) )) {
198 panic(12,"could not open interface '%s' for listener: %s\n",interface,errbuf);
201 dprintf(1,"opened listener (%s,%d,%d)\n",interface,snaplen, promisc);
203 if (pcap_compile(listener, &start_filter, start_filter_str, 1, 0) < 0) {
204 panic(13,"could not compile start filter: %s\n",pcap_geterr(listener));
207 dprintf(2,"compiled start filter %s\n",start_filter_str);
209 if (pcap_compile(listener, &stop_filter, stop_filter_str, 1, 0) < 0) {
210 panic(14,"could not compile stop filter: %s\n",pcap_geterr(listener));
213 dprintf(2,"compiled stop filter %s\n",stop_filter_str);
215 #ifdef HAVE_PCAP_OPEN
216 if ( ! ( capturer = pcap_open(interface, snaplen, promisc, 1, NULL, errbuf) )) {
218 if ( ! ( capturer = pcap_open_live(interface, snaplen, promisc, 1, errbuf) )) {
220 panic(15,"could not open interface '%s' for capturer: %s\n",interface, errbuf);
223 dprintf(1,"opened capturer (%s,%d,%d)\n",interface,snaplen, promisc);
225 if (capture_filter_str) {
226 if (pcap_compile(capturer, &capture_filter, capture_filter_str, 1, 0) < 0) {
227 panic(16,"could not compile capture filter: %s\n",pcap_geterr(capturer));
229 if (pcap_setfilter(capturer, &capture_filter) < 0) {
230 panic(17,"could not apply start filter to capturer: %s\n",pcap_geterr(capturer));
233 dprintf(2,"compiled and set capture filter (%s)\n",capture_filter_str);
236 if (pcap_setfilter(listener, &start_filter) < 0) {
237 panic(18,"could not apply start filter to listener: %s\n",pcap_geterr(listener));
239 dprintf(2,"set start filter on listener\n");
242 if (pcap_setnonblock(listener, 0, errbuf) < 0) {
243 panic(19,"could not set listener in blocking mode: %s\n",errbuf);
245 dprintf(2,"listener -> blocking\n");
247 if (pcap_setnonblock(capturer, 1, errbuf) < 0) {
248 panic(20,"could not set capturer in non blocking mode: %s\n",errbuf);
250 dprintf(2,"capturer -> non_blocking\n");
252 if (! (dumper = pcap_dump_open(listener,outfile)) ) {
253 panic(21,"open dumper file '%s': %s\n",outfile,pcap_geterr(listener));
255 dprintf(2,"opened dumper file '%s'\n",outfile);
257 signal(SIGINT, sig_int);
259 signal(SIGQUIT, sig_int);
262 signal(SIGTERM, sig_int);
265 signal(SIGSTOP, sig_int);
272 if (pcap_dispatch(listener, -1, listener_handler, NULL) < 0 ) {
273 panic(22,"pcap_dispatch(listener) failed: %s\n",pcap_geterr(listener));
276 if (pcap_dispatch(capturer, -1, capture_handler, (void*)dumper) < 0 ) {
277 panic(23,"pcap_dispatch(capturer) failed: %s\n",pcap_geterr(capturer));
282 printf("%d packets captured\n",captured);
285 dprintf(1,"done!\n");
287 pcap_dump_close(dumper);
288 pcap_close(listener);
289 pcap_close(capturer);
295 * Editor modelines - http://www.wireshark.org/tools/modelines.html
300 * indent-tabs-mode: t
303 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
304 * :indentSize=8:tabSize=8:noTabs=false: