I really just want to make my beautiful lady so proud of me
This commit is contained in:
parent
c7be44cd1c
commit
d97799cbe0
6 changed files with 108 additions and 74 deletions
|
@ -23,9 +23,11 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t handle_block(int fd, uint32_t type, size_t len, int *error) {
|
static ssize_t handle_block(hexagram_pcapng_stream *stream,
|
||||||
|
uint32_t type,
|
||||||
|
size_t len) {
|
||||||
hexagram_pcapng_packet header;
|
hexagram_pcapng_packet header;
|
||||||
uint8_t packet[65535];
|
uint8_t body[65535];
|
||||||
|
|
||||||
ssize_t readlen,
|
ssize_t readlen,
|
||||||
remaining = len;
|
remaining = len;
|
||||||
|
@ -38,7 +40,7 @@ static ssize_t handle_block(int fd, uint32_t type, size_t len, int *error) {
|
||||||
printf("Read block type %08"PRIx32" len %zu\n",
|
printf("Read block type %08"PRIx32" len %zu\n",
|
||||||
type, len);
|
type, len);
|
||||||
|
|
||||||
if (lseek(fd, len, SEEK_CUR) < 0) {
|
if (lseek(stream->fd, len, SEEK_CUR) < 0) {
|
||||||
perror("lseek()");
|
perror("lseek()");
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
|
@ -51,7 +53,7 @@ static ssize_t handle_block(int fd, uint32_t type, size_t len, int *error) {
|
||||||
* Otherwise, read the packet block header so that we may determine the
|
* Otherwise, read the packet block header so that we may determine the
|
||||||
* size of the payload to continue to read.
|
* size of the payload to continue to read.
|
||||||
*/
|
*/
|
||||||
if ((readlen = read(fd, &header, sizeof(header))) < 0) {
|
if ((readlen = read(stream->fd, &header, sizeof(header))) < 0) {
|
||||||
perror("read()");
|
perror("read()");
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
|
@ -60,9 +62,9 @@ static ssize_t handle_block(int fd, uint32_t type, size_t len, int *error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read the packet data into our scratchpad.
|
* Read the packet body into our scratchpad.
|
||||||
*/
|
*/
|
||||||
if ((readlen = read(fd, &packet, header.caplen)) < 0) {
|
if ((readlen = read(stream->fd, &body, header.caplen)) < 0) {
|
||||||
perror("read()");
|
perror("read()");
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
|
@ -77,24 +79,25 @@ static ssize_t handle_block(int fd, uint32_t type, size_t len, int *error) {
|
||||||
* The remaining data here should be pcapng option values, and since we do
|
* The remaining data here should be pcapng option values, and since we do
|
||||||
* not presently require them, we can safely seek past them.
|
* not presently require them, we can safely seek past them.
|
||||||
*/
|
*/
|
||||||
if (lseek(fd, remaining, SEEK_CUR) < 0) {
|
if (lseek(stream->fd, remaining, SEEK_CUR) < 0) {
|
||||||
perror("lseek()");
|
perror("lseek()");
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
}
|
}
|
||||||
|
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_OK;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_io:
|
error_io:
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_IO;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int fd, error;
|
int fd;
|
||||||
|
hexagram_pcapng_stream *stream;
|
||||||
|
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
fd = fileno(stdin);
|
fd = fileno(stdin);
|
||||||
|
@ -106,21 +109,31 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
usage(argc, argv, NULL);
|
usage(argc, argv, NULL);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hexagram_pcapng_stream_read(fd, handle_block, &error) < 0) {
|
if ((stream = hexagram_pcapng_stream_open_fd(fd)) < 0) {
|
||||||
|
goto error_pcapng_stream_open_fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hexagram_pcapng_stream_read(stream, handle_block) < 0) {
|
||||||
perror("hexagram_pcapng_stream_read()");
|
perror("hexagram_pcapng_stream_read()");
|
||||||
|
|
||||||
goto error_io;
|
goto error_pcapng_stream_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hexagram_pcapng_stream_destroy(stream);
|
||||||
|
|
||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_io:
|
error_pcapng_stream_read:
|
||||||
|
hexagram_pcapng_stream_destroy(stream);
|
||||||
|
|
||||||
|
error_pcapng_stream_open_fd:
|
||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,28 @@
|
||||||
#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>
|
|
||||||
|
typedef struct _hexagram_pcapng_stream {
|
||||||
|
int fd,
|
||||||
|
error;
|
||||||
|
|
||||||
|
uint8_t if_tsresol;
|
||||||
|
} hexagram_pcapng_stream;
|
||||||
|
|
||||||
|
typedef ssize_t (hexagram_pcapng_block_handler)(hexagram_pcapng_stream *,
|
||||||
|
uint32_t type,
|
||||||
|
size_t length);
|
||||||
|
|
||||||
|
typedef ssize_t (hexagram_pcapng_option_handler)(hexagram_pcapng_stream *,
|
||||||
|
hexagram_pcapng_block_header *,
|
||||||
|
uint16_t code,
|
||||||
|
uint16_t length);
|
||||||
|
|
||||||
|
hexagram_pcapng_stream *hexagram_pcapng_stream_open_fd(int fd);
|
||||||
|
|
||||||
|
void hexagram_pcapng_stream_destroy(hexagram_pcapng_stream *stream);
|
||||||
|
|
||||||
|
ssize_t hexagram_pcapng_stream_read(hexagram_pcapng_stream *stream,
|
||||||
|
hexagram_pcapng_block_handler *handler);
|
||||||
|
|
||||||
#endif /* _HEXAGRAM_PCAPNG_H */
|
#endif /* _HEXAGRAM_PCAPNG_H */
|
||||||
|
|
|
@ -20,9 +20,4 @@ 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 */
|
||||||
|
|
|
@ -16,14 +16,4 @@ 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,
|
|
||||||
int *error);
|
|
||||||
|
|
||||||
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 */
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
#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 */
|
|
91
src/pcapng.c
91
src/pcapng.c
|
@ -1,5 +1,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
@ -7,50 +8,69 @@
|
||||||
|
|
||||||
#include <hexagram/pcapng.h>
|
#include <hexagram/pcapng.h>
|
||||||
|
|
||||||
struct _hexagram_pcapng_stream {
|
static ssize_t handle_option(hexagram_pcapng_stream *stream,
|
||||||
int fd;
|
hexagram_pcapng_block_header *header,
|
||||||
uint32_t if_count;
|
uint16_t code,
|
||||||
hexagram_pcapng_section *section;
|
uint16_t len) {
|
||||||
hexagram_pcapng_if *ifs;
|
ssize_t readlen;
|
||||||
hexagram_pcapng_if_stats *stats;
|
uint8_t data[65535];
|
||||||
};
|
|
||||||
|
|
||||||
ssize_t hexagram_pcapng_option_read(int fd,
|
if ((readlen = read(stream->fd, data, (size_t)len)) < 0) {
|
||||||
hexagram_pcapng_option_handler *handler,
|
goto error_io;
|
||||||
size_t len,
|
}
|
||||||
int *error) {
|
|
||||||
hexagram_pcapng_option header;
|
if (header->type == HEXAGRAM_PCAPNG_BLOCK_IF) {
|
||||||
|
if (code == HEXAGRAM_PCAPNG_OPTION_IF_TSRESOL) {
|
||||||
|
stream->if_tsresol = data[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||||
|
|
||||||
|
return readlen;
|
||||||
|
|
||||||
|
error_io:
|
||||||
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t read_options(hexagram_pcapng_stream *stream,
|
||||||
|
hexagram_pcapng_block_header *header,
|
||||||
|
hexagram_pcapng_option_handler *handler,
|
||||||
|
size_t len) {
|
||||||
|
hexagram_pcapng_option option;
|
||||||
ssize_t total = 0;
|
ssize_t total = 0;
|
||||||
|
|
||||||
while (total < len) {
|
while (total < len) {
|
||||||
ssize_t readlen;
|
ssize_t readlen;
|
||||||
|
|
||||||
if ((readlen = read(fd, &header, sizeof(header))) < 0) {
|
if ((readlen = read(stream->fd, &option, sizeof(option))) < 0) {
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_IO;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
total += readlen;
|
total += readlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((readlen = handler(fd, header.code, header.length, error)) < 0) {
|
if ((readlen = handler(stream, header, option.code, option.length)) < 0) {
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else if (readlen < header.length) {
|
} else if (readlen < option.length) {
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
total += readlen;
|
total += readlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((readlen = lseek(fd, header.length % sizeof(uint32_t), SEEK_CUR)) < 0) {
|
if ((readlen = lseek(stream->fd, option.length % sizeof(uint32_t), SEEK_CUR)) < 0) {
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
total += readlen;
|
total += readlen;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_OK;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
|
|
||||||
|
@ -65,7 +85,9 @@ hexagram_pcapng_stream *hexagram_pcapng_stream_open_fd(int fd) {
|
||||||
goto error_malloc_stream;
|
goto error_malloc_stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
stream->if_count = 0;
|
stream->fd = fd;
|
||||||
|
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||||
|
stream->if_tsresol = 0;
|
||||||
|
|
||||||
return stream;
|
return stream;
|
||||||
|
|
||||||
|
@ -73,9 +95,14 @@ error_malloc_stream:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t hexagram_pcapng_stream_read(int fd,
|
void hexagram_pcapng_stream_destroy(hexagram_pcapng_stream *stream) {
|
||||||
hexagram_pcapng_block_handler *handler,
|
memset(stream, '\0', sizeof(*stream));
|
||||||
int *error) {
|
|
||||||
|
free(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t hexagram_pcapng_stream_read(hexagram_pcapng_stream *stream,
|
||||||
|
hexagram_pcapng_block_handler *handler) {
|
||||||
hexagram_pcapng_block_header header;
|
hexagram_pcapng_block_header header;
|
||||||
hexagram_pcapng_block_footer footer;
|
hexagram_pcapng_block_footer footer;
|
||||||
ssize_t total = 0;
|
ssize_t total = 0;
|
||||||
|
@ -84,14 +111,14 @@ ssize_t hexagram_pcapng_stream_read(int fd,
|
||||||
size_t expected;
|
size_t expected;
|
||||||
ssize_t readlen;
|
ssize_t readlen;
|
||||||
|
|
||||||
if ((readlen = read(fd, &header, sizeof(header))) < 0) {
|
if ((readlen = read(stream->fd, &header, sizeof(header))) < 0) {
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_IO;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else if (readlen == 0) {
|
} else if (readlen == 0) {
|
||||||
goto done;
|
goto done;
|
||||||
} else if (readlen < sizeof(header)) {
|
} else if (readlen < sizeof(header)) {
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
|
@ -100,24 +127,24 @@ ssize_t hexagram_pcapng_stream_read(int fd,
|
||||||
|
|
||||||
expected = header.length - sizeof(header) - sizeof(footer);
|
expected = header.length - sizeof(header) - sizeof(footer);
|
||||||
|
|
||||||
if ((readlen = handler(fd, header.type, expected, error)) < 0) {
|
if ((readlen = handler(stream, header.type, expected)) < 0) {
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
total += readlen;
|
total += readlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((readlen = read(fd, &footer, sizeof(footer))) < 0) {
|
if ((readlen = read(stream->fd, &footer, sizeof(footer))) < 0) {
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_IO;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else if (readlen == 0) {
|
} else if (readlen == 0) {
|
||||||
goto done;
|
goto done;
|
||||||
} else if (readlen < sizeof(footer)) {
|
} else if (readlen < sizeof(footer)) {
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_TRUNCATED;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else if (footer.length != header.length) {
|
} else if (footer.length != header.length) {
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_FORMAT;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_FORMAT;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
|
@ -126,7 +153,7 @@ ssize_t hexagram_pcapng_stream_read(int fd,
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
*error = HEXAGRAM_PCAPNG_ERROR_OK;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue