2 * Routines for packet capture windows
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * <laurent.deniel@free.fr>
26 * Almost completely rewritten in order to:
28 * - be able to use a unlimited number of ringbuffer files
29 * - close the current file and open (truncating) the next file at switch
30 * - set the final file name once open (or reopen)
31 * - avoid the deletion of files that could not be truncated (can't arise now)
32 * and do not erase empty files
34 * The idea behind that is to remove the limitation of the maximum # of
35 * ringbuffer files being less than the maximum # of open fd per process
36 * and to be able to reduce the amount of virtual memory usage (having only
37 * one file open at most) or the amount of file system usage (by truncating
38 * the files at switch and not the capture stop, and by closing them which
39 * makes possible their move or deletion after a switch).
52 #include <wsutil/wspcap.h>
56 #include "ringbuffer.h"
57 #include <wsutil/file_util.h>
60 /* Ringbuffer file structure */
61 typedef struct _rb_file {
65 /* Ringbuffer data structure */
66 typedef struct _ringbuf_data {
68 guint num_files; /* Number of ringbuffer files (1 to ...) */
69 guint curr_file_num; /* Number of the current file (ever increasing) */
70 gchar *fprefix; /* Filename prefix */
71 gchar *fsuffix; /* Filename suffix */
72 gboolean unlimited; /* TRUE if unlimited number of files */
74 int fd; /* Current ringbuffer file descriptor */
76 gboolean group_read_access; /* TRUE if files need to be opened with group read access */
79 static ringbuf_data rb_data;
83 * create the next filename and open a new binary file with that name
85 static int ringbuf_open_file(rb_file *rfile, int *err)
92 if (rfile->name != NULL) {
93 if (rb_data.unlimited == FALSE) {
94 /* remove old file (if any, so ignore error) */
95 ws_unlink(rfile->name);
103 current_time = time(NULL);
105 g_snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % RINGBUFFER_MAX_NUM_FILES);
106 tm = localtime(¤t_time);
108 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", tm);
110 g_strlcpy(timestr, "196912312359", sizeof(timestr)); /* second before the Epoch */
111 rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr,
112 rb_data.fsuffix, NULL);
114 if (rfile->name == NULL) {
120 rb_data.fd = ws_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
121 rb_data.group_read_access ? 0640 : 0600);
123 if (rb_data.fd == -1 && err != NULL) {
131 * Initialize the ringbuffer data structures
134 ringbuf_init(const char *capfile_name, guint num_files, gboolean group_read_access)
137 char *pfx, *last_pathsep;
140 rb_data.files = NULL;
141 rb_data.curr_file_num = 0;
142 rb_data.fprefix = NULL;
143 rb_data.fsuffix = NULL;
144 rb_data.unlimited = FALSE;
147 rb_data.group_read_access = group_read_access;
149 /* just to be sure ... */
150 if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
151 rb_data.num_files = num_files;
153 rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
156 /* Check file name */
157 if (capfile_name == NULL) {
158 /* ringbuffer does not work with temporary files! */
162 /* set file name prefix/suffix */
164 save_file = g_strdup(capfile_name);
165 last_pathsep = strrchr(save_file, G_DIR_SEPARATOR);
166 pfx = strrchr(save_file,'.');
167 if (pfx != NULL && (last_pathsep == NULL || pfx > last_pathsep)) {
168 /* The pathname has a "." in it, and it's in the last component
169 of the pathname (because there is either only one component,
170 i.e. last_pathsep is null as there are no path separators,
171 or the "." is after the path separator before the last
174 Treat it as a separator between the rest of the file name and
175 the file name suffix, and arrange that the names given to the
176 ring buffer files have the specified suffix, i.e. put the
177 changing part of the name *before* the suffix. */
179 rb_data.fprefix = g_strdup(save_file);
180 pfx[0] = '.'; /* restore capfile_name */
181 rb_data.fsuffix = g_strdup(pfx);
183 /* Either there's no "." in the pathname, or it's in a directory
184 component, so the last component has no suffix. */
185 rb_data.fprefix = g_strdup(save_file);
186 rb_data.fsuffix = NULL;
191 /* allocate rb_file structures (only one if unlimited since there is no
192 need to save all file names in that case) */
194 if (num_files == RINGBUFFER_UNLIMITED_FILES) {
195 rb_data.unlimited = TRUE;
196 rb_data.num_files = 1;
199 rb_data.files = (rb_file *)g_malloc(rb_data.num_files * sizeof(rb_file));
200 if (rb_data.files == NULL) {
204 for (i=0; i < rb_data.num_files; i++) {
205 rb_data.files[i].name = NULL;
208 /* create the first file */
209 if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
210 ringbuf_error_cleanup();
218 const gchar *ringbuf_current_filename(void)
220 return rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
224 * Calls ws_fdopen() for the current ringbuffer file
227 ringbuf_init_libpcap_fdopen(int *err)
229 rb_data.pdh = ws_fdopen(rb_data.fd, "wb");
230 if (rb_data.pdh == NULL) {
239 * Switches to the next ringbuffer file
242 ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
245 rb_file *next_rfile = NULL;
247 /* close current file */
249 if (fclose(rb_data.pdh) == EOF) {
253 ws_close(rb_data.fd); /* XXX - the above should have closed this already */
254 rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
262 /* get the next file number and open it */
264 rb_data.curr_file_num++ /* = next_file_num*/;
265 next_file_index = (rb_data.curr_file_num) % rb_data.num_files;
266 next_rfile = &rb_data.files[next_file_index];
268 if (ringbuf_open_file(next_rfile, err) == -1) {
272 if (ringbuf_init_libpcap_fdopen(err) == NULL) {
276 /* switch to the new file */
277 *save_file = next_rfile->name;
278 *save_file_fd = rb_data.fd;
279 (*pdh) = rb_data.pdh;
285 * Calls fclose() for the current ringbuffer file
288 ringbuf_libpcap_dump_close(gchar **save_file, int *err)
290 gboolean ret_val = TRUE;
292 /* close current file, if it's open */
293 if (rb_data.pdh != NULL) {
294 if (fclose(rb_data.pdh) == EOF) {
298 ws_close(rb_data.fd);
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;
311 * Frees all memory allocated by the ringbuffer
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;
325 g_free(rb_data.files);
326 rb_data.files = NULL;
328 if (rb_data.fprefix != NULL) {
329 g_free(rb_data.fprefix);
330 rb_data.fprefix = NULL;
332 if (rb_data.fsuffix != NULL) {
333 g_free(rb_data.fsuffix);
334 rb_data.fsuffix = NULL;
339 * Frees all memory allocated by the ringbuffer
342 ringbuf_error_cleanup(void)
346 /* try to close via wtap */
347 if (rb_data.pdh != NULL) {
348 if (fclose(rb_data.pdh) == 0) {
354 /* close directly if still open */
355 if (rb_data.fd != -1) {
356 ws_close(rb_data.fd);
360 if (rb_data.files != NULL) {
361 for (i=0; i < rb_data.num_files; i++) {
362 if (rb_data.files[i].name != NULL) {
363 ws_unlink(rb_data.files[i].name);
367 /* free the memory */
371 #endif /* HAVE_LIBPCAP */
374 * Editor modelines - http://www.wireshark.org/tools/modelines.html
379 * indent-tabs-mode: nil
382 * ex: set shiftwidth=2 tabstop=8 expandtab:
383 * :indentSize=2:tabSize=8:noTabs=true: