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,
|
||||
hexagram_capture *capture) {
|
||||
hexagram_pcapng_packet header;
|
||||
uint8_t body[65535];
|
||||
struct can_frame frame;
|
||||
|
||||
ssize_t readlen,
|
||||
total = 0;
|
||||
|
@ -98,28 +98,35 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
|
|||
total += readlen;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
readlen = header.caplen;
|
||||
} else if ((readlen = read(stream->fd, &frame, header.caplen)) < 0) {
|
||||
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
||||
|
||||
goto error_io;
|
||||
} else {
|
||||
remaining -= readlen;
|
||||
total += readlen;
|
||||
}
|
||||
|
||||
usec = ((uint64_t)header.timestamp[0] << 32)
|
||||
| (uint64_t)header.timestamp[1];
|
||||
remaining -= readlen;
|
||||
total += readlen;
|
||||
|
||||
timestamp.tv_sec = usec / 1000000;
|
||||
timestamp.tv_usec = usec % 1000000;
|
||||
if (header.caplen <= sizeof(frame)) {
|
||||
usec = ((uint64_t)header.timestamp[0] << 32)
|
||||
| (uint64_t)header.timestamp[1];
|
||||
|
||||
((struct can_frame *)body)->can_id = be32toh(((struct can_frame *)body)->can_id);
|
||||
timestamp.tv_sec = usec / 1000000;
|
||||
timestamp.tv_usec = usec % 1000000;
|
||||
|
||||
if (hexagram_capture_write(capture, ×tamp, (struct can_frame *)body) < 0) {
|
||||
goto error_io;
|
||||
frame.can_id = be32toh(frame.can_id);
|
||||
|
||||
if (hexagram_capture_write(capture, ×tamp, &frame) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
if (header.caplen % sizeof(uint32_t)) {
|
||||
|
|
|
@ -114,7 +114,7 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
|
|||
size_t len,
|
||||
struct pcapinfo *data) {
|
||||
hexagram_pcapng_packet header;
|
||||
uint8_t body[65535];
|
||||
struct can_frame frame;
|
||||
|
||||
ssize_t readlen,
|
||||
total = 0;
|
||||
|
@ -137,30 +137,38 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
|
|||
/*
|
||||
* 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;
|
||||
|
||||
goto error_io;
|
||||
} else {
|
||||
remaining -= readlen;
|
||||
total += readlen;
|
||||
}
|
||||
|
||||
if (data->last_time) {
|
||||
usleep((useconds_t)header.timestamp[1] - data->last_time);
|
||||
}
|
||||
remaining -= readlen;
|
||||
total += readlen;
|
||||
|
||||
data->last_time = (useconds_t)header.timestamp[1];
|
||||
if (header.caplen <= sizeof(frame)) {
|
||||
if (data->last_time) {
|
||||
usleep((useconds_t)header.timestamp[1] - data->last_time);
|
||||
}
|
||||
|
||||
((struct can_frame *)body)->can_id = be32toh(((struct can_frame *)body)->can_id);
|
||||
data->last_time = (useconds_t)header.timestamp[1];
|
||||
|
||||
printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32" id %8"PRIx32" payload %d\n",
|
||||
header.caplen, header.timestamp[0], header.timestamp[1],
|
||||
((struct can_frame *)&body)->can_id,
|
||||
((struct can_frame *)&body)->can_dlc);
|
||||
frame.can_id = be32toh(frame.can_id);
|
||||
|
||||
if (write(data->sock, body, sizeof(struct can_frame)) < 0) {
|
||||
goto error_io;
|
||||
printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32" id %8"PRIx32" payload %d\n",
|
||||
header.caplen, header.timestamp[0], header.timestamp[1],
|
||||
frame.can_id,
|
||||
frame.can_dlc);
|
||||
|
||||
if (write(data->sock, &frame, sizeof(struct can_frame)) < 0) {
|
||||
goto error_io;
|
||||
}
|
||||
}
|
||||
|
||||
if (header.caplen % sizeof(uint32_t)) {
|
||||
|
|
Loading…
Add table
Reference in a new issue