From e91af83c637adaa7a9837de65f50596a32b08db0 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 28 Sep 2016 21:35:12 -0700 Subject: Replace some seeks forward with wtap_read_bytes() with a null buffer pointer. If the seek forward is just skipping record content that's not (currently) interesting, use wtap_read_bytes() with a null buffer pointer; it catches short "reads" and requires less seeking, so it may work better when reading from a pipe. Change-Id: Ifb07d20e0391a8ed97da85149d971b4e9ef093a8 Reviewed-on: https://code.wireshark.org/review/17976 Reviewed-by: Guy Harris --- wiretap/5views.c | 2 +- wiretap/lanalyzer.c | 3 ++- wiretap/libpcap.c | 2 +- wiretap/mp2t.c | 2 +- wiretap/nettl.c | 16 +++++++++------- wiretap/network_instruments.c | 12 +++++++----- wiretap/netxray.c | 2 +- wiretap/peektagged.c | 5 +++-- wiretap/radcom.c | 22 +++++++++++++++------- wiretap/snoop.c | 2 +- wiretap/vwr.c | 2 +- 11 files changed, 42 insertions(+), 28 deletions(-) (limited to 'wiretap') diff --git a/wiretap/5views.c b/wiretap/5views.c index 1c693a4c58..3ee32d17e9 100644 --- a/wiretap/5views.c +++ b/wiretap/5views.c @@ -211,7 +211,7 @@ _5views_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) /* * Not a packet - skip to the next record. */ - if (file_seek(wth->fh, TimeStamped_Header.RecSize, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, TimeStamped_Header.RecSize, err, err_info)) return FALSE; } while (1); diff --git a/wiretap/lanalyzer.c b/wiretap/lanalyzer.c index 6e6f3f49dc..e18604246e 100644 --- a/wiretap/lanalyzer.c +++ b/wiretap/lanalyzer.c @@ -424,7 +424,8 @@ wtap_open_return_val lanalyzer_open(wtap *wth, int *err, gchar **err_info) return WTAP_OPEN_MINE; default: - if (file_seek(wth->fh, record_length, SEEK_CUR, err) == -1) { + /* Unknown record type - skip it */ + if (!wtap_read_bytes(wth->fh, NULL, record_length, err, err_info)) { return WTAP_OPEN_ERROR; } break; diff --git a/wiretap/libpcap.c b/wiretap/libpcap.c index e8903b9e3c..e0133e2dc9 100644 --- a/wiretap/libpcap.c +++ b/wiretap/libpcap.c @@ -494,7 +494,7 @@ static int libpcap_try(wtap *wth, int *err, gchar **err_info) * Now skip over the first record's data, under the assumption * that the header is sane. */ - if (file_seek(wth->fh, first_rec_hdr.hdr.incl_len, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, first_rec_hdr.hdr.incl_len, err, err_info)) return -1; /* diff --git a/wiretap/mp2t.c b/wiretap/mp2t.c index 44ee0b728c..5507eef3bd 100644 --- a/wiretap/mp2t.c +++ b/wiretap/mp2t.c @@ -120,7 +120,7 @@ mp2t_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) /* if there's a trailer, skip it and go to the start of the next packet */ if (mp2t->trailer_len!=0) { - if (-1 == file_seek(wth->fh, mp2t->trailer_len, SEEK_CUR, err)) { + if (!wtap_read_bytes(wth->fh, NULL, mp2t->trailer_len, err, err_info)) { return FALSE; } } diff --git a/wiretap/nettl.c b/wiretap/nettl.c index c8481fe899..22cb007c6e 100644 --- a/wiretap/nettl.c +++ b/wiretap/nettl.c @@ -354,7 +354,8 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, return FALSE; subsys = g_ntohs(rec_hdr.subsys); hdr_len -= NETTL_REC_HDR_LEN; - if (file_seek(fh, hdr_len, SEEK_CUR, err) == -1) + /* Skip the rest of the header. */ + if (!wtap_read_bytes(fh, NULL, hdr_len, err, err_info)) return FALSE; if ( (pntoh32(&rec_hdr.kind) & NETTL_HDR_PDU_MASK) == 0 ) { @@ -443,7 +444,7 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, return FALSE; /* padding is usually either a total 11 or 16 bytes??? */ padlen = (int)dummyc[8]; - if (file_seek(fh, padlen, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(fh, NULL, padlen, err, err_info)) return FALSE; padlen += 9; } @@ -451,12 +452,12 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, || (subsys == NETTL_SUBSYS_EISA_FDDI) || (subsys == NETTL_SUBSYS_HSC_FDDI) ) { /* other flavor FDDI cards have an extra 3 bytes of padding */ - if (file_seek(fh, 3, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(fh, NULL, 3, err, err_info)) return FALSE; padlen = 3; } else if (subsys == NETTL_SUBSYS_NS_LS_LOOPBACK) { /* LOOPBACK has an extra 26 bytes of padding */ - if (file_seek(fh, 26, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(fh, NULL, 26, err, err_info)) return FALSE; padlen = 26; } else if (subsys == NETTL_SUBSYS_NS_LS_SCTP) { @@ -470,7 +471,7 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, * 1 = Inbound * 2 = Outbound */ - if (file_seek(fh, 8, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(fh, NULL, 8, err, err_info)) return FALSE; padlen = 8; } else { @@ -497,7 +498,8 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, * And what are the extra two bytes? */ if (nettl->is_hpux_11) { - if (file_seek(fh, 2, SEEK_CUR, err) == -1) return FALSE; + if (!wtap_read_bytes(fh, NULL, 2, err, err_info)) + return FALSE; } padlen = 0; break; @@ -526,7 +528,7 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, length = pntoh32(&rec_hdr.length); caplen = pntoh32(&rec_hdr.caplen); padlen = 24; /* sizeof (struct nettlrec_sx25l2_hdr) - NETTL_REC_HDR_LEN + 4 */ - if (file_seek(fh, padlen, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(fh, NULL, padlen, err, err_info)) return FALSE; break; diff --git a/wiretap/network_instruments.c b/wiretap/network_instruments.c index 286243a312..19ff6fbba8 100644 --- a/wiretap/network_instruments.c +++ b/wiretap/network_instruments.c @@ -170,6 +170,7 @@ wtap_open_return_val network_instruments_open(wtap *wth, int *err, gchar **err_i /* process (or skip over) the current TLV */ switch (tlvh.type) { case INFORMATION_TYPE_TIME_INFO: + /* XXX - what if tlvh.length != sizeof sizeof private_state->time_format? */ if (!wtap_read_bytes(wth->fh, &private_state->time_format, sizeof private_state->time_format, err, err_info)) @@ -180,7 +181,7 @@ wtap_open_return_val network_instruments_open(wtap *wth, int *err, gchar **err_i default: seek_increment = tlvh.length - (int)sizeof tlvh; if (seek_increment > 0) { - if (file_seek(wth->fh, seek_increment, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, seek_increment, err, err_info)) return WTAP_OPEN_ERROR; } offset += seek_increment; @@ -196,7 +197,7 @@ wtap_open_return_val network_instruments_open(wtap *wth, int *err, gchar **err_i } seek_increment = header_offset - offset; if (seek_increment > 0) { - if (file_seek(wth->fh, seek_increment, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, seek_increment, err, err_info)) return WTAP_OPEN_ERROR; } @@ -401,6 +402,7 @@ read_packet_header(wtap *wth, FILE_T fh, union wtap_pseudo_header *pseudo_header /* process (or skip over) the current TLV */ switch (tlvh.type) { case INFORMATION_TYPE_WIRELESS: + /* XXX - what if tlvh.length != sizeof wireless_header? */ if (!wtap_read_bytes(fh, &wireless_header, sizeof wireless_header, err, err_info)) return -1; @@ -419,7 +421,7 @@ read_packet_header(wtap *wth, FILE_T fh, union wtap_pseudo_header *pseudo_header /* skip the TLV data */ seek_increment = tlvh.length - (int)sizeof tlvh; if (seek_increment > 0) { - if (file_seek(fh, seek_increment, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(fh, NULL, seek_increment, err, err_info)) return -1; } offset += seek_increment; @@ -524,7 +526,7 @@ read_packet_data(FILE_T fh, int offset_to_frame, int current_offset_from_packet_ /* skip to the packet data */ seek_increment = offset_to_frame - current_offset_from_packet_header; if (seek_increment > 0) { - if (file_seek(fh, seek_increment, SEEK_CUR, err) == -1) { + if (!wtap_read_bytes(fh, NULL, seek_increment, err, err_info)) { return -1; } bytes_consumed += seek_increment; @@ -558,7 +560,7 @@ skip_to_next_packet(wtap *wth, int offset_to_next_packet, int current_offset_fro /* skip to the next packet header */ seek_increment = offset_to_next_packet - current_offset_from_packet_header; if (seek_increment > 0) { - if (file_seek(wth->fh, seek_increment, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, seek_increment, err, err_info)) return FALSE; } diff --git a/wiretap/netxray.c b/wiretap/netxray.c index 8d09a0e335..cf1d6fb41c 100644 --- a/wiretap/netxray.c +++ b/wiretap/netxray.c @@ -1061,7 +1061,7 @@ reread: /* * If there's extra stuff at the end of the record, skip it. */ - if (file_seek(wth->fh, padding, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, padding, err, err_info)) return FALSE; /* diff --git a/wiretap/peektagged.c b/wiretap/peektagged.c index 6d46e26fa9..943ca0e058 100644 --- a/wiretap/peektagged.c +++ b/wiretap/peektagged.c @@ -372,8 +372,9 @@ wtap_open_return_val peektagged_open(wtap *wth, int *err, gchar **err_info) } /* skip 8 zero bytes */ - if (file_seek (wth->fh, 8L, SEEK_CUR, err) == -1) - return WTAP_OPEN_NOT_MINE; + if (!wtap_read_bytes (wth->fh, NULL, 8, err, err_info)) { + return WTAP_OPEN_ERROR; + } /* * This is an Peek tagged file. diff --git a/wiretap/radcom.c b/wiretap/radcom.c index e7fc81946b..f86b1f3e23 100644 --- a/wiretap/radcom.c +++ b/wiretap/radcom.c @@ -144,8 +144,13 @@ wtap_open_return_val radcom_open(wtap *wth, int *err, gchar **err_info) return WTAP_OPEN_NOT_MINE; } - if (file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR, err) == -1) - return WTAP_OPEN_ERROR; + /* So what time is this? */ + if (!wtap_read_bytes(wth->fh, NULL, sizeof(struct frame_date), + err, err_info)) { + if (*err != WTAP_ERR_SHORT_READ) + return WTAP_OPEN_ERROR; + return WTAP_OPEN_NOT_MINE; + } for (;;) { if (!wtap_read_bytes(wth->fh, search_encap, 4, @@ -167,8 +172,11 @@ wtap_open_return_val radcom_open(wtap *wth, int *err, gchar **err_info) if (file_seek(wth->fh, -3, SEEK_CUR, err) == -1) return WTAP_OPEN_ERROR; } - if (file_seek(wth->fh, 12, SEEK_CUR, err) == -1) - return WTAP_OPEN_ERROR; + if (!wtap_read_bytes(wth->fh, NULL, 12, err, err_info)) { + if (*err != WTAP_ERR_SHORT_READ) + return WTAP_OPEN_ERROR; + return WTAP_OPEN_NOT_MINE; + } if (!wtap_read_bytes(wth->fh, search_encap, 4, err, err_info)) { if (*err != WTAP_ERR_SHORT_READ) return WTAP_OPEN_ERROR; @@ -220,13 +228,13 @@ wtap_open_return_val radcom_open(wtap *wth, int *err, gchar **err_info) #endif if (wth->file_encap == WTAP_ENCAP_ETHERNET) { - if (file_seek(wth->fh, 294, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, 294, err, err_info)) return WTAP_OPEN_ERROR; } else if (wth->file_encap == WTAP_ENCAP_LAPB) { - if (file_seek(wth->fh, 297, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, 297, err, err_info)) return WTAP_OPEN_ERROR; } else if (wth->file_encap == WTAP_ENCAP_ATM_RFC1483) { - if (file_seek(wth->fh, 504, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(wth->fh, NULL, 504, err, err_info)) return WTAP_OPEN_ERROR; } diff --git a/wiretap/snoop.c b/wiretap/snoop.c index 6939c6c752..1831e422c6 100644 --- a/wiretap/snoop.c +++ b/wiretap/snoop.c @@ -735,7 +735,7 @@ snoop_read_shomiti_wireless_pseudoheader(FILE_T fh, } /* Skip the header. */ rsize = ((int) whdr.pad[3]) - 8; - if (file_seek(fh, rsize, SEEK_CUR, err) == -1) + if (!wtap_read_bytes(fh, NULL, rsize, err, err_info)) return FALSE; memset(&pseudo_header->ieee_802_11, 0, sizeof(pseudo_header->ieee_802_11)); diff --git a/wiretap/vwr.c b/wiretap/vwr.c index 9088c60a54..b125759f2f 100644 --- a/wiretap/vwr.c +++ b/wiretap/vwr.c @@ -779,7 +779,7 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info) return UNKNOWN_FPGA; } else if (v_type != VT_FRAME) { - if (file_seek(wth->fh, f_len, SEEK_CUR, err) < 0) { + if (!wtap_read_bytes(wth->fh, NULL, f_len, err, err_info)) { g_free(rec); return -1; } -- cgit v1.2.1