Remove an extraneous backslash
[obnox/wireshark/wip.git] / trigcap.c
index 37ce60a..394c50a 100644 (file)
--- a/trigcap.c
+++ b/trigcap.c
@@ -4,7 +4,7 @@
  *
  * $Id$
  *
- * (c) 2007, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
+ * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -48,13 +48,13 @@ static void panic(int err, const char* fmt, ...) {
 
 static void dprintf(int lev, const char* fmt, ...) {
        va_list ap;
-       
-       va_start(ap,fmt);
+
        if (lev <= debug_level) {
+               va_start(ap,fmt);
                vfprintf(stderr,fmt,ap);
+               va_end(ap);
                fflush(stderr);
        }
-       va_end(ap);
 }
 
 
@@ -68,16 +68,16 @@ static void usage(int err) {
        "   -p promiscuous mode\n"
        "   -s snapshot length\n"
        "   -q quiet\n"
-       "   -d increase deug level\n"
+       "   -d increase debug level\n"
        "   -h prints this message\n"
        ;
-       
+
        panic(err,usage_str);
 }
 
 static void listener_handler(u_char* u, const struct pcap_pkthdr * ph, const u_char* buf) {
        char errbuf[PCAP_ERRBUF_SIZE];
-       
+
        dprintf(2,"listener handler invoked dumping=%d\n",dumping);
 
        if (dumping) {
@@ -89,11 +89,11 @@ static void listener_handler(u_char* u, const struct pcap_pkthdr * ph, const u_c
                }
 
                dprintf(2,"apply stop filter to listener\n");
-               
+
                if (pcap_setnonblock(listener, 1, errbuf) < 0) {
                        panic(24,"could not set listener in non blocking mode: %s\n",errbuf);
                }
-               
+
                dprintf(2,"listener -> non_blocking\n");
                dumping = 1;
        }
@@ -126,16 +126,16 @@ int main(int argc, char** argv) {
        pcap_t* capturer = NULL;
        pcap_dumper_t* dumper = NULL;
        int opt;
-       
+
        while ((opt = getopt(argc, argv, "i:w:s:b:e:f:phdq")) != -1) {
                switch (opt) {
                        case 'i':
                                if (interface) panic(1,"interface already given");
-                               interface = strdup(optarg);
+                               interface = g_strdup(optarg);
                                break;
                        case 'w':
                                if (outfile) panic(3,"output file already given");
-                               outfile = strdup(optarg);
+                               outfile = g_strdup(optarg);
                                break;
                        case 's':
                                snaplen = strtoul(optarg,NULL,10);
@@ -144,15 +144,15 @@ int main(int argc, char** argv) {
                                break;
                        case 'b':
                                if (start_filter_str) panic(5,"start filter already given");
-                               start_filter_str = strdup(optarg);
+                               start_filter_str = g_strdup(optarg);
                                break;
                        case 'e':
                                if (stop_filter_str) panic(6,"stop filter already given");
-                               stop_filter_str = strdup(optarg);
+                               stop_filter_str = g_strdup(optarg);
                                break;
                        case 'f':
                                if (capture_filter_str) panic(7,"capture filter already given");
-                               capture_filter_str = strdup(optarg);
+                               capture_filter_str = g_strdup(optarg);
                                break;
                        case 'p':
                                promisc = 1;
@@ -169,10 +169,10 @@ int main(int argc, char** argv) {
                                break;
                        }
                }
-       
+
        dprintf(1,"starting with:\n interface: %s\n snaplen: %d\n promisc: %d"
                        "\n outfile: %s\n capture filter: %s\n start: %s\n stop: %s\n debug level: %d\n",
-                       interface ? interface : "to be choosen",
+                       interface ? interface : "to be chosen",
                        snaplen,
                        promisc,
                        outfile ? outfile : "** missing **",
@@ -180,19 +180,23 @@ int main(int argc, char** argv) {
                        start_filter_str ? start_filter_str : "** missing **",
                        stop_filter_str ? stop_filter_str : "** missing **",
                        debug_level);
-       
+
        if (! ( start_filter_str && stop_filter_str && outfile ) ) {
                usage(10);
        }
-       
+
        if (! interface) {
                interface = pcap_lookupdev(errbuf);
                if (!interface) {
                        panic(11, "could not obtain an interface: %s\n",errbuf);
                }
        }
-       
-       if ( ! ( listener = pcap_open_live(interface, snaplen, promisc, 1, errbuf) )) {
+
+#ifdef HAVE_PCAP_OPEN
+       if ( ! ( capturer = pcap_open(interface, snaplen, promisc, 1, NULL, errbuf) )) {
+#else
+       if ( ! ( capturer = pcap_open_live(interface, snaplen, promisc, 1, errbuf) )) {
+#endif
                panic(12,"could not open interface '%s' for listener: %s\n",interface,errbuf);
        }
 
@@ -206,14 +210,18 @@ int main(int argc, char** argv) {
 
        if (pcap_compile(listener, &stop_filter, stop_filter_str, 1, 0) < 0) {
                panic(14,"could not compile stop filter: %s\n",pcap_geterr(listener));
-       }       
+       }
 
        dprintf(2,"compiled stop filter %s\n",stop_filter_str);
 
+#ifdef HAVE_PCAP_OPEN
+       if ( ! ( capturer = pcap_open(interface, snaplen, promisc, 1, NULL, errbuf) )) {
+#else
        if ( ! ( capturer = pcap_open_live(interface, snaplen, promisc, 1, errbuf) )) {
+#endif
                panic(15,"could not open interface '%s' for capturer: %s\n",interface, errbuf);
        }
-       
+
        dprintf(1,"opened capturer (%s,%d,%d)\n",interface,snaplen, promisc);
 
        if (capture_filter_str) {
@@ -223,7 +231,7 @@ int main(int argc, char** argv) {
                if (pcap_setfilter(capturer, &capture_filter) < 0) {
                        panic(17,"could not apply start filter to capturer: %s\n",pcap_geterr(capturer));
                }
-               
+
                dprintf(2,"compiled and set capture filter (%s)\n",capture_filter_str);
        }
 
@@ -232,49 +240,55 @@ int main(int argc, char** argv) {
        }
        dprintf(2,"set start filter on listener\n");
 
-               
+
        if (pcap_setnonblock(listener, 0, errbuf) < 0) {
                panic(19,"could not set listener in blocking mode: %s\n",errbuf);
        }
        dprintf(2,"listener -> blocking\n");
-       
+
        if (pcap_setnonblock(capturer, 1, errbuf) < 0) {
                panic(20,"could not set capturer in non blocking mode: %s\n",errbuf);
        }
        dprintf(2,"capturer -> non_blocking\n");
-       
+
        if (! (dumper = pcap_dump_open(listener,outfile)) ) {
                panic(21,"open dumper file '%s': %s\n",outfile,pcap_geterr(listener));
        }
        dprintf(2,"opened dumper file '%s'\n",outfile);
-       
+
        signal(SIGINT, sig_int);
+#ifdef SIGQUIT
        signal(SIGQUIT, sig_int);
+#endif
+#ifdef SIGTERM
        signal(SIGTERM, sig_int);
+#endif
+#ifdef SIGSTOP
        signal(SIGSTOP, sig_int);
-       
+#endif
+
        keep_going = 1;
        dumping = 0;
-       
+
        do {
                if (pcap_dispatch(listener, -1, listener_handler, NULL) < 0 ) {
                        panic(22,"pcap_dispatch(listener) failed: %s\n",pcap_geterr(listener));
                }
-               
+
                if (pcap_dispatch(capturer, -1, capture_handler, (void*)dumper) < 0 ) {
                        panic(23,"pcap_dispatch(capturer) failed: %s\n",pcap_geterr(capturer));
                }
        } while(keep_going);
-       
+
        if (!quiet) {
                printf("%d packets captured\n",captured);
        }
-       
+
        dprintf(1,"done!\n");
-       
+
        pcap_dump_close(dumper);
        pcap_close(listener);
        pcap_close(capturer);
-       
+
        return 0;
 }