2 * Routines for packet capture windows
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
26 * <laurent.deniel@free.fr>
28 * Almost completely rewritten in order to:
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
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).
69 #include "ringbuffer.h"
70 #include <wsutil/file_util.h>
73 /* Ringbuffer file structure */
74 typedef struct _rb_file {
78 /* Ringbuffer data structure */
79 typedef struct _ringbuf_data {
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 */
87 int fd; /* Current ringbuffer file descriptor */
89 gboolean group_read_access; /* TRUE if files need to be opened with group read access */
92 static ringbuf_data rb_data;
96 * create the next filename and open a new binary file with that name
98 static int ringbuf_open_file(rb_file *rfile, int *err)
104 if (rfile->name != NULL) {
105 if (rb_data.unlimited == FALSE) {
106 /* remove old file (if any, so ignore error) */
107 ws_unlink(rfile->name);
115 current_time = time(NULL);
117 g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % RINGBUFFER_MAX_NUM_FILES);
118 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
119 rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
120 rb_data.fsuffix, NULL);
122 if (rfile->name == NULL) {
127 rb_data.fd = ws_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
128 rb_data.group_read_access ? 0640 : 0600);
130 if (rb_data.fd == -1 && err != NULL) {
138 * Initialize the ringbuffer data structures
141 ringbuf_init(const char *capfile_name, guint num_files, gboolean group_read_access)
144 char *pfx, *last_pathsep;
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;
154 rb_data.group_read_access = group_read_access;
156 /* just to be sure ... */
157 if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
158 rb_data.num_files = num_files;
160 rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
163 /* Check file name */
164 if (capfile_name == NULL) {
165 /* ringbuffer does not work with temporary files! */
169 /* set file name prefix/suffix */
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
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. */
186 rb_data.fprefix = g_strdup(save_file);
187 pfx[0] = '.'; /* restore capfile_name */
188 rb_data.fsuffix = g_strdup(pfx);
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;
198 /* allocate rb_file structures (only one if unlimited since there is no
199 need to save all file names in that case) */
201 if (num_files == RINGBUFFER_UNLIMITED_FILES) {
202 rb_data.unlimited = TRUE;
203 rb_data.num_files = 1;
206 rb_data.files = (rb_file *)g_malloc(rb_data.num_files * sizeof(rb_file));
207 if (rb_data.files == NULL) {
211 for (i=0; i < rb_data.num_files; i++) {
212 rb_data.files[i].name = NULL;
215 /* create the first file */
216 if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
217 ringbuf_error_cleanup();
225 const gchar *ringbuf_current_filename(void)
227 return rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
231 * Calls libpcap_fdopen() for the current ringbuffer file
234 ringbuf_init_libpcap_fdopen(int *err)
236 rb_data.pdh = libpcap_fdopen(rb_data.fd, err);
241 * Switches to the next ringbuffer file
244 ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
247 rb_file *next_rfile = NULL;
249 /* close current file */
251 if (!libpcap_dump_close(rb_data.pdh, err)) {
252 ws_close(rb_data.fd); /* XXX - the above should have closed this already */
253 rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
261 /* get the next file number and open it */
263 rb_data.curr_file_num++ /* = next_file_num*/;
264 next_file_index = (rb_data.curr_file_num) % rb_data.num_files;
265 next_rfile = &rb_data.files[next_file_index];
267 if (ringbuf_open_file(next_rfile, err) == -1) {
271 if (ringbuf_init_libpcap_fdopen(err) == NULL) {
275 /* switch to the new file */
276 *save_file = next_rfile->name;
277 *save_file_fd = rb_data.fd;
278 (*pdh) = rb_data.pdh;
284 * Calls libpcap_dump_close() for the current ringbuffer file
287 ringbuf_libpcap_dump_close(gchar **save_file, int *err)
289 gboolean ret_val = TRUE;
291 /* close current file, if it's open */
292 if (rb_data.pdh != NULL) {
293 if (!libpcap_dump_close(rb_data.pdh, err)) {
294 ws_close(rb_data.fd);
302 /* set the save file name to the current file */
303 *save_file = rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
308 * Frees all memory allocated by the ringbuffer
315 if (rb_data.files != NULL) {
316 for (i=0; i < rb_data.num_files; i++) {
317 if (rb_data.files[i].name != NULL) {
318 g_free(rb_data.files[i].name);
319 rb_data.files[i].name = NULL;
322 g_free(rb_data.files);
323 rb_data.files = NULL;
325 if (rb_data.fprefix != NULL) {
326 g_free(rb_data.fprefix);
327 rb_data.fprefix = NULL;
329 if (rb_data.fsuffix != NULL) {
330 g_free(rb_data.fsuffix);
331 rb_data.fsuffix = NULL;
336 * Frees all memory allocated by the ringbuffer
339 ringbuf_error_cleanup(void)
343 /* try to close via wtap */
344 if (rb_data.pdh != NULL) {
345 if (libpcap_dump_close(rb_data.pdh, NULL)) {
351 /* close directly if still open */
352 /* XXX - it shouldn't still be open; "libpcap_dump_close()" should leave the
353 file closed even if it fails */
354 if (rb_data.fd != -1) {
355 ws_close(rb_data.fd);
359 if (rb_data.files != NULL) {
360 for (i=0; i < rb_data.num_files; i++) {
361 if (rb_data.files[i].name != NULL) {
362 ws_unlink(rb_data.files[i].name);
366 /* free the memory */
370 #endif /* HAVE_LIBPCAP */