diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2014-03-11 15:12:26 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2014-03-11 15:12:26 +0100 |
commit | 632e1a699e5d1d3edb822b8f55c0b55b9c7e161e (patch) | |
tree | 36dc1d9856fee27050eec85135d7568f51b41893 | |
parent | e1af615aca2fa6e5927645eaa944db6fa89f61a4 (diff) |
pcap_io: Only check byteswapped linktype if pcap magic is swapped
In pcap_validate_header() the linktype and the byte-swapped linktype are
currently checked against the supported linktypes. Since the swapped
linktype is always larger than LINKTYPE_MAX, only one of the two tests
was actually done. Make this intention a bit more clear by explicitly
checking only for either the swapped or non-swapped linktype, depending
on whether the pcap magic is swapped.
Also make the error messages a bit more verbose regarding the
major/minor version.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r-- | pcap_io.h | 21 |
1 files changed, 8 insertions, 13 deletions
@@ -660,27 +660,22 @@ static const bool pcap_supported_linktypes[LINKTYPE_MAX] __maybe_unused = { static inline void pcap_validate_header(const struct pcap_filehdr *hdr) { bool good = false; - uint32_t linktype_swab = bswap_32(hdr->linktype); + uint32_t linktype; + pcap_check_magic(hdr->magic); - if (hdr->linktype < LINKTYPE_MAX) { - if (pcap_supported_linktypes[hdr->linktype]) - good = true; - } - - if (linktype_swab < LINKTYPE_MAX) { - if (pcap_supported_linktypes[linktype_swab]) - good = true; - } + linktype = pcap_magic_is_swapped(hdr->magic) ? bswap_32(hdr->linktype) : hdr->linktype; + if (linktype < LINKTYPE_MAX) + good = pcap_supported_linktypes[linktype]; if (!good) - panic("This file has an unsupported pcap link type (%d)!\n", hdr->linktype); + panic("This file has an unsupported pcap link type (%d)!\n", linktype); if (unlikely(hdr->version_major != PCAP_VERSION_MAJOR) && ___constant_swab16(hdr->version_major) != PCAP_VERSION_MAJOR) - panic("This file has not a valid pcap header\n"); + panic("This file has an invalid pcap major version (must be %d)\n", PCAP_VERSION_MAJOR); if (unlikely(hdr->version_minor != PCAP_VERSION_MINOR) && ___constant_swab16(hdr->version_minor) != PCAP_VERSION_MINOR) - panic("This file has not a valid pcap header\n"); + panic("This file has an invalid pcap minor version (must be %d)\n", PCAP_VERSION_MINOR); } static int pcap_generic_pull_fhdr(int fd, uint32_t *magic, |