Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return errors from pcap_dump and pcap_dump_close #1048

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
Finalize moving of bpf_filter.c. (GH #1166)
Address a few compiler warnings on Haiku.
struct pcap: Update buffer type from "void *" to "u_char *".
Add pcap_dump1 and pcap_dump_close1 functions that propagates
write errors to the caller.
Link-layer types:
Add LINKTYPE_ETW/DLT_ETW.
Add LINKTYPE_NETANALYZER_NG/DLT_NETANALYZER_NG (pull request
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3555,7 +3555,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)
Expand Down
4 changes: 4 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,12 @@ install: install-shared install-archive libpcap.pc pcap-config @INSTALL_RPCAPD@
rm -f pcap_datalink_val_to_description_or_dlt.3pcap && \
$(LN_S) pcap_datalink_val_to_name.3pcap \
pcap_datalink_val_to_description_or_dlt.3pcap && \
rm -f pcap_dump_close1.3pcap && \
$(LN_S) pcap_dump_close.3pcap pcap_dump_close1.3pcap && \
rm -f pcap_dump_fopen.3pcap && \
$(LN_S) pcap_dump_open.3pcap pcap_dump_fopen.3pcap && \
rm -f pcap_dump1.3pcap && \
$(LN_S) pcap_dump.3pcap pcap_dump1.3pcap && \
rm -f pcap_freealldevs.3pcap && \
$(LN_S) pcap_findalldevs.3pcap pcap_freealldevs.3pcap && \
rm -f pcap_perror.3pcap && \
Expand Down
7 changes: 7 additions & 0 deletions pcap/pcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,18 @@ PCAP_AVAILABLE_0_8
PCAP_API int pcap_dump_flush(pcap_dumper_t *);

PCAP_AVAILABLE_0_4
PCAP_DEPRECATED("use 'pcap_dump_close1'")
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 *);

Expand Down
11 changes: 10 additions & 1 deletion pcap_dump.3pcap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +29,9 @@ pcap_dump \- write a packet to a capture file
.ft B
void pcap_dump(u_char *user, struct pcap_pkthdr *h,
u_char *sp);
.ti +8
int pcap_dump1(u_char *user, struct pcap_pkthdr *h,
u_char *sp);
.ft
.fi
.SH DESCRIPTION
Expand All @@ -45,5 +48,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)
9 changes: 8 additions & 1 deletion pcap_dump_close.3pcap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
32 changes: 23 additions & 9 deletions sf-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,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;
Expand All @@ -766,7 +772,7 @@ pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
* http://pubs.opengroup.org/onlinepubs/009695399/functions/fwrite.html
*/
if (ferror(f))
return;
return (-1);
/*
* Better not try writing pcap files after
* 2038-01-19 03:14:07 UTC; switch to pcapng.
Expand All @@ -783,9 +789,14 @@ pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
* way to set ferror() to prevent future writes from being
* attempted, but it is better than nothing.
*/
if (fwrite(&sf_hdr, sizeof(sf_hdr), 1, f) == 1) {
(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 *
Expand Down Expand Up @@ -1181,11 +1192,14 @@ pcap_dump_flush(pcap_dumper_t *p)
void
pcap_dump_close(pcap_dumper_t *p)
{
(void)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);
}