2 * Copyright (c) 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * $FreeBSD: src/lib/libc/net/ns_addr.c,v 1.3.6.2 2001/07/04 22:34:51 kris Exp $
33 * $DragonFly: src/lib/libc/net/ns_addr.c,v 1.4 2005/09/19 09:34:53 asmodai Exp $
35 * @(#)ns_addr.c 8.1 (Berkeley) 6/7/93
38 #include <sys/param.h>
43 static struct ns_addr addr, zero_addr;
45 static void Field(), cvtbase();
52 char *hostname, *socketname, *cp;
55 (void)strncpy(buf, name, sizeof(buf) - 1);
56 buf[sizeof(buf) - 1] = '\0';
59 * First, figure out what he intends as a field separtor.
60 * Despite the way this routine is written, the preferred
61 * form 2-272.AA001234H.01777, i.e. XDE standard.
62 * Great efforts are made to insure backward compatibility.
64 if ((hostname = strchr(buf, '#')) != NULL)
67 hostname = strchr(buf, '.');
68 if ((cp = strchr(buf, ':')) &&
69 ((hostname && cp < hostname) || (hostname == 0))) {
79 Field(buf, addr.x_net.c_net, 4);
81 return (addr); /* No separator means net only */
83 socketname = strchr(hostname, separator);
86 Field(socketname, (u_char *)&addr.x_port, 2);
89 Field(hostname, addr.x_host.c_host, 6);
101 int i, ibase, base16 = 0, base10 = 0, clen = 0;
106 * first try 2-273#2-852-151-014#socket
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);
115 * try form 8E1#0.0.AA.0.5E.E6#socket
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);
123 * try form 8E1#0:0:AA:0:5E:E6#socket
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);
131 * This is REALLY stretching it but there was a
132 * comma notation separting shorts -- definitely non standard
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);
142 /* Need to decide if base 10, 16 or 8 */
143 while (*bp) switch (*bp++) {
145 case '0': case '1': case '2': case '3': case '4': case '5':
146 case '6': case '7': case '-':
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':
168 *--bp = 0; /* Ends Loop */
173 } else if (base10 == 0 && *buf == '0') {
181 for (bp = buf; *bp++; ) clen++;
182 if (clen == 0) clen++;
183 if (clen > 18) clen = 18;
184 i = ((clen - 1) / 3) + 1;
189 (void)sscanf(bp, fmt, hp);
194 (void)sscanf(buf, fmt, hp);
195 cvtbase((long)ibase, 256, hb, i, out, len);
199 cvtbase(oldbase,newbase,input,inlen,result,reslen)
204 unsigned char result[];
211 while (e > 0 && reslen > 0) {
212 d = 0; e = 0; sum = 0;
213 /* long division: input=input/newbase */
215 sum = sum*oldbase + (long) input[d];
217 input[d++] = sum / newbase;
220 result[--reslen] = sum; /* accumulate remainder */
222 for (d=0; d < reslen; d++)