michael@0: /* $NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $ */ michael@0: michael@0: /* michael@0: * Copyright (c) 2008 Android Open Source Project (query id randomization) michael@0: * Copyright (c) 1985, 1993 michael@0: * The Regents of the University of California. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. All advertising materials mentioning features or use of this software michael@0: * must display the following acknowledgement: michael@0: * This product includes software developed by the University of michael@0: * California, Berkeley and its contributors. michael@0: * 4. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: */ michael@0: michael@0: /* michael@0: * Portions Copyright (c) 1993 by Digital Equipment Corporation. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies, and that michael@0: * the name of Digital Equipment Corporation not be used in advertising or michael@0: * publicity pertaining to distribution of the document or software without michael@0: * specific, written prior permission. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL michael@0: * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT michael@0: * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL michael@0: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR michael@0: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS michael@0: * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS michael@0: * SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") michael@0: * Portions Copyright (c) 1996-1999 by Internet Software Consortium. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose with or without fee is hereby granted, provided that the above michael@0: * copyright notice and this permission notice appear in all copies. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES michael@0: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF michael@0: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR michael@0: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES michael@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN michael@0: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT michael@0: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. michael@0: */ michael@0: michael@0: /* michael@0: * This version of this file is derived from Android 2.3 "Gingerbread", michael@0: * which contains uncredited changes by Android/Google developers. It has michael@0: * been modified in 2011 for use in the Android build of Mozilla Firefox by michael@0: * Mozilla contributors (including Michael Edwards , michael@0: * and Steve Workman ). michael@0: * These changes are offered under the same license as the original NetBSD michael@0: * file, whose copyright and license are unchanged above. michael@0: */ michael@0: michael@0: #define ANDROID_CHANGES 1 michael@0: #define MOZILLA_NECKO_EXCLUDE_CODE 1 michael@0: michael@0: #include michael@0: #if defined(LIBC_SCCS) && !defined(lint) michael@0: #ifdef notdef michael@0: static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; michael@0: static const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp"; michael@0: #else michael@0: __RCSID("$NetBSD: res_mkquery.c,v 1.6 2006/01/24 17:40:32 christos Exp $"); michael@0: #endif michael@0: #endif /* LIBC_SCCS and not lint */ michael@0: michael@0: michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "arpa_nameser.h" michael@0: #include michael@0: #ifdef ANDROID_CHANGES michael@0: #include "resolv_private.h" michael@0: #else michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: michael@0: /* Options. Leave them on. */ michael@0: #ifndef DEBUG michael@0: #define DEBUG michael@0: #endif michael@0: michael@0: #ifndef lint michael@0: #define UNUSED(a) (void)&a michael@0: #else michael@0: #define UNUSED(a) a = a michael@0: #endif michael@0: michael@0: extern const char *_res_opcodes[]; michael@0: michael@0: /* michael@0: * Form all types of queries. michael@0: * Returns the size of the result or -1. michael@0: */ michael@0: int michael@0: res_nmkquery(res_state statp, michael@0: int op, /* opcode of query */ michael@0: const char *dname, /* domain name */ michael@0: int class, int type, /* class and type of query */ michael@0: const u_char *data, /* resource record data */ michael@0: int datalen, /* length of data */ michael@0: const u_char *newrr_in, /* new rr for modify or append */ michael@0: u_char *buf, /* buffer to put query */ michael@0: int buflen) /* size of buffer */ michael@0: { michael@0: register HEADER *hp; michael@0: register u_char *cp, *ep; michael@0: register int n; michael@0: u_char *dnptrs[20], **dpp, **lastdnptr; michael@0: michael@0: UNUSED(newrr_in); michael@0: michael@0: #ifdef DEBUG michael@0: if (statp->options & RES_DEBUG) michael@0: printf(";; res_nmkquery(%s, %s, %s, %s)\n", michael@0: _res_opcodes[op], dname, p_class(class), p_type(type)); michael@0: #endif michael@0: /* michael@0: * Initialize header fields. michael@0: */ michael@0: if ((buf == NULL) || (buflen < HFIXEDSZ)) michael@0: return (-1); michael@0: memset(buf, 0, HFIXEDSZ); michael@0: hp = (HEADER *)(void *)buf; michael@0: hp->id = htons(res_randomid()); michael@0: hp->opcode = op; michael@0: hp->rd = (statp->options & RES_RECURSE) != 0U; michael@0: hp->rcode = NOERROR; michael@0: cp = buf + HFIXEDSZ; michael@0: ep = buf + buflen; michael@0: dpp = dnptrs; michael@0: *dpp++ = buf; michael@0: *dpp++ = NULL; michael@0: lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; michael@0: /* michael@0: * perform opcode specific processing michael@0: */ michael@0: switch (op) { michael@0: case QUERY: /*FALLTHROUGH*/ michael@0: case NS_NOTIFY_OP: michael@0: if (ep - cp < QFIXEDSZ) michael@0: return (-1); michael@0: if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, michael@0: lastdnptr)) < 0) michael@0: return (-1); michael@0: cp += n; michael@0: ns_put16(type, cp); michael@0: cp += INT16SZ; michael@0: ns_put16(class, cp); michael@0: cp += INT16SZ; michael@0: hp->qdcount = htons(1); michael@0: if (op == QUERY || data == NULL) michael@0: break; michael@0: /* michael@0: * Make an additional record for completion domain. michael@0: */ michael@0: if ((ep - cp) < RRFIXEDSZ) michael@0: return (-1); michael@0: n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ, michael@0: dnptrs, lastdnptr); michael@0: if (n < 0) michael@0: return (-1); michael@0: cp += n; michael@0: ns_put16(T_NULL, cp); michael@0: cp += INT16SZ; michael@0: ns_put16(class, cp); michael@0: cp += INT16SZ; michael@0: ns_put32(0, cp); michael@0: cp += INT32SZ; michael@0: ns_put16(0, cp); michael@0: cp += INT16SZ; michael@0: hp->arcount = htons(1); michael@0: break; michael@0: michael@0: case IQUERY: michael@0: /* michael@0: * Initialize answer section michael@0: */ michael@0: if (ep - cp < 1 + RRFIXEDSZ + datalen) michael@0: return (-1); michael@0: *cp++ = '\0'; /* no domain name */ michael@0: ns_put16(type, cp); michael@0: cp += INT16SZ; michael@0: ns_put16(class, cp); michael@0: cp += INT16SZ; michael@0: ns_put32(0, cp); michael@0: cp += INT32SZ; michael@0: ns_put16(datalen, cp); michael@0: cp += INT16SZ; michael@0: if (datalen) { michael@0: memcpy(cp, data, (size_t)datalen); michael@0: cp += datalen; michael@0: } michael@0: hp->ancount = htons(1); michael@0: break; michael@0: michael@0: default: michael@0: return (-1); michael@0: } michael@0: return (cp - buf); michael@0: } michael@0: michael@0: #ifdef RES_USE_EDNS0 michael@0: /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ michael@0: #ifndef T_OPT michael@0: #define T_OPT 41 michael@0: #endif michael@0: michael@0: int michael@0: res_nopt(res_state statp, michael@0: int n0, /* current offset in buffer */ michael@0: u_char *buf, /* buffer to put query */ michael@0: int buflen, /* size of buffer */ michael@0: int anslen) /* UDP answer buffer size */ michael@0: { michael@0: register HEADER *hp; michael@0: register u_char *cp, *ep; michael@0: u_int16_t flags = 0; michael@0: michael@0: #ifdef DEBUG michael@0: if ((statp->options & RES_DEBUG) != 0U) michael@0: printf(";; res_nopt()\n"); michael@0: #endif michael@0: michael@0: hp = (HEADER *)(void *)buf; michael@0: cp = buf + n0; michael@0: ep = buf + buflen; michael@0: michael@0: if ((ep - cp) < 1 + RRFIXEDSZ) michael@0: return (-1); michael@0: michael@0: *cp++ = 0; /* "." */ michael@0: michael@0: ns_put16(T_OPT, cp); /* TYPE */ michael@0: cp += INT16SZ; michael@0: ns_put16(anslen & 0xffff, cp); /* CLASS = UDP payload size */ michael@0: cp += INT16SZ; michael@0: *cp++ = NOERROR; /* extended RCODE */ michael@0: *cp++ = 0; /* EDNS version */ michael@0: if (statp->options & RES_USE_DNSSEC) { michael@0: #ifdef DEBUG michael@0: if (statp->options & RES_DEBUG) michael@0: printf(";; res_opt()... ENDS0 DNSSEC\n"); michael@0: #endif michael@0: flags |= NS_OPT_DNSSEC_OK; michael@0: } michael@0: ns_put16(flags, cp); michael@0: cp += INT16SZ; michael@0: ns_put16(0, cp); /* RDLEN */ michael@0: cp += INT16SZ; michael@0: hp->arcount = htons(ntohs(hp->arcount) + 1); michael@0: michael@0: return (cp - buf); michael@0: } michael@0: #endif