123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <libgen.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <net/if.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <sys/time.h>
- #include <linux/can.h>
- #include <linux/can/raw.h>
- #include "lib.h"
- #define DEFAULT_GAP 1
- #define DEFAULT_LOOPS 1
- #define CHANNELS 20
- #define COMMENTSZ 200
- #define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ)
- #define STDOUTIDX 65536
- struct assignment {
- char txif[IFNAMSIZ];
- int txifidx;
- char rxif[IFNAMSIZ];
- };
- static struct assignment asgn[CHANNELS];
- const int canfd_on = 1;
- extern int optind, opterr, optopt;
- void print_usage(char *prg)
- {
- fprintf(stderr, "\nUsage: %s <options> [interface assignment]*\n\n", prg);
- fprintf(stderr, "Options: -I <infile> (default stdin)\n");
- fprintf(stderr, " -l <num> "
- "(process input file <num> times)\n"
- " "
- "(Use 'i' for infinite loop - default: %d)\n", DEFAULT_LOOPS);
- fprintf(stderr, " -t (ignore timestamps: "
- "send frames immediately)\n");
- fprintf(stderr, " -g <ms> (gap in milli "
- "seconds - default: %d ms)\n", DEFAULT_GAP);
- fprintf(stderr, " -s <s> (skip gaps in "
- "timestamps > 's' seconds)\n");
- fprintf(stderr, " -x (disable local "
- "loopback of sent CAN frames)\n");
- fprintf(stderr, " -v (verbose: print "
- "sent CAN frames)\n\n");
- fprintf(stderr, "Interface assignment: 0..n assignments like "
- "<write-if>=<log-if>\n");
- fprintf(stderr, "e.g. vcan2=can0 ( send frames received from can0 on "
- "vcan2 )\n");
- fprintf(stderr, "extra hook: stdout=can0 ( print logfile line marked with can0 on "
- "stdout )\n");
- fprintf(stderr, "No assignments => send frames to the interface(s) they "
- "had been received from.\n\n");
- fprintf(stderr, "Lines in the logfile not beginning with '(' (start of "
- "timestamp) are ignored.\n\n");
- }
- static inline int timeval_compare(struct timeval *lhs, struct timeval *rhs)
- {
- if (lhs->tv_sec < rhs->tv_sec)
- return -1;
- if (lhs->tv_sec > rhs->tv_sec)
- return 1;
- return lhs->tv_usec - rhs->tv_usec;
- }
- static inline void create_diff_tv(struct timeval *today, struct timeval *diff,
- struct timeval *log) {
-
- diff->tv_sec = today->tv_sec - log->tv_sec;
- diff->tv_usec = today->tv_usec - log->tv_usec;
- }
- static inline int frames_to_send(struct timeval *today, struct timeval *diff,
- struct timeval *log)
- {
-
- struct timeval cmp;
- cmp.tv_sec = log->tv_sec + diff->tv_sec;
- cmp.tv_usec = log->tv_usec + diff->tv_usec;
- if (cmp.tv_usec > 1000000) {
- cmp.tv_usec -= 1000000;
- cmp.tv_sec++;
- }
- if (cmp.tv_usec < 0) {
- cmp.tv_usec += 1000000;
- cmp.tv_sec--;
- }
- return timeval_compare(&cmp, today);
- }
- int get_txidx(char *logif_name) {
- int i;
- for (i=0; i<CHANNELS; i++) {
- if (asgn[i].rxif[0] == 0)
- break;
- if (strcmp(asgn[i].rxif, logif_name) == 0)
- break;
- }
- if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
- return 0;
- return asgn[i].txifidx;
- }
- char *get_txname(char *logif_name) {
- int i;
- for (i=0; i<CHANNELS; i++) {
- if (asgn[i].rxif[0] == 0)
- break;
- if (strcmp(asgn[i].rxif, logif_name) == 0)
- break;
- }
- if ((i == CHANNELS) || (asgn[i].rxif[0] == 0))
- return 0;
- return asgn[i].txif;
- }
- int add_assignment(char *mode, int socket, char *txname, char *rxname,
- int verbose) {
- struct ifreq ifr;
- int i;
-
- for (i=0; i<CHANNELS; i++) {
- if (asgn[i].txif[0] == 0)
- break;
- }
- if (i == CHANNELS) {
- fprintf(stderr, "Assignment table exceeded!\n");
- return 1;
- }
- if (strlen(txname) >= IFNAMSIZ) {
- fprintf(stderr, "write-if interface name '%s' too long!", txname);
- return 1;
- }
- strcpy(asgn[i].txif, txname);
- if (strlen(rxname) >= IFNAMSIZ) {
- fprintf(stderr, "log-if interface name '%s' too long!", rxname);
- return 1;
- }
- strcpy(asgn[i].rxif, rxname);
- if (strcmp(txname, "stdout")) {
- strcpy(ifr.ifr_name, txname);
- if (ioctl(socket, SIOCGIFINDEX, &ifr) < 0) {
- perror("SIOCGIFINDEX");
- fprintf(stderr, "write-if interface name '%s' is wrong!\n", txname);
- return 1;
- }
- asgn[i].txifidx = ifr.ifr_ifindex;
- } else
- asgn[i].txifidx = STDOUTIDX;
- if (verbose > 1)
- printf("added %s assignment: log-if=%s write-if=%s write-if-idx=%d\n",
- mode, asgn[i].rxif, asgn[i].txif, asgn[i].txifidx);
- return 0;
- }
- int main(int argc, char **argv)
- {
- static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ];
- struct sockaddr_can addr;
- static struct canfd_frame frame;
- static struct timeval today_tv, log_tv, last_log_tv, diff_tv;
- struct timespec sleep_ts;
- int s;
- FILE *infile = stdin;
- unsigned long gap = DEFAULT_GAP;
- int use_timestamps = 1;
- static int verbose, opt, delay_loops;
- static unsigned long skipgap;
- static int loopback_disable = 0;
- static int infinite_loops = 0;
- static int loops = DEFAULT_LOOPS;
- int assignments;
- int txidx;
- int eof, txmtu, i, j;
- char *fret;
- while ((opt = getopt(argc, argv, "I:l:tg:s:xv?")) != -1) {
- switch (opt) {
- case 'I':
- infile = fopen(optarg, "r");
- if (!infile) {
- perror("infile");
- return 1;
- }
- break;
- case 'l':
- if (optarg[0] == 'i')
- infinite_loops = 1;
- else
- if (!(loops = atoi(optarg))) {
- fprintf(stderr, "Invalid argument for option -l !\n");
- return 1;
- }
- break;
- case 't':
- use_timestamps = 0;
- break;
- case 'g':
- gap = strtoul(optarg, NULL, 10);
- break;
- case 's':
- skipgap = strtoul(optarg, NULL, 10);
- if (skipgap < 1) {
- fprintf(stderr, "Invalid argument for option -s !\n");
- return 1;
- }
- break;
- case 'x':
- loopback_disable = 1;
- break;
- case 'v':
- verbose++;
- break;
- case '?':
- default:
- print_usage(basename(argv[0]));
- return 1;
- break;
- }
- }
- assignments = argc - optind;
- if (infile == stdin) {
- infinite_loops = 0;
- loops = 1;
- }
- if (verbose > 1) {
- if (infinite_loops)
- printf("infinite_loops\n");
- else
- printf("%d loops\n", loops);
- }
- sleep_ts.tv_sec = gap / 1000;
- sleep_ts.tv_nsec = (gap % 1000) * 1000000;
-
- if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
- perror("socket");
- return 1;
- }
- addr.can_family = AF_CAN;
- addr.can_ifindex = 0;
-
- setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
-
- setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
- if (loopback_disable) {
- int loopback = 0;
- setsockopt(s, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
- &loopback, sizeof(loopback));
- }
- if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
- perror("bind");
- return 1;
- }
- if (assignments) {
-
- for (i=0; i<assignments; i++) {
- if (strlen(argv[optind+i]) >= BUFSZ) {
- fprintf(stderr, "Assignment too long!\n");
- print_usage(basename(argv[0]));
- return 1;
- }
- strcpy(buf, argv[optind+i]);
- for (j=0; j<BUFSZ; j++) {
- if (buf[j] == '=')
- break;
- }
- if ((j == BUFSZ) || (buf[j] != '=')) {
- fprintf(stderr, "'=' missing in assignment!\n");
- print_usage(basename(argv[0]));
- return 1;
- }
- buf[j] = 0;
- if (add_assignment("user", s, &buf[0], &buf[j+1], verbose))
- return 1;
- }
- }
- while (infinite_loops || loops--) {
- if (infile != stdin)
- rewind(infile);
- if (verbose > 1)
- printf (">>>>>>>>> start reading file. remaining loops = %d\n", loops);
-
- while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
- if (strlen(buf) >= BUFSZ-2) {
- fprintf(stderr, "comment line too long for input buffer\n");
- return 1;
- }
- }
- if (!fret)
- goto out;
- eof = 0;
- if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
- device, ascframe) != 4) {
- fprintf(stderr, "incorrect line format in logfile\n");
- return 1;
- }
- if (use_timestamps) {
- gettimeofday(&today_tv, NULL);
- create_diff_tv(&today_tv, &diff_tv, &log_tv);
- last_log_tv = log_tv;
- }
- while (!eof) {
- while ((!use_timestamps) ||
- (frames_to_send(&today_tv, &diff_tv, &log_tv) < 0)) {
-
- if (strlen(device) >= IFNAMSIZ) {
- fprintf(stderr, "log interface name '%s' too long!", device);
- return 1;
- }
- txidx = get_txidx(device);
-
- if ((!txidx) && (!assignments)) {
-
-
- if (add_assignment("auto", s, device, device, verbose))
- return 1;
- txidx = get_txidx(device);
- }
- if (txidx == STDOUTIDX) {
- printf("%s", buf);
- fflush(stdout);
- } else if (txidx > 0) {
- txmtu = parse_canframe(ascframe, &frame);
- if (!txmtu) {
- fprintf(stderr, "wrong CAN frame format: '%s'!", ascframe);
- return 1;
- }
- addr.can_family = AF_CAN;
- addr.can_ifindex = txidx;
-
- if (sendto(s, &frame, txmtu, 0, (struct sockaddr*)&addr, sizeof(addr)) != txmtu) {
- perror("sendto");
- return 1;
- }
- if (verbose) {
- printf("%s (%s) ", get_txname(device), device);
- if (txmtu == CAN_MTU)
- fprint_long_canframe(stdout, &frame, "\n", CANLIB_VIEW_INDENT_SFF, CAN_MAX_DLEN);
- else
- fprint_long_canframe(stdout, &frame, "\n", CANLIB_VIEW_INDENT_SFF, CANFD_MAX_DLEN);
- }
- }
-
- while ((fret = fgets(buf, BUFSZ-1, infile)) != NULL && buf[0] != '(') {
- if (strlen(buf) >= BUFSZ-2) {
- fprintf(stderr, "comment line too long for input buffer\n");
- return 1;
- }
- }
- if (!fret) {
- eof = 1;
- break;
- }
- if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec,
- device, ascframe) != 4) {
- fprintf(stderr, "incorrect line format in logfile\n");
- return 1;
- }
-
- if (strchr(buf, ')') - strchr(buf, '.') != 7) {
- fprintf(stderr, "timestamp format in logfile requires 6 decimal places\n");
- return 1;
- }
- if (use_timestamps) {
- gettimeofday(&today_tv, NULL);
-
-
- if ((last_log_tv.tv_sec > log_tv.tv_sec) ||
- (skipgap && labs(last_log_tv.tv_sec - log_tv.tv_sec) > skipgap))
- create_diff_tv(&today_tv, &diff_tv, &log_tv);
- last_log_tv = log_tv;
- }
- }
- if (nanosleep(&sleep_ts, NULL))
- return 1;
- delay_loops++;
- gettimeofday(&today_tv, NULL);
- }
- }
- out:
- close(s);
- fclose(infile);
- if (verbose > 1)
- printf("%d delay_loops\n", delay_loops);
- return 0;
- }
|