Ethereal-dev: Re: [Ethereal-dev] [PATCH] Flush tethereal capture file if FIFO

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Graeme Hewson <ghewson@xxxxxxxxxxxxxxxxxxx>
Date: Sun, 30 Jun 2002 19:11:13 +0100
Guy Harris said:

> It might be worth having "capture()", in Tethereal, if no capture file
> size stop condition is in effect, pass to "pcap_dispatch()" as the
> packet count argument a value of:
>
>         -1 if "packet_count" is 0;
>
>         "packet_count" if "packet_count" is non-zero;
>
> and move the flushing of the output stream into "capture()" out of
> "wtap_dispatch_cb_write()",

Done.  Also, the check after test_for_fifo() was broken.


Graeme Hewson
--- tethereal.c.orig	Fri Jun 28 23:23:54 2002
+++ tethereal.c	Sun Jun 30 18:09:37 2002
@@ -677,11 +677,10 @@
   ld.output_to_pipe = FALSE;
   if (cfile.save_file != NULL) {
     err = test_for_fifo(cfile.save_file);
-    switch (errno) {
+    switch (err) {
 
     case ENOENT:	/* it doesn't exist, so we'll be creating it,
     			   and it won't be a FIFO */
-    case ENOTDIR:	/* XXX - why ignore this? */
     case 0:		/* found it, but it's not a FIFO */
       break;
 
@@ -884,6 +883,7 @@
   void        (*oldhandler)(int);
   int         err = 0;
   volatile int inpkts = 0;
+  int         pcap_cnt;
   char        errmsg[1024+1];
   condition  *volatile cnd_stop_capturesize = NULL;
   condition  *volatile cnd_stop_timeout = NULL;
@@ -1044,7 +1044,15 @@
     ld.go = FALSE;
   ld.packet_count = 0;
   while (ld.go) {
-    inpkts = pcap_dispatch(ld.pch, 1, capture_pcap_cb, (u_char *) &ld);
+    if (cnd_stop_capturesize == NULL) {
+      if (capture_opts.autostop_count == 0)
+        pcap_cnt = -1;
+      else pcap_cnt = capture_opts.autostop_count - ld.packet_count;
+    } else {
+      /* We need to check the capture file size after each packet */
+      pcap_cnt = 1;
+    }
+    inpkts = pcap_dispatch(ld.pch, pcap_cnt, capture_pcap_cb, (u_char *) &ld);
     if (inpkts < 0) {
       /* Error from "pcap_dispatch()". */
       ld.go = FALSE;
@@ -1057,7 +1065,7 @@
     } else if (cnd_stop_timeout != NULL && cnd_eval(cnd_stop_timeout)) {
       /* The specified capture time has elapsed; stop the capture. */
       ld.go = FALSE;
-    } else if (ld.pdh != NULL && cnd_stop_capturesize != NULL &&
+    } else if (cnd_stop_capturesize != NULL &&
                   cnd_eval(cnd_stop_capturesize, 
                             (guint32)wtap_get_bytes_dumped(ld.pdh))) {
       /* We're saving the capture to a file, and the capture file reached
@@ -1077,6 +1085,12 @@
         ld.go = FALSE;
       }
     }
+    if (ld.output_to_pipe) {
+      if (fflush(wtap_dump_file(ld.pdh))) {
+        err = errno;
+        ld.go = FALSE;
+      }
+    }
   }
   
   /* delete stop conditions */
@@ -1419,7 +1433,6 @@
   wtap_dumper  *pdh = args->pdh;
   frame_data    fdata;
   int           err;
-  gboolean      io_ok;
   gboolean      passed;
   epan_dissect_t *edt;
 
@@ -1437,13 +1450,7 @@
   if (passed) {
     /* The packet passed the read filter. */
     ld.packet_count++;
-    io_ok = wtap_dump(pdh, phdr, pseudo_header, buf, &err);
-    if (io_ok && ld.output_to_pipe) {
-      io_ok = ! fflush(wtap_dump_file(ld.pdh));
-      if (!io_ok)
-        err = errno;
-    }
-    if (!io_ok) {
+    if (!wtap_dump(pdh, phdr, pseudo_header, buf, &err)) {
 #ifdef HAVE_LIBPCAP
       if (ld.pch != NULL && !quiet) {
       	/* We're capturing packets, so (if -q not specified) we're printing