1 /* $NetBSD: compat_ns_addr.c,v 1.3 2012/10/15 22:22:01 msaitoh 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: compat_ns_addr.c,v 1.3 2012/10/15 22:22:01 msaitoh 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(char *, uint8_t *, int);
53 static void cvtbase(long, int, int[], int, uint8_t [], int);
56 ns_addr(const char *name
)
59 char *hostname
, *socketname
, *cp
;
61 static struct ns_addr addr
;
63 _DIAGASSERT(name
!= NULL
);
65 (void)strlcpy(buf
, name
, sizeof(buf
));
68 * First, figure out what he intends as a field separtor.
69 * Despite the way this routine is written, the prefered
70 * form 2-272.AA001234H.01777, i.e. XDE standard.
71 * Great efforts are made to insure backward compatibility.
73 if ((hostname
= strchr(buf
, '#')) != NULL
)
76 hostname
= strchr(buf
, '.');
77 if ((cp
= strchr(buf
, ':')) &&
78 ((hostname
&& cp
< hostname
) || (hostname
== 0))) {
87 memset(&addr
, '\0', sizeof(addr
));
88 Field(buf
, addr
.x_net
.c_net
, 4);
90 return (addr
); /* No separator means net only */
92 socketname
= strchr(hostname
, separator
);
95 Field(socketname
, (uint8_t *)(void *)&addr
.x_port
, 2);
98 Field(hostname
, addr
.x_host
.c_host
, 6);
104 Field(char *buf
, uint8_t *out
, int len
)
106 register char *bp
= buf
;
107 int i
, ibase
, base16
= 0, base10
= 0;
108 unsigned int clen
= 0;
111 _DIAGASSERT(buf
!= NULL
);
112 _DIAGASSERT(out
!= NULL
);
115 * first try 2-273#2-852-151-014#socket
118 (1 < (i
= sscanf(buf
, "%d-%d-%d-%d-%d",
119 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4])))) {
120 cvtbase(1000L, 256, hb
, i
, out
, len
);
124 * try form 8E1#0.0.AA.0.5E.E6#socket
126 if (1 < (i
= sscanf(buf
,"%x.%x.%x.%x.%x.%x",
127 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4], &hb
[5]))) {
128 cvtbase(256L, 256, hb
, i
, out
, len
);
132 * try form 8E1#0:0:AA:0:5E:E6#socket
134 if (1 < (i
= sscanf(buf
,"%x:%x:%x:%x:%x:%x",
135 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4], &hb
[5]))) {
136 cvtbase(256L, 256, hb
, i
, out
, len
);
140 * This is REALLY stretching it but there was a
141 * comma notation separting shorts -- definitely non standard
143 if (1 < (i
= sscanf(buf
,"%x,%x,%x",
144 &hb
[0], &hb
[1], &hb
[2]))) {
145 hb
[0] = htons(hb
[0]); hb
[1] = htons(hb
[1]);
146 hb
[2] = htons(hb
[2]);
147 cvtbase(65536L, 256, hb
, i
, out
, len
);
151 /* Need to decide if base 10, 16 or 8 */
152 while (*bp
) switch (*bp
++) {
154 case '0': case '1': case '2': case '3': case '4': case '5':
155 case '6': case '7': case '-':
162 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
163 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
177 *--bp
= 0; /* Ends Loop */
181 } else if (base10
== 0 && *buf
== '0') {
188 for (bp
= buf
; *bp
++; ) clen
++;
189 if (clen
== 0) clen
++;
190 if (clen
> 18) clen
= 18;
191 i
= ((clen
- 1) / 3) + 1;
197 (void)sscanf(bp
, "%3x", hp
);
199 (void)sscanf(bp
, "%3d", hp
);
201 (void)sscanf(bp
, "%3o", hp
);
208 (void)sscanf(buf
, "%3x", hp
);
210 (void)sscanf(buf
, "%3d", hp
);
212 (void)sscanf(buf
, "%3o", hp
);
214 cvtbase((long)ibase
, 256, hb
, i
, out
, len
);
218 cvtbase(long oldbase
, int newbase
, int input
[], int inlen
,
219 uint8_t result
[], int reslen
)
224 _DIAGASSERT(input
!= NULL
);
225 _DIAGASSERT(result
!= NULL
);
226 _DIAGASSERT(inlen
> 0);
229 while (e
> 0 && reslen
> 0) {
230 d
= 0; e
= 0; sum
= 0;
231 /* long division: input=input/newbase */
233 sum
= sum
*oldbase
+ (long) input
[d
];
235 input
[d
++] = (int) (sum
/ newbase
);
238 /* accumulate remainder */
239 result
[--reslen
] = (unsigned char)sum
;
241 for (d
=0; d
< reslen
; d
++)