aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--dumpcap.c41
-rw-r--r--pcapio.c31
-rw-r--r--pcapio.h12
-rw-r--r--ringbuffer.c18
-rw-r--r--ringbuffer.h5
5 files changed, 63 insertions, 44 deletions
diff --git a/dumpcap.c b/dumpcap.c
index 092a8a7..3ee06b8 100644
--- a/dumpcap.c
+++ b/dumpcap.c
@@ -213,6 +213,7 @@ typedef struct _loop_data {
/* output file */
FILE *pdh;
int linktype;
+ int file_snaplen;
gint wtap_linktype;
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 */
static gboolean
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;
@@ -1462,19 +1462,25 @@ capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_d
/* get snaplen */
if (ld->from_cap_pipe) {
- file_snaplen = ld->cap_pipe_hdr.snaplen;
+ ld->file_snaplen = ld->cap_pipe_hdr.snaplen;
} else
{
- file_snaplen = pcap_snapshot(ld->pcap_h);
+ ld->file_snaplen = pcap_snapshot(ld->pcap_h);
}
/* Set up to write to the capture file. */
if (capture_opts->multi_files_on) {
- ld->pdh = ringbuf_init_libpcap_fdopen(ld->linktype, file_snaplen,
- &ld->bytes_written, &err);
+ ld->pdh = ringbuf_init_libpcap_fdopen(&err);
} else {
- ld->pdh = libpcap_fdopen(save_file_fd, ld->linktype, file_snaplen,
- &ld->bytes_written, &err);
+ ld->pdh = libpcap_fdopen(save_file_fd, &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) {
@@ -1980,8 +1986,16 @@ capture_loop_start(capture_options *capture_opts, gboolean *stats_known, struct
/* Switch to the next ringbuffer 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 */
+ 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);
if (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 */
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 */
+ 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);
if(cnd_autostop_size)
cnd_reset(cnd_autostop_size);
diff --git a/pcapio.c b/pcapio.c
index d6df784..bed260c 100644
--- a/pcapio.c
+++ b/pcapio.c
@@ -92,21 +92,27 @@ struct pcaprec_hdr {
guint32 orig_len; /* actual length of packet */
};
-/* Returns a FILE * to write to on success, NULL on failure; sets "*err" to
- an error code, or 0 for a short write, on failure */
+/* Returns a FILE * to write to on success, NULL on failure */
FILE *
-libpcap_fdopen(int fd, int linktype, int snaplen, long *bytes_written,
- int *err)
+libpcap_fdopen(int fd, int *err)
{
FILE *fp;
- struct pcap_hdr file_hdr;
- size_t nwritten;
fp = fdopen(fd, "wb");
if (fp == NULL) {
*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;
/* 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.snaplen = snaplen;
file_hdr.network = linktype;
- nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, fp);
- if (nwritten != sizeof file_hdr) {
+ nwritten = fwrite(&file_hdr, 1, sizeof(file_hdr), fp);
+ if (nwritten != sizeof(file_hdr)) {
if (nwritten == 0 && ferror(fp))
*err = errno;
else
*err = 0; /* short write */
- fclose(fp);
- return NULL;
+ return FALSE;
}
- *bytes_written = sizeof file_hdr;
+ *bytes_written += sizeof(file_hdr);
- return fp;
+ return TRUE;
}
/* Write a record for a packet to a dump file.
diff --git a/pcapio.h b/pcapio.h
index ae2d4e9..04cded1 100644
--- a/pcapio.h
+++ b/pcapio.h
@@ -25,11 +25,15 @@
* 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
- an error code, or 0 for a short write, on failure */
+/* Returns a FILE * to write to on success, NULL on failure */
extern FILE *
-libpcap_fdopen(int fd, int linktype, int snaplen, long *bytes_written,
- int *err);
+libpcap_fdopen(int fd, 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.
Returns TRUE on success, FALSE on failure. */
diff --git a/ringbuffer.c b/ringbuffer.c
index 1cced9a..b32f03c 100644
--- a/ringbuffer.c
+++ b/ringbuffer.c
@@ -83,8 +83,6 @@ typedef struct _ringbuf_data {
gchar *fprefix; /* Filename prefix */
gchar *fsuffix; /* Filename suffix */
gboolean unlimited; /* TRUE if unlimited number of files */
- int linktype;
- int snaplen;
int fd; /* Current ringbuffer file descriptor */
FILE *pdh;
@@ -230,15 +228,9 @@ const gchar *ringbuf_current_filename(void)
* Calls libpcap_fdopen() for the current ringbuffer file
*/
FILE *
-ringbuf_init_libpcap_fdopen(int linktype, int snaplen,
- long *bytes_written, int *err)
+ringbuf_init_libpcap_fdopen(int *err)
{
-
- rb_data.linktype = linktype;
- rb_data.snaplen = snaplen;
-
- rb_data.pdh = libpcap_fdopen(rb_data.fd, linktype, snaplen, bytes_written,
- err);
+ rb_data.pdh = libpcap_fdopen(rb_data.fd, err);
return rb_data.pdh;
}
@@ -246,8 +238,7 @@ ringbuf_init_libpcap_fdopen(int linktype, int snaplen,
* Switches to the next ringbuffer file
*/
gboolean
-ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd,
- long *bytes_written, int *err)
+ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd, int *err)
{
int next_file_index;
rb_file *next_rfile = NULL;
@@ -274,8 +265,7 @@ ringbuf_switch_file(FILE **pdh, gchar **save_file, int *save_file_fd,
return FALSE;
}
- if (ringbuf_init_libpcap_fdopen(rb_data.linktype, rb_data.snaplen,
- bytes_written, err) == NULL) {
+ if (ringbuf_init_libpcap_fdopen(err) == NULL) {
return FALSE;
}
diff --git a/ringbuffer.h b/ringbuffer.h
index 9ecf237..a0c3cc6 100644
--- a/ringbuffer.h
+++ b/ringbuffer.h
@@ -40,10 +40,9 @@
int ringbuf_init(const char *capture_name, guint num_files);
const gchar *ringbuf_current_filename(void);
-FILE *ringbuf_init_libpcap_fdopen(int linktype, int snaplen,
- long *bytes_written, int *err);
+FILE *ringbuf_init_libpcap_fdopen(int *err);
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);
void ringbuf_free(void);
void ringbuf_error_cleanup(void);