Home | History | Annotate | Line # | Download | only in resolv
res_mkquery.c revision 1.10.6.2
      1  1.10.6.2  christos /*	$NetBSD: res_mkquery.c,v 1.10.6.2 2008/06/21 23:37:54 christos Exp $	*/
      2  1.10.6.2  christos 
      3  1.10.6.2  christos /*
      4  1.10.6.2  christos  * Copyright (c) 1985, 1993
      5  1.10.6.2  christos  *    The Regents of the University of California.  All rights reserved.
      6  1.10.6.2  christos  *
      7  1.10.6.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.10.6.2  christos  * modification, are permitted provided that the following conditions
      9  1.10.6.2  christos  * are met:
     10  1.10.6.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.10.6.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.10.6.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.10.6.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.10.6.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.10.6.2  christos  * 3. All advertising materials mentioning features or use of this software
     16  1.10.6.2  christos  *    must display the following acknowledgement:
     17  1.10.6.2  christos  * 	This product includes software developed by the University of
     18  1.10.6.2  christos  * 	California, Berkeley and its contributors.
     19  1.10.6.2  christos  * 4. Neither the name of the University nor the names of its contributors
     20  1.10.6.2  christos  *    may be used to endorse or promote products derived from this software
     21  1.10.6.2  christos  *    without specific prior written permission.
     22  1.10.6.2  christos  *
     23  1.10.6.2  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.10.6.2  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.10.6.2  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.10.6.2  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.10.6.2  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.10.6.2  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.10.6.2  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.10.6.2  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.10.6.2  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.10.6.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.10.6.2  christos  * SUCH DAMAGE.
     34  1.10.6.2  christos  */
     35  1.10.6.2  christos 
     36  1.10.6.2  christos /*
     37  1.10.6.2  christos  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
     38  1.10.6.2  christos  *
     39  1.10.6.2  christos  * Permission to use, copy, modify, and distribute this software for any
     40  1.10.6.2  christos  * purpose with or without fee is hereby granted, provided that the above
     41  1.10.6.2  christos  * copyright notice and this permission notice appear in all copies, and that
     42  1.10.6.2  christos  * the name of Digital Equipment Corporation not be used in advertising or
     43  1.10.6.2  christos  * publicity pertaining to distribution of the document or software without
     44  1.10.6.2  christos  * specific, written prior permission.
     45  1.10.6.2  christos  *
     46  1.10.6.2  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
     47  1.10.6.2  christos  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
     48  1.10.6.2  christos  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
     49  1.10.6.2  christos  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     50  1.10.6.2  christos  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     51  1.10.6.2  christos  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     52  1.10.6.2  christos  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     53  1.10.6.2  christos  * SOFTWARE.
     54  1.10.6.2  christos  */
     55  1.10.6.2  christos 
     56  1.10.6.2  christos /*
     57  1.10.6.2  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     58  1.10.6.2  christos  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
     59  1.10.6.2  christos  *
     60  1.10.6.2  christos  * Permission to use, copy, modify, and distribute this software for any
     61  1.10.6.2  christos  * purpose with or without fee is hereby granted, provided that the above
     62  1.10.6.2  christos  * copyright notice and this permission notice appear in all copies.
     63  1.10.6.2  christos  *
     64  1.10.6.2  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     65  1.10.6.2  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     66  1.10.6.2  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     67  1.10.6.2  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     68  1.10.6.2  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     69  1.10.6.2  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     70  1.10.6.2  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     71  1.10.6.2  christos  */
     72  1.10.6.2  christos 
     73  1.10.6.2  christos #include <sys/cdefs.h>
     74  1.10.6.2  christos #if defined(LIBC_SCCS) && !defined(lint)
     75  1.10.6.2  christos #ifdef notdef
     76  1.10.6.2  christos static const char sccsid[] = "@(#)res_mkquery.c	8.1 (Berkeley) 6/4/93";
     77  1.10.6.2  christos static const char rcsid[] = "Id: res_mkquery.c,v 1.6.672.1 2008/04/03 02:12:21 marka Exp";
     78  1.10.6.2  christos #else
     79  1.10.6.2  christos __RCSID("$NetBSD: res_mkquery.c,v 1.10.6.2 2008/06/21 23:37:54 christos Exp $");
     80  1.10.6.2  christos #endif
     81  1.10.6.2  christos #endif /* LIBC_SCCS and not lint */
     82  1.10.6.2  christos 
     83  1.10.6.2  christos #include "port_before.h"
     84  1.10.6.2  christos 
     85  1.10.6.2  christos #include "namespace.h"
     86  1.10.6.2  christos #include <sys/types.h>
     87  1.10.6.2  christos #include <sys/param.h>
     88  1.10.6.2  christos #include <netinet/in.h>
     89  1.10.6.2  christos #include <arpa/nameser.h>
     90  1.10.6.2  christos #include <netdb.h>
     91  1.10.6.2  christos #include <resolv.h>
     92  1.10.6.2  christos #include <stdio.h>
     93  1.10.6.2  christos #include <string.h>
     94  1.10.6.2  christos #include "port_after.h"
     95  1.10.6.2  christos 
     96  1.10.6.2  christos #if 0
     97  1.10.6.2  christos #ifdef __weak_alias
     98  1.10.6.2  christos __weak_alias(res_nmkquery,_res_nmkquery)
     99  1.10.6.2  christos __weak_alias(res_nopt,_res_nopt)
    100  1.10.6.2  christos #endif
    101  1.10.6.2  christos #endif
    102  1.10.6.2  christos 
    103  1.10.6.2  christos /* Options.  Leave them on. */
    104  1.10.6.2  christos #ifndef DEBUG
    105  1.10.6.2  christos #define DEBUG
    106  1.10.6.2  christos #endif
    107  1.10.6.2  christos 
    108  1.10.6.2  christos extern const char *_res_opcodes[];
    109  1.10.6.2  christos 
    110  1.10.6.2  christos /*%
    111  1.10.6.2  christos  * Form all types of queries.
    112  1.10.6.2  christos  * Returns the size of the result or -1.
    113  1.10.6.2  christos  */
    114  1.10.6.2  christos int
    115  1.10.6.2  christos res_nmkquery(res_state statp,
    116  1.10.6.2  christos 	     int op,			/*!< opcode of query  */
    117  1.10.6.2  christos 	     const char *dname,		/*!< domain name  */
    118  1.10.6.2  christos 	     int class, int type,	/*!< class and type of query  */
    119  1.10.6.2  christos 	     const u_char *data,	/*!< resource record data  */
    120  1.10.6.2  christos 	     int datalen,		/*!< length of data  */
    121  1.10.6.2  christos 	     const u_char *newrr_in,	/*!< new rr for modify or append  */
    122  1.10.6.2  christos 	     u_char *buf,		/*!< buffer to put query  */
    123  1.10.6.2  christos 	     int buflen)		/*!< size of buffer  */
    124  1.10.6.2  christos {
    125  1.10.6.2  christos 	register HEADER *hp;
    126  1.10.6.2  christos 	register u_char *cp, *ep;
    127  1.10.6.2  christos 	register int n;
    128  1.10.6.2  christos 	u_char *dnptrs[20], **dpp, **lastdnptr;
    129  1.10.6.2  christos 
    130  1.10.6.2  christos 	UNUSED(newrr_in);
    131  1.10.6.2  christos 
    132  1.10.6.2  christos #ifdef DEBUG
    133  1.10.6.2  christos 	if (statp->options & RES_DEBUG)
    134  1.10.6.2  christos 		printf(";; res_nmkquery(%s, %s, %s, %s)\n",
    135  1.10.6.2  christos 		       _res_opcodes[op], dname, p_class(class), p_type(type));
    136  1.10.6.2  christos #endif
    137  1.10.6.2  christos 	/*
    138  1.10.6.2  christos 	 * Initialize header fields.
    139  1.10.6.2  christos 	 */
    140  1.10.6.2  christos 	if ((buf == NULL) || (buflen < HFIXEDSZ))
    141  1.10.6.2  christos 		return (-1);
    142  1.10.6.2  christos 	memset(buf, 0, HFIXEDSZ);
    143  1.10.6.2  christos 	hp = (HEADER *)(void *)buf;
    144  1.10.6.2  christos 	hp->id = htons(++statp->id);
    145  1.10.6.2  christos 	hp->opcode = op;
    146  1.10.6.2  christos 	hp->rd = (statp->options & RES_RECURSE) != 0U;
    147  1.10.6.2  christos 	hp->rcode = NOERROR;
    148  1.10.6.2  christos 	cp = buf + HFIXEDSZ;
    149  1.10.6.2  christos 	ep = buf + buflen;
    150  1.10.6.2  christos 	dpp = dnptrs;
    151  1.10.6.2  christos 	*dpp++ = buf;
    152  1.10.6.2  christos 	*dpp++ = NULL;
    153  1.10.6.2  christos 	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
    154  1.10.6.2  christos 	/*
    155  1.10.6.2  christos 	 * perform opcode specific processing
    156  1.10.6.2  christos 	 */
    157  1.10.6.2  christos 	switch (op) {
    158  1.10.6.2  christos 	case QUERY:	/*FALLTHROUGH*/
    159  1.10.6.2  christos 	case NS_NOTIFY_OP:
    160  1.10.6.2  christos 		if (ep - cp < QFIXEDSZ)
    161  1.10.6.2  christos 			return (-1);
    162  1.10.6.2  christos 		if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
    163  1.10.6.2  christos 		    lastdnptr)) < 0)
    164  1.10.6.2  christos 			return (-1);
    165  1.10.6.2  christos 		cp += n;
    166  1.10.6.2  christos 		ns_put16(type, cp);
    167  1.10.6.2  christos 		cp += INT16SZ;
    168  1.10.6.2  christos 		ns_put16(class, cp);
    169  1.10.6.2  christos 		cp += INT16SZ;
    170  1.10.6.2  christos 		hp->qdcount = htons(1);
    171  1.10.6.2  christos 		if (op == QUERY || data == NULL)
    172  1.10.6.2  christos 			break;
    173  1.10.6.2  christos 		/*
    174  1.10.6.2  christos 		 * Make an additional record for completion domain.
    175  1.10.6.2  christos 		 */
    176  1.10.6.2  christos 		if ((ep - cp) < RRFIXEDSZ)
    177  1.10.6.2  christos 			return (-1);
    178  1.10.6.2  christos 		n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
    179  1.10.6.2  christos 			    dnptrs, lastdnptr);
    180  1.10.6.2  christos 		if (n < 0)
    181  1.10.6.2  christos 			return (-1);
    182  1.10.6.2  christos 		cp += n;
    183  1.10.6.2  christos 		ns_put16(T_NULL, cp);
    184  1.10.6.2  christos 		cp += INT16SZ;
    185  1.10.6.2  christos 		ns_put16(class, cp);
    186  1.10.6.2  christos 		cp += INT16SZ;
    187  1.10.6.2  christos 		ns_put32(0, cp);
    188  1.10.6.2  christos 		cp += INT32SZ;
    189  1.10.6.2  christos 		ns_put16(0, cp);
    190  1.10.6.2  christos 		cp += INT16SZ;
    191  1.10.6.2  christos 		hp->arcount = htons(1);
    192  1.10.6.2  christos 		break;
    193  1.10.6.2  christos 
    194  1.10.6.2  christos 	case IQUERY:
    195  1.10.6.2  christos 		/*
    196  1.10.6.2  christos 		 * Initialize answer section
    197  1.10.6.2  christos 		 */
    198  1.10.6.2  christos 		if (ep - cp < 1 + RRFIXEDSZ + datalen)
    199  1.10.6.2  christos 			return (-1);
    200  1.10.6.2  christos 		*cp++ = '\0';	/*%< no domain name */
    201  1.10.6.2  christos 		ns_put16(type, cp);
    202  1.10.6.2  christos 		cp += INT16SZ;
    203  1.10.6.2  christos 		ns_put16(class, cp);
    204  1.10.6.2  christos 		cp += INT16SZ;
    205  1.10.6.2  christos 		ns_put32(0, cp);
    206  1.10.6.2  christos 		cp += INT32SZ;
    207  1.10.6.2  christos 		ns_put16(datalen, cp);
    208  1.10.6.2  christos 		cp += INT16SZ;
    209  1.10.6.2  christos 		if (datalen) {
    210  1.10.6.2  christos 			memcpy(cp, data, (size_t)datalen);
    211  1.10.6.2  christos 			cp += datalen;
    212  1.10.6.2  christos 		}
    213  1.10.6.2  christos 		hp->ancount = htons(1);
    214  1.10.6.2  christos 		break;
    215  1.10.6.2  christos 
    216  1.10.6.2  christos 	default:
    217  1.10.6.2  christos 		return (-1);
    218  1.10.6.2  christos 	}
    219  1.10.6.2  christos 	return (cp - buf);
    220  1.10.6.2  christos }
    221  1.10.6.2  christos 
    222  1.10.6.2  christos #ifdef RES_USE_EDNS0
    223  1.10.6.2  christos /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
    224  1.10.6.2  christos 
    225  1.10.6.2  christos int
    226  1.10.6.2  christos res_nopt(res_state statp,
    227  1.10.6.2  christos 	 int n0,		/*%< current offset in buffer */
    228  1.10.6.2  christos 	 u_char *buf,		/*%< buffer to put query */
    229  1.10.6.2  christos 	 int buflen,		/*%< size of buffer */
    230  1.10.6.2  christos 	 int anslen)		/*%< UDP answer buffer size */
    231  1.10.6.2  christos {
    232  1.10.6.2  christos 	register HEADER *hp;
    233  1.10.6.2  christos 	register u_char *cp, *ep;
    234  1.10.6.2  christos 	u_int16_t flags = 0;
    235  1.10.6.2  christos 
    236  1.10.6.2  christos #ifdef DEBUG
    237  1.10.6.2  christos 	if ((statp->options & RES_DEBUG) != 0U)
    238  1.10.6.2  christos 		printf(";; res_nopt()\n");
    239  1.10.6.2  christos #endif
    240  1.10.6.2  christos 
    241  1.10.6.2  christos 	hp = (HEADER *)(void *)buf;
    242  1.10.6.2  christos 	cp = buf + n0;
    243  1.10.6.2  christos 	ep = buf + buflen;
    244  1.10.6.2  christos 
    245  1.10.6.2  christos 	if ((ep - cp) < 1 + RRFIXEDSZ)
    246  1.10.6.2  christos 		return (-1);
    247  1.10.6.2  christos 
    248  1.10.6.2  christos 	*cp++ = 0;			/*%< "." */
    249  1.10.6.2  christos 	ns_put16(ns_t_opt, cp);		/*%< TYPE */
    250  1.10.6.2  christos 	cp += INT16SZ;
    251  1.10.6.2  christos 	ns_put16(anslen & 0xffff, cp);	/*%< CLASS = UDP payload size */
    252  1.10.6.2  christos 	cp += INT16SZ;
    253  1.10.6.2  christos 	*cp++ = NOERROR;		/*%< extended RCODE */
    254  1.10.6.2  christos 	*cp++ = 0;			/*%< EDNS version */
    255  1.10.6.2  christos 
    256  1.10.6.2  christos 	if (statp->options & RES_USE_DNSSEC) {
    257  1.10.6.2  christos #ifdef DEBUG
    258  1.10.6.2  christos 		if (statp->options & RES_DEBUG)
    259  1.10.6.2  christos 			printf(";; res_opt()... ENDS0 DNSSEC\n");
    260  1.10.6.2  christos #endif
    261  1.10.6.2  christos 		flags |= NS_OPT_DNSSEC_OK;
    262  1.10.6.2  christos 	}
    263  1.10.6.2  christos 	ns_put16(flags, cp);
    264  1.10.6.2  christos 	cp += INT16SZ;
    265  1.10.6.2  christos 
    266  1.10.6.2  christos 	ns_put16(0U, cp);		/*%< RDLEN */
    267  1.10.6.2  christos 	cp += INT16SZ;
    268  1.10.6.2  christos 
    269  1.10.6.2  christos 	hp->arcount = htons(ntohs(hp->arcount) + 1);
    270  1.10.6.2  christos 
    271  1.10.6.2  christos 	return (cp - buf);
    272  1.10.6.2  christos }
    273  1.10.6.2  christos 
    274  1.10.6.2  christos /*
    275  1.10.6.2  christos  * Construct variable data (RDATA) block for OPT psuedo-RR, append it
    276  1.10.6.2  christos  * to the buffer, then update the RDLEN field (previously set to zero by
    277  1.10.6.2  christos  * res_nopt()) with the new RDATA length.
    278  1.10.6.2  christos  */
    279  1.10.6.2  christos int
    280  1.10.6.2  christos res_nopt_rdata(res_state statp,
    281  1.10.6.2  christos 	  int n0,	 	/*%< current offset in buffer */
    282  1.10.6.2  christos 	  u_char *buf,	 	/*%< buffer to put query */
    283  1.10.6.2  christos 	  int buflen,		/*%< size of buffer */
    284  1.10.6.2  christos 	  u_char *rdata,	/*%< ptr to start of opt rdata */
    285  1.10.6.2  christos 	  u_short code,		/*%< OPTION-CODE */
    286  1.10.6.2  christos 	  u_short len,		/*%< OPTION-LENGTH */
    287  1.10.6.2  christos 	  u_char *data)		/*%< OPTION_DATA */
    288  1.10.6.2  christos {
    289  1.10.6.2  christos 	register u_char *cp, *ep;
    290  1.10.6.2  christos 
    291  1.10.6.2  christos #ifdef DEBUG
    292  1.10.6.2  christos 	if ((statp->options & RES_DEBUG) != 0U)
    293  1.10.6.2  christos 		printf(";; res_nopt_rdata()\n");
    294  1.10.6.2  christos #endif
    295  1.10.6.2  christos 
    296  1.10.6.2  christos 	cp = buf + n0;
    297  1.10.6.2  christos 	ep = buf + buflen;
    298  1.10.6.2  christos 
    299  1.10.6.2  christos 	if ((ep - cp) < (4 + len))
    300  1.10.6.2  christos 		return (-1);
    301  1.10.6.2  christos 
    302  1.10.6.2  christos 	if (rdata < (buf + 2) || rdata >= ep)
    303  1.10.6.2  christos 		return (-1);
    304  1.10.6.2  christos 
    305  1.10.6.2  christos 	ns_put16(code, cp);
    306  1.10.6.2  christos 	cp += INT16SZ;
    307  1.10.6.2  christos 
    308  1.10.6.2  christos 	ns_put16(len, cp);
    309  1.10.6.2  christos 	cp += INT16SZ;
    310  1.10.6.2  christos 
    311  1.10.6.2  christos 	(void)memcpy(cp, data, (size_t)len);
    312  1.10.6.2  christos 	cp += len;
    313  1.10.6.2  christos 
    314  1.10.6.2  christos 	len = cp - rdata;
    315  1.10.6.2  christos 	ns_put16(len, rdata - 2);	/* Update RDLEN field */
    316  1.10.6.2  christos 
    317  1.10.6.2  christos 	return (cp - buf);
    318  1.10.6.2  christos }
    319  1.10.6.2  christos #endif
    320  1.10.6.2  christos 
    321  1.10.6.2  christos /*! \file */
    322