Skip to content

Commit 40b4594

Browse files
committed
Return errors from pcap_dump and pcap_dump_close
1 parent 340d28d commit 40b4594

File tree

7 files changed

+55
-11
lines changed

7 files changed

+55
-11
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
88
Finalize moving of bpf_filter.c. (GH #1166)
99
Address a few compiler warnings on Haiku.
1010
struct pcap: Update buffer type from "void *" to "u_char *".
11+
Add pcap_dump1 and pcap_dump_close1 functions that propagates
12+
write errors to the caller.
1113
Link-layer types:
1214
Add LINKTYPE_ETW/DLT_ETW.
1315
Add LINKTYPE_NETANALYZER_NG/DLT_NETANALYZER_NG (pull request

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,7 +3555,9 @@ if(NOT MSVC)
35553555
install(FILES ${MAN3PCAP} DESTINATION ${CMAKE_INSTALL_MANDIR}/man3)
35563556
install_manpage_symlink(pcap_datalink_val_to_name.3pcap pcap_datalink_val_to_description.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
35573557
install_manpage_symlink(pcap_datalink_val_to_name.3pcap pcap_datalink_val_to_description_or_dlt.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3558+
install_manpage_symlink(pcap_dump_close.3pcap pcap_dump_close1.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
35583559
install_manpage_symlink(pcap_dump_open.3pcap pcap_dump_fopen.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3560+
install_manpage_symlink(pcap_dump.3pcap pcap_dump1.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
35593561
install_manpage_symlink(pcap_findalldevs.3pcap pcap_freealldevs.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
35603562
install_manpage_symlink(pcap_geterr.3pcap pcap_perror.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
35613563
install_manpage_symlink(pcap_inject.3pcap pcap_sendpacket.3pcap ${CMAKE_INSTALL_MANDIR}/man3)

Makefile.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,12 @@ install: install-shared install-archive libpcap.pc pcap-config @INSTALL_RPCAPD@
627627
rm -f pcap_datalink_val_to_description_or_dlt.3pcap && \
628628
$(LN_S) pcap_datalink_val_to_name.3pcap \
629629
pcap_datalink_val_to_description_or_dlt.3pcap && \
630+
rm -f pcap_dump_close1.3pcap && \
631+
$(LN_S) pcap_dump_close.3pcap pcap_dump_close1.3pcap && \
630632
rm -f pcap_dump_fopen.3pcap && \
631633
$(LN_S) pcap_dump_open.3pcap pcap_dump_fopen.3pcap && \
634+
rm -f pcap_dump1.3pcap && \
635+
$(LN_S) pcap_dump.3pcap pcap_dump1.3pcap && \
632636
rm -f pcap_freealldevs.3pcap && \
633637
$(LN_S) pcap_findalldevs.3pcap pcap_freealldevs.3pcap && \
634638
rm -f pcap_perror.3pcap && \

pcap/pcap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,15 @@ PCAP_API int pcap_dump_flush(pcap_dumper_t *);
788788
PCAP_AVAILABLE_0_4
789789
PCAP_API void pcap_dump_close(pcap_dumper_t *);
790790

791+
PCAP_AVAILABLE_1_11
792+
PCAP_API int pcap_dump_close1(pcap_dumper_t *);
793+
791794
PCAP_AVAILABLE_0_4
792795
PCAP_API void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);
793796

797+
PCAP_AVAILABLE_1_11
798+
PCAP_API int pcap_dump1(u_char *, const struct pcap_pkthdr *, const u_char *);
799+
794800
PCAP_AVAILABLE_0_7
795801
PCAP_API int pcap_findalldevs(pcap_if_t **, char *);
796802

pcap_dump.3pcap

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
.\"
2020
.TH PCAP_DUMP 3PCAP "8 March 2015"
2121
.SH NAME
22-
pcap_dump \- write a packet to a capture file
22+
pcap_dump, pcap_dump1 \- write a packet to a capture file
2323
.SH SYNOPSIS
2424
.nf
2525
.ft B
@@ -29,6 +29,9 @@ pcap_dump \- write a packet to a capture file
2929
.ft B
3030
void pcap_dump(u_char *user, struct pcap_pkthdr *h,
3131
u_char *sp);
32+
.ti +8
33+
int pcap_dump1(u_char *user, struct pcap_pkthdr *h,
34+
u_char *sp);
3235
.ft
3336
.fi
3437
.SH DESCRIPTION
@@ -45,5 +48,11 @@ parameter is of type
4548
.B pcap_dumper_t
4649
as returned by
4750
.BR pcap_dump_open ().
51+
.SH RETURN VALUE
52+
Returns
53+
.B 0
54+
on success and
55+
.B PCAP_ERROR
56+
on failure.
4857
.SH SEE ALSO
4958
.BR pcap (3PCAP)

pcap_dump_close.3pcap

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
.\"
2020
.TH PCAP_DUMP_CLOSE 3PCAP "3 January 2014"
2121
.SH NAME
22-
pcap_dump_close \- close a savefile being written to
22+
pcap_dump_close, pcap_dump_close1 \- close a savefile being written to
2323
.SH SYNOPSIS
2424
.nf
2525
.ft B
@@ -28,11 +28,18 @@ pcap_dump_close \- close a savefile being written to
2828
.LP
2929
.ft B
3030
void pcap_dump_close(pcap_dumper_t *p);
31+
int pcap_dump_close1(pcap_dumper_t *p);
3132
.ft
3233
.fi
3334
.SH DESCRIPTION
3435
.BR pcap_dump_close ()
3536
closes the ``savefile.''
37+
.SH RETURN VALUE
38+
Returns
39+
.B 0
40+
on success and
41+
.B PCAP_ERROR
42+
on failure.
3643
.SH SEE ALSO
3744
.BR pcap (3PCAP),
3845
.BR pcap_dump_open (3PCAP),

sf-pcap.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,12 @@ sf_write_header(pcap_t *p, FILE *fp, int linktype, int snaplen)
744744
*/
745745
void
746746
pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
747+
{
748+
(void)pcap_dump1(user, h, sp);
749+
}
750+
751+
int
752+
pcap_dump1(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
747753
{
748754
register FILE *f;
749755
struct pcap_sf_pkthdr sf_hdr;
@@ -766,7 +772,7 @@ pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
766772
* http://pubs.opengroup.org/onlinepubs/009695399/functions/fwrite.html
767773
*/
768774
if (ferror(f))
769-
return;
775+
return (-1);
770776
/*
771777
* Better not try writing pcap files after
772778
* 2038-01-19 03:14:07 UTC; switch to pcapng.
@@ -783,9 +789,14 @@ pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
783789
* way to set ferror() to prevent future writes from being
784790
* attempted, but it is better than nothing.
785791
*/
786-
if (fwrite(&sf_hdr, sizeof(sf_hdr), 1, f) == 1) {
787-
(void)fwrite(sp, h->caplen, 1, f);
792+
if (fwrite(&sf_hdr, 1, sizeof(sf_hdr), f) != sizeof(sf_hdr)) {
793+
return (-1);
794+
}
795+
if (fwrite(sp, 1, h->caplen, f) != h->caplen) {
796+
return (-1);
788797
}
798+
799+
return (0);
789800
}
790801

791802
static pcap_dumper_t *
@@ -1181,11 +1192,14 @@ pcap_dump_flush(pcap_dumper_t *p)
11811192
void
11821193
pcap_dump_close(pcap_dumper_t *p)
11831194
{
1195+
(void)pcap_dump_close1(p);
1196+
}
11841197

1185-
#ifdef notyet
1186-
if (ferror((FILE *)p))
1187-
return-an-error;
1188-
/* XXX should check return from fclose() too */
1189-
#endif
1190-
(void)fclose((FILE *)p);
1198+
int
1199+
pcap_dump_close1(pcap_dumper_t *p)
1200+
{
1201+
if (fclose((FILE *)p) == EOF) {
1202+
return (-1);
1203+
}
1204+
return (0);
11911205
}

0 commit comments

Comments
 (0)