/*\r
* Copyright (c) 1986, 1993\r
- * The Regents of the University of California. All rights reserved.\r
+ * The Regents of the University of California. All rights reserved.\r
*\r
* This code is derived from software contributed to Berkeley by\r
* J.Q. Johnson.\r
* Portions copyright (c) 1999, 2000\r
* Intel Corporation.\r
* All rights reserved.\r
- * \r
+ *\r
* Redistribution and use in source and binary forms, with or without\r
* modification, are permitted provided that the following conditions\r
* are met:\r
- * \r
+ *\r
* 1. Redistributions of source code must retain the above copyright\r
* notice, this list of conditions and the following disclaimer.\r
- * \r
+ *\r
* 2. Redistributions in binary form must reproduce the above copyright\r
* notice, this list of conditions and the following disclaimer in the\r
* documentation and/or other materials provided with the distribution.\r
- * \r
+ *\r
* 3. All advertising materials mentioning features or use of this software\r
* must display the following acknowledgement:\r
- * \r
+ *\r
* This product includes software developed by the University of\r
* California, Berkeley, Intel Corporation, and its contributors.\r
- * \r
+ *\r
* 4. Neither the name of University, Intel Corporation, or their respective\r
* contributors may be used to endorse or promote products derived from\r
* this software without specific prior written permission.\r
- * \r
+ *\r
* THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND\r
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
*/\r
\r
#if defined(LIBC_SCCS) && !defined(lint)\r
-static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";\r
+static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";\r
#endif /* LIBC_SCCS and not lint */\r
\r
#include <sys/param.h>\r
\r
static struct ns_addr addr, zero_addr;\r
\r
-static void Field(), cvtbase();\r
+static void Field (char *buf, u_char *out, int len);\r
+static void cvtbase (long oldbase, int newbase, int input[], int inlen, unsigned char result[], int reslen);\r
\r
struct ns_addr\r
ns_addr(\r
- const char *name\r
- )\r
+ const char *name\r
+ )\r
{\r
- char separator;\r
- char *hostname, *socketname, *cp;\r
- char buf[50];\r
-\r
- (void)strncpy(buf, name, sizeof(buf) - 1);\r
- buf[sizeof(buf) - 1] = '\0';\r
-\r
- /*\r
- * First, figure out what he intends as a field separtor.\r
- * Despite the way this routine is written, the prefered\r
- * form 2-272.AA001234H.01777, i.e. XDE standard.\r
- * Great efforts are made to insure backward compatability.\r
- */\r
- if ((hostname = strchr(buf, '#')) != NULL)\r
- separator = '#';\r
- else {\r
- hostname = strchr(buf, '.');\r
- if ((cp = strchr(buf, ':')) &&\r
- ((hostname && cp < hostname) || (hostname == 0))) {\r
- hostname = cp;\r
- separator = ':';\r
- } else\r
- separator = '.';\r
- }\r
- if (hostname)\r
- *hostname++ = 0;\r
-\r
- addr = zero_addr;\r
- Field(buf, addr.x_net.c_net, 4);\r
- if (hostname == 0)\r
- return (addr); /* No separator means net only */\r
-\r
- socketname = strchr(hostname, separator);\r
- if (socketname) {\r
- *socketname++ = 0;\r
- Field(socketname, (u_char *)&addr.x_port, 2);\r
- }\r
-\r
- Field(hostname, addr.x_host.c_host, 6);\r
-\r
- return (addr);\r
+ char separator;\r
+ char *hostname, *socketname, *cp;\r
+ char buf[50];\r
+\r
+ (void)strncpy(buf, name, sizeof(buf) - 1);\r
+ buf[sizeof(buf) - 1] = '\0';\r
+\r
+ /*\r
+ * First, figure out what he intends as a field separtor.\r
+ * Despite the way this routine is written, the prefered\r
+ * form 2-272.AA001234H.01777, i.e. XDE standard.\r
+ * Great efforts are made to insure backward compatability.\r
+ */\r
+ if ((hostname = strchr(buf, '#')) != NULL)\r
+ separator = '#';\r
+ else {\r
+ hostname = strchr(buf, '.');\r
+ if ((cp = strchr(buf, ':')) &&\r
+ ((hostname && cp < hostname) || (hostname == 0))) {\r
+ hostname = cp;\r
+ separator = ':';\r
+ } else\r
+ separator = '.';\r
+ }\r
+ if (hostname)\r
+ *hostname++ = 0;\r
+\r
+ addr = zero_addr;\r
+ Field(buf, addr.x_net.c_net, 4);\r
+ if (hostname == 0)\r
+ return (addr); /* No separator means net only */\r
+\r
+ socketname = strchr(hostname, separator);\r
+ if (socketname) {\r
+ *socketname++ = 0;\r
+ Field(socketname, (u_char *)&addr.x_port, 2);\r
+ }\r
+\r
+ Field(hostname, addr.x_host.c_host, 6);\r
+\r
+ return (addr);\r
}\r
\r
static void\r
Field(\r
- char *buf,\r
- u_char *out,\r
- int len\r
- )\r
+ char *buf,\r
+ u_char *out,\r
+ int len\r
+ )\r
{\r
- register char *bp = buf;\r
- int i, ibase, base16 = 0, base10 = 0, clen = 0;\r
- int hb[6], *hp;\r
- char *fmt;\r
-\r
- /*\r
- * first try 2-273#2-852-151-014#socket\r
- */\r
- if ((*buf != '-') &&\r
- (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",\r
- &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {\r
- cvtbase(1000L, 256, hb, i, out, len);\r
- return;\r
- }\r
- /*\r
- * try form 8E1#0.0.AA.0.5E.E6#socket\r
- */\r
- if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",\r
- &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {\r
- cvtbase(256L, 256, hb, i, out, len);\r
- return;\r
- }\r
- /*\r
- * try form 8E1#0:0:AA:0:5E:E6#socket\r
- */\r
- if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",\r
- &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {\r
- cvtbase(256L, 256, hb, i, out, len);\r
- return;\r
- }\r
- /*\r
- * This is REALLY stretching it but there was a\r
- * comma notation separting shorts -- definitely non standard\r
- */\r
- if (1 < (i = sscanf(buf,"%x,%x,%x",\r
- &hb[0], &hb[1], &hb[2]))) {\r
- hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);\r
- hb[2] = htons(hb[2]);\r
- cvtbase(65536L, 256, hb, i, out, len);\r
- return;\r
- }\r
-\r
- /* Need to decide if base 10, 16 or 8 */\r
- while (*bp) switch (*bp++) {\r
-\r
- case '0': case '1': case '2': case '3': case '4': case '5':\r
- case '6': case '7': case '-':\r
- break;\r
-\r
- case '8': case '9':\r
- base10 = 1;\r
- break;\r
-\r
- case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':\r
- case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':\r
- base16 = 1;\r
- break;\r
-\r
- case 'x': case 'X':\r
- *--bp = '0';\r
- base16 = 1;\r
- break;\r
-\r
- case 'h': case 'H':\r
- base16 = 1;\r
- /* fall into */\r
-\r
- default:\r
- *--bp = 0; /* Ends Loop */\r
- }\r
- if (base16) {\r
- fmt = "%3x";\r
- ibase = 4096;\r
- } else if (base10 == 0 && *buf == '0') {\r
- fmt = "%3o";\r
- ibase = 512;\r
- } else {\r
- fmt = "%3d";\r
- ibase = 1000;\r
- }\r
-\r
- for (bp = buf; *bp++; ) clen++;\r
- if (clen == 0) clen++;\r
- if (clen > 18) clen = 18;\r
- i = ((clen - 1) / 3) + 1;\r
- bp = clen + buf - 3;\r
- hp = hb + i - 1;\r
-\r
- while (hp > hb) {\r
- (void)sscanf(bp, fmt, hp);\r
- bp[0] = 0;\r
- hp--;\r
- bp -= 3;\r
- }\r
- (void)sscanf(buf, fmt, hp);\r
- cvtbase((long)ibase, 256, hb, i, out, len);\r
+ register char *bp = buf;\r
+ int i, ibase, base16 = 0, base10 = 0, clen = 0;\r
+ int hb[6], *hp;\r
+ char *fmt;\r
+\r
+ /*\r
+ * first try 2-273#2-852-151-014#socket\r
+ */\r
+ if ((*buf != '-') &&\r
+ (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",\r
+ &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {\r
+ cvtbase(1000L, 256, hb, i, out, len);\r
+ return;\r
+ }\r
+ /*\r
+ * try form 8E1#0.0.AA.0.5E.E6#socket\r
+ */\r
+ if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",\r
+ &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {\r
+ cvtbase(256L, 256, hb, i, out, len);\r
+ return;\r
+ }\r
+ /*\r
+ * try form 8E1#0:0:AA:0:5E:E6#socket\r
+ */\r
+ if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",\r
+ &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {\r
+ cvtbase(256L, 256, hb, i, out, len);\r
+ return;\r
+ }\r
+ /*\r
+ * This is REALLY stretching it but there was a\r
+ * comma notation separting shorts -- definitely non standard\r
+ */\r
+ if (1 < (i = sscanf(buf,"%x,%x,%x",\r
+ &hb[0], &hb[1], &hb[2]))) {\r
+ hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);\r
+ hb[2] = htons(hb[2]);\r
+ cvtbase(65536L, 256, hb, i, out, len);\r
+ return;\r
+ }\r
+\r
+ /* Need to decide if base 10, 16 or 8 */\r
+ while (*bp) switch (*bp++) {\r
+\r
+ case '0': case '1': case '2': case '3': case '4': case '5':\r
+ case '6': case '7': case '-':\r
+ break;\r
+\r
+ case '8': case '9':\r
+ base10 = 1;\r
+ break;\r
+\r
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':\r
+ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':\r
+ base16 = 1;\r
+ break;\r
+\r
+ case 'x': case 'X':\r
+ *--bp = '0';\r
+ base16 = 1;\r
+ break;\r
+\r
+ case 'h': case 'H':\r
+ base16 = 1;\r
+ /* fall into */\r
+\r
+ default:\r
+ *--bp = 0; /* Ends Loop */\r
+ }\r
+ if (base16) {\r
+ fmt = "%3x";\r
+ ibase = 4096;\r
+ } else if (base10 == 0 && *buf == '0') {\r
+ fmt = "%3o";\r
+ ibase = 512;\r
+ } else {\r
+ fmt = "%3d";\r
+ ibase = 1000;\r
+ }\r
+\r
+ for (bp = buf; *bp++; ) clen++;\r
+ if (clen == 0) clen++;\r
+ if (clen > 18) clen = 18;\r
+ i = ((clen - 1) / 3) + 1;\r
+ bp = clen + buf - 3;\r
+ hp = hb + i - 1;\r
+\r
+ while (hp > hb) {\r
+ (void)sscanf(bp, fmt, hp);\r
+ bp[0] = 0;\r
+ hp--;\r
+ bp -= 3;\r
+ }\r
+ (void)sscanf(buf, fmt, hp);\r
+ cvtbase((long)ibase, 256, hb, i, out, len);\r
}\r
\r
static void\r
cvtbase(\r
- long oldbase,\r
- int newbase,\r
- int input[],\r
- int inlen,\r
- unsigned char result[],\r
- int reslen\r
- )\r
+ long oldbase,\r
+ int newbase,\r
+ int input[],\r
+ int inlen,\r
+ unsigned char result[],\r
+ int reslen\r
+ )\r
{\r
- int d, e;\r
- long sum;\r
-\r
- e = 1;\r
- while (e > 0 && reslen > 0) {\r
- d = 0; e = 0; sum = 0;\r
- /* long division: input=input/newbase */\r
- while (d < inlen) {\r
- sum = sum*oldbase + (long) input[d];\r
- e += (sum > 0);\r
- input[d++] = sum / newbase;\r
- sum %= newbase;\r
- }\r
- result[--reslen] = (u_char)sum; /* accumulate remainder */\r
- }\r
- for (d=0; d < reslen; d++)\r
- result[d] = 0;\r
+ int d, e;\r
+ long sum;\r
+\r
+ e = 1;\r
+ while (e > 0 && reslen > 0) {\r
+ d = 0; e = 0; sum = 0;\r
+ /* long division: input=input/newbase */\r
+ while (d < inlen) {\r
+ sum = sum*oldbase + (long) input[d];\r
+ e += (sum > 0);\r
+ input[d++] = sum / newbase;\r
+ sum %= newbase;\r
+ }\r
+ result[--reslen] = (u_char)sum; /* accumulate remainder */\r
+ }\r
+ for (d=0; d < reslen; d++)\r
+ result[d] = 0;\r
}\r
/*\r
* Copyright (c) 1986, 1993\r
- * The Regents of the University of California. All rights reserved.\r
+ * The Regents of the University of California. All rights reserved.\r
*\r
* Redistribution and use in source and binary forms, with or without\r
* modification, are permitted provided that the following conditions\r
* documentation and/or other materials provided with the distribution.\r
* 3. All advertising materials mentioning features or use of this software\r
* must display the following acknowledgement:\r
- * This product includes software developed by the University of\r
- * California, Berkeley and its contributors.\r
+ * This product includes software developed by the University of\r
+ * California, Berkeley and its contributors.\r
* 4. Neither the name of the University nor the names of its contributors\r
* may be used to endorse or promote products derived from this software\r
* without specific prior written permission.\r
*/\r
\r
#if defined(LIBC_SCCS) && !defined(lint)\r
-static char sccsid[] = "@(#)ns_ntoa.c 8.1 (Berkeley) 6/4/93";\r
+static char sccsid[] = "@(#)ns_ntoa.c 8.1 (Berkeley) 6/4/93";\r
#endif /* LIBC_SCCS and not lint */\r
\r
#include <sys/param.h>\r
#include <netns/ns.h>\r
#include <stdio.h>\r
\r
-static char *spectHex();\r
+static char *spectHex(char *p0);\r
\r
char *\r
ns_ntoa(\r
- struct ns_addr addr\r
- )\r
+ struct ns_addr addr\r
+ )\r
{\r
- static char obuf[40];\r
- union { union ns_net net_e; u_long long_e; } net;\r
- u_short port = htons(addr.x_port);\r
- register char *cp;\r
- char *cp2;\r
- register u_char *up = addr.x_host.c_host;\r
- u_char *uplim = up + 6;\r
+ static char obuf[40];\r
+ union { union ns_net net_e; u_long long_e; } net;\r
+ u_short port = htons(addr.x_port);\r
+ register char *cp;\r
+ char *cp2;\r
+ register u_char *up = addr.x_host.c_host;\r
+ u_char *uplim = up + 6;\r
\r
- net.net_e = addr.x_net;\r
- sprintf(obuf, "%x", (UINT32)ntohl(net.long_e));\r
- cp = spectHex(obuf);\r
- cp2 = cp + 1;\r
- while (*up==0 && up < uplim) up++;\r
- if (up == uplim) {\r
- if (port) {\r
- sprintf(cp, ".0");\r
- cp += 2;\r
- }\r
- } else {\r
- sprintf(cp, ".%x", *up++);\r
- while (up < uplim) {\r
- while (*cp) cp++;\r
- sprintf(cp, "%02x", *up++);\r
- }\r
- cp = spectHex(cp2);\r
- }\r
- if (port) {\r
- sprintf(cp, ".%x", port);\r
- spectHex(cp + 1);\r
- }\r
- return (obuf);\r
+ net.net_e = addr.x_net;\r
+ sprintf(obuf, "%x", (UINT32)ntohl(net.long_e));\r
+ cp = spectHex(obuf);\r
+ cp2 = cp + 1;\r
+ while (*up==0 && up < uplim) up++;\r
+ if (up == uplim) {\r
+ if (port) {\r
+ sprintf(cp, ".0");\r
+ cp += 2;\r
+ }\r
+ } else {\r
+ sprintf(cp, ".%x", *up++);\r
+ while (up < uplim) {\r
+ while (*cp) cp++;\r
+ sprintf(cp, "%02x", *up++);\r
+ }\r
+ cp = spectHex(cp2);\r
+ }\r
+ if (port) {\r
+ sprintf(cp, ".%x", port);\r
+ spectHex(cp + 1);\r
+ }\r
+ return (obuf);\r
}\r
\r
static char *\r
spectHex(\r
- char *p0\r
- )\r
+ char *p0\r
+ )\r
{\r
- int ok = 0;\r
- int nonzero = 0;\r
- register char *p = p0;\r
- for (; *p; p++) switch (*p) {\r
+ int ok = 0;\r
+ int nonzero = 0;\r
+ register char *p = p0;\r
+ for (; *p; p++) switch (*p) {\r
\r
- case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':\r
- *p += ('A' - 'a');\r
- /* fall into . . . */\r
- case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':\r
- ok = 1;\r
- case '1': case '2': case '3': case '4': case '5':\r
- case '6': case '7': case '8': case '9':\r
- nonzero = 1;\r
- }\r
- if (nonzero && !ok) { *p++ = 'H'; *p = 0; }\r
- return (p);\r
+ case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':\r
+ *p += ('A' - 'a');\r
+ /* fall into . . . */\r
+ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':\r
+ ok = 1;\r
+ case '1': case '2': case '3': case '4': case '5':\r
+ case '6': case '7': case '8': case '9':\r
+ nonzero = 1;\r
+ }\r
+ if (nonzero && !ok) { *p++ = 'H'; *p = 0; }\r
+ return (p);\r
}\r