Annotation of src/lib/libc/resolv/res_mkquery.c, Revision 1.4
1.4 ! christos 1: /* $NetBSD: res_mkquery.c,v 1.3 2004/05/20 19:43:39 christos Exp $ */
1.1 christos 2:
3: /*
4: * Copyright (c) 1985, 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
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:
56: /*
57: * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58: * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
59: *
60: * Permission to use, copy, modify, and distribute this software for any
61: * purpose with or without fee is hereby granted, provided that the above
62: * copyright notice and this permission notice appear in all copies.
63: *
64: * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
65: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
66: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
67: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
69: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
70: * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71: */
72:
1.3 christos 73: #include <sys/cdefs.h>
1.1 christos 74: #if defined(LIBC_SCCS) && !defined(lint)
1.3 christos 75: #ifdef notdef
1.1 christos 76: static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
77: static const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp";
1.3 christos 78: #else
1.4 ! christos 79: __RCSID("$NetBSD: res_mkquery.c,v 1.3 2004/05/20 19:43:39 christos Exp $");
1.3 christos 80: #endif
1.1 christos 81: #endif /* LIBC_SCCS and not lint */
82:
83: #include "port_before.h"
1.4 ! christos 84:
! 85: #include "namespace.h"
1.1 christos 86: #include <sys/types.h>
87: #include <sys/param.h>
88: #include <netinet/in.h>
89: #include <arpa/nameser.h>
90: #include <netdb.h>
91: #include <resolv.h>
92: #include <stdio.h>
93: #include <string.h>
94: #include "port_after.h"
95:
1.4 ! christos 96: #ifdef __weak_alias
! 97: __weak_alias(res_nmkquery,_res_nmkquery)
! 98: __weak_alias(res_nopt,_res_nopt)
! 99: #endif
! 100:
1.1 christos 101: /* Options. Leave them on. */
102: #define DEBUG
103:
104: extern const char *_res_opcodes[];
105:
106: /*
107: * Form all types of queries.
108: * Returns the size of the result or -1.
109: */
110: int
111: res_nmkquery(res_state statp,
112: int op, /* opcode of query */
113: const char *dname, /* domain name */
114: int class, int type, /* class and type of query */
115: const u_char *data, /* resource record data */
116: int datalen, /* length of data */
117: const u_char *newrr_in, /* new rr for modify or append */
118: u_char *buf, /* buffer to put query */
119: int buflen) /* size of buffer */
120: {
121: register HEADER *hp;
122: register u_char *cp, *ep;
123: register int n;
124: u_char *dnptrs[20], **dpp, **lastdnptr;
125:
126: UNUSED(newrr_in);
127:
128: #ifdef DEBUG
129: if (statp->options & RES_DEBUG)
130: printf(";; res_nmkquery(%s, %s, %s, %s)\n",
131: _res_opcodes[op], dname, p_class(class), p_type(type));
132: #endif
133: /*
134: * Initialize header fields.
135: */
136: if ((buf == NULL) || (buflen < HFIXEDSZ))
137: return (-1);
138: memset(buf, 0, HFIXEDSZ);
1.2 christos 139: hp = (HEADER *)(void *)buf;
1.1 christos 140: hp->id = htons(++statp->id);
141: hp->opcode = op;
142: hp->rd = (statp->options & RES_RECURSE) != 0U;
143: hp->rcode = NOERROR;
144: cp = buf + HFIXEDSZ;
145: ep = buf + buflen;
146: dpp = dnptrs;
147: *dpp++ = buf;
148: *dpp++ = NULL;
149: lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
150: /*
151: * perform opcode specific processing
152: */
153: switch (op) {
154: case QUERY: /*FALLTHROUGH*/
155: case NS_NOTIFY_OP:
156: if (ep - cp < QFIXEDSZ)
157: return (-1);
158: if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
159: lastdnptr)) < 0)
160: return (-1);
161: cp += n;
162: ns_put16(type, cp);
163: cp += INT16SZ;
164: ns_put16(class, cp);
165: cp += INT16SZ;
166: hp->qdcount = htons(1);
167: if (op == QUERY || data == NULL)
168: break;
169: /*
170: * Make an additional record for completion domain.
171: */
172: if ((ep - cp) < RRFIXEDSZ)
173: return (-1);
174: n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
175: dnptrs, lastdnptr);
176: if (n < 0)
177: return (-1);
178: cp += n;
179: ns_put16(T_NULL, cp);
180: cp += INT16SZ;
181: ns_put16(class, cp);
182: cp += INT16SZ;
183: ns_put32(0, cp);
184: cp += INT32SZ;
185: ns_put16(0, cp);
186: cp += INT16SZ;
187: hp->arcount = htons(1);
188: break;
189:
190: case IQUERY:
191: /*
192: * Initialize answer section
193: */
194: if (ep - cp < 1 + RRFIXEDSZ + datalen)
195: return (-1);
196: *cp++ = '\0'; /* no domain name */
197: ns_put16(type, cp);
198: cp += INT16SZ;
199: ns_put16(class, cp);
200: cp += INT16SZ;
201: ns_put32(0, cp);
202: cp += INT32SZ;
203: ns_put16(datalen, cp);
204: cp += INT16SZ;
205: if (datalen) {
1.2 christos 206: memcpy(cp, data, (size_t)datalen);
1.1 christos 207: cp += datalen;
208: }
209: hp->ancount = htons(1);
210: break;
211:
212: default:
213: return (-1);
214: }
215: return (cp - buf);
216: }
217:
218: #ifdef RES_USE_EDNS0
219: /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
220: #ifndef T_OPT
221: #define T_OPT 41
222: #endif
223:
224: int
225: res_nopt(res_state statp,
226: int n0, /* current offset in buffer */
227: u_char *buf, /* buffer to put query */
228: int buflen, /* size of buffer */
229: int anslen) /* UDP answer buffer size */
230: {
231: register HEADER *hp;
232: register u_char *cp, *ep;
233: u_int16_t flags = 0;
234:
235: #ifdef DEBUG
236: if ((statp->options & RES_DEBUG) != 0U)
237: printf(";; res_nopt()\n");
238: #endif
239:
1.2 christos 240: hp = (HEADER *)(void *)buf;
1.1 christos 241: cp = buf + n0;
242: ep = buf + buflen;
243:
244: if ((ep - cp) < 1 + RRFIXEDSZ)
245: return (-1);
246:
247: *cp++ = 0; /* "." */
248:
249: ns_put16(T_OPT, cp); /* TYPE */
250: cp += INT16SZ;
251: ns_put16(anslen & 0xffff, cp); /* CLASS = UDP payload size */
252: cp += INT16SZ;
253: *cp++ = NOERROR; /* extended RCODE */
254: *cp++ = 0; /* EDNS version */
255: if (statp->options & RES_USE_DNSSEC) {
256: #ifdef DEBUG
257: if (statp->options & RES_DEBUG)
258: printf(";; res_opt()... ENDS0 DNSSEC\n");
259: #endif
260: flags |= NS_OPT_DNSSEC_OK;
261: }
262: ns_put16(flags, cp);
263: cp += INT16SZ;
264: ns_put16(0, cp); /* RDLEN */
265: cp += INT16SZ;
266: hp->arcount = htons(ntohs(hp->arcount) + 1);
267:
268: return (cp - buf);
269: }
270: #endif
CVSweb <webmaster@jp.NetBSD.org>