Well, it doesn't compile yet, but I don't want to lose it, either

This commit is contained in:
XANTRONIX Development 2019-01-21 08:51:30 -06:00
parent a9f5334524
commit 188bc92972
7 changed files with 76 additions and 21 deletions

View file

@ -23,7 +23,7 @@ static void usage(int argc, char **argv, const char *message, ...) {
exit(1); exit(1);
} }
static int handle_block(int fd, uint32_t type, size_t len) { static int handle_block(int fd, uint32_t type, size_t len, int *error) {
hexagram_pcapng_packet header; hexagram_pcapng_packet header;
uint8_t packet[65535]; uint8_t packet[65535];
@ -83,9 +83,13 @@ static int handle_block(int fd, uint32_t type, size_t len) {
goto error_io; goto error_io;
} }
*error = HEXAGRAM_PCAPNG_ERROR_OK;
return 0; return 0;
error_io: error_io:
*error = HEXAGRAM_PCAPNG_ERROR_IO;
return -1; return -1;
} }

View file

@ -11,20 +11,6 @@
#include <hexagram/pcapng/if.h> #include <hexagram/pcapng/if.h>
#include <hexagram/pcapng/if_stats.h> #include <hexagram/pcapng/if_stats.h>
#include <hexagram/pcapng/packet.h> #include <hexagram/pcapng/packet.h>
#include <hexagram/pcapng/stream.h>
/*
* pcapng stream reading facilities
*/
typedef struct _hexagram_pcapng_stream hexagram_pcapng_stream;
typedef int (hexagram_pcapng_block_handler)(int fd,
uint32_t type,
size_t length);
ssize_t hexagram_pcapng_stream_read(int fd,
hexagram_pcapng_block_handler *handler,
int *error);
#endif /* _HEXAGRAM_PCAPNG_H */ #endif /* _HEXAGRAM_PCAPNG_H */

View file

@ -20,4 +20,9 @@ typedef struct _hexagram_pcapng_block_footer {
uint32_t length; uint32_t length;
} hexagram_pcapng_block_footer; } hexagram_pcapng_block_footer;
typedef ssize_t (hexagram_pcapng_block_handler)(int fd,
uint32_t type,
size_t length,
int *error);
#endif /* _HEXAGRAM_PCAPNG_BLOCK_H */ #endif /* _HEXAGRAM_PCAPNG_BLOCK_H */

View file

@ -8,8 +8,7 @@ typedef enum {
HEXAGRAM_PCAPNG_ERROR_OK = 0, HEXAGRAM_PCAPNG_ERROR_OK = 0,
HEXAGRAM_PCAPNG_ERROR_IO = 1, HEXAGRAM_PCAPNG_ERROR_IO = 1,
HEXAGRAM_PCAPNG_ERROR_TRUNCATED = 2, HEXAGRAM_PCAPNG_ERROR_TRUNCATED = 2,
HEXAGRAM_PCAPNG_ERROR_FORMAT = 3, HEXAGRAM_PCAPNG_ERROR_FORMAT = 3
HEXAGRAM_PCAPNG_ERROR_HANDLER = 4
} hexagram_pcapng_error; } hexagram_pcapng_error;
#endif /* _HEXAGRAM_PCAPNG_ERROR_H */ #endif /* _HEXAGRAM_PCAPNG_ERROR_H */

View file

@ -16,4 +16,13 @@ typedef struct _hexagram_pcapng_option {
length; length;
} hexagram_pcapng_option; } hexagram_pcapng_option;
typedef ssize_t (hexagram_pcapng_option_handler)(int fd,
uint16_t code,
uint16_t length);
ssize_t hexagram_pcapng_option_read(int fd,
hexagram_pcapng_option_handler *handler,
size_t len,
int *error);
#endif /* _HEXAGRAM_PCAPNG_OPTION_H */ #endif /* _HEXAGRAM_PCAPNG_OPTION_H */

View file

@ -0,0 +1,13 @@
#ifndef _HEXAGRAM_PCAPNG_STREAM_H
#define _HEXAGRAM_PCAPNG_STREAM_H
/*
* pcapng stream reading facilities
*/
typedef struct _hexagram_pcapng_stream hexagram_pcapng_stream;
ssize_t hexagram_pcapng_stream_read(int fd,
hexagram_pcapng_block_handler *handler,
int *error);
#endif /* _HEXAGRAM_PCAPNG_STREAM_H */

View file

@ -15,6 +15,47 @@ struct _hexagram_pcapng_stream {
hexagram_pcapng_if_stats *stats; hexagram_pcapng_if_stats *stats;
}; };
ssize_t hexagram_pcapng_option_read(int fd,
hexagram_pcapng_option_handler *handler,
size_t len,
int *error) {
hexagram_pcapng_option header;
ssize_t total = 0;
while (total < len) {
size_t expected;
ssize_t readlen;
if ((readlen = read(fd, &header, sizeof(header))) < 0) {
*error = HEXAGRAM_PCAPNG_ERROR_IO;
goto error_io;
} else {
total += readlen;
}
expected = header.length + (header.length % sizeof(uint32_t));
if ((readlen = handler(fd, header.code, expected, error)) < 0) {
goto error_io;
} else if (readlen < expected) {
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
goto error_io;
} else {
total += readlen;
}
}
done:
*error = HEXAGRAM_PCAPNG_ERROR_OK;
return total;
error_io:
return -1;
}
hexagram_pcapng_stream *hexagram_pcapng_stream_open_fd(int fd) { hexagram_pcapng_stream *hexagram_pcapng_stream_open_fd(int fd) {
hexagram_pcapng_stream *stream; hexagram_pcapng_stream *stream;
@ -56,9 +97,7 @@ ssize_t hexagram_pcapng_stream_read(fd, handler, error)
total += readlen; total += readlen;
if (handler(fd, header.type, header.length - sizeof(header) - sizeof(footer)) < 0) { if (handler(fd, header.type, header.length - sizeof(header) - sizeof(footer), error) < 0) {
*error = HEXAGRAM_PCAPNG_ERROR_HANDLER;
goto error_io; goto error_io;
} }