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