From c42634dd820de5dbb8f80b5102d7ebd96c2ab41c Mon Sep 17 00:00:00 2001 From: Laurent Deniel Date: Wed, 28 Jul 1999 20:17:24 +0000 Subject: [PATCH] Fix the -S option : - read only the real number of packets that have been written by the child process. That's avoid incomplete packet read. - special timeout handling no more necessary and the whole real time capture and display behavior is much more satisfying with this patch. - wiretap modified to allow the reading of 'count' packets with wtap_loop. svn path=/trunk/; revision=398 --- capture.c | 16 ++++++++++++---- capture.h | 3 ++- file.c | 36 ++++++++++++++++++++++++++++++------ wiretap/wtap.c | 5 +++-- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/capture.c b/capture.c index 4e8f8b652a..776907108a 100644 --- a/capture.c +++ b/capture.c @@ -1,7 +1,7 @@ /* capture.c * Routines for packet capture windows * - * $Id: capture.c,v 1.36 1999/07/28 02:40:16 gerald Exp $ + * $Id: capture.c,v 1.37 1999/07/28 20:17:14 deniel Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -413,6 +413,7 @@ capture(void) { ld.max = cf.count; ld.linktype = DLT_NULL; ld.sync_time = 0; + ld.sync_packets = 0; ld.counts.tcp = 0; ld.counts.udp = 0; ld.counts.ospf = 0; @@ -614,14 +615,21 @@ capture_pcap_cb(u_char *user, const struct pcap_pkthdr *phdr, if (ld->pdh) pcap_dump((u_char *) ld->pdh, phdr, pd); cur_time = time(NULL); + + ld->sync_packets ++; + if (cur_time > *sync_time) { /* sync every second */ *sync_time = cur_time; fflush((FILE *)ld->pdh); - if (sync_mode) - write(1, "D", 1); + if (sync_mode && ld->sync_packets) { + char tmp[20]; + sprintf(tmp, "%d*", ld->sync_packets); + write(1, tmp, strlen(tmp)); + ld->sync_packets = 0; + } } - + switch (ld->linktype) { case DLT_EN10MB : capture_eth(pd, phdr->caplen, &ld->counts); diff --git a/capture.h b/capture.h index 43395bf067..1d828411e8 100644 --- a/capture.h +++ b/capture.h @@ -1,7 +1,7 @@ /* capture.h * Definitions for packet capture windows * - * $Id: capture.h,v 1.9 1999/07/20 06:16:09 guy Exp $ + * $Id: capture.h,v 1.10 1999/07/28 20:17:16 deniel Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -36,6 +36,7 @@ typedef struct _loop_data { gint go; gint max; gint linktype; + gint sync_packets; time_t sync_time; packet_counts counts; pcap_dumper_t *pdh; diff --git a/file.c b/file.c index f62b2c047f..7e1e320673 100644 --- a/file.c +++ b/file.c @@ -1,7 +1,7 @@ /* file.c * File I/O routines * - * $Id: file.c,v 1.49 1999/07/28 03:38:42 guy Exp $ + * $Id: file.c,v 1.50 1999/07/28 20:17:15 deniel Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs @@ -261,7 +261,10 @@ void cap_file_input_cb (gpointer data, gint source, GdkInputCondition condition) { capture_file *cf = (capture_file *)data; - char buffer[256]; + char buffer[256], *p = buffer, *q = buffer; + int nread; + int to_read = 0; + gboolean exit_loop = FALSE; /* avoid reentrancy problems and stack overflow */ gtk_input_remove(cap_input_id); @@ -283,7 +286,7 @@ cap_file_input_cb (gpointer data, gint source, GdkInputCondition condition) { and do it only if we have to? */ fseek(cf->wth->fh, 0, SEEK_CUR); - if (read(sync_pipe[0], buffer, 256) <= 0) { + if ((nread = read(sync_pipe[0], buffer, 256)) <= 0) { /* The child has closed the sync pipe, meaning it's not going to be capturing any more packets. Read what remains of the capture file, @@ -309,9 +312,28 @@ cap_file_input_cb (gpointer data, gint source, GdkInputCondition condition) { return; } - gtk_clist_freeze(GTK_CLIST(packet_list)); - wtap_loop(cf->wth, 0, wtap_dispatch_cb, (u_char *) cf); + buffer[nread] = '\0'; + while(!exit_loop) { + /* look for (possibly multiple) '*' */ + switch (*q) { + case '*' : + to_read += atoi(p); + p = q + 1; + q++; + break; + case '\0' : + /* XXX should handle the case of a pipe full (i.e. no star found) */ + exit_loop = TRUE; + break; + default : + q++; + break; + } + } + + gtk_clist_freeze(GTK_CLIST(packet_list)); + wtap_loop(cf->wth, to_read, wtap_dispatch_cb, (u_char *) cf); gtk_clist_thaw(GTK_CLIST(packet_list)); /* restore pipe handler */ @@ -322,9 +344,11 @@ cap_file_input_cb (gpointer data, gint source, GdkInputCondition condition) { (gpointer) cf, NULL); +#if 0 + /* XXX tail_timeout feature to be removed */ /* only useful in case of low amount of captured data */ tail_timeout_id = gtk_timeout_add(TAIL_TIMEOUT, tail_timeout_cb, (gpointer) cf); - +#endif } gint diff --git a/wiretap/wtap.c b/wiretap/wtap.c index 359049aa6a..c05b491217 100644 --- a/wiretap/wtap.c +++ b/wiretap/wtap.c @@ -1,6 +1,6 @@ /* wtap.c * - * $Id: wtap.c,v 1.10 1999/07/13 02:53:26 gram Exp $ + * $Id: wtap.c,v 1.11 1999/07/28 20:17:24 deniel Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -74,10 +74,11 @@ void wtap_close(wtap *wth) void wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user) { - int data_offset; + int data_offset, loop = 0; while ((data_offset = wth->subtype_read(wth)) > 0) { callback(user, &wth->phdr, data_offset, buffer_start_ptr(wth->frame_buffer)); + if (count > 0 && ++loop >= count) break; } }