]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/BsdSocketLib/ns_addr.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / BsdSocketLib / ns_addr.c
diff --git a/StdLib/BsdSocketLib/ns_addr.c b/StdLib/BsdSocketLib/ns_addr.c
new file mode 100644 (file)
index 0000000..81fd625
--- /dev/null
@@ -0,0 +1,240 @@
+/*\r
+ * Copyright (c) 1986, 1993\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
+ *\r
+ * Portions copyright (c) 1999, 2000\r
+ * Intel Corporation.\r
+ * 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
+ * are met:\r
+ * \r
+ * 1. Redistributions of source code must retain the above copyright\r
+ *    notice, this list of conditions and the following disclaimer.\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
+ * 3. All advertising materials mentioning features or use of this software\r
+ *    must display the following acknowledgement:\r
+ * \r
+ *    This product includes software developed by the University of\r
+ *    California, Berkeley, Intel Corporation, and its contributors.\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
+ * 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
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS,\r
+ * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ */\r
+\r
+#if defined(LIBC_SCCS) && !defined(lint)\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
+#include <netns/ns.h>\r
+#include <stdio.h>\r
+#include <string.h>\r
+\r
+static struct ns_addr addr, zero_addr;\r
+\r
+static void Field(), cvtbase();\r
+\r
+struct ns_addr\r
+ns_addr(\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
+}\r
+\r
+static void\r
+Field(\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
+}\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
+{\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