My girl would be so proud of me
This commit is contained in:
parent
f70d13c541
commit
bb0363a1a2
1 changed files with 80 additions and 31 deletions
|
@ -23,10 +23,10 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t handle_options(hexagram_pcapng_stream *stream,
|
static ssize_t handle_option(hexagram_pcapng_stream *stream,
|
||||||
uint32_t type,
|
uint32_t type,
|
||||||
uint16_t code,
|
uint16_t code,
|
||||||
uint16_t len) {
|
uint16_t len) {
|
||||||
ssize_t readlen;
|
ssize_t readlen;
|
||||||
uint8_t data[65535];
|
uint8_t data[65535];
|
||||||
|
|
||||||
|
@ -50,40 +50,59 @@ error_io:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t handle_block(hexagram_pcapng_stream *stream,
|
static ssize_t handle_block_if(hexagram_pcapng_stream *stream,
|
||||||
uint32_t type,
|
uint32_t type,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
hexagram_pcapng_packet header;
|
hexagram_pcapng_if iface;
|
||||||
uint8_t body[65535];
|
|
||||||
|
ssize_t readlen,
|
||||||
|
total = 0;
|
||||||
|
|
||||||
size_t remaining = len;
|
size_t remaining = len;
|
||||||
|
|
||||||
ssize_t total = 0,
|
|
||||||
readlen;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we've received anything other than a packet block, then seek past
|
* Read the interface block header.
|
||||||
* that data and move on.
|
|
||||||
*/
|
*/
|
||||||
if (type != HEXAGRAM_PCAPNG_BLOCK_PACKET) {
|
if ((readlen = read(stream->fd, &iface, sizeof(iface))) < 0) {
|
||||||
printf("Read block type %08"PRIx32" len %zu\n",
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
type, len);
|
|
||||||
|
|
||||||
if ((readlen = lseek(stream->fd, len, SEEK_CUR)) < 0) {
|
goto error_io;
|
||||||
perror("lseek()");
|
} else {
|
||||||
|
remaining -= readlen;
|
||||||
goto error_io;
|
total += readlen;
|
||||||
}
|
|
||||||
|
|
||||||
return readlen;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((readlen = hexagram_pcapng_read_options(stream, handle_option, type, remaining)) < 0) {
|
||||||
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
} else {
|
||||||
|
total += readlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total;
|
||||||
|
|
||||||
|
error_io:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
|
||||||
|
uint32_t type,
|
||||||
|
size_t len) {
|
||||||
|
hexagram_pcapng_packet header;
|
||||||
|
uint8_t body[65535];
|
||||||
|
|
||||||
|
ssize_t readlen,
|
||||||
|
total = 0;
|
||||||
|
|
||||||
|
size_t remaining = len;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Otherwise, read the packet block header so that we may determine the
|
* Read the packet block header so that we may determine the size of the
|
||||||
* size of the payload to continue to read.
|
* payload to continue to read.
|
||||||
*/
|
*/
|
||||||
if ((readlen = read(stream->fd, &header, sizeof(header))) < 0) {
|
if ((readlen = read(stream->fd, &header, sizeof(header))) < 0) {
|
||||||
perror("read()");
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
|
@ -95,7 +114,7 @@ static ssize_t handle_block(hexagram_pcapng_stream *stream,
|
||||||
* Read the packet body into our scratchpad.
|
* Read the packet body into our scratchpad.
|
||||||
*/
|
*/
|
||||||
if ((readlen = read(stream->fd, &body, header.caplen)) < 0) {
|
if ((readlen = read(stream->fd, &body, header.caplen)) < 0) {
|
||||||
perror("read()");
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
|
@ -110,7 +129,7 @@ static ssize_t handle_block(hexagram_pcapng_stream *stream,
|
||||||
size_t padding = sizeof(uint32_t) - (header.caplen % sizeof(uint32_t));
|
size_t padding = sizeof(uint32_t) - (header.caplen % sizeof(uint32_t));
|
||||||
|
|
||||||
if (lseek(stream->fd, padding, SEEK_CUR) < 0) {
|
if (lseek(stream->fd, padding, SEEK_CUR) < 0) {
|
||||||
perror("lseek()");
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
|
@ -123,10 +142,12 @@ static ssize_t handle_block(hexagram_pcapng_stream *stream,
|
||||||
* 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 ((readlen = hexagram_pcapng_read_options(stream, handle_options, type, remaining)) < 0) {
|
if (lseek(stream->fd, remaining, SEEK_CUR) < 0) {
|
||||||
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
} else {
|
||||||
total += readlen;
|
total += remaining;
|
||||||
}
|
}
|
||||||
|
|
||||||
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
||||||
|
@ -139,6 +160,34 @@ error_io:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ssize_t handle_block(hexagram_pcapng_stream *stream,
|
||||||
|
uint32_t type,
|
||||||
|
size_t len) {
|
||||||
|
switch (type) {
|
||||||
|
case HEXAGRAM_PCAPNG_BLOCK_IF:
|
||||||
|
return handle_block_if(stream, type, len);
|
||||||
|
|
||||||
|
case HEXAGRAM_PCAPNG_BLOCK_PACKET:
|
||||||
|
return handle_block_packet(stream, type, len);
|
||||||
|
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Read block type %08"PRIx32" len %zu\n",
|
||||||
|
type, len);
|
||||||
|
|
||||||
|
if (lseek(stream->fd, len, SEEK_CUR) < 0) {
|
||||||
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
|
||||||
|
error_io:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int fd;
|
int fd;
|
||||||
hexagram_pcapng_stream *stream;
|
hexagram_pcapng_stream *stream;
|
||||||
|
|
Loading…
Add table
Reference in a new issue