Change "Found no match" to "No match found"
[obnox/wireshark/wip.git] / ringbuffer.c
index 90f8708f24624067d45ce1a7756efbbe4700a2cc..626b5de248630372b4c74b223e10ad0b68214704 100644 (file)
@@ -1,10 +1,10 @@
 /* ringbuffer.c
  * Routines for packet capture windows
  *
- * $Id: ringbuffer.c,v 1.7 2003/06/22 16:06:03 deniel Exp $
+ * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
 
 #ifdef HAVE_LIBPCAP
 
-#ifdef HAVE_IO_H
-#include <io.h>
-#endif
-
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
 
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #include <time.h>
 #include <errno.h>
 
-#ifdef NEED_SNPRINTF_H
-#include "snprintf.h"
-#endif
+#include <pcap.h>
+
+#include <glib.h>
 
-#include "wiretap/wtap.h"
+#include "pcapio.h"
 #include "ringbuffer.h"
+#include "file_util.h"
 
-/* Win32 needs the O_BINARY flag for open() */
-#ifndef O_BINARY
-#define O_BINARY       0
-#endif
 
 /* Ringbuffer file structure */
 typedef struct _rb_file {
@@ -89,21 +78,24 @@ typedef struct _rb_file {
 /* Ringbuffer data structure */
 typedef struct _ringbuf_data {
   rb_file      *files;
-  guint         num_files;           /* Number of ringbuffer files */
-  guint         curr_file_num;       /* Number of the current file */
+  guint         num_files;           /* Number of ringbuffer files (1 to ...) */
+  guint         curr_file_num;       /* Number of the current file (ever increasing) */
   gchar        *fprefix;             /* Filename prefix */
   gchar        *fsuffix;             /* Filename suffix */
   gboolean      unlimited;           /* TRUE if unlimited number of files */
-  int           filetype;
   int           linktype;
   int           snaplen;
-  guint16       number;
+
   int           fd;                 /* Current ringbuffer file descriptor */
-  wtap_dumper  *pdh;  
+  FILE         *pdh;
 } ringbuf_data;
 
 static ringbuf_data rb_data;
 
+
+/*
+ * create the next filename and open a new binary file with that name 
+ */
 static int ringbuf_open_file(rb_file *rfile, int *err)
 {
   char    filenum[5+1];
@@ -113,7 +105,7 @@ static int ringbuf_open_file(rb_file *rfile, int *err)
   if (rfile->name != NULL) {
     if (rb_data.unlimited == FALSE) {
       /* remove old file (if any, so ignore error) */
-      unlink(rfile->name);
+      eth_unlink(rfile->name);
     }
     g_free(rfile->name);
   }
@@ -123,9 +115,7 @@ static int ringbuf_open_file(rb_file *rfile, int *err)
 #endif
   current_time = time(NULL);
 
-  rb_data.number++;
-
-  snprintf(filenum, sizeof(filenum), "%05d", rb_data.number);
+  g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % 100000);
   strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
   rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
                            rb_data.fsuffix, NULL);
@@ -135,7 +125,7 @@ static int ringbuf_open_file(rb_file *rfile, int *err)
     return -1;
   }
 
-  rb_data.fd = open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT, 0600);
+  rb_data.fd = eth_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT, 0600);
 
   if (rb_data.fd == -1 && err != NULL) {
     *err = errno;
@@ -151,7 +141,7 @@ int
 ringbuf_init(const char *capfile_name, guint num_files)
 {
   unsigned int i;
-  char        *pfx;
+  char        *pfx, *last_pathsep;
   gchar       *save_file;
 
   rb_data.files = NULL;
@@ -161,7 +151,6 @@ ringbuf_init(const char *capfile_name, guint num_files)
   rb_data.unlimited = FALSE;
   rb_data.fd = -1;
   rb_data.pdh = NULL;
-  rb_data.number = 0;
 
   /* just to be sure ... */
   if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
@@ -179,13 +168,26 @@ ringbuf_init(const char *capfile_name, guint num_files)
   /* set file name prefix/suffix */
 
   save_file = g_strdup(capfile_name);
+  last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
   pfx = strrchr(save_file,'.');
-  if (pfx != NULL) {
+  if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
+    /* The pathname has a "." in it, and it's in the last component
+       of the pathname (because there is either only one component,
+       i.e. last_pathsep is null as there are no path separators,
+       or the "." is after the path separator before the last
+       component.
+
+       Treat it as a separator between the rest of the file name and
+       the file name suffix, and arrange that the names given to the
+       ring buffer files have the specified suffix, i.e. put the
+       changing part of the name *before* the suffix. */
     pfx[0] = '\0';
     rb_data.fprefix = g_strdup(save_file);
     pfx[0] = '.'; /* restore capfile_name */
     rb_data.fsuffix = g_strdup(pfx);
   } else {
+    /* Either there's no "." in the pathname, or it's in a directory
+       component, so the last component has no suffix. */
     rb_data.fprefix = g_strdup(save_file);
     rb_data.fsuffix = NULL;
   }
@@ -218,19 +220,25 @@ ringbuf_init(const char *capfile_name, guint num_files)
   return rb_data.fd;
 }
 
+
+const gchar *ringbuf_current_filename(void)
+{
+  return rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
+}
+
 /*
- * Calls wtap_dump_fdopen() for the current ringbuffer file
+ * Calls libpcap_fdopen() for the current ringbuffer file
  */
-wtap_dumper*
-ringbuf_init_wtap_dump_fdopen(int filetype, int linktype, int snaplen, int *err)
+FILE *
+ringbuf_init_libpcap_fdopen(int linktype, int snaplen,
+                            long *bytes_written, int *err)
 {
 
-  rb_data.filetype = filetype;
   rb_data.linktype = linktype;
   rb_data.snaplen  = snaplen;
 
-  rb_data.pdh = wtap_dump_fdopen(rb_data.fd, filetype, linktype, snaplen, err);
-
+  rb_data.pdh = libpcap_fdopen(rb_data.fd, linktype, snaplen, bytes_written,
+                               err);
   return rb_data.pdh;
 }
 
@@ -238,15 +246,17 @@ ringbuf_init_wtap_dump_fdopen(int filetype, int linktype, int snaplen, int *err)
  * Switches to the next ringbuffer file
  */
 gboolean
-ringbuf_switch_file(capture_file *cf, wtap_dumper **pdh, int *err)
+ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, 
+                    long *bytes_written, int *err)
 {
-  int     next_file_num;
+  int     next_file_index;
   rb_file *next_rfile = NULL;
 
   /* close current file */
 
-  if (!wtap_dump_close(rb_data.pdh, err)) {
-    close(rb_data.fd);
+  if (!libpcap_dump_close(rb_data.pdh, err)) {
+    eth_close(rb_data.fd);     /* XXX - the above should have closed this already */
+    rb_data.pdh = NULL;        /* it's still closed, we just got an error while closing */
     rb_data.fd = -1;
     return FALSE;
   }
@@ -256,46 +266,48 @@ ringbuf_switch_file(capture_file *cf, wtap_dumper **pdh, int *err)
 
   /* get the next file number and open it */
 
-  next_file_num = (rb_data.curr_file_num + 1) % rb_data.num_files;  
-  next_rfile = &rb_data.files[next_file_num];
+  rb_data.curr_file_num++ /* = next_file_num*/;
+  next_file_index = (rb_data.curr_file_num) % rb_data.num_files;
+  next_rfile = &rb_data.files[next_file_index];
 
   if (ringbuf_open_file(next_rfile, err) == -1) {
     return FALSE;
   }
 
-  if (ringbuf_init_wtap_dump_fdopen(rb_data.filetype, rb_data.linktype,
-                                   rb_data.snaplen, err) == NULL) {
+  if (ringbuf_init_libpcap_fdopen(rb_data.linktype, rb_data.snaplen,
+                                  bytes_written, err) == NULL) {
     return FALSE;
   }
 
   /* switch to the new file */
-  rb_data.curr_file_num = next_file_num;
-  cf->save_file = next_rfile->name;
-  cf->save_file_fd = rb_data.fd;
+  *save_file = next_rfile->name;
+  *save_file_fd = rb_data.fd;
   (*pdh) = rb_data.pdh;
 
   return TRUE;
 }
 
 /*
- * Calls wtap_dump_close() for the current ringbuffer file
+ * Calls libpcap_dump_close() for the current ringbuffer file
  */
 gboolean
-ringbuf_wtap_dump_close(capture_file *cf, int *err)
+ringbuf_libpcap_dump_close(gchar **save_file, int *err)
 {
   gboolean  ret_val = TRUE;
 
-  /* close current file */
+  /* close current file, if it's open */
+  if (rb_data.pdh != NULL) {
+    if (!libpcap_dump_close(rb_data.pdh, err)) {
+      eth_close(rb_data.fd);
+      ret_val = FALSE;
+    }
 
-  if (!wtap_dump_close(rb_data.pdh, err)) {
-    close(rb_data.fd);
-    ret_val = FALSE;
+    rb_data.pdh = NULL;
+    rb_data.fd  = -1;
   }
 
-  rb_data.pdh = NULL;
-  rb_data.fd  = -1;
-
-  cf->save_file = rb_data.files[rb_data.curr_file_num].name;
+  /* set the save file name to the current file */
+  *save_file = rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
   return ret_val;
 }
 
@@ -337,21 +349,25 @@ ringbuf_error_cleanup(void)
 
   /* try to close via wtap */
   if (rb_data.pdh != NULL) {
-    if (wtap_dump_close(rb_data.pdh, NULL)) {
+    if (libpcap_dump_close(rb_data.pdh, NULL)) {
       rb_data.fd = -1;
     }
     rb_data.pdh = NULL;
   }
 
   /* close directly if still open */
+  /* XXX - it shouldn't still be open; "libpcap_dump_close()" should leave the
+     file closed even if it fails */
   if (rb_data.fd != -1) {
-    close(rb_data.fd);
+    eth_close(rb_data.fd);
     rb_data.fd = -1;
   }
 
-  for (i=0; i < rb_data.num_files; i++) {
-    if (rb_data.files[i].name != NULL) {
-      unlink(rb_data.files[i].name);
+  if (rb_data.files != NULL) {
+    for (i=0; i < rb_data.num_files; i++) {
+      if (rb_data.files[i].name != NULL) {
+        eth_unlink(rb_data.files[i].name);
+      }
     }
   }
   /* free the memory */