Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / compat / net / compat_ns_addr.c
blob33c4204ab1b9ed203e27af413b9e16c6086e85d6
1 /* $NetBSD: ns_addr.c,v 1.17 2004/05/09 11:25:20 kleink Exp $ */
3 /*
4 * Copyright (c) 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * J.Q. Johnson.
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.
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.
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: ns_addr.c,v 1.17 2004/05/09 11:25:20 kleink Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <compat/include/ns.h>
48 #include <assert.h>
49 #include <stdio.h>
50 #include <string.h>
52 static void Field __P((char *, u_int8_t *, int));
53 static void cvtbase __P((long, int, int[], int, u_int8_t[], int));
55 struct ns_addr
56 ns_addr(name)
57 const char *name;
59 char separator;
60 char *hostname, *socketname, *cp;
61 char buf[50];
62 static struct ns_addr addr;
64 _DIAGASSERT(name != NULL);
66 (void)strlcpy(buf, name, sizeof(buf));
69 * First, figure out what he intends as a field separtor.
70 * Despite the way this routine is written, the prefered
71 * form 2-272.AA001234H.01777, i.e. XDE standard.
72 * Great efforts are made to insure backward compatibility.
74 if ((hostname = strchr(buf, '#')) != NULL)
75 separator = '#';
76 else {
77 hostname = strchr(buf, '.');
78 if ((cp = strchr(buf, ':')) &&
79 ((hostname && cp < hostname) || (hostname == 0))) {
80 hostname = cp;
81 separator = ':';
82 } else
83 separator = '.';
85 if (hostname)
86 *hostname++ = 0;
88 memset(&addr, '\0', sizeof(addr));
89 Field(buf, addr.x_net.c_net, 4);
90 if (hostname == 0)
91 return (addr); /* No separator means net only */
93 socketname = strchr(hostname, separator);
94 if (socketname) {
95 *socketname++ = 0;
96 Field(socketname, (u_int8_t *)(void *)&addr.x_port, 2);
99 Field(hostname, addr.x_host.c_host, 6);
101 return (addr);
104 static void
105 Field(buf, out, len)
106 char *buf;
107 u_int8_t *out;
108 int len;
110 register char *bp = buf;
111 int i, ibase, base16 = 0, base10 = 0, clen = 0;
112 int hb[6], *hp;
114 _DIAGASSERT(buf != NULL);
115 _DIAGASSERT(out != NULL);
118 * first try 2-273#2-852-151-014#socket
120 if ((*buf != '-') &&
121 (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
122 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
123 cvtbase(1000L, 256, hb, i, out, len);
124 return;
127 * try form 8E1#0.0.AA.0.5E.E6#socket
129 if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
130 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
131 cvtbase(256L, 256, hb, i, out, len);
132 return;
135 * try form 8E1#0:0:AA:0:5E:E6#socket
137 if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
138 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
139 cvtbase(256L, 256, hb, i, out, len);
140 return;
143 * This is REALLY stretching it but there was a
144 * comma notation separting shorts -- definitely non standard
146 if (1 < (i = sscanf(buf,"%x,%x,%x",
147 &hb[0], &hb[1], &hb[2]))) {
148 hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
149 hb[2] = htons(hb[2]);
150 cvtbase(65536L, 256, hb, i, out, len);
151 return;
154 /* Need to decide if base 10, 16 or 8 */
155 while (*bp) switch (*bp++) {
157 case '0': case '1': case '2': case '3': case '4': case '5':
158 case '6': case '7': case '-':
159 break;
161 case '8': case '9':
162 base10 = 1;
163 break;
165 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
166 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
167 base16 = 1;
168 break;
170 case 'x': case 'X':
171 *--bp = '0';
172 base16 = 1;
173 break;
175 case 'h': case 'H':
176 base16 = 1;
177 /* FALLTHROUGH */
179 default:
180 *--bp = 0; /* Ends Loop */
182 if (base16) {
183 ibase = 4096;
184 } else if (base10 == 0 && *buf == '0') {
185 ibase = 512;
186 } else {
187 base10 = 1;
188 ibase = 1000;
191 for (bp = buf; *bp++; ) clen++;
192 if (clen == 0) clen++;
193 if (clen > 18) clen = 18;
194 i = ((clen - 1) / 3) + 1;
195 bp = clen + buf - 3;
196 hp = hb + i - 1;
198 while (hp > hb) {
199 if (base16)
200 (void)sscanf(bp, "%3x", hp);
201 else if (base10)
202 (void)sscanf(bp, "%3d", hp);
203 else
204 (void)sscanf(bp, "%3o", hp);
206 bp[0] = 0;
207 hp--;
208 bp -= 3;
210 if (base16)
211 (void)sscanf(buf, "%3x", hp);
212 else if (base10)
213 (void)sscanf(buf, "%3d", hp);
214 else
215 (void)sscanf(buf, "%3o", hp);
217 cvtbase((long)ibase, 256, hb, i, out, len);
220 static void
221 cvtbase(oldbase,newbase,input,inlen,result,reslen)
222 long oldbase;
223 int newbase;
224 int input[];
225 int inlen;
226 unsigned char result[];
227 int reslen;
229 int d, e;
230 long sum;
232 _DIAGASSERT(input != NULL);
233 _DIAGASSERT(result != NULL);
235 e = 1;
236 while (e > 0 && reslen > 0) {
237 d = 0; e = 0; sum = 0;
238 /* long division: input=input/newbase */
239 while (d < inlen) {
240 sum = sum*oldbase + (long) input[d];
241 e += (sum > 0);
242 input[d++] = (int) (sum / newbase);
243 sum %= newbase;
245 /* accumulate remainder */
246 result[--reslen] = (unsigned char)sum;
248 for (d=0; d < reslen; d++)
249 result[d] = 0;