]>
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.
58 #include <sys/types.h>
60 #include <netinet/in.h>
61 #include <arpa/nameser.h>
69 static char digits
[] = "0123456789";
73 static int special(int);
74 static int printable(int);
75 static int dn_find(const u_char
*, const u_char
*,
76 const u_char
* const *,
77 const u_char
* const *);
82 * ns_name_ntop(src, dst, dstsiz)
83 * Convert an encoded domain name to printable ascii as per RFC1035.
85 * Number of bytes written to buffer, or -1 (with errno set)
87 * The root is returned as "."
88 * All other domains are returned in non absolute form
91 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
) {
101 while ((n
= *cp
++) != 0) {
102 if ((n
& NS_CMPRSFLGS
) != 0) {
103 /* Some kind of compression pointer. */
118 for ((void)NULL
; n
> 0; n
--) {
127 } else if (!printable(c
)) {
133 *dn
++ = digits
[c
/ 100];
134 *dn
++ = digits
[(c
% 100) / 10];
135 *dn
++ = digits
[c
% 10];
157 return ((int)(dn
- dst
));
161 * ns_name_pton(src, dst, dstsiz)
162 * Convert a ascii string into an encoded domain name as per RFC1035.
165 * 1 if string was fully qualified
166 * 0 is string was not fully qualified
168 * Enforces label and domain length limits.
172 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
173 u_char
*label
, *bp
, *eom
;
182 while ((c
= *src
++) != 0) {
184 if ((cp
= strchr(digits
, c
)) != NULL
) {
185 n
= (int)(cp
- digits
) * 100;
186 if ((c
= *src
++) == 0 ||
187 (cp
= strchr(digits
, c
)) == NULL
) {
191 n
+= (int)(cp
- digits
) * 10;
192 if ((c
= *src
++) == 0 ||
193 (cp
= strchr(digits
, c
)) == NULL
) {
197 n
+= (int)(cp
- digits
);
205 } else if (c
== '\\') {
208 } else if (c
== '.') {
209 c
= ((int)(bp
- label
) - 1);
210 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
219 /* Fully qualified ? */
228 if ((bp
- dst
) > MAXCDNAME
) {
247 c
= ((int)(bp
- label
) - 1);
248 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
264 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
272 * ns_name_unpack(msg, eom, src, dst, dstsiz)
273 * Unpack a domain name from a message, source may be compressed.
275 * -1 if it fails, or consumed octets if it succeeds.
278 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
279 u_char
*dst
, size_t dstsiz
)
281 const u_char
*srcp
, *dstlim
;
289 dstlim
= dst
+ dstsiz
;
290 if (srcp
< msg
|| srcp
>= eom
) {
294 /* Fetch next label in domain name. */
295 while ((n
= *srcp
++) != 0) {
296 /* Check for indirection. */
297 switch (n
& NS_CMPRSFLGS
) {
300 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
306 memcpy(dstp
, srcp
, n
);
317 len
= (int)(srcp
- src
) + 1;
318 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
319 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
325 * Check for loops in the compressed name;
326 * if we've looked at the whole message,
327 * there must be a loop.
329 if (checked
>= eom
- msg
) {
337 return (-1); /* flag error */
342 len
= (int)(srcp
- src
);
347 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
348 * Pack domain name 'domain' into 'comp_dn'.
350 * Size of the compressed name, or -1.
352 * 'dnptrs' is an array of pointers to previous compressed names.
353 * dnptrs[0] is a pointer to the beginning of the message. The array
355 * 'lastdnptr' is a pointer to the end of the array pointed to
358 * The list of pointers in dnptrs is updated for labels inserted into
359 * the message as we compress the name. If 'dnptr' is NULL, we don't
360 * try to compress names. If 'lastdnptr' is NULL, we don't update the
364 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
365 const u_char
**dnptrs
, const u_char
**lastdnptr
)
368 const u_char
**cpp
, **lpp
, *eob
, *msg
;
376 if (dnptrs
!= NULL
) {
377 if ((msg
= *dnptrs
++) != NULL
) {
378 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
380 lpp
= cpp
; /* end of list to search */
385 /* make sure the domain we are about to add is legal */
389 if ((n
& NS_CMPRSFLGS
) != 0) {
403 /* Look to see if we can use pointers. */
405 if (n
!= 0 && msg
!= NULL
) {
406 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
407 (const u_char
* const *)lpp
);
409 if (dstp
+ 1 >= eob
) {
413 *dstp
++ = (u_char
)((l
>> 8) | NS_CMPRSFLGS
);
414 *dstp
++ = (u_char
)( l
% 256 );
415 return ((int)(dstp
- dst
));
417 /* Not found, save it. */
418 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
419 (dstp
- msg
) < 0x4000) {
424 /* copy label to buffer */
425 if (n
& NS_CMPRSFLGS
) { /* Should not happen. */
429 if (dstp
+ 1 + n
>= eob
) {
433 memcpy(dstp
, srcp
, n
+ 1);
444 return ((int)(dstp
- dst
));
448 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
449 * Expand compressed domain name to presentation format.
451 * Number of bytes read out of `src', or -1 (with errno set).
453 * Root domain returns as "." not "".
456 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
457 char *dst
, size_t dstsiz
)
459 u_char tmp
[NS_MAXCDNAME
];
462 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
464 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
470 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
471 * Compress a domain name into wire format, using compression pointers.
473 * Number of bytes consumed in `dst' or -1 (with errno set).
475 * 'dnptrs' is an array of pointers to previous compressed names.
476 * dnptrs[0] is a pointer to the beginning of the message.
477 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
478 * array pointed to by 'dnptrs'. Side effect is to update the list of
479 * pointers for labels inserted into the message as we compress the name.
480 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
481 * is NULL, we don't update the list.
484 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
485 const u_char
**dnptrs
, const u_char
**lastdnptr
)
487 u_char tmp
[NS_MAXCDNAME
];
489 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
491 return (ns_name_pack(tmp
, dst
, (int)dstsiz
, dnptrs
, lastdnptr
));
495 * ns_name_skip(ptrptr, eom)
496 * Advance *ptrptr to skip over the compressed name it points at.
498 * 0 on success, -1 (with errno set) on failure.
501 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
) {
506 while (cp
< eom
&& (n
= *cp
++) != 0) {
507 /* Check for indirection. */
508 switch (n
& NS_CMPRSFLGS
) {
509 case 0: /* normal case, n == len */
512 case NS_CMPRSFLGS
: /* indirection */
515 default: /* illegal type */
533 * Thinking in noninternationalized USASCII (per the DNS spec),
534 * is this characted special ("in need of quoting") ?
544 case 0x5C: /* '\\' */
545 /* Special modifiers in zone files. */
556 * Thinking in noninternationalized USASCII (per the DNS spec),
557 * is this character visible and not a space when printed ?
563 return (ch
> 0x20 && ch
< 0x7f);
567 * Thinking in noninternationalized USASCII (per the DNS spec),
568 * convert this character to lower case if it's upper case.
572 if (ch
>= 0x41 && ch
<= 0x5A)
578 * dn_find(domain, msg, dnptrs, lastdnptr)
579 * Search for the counted-label name in an array of compressed names.
581 * offset from msg if found, or -1.
583 * dnptrs is the pointer to the first name on the list,
584 * not the pointer to the start of the message.
587 dn_find(const u_char
*domain
, const u_char
*msg
,
588 const u_char
* const *dnptrs
,
589 const u_char
* const *lastdnptr
)
591 const u_char
*dn
, *cp
, *sp
;
592 const u_char
* const *cpp
;
595 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
598 while ((n
= *cp
++) != 0) {
600 * check for indirection
602 switch (n
& NS_CMPRSFLGS
) {
603 case 0: /* normal case, n == len */
606 for ((void)NULL
; n
> 0; n
--)
607 if (mklower(*dn
++) != mklower(*cp
++))
609 /* Is next root for both ? */
610 if (*dn
== '\0' && *cp
== '\0')
611 return ((int)(sp
- msg
));
616 case NS_CMPRSFLGS
: /* indirection */
617 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
620 default: /* illegal type */