diff --git a/examples/pcapread.c b/examples/pcapread.c index 324f563..8bac86b 100644 --- a/examples/pcapread.c +++ b/examples/pcapread.c @@ -6,10 +6,17 @@ #include #include #include +#include +#include + +#include +#include +#include #include struct pcapinfo { + int sock; useconds_t last_time; }; @@ -22,7 +29,7 @@ static void usage(int argc, char **argv, const char *message, ...) { va_end(args); } - fprintf(stderr, "usage: %s [file.pcapng]\n", argv[0]); + fprintf(stderr, "usage: %s canif [file.pcapng]\n", argv[0]); exit(1); } @@ -99,9 +106,11 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream, size_t len, struct pcapinfo *data) { hexagram_pcapng_packet header; + struct can_frame frame; uint8_t body[65535]; ssize_t readlen, + wrlen, total = 0; size_t remaining = len; @@ -137,6 +146,8 @@ static ssize_t handle_block_packet(hexagram_pcapng_stream *stream, data->last_time = (useconds_t)header.timestamp[1]; + + printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32"\n", header.caplen, header.timestamp[0], header.timestamp[1]); @@ -206,16 +217,21 @@ error_io: int main(int argc, char **argv) { int fd; - hexagram_pcapng_stream *stream; + + struct sockaddr_can addr; + struct can_frame frame; + struct ifreq ifr; struct pcapinfo data = { .last_time = 0 }; - if (argc == 1) { + hexagram_pcapng_stream *stream; + + if (argc == 2) { fd = fileno(stdin); - } else if (argc == 2) { - if ((fd = open(argv[1], O_RDONLY)) < 0) { + } else if (argc == 3) { + if ((fd = open(argv[2], O_RDONLY)) < 0) { perror("open()"); goto error_open; @@ -225,6 +241,25 @@ int main(int argc, char **argv) { exit(1); } + 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; + } + if ((stream = hexagram_pcapng_stream_open_fd(fd)) < 0) { goto error_pcapng_stream_open_fd; } @@ -237,6 +272,8 @@ int main(int argc, char **argv) { hexagram_pcapng_stream_destroy(stream); + close(data.sock); + if (argc == 2) { close(fd); } @@ -247,10 +284,16 @@ error_pcapng_stream_read: hexagram_pcapng_stream_destroy(stream); error_pcapng_stream_open_fd: +error_bind: + close(data.sock); + +error_socket: if (argc == 2) { close(fd); } + close(fd); + error_open: return 1; }