1156952Sume/* 2156952Sume * Copyright (c) 1985, 1993 3156952Sume * The Regents of the University of California. All rights reserved. 4156952Sume * 5156952Sume * Redistribution and use in source and binary forms, with or without 6156952Sume * modification, are permitted provided that the following conditions 7156952Sume * are met: 8156952Sume * 1. Redistributions of source code must retain the above copyright 9156952Sume * notice, this list of conditions and the following disclaimer. 10156952Sume * 2. Redistributions in binary form must reproduce the above copyright 11156952Sume * notice, this list of conditions and the following disclaimer in the 12156952Sume * documentation and/or other materials provided with the distribution. 13156952Sume * 4. Neither the name of the University nor the names of its contributors 14156952Sume * may be used to endorse or promote products derived from this software 15156952Sume * without specific prior written permission. 16156952Sume * 17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27156952Sume * SUCH DAMAGE. 28156952Sume */ 29156952Sume 30156952Sume/* 31156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 32156952Sume * 33156952Sume * Permission to use, copy, modify, and distribute this software for any 34156952Sume * purpose with or without fee is hereby granted, provided that the above 35156952Sume * copyright notice and this permission notice appear in all copies, and that 36156952Sume * the name of Digital Equipment Corporation not be used in advertising or 37156952Sume * publicity pertaining to distribution of the document or software without 38156952Sume * specific, written prior permission. 39156952Sume * 40156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 41156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 42156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 43156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 44156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 45156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 46156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 47156952Sume * SOFTWARE. 48156952Sume */ 49156952Sume 50156952Sume/* 51156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 52156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 53156952Sume * 54156952Sume * Permission to use, copy, modify, and distribute this software for any 55156952Sume * purpose with or without fee is hereby granted, provided that the above 56156952Sume * copyright notice and this permission notice appear in all copies. 57156952Sume * 58156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 59156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 60156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 61156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 62156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 63156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 64156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 65156952Sume */ 66156952Sume 67156952Sume#if defined(LIBC_SCCS) && !defined(lint) 68156952Sumestatic const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; 69186090Sumestatic const char rcsid[] = "$Id: res_mkquery.c,v 1.5.18.2 2008/04/03 23:15:15 marka Exp $"; 70156952Sume#endif /* LIBC_SCCS and not lint */ 71156956Sume#include <sys/cdefs.h> 72156956Sume__FBSDID("$FreeBSD$"); 73156952Sume 74156952Sume#include "port_before.h" 75156952Sume#include <sys/types.h> 76156952Sume#include <sys/param.h> 77156952Sume#include <netinet/in.h> 78156952Sume#include <arpa/nameser.h> 79156952Sume#include <netdb.h> 80156952Sume#include <resolv.h> 81156952Sume#include <stdio.h> 82156952Sume#include <string.h> 83156952Sume#include "port_after.h" 84156952Sume 85156952Sume/* Options. Leave them on. */ 86156952Sume#define DEBUG 87156952Sume 88156952Sumeextern const char *_res_opcodes[]; 89156952Sume 90170244Sume/*% 91156952Sume * Form all types of queries. 92156952Sume * Returns the size of the result or -1. 93156952Sume */ 94156952Sumeint 95156952Sumeres_nmkquery(res_state statp, 96170244Sume int op, /*!< opcode of query */ 97170244Sume const char *dname, /*!< domain name */ 98170244Sume int class, int type, /*!< class and type of query */ 99170244Sume const u_char *data, /*!< resource record data */ 100170244Sume int datalen, /*!< length of data */ 101170244Sume const u_char *newrr_in, /*!< new rr for modify or append */ 102170244Sume u_char *buf, /*!< buffer to put query */ 103170244Sume int buflen) /*!< size of buffer */ 104156952Sume{ 105156956Sume HEADER *hp; 106156956Sume u_char *cp, *ep; 107156956Sume int n; 108156952Sume u_char *dnptrs[20], **dpp, **lastdnptr; 109156952Sume 110156952Sume UNUSED(newrr_in); 111156952Sume 112156952Sume#ifdef DEBUG 113156952Sume if (statp->options & RES_DEBUG) 114156952Sume printf(";; res_nmkquery(%s, %s, %s, %s)\n", 115156952Sume _res_opcodes[op], dname, p_class(class), p_type(type)); 116156952Sume#endif 117156952Sume /* 118156952Sume * Initialize header fields. 119156952Sume */ 120156952Sume if ((buf == NULL) || (buflen < HFIXEDSZ)) 121156952Sume return (-1); 122156952Sume memset(buf, 0, HFIXEDSZ); 123156952Sume hp = (HEADER *) buf; 124156952Sume hp->id = htons(++statp->id); 125156952Sume hp->opcode = op; 126156952Sume hp->rd = (statp->options & RES_RECURSE) != 0U; 127156952Sume hp->rcode = NOERROR; 128156952Sume cp = buf + HFIXEDSZ; 129156952Sume ep = buf + buflen; 130156952Sume dpp = dnptrs; 131156952Sume *dpp++ = buf; 132156952Sume *dpp++ = NULL; 133156952Sume lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; 134156952Sume /* 135156952Sume * perform opcode specific processing 136156952Sume */ 137156952Sume switch (op) { 138156952Sume case QUERY: /*FALLTHROUGH*/ 139156952Sume case NS_NOTIFY_OP: 140156952Sume if (ep - cp < QFIXEDSZ) 141156952Sume return (-1); 142156952Sume if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, 143156952Sume lastdnptr)) < 0) 144156952Sume return (-1); 145156952Sume cp += n; 146156952Sume ns_put16(type, cp); 147156952Sume cp += INT16SZ; 148156952Sume ns_put16(class, cp); 149156952Sume cp += INT16SZ; 150156952Sume hp->qdcount = htons(1); 151156952Sume if (op == QUERY || data == NULL) 152156952Sume break; 153156952Sume /* 154156952Sume * Make an additional record for completion domain. 155156952Sume */ 156156952Sume if ((ep - cp) < RRFIXEDSZ) 157156952Sume return (-1); 158156952Sume n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ, 159156952Sume dnptrs, lastdnptr); 160156952Sume if (n < 0) 161156952Sume return (-1); 162156952Sume cp += n; 163156952Sume ns_put16(T_NULL, cp); 164156952Sume cp += INT16SZ; 165156952Sume ns_put16(class, cp); 166156952Sume cp += INT16SZ; 167156952Sume ns_put32(0, cp); 168156952Sume cp += INT32SZ; 169156952Sume ns_put16(0, cp); 170156952Sume cp += INT16SZ; 171156952Sume hp->arcount = htons(1); 172156952Sume break; 173156952Sume 174156952Sume case IQUERY: 175156952Sume /* 176156952Sume * Initialize answer section 177156952Sume */ 178156952Sume if (ep - cp < 1 + RRFIXEDSZ + datalen) 179156952Sume return (-1); 180170244Sume *cp++ = '\0'; /*%< no domain name */ 181156952Sume ns_put16(type, cp); 182156952Sume cp += INT16SZ; 183156952Sume ns_put16(class, cp); 184156952Sume cp += INT16SZ; 185156952Sume ns_put32(0, cp); 186156952Sume cp += INT32SZ; 187156952Sume ns_put16(datalen, cp); 188156952Sume cp += INT16SZ; 189156952Sume if (datalen) { 190156952Sume memcpy(cp, data, datalen); 191156952Sume cp += datalen; 192156952Sume } 193156952Sume hp->ancount = htons(1); 194156952Sume break; 195156952Sume 196156952Sume default: 197156952Sume return (-1); 198156952Sume } 199156952Sume return (cp - buf); 200156952Sume} 201156952Sume 202156952Sume#ifdef RES_USE_EDNS0 203156952Sume/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ 204156952Sume 205156952Sumeint 206156952Sumeres_nopt(res_state statp, 207170244Sume int n0, /*%< current offset in buffer */ 208170244Sume u_char *buf, /*%< buffer to put query */ 209170244Sume int buflen, /*%< size of buffer */ 210170244Sume int anslen) /*%< UDP answer buffer size */ 211156952Sume{ 212156956Sume HEADER *hp; 213156956Sume u_char *cp, *ep; 214156952Sume u_int16_t flags = 0; 215156952Sume 216156952Sume#ifdef DEBUG 217156952Sume if ((statp->options & RES_DEBUG) != 0U) 218156952Sume printf(";; res_nopt()\n"); 219156952Sume#endif 220156952Sume 221156952Sume hp = (HEADER *) buf; 222156952Sume cp = buf + n0; 223156952Sume ep = buf + buflen; 224156952Sume 225156952Sume if ((ep - cp) < 1 + RRFIXEDSZ) 226156952Sume return (-1); 227156952Sume 228186090Sume *cp++ = 0; /*%< "." */ 229186090Sume ns_put16(ns_t_opt, cp); /*%< TYPE */ 230156952Sume cp += INT16SZ; 231156956Sume if (anslen > 0xffff) 232156956Sume anslen = 0xffff; /* limit to 16bit value */ 233186090Sume ns_put16(anslen & 0xffff, cp); /*%< CLASS = UDP payload size */ 234156952Sume cp += INT16SZ; 235186090Sume *cp++ = NOERROR; /*%< extended RCODE */ 236186090Sume *cp++ = 0; /*%< EDNS version */ 237186090Sume 238156952Sume if (statp->options & RES_USE_DNSSEC) { 239156952Sume#ifdef DEBUG 240156952Sume if (statp->options & RES_DEBUG) 241156952Sume printf(";; res_opt()... ENDS0 DNSSEC\n"); 242156952Sume#endif 243156952Sume flags |= NS_OPT_DNSSEC_OK; 244156952Sume } 245156952Sume ns_put16(flags, cp); 246156952Sume cp += INT16SZ; 247186090Sume 248186090Sume ns_put16(0U, cp); /*%< RDLEN */ 249156952Sume cp += INT16SZ; 250186090Sume 251156952Sume hp->arcount = htons(ntohs(hp->arcount) + 1); 252156952Sume 253156952Sume return (cp - buf); 254156952Sume} 255186090Sume 256186090Sume/* 257186090Sume * Construct variable data (RDATA) block for OPT psuedo-RR, append it 258186090Sume * to the buffer, then update the RDLEN field (previously set to zero by 259186090Sume * res_nopt()) with the new RDATA length. 260186090Sume */ 261186090Sumeint 262186090Sumeres_nopt_rdata(res_state statp, 263186090Sume int n0, /*%< current offset in buffer */ 264186090Sume u_char *buf, /*%< buffer to put query */ 265186090Sume int buflen, /*%< size of buffer */ 266186090Sume u_char *rdata, /*%< ptr to start of opt rdata */ 267186090Sume u_short code, /*%< OPTION-CODE */ 268186090Sume u_short len, /*%< OPTION-LENGTH */ 269186090Sume u_char *data) /*%< OPTION_DATA */ 270186090Sume{ 271186090Sume register u_char *cp, *ep; 272186090Sume 273186090Sume#ifdef DEBUG 274186090Sume if ((statp->options & RES_DEBUG) != 0U) 275186090Sume printf(";; res_nopt_rdata()\n"); 276156952Sume#endif 277170244Sume 278186090Sume cp = buf + n0; 279186090Sume ep = buf + buflen; 280186090Sume 281186090Sume if ((ep - cp) < (4 + len)) 282186090Sume return (-1); 283186090Sume 284186090Sume if (rdata < (buf + 2) || rdata >= ep) 285186090Sume return (-1); 286186090Sume 287186090Sume ns_put16(code, cp); 288186090Sume cp += INT16SZ; 289186090Sume 290186090Sume ns_put16(len, cp); 291186090Sume cp += INT16SZ; 292186090Sume 293186090Sume memcpy(cp, data, len); 294186090Sume cp += len; 295186090Sume 296186090Sume len = cp - rdata; 297186090Sume ns_put16(len, rdata - 2); /* Update RDLEN field */ 298186090Sume 299186090Sume return (cp - buf); 300186090Sume} 301186090Sume#endif 302186090Sume 303170244Sume/*! \file */ 304