66 lines
1.5 KiB
C
66 lines
1.5 KiB
C
|
#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;
|
||
|
}
|