From 29290505f76f2d70c28216c50b7e44dbf5ccd665 Mon Sep 17 00:00:00 2001 From: Oliver Hartkopp Date: Thu, 29 Feb 2024 08:15:53 +0100 Subject: [PATCH] log: finalize unified buffer handling Signed-off-by: Oliver Hartkopp --- canlogserver.c | 15 ++++++--------- canplayer.c | 22 ++++++++++++++++++---- lib.h | 2 -- log2asc.c | 24 +++++++++++++++++++----- log2long.c | 33 ++++++++++++++++++++++++++------- 5 files changed, 69 insertions(+), 27 deletions(-) diff --git a/canlogserver.c b/canlogserver.c index e64cc86..e9c2b80 100644 --- a/canlogserver.c +++ b/canlogserver.c @@ -72,9 +72,6 @@ #define ANYDEV "any" #define ANL "\r\n" /* newline in ASC mode */ -#define COMMENTSZ 200 -#define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ) /* for one line in the logfile */ - #define DEFPORT 28700 static char devname[MAXDEV][IFNAMSIZ+1]; @@ -199,7 +196,7 @@ int main(int argc, char **argv) struct sockaddr_in inaddr; struct sockaddr_in clientaddr; socklen_t sin_size = sizeof(clientaddr); - char temp[BUFSZ]; + char afrbuf[AFRSZ]; sigemptyset(&sigset); signalaction.sa_handler = &childdied; @@ -416,19 +413,19 @@ int main(int argc, char **argv) idx = idx2dindex(addr.can_ifindex, s[i]); - sprintf(temp, "(%llu.%06llu) %*s ", + sprintf(afrbuf, "(%llu.%06llu) %*s ", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec, max_devname_len, devname[idx]); - sprint_canframe(temp+strlen(temp), (cu_t *)&frame, 0); - strcat(temp, "\n"); + sprint_canframe(afrbuf+strlen(afrbuf), (cu_t *)&frame, 0); + strcat(afrbuf, "\n"); - if (write(accsocket, temp, strlen(temp)) < 0) { + if (write(accsocket, afrbuf, strlen(afrbuf)) < 0) { perror("writeaccsock"); return 1; } #if 0 /* print CAN frame in log file style to stdout */ - printf("%s", temp); + printf("%s", afrbuf); #endif } diff --git a/canplayer.c b/canplayer.c index 9499077..a38a82a 100644 --- a/canplayer.c +++ b/canplayer.c @@ -61,10 +61,24 @@ #define DEFAULT_GAP 1 /* ms */ #define DEFAULT_LOOPS 1 /* only one replay */ #define CHANNELS 20 /* anyone using more than 20 CAN interfaces at a time? */ -#define COMMENTSZ 200 -#define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ) /* for one line in the logfile */ #define STDOUTIDX 65536 /* interface index for printing on stdout - bigger than max uint16 */ +#if (IFNAMSIZ != 16) +#error "IFNAMSIZ value does not to DEVSZ calculation!" +#endif + +#define DEVSZ 22 /* IFNAMSZ + 6 */ +#define TIMESZ sizeof("(1345212884.318850) ") +#define BUFSZ (TIMESZ + DEVSZ + AFRSZ) + +/* adapt sscanf() functions below on error */ +#if (AFRSZ != 6300) +#error "AFRSZ value does not fit sscanf restrictions!" +#endif +#if (DEVSZ != 22) +#error "DEVSZ value does not fit sscanf restrictions!" +#endif + struct assignment { char txif[IFNAMSIZ]; int txifidx; @@ -239,7 +253,7 @@ int add_assignment(char *mode, int socket, char *txname, char *rxname, int verbo int main(int argc, char **argv) { - static char buf[BUFSZ], device[BUFSZ], afrbuf[AFRSZ]; + static char buf[BUFSZ], device[DEVSZ], afrbuf[AFRSZ]; struct sockaddr_can addr; struct can_raw_vcid_options vcid_opts = { .flags = CAN_RAW_XL_VCID_TX_PASS, @@ -426,7 +440,7 @@ int main(int argc, char **argv) eof = 0; - if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, device, afrbuf) != 4) { + if (sscanf(buf, "(%llu.%llu) %21s %6299s", &sec, &usec, device, afrbuf) != 4) { fprintf(stderr, "incorrect line format in logfile\n"); return 1; } diff --git a/lib.h b/lib.h index a0f2b99..d31f50b 100644 --- a/lib.h +++ b/lib.h @@ -63,8 +63,6 @@ typedef union { struct canxl_frame xl; } cu_t; -#define CL_CFSZ 400 /* to be removed */ - /* * The buffer size for ASCII CAN frame string representations * covers also the 'long' CAN frame output from sprint_long_canframe() diff --git a/log2asc.c b/log2asc.c index f87a7bb..81d69bc 100644 --- a/log2asc.c +++ b/log2asc.c @@ -54,8 +54,6 @@ #include "lib.h" -#define BUFSZ 400 /* for one line in the logfile */ - extern int optind, opterr, optopt; void print_usage(char *prg) @@ -181,9 +179,25 @@ void canfd_asc(struct canfd_frame *cf, int devno, int mtu, char *extra_info, FIL fprintf(outfile, " %8d %4d %8X 0 0 0 0 0", 130000, 130, flags); } +#define DEVSZ 22 +#define EXTRASZ 20 +#define TIMESZ sizeof("(1345212884.318850) ") +#define BUFSZ (DEVSZ + AFRSZ + EXTRASZ + TIMESZ) + +/* adapt sscanf() functions below on error */ +#if (AFRSZ != 6300) +#error "AFRSZ value does not fit sscanf restrictions!" +#endif +#if (DEVSZ != 22) +#error "DEVSZ value does not fit sscanf restrictions!" +#endif +#if (EXTRASZ != 20) +#error "EXTRASZ value does not fit sscanf restrictions!" +#endif + int main(int argc, char **argv) { - static char buf[BUFSZ], device[BUFSZ], afrbuf[AFRSZ], extra_info[BUFSZ]; + static char buf[BUFSZ], device[DEVSZ], afrbuf[AFRSZ], extra_info[EXTRASZ]; static cu_t cu; static struct timeval tv, start_tv; @@ -261,13 +275,13 @@ int main(int argc, char **argv) if (buf[0] != '(') continue; - if (sscanf(buf, "(%llu.%llu) %s %s %s", &sec, &usec, + if (sscanf(buf, "(%llu.%llu) %21s %6299s %19s", &sec, &usec, device, afrbuf, extra_info) != 5) { /* do not evaluate the extra info */ extra_info[0] = 0; - if (sscanf(buf, "(%llu.%llu) %s %s", &sec, &usec, + if (sscanf(buf, "(%llu.%llu) %21s %6299s", &sec, &usec, device, afrbuf) != 4) { fprintf(stderr, "incorrect line format in logfile\n"); return 1; diff --git a/log2long.c b/log2long.c index 332aa6c..0fcb0cc 100644 --- a/log2long.c +++ b/log2long.c @@ -43,26 +43,45 @@ */ #include +#include #include #include #include "lib.h" -#define COMMENTSZ 200 -#define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ) /* for one line in the logfile */ +#define DEVSZ 22 +#define TIMESZ 22 /* sizeof("(1345212884.318850) ") */ +#define BUFSZ (DEVSZ + AFRSZ + TIMESZ) + +/* adapt sscanf() functions below on error */ +#if (AFRSZ != 6300) +#error "AFRSZ value does not fit sscanf restrictions!" +#endif +#if (DEVSZ != 22) +#error "DEVSZ value does not fit sscanf restrictions!" +#endif +#if (TIMESZ != 22) +#error "TIMESZ value does not fit sscanf restrictions!" +#endif int main(void) { - char buf[BUFSZ], timestamp[BUFSZ], device[BUFSZ], ascframe[BUFSZ]; + char buf[BUFSZ], timestamp[TIMESZ], device[DEVSZ], afrbuf[AFRSZ]; static cu_t cu; int mtu; while (fgets(buf, BUFSZ-1, stdin)) { - if (sscanf(buf, "%s %s %s", timestamp, device, ascframe) != 3) + + if (strlen(buf) >= BUFSZ-2) { + fprintf(stderr, "line too long for input buffer\n"); + return 1; + } + + if (sscanf(buf, "%21s %21s %6299s", timestamp, device, afrbuf) != 3) return 1; - mtu = parse_canframe(ascframe, &cu); + mtu = parse_canframe(afrbuf, &cu); /* mark dual-use struct canfd_frame - no CAN_XL support */ if (mtu == CAN_MTU) @@ -75,10 +94,10 @@ int main(void) } /* with ASCII output */ - sprint_long_canframe(ascframe, &cu, + sprint_long_canframe(afrbuf, &cu, (CANLIB_VIEW_INDENT_SFF | CANLIB_VIEW_ASCII)); - printf("%s %s %s\n", timestamp, device, ascframe); + printf("%s %s %s\n", timestamp, device, afrbuf); } return 0;