Separate the body from the mind

This commit is contained in:
XANTRONIX Development 2019-01-30 01:27:04 -06:00
parent 7f2b6c9a36
commit ccf2b5ec7a

View file

@ -9,6 +9,10 @@
#include <hexagram/pcapng.h>
struct pcapinfo {
useconds_t last_time;
};
static void usage(int argc, char **argv, const char *message, ...) {
if (message) {
va_list args;
@ -26,17 +30,20 @@ static void usage(int argc, char **argv, const char *message, ...) {
static ssize_t handle_option(hexagram_pcapng_stream *stream,
uint32_t type,
uint16_t code,
uint16_t len) {
uint16_t len,
void *data) {
ssize_t readlen;
uint8_t data[65535];
uint8_t buf[65535];
if ((readlen = read(stream->fd, data, (size_t)len)) < 0) {
if ((readlen = read(stream->fd, buf, (size_t)len)) < 0) {
goto error_io;
}
if (type == HEXAGRAM_PCAPNG_BLOCK_IF) {
if (code == HEXAGRAM_PCAPNG_OPTION_IF_TSRESOL) {
stream->if_tsresol = data[0];
stream->if_tsresol = buf[0];
printf("Timestamp resolution is %d\n", buf[0]);
}
}
@ -52,7 +59,8 @@ error_io:
static ssize_t handle_block_if(hexagram_pcapng_stream *stream,
uint32_t type,
size_t len) {
size_t len,
void *data) {
hexagram_pcapng_if iface;
ssize_t readlen,
@ -72,7 +80,7 @@ static ssize_t handle_block_if(hexagram_pcapng_stream *stream,
total += readlen;
}
if ((readlen = hexagram_pcapng_read_options(stream, handle_option, type, remaining)) < 0) {
if ((readlen = hexagram_pcapng_read_options(stream, handle_option, type, remaining, data)) < 0) {
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
goto error_io;
@ -88,7 +96,8 @@ error_io:
static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
uint32_t type,
size_t len) {
size_t len,
struct pcapinfo *data) {
hexagram_pcapng_packet header;
uint8_t body[65535];
@ -122,6 +131,12 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
total += readlen;
}
if (data->last_time) {
usleep((useconds_t)header.timestamp[1] - data->last_time);
}
data->last_time = (useconds_t)header.timestamp[1];
printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32"\n",
header.caplen, header.timestamp[0], header.timestamp[1]);
@ -162,13 +177,14 @@ error_io:
static ssize_t handle_block(hexagram_pcapng_stream *stream,
uint32_t type,
size_t len) {
size_t len,
void *data) {
switch (type) {
case HEXAGRAM_PCAPNG_BLOCK_IF:
return handle_block_if(stream, type, len);
return handle_block_if(stream, type, len, data);
case HEXAGRAM_PCAPNG_BLOCK_PACKET:
return handle_block_packet(stream, type, len);
return handle_block_packet(stream, type, len, (struct pcapinfo *)data);
default: break;
}
@ -192,6 +208,10 @@ int main(int argc, char **argv) {
int fd;
hexagram_pcapng_stream *stream;
struct pcapinfo data = {
.last_time = 0
};
if (argc == 1) {
fd = fileno(stdin);
} else if (argc == 2) {
@ -209,7 +229,7 @@ int main(int argc, char **argv) {
goto error_pcapng_stream_open_fd;
}
if (hexagram_pcapng_stream_read(stream, handle_block) < 0) {
if (hexagram_pcapng_stream_read(stream, handle_block, &data) < 0) {
perror("hexagram_pcapng_stream_read()");
goto error_pcapng_stream_read;