1/* 2 * Copyright (c) 1985, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30/* 31 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 32 * 33 * Permission to use, copy, modify, and distribute this software for any 34 * purpose with or without fee is hereby granted, provided that the above 35 * copyright notice and this permission notice appear in all copies, and that 36 * the name of Digital Equipment Corporation not be used in advertising or 37 * publicity pertaining to distribution of the document or software without 38 * specific, written prior permission. 39 * 40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 47 * SOFTWARE. 48 */ 49 50/* 51 * Portions Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 52 * Portions Copyright (c) 1996-2003 by Internet Software Consortium 53 * 54 * Permission to use, copy, modify, and distribute this software for any 55 * purpose with or without fee is hereby granted, provided that the above 56 * copyright notice and this permission notice appear in all copies. 57 * 58 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 59 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 60 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 61 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 62 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 63 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 64 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 65 * 66 * Internet Systems Consortium, Inc. 67 * 950 Charter Street 68 * Redwood City, CA 94063 69 * <info@isc.org> 70 * http://www.isc.org/ 71 */ 72 73#if defined(LIBC_SCCS) && !defined(lint) 74static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; 75static const char rcsid[] = "$Id: res_mkquery.c,v 1.3 2005/08/11 17:13:26 drochner Exp $"; 76#endif /* LIBC_SCCS and not lint */ 77 78#include <sys/types.h> 79#include <sys/param.h> 80#include <netinet/in.h> 81#include <netdb.h> 82#include <stdio.h> 83#include <string.h> 84#include <sys/socket.h> 85 86#include "minires/minires.h" 87#include "arpa/nameser.h" 88 89extern const char *_res_opcodes[]; 90 91/* 92 * Form all types of queries. 93 * Returns the size of the result or -1. 94 */ 95isc_result_t 96res_nmkquery(res_state statp, 97 int op, /* opcode of query */ 98 const char *dname, /* domain name */ 99 ns_class class, ns_type type, /* class and type of query */ 100 const u_char *data, /* resource record data */ 101 unsigned datalen, /* length of data */ 102 const u_char *newrr_in, /* new rr for modify or append */ 103 double *buf, /* buffer to put query */ 104 unsigned buflen, /* size of buffer */ 105 unsigned *rbuflen) /* returned size of buffer */ 106{ 107 register HEADER *hp; 108 register u_char *cp; 109 register int n; 110 u_char *dnptrs[20], **dpp, **lastdnptr; 111 112 /* 113 * Initialize header fields. 114 */ 115 if ((buf == NULL) || (buflen < HFIXEDSZ)) 116 return ISC_R_INVALIDARG; 117 memset(buf, 0, HFIXEDSZ); 118 hp = (HEADER *) buf; 119 hp->id = htons(++statp->id); 120 hp->opcode = op; 121 hp->rd = (statp->options & RES_RECURSE) != 0; 122 hp->rcode = NOERROR; 123 cp = ((u_char *)buf) + HFIXEDSZ; 124 buflen -= HFIXEDSZ; 125 dpp = dnptrs; 126 *dpp++ = (u_char *)buf; 127 *dpp++ = NULL; 128 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; 129 /* 130 * perform opcode specific processing 131 */ 132 switch (op) { 133 case QUERY: /*FALLTHROUGH*/ 134 case NS_NOTIFY_OP: 135 if ((buflen -= QFIXEDSZ) < 0) 136 return ISC_R_NOSPACE; 137 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) 138 return ISC_R_NOSPACE; 139 cp += n; 140 buflen -= n; 141 putUShort(cp, type); 142 cp += INT16SZ; 143 putUShort(cp, class); 144 cp += INT16SZ; 145 hp->qdcount = htons(1); 146 if (op == QUERY || data == NULL) 147 break; 148 /* 149 * Make an additional record for completion domain. 150 */ 151 buflen -= RRFIXEDSZ; 152 n = dn_comp((const char *)data, cp, buflen, dnptrs, lastdnptr); 153 if (n < 0) 154 return ISC_R_NOSPACE; 155 cp += n; 156 buflen -= n; 157 putUShort(cp, T_NULL); 158 cp += INT16SZ; 159 putUShort(cp, class); 160 cp += INT16SZ; 161 putULong(cp, 0); 162 cp += INT32SZ; 163 putUShort(cp, 0); 164 cp += INT16SZ; 165 hp->arcount = htons(1); 166 break; 167 168 case IQUERY: 169 /* 170 * Initialize answer section 171 */ 172 if (buflen < 1 + RRFIXEDSZ + datalen) 173 return ISC_R_NOSPACE; 174 *cp++ = '\0'; /* no domain name */ 175 putUShort(cp, type); 176 cp += INT16SZ; 177 putUShort(cp, class); 178 cp += INT16SZ; 179 putULong(cp, 0); 180 cp += INT32SZ; 181 putUShort(cp, datalen); 182 cp += INT16SZ; 183 if (datalen) { 184 memcpy(cp, data, datalen); 185 cp += datalen; 186 } 187 hp->ancount = htons(1); 188 break; 189 190 default: 191 return ISC_R_NOTIMPLEMENTED; 192 } 193 *rbuflen = cp - ((u_char *)buf); 194 return ISC_R_SUCCESS; 195} 196