2 * Copyright (c) 1996,1999 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
20 #include <sys/types.h>
22 #ifdef HAVE_NETINET_IN_H
23 # include <netinet/in.h>
25 #ifdef HAVE_ARPA_NAMESER_H
26 # include <arpa/nameser.h>
39 static const char digits
[] = "0123456789";
43 static int special(int);
44 static int printable(int);
49 * dns_ns_name_ntop(src, dst, dstsiz)
50 * Convert an encoded domain name to printable ascii as per RFC1035.
52 * Number of bytes written to buffer, or -1 (with errno set)
54 * The root is returned as "."
55 * All other domains are returned in non absolute form
58 dns_ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
) {
68 while ((n
= *cp
++) != 0) {
69 if ((n
& NS_CMPRSFLGS
) != 0 && n
!= 0x41) {
70 /* Some kind of compression pointer. */
82 if (dn
+ n
* 2 + 4 >= eom
) {
93 *dn
++ = u
> 9 ? 'a' + u
- 10 : '0' + u
;
95 *dn
++ = u
> 9 ? 'a' + u
- 10 : '0' + u
;
105 for ((void)NULL
; n
> 0; n
--) {
113 } else if (!printable(c
)) {
118 *dn
++ = digits
[c
/ 100];
119 *dn
++ = digits
[(c
% 100) / 10];
120 *dn
++ = digits
[c
% 10];
143 * dns_ns_name_pton(src, dst, dstsiz)
144 * Convert a ascii string into an encoded domain name as per RFC1035.
147 * 1 if string was fully qualified
148 * 0 is string was not fully qualified
150 * Enforces label and domain length limits.
154 dns_ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
155 u_char
*label
, *bp
, *eom
;
164 while ((c
= *src
++) != 0) {
166 if ((cp
= strchr(digits
, c
)) != NULL
) {
167 n
= (cp
- digits
) * 100;
168 if ((c
= *src
++) == 0 ||
169 (cp
= strchr(digits
, c
)) == NULL
) {
172 n
+= (cp
- digits
) * 10;
173 if ((c
= *src
++) == 0 ||
174 (cp
= strchr(digits
, c
)) == NULL
) {
182 } else if (c
== '[' && label
== bp
- 1 && *src
== 'x') {
183 /* Theoretically we would have to handle \[o
184 as well but we do not since we do not need
189 while (isxdigit (*src
)) {
190 n
= *src
> '9' ? *src
- 'a' + 10 : *src
- '0';
192 if (! isxdigit(*src
)) {
196 n
+= *src
> '9' ? *src
- 'a' + 10 : *src
- '0';
203 *label
= (bp
- label
- 1) * 8;
204 if (*src
++ != ']' || *src
++ != '.') {
215 } else if (c
== '\\') {
218 } else if (c
== '.') {
219 c
= (bp
- label
- 1);
220 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
227 /* Fully qualified ? */
235 if ((bp
- dst
) > NS_MAXCDNAME
) {
240 if (c
== 0 || *src
== '.') {
251 c
= (bp
- label
- 1);
252 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
265 if ((bp
- dst
) > NS_MAXCDNAME
) { /* src too big */
273 * dns_ns_name_unpack(msg, eom, src, dst, dstsiz)
274 * Unpack a domain name from a message, source may be compressed.
276 * -1 if it fails, or consumed octets if it succeeds.
279 dns_ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
280 u_char
*dst
, size_t dstsiz
)
282 const u_char
*srcp
, *dstlim
;
290 dstlim
= dst
+ dstsiz
;
291 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
+ 1 >= dstlim
) {
307 return (-1); /* flag error */
312 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
316 dstp
= memcpy(dstp
, srcp
- 1, n
+ 1);
326 len
= srcp
- src
+ 1;
327 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
328 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
333 * Check for loops in the compressed name;
334 * if we've looked at the whole message,
335 * there must be a loop.
337 if (checked
>= eom
- msg
) {
343 return (-1); /* flag error */
354 * dns_ns_name_uncompress(msg, eom, src, dst, dstsiz)
355 * Expand compressed domain name to presentation format.
357 * Number of bytes read out of `src', or -1 (with errno set).
359 * Root domain returns as "." not "".
362 dns_ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
363 char *dst
, size_t dstsiz
)
365 u_char tmp
[NS_MAXCDNAME
];
368 if ((n
= dns_ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
370 if (dns_ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
377 * dns_ns_name_skip(ptrptr, eom)
378 * Advance *ptrptr to skip over the compressed name it points at.
380 * 0 on success, -1 (with errno set) on failure.
383 dns_ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
) {
388 while (cp
< eom
&& (n
= *cp
++) != 0) {
389 /* Check for indirection. */
390 switch (n
& NS_CMPRSFLGS
) {
391 case 0: /* normal case, n == len */
394 case NS_CMPRSFLGS
: /* indirection */
397 default: /* illegal type */
413 * Thinking in noninternationalized USASCII (per the DNS spec),
414 * is this characted special ("in need of quoting") ?
424 case 0x5C: /* '\\' */
425 /* Special modifiers in zone files. */
436 * Thinking in noninternationalized USASCII (per the DNS spec),
437 * is this character visible and not a space when printed ?
443 return (ch
> 0x20 && ch
< 0x7f);