1*0a6a1f1dSLionel Sambuc /* $NetBSD: res_mkquery.c,v 1.15 2015/02/24 17:56:20 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Portions Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
52fe8fb19SBen Gras * Portions Copyright (C) 1996, 1997, 1988, 1999, 2001, 2003 Internet Software Consortium.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Permission to use, copy, modify, and/or distribute this software for any
82fe8fb19SBen Gras * purpose with or without fee is hereby granted, provided that the above
92fe8fb19SBen Gras * copyright notice and this permission notice appear in all copies.
102fe8fb19SBen Gras *
112fe8fb19SBen Gras * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
122fe8fb19SBen Gras * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
132fe8fb19SBen Gras * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
142fe8fb19SBen Gras * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
152fe8fb19SBen Gras * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
162fe8fb19SBen Gras * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
172fe8fb19SBen Gras * PERFORMANCE OF THIS SOFTWARE.
182fe8fb19SBen Gras */
192fe8fb19SBen Gras
202fe8fb19SBen Gras /*
212fe8fb19SBen Gras * Copyright (c) 1985, 1993
222fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
232fe8fb19SBen Gras *
242fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
252fe8fb19SBen Gras * modification, are permitted provided that the following conditions
262fe8fb19SBen Gras * are met:
272fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
282fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
292fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
302fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
312fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
32*0a6a1f1dSLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
332fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
342fe8fb19SBen Gras * without specific prior written permission.
352fe8fb19SBen Gras *
362fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
372fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
382fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
392fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
402fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
412fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
422fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
432fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
442fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
452fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
462fe8fb19SBen Gras * SUCH DAMAGE.
472fe8fb19SBen Gras */
482fe8fb19SBen Gras
492fe8fb19SBen Gras /*
502fe8fb19SBen Gras * Portions Copyright (c) 1993 by Digital Equipment Corporation.
512fe8fb19SBen Gras *
522fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this software for any
532fe8fb19SBen Gras * purpose with or without fee is hereby granted, provided that the above
542fe8fb19SBen Gras * copyright notice and this permission notice appear in all copies, and that
552fe8fb19SBen Gras * the name of Digital Equipment Corporation not be used in advertising or
562fe8fb19SBen Gras * publicity pertaining to distribution of the document or software without
572fe8fb19SBen Gras * specific, written prior permission.
582fe8fb19SBen Gras *
592fe8fb19SBen Gras * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
602fe8fb19SBen Gras * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
612fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
622fe8fb19SBen Gras * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
632fe8fb19SBen Gras * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
642fe8fb19SBen Gras * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
652fe8fb19SBen Gras * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
662fe8fb19SBen Gras * SOFTWARE.
672fe8fb19SBen Gras */
682fe8fb19SBen Gras
692fe8fb19SBen Gras #include <sys/cdefs.h>
702fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
712fe8fb19SBen Gras #ifdef notdef
722fe8fb19SBen Gras static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
732fe8fb19SBen Gras static const char rcsid[] = "Id: res_mkquery.c,v 1.10 2008/12/11 09:59:00 marka Exp";
742fe8fb19SBen Gras #else
75*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: res_mkquery.c,v 1.15 2015/02/24 17:56:20 christos Exp $");
762fe8fb19SBen Gras #endif
772fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
782fe8fb19SBen Gras
792fe8fb19SBen Gras #include "port_before.h"
802fe8fb19SBen Gras
812fe8fb19SBen Gras #include "namespace.h"
822fe8fb19SBen Gras #include <sys/types.h>
832fe8fb19SBen Gras #include <sys/param.h>
842fe8fb19SBen Gras #include <netinet/in.h>
852fe8fb19SBen Gras #include <arpa/nameser.h>
86f14fb602SLionel Sambuc #include <assert.h>
872fe8fb19SBen Gras #include <netdb.h>
882fe8fb19SBen Gras #include <resolv.h>
892fe8fb19SBen Gras #include <stdio.h>
902fe8fb19SBen Gras #include <string.h>
912fe8fb19SBen Gras #include "port_after.h"
922fe8fb19SBen Gras
932fe8fb19SBen Gras #if 0
942fe8fb19SBen Gras #ifdef __weak_alias
952fe8fb19SBen Gras __weak_alias(res_nmkquery,_res_nmkquery)
962fe8fb19SBen Gras __weak_alias(res_nopt,_res_nopt)
972fe8fb19SBen Gras #endif
982fe8fb19SBen Gras #endif
992fe8fb19SBen Gras
1002fe8fb19SBen Gras /* Options. Leave them on. */
1012fe8fb19SBen Gras #ifndef DEBUG
1022fe8fb19SBen Gras #define DEBUG
1032fe8fb19SBen Gras #endif
1042fe8fb19SBen Gras
1052fe8fb19SBen Gras extern const char *_res_opcodes[];
1062fe8fb19SBen Gras
1072fe8fb19SBen Gras /*%
1082fe8fb19SBen Gras * Form all types of queries.
1092fe8fb19SBen Gras * Returns the size of the result or -1.
1102fe8fb19SBen Gras */
1112fe8fb19SBen Gras int
res_nmkquery(res_state statp,int op,const char * dname,int class,int type,const u_char * data,int datalen,const u_char * newrr_in,u_char * buf,int buflen)1122fe8fb19SBen Gras res_nmkquery(res_state statp,
1132fe8fb19SBen Gras int op, /*!< opcode of query */
1142fe8fb19SBen Gras const char *dname, /*!< domain name */
1152fe8fb19SBen Gras int class, int type, /*!< class and type of query */
1162fe8fb19SBen Gras const u_char *data, /*!< resource record data */
1172fe8fb19SBen Gras int datalen, /*!< length of data */
1182fe8fb19SBen Gras const u_char *newrr_in, /*!< new rr for modify or append */
1192fe8fb19SBen Gras u_char *buf, /*!< buffer to put query */
1202fe8fb19SBen Gras int buflen) /*!< size of buffer */
1212fe8fb19SBen Gras {
1222fe8fb19SBen Gras register HEADER *hp;
1232fe8fb19SBen Gras register u_char *cp, *ep;
1242fe8fb19SBen Gras register int n;
1252fe8fb19SBen Gras u_char *dnptrs[20], **dpp, **lastdnptr;
1262fe8fb19SBen Gras
1272fe8fb19SBen Gras UNUSED(newrr_in);
1282fe8fb19SBen Gras
1292fe8fb19SBen Gras #ifdef DEBUG
1302fe8fb19SBen Gras if (statp->options & RES_DEBUG)
1312fe8fb19SBen Gras printf(";; res_nmkquery(%s, %s, %s, %s)\n",
1322fe8fb19SBen Gras _res_opcodes[op], dname, p_class(class), p_type(type));
1332fe8fb19SBen Gras #endif
1342fe8fb19SBen Gras /*
1352fe8fb19SBen Gras * Initialize header fields.
1362fe8fb19SBen Gras */
1372fe8fb19SBen Gras if ((buf == NULL) || (buflen < HFIXEDSZ))
1382fe8fb19SBen Gras return (-1);
1392fe8fb19SBen Gras memset(buf, 0, HFIXEDSZ);
1402fe8fb19SBen Gras hp = (HEADER *)(void *)buf;
1412fe8fb19SBen Gras statp->id = res_nrandomid(statp);
1422fe8fb19SBen Gras hp->id = htons(statp->id);
1432fe8fb19SBen Gras hp->opcode = op;
1442fe8fb19SBen Gras hp->rd = (statp->options & RES_RECURSE) != 0U;
1452fe8fb19SBen Gras hp->rcode = NOERROR;
1462fe8fb19SBen Gras cp = buf + HFIXEDSZ;
1472fe8fb19SBen Gras ep = buf + buflen;
1482fe8fb19SBen Gras dpp = dnptrs;
1492fe8fb19SBen Gras *dpp++ = buf;
1502fe8fb19SBen Gras *dpp++ = NULL;
1512fe8fb19SBen Gras lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1522fe8fb19SBen Gras /*
1532fe8fb19SBen Gras * perform opcode specific processing
1542fe8fb19SBen Gras */
1552fe8fb19SBen Gras switch (op) {
1562fe8fb19SBen Gras case QUERY: /*FALLTHROUGH*/
1572fe8fb19SBen Gras case NS_NOTIFY_OP:
1582fe8fb19SBen Gras if (ep - cp < QFIXEDSZ)
1592fe8fb19SBen Gras return (-1);
160f14fb602SLionel Sambuc if ((n = dn_comp(dname, cp, (int)(ep - cp - QFIXEDSZ), dnptrs,
1612fe8fb19SBen Gras lastdnptr)) < 0)
1622fe8fb19SBen Gras return (-1);
1632fe8fb19SBen Gras cp += n;
1642fe8fb19SBen Gras ns_put16(type, cp);
1652fe8fb19SBen Gras cp += INT16SZ;
1662fe8fb19SBen Gras ns_put16(class, cp);
1672fe8fb19SBen Gras cp += INT16SZ;
1682fe8fb19SBen Gras hp->qdcount = htons(1);
1692fe8fb19SBen Gras if (op == QUERY || data == NULL)
1702fe8fb19SBen Gras break;
1712fe8fb19SBen Gras /*
1722fe8fb19SBen Gras * Make an additional record for completion domain.
1732fe8fb19SBen Gras */
1742fe8fb19SBen Gras if ((ep - cp) < RRFIXEDSZ)
1752fe8fb19SBen Gras return (-1);
176f14fb602SLionel Sambuc n = dn_comp((const char *)data, cp, (int)(ep - cp - RRFIXEDSZ),
1772fe8fb19SBen Gras dnptrs, lastdnptr);
1782fe8fb19SBen Gras if (n < 0)
1792fe8fb19SBen Gras return (-1);
1802fe8fb19SBen Gras cp += n;
1812fe8fb19SBen Gras ns_put16(T_NULL, cp);
1822fe8fb19SBen Gras cp += INT16SZ;
1832fe8fb19SBen Gras ns_put16(class, cp);
1842fe8fb19SBen Gras cp += INT16SZ;
1852fe8fb19SBen Gras ns_put32(0, cp);
1862fe8fb19SBen Gras cp += INT32SZ;
1872fe8fb19SBen Gras ns_put16(0, cp);
1882fe8fb19SBen Gras cp += INT16SZ;
1892fe8fb19SBen Gras hp->arcount = htons(1);
1902fe8fb19SBen Gras break;
1912fe8fb19SBen Gras
1922fe8fb19SBen Gras case IQUERY:
1932fe8fb19SBen Gras /*
1942fe8fb19SBen Gras * Initialize answer section
1952fe8fb19SBen Gras */
1962fe8fb19SBen Gras if (ep - cp < 1 + RRFIXEDSZ + datalen)
1972fe8fb19SBen Gras return (-1);
1982fe8fb19SBen Gras *cp++ = '\0'; /*%< no domain name */
1992fe8fb19SBen Gras ns_put16(type, cp);
2002fe8fb19SBen Gras cp += INT16SZ;
2012fe8fb19SBen Gras ns_put16(class, cp);
2022fe8fb19SBen Gras cp += INT16SZ;
2032fe8fb19SBen Gras ns_put32(0, cp);
2042fe8fb19SBen Gras cp += INT32SZ;
2052fe8fb19SBen Gras ns_put16(datalen, cp);
2062fe8fb19SBen Gras cp += INT16SZ;
2072fe8fb19SBen Gras if (datalen) {
2082fe8fb19SBen Gras memcpy(cp, data, (size_t)datalen);
2092fe8fb19SBen Gras cp += datalen;
2102fe8fb19SBen Gras }
2112fe8fb19SBen Gras hp->ancount = htons(1);
2122fe8fb19SBen Gras break;
2132fe8fb19SBen Gras
2142fe8fb19SBen Gras default:
2152fe8fb19SBen Gras return (-1);
2162fe8fb19SBen Gras }
217f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, cp - buf));
218f14fb602SLionel Sambuc return (int)(cp - buf);
2192fe8fb19SBen Gras }
2202fe8fb19SBen Gras
2212fe8fb19SBen Gras #ifdef RES_USE_EDNS0
2222fe8fb19SBen Gras /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
2232fe8fb19SBen Gras
2242fe8fb19SBen Gras int
res_nopt(res_state statp,int n0,u_char * buf,int buflen,int anslen)2252fe8fb19SBen Gras res_nopt(res_state statp,
2262fe8fb19SBen Gras int n0, /*%< current offset in buffer */
2272fe8fb19SBen Gras u_char *buf, /*%< buffer to put query */
2282fe8fb19SBen Gras int buflen, /*%< size of buffer */
2292fe8fb19SBen Gras int anslen) /*%< UDP answer buffer size */
2302fe8fb19SBen Gras {
2312fe8fb19SBen Gras register HEADER *hp;
2322fe8fb19SBen Gras register u_char *cp, *ep;
2332fe8fb19SBen Gras u_int16_t flags = 0;
2342fe8fb19SBen Gras
2352fe8fb19SBen Gras #ifdef DEBUG
2362fe8fb19SBen Gras if ((statp->options & RES_DEBUG) != 0U)
2372fe8fb19SBen Gras printf(";; res_nopt()\n");
2382fe8fb19SBen Gras #endif
2392fe8fb19SBen Gras
2402fe8fb19SBen Gras hp = (HEADER *)(void *)buf;
2412fe8fb19SBen Gras cp = buf + n0;
2422fe8fb19SBen Gras ep = buf + buflen;
2432fe8fb19SBen Gras
2442fe8fb19SBen Gras if ((ep - cp) < 1 + RRFIXEDSZ)
2452fe8fb19SBen Gras return (-1);
2462fe8fb19SBen Gras
2472fe8fb19SBen Gras *cp++ = 0; /*%< "." */
2482fe8fb19SBen Gras ns_put16(ns_t_opt, cp); /*%< TYPE */
2492fe8fb19SBen Gras cp += INT16SZ;
250*0a6a1f1dSLionel Sambuc if (anslen > 0xffff)
251*0a6a1f1dSLionel Sambuc anslen = 0xffff;
252*0a6a1f1dSLionel Sambuc ns_put16(anslen, cp); /*%< CLASS = UDP payload size */
2532fe8fb19SBen Gras cp += INT16SZ;
2542fe8fb19SBen Gras *cp++ = NOERROR; /*%< extended RCODE */
2552fe8fb19SBen Gras *cp++ = 0; /*%< EDNS version */
2562fe8fb19SBen Gras
2572fe8fb19SBen Gras if (statp->options & RES_USE_DNSSEC) {
2582fe8fb19SBen Gras #ifdef DEBUG
2592fe8fb19SBen Gras if (statp->options & RES_DEBUG)
2602fe8fb19SBen Gras printf(";; res_opt()... ENDS0 DNSSEC\n");
2612fe8fb19SBen Gras #endif
2622fe8fb19SBen Gras flags |= NS_OPT_DNSSEC_OK;
2632fe8fb19SBen Gras }
2642fe8fb19SBen Gras ns_put16(flags, cp);
2652fe8fb19SBen Gras cp += INT16SZ;
2662fe8fb19SBen Gras
2672fe8fb19SBen Gras ns_put16(0U, cp); /*%< RDLEN */
2682fe8fb19SBen Gras cp += INT16SZ;
2692fe8fb19SBen Gras
2702fe8fb19SBen Gras hp->arcount = htons(ntohs(hp->arcount) + 1);
2712fe8fb19SBen Gras
272f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, cp - buf));
273f14fb602SLionel Sambuc return (int)(cp - buf);
2742fe8fb19SBen Gras }
2752fe8fb19SBen Gras
2762fe8fb19SBen Gras /*
2772fe8fb19SBen Gras * Construct variable data (RDATA) block for OPT psuedo-RR, append it
2782fe8fb19SBen Gras * to the buffer, then update the RDLEN field (previously set to zero by
2792fe8fb19SBen Gras * res_nopt()) with the new RDATA length.
2802fe8fb19SBen Gras */
2812fe8fb19SBen Gras int
res_nopt_rdata(res_state statp,int n0,u_char * buf,int buflen,u_char * rdata,u_short code,u_short len,u_char * data)2822fe8fb19SBen Gras res_nopt_rdata(res_state statp,
2832fe8fb19SBen Gras int n0, /*%< current offset in buffer */
2842fe8fb19SBen Gras u_char *buf, /*%< buffer to put query */
2852fe8fb19SBen Gras int buflen, /*%< size of buffer */
2862fe8fb19SBen Gras u_char *rdata, /*%< ptr to start of opt rdata */
2872fe8fb19SBen Gras u_short code, /*%< OPTION-CODE */
2882fe8fb19SBen Gras u_short len, /*%< OPTION-LENGTH */
2892fe8fb19SBen Gras u_char *data) /*%< OPTION_DATA */
2902fe8fb19SBen Gras {
2912fe8fb19SBen Gras register u_char *cp, *ep;
2922fe8fb19SBen Gras
2932fe8fb19SBen Gras #ifdef DEBUG
2942fe8fb19SBen Gras if ((statp->options & RES_DEBUG) != 0U)
2952fe8fb19SBen Gras printf(";; res_nopt_rdata()\n");
2962fe8fb19SBen Gras #endif
2972fe8fb19SBen Gras
2982fe8fb19SBen Gras cp = buf + n0;
2992fe8fb19SBen Gras ep = buf + buflen;
3002fe8fb19SBen Gras
3012fe8fb19SBen Gras if ((ep - cp) < (4 + len))
3022fe8fb19SBen Gras return (-1);
3032fe8fb19SBen Gras
3042fe8fb19SBen Gras if (rdata < (buf + 2) || rdata >= ep)
3052fe8fb19SBen Gras return (-1);
3062fe8fb19SBen Gras
3072fe8fb19SBen Gras ns_put16(code, cp);
3082fe8fb19SBen Gras cp += INT16SZ;
3092fe8fb19SBen Gras
3102fe8fb19SBen Gras ns_put16(len, cp);
3112fe8fb19SBen Gras cp += INT16SZ;
3122fe8fb19SBen Gras
3132fe8fb19SBen Gras memcpy(cp, data, (size_t)len);
3142fe8fb19SBen Gras cp += len;
3152fe8fb19SBen Gras
316f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(u_short, cp - rdata));
317f14fb602SLionel Sambuc len = (u_short)(cp - rdata);
3182fe8fb19SBen Gras ns_put16(len, rdata - 2); /* Update RDLEN field */
3192fe8fb19SBen Gras
320f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, cp - buf));
321f14fb602SLionel Sambuc return (int)(cp - buf);
3222fe8fb19SBen Gras }
3232fe8fb19SBen Gras #endif
3242fe8fb19SBen Gras
3252fe8fb19SBen Gras /*! \file */
326