etc/protocols - sync with NetBSD-8
[minix.git] / external / bsd / dhcp / dist / common / ns_name.c
blob8ff594a33aa1e81e627a093dedaa2063ad1f1dec
1 /* $NetBSD: ns_name.c,v 1.7 2014/07/12 12:09:37 spz Exp $ */
2 /*
3 * Copyright (c) 2004,2009 by Internet Systems Consortium, Inc. ("ISC")
4 * Copyright (c) 1996-2003 by Internet Software Consortium
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Internet Systems Consortium, Inc.
19 * 950 Charter Street
20 * Redwood City, CA 94063
21 * <info@isc.org>
22 * http://www.isc.org/
25 #include <sys/cdefs.h>
26 __RCSID("$NetBSD: ns_name.c,v 1.7 2014/07/12 12:09:37 spz Exp $");
28 #ifndef lint
29 static const char rcsid[] = "Id: ns_name.c,v 1.2 2009/10/28 04:12:29 sar Exp ";
30 #endif
32 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <sys/socket.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <ctype.h>
41 #include "minires.h"
42 #include "arpa/nameser.h"
44 /* Data. */
46 static const char digits[] = "0123456789";
48 /* Forward. */
50 static int special(int);
51 static int printable(int);
52 static int dn_find(const u_char *, const u_char *,
53 const u_char * const *,
54 const u_char * const *);
56 /* Public. */
59 * MRns_name_ntop(src, dst, dstsiz)
60 * Convert an encoded domain name to printable ascii as per RFC1035.
61 * return:
62 * Number of bytes written to buffer, or -1 (with errno set)
63 * notes:
64 * The root is returned as "."
65 * All other domains are returned in non absolute form
67 int
68 MRns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
69 const u_char *cp;
70 char *dn, *eom;
71 u_char c;
72 u_int n;
74 cp = src;
75 dn = dst;
76 eom = dst + dstsiz;
78 while ((n = *cp++) != 0) {
79 if ((n & NS_CMPRSFLGS) != 0) {
80 /* Some kind of compression pointer. */
81 errno = EMSGSIZE;
82 return (-1);
84 if (dn != dst) {
85 if (dn >= eom) {
86 errno = EMSGSIZE;
87 return (-1);
89 *dn++ = '.';
91 if (dn + n >= eom) {
92 errno = EMSGSIZE;
93 return (-1);
95 for ((void)NULL; n > 0; n--) {
96 c = *cp++;
97 if (special(c)) {
98 if (dn + 1 >= eom) {
99 errno = EMSGSIZE;
100 return (-1);
102 *dn++ = '\\';
103 *dn++ = (char)c;
104 } else if (!printable(c)) {
105 if (dn + 3 >= eom) {
106 errno = EMSGSIZE;
107 return (-1);
109 *dn++ = '\\';
110 *dn++ = digits[c / 100];
111 *dn++ = digits[(c % 100) / 10];
112 *dn++ = digits[c % 10];
113 } else {
114 if (dn >= eom) {
115 errno = EMSGSIZE;
116 return (-1);
118 *dn++ = (char)c;
122 if (dn == dst) {
123 if (dn >= eom) {
124 errno = EMSGSIZE;
125 return (-1);
127 *dn++ = '.';
129 if (dn >= eom) {
130 errno = EMSGSIZE;
131 return (-1);
133 *dn++ = '\0';
134 return (dn - dst);
138 * MRns_name_pton(src, dst, dstsiz)
139 * Convert a ascii string into an encoded domain name as per RFC1035.
140 * return:
141 * -1 if it fails
142 * 1 if string was fully qualified
143 * 0 is string was not fully qualified
144 * notes:
145 * Enforces label and domain length limits.
149 MRns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
150 u_char *label, *bp, *eom;
151 int c, n, escaped;
152 char *cp;
154 escaped = 0;
155 bp = dst;
156 eom = dst + dstsiz;
157 label = bp++;
159 while ((c = *src++) != 0) {
160 if (escaped) {
161 if ((cp = strchr(digits, c)) != NULL) {
162 n = (cp - digits) * 100;
163 if ((c = *src++) == 0 ||
164 (cp = strchr(digits, c)) == NULL) {
165 errno = EMSGSIZE;
166 return (-1);
168 n += (cp - digits) * 10;
169 if ((c = *src++) == 0 ||
170 (cp = strchr(digits, c)) == NULL) {
171 errno = EMSGSIZE;
172 return (-1);
174 n += (cp - digits);
175 if (n > 255) {
176 errno = EMSGSIZE;
177 return (-1);
179 c = n;
181 escaped = 0;
182 } else if (c == '\\') {
183 escaped = 1;
184 continue;
185 } else if (c == '.') {
186 c = (bp - label - 1);
187 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
188 errno = EMSGSIZE;
189 return (-1);
191 if (label >= eom) {
192 errno = EMSGSIZE;
193 return (-1);
195 *label = c;
196 /* Fully qualified ? */
197 if (*src == '\0') {
198 if (c != 0) {
199 if (bp >= eom) {
200 errno = EMSGSIZE;
201 return (-1);
203 *bp++ = '\0';
205 if ((bp - dst) > MAXCDNAME) {
206 errno = EMSGSIZE;
207 return (-1);
209 return (1);
211 if (c == 0 || *src == '.') {
212 errno = EMSGSIZE;
213 return (-1);
215 label = bp++;
216 continue;
218 if (bp >= eom) {
219 errno = EMSGSIZE;
220 return (-1);
222 *bp++ = (u_char)c;
224 c = (bp - label - 1);
225 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
226 errno = EMSGSIZE;
227 return (-1);
229 if (label >= eom) {
230 errno = EMSGSIZE;
231 return (-1);
233 *label = c;
234 if (c != 0) {
235 if (bp >= eom) {
236 errno = EMSGSIZE;
237 return (-1);
239 *bp++ = 0;
241 if ((bp - dst) > MAXCDNAME) { /* src too big */
242 errno = EMSGSIZE;
243 return (-1);
245 return (0);
248 #ifdef notdef
250 * MRns_name_ntol(src, dst, dstsiz)
251 * Convert a network strings labels into all lowercase.
252 * return:
253 * Number of bytes written to buffer, or -1 (with errno set)
254 * notes:
255 * Enforces label and domain length limits.
259 MRns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) {
260 const u_char *cp;
261 u_char *dn, *eom;
262 u_char c;
263 u_int n;
265 cp = src;
266 dn = dst;
267 eom = dst + dstsiz;
269 if (dn >= eom) {
270 errno = EMSGSIZE;
271 return (-1);
273 while ((n = *cp++) != 0) {
274 if ((n & NS_CMPRSFLGS) != 0) {
275 /* Some kind of compression pointer. */
276 errno = EMSGSIZE;
277 return (-1);
279 *dn++ = n;
280 if (dn + n >= eom) {
281 errno = EMSGSIZE;
282 return (-1);
284 for ((void)NULL; n > 0; n--) {
285 c = *cp++;
286 if (isupper(c))
287 *dn++ = tolower(c);
288 else
289 *dn++ = c;
292 *dn++ = '\0';
293 return (dn - dst);
295 #endif
298 * MRns_name_unpack(msg, eom, src, dst, dstsiz)
299 * Unpack a domain name from a message, source may be compressed.
300 * return:
301 * -1 if it fails, or consumed octets if it succeeds.
304 MRns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
305 u_char *dst, size_t dstsiz)
307 const u_char *srcp, *dstlim;
308 u_char *dstp;
309 unsigned n;
310 int len;
311 int checked;
313 len = -1;
314 checked = 0;
315 dstp = dst;
316 srcp = src;
317 dstlim = dst + dstsiz;
318 if (srcp < msg || srcp >= eom) {
319 errno = EMSGSIZE;
320 return (-1);
322 /* Fetch next label in domain name. */
323 while ((n = *srcp++) != 0) {
324 /* Check for indirection. */
325 switch (n & NS_CMPRSFLGS) {
326 case 0:
327 /* Limit checks. */
328 if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
329 errno = EMSGSIZE;
330 return (-1);
332 checked += n + 1;
333 *dstp++ = n;
334 memcpy(dstp, srcp, n);
335 dstp += n;
336 srcp += n;
337 break;
339 case NS_CMPRSFLGS:
340 if (srcp >= eom) {
341 errno = EMSGSIZE;
342 return (-1);
344 if (len < 0)
345 len = srcp - src + 1;
346 n = ((n & 0x3f) << 8) | (*srcp & 0xff);
347 if (n >= eom - msg) { /* Out of range. */
348 errno = EMSGSIZE;
349 return (-1);
351 srcp = msg + n;
352 checked += 2;
354 * Check for loops in the compressed name;
355 * if we've looked at the whole message,
356 * there must be a loop.
358 if (checked >= eom - msg) {
359 errno = EMSGSIZE;
360 return (-1);
362 break;
364 default:
365 errno = EMSGSIZE;
366 return (-1); /* flag error */
369 *dstp = '\0';
370 if (len < 0)
371 len = srcp - src;
372 return (len);
376 * MRns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
377 * Pack domain name 'domain' into 'comp_dn'.
378 * return:
379 * Size of the compressed name, or -1.
380 * notes:
381 * 'dnptrs' is an array of pointers to previous compressed names.
382 * dnptrs[0] is a pointer to the beginning of the message. The array
383 * ends with NULL.
384 * 'lastdnptr' is a pointer to the end of the array pointed to
385 * by 'dnptrs'.
386 * Side effects:
387 * The list of pointers in dnptrs is updated for labels inserted into
388 * the message as we compress the name. If 'dnptr' is NULL, we don't
389 * try to compress names. If 'lastdnptr' is NULL, we don't update the
390 * list.
393 MRns_name_pack(const u_char *src, u_char *dst, unsigned dstsiz,
394 const u_char **dnptrs, const u_char **lastdnptr)
396 u_char *dstp;
397 const u_char **cpp, **lpp, *eob, *msg;
398 const u_char *srcp;
399 unsigned n;
400 int l;
402 srcp = src;
403 dstp = dst;
404 eob = dstp + dstsiz;
405 lpp = cpp = NULL;
406 if (dnptrs != NULL) {
407 if ((msg = *dnptrs++) != NULL) {
408 for (cpp = dnptrs; *cpp != NULL; cpp++)
409 (void)NULL;
410 lpp = cpp; /* end of list to search */
412 } else
413 msg = NULL;
415 /* make sure the domain we are about to add is legal */
416 l = 0;
417 do {
418 n = *srcp;
419 if ((n & NS_CMPRSFLGS) != 0) {
420 errno = EMSGSIZE;
421 return (-1);
423 l += n + 1;
424 if (l > MAXCDNAME) {
425 errno = EMSGSIZE;
426 return (-1);
428 srcp += n + 1;
429 } while (n != 0);
431 /* from here on we need to reset compression pointer array on error */
432 srcp = src;
433 do {
434 /* Look to see if we can use pointers. */
435 n = *srcp;
436 if (n != 0 && msg != NULL) {
437 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
438 (const u_char * const *)lpp);
439 if (l >= 0) {
440 if (dstp + 1 >= eob) {
441 goto cleanup;
443 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
444 *dstp++ = l % 256;
445 return (dstp - dst);
447 /* Not found, save it. */
448 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
449 (dstp - msg) < 0x4000) {
450 *cpp++ = dstp;
451 *cpp = NULL;
454 /* copy label to buffer */
455 if (n & NS_CMPRSFLGS) { /* Should not happen. */
456 goto cleanup;
458 if (dstp + 1 + n >= eob) {
459 goto cleanup;
461 memcpy(dstp, srcp, n + 1);
462 srcp += n + 1;
463 dstp += n + 1;
464 } while (n != 0);
466 if (dstp > eob) {
467 cleanup:
468 if (msg != NULL)
469 *lpp = NULL;
470 errno = EMSGSIZE;
471 return (-1);
473 return (dstp - dst);
476 #ifdef notdef
478 * MRns_name_uncompress(msg, eom, src, dst, dstsiz)
479 * Expand compressed domain name to presentation format.
480 * return:
481 * Number of bytes read out of `src', or -1 (with errno set).
482 * note:
483 * Root domain returns as "." not "".
486 MRns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
487 char *dst, size_t dstsiz)
489 u_char tmp[NS_MAXCDNAME];
490 int n;
492 if ((n = MRns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
493 return (-1);
494 if (MRns_name_ntop(tmp, dst, dstsiz) == -1)
495 return (-1);
496 return (n);
498 #endif
501 * MRns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
502 * Compress a domain name into wire format, using compression pointers.
503 * return:
504 * Number of bytes consumed in `dst' or -1 (with errno set).
505 * notes:
506 * 'dnptrs' is an array of pointers to previous compressed names.
507 * dnptrs[0] is a pointer to the beginning of the message.
508 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
509 * array pointed to by 'dnptrs'. Side effect is to update the list of
510 * pointers for labels inserted into the message as we compress the name.
511 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
512 * is NULL, we don't update the list.
515 MRns_name_compress(const char *src, u_char *dst, size_t dstsiz,
516 const u_char **dnptrs, const u_char **lastdnptr)
518 u_char tmp[NS_MAXCDNAME];
520 if (MRns_name_pton(src, tmp, sizeof tmp) == -1)
521 return (-1);
522 return (MRns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
525 #ifdef notdef
527 * MRns_name_skip(ptrptr, eom)
528 * Advance *ptrptr to skip over the compressed name it points at.
529 * return:
530 * 0 on success, -1 (with errno set) on failure.
533 MRns_name_skip(const u_char **ptrptr, const u_char *eom) {
534 const u_char *cp;
535 u_int n;
537 cp = *ptrptr;
538 while (cp < eom && (n = *cp++) != 0) {
539 /* Check for indirection. */
540 switch (n & NS_CMPRSFLGS) {
541 case 0: /* normal case, n == len */
542 cp += n;
543 continue;
544 case NS_CMPRSFLGS: /* indirection */
545 cp++;
546 break;
547 default: /* illegal type */
548 errno = EMSGSIZE;
549 return (-1);
551 break;
553 if (cp > eom) {
554 errno = EMSGSIZE;
555 return (-1);
557 *ptrptr = cp;
558 return (0);
560 #endif
562 /* Private. */
565 * special(ch)
566 * Thinking in noninternationalized USASCII (per the DNS spec),
567 * is this characted special ("in need of quoting") ?
568 * return:
569 * boolean.
571 static int
572 special(int ch) {
573 switch (ch) {
574 case 0x22: /* '"' */
575 case 0x2E: /* '.' */
576 case 0x3B: /* ';' */
577 case 0x5C: /* '\\' */
578 /* Special modifiers in zone files. */
579 case 0x40: /* '@' */
580 case 0x24: /* '$' */
581 return (1);
582 default:
583 return (0);
588 * printable(ch)
589 * Thinking in noninternationalized USASCII (per the DNS spec),
590 * is this character visible and not a space when printed ?
591 * return:
592 * boolean.
594 static int
595 printable(int ch) {
596 return (ch > 0x20 && ch < 0x7f);
600 * Thinking in noninternationalized USASCII (per the DNS spec),
601 * convert this character to lower case if it's upper case.
603 static int
604 mklower(int ch) {
605 if (ch >= 0x41 && ch <= 0x5A)
606 return (ch + 0x20);
607 return (ch);
611 * dn_find(domain, msg, dnptrs, lastdnptr)
612 * Search for the counted-label name in an array of compressed names.
613 * return:
614 * offset from msg if found, or -1.
615 * notes:
616 * dnptrs is the pointer to the first name on the list,
617 * not the pointer to the start of the message.
619 static int
620 dn_find(const u_char *domain, const u_char *msg,
621 const u_char * const *dnptrs,
622 const u_char * const *lastdnptr)
624 const u_char *dn, *cp, *sp;
625 const u_char * const *cpp;
626 u_int n;
628 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
629 dn = domain;
630 sp = cp = *cpp;
631 while ((n = *cp++) != 0) {
633 * check for indirection
635 switch (n & NS_CMPRSFLGS) {
636 case 0: /* normal case, n == len */
637 if (n != *dn++)
638 goto next;
639 for ((void)NULL; n > 0; n--)
640 if (mklower(*dn++) != mklower(*cp++))
641 goto next;
642 /* Is next root for both ? */
643 if (*dn == '\0' && *cp == '\0')
644 return (sp - msg);
645 if (*dn)
646 continue;
647 goto next;
649 case NS_CMPRSFLGS: /* indirection */
650 cp = msg + (((n & 0x3f) << 8) | *cp);
651 break;
653 default: /* illegal type */
654 errno = EMSGSIZE;
655 return (-1);
658 next: ;
660 errno = ENOENT;
661 return (-1);