res_mkquery.c revision 170244
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"; 69170244Sumestatic const char rcsid[] = "$Id: res_mkquery.c,v 1.5.18.1 2005/04/27 05:01:11 sra Exp $"; 70156952Sume#endif /* LIBC_SCCS and not lint */ 71156956Sume#include <sys/cdefs.h> 72156956Sume__FBSDID("$FreeBSD: head/lib/libc/resolv/res_mkquery.c 170244 2007-06-03 17:20:27Z ume $"); 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#ifndef T_OPT 205156952Sume#define T_OPT 41 206156952Sume#endif 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 231170244Sume *cp++ = 0; /*%< "." */ 232170244Sume ns_put16(T_OPT, cp); /*%< TYPE */ 233156952Sume cp += INT16SZ; 234156956Sume if (anslen > 0xffff) 235156956Sume anslen = 0xffff; /* limit to 16bit value */ 236170244Sume ns_put16(anslen & 0xffff, cp); /*%< CLASS = UDP payload size */ 237156952Sume cp += INT16SZ; 238170244Sume *cp++ = NOERROR; /*%< extended RCODE */ 239170244Sume *cp++ = 0; /*%< EDNS version */ 240156952Sume if (statp->options & RES_USE_DNSSEC) { 241156952Sume#ifdef DEBUG 242156952Sume if (statp->options & RES_DEBUG) 243156952Sume printf(";; res_opt()... ENDS0 DNSSEC\n"); 244156952Sume#endif 245156952Sume flags |= NS_OPT_DNSSEC_OK; 246156952Sume } 247156952Sume ns_put16(flags, cp); 248156952Sume cp += INT16SZ; 249170244Sume ns_put16(0, cp); /*%< RDLEN */ 250156952Sume cp += INT16SZ; 251156952Sume hp->arcount = htons(ntohs(hp->arcount) + 1); 252156952Sume 253156952Sume return (cp - buf); 254156952Sume} 255156952Sume#endif 256170244Sume 257170244Sume/*! \file */ 258