Feed my will to feel my moment drawing way outside the lines
This commit is contained in:
parent
12c0973fff
commit
f6fe91bb10
1 changed files with 48 additions and 5 deletions
|
@ -6,10 +6,17 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <linux/can.h>
|
||||||
|
#include <linux/can/raw.h>
|
||||||
|
|
||||||
#include <hexagram/pcapng.h>
|
#include <hexagram/pcapng.h>
|
||||||
|
|
||||||
struct pcapinfo {
|
struct pcapinfo {
|
||||||
|
int sock;
|
||||||
useconds_t last_time;
|
useconds_t last_time;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -22,7 +29,7 @@ static void usage(int argc, char **argv, const char *message, ...) {
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "usage: %s [file.pcapng]\n", argv[0]);
|
fprintf(stderr, "usage: %s canif [file.pcapng]\n", argv[0]);
|
||||||
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -99,9 +106,11 @@ 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;
|
||||||
|
struct can_frame frame;
|
||||||
uint8_t body[65535];
|
uint8_t body[65535];
|
||||||
|
|
||||||
ssize_t readlen,
|
ssize_t readlen,
|
||||||
|
wrlen,
|
||||||
total = 0;
|
total = 0;
|
||||||
|
|
||||||
size_t remaining = len;
|
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];
|
data->last_time = (useconds_t)header.timestamp[1];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32"\n",
|
printf("Read packet %"PRIu32" bytes time hi %"PRIu32" lo %"PRIu32"\n",
|
||||||
header.caplen, header.timestamp[0], header.timestamp[1]);
|
header.caplen, header.timestamp[0], header.timestamp[1]);
|
||||||
|
|
||||||
|
@ -206,16 +217,21 @@ error_io:
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int fd;
|
int fd;
|
||||||
hexagram_pcapng_stream *stream;
|
|
||||||
|
struct sockaddr_can addr;
|
||||||
|
struct can_frame frame;
|
||||||
|
struct ifreq ifr;
|
||||||
|
|
||||||
struct pcapinfo data = {
|
struct pcapinfo data = {
|
||||||
.last_time = 0
|
.last_time = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
if (argc == 1) {
|
hexagram_pcapng_stream *stream;
|
||||||
|
|
||||||
|
if (argc == 2) {
|
||||||
fd = fileno(stdin);
|
fd = fileno(stdin);
|
||||||
} else if (argc == 2) {
|
} else if (argc == 3) {
|
||||||
if ((fd = open(argv[1], O_RDONLY)) < 0) {
|
if ((fd = open(argv[2], O_RDONLY)) < 0) {
|
||||||
perror("open()");
|
perror("open()");
|
||||||
|
|
||||||
goto error_open;
|
goto error_open;
|
||||||
|
@ -225,6 +241,25 @@ int main(int argc, char **argv) {
|
||||||
exit(1);
|
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) {
|
if ((stream = hexagram_pcapng_stream_open_fd(fd)) < 0) {
|
||||||
goto error_pcapng_stream_open_fd;
|
goto error_pcapng_stream_open_fd;
|
||||||
}
|
}
|
||||||
|
@ -237,6 +272,8 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
hexagram_pcapng_stream_destroy(stream);
|
hexagram_pcapng_stream_destroy(stream);
|
||||||
|
|
||||||
|
close(data.sock);
|
||||||
|
|
||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
@ -247,10 +284,16 @@ error_pcapng_stream_read:
|
||||||
hexagram_pcapng_stream_destroy(stream);
|
hexagram_pcapng_stream_destroy(stream);
|
||||||
|
|
||||||
error_pcapng_stream_open_fd:
|
error_pcapng_stream_open_fd:
|
||||||
|
error_bind:
|
||||||
|
close(data.sock);
|
||||||
|
|
||||||
|
error_socket:
|
||||||
if (argc == 2) {
|
if (argc == 2) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
error_open:
|
error_open:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue