Initial commit of examples/
This commit is contained in:
parent
b9eff396ce
commit
d386a9984b
2 changed files with 96 additions and 0 deletions
20
examples/Makefile
Normal file
20
examples/Makefile
Normal file
|
@ -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)
|
76
examples/pcapread.c
Normal file
76
examples/pcapread.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <inttypes.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <hexagram/pcapng.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Add table
Reference in a new issue