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.c223
1 files changed, 223 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..0f8feda55c
--- /dev/null
+++ b/src/lib/libc/net/ns_addr.c
@@ -0,0 +1,223 @@
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.8 2003/06/02 20:18:35 millert 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(name)
49 const char *name;
50{
51 char separator;
52 char *hostname, *socketname, *cp;
53 char buf[50];
54
55 strlcpy(buf, name, sizeof(buf));
56
57 /*
58 * First, figure out what he intends as a field separtor.
59 * Despite the way this routine is written, the prefered
60 * form 2-272.AA001234H.01777, i.e. XDE standard.
61 * Great efforts are made to insure backward compatibility.
62 */
63 if ((hostname = strchr(buf, '#')))
64 separator = '#';
65 else {
66 hostname = strchr(buf, '.');
67 if ((cp = strchr(buf, ':')) &&
68 ((hostname && cp < hostname) || (hostname == 0))) {
69 hostname = cp;
70 separator = ':';
71 } else
72 separator = '.';
73 }
74 if (hostname)
75 *hostname++ = 0;
76
77 addr = zero_addr;
78 Field(buf, addr.x_net.c_net, 4);
79 if (hostname == 0)
80 return (addr); /* No separator means net only */
81
82 socketname = strchr(hostname, separator);
83 if (socketname) {
84 *socketname++ = 0;
85 Field(socketname, (u_char *)&addr.x_port, 2);
86 }
87
88 Field(hostname, (u_char *)addr.x_host.c_host, 6);
89
90 return (addr);
91}
92
93static void
94Field(buf, out, len)
95 char *buf;
96 u_char *out;
97 int len;
98{
99 register char *bp = buf;
100 int i, ibase, base16 = 0, base10 = 0, clen = 0;
101 int hb[6], *hp;
102 char *fmt;
103
104 /*
105 * first try 2-273#2-852-151-014#socket
106 */
107 if ((*buf != '-') &&
108 (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
109 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
110 cvtbase(1000L, 256, hb, i, out, len);
111 return;
112 }
113 /*
114 * try form 8E1#0.0.AA.0.5E.E6#socket
115 */
116 if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
117 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
118 cvtbase(256L, 256, hb, i, out, len);
119 return;
120 }
121 /*
122 * try form 8E1#0:0:AA:0:5E:E6#socket
123 */
124 if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
125 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
126 cvtbase(256L, 256, hb, i, out, len);
127 return;
128 }
129 /*
130 * This is REALLY stretching it but there was a
131 * comma notation separting shorts -- definitely non standard
132 */
133 if (1 < (i = sscanf(buf,"%x,%x,%x",
134 &hb[0], &hb[1], &hb[2]))) {
135 hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
136 hb[2] = htons(hb[2]);
137 cvtbase(65536L, 256, hb, i, out, len);
138 return;
139 }
140
141 /* Need to decide if base 10, 16 or 8 */
142 while (*bp) switch (*bp++) {
143
144 case '0': case '1': case '2': case '3': case '4': case '5':
145 case '6': case '7': case '-':
146 break;
147
148 case '8': case '9':
149 base10 = 1;
150 break;
151
152 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
153 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
154 base16 = 1;
155 break;
156
157 case 'x': case 'X':
158 *--bp = '0';
159 base16 = 1;
160 break;
161
162 case 'h': case 'H':
163 base16 = 1;
164 /* fall into */
165
166 default:
167 *--bp = 0; /* Ends Loop */
168 }
169 if (base16) {
170 fmt = "%3x";
171 ibase = 4096;
172 } else if (base10 == 0 && *buf == '0') {
173 fmt = "%3o";
174 ibase = 512;
175 } else {
176 fmt = "%3d";
177 ibase = 1000;
178 }
179
180 for (bp = buf; *bp++; ) clen++;
181 if (clen == 0) clen++;
182 if (clen > 18) clen = 18;
183 i = ((clen - 1) / 3) + 1;
184 bp = clen + buf - 3;
185 hp = hb + i - 1;
186
187 while (hp > hb) {
188 (void)sscanf(bp, fmt, hp);
189 bp[0] = 0;
190 hp--;
191 bp -= 3;
192 }
193 (void)sscanf(buf, fmt, hp);
194 cvtbase((long)ibase, 256, hb, i, out, len);
195}
196
197static void
198cvtbase(oldbase,newbase,input,inlen,result,reslen)
199 long oldbase;
200 int newbase;
201 int input[];
202 int inlen;
203 unsigned char result[];
204 int reslen;
205{
206 int d, e;
207 long sum;
208
209 e = 1;
210 while (e > 0 && reslen > 0) {
211 d = 0; e = 0; sum = 0;
212 /* long division: input=input/newbase */
213 while (d < inlen) {
214 sum = sum*oldbase + (long) input[d];
215 e += (sum > 0);
216 input[d++] = sum / newbase;
217 sum %= newbase;
218 }
219 result[--reslen] = sum; /* accumulate remainder */
220 }
221 for (d=0; d < reslen; d++)
222 result[d] = 0;
223}