forked from osmocom/wireshark
Make ringbuffer.[ch] file format agnostic.
Move write routines to dumpcap.c This is a preparation for pcapng support. svn path=/trunk/; revision=28155
This commit is contained in:
parent
c68e344284
commit
f5547c0d78
41
dumpcap.c
41
dumpcap.c
|
@ -213,6 +213,7 @@ typedef struct _loop_data {
|
||||||
/* output file */
|
/* output file */
|
||||||
FILE *pdh;
|
FILE *pdh;
|
||||||
int linktype;
|
int linktype;
|
||||||
|
int file_snaplen;
|
||||||
gint wtap_linktype;
|
gint wtap_linktype;
|
||||||
long bytes_written;
|
long bytes_written;
|
||||||
|
|
||||||
|
@ -1454,7 +1455,6 @@ capture_loop_init_filter(pcap_t *pcap_h, gboolean from_cap_pipe, gchar * iface,
|
||||||
/* set up to write to the already-opened capture output file/files */
|
/* set up to write to the already-opened capture output file/files */
|
||||||
static gboolean
|
static gboolean
|
||||||
capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_data *ld, char *errmsg, int errmsg_len) {
|
capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_data *ld, char *errmsg, int errmsg_len) {
|
||||||
int file_snaplen;
|
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
|
|
||||||
|
@ -1462,19 +1462,25 @@ capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_d
|
||||||
|
|
||||||
/* get snaplen */
|
/* get snaplen */
|
||||||
if (ld->from_cap_pipe) {
|
if (ld->from_cap_pipe) {
|
||||||
file_snaplen = ld->cap_pipe_hdr.snaplen;
|
ld->file_snaplen = ld->cap_pipe_hdr.snaplen;
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
file_snaplen = pcap_snapshot(ld->pcap_h);
|
ld->file_snaplen = pcap_snapshot(ld->pcap_h);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set up to write to the capture file. */
|
/* Set up to write to the capture file. */
|
||||||
if (capture_opts->multi_files_on) {
|
if (capture_opts->multi_files_on) {
|
||||||
ld->pdh = ringbuf_init_libpcap_fdopen(ld->linktype, file_snaplen,
|
ld->pdh = ringbuf_init_libpcap_fdopen(&err);
|
||||||
&ld->bytes_written, &err);
|
|
||||||
} else {
|
} else {
|
||||||
ld->pdh = libpcap_fdopen(save_file_fd, ld->linktype, file_snaplen,
|
ld->pdh = libpcap_fdopen(save_file_fd, &err);
|
||||||
&ld->bytes_written, &err);
|
}
|
||||||
|
if (ld->pdh) {
|
||||||
|
ld->bytes_written = 0;
|
||||||
|
if (!libpcap_write_file_header(ld->pdh, ld->linktype, ld->file_snaplen,
|
||||||
|
&ld->bytes_written, &err)) {
|
||||||
|
fclose(ld->pdh);
|
||||||
|
ld->pdh = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ld->pdh == NULL) {
|
if (ld->pdh == NULL) {
|
||||||
|
@ -1980,8 +1986,16 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct
|
||||||
|
|
||||||
/* Switch to the next ringbuffer file */
|
/* Switch to the next ringbuffer file */
|
||||||
if (ringbuf_switch_file(&global_ld.pdh, &capture_opts->save_file,
|
if (ringbuf_switch_file(&global_ld.pdh, &capture_opts->save_file,
|
||||||
&save_file_fd, &global_ld.bytes_written, &global_ld.err)) {
|
&save_file_fd, &global_ld.err)) {
|
||||||
/* File switch succeeded: reset the conditions */
|
/* File switch succeeded: reset the conditions */
|
||||||
|
global_ld.bytes_written = 0;
|
||||||
|
if (!libpcap_write_file_header(global_ld.pdh, global_ld.linktype, global_ld.file_snaplen,
|
||||||
|
&global_ld.bytes_written, &global_ld.err)) {
|
||||||
|
fclose(global_ld.pdh);
|
||||||
|
global_ld.pdh = NULL;
|
||||||
|
global_ld.go = FALSE;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
cnd_reset(cnd_autostop_size);
|
cnd_reset(cnd_autostop_size);
|
||||||
if (cnd_file_duration) {
|
if (cnd_file_duration) {
|
||||||
cnd_reset(cnd_file_duration);
|
cnd_reset(cnd_file_duration);
|
||||||
|
@ -2053,9 +2067,16 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct
|
||||||
|
|
||||||
/* Switch to the next ringbuffer file */
|
/* Switch to the next ringbuffer file */
|
||||||
if (ringbuf_switch_file(&global_ld.pdh, &capture_opts->save_file,
|
if (ringbuf_switch_file(&global_ld.pdh, &capture_opts->save_file,
|
||||||
&save_file_fd, &global_ld.bytes_written,
|
&save_file_fd, &global_ld.err)) {
|
||||||
&global_ld.err)) {
|
|
||||||
/* file switch succeeded: reset the conditions */
|
/* file switch succeeded: reset the conditions */
|
||||||
|
global_ld.bytes_written = 0;
|
||||||
|
if (!libpcap_write_file_header(global_ld.pdh, global_ld.linktype, global_ld.file_snaplen,
|
||||||
|
&global_ld.bytes_written, &global_ld.err)) {
|
||||||
|
fclose(global_ld.pdh);
|
||||||
|
global_ld.pdh = NULL;
|
||||||
|
global_ld.go = FALSE;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
cnd_reset(cnd_file_duration);
|
cnd_reset(cnd_file_duration);
|
||||||
if(cnd_autostop_size)
|
if(cnd_autostop_size)
|
||||||
cnd_reset(cnd_autostop_size);
|
cnd_reset(cnd_autostop_size);
|
||||||
|
|
31
pcapio.c
31
pcapio.c
|
@ -92,21 +92,27 @@ struct pcaprec_hdr {
|
||||||
guint32 orig_len; /* actual length of packet */
|
guint32 orig_len; /* actual length of packet */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Returns a FILE * to write to on success, NULL on failure; sets "*err" to
|
/* Returns a FILE * to write to on success, NULL on failure */
|
||||||
an error code, or 0 for a short write, on failure */
|
|
||||||
FILE *
|
FILE *
|
||||||
libpcap_fdopen(int fd, int linktype, int snaplen, long *bytes_written,
|
libpcap_fdopen(int fd, int *err)
|
||||||
int *err)
|
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
struct pcap_hdr file_hdr;
|
|
||||||
size_t nwritten;
|
|
||||||
|
|
||||||
fp = fdopen(fd, "wb");
|
fp = fdopen(fd, "wb");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
*err = errno;
|
*err = errno;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
return fp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write the file header to a dump file.
|
||||||
|
Returns TRUE on success, FALSE on failure.
|
||||||
|
Sets "*err" to an error code, or 0 for a short write, on failure*/
|
||||||
|
gboolean
|
||||||
|
libpcap_write_file_header(FILE *fp, int linktype, int snaplen, long *bytes_written, int *err)
|
||||||
|
{
|
||||||
|
struct pcap_hdr file_hdr;
|
||||||
|
size_t nwritten;
|
||||||
|
|
||||||
file_hdr.magic = PCAP_MAGIC;
|
file_hdr.magic = PCAP_MAGIC;
|
||||||
/* current "libpcap" format is 2.4 */
|
/* current "libpcap" format is 2.4 */
|
||||||
|
@ -116,18 +122,17 @@ libpcap_fdopen(int fd, int linktype, int snaplen, long *bytes_written,
|
||||||
file_hdr.sigfigs = 0; /* unknown, but also apparently unused */
|
file_hdr.sigfigs = 0; /* unknown, but also apparently unused */
|
||||||
file_hdr.snaplen = snaplen;
|
file_hdr.snaplen = snaplen;
|
||||||
file_hdr.network = linktype;
|
file_hdr.network = linktype;
|
||||||
nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, fp);
|
nwritten = fwrite(&file_hdr, 1, sizeof(file_hdr), fp);
|
||||||
if (nwritten != sizeof file_hdr) {
|
if (nwritten != sizeof(file_hdr)) {
|
||||||
if (nwritten == 0 && ferror(fp))
|
if (nwritten == 0 && ferror(fp))
|
||||||
*err = errno;
|
*err = errno;
|
||||||
else
|
else
|
||||||
*err = 0; /* short write */
|
*err = 0; /* short write */
|
||||||
fclose(fp);
|
return FALSE;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
*bytes_written = sizeof file_hdr;
|
*bytes_written += sizeof(file_hdr);
|
||||||
|
|
||||||
return fp;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write a record for a packet to a dump file.
|
/* Write a record for a packet to a dump file.
|
||||||
|
|
12
pcapio.h
12
pcapio.h
|
@ -25,11 +25,15 @@
|
||||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Returns a FILE * to write to on success, NULL on failure; sets "*err" to
|
/* Returns a FILE * to write to on success, NULL on failure */
|
||||||
an error code, or 0 for a short write, on failure */
|
|
||||||
extern FILE *
|
extern FILE *
|
||||||
libpcap_fdopen(int fd, int linktype, int snaplen, long *bytes_written,
|
libpcap_fdopen(int fd, int *err);
|
||||||
int *err);
|
|
||||||
|
/* Write the file header to a dump file.
|
||||||
|
Returns TRUE on success, FALSE on failure.
|
||||||
|
Sets "*err" to an error code, or 0 for a short write, on failure*/
|
||||||
|
extern gboolean
|
||||||
|
libpcap_write_file_header(FILE *fp, int linktype, int snaplen, long *bytes_written, int *err);
|
||||||
|
|
||||||
/* Write a record for a packet to a dump file.
|
/* Write a record for a packet to a dump file.
|
||||||
Returns TRUE on success, FALSE on failure. */
|
Returns TRUE on success, FALSE on failure. */
|
||||||
|
|
18
ringbuffer.c
18
ringbuffer.c
|
@ -83,8 +83,6 @@ typedef struct _ringbuf_data {
|
||||||
gchar *fprefix; /* Filename prefix */
|
gchar *fprefix; /* Filename prefix */
|
||||||
gchar *fsuffix; /* Filename suffix */
|
gchar *fsuffix; /* Filename suffix */
|
||||||
gboolean unlimited; /* TRUE if unlimited number of files */
|
gboolean unlimited; /* TRUE if unlimited number of files */
|
||||||
int linktype;
|
|
||||||
int snaplen;
|
|
||||||
|
|
||||||
int fd; /* Current ringbuffer file descriptor */
|
int fd; /* Current ringbuffer file descriptor */
|
||||||
FILE *pdh;
|
FILE *pdh;
|
||||||
|
@ -230,15 +228,9 @@ const gchar *ringbuf_current_filename(void)
|
||||||
* Calls libpcap_fdopen() for the current ringbuffer file
|
* Calls libpcap_fdopen() for the current ringbuffer file
|
||||||
*/
|
*/
|
||||||
FILE *
|
FILE *
|
||||||
ringbuf_init_libpcap_fdopen(int linktype, int snaplen,
|
ringbuf_init_libpcap_fdopen(int *err)
|
||||||
long *bytes_written, int *err)
|
|
||||||
{
|
{
|
||||||
|
rb_data.pdh = libpcap_fdopen(rb_data.fd, err);
|
||||||
rb_data.linktype = linktype;
|
|
||||||
rb_data.snaplen = snaplen;
|
|
||||||
|
|
||||||
rb_data.pdh = libpcap_fdopen(rb_data.fd, linktype, snaplen, bytes_written,
|
|
||||||
err);
|
|
||||||
return rb_data.pdh;
|
return rb_data.pdh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,8 +238,7 @@ ringbuf_init_libpcap_fdopen(int linktype, int snaplen,
|
||||||
* Switches to the next ringbuffer file
|
* Switches to the next ringbuffer file
|
||||||
*/
|
*/
|
||||||
gboolean
|
gboolean
|
||||||
ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd,
|
ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
|
||||||
long *bytes_written, int *err)
|
|
||||||
{
|
{
|
||||||
int next_file_index;
|
int next_file_index;
|
||||||
rb_file *next_rfile = NULL;
|
rb_file *next_rfile = NULL;
|
||||||
|
@ -274,8 +265,7 @@ ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd,
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ringbuf_init_libpcap_fdopen(rb_data.linktype, rb_data.snaplen,
|
if (ringbuf_init_libpcap_fdopen(err) == NULL) {
|
||||||
bytes_written, err) == NULL) {
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,10 +40,9 @@
|
||||||
|
|
||||||
int ringbuf_init(const char *capture_name, guint num_files);
|
int ringbuf_init(const char *capture_name, guint num_files);
|
||||||
const gchar *ringbuf_current_filename(void);
|
const gchar *ringbuf_current_filename(void);
|
||||||
FILE *ringbuf_init_libpcap_fdopen(int linktype, int snaplen,
|
FILE *ringbuf_init_libpcap_fdopen(int *err);
|
||||||
long *bytes_written, int *err);
|
|
||||||
gboolean ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd,
|
gboolean ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd,
|
||||||
long *bytes_written, int *err);
|
int *err);
|
||||||
gboolean ringbuf_libpcap_dump_close(gchar **save_file, int *err);
|
gboolean ringbuf_libpcap_dump_close(gchar **save_file, int *err);
|
||||||
void ringbuf_free(void);
|
void ringbuf_free(void);
|
||||||
void ringbuf_error_cleanup(void);
|
void ringbuf_error_cleanup(void);
|
||||||
|
|
Loading…
Reference in New Issue