From a9f53345240036b9961d108755d7331bbebebfdd Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 20 Jan 2019 15:55:40 -0600 Subject: [PATCH] Just trundling along --- examples/pcapread.c | 56 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/examples/pcapread.c b/examples/pcapread.c index 5ac1250..d3f1fd5 100644 --- a/examples/pcapread.c +++ b/examples/pcapread.c @@ -24,10 +24,60 @@ static void usage(int argc, char **argv, const char *message, ...) { } static int handle_block(int fd, uint32_t type, size_t len) { - printf("Read block type %08"PRIx32" len %zu\n", - type, len); + hexagram_pcapng_packet header; + uint8_t packet[65535]; - if (lseek(fd, len, SEEK_CUR) < 0) { + ssize_t readlen, + remaining = len; + + /* + * If we've received anything other than a packet block, then seek past + * that data and move on. + */ + if (type != HEXAGRAM_PCAPNG_BLOCK_PACKET) { + printf("Read block type %08"PRIx32" len %zu\n", + type, len); + + if (lseek(fd, len, SEEK_CUR) < 0) { + perror("lseek()"); + + goto error_io; + } + + return 0; + } + + /* + * Otherwise, read the packet block header so that we may determine the + * size of the payload to continue to read. + */ + if ((readlen = read(fd, &header, sizeof(header))) < 0) { + perror("read()"); + + goto error_io; + } else { + remaining -= readlen; + } + + /* + * Read the packet data into our scratchpad. + */ + if ((readlen = read(fd, &packet, header.caplen)) < 0) { + perror("read()"); + + goto error_io; + } else { + remaining -= readlen; + } + + printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32"\n", + header.caplen, header.timestamp[0], header.timestamp[1]); + + /* + * The remaining data here should be pcapng option values, and since we do + * not presently require them, we can safely seek past them. + */ + if (lseek(fd, remaining, SEEK_CUR) < 0) { perror("lseek()"); goto error_io;