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);
133 static void add_nameserver(char *arg);
135 /** Array of decimal digits, indexed by value. */
136 static const char digits[] = "0123456789";
137 static int labellen(const unsigned char *lp);
138 static int special(int ch);
139 static int printable(int ch);
140 static int irc_decode_bitstring(const char **cpp, char *dn, const char *eom);
141 static int irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz,
142 const unsigned char **dnptrs, const unsigned char **lastdnptr);
143 static int irc_dn_find(const unsigned char *, const unsigned char *, const unsigned char * const *,
144 const unsigned char * const *);
145 static int irc_encode_bitsring(const char **, const char *, unsigned char **, unsigned char **,
147 static int mklower(int ch);
149 /** Initialize the resolver library.
150 * @return Zero on success, non-zero on failure.
156 return(parse_resvconf());
159 /** Read resolver configuration file for domain and nameserver lines.
160 * The "domain" line is used to overwrite #irc_domain.
161 * Addresses in "nameserver" lines are appended to #irc_nsaddr_list.
162 * @return Zero on success, non-zero on failure.
173 /* XXX "/etc/resolv.conf" should be from a define in setup.h perhaps
174 * for cygwin support etc. this hardcodes it to unix for now -db
176 if ((file = fbopen("/etc/resolv.conf", "r")) == NULL)
179 while (fbgets(input, MAXLINE, file) != NULL)
181 /* blow away any newline */
182 if ((p = strpbrk(input, "\r\n")) != NULL)
185 /* Ignore comment lines immediately */
190 /* skip until something that's not a space is seen */
193 /* if at this point, have a '\0' then continue */
197 /* skip until a space is found */
201 continue; /* no arguments?.. ignore this line */
202 /* blow away the space character */
205 /* skip these spaces that are before the argument */
208 /* Now arg should be right where p is pointing */
210 if ((p = strpbrk(arg, " \t")) != NULL)
211 *p = '\0'; /* take the first word */
213 if (strcasecmp(opt, "domain") == 0)
214 ircd_strncpy(irc_domain, arg, HOSTLEN);
215 else if (strcasecmp(opt, "nameserver") == 0)
223 /** Add a resolver to #irc_nsaddr_list.
224 * @param[in] arg Dotted quad or IPv6 text form of nameserver address.
227 add_nameserver(char *arg)
229 struct irc_sockaddr res;
231 /* Done max number of nameservers? */
232 if ((irc_nscount + 1) >= IRCD_MAXNS)
235 /* Failure converting from numeric string? */
236 if (!ircd_aton(&res.addr, arg))
239 memcpy(&irc_nsaddr_list[irc_nscount], &res, sizeof(irc_nsaddr_list[irc_nscount]));
244 * Expand compressed domain name to full domain name.
245 * Like irc_ns_name_uncompress(), but checks for a well-formed result.
246 * @param[in] msg Pointer to the beginning of the message.
247 * @param[in] eom First location after the message.
248 * @param[in] src Pointer to where to starting decoding.
249 * @param[out] dst Output buffer.
250 * @param[in] dstsiz Number of bytes that can be written to \a dst.
251 * @return Number of bytes written to \a dst.
254 irc_dn_expand(const unsigned char *msg, const unsigned char *eom,
255 const unsigned char *src, char *dst, int dstsiz)
257 int n = irc_ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
259 if (n > 0 && dst[0] == '.')
265 * Expand compressed domain name to full domain name.
266 * @param[in] msg Pointer to the beginning of the message.
267 * @param[in] eom First location after the message.
268 * @param[in] src Pointer to where to starting decoding.
269 * @param[out] dst Output buffer.
270 * @param[in] dstsiz Number of bytes that can be written to \a dst.
271 * @return Number of bytes written to \a dst.
274 irc_ns_name_uncompress(const unsigned char *msg, const unsigned char *eom,
275 const unsigned char *src, char *dst, size_t dstsiz)
277 unsigned char tmp[NS_MAXCDNAME];
280 if ((n = irc_ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
282 if (irc_ns_name_ntop((char*)tmp, (char*)dst, dstsiz) == -1)
288 * Unpack compressed domain name to uncompressed form.
289 * @param[in] msg Pointer to the beginning of the message.
290 * @param[in] eom First location after the message.
291 * @param[in] src Pointer to where to starting decoding.
292 * @param[out] dst Output buffer.
293 * @param[in] dstsiz Number of bytes that can be written to \a dst.
294 * @return Number of bytes written to \a dst.
297 irc_ns_name_unpack(const unsigned char *msg, const unsigned char *eom,
298 const unsigned char *src, unsigned char *dst,
301 const unsigned char *srcp, *dstlim;
303 int n, len, checked, l;
309 dstlim = dst + dstsiz;
310 if (srcp < msg || srcp >= eom) {
314 /* Fetch next label in domain name. */
315 while ((n = *srcp++) != 0) {
316 /* Check for indirection. */
317 switch (n & NS_CMPRSFLGS) {
321 if ((l = labellen(srcp - 1)) < 0) {
325 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
331 memcpy(dstp, srcp, l);
342 len = srcp - src + 1;
343 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
344 if (srcp < msg || srcp >= eom) { /* Out of range. */
350 * Check for loops in the compressed name;
351 * if we've looked at the whole message,
352 * there must be a loop.
354 if (checked >= eom - msg) {
362 return (-1); /* flag error */
372 * Convert RFC1035 length-prefixed tag sequence to printable ASCII.
373 * @param[in] src Input tag sequence (effectively NUL terminated).
374 * @param[out] dst Buffer for uncompressed output.
375 * @param[in] dstsiz Number of bytes that can be written to \a dst.
376 * @return Number of bytes written to \a dst.
379 irc_ns_name_ntop(const char *src, char *dst, size_t dstsiz)
391 while ((n = *cp++) != 0) {
392 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
393 /* Some kind of compression pointer. */
404 if ((l = labellen((const unsigned char*)(cp - 1))) < 0) {
405 errno = EMSGSIZE; /* XXX */
412 if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
415 if (n != DNS_LABELTYPE_BITSTRING) {
416 /* XXX: labellen should reject this case */
420 if ((m = irc_decode_bitstring(&cp, dn, eom)) < 0)
428 for ((void)NULL; l > 0; l--) {
437 } else if (!printable(c)) {
443 *dn++ = digits[c / 100];
444 *dn++ = digits[(c % 100) / 10];
445 *dn++ = digits[c % 10];
470 /** Pack domain name from presentation form into compressed format.
471 * @param[in] src Presentation form of name.
472 * @param[out] dst Output buffer.
473 * @param[in] dstsiz Number of bytes that can be written to \a dst.
474 * @param[in,out] dnptrs Array of previously seen labels.
475 * @param[in] lastdnptr End of \a dnptrs array.
476 * @return Number of bytes written to \a dst.
479 irc_dn_comp(const char *src, unsigned char *dst, int dstsiz,
480 unsigned char **dnptrs, unsigned char **lastdnptr)
482 return(irc_ns_name_compress(src, dst, (size_t)dstsiz,
483 (const unsigned char **)dnptrs,
484 (const unsigned char **)lastdnptr));
487 /** Skip over a compressed domain name.
488 * @param[in] ptr Start of compressed name.
489 * @param[in] eom End of message.
490 * @return Length of the compressed name, or -1 on error.
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)
498 return(ptr - saveptr);
501 /** Advance \a ptrptr to skip over the compressed name it points at.
502 * @param[in,out] ptrptr Pointer to the compressed name.
503 * @param[in] eom End of message.
504 * @return Zero on success; non-zero (with errno set) on failure.
507 irc_ns_name_skip(const unsigned char **ptrptr, const unsigned char *eom)
509 const unsigned char *cp;
515 while (cp < eom && (n = *cp++) != 0)
517 /* Check for indirection. */
518 switch (n & NS_CMPRSFLGS)
520 case 0: /* normal case, n == len */
523 case NS_TYPE_ELT: /* EDNS0 extended label */
524 if ((l = labellen(cp - 1)) < 0)
526 errno = EMSGSIZE; /* XXX */
532 case NS_CMPRSFLGS: /* indirection */
535 default: /* illegal type */
553 /** Read a 16-bit network-endian value from \a src.
554 * @param[in] src Input data buffer.
555 * @return Value retrieved from buffer.
558 irc_ns_get16(const unsigned char *src)
562 IRC_NS_GET16(dst, src);
566 /** Read a 32-bit network-endian value from \a src.
567 * @param[in] src Input data buffer.
568 * @return Value retrieved from buffer.
571 irc_ns_get32(const unsigned char *src)
575 IRC_NS_GET32(dst, src);
579 /** Write a 16-bit network-endian value to \a dst.
580 * @param[in] src Value to write.
581 * @param[out] dst Output buffer.
584 irc_ns_put16(unsigned int src, unsigned char *dst)
586 IRC_NS_PUT16(src, dst);
589 /** Write a 32-bit network-endian value to \a dst.
590 * @param[in] src Value to write.
591 * @param[out] dst Output buffer.
594 irc_ns_put32(unsigned long src, unsigned char *dst)
596 IRC_NS_PUT32(src, dst);
601 /** Indicate whether a character needs quoting.
602 * (What RFC does this come from?)
603 * @param[in] ch Character to check for specialness.
604 * @return Non-zero if the character should be quoted.
614 case 0x5C: /* '\\' */
617 /* Special modifiers in zone files. */
626 /** Calculate the length of a particular DNS label.
627 * @param[in] lp Start of label.
628 * @return Length of label, or -1 on error.
631 labellen(const unsigned char *lp)
634 unsigned char l = *lp;
636 if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS)
638 /* should be avoided by the caller */
642 if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT)
644 if (l == DNS_LABELTYPE_BITSTRING)
646 if ((bitlen = *(lp + 1)) == 0)
648 return((bitlen + 7 ) / 8 + 1);
651 return(-1); /* unknown ELT */
658 /** Indicate whether a character is printable.
659 * @param[in] ch Character to check for printability.
660 * @return Non-zero if the character is printable; zero if it is not.
665 return(ch > 0x20 && ch < 0x7f);
668 /** Decode a bitstring label from DNS.
669 * @param[in,out] cpp Pointer to start of label.
670 * @param[in,out] dn Output buffer.
671 * @param[in] eom End of message.
674 irc_decode_bitstring(const char **cpp, char *dn, const char *eom)
676 const char *cp = *cpp;
680 if ((blen = (*cp & 0xff)) == 0)
682 plen = (blen + 3) / 4;
683 plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
684 if (dn + plen >= eom)
688 dn += sprintf(dn, "\\[x");
689 for (b = blen; b > 7; b -= 8, cp++)
690 dn += sprintf(dn, "%02x", *cp & 0xff);
693 dn += sprintf(dn, "%02x", tc & (0xff << (8 - b)));
696 dn += sprintf(dn, "%1x",
697 ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
699 dn += sprintf(dn, "/%d]", blen);
705 /** Convert an ASCII name string into an encoded domain name.
706 * This function enforces per-label and total name lengths.
707 * @param[in] src ASCII name string.
708 * @param[out] dst Destination buffer.
709 * @param[in] dstsiz Number of bytes that can be written to \a dst.
710 * @return -1 on failure, 0 if \a src was not fully qualified, 1 if \a
711 * src was fully qualified.
714 irc_ns_name_pton(const char *src, unsigned char *dst, size_t dstsiz)
716 unsigned char *label, *bp, *eom;
718 int c, n, escaped, e = 0;
726 while ((c = *src++) != 0) {
728 if (c == '[') { /* start a bit string label */
729 if ((cp = strchr(src, ']')) == NULL) {
730 errno = EINVAL; /* ??? */
733 if ((e = irc_encode_bitsring(&src,
744 if ((c = *src++) == 0)
752 else if ((cp = strchr(digits, c)) != NULL) {
753 n = (cp - digits) * 100;
754 if ((c = *src++) == 0 ||
755 (cp = strchr(digits, c)) == NULL) {
759 n += (cp - digits) * 10;
760 if ((c = *src++) == 0 ||
761 (cp = strchr(digits, c)) == NULL) {
773 } else if (c == '\\') {
776 } else if (c == '.') {
777 c = (bp - label - 1);
778 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
787 /* Fully qualified ? */
796 if ((bp - dst) > NS_MAXCDNAME) {
802 if (c == 0 || *src == '.') {
813 *bp++ = (unsigned char)c;
815 c = (bp - label - 1);
816 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
834 if ((bp - dst) > NS_MAXCDNAME)
843 /** Compress a domain name.
844 * @param[in] src List of length-prefixed labels.
845 * @param[out] dst Output buffer.
846 * @param[in] dstsiz Number of bytes that can be written to \a dst.
847 * @param[in,out] dnptrs Array of pointers to previously compressed names.
848 * @param[in] lastdnptr End of \a dnptrs array.
849 * @return Number of bytes written to \a dst.
852 irc_ns_name_pack(const unsigned char *src, unsigned char *dst, int dstsiz,
853 const unsigned char **dnptrs, const unsigned char **lastdnptr)
856 const unsigned char **cpp, **lpp, *eob, *msg;
857 const unsigned char *srcp;
864 if (dnptrs != NULL) {
865 if ((msg = *dnptrs++) != NULL) {
866 for (cpp = dnptrs; *cpp != NULL; cpp++)
868 lpp = cpp; /* end of list to search */
873 /* make sure the domain we are about to add is legal */
879 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
883 if ((l0 = labellen(srcp)) < 0) {
888 if (l > NS_MAXCDNAME) {
895 /* from here on we need to reset compression pointer array on error */
898 /* Look to see if we can use pointers. */
900 if (n != 0 && msg != NULL) {
901 l = irc_dn_find(srcp, msg, (const unsigned char * const *)dnptrs,
902 (const unsigned char * const *)lpp);
904 if (dstp + 1 >= eob) {
907 *dstp++ = (l >> 8) | NS_CMPRSFLGS;
911 /* Not found, save it. */
912 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
913 (dstp - msg) < 0x4000 && first) {
919 /* copy label to buffer */
920 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
921 /* Should not happen. */
925 if (dstp + 1 + n >= eob) {
928 memcpy(dstp, srcp, n + 1);
943 /** Encode and compress an ASCII domain name.
944 * @param[in] src ASCII domain name.
945 * @param[out] dst Output buffer.
946 * @param[in] dstsiz Number of bytes that can be written to \a dst.
947 * @param[in] dnptrs Array of previously compressed names.
948 * @param[in] lastdnptr End of \a dnptrs array.
951 irc_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz,
952 const unsigned char **dnptrs, const unsigned char **lastdnptr)
954 unsigned char tmp[NS_MAXCDNAME];
956 if (irc_ns_name_pton(src, tmp, sizeof tmp) == -1)
958 return(irc_ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
961 /** Encode a bitstring label.
962 * @param[in,out] bp Input buffer pointer.
963 * @param[in] end End of input buffer.
964 * @param[out] labelp Pointer to output label.
965 * @param[out] dst Output buffer.
966 * @param[out] eom End of output buffer.
969 irc_encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
970 unsigned char **dst, const char *eom)
973 const char *cp = *bp;
975 const char *beg_blen;
976 char *end_blen = NULL;
977 int value = 0, count = 0, tbcount = 0, blen = 0;
979 beg_blen = end_blen = NULL;
981 /* a bitstring must contain at least 2 characters */
985 /* XXX: currently, only hex strings are supported */
988 if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */
991 for (tp = (char*)(*dst + 1); cp < end && tp < eom; cp++) {
993 case ']': /* end of the bitstring */
995 if (beg_blen == NULL)
997 blen = (int)strtol(beg_blen, &end_blen, 10);
998 if (*end_blen != ']')
1002 *tp++ = ((value << 4) & 0xff);
1003 cp++; /* skip ']' */
1010 if (!isdigit(c&0xff))
1012 if (beg_blen == NULL) {
1015 /* blen never begins with 0 */
1021 if (!isxdigit(c&0xff))
1024 value += digitvalue[(int)c];
1038 if (cp >= end || tp >= eom)
1042 * bit length validation:
1043 * If a <length> is present, the number of digits in the <bit-data>
1044 * MUST be just sufficient to contain the number of bits specified
1045 * by the <length>. If there are insignificant bits in a final
1046 * hexadecimal or octal digit, they MUST be zero.
1047 * RFC 2673, Section 3.2.
1052 if (((blen + 3) & ~3) != tbcount)
1054 traillen = tbcount - blen; /* between 0 and 3 */
1055 if (((value << (8 - traillen)) & 0xff) != 0)
1063 /* encode the type and the significant bit fields */
1064 **labelp = DNS_LABELTYPE_BITSTRING;
1068 *dst = (unsigned char*)tp;
1073 /** Find a name in an array of compressed name.
1074 * @param[in] domain Name to search for.
1075 * @param[in] msg Start of DNS message.
1076 * @param[in] dnptrs Start of compressed name array.
1077 * @param[in] lastdnptr End of compressed name array.
1078 * @return Non-negative offset from \a msg, or -1 if not found.
1081 irc_dn_find(const unsigned char *domain, const unsigned char *msg,
1082 const unsigned char * const *dnptrs,
1083 const unsigned char * const *lastdnptr)
1085 const unsigned char *dn, *cp, *sp;
1086 const unsigned char * const *cpp;
1089 for (cpp = dnptrs; cpp < lastdnptr; cpp++)
1093 * terminate search on:
1095 * compression pointer
1098 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
1099 (sp - msg) < 0x4000) {
1102 while ((n = *cp++) != 0) {
1104 * check for indirection
1106 switch (n & NS_CMPRSFLGS) {
1107 case 0: /* normal case, n == len */
1108 n = labellen(cp - 1); /* XXX */
1113 for ((void)NULL; n > 0; n--)
1114 if (mklower(*dn++) !=
1117 /* Is next root for both ? */
1118 if (*dn == '\0' && *cp == '\0')
1123 case NS_CMPRSFLGS: /* indirection */
1124 cp = msg + (((n & 0x3f) << 8) | *cp);
1127 default: /* illegal type */
1140 /** Convert a character to lowercase, assuming ASCII encoded English.
1141 * @param[in] ch Character to convert.
1142 * @return Lower-case version of \a ch.
1147 if (ch >= 0x41 && ch <= 0x5A)
1153 /* From resolv/mkquery.c */
1155 /** Form a query for \a dname in \a buf.
1156 * @param[in] dname Domain name to look up.
1157 * @param[in] class Query class to set in header.
1158 * @param[in] type Query type to set in header.
1159 * @param[out] buf Output buffer for query.
1160 * @param[in] buflen Number of bytes that can be written to \a buf.
1161 * @return Length of message written to \a buf, or -1 on error.
1165 const char *dname, /* domain name */
1166 int class, int type, /* class and type of query */
1167 unsigned char *buf, /* buffer to put query */
1168 int buflen) /* size of buffer */
1173 unsigned char *dnptrs[20], **dpp, **lastdnptr;
1176 * Initialize header fields.
1178 if ((buf == NULL) || (buflen < HFIXEDSZ))
1180 memset(buf, 0, HFIXEDSZ);
1181 hp = (HEADER *) buf;
1185 hp->rd = 1; /* recurse */
1186 hp->rcode = NO_ERRORS;
1187 cp = buf + HFIXEDSZ;
1192 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1194 if ((buflen -= QFIXEDSZ) < 0)
1196 if ((n = irc_dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
1201 IRC_NS_PUT16(type, cp);
1202 IRC_NS_PUT16(class, cp);
1203 hp->qdcount = htons(1);