Skip to content

Commit

Permalink
Return errors from pcap_dump and pcap_dump_close
Browse files Browse the repository at this point in the history
  • Loading branch information
rigtorp committed Aug 25, 2021
1 parent 139a684 commit 821e1c1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pcap/pcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);

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 @@ -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
Expand All @@ -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)
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 @@ -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;
Expand All @@ -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 *
Expand Down Expand Up @@ -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);
}

0 comments on commit 821e1c1

Please sign in to comment.