changeset 178:7ab6b29e76bb

rvinterf: forwarding of Rx packets to clients implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 23 Nov 2013 22:15:16 +0000
parents fef035264dd4
children ebfa9657d03d
files rvinterf/localsock.h rvinterf/lowlevel/Makefile rvinterf/lowlevel/clientcmd.c rvinterf/lowlevel/localsock.c rvinterf/lowlevel/pktfwd.c rvinterf/lowlevel/rvifmain.c
diffstat 6 files changed, 168 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/rvinterf/localsock.h	Sat Nov 23 20:15:02 2013 +0000
+++ b/rvinterf/localsock.h	Sat Nov 23 22:15:16 2013 +0000
@@ -5,9 +5,8 @@
  * The UNIX domain sockets used for this ad hoc interface are of the
  * SOCK_STREAM kind, but the true nature of the communication is message-based.
  * We use the same trick that is used for DNS over TCP: every message in each
- * direction is preceded by a 2-byte length.  This length and all other
- * multibyte numbers are sent in the native byte order of the machine on which
- * the rvinterf suite is running.  The limit on the size of these messages
+ * direction is preceded by a 2-byte length.  This length is sent MSB first
+ * just like in DNS over TCP.  The limit on the size of these messages
  * (for sizing buffers etc) is:
  */
 
@@ -29,12 +28,11 @@
  * interested in receiving copies of certain packets coming from the target.
  *
  * The CLI2RVI_WANT_RVTRACE opcode needs to be followed by a USEID mask value
- * and a USEID match value, both in the native byte order of the machine
- * running rvinterf, for a total message length of 9 bytes.  For every RV
- * trace message received from the target, rvinterf will iterate through all
- * active clients to see who is interested: if the received USEID ANDed with
- * the mask equals the match value, the message will be forwarded to that
- * client.
+ * and a USEID match value, both in the network byte order, i.e., MSB first,
+ * for a total message length of 9 bytes.  For every RV trace message received
+ * from the target, rvinterf will iterate through all active clients to see who
+ * is interested: if the received USEID ANDed with the mask equals the match
+ * value, the message will be forwarded to that client.
  *
  * The CLI2RVI_WANT_MUXPROTO opcode needs to be followed by one byte
  * identifying the RVTMUX protocol of interest, i.e., the first byte of the
--- a/rvinterf/lowlevel/Makefile	Sat Nov 23 20:15:02 2013 +0000
+++ b/rvinterf/lowlevel/Makefile	Sat Nov 23 22:15:16 2013 +0000
@@ -5,8 +5,8 @@
 
 RVTDUMP_OBJS=	format.o format_g23.o openport.o output.o packetrx.o rvtdump.o
 
-RVINTERF_OBJS=	format.o format_g23.o localsock.o logsent.o openport.o output.o\
-		packetrx.o packettx.o rvifmain.o
+RVINTERF_OBJS=	clientcmd.o format.o format_g23.o localsock.o logsent.o \
+		openport.o output.o packetrx.o packettx.o pktfwd.o rvifmain.o
 
 all:	${PROGS}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/lowlevel/clientcmd.c	Sat Nov 23 22:15:16 2013 +0000
@@ -0,0 +1,61 @@
+/*
+ * This rvinterf module implements the handling of client commands.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "../pktmux.h"
+#include "../localsock.h"
+#include "client.h"
+
+void
+process_msg_from_client(cli)
+	struct client *cli;
+{
+	int c;
+
+	switch (cli->rx_buf[0]) {
+	case CLI2RVI_WANT_RVTRACE:
+		if (cli->rx_msglen != 9) {
+			send_local_msg_to_client(cli, "-Bad command length");
+			return;
+		}
+		c = cli->int_rvt_count;
+		if (c >= MAX_RVT_INTEREST) {
+			send_local_msg_to_client(cli,
+					"-Error: too many RVT interests");
+			return;
+		}
+		cli->int_rvt_mask[c] = cli->rx_buf[1] << 24 |
+					cli->rx_buf[2] << 16 |
+					cli->rx_buf[3] << 8 | cli->rx_buf[4];
+		cli->int_rvt_match[c] = cli->rx_buf[5] << 24 |
+					cli->rx_buf[6] << 16 |
+					cli->rx_buf[7] << 8 | cli->rx_buf[8];
+		cli->int_rvt_count++;
+		send_local_msg_to_client(cli, "+OK");
+		return;
+	case CLI2RVI_WANT_MUXPROTO:
+		if (cli->rx_msglen != 2) {
+			send_local_msg_to_client(cli, "-Bad command length");
+			return;
+		}
+		if (cli->rx_buf[1] < 0x12 || cli->rx_buf[1] > 0x18) {
+			send_local_msg_to_client(cli,
+					"-Unsupported protocol MUX value");
+			return;
+		}
+		cli->int_proto[cli->rx_buf[1] - 0x12] = 1;
+		send_local_msg_to_client(cli, "+OK");
+		return;
+	case CLI2RVI_PKT_TO_TARGET:
+	case CLI2RVI_RAWBYTES_TO_TARGET:
+		/* To be implemented */
+	default:
+		send_local_msg_to_client(cli, "-Unimplemented command");
+	}
+}
--- a/rvinterf/lowlevel/localsock.c	Sat Nov 23 20:15:02 2013 +0000
+++ b/rvinterf/lowlevel/localsock.c	Sat Nov 23 22:15:16 2013 +0000
@@ -110,6 +110,22 @@
 	return(0);
 }
 
+send_local_msg_to_client(cli, msg)
+	struct client *cli;
+	char *msg;
+{
+	int len, len1;
+	u_char hdr[3];
+
+	len = strlen(msg);
+	len1 = len + 1;
+	hdr[0] = len1 >> 8;
+	hdr[1] = len1 & 0xFF;
+	hdr[2] = RVI2CLI_LOCAL_CMD_RESP;
+	write(cli->fd, hdr, 3);
+	write(cli->fd, msg, len);
+}
+
 void
 handle_client_select(cli)
 	struct client *cli;
@@ -129,11 +145,12 @@
 	/* got the thing, process it */
 	if (cli->rx_state) {
 		prep_for_length_rx(cli);
-		/* process_msg_from_client(cli); */
+		process_msg_from_client(cli);
 	} else {
-		cli->rx_msglen = *(u_short *)cli->rx_buf;
+		cli->rx_msglen = cli->rx_buf[0] << 8 | cli->rx_buf[1];
 		if (cli->rx_msglen < 1 || cli->rx_msglen > LOCALSOCK_MAX_MSG) {
-			/* TODO: report invalid length to the client */
+			send_local_msg_to_client(cli,
+					"-Invalid length, closing socket");
 			goto close_socket;
 		}
 		prep_for_message_rx(cli);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/lowlevel/pktfwd.c	Sat Nov 23 22:15:16 2013 +0000
@@ -0,0 +1,62 @@
+/*
+ * This rvinterf module handles the forwarding of target->host packets
+ * to clients connected via the local socket interface.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "../pktmux.h"
+#include "../localsock.h"
+#include "client.h"
+
+extern u_char rxpkt[];
+extern size_t rxpkt_len;
+
+extern struct client *client_head;
+
+forward_pkt_to_client(cli)
+	struct client *cli;
+{
+	int len1;
+	u_char hdr[3];
+
+	len1 = rxpkt_len + 1;
+	hdr[0] = len1 >> 8;
+	hdr[1] = len1 & 0xFF;
+	hdr[2] = RVI2CLI_PKT_FROM_TARGET;
+	write(cli->fd, hdr, 3);
+	write(cli->fd, rxpkt, rxpkt_len);
+}
+
+forward_rv_trace()
+{
+	u32 useid;
+	struct client *cli;
+	int i, match;
+
+	useid = rxpkt[1] << 24 | rxpkt[2] << 16 | rxpkt[3] << 8 | rxpkt[4];
+	for (cli = client_head; cli; cli = cli->next) {
+		match = 0;
+		for (i = 0; i < cli->int_rvt_count; i++)
+			if ((useid & cli->int_rvt_mask[i]) ==
+			    cli->int_rvt_match[i]) {
+				match = 1;
+				break;
+			}
+		if (match)
+			forward_pkt_to_client(cli);
+	}
+}
+
+forward_nonrvt_pkt()
+{
+	struct client *cli;
+
+	for (cli = client_head; cli; cli = cli->next)
+		if (cli->int_proto[rxpkt[0] - 0x12])
+			forward_pkt_to_client(cli);
+}
--- a/rvinterf/lowlevel/rvifmain.c	Sat Nov 23 20:15:02 2013 +0000
+++ b/rvinterf/lowlevel/rvifmain.c	Sat Nov 23 22:15:16 2013 +0000
@@ -123,16 +123,28 @@
 	case RVT_RV_HEADER:
 		if (rxpkt_len < 6)
 			goto unknown;
-		print_rv_trace();
+		if (!background || logF)
+			print_rv_trace();
+		if (client_head)
+			forward_rv_trace();
 		return;
 	case RVT_L1_HEADER:
-		print_l1_trace();
+		if (!background || logF)
+			print_l1_trace();
+		if (client_head)
+			forward_nonrvt_pkt();
 		return;
 	case RVT_L23_HEADER:
-		print_g23_trace();
+		if (!background || logF)
+			print_g23_trace();
+		if (client_head)
+			forward_nonrvt_pkt();
 		return;
 	case RVT_TM_HEADER:
-		print_etm_output_raw();
+		if (!background || logF)
+			print_etm_output_raw();
+		if (client_head)
+			forward_nonrvt_pkt();
 		return;
 	default:
 	unknown: