it's all just kinda clicking into place
This commit is contained in:
parent
188bc92972
commit
c7be44cd1c
3 changed files with 25 additions and 19 deletions
|
@ -23,7 +23,7 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
static int handle_block(int fd, uint32_t type, size_t len, int *error) {
|
||||
static ssize_t handle_block(int fd, uint32_t type, size_t len, int *error) {
|
||||
hexagram_pcapng_packet header;
|
||||
uint8_t packet[65535];
|
||||
|
||||
|
|
|
@ -18,7 +18,8 @@ typedef struct _hexagram_pcapng_option {
|
|||
|
||||
typedef ssize_t (hexagram_pcapng_option_handler)(int fd,
|
||||
uint16_t code,
|
||||
uint16_t length);
|
||||
uint16_t length,
|
||||
int *error);
|
||||
|
||||
ssize_t hexagram_pcapng_option_read(int fd,
|
||||
hexagram_pcapng_option_handler *handler,
|
||||
|
|
39
src/pcapng.c
39
src/pcapng.c
|
@ -23,7 +23,6 @@ ssize_t hexagram_pcapng_option_read(int fd,
|
|||
ssize_t total = 0;
|
||||
|
||||
while (total < len) {
|
||||
size_t expected;
|
||||
ssize_t readlen;
|
||||
|
||||
if ((readlen = read(fd, &header, sizeof(header))) < 0) {
|
||||
|
@ -34,20 +33,23 @@ ssize_t hexagram_pcapng_option_read(int fd,
|
|||
total += readlen;
|
||||
}
|
||||
|
||||
expected = header.length + (header.length % sizeof(uint32_t));
|
||||
|
||||
if ((readlen = handler(fd, header.code, expected, error)) < 0) {
|
||||
if ((readlen = handler(fd, header.code, header.length, error)) < 0) {
|
||||
goto error_io;
|
||||
} else if (readlen < expected) {
|
||||
} else if (readlen < header.length) {
|
||||
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
||||
|
||||
goto error_io;
|
||||
} else {
|
||||
total += readlen;
|
||||
}
|
||||
|
||||
if ((readlen = lseek(fd, header.length % sizeof(uint32_t), SEEK_CUR)) < 0) {
|
||||
goto error_io;
|
||||
} else {
|
||||
total += readlen;
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
*error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||
|
||||
return total;
|
||||
|
@ -71,16 +73,15 @@ error_malloc_stream:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
ssize_t hexagram_pcapng_stream_read(fd, handler, error)
|
||||
int fd;
|
||||
hexagram_pcapng_block_handler *handler;
|
||||
int *error;
|
||||
{
|
||||
ssize_t hexagram_pcapng_stream_read(int fd,
|
||||
hexagram_pcapng_block_handler *handler,
|
||||
int *error) {
|
||||
hexagram_pcapng_block_header header;
|
||||
hexagram_pcapng_block_footer footer;
|
||||
ssize_t total = 0;
|
||||
|
||||
while (1) {
|
||||
size_t expected;
|
||||
ssize_t readlen;
|
||||
|
||||
if ((readlen = read(fd, &header, sizeof(header))) < 0) {
|
||||
|
@ -93,12 +94,16 @@ ssize_t hexagram_pcapng_stream_read(fd, handler, error)
|
|||
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
||||
|
||||
goto error_io;
|
||||
} else {
|
||||
total += readlen;
|
||||
}
|
||||
|
||||
total += readlen;
|
||||
expected = header.length - sizeof(header) - sizeof(footer);
|
||||
|
||||
if (handler(fd, header.type, header.length - sizeof(header) - sizeof(footer), error) < 0) {
|
||||
if ((readlen = handler(fd, header.type, expected, error)) < 0) {
|
||||
goto error_io;
|
||||
} else {
|
||||
total += readlen;
|
||||
}
|
||||
|
||||
if ((readlen = read(fd, &footer, sizeof(footer))) < 0) {
|
||||
|
@ -111,18 +116,18 @@ ssize_t hexagram_pcapng_stream_read(fd, handler, error)
|
|||
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
||||
|
||||
goto error_io;
|
||||
}
|
||||
|
||||
if (footer.length != header.length) {
|
||||
} else if (footer.length != header.length) {
|
||||
*error = HEXAGRAM_PCAPNG_ERROR_FORMAT;
|
||||
|
||||
goto error_io;
|
||||
} else {
|
||||
total += readlen;
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
*error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||
|
||||
done:
|
||||
return total;
|
||||
|
||||
error_io:
|
||||
|
|
Loading…
Add table
Reference in a new issue