2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
8 * Portions Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
9 * Portions Copyright (C) 1996, 1997, 1988, 1999, 2001, 2003 Internet Software Consortium.
11 * Permission to use, copy, modify, and/or distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
16 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
18 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
25 * Copyright (c) 1985, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
77 #if defined(LIBC_SCCS) && !defined(lint)
78 static const char sccsid
[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
79 static const char rcsid
[] = "$Id: res_mkquery.c,v 1.10 2008/12/11 09:59:00 marka Exp $";
80 #endif /* LIBC_SCCS and not lint */
82 #include "port_before.h"
83 #include <sys/types.h>
84 #include <sys/param.h>
85 #include <netinet/in.h>
86 #include <arpa/nameser.h>
89 #include <sys/socket.h>
92 #endif /* SUNW_CONFCHECK */
99 #include "port_after.h"
101 /* Options. Leave them on. */
104 extern const char *_res_opcodes
[];
106 #ifdef SUNW_CONFCHECK
107 static int _confcheck(res_state statp
);
108 #endif /* SUNW_CONFCHECK */
112 * Form all types of queries.
113 * Returns the size of the result or -1.
116 res_nmkquery(res_state statp
,
117 int op
, /*!< opcode of query */
118 const char *dname
, /*!< domain name */
119 int class, int type
, /*!< class and type of query */
120 const u_char
*data
, /*!< resource record data */
121 int datalen
, /*!< length of data */
122 const u_char
*newrr_in
, /*!< new rr for modify or append */
123 u_char
*buf
, /*!< buffer to put query */
124 int buflen
) /*!< size of buffer */
127 register u_char
*cp
, *ep
;
129 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
134 if (statp
->options
& RES_DEBUG
)
135 printf(";; res_nmkquery(%s, %s, %s, %s)\n",
136 _res_opcodes
[op
], dname
, p_class(class), p_type(type
));
139 #ifdef SUNW_CONFCHECK
141 * 1247019, 1265838, and 4034368: Check to see if we can
144 if (_confcheck(statp
) == -1) {
145 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
148 #endif /* SUNW_CONFCHECK */
151 * Initialize header fields.
153 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
155 memset(buf
, 0, HFIXEDSZ
);
157 statp
->id
= res_nrandomid(statp
);
158 hp
->id
= htons(statp
->id
);
160 hp
->rd
= (statp
->options
& RES_RECURSE
) != 0U;
167 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
169 * perform opcode specific processing
172 case QUERY
: /*FALLTHROUGH*/
174 if (ep
- cp
< QFIXEDSZ
)
176 if ((n
= dn_comp(dname
, cp
, ep
- cp
- QFIXEDSZ
, dnptrs
,
184 hp
->qdcount
= htons(1);
185 if (op
== QUERY
|| data
== NULL
)
188 * Make an additional record for completion domain.
190 if ((ep
- cp
) < RRFIXEDSZ
)
192 n
= dn_comp((const char *)data
, cp
, ep
- cp
- RRFIXEDSZ
,
197 ns_put16(T_NULL
, cp
);
205 hp
->arcount
= htons(1);
210 * Initialize answer section
212 if (ep
- cp
< 1 + RRFIXEDSZ
+ datalen
)
214 *cp
++ = '\0'; /*%< no domain name */
221 ns_put16(datalen
, cp
);
224 memcpy(cp
, data
, datalen
);
227 hp
->ancount
= htons(1);
237 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
240 res_nopt(res_state statp
,
241 int n0
, /*%< current offset in buffer */
242 u_char
*buf
, /*%< buffer to put query */
243 int buflen
, /*%< size of buffer */
244 int anslen
) /*%< UDP answer buffer size */
247 register u_char
*cp
, *ep
;
251 if ((statp
->options
& RES_DEBUG
) != 0U)
252 printf(";; res_nopt()\n");
259 if ((ep
- cp
) < 1 + RRFIXEDSZ
)
262 *cp
++ = 0; /*%< "." */
263 ns_put16(ns_t_opt
, cp
); /*%< TYPE */
265 ns_put16(anslen
& 0xffff, cp
); /*%< CLASS = UDP payload size */
267 *cp
++ = NOERROR
; /*%< extended RCODE */
268 *cp
++ = 0; /*%< EDNS version */
270 if (statp
->options
& RES_USE_DNSSEC
) {
272 if (statp
->options
& RES_DEBUG
)
273 printf(";; res_opt()... ENDS0 DNSSEC\n");
275 flags
|= NS_OPT_DNSSEC_OK
;
280 ns_put16(0U, cp
); /*%< RDLEN */
283 hp
->arcount
= htons(ntohs(hp
->arcount
) + 1);
289 * Construct variable data (RDATA) block for OPT psuedo-RR, append it
290 * to the buffer, then update the RDLEN field (previously set to zero by
291 * res_nopt()) with the new RDATA length.
294 res_nopt_rdata(res_state statp
,
295 int n0
, /*%< current offset in buffer */
296 u_char
*buf
, /*%< buffer to put query */
297 int buflen
, /*%< size of buffer */
298 u_char
*rdata
, /*%< ptr to start of opt rdata */
299 u_short code
, /*%< OPTION-CODE */
300 u_short len
, /*%< OPTION-LENGTH */
301 u_char
*data
) /*%< OPTION_DATA */
303 register u_char
*cp
, *ep
;
306 if ((statp
->options
& RES_DEBUG
) != 0U)
307 printf(";; res_nopt_rdata()\n");
313 if ((ep
- cp
) < (4 + len
))
316 if (rdata
< (buf
+ 2) || rdata
>= ep
)
325 memcpy(cp
, data
, len
);
329 ns_put16(len
, rdata
- 2); /* Update RDLEN field */
335 #ifdef SUNW_CONFCHECK
338 * Time out quickly if there is no /etc/resolv.conf and a TCP connection
339 * to the local DNS server fails.
341 static int _confcheck(res_state statp
)
345 struct sockaddr_in ns_sin
;
347 /* First, we check to see if /etc/resolv.conf exists.
348 * If it doesn't, then it is likely that the localhost is
351 if (stat(_PATH_RESCONF
, &rc_stat
) == -1 && errno
== ENOENT
) {
353 /* Next, we check to see if _res.nsaddr is set to loopback.
354 * If it isn't, it has been altered by the application
355 * explicitly and we then want to bail with success.
357 if (statp
->nsaddr
.sin_addr
.S_un
.S_addr
==
358 htonl(INADDR_LOOPBACK
)) {
360 /* Lastly, we try to connect to the TCP port of the
361 * nameserver. If this fails, then we know that
362 * DNS is misconfigured and we can quickly exit.
364 ns
= socket(AF_INET
, SOCK_STREAM
, 0);
365 IN_SET_LOOPBACK_ADDR(&ns_sin
);
366 ns_sin
.sin_port
= htons(NAMESERVER_PORT
);
367 if (connect(ns
, (struct sockaddr
*) &ns_sin
,
368 sizeof ns_sin
) == -1) {
384 #endif /* SUNW_CONFCHECK */