From bb0363a1a281f5863da7588148b9d6291cc55b4a Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 24 Jan 2019 01:55:31 -0600 Subject: [PATCH] My girl would be so proud of me --- examples/pcapread.c | 111 +++++++++++++++++++++++++++++++------------- 1 file changed, 80 insertions(+), 31 deletions(-) diff --git a/examples/pcapread.c b/examples/pcapread.c index 468695f..7bcb84c 100644 --- a/examples/pcapread.c +++ b/examples/pcapread.c @@ -23,10 +23,10 @@ static void usage(int argc, char **argv, const char *message, ...) { exit(1); } -static ssize_t handle_options(hexagram_pcapng_stream *stream, - uint32_t type, - uint16_t code, - uint16_t len) { +static ssize_t handle_option(hexagram_pcapng_stream *stream, + uint32_t type, + uint16_t code, + uint16_t len) { ssize_t readlen; uint8_t data[65535]; @@ -50,40 +50,59 @@ error_io: return -1; } -static ssize_t handle_block(hexagram_pcapng_stream *stream, - uint32_t type, - size_t len) { - hexagram_pcapng_packet header; - uint8_t body[65535]; +static ssize_t handle_block_if(hexagram_pcapng_stream *stream, + uint32_t type, + size_t len) { + hexagram_pcapng_if iface; + + ssize_t readlen, + total = 0; size_t remaining = len; - ssize_t total = 0, - readlen; - /* - * If we've received anything other than a packet block, then seek past - * that data and move on. + * Read the interface block header. */ - if (type != HEXAGRAM_PCAPNG_BLOCK_PACKET) { - printf("Read block type %08"PRIx32" len %zu\n", - type, len); + if ((readlen = read(stream->fd, &iface, sizeof(iface))) < 0) { + stream->error = HEXAGRAM_PCAPNG_ERROR_IO; - if ((readlen = lseek(stream->fd, len, SEEK_CUR)) < 0) { - perror("lseek()"); - - goto error_io; - } - - return readlen; + goto error_io; + } else { + remaining -= readlen; + total += readlen; } + if ((readlen = hexagram_pcapng_read_options(stream, handle_option, type, remaining)) < 0) { + stream->error = HEXAGRAM_PCAPNG_ERROR_IO; + + goto error_io; + } else { + total += readlen; + } + + return total; + +error_io: + return -1; +} + +static ssize_t handle_block_packet(hexagram_pcapng_stream *stream, + uint32_t type, + size_t len) { + hexagram_pcapng_packet header; + uint8_t body[65535]; + + ssize_t readlen, + total = 0; + + size_t remaining = len; + /* - * Otherwise, read the packet block header so that we may determine the - * size of the payload to continue to read. + * Read the packet block header so that we may determine the size of the + * payload to continue to read. */ if ((readlen = read(stream->fd, &header, sizeof(header))) < 0) { - perror("read()"); + stream->error = HEXAGRAM_PCAPNG_ERROR_IO; goto error_io; } else { @@ -95,7 +114,7 @@ static ssize_t handle_block(hexagram_pcapng_stream *stream, * Read the packet body into our scratchpad. */ if ((readlen = read(stream->fd, &body, header.caplen)) < 0) { - perror("read()"); + stream->error = HEXAGRAM_PCAPNG_ERROR_IO; goto error_io; } else { @@ -110,7 +129,7 @@ static ssize_t handle_block(hexagram_pcapng_stream *stream, size_t padding = sizeof(uint32_t) - (header.caplen % sizeof(uint32_t)); if (lseek(stream->fd, padding, SEEK_CUR) < 0) { - perror("lseek()"); + stream->error = HEXAGRAM_PCAPNG_ERROR_IO; goto error_io; } else { @@ -123,10 +142,12 @@ static ssize_t handle_block(hexagram_pcapng_stream *stream, * The remaining data here should be pcapng option values, and since we do * not presently require them, we can safely seek past them. */ - if ((readlen = hexagram_pcapng_read_options(stream, handle_options, type, remaining)) < 0) { + if (lseek(stream->fd, remaining, SEEK_CUR) < 0) { + stream->error = HEXAGRAM_PCAPNG_ERROR_IO; + goto error_io; } else { - total += readlen; + total += remaining; } stream->error = HEXAGRAM_PCAPNG_ERROR_OK; @@ -139,6 +160,34 @@ error_io: return -1; } +static ssize_t handle_block(hexagram_pcapng_stream *stream, + uint32_t type, + size_t len) { + switch (type) { + case HEXAGRAM_PCAPNG_BLOCK_IF: + return handle_block_if(stream, type, len); + + case HEXAGRAM_PCAPNG_BLOCK_PACKET: + return handle_block_packet(stream, type, len); + + default: break; + } + + printf("Read block type %08"PRIx32" len %zu\n", + type, len); + + if (lseek(stream->fd, len, SEEK_CUR) < 0) { + stream->error = HEXAGRAM_PCAPNG_ERROR_IO; + + goto error_io; + } + + return len; + +error_io: + return -1; +} + int main(int argc, char **argv) { int fd; hexagram_pcapng_stream *stream;