Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 11 Dec 1999 15:33:58 +0600 (ALMT)
From:      Boris Popov <bp@butya.kz>
To:        freebsd-net@freebsd.org
Subject:   [patch for review] ifunit() and subinterfaces
Message-ID:  <Pine.BSF.4.10.9912111519550.56196-100000@lion.butya.kz>

next in thread | raw e-mail | index | archive | help
	Hello,

	Current implementation of ifunit() function does not allow using
interface names like 'ed0blah2', eg. it finds first numeric character and
starts calculation of unit number. However it is usable in some cases to
have names which looks like subinterfaces (ed0f1, ed0f2) of existing
interface.

	So I'm propose the following patch to do it (as a side effect
function also getting a little bit smaller):

diff -u org/if.c ./if.c
--- org/if.c	Sat Dec 11 12:04:08 1999
+++ ./if.c	Sat Dec 11 13:49:36 1999
@@ -597,41 +597,36 @@
  * interface structure pointer.
  */
 struct ifnet *
-ifunit(name)
-	register char *name;
+ifunit(char *name)
 {
 	char namebuf[IFNAMSIZ + 1];
-	register char *cp, *cp2;
-	char *end;
-	register struct ifnet *ifp;
+	char *cp;
+	struct ifnet *ifp;
 	int unit;
-	unsigned len;
-	register char c = '\0';
+	unsigned len, m;
+	char c;
 
-	/*
-	 * Look for a non numeric part
-	 */
-	end = name + IFNAMSIZ;
-	cp2 = namebuf;
-	cp = name;
-	while ((cp < end) && (c = *cp)) {
-		if (c >= '0' && c <= '9')
-			break;
-		*cp2++ = c;
-		cp++;
-	}
-	if ((cp == end) || (c == '\0') || (cp == name))
-		return ((struct ifnet *)0);
-	*cp2 = '\0';
-	/*
-	 * check we have a legal number (limit to 7 digits?)
-	 */
+	len = strlen(name);
+	if (len < 2 || len > IFNAMSIZ)
+		return NULL;
+	cp = name + len - 1;
+	c = *cp;
+	if (c < '0' || c > '9')
+		return NULL;		/* trailing garbage */
+	unit = 0;
+	m = 1;
+	do {
+		if (cp == name)
+			return NULL;	/* no interface name */
+		unit += (c - '0') * m;
+		if (unit > 1000000)
+			return NULL;	/* number is unreasonable */
+		m *= 10;
+		c = *--cp;
+	} while (c >= '0' && c <= '9');
 	len = cp - name + 1;
-	for (unit = 0;
-	    ((c = *cp) >= '0') && (c <= '9') && (unit < 1000000); cp++ )
-		unit = (unit * 10) + (c - '0');
-	if (*cp != '\0')
-		return 0;	/* no trailing garbage allowed */
+	bcopy(name, namebuf, len);
+	namebuf[len++] = '\0';
 	/*
 	 * Now search all the interfaces for this name/number
 	 */

--
Boris Popov
http://www.butya.kz/~bp/



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-net" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.10.9912111519550.56196-100000>