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 orginal files
84 #include "ircd_defs.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
99 struct irc_sockaddr_storage irc_nsaddr_list
[IRCD_MAXNS
];
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 **,
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 *,
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
);
149 return parse_resvconf();
155 * output - -1 if failure 0 if success
156 * side effects - fills in irc_nsaddr_list
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
)
173 while (fgets(input
, sizeof(input
), file
) != NULL
)
175 /* blow away any newline */
176 if ((p
= strpbrk(input
, "\r\n")) != NULL
)
180 /* skip until something thats not a space is seen */
183 /* if at this point, have a '\0' then continue */
187 /* Ignore comment lines immediately */
188 if (*p
== '#' || *p
== ';')
191 /* skip until a space is found */
193 while (!IsSpace(*p
) && *p
!= '\0')
196 continue; /* no arguments?.. ignore this line */
197 /* blow away the space character */
200 /* skip these spaces that are before the argument */
203 /* Now arg should be right where p is pointing */
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)
220 * input - either an IPV4 address in dotted quad
221 * or an IPV6 address in : format
223 * side effects - entry in irc_nsaddr_list is filled in as needed
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
);
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
))
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
);
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] == '.')
273 * irc_ns_name_uncompress(msg, eom, src, dst, dstsiz)
274 * Expand compressed domain name to presentation format.
276 * Number of bytes read out of `src', or -1 (with errno set).
278 * Root domain returns as "." not "".
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
];
287 if ((n
= irc_ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
289 if (irc_ns_name_ntop((char*)tmp
, dst
, dstsiz
) == -1)
294 * irc_ns_name_unpack(msg, eom, src, dst, dstsiz)
295 * Unpack a domain name from a message, source may be compressed.
297 * -1 if it fails, or consumed octets if it succeeds.
300 irc_ns_name_unpack(const unsigned char *msg
, const unsigned char *eom
,
301 const unsigned char *src
, unsigned char *dst
,
304 const unsigned char *srcp
, *dstlim
;
306 int n
, len
, checked
, l
;
312 dstlim
= dst
+ dstsiz
;
313 if (srcp
< msg
|| srcp
>= eom
) {
317 /* Fetch next label in domain name. */
318 while ((n
= *srcp
++) != 0) {
319 /* Check for indirection. */
320 switch (n
& NS_CMPRSFLGS
) {
324 if ((l
= labellen(srcp
- 1)) < 0) {
328 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
334 memcpy(dstp
, srcp
, l
);
345 len
= srcp
- src
+ 1;
346 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
347 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
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
) {
365 return (-1); /* flag error */
375 * irc_ns_name_ntop(src, dst, dstsiz)
376 * Convert an encoded domain name to printable ascii as per RFC1035.
378 * Number of bytes written to buffer, or -1 (with errno set)
380 * The root is returned as "."
381 * All other domains are returned in non absolute form
384 irc_ns_name_ntop(const char *src
, char *dst
, size_t dstsiz
)
396 while ((n
= *cp
++) != 0) {
397 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
398 /* Some kind of compression pointer. */
409 if ((l
= labellen((unsigned char*)(cp
- 1))) < 0) {
410 errno
= EMSGSIZE
; /* XXX */
417 if ((n
& NS_CMPRSFLGS
) == NS_TYPE_ELT
) {
420 if (n
!= DNS_LABELTYPE_BITSTRING
) {
421 /* XXX: labellen should reject this case */
425 if ((m
= irc_decode_bitstring(&cp
, dn
, eom
)) < 0)
433 for ((void)NULL
; l
> 0; l
--) {
442 } else if (!printable(c
)) {
448 *dn
++ = digits
[c
/ 100];
449 *dn
++ = digits
[(c
% 100) / 10];
450 *dn
++ = digits
[c
% 10];
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'.
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)
498 return(ptr
- saveptr
);
502 * ns_name_skip(ptrptr, eom)
503 * Advance *ptrptr to skip over the compressed name it points at.
505 * 0 on success, -1 (with errno set) on failure.
508 irc_ns_name_skip(const unsigned char **ptrptr
, const unsigned char *eom
)
510 const unsigned char *cp
;
516 while (cp
< eom
&& (n
= *cp
++) != 0)
518 /* Check for indirection. */
519 switch (n
& NS_CMPRSFLGS
)
521 case 0: /* normal case, n == len */
524 case NS_TYPE_ELT
: /* EDNS0 extended label */
525 if ((l
= labellen(cp
- 1)) < 0)
527 errno
= EMSGSIZE
; /* XXX */
533 case NS_CMPRSFLGS
: /* indirection */
536 default: /* illegal type */
555 irc_ns_get16(const unsigned char *src
)
559 IRC_NS_GET16(dst
, src
);
564 irc_ns_get32(const unsigned char *src
)
568 IRC_NS_GET32(dst
, src
);
573 irc_ns_put16(unsigned int src
, unsigned char *dst
)
575 IRC_NS_PUT16(src
, dst
);
579 irc_ns_put32(unsigned long src
, unsigned char *dst
)
581 IRC_NS_PUT32(src
, dst
);
588 * Thinking in noninternationalized USASCII (per the DNS spec),
589 * is this characted special ("in need of quoting") ?
601 case 0x5C: /* '\\' */
604 /* Special modifiers in zone files. */
614 labellen(const unsigned char *lp
)
617 unsigned char l
= *lp
;
619 if ((l
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
)
621 /* should be avoided by the caller */
625 if ((l
& NS_CMPRSFLGS
) == NS_TYPE_ELT
)
627 if (l
== DNS_LABELTYPE_BITSTRING
)
629 if ((bitlen
= *(lp
+ 1)) == 0)
631 return((bitlen
+ 7 ) / 8 + 1);
634 return(-1); /* unknwon ELT */
643 * Thinking in noninternationalized USASCII (per the DNS spec),
644 * is this character visible and not a space when printed ?
651 return(ch
> 0x20 && ch
< 0x7f);
655 irc_decode_bitstring(const char **cpp
, char *dn
, const char *eom
)
657 const char *cp
= *cpp
;
661 if ((blen
= (*cp
& 0xff)) == 0)
663 plen
= (blen
+ 3) / 4;
664 plen
+= sizeof("\\[x/]") + (blen
> 99 ? 3 : (blen
> 9) ? 2 : 1);
665 if (dn
+ plen
>= eom
)
669 dn
+= sprintf(dn
, "\\[x");
670 for (b
= blen
; b
> 7; b
-= 8, cp
++)
671 dn
+= sprintf(dn
, "%02x", *cp
& 0xff);
674 dn
+= sprintf(dn
, "%02x", tc
& (0xff << (8 - b
)));
677 dn
+= sprintf(dn
, "%1x",
678 ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
680 dn
+= sprintf(dn
, "/%d]", blen
);
687 * irc_ns_name_pton(src, dst, dstsiz)
688 * Convert a ascii string into an encoded domain name as per RFC1035.
691 * 1 if string was fully qualified
692 * 0 is string was not fully qualified
694 * Enforces label and domain length limits.
697 irc_ns_name_pton(const char *src
, unsigned char *dst
, size_t dstsiz
)
699 unsigned char *label
, *bp
, *eom
;
701 int c
, n
, escaped
, e
= 0;
709 while ((c
= *src
++) != 0) {
711 if (c
== '[') { /* start a bit string label */
712 if ((cp
= strchr(src
, ']')) == NULL
) {
713 errno
= EINVAL
; /* ??? */
716 if ((e
= irc_encode_bitsring(&src
,
727 if ((c
= *src
++) == 0)
735 else if ((cp
= strchr(digits
, c
)) != NULL
) {
736 n
= (cp
- digits
) * 100;
737 if ((c
= *src
++) == 0 ||
738 (cp
= strchr(digits
, c
)) == NULL
) {
742 n
+= (cp
- digits
) * 10;
743 if ((c
= *src
++) == 0 ||
744 (cp
= strchr(digits
, c
)) == NULL
) {
756 } else if (c
== '\\') {
759 } else if (c
== '.') {
760 c
= (bp
- label
- 1);
761 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
770 /* Fully qualified ? */
779 if ((bp
- dst
) > NS_MAXCDNAME
) {
785 if (c
== 0 || *src
== '.') {
796 *bp
++ = (unsigned char)c
;
798 c
= (bp
- label
- 1);
799 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
817 if ((bp
- dst
) > NS_MAXCDNAME
)
827 * irc_ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
828 * Pack domain name 'domain' into 'comp_dn'.
830 * Size of the compressed name, or -1.
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
835 * 'lastdnptr' is a pointer to the end of the array pointed to
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
844 irc_ns_name_pack(const unsigned char *src
, unsigned char *dst
, int dstsiz
,
845 const unsigned char **dnptrs
, const unsigned char **lastdnptr
)
848 const unsigned char **cpp
, **lpp
, *eob
, *msg
;
849 const unsigned char *srcp
;
856 if (dnptrs
!= NULL
) {
857 if ((msg
= *dnptrs
++) != NULL
) {
858 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
860 lpp
= cpp
; /* end of list to search */
865 /* make sure the domain we are about to add is legal */
871 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
875 if ((l0
= labellen(srcp
)) < 0) {
880 if (l
> NS_MAXCDNAME
) {
887 /* from here on we need to reset compression pointer array on error */
890 /* Look to see if we can use pointers. */
892 if (n
!= 0 && msg
!= NULL
) {
893 l
= irc_dn_find(srcp
, msg
, (const unsigned char * const *)dnptrs
,
894 (const unsigned char * const *)lpp
);
896 if (dstp
+ 1 >= eob
) {
899 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
903 /* Not found, save it. */
904 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
905 (dstp
- msg
) < 0x4000 && first
) {
911 /* copy label to buffer */
912 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
913 /* Should not happen. */
917 if (dstp
+ 1 + n
>= eob
) {
920 memcpy(dstp
, srcp
, n
+ 1);
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)
943 return(irc_ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
947 irc_encode_bitsring(const char **bp
, const char *end
, unsigned char **labelp
,
948 unsigned char **dst
, const char *eom
)
951 const char *cp
= *bp
;
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 */
963 /* XXX: currently, only hex strings are supported */
966 if (!isxdigit((*cp
) & 0xff)) /* reject '\[x/BLEN]' */
969 for (tp
= (char*)(dst
+ 1); cp
< end
&& tp
< eom
; cp
++) {
971 case ']': /* end of the bitstring */
973 if (beg_blen
== NULL
)
975 blen
= (int)strtol(beg_blen
, &end_blen
, 10);
976 if (*end_blen
!= ']')
980 *tp
++ = ((value
<< 4) & 0xff);
988 if (!isdigit(c
&0xff))
990 if (beg_blen
== NULL
) {
993 /* blen never begings with 0 */
999 if (!isxdigit(c
&0xff))
1002 value
+= digitvalue
[(int)c
];
1016 if (cp
>= end
|| tp
>= eom
)
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.
1030 if (((blen
+ 3) & ~3) != tbcount
)
1032 traillen
= tbcount
- blen
; /* between 0 and 3 */
1033 if (((value
<< (8 - traillen
)) & 0xff) != 0)
1041 /* encode the type and the significant bit fields */
1042 **labelp
= DNS_LABELTYPE_BITSTRING
;
1046 *dst
= (unsigned char*)tp
;
1052 * dn_find(domain, msg, dnptrs, lastdnptr)
1053 * Search for the counted-label name in an array of compressed names.
1055 * offset from msg if found, or -1.
1057 * dnptrs is the pointer to the first name on the list,
1058 * not the pointer to the start of the message.
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
;
1069 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++)
1073 * terminate search on:
1075 * compression pointer
1078 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
1079 (sp
- msg
) < 0x4000) {
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 */
1093 for ((void)NULL
; n
> 0; n
--)
1094 if (mklower(*dn
++) !=
1097 /* Is next root for both ? */
1098 if (*dn
== '\0' && *cp
== '\0')
1103 case NS_CMPRSFLGS
: /* indirection */
1104 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
1107 default: /* illegal type */
1121 * * Thinking in noninternationalized USASCII (per the DNS spec),
1122 * * convert this character to lower case if it's upper case.
1127 if (ch
>= 0x41 && ch
<= 0x5A)
1133 /* From resolv/mkquery.c */
1136 * Form all types of queries.
1137 * Returns the size of the result or -1.
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 */
1149 unsigned char *dnptrs
[20], **dpp
, **lastdnptr
;
1152 * Initialize header fields.
1154 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
1156 memset(buf
, 0, HFIXEDSZ
);
1157 hp
= (HEADER
*) buf
;
1161 hp
->rd
= 1; /* recurse */
1162 hp
->rcode
= NO_ERRORS
;
1163 cp
= buf
+ HFIXEDSZ
;
1168 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
1170 if ((buflen
-= QFIXEDSZ
) < 0)
1172 if ((n
= irc_dn_comp(dname
, cp
, buflen
, dnptrs
, lastdnptr
)) < 0)
1177 IRC_NS_PUT16(type
, cp
);
1178 IRC_NS_PUT16(class, cp
);
1179 hp
->qdcount
= htons(1);