1193323Sed/* 2193323Sed * Copyright (c) 1985, 1993 3193323Sed * The Regents of the University of California. All rights reserved. 4193323Sed * 5193323Sed * Redistribution and use in source and binary forms, with or without 6193323Sed * modification, are permitted provided that the following conditions 7193323Sed * are met: 8193323Sed * 1. Redistributions of source code must retain the above copyright 9193323Sed * notice, this list of conditions and the following disclaimer. 10193323Sed * 2. Redistributions in binary form must reproduce the above copyright 11193323Sed * notice, this list of conditions and the following disclaimer in the 12193323Sed * documentation and/or other materials provided with the distribution. 13193323Sed * 3. Neither the name of the University nor the names of its contributors 14193323Sed * may be used to endorse or promote products derived from this software 15193323Sed * without specific prior written permission. 16193323Sed * 17193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20193323Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24223017Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27193323Sed * SUCH DAMAGE. 28198090Srdivacky */ 29198090Srdivacky 30198090Srdivacky/* 31198090Srdivacky * Portions Copyright (c) 1993 by Digital Equipment Corporation. 32198090Srdivacky * 33198090Srdivacky * Permission to use, copy, modify, and distribute this software for any 34193323Sed * purpose with or without fee is hereby granted, provided that the above 35199481Srdivacky * copyright notice and this permission notice appear in all copies, and that 36193323Sed * the name of Digital Equipment Corporation not be used in advertising or 37198090Srdivacky * publicity pertaining to distribution of the document or software without 38198090Srdivacky * specific, written prior permission. 39193323Sed * 40193323Sed * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 41193323Sed * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 42198396Srdivacky * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 43198396Srdivacky * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 44198396Srdivacky * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 45198396Srdivacky * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 46198892Srdivacky * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 47198892Srdivacky * SOFTWARE. 48198892Srdivacky */ 49198892Srdivacky 50193323Sed/* 51193323Sed * Portions Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 52193323Sed * Portions Copyright (c) 1996-2003 by Internet Software Consortium 53193323Sed * 54193323Sed * Permission to use, copy, modify, and distribute this software for any 55199481Srdivacky * purpose with or without fee is hereby granted, provided that the above 56199481Srdivacky * copyright notice and this permission notice appear in all copies. 57193323Sed * 58193323Sed * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 59193323Sed * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 60193323Sed * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 61193323Sed * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 62193323Sed * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 63198090Srdivacky * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 64198090Srdivacky * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 65193323Sed * 66193323Sed * Internet Systems Consortium, Inc. 67198090Srdivacky * 950 Charter Street 68198090Srdivacky * Redwood City, CA 94063 69193323Sed * <info@isc.org> 70193323Sed * http://www.isc.org/ 71198090Srdivacky */ 72198090Srdivacky 73198090Srdivacky#if defined(LIBC_SCCS) && !defined(lint) 74193323Sedstatic const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; 75193323Sedstatic const char rcsid[] = "$Id: res_mkquery.c,v 1.3 2005/08/11 17:13:26 drochner Exp $"; 76198090Srdivacky#endif /* LIBC_SCCS and not lint */ 77198090Srdivacky 78193323Sed#include <sys/types.h> 79193323Sed#include <sys/param.h> 80199481Srdivacky#include <netinet/in.h> 81199481Srdivacky#include <netdb.h> 82199481Srdivacky#include <stdio.h> 83199481Srdivacky#include <string.h> 84199481Srdivacky#include <sys/socket.h> 85199481Srdivacky 86199481Srdivacky#include "minires/minires.h" 87199481Srdivacky#include "arpa/nameser.h" 88199481Srdivacky 89199481Srdivackyextern const char *_res_opcodes[]; 90199481Srdivacky 91199481Srdivacky/* 92199481Srdivacky * Form all types of queries. 93199481Srdivacky * Returns the size of the result or -1. 94199481Srdivacky */ 95199481Srdivackyisc_result_t 96199481Srdivackyres_nmkquery(res_state statp, 97199481Srdivacky int op, /* opcode of query */ 98199481Srdivacky const char *dname, /* domain name */ 99199481Srdivacky ns_class class, ns_type type, /* class and type of query */ 100199481Srdivacky const u_char *data, /* resource record data */ 101199481Srdivacky unsigned datalen, /* length of data */ 102199481Srdivacky const u_char *newrr_in, /* new rr for modify or append */ 103199481Srdivacky double *buf, /* buffer to put query */ 104199481Srdivacky unsigned buflen, /* size of buffer */ 105199481Srdivacky unsigned *rbuflen) /* returned size of buffer */ 106199481Srdivacky{ 107199481Srdivacky register HEADER *hp; 108199481Srdivacky register u_char *cp; 109199481Srdivacky register int n; 110193323Sed u_char *dnptrs[20], **dpp, **lastdnptr; 111193323Sed 112193323Sed /* 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