PageRenderTime 1015ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

lib/libc/compat/net/compat_ns_addr.c

http://www.minix3.org/
C | 250 lines | 165 code | 30 blank | 55 comment | 46 complexity | a31d95eb32073e1d8adf9977ff100a5c MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /* $NetBSD: compat_ns_addr.c,v 1.1 2006/08/26 16:07:01 matt Exp $ */
  2. /*
  3. * Copyright (c) 1986, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * J.Q. Johnson.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. #if 0
  36. static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";
  37. #else
  38. __RCSID("$NetBSD: compat_ns_addr.c,v 1.1 2006/08/26 16:07:01 matt Exp $");
  39. #endif
  40. #endif /* LIBC_SCCS and not lint */
  41. #include "namespace.h"
  42. #include <sys/param.h>
  43. #include <compat/include/ns.h>
  44. #include <assert.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47. static void Field __P((char *, u_int8_t *, int));
  48. static void cvtbase __P((long, int, int[], int, u_int8_t[], int));
  49. struct ns_addr
  50. ns_addr(name)
  51. const char *name;
  52. {
  53. char separator;
  54. char *hostname, *socketname, *cp;
  55. char buf[50];
  56. static struct ns_addr addr;
  57. _DIAGASSERT(name != NULL);
  58. (void)strlcpy(buf, name, sizeof(buf));
  59. /*
  60. * First, figure out what he intends as a field separtor.
  61. * Despite the way this routine is written, the prefered
  62. * form 2-272.AA001234H.01777, i.e. XDE standard.
  63. * Great efforts are made to insure backward compatibility.
  64. */
  65. if ((hostname = strchr(buf, '#')) != NULL)
  66. separator = '#';
  67. else {
  68. hostname = strchr(buf, '.');
  69. if ((cp = strchr(buf, ':')) &&
  70. ((hostname && cp < hostname) || (hostname == 0))) {
  71. hostname = cp;
  72. separator = ':';
  73. } else
  74. separator = '.';
  75. }
  76. if (hostname)
  77. *hostname++ = 0;
  78. memset(&addr, '\0', sizeof(addr));
  79. Field(buf, addr.x_net.c_net, 4);
  80. if (hostname == 0)
  81. return (addr); /* No separator means net only */
  82. socketname = strchr(hostname, separator);
  83. if (socketname) {
  84. *socketname++ = 0;
  85. Field(socketname, (u_int8_t *)(void *)&addr.x_port, 2);
  86. }
  87. Field(hostname, addr.x_host.c_host, 6);
  88. return (addr);
  89. }
  90. static void
  91. Field(buf, out, len)
  92. char *buf;
  93. u_int8_t *out;
  94. int len;
  95. {
  96. register char *bp = buf;
  97. int i, ibase, base16 = 0, base10 = 0, clen = 0;
  98. int hb[6], *hp;
  99. _DIAGASSERT(buf != NULL);
  100. _DIAGASSERT(out != NULL);
  101. /*
  102. * first try 2-273#2-852-151-014#socket
  103. */
  104. if ((*buf != '-') &&
  105. (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
  106. &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
  107. cvtbase(1000L, 256, hb, i, out, len);
  108. return;
  109. }
  110. /*
  111. * try form 8E1#0.0.AA.0.5E.E6#socket
  112. */
  113. if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
  114. &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
  115. cvtbase(256L, 256, hb, i, out, len);
  116. return;
  117. }
  118. /*
  119. * try form 8E1#0:0:AA:0:5E:E6#socket
  120. */
  121. if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
  122. &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
  123. cvtbase(256L, 256, hb, i, out, len);
  124. return;
  125. }
  126. /*
  127. * This is REALLY stretching it but there was a
  128. * comma notation separting shorts -- definitely non standard
  129. */
  130. if (1 < (i = sscanf(buf,"%x,%x,%x",
  131. &hb[0], &hb[1], &hb[2]))) {
  132. hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
  133. hb[2] = htons(hb[2]);
  134. cvtbase(65536L, 256, hb, i, out, len);
  135. return;
  136. }
  137. /* Need to decide if base 10, 16 or 8 */
  138. while (*bp) switch (*bp++) {
  139. case '0': case '1': case '2': case '3': case '4': case '5':
  140. case '6': case '7': case '-':
  141. break;
  142. case '8': case '9':
  143. base10 = 1;
  144. break;
  145. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  146. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  147. base16 = 1;
  148. break;
  149. case 'x': case 'X':
  150. *--bp = '0';
  151. base16 = 1;
  152. break;
  153. case 'h': case 'H':
  154. base16 = 1;
  155. /* FALLTHROUGH */
  156. default:
  157. *--bp = 0; /* Ends Loop */
  158. }
  159. if (base16) {
  160. ibase = 4096;
  161. } else if (base10 == 0 && *buf == '0') {
  162. ibase = 512;
  163. } else {
  164. base10 = 1;
  165. ibase = 1000;
  166. }
  167. for (bp = buf; *bp++; ) clen++;
  168. if (clen == 0) clen++;
  169. if (clen > 18) clen = 18;
  170. i = ((clen - 1) / 3) + 1;
  171. bp = clen + buf - 3;
  172. hp = hb + i - 1;
  173. while (hp > hb) {
  174. if (base16)
  175. (void)sscanf(bp, "%3x", hp);
  176. else if (base10)
  177. (void)sscanf(bp, "%3d", hp);
  178. else
  179. (void)sscanf(bp, "%3o", hp);
  180. bp[0] = 0;
  181. hp--;
  182. bp -= 3;
  183. }
  184. if (base16)
  185. (void)sscanf(buf, "%3x", hp);
  186. else if (base10)
  187. (void)sscanf(buf, "%3d", hp);
  188. else
  189. (void)sscanf(buf, "%3o", hp);
  190. cvtbase((long)ibase, 256, hb, i, out, len);
  191. }
  192. static void
  193. cvtbase(oldbase,newbase,input,inlen,result,reslen)
  194. long oldbase;
  195. int newbase;
  196. int input[];
  197. int inlen;
  198. unsigned char result[];
  199. int reslen;
  200. {
  201. int d, e;
  202. long sum;
  203. _DIAGASSERT(input != NULL);
  204. _DIAGASSERT(result != NULL);
  205. e = 1;
  206. while (e > 0 && reslen > 0) {
  207. d = 0; e = 0; sum = 0;
  208. /* long division: input=input/newbase */
  209. while (d < inlen) {
  210. sum = sum*oldbase + (long) input[d];
  211. e += (sum > 0);
  212. input[d++] = (int) (sum / newbase);
  213. sum %= newbase;
  214. }
  215. /* accumulate remainder */
  216. result[--reslen] = (unsigned char)sum;
  217. }
  218. for (d=0; d < reslen; d++)
  219. result[d] = 0;
  220. }