]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/ns_name.c
2 * Copyright (c) 1996 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19 * Portions copyright (c) 1999, 2000
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
37 * This product includes software developed by Intel Corporation and
40 * 4. Neither the name of Intel Corporation or its contributors may be
41 * used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
59 static char rcsid
[] = "$Id: ns_name.c,v 1.1.1.1 2003/11/19 01:51:32 kyu3 Exp $";
62 #include <sys/types.h>
64 #include <netinet/in.h>
65 #include <arpa/nameser.h>
73 static char digits
[] = "0123456789";
77 static int special(int);
78 static int printable(int);
79 static int dn_find(const u_char
*, const u_char
*,
80 const u_char
* const *,
81 const u_char
* const *);
86 * ns_name_ntop(src, dst, dstsiz)
87 * Convert an encoded domain name to printable ascii as per RFC1035.
89 * Number of bytes written to buffer, or -1 (with errno set)
91 * The root is returned as "."
92 * All other domains are returned in non absolute form
95 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
) {
105 while ((n
= *cp
++) != 0) {
106 if ((n
& NS_CMPRSFLGS
) != 0) {
107 /* Some kind of compression pointer. */
122 for ((void)NULL
; n
> 0; n
--) {
131 } else if (!printable(c
)) {
137 *dn
++ = digits
[c
/ 100];
138 *dn
++ = digits
[(c
% 100) / 10];
139 *dn
++ = digits
[c
% 10];
161 return ((int)(dn
- dst
));
165 * ns_name_pton(src, dst, dstsiz)
166 * Convert a ascii string into an encoded domain name as per RFC1035.
169 * 1 if string was fully qualified
170 * 0 is string was not fully qualified
172 * Enforces label and domain length limits.
176 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
177 u_char
*label
, *bp
, *eom
;
186 while ((c
= *src
++) != 0) {
188 if ((cp
= strchr(digits
, c
)) != NULL
) {
189 n
= (int)(cp
- digits
) * 100;
190 if ((c
= *src
++) == 0 ||
191 (cp
= strchr(digits
, c
)) == NULL
) {
195 n
+= (int)(cp
- digits
) * 10;
196 if ((c
= *src
++) == 0 ||
197 (cp
= strchr(digits
, c
)) == NULL
) {
201 n
+= (int)(cp
- digits
);
209 } else if (c
== '\\') {
212 } else if (c
== '.') {
213 c
= ((int)(bp
- label
) - 1);
214 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
223 /* Fully qualified ? */
232 if ((bp
- dst
) > MAXCDNAME
) {
251 c
= ((int)(bp
- label
) - 1);
252 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
268 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
276 * ns_name_unpack(msg, eom, src, dst, dstsiz)
277 * Unpack a domain name from a message, source may be compressed.
279 * -1 if it fails, or consumed octets if it succeeds.
282 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
283 u_char
*dst
, size_t dstsiz
)
285 const u_char
*srcp
, *dstlim
;
293 dstlim
= dst
+ dstsiz
;
294 if (srcp
< msg
|| srcp
>= eom
) {
298 /* Fetch next label in domain name. */
299 while ((n
= *srcp
++) != 0) {
300 /* Check for indirection. */
301 switch (n
& NS_CMPRSFLGS
) {
304 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
310 memcpy(dstp
, srcp
, n
);
321 len
= (int)(srcp
- src
) + 1;
322 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
323 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
329 * Check for loops in the compressed name;
330 * if we've looked at the whole message,
331 * there must be a loop.
333 if (checked
>= eom
- msg
) {
341 return (-1); /* flag error */
346 len
= (int)(srcp
- src
);
351 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
352 * Pack domain name 'domain' into 'comp_dn'.
354 * Size of the compressed name, or -1.
356 * 'dnptrs' is an array of pointers to previous compressed names.
357 * dnptrs[0] is a pointer to the beginning of the message. The array
359 * 'lastdnptr' is a pointer to the end of the array pointed to
362 * The list of pointers in dnptrs is updated for labels inserted into
363 * the message as we compress the name. If 'dnptr' is NULL, we don't
364 * try to compress names. If 'lastdnptr' is NULL, we don't update the
368 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
369 const u_char
**dnptrs
, const u_char
**lastdnptr
)
372 const u_char
**cpp
, **lpp
, *eob
, *msg
;
380 if (dnptrs
!= NULL
) {
381 if ((msg
= *dnptrs
++) != NULL
) {
382 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
384 lpp
= cpp
; /* end of list to search */
389 /* make sure the domain we are about to add is legal */
393 if ((n
& NS_CMPRSFLGS
) != 0) {
407 /* Look to see if we can use pointers. */
409 if (n
!= 0 && msg
!= NULL
) {
410 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
411 (const u_char
* const *)lpp
);
413 if (dstp
+ 1 >= eob
) {
417 *dstp
++ = (u_char
)((l
>> 8) | NS_CMPRSFLGS
);
418 *dstp
++ = (u_char
)( l
% 256 );
419 return ((int)(dstp
- dst
));
421 /* Not found, save it. */
422 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
423 (dstp
- msg
) < 0x4000) {
428 /* copy label to buffer */
429 if (n
& NS_CMPRSFLGS
) { /* Should not happen. */
433 if (dstp
+ 1 + n
>= eob
) {
437 memcpy(dstp
, srcp
, n
+ 1);
448 return ((int)(dstp
- dst
));
452 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
453 * Expand compressed domain name to presentation format.
455 * Number of bytes read out of `src', or -1 (with errno set).
457 * Root domain returns as "." not "".
460 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
461 char *dst
, size_t dstsiz
)
463 u_char tmp
[NS_MAXCDNAME
];
466 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
468 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
474 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
475 * Compress a domain name into wire format, using compression pointers.
477 * Number of bytes consumed in `dst' or -1 (with errno set).
479 * 'dnptrs' is an array of pointers to previous compressed names.
480 * dnptrs[0] is a pointer to the beginning of the message.
481 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
482 * array pointed to by 'dnptrs'. Side effect is to update the list of
483 * pointers for labels inserted into the message as we compress the name.
484 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
485 * is NULL, we don't update the list.
488 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
489 const u_char
**dnptrs
, const u_char
**lastdnptr
)
491 u_char tmp
[NS_MAXCDNAME
];
493 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
495 return (ns_name_pack(tmp
, dst
, (int)dstsiz
, dnptrs
, lastdnptr
));
499 * ns_name_skip(ptrptr, eom)
500 * Advance *ptrptr to skip over the compressed name it points at.
502 * 0 on success, -1 (with errno set) on failure.
505 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
) {
510 while (cp
< eom
&& (n
= *cp
++) != 0) {
511 /* Check for indirection. */
512 switch (n
& NS_CMPRSFLGS
) {
513 case 0: /* normal case, n == len */
516 case NS_CMPRSFLGS
: /* indirection */
519 default: /* illegal type */
537 * Thinking in noninternationalized USASCII (per the DNS spec),
538 * is this characted special ("in need of quoting") ?
548 case 0x5C: /* '\\' */
549 /* Special modifiers in zone files. */
560 * Thinking in noninternationalized USASCII (per the DNS spec),
561 * is this character visible and not a space when printed ?
567 return (ch
> 0x20 && ch
< 0x7f);
571 * Thinking in noninternationalized USASCII (per the DNS spec),
572 * convert this character to lower case if it's upper case.
576 if (ch
>= 0x41 && ch
<= 0x5A)
582 * dn_find(domain, msg, dnptrs, lastdnptr)
583 * Search for the counted-label name in an array of compressed names.
585 * offset from msg if found, or -1.
587 * dnptrs is the pointer to the first name on the list,
588 * not the pointer to the start of the message.
591 dn_find(const u_char
*domain
, const u_char
*msg
,
592 const u_char
* const *dnptrs
,
593 const u_char
* const *lastdnptr
)
595 const u_char
*dn
, *cp
, *sp
;
596 const u_char
* const *cpp
;
599 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
602 while ((n
= *cp
++) != 0) {
604 * check for indirection
606 switch (n
& NS_CMPRSFLGS
) {
607 case 0: /* normal case, n == len */
610 for ((void)NULL
; n
> 0; n
--)
611 if (mklower(*dn
++) != mklower(*cp
++))
613 /* Is next root for both ? */
614 if (*dn
== '\0' && *cp
== '\0')
615 return ((int)(sp
- msg
));
620 case NS_CMPRSFLGS
: /* indirection */
621 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
624 default: /* illegal type */