1*2219b53cSchristos /* $NetBSD: res_mkquery.c,v 1.16 2017/09/28 23:32:01 christos Exp $ */
259a755a4Schristos
359a755a4Schristos /*
459a755a4Schristos * Portions Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
559a755a4Schristos * Portions Copyright (C) 1996, 1997, 1988, 1999, 2001, 2003 Internet Software Consortium.
659a755a4Schristos *
759a755a4Schristos * Permission to use, copy, modify, and/or distribute this software for any
859a755a4Schristos * purpose with or without fee is hereby granted, provided that the above
959a755a4Schristos * copyright notice and this permission notice appear in all copies.
1059a755a4Schristos *
1159a755a4Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
1259a755a4Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1359a755a4Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
1459a755a4Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1559a755a4Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
1659a755a4Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1759a755a4Schristos * PERFORMANCE OF THIS SOFTWARE.
1859a755a4Schristos */
192b237084Schristos
202b237084Schristos /*
212b237084Schristos * Copyright (c) 1985, 1993
222b237084Schristos * The Regents of the University of California. All rights reserved.
232b237084Schristos *
242b237084Schristos * Redistribution and use in source and binary forms, with or without
252b237084Schristos * modification, are permitted provided that the following conditions
262b237084Schristos * are met:
272b237084Schristos * 1. Redistributions of source code must retain the above copyright
282b237084Schristos * notice, this list of conditions and the following disclaimer.
292b237084Schristos * 2. Redistributions in binary form must reproduce the above copyright
302b237084Schristos * notice, this list of conditions and the following disclaimer in the
312b237084Schristos * documentation and/or other materials provided with the distribution.
3252799bbfSchristos * 3. Neither the name of the University nor the names of its contributors
332b237084Schristos * may be used to endorse or promote products derived from this software
342b237084Schristos * without specific prior written permission.
352b237084Schristos *
362b237084Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
372b237084Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
382b237084Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
392b237084Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
402b237084Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
412b237084Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
422b237084Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
432b237084Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
442b237084Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
452b237084Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
462b237084Schristos * SUCH DAMAGE.
472b237084Schristos */
482b237084Schristos
492b237084Schristos /*
502b237084Schristos * Portions Copyright (c) 1993 by Digital Equipment Corporation.
512b237084Schristos *
522b237084Schristos * Permission to use, copy, modify, and distribute this software for any
532b237084Schristos * purpose with or without fee is hereby granted, provided that the above
542b237084Schristos * copyright notice and this permission notice appear in all copies, and that
552b237084Schristos * the name of Digital Equipment Corporation not be used in advertising or
562b237084Schristos * publicity pertaining to distribution of the document or software without
572b237084Schristos * specific, written prior permission.
582b237084Schristos *
592b237084Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
602b237084Schristos * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
612b237084Schristos * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
622b237084Schristos * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
632b237084Schristos * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
642b237084Schristos * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
652b237084Schristos * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
662b237084Schristos * SOFTWARE.
672b237084Schristos */
682b237084Schristos
69ca31adbdSchristos #include <sys/cdefs.h>
702b237084Schristos #if defined(LIBC_SCCS) && !defined(lint)
71ca31adbdSchristos #ifdef notdef
722b237084Schristos static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
7359a755a4Schristos static const char rcsid[] = "Id: res_mkquery.c,v 1.10 2008/12/11 09:59:00 marka Exp";
74ca31adbdSchristos #else
75*2219b53cSchristos __RCSID("$NetBSD: res_mkquery.c,v 1.16 2017/09/28 23:32:01 christos Exp $");
76ca31adbdSchristos #endif
772b237084Schristos #endif /* LIBC_SCCS and not lint */
782b237084Schristos
792b237084Schristos #include "port_before.h"
806f3786f4Schristos
816f3786f4Schristos #include "namespace.h"
822b237084Schristos #include <sys/types.h>
832b237084Schristos #include <sys/param.h>
842b237084Schristos #include <netinet/in.h>
852b237084Schristos #include <arpa/nameser.h>
86e826745eSchristos #include <assert.h>
872b237084Schristos #include <netdb.h>
882b237084Schristos #include <resolv.h>
892b237084Schristos #include <stdio.h>
902b237084Schristos #include <string.h>
912b237084Schristos #include "port_after.h"
922b237084Schristos
935ce4c264Schristos #if 0
946f3786f4Schristos #ifdef __weak_alias
956f3786f4Schristos __weak_alias(res_nmkquery,_res_nmkquery)
966f3786f4Schristos __weak_alias(res_nopt,_res_nopt)
976f3786f4Schristos #endif
985ce4c264Schristos #endif
996f3786f4Schristos
1002b237084Schristos /* Options. Leave them on. */
1017ce5e09dSchristos #ifndef DEBUG
1022b237084Schristos #define DEBUG
1037ce5e09dSchristos #endif
1042b237084Schristos
1052b237084Schristos extern const char *_res_opcodes[];
1062b237084Schristos
107d73eb73dSchristos /*%
1082b237084Schristos * Form all types of queries.
1092b237084Schristos * Returns the size of the result or -1.
1102b237084Schristos */
1112b237084Schristos 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)1122b237084Schristos res_nmkquery(res_state statp,
113d73eb73dSchristos int op, /*!< opcode of query */
114d73eb73dSchristos const char *dname, /*!< domain name */
115d73eb73dSchristos int class, int type, /*!< class and type of query */
116d73eb73dSchristos const u_char *data, /*!< resource record data */
117d73eb73dSchristos int datalen, /*!< length of data */
118d73eb73dSchristos const u_char *newrr_in, /*!< new rr for modify or append */
119d73eb73dSchristos u_char *buf, /*!< buffer to put query */
120d73eb73dSchristos int buflen) /*!< size of buffer */
1212b237084Schristos {
1222b237084Schristos register HEADER *hp;
1232b237084Schristos register u_char *cp, *ep;
1242b237084Schristos register int n;
1252b237084Schristos u_char *dnptrs[20], **dpp, **lastdnptr;
1262b237084Schristos
1272b237084Schristos UNUSED(newrr_in);
1282b237084Schristos
1292b237084Schristos #ifdef DEBUG
1302b237084Schristos if (statp->options & RES_DEBUG)
1312b237084Schristos printf(";; res_nmkquery(%s, %s, %s, %s)\n",
1322b237084Schristos _res_opcodes[op], dname, p_class(class), p_type(type));
1332b237084Schristos #endif
1342b237084Schristos /*
1352b237084Schristos * Initialize header fields.
1362b237084Schristos */
1372b237084Schristos if ((buf == NULL) || (buflen < HFIXEDSZ))
1382b237084Schristos return (-1);
1392b237084Schristos memset(buf, 0, HFIXEDSZ);
140f5b0f7dfSchristos hp = (HEADER *)(void *)buf;
14159a755a4Schristos statp->id = res_nrandomid(statp);
14259a755a4Schristos hp->id = htons(statp->id);
1432b237084Schristos hp->opcode = op;
1442b237084Schristos hp->rd = (statp->options & RES_RECURSE) != 0U;
145*2219b53cSchristos hp->ad = (statp->options & RES_USE_DNSSEC) != 0U;
1462b237084Schristos hp->rcode = NOERROR;
1472b237084Schristos cp = buf + HFIXEDSZ;
1482b237084Schristos ep = buf + buflen;
1492b237084Schristos dpp = dnptrs;
1502b237084Schristos *dpp++ = buf;
1512b237084Schristos *dpp++ = NULL;
1522b237084Schristos lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1532b237084Schristos /*
1542b237084Schristos * perform opcode specific processing
1552b237084Schristos */
1562b237084Schristos switch (op) {
1572b237084Schristos case QUERY: /*FALLTHROUGH*/
1582b237084Schristos case NS_NOTIFY_OP:
1592b237084Schristos if (ep - cp < QFIXEDSZ)
1602b237084Schristos return (-1);
161e826745eSchristos if ((n = dn_comp(dname, cp, (int)(ep - cp - QFIXEDSZ), dnptrs,
1622b237084Schristos lastdnptr)) < 0)
1632b237084Schristos return (-1);
1642b237084Schristos cp += n;
1652b237084Schristos ns_put16(type, cp);
1662b237084Schristos cp += INT16SZ;
1672b237084Schristos ns_put16(class, cp);
1682b237084Schristos cp += INT16SZ;
1692b237084Schristos hp->qdcount = htons(1);
1702b237084Schristos if (op == QUERY || data == NULL)
1712b237084Schristos break;
1722b237084Schristos /*
1732b237084Schristos * Make an additional record for completion domain.
1742b237084Schristos */
1752b237084Schristos if ((ep - cp) < RRFIXEDSZ)
1762b237084Schristos return (-1);
177e826745eSchristos n = dn_comp((const char *)data, cp, (int)(ep - cp - RRFIXEDSZ),
1782b237084Schristos dnptrs, lastdnptr);
1792b237084Schristos if (n < 0)
1802b237084Schristos return (-1);
1812b237084Schristos cp += n;
1822b237084Schristos ns_put16(T_NULL, cp);
1832b237084Schristos cp += INT16SZ;
1842b237084Schristos ns_put16(class, cp);
1852b237084Schristos cp += INT16SZ;
1862b237084Schristos ns_put32(0, cp);
1872b237084Schristos cp += INT32SZ;
1882b237084Schristos ns_put16(0, cp);
1892b237084Schristos cp += INT16SZ;
1902b237084Schristos hp->arcount = htons(1);
1912b237084Schristos break;
1922b237084Schristos
1932b237084Schristos case IQUERY:
1942b237084Schristos /*
1952b237084Schristos * Initialize answer section
1962b237084Schristos */
1972b237084Schristos if (ep - cp < 1 + RRFIXEDSZ + datalen)
1982b237084Schristos return (-1);
199d73eb73dSchristos *cp++ = '\0'; /*%< no domain name */
2002b237084Schristos ns_put16(type, cp);
2012b237084Schristos cp += INT16SZ;
2022b237084Schristos ns_put16(class, cp);
2032b237084Schristos cp += INT16SZ;
2042b237084Schristos ns_put32(0, cp);
2052b237084Schristos cp += INT32SZ;
2062b237084Schristos ns_put16(datalen, cp);
2072b237084Schristos cp += INT16SZ;
2082b237084Schristos if (datalen) {
209f5b0f7dfSchristos memcpy(cp, data, (size_t)datalen);
2102b237084Schristos cp += datalen;
2112b237084Schristos }
2122b237084Schristos hp->ancount = htons(1);
2132b237084Schristos break;
2142b237084Schristos
2152b237084Schristos default:
2162b237084Schristos return (-1);
2172b237084Schristos }
218e826745eSchristos _DIAGASSERT(__type_fit(int, cp - buf));
219e826745eSchristos return (int)(cp - buf);
2202b237084Schristos }
2212b237084Schristos
2222b237084Schristos #ifdef RES_USE_EDNS0
2232b237084Schristos /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
2242b237084Schristos
2252b237084Schristos int
res_nopt(res_state statp,int n0,u_char * buf,int buflen,int anslen)2262b237084Schristos res_nopt(res_state statp,
227d73eb73dSchristos int n0, /*%< current offset in buffer */
228d73eb73dSchristos u_char *buf, /*%< buffer to put query */
229d73eb73dSchristos int buflen, /*%< size of buffer */
230d73eb73dSchristos int anslen) /*%< UDP answer buffer size */
2312b237084Schristos {
2322b237084Schristos register HEADER *hp;
2332b237084Schristos register u_char *cp, *ep;
2342b237084Schristos u_int16_t flags = 0;
2352b237084Schristos
2362b237084Schristos #ifdef DEBUG
2372b237084Schristos if ((statp->options & RES_DEBUG) != 0U)
2382b237084Schristos printf(";; res_nopt()\n");
2392b237084Schristos #endif
2402b237084Schristos
241f5b0f7dfSchristos hp = (HEADER *)(void *)buf;
2422b237084Schristos cp = buf + n0;
2432b237084Schristos ep = buf + buflen;
2442b237084Schristos
2452b237084Schristos if ((ep - cp) < 1 + RRFIXEDSZ)
2462b237084Schristos return (-1);
2472b237084Schristos
248d73eb73dSchristos *cp++ = 0; /*%< "." */
2493873655bSchristos ns_put16(ns_t_opt, cp); /*%< TYPE */
2502b237084Schristos cp += INT16SZ;
251abcfa452Schristos if (anslen > 0xffff)
252abcfa452Schristos anslen = 0xffff;
253abcfa452Schristos ns_put16(anslen, cp); /*%< CLASS = UDP payload size */
2542b237084Schristos cp += INT16SZ;
255d73eb73dSchristos *cp++ = NOERROR; /*%< extended RCODE */
256d73eb73dSchristos *cp++ = 0; /*%< EDNS version */
2573873655bSchristos
2582b237084Schristos if (statp->options & RES_USE_DNSSEC) {
2592b237084Schristos #ifdef DEBUG
2602b237084Schristos if (statp->options & RES_DEBUG)
2612b237084Schristos printf(";; res_opt()... ENDS0 DNSSEC\n");
2622b237084Schristos #endif
2632b237084Schristos flags |= NS_OPT_DNSSEC_OK;
2642b237084Schristos }
2652b237084Schristos ns_put16(flags, cp);
2662b237084Schristos cp += INT16SZ;
2673873655bSchristos
2683873655bSchristos ns_put16(0U, cp); /*%< RDLEN */
2692b237084Schristos cp += INT16SZ;
2703873655bSchristos
2712b237084Schristos hp->arcount = htons(ntohs(hp->arcount) + 1);
2722b237084Schristos
273e826745eSchristos _DIAGASSERT(__type_fit(int, cp - buf));
274e826745eSchristos return (int)(cp - buf);
2752b237084Schristos }
2763873655bSchristos
2773873655bSchristos /*
2783873655bSchristos * Construct variable data (RDATA) block for OPT psuedo-RR, append it
2793873655bSchristos * to the buffer, then update the RDLEN field (previously set to zero by
2803873655bSchristos * res_nopt()) with the new RDATA length.
2813873655bSchristos */
2823873655bSchristos 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)2833873655bSchristos res_nopt_rdata(res_state statp,
2843873655bSchristos int n0, /*%< current offset in buffer */
2853873655bSchristos u_char *buf, /*%< buffer to put query */
2863873655bSchristos int buflen, /*%< size of buffer */
2873873655bSchristos u_char *rdata, /*%< ptr to start of opt rdata */
2883873655bSchristos u_short code, /*%< OPTION-CODE */
2893873655bSchristos u_short len, /*%< OPTION-LENGTH */
2903873655bSchristos u_char *data) /*%< OPTION_DATA */
2913873655bSchristos {
2923873655bSchristos register u_char *cp, *ep;
2933873655bSchristos
2943873655bSchristos #ifdef DEBUG
2953873655bSchristos if ((statp->options & RES_DEBUG) != 0U)
2963873655bSchristos printf(";; res_nopt_rdata()\n");
2973873655bSchristos #endif
2983873655bSchristos
2993873655bSchristos cp = buf + n0;
3003873655bSchristos ep = buf + buflen;
3013873655bSchristos
3023873655bSchristos if ((ep - cp) < (4 + len))
3033873655bSchristos return (-1);
3043873655bSchristos
3053873655bSchristos if (rdata < (buf + 2) || rdata >= ep)
3063873655bSchristos return (-1);
3073873655bSchristos
3083873655bSchristos ns_put16(code, cp);
3093873655bSchristos cp += INT16SZ;
3103873655bSchristos
3113873655bSchristos ns_put16(len, cp);
3123873655bSchristos cp += INT16SZ;
3133873655bSchristos
31450ce2a6bSchristos memcpy(cp, data, (size_t)len);
3153873655bSchristos cp += len;
3163873655bSchristos
317e826745eSchristos _DIAGASSERT(__type_fit(u_short, cp - rdata));
318e826745eSchristos len = (u_short)(cp - rdata);
3193873655bSchristos ns_put16(len, rdata - 2); /* Update RDLEN field */
3203873655bSchristos
321e826745eSchristos _DIAGASSERT(__type_fit(int, cp - buf));
322e826745eSchristos return (int)(cp - buf);
3233873655bSchristos }
3242b237084Schristos #endif
325d73eb73dSchristos
326d73eb73dSchristos /*! \file */
327