Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / reslib.c
blobaed07245abb4946012528aa413ae87ad5c5f860f
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
55 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
57 * Permission to use, copy, modify, and distribute this software for any
58 * purpose with or without fee is hereby granted, provided that the above
59 * copyright notice and this permission notice appear in all copies.
61 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
62 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
63 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
64 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
65 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
66 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
67 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
68 * SOFTWARE.
71 /* Original copyright ISC as above.
72 * Code modified specifically for ircd use from the following orginal files
73 * in bind ...
75 * res_comp.c
76 * ns_name.c
77 * ns_netint.c
78 * res_init.c
80 * - Dianora
83 #include "stdinc.h"
84 #include "ircd_defs.h"
85 #include "common.h"
86 #include "ircd.h"
87 #include "commio.h"
88 #include "res.h"
89 #include "reslib.h"
90 #include "tools.h"
91 #include "event.h"
92 #include "irc_string.h"
93 #include "sprintf_irc.h"
95 #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */
96 #define DNS_LABELTYPE_BITSTRING 0x41
97 #define MAXLINE 128
99 struct irc_sockaddr_storage irc_nsaddr_list[IRCD_MAXNS];
100 int irc_nscount = 0;
101 char irc_domain[HOSTLEN + 1];
103 static const char digitvalue[256] = {
104 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
105 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
106 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
107 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
108 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
109 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
110 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
111 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
112 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
113 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
114 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
115 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
116 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
117 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
118 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
119 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
122 static int parse_resvconf(void);
123 static void add_nameserver(const char *);
125 static const char digits[] = "0123456789";
126 static int labellen(const unsigned char *lp);
127 static int special(int ch);
128 static int printable(int ch);
129 static int irc_decode_bitstring(const char **cpp, char *dn, const char *eom);
130 static int irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz,
131 const unsigned char **dnptrs, const unsigned char **lastdnptr);
132 static int irc_dn_find(const unsigned char *, const unsigned char *, const unsigned char * const *,
133 const unsigned char * const *);
134 static int irc_encode_bitsring(const char **, const char *, unsigned char **, unsigned char **,
135 const char *);
136 static int irc_ns_name_uncompress(const unsigned char *, const unsigned char *,
137 const unsigned char *, char *, size_t);
138 static int irc_ns_name_unpack(const unsigned char *, const unsigned char *,
139 const unsigned char *, unsigned char *,
140 size_t);
141 static int irc_ns_name_ntop(const char *, char *, size_t);
142 static int irc_ns_name_skip(const unsigned char **, const unsigned char *);
143 static int mklower(int ch);
146 irc_res_init(void)
148 irc_nscount = 0;
149 return parse_resvconf();
152 /* parse_resvconf()
154 * inputs - NONE
155 * output - -1 if failure 0 if success
156 * side effects - fills in irc_nsaddr_list
158 static int
159 parse_resvconf(void)
161 char *p;
162 char *opt;
163 char *arg;
164 char input[MAXLINE];
165 FILE *file;
167 /* XXX "/etc/resolv.conf" should be from a define in setup.h perhaps
168 * for cygwin support etc. this hardcodes it to unix for now -db
170 if ((file = fopen("/etc/resolv.conf", "r")) == NULL)
171 return -1;
173 while (fgets(input, sizeof(input), file) != NULL)
175 /* blow away any newline */
176 if ((p = strpbrk(input, "\r\n")) != NULL)
177 *p = '\0';
179 p = input;
180 /* skip until something thats not a space is seen */
181 while (IsSpace(*p))
182 p++;
183 /* if at this point, have a '\0' then continue */
184 if (*p == '\0')
185 continue;
187 /* Ignore comment lines immediately */
188 if (*p == '#' || *p == ';')
189 continue;
191 /* skip until a space is found */
192 opt = p;
193 while (!IsSpace(*p) && *p != '\0')
194 p++;
195 if (*p == '\0')
196 continue; /* no arguments?.. ignore this line */
197 /* blow away the space character */
198 *p++ = '\0';
200 /* skip these spaces that are before the argument */
201 while (IsSpace(*p))
202 p++;
203 /* Now arg should be right where p is pointing */
204 arg = p;
205 if ((p = strpbrk(arg, " \t")) != NULL)
206 *p = '\0'; /* take the first word */
208 if (irccmp(opt, "domain") == 0)
209 strlcpy(irc_domain, arg, sizeof(irc_domain));
210 else if (irccmp(opt, "nameserver") == 0)
211 add_nameserver(arg);
214 fclose(file);
215 return 0;
218 /* add_nameserver()
220 * input - either an IPV4 address in dotted quad
221 * or an IPV6 address in : format
222 * output - NONE
223 * side effects - entry in irc_nsaddr_list is filled in as needed
225 static void
226 add_nameserver(const char *arg)
228 struct addrinfo hints, *res;
230 /* Done max number of nameservers? */
231 if (irc_nscount >= IRCD_MAXNS)
233 ilog (L_MAIN, "Too many nameservers, ignoring %s", arg);
234 return;
237 memset(&hints, 0, sizeof(hints));
238 hints.ai_family = PF_UNSPEC;
239 hints.ai_socktype = SOCK_DGRAM;
240 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
242 if (getaddrinfo(arg, "domain", &hints, &res))
243 return;
245 if (res == NULL)
246 return;
248 memcpy(&irc_nsaddr_list[irc_nscount], res->ai_addr, res->ai_addrlen);
249 SET_SS_LEN(irc_nsaddr_list[irc_nscount], res->ai_addrlen);
250 irc_nscount++;
251 freeaddrinfo(res);
255 * Expand compressed domain name 'comp_dn' to full domain name.
256 * 'msg' is a pointer to the begining of the message,
257 * 'eomorig' points to the first location after the message,
258 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
259 * Return size of compressed name or -1 if there was an error.
262 irc_dn_expand(const unsigned char *msg, const unsigned char *eom,
263 const unsigned char *src, char *dst, int dstsiz)
265 int n = irc_ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
267 if (n > 0 && dst[0] == '.')
268 dst[0] = '\0';
269 return(n);
273 * irc_ns_name_uncompress(msg, eom, src, dst, dstsiz)
274 * Expand compressed domain name to presentation format.
275 * return:
276 * Number of bytes read out of `src', or -1 (with errno set).
277 * note:
278 * Root domain returns as "." not "".
280 static int
281 irc_ns_name_uncompress(const unsigned char *msg, const unsigned char *eom,
282 const unsigned char *src, char *dst, size_t dstsiz)
284 unsigned char tmp[NS_MAXCDNAME];
285 int n;
287 if ((n = irc_ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
288 return(-1);
289 if (irc_ns_name_ntop((char*)tmp, dst, dstsiz) == -1)
290 return(-1);
291 return(n);
294 * irc_ns_name_unpack(msg, eom, src, dst, dstsiz)
295 * Unpack a domain name from a message, source may be compressed.
296 * return:
297 * -1 if it fails, or consumed octets if it succeeds.
299 static int
300 irc_ns_name_unpack(const unsigned char *msg, const unsigned char *eom,
301 const unsigned char *src, unsigned char *dst,
302 size_t dstsiz)
304 const unsigned char *srcp, *dstlim;
305 unsigned char *dstp;
306 int n, len, checked, l;
308 len = -1;
309 checked = 0;
310 dstp = dst;
311 srcp = src;
312 dstlim = dst + dstsiz;
313 if (srcp < msg || srcp >= eom) {
314 errno = EMSGSIZE;
315 return (-1);
317 /* Fetch next label in domain name. */
318 while ((n = *srcp++) != 0) {
319 /* Check for indirection. */
320 switch (n & NS_CMPRSFLGS) {
321 case 0:
322 case NS_TYPE_ELT:
323 /* Limit checks. */
324 if ((l = labellen(srcp - 1)) < 0) {
325 errno = EMSGSIZE;
326 return(-1);
328 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
329 errno = EMSGSIZE;
330 return (-1);
332 checked += l + 1;
333 *dstp++ = n;
334 memcpy(dstp, srcp, l);
335 dstp += l;
336 srcp += l;
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 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
347 if (srcp < msg || srcp >= eom) { /* Out of range. */
348 errno = EMSGSIZE;
349 return (-1);
351 checked += 2;
353 * Check for loops in the compressed name;
354 * if we've looked at the whole message,
355 * there must be a loop.
357 if (checked >= eom - msg) {
358 errno = EMSGSIZE;
359 return (-1);
361 break;
363 default:
364 errno = EMSGSIZE;
365 return (-1); /* flag error */
368 *dstp = '\0';
369 if (len < 0)
370 len = srcp - src;
371 return (len);
375 * irc_ns_name_ntop(src, dst, dstsiz)
376 * Convert an encoded domain name to printable ascii as per RFC1035.
377 * return:
378 * Number of bytes written to buffer, or -1 (with errno set)
379 * notes:
380 * The root is returned as "."
381 * All other domains are returned in non absolute form
383 static int
384 irc_ns_name_ntop(const char *src, char *dst, size_t dstsiz)
386 const char *cp;
387 char *dn, *eom;
388 unsigned char c;
389 unsigned int n;
390 int l;
392 cp = src;
393 dn = dst;
394 eom = dst + dstsiz;
396 while ((n = *cp++) != 0) {
397 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
398 /* Some kind of compression pointer. */
399 errno = EMSGSIZE;
400 return (-1);
402 if (dn != dst) {
403 if (dn >= eom) {
404 errno = EMSGSIZE;
405 return (-1);
407 *dn++ = '.';
409 if ((l = labellen((unsigned char*)(cp - 1))) < 0) {
410 errno = EMSGSIZE; /* XXX */
411 return(-1);
413 if (dn + l >= eom) {
414 errno = EMSGSIZE;
415 return (-1);
417 if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
418 int m;
420 if (n != DNS_LABELTYPE_BITSTRING) {
421 /* XXX: labellen should reject this case */
422 errno = EINVAL;
423 return(-1);
425 if ((m = irc_decode_bitstring(&cp, dn, eom)) < 0)
427 errno = EMSGSIZE;
428 return(-1);
430 dn += m;
431 continue;
433 for ((void)NULL; l > 0; l--) {
434 c = *cp++;
435 if (special(c)) {
436 if (dn + 1 >= eom) {
437 errno = EMSGSIZE;
438 return (-1);
440 *dn++ = '\\';
441 *dn++ = (char)c;
442 } else if (!printable(c)) {
443 if (dn + 3 >= eom) {
444 errno = EMSGSIZE;
445 return (-1);
447 *dn++ = '\\';
448 *dn++ = digits[c / 100];
449 *dn++ = digits[(c % 100) / 10];
450 *dn++ = digits[c % 10];
451 } else {
452 if (dn >= eom) {
453 errno = EMSGSIZE;
454 return (-1);
456 *dn++ = (char)c;
460 if (dn == dst) {
461 if (dn >= eom) {
462 errno = EMSGSIZE;
463 return (-1);
465 *dn++ = '.';
467 if (dn >= eom) {
468 errno = EMSGSIZE;
469 return (-1);
471 *dn++ = '\0';
472 return (dn - dst);
476 * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
477 * Return the size of the compressed name or -1.
478 * 'length' is the size of the array pointed to by 'comp_dn'.
480 static int
481 irc_dn_comp(const char *src, unsigned char *dst, int dstsiz,
482 unsigned char **dnptrs, unsigned char **lastdnptr)
484 return(irc_ns_name_compress(src, dst, (size_t)dstsiz,
485 (const unsigned char **)dnptrs,
486 (const unsigned char **)lastdnptr));
490 * Skip over a compressed domain name. Return the size or -1.
493 irc_dn_skipname(const unsigned char *ptr, const unsigned char *eom) {
494 const unsigned char *saveptr = ptr;
496 if (irc_ns_name_skip(&ptr, eom) == -1)
497 return(-1);
498 return(ptr - saveptr);
502 * ns_name_skip(ptrptr, eom)
503 * Advance *ptrptr to skip over the compressed name it points at.
504 * return:
505 * 0 on success, -1 (with errno set) on failure.
507 static int
508 irc_ns_name_skip(const unsigned char **ptrptr, const unsigned char *eom)
510 const unsigned char *cp;
511 unsigned int n;
512 int l;
514 cp = *ptrptr;
516 while (cp < eom && (n = *cp++) != 0)
518 /* Check for indirection. */
519 switch (n & NS_CMPRSFLGS)
521 case 0: /* normal case, n == len */
522 cp += n;
523 continue;
524 case NS_TYPE_ELT: /* EDNS0 extended label */
525 if ((l = labellen(cp - 1)) < 0)
527 errno = EMSGSIZE; /* XXX */
528 return(-1);
531 cp += l;
532 continue;
533 case NS_CMPRSFLGS: /* indirection */
534 cp++;
535 break;
536 default: /* illegal type */
537 errno = EMSGSIZE;
538 return(-1);
541 break;
544 if (cp > eom)
546 errno = EMSGSIZE;
547 return (-1);
550 *ptrptr = cp;
551 return(0);
554 unsigned int
555 irc_ns_get16(const unsigned char *src)
557 unsigned int dst;
559 IRC_NS_GET16(dst, src);
560 return(dst);
563 unsigned long
564 irc_ns_get32(const unsigned char *src)
566 unsigned long dst;
568 IRC_NS_GET32(dst, src);
569 return(dst);
572 void
573 irc_ns_put16(unsigned int src, unsigned char *dst)
575 IRC_NS_PUT16(src, dst);
578 void
579 irc_ns_put32(unsigned long src, unsigned char *dst)
581 IRC_NS_PUT32(src, dst);
584 /* From ns_name.c */
587 * special(ch)
588 * Thinking in noninternationalized USASCII (per the DNS spec),
589 * is this characted special ("in need of quoting") ?
590 * return:
591 * boolean.
593 static int
594 special(int ch)
596 switch (ch)
598 case 0x22: /* '"' */
599 case 0x2E: /* '.' */
600 case 0x3B: /* ';' */
601 case 0x5C: /* '\\' */
602 case 0x28: /* '(' */
603 case 0x29: /* ')' */
604 /* Special modifiers in zone files. */
605 case 0x40: /* '@' */
606 case 0x24: /* '$' */
607 return(1);
608 default:
609 return(0);
613 static int
614 labellen(const unsigned char *lp)
616 int bitlen;
617 unsigned char l = *lp;
619 if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS)
621 /* should be avoided by the caller */
622 return(-1);
625 if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT)
627 if (l == DNS_LABELTYPE_BITSTRING)
629 if ((bitlen = *(lp + 1)) == 0)
630 bitlen = 256;
631 return((bitlen + 7 ) / 8 + 1);
634 return(-1); /* unknwon ELT */
637 return(l);
642 * printable(ch)
643 * Thinking in noninternationalized USASCII (per the DNS spec),
644 * is this character visible and not a space when printed ?
645 * return:
646 * boolean.
648 static int
649 printable(int ch)
651 return(ch > 0x20 && ch < 0x7f);
654 static int
655 irc_decode_bitstring(const char **cpp, char *dn, const char *eom)
657 const char *cp = *cpp;
658 char *beg = dn, tc;
659 int b, blen, plen;
661 if ((blen = (*cp & 0xff)) == 0)
662 blen = 256;
663 plen = (blen + 3) / 4;
664 plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
665 if (dn + plen >= eom)
666 return(-1);
668 cp++;
669 dn += sprintf(dn, "\\[x");
670 for (b = blen; b > 7; b -= 8, cp++)
671 dn += sprintf(dn, "%02x", *cp & 0xff);
672 if (b > 4) {
673 tc = *cp++;
674 dn += sprintf(dn, "%02x", tc & (0xff << (8 - b)));
675 } else if (b > 0) {
676 tc = *cp++;
677 dn += sprintf(dn, "%1x",
678 ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
680 dn += sprintf(dn, "/%d]", blen);
682 *cpp = cp;
683 return(dn - beg);
687 * irc_ns_name_pton(src, dst, dstsiz)
688 * Convert a ascii string into an encoded domain name as per RFC1035.
689 * return:
690 * -1 if it fails
691 * 1 if string was fully qualified
692 * 0 is string was not fully qualified
693 * notes:
694 * Enforces label and domain length limits.
696 static int
697 irc_ns_name_pton(const char *src, unsigned char *dst, size_t dstsiz)
699 unsigned char *label, *bp, *eom;
700 char *cp;
701 int c, n, escaped, e = 0;
703 escaped = 0;
704 bp = dst;
705 eom = dst + dstsiz;
706 label = bp++;
709 while ((c = *src++) != 0) {
710 if (escaped) {
711 if (c == '[') { /* start a bit string label */
712 if ((cp = strchr(src, ']')) == NULL) {
713 errno = EINVAL; /* ??? */
714 return(-1);
716 if ((e = irc_encode_bitsring(&src,
717 cp + 2,
718 &label,
719 &bp,
720 (const char *)eom))
721 != 0) {
722 errno = e;
723 return(-1);
725 escaped = 0;
726 label = bp++;
727 if ((c = *src++) == 0)
728 goto done;
729 else if (c != '.') {
730 errno = EINVAL;
731 return(-1);
733 continue;
735 else if ((cp = strchr(digits, c)) != NULL) {
736 n = (cp - digits) * 100;
737 if ((c = *src++) == 0 ||
738 (cp = strchr(digits, c)) == NULL) {
739 errno = EMSGSIZE;
740 return (-1);
742 n += (cp - digits) * 10;
743 if ((c = *src++) == 0 ||
744 (cp = strchr(digits, c)) == NULL) {
745 errno = EMSGSIZE;
746 return (-1);
748 n += (cp - digits);
749 if (n > 255) {
750 errno = EMSGSIZE;
751 return (-1);
753 c = n;
755 escaped = 0;
756 } else if (c == '\\') {
757 escaped = 1;
758 continue;
759 } else if (c == '.') {
760 c = (bp - label - 1);
761 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
762 errno = EMSGSIZE;
763 return (-1);
765 if (label >= eom) {
766 errno = EMSGSIZE;
767 return (-1);
769 *label = c;
770 /* Fully qualified ? */
771 if (*src == '\0') {
772 if (c != 0) {
773 if (bp >= eom) {
774 errno = EMSGSIZE;
775 return (-1);
777 *bp++ = '\0';
779 if ((bp - dst) > NS_MAXCDNAME) {
780 errno = EMSGSIZE;
781 return (-1);
783 return (1);
785 if (c == 0 || *src == '.') {
786 errno = EMSGSIZE;
787 return (-1);
789 label = bp++;
790 continue;
792 if (bp >= eom) {
793 errno = EMSGSIZE;
794 return (-1);
796 *bp++ = (unsigned char)c;
798 c = (bp - label - 1);
799 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
800 errno = EMSGSIZE;
801 return (-1);
803 done:
804 if (label >= eom) {
805 errno = EMSGSIZE;
806 return (-1);
808 *label = c;
809 if (c != 0) {
810 if (bp >= eom) {
811 errno = EMSGSIZE;
812 return (-1);
814 *bp++ = 0;
817 if ((bp - dst) > NS_MAXCDNAME)
818 { /* src too big */
819 errno = EMSGSIZE;
820 return (-1);
823 return (0);
827 * irc_ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
828 * Pack domain name 'domain' into 'comp_dn'.
829 * return:
830 * Size of the compressed name, or -1.
831 * notes:
832 * 'dnptrs' is an array of pointers to previous compressed names.
833 * dnptrs[0] is a pointer to the beginning of the message. The array
834 * ends with NULL.
835 * 'lastdnptr' is a pointer to the end of the array pointed to
836 * by 'dnptrs'.
837 * Side effects:
838 * The list of pointers in dnptrs is updated for labels inserted into
839 * the message as we compress the name. If 'dnptr' is NULL, we don't
840 * try to compress names. If 'lastdnptr' is NULL, we don't update the
841 * list.
843 static int
844 irc_ns_name_pack(const unsigned char *src, unsigned char *dst, int dstsiz,
845 const unsigned char **dnptrs, const unsigned char **lastdnptr)
847 unsigned char *dstp;
848 const unsigned char **cpp, **lpp, *eob, *msg;
849 const unsigned char *srcp;
850 int n, l, first = 1;
852 srcp = src;
853 dstp = dst;
854 eob = dstp + dstsiz;
855 lpp = cpp = NULL;
856 if (dnptrs != NULL) {
857 if ((msg = *dnptrs++) != NULL) {
858 for (cpp = dnptrs; *cpp != NULL; cpp++)
859 (void)NULL;
860 lpp = cpp; /* end of list to search */
862 } else
863 msg = NULL;
865 /* make sure the domain we are about to add is legal */
866 l = 0;
867 do {
868 int l0;
870 n = *srcp;
871 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
872 errno = EMSGSIZE;
873 return (-1);
875 if ((l0 = labellen(srcp)) < 0) {
876 errno = EINVAL;
877 return(-1);
879 l += l0 + 1;
880 if (l > NS_MAXCDNAME) {
881 errno = EMSGSIZE;
882 return (-1);
884 srcp += l0 + 1;
885 } while (n != 0);
887 /* from here on we need to reset compression pointer array on error */
888 srcp = src;
889 do {
890 /* Look to see if we can use pointers. */
891 n = *srcp;
892 if (n != 0 && msg != NULL) {
893 l = irc_dn_find(srcp, msg, (const unsigned char * const *)dnptrs,
894 (const unsigned char * const *)lpp);
895 if (l >= 0) {
896 if (dstp + 1 >= eob) {
897 goto cleanup;
899 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
900 *dstp++ = l % 256;
901 return (dstp - dst);
903 /* Not found, save it. */
904 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
905 (dstp - msg) < 0x4000 && first) {
906 *cpp++ = dstp;
907 *cpp = NULL;
908 first = 0;
911 /* copy label to buffer */
912 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
913 /* Should not happen. */
914 goto cleanup;
916 n = labellen(srcp);
917 if (dstp + 1 + n >= eob) {
918 goto cleanup;
920 memcpy(dstp, srcp, n + 1);
921 srcp += n + 1;
922 dstp += n + 1;
923 } while (n != 0);
925 if (dstp > eob) {
926 cleanup:
927 if (msg != NULL)
928 *lpp = NULL;
929 errno = EMSGSIZE;
930 return (-1);
932 return(dstp - dst);
935 static int
936 irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz,
937 const unsigned char **dnptrs, const unsigned char **lastdnptr)
939 unsigned char tmp[NS_MAXCDNAME];
941 if (irc_ns_name_pton(src, tmp, sizeof tmp) == -1)
942 return(-1);
943 return(irc_ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
946 static int
947 irc_encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
948 unsigned char **dst, const char *eom)
950 int afterslash = 0;
951 const char *cp = *bp;
952 char *tp, c;
953 const char *beg_blen;
954 char *end_blen = NULL;
955 int value = 0, count = 0, tbcount = 0, blen = 0;
957 beg_blen = end_blen = NULL;
959 /* a bitstring must contain at least 2 characters */
960 if (end - cp < 2)
961 return(EINVAL);
963 /* XXX: currently, only hex strings are supported */
964 if (*cp++ != 'x')
965 return(EINVAL);
966 if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */
967 return(EINVAL);
969 for (tp = (char*)(dst + 1); cp < end && tp < eom; cp++) {
970 switch((c = *cp)) {
971 case ']': /* end of the bitstring */
972 if (afterslash) {
973 if (beg_blen == NULL)
974 return(EINVAL);
975 blen = (int)strtol(beg_blen, &end_blen, 10);
976 if (*end_blen != ']')
977 return(EINVAL);
979 if (count)
980 *tp++ = ((value << 4) & 0xff);
981 cp++; /* skip ']' */
982 goto done;
983 case '/':
984 afterslash = 1;
985 break;
986 default:
987 if (afterslash) {
988 if (!isdigit(c&0xff))
989 return(EINVAL);
990 if (beg_blen == NULL) {
992 if (c == '0') {
993 /* blen never begings with 0 */
994 return(EINVAL);
996 beg_blen = cp;
998 } else {
999 if (!isxdigit(c&0xff))
1000 return(EINVAL);
1001 value <<= 4;
1002 value += digitvalue[(int)c];
1003 count += 4;
1004 tbcount += 4;
1005 if (tbcount > 256)
1006 return(EINVAL);
1007 if (count == 8) {
1008 *tp++ = value;
1009 count = 0;
1012 break;
1015 done:
1016 if (cp >= end || tp >= eom)
1017 return(EMSGSIZE);
1020 * bit length validation:
1021 * If a <length> is present, the number of digits in the <bit-data>
1022 * MUST be just sufficient to contain the number of bits specified
1023 * by the <length>. If there are insignificant bits in a final
1024 * hexadecimal or octal digit, they MUST be zero.
1025 * RFC 2673, Section 3.2.
1027 if (blen > 0) {
1028 int traillen;
1030 if (((blen + 3) & ~3) != tbcount)
1031 return(EINVAL);
1032 traillen = tbcount - blen; /* between 0 and 3 */
1033 if (((value << (8 - traillen)) & 0xff) != 0)
1034 return(EINVAL);
1036 else
1037 blen = tbcount;
1038 if (blen == 256)
1039 blen = 0;
1041 /* encode the type and the significant bit fields */
1042 **labelp = DNS_LABELTYPE_BITSTRING;
1043 **dst = blen;
1045 *bp = cp;
1046 *dst = (unsigned char*)tp;
1048 return(0);
1052 * dn_find(domain, msg, dnptrs, lastdnptr)
1053 * Search for the counted-label name in an array of compressed names.
1054 * return:
1055 * offset from msg if found, or -1.
1056 * notes:
1057 * dnptrs is the pointer to the first name on the list,
1058 * not the pointer to the start of the message.
1060 static int
1061 irc_dn_find(const unsigned char *domain, const unsigned char *msg,
1062 const unsigned char * const *dnptrs,
1063 const unsigned char * const *lastdnptr)
1065 const unsigned char *dn, *cp, *sp;
1066 const unsigned char * const *cpp;
1067 unsigned int n;
1069 for (cpp = dnptrs; cpp < lastdnptr; cpp++)
1071 sp = *cpp;
1073 * terminate search on:
1074 * root label
1075 * compression pointer
1076 * unusable offset
1078 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
1079 (sp - msg) < 0x4000) {
1080 dn = domain;
1081 cp = sp;
1082 while ((n = *cp++) != 0) {
1084 * check for indirection
1086 switch (n & NS_CMPRSFLGS) {
1087 case 0: /* normal case, n == len */
1088 n = labellen(cp - 1); /* XXX */
1090 if (n != *dn++)
1091 goto next;
1093 for ((void)NULL; n > 0; n--)
1094 if (mklower(*dn++) !=
1095 mklower(*cp++))
1096 goto next;
1097 /* Is next root for both ? */
1098 if (*dn == '\0' && *cp == '\0')
1099 return (sp - msg);
1100 if (*dn)
1101 continue;
1102 goto next;
1103 case NS_CMPRSFLGS: /* indirection */
1104 cp = msg + (((n & 0x3f) << 8) | *cp);
1105 break;
1107 default: /* illegal type */
1108 errno = EMSGSIZE;
1109 return (-1);
1112 next: ;
1113 sp += *sp + 1;
1116 errno = ENOENT;
1117 return (-1);
1121 * * Thinking in noninternationalized USASCII (per the DNS spec),
1122 * * convert this character to lower case if it's upper case.
1123 * */
1124 static int
1125 mklower(int ch)
1127 if (ch >= 0x41 && ch <= 0x5A)
1128 return(ch + 0x20);
1130 return(ch);
1133 /* From resolv/mkquery.c */
1136 * Form all types of queries.
1137 * Returns the size of the result or -1.
1140 irc_res_mkquery(
1141 const char *dname, /* domain name */
1142 int class, int type, /* class and type of query */
1143 unsigned char *buf, /* buffer to put query */
1144 int buflen) /* size of buffer */
1146 HEADER *hp;
1147 unsigned char *cp;
1148 int n;
1149 unsigned char *dnptrs[20], **dpp, **lastdnptr;
1152 * Initialize header fields.
1154 if ((buf == NULL) || (buflen < HFIXEDSZ))
1155 return (-1);
1156 memset(buf, 0, HFIXEDSZ);
1157 hp = (HEADER *) buf;
1159 hp->id = 0;
1160 hp->opcode = QUERY;
1161 hp->rd = 1; /* recurse */
1162 hp->rcode = NO_ERRORS;
1163 cp = buf + HFIXEDSZ;
1164 buflen -= HFIXEDSZ;
1165 dpp = dnptrs;
1166 *dpp++ = buf;
1167 *dpp++ = NULL;
1168 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1170 if ((buflen -= QFIXEDSZ) < 0)
1171 return (-1);
1172 if ((n = irc_dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
1173 return (-1);
1175 cp += n;
1176 buflen -= n;
1177 IRC_NS_PUT16(type, cp);
1178 IRC_NS_PUT16(class, cp);
1179 hp->qdcount = htons(1);
1181 return (cp - buf);