1156952Sume/* 2270838Sume * Portions Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") 3270838Sume * Portions Copyright (C) 1996, 1997, 1988, 1999, 2001, 2003 Internet Software Consortium. 4270838Sume * 5270838Sume * Permission to use, copy, modify, and/or distribute this software for any 6270838Sume * purpose with or without fee is hereby granted, provided that the above 7270838Sume * copyright notice and this permission notice appear in all copies. 8270838Sume * 9270838Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10270838Sume * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11270838Sume * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12270838Sume * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13270838Sume * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14270838Sume * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15270838Sume * PERFORMANCE OF THIS SOFTWARE. 16270838Sume */ 17270838Sume 18270838Sume/* 19156952Sume * Copyright (c) 1985, 1993 20156952Sume * The Regents of the University of California. All rights reserved. 21270838Sume * 22156952Sume * Redistribution and use in source and binary forms, with or without 23156952Sume * modification, are permitted provided that the following conditions 24156952Sume * are met: 25156952Sume * 1. Redistributions of source code must retain the above copyright 26156952Sume * notice, this list of conditions and the following disclaimer. 27156952Sume * 2. Redistributions in binary form must reproduce the above copyright 28156952Sume * notice, this list of conditions and the following disclaimer in the 29156952Sume * documentation and/or other materials provided with the distribution. 30156952Sume * 4. Neither the name of the University nor the names of its contributors 31156952Sume * may be used to endorse or promote products derived from this software 32156952Sume * without specific prior written permission. 33270838Sume * 34156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 35156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 38156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 39156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 40156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 42156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 43156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44156952Sume * SUCH DAMAGE. 45156952Sume */ 46156952Sume 47156952Sume/* 48156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 49270838Sume * 50156952Sume * Permission to use, copy, modify, and distribute this software for any 51156952Sume * purpose with or without fee is hereby granted, provided that the above 52156952Sume * copyright notice and this permission notice appear in all copies, and that 53156952Sume * the name of Digital Equipment Corporation not be used in advertising or 54156952Sume * publicity pertaining to distribution of the document or software without 55156952Sume * specific, written prior permission. 56270838Sume * 57156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 58156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 59156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 60156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 61156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 62156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 63156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 64156952Sume * SOFTWARE. 65156952Sume */ 66156952Sume 67156952Sume#if defined(LIBC_SCCS) && !defined(lint) 68156952Sumestatic const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; 69270838Sumestatic const char rcsid[] = "$Id: res_mkquery.c,v 1.10 2008/12/11 09:59:00 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. */ 86292430Sngie#ifndef DEBUG 87292430Sngie#define DEBUG 88292430Sngie#endif 89156952Sume 90156952Sumeextern const char *_res_opcodes[]; 91156952Sume 92170244Sume/*% 93156952Sume * Form all types of queries. 94156952Sume * Returns the size of the result or -1. 95156952Sume */ 96156952Sumeint 97156952Sumeres_nmkquery(res_state statp, 98170244Sume int op, /*!< opcode of query */ 99170244Sume const char *dname, /*!< domain name */ 100170244Sume int class, int type, /*!< class and type of query */ 101170244Sume const u_char *data, /*!< resource record data */ 102170244Sume int datalen, /*!< length of data */ 103170244Sume const u_char *newrr_in, /*!< new rr for modify or append */ 104170244Sume u_char *buf, /*!< buffer to put query */ 105170244Sume int buflen) /*!< size of buffer */ 106156952Sume{ 107156956Sume HEADER *hp; 108156956Sume u_char *cp, *ep; 109156956Sume int n; 110156952Sume u_char *dnptrs[20], **dpp, **lastdnptr; 111156952Sume 112156952Sume UNUSED(newrr_in); 113156952Sume 114156952Sume#ifdef DEBUG 115156952Sume if (statp->options & RES_DEBUG) 116156952Sume printf(";; res_nmkquery(%s, %s, %s, %s)\n", 117156952Sume _res_opcodes[op], dname, p_class(class), p_type(type)); 118156952Sume#endif 119156952Sume /* 120156952Sume * Initialize header fields. 121156952Sume */ 122156952Sume if ((buf == NULL) || (buflen < HFIXEDSZ)) 123156952Sume return (-1); 124156952Sume memset(buf, 0, HFIXEDSZ); 125156952Sume hp = (HEADER *) buf; 126270838Sume statp->id = res_nrandomid(statp); 127270838Sume hp->id = htons(statp->id); 128156952Sume hp->opcode = op; 129156952Sume hp->rd = (statp->options & RES_RECURSE) != 0U; 130156952Sume hp->rcode = NOERROR; 131156952Sume cp = buf + HFIXEDSZ; 132156952Sume ep = buf + buflen; 133156952Sume dpp = dnptrs; 134156952Sume *dpp++ = buf; 135156952Sume *dpp++ = NULL; 136156952Sume lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; 137156952Sume /* 138156952Sume * perform opcode specific processing 139156952Sume */ 140156952Sume switch (op) { 141156952Sume case QUERY: /*FALLTHROUGH*/ 142156952Sume case NS_NOTIFY_OP: 143156952Sume if (ep - cp < QFIXEDSZ) 144156952Sume return (-1); 145156952Sume if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, 146156952Sume lastdnptr)) < 0) 147156952Sume return (-1); 148156952Sume cp += n; 149156952Sume ns_put16(type, cp); 150156952Sume cp += INT16SZ; 151156952Sume ns_put16(class, cp); 152156952Sume cp += INT16SZ; 153156952Sume hp->qdcount = htons(1); 154156952Sume if (op == QUERY || data == NULL) 155156952Sume break; 156156952Sume /* 157156952Sume * Make an additional record for completion domain. 158156952Sume */ 159156952Sume if ((ep - cp) < RRFIXEDSZ) 160156952Sume return (-1); 161156952Sume n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ, 162156952Sume dnptrs, lastdnptr); 163156952Sume if (n < 0) 164156952Sume return (-1); 165156952Sume cp += n; 166156952Sume ns_put16(T_NULL, cp); 167156952Sume cp += INT16SZ; 168156952Sume ns_put16(class, cp); 169156952Sume cp += INT16SZ; 170156952Sume ns_put32(0, cp); 171156952Sume cp += INT32SZ; 172156952Sume ns_put16(0, cp); 173156952Sume cp += INT16SZ; 174156952Sume hp->arcount = htons(1); 175156952Sume break; 176156952Sume 177156952Sume case IQUERY: 178156952Sume /* 179156952Sume * Initialize answer section 180156952Sume */ 181156952Sume if (ep - cp < 1 + RRFIXEDSZ + datalen) 182156952Sume return (-1); 183170244Sume *cp++ = '\0'; /*%< no domain name */ 184156952Sume ns_put16(type, cp); 185156952Sume cp += INT16SZ; 186156952Sume ns_put16(class, cp); 187156952Sume cp += INT16SZ; 188156952Sume ns_put32(0, cp); 189156952Sume cp += INT32SZ; 190156952Sume ns_put16(datalen, cp); 191156952Sume cp += INT16SZ; 192156952Sume if (datalen) { 193156952Sume memcpy(cp, data, datalen); 194156952Sume cp += datalen; 195156952Sume } 196156952Sume hp->ancount = htons(1); 197156952Sume break; 198156952Sume 199156952Sume default: 200156952Sume return (-1); 201156952Sume } 202156952Sume return (cp - buf); 203156952Sume} 204156952Sume 205156952Sume#ifdef RES_USE_EDNS0 206156952Sume/* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ 207156952Sume 208156952Sumeint 209156952Sumeres_nopt(res_state statp, 210170244Sume int n0, /*%< current offset in buffer */ 211170244Sume u_char *buf, /*%< buffer to put query */ 212170244Sume int buflen, /*%< size of buffer */ 213170244Sume int anslen) /*%< UDP answer buffer size */ 214156952Sume{ 215156956Sume HEADER *hp; 216156956Sume u_char *cp, *ep; 217156952Sume u_int16_t flags = 0; 218156952Sume 219156952Sume#ifdef DEBUG 220156952Sume if ((statp->options & RES_DEBUG) != 0U) 221156952Sume printf(";; res_nopt()\n"); 222156952Sume#endif 223156952Sume 224156952Sume hp = (HEADER *) buf; 225156952Sume cp = buf + n0; 226156952Sume ep = buf + buflen; 227156952Sume 228156952Sume if ((ep - cp) < 1 + RRFIXEDSZ) 229156952Sume return (-1); 230156952Sume 231186090Sume *cp++ = 0; /*%< "." */ 232186090Sume ns_put16(ns_t_opt, cp); /*%< TYPE */ 233156952Sume cp += INT16SZ; 234156956Sume if (anslen > 0xffff) 235156956Sume anslen = 0xffff; /* limit to 16bit value */ 236186090Sume ns_put16(anslen & 0xffff, cp); /*%< CLASS = UDP payload size */ 237156952Sume cp += INT16SZ; 238186090Sume *cp++ = NOERROR; /*%< extended RCODE */ 239186090Sume *cp++ = 0; /*%< EDNS version */ 240186090Sume 241156952Sume if (statp->options & RES_USE_DNSSEC) { 242156952Sume#ifdef DEBUG 243156952Sume if (statp->options & RES_DEBUG) 244156952Sume printf(";; res_opt()... ENDS0 DNSSEC\n"); 245156952Sume#endif 246156952Sume flags |= NS_OPT_DNSSEC_OK; 247156952Sume } 248156952Sume ns_put16(flags, cp); 249156952Sume cp += INT16SZ; 250186090Sume 251186090Sume ns_put16(0U, cp); /*%< RDLEN */ 252156952Sume cp += INT16SZ; 253186090Sume 254156952Sume hp->arcount = htons(ntohs(hp->arcount) + 1); 255156952Sume 256156952Sume return (cp - buf); 257156952Sume} 258186090Sume 259186090Sume/* 260186090Sume * Construct variable data (RDATA) block for OPT psuedo-RR, append it 261186090Sume * to the buffer, then update the RDLEN field (previously set to zero by 262186090Sume * res_nopt()) with the new RDATA length. 263186090Sume */ 264186090Sumeint 265186090Sumeres_nopt_rdata(res_state statp, 266186090Sume int n0, /*%< current offset in buffer */ 267186090Sume u_char *buf, /*%< buffer to put query */ 268186090Sume int buflen, /*%< size of buffer */ 269186090Sume u_char *rdata, /*%< ptr to start of opt rdata */ 270186090Sume u_short code, /*%< OPTION-CODE */ 271186090Sume u_short len, /*%< OPTION-LENGTH */ 272186090Sume u_char *data) /*%< OPTION_DATA */ 273186090Sume{ 274186090Sume register u_char *cp, *ep; 275186090Sume 276186090Sume#ifdef DEBUG 277186090Sume if ((statp->options & RES_DEBUG) != 0U) 278186090Sume printf(";; res_nopt_rdata()\n"); 279156952Sume#endif 280170244Sume 281186090Sume cp = buf + n0; 282186090Sume ep = buf + buflen; 283186090Sume 284186090Sume if ((ep - cp) < (4 + len)) 285186090Sume return (-1); 286186090Sume 287186090Sume if (rdata < (buf + 2) || rdata >= ep) 288186090Sume return (-1); 289186090Sume 290186090Sume ns_put16(code, cp); 291186090Sume cp += INT16SZ; 292186090Sume 293186090Sume ns_put16(len, cp); 294186090Sume cp += INT16SZ; 295186090Sume 296186090Sume memcpy(cp, data, len); 297186090Sume cp += len; 298186090Sume 299186090Sume len = cp - rdata; 300186090Sume ns_put16(len, rdata - 2); /* Update RDLEN field */ 301186090Sume 302186090Sume return (cp - buf); 303186090Sume} 304186090Sume#endif 305186090Sume 306170244Sume/*! \file */ 307