No more type punning
This commit is contained in:
parent
1fe68ef639
commit
ab61c674a0
2 changed files with 46 additions and 31 deletions
|
@ -75,7 +75,7 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
|
||||||
size_t len,
|
size_t len,
|
||||||
hexagram_capture *capture) {
|
hexagram_capture *capture) {
|
||||||
hexagram_pcapng_packet header;
|
hexagram_pcapng_packet header;
|
||||||
uint8_t body[65535];
|
struct can_frame frame;
|
||||||
|
|
||||||
ssize_t readlen,
|
ssize_t readlen,
|
||||||
total = 0;
|
total = 0;
|
||||||
|
@ -98,29 +98,36 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
|
||||||
total += readlen;
|
total += readlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (header.caplen > sizeof(frame)) {
|
||||||
* Read the packet body into our scratchpad.
|
if (lseek(stream->fd, header.caplen, SEEK_CUR) < 0) {
|
||||||
*/
|
|
||||||
if ((readlen = read(stream->fd, &body, header.caplen)) < 0) {
|
|
||||||
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
|
||||||
remaining -= readlen;
|
|
||||||
total += readlen;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
readlen = header.caplen;
|
||||||
|
} else if ((readlen = read(stream->fd, &frame, header.caplen)) < 0) {
|
||||||
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
|
|
||||||
|
remaining -= readlen;
|
||||||
|
total += readlen;
|
||||||
|
|
||||||
|
if (header.caplen <= sizeof(frame)) {
|
||||||
usec = ((uint64_t)header.timestamp[0] << 32)
|
usec = ((uint64_t)header.timestamp[0] << 32)
|
||||||
| (uint64_t)header.timestamp[1];
|
| (uint64_t)header.timestamp[1];
|
||||||
|
|
||||||
timestamp.tv_sec = usec / 1000000;
|
timestamp.tv_sec = usec / 1000000;
|
||||||
timestamp.tv_usec = usec % 1000000;
|
timestamp.tv_usec = usec % 1000000;
|
||||||
|
|
||||||
((struct can_frame *)body)->can_id = be32toh(((struct can_frame *)body)->can_id);
|
frame.can_id = be32toh(frame.can_id);
|
||||||
|
|
||||||
if (hexagram_capture_write(capture, ×tamp, (struct can_frame *)body) < 0) {
|
if (hexagram_capture_write(capture, ×tamp, &frame) < 0) {
|
||||||
goto error_io;
|
goto error_io;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (header.caplen % sizeof(uint32_t)) {
|
if (header.caplen % sizeof(uint32_t)) {
|
||||||
size_t padding = sizeof(uint32_t) - (header.caplen % sizeof(uint32_t));
|
size_t padding = sizeof(uint32_t) - (header.caplen % sizeof(uint32_t));
|
||||||
|
|
|
@ -114,7 +114,7 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
|
||||||
size_t len,
|
size_t len,
|
||||||
struct pcapinfo *data) {
|
struct pcapinfo *data) {
|
||||||
hexagram_pcapng_packet header;
|
hexagram_pcapng_packet header;
|
||||||
uint8_t body[65535];
|
struct can_frame frame;
|
||||||
|
|
||||||
ssize_t readlen,
|
ssize_t readlen,
|
||||||
total = 0;
|
total = 0;
|
||||||
|
@ -137,31 +137,39 @@ static ssize_t handle_block_packet(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 (header.caplen > sizeof(frame)) {
|
||||||
|
if (lseek(stream->fd, header.caplen, SEEK_CUR) < 0) {
|
||||||
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
|
} else if ((readlen = read(stream->fd, &frame, header.caplen)) < 0) {
|
||||||
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||||
|
|
||||||
goto error_io;
|
goto error_io;
|
||||||
} else {
|
|
||||||
remaining -= readlen;
|
|
||||||
total += readlen;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remaining -= readlen;
|
||||||
|
total += readlen;
|
||||||
|
|
||||||
|
if (header.caplen <= sizeof(frame)) {
|
||||||
if (data->last_time) {
|
if (data->last_time) {
|
||||||
usleep((useconds_t)header.timestamp[1] - data->last_time);
|
usleep((useconds_t)header.timestamp[1] - data->last_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
data->last_time = (useconds_t)header.timestamp[1];
|
data->last_time = (useconds_t)header.timestamp[1];
|
||||||
|
|
||||||
((struct can_frame *)body)->can_id = be32toh(((struct can_frame *)body)->can_id);
|
frame.can_id = be32toh(frame.can_id);
|
||||||
|
|
||||||
printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32" id %8"PRIx32" payload %d\n",
|
printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32" id %8"PRIx32" payload %d\n",
|
||||||
header.caplen, header.timestamp[0], header.timestamp[1],
|
header.caplen, header.timestamp[0], header.timestamp[1],
|
||||||
((struct can_frame *)&body)->can_id,
|
frame.can_id,
|
||||||
((struct can_frame *)&body)->can_dlc);
|
frame.can_dlc);
|
||||||
|
|
||||||
if (write(data->sock, body, sizeof(struct can_frame)) < 0) {
|
if (write(data->sock, &frame, sizeof(struct can_frame)) < 0) {
|
||||||
goto error_io;
|
goto error_io;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (header.caplen % sizeof(uint32_t)) {
|
if (header.caplen % sizeof(uint32_t)) {
|
||||||
size_t padding = sizeof(uint32_t) - (header.caplen % sizeof(uint32_t));
|
size_t padding = sizeof(uint32_t) - (header.caplen % sizeof(uint32_t));
|
||||||
|
|
Loading…
Add table
Reference in a new issue