Have "cf_merge_files()" take a pointer-to-pointer-to-char as the output
[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_IO_H
52 #include <io.h>
53 #endif
54
55 #ifdef HAVE_FCNTL_H
56 #include <fcntl.h>
57 #endif
58
59 #ifdef HAVE_SYS_STAT_H
60 #include <sys/stat.h>
61 #endif
62
63 #ifdef HAVE_UNISTD_H
64 #include <unistd.h>
65 #endif
66
67 #include <stdio.h>
68 #include <string.h>
69 #include <time.h>
70 #include <errno.h>
71
72 #ifdef NEED_SNPRINTF_H
73 #include "snprintf.h"
74 #endif
75
76 #include "wiretap/wtap.h"
77 #include "ringbuffer.h"
78
79 /* Win32 needs the O_BINARY flag for open() */
80 #ifndef O_BINARY
81 #define O_BINARY        0
82 #endif
83
84 /* Ringbuffer file structure */
85 typedef struct _rb_file {
86   gchar         *name;
87 } rb_file;
88
89 /* Ringbuffer data structure */
90 typedef struct _ringbuf_data {
91   rb_file      *files;
92   guint         num_files;           /* Number of ringbuffer files */
93   guint         curr_file_num;       /* Number of the current file */
94   gchar        *fprefix;             /* Filename prefix */
95   gchar        *fsuffix;             /* Filename suffix */
96   gboolean      unlimited;           /* TRUE if unlimited number of files */
97   int           filetype;
98   int           linktype;
99   int           snaplen;
100
101   int           fd;                  /* Current ringbuffer file descriptor */
102   wtap_dumper  *pdh;  
103 } ringbuf_data;
104
105 static ringbuf_data rb_data;
106
107
108 /*
109  * create the next filename and open a new binary file with that name 
110  */
111 static int ringbuf_open_file(rb_file *rfile, int *err)
112 {
113   char    filenum[5+1];
114   char    timestr[14+1];
115   time_t  current_time;
116
117   if (rfile->name != NULL) {
118     if (rb_data.unlimited == FALSE) {
119       /* remove old file (if any, so ignore error) */
120       unlink(rfile->name);
121     }
122     g_free(rfile->name);
123   }
124
125 #ifdef _WIN32
126   _tzset();
127 #endif
128   current_time = time(NULL);
129
130   snprintf(filenum, sizeof(filenum), "%05d", rb_data.curr_file_num + 1 /*.number*/);
131   strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
132   rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
133                             rb_data.fsuffix, NULL);
134
135   if (rfile->name == NULL) {
136     *err = ENOMEM;
137     return -1;
138   }
139
140   rb_data.fd = open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT, 0600);
141
142   if (rb_data.fd == -1 && err != NULL) {
143     *err = errno;
144   }
145
146   return rb_data.fd;
147 }
148
149 /*
150  * Initialize the ringbuffer data structures
151  */
152 int
153 ringbuf_init(const char *capfile_name, guint num_files)
154 {
155   unsigned int i;
156   char        *pfx, *last_pathsep;
157   gchar       *save_file;
158
159   rb_data.files = NULL;
160   rb_data.curr_file_num = 0;
161   rb_data.fprefix = NULL;
162   rb_data.fsuffix = NULL;
163   rb_data.unlimited = FALSE;
164   rb_data.fd = -1;
165   rb_data.pdh = NULL;
166
167   /* just to be sure ... */
168   if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
169     rb_data.num_files = num_files;
170   } else {
171     rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
172   }
173
174   /* Check file name */
175   if (capfile_name == NULL) {
176     /* ringbuffer does not work with temporary files! */
177     return -1;
178   }
179
180   /* set file name prefix/suffix */
181
182   save_file = g_strdup(capfile_name);
183   last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
184   pfx = strrchr(save_file,'.');
185   if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
186     /* The pathname has a "." in it, and it's in the last component
187        of the pathname (because there is either only one component,
188        i.e. last_pathsep is null as there are no path separators,
189        or the "." is after the path separator before the last
190        component.
191
192        Treat it as a separator between the rest of the file name and
193        the file name suffix, and arrange that the names given to the
194        ring buffer files have the specified suffix, i.e. put the
195        changing part of the name *before* the suffix. */
196     pfx[0] = '\0';
197     rb_data.fprefix = g_strdup(save_file);
198     pfx[0] = '.'; /* restore capfile_name */
199     rb_data.fsuffix = g_strdup(pfx);
200   } else {
201     /* Either there's no "." in the pathname, or it's in a directory
202        component, so the last component has no suffix. */
203     rb_data.fprefix = g_strdup(save_file);
204     rb_data.fsuffix = NULL;
205   }
206   g_free(save_file);
207   save_file = NULL;
208
209   /* allocate rb_file structures (only one if unlimited since there is no
210      need to save all file names in that case) */
211
212   if (num_files == RINGBUFFER_UNLIMITED_FILES) {
213     rb_data.unlimited = TRUE;
214     rb_data.num_files = 1;
215   }
216
217   rb_data.files = g_malloc(rb_data.num_files * sizeof(rb_file));
218   if (rb_data.files == NULL) {
219     return -1;
220   }
221
222   for (i=0; i < rb_data.num_files; i++) {
223     rb_data.files[i].name = NULL;
224   }
225
226   /* create the first file */
227   if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
228     ringbuf_error_cleanup();
229     return -1;
230   }
231
232   return rb_data.fd;
233 }
234
235 /*
236  * Calls wtap_dump_fdopen() for the current ringbuffer file
237  */
238 wtap_dumper*
239 ringbuf_init_wtap_dump_fdopen(int filetype, int linktype, int snaplen, int *err)
240 {
241
242   rb_data.filetype = filetype;
243   rb_data.linktype = linktype;
244   rb_data.snaplen  = snaplen;
245
246   rb_data.pdh = wtap_dump_fdopen(rb_data.fd, filetype, linktype, snaplen, err);
247
248   return rb_data.pdh;
249 }
250
251 /*
252  * Switches to the next ringbuffer file
253  */
254 gboolean
255 ringbuf_switch_file(wtap_dumper **pdh, gchar **save_file, int *save_file_fd, int *err)
256 {
257   int     next_file_num;
258   rb_file *next_rfile = NULL;
259
260   /* close current file */
261
262   if (!wtap_dump_close(rb_data.pdh, err)) {
263     close(rb_data.fd);  /* XXX - the above should have closed this already */
264     rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
265     rb_data.fd = -1;
266     return FALSE;
267   }
268
269   rb_data.pdh = NULL;
270   rb_data.fd  = -1;
271
272   /* get the next file number and open it */
273
274   next_file_num = (rb_data.curr_file_num + 1) % rb_data.num_files;  
275   next_rfile = &rb_data.files[next_file_num];
276
277   if (ringbuf_open_file(next_rfile, err) == -1) {
278     return FALSE;
279   }
280
281   if (ringbuf_init_wtap_dump_fdopen(rb_data.filetype, rb_data.linktype,
282                                     rb_data.snaplen, err) == NULL) {
283     return FALSE;
284   }
285
286   /* switch to the new file */
287   rb_data.curr_file_num = next_file_num;
288   *save_file = next_rfile->name;
289   *save_file_fd = rb_data.fd;
290   (*pdh) = rb_data.pdh;
291
292   return TRUE;
293 }
294
295 /*
296  * Calls wtap_dump_close() for the current ringbuffer file
297  */
298 gboolean
299 ringbuf_wtap_dump_close(gchar **save_file, int *err)
300 {
301   gboolean  ret_val = TRUE;
302
303   /* close current file, if it's open */
304   if (rb_data.pdh != NULL) {
305     if (!wtap_dump_close(rb_data.pdh, err)) {
306       close(rb_data.fd);
307       ret_val = FALSE;
308     }
309
310     rb_data.pdh = NULL;
311     rb_data.fd  = -1;
312   }
313
314   /* set the save file name to the current file */
315   *save_file = rb_data.files[rb_data.curr_file_num].name;
316   return ret_val;
317 }
318
319 /*
320  * Frees all memory allocated by the ringbuffer
321  */
322 void
323 ringbuf_free()
324 {
325   unsigned int i;
326
327   if (rb_data.files != NULL) {
328     for (i=0; i < rb_data.num_files; i++) {
329       if (rb_data.files[i].name != NULL) {
330         g_free(rb_data.files[i].name);
331         rb_data.files[i].name = NULL;
332       }
333     }
334     g_free(rb_data.files);
335     rb_data.files = NULL;
336   }
337   if (rb_data.fprefix != NULL) {
338     g_free(rb_data.fprefix);
339     rb_data.fprefix = NULL;
340   }
341   if (rb_data.fsuffix != NULL) {
342     g_free(rb_data.fsuffix);
343     rb_data.fsuffix = NULL;
344   }
345 }
346
347 /*
348  * Frees all memory allocated by the ringbuffer
349  */
350 void
351 ringbuf_error_cleanup(void)
352 {
353   unsigned int i;
354
355   /* try to close via wtap */
356   if (rb_data.pdh != NULL) {
357     if (wtap_dump_close(rb_data.pdh, NULL)) {
358       rb_data.fd = -1;
359     }
360     rb_data.pdh = NULL;
361   }
362
363   /* close directly if still open */
364   /* XXX - it shouldn't still be open; "wtap_dump_close()" should leave the
365      file closed even if it fails */
366   if (rb_data.fd != -1) {
367     close(rb_data.fd);
368     rb_data.fd = -1;
369   }
370
371   if (rb_data.files != NULL) {
372     for (i=0; i < rb_data.num_files; i++) {
373       if (rb_data.files[i].name != NULL) {
374         unlink(rb_data.files[i].name);
375       }
376     }
377   }
378   /* free the memory */
379   ringbuf_free();
380 }
381
382 #endif /* HAVE_LIBPCAP */