From Hannes Gredler:
[obnox/wireshark/wip.git] / ringbuffer.c
1 /* ringbuffer.c
2  * Routines for packet capture windows
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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   long          bytes_written;       /* Bytes written to the current file */
92 } ringbuf_data;
93
94 static ringbuf_data rb_data;
95
96
97 /*
98  * create the next filename and open a new binary file with that name 
99  */
100 static int ringbuf_open_file(rb_file *rfile, int *err)
101 {
102   char    filenum[5+1];
103   char    timestr[14+1];
104   time_t  current_time;
105
106   if (rfile->name != NULL) {
107     if (rb_data.unlimited == FALSE) {
108       /* remove old file (if any, so ignore error) */
109       eth_unlink(rfile->name);
110     }
111     g_free(rfile->name);
112   }
113
114 #ifdef _WIN32
115   _tzset();
116 #endif
117   current_time = time(NULL);
118
119   g_snprintf(filenum, sizeof(filenum), "%05d", rb_data.curr_file_num + 1 /*.number*/);
120   strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
121   rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
122                             rb_data.fsuffix, NULL);
123
124   if (rfile->name == NULL) {
125     *err = ENOMEM;
126     return -1;
127   }
128
129   rb_data.fd = eth_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT, 0600);
130
131   if (rb_data.fd == -1 && err != NULL) {
132     *err = errno;
133   }
134
135   return rb_data.fd;
136 }
137
138 /*
139  * Initialize the ringbuffer data structures
140  */
141 int
142 ringbuf_init(const char *capfile_name, guint num_files)
143 {
144   unsigned int i;
145   char        *pfx, *last_pathsep;
146   gchar       *save_file;
147
148   rb_data.files = NULL;
149   rb_data.curr_file_num = 0;
150   rb_data.fprefix = NULL;
151   rb_data.fsuffix = NULL;
152   rb_data.unlimited = FALSE;
153   rb_data.fd = -1;
154   rb_data.pdh = NULL;
155
156   /* just to be sure ... */
157   if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
158     rb_data.num_files = num_files;
159   } else {
160     rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
161   }
162
163   /* Check file name */
164   if (capfile_name == NULL) {
165     /* ringbuffer does not work with temporary files! */
166     return -1;
167   }
168
169   /* set file name prefix/suffix */
170
171   save_file = g_strdup(capfile_name);
172   last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
173   pfx = strrchr(save_file,'.');
174   if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
175     /* The pathname has a "." in it, and it's in the last component
176        of the pathname (because there is either only one component,
177        i.e. last_pathsep is null as there are no path separators,
178        or the "." is after the path separator before the last
179        component.
180
181        Treat it as a separator between the rest of the file name and
182        the file name suffix, and arrange that the names given to the
183        ring buffer files have the specified suffix, i.e. put the
184        changing part of the name *before* the suffix. */
185     pfx[0] = '\0';
186     rb_data.fprefix = g_strdup(save_file);
187     pfx[0] = '.'; /* restore capfile_name */
188     rb_data.fsuffix = g_strdup(pfx);
189   } else {
190     /* Either there's no "." in the pathname, or it's in a directory
191        component, so the last component has no suffix. */
192     rb_data.fprefix = g_strdup(save_file);
193     rb_data.fsuffix = NULL;
194   }
195   g_free(save_file);
196   save_file = NULL;
197
198   /* allocate rb_file structures (only one if unlimited since there is no
199      need to save all file names in that case) */
200
201   if (num_files == RINGBUFFER_UNLIMITED_FILES) {
202     rb_data.unlimited = TRUE;
203     rb_data.num_files = 1;
204   }
205
206   rb_data.files = g_malloc(rb_data.num_files * sizeof(rb_file));
207   if (rb_data.files == NULL) {
208     return -1;
209   }
210
211   for (i=0; i < rb_data.num_files; i++) {
212     rb_data.files[i].name = NULL;
213   }
214
215   /* create the first file */
216   if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
217     ringbuf_error_cleanup();
218     return -1;
219   }
220
221   return rb_data.fd;
222 }
223
224
225 const gchar *ringbuf_current_filename(void)
226 {
227   return rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
228 }
229
230 /*
231  * Calls libpcap_fdopen() for the current ringbuffer file
232  */
233 FILE *
234 ringbuf_init_libpcap_fdopen(int linktype, int snaplen, 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,
241                                &rb_data.bytes_written, 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, int *err)
250 {
251   int     next_file_num;
252   rb_file *next_rfile = NULL;
253
254   /* close current file */
255
256   if (!libpcap_dump_close(rb_data.pdh, err)) {
257     eth_close(rb_data.fd);      /* XXX - the above should have closed this already */
258     rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
259     rb_data.fd = -1;
260     return FALSE;
261   }
262
263   rb_data.pdh = NULL;
264   rb_data.fd  = -1;
265
266   /* get the next file number and open it */
267
268   rb_data.curr_file_num++ /* = next_file_num*/;
269   next_file_num = (rb_data.curr_file_num) % rb_data.num_files;  
270   next_rfile = &rb_data.files[next_file_num];
271
272   if (ringbuf_open_file(next_rfile, err) == -1) {
273     return FALSE;
274   }
275
276   if (ringbuf_init_libpcap_fdopen(rb_data.linktype, rb_data.snaplen,
277                                   err) == NULL) {
278     return FALSE;
279   }
280
281   /* switch to the new file */
282   *save_file = next_rfile->name;
283   *save_file_fd = rb_data.fd;
284   (*pdh) = rb_data.pdh;
285
286   return TRUE;
287 }
288
289 /*
290  * Calls libpcap_dump_close() for the current ringbuffer file
291  */
292 gboolean
293 ringbuf_libpcap_dump_close(gchar **save_file, int *err)
294 {
295   gboolean  ret_val = TRUE;
296
297   /* close current file, if it's open */
298   if (rb_data.pdh != NULL) {
299     if (!libpcap_dump_close(rb_data.pdh, err)) {
300       eth_close(rb_data.fd);
301       ret_val = FALSE;
302     }
303
304     rb_data.pdh = NULL;
305     rb_data.fd  = -1;
306   }
307
308   /* set the save file name to the current file */
309   *save_file = rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
310   return ret_val;
311 }
312
313 /*
314  * Frees all memory allocated by the ringbuffer
315  */
316 void
317 ringbuf_free()
318 {
319   unsigned int i;
320
321   if (rb_data.files != NULL) {
322     for (i=0; i < rb_data.num_files; i++) {
323       if (rb_data.files[i].name != NULL) {
324         g_free(rb_data.files[i].name);
325         rb_data.files[i].name = NULL;
326       }
327     }
328     g_free(rb_data.files);
329     rb_data.files = NULL;
330   }
331   if (rb_data.fprefix != NULL) {
332     g_free(rb_data.fprefix);
333     rb_data.fprefix = NULL;
334   }
335   if (rb_data.fsuffix != NULL) {
336     g_free(rb_data.fsuffix);
337     rb_data.fsuffix = NULL;
338   }
339 }
340
341 /*
342  * Frees all memory allocated by the ringbuffer
343  */
344 void
345 ringbuf_error_cleanup(void)
346 {
347   unsigned int i;
348
349   /* try to close via wtap */
350   if (rb_data.pdh != NULL) {
351     if (libpcap_dump_close(rb_data.pdh, NULL)) {
352       rb_data.fd = -1;
353     }
354     rb_data.pdh = NULL;
355   }
356
357   /* close directly if still open */
358   /* XXX - it shouldn't still be open; "libpcap_dump_close()" should leave the
359      file closed even if it fails */
360   if (rb_data.fd != -1) {
361     eth_close(rb_data.fd);
362     rb_data.fd = -1;
363   }
364
365   if (rb_data.files != NULL) {
366     for (i=0; i < rb_data.num_files; i++) {
367       if (rb_data.files[i].name != NULL) {
368         eth_unlink(rb_data.files[i].name);
369       }
370     }
371   }
372   /* free the memory */
373   ringbuf_free();
374 }
375
376 #endif /* HAVE_LIBPCAP */