forked from osmocom/wireshark
If we have pcap_open, call it instead of pcap_open_live, otherwise we might
crash. The changes to trigcap.c haven't been tested, but _should_ work. svn path=/trunk/; revision=25279
This commit is contained in:
parent
27ccd63ce7
commit
20186d4947
|
@ -153,7 +153,7 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
|
||||||
g_log(log_domain, log_level, "Filter : %s", capture_opts->cfilter);
|
g_log(log_domain, log_level, "Filter : %s", capture_opts->cfilter);
|
||||||
g_log(log_domain, log_level, "Interface : %s", capture_opts->iface);
|
g_log(log_domain, log_level, "Interface : %s", capture_opts->iface);
|
||||||
/* iface_descr may not been filled in and some C Libraries hate a null ptr for %s */
|
/* iface_descr may not been filled in and some C Libraries hate a null ptr for %s */
|
||||||
g_log(log_domain, log_level, "Interface Descr : %s",
|
g_log(log_domain, log_level, "Interface Descr : %s",
|
||||||
capture_opts->iface_descr ? capture_opts->iface_descr : "<null>");
|
capture_opts->iface_descr ? capture_opts->iface_descr : "<null>");
|
||||||
#ifdef HAVE_PCAP_REMOTE
|
#ifdef HAVE_PCAP_REMOTE
|
||||||
g_log(log_domain, log_level, "Capture source : %s",
|
g_log(log_domain, log_level, "Capture source : %s",
|
||||||
|
@ -735,7 +735,11 @@ capture_opts_print_statistics(gboolean machine_readable)
|
||||||
|
|
||||||
for (if_entry = g_list_first(if_list); if_entry != NULL; if_entry = g_list_next(if_entry)) {
|
for (if_entry = g_list_first(if_list); if_entry != NULL; if_entry = g_list_next(if_entry)) {
|
||||||
if_info = if_entry->data;
|
if_info = if_entry->data;
|
||||||
|
#ifdef HAVE_PCAP_OPEN
|
||||||
|
pch = pcap_open(if_info->name, MIN_PACKET_SIZE, 0, 0, NULL, errbuf);
|
||||||
|
#else
|
||||||
pch = pcap_open_live(if_info->name, MIN_PACKET_SIZE, 0, 0, errbuf);
|
pch = pcap_open_live(if_info->name, MIN_PACKET_SIZE, 0, 0, errbuf);
|
||||||
|
#endif
|
||||||
|
|
||||||
if (pch) {
|
if (pch) {
|
||||||
if_stat = g_malloc(sizeof(if_stat_t));
|
if_stat = g_malloc(sizeof(if_stat_t));
|
||||||
|
|
58
trigcap.c
58
trigcap.c
|
@ -48,7 +48,7 @@ static void panic(int err, const char* fmt, ...) {
|
||||||
|
|
||||||
static void dprintf(int lev, const char* fmt, ...) {
|
static void dprintf(int lev, const char* fmt, ...) {
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap,fmt);
|
va_start(ap,fmt);
|
||||||
if (lev <= debug_level) {
|
if (lev <= debug_level) {
|
||||||
vfprintf(stderr,fmt,ap);
|
vfprintf(stderr,fmt,ap);
|
||||||
|
@ -71,13 +71,13 @@ static void usage(int err) {
|
||||||
" -d increase deug level\n"
|
" -d increase deug level\n"
|
||||||
" -h prints this message\n"
|
" -h prints this message\n"
|
||||||
;
|
;
|
||||||
|
|
||||||
panic(err,usage_str);
|
panic(err,usage_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void listener_handler(u_char* u, const struct pcap_pkthdr * ph, const u_char* buf) {
|
static void listener_handler(u_char* u, const struct pcap_pkthdr * ph, const u_char* buf) {
|
||||||
char errbuf[PCAP_ERRBUF_SIZE];
|
char errbuf[PCAP_ERRBUF_SIZE];
|
||||||
|
|
||||||
dprintf(2,"listener handler invoked dumping=%d\n",dumping);
|
dprintf(2,"listener handler invoked dumping=%d\n",dumping);
|
||||||
|
|
||||||
if (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");
|
dprintf(2,"apply stop filter to listener\n");
|
||||||
|
|
||||||
if (pcap_setnonblock(listener, 1, errbuf) < 0) {
|
if (pcap_setnonblock(listener, 1, errbuf) < 0) {
|
||||||
panic(24,"could not set listener in non blocking mode: %s\n",errbuf);
|
panic(24,"could not set listener in non blocking mode: %s\n",errbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
dprintf(2,"listener -> non_blocking\n");
|
dprintf(2,"listener -> non_blocking\n");
|
||||||
dumping = 1;
|
dumping = 1;
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ int main(int argc, char** argv) {
|
||||||
pcap_t* capturer = NULL;
|
pcap_t* capturer = NULL;
|
||||||
pcap_dumper_t* dumper = NULL;
|
pcap_dumper_t* dumper = NULL;
|
||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "i:w:s:b:e:f:phdq")) != -1) {
|
while ((opt = getopt(argc, argv, "i:w:s:b:e:f:phdq")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'i':
|
case 'i':
|
||||||
|
@ -169,7 +169,7 @@ int main(int argc, char** argv) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dprintf(1,"starting with:\n interface: %s\n snaplen: %d\n promisc: %d"
|
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",
|
"\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 choosen",
|
||||||
|
@ -180,19 +180,23 @@ int main(int argc, char** argv) {
|
||||||
start_filter_str ? start_filter_str : "** missing **",
|
start_filter_str ? start_filter_str : "** missing **",
|
||||||
stop_filter_str ? stop_filter_str : "** missing **",
|
stop_filter_str ? stop_filter_str : "** missing **",
|
||||||
debug_level);
|
debug_level);
|
||||||
|
|
||||||
if (! ( start_filter_str && stop_filter_str && outfile ) ) {
|
if (! ( start_filter_str && stop_filter_str && outfile ) ) {
|
||||||
usage(10);
|
usage(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! interface) {
|
if (! interface) {
|
||||||
interface = pcap_lookupdev(errbuf);
|
interface = pcap_lookupdev(errbuf);
|
||||||
if (!interface) {
|
if (!interface) {
|
||||||
panic(11, "could not obtain an interface: %s\n",errbuf);
|
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);
|
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) {
|
if (pcap_compile(listener, &stop_filter, stop_filter_str, 1, 0) < 0) {
|
||||||
panic(14,"could not compile stop filter: %s\n",pcap_geterr(listener));
|
panic(14,"could not compile stop filter: %s\n",pcap_geterr(listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
dprintf(2,"compiled stop filter %s\n",stop_filter_str);
|
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) )) {
|
if ( ! ( capturer = pcap_open_live(interface, snaplen, promisc, 1, errbuf) )) {
|
||||||
|
#endif
|
||||||
panic(15,"could not open interface '%s' for capturer: %s\n",interface, errbuf);
|
panic(15,"could not open interface '%s' for capturer: %s\n",interface, errbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
dprintf(1,"opened capturer (%s,%d,%d)\n",interface,snaplen, promisc);
|
dprintf(1,"opened capturer (%s,%d,%d)\n",interface,snaplen, promisc);
|
||||||
|
|
||||||
if (capture_filter_str) {
|
if (capture_filter_str) {
|
||||||
|
@ -223,7 +231,7 @@ int main(int argc, char** argv) {
|
||||||
if (pcap_setfilter(capturer, &capture_filter) < 0) {
|
if (pcap_setfilter(capturer, &capture_filter) < 0) {
|
||||||
panic(17,"could not apply start filter to capturer: %s\n",pcap_geterr(capturer));
|
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);
|
dprintf(2,"compiled and set capture filter (%s)\n",capture_filter_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -232,22 +240,22 @@ int main(int argc, char** argv) {
|
||||||
}
|
}
|
||||||
dprintf(2,"set start filter on listener\n");
|
dprintf(2,"set start filter on listener\n");
|
||||||
|
|
||||||
|
|
||||||
if (pcap_setnonblock(listener, 0, errbuf) < 0) {
|
if (pcap_setnonblock(listener, 0, errbuf) < 0) {
|
||||||
panic(19,"could not set listener in blocking mode: %s\n",errbuf);
|
panic(19,"could not set listener in blocking mode: %s\n",errbuf);
|
||||||
}
|
}
|
||||||
dprintf(2,"listener -> blocking\n");
|
dprintf(2,"listener -> blocking\n");
|
||||||
|
|
||||||
if (pcap_setnonblock(capturer, 1, errbuf) < 0) {
|
if (pcap_setnonblock(capturer, 1, errbuf) < 0) {
|
||||||
panic(20,"could not set capturer in non blocking mode: %s\n",errbuf);
|
panic(20,"could not set capturer in non blocking mode: %s\n",errbuf);
|
||||||
}
|
}
|
||||||
dprintf(2,"capturer -> non_blocking\n");
|
dprintf(2,"capturer -> non_blocking\n");
|
||||||
|
|
||||||
if (! (dumper = pcap_dump_open(listener,outfile)) ) {
|
if (! (dumper = pcap_dump_open(listener,outfile)) ) {
|
||||||
panic(21,"open dumper file '%s': %s\n",outfile,pcap_geterr(listener));
|
panic(21,"open dumper file '%s': %s\n",outfile,pcap_geterr(listener));
|
||||||
}
|
}
|
||||||
dprintf(2,"opened dumper file '%s'\n",outfile);
|
dprintf(2,"opened dumper file '%s'\n",outfile);
|
||||||
|
|
||||||
signal(SIGINT, sig_int);
|
signal(SIGINT, sig_int);
|
||||||
#ifdef SIGQUIT
|
#ifdef SIGQUIT
|
||||||
signal(SIGQUIT, sig_int);
|
signal(SIGQUIT, sig_int);
|
||||||
|
@ -258,29 +266,29 @@ int main(int argc, char** argv) {
|
||||||
#ifdef SIGSTOP
|
#ifdef SIGSTOP
|
||||||
signal(SIGSTOP, sig_int);
|
signal(SIGSTOP, sig_int);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
keep_going = 1;
|
keep_going = 1;
|
||||||
dumping = 0;
|
dumping = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (pcap_dispatch(listener, -1, listener_handler, NULL) < 0 ) {
|
if (pcap_dispatch(listener, -1, listener_handler, NULL) < 0 ) {
|
||||||
panic(22,"pcap_dispatch(listener) failed: %s\n",pcap_geterr(listener));
|
panic(22,"pcap_dispatch(listener) failed: %s\n",pcap_geterr(listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pcap_dispatch(capturer, -1, capture_handler, (void*)dumper) < 0 ) {
|
if (pcap_dispatch(capturer, -1, capture_handler, (void*)dumper) < 0 ) {
|
||||||
panic(23,"pcap_dispatch(capturer) failed: %s\n",pcap_geterr(capturer));
|
panic(23,"pcap_dispatch(capturer) failed: %s\n",pcap_geterr(capturer));
|
||||||
}
|
}
|
||||||
} while(keep_going);
|
} while(keep_going);
|
||||||
|
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
printf("%d packets captured\n",captured);
|
printf("%d packets captured\n",captured);
|
||||||
}
|
}
|
||||||
|
|
||||||
dprintf(1,"done!\n");
|
dprintf(1,"done!\n");
|
||||||
|
|
||||||
pcap_dump_close(dumper);
|
pcap_dump_close(dumper);
|
||||||
pcap_close(listener);
|
pcap_close(listener);
|
||||||
pcap_close(capturer);
|
pcap_close(capturer);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue