xref: /netbsd-src/lib/libc/compat/net/compat_ns_addr.c (revision 369abe069b9a5e1b28efe903ba0c77aa728c498e)
1 /*	$NetBSD: compat_ns_addr.c,v 1.4 2021/11/07 20:31:09 andvar 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.4 2021/11/07 20:31:09 andvar 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
ns_addr(const char * name)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 separator.
69 	 * Despite the way this routine is written, the preferred
70 	 * form  2-272.AA001234H.01777, i.e. XDE standard.
71 	 * Great efforts are made to ensure 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
Field(char * buf,uint8_t * out,int len)104 Field(char *buf, uint8_t *out, int len)
105 {
106 	register char *bp = buf;
107 	int i, ibase, base16 = 0, base10 = 0;
108 	unsigned int clen = 0;
109 	int hb[6], *hp;
110 
111 	_DIAGASSERT(buf != NULL);
112 	_DIAGASSERT(out != NULL);
113 
114 	/*
115 	 * first try 2-273#2-852-151-014#socket
116 	 */
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;
122 	}
123 	/*
124 	 * try form 8E1#0.0.AA.0.5E.E6#socket
125 	 */
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;
130 	}
131 	/*
132 	 * try form 8E1#0:0:AA:0:5E:E6#socket
133 	 */
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;
138 	}
139 	/*
140 	 * This is REALLY stretching it but there was a
141 	 * comma notation separating shorts -- definitely non standard
142 	 */
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;
149 	}
150 
151 	/* Need to decide if base 10, 16 or 8 */
152 	while (*bp) switch (*bp++) {
153 
154 	case '0': case '1': case '2': case '3': case '4': case '5':
155 	case '6': case '7': case '-':
156 		break;
157 
158 	case '8': case '9':
159 		base10 = 1;
160 		break;
161 
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;
166 
167 	case 'x': case 'X':
168 		*--bp = '0';
169 		base16 = 1;
170 		break;
171 
172 	case 'h': case 'H':
173 		base16 = 1;
174 		/* FALLTHROUGH */
175 
176 	default:
177 		*--bp = 0; /* Ends Loop */
178 	}
179 	if (base16) {
180 		ibase = 4096;
181 	} else if (base10 == 0 && *buf == '0') {
182 		ibase = 512;
183 	} else {
184 		base10 = 1;
185 		ibase = 1000;
186 	}
187 
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;
194 
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);
202 
203 		bp[0] = 0;
204 		hp--;
205 		bp -= 3;
206 	}
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);
213 
214 	cvtbase((long)ibase, 256, hb, i, out, len);
215 }
216 
217 static void
cvtbase(long oldbase,int newbase,int input[],int inlen,uint8_t result[],int reslen)218 cvtbase(long oldbase, int newbase, int input[], int inlen,
219 	uint8_t result[], int reslen)
220 {
221 	int d, e;
222 	long sum;
223 
224 	_DIAGASSERT(input != NULL);
225 	_DIAGASSERT(result != NULL);
226 	_DIAGASSERT(inlen > 0);
227 
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;
237 		}
238 		/* accumulate remainder */
239 		result[--reslen] = (unsigned char)sum;
240 	}
241 	for (d=0; d < reslen; d++)
242 		result[d] = 0;
243 }
244