Initial commit
This commit is contained in:
commit
8a202ebc6d
5 changed files with 264 additions and 0 deletions
11
Makefile
Normal file
11
Makefile
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
all:
|
||||||
|
$(MAKE) -C src all
|
||||||
|
$(MAKE) -C examples all
|
||||||
|
|
||||||
|
install:
|
||||||
|
$(MAKE) -C src install
|
||||||
|
$(MAKE) -C examples install
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(MAKE) -C src clean
|
||||||
|
$(MAKE) -C examples clean
|
100
configure
vendored
Executable file
100
configure
vendored
Executable file
|
@ -0,0 +1,100 @@
|
||||||
|
#! /bin/sh
|
||||||
|
|
||||||
|
OS=`uname -s`
|
||||||
|
DEBUG=0
|
||||||
|
|
||||||
|
create_linux_config_h() {
|
||||||
|
cat <<EOF > src/config.h
|
||||||
|
#ifndef _CONFIG_H
|
||||||
|
#define _CONFIG_H
|
||||||
|
#include <endian.h>
|
||||||
|
#endif /* _CONFIG_H */
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
create_darwin_config_h() {
|
||||||
|
cat <<EOF > src/config.h
|
||||||
|
#ifndef _CONFIG_H
|
||||||
|
#define _CONFIG_H
|
||||||
|
#include <architecture/byte_order.h>
|
||||||
|
|
||||||
|
#ifdef __LITTLE_ENDIAN__
|
||||||
|
#define __DO_SWAP_BYTES
|
||||||
|
#endif /* _DO_SWAP_BYTES */
|
||||||
|
#endif /* _CONFIG_H */
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
create_common_build_mk() {
|
||||||
|
if [ "$DEBUG" = 1 ]; then
|
||||||
|
cat <<'EOF' > mk/build.mk
|
||||||
|
CGFLAGS = -g -fno-inline
|
||||||
|
EOF
|
||||||
|
else
|
||||||
|
cat <<'EOF' > mk/build.mk
|
||||||
|
CGFLAGS =
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat <<'EOF' >> mk/build.mk
|
||||||
|
CWFLAGS = -Wall
|
||||||
|
COFLAGS = -O2
|
||||||
|
CFLAGS = $(CGFLAGS) $(CWFLAGS) $(COFLAGS)
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
create_linux_build_mk() {
|
||||||
|
create_common_build_mk $@
|
||||||
|
|
||||||
|
cat <<'EOF' >> mk/build.mk
|
||||||
|
LLFLAGS = -shared -Wl,-soname=$(SONAME)
|
||||||
|
|
||||||
|
STATIC = lib$(LIBNAME).a
|
||||||
|
|
||||||
|
SONAME_SHORT = lib$(LIBNAME).so
|
||||||
|
SONAME = $(SONAME_SHORT).$(VERSION_MAJOR)
|
||||||
|
SONAME_FULL = $(SONAME_SHORT).$(VERSION)
|
||||||
|
|
||||||
|
PREFIX = /usr/local
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
create_darwin_build_mk() {
|
||||||
|
create_common_build_mk $@
|
||||||
|
|
||||||
|
cat <<'EOF' >> mk/build.mk
|
||||||
|
LLFLAGS = -dynamiclib -current_version $(VERSION)
|
||||||
|
|
||||||
|
STATIC = lib$(LIBNAME).a
|
||||||
|
|
||||||
|
SONAME_SHORT = lib$(LIBNAME).dylib
|
||||||
|
SONAME = lib$(LIBNAME).$(VERSION_MAJOR).dylib
|
||||||
|
SONAME_FULL = lib$(LIBNAME).$(VERSION).dylib
|
||||||
|
|
||||||
|
PREFIX = /usr/local
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
for arg in $@; do
|
||||||
|
case $arg in
|
||||||
|
"--enable-debug")
|
||||||
|
DEBUG=1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ ! -d "mk" ]; then
|
||||||
|
mkdir -m 0755 mk
|
||||||
|
fi
|
||||||
|
|
||||||
|
case $OS in
|
||||||
|
Linux)
|
||||||
|
create_linux_config_h
|
||||||
|
create_linux_build_mk
|
||||||
|
;;
|
||||||
|
|
||||||
|
Darwin)
|
||||||
|
create_darwin_config_h
|
||||||
|
create_darwin_build_mk
|
||||||
|
;;
|
||||||
|
esac
|
30
include/hexagram/pcapng.h
Normal file
30
include/hexagram/pcapng.h
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef _HEXAGRAM_PCAPNG_H
|
||||||
|
#define _HEXAGRAM_PCAPNG_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
enum {
|
||||||
|
HEXAGRAM_PCAPNG_ERROR_OK = 0,
|
||||||
|
HEXAGRAM_PCAPNG_ERROR_IO = 1,
|
||||||
|
HEXAGRAM_PCAPNG_ERROR_TRUNCATED = 2,
|
||||||
|
HEXAGRAM_PCAPNG_ERROR_HANDLER = 3
|
||||||
|
} hexagram_pcapng_error;
|
||||||
|
|
||||||
|
typedef struct _hexagram_pcapng_header {
|
||||||
|
uint32_t type,
|
||||||
|
length;
|
||||||
|
} hexagram_pcapng_header;
|
||||||
|
|
||||||
|
typedef struct _hexagram_pcapng_footer {
|
||||||
|
uint32_t length;
|
||||||
|
} hexagram_pcapng_footer;
|
||||||
|
|
||||||
|
typedef int (hexagram_pcapng_block_handler)(int fd,
|
||||||
|
uint32_t type,
|
||||||
|
uint32_t length);
|
||||||
|
|
||||||
|
ssize_t hexagram_pcapng_stream_read(int fd,
|
||||||
|
hexagram_pcapng_block_handler *handler,
|
||||||
|
int *error);
|
||||||
|
|
||||||
|
#endif /* _HEXAGRAM_PCAPNG_H */
|
58
src/Makefile
Normal file
58
src/Makefile
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
include ../mk/build.mk
|
||||||
|
|
||||||
|
INCLUDE_PATH = ../include
|
||||||
|
HEADER_SUBDIR = hexagram
|
||||||
|
|
||||||
|
CC = $(CROSS)cc
|
||||||
|
CFLAGS += -fPIC -I$(INCLUDE_PATH)
|
||||||
|
LDFLAGS =
|
||||||
|
|
||||||
|
HEADERS = pcapng.h
|
||||||
|
|
||||||
|
OBJS = pcapng.o
|
||||||
|
|
||||||
|
VERSION_MAJOR = 0
|
||||||
|
VERSION_MINOR = 0.1
|
||||||
|
VERSION = $(VERSION_MAJOR).$(VERSION_MINOR)
|
||||||
|
|
||||||
|
LIBNAME = hexagram
|
||||||
|
|
||||||
|
HEADERS_BUILD = $(addprefix $(INCLUDE_PATH)/$(HEADER_SUBDIR)/, $(HEADERS))
|
||||||
|
|
||||||
|
AR = $(CROSS)ar
|
||||||
|
RANLIB = $(CROSS)ranlib
|
||||||
|
|
||||||
|
RM = /bin/rm
|
||||||
|
LN = /bin/ln
|
||||||
|
RMDIR = /bin/rmdir
|
||||||
|
INSTALL = /usr/bin/install
|
||||||
|
|
||||||
|
all: $(STATIC) $(SONAME_FULL) $(SONAME) $(SONAME_SHORT)
|
||||||
|
|
||||||
|
$(STATIC): $(OBJS)
|
||||||
|
$(AR) rc $(STATIC) $(OBJS)
|
||||||
|
$(RANLIB) $(STATIC)
|
||||||
|
|
||||||
|
$(SONAME_FULL): $(OBJS)
|
||||||
|
$(CC) $(LLFLAGS) $(OBJS) $(LDFLAGS) -o $(SONAME_FULL)
|
||||||
|
|
||||||
|
$(SONAME): $(SONAME_FULL)
|
||||||
|
$(LN) -s $< $@
|
||||||
|
|
||||||
|
$(SONAME_SHORT): $(SONAME_FULL)
|
||||||
|
$(LN) -s $< $@
|
||||||
|
|
||||||
|
$(OBJS): %.o: %.c $(HEADERS_BUILD)
|
||||||
|
$(CC) $(CFLAGS) -c $<
|
||||||
|
|
||||||
|
install: $(SONAME_FULL) $(STATIC)
|
||||||
|
$(INSTALL) -d -m 0755 $(PREFIX)/lib
|
||||||
|
$(INSTALL) -c -m 0644 $(STATIC) $(PREFIX)/lib
|
||||||
|
$(INSTALL) -c -m 0755 $(SONAME_FULL) $(PREFIX)/lib
|
||||||
|
$(LN) -s -f $(SONAME_FULL) $(PREFIX)/lib/$(SONAME)
|
||||||
|
$(LN) -s -f $(SONAME_FULL) $(PREFIX)/lib/$(SONAME_SHORT)
|
||||||
|
$(INSTALL) -d -m 0755 $(PREFIX)/include/$(HEADER_SUBDIR)
|
||||||
|
$(INSTALL) -c -m 0644 $(HEADERS_BUILD) $(PREFIX)/include/$(HEADER_SUBDIR)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) -f $(SONAME_SHORT) $(SONAME) $(SONAME_FULL) $(STATIC) $(OBJS)
|
65
src/pcapng.c
Normal file
65
src/pcapng.c
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
ssize_t hexagram_pcapng_stream_read(fd, handler, error)
|
||||||
|
int fd;
|
||||||
|
hexagram_pcapng_block_handler *handler;
|
||||||
|
int *error;
|
||||||
|
{
|
||||||
|
hexagram_pcapng_header header;
|
||||||
|
hexagram_pcapng_footer footer;
|
||||||
|
ssize_t total = 0;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
ssize_t readlen;
|
||||||
|
size_t bodylen;
|
||||||
|
off_t seeklen;
|
||||||
|
|
||||||
|
if ((readlen = read(fd, &header, sizeof(header))) < 0) {
|
||||||
|
*error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
} else if (readlen == 0) {
|
||||||
|
goto done;
|
||||||
|
} else if (readlen < sizeof(header)) {
|
||||||
|
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
|
|
||||||
|
total += readlen;
|
||||||
|
bodylen = header.length - sizeof(header) - sizeof(footer);
|
||||||
|
|
||||||
|
if (handler(fd, header.type, header.length) < 0) {
|
||||||
|
*error = HEXAGRAM_PCAPNG_ERROR_HANDLER;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((readlen = read(fd, &footer, sizeof(footer))) < 0) {
|
||||||
|
*error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
} else if (readlen == 0) {
|
||||||
|
goto done;
|
||||||
|
} else if (readlen < sizeof(footer)) {
|
||||||
|
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||||
|
|
||||||
|
return total;
|
||||||
|
|
||||||
|
error_io:
|
||||||
|
return -1;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue