xref: /netbsd-src/lib/libc/compat/net/compat_ns_addr.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: compat_ns_addr.c,v 1.2 2012/03/20 17:05:59 matt Exp $	*/
2 
3 /*
4  * Copyright (c) 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * J.Q. Johnson.
9  *
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.
21  *
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.
33  */
34 
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.2 2012/03/20 17:05:59 matt Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <compat/include/ns.h>
47 
48 #include <assert.h>
49 #include <stdio.h>
50 #include <string.h>
51 
52 static void Field(char *, uint8_t *, int);
53 static void cvtbase(long, int, int[], int, uint8_t [], int);
54 
55 struct ns_addr
56 ns_addr(const char *name)
57 {
58 	char separator;
59 	char *hostname, *socketname, *cp;
60 	char buf[50];
61 	static struct ns_addr addr;
62 
63 	_DIAGASSERT(name != NULL);
64 
65 	(void)strlcpy(buf, name, sizeof(buf));
66 
67 	/*
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.
72 	 */
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 = '.';
83 	}
84 	if (hostname)
85 		*hostname++ = 0;
86 
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 */
91 
92 	socketname = strchr(hostname, separator);
93 	if (socketname) {
94 		*socketname++ = 0;
95 		Field(socketname, (uint8_t *)(void *)&addr.x_port, 2);
96 	}
97 
98 	Field(hostname, addr.x_host.c_host, 6);
99 
100 	return (addr);
101 }
102 
103 static void
104 Field(char *buf, uint8_t *out, int len)
105 {
106 	register char *bp = buf;
107 	int i, ibase, base16 = 0, base10 = 0, clen = 0;
108 	int hb[6], *hp;
109 
110 	_DIAGASSERT(buf != NULL);
111 	_DIAGASSERT(out != NULL);
112 
113 	/*
114 	 * first try 2-273#2-852-151-014#socket
115 	 */
116 	if ((*buf != '-') &&
117 	    (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
118 			&hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
119 		cvtbase(1000L, 256, hb, i, out, len);
120 		return;
121 	}
122 	/*
123 	 * try form 8E1#0.0.AA.0.5E.E6#socket
124 	 */
125 	if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
126 			&hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
127 		cvtbase(256L, 256, hb, i, out, len);
128 		return;
129 	}
130 	/*
131 	 * try form 8E1#0:0:AA:0:5E:E6#socket
132 	 */
133 	if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
134 			&hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
135 		cvtbase(256L, 256, hb, i, out, len);
136 		return;
137 	}
138 	/*
139 	 * This is REALLY stretching it but there was a
140 	 * comma notation separting shorts -- definitely non standard
141 	 */
142 	if (1 < (i = sscanf(buf,"%x,%x,%x",
143 			&hb[0], &hb[1], &hb[2]))) {
144 		hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
145 		hb[2] = htons(hb[2]);
146 		cvtbase(65536L, 256, hb, i, out, len);
147 		return;
148 	}
149 
150 	/* Need to decide if base 10, 16 or 8 */
151 	while (*bp) switch (*bp++) {
152 
153 	case '0': case '1': case '2': case '3': case '4': case '5':
154 	case '6': case '7': case '-':
155 		break;
156 
157 	case '8': case '9':
158 		base10 = 1;
159 		break;
160 
161 	case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
162 	case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
163 		base16 = 1;
164 		break;
165 
166 	case 'x': case 'X':
167 		*--bp = '0';
168 		base16 = 1;
169 		break;
170 
171 	case 'h': case 'H':
172 		base16 = 1;
173 		/* FALLTHROUGH */
174 
175 	default:
176 		*--bp = 0; /* Ends Loop */
177 	}
178 	if (base16) {
179 		ibase = 4096;
180 	} else if (base10 == 0 && *buf == '0') {
181 		ibase = 512;
182 	} else {
183 		base10 = 1;
184 		ibase = 1000;
185 	}
186 
187 	for (bp = buf; *bp++; ) clen++;
188 	if (clen == 0) clen++;
189 	if (clen > 18) clen = 18;
190 	i = ((clen - 1) / 3) + 1;
191 	bp = clen + buf - 3;
192 	hp = hb + i - 1;
193 
194 	while (hp > hb) {
195 		if (base16)
196 			(void)sscanf(bp, "%3x", hp);
197 		else if (base10)
198 			(void)sscanf(bp, "%3d", hp);
199 		else
200 			(void)sscanf(bp, "%3o", hp);
201 
202 		bp[0] = 0;
203 		hp--;
204 		bp -= 3;
205 	}
206 	if (base16)
207 		(void)sscanf(buf, "%3x", hp);
208 	else if (base10)
209 		(void)sscanf(buf, "%3d", hp);
210 	else
211 		(void)sscanf(buf, "%3o", hp);
212 
213 	cvtbase((long)ibase, 256, hb, i, out, len);
214 }
215 
216 static void
217 cvtbase(long oldbase, int newbase, int input[], int inlen,
218 	uint8_t result[], int reslen)
219 {
220 	int d, e;
221 	long sum;
222 
223 	_DIAGASSERT(input != NULL);
224 	_DIAGASSERT(result != NULL);
225 	_DIAGASSERT(inlen > 0);
226 
227 	e = 1;
228 	while (e > 0 && reslen > 0) {
229 		d = 0; e = 0; sum = 0;
230 		/* long division: input=input/newbase */
231 		while (d < inlen) {
232 			sum = sum*oldbase + (long) input[d];
233 			e += (sum > 0);
234 			input[d++] = (int) (sum / newbase);
235 			sum %= newbase;
236 		}
237 		/* accumulate remainder */
238 		result[--reslen] = (unsigned char)sum;
239 	}
240 	for (d=0; d < reslen; d++)
241 		result[d] = 0;
242 }
243