Skip to contentSkip to conversation

Accept named realm

PATCH Applied Simon Lodal 2 participants 3 messages May 21, 2006 – May 24, 2006 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 Accept named realm

Make the realm match accept named realms, defined in /etc/iproute2/rt_realms.

extensions/libipt_realm.c +57 -7
diff -ruN iptables-1.3.5.mod1/extensions/libipt_realm.c iptables-1.3.5.mod2/extensions/libipt_realm.c
@@ -3,6 +3,7 @@
 #include <netdb.h>
 #include <string.h>
 #include <stdlib.h>
+#include <errno.h>
 #include <getopt.h>
 #if defined(__GLIBC__) && __GLIBC__ == 2
 #include <net/ethernet.h>
@@ -28,6 +29,46 @@
 	{0}
 };
 
+/* Lookup realm in /etc/iproute2/rt_realms. Return: True and realm id in *rid
+ * if found, false and *rid not touched if not found.
+ */
+static int
+find_named_realm(const char* rnm, u_int32_t* rid)
+{
+	const char* rfnm = "/etc/iproute2/rt_realms";
+	char buf[512];
+	FILE *fil;
+	char *cur, *nxt;
+	unsigned long int id;
+	int	len = strlen(rnm);
+
+	fil = fopen(rfnm, "r");
+	if (!fil) return 0;
+
+	while (fgets(buf, sizeof(buf), fil)) {
+		cur = buf;
+		while ((*cur == ' ') || (*cur == '\t')) cur++;
+		if ((*cur == '#') || (*cur == '\n')) continue;
+
+		id = strtoul(cur, &nxt, 0);
+		if ((nxt == cur) || errno) continue;
+		cur = nxt;
+
+		while ((*cur == ' ') || (*cur == '\t')) cur++;
+		if (strncmp(cur, rnm, len)) continue;
+		nxt = cur + len;
+		while ((*nxt == ' ') || (*nxt == '\t')) nxt++;
+		if ((*nxt == '\n') || (*nxt == 0) || (*nxt == '#')) {
+			*rid = (u_int32_t)id;
+			fclose(fil);
+			return 1;
+		}
+	}
+
+	fclose(fil);
+	return 0;
+}
+
 /* Function which parses command options; returns true if it
    ate an option */
 static int
@@ -42,14 +83,23 @@
 		char *end;
 	case '1':
 		check_inverse(argv[optind-1], &invert, &optind, 0);
-		optarg = argv[optind-1];
+		end = optarg = argv[optind-1];
 		realminfo->id = strtoul(optarg, &end, 0);
-		if (*end == '/') {
-			realminfo->mask = strtoul(end+1, &end, 0);
-		} else
-			realminfo->mask = 0xffffffff;
-		if (*end != '\0' || end == optarg)
-			exit_error(PARAMETER_PROBLEM, "Bad realm value `%s'", optarg);
+		if ((end != optarg) && (('/' == *end) || ('\0' == *end))) {
+			if (*end == '/') {
+				realminfo->mask = strtoul(end+1, &end, 0);
+			} else
+				realminfo->mask = 0xffffffff;
+			if (*end != '\0' || end == optarg)
+				exit_error(PARAMETER_PROBLEM,
+					   "Bad realm value `%s'", optarg);
+		} else {
+			if (find_named_realm(optarg, &realminfo->id))
+				realminfo->mask = 0xffffffff;
+			else
+				exit_error(PARAMETER_PROBLEM,
+					   "Realm `%s' not found", optarg);
+		}
 		if (invert)
 			realminfo->invert = 1;
 		*flags = 1;
extensions/libipt_realm.man +4 -2
diff -ruN iptables-1.3.5.mod1/extensions/libipt_realm.man iptables-1.3.5.mod2/extensions/libipt_realm.man
@@ -1,5 +1,7 @@
 This matches the routing realm.  Routing realms are used in complex routing
 setups involving dynamic routing protocols like BGP.
 .TP
-.BI "--realm " "[!]" "value[/mask]"
-Matches a given realm number (and optionally mask).
+.BI "--realm " "[!] " "value[/mask]"
+Matches a given realm number (and optionally mask). If not a number, value
+can be a named realm from /etc/iproute2/rt_realms (mask can not be used in
+that case).
#1 lore raw
Quoted 1 line · Simon Lodal

Make the realm match accept named realms, defined in /etc/iproute2/rt_realms.

I think this is a good idea, but it should also show realm names when listing without -n. For that it makes little sense to read in the realms file once for each rule shown, so you should keep a map of realm->name values. I actually had a patch to do exactly that long ago, but I lost it.

#2 lore raw
Quoted 9 lines · Patrick McHardy
Quoted 2 lines · Simon Lodal

Make the realm match accept named realms, defined in /etc/iproute2/rt_realms.

I think this is a good idea, but it should also show realm names when listing without -n. For that it makes little sense to read in the realms file once for each rule shown, so you should keep a map of realm->name values. I actually had a patch to do exactly that long ago, but I lost it.

Same thought, I will post another patch soon.