hexagram/src/pcapng.c

92 lines
2 KiB
C
Raw Normal View History

#include <stdlib.h>
2018-12-16 02:11:53 -06:00
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
2018-12-16 02:15:33 -06:00
#include <hexagram/pcapng.h>
struct _hexagram_pcapng_stream {
int fd;
uint32_t if_count;
hexagram_pcapng_section *section;
hexagram_pcapng_if *ifs;
hexagram_pcapng_if_stats *stats;
};
hexagram_pcapng_stream *hexagram_pcapng_stream_open_fd(int fd) {
hexagram_pcapng_stream *stream;
if ((stream = malloc(sizeof(*stream))) == NULL) {
goto error_malloc_stream;
}
stream->if_count = 0;
return stream;
error_malloc_stream:
return NULL;
}
2018-12-16 02:11:53 -06:00
ssize_t hexagram_pcapng_stream_read(fd, handler, error)
int fd;
hexagram_pcapng_block_handler *handler;
int *error;
{
hexagram_pcapng_block_header header;
hexagram_pcapng_block_footer footer;
2018-12-16 02:11:53 -06:00
ssize_t total = 0;
while (1) {
ssize_t readlen;
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;
}
2018-12-16 02:26:13 -06:00
total += readlen;
2018-12-16 02:11:53 -06:00
2018-12-16 03:00:28 -06:00
if (handler(fd, header.type, header.length - sizeof(header) - sizeof(footer)) < 0) {
2018-12-16 02:11:53 -06:00
*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;
}
2018-12-16 03:00:28 -06:00
if (footer.length != header.length) {
*error = HEXAGRAM_PCAPNG_ERROR_FORMAT;
goto error_io;
}
2018-12-16 02:11:53 -06:00
}
*error = HEXAGRAM_PCAPNG_ERROR_OK;
2018-12-16 02:15:33 -06:00
done:
2018-12-16 02:11:53 -06:00
return total;
error_io:
return -1;
}