Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / net / ns_addr.c
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/lib/libc/net/ns_addr.c,v 1.3.6.2 2001/07/04 22:34:51 kris Exp $
37  */
38
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)ns_addr.c   8.1 (Berkeley) 6/7/93";
41 #endif /* LIBC_SCCS and not lint */
42
43 #include <sys/param.h>
44 #include <netns/ns.h>
45 #include <stdio.h>
46 #include <string.h>
47
48 static struct ns_addr addr, zero_addr;
49
50 static void Field(), cvtbase();
51
52 struct ns_addr
53 ns_addr(name)
54         const char *name;
55 {
56         char separator;
57         char *hostname, *socketname, *cp;
58         char buf[50];
59
60         (void)strncpy(buf, name, sizeof(buf) - 1);
61         buf[sizeof(buf) - 1] = '\0';
62
63         /*
64          * First, figure out what he intends as a field separtor.
65          * Despite the way this routine is written, the preferred
66          * form  2-272.AA001234H.01777, i.e. XDE standard.
67          * Great efforts are made to insure backward compatibility.
68          */
69         if ((hostname = strchr(buf, '#')) != NULL)
70                 separator = '#';
71         else {
72                 hostname = strchr(buf, '.');
73                 if ((cp = strchr(buf, ':')) &&
74                     ((hostname && cp < hostname) || (hostname == 0))) {
75                         hostname = cp;
76                         separator = ':';
77                 } else
78                         separator = '.';
79         }
80         if (hostname)
81                 *hostname++ = 0;
82
83         addr = zero_addr;
84         Field(buf, addr.x_net.c_net, 4);
85         if (hostname == 0)
86                 return (addr);  /* No separator means net only */
87
88         socketname = strchr(hostname, separator);
89         if (socketname) {
90                 *socketname++ = 0;
91                 Field(socketname, (u_char *)&addr.x_port, 2);
92         }
93
94         Field(hostname, addr.x_host.c_host, 6);
95
96         return (addr);
97 }
98
99 static void
100 Field(buf, out, len)
101         char *buf;
102         u_char *out;
103         int len;
104 {
105         register char *bp = buf;
106         int i, ibase, base16 = 0, base10 = 0, clen = 0;
107         int hb[6], *hp;
108         char *fmt;
109
110         /*
111          * first try 2-273#2-852-151-014#socket
112          */
113         if ((*buf != '-') &&
114             (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
115                         &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
116                 cvtbase(1000L, 256, hb, i, out, len);
117                 return;
118         }
119         /*
120          * try form 8E1#0.0.AA.0.5E.E6#socket
121          */
122         if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
123                         &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
124                 cvtbase(256L, 256, hb, i, out, len);
125                 return;
126         }
127         /*
128          * try form 8E1#0:0:AA:0:5E:E6#socket
129          */
130         if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
131                         &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
132                 cvtbase(256L, 256, hb, i, out, len);
133                 return;
134         }
135         /*
136          * This is REALLY stretching it but there was a
137          * comma notation separting shorts -- definitely non standard
138          */
139         if (1 < (i = sscanf(buf,"%x,%x,%x",
140                         &hb[0], &hb[1], &hb[2]))) {
141                 hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
142                 hb[2] = htons(hb[2]);
143                 cvtbase(65536L, 256, hb, i, out, len);
144                 return;
145         }
146
147         /* Need to decide if base 10, 16 or 8 */
148         while (*bp) switch (*bp++) {
149
150         case '0': case '1': case '2': case '3': case '4': case '5':
151         case '6': case '7': case '-':
152                 break;
153
154         case '8': case '9':
155                 base10 = 1;
156                 break;
157
158         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
159         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
160                 base16 = 1;
161                 break;
162
163         case 'x': case 'X':
164                 *--bp = '0';
165                 base16 = 1;
166                 break;
167
168         case 'h': case 'H':
169                 base16 = 1;
170                 /* fall into */
171
172         default:
173                 *--bp = 0; /* Ends Loop */
174         }
175         if (base16) {
176                 fmt = "%3x";
177                 ibase = 4096;
178         } else if (base10 == 0 && *buf == '0') {
179                 fmt = "%3o";
180                 ibase = 512;
181         } else {
182                 fmt = "%3d";
183                 ibase = 1000;
184         }
185
186         for (bp = buf; *bp++; ) clen++;
187         if (clen == 0) clen++;
188         if (clen > 18) clen = 18;
189         i = ((clen - 1) / 3) + 1;
190         bp = clen + buf - 3;
191         hp = hb + i - 1;
192
193         while (hp > hb) {
194                 (void)sscanf(bp, fmt, hp);
195                 bp[0] = 0;
196                 hp--;
197                 bp -= 3;
198         }
199         (void)sscanf(buf, fmt, hp);
200         cvtbase((long)ibase, 256, hb, i, out, len);
201 }
202
203 static void
204 cvtbase(oldbase,newbase,input,inlen,result,reslen)
205         long oldbase;
206         int newbase;
207         int input[];
208         int inlen;
209         unsigned char result[];
210         int reslen;
211 {
212         int d, e;
213         long sum;
214
215         e = 1;
216         while (e > 0 && reslen > 0) {
217                 d = 0; e = 0; sum = 0;
218                 /* long division: input=input/newbase */
219                 while (d < inlen) {
220                         sum = sum*oldbase + (long) input[d];
221                         e += (sum > 0);
222                         input[d++] = sum / newbase;
223                         sum %= newbase;
224                 }
225                 result[--reslen] = sum; /* accumulate remainder */
226         }
227         for (d=0; d < reslen; d++)
228                 result[d] = 0;
229 }