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