]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/ns_addr.c
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 * Portions copyright (c) 1999, 2000
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley, Intel Corporation, and its contributors.
29 * 4. Neither the name of University, Intel Corporation, or their respective
30 * contributors may be used to endorse or promote products derived from
31 * this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
34 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
35 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
36 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
37 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #if defined(LIBC_SCCS) && !defined(lint)
48 static char sccsid
[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";
49 #endif /* LIBC_SCCS and not lint */
51 #include <sys/param.h>
56 static struct ns_addr addr
, zero_addr
;
58 static void Field(), cvtbase();
66 char *hostname
, *socketname
, *cp
;
69 (void)strncpy(buf
, name
, sizeof(buf
) - 1);
70 buf
[sizeof(buf
) - 1] = '\0';
73 * First, figure out what he intends as a field separtor.
74 * Despite the way this routine is written, the prefered
75 * form 2-272.AA001234H.01777, i.e. XDE standard.
76 * Great efforts are made to insure backward compatability.
78 if ((hostname
= strchr(buf
, '#')) != NULL
)
81 hostname
= strchr(buf
, '.');
82 if ((cp
= strchr(buf
, ':')) &&
83 ((hostname
&& cp
< hostname
) || (hostname
== 0))) {
93 Field(buf
, addr
.x_net
.c_net
, 4);
95 return (addr
); /* No separator means net only */
97 socketname
= strchr(hostname
, separator
);
100 Field(socketname
, (u_char
*)&addr
.x_port
, 2);
103 Field(hostname
, addr
.x_host
.c_host
, 6);
115 register char *bp
= buf
;
116 int i
, ibase
, base16
= 0, base10
= 0, clen
= 0;
121 * first try 2-273#2-852-151-014#socket
124 (1 < (i
= sscanf(buf
, "%d-%d-%d-%d-%d",
125 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4])))) {
126 cvtbase(1000L, 256, hb
, i
, out
, len
);
130 * try form 8E1#0.0.AA.0.5E.E6#socket
132 if (1 < (i
= sscanf(buf
,"%x.%x.%x.%x.%x.%x",
133 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4], &hb
[5]))) {
134 cvtbase(256L, 256, hb
, i
, out
, len
);
138 * try form 8E1#0:0:AA:0:5E:E6#socket
140 if (1 < (i
= sscanf(buf
,"%x:%x:%x:%x:%x:%x",
141 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4], &hb
[5]))) {
142 cvtbase(256L, 256, hb
, i
, out
, len
);
146 * This is REALLY stretching it but there was a
147 * comma notation separting shorts -- definitely non standard
149 if (1 < (i
= sscanf(buf
,"%x,%x,%x",
150 &hb
[0], &hb
[1], &hb
[2]))) {
151 hb
[0] = htons(hb
[0]); hb
[1] = htons(hb
[1]);
152 hb
[2] = htons(hb
[2]);
153 cvtbase(65536L, 256, hb
, i
, out
, len
);
157 /* Need to decide if base 10, 16 or 8 */
158 while (*bp
) switch (*bp
++) {
160 case '0': case '1': case '2': case '3': case '4': case '5':
161 case '6': case '7': case '-':
168 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
169 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
183 *--bp
= 0; /* Ends Loop */
188 } else if (base10
== 0 && *buf
== '0') {
196 for (bp
= buf
; *bp
++; ) clen
++;
197 if (clen
== 0) clen
++;
198 if (clen
> 18) clen
= 18;
199 i
= ((clen
- 1) / 3) + 1;
204 (void)sscanf(bp
, fmt
, hp
);
209 (void)sscanf(buf
, fmt
, hp
);
210 cvtbase((long)ibase
, 256, hb
, i
, out
, len
);
219 unsigned char result
[],
227 while (e
> 0 && reslen
> 0) {
228 d
= 0; e
= 0; sum
= 0;
229 /* long division: input=input/newbase */
231 sum
= sum
*oldbase
+ (long) input
[d
];
233 input
[d
++] = sum
/ newbase
;
236 result
[--reslen
] = (u_char
)sum
; /* accumulate remainder */
238 for (d
=0; d
< reslen
; d
++)