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