If we have pcap_open, call it instead of pcap_open_live, otherwise we might
[obnox/wireshark/wip.git] / ringbuffer.c
1 /* ringbuffer.c
2  * Routines for packet capture windows
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 /*
26  * <laurent.deniel@free.fr>
27  *
28  * Almost completely rewritten in order to:
29  * 
30  * - be able to use a unlimited number of ringbuffer files
31  * - close the current file and open (truncating) the next file at switch
32  * - set the final file name once open (or reopen)
33  * - avoid the deletion of files that could not be truncated (can't arise now)
34  *   and do not erase empty files
35  *
36  * The idea behind that is to remove the limitation of the maximum # of 
37  * ringbuffer files being less than the maximum # of open fd per process
38  * and to be able to reduce the amount of virtual memory usage (having only
39  * one file open at most) or the amount of file system usage (by truncating
40  * the files at switch and not the capture stop, and by closing them which 
41  * makes possible their move or deletion after a switch).
42  *
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #ifdef HAVE_LIBPCAP
50
51 #ifdef HAVE_FCNTL_H
52 #include <fcntl.h>
53 #endif
54
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58
59 #include <stdio.h>
60 #include <string.h>
61 #include <time.h>
62 #include <errno.h>
63
64 #include <pcap.h>
65
66 #include <glib.h>
67
68 #include "pcapio.h"
69 #include "ringbuffer.h"
70 #include "file_util.h"
71
72
73 /* Ringbuffer file structure */
74 typedef struct _rb_file {
75   gchar         *name;
76 } rb_file;
77
78 /* Ringbuffer data structure */
79 typedef struct _ringbuf_data {
80   rb_file      *files;
81   guint         num_files;           /* Number of ringbuffer files (1 to ...) */
82   guint         curr_file_num;       /* Number of the current file (ever increasing) */
83   gchar        *fprefix;             /* Filename prefix */
84   gchar        *fsuffix;             /* Filename suffix */
85   gboolean      unlimited;           /* TRUE if unlimited number of files */
86   int           linktype;
87   int           snaplen;
88
89   int           fd;                  /* Current ringbuffer file descriptor */
90   FILE         *pdh;
91 } ringbuf_data;
92
93 static ringbuf_data rb_data;
94
95
96 /*
97  * create the next filename and open a new binary file with that name 
98  */
99 static int ringbuf_open_file(rb_file *rfile, int *err)
100 {
101   char    filenum[5+1];
102   char    timestr[14+1];
103   time_t  current_time;
104
105   if (rfile->name != NULL) {
106     if (rb_data.unlimited == FALSE) {
107       /* remove old file (if any, so ignore error) */
108       eth_unlink(rfile->name);
109     }
110     g_free(rfile->name);
111   }
112
113 #ifdef _WIN32
114   _tzset();
115 #endif
116   current_time = time(NULL);
117
118   g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % 100000);
119   strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
120   rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
121                             rb_data.fsuffix, NULL);
122
123   if (rfile->name == NULL) {
124     *err = ENOMEM;
125     return -1;
126   }
127
128   rb_data.fd = eth_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT, 0600);
129
130   if (rb_data.fd == -1 && err != NULL) {
131     *err = errno;
132   }
133
134   return rb_data.fd;
135 }
136
137 /*
138  * Initialize the ringbuffer data structures
139  */
140 int
141 ringbuf_init(const char *capfile_name, guint num_files)
142 {
143   unsigned int i;
144   char        *pfx, *last_pathsep;
145   gchar       *save_file;
146
147   rb_data.files = NULL;
148   rb_data.curr_file_num = 0;
149   rb_data.fprefix = NULL;
150   rb_data.fsuffix = NULL;
151   rb_data.unlimited = FALSE;
152   rb_data.fd = -1;
153   rb_data.pdh = NULL;
154
155   /* just to be sure ... */
156   if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
157     rb_data.num_files = num_files;
158   } else {
159     rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
160   }
161
162   /* Check file name */
163   if (capfile_name == NULL) {
164     /* ringbuffer does not work with temporary files! */
165     return -1;
166   }
167
168   /* set file name prefix/suffix */
169
170   save_file = g_strdup(capfile_name);
171   last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
172   pfx = strrchr(save_file,'.');
173   if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
174     /* The pathname has a "." in it, and it's in the last component
175        of the pathname (because there is either only one component,
176        i.e. last_pathsep is null as there are no path separators,
177        or the "." is after the path separator before the last
178        component.
179
180        Treat it as a separator between the rest of the file name and
181        the file name suffix, and arrange that the names given to the
182        ring buffer files have the specified suffix, i.e. put the
183        changing part of the name *before* the suffix. */
184     pfx[0] = '\0';
185     rb_data.fprefix = g_strdup(save_file);
186     pfx[0] = '.'; /* restore capfile_name */
187     rb_data.fsuffix = g_strdup(pfx);
188   } else {
189     /* Either there's no "." in the pathname, or it's in a directory
190        component, so the last component has no suffix. */
191     rb_data.fprefix = g_strdup(save_file);
192     rb_data.fsuffix = NULL;
193   }
194   g_free(save_file);
195   save_file = NULL;
196
197   /* allocate rb_file structures (only one if unlimited since there is no
198      need to save all file names in that case) */
199
200   if (num_files == RINGBUFFER_UNLIMITED_FILES) {
201     rb_data.unlimited = TRUE;
202     rb_data.num_files = 1;
203   }
204
205   rb_data.files = g_malloc(rb_data.num_files * sizeof(rb_file));
206   if (rb_data.files == NULL) {
207     return -1;
208   }
209
210   for (i=0; i < rb_data.num_files; i++) {
211     rb_data.files[i].name = NULL;
212   }
213
214   /* create the first file */
215   if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
216     ringbuf_error_cleanup();
217     return -1;
218   }
219
220   return rb_data.fd;
221 }
222
223
224 const gchar *ringbuf_current_filename(void)
225 {
226   return rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
227 }
228
229 /*
230  * Calls libpcap_fdopen() for the current ringbuffer file
231  */
232 FILE *
233 ringbuf_init_libpcap_fdopen(int linktype, int snaplen,
234                             long *bytes_written, int *err)
235 {
236
237   rb_data.linktype = linktype;
238   rb_data.snaplen  = snaplen;
239
240   rb_data.pdh = libpcap_fdopen(rb_data.fd, linktype, snaplen, bytes_written,
241                                err);
242   return rb_data.pdh;
243 }
244
245 /*
246  * Switches to the next ringbuffer file
247  */
248 gboolean
249 ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, 
250                     long *bytes_written, int *err)
251 {
252   int     next_file_index;
253   rb_file *next_rfile = NULL;
254
255   /* close current file */
256
257   if (!libpcap_dump_close(rb_data.pdh, err)) {
258     eth_close(rb_data.fd);      /* XXX - the above should have closed this already */
259     rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
260     rb_data.fd = -1;
261     return FALSE;
262   }
263
264   rb_data.pdh = NULL;
265   rb_data.fd  = -1;
266
267   /* get the next file number and open it */
268
269   rb_data.curr_file_num++ /* = next_file_num*/;
270   next_file_index = (rb_data.curr_file_num) % rb_data.num_files;
271   next_rfile = &rb_data.files[next_file_index];
272
273   if (ringbuf_open_file(next_rfile, err) == -1) {
274     return FALSE;
275   }
276
277   if (ringbuf_init_libpcap_fdopen(rb_data.linktype, rb_data.snaplen,
278                                   bytes_written, err) == NULL) {
279     return FALSE;
280   }
281
282   /* switch to the new file */
283   *save_file = next_rfile->name;
284   *save_file_fd = rb_data.fd;
285   (*pdh) = rb_data.pdh;
286
287   return TRUE;
288 }
289
290 /*
291  * Calls libpcap_dump_close() for the current ringbuffer file
292  */
293 gboolean
294 ringbuf_libpcap_dump_close(gchar **save_file, int *err)
295 {
296   gboolean  ret_val = TRUE;
297
298   /* close current file, if it's open */
299   if (rb_data.pdh != NULL) {
300     if (!libpcap_dump_close(rb_data.pdh, err)) {
301       eth_close(rb_data.fd);
302       ret_val = FALSE;
303     }
304
305     rb_data.pdh = NULL;
306     rb_data.fd  = -1;
307   }
308
309   /* set the save file name to the current file */
310   *save_file = rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
311   return ret_val;
312 }
313
314 /*
315  * Frees all memory allocated by the ringbuffer
316  */
317 void
318 ringbuf_free()
319 {
320   unsigned int i;
321
322   if (rb_data.files != NULL) {
323     for (i=0; i < rb_data.num_files; i++) {
324       if (rb_data.files[i].name != NULL) {
325         g_free(rb_data.files[i].name);
326         rb_data.files[i].name = NULL;
327       }
328     }
329     g_free(rb_data.files);
330     rb_data.files = NULL;
331   }
332   if (rb_data.fprefix != NULL) {
333     g_free(rb_data.fprefix);
334     rb_data.fprefix = NULL;
335   }
336   if (rb_data.fsuffix != NULL) {
337     g_free(rb_data.fsuffix);
338     rb_data.fsuffix = NULL;
339   }
340 }
341
342 /*
343  * Frees all memory allocated by the ringbuffer
344  */
345 void
346 ringbuf_error_cleanup(void)
347 {
348   unsigned int i;
349
350   /* try to close via wtap */
351   if (rb_data.pdh != NULL) {
352     if (libpcap_dump_close(rb_data.pdh, NULL)) {
353       rb_data.fd = -1;
354     }
355     rb_data.pdh = NULL;
356   }
357
358   /* close directly if still open */
359   /* XXX - it shouldn't still be open; "libpcap_dump_close()" should leave the
360      file closed even if it fails */
361   if (rb_data.fd != -1) {
362     eth_close(rb_data.fd);
363     rb_data.fd = -1;
364   }
365
366   if (rb_data.files != NULL) {
367     for (i=0; i < rb_data.num_files; i++) {
368       if (rb_data.files[i].name != NULL) {
369         eth_unlink(rb_data.files[i].name);
370       }
371     }
372   }
373   /* free the memory */
374   ringbuf_free();
375 }
376
377 #endif /* HAVE_LIBPCAP */