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