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