1 /* $NetBSD: ns_addr.c,v 1.17 2004/05/09 11:25:20 kleink Exp $ */
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";
40 __RCSID("$NetBSD: ns_addr.c,v 1.17 2004/05/09 11:25:20 kleink Exp $");
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <compat/include/ns.h>
52 static void Field
__P((char *, u_int8_t
*, int));
53 static void cvtbase
__P((long, int, int[], int, u_int8_t
[], int));
60 char *hostname
, *socketname
, *cp
;
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
)
77 hostname
= strchr(buf
, '.');
78 if ((cp
= strchr(buf
, ':')) &&
79 ((hostname
&& cp
< hostname
) || (hostname
== 0))) {
88 memset(&addr
, '\0', sizeof(addr
));
89 Field(buf
, addr
.x_net
.c_net
, 4);
91 return (addr
); /* No separator means net only */
93 socketname
= strchr(hostname
, separator
);
96 Field(socketname
, (u_int8_t
*)(void *)&addr
.x_port
, 2);
99 Field(hostname
, addr
.x_host
.c_host
, 6);
110 register char *bp
= buf
;
111 int i
, ibase
, base16
= 0, base10
= 0, clen
= 0;
114 _DIAGASSERT(buf
!= NULL
);
115 _DIAGASSERT(out
!= NULL
);
118 * first try 2-273#2-852-151-014#socket
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
);
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
);
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
);
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
);
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 '-':
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':
180 *--bp
= 0; /* Ends Loop */
184 } else if (base10
== 0 && *buf
== '0') {
191 for (bp
= buf
; *bp
++; ) clen
++;
192 if (clen
== 0) clen
++;
193 if (clen
> 18) clen
= 18;
194 i
= ((clen
- 1) / 3) + 1;
200 (void)sscanf(bp
, "%3x", hp
);
202 (void)sscanf(bp
, "%3d", hp
);
204 (void)sscanf(bp
, "%3o", hp
);
211 (void)sscanf(buf
, "%3x", hp
);
213 (void)sscanf(buf
, "%3d", hp
);
215 (void)sscanf(buf
, "%3o", hp
);
217 cvtbase((long)ibase
, 256, hb
, i
, out
, len
);
221 cvtbase(oldbase
,newbase
,input
,inlen
,result
,reslen
)
226 unsigned char result
[];
232 _DIAGASSERT(input
!= NULL
);
233 _DIAGASSERT(result
!= NULL
);
236 while (e
> 0 && reslen
> 0) {
237 d
= 0; e
= 0; sum
= 0;
238 /* long division: input=input/newbase */
240 sum
= sum
*oldbase
+ (long) input
[d
];
242 input
[d
++] = (int) (sum
/ newbase
);
245 /* accumulate remainder */
246 result
[--reslen
] = (unsigned char)sum
;
248 for (d
=0; d
< reslen
; d
++)