2 * Routines for packet capture windows
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
12 * <laurent.deniel@free.fr>
14 * Almost completely rewritten in order to:
16 * - be able to use a unlimited number of ringbuffer files
17 * - close the current file and open (truncating) the next file at switch
18 * - set the final file name once open (or reopen)
19 * - avoid the deletion of files that could not be truncated (can't arise now)
20 * and do not erase empty files
22 * The idea behind that is to remove the limitation of the maximum # of
23 * ringbuffer files being less than the maximum # of open fd per process
24 * and to be able to reduce the amount of virtual memory usage (having only
25 * one file open at most) or the amount of file system usage (by truncating
26 * the files at switch and not the capture stop, and by closing them which
27 * makes possible their move or deletion after a switch).
40 #include <wsutil/wspcap.h>
44 #include "ringbuffer.h"
45 #include <wsutil/file_util.h>
48 /* Ringbuffer file structure */
49 typedef struct _rb_file {
53 /* Ringbuffer data structure */
54 typedef struct _ringbuf_data {
56 guint num_files; /* Number of ringbuffer files (1 to ...) */
57 guint curr_file_num; /* Number of the current file (ever increasing) */
58 gchar *fprefix; /* Filename prefix */
59 gchar *fsuffix; /* Filename suffix */
60 gboolean unlimited; /* TRUE if unlimited number of files */
62 int fd; /* Current ringbuffer file descriptor */
64 gboolean group_read_access; /* TRUE if files need to be opened with group read access */
67 static ringbuf_data rb_data;
71 * create the next filename and open a new binary file with that name
73 static int ringbuf_open_file(rb_file *rfile, int *err)
80 if (rfile->name != NULL) {
81 if (rb_data.unlimited == FALSE) {
82 /* remove old file (if any, so ignore error) */
83 ws_unlink(rfile->name);
91 current_time = time(NULL);
93 g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % RINGBUFFER_MAX_NUM_FILES);
94 tm = localtime(¤t_time);
96 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", tm);
98 g_strlcpy(timestr, "196912312359", sizeof(timestr)); /* second before the Epoch */
99 rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
100 rb_data.fsuffix, NULL);
102 if (rfile->name == NULL) {
108 rb_data.fd = ws_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
109 rb_data.group_read_access ? 0640 : 0600);
111 if (rb_data.fd == -1 && err != NULL) {
119 * Initialize the ringbuffer data structures
122 ringbuf_init(const char *capfile_name, guint num_files, gboolean group_read_access)
125 char *pfx, *last_pathsep;
128 rb_data.files = NULL;
129 rb_data.curr_file_num = 0;
130 rb_data.fprefix = NULL;
131 rb_data.fsuffix = NULL;
132 rb_data.unlimited = FALSE;
135 rb_data.group_read_access = group_read_access;
137 /* just to be sure ... */
138 if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
139 rb_data.num_files = num_files;
141 rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
144 /* Check file name */
145 if (capfile_name == NULL) {
146 /* ringbuffer does not work with temporary files! */
150 /* set file name prefix/suffix */
152 save_file = g_strdup(capfile_name);
153 last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
154 pfx = strrchr(save_file,'.');
155 if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
156 /* The pathname has a "." in it, and it's in the last component
157 of the pathname (because there is either only one component,
158 i.e. last_pathsep is null as there are no path separators,
159 or the "." is after the path separator before the last
162 Treat it as a separator between the rest of the file name and
163 the file name suffix, and arrange that the names given to the
164 ring buffer files have the specified suffix, i.e. put the
165 changing part of the name *before* the suffix. */
167 rb_data.fprefix = g_strdup(save_file);
168 pfx[0] = '.'; /* restore capfile_name */
169 rb_data.fsuffix = g_strdup(pfx);
171 /* Either there's no "." in the pathname, or it's in a directory
172 component, so the last component has no suffix. */
173 rb_data.fprefix = g_strdup(save_file);
174 rb_data.fsuffix = NULL;
179 /* allocate rb_file structures (only one if unlimited since there is no
180 need to save all file names in that case) */
182 if (num_files == RINGBUFFER_UNLIMITED_FILES) {
183 rb_data.unlimited = TRUE;
184 rb_data.num_files = 1;
187 rb_data.files = (rb_file *)g_malloc(rb_data.num_files * sizeof(rb_file));
188 if (rb_data.files == NULL) {
192 for (i=0; i < rb_data.num_files; i++) {
193 rb_data.files[i].name = NULL;
196 /* create the first file */
197 if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
198 ringbuf_error_cleanup();
206 const gchar *ringbuf_current_filename(void)
208 return rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
212 * Calls ws_fdopen() for the current ringbuffer file
215 ringbuf_init_libpcap_fdopen(int *err)
217 rb_data.pdh = ws_fdopen(rb_data.fd, "wb");
218 if (rb_data.pdh == NULL) {
227 * Switches to the next ringbuffer file
230 ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
233 rb_file *next_rfile = NULL;
235 /* close current file */
237 if (fclose(rb_data.pdh) == EOF) {
241 ws_close(rb_data.fd); /* XXX - the above should have closed this already */
242 rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
250 /* get the next file number and open it */
252 rb_data.curr_file_num++ /* = next_file_num*/;
253 next_file_index = (rb_data.curr_file_num) % rb_data.num_files;
254 next_rfile = &rb_data.files[next_file_index];
256 if (ringbuf_open_file(next_rfile, err) == -1) {
260 if (ringbuf_init_libpcap_fdopen(err) == NULL) {
264 /* switch to the new file */
265 *save_file = next_rfile->name;
266 *save_file_fd = rb_data.fd;
267 (*pdh) = rb_data.pdh;
273 * Calls fclose() for the current ringbuffer file
276 ringbuf_libpcap_dump_close(gchar **save_file, int *err)
278 gboolean ret_val = TRUE;
280 /* close current file, if it's open */
281 if (rb_data.pdh != NULL) {
282 if (fclose(rb_data.pdh) == EOF) {
286 ws_close(rb_data.fd);
293 /* set the save file name to the current file */
294 *save_file = rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
299 * Frees all memory allocated by the ringbuffer
306 if (rb_data.files != NULL) {
307 for (i=0; i < rb_data.num_files; i++) {
308 if (rb_data.files[i].name != NULL) {
309 g_free(rb_data.files[i].name);
310 rb_data.files[i].name = NULL;
313 g_free(rb_data.files);
314 rb_data.files = NULL;
316 if (rb_data.fprefix != NULL) {
317 g_free(rb_data.fprefix);
318 rb_data.fprefix = NULL;
320 if (rb_data.fsuffix != NULL) {
321 g_free(rb_data.fsuffix);
322 rb_data.fsuffix = NULL;
327 * Frees all memory allocated by the ringbuffer
330 ringbuf_error_cleanup(void)
334 /* try to close via wtap */
335 if (rb_data.pdh != NULL) {
336 if (fclose(rb_data.pdh) == 0) {
342 /* close directly if still open */
343 if (rb_data.fd != -1) {
344 ws_close(rb_data.fd);
348 if (rb_data.files != NULL) {
349 for (i=0; i < rb_data.num_files; i++) {
350 if (rb_data.files[i].name != NULL) {
351 ws_unlink(rb_data.files[i].name);
355 /* free the memory */
359 #endif /* HAVE_LIBPCAP */
362 * Editor modelines - http://www.wireshark.org/tools/modelines.html
367 * indent-tabs-mode: nil
370 * ex: set shiftwidth=2 tabstop=8 expandtab:
371 * :indentSize=2:tabSize=8:noTabs=true: