From 821e1c1b555351a60bdc7e5094daa63b947a3a02 Mon Sep 17 00:00:00 2001 From: Erik Rigtorp Date: Wed, 25 Aug 2021 16:13:05 +0200 Subject: [PATCH] Return errors from pcap_dump and pcap_dump_close --- CMakeLists.txt | 2 ++ pcap/pcap.h | 6 ++++++ pcap_dump.3pcap | 11 ++++++++++- pcap_dump_close.3pcap | 9 ++++++++- sf-pcap.c | 32 +++++++++++++++++++++++--------- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fdd91800f4..f9a165f570 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2772,7 +2772,9 @@ if(NOT MSVC) install(FILES ${MAN3PCAP} DESTINATION ${CMAKE_INSTALL_MANDIR}/man3) install_manpage_symlink(pcap_datalink_val_to_name.3pcap pcap_datalink_val_to_description.3pcap ${CMAKE_INSTALL_MANDIR}/man3) install_manpage_symlink(pcap_datalink_val_to_name.3pcap pcap_datalink_val_to_description_or_dlt.3pcap ${CMAKE_INSTALL_MANDIR}/man3) + install_manpage_symlink(pcap_dump_close.3pcap pcap_dump_close1.3pcap ${CMAKE_INSTALL_MANDIR}/man3) install_manpage_symlink(pcap_dump_open.3pcap pcap_dump_fopen.3pcap ${CMAKE_INSTALL_MANDIR}/man3) + install_manpage_symlink(pcap_dump.3pcap pcap_dump1.3pcap ${CMAKE_INSTALL_MANDIR}/man3) install_manpage_symlink(pcap_findalldevs.3pcap pcap_freealldevs.3pcap ${CMAKE_INSTALL_MANDIR}/man3) install_manpage_symlink(pcap_geterr.3pcap pcap_perror.3pcap ${CMAKE_INSTALL_MANDIR}/man3) install_manpage_symlink(pcap_inject.3pcap pcap_sendpacket.3pcap ${CMAKE_INSTALL_MANDIR}/man3) diff --git a/pcap/pcap.h b/pcap/pcap.h index 8182bef44a..9fadfe5d25 100644 --- a/pcap/pcap.h +++ b/pcap/pcap.h @@ -738,9 +738,15 @@ PCAP_API int pcap_dump_flush(pcap_dumper_t *); PCAP_AVAILABLE_0_4 PCAP_API void pcap_dump_close(pcap_dumper_t *); +PCAP_AVAILABLE_1_11 +PCAP_API int pcap_dump_close1(pcap_dumper_t *); + PCAP_AVAILABLE_0_4 PCAP_API void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *); +PCAP_AVAILABLE_1_11 +PCAP_API int pcap_dump1(u_char *, const struct pcap_pkthdr *, const u_char *); + PCAP_AVAILABLE_0_7 PCAP_API int pcap_findalldevs(pcap_if_t **, char *); diff --git a/pcap_dump.3pcap b/pcap_dump.3pcap index ba7f75adb3..99b7cb094f 100644 --- a/pcap_dump.3pcap +++ b/pcap_dump.3pcap @@ -19,7 +19,7 @@ .\" .TH PCAP_DUMP 3PCAP "8 March 2015" .SH NAME -pcap_dump \- write a packet to a capture file +pcap_dump, pcap_dump1 \- write a packet to a capture file .SH SYNOPSIS .nf .ft B @@ -30,6 +30,9 @@ pcap_dump \- write a packet to a capture file void pcap_dump(u_char *user, struct pcap_pkthdr *h, .ti +8 u_char *sp); +int pcap_dump(u_char *user, struct pcap_pkthdr *h, +.ti +8 +u_char *sp); .ft .fi .SH DESCRIPTION @@ -46,5 +49,11 @@ parameter is of type .B pcap_dumper_t as returned by .BR pcap_dump_open (). +.SH RETURN VALUE +Returns +.B 0 +on success and +.B PCAP_ERROR +on failure. .SH SEE ALSO .BR pcap (3PCAP) diff --git a/pcap_dump_close.3pcap b/pcap_dump_close.3pcap index a62eb34dbf..7569a6db9d 100644 --- a/pcap_dump_close.3pcap +++ b/pcap_dump_close.3pcap @@ -19,7 +19,7 @@ .\" .TH PCAP_DUMP_CLOSE 3PCAP "3 January 2014" .SH NAME -pcap_dump_close \- close a savefile being written to +pcap_dump_close, pcap_dump_close1 \- close a savefile being written to .SH SYNOPSIS .nf .ft B @@ -28,11 +28,18 @@ pcap_dump_close \- close a savefile being written to .LP .ft B void pcap_dump_close(pcap_dumper_t *p); +int pcap_dump_close1(pcap_dumper_t *p); .ft .fi .SH DESCRIPTION .BR pcap_dump_close () closes the ``savefile.'' +.SH RETURN VALUE +Returns +.B 0 +on success and +.B PCAP_ERROR +on failure. .SH SEE ALSO .BR pcap (3PCAP), .BR pcap_dump_open (3PCAP), diff --git a/sf-pcap.c b/sf-pcap.c index d8443e9825..91ed70c1c2 100644 --- a/sf-pcap.c +++ b/sf-pcap.c @@ -743,6 +743,12 @@ sf_write_header(pcap_t *p, FILE *fp, int linktype, int snaplen) */ void pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) +{ + (void)pcap_dump1(user, h, sp); +} + +int +pcap_dump1(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) { register FILE *f; struct pcap_sf_pkthdr sf_hdr; @@ -756,9 +762,14 @@ pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) sf_hdr.ts.tv_usec = (bpf_int32)h->ts.tv_usec; sf_hdr.caplen = h->caplen; sf_hdr.len = h->len; - /* XXX we should check the return status */ - (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f); - (void)fwrite(sp, h->caplen, 1, f); + if (fwrite(&sf_hdr, 1, sizeof(sf_hdr), f) != sizeof(sf_hdr)) { + return -1; + } + if (fwrite(sp, 1, h->caplen, f) != h->caplen) { + return -1; + } + + return 0; } static pcap_dumper_t * @@ -1154,11 +1165,14 @@ pcap_dump_flush(pcap_dumper_t *p) void pcap_dump_close(pcap_dumper_t *p) { + pcap_dump_close1(p); +} -#ifdef notyet - if (ferror((FILE *)p)) - return-an-error; - /* XXX should check return from fclose() too */ -#endif - (void)fclose((FILE *)p); +int +pcap_dump_close1(pcap_dumper_t *p) +{ + if (fclose((FILE *)p) == EOF) { + return (-1); + } + return (0); }