Skip to contentSkip to conversation

binary protocol support + backticks for nfsim

PATCH New Max Kellermann 1 participant 5 messages Feb 21, 2005 netfilter

lore
Share & tools
lore
#0 lore raw

cover binary protocol support + backticks for nfsim

Hi,

this set of 4 patches adds support for binary protocols by escaping all non-printable and 8 bit characters and by adding the "backtick" feature known from the shell and perl. This enables you to write a small program which generates packets for nfsim.

Without these patches, it is a major pain to write nfsim tests for binary protocols like H.323.

Consider the following snippet (NAT helper test):

expect gen_ip send:eth1 {*DATA ./generate-h225-connect 192.168.0.2} tcpsession DATA original ./generate-h225-connect 192.168.0.1

Now imagine the same expressed in inlined hex notation.

Max

#1 lore raw

PATCH 1/4 Linux 2.6.11 API fixes

This patch ports nfsim to the Linux 2.6.11-rc4 API.

Mon Feb 21 16:47:08 CET 2005 [email protected]
* linux 2.6.11-rc4 api

core/ipv4/ipv4.c +1 -1
diff -rN -u nfsim-1-old/core/ipv4/ipv4.c nfsim-1-new/core/ipv4/ipv4.c
@@ -530,7 +530,7 @@
 	return skb;
 }
 
-struct sk_buff *ip_defrag(struct sk_buff *skb)
+struct sk_buff *ip_defrag(struct sk_buff *skb, u32 user)
 {
 	struct fraglist *i;
 
core/ipv4/ipv4.h +1 -1
diff -rN -u nfsim-1-old/core/ipv4/ipv4.h nfsim-1-new/core/ipv4/ipv4.h
@@ -562,7 +562,7 @@
 int ip_route(struct sk_buff *skb);
 
 int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff*));
-struct sk_buff *ip_defrag(struct sk_buff *skb);
+struct sk_buff *ip_defrag(struct sk_buff *skb, u32 user);
 void ipfrag_flush(void);
 
 void icmp_send(struct sk_buff *skb_in, int type, int code, u32 info);
kernelenv/include/kernelenv.h +12 -0
diff -rN -u nfsim-1-old/kernelenv/include/kernelenv.h nfsim-1-new/kernelenv/include/kernelenv.h
@@ -40,6 +40,18 @@
 #include <ctype.h>
 #include <list.h>
 
+enum ip_defrag_users
+{
+        IP_DEFRAG_LOCAL_DELIVER,
+        IP_DEFRAG_CALL_RA_CHAIN,
+        IP_DEFRAG_CONNTRACK_IN,
+        IP_DEFRAG_CONNTRACK_OUT,
+        IP_DEFRAG_NAT_OUT,
+        IP_DEFRAG_VS_IN,
+        IP_DEFRAG_VS_OUT,
+        IP_DEFRAG_VS_FWD
+};
+
 /* Types */
 typedef uint8_t u8;
 typedef int8_t s8;
#2 lore raw

PATCH 2/4 check current_command in expect_log_hook()

In reply to Max Kellermann

Allow script_fail() calls even when there is no current_command parsed yet.

Mon Feb 21 16:49:24 CET 2005 [email protected]
* allow script_fail call even when there is no current_command parsed yet

core/expect.c +3 -0
diff -rN -u nfsim-1-old/core/expect.c nfsim-1-new/core/expect.c
@@ -130,6 +130,9 @@
 	struct expect *e;
 	bool ret = false;
 
+	if (current_command == NULL)
+		return false;
+
 	/* Only allow each pattern to match once, so we can easily
 	 * expect something to happen twice. */
 	for (e = expect; e; e = e->next) {
#3 lore raw

PATCH 3/4 escape non-printables and 8bit characters

In reply to Max Kellermann

escape all non-printable and 8 bit characters in print_data(). This is required to write nfsim scripts in 7bit US-ASCII.

This patch is actually a repost from:
http://lists.netfilter.org/pipermail/netfilter-devel/2005-February/018451.html

Mon Feb 21 16:51:04 CET 2005 [email protected]
* escape all non-printable characters, including 8bit

core/ipv4/ipv4.c +6 -0
diff -rN -u nfsim-1-old/core/ipv4/ipv4.c nfsim-1-new/core/ipv4/ipv4.c
@@ -740,6 +740,7 @@
 
 static char *print_data(char *ptr, const char *data, int len)
 {
+	static const char hexbuf[]= "0123456789abcdef";
 	int i;
 
 	ptr += sprintf(ptr, "DATA ");
@@ -759,6 +760,11 @@
 		} else if (data[i] == '\\') {
 			*ptr++ = '\\';
 			*ptr++ = '\\';
+		} else if (data[i] & 0x80 || (data[i] & 0xe0) == 0) {
+			*ptr++ = '\\';
+			*ptr++ = 'x';
+			*ptr++ = hexbuf[(data[i] >> 4) & 0xf];
+			*ptr++ = hexbuf[data[i] & 0xf];
 		} else
 			*ptr++ = data[i];
 	}
#4 lore raw

PATCH 4/4 implement backticks

In reply to Max Kellermann

expand backticks to the output of the specified command

Mon Feb 21 17:50:22 CET 2005 [email protected]
* implemented backticks

core/tui.c +148 -0
diff -rN -u nfsim-1-old/core/tui.c nfsim-1-new/core/tui.c
@@ -25,6 +25,7 @@
 #include "expect.h"
 #include "utils.h"
 
+#include <sys/wait.h>
 #include <stdio.h>
 #include <readline/readline.h>
 #include <readline/history.h>
@@ -220,6 +221,67 @@
 	return false;
 }
 
+/**
+ * backslash-escape a binary data block into a newly allocated
+ * string
+ *
+ * @param src a pointer to the data block
+ * @param src_len the length of the data block
+ * @return NULL if out of memory, or a pointer to the allocated escaped
+ *    string, which is terminated with a '\0' character
+ */
+static char *escape(const char *src, size_t src_len) {
+	static const char hexbuf[]= "0123456789abcdef";
+	char *dest, *p;
+	size_t i;
+
+	/* src_len * 4 is always safe, it's the longest escape
+	   sequence for all characters */
+	dest = malloc(src_len * 4 + 1);
+	if (dest == NULL)
+		return NULL;
+
+	p = dest;
+
+	for (i = 0; i < src_len; i++) {
+		if (src[i] == '\n') {
+			*p++ = '\\';
+			*p++ = 'n';
+		} else if (src[i] == '\r') {
+			*p++ = '\\';
+			*p++ = 'r';
+		} else if (src[i] == '\0') {
+			*p++ = '\\';
+			*p++ = '0';
+		} else if (src[i] == '\t') {
+			*p++ = '\\';
+			*p++ = 't';
+		} else if (src[i] == '\\') {
+			*p++ = '\\';
+			*p++ = '\\';
+		} else if (src[i] & 0x80 || (src[i] & 0xe0) == 0) {
+			*p++ = '\\';
+			*p++ = 'x';
+			*p++ = hexbuf[(src[i] >> 4) & 0xf];
+			*p++ = hexbuf[src[i] & 0xf];
+		} else
+			*p++ = src[i];
+	}
+
+	*p++ = 0;
+
+	i = p - dest;
+
+	/* reallocate to save memory */
+	p = realloc(dest, i);
+	if (p == NULL) {
+		free(dest);
+		return NULL;
+	}
+
+	return p;
+}
+
 static void process_line(char *line)
 {
 	int argc, prevspace;
@@ -237,6 +299,92 @@
 		if (isspace(*line)) {
 			*line = 0;
 			prevspace = 1;
+		} else if (prevspace && *line == '`') {
+			char *end;
+			pid_t pid, pid2;
+			int ret, fds[2];
+			char *buffer = NULL, *new_buffer;
+			size_t buffer_size = 0, buffer_pos = 0;
+			ssize_t nbytes;
+
+			/* find the end delimiter */
+			end = strchr(++line, '`');
+			if (end == NULL)
+				script_fail("no matching \"`\" found");
+
+			/* fork and execute the specified program in a
+			   shell */
+			ret = pipe(fds);
+			if (ret < 0)
+				script_fail("pipe() failed: %s",
+					    strerror(errno));
+
+			pid = fork();
+			if (pid < 0) {
+				script_fail("fork() failed: %s",
+					    strerror(errno));
+			} else if (pid == 0) {
+				dup2(fds[1], 1);
+				close(fds[0]);
+				close(fds[1]);
+
+				*end = 0;
+				execl("/bin/sh", "sh", "-c", line, NULL);
+				fprintf(stderr, "failed to execute '%s': %s\n",
+					line, strerror(errno));
+				exit(1);
+			}
+
+			close(fds[1]);
+
+			/* read output from the child process */
+			while (1) {
+				/* grow the buffer if required; limit
+				   that to 1 MB for now */
+				if (buffer_pos == buffer_size &&
+				    buffer_size < 1024 * 1024) {
+					buffer_size += 4096;
+					new_buffer = realloc(buffer, buffer_size);
+					if (new_buffer == NULL)
+						script_fail("out of memory");
+					buffer = new_buffer;
+				}
+
+				/* read the next chunk */
+				nbytes = read(fds[0], buffer + buffer_pos, buffer_size - buffer_pos);
+				if (nbytes <= 0)
+					break;
+
+				buffer_pos += (size_t)nbytes;
+			}
+
+			close(fds[0]);
+
+			/* check child's exit status */
+			pid2 = waitpid(pid, &ret, 0);
+			if (pid2 != pid)
+				script_fail("waitpid() failed");
+
+			if (WIFSIGNALED(ret) != 0)
+				script_fail("child terminated by signal %d",
+					    WIFSIGNALED(ret));
+
+			if (WEXITSTATUS(ret) != 0)
+				script_fail("child exited with status %d",
+					    WEXITSTATUS(ret));
+
+			/* escape the output */
+			argv[argc] = escape(buffer, buffer_pos);
+			if (argv[argc] == NULL)
+				script_fail("out of memory");
+
+			/* XXX: the escaped buffer is never freed */
+
+			free(buffer);
+
+			argc++;
+			prevspace = 0;
+			line = end;
 		} else if (prevspace) {
 			argv[argc++] = line;
 			prevspace = 0;