]>
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 (char *buf
, u_char
*out
, int len
);
59 static void cvtbase (long oldbase
, int newbase
, int input
[], int inlen
, unsigned char result
[], int reslen
);
67 char *hostname
, *socketname
, *cp
;
70 (void)strncpy(buf
, name
, sizeof(buf
) - 1);
71 buf
[sizeof(buf
) - 1] = '\0';
74 * First, figure out what he intends as a field separtor.
75 * Despite the way this routine is written, the prefered
76 * form 2-272.AA001234H.01777, i.e. XDE standard.
77 * Great efforts are made to insure backward compatability.
79 if ((hostname
= strchr(buf
, '#')) != NULL
)
82 hostname
= strchr(buf
, '.');
83 if ((cp
= strchr(buf
, ':')) &&
84 ((hostname
&& cp
< hostname
) || (hostname
== 0))) {
94 Field(buf
, addr
.x_net
.c_net
, 4);
96 return (addr
); /* No separator means net only */
98 socketname
= strchr(hostname
, separator
);
101 Field(socketname
, (u_char
*)&addr
.x_port
, 2);
104 Field(hostname
, addr
.x_host
.c_host
, 6);
116 register char *bp
= buf
;
117 int i
, ibase
, base16
= 0, base10
= 0, clen
= 0;
122 * first try 2-273#2-852-151-014#socket
125 (1 < (i
= sscanf(buf
, "%d-%d-%d-%d-%d",
126 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4])))) {
127 cvtbase(1000L, 256, hb
, i
, out
, len
);
131 * try form 8E1#0.0.AA.0.5E.E6#socket
133 if (1 < (i
= sscanf(buf
,"%x.%x.%x.%x.%x.%x",
134 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4], &hb
[5]))) {
135 cvtbase(256L, 256, hb
, i
, out
, len
);
139 * try form 8E1#0:0:AA:0:5E:E6#socket
141 if (1 < (i
= sscanf(buf
,"%x:%x:%x:%x:%x:%x",
142 &hb
[0], &hb
[1], &hb
[2], &hb
[3], &hb
[4], &hb
[5]))) {
143 cvtbase(256L, 256, hb
, i
, out
, len
);
147 * This is REALLY stretching it but there was a
148 * comma notation separting shorts -- definitely non standard
150 if (1 < (i
= sscanf(buf
,"%x,%x,%x",
151 &hb
[0], &hb
[1], &hb
[2]))) {
152 hb
[0] = htons(hb
[0]); hb
[1] = htons(hb
[1]);
153 hb
[2] = htons(hb
[2]);
154 cvtbase(65536L, 256, hb
, i
, out
, len
);
158 /* Need to decide if base 10, 16 or 8 */
159 while (*bp
) switch (*bp
++) {
161 case '0': case '1': case '2': case '3': case '4': case '5':
162 case '6': case '7': case '-':
169 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
170 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
184 *--bp
= 0; /* Ends Loop */
189 } else if (base10
== 0 && *buf
== '0') {
197 for (bp
= buf
; *bp
++; ) clen
++;
198 if (clen
== 0) clen
++;
199 if (clen
> 18) clen
= 18;
200 i
= ((clen
- 1) / 3) + 1;
205 (void)sscanf(bp
, fmt
, hp
);
210 (void)sscanf(buf
, fmt
, hp
);
211 cvtbase((long)ibase
, 256, hb
, i
, out
, len
);
220 unsigned char result
[],
228 while (e
> 0 && reslen
> 0) {
229 d
= 0; e
= 0; sum
= 0;
230 /* long division: input=input/newbase */
232 sum
= sum
*oldbase
+ (long) input
[d
];
234 input
[d
++] = sum
/ newbase
;
237 result
[--reslen
] = (u_char
)sum
; /* accumulate remainder */
239 for (d
=0; d
< reslen
; d
++)