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