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.c233
1 files changed, 0 insertions, 233 deletions
diff --git a/src/lib/libc/net/ns_addr.c b/src/lib/libc/net/ns_addr.c
deleted file mode 100644
index f75ddb23b7..0000000000
--- a/src/lib/libc/net/ns_addr.c
+++ /dev/null
@@ -1,233 +0,0 @@
1/* $NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#if defined(LIBC_SCCS) && !defined(lint)
40#if 0
41static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";
42#else
43static char rcsid[] = "$NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd Exp $";
44#endif
45#endif /* LIBC_SCCS and not lint */
46
47#include <sys/param.h>
48#include <netns/ns.h>
49#include <stdio.h>
50#include <string.h>
51
52static struct ns_addr addr, zero_addr;
53
54static void Field(), cvtbase();
55
56struct ns_addr
57ns_addr(name)
58 const char *name;
59{
60 char separator;
61 char *hostname, *socketname, *cp;
62 char buf[50];
63
64 (void)strncpy(buf, name, sizeof(buf) - 1);
65 buf[sizeof(buf) - 1] = '\0';
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 compatability.
72 */
73 if (hostname = strchr(buf, '#'))
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 addr = zero_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, (u_char *)&addr.x_port, 2);
96 }
97
98 Field(hostname, addr.x_host.c_host, 6);
99
100 return (addr);
101}
102
103static void
104Field(buf, out, len)
105 char *buf;
106 u_char *out;
107 int len;
108{
109 register char *bp = buf;
110 int i, ibase, base16 = 0, base10 = 0, clen = 0;
111 int hb[6], *hp;
112 char *fmt;
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 separting 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 /* fall into */
175
176 default:
177 *--bp = 0; /* Ends Loop */
178 }
179 if (base16) {
180 fmt = "%3x";
181 ibase = 4096;
182 } else if (base10 == 0 && *buf == '0') {
183 fmt = "%3o";
184 ibase = 512;
185 } else {
186 fmt = "%3d";
187 ibase = 1000;
188 }
189
190 for (bp = buf; *bp++; ) clen++;
191 if (clen == 0) clen++;
192 if (clen > 18) clen = 18;
193 i = ((clen - 1) / 3) + 1;
194 bp = clen + buf - 3;
195 hp = hb + i - 1;
196
197 while (hp > hb) {
198 (void)sscanf(bp, fmt, hp);
199 bp[0] = 0;
200 hp--;
201 bp -= 3;
202 }
203 (void)sscanf(buf, fmt, hp);
204 cvtbase((long)ibase, 256, hb, i, out, len);
205}
206
207static void
208cvtbase(oldbase,newbase,input,inlen,result,reslen)
209 long oldbase;
210 int newbase;
211 int input[];
212 int inlen;
213 unsigned char result[];
214 int reslen;
215{
216 int d, e;
217 long sum;
218
219 e = 1;
220 while (e > 0 && reslen > 0) {
221 d = 0; e = 0; sum = 0;
222 /* long division: input=input/newbase */
223 while (d < inlen) {
224 sum = sum*oldbase + (long) input[d];
225 e += (sum > 0);
226 input[d++] = sum / newbase;
227 sum %= newbase;
228 }
229 result[--reslen] = sum; /* accumulate remainder */
230 }
231 for (d=0; d < reslen; d++)
232 result[d] = 0;
233}