2018-12-16 11:37:04 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2019-05-07 20:39:39 -05:00
|
|
|
#include <endian.h>
|
2018-12-16 11:37:04 -06:00
|
|
|
#include <sys/types.h>
|
2019-01-30 02:04:40 -06:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <linux/can.h>
|
|
|
|
#include <linux/can/raw.h>
|
2018-12-16 11:37:04 -06:00
|
|
|
|
|
|
|
#include <hexagram/pcapng.h>
|
|
|
|
|
2019-05-22 22:02:58 -05:00
|
|
|
#include "pcapreplay.h"
|
2019-05-07 20:39:39 -05:00
|
|
|
|
2019-01-30 01:27:04 -06:00
|
|
|
struct pcapinfo {
|
2019-01-30 02:04:40 -06:00
|
|
|
int sock;
|
2019-01-30 01:27:04 -06:00
|
|
|
useconds_t last_time;
|
|
|
|
};
|
|
|
|
|
2019-05-22 22:02:58 -05:00
|
|
|
char *hexagram_arglist_pcapreplay(void) {
|
2019-05-07 20:39:39 -05:00
|
|
|
return "canif [file.pcapng]";
|
|
|
|
}
|
|
|
|
|
2018-12-16 11:37:04 -06:00
|
|
|
static void usage(int argc, char **argv, const char *message, ...) {
|
|
|
|
if (message) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, message);
|
|
|
|
vfprintf(stderr, message, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2019-05-07 20:39:39 -05:00
|
|
|
fprintf(stderr, "usage: hexagram %s %s\n",
|
2019-05-22 22:02:58 -05:00
|
|
|
argv[0], hexagram_arglist_pcapreplay());
|
2018-12-16 11:37:04 -06:00
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-01-24 01:55:31 -06:00
|
|
|
static ssize_t handle_option(hexagram_pcapng_stream *stream,
|
|
|
|
uint32_t type,
|
|
|
|
uint16_t code,
|
2019-01-30 01:27:04 -06:00
|
|
|
uint16_t len,
|
|
|
|
void *data) {
|
2019-01-24 01:26:44 -06:00
|
|
|
ssize_t readlen;
|
2019-01-30 01:27:04 -06:00
|
|
|
uint8_t buf[65535];
|
2019-01-24 01:26:44 -06:00
|
|
|
|
2019-01-30 01:27:04 -06:00
|
|
|
if ((readlen = read(stream->fd, buf, (size_t)len)) < 0) {
|
2019-01-24 01:26:44 -06:00
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == HEXAGRAM_PCAPNG_BLOCK_IF) {
|
|
|
|
if (code == HEXAGRAM_PCAPNG_OPTION_IF_TSRESOL) {
|
2019-01-30 01:27:04 -06:00
|
|
|
stream->if_tsresol = buf[0];
|
|
|
|
|
|
|
|
printf("Timestamp resolution is %d\n", buf[0]);
|
2019-01-24 01:26:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
|
|
|
|
|
|
|
return readlen;
|
|
|
|
|
|
|
|
error_io:
|
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-24 01:55:31 -06:00
|
|
|
static ssize_t handle_block_if(hexagram_pcapng_stream *stream,
|
|
|
|
uint32_t type,
|
2019-01-30 01:27:04 -06:00
|
|
|
size_t len,
|
|
|
|
void *data) {
|
2019-01-24 01:55:31 -06:00
|
|
|
hexagram_pcapng_if iface;
|
2018-12-16 11:37:04 -06:00
|
|
|
|
2019-01-24 01:55:31 -06:00
|
|
|
ssize_t readlen,
|
|
|
|
total = 0;
|
2019-01-24 01:26:44 -06:00
|
|
|
|
2019-01-24 01:55:31 -06:00
|
|
|
size_t remaining = len;
|
2019-01-20 15:55:40 -06:00
|
|
|
|
|
|
|
/*
|
2019-01-24 01:55:31 -06:00
|
|
|
* Read the interface block header.
|
2019-01-20 15:55:40 -06:00
|
|
|
*/
|
2019-01-24 01:55:31 -06:00
|
|
|
if ((readlen = read(stream->fd, &iface, sizeof(iface))) < 0) {
|
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
2019-01-20 15:55:40 -06:00
|
|
|
|
2019-01-24 01:55:31 -06:00
|
|
|
goto error_io;
|
|
|
|
} else {
|
|
|
|
remaining -= readlen;
|
|
|
|
total += readlen;
|
|
|
|
}
|
2019-01-20 15:55:40 -06:00
|
|
|
|
2019-01-30 01:27:04 -06:00
|
|
|
if ((readlen = hexagram_pcapng_read_options(stream, handle_option, type, remaining, data)) < 0) {
|
2019-01-24 01:55:31 -06:00
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
2019-01-20 15:55:40 -06:00
|
|
|
|
2019-01-24 01:55:31 -06:00
|
|
|
goto error_io;
|
|
|
|
} else {
|
|
|
|
total += readlen;
|
2019-01-20 15:55:40 -06:00
|
|
|
}
|
|
|
|
|
2019-01-24 01:55:31 -06:00
|
|
|
return total;
|
|
|
|
|
|
|
|
error_io:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t handle_block_packet(hexagram_pcapng_stream *stream,
|
|
|
|
uint32_t type,
|
2019-01-30 01:27:04 -06:00
|
|
|
size_t len,
|
|
|
|
struct pcapinfo *data) {
|
2019-01-24 01:55:31 -06:00
|
|
|
hexagram_pcapng_packet header;
|
2019-05-21 12:04:35 -05:00
|
|
|
struct can_frame frame;
|
2019-01-24 01:55:31 -06:00
|
|
|
|
|
|
|
ssize_t readlen,
|
|
|
|
total = 0;
|
|
|
|
|
|
|
|
size_t remaining = len;
|
|
|
|
|
2019-01-20 15:55:40 -06:00
|
|
|
/*
|
2019-01-24 01:55:31 -06:00
|
|
|
* Read the packet block header so that we may determine the size of the
|
|
|
|
* payload to continue to read.
|
2019-01-20 15:55:40 -06:00
|
|
|
*/
|
2019-01-24 00:20:55 -06:00
|
|
|
if ((readlen = read(stream->fd, &header, sizeof(header))) < 0) {
|
2019-01-24 01:55:31 -06:00
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
2019-01-20 15:55:40 -06:00
|
|
|
|
|
|
|
goto error_io;
|
|
|
|
} else {
|
|
|
|
remaining -= readlen;
|
2019-01-24 01:26:44 -06:00
|
|
|
total += readlen;
|
2019-01-20 15:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-01-24 00:20:55 -06:00
|
|
|
* Read the packet body into our scratchpad.
|
2019-01-20 15:55:40 -06:00
|
|
|
*/
|
2019-05-21 12:04:35 -05:00
|
|
|
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) {
|
2019-01-24 01:55:31 -06:00
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
2019-01-20 15:55:40 -06:00
|
|
|
|
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
2019-05-21 12:04:35 -05:00
|
|
|
remaining -= readlen;
|
|
|
|
total += readlen;
|
2019-01-30 01:27:04 -06:00
|
|
|
|
2019-05-21 12:04:35 -05:00
|
|
|
if (header.caplen <= sizeof(frame)) {
|
|
|
|
if (data->last_time) {
|
|
|
|
usleep((useconds_t)header.timestamp[1] - data->last_time);
|
|
|
|
}
|
2019-01-30 01:27:04 -06:00
|
|
|
|
2019-05-21 12:04:35 -05:00
|
|
|
data->last_time = (useconds_t)header.timestamp[1];
|
2019-01-30 02:04:40 -06:00
|
|
|
|
2019-05-21 12:04:35 -05:00
|
|
|
frame.can_id = be32toh(frame.can_id);
|
2019-02-01 16:45:07 -06:00
|
|
|
|
2019-05-21 12:04:35 -05:00
|
|
|
if (write(data->sock, &frame, sizeof(struct can_frame)) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
2019-02-01 16:45:07 -06:00
|
|
|
}
|
2019-01-20 15:55:40 -06:00
|
|
|
|
2019-01-24 01:26:44 -06:00
|
|
|
if (header.caplen % sizeof(uint32_t)) {
|
|
|
|
size_t padding = sizeof(uint32_t) - (header.caplen % sizeof(uint32_t));
|
|
|
|
|
|
|
|
if (lseek(stream->fd, padding, SEEK_CUR) < 0) {
|
2019-01-24 01:55:31 -06:00
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
2019-01-24 01:26:44 -06:00
|
|
|
|
|
|
|
goto error_io;
|
|
|
|
} else {
|
|
|
|
remaining -= padding;
|
|
|
|
total += padding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 15:55:40 -06:00
|
|
|
/*
|
|
|
|
* The remaining data here should be pcapng option values, and since we do
|
|
|
|
* not presently require them, we can safely seek past them.
|
|
|
|
*/
|
2019-01-24 01:55:31 -06:00
|
|
|
if (lseek(stream->fd, remaining, SEEK_CUR) < 0) {
|
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
|
|
|
|
2018-12-16 11:37:04 -06:00
|
|
|
goto error_io;
|
2019-01-24 01:26:44 -06:00
|
|
|
} else {
|
2019-01-24 01:55:31 -06:00
|
|
|
total += remaining;
|
2018-12-16 11:37:04 -06:00
|
|
|
}
|
|
|
|
|
2019-01-24 00:20:55 -06:00
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_OK;
|
2019-01-21 08:51:30 -06:00
|
|
|
|
2019-01-24 01:26:44 -06:00
|
|
|
return total;
|
2018-12-16 11:37:04 -06:00
|
|
|
|
|
|
|
error_io:
|
2019-01-24 00:20:55 -06:00
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
2019-01-21 08:51:30 -06:00
|
|
|
|
2018-12-16 11:37:04 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-24 01:55:31 -06:00
|
|
|
static ssize_t handle_block(hexagram_pcapng_stream *stream,
|
|
|
|
uint32_t type,
|
2019-01-30 01:27:04 -06:00
|
|
|
size_t len,
|
|
|
|
void *data) {
|
2019-01-24 01:55:31 -06:00
|
|
|
switch (type) {
|
|
|
|
case HEXAGRAM_PCAPNG_BLOCK_IF:
|
2019-01-30 01:27:04 -06:00
|
|
|
return handle_block_if(stream, type, len, data);
|
2019-01-24 01:55:31 -06:00
|
|
|
|
|
|
|
case HEXAGRAM_PCAPNG_BLOCK_PACKET:
|
2019-01-30 01:27:04 -06:00
|
|
|
return handle_block_packet(stream, type, len, (struct pcapinfo *)data);
|
2019-01-24 01:55:31 -06:00
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lseek(stream->fd, len, SEEK_CUR) < 0) {
|
|
|
|
stream->error = HEXAGRAM_PCAPNG_ERROR_IO;
|
|
|
|
|
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
|
|
|
error_io:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-22 22:02:58 -05:00
|
|
|
int hexagram_main_pcapreplay(int argc, char **argv) {
|
2019-01-24 00:20:55 -06:00
|
|
|
int fd;
|
2019-01-30 02:04:40 -06:00
|
|
|
|
|
|
|
struct sockaddr_can addr;
|
|
|
|
struct ifreq ifr;
|
2018-12-16 11:37:04 -06:00
|
|
|
|
2019-01-30 01:27:04 -06:00
|
|
|
struct pcapinfo data = {
|
|
|
|
.last_time = 0
|
|
|
|
};
|
|
|
|
|
2019-01-30 02:04:40 -06:00
|
|
|
hexagram_pcapng_stream *stream;
|
|
|
|
|
|
|
|
if (argc == 2) {
|
2018-12-16 11:37:04 -06:00
|
|
|
fd = fileno(stdin);
|
2019-01-30 02:04:40 -06:00
|
|
|
} else if (argc == 3) {
|
|
|
|
if ((fd = open(argv[2], O_RDONLY)) < 0) {
|
2018-12-16 11:37:04 -06:00
|
|
|
perror("open()");
|
|
|
|
|
|
|
|
goto error_open;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usage(argc, argv, NULL);
|
2019-01-24 00:20:55 -06:00
|
|
|
exit(1);
|
2018-12-16 11:37:04 -06:00
|
|
|
}
|
|
|
|
|
2019-01-30 02:04:40 -06:00
|
|
|
if ((data.sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
|
|
|
|
perror("socket()");
|
|
|
|
|
|
|
|
goto error_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(ifr.ifr_name, argv[1]);
|
|
|
|
|
|
|
|
ioctl(data.sock, SIOCGIFINDEX, &ifr);
|
|
|
|
|
|
|
|
addr.can_family = AF_CAN;
|
|
|
|
addr.can_ifindex = ifr.ifr_ifindex;
|
|
|
|
|
|
|
|
if (bind(data.sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
|
|
|
perror("bind()");
|
|
|
|
|
|
|
|
goto error_bind;
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:20:55 -06:00
|
|
|
if ((stream = hexagram_pcapng_stream_open_fd(fd)) < 0) {
|
|
|
|
goto error_pcapng_stream_open_fd;
|
|
|
|
}
|
|
|
|
|
2019-01-30 01:27:04 -06:00
|
|
|
if (hexagram_pcapng_stream_read(stream, handle_block, &data) < 0) {
|
2018-12-16 11:37:04 -06:00
|
|
|
perror("hexagram_pcapng_stream_read()");
|
|
|
|
|
2019-01-24 00:20:55 -06:00
|
|
|
goto error_pcapng_stream_read;
|
2018-12-16 11:37:04 -06:00
|
|
|
}
|
|
|
|
|
2019-01-24 00:20:55 -06:00
|
|
|
hexagram_pcapng_stream_destroy(stream);
|
|
|
|
|
2019-01-30 02:04:40 -06:00
|
|
|
close(data.sock);
|
|
|
|
|
2018-12-16 11:37:04 -06:00
|
|
|
if (argc == 2) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2019-01-24 00:20:55 -06:00
|
|
|
error_pcapng_stream_read:
|
|
|
|
hexagram_pcapng_stream_destroy(stream);
|
|
|
|
|
|
|
|
error_pcapng_stream_open_fd:
|
2019-01-30 02:04:40 -06:00
|
|
|
error_bind:
|
|
|
|
close(data.sock);
|
|
|
|
|
|
|
|
error_socket:
|
2018-12-16 11:37:04 -06:00
|
|
|
if (argc == 2) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2019-01-30 02:04:40 -06:00
|
|
|
close(fd);
|
|
|
|
|
2018-12-16 11:37:04 -06:00
|
|
|
error_open:
|
|
|
|
return 1;
|
|
|
|
}
|