2001-12-04 08:45:04 +00:00
|
|
|
/* ringbuffer.c
|
|
|
|
* Routines for packet capture windows
|
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2001-12-04 08:45:04 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
2018-02-07 12:26:45 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2001-12-04 08:45:04 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-22 16:09:04 +00:00
|
|
|
/*
|
|
|
|
* <laurent.deniel@free.fr>
|
|
|
|
*
|
|
|
|
* Almost completely rewritten in order to:
|
2008-05-22 15:46:27 +00:00
|
|
|
*
|
2003-06-22 16:09:04 +00:00
|
|
|
* - be able to use a unlimited number of ringbuffer files
|
|
|
|
* - close the current file and open (truncating) the next file at switch
|
|
|
|
* - set the final file name once open (or reopen)
|
|
|
|
* - avoid the deletion of files that could not be truncated (can't arise now)
|
|
|
|
* and do not erase empty files
|
|
|
|
*
|
2008-05-22 15:46:27 +00:00
|
|
|
* The idea behind that is to remove the limitation of the maximum # of
|
2003-06-22 16:09:04 +00:00
|
|
|
* ringbuffer files being less than the maximum # of open fd per process
|
|
|
|
* and to be able to reduce the amount of virtual memory usage (having only
|
|
|
|
* one file open at most) or the amount of file system usage (by truncating
|
2008-05-22 15:46:27 +00:00
|
|
|
* the files at switch and not the capture stop, and by closing them which
|
2003-06-22 16:09:04 +00:00
|
|
|
* makes possible their move or deletion after a switch).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-22 22:13:05 +01:00
|
|
|
#include <config.h>
|
2001-12-04 08:45:04 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBPCAP
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2020-08-09 12:12:24 +09:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <glib.h>
|
2001-12-04 08:45:04 +00:00
|
|
|
|
2025-01-07 06:14:20 -05:00
|
|
|
#include <pcap/pcap.h>
|
2006-03-04 22:33:04 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2020-08-09 12:12:24 +09:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <wsutil/win32-utils.h>
|
|
|
|
#endif
|
|
|
|
|
2001-12-04 08:45:04 +00:00
|
|
|
#include "ringbuffer.h"
|
2024-05-20 20:08:45 +10:00
|
|
|
#include <wsutil/array.h>
|
2008-05-22 15:46:27 +00:00
|
|
|
#include <wsutil/file_util.h>
|
2001-12-04 08:45:04 +00:00
|
|
|
|
|
|
|
/* Ringbuffer file structure */
|
|
|
|
typedef struct _rb_file {
|
2024-07-07 09:29:58 -04:00
|
|
|
char *name;
|
2001-12-04 08:45:04 +00:00
|
|
|
} rb_file;
|
|
|
|
|
2020-08-09 12:12:24 +09:00
|
|
|
#define MAX_FILENAME_QUEUE 100
|
|
|
|
|
2019-01-03 15:51:56 +01:00
|
|
|
/** Ringbuffer data structure */
|
2001-12-04 08:45:04 +00:00
|
|
|
typedef struct _ringbuf_data {
|
2022-02-20 19:39:37 +00:00
|
|
|
rb_file *files;
|
2024-07-07 09:29:58 -04:00
|
|
|
unsigned num_files; /**< Number of ringbuffer files (1 to ...) */
|
|
|
|
unsigned curr_file_num; /**< Number of the current file (ever increasing) */
|
|
|
|
char *fprefix; /**< Filename prefix */
|
|
|
|
char *fsuffix; /**< Filename suffix */
|
|
|
|
bool nametimenum; /**< ...num_time... or ...time_num... */
|
|
|
|
bool unlimited; /**< true if unlimited number of files */
|
2022-02-20 19:39:37 +00:00
|
|
|
|
|
|
|
int fd; /**< Current ringbuffer file descriptor */
|
2025-01-21 09:47:46 -05:00
|
|
|
pcapio_writer* pdh;
|
2024-07-07 09:29:58 -04:00
|
|
|
bool group_read_access; /**< true if files need to be opened with group read access */
|
2022-02-20 19:39:37 +00:00
|
|
|
FILE *name_h; /**< write names of completed files to this handle */
|
2025-01-21 09:47:46 -05:00
|
|
|
const char *compress_type; /**< compress type */
|
2002-08-28 21:04:11 +00:00
|
|
|
} ringbuf_data;
|
2001-12-04 08:45:04 +00:00
|
|
|
|
|
|
|
static ringbuf_data rb_data;
|
|
|
|
|
2005-01-12 21:41:30 +00:00
|
|
|
/*
|
2008-05-22 15:46:27 +00:00
|
|
|
* create the next filename and open a new binary file with that name
|
2005-01-12 21:41:30 +00:00
|
|
|
*/
|
2022-02-20 19:39:37 +00:00
|
|
|
static int
|
|
|
|
ringbuf_open_file(rb_file *rfile, int *err)
|
2003-06-22 16:09:04 +00:00
|
|
|
{
|
2022-02-20 19:39:37 +00:00
|
|
|
char filenum[5+1];
|
|
|
|
char timestr[14+1];
|
|
|
|
time_t current_time;
|
|
|
|
struct tm *tm;
|
|
|
|
|
|
|
|
if (rfile->name != NULL) {
|
2024-07-07 09:29:58 -04:00
|
|
|
if (rb_data.unlimited == false) {
|
2022-02-20 19:39:37 +00:00
|
|
|
/* remove old file (if any, so ignore error) */
|
|
|
|
ws_unlink(rfile->name);
|
|
|
|
}
|
|
|
|
g_free(rfile->name);
|
|
|
|
}
|
2003-06-22 16:09:04 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2022-02-20 19:39:37 +00:00
|
|
|
_tzset();
|
2003-06-22 16:09:04 +00:00
|
|
|
#endif
|
2022-02-20 19:39:37 +00:00
|
|
|
current_time = time(NULL);
|
|
|
|
|
|
|
|
snprintf(filenum, sizeof(filenum), "%05u", (rb_data.curr_file_num + 1) % RINGBUFFER_MAX_NUM_FILES);
|
|
|
|
tm = localtime(¤t_time);
|
|
|
|
if (tm != NULL)
|
|
|
|
strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", tm);
|
|
|
|
else
|
|
|
|
(void) g_strlcpy(timestr, "196912312359", sizeof(timestr)); /* second before the Epoch */
|
|
|
|
if (rb_data.nametimenum) {
|
|
|
|
rfile->name = g_strconcat(rb_data.fprefix, "_", timestr, "_", filenum, rb_data.fsuffix, NULL);
|
|
|
|
} else {
|
|
|
|
rfile->name = g_strconcat(rb_data.fprefix, "_", filenum, "_", timestr, rb_data.fsuffix, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rfile->name == NULL) {
|
|
|
|
if (err != NULL)
|
|
|
|
*err = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_data.fd = ws_open(rfile->name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
|
|
|
|
rb_data.group_read_access ? 0640 : 0600);
|
|
|
|
|
|
|
|
if (rb_data.fd == -1 && err != NULL) {
|
|
|
|
*err = errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_data.fd;
|
2003-06-22 16:09:04 +00:00
|
|
|
}
|
|
|
|
|
2002-08-28 21:04:11 +00:00
|
|
|
/*
|
2003-06-22 16:09:04 +00:00
|
|
|
* Initialize the ringbuffer data structures
|
2001-12-04 08:45:04 +00:00
|
|
|
*/
|
2002-08-28 21:04:11 +00:00
|
|
|
int
|
2024-07-07 09:29:58 -04:00
|
|
|
ringbuf_init(const char *capfile_name, unsigned num_files, bool group_read_access,
|
2025-01-21 09:47:46 -05:00
|
|
|
const char *compress_type, bool has_nametimenum)
|
2001-12-04 08:45:04 +00:00
|
|
|
{
|
2022-02-20 19:39:37 +00:00
|
|
|
unsigned int i;
|
2024-01-24 09:05:25 -05:00
|
|
|
char *pfx;
|
|
|
|
char *dir_name, *base_name;
|
2022-02-20 19:39:37 +00:00
|
|
|
|
|
|
|
rb_data.files = NULL;
|
|
|
|
rb_data.curr_file_num = 0;
|
|
|
|
rb_data.fprefix = NULL;
|
2001-12-04 08:45:04 +00:00
|
|
|
rb_data.fsuffix = NULL;
|
2022-02-20 19:39:37 +00:00
|
|
|
rb_data.nametimenum = has_nametimenum;
|
2024-07-07 09:29:58 -04:00
|
|
|
rb_data.unlimited = false;
|
2022-02-20 19:39:37 +00:00
|
|
|
rb_data.fd = -1;
|
|
|
|
rb_data.pdh = NULL;
|
|
|
|
rb_data.group_read_access = group_read_access;
|
|
|
|
rb_data.name_h = NULL;
|
|
|
|
rb_data.compress_type = compress_type;
|
|
|
|
|
|
|
|
/* just to be sure ... */
|
|
|
|
if (num_files <= RINGBUFFER_MAX_NUM_FILES) {
|
|
|
|
rb_data.num_files = num_files;
|
|
|
|
} else {
|
|
|
|
rb_data.num_files = RINGBUFFER_MAX_NUM_FILES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check file name */
|
|
|
|
if (capfile_name == NULL) {
|
|
|
|
/* ringbuffer does not work with temporary files! */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set file name prefix/suffix */
|
|
|
|
|
2024-01-24 09:05:25 -05:00
|
|
|
base_name = g_path_get_basename(capfile_name);
|
|
|
|
dir_name = g_path_get_dirname(capfile_name);
|
|
|
|
pfx = strrchr(base_name, '.');
|
|
|
|
if (pfx != NULL) {
|
|
|
|
/* The basename has a "." in it.
|
2022-02-20 19:39:37 +00:00
|
|
|
|
|
|
|
Treat it as a separator between the rest of the file name and
|
|
|
|
the file name suffix, and arrange that the names given to the
|
|
|
|
ring buffer files have the specified suffix, i.e. put the
|
2025-01-21 09:47:46 -05:00
|
|
|
changing part of the name *before* the suffix. */
|
2022-02-20 19:39:37 +00:00
|
|
|
pfx[0] = '\0';
|
2025-01-21 09:47:46 -05:00
|
|
|
/* Is the suffix a compression type extension? (XXX - Should
|
|
|
|
* we only check this if compressing, and only for the compression
|
|
|
|
* type used?) */
|
|
|
|
GSList *compression_type_extensions = wtap_get_all_compression_type_extensions_list();
|
|
|
|
for (GSList *compression_type_extension = compression_type_extensions;
|
|
|
|
compression_type_extension != NULL;
|
|
|
|
compression_type_extension = g_slist_next(compression_type_extension)) {
|
|
|
|
|
|
|
|
if (g_ascii_strcasecmp(pfx + 1, (const char*)compression_type_extension->data) == 0) {
|
|
|
|
/* It's a compression type extension. Is there a previous extension? */
|
|
|
|
char *sfx = strrchr(base_name, '.');
|
|
|
|
if (sfx != NULL) {
|
|
|
|
/* Yes. Use both extensions as the suffix. */
|
|
|
|
pfx[0] = '.'; /* restore last suffix */
|
|
|
|
sfx[0] = '\0';
|
|
|
|
pfx = sfx;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_slist_free(compression_type_extensions);
|
2024-01-24 09:05:25 -05:00
|
|
|
rb_data.fprefix = g_build_filename(dir_name, base_name, NULL);
|
2022-02-20 19:39:37 +00:00
|
|
|
pfx[0] = '.'; /* restore capfile_name */
|
|
|
|
rb_data.fsuffix = g_strdup(pfx);
|
|
|
|
} else {
|
2024-01-24 09:05:25 -05:00
|
|
|
/* The last component has no suffix. */
|
|
|
|
rb_data.fprefix = g_strdup(capfile_name);
|
2022-02-20 19:39:37 +00:00
|
|
|
rb_data.fsuffix = NULL;
|
|
|
|
}
|
2024-01-24 09:05:25 -05:00
|
|
|
g_free(dir_name);
|
|
|
|
g_free(base_name);
|
2022-02-20 19:39:37 +00:00
|
|
|
|
|
|
|
/* allocate rb_file structures (only one if unlimited since there is no
|
|
|
|
need to save all file names in that case) */
|
|
|
|
|
|
|
|
if (num_files == RINGBUFFER_UNLIMITED_FILES) {
|
2024-07-07 09:29:58 -04:00
|
|
|
rb_data.unlimited = true;
|
2022-02-20 19:39:37 +00:00
|
|
|
rb_data.num_files = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_data.files = g_new(rb_file, rb_data.num_files);
|
|
|
|
if (rb_data.files == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i < rb_data.num_files; i++) {
|
|
|
|
rb_data.files[i].name = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the first file */
|
|
|
|
if (ringbuf_open_file(&rb_data.files[0], NULL) == -1) {
|
|
|
|
ringbuf_error_cleanup();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_data.fd;
|
2001-12-04 08:45:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 09:36:19 -04:00
|
|
|
/*
|
|
|
|
* Set name of file to which to print ringbuffer file names.
|
|
|
|
*/
|
2024-07-07 09:29:58 -04:00
|
|
|
bool
|
|
|
|
ringbuf_set_print_name(char *name, int *err)
|
2020-07-29 09:36:19 -04:00
|
|
|
{
|
2022-02-20 19:39:37 +00:00
|
|
|
if (rb_data.name_h != NULL) {
|
|
|
|
if (EOF == fclose(rb_data.name_h)) {
|
|
|
|
if (err != NULL) {
|
|
|
|
*err = errno;
|
|
|
|
}
|
2024-07-07 09:29:58 -04:00
|
|
|
return false;
|
2022-02-20 19:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "-") || !strcmp(name, "stdout")) {
|
|
|
|
rb_data.name_h = stdout;
|
|
|
|
} else if (!strcmp(name, "stderr")) {
|
|
|
|
rb_data.name_h = stderr;
|
|
|
|
} else {
|
|
|
|
if (NULL == (rb_data.name_h = ws_fopen(name, "wt"))) {
|
|
|
|
if (err != NULL) {
|
|
|
|
*err = errno;
|
|
|
|
}
|
2024-07-07 09:29:58 -04:00
|
|
|
return false;
|
2022-02-20 19:39:37 +00:00
|
|
|
}
|
2020-07-29 09:36:19 -04:00
|
|
|
}
|
2024-07-07 09:29:58 -04:00
|
|
|
return true;
|
2020-07-29 09:36:19 -04:00
|
|
|
}
|
2005-03-28 00:19:02 +00:00
|
|
|
|
2019-01-25 17:04:33 +01:00
|
|
|
/*
|
|
|
|
* Whether the ringbuf filenames are ready.
|
|
|
|
* (Whether ringbuf_init is called and ringbuf_free is not called.)
|
|
|
|
*/
|
2024-07-07 09:29:58 -04:00
|
|
|
bool
|
2022-02-20 19:39:37 +00:00
|
|
|
ringbuf_is_initialized(void)
|
2019-01-25 17:04:33 +01:00
|
|
|
{
|
2022-02-20 19:39:37 +00:00
|
|
|
return rb_data.files != NULL;
|
2019-01-25 17:04:33 +01:00
|
|
|
}
|
|
|
|
|
2024-07-07 09:29:58 -04:00
|
|
|
const char *
|
2022-02-20 19:39:37 +00:00
|
|
|
ringbuf_current_filename(void)
|
2005-03-28 00:19:02 +00:00
|
|
|
{
|
2022-02-20 19:39:37 +00:00
|
|
|
return rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
|
2005-03-28 00:19:02 +00:00
|
|
|
}
|
|
|
|
|
2002-08-28 21:04:11 +00:00
|
|
|
/*
|
2012-12-20 14:53:09 +00:00
|
|
|
* Calls ws_fdopen() for the current ringbuffer file
|
2001-12-04 08:45:04 +00:00
|
|
|
*/
|
2025-01-21 09:47:46 -05:00
|
|
|
pcapio_writer*
|
2009-04-26 15:51:25 +00:00
|
|
|
ringbuf_init_libpcap_fdopen(int *err)
|
2001-12-04 08:45:04 +00:00
|
|
|
{
|
2025-01-21 09:47:46 -05:00
|
|
|
rb_data.pdh = writecap_fdopen(rb_data.fd, wtap_name_to_compression_type(rb_data.compress_type), err);
|
2019-01-03 15:51:56 +01:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
return rb_data.pdh;
|
2001-12-04 08:45:04 +00:00
|
|
|
}
|
|
|
|
|
2002-08-28 21:04:11 +00:00
|
|
|
/*
|
2001-12-04 08:45:04 +00:00
|
|
|
* Switches to the next ringbuffer file
|
|
|
|
*/
|
2024-07-07 09:29:58 -04:00
|
|
|
bool
|
2025-01-21 09:47:46 -05:00
|
|
|
ringbuf_switch_file(pcapio_writer* *pdh, char **save_file, int *save_file_fd, int *err)
|
2001-12-04 08:45:04 +00:00
|
|
|
{
|
2022-02-20 19:39:37 +00:00
|
|
|
int next_file_index;
|
|
|
|
rb_file *next_rfile = NULL;
|
2003-06-22 16:09:04 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
/* close current file */
|
2003-06-22 16:09:04 +00:00
|
|
|
|
2025-01-21 09:47:46 -05:00
|
|
|
if (!writecap_close(rb_data.pdh, err)) {
|
2022-02-20 19:39:37 +00:00
|
|
|
ws_close(rb_data.fd); /* XXX - the above should have closed this already */
|
|
|
|
rb_data.pdh = NULL; /* it's still closed, we just got an error while closing */
|
|
|
|
rb_data.fd = -1;
|
2024-07-07 09:29:58 -04:00
|
|
|
return false;
|
2012-12-20 14:53:09 +00:00
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
rb_data.pdh = NULL;
|
|
|
|
rb_data.fd = -1;
|
2003-06-22 16:09:04 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
if (rb_data.name_h != NULL) {
|
|
|
|
fprintf(rb_data.name_h, "%s\n", ringbuf_current_filename());
|
|
|
|
fflush(rb_data.name_h);
|
|
|
|
}
|
2020-07-29 09:36:19 -04:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
/* get the next file number and open it */
|
2003-06-22 16:09:04 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
rb_data.curr_file_num++ /* = next_file_num*/;
|
|
|
|
next_file_index = (rb_data.curr_file_num) % rb_data.num_files;
|
|
|
|
next_rfile = &rb_data.files[next_file_index];
|
2003-06-22 16:09:04 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
if (ringbuf_open_file(next_rfile, err) == -1) {
|
2024-07-07 09:29:58 -04:00
|
|
|
return false;
|
2022-02-20 19:39:37 +00:00
|
|
|
}
|
2001-12-04 08:45:04 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
if (ringbuf_init_libpcap_fdopen(err) == NULL) {
|
2024-07-07 09:29:58 -04:00
|
|
|
return false;
|
2022-02-20 19:39:37 +00:00
|
|
|
}
|
2003-06-22 16:09:04 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
/* switch to the new file */
|
|
|
|
*save_file = next_rfile->name;
|
|
|
|
*save_file_fd = rb_data.fd;
|
|
|
|
(*pdh) = rb_data.pdh;
|
2002-06-23 20:30:01 +00:00
|
|
|
|
2024-07-07 09:29:58 -04:00
|
|
|
return true;
|
2001-12-04 08:45:04 +00:00
|
|
|
}
|
|
|
|
|
2002-08-28 21:04:11 +00:00
|
|
|
/*
|
2012-12-20 20:00:06 +00:00
|
|
|
* Calls fclose() for the current ringbuffer file
|
2001-12-04 08:45:04 +00:00
|
|
|
*/
|
2024-07-07 09:29:58 -04:00
|
|
|
bool
|
|
|
|
ringbuf_libpcap_dump_close(char **save_file, int *err)
|
2001-12-04 08:45:04 +00:00
|
|
|
{
|
2024-07-07 09:29:58 -04:00
|
|
|
bool ret_val = true;
|
2022-02-20 19:39:37 +00:00
|
|
|
|
|
|
|
/* close current file, if it's open */
|
|
|
|
if (rb_data.pdh != NULL) {
|
2025-01-21 09:47:46 -05:00
|
|
|
if (!writecap_close(rb_data.pdh, err)) {
|
2022-02-20 19:39:37 +00:00
|
|
|
ws_close(rb_data.fd);
|
2024-07-07 09:29:58 -04:00
|
|
|
ret_val = false;
|
2022-02-20 19:39:37 +00:00
|
|
|
}
|
|
|
|
rb_data.pdh = NULL;
|
|
|
|
rb_data.fd = -1;
|
If, when rotating capture files, the attempt to close the current file
fails, set "rb_data.pdh" to NULL, so we know it's not open (if
"wtap_dump_close()" fails, the wtap_dumper_t is still closed - and the
file descriptor for it is probably closed, too, as, if "close()" fails,
the FD is probably closed; the Single UNIX Specification Version 3 says
the state of the FD is unspecified, but in practice most OSes probably
still close it).
If we try to close the current file, first check to make sure it's open,
i.e. that "rb_data.pdh" is non-null. (Or perhaps we should avoid trying
to close it if the open *or* the most recent attempt to rotate the
capture files failed.)
Note that if "wtap_dump_close()" fails we might not need to close the
underlying file descriptor (and, even if we do, there's no guarantee
that attempt won't also fail and leave the FD still open - which is why
I suspect that a failed "close()" leaves the FD closed on most OSes).
svn path=/trunk/; revision=11075
2004-06-02 18:49:40 +00:00
|
|
|
}
|
2002-09-22 16:17:41 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
if (rb_data.name_h != NULL) {
|
|
|
|
fprintf(rb_data.name_h, "%s\n", ringbuf_current_filename());
|
|
|
|
fflush(rb_data.name_h);
|
2020-07-29 09:36:19 -04:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
if (EOF == fclose(rb_data.name_h)) {
|
|
|
|
/* Can't really do much about this, can we? */
|
|
|
|
}
|
2020-07-29 09:36:19 -04:00
|
|
|
}
|
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
/* set the save file name to the current file */
|
|
|
|
*save_file = rb_data.files[rb_data.curr_file_num % rb_data.num_files].name;
|
|
|
|
return ret_val;
|
2001-12-04 08:45:04 +00:00
|
|
|
}
|
|
|
|
|
2002-08-28 21:04:11 +00:00
|
|
|
/*
|
2001-12-04 08:45:04 +00:00
|
|
|
* Frees all memory allocated by the ringbuffer
|
|
|
|
*/
|
|
|
|
void
|
2011-05-17 23:35:12 +00:00
|
|
|
ringbuf_free(void)
|
2002-08-28 21:04:11 +00:00
|
|
|
{
|
2022-02-20 19:39:37 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (rb_data.files != NULL) {
|
|
|
|
for (i=0; i < rb_data.num_files; i++) {
|
|
|
|
if (rb_data.files[i].name != NULL) {
|
|
|
|
g_free(rb_data.files[i].name);
|
|
|
|
rb_data.files[i].name = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(rb_data.files);
|
|
|
|
rb_data.files = NULL;
|
|
|
|
}
|
|
|
|
if (rb_data.fprefix != NULL) {
|
|
|
|
g_free(rb_data.fprefix);
|
|
|
|
rb_data.fprefix = NULL;
|
|
|
|
}
|
|
|
|
if (rb_data.fsuffix != NULL) {
|
|
|
|
g_free(rb_data.fsuffix);
|
|
|
|
rb_data.fsuffix = NULL;
|
2001-12-04 08:45:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-28 21:04:11 +00:00
|
|
|
/*
|
2001-12-04 08:45:04 +00:00
|
|
|
* Frees all memory allocated by the ringbuffer
|
|
|
|
*/
|
2002-08-28 21:04:11 +00:00
|
|
|
void
|
2002-05-04 10:10:42 +00:00
|
|
|
ringbuf_error_cleanup(void)
|
2001-12-04 08:45:04 +00:00
|
|
|
{
|
2022-02-20 19:39:37 +00:00
|
|
|
unsigned int i;
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
/* try to close via wtap */
|
|
|
|
if (rb_data.pdh != NULL) {
|
2025-01-21 09:47:46 -05:00
|
|
|
if (writecap_close(rb_data.pdh, NULL) == 0) {
|
2022-02-20 19:39:37 +00:00
|
|
|
rb_data.fd = -1;
|
|
|
|
}
|
|
|
|
rb_data.pdh = NULL;
|
2003-06-22 16:09:04 +00:00
|
|
|
}
|
2001-12-04 08:45:04 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
/* close directly if still open */
|
|
|
|
if (rb_data.fd != -1) {
|
|
|
|
ws_close(rb_data.fd);
|
|
|
|
rb_data.fd = -1;
|
|
|
|
}
|
2003-06-22 16:09:04 +00:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
if (rb_data.files != NULL) {
|
|
|
|
for (i=0; i < rb_data.num_files; i++) {
|
|
|
|
if (rb_data.files[i].name != NULL) {
|
|
|
|
ws_unlink(rb_data.files[i].name);
|
|
|
|
}
|
|
|
|
}
|
2001-12-04 08:45:04 +00:00
|
|
|
}
|
2019-01-03 15:51:56 +01:00
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
if (rb_data.name_h != NULL) {
|
|
|
|
if (EOF == fclose(rb_data.name_h)) {
|
|
|
|
/* Can't really do much about this, can we? */
|
|
|
|
}
|
2020-07-29 09:36:19 -04:00
|
|
|
}
|
|
|
|
|
2022-02-20 19:39:37 +00:00
|
|
|
/* free the memory */
|
|
|
|
ringbuf_free();
|
2001-12-04 08:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LIBPCAP */
|