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).
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 char *io_buffer
; /**< The IO buffer used to write to the file */
65 gboolean group_read_access
; /**< TRUE if files need to be opened with group read access */
66 FILE *name_h
; /**< write names of completed files to this handle */
69 static ringbuf_data rb_data
;
73 * create the next filename and open a new binary file with that name
75 static int ringbuf_open_file(rb_file
*rfile
, int *err
)
82 if (rfile
->name
!= NULL
) {
83 if (rb_data
.unlimited
== FALSE
) {
84 /* remove old file (if any, so ignore error) */
85 ws_unlink(rfile
->name
);
93 current_time
= time(NULL
);
95 g_snprintf(filenum
, sizeof(filenum
), "%05u", (rb_data
.curr_file_num
+ 1) % RINGBUFFER_MAX_NUM_FILES
);
96 tm
= localtime(¤t_time
);
98 strftime(timestr
, sizeof(timestr
), "%Y%m%d%H%M%S", tm
);
100 g_strlcpy(timestr
, "196912312359", sizeof(timestr
)); /* second before the Epoch */
101 rfile
->name
= g_strconcat(rb_data
.fprefix
, "_", filenum
, "_", timestr
,
102 rb_data
.fsuffix
, NULL
);
104 if (rfile
->name
== NULL
) {
110 rb_data
.fd
= ws_open(rfile
->name
, O_RDWR
|O_BINARY
|O_TRUNC
|O_CREAT
,
111 rb_data
.group_read_access
? 0640 : 0600);
113 if (rb_data
.fd
== -1 && err
!= NULL
) {
121 * Initialize the ringbuffer data structures
124 ringbuf_init(const char *capfile_name
, guint num_files
, gboolean group_read_access
)
127 char *pfx
, *last_pathsep
;
130 rb_data
.files
= NULL
;
131 rb_data
.curr_file_num
= 0;
132 rb_data
.fprefix
= NULL
;
133 rb_data
.fsuffix
= NULL
;
134 rb_data
.unlimited
= FALSE
;
137 rb_data
.io_buffer
= NULL
;
138 rb_data
.group_read_access
= group_read_access
;
139 rb_data
.name_h
= NULL
;
141 /* just to be sure ... */
142 if (num_files
<= RINGBUFFER_MAX_NUM_FILES
) {
143 rb_data
.num_files
= num_files
;
145 rb_data
.num_files
= RINGBUFFER_MAX_NUM_FILES
;
148 /* Check file name */
149 if (capfile_name
== NULL
) {
150 /* ringbuffer does not work with temporary files! */
154 /* set file name prefix/suffix */
156 save_file
= g_strdup(capfile_name
);
157 last_pathsep
= strrchr(save_file
, G_DIR_SEPARATOR
);
158 pfx
= strrchr(save_file
,'.');
159 if (pfx
!= NULL
&& (last_pathsep
== NULL
|| pfx
> last_pathsep
)) {
160 /* The pathname has a "." in it, and it's in the last component
161 of the pathname (because there is either only one component,
162 i.e. last_pathsep is null as there are no path separators,
163 or the "." is after the path separator before the last
166 Treat it as a separator between the rest of the file name and
167 the file name suffix, and arrange that the names given to the
168 ring buffer files have the specified suffix, i.e. put the
169 changing part of the name *before* the suffix. */
171 rb_data
.fprefix
= g_strdup(save_file
);
172 pfx
[0] = '.'; /* restore capfile_name */
173 rb_data
.fsuffix
= g_strdup(pfx
);
175 /* Either there's no "." in the pathname, or it's in a directory
176 component, so the last component has no suffix. */
177 rb_data
.fprefix
= g_strdup(save_file
);
178 rb_data
.fsuffix
= NULL
;
183 /* allocate rb_file structures (only one if unlimited since there is no
184 need to save all file names in that case) */
186 if (num_files
== RINGBUFFER_UNLIMITED_FILES
) {
187 rb_data
.unlimited
= TRUE
;
188 rb_data
.num_files
= 1;
191 rb_data
.files
= (rb_file
*)g_malloc(rb_data
.num_files
* sizeof(rb_file
));
192 if (rb_data
.files
== NULL
) {
196 for (i
=0; i
< rb_data
.num_files
; i
++) {
197 rb_data
.files
[i
].name
= NULL
;
200 /* create the first file */
201 if (ringbuf_open_file(&rb_data
.files
[0], NULL
) == -1) {
202 ringbuf_error_cleanup();
210 * Set name of file to which to print ringbuffer file names.
213 ringbuf_set_print_name(gchar
*name
, int *err
)
215 if (rb_data
.name_h
!= NULL
) {
216 if (EOF
== fclose(rb_data
.name_h
)) {
223 if (!strcmp(name
, "-") || !strcmp(name
, "stdout")) {
224 rb_data
.name_h
= stdout
;
225 } else if (!strcmp(name
, "stderr")) {
226 rb_data
.name_h
= stderr
;
228 if (NULL
== (rb_data
.name_h
= ws_fopen(name
, "wt"))) {
239 * Whether the ringbuf filenames are ready.
240 * (Whether ringbuf_init is called and ringbuf_free is not called.)
242 gboolean
ringbuf_is_initialized(void)
244 return rb_data
.files
!= NULL
;
247 const gchar
*ringbuf_current_filename(void)
249 /* g_assert(ringbuf_is_initialized()); */
250 return rb_data
.files
[rb_data
.curr_file_num
% rb_data
.num_files
].name
;
254 * Calls ws_fdopen() for the current ringbuffer file
257 ringbuf_init_libpcap_fdopen(int *err
)
259 rb_data
.pdh
= ws_fdopen(rb_data
.fd
, "wb");
260 if (rb_data
.pdh
== NULL
) {
265 size_t buffsize
= IO_BUF_SIZE
;
266 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
269 if (ws_fstat64(rb_data
.fd
, &statb
) == 0) {
270 if (statb
.st_blksize
> IO_BUF_SIZE
) {
271 buffsize
= statb
.st_blksize
;
275 /* Increase the size of the IO buffer */
276 rb_data
.io_buffer
= (char *)g_realloc(rb_data
.io_buffer
, buffsize
);
277 setvbuf(rb_data
.pdh
, rb_data
.io_buffer
, _IOFBF
, buffsize
);
284 * Switches to the next ringbuffer file
287 ringbuf_switch_file(FILE **pdh
, gchar
**save_file
, int *save_file_fd
, int *err
)
290 rb_file
*next_rfile
= NULL
;
292 /* close current file */
294 if (fclose(rb_data
.pdh
) == EOF
) {
298 ws_close(rb_data
.fd
); /* XXX - the above should have closed this already */
299 rb_data
.pdh
= NULL
; /* it's still closed, we just got an error while closing */
301 g_free(rb_data
.io_buffer
);
302 rb_data
.io_buffer
= NULL
;
309 if (rb_data
.name_h
!= NULL
) {
310 fprintf(rb_data
.name_h
, "%s\n", ringbuf_current_filename());
311 fflush(rb_data
.name_h
);
314 /* get the next file number and open it */
316 rb_data
.curr_file_num
++ /* = next_file_num*/;
317 next_file_index
= (rb_data
.curr_file_num
) % rb_data
.num_files
;
318 next_rfile
= &rb_data
.files
[next_file_index
];
320 if (ringbuf_open_file(next_rfile
, err
) == -1) {
324 if (ringbuf_init_libpcap_fdopen(err
) == NULL
) {
328 /* switch to the new file */
329 *save_file
= next_rfile
->name
;
330 *save_file_fd
= rb_data
.fd
;
331 (*pdh
) = rb_data
.pdh
;
337 * Calls fclose() for the current ringbuffer file
340 ringbuf_libpcap_dump_close(gchar
**save_file
, int *err
)
342 gboolean ret_val
= TRUE
;
344 /* close current file, if it's open */
345 if (rb_data
.pdh
!= NULL
) {
346 if (fclose(rb_data
.pdh
) == EOF
) {
350 ws_close(rb_data
.fd
);
355 g_free(rb_data
.io_buffer
);
356 rb_data
.io_buffer
= NULL
;
360 if (rb_data
.name_h
!= NULL
) {
361 fprintf(rb_data
.name_h
, "%s\n", ringbuf_current_filename());
362 fflush(rb_data
.name_h
);
364 if (EOF
== fclose(rb_data
.name_h
)) {
365 /* Can't really do much about this, can we? */
369 /* set the save file name to the current file */
370 *save_file
= rb_data
.files
[rb_data
.curr_file_num
% rb_data
.num_files
].name
;
375 * Frees all memory allocated by the ringbuffer
382 if (rb_data
.files
!= NULL
) {
383 for (i
=0; i
< rb_data
.num_files
; i
++) {
384 if (rb_data
.files
[i
].name
!= NULL
) {
385 g_free(rb_data
.files
[i
].name
);
386 rb_data
.files
[i
].name
= NULL
;
389 g_free(rb_data
.files
);
390 rb_data
.files
= NULL
;
392 if (rb_data
.fprefix
!= NULL
) {
393 g_free(rb_data
.fprefix
);
394 rb_data
.fprefix
= NULL
;
396 if (rb_data
.fsuffix
!= NULL
) {
397 g_free(rb_data
.fsuffix
);
398 rb_data
.fsuffix
= NULL
;
403 * Frees all memory allocated by the ringbuffer
406 ringbuf_error_cleanup(void)
410 /* try to close via wtap */
411 if (rb_data
.pdh
!= NULL
) {
412 if (fclose(rb_data
.pdh
) == 0) {
418 /* close directly if still open */
419 if (rb_data
.fd
!= -1) {
420 ws_close(rb_data
.fd
);
424 if (rb_data
.files
!= NULL
) {
425 for (i
=0; i
< rb_data
.num_files
; i
++) {
426 if (rb_data
.files
[i
].name
!= NULL
) {
427 ws_unlink(rb_data
.files
[i
].name
);
431 g_free(rb_data
.io_buffer
);
432 rb_data
.io_buffer
= NULL
;
434 if (rb_data
.name_h
!= NULL
) {
435 if (EOF
== fclose(rb_data
.name_h
)) {
436 /* Can't really do much about this, can we? */
440 /* free the memory */
444 #endif /* HAVE_LIBPCAP */
447 * Editor modelines - https://www.wireshark.org/tools/modelines.html
452 * indent-tabs-mode: nil
455 * ex: set shiftwidth=2 tabstop=8 expandtab:
456 * :indentSize=2:tabSize=8:noTabs=true: