2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
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
71 /* Original copyright ISC as above.
72 * Code modified specifically for ircd use from the following original files
85 #include "ircd_reslib.h"
86 #include "ircd_defs.h"
88 #include "ircd_string.h"
96 #define NS_TYPE_ELT 0x40 /**< EDNS0 extended label type */
97 #define DNS_LABELTYPE_BITSTRING 0x41 /**< Bitstring label */
98 #define MAXLINE 128 /**< Maximum line length for resolv.conf */
101 * @brief DNS resolver library functions.
105 /** Array of nameserver addresses. */
106 struct irc_sockaddr irc_nsaddr_list[IRCD_MAXNS];
107 /** Number of nameservers in #irc_nsaddr_list. */
109 /** Local domain to use as a search suffix. */
110 char irc_domain[HOSTLEN + 1];
112 /** Maps hex digits to their values, or -1 for other characters. */
113 static const char digitvalue[256] = {
114 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
115 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
116 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
117 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
118 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
119 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
120 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
121 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
122 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
123 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
124 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
125 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
126 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
127 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
128 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
129 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
132 static int parse_resvconf(void);
134 /** Array of decimal digits, indexed by value. */
135 static const char digits[] = "0123456789";
136 static int labellen(const unsigned char *lp);
137 static int special(int ch);
138 static int printable(int ch);
139 static int irc_decode_bitstring(const char **cpp, char *dn, const char *eom);
140 static int irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz,
141 const unsigned char **dnptrs, const unsigned char **lastdnptr);
142 static int irc_dn_find(const unsigned char *, const unsigned char *, const unsigned char * const *,
143 const unsigned char * const *);
144 static int irc_encode_bitsring(const char **, const char *, unsigned char **, unsigned char **,
146 static int mklower(int ch);
148 /** Initialize the resolver library.
149 * @return Zero on success, non-zero on failure.
154 return (irc_nscount == 0) ? parse_resvconf() : 0;
157 /** Read resolver configuration file for domain and nameserver lines.
158 * The "domain" line is used to overwrite #irc_domain.
159 * Addresses in "nameserver" lines are appended to #irc_nsaddr_list.
160 * @return Zero on success, non-zero on failure.
171 /* XXX "/etc/resolv.conf" should be from a define in setup.h perhaps
172 * for cygwin support etc. this hardcodes it to unix for now -db
174 if ((file = fbopen("/etc/resolv.conf", "r")) == NULL)
177 while (fbgets(input, MAXLINE, file) != NULL)
179 /* blow away any newline */
180 if ((p = strpbrk(input, "\r\n")) != NULL)
183 /* Ignore comment lines immediately */
188 /* skip until something that's not a space is seen */
191 /* if at this point, have a '\0' then continue */
195 /* skip until a space is found */
199 continue; /* no arguments?.. ignore this line */
200 /* blow away the space character */
203 /* skip these spaces that are before the argument */
206 /* Now arg should be right where p is pointing */
208 if ((p = strpbrk(arg, " \t")) != NULL)
209 *p = '\0'; /* take the first word */
211 if (strcasecmp(opt, "domain") == 0)
212 ircd_strncpy(irc_domain, arg, HOSTLEN);
213 else if (strcasecmp(opt, "nameserver") == 0)
221 /** Add a resolver to #irc_nsaddr_list.
222 * @param[in] arg Dotted quad or IPv6 text form of nameserver address.
225 add_nameserver(const char *arg)
227 struct irc_sockaddr res;
229 /* Done max number of nameservers? */
230 if (irc_nscount >= IRCD_MAXNS)
233 /* Failure converting from numeric string? */
234 if (!ircd_aton(&res.addr, arg))
237 memcpy(&irc_nsaddr_list[irc_nscount], &res, sizeof(irc_nsaddr_list[irc_nscount]));
242 * Expand compressed domain name to full domain name.
243 * Like irc_ns_name_uncompress(), but checks for a well-formed result.
244 * @param[in] msg Pointer to the beginning of the message.
245 * @param[in] eom First location after the message.
246 * @param[in] src Pointer to where to starting decoding.
247 * @param[out] dst Output buffer.
248 * @param[in] dstsiz Number of bytes that can be written to \a dst.
249 * @return Number of bytes written to \a dst.
252 irc_dn_expand(const unsigned char *msg, const unsigned char *eom,
253 const unsigned char *src, char *dst, int dstsiz)
255 int n = irc_ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
257 if (n > 0 && dst[0] == '.')
263 * Expand compressed domain name to full domain name.
264 * @param[in] msg Pointer to the beginning of the message.
265 * @param[in] eom First location after the message.
266 * @param[in] src Pointer to where to starting decoding.
267 * @param[out] dst Output buffer.
268 * @param[in] dstsiz Number of bytes that can be written to \a dst.
269 * @return Number of bytes written to \a dst.
272 irc_ns_name_uncompress(const unsigned char *msg, const unsigned char *eom,
273 const unsigned char *src, char *dst, size_t dstsiz)
275 unsigned char tmp[NS_MAXCDNAME];
278 if ((n = irc_ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
280 if (irc_ns_name_ntop((char*)tmp, (char*)dst, dstsiz) == -1)
286 * Unpack compressed domain name to uncompressed form.
287 * @param[in] msg Pointer to the beginning of the message.
288 * @param[in] eom First location after the message.
289 * @param[in] src Pointer to where to starting decoding.
290 * @param[out] dst Output buffer.
291 * @param[in] dstsiz Number of bytes that can be written to \a dst.
292 * @return Number of bytes written to \a dst.
295 irc_ns_name_unpack(const unsigned char *msg, const unsigned char *eom,
296 const unsigned char *src, unsigned char *dst,
299 const unsigned char *srcp, *dstlim;
301 int n, len, checked, l;
307 dstlim = dst + dstsiz;
308 if (srcp < msg || srcp >= eom) {
312 /* Fetch next label in domain name. */
313 while ((n = *srcp++) != 0) {
314 /* Check for indirection. */
315 switch (n & NS_CMPRSFLGS) {
319 if ((l = labellen(srcp - 1)) < 0) {
323 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
329 memcpy(dstp, srcp, l);
340 len = srcp - src + 1;
341 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
342 if (srcp < msg || srcp >= eom) { /* Out of range. */
348 * Check for loops in the compressed name;
349 * if we've looked at the whole message,
350 * there must be a loop.
352 if (checked >= eom - msg) {
360 return (-1); /* flag error */
370 * Convert RFC1035 length-prefixed tag sequence to printable ASCII.
371 * @param[in] src Input tag sequence (effectively NUL terminated).
372 * @param[out] dst Buffer for uncompressed output.
373 * @param[in] dstsiz Number of bytes that can be written to \a dst.
374 * @return Number of bytes written to \a dst.
377 irc_ns_name_ntop(const char *src, char *dst, size_t dstsiz)
389 while ((n = *cp++) != 0) {
390 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
391 /* Some kind of compression pointer. */
402 if ((l = labellen((const unsigned char*)(cp - 1))) < 0) {
403 errno = EMSGSIZE; /* XXX */
410 if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
413 if (n != DNS_LABELTYPE_BITSTRING) {
414 /* XXX: labellen should reject this case */
418 if ((m = irc_decode_bitstring(&cp, dn, eom)) < 0)
426 for ((void)NULL; l > 0; l--) {
435 } else if (!printable(c)) {
441 *dn++ = digits[c / 100];
442 *dn++ = digits[(c % 100) / 10];
443 *dn++ = digits[c % 10];
468 /** Pack domain name from presentation form into compressed format.
469 * @param[in] src Presentation form of name.
470 * @param[out] dst Output buffer.
471 * @param[in] dstsiz Number of bytes that can be written to \a dst.
472 * @param[in,out] dnptrs Array of previously seen labels.
473 * @param[in] lastdnptr End of \a dnptrs array.
474 * @return Number of bytes written to \a dst.
477 irc_dn_comp(const char *src, unsigned char *dst, int dstsiz,
478 unsigned char **dnptrs, unsigned char **lastdnptr)
480 return(irc_ns_name_compress(src, dst, (size_t)dstsiz,
481 (const unsigned char **)dnptrs,
482 (const unsigned char **)lastdnptr));
485 /** Skip over a compressed domain name.
486 * @param[in] ptr Start of compressed name.
487 * @param[in] eom End of message.
488 * @return Length of the compressed name, or -1 on error.
491 irc_dn_skipname(const unsigned char *ptr, const unsigned char *eom) {
492 const unsigned char *saveptr = ptr;
494 if (irc_ns_name_skip(&ptr, eom) == -1)
496 return(ptr - saveptr);
499 /** Advance \a ptrptr to skip over the compressed name it points at.
500 * @param[in,out] ptrptr Pointer to the compressed name.
501 * @param[in] eom End of message.
502 * @return Zero on success; non-zero (with errno set) on failure.
505 irc_ns_name_skip(const unsigned char **ptrptr, const unsigned char *eom)
507 const unsigned char *cp;
513 while (cp < eom && (n = *cp++) != 0)
515 /* Check for indirection. */
516 switch (n & NS_CMPRSFLGS)
518 case 0: /* normal case, n == len */
521 case NS_TYPE_ELT: /* EDNS0 extended label */
522 if ((l = labellen(cp - 1)) < 0)
524 errno = EMSGSIZE; /* XXX */
530 case NS_CMPRSFLGS: /* indirection */
533 default: /* illegal type */
551 /** Read a 16-bit network-endian value from \a src.
552 * @param[in] src Input data buffer.
553 * @return Value retrieved from buffer.
556 irc_ns_get16(const unsigned char *src)
560 IRC_NS_GET16(dst, src);
564 /** Read a 32-bit network-endian value from \a src.
565 * @param[in] src Input data buffer.
566 * @return Value retrieved from buffer.
569 irc_ns_get32(const unsigned char *src)
573 IRC_NS_GET32(dst, src);
577 /** Write a 16-bit network-endian value to \a dst.
578 * @param[in] src Value to write.
579 * @param[out] dst Output buffer.
582 irc_ns_put16(unsigned int src, unsigned char *dst)
584 IRC_NS_PUT16(src, dst);
587 /** Write a 32-bit network-endian value to \a dst.
588 * @param[in] src Value to write.
589 * @param[out] dst Output buffer.
592 irc_ns_put32(unsigned long src, unsigned char *dst)
594 IRC_NS_PUT32(src, dst);
599 /** Indicate whether a character needs quoting.
600 * (What RFC does this come from?)
601 * @param[in] ch Character to check for specialness.
602 * @return Non-zero if the character should be quoted.
612 case 0x5C: /* '\\' */
615 /* Special modifiers in zone files. */
624 /** Calculate the length of a particular DNS label.
625 * @param[in] lp Start of label.
626 * @return Length of label, or -1 on error.
629 labellen(const unsigned char *lp)
632 unsigned char l = *lp;
634 if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS)
636 /* should be avoided by the caller */
640 if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT)
642 if (l == DNS_LABELTYPE_BITSTRING)
644 if ((bitlen = *(lp + 1)) == 0)
646 return((bitlen + 7 ) / 8 + 1);
649 return(-1); /* unknown ELT */
656 /** Indicate whether a character is printable.
657 * @param[in] ch Character to check for printability.
658 * @return Non-zero if the character is printable; zero if it is not.
663 return(ch > 0x20 && ch < 0x7f);
666 /** Decode a bitstring label from DNS.
667 * @param[in,out] cpp Pointer to start of label.
668 * @param[in,out] dn Output buffer.
669 * @param[in] eom End of message.
672 irc_decode_bitstring(const char **cpp, char *dn, const char *eom)
674 const char *cp = *cpp;
678 if ((blen = (*cp & 0xff)) == 0)
680 plen = (blen + 3) / 4;
681 plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
682 if (dn + plen >= eom)
686 dn += sprintf(dn, "\\[x");
687 for (b = blen; b > 7; b -= 8, cp++)
688 dn += sprintf(dn, "%02x", *cp & 0xff);
691 dn += sprintf(dn, "%02x", tc & (0xff << (8 - b)));
694 dn += sprintf(dn, "%1x",
695 ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
697 dn += sprintf(dn, "/%d]", blen);
703 /** Convert an ASCII name string into an encoded domain name.
704 * This function enforces per-label and total name lengths.
705 * @param[in] src ASCII name string.
706 * @param[out] dst Destination buffer.
707 * @param[in] dstsiz Number of bytes that can be written to \a dst.
708 * @return -1 on failure, 0 if \a src was not fully qualified, 1 if \a
709 * src was fully qualified.
712 irc_ns_name_pton(const char *src, unsigned char *dst, size_t dstsiz)
714 unsigned char *label, *bp, *eom;
716 int c, n, escaped, e = 0;
724 while ((c = *src++) != 0) {
726 if (c == '[') { /* start a bit string label */
727 if ((cp = strchr(src, ']')) == NULL) {
728 errno = EINVAL; /* ??? */
731 if ((e = irc_encode_bitsring(&src,
742 if ((c = *src++) == 0)
750 else if ((cp = strchr(digits, c)) != NULL) {
751 n = (cp - digits) * 100;
752 if ((c = *src++) == 0 ||
753 (cp = strchr(digits, c)) == NULL) {
757 n += (cp - digits) * 10;
758 if ((c = *src++) == 0 ||
759 (cp = strchr(digits, c)) == NULL) {
771 } else if (c == '\\') {
774 } else if (c == '.') {
775 c = (bp - label - 1);
776 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
785 /* Fully qualified ? */
794 if ((bp - dst) > NS_MAXCDNAME) {
800 if (c == 0 || *src == '.') {
811 *bp++ = (unsigned char)c;
813 c = (bp - label - 1);
814 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
832 if ((bp - dst) > NS_MAXCDNAME)
841 /** Compress a domain name.
842 * @param[in] src List of length-prefixed labels.
843 * @param[out] dst Output buffer.
844 * @param[in] dstsiz Number of bytes that can be written to \a dst.
845 * @param[in,out] dnptrs Array of pointers to previously compressed names.
846 * @param[in] lastdnptr End of \a dnptrs array.
847 * @return Number of bytes written to \a dst.
850 irc_ns_name_pack(const unsigned char *src, unsigned char *dst, int dstsiz,
851 const unsigned char **dnptrs, const unsigned char **lastdnptr)
854 const unsigned char **cpp, **lpp, *eob, *msg;
855 const unsigned char *srcp;
862 if (dnptrs != NULL) {
863 if ((msg = *dnptrs++) != NULL) {
864 for (cpp = dnptrs; *cpp != NULL; cpp++)
866 lpp = cpp; /* end of list to search */
871 /* make sure the domain we are about to add is legal */
877 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
881 if ((l0 = labellen(srcp)) < 0) {
886 if (l > NS_MAXCDNAME) {
893 /* from here on we need to reset compression pointer array on error */
896 /* Look to see if we can use pointers. */
898 if (n != 0 && msg != NULL) {
899 l = irc_dn_find(srcp, msg, (const unsigned char * const *)dnptrs,
900 (const unsigned char * const *)lpp);
902 if (dstp + 1 >= eob) {
905 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
909 /* Not found, save it. */
910 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
911 (dstp - msg) < 0x4000 && first) {
917 /* copy label to buffer */
918 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
919 /* Should not happen. */
923 if (dstp + 1 + n >= eob) {
926 memcpy(dstp, srcp, n + 1);
941 /** Encode and compress an ASCII domain name.
942 * @param[in] src ASCII domain name.
943 * @param[out] dst Output buffer.
944 * @param[in] dstsiz Number of bytes that can be written to \a dst.
945 * @param[in] dnptrs Array of previously compressed names.
946 * @param[in] lastdnptr End of \a dnptrs array.
949 irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz,
950 const unsigned char **dnptrs, const unsigned char **lastdnptr)
952 unsigned char tmp[NS_MAXCDNAME];
954 if (irc_ns_name_pton(src, tmp, sizeof tmp) == -1)
956 return(irc_ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
959 /** Encode a bitstring label.
960 * @param[in,out] bp Input buffer pointer.
961 * @param[in] end End of input buffer.
962 * @param[out] labelp Pointer to output label.
963 * @param[out] dst Output buffer.
964 * @param[out] eom End of output buffer.
967 irc_encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
968 unsigned char **dst, const char *eom)
971 const char *cp = *bp;
973 const char *beg_blen;
974 char *end_blen = NULL;
975 int value = 0, count = 0, tbcount = 0, blen = 0;
977 beg_blen = end_blen = NULL;
979 /* a bitstring must contain at least 2 characters */
983 /* XXX: currently, only hex strings are supported */
986 if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */
989 for (tp = (char*)(*dst + 1); cp < end && tp < eom; cp++) {
991 case ']': /* end of the bitstring */
993 if (beg_blen == NULL)
995 blen = (int)strtol(beg_blen, &end_blen, 10);
996 if (*end_blen != ']')
1000 *tp++ = ((value << 4) & 0xff);
1001 cp++; /* skip ']' */
1008 if (!isdigit(c&0xff))
1010 if (beg_blen == NULL) {
1013 /* blen never begins with 0 */
1019 if (!isxdigit(c&0xff))
1022 value += digitvalue[(int)c];
1036 if (cp >= end || tp >= eom)
1040 * bit length validation:
1041 * If a <length> is present, the number of digits in the <bit-data>
1042 * MUST be just sufficient to contain the number of bits specified
1043 * by the <length>. If there are insignificant bits in a final
1044 * hexadecimal or octal digit, they MUST be zero.
1045 * RFC 2673, Section 3.2.
1050 if (((blen + 3) & ~3) != tbcount)
1052 traillen = tbcount - blen; /* between 0 and 3 */
1053 if (((value << (8 - traillen)) & 0xff) != 0)
1061 /* encode the type and the significant bit fields */
1062 **labelp = DNS_LABELTYPE_BITSTRING;
1066 *dst = (unsigned char*)tp;
1071 /** Find a name in an array of compressed name.
1072 * @param[in] domain Name to search for.
1073 * @param[in] msg Start of DNS message.
1074 * @param[in] dnptrs Start of compressed name array.
1075 * @param[in] lastdnptr End of compressed name array.
1076 * @return Non-negative offset from \a msg, or -1 if not found.
1079 irc_dn_find(const unsigned char *domain, const unsigned char *msg,
1080 const unsigned char * const *dnptrs,
1081 const unsigned char * const *lastdnptr)
1083 const unsigned char *dn, *cp, *sp;
1084 const unsigned char * const *cpp;
1087 for (cpp = dnptrs; cpp < lastdnptr; cpp++)
1091 * terminate search on:
1093 * compression pointer
1096 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
1097 (sp - msg) < 0x4000) {
1100 while ((n = *cp++) != 0) {
1102 * check for indirection
1104 switch (n & NS_CMPRSFLGS) {
1105 case 0: /* normal case, n == len */
1106 n = labellen(cp - 1); /* XXX */
1111 for ((void)NULL; n > 0; n--)
1112 if (mklower(*dn++) !=
1115 /* Is next root for both ? */
1116 if (*dn == '\0' && *cp == '\0')
1121 case NS_CMPRSFLGS: /* indirection */
1122 cp = msg + (((n & 0x3f) << 8) | *cp);
1125 default: /* illegal type */
1138 /** Convert a character to lowercase, assuming ASCII encoded English.
1139 * @param[in] ch Character to convert.
1140 * @return Lower-case version of \a ch.
1145 if (ch >= 0x41 && ch <= 0x5A)
1151 /* From resolv/mkquery.c */
1153 /** Form a query for \a dname in \a buf.
1154 * @param[in] dname Domain name to look up.
1155 * @param[in] class Query class to set in header.
1156 * @param[in] type Query type to set in header.
1157 * @param[out] buf Output buffer for query.
1158 * @param[in] buflen Number of bytes that can be written to \a buf.
1159 * @return Length of message written to \a buf, or -1 on error.
1163 const char *dname, /* domain name */
1164 int class, int type, /* class and type of query */
1165 unsigned char *buf, /* buffer to put query */
1166 int buflen) /* size of buffer */
1171 unsigned char *dnptrs[20], **dpp, **lastdnptr;
1174 * Initialize header fields.
1176 if ((buf == NULL) || (buflen < HFIXEDSZ))
1178 memset(buf, 0, HFIXEDSZ);
1179 hp = (HEADER *) buf;
1183 hp->rd = 1; /* recurse */
1184 hp->rcode = NO_ERRORS;
1185 cp = buf + HFIXEDSZ;
1190 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1192 if ((buflen -= QFIXEDSZ) < 0)
1194 if ((n = irc_dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
1199 IRC_NS_PUT16(type, cp);
1200 IRC_NS_PUT16(class, cp);
1201 hp->qdcount = htons(1);