MFC r1.28:
[dragonfly.git] / lib / libc / net / ns_addr.c
blobe4904a3a4645b2d7b7af5451e636a4bed71412a6
1 /*
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
6 * J.Q. Johnson.
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.
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.
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.5 2005/11/13 02:04:47 swildner Exp $
35 * @(#)ns_addr.c 8.1 (Berkeley) 6/7/93
38 #include <sys/param.h>
39 #include <netns/ns.h>
40 #include <stdio.h>
41 #include <string.h>
43 static struct ns_addr addr, zero_addr;
45 static void Field(), cvtbase();
47 struct ns_addr
48 ns_addr(const char *name)
50 char separator;
51 char *hostname, *socketname, *cp;
52 char buf[50];
54 strncpy(buf, name, sizeof(buf) - 1);
55 buf[sizeof(buf) - 1] = '\0';
58 * First, figure out what he intends as a field separtor.
59 * Despite the way this routine is written, the preferred
60 * form 2-272.AA001234H.01777, i.e. XDE standard.
61 * Great efforts are made to insure backward compatibility.
63 if ((hostname = strchr(buf, '#')) != NULL)
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 = '.';
74 if (hostname)
75 *hostname++ = 0;
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 */
82 socketname = strchr(hostname, separator);
83 if (socketname) {
84 *socketname++ = 0;
85 Field(socketname, (u_char *)&addr.x_port, 2);
88 Field(hostname, addr.x_host.c_host, 6);
90 return (addr);
93 static void
94 Field(char *buf, u_char *out, int len)
96 char *bp = buf;
97 int i, ibase, base16 = 0, base10 = 0, clen = 0;
98 int hb[6], *hp;
99 char *fmt;
102 * first try 2-273#2-852-151-014#socket
104 if ((*buf != '-') &&
105 (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
106 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
107 cvtbase(1000L, 256, hb, i, out, len);
108 return;
111 * try form 8E1#0.0.AA.0.5E.E6#socket
113 if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
114 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
115 cvtbase(256L, 256, hb, i, out, len);
116 return;
119 * try form 8E1#0:0:AA:0:5E:E6#socket
121 if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
122 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
123 cvtbase(256L, 256, hb, i, out, len);
124 return;
127 * This is REALLY stretching it but there was a
128 * comma notation separting shorts -- definitely non standard
130 if (1 < (i = sscanf(buf,"%x,%x,%x",
131 &hb[0], &hb[1], &hb[2]))) {
132 hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
133 hb[2] = htons(hb[2]);
134 cvtbase(65536L, 256, hb, i, out, len);
135 return;
138 /* Need to decide if base 10, 16 or 8 */
139 while (*bp) switch (*bp++) {
141 case '0': case '1': case '2': case '3': case '4': case '5':
142 case '6': case '7': case '-':
143 break;
145 case '8': case '9':
146 base10 = 1;
147 break;
149 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
150 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
151 base16 = 1;
152 break;
154 case 'x': case 'X':
155 *--bp = '0';
156 base16 = 1;
157 break;
159 case 'h': case 'H':
160 base16 = 1;
161 /* fall into */
163 default:
164 *--bp = 0; /* Ends Loop */
166 if (base16) {
167 fmt = "%3x";
168 ibase = 4096;
169 } else if (base10 == 0 && *buf == '0') {
170 fmt = "%3o";
171 ibase = 512;
172 } else {
173 fmt = "%3d";
174 ibase = 1000;
177 for (bp = buf; *bp++; ) clen++;
178 if (clen == 0) clen++;
179 if (clen > 18) clen = 18;
180 i = ((clen - 1) / 3) + 1;
181 bp = clen + buf - 3;
182 hp = hb + i - 1;
184 while (hp > hb) {
185 sscanf(bp, fmt, hp);
186 bp[0] = 0;
187 hp--;
188 bp -= 3;
190 sscanf(buf, fmt, hp);
191 cvtbase((long)ibase, 256, hb, i, out, len);
194 static void
195 cvtbase(long oldbase, int newbase, int input[], int inlen,
196 unsigned char result[], int reslen)
198 int d, e;
199 long sum;
201 e = 1;
202 while (e > 0 && reslen > 0) {
203 d = 0; e = 0; sum = 0;
204 /* long division: input=input/newbase */
205 while (d < inlen) {
206 sum = sum*oldbase + (long) input[d];
207 e += (sum > 0);
208 input[d++] = sum / newbase;
209 sum %= newbase;
211 result[--reslen] = sum; /* accumulate remainder */
213 for (d=0; d < reslen; d++)
214 result[d] = 0;