From d386a9984b3e3ba30efafb4bfbac187c654fc26d Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 16 Dec 2018 11:37:04 -0600 Subject: [PATCH] Initial commit of examples/ --- examples/Makefile | 20 ++++++++++++ examples/pcapread.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/pcapread.c diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..b4daf89 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,20 @@ +include ../mk/build.mk + +CC = $(CROSS)cc + +INCLUDE_PATH = ../include + +CFLAGS += -I$(INCLUDE_PATH) +LDFLAGS = -L../src -lhexagram + +EXAMPLES = pcapread + +RM = /bin/rm + +all: $(EXAMPLES) + +$(EXAMPLES): %: %.c + $(CC) $(CFLAGS) ../src/libhexagram.a $< -o $@ + +clean: + $(RM) -f $(EXAMPLES) diff --git a/examples/pcapread.c b/examples/pcapread.c new file mode 100644 index 0000000..5ac1250 --- /dev/null +++ b/examples/pcapread.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static void usage(int argc, char **argv, const char *message, ...) { + if (message) { + va_list args; + + va_start(args, message); + vfprintf(stderr, message, args); + va_end(args); + } + + fprintf(stderr, "usage: %s [file.pcapng]\n", argv[0]); + + exit(1); +} + +static int handle_block(int fd, uint32_t type, size_t len) { + 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; + +error_io: + return -1; +} + +int main(int argc, char **argv) { + int fd, error; + + if (argc == 1) { + fd = fileno(stdin); + } else if (argc == 2) { + if ((fd = open(argv[1], O_RDONLY)) < 0) { + perror("open()"); + + goto error_open; + } + } else { + usage(argc, argv, NULL); + } + + if (hexagram_pcapng_stream_read(fd, handle_block, &error) < 0) { + perror("hexagram_pcapng_stream_read()"); + + goto error_io; + } + + if (argc == 2) { + close(fd); + } + + return 0; + +error_io: + if (argc == 2) { + close(fd); + } + +error_open: + return 1; +}