changeset 855:ea458ee48691

rvinterf/l1filter: new program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 09 Nov 2021 23:14:22 +0000
parents 74331b35b1da
children ab20ff8156ea
files .hgignore rvinterf/l1filter/Makefile rvinterf/l1filter/init.c rvinterf/l1filter/main.c rvinterf/l1filter/pktsort.c rvinterf/l1filter/rvif_rx.c
diffstat 6 files changed, 325 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Nov 09 16:39:52 2021 +0000
+++ b/.hgignore	Tue Nov 09 23:14:22 2021 +0000
@@ -56,6 +56,7 @@
 ^rvinterf/etmsync/fc-memdump$
 ^rvinterf/etmsync/fc-readcal$
 ^rvinterf/etmsync/fc-tmsync$
+^rvinterf/l1filter/l1trace-filter$
 ^rvinterf/lowlevel/rvinterf$
 ^rvinterf/lowlevel/rvtdump$
 ^rvinterf/lowlevel/tfc139$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/l1filter/Makefile	Tue Nov 09 23:14:22 2021 +0000
@@ -0,0 +1,22 @@
+CC=	gcc
+CFLAGS=	-O2
+CPPFLAGS=-I../include
+PROG=	l1trace-filter
+OBJS=	init.o main.o pktsort.o rvif_rx.o
+LIBS=	../libinterf/libinterf.a
+
+INSTALL_PREFIX=	/opt/freecalypso
+
+INSTBIN=${INSTALL_PREFIX}/bin
+
+all:	${PROG}
+
+${PROG}: ${OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS}
+
+install:	${PROG}
+	mkdir -p ${INSTBIN}
+	install -c ${PROG} ${INSTBIN}
+
+clean:
+	rm -f *.o *.out *errs ${PROG}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/l1filter/init.c	Tue Nov 09 23:14:22 2021 +0000
@@ -0,0 +1,31 @@
+/*
+ * This module implements some init for l1trace-filter.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "pktmux.h"
+#include "localsock.h"
+
+extern int sock;
+
+send_init_command(cmdpkt, cmdlen)
+	u_char *cmdpkt;
+{
+	u_char lenbuf[2];
+
+	lenbuf[0] = 0;
+	lenbuf[1] = cmdlen;
+	write(sock, lenbuf, 2);
+	write(sock, cmdpkt, cmdlen);
+}
+
+init()
+{
+	static u_char want_l1_mux[2] = {CLI2RVI_WANT_MUXPROTO, RVT_L1_HEADER};
+
+	localsock_prep_for_length_rx();
+	send_init_command(want_l1_mux, 2);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/l1filter/main.c	Tue Nov 09 23:14:22 2021 +0000
@@ -0,0 +1,87 @@
+/*
+ * This module contains the main() function for l1trace-filter.
+ */
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "exitcodes.h"
+
+extern char *socket_pathname;
+extern char *rvinterf_ttyport, *rvinterf_Bopt, *rvinterf_lopt, *rvinterf_wopt;
+extern int sock;
+
+char **filter_list;
+
+main(argc, argv)
+	char **argv;
+{
+	extern int optind;
+	extern char *optarg;
+	int c, sopt = 0;
+	fd_set fds;
+
+	while ((c = getopt(argc, argv, "B:l:p:s:w:")) != EOF)
+		switch (c) {
+		case 'B':
+			rvinterf_Bopt = optarg;
+			continue;
+		case 'l':
+			rvinterf_lopt = optarg;
+			continue;
+		case 'p':
+			rvinterf_ttyport = optarg;
+			continue;
+		case 's':
+			socket_pathname = optarg;
+			sopt++;
+			continue;
+		case 'w':
+			rvinterf_wopt = optarg;
+			continue;
+		case '?':
+		default:
+			/* error msg already printed */
+			exit(ERROR_USAGE);
+		}
+	if (!argv[optind]) {
+		fprintf(stderr, "error: no filter keywords specified\n");
+		exit(ERROR_USAGE);
+	}
+	if (rvinterf_ttyport) {
+		if (sopt) {
+			fprintf(stderr,
+			"%s error: -p and -s options are mutually exclusive\n",
+				argv[0]);
+			exit(ERROR_USAGE);
+		}
+		launch_rvinterf(0);
+	} else {
+		if (rvinterf_Bopt || rvinterf_lopt || rvinterf_wopt) {
+			fprintf(stderr,
+"%s error: -B, -l and -w options are meaningful only when launching rvinterf\n",
+				argv[0]);
+			exit(ERROR_USAGE);
+		}
+		connect_local_socket();
+	}
+
+	filter_list = argv + optind;
+	init();
+	for (;;) {
+		FD_ZERO(&fds);
+		FD_SET(sock, &fds);
+		c = select(sock+1, &fds, 0, 0, 0);
+		if (c < 0) {
+			if (errno == EINTR)
+				continue;
+			perror("select");
+			exit(ERROR_UNIX);
+		}
+		if (FD_ISSET(sock, &fds))
+			handle_rvinterf_input();
+		fflush(stdout);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/l1filter/pktsort.c	Tue Nov 09 23:14:22 2021 +0000
@@ -0,0 +1,96 @@
+/*
+ * Here we sort out incoming packets from the target relayed via rvinterf.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include "pktmux.h"
+#include "limits.h"
+#include "localsock.h"
+#include "localtypes.h"
+#include "exitcodes.h"
+
+extern u_char rvi_msg[];
+extern int rvi_msg_len;
+extern char **filter_list;
+
+static
+is_filter_match()
+{
+	char **fp;
+	int len;
+
+	for (fp = filter_list; *fp; fp++) {
+		len = strlen(*fp);
+		if (rvi_msg_len < len + 3)
+			continue;
+		if (strncmp(rvi_msg + 2, *fp, len))
+			continue;
+		if (rvi_msg[len+2] != ' ')
+			continue;
+		return 1;
+	}
+	return 0;
+}
+
+static void
+print_l1_trace()
+{
+	u_char *dp, *endp;
+	int c, newline;
+
+	dp = rvi_msg + 2;
+	endp = rvi_msg + rvi_msg_len;
+	while (dp < endp) {
+		c = *dp++;
+		if (c == '\r')
+			continue;
+		if (c == '\n') {
+			putchar(c);
+			newline = 1;
+			continue;
+		}
+		newline = 0;
+		if (c & 0x80) {
+			putchar('M');
+			putchar('-');
+			c &= 0x7F;
+		}
+		if (c == 0x7F) {
+			putchar('^');
+			putchar('?');
+			continue;
+		}
+		if (c < 0x20) {
+			putchar('^');
+			c += '@';
+		}
+		putchar(c);
+	}
+	if (!newline)
+		putchar('\n');
+}
+
+static void
+l1_packet_rx()
+{
+	if (is_filter_match())
+		print_l1_trace();
+}
+
+void
+process_pkt_from_target()
+{
+	switch (rvi_msg[1]) {
+	case RVT_L1_HEADER:
+		l1_packet_rx();
+		return;
+	default:
+		fprintf(stderr, "unexpected fwd of MUX %02X from rvinterf\n",
+			rvi_msg[1]);
+		exit(ERROR_RVINTERF);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/l1filter/rvif_rx.c	Tue Nov 09 23:14:22 2021 +0000
@@ -0,0 +1,88 @@
+/*
+ * This module implements message Rx from rvinterf.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "localsock.h"
+#include "exitcodes.h"
+
+extern int sock;
+
+u_char rvi_msg[LOCALSOCK_MAX_MSG];
+int rvi_msg_len;
+
+static int rx_state, rx_left;
+static u_char *rx_ptr;
+
+void
+localsock_prep_for_length_rx()
+{
+	rx_state = 0;
+	rx_ptr = rvi_msg;
+	rx_left = 2;
+}
+
+static void
+prep_for_message_rx()
+{
+	rx_state = 1;
+	rx_ptr = rvi_msg;
+	rx_left = rvi_msg_len;
+}
+
+void
+process_msg_from_rvinterf()
+{
+	switch (rvi_msg[0]) {
+	case RVI2CLI_PKT_FROM_TARGET:
+		process_pkt_from_target();
+		return;
+	case RVI2CLI_LOCAL_CMD_RESP:
+		if (rvi_msg_len < 2)
+			goto bad;
+		if (rvi_msg[1] == '+')
+			return;
+		fprintf(stderr, "Error from rvinterf: %.*s\n", rvi_msg_len - 1,
+			rvi_msg + 1);
+		exit(ERROR_RVINTERF);
+	default:
+	bad:
+		fprintf(stderr,
+			"Error: unexpected message type %02X from rvinterf\n",
+			rvi_msg[0]);
+		exit(ERROR_RVINTERF);
+	}
+}
+
+void
+handle_rvinterf_input()
+{
+	int cc;
+
+	cc = read(sock, rx_ptr, rx_left);
+	if (cc <= 0) {
+		perror("read from rvinterf socket");
+		exit(ERROR_RVINTERF);
+	}
+	rx_ptr += cc;
+	rx_left -= cc;
+	if (rx_left)
+		return;
+	/* got the thing, process it */
+	if (rx_state) {
+		process_msg_from_rvinterf();
+		localsock_prep_for_length_rx();
+	} else {
+		rvi_msg_len = rvi_msg[0] << 8 | rvi_msg[1];
+		if (rvi_msg_len < 1 || rvi_msg_len > LOCALSOCK_MAX_MSG) {
+			fprintf(stderr,
+				"Invalid length from rvinterf: %02X%02X\n",
+				rvi_msg[0], rvi_msg[1]);
+			exit(ERROR_RVINTERF);
+		}
+		prep_for_message_rx();
+	}
+}