diff options
Diffstat (limited to 'src/lib/libc/net/res_mkquery.c')
| -rw-r--r-- | src/lib/libc/net/res_mkquery.c | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_mkquery.c b/src/lib/libc/net/res_mkquery.c new file mode 100644 index 0000000000..6cd6a00dbe --- /dev/null +++ b/src/lib/libc/net/res_mkquery.c | |||
| @@ -0,0 +1,242 @@ | |||
| 1 | /* $OpenBSD: res_mkquery.c,v 1.12 2002/08/28 03:19:38 itojun Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * ++Copyright++ 1985, 1993 | ||
| 5 | * - | ||
| 6 | * Copyright (c) 1985, 1993 | ||
| 7 | * The Regents of the University of California. All rights reserved. | ||
| 8 | * | ||
| 9 | * Redistribution and use in source and binary forms, with or without | ||
| 10 | * modification, are permitted provided that the following conditions | ||
| 11 | * are met: | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 15 | * notice, this list of conditions and the following disclaimer in the | ||
| 16 | * documentation and/or other materials provided with the distribution. | ||
| 17 | * 3. All advertising materials mentioning features or use of this software | ||
| 18 | * must display the following acknowledgement: | ||
| 19 | * This product includes software developed by the University of | ||
| 20 | * California, Berkeley and its contributors. | ||
| 21 | * 4. Neither the name of the University nor the names of its contributors | ||
| 22 | * may be used to endorse or promote products derived from this software | ||
| 23 | * without specific prior written permission. | ||
| 24 | * | ||
| 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 35 | * SUCH DAMAGE. | ||
| 36 | * - | ||
| 37 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 38 | * | ||
| 39 | * Permission to use, copy, modify, and distribute this software for any | ||
| 40 | * purpose with or without fee is hereby granted, provided that the above | ||
| 41 | * copyright notice and this permission notice appear in all copies, and that | ||
| 42 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 43 | * publicity pertaining to distribution of the document or software without | ||
| 44 | * specific, written prior permission. | ||
| 45 | * | ||
| 46 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 47 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 48 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 49 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 50 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 51 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 52 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 53 | * SOFTWARE. | ||
| 54 | * - | ||
| 55 | * --Copyright-- | ||
| 56 | */ | ||
| 57 | |||
| 58 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 59 | #if 0 | ||
| 60 | static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; | ||
| 61 | static char rcsid[] = "$From: res_mkquery.c,v 8.5 1996/08/27 08:33:28 vixie Exp $"; | ||
| 62 | #else | ||
| 63 | static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.12 2002/08/28 03:19:38 itojun Exp $"; | ||
| 64 | #endif | ||
| 65 | #endif /* LIBC_SCCS and not lint */ | ||
| 66 | |||
| 67 | #include <sys/types.h> | ||
| 68 | #include <sys/param.h> | ||
| 69 | #include <netinet/in.h> | ||
| 70 | #include <arpa/nameser.h> | ||
| 71 | |||
| 72 | #include <stdio.h> | ||
| 73 | #include <netdb.h> | ||
| 74 | #include <resolv.h> | ||
| 75 | #include <string.h> | ||
| 76 | |||
| 77 | /* | ||
| 78 | * Form all types of queries. | ||
| 79 | * Returns the size of the result or -1. | ||
| 80 | */ | ||
| 81 | /* ARGSUSED */ | ||
| 82 | int | ||
| 83 | res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | ||
| 84 | int op; /* opcode of query */ | ||
| 85 | const char *dname; /* domain name */ | ||
| 86 | int class, type; /* class and type of query */ | ||
| 87 | const u_char *data; /* resource record data */ | ||
| 88 | int datalen; /* length of data */ | ||
| 89 | const u_char *newrr_in; /* new rr for modify or append */ | ||
| 90 | u_char *buf; /* buffer to put query */ | ||
| 91 | int buflen; /* size of buffer */ | ||
| 92 | { | ||
| 93 | register HEADER *hp; | ||
| 94 | register u_char *cp, *ep; | ||
| 95 | register int n; | ||
| 96 | u_char *dnptrs[20], **dpp, **lastdnptr; | ||
| 97 | |||
| 98 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { | ||
| 99 | h_errno = NETDB_INTERNAL; | ||
| 100 | return (-1); | ||
| 101 | } | ||
| 102 | #ifdef DEBUG | ||
| 103 | if (_res.options & RES_DEBUG) | ||
| 104 | printf(";; res_mkquery(%d, %s, %d, %d)\n", | ||
| 105 | op, dname, class, type); | ||
| 106 | #endif | ||
| 107 | /* | ||
| 108 | * Initialize header fields. | ||
| 109 | * | ||
| 110 | * A special random number generator is used to create non predictable | ||
| 111 | * and non repeating ids over a long period. It also avoids reuse | ||
| 112 | * by switching between two distinct number cycles. | ||
| 113 | */ | ||
| 114 | |||
| 115 | if ((buf == NULL) || (buflen < HFIXEDSZ)) | ||
| 116 | return (-1); | ||
| 117 | bzero(buf, HFIXEDSZ); | ||
| 118 | hp = (HEADER *) buf; | ||
| 119 | _res.id = res_randomid(); | ||
| 120 | hp->id = htons(_res.id); | ||
| 121 | hp->opcode = op; | ||
| 122 | hp->rd = (_res.options & RES_RECURSE) != 0; | ||
| 123 | hp->rcode = NOERROR; | ||
| 124 | cp = buf + HFIXEDSZ; | ||
| 125 | ep = buf + buflen; | ||
| 126 | dpp = dnptrs; | ||
| 127 | *dpp++ = buf; | ||
| 128 | *dpp++ = NULL; | ||
| 129 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; | ||
| 130 | /* | ||
| 131 | * perform opcode specific processing | ||
| 132 | */ | ||
| 133 | switch (op) { | ||
| 134 | case QUERY: /*FALLTHROUGH*/ | ||
| 135 | case NS_NOTIFY_OP: | ||
| 136 | if (ep - cp < QFIXEDSZ) | ||
| 137 | return (-1); | ||
| 138 | if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, | ||
| 139 | lastdnptr)) < 0) | ||
| 140 | return (-1); | ||
| 141 | cp += n; | ||
| 142 | __putshort(type, cp); | ||
| 143 | cp += INT16SZ; | ||
| 144 | __putshort(class, cp); | ||
| 145 | cp += INT16SZ; | ||
| 146 | hp->qdcount = htons(1); | ||
| 147 | if (op == QUERY || data == NULL) | ||
| 148 | break; | ||
| 149 | /* | ||
| 150 | * Make an additional record for completion domain. | ||
| 151 | */ | ||
| 152 | if (ep - cp < RRFIXEDSZ) | ||
| 153 | return (-1); | ||
| 154 | n = dn_comp((char *)data, cp, ep - cp - RRFIXEDSZ, dnptrs, | ||
| 155 | lastdnptr); | ||
| 156 | if (n < 0) | ||
| 157 | return (-1); | ||
| 158 | cp += n; | ||
| 159 | __putshort(T_NULL, cp); | ||
| 160 | cp += INT16SZ; | ||
| 161 | __putshort(class, cp); | ||
| 162 | cp += INT16SZ; | ||
| 163 | __putlong(0, cp); | ||
| 164 | cp += INT32SZ; | ||
| 165 | __putshort(0, cp); | ||
| 166 | cp += INT16SZ; | ||
| 167 | hp->arcount = htons(1); | ||
| 168 | break; | ||
| 169 | |||
| 170 | case IQUERY: | ||
| 171 | /* | ||
| 172 | * Initialize answer section | ||
| 173 | */ | ||
| 174 | if (ep - cp < 1 + RRFIXEDSZ + datalen) | ||
| 175 | return (-1); | ||
| 176 | *cp++ = '\0'; /* no domain name */ | ||
| 177 | __putshort(type, cp); | ||
| 178 | cp += INT16SZ; | ||
| 179 | __putshort(class, cp); | ||
| 180 | cp += INT16SZ; | ||
| 181 | __putlong(0, cp); | ||
| 182 | cp += INT32SZ; | ||
| 183 | __putshort(datalen, cp); | ||
| 184 | cp += INT16SZ; | ||
| 185 | if (datalen) { | ||
| 186 | bcopy(data, cp, datalen); | ||
| 187 | cp += datalen; | ||
| 188 | } | ||
| 189 | hp->ancount = htons(1); | ||
| 190 | break; | ||
| 191 | |||
| 192 | default: | ||
| 193 | return (-1); | ||
| 194 | } | ||
| 195 | return (cp - buf); | ||
| 196 | } | ||
| 197 | |||
| 198 | /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ | ||
| 199 | int | ||
| 200 | res_opt(n0, buf, buflen, anslen) | ||
| 201 | int n0; | ||
| 202 | u_char *buf; /* buffer to put query */ | ||
| 203 | int buflen; /* size of buffer */ | ||
| 204 | int anslen; /* answer buffer length */ | ||
| 205 | { | ||
| 206 | register HEADER *hp; | ||
| 207 | register u_char *cp, *ep; | ||
| 208 | |||
| 209 | hp = (HEADER *) buf; | ||
| 210 | cp = buf + n0; | ||
| 211 | ep = buf + buflen; | ||
| 212 | |||
| 213 | if (ep - cp < 1 + RRFIXEDSZ) | ||
| 214 | return -1; | ||
| 215 | |||
| 216 | *cp++ = 0; /* "." */ | ||
| 217 | |||
| 218 | __putshort(T_OPT, cp); /* TYPE */ | ||
| 219 | cp += INT16SZ; | ||
| 220 | if (anslen > 0xffff) | ||
| 221 | anslen = 0xffff; /* limit to 16bit value */ | ||
| 222 | __putshort(anslen & 0xffff, cp); /* CLASS = UDP payload size */ | ||
| 223 | cp += INT16SZ; | ||
| 224 | *cp++ = NOERROR; /* extended RCODE */ | ||
| 225 | *cp++ = 0; /* EDNS version */ | ||
| 226 | if (_res.options & RES_USE_DNSSEC) { | ||
| 227 | #ifdef DEBUG | ||
| 228 | if (_res.options & RES_DEBUG) | ||
| 229 | printf(";; res_opt()... ENDS0 DNSSEC OK\n"); | ||
| 230 | #endif /* DEBUG */ | ||
| 231 | __putshort(DNS_MESSAGEEXTFLAG_DO, cp); /* EDNS Z field */ | ||
| 232 | cp += INT16SZ; | ||
| 233 | } else { | ||
| 234 | __putshort(0, cp); /* EDNS Z field */ | ||
| 235 | cp += INT16SZ; | ||
| 236 | } | ||
| 237 | __putshort(0, cp); /* RDLEN */ | ||
| 238 | cp += INT16SZ; | ||
| 239 | hp->arcount = htons(ntohs(hp->arcount) + 1); | ||
| 240 | |||
| 241 | return cp - buf; | ||
| 242 | } | ||
