]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/resolv/res_mkquery.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libc / resolv / res_mkquery.c
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  * 4. 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  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52  * Portions Copyright (c) 1996-1999 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
67 #if defined(LIBC_SCCS) && !defined(lint)
68 static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
69 static const char rcsid[] = "$Id: res_mkquery.c,v 1.5.18.2 2008/04/03 23:15:15 marka Exp $";
70 #endif /* LIBC_SCCS and not lint */
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD$");
73
74 #include "port_before.h"
75 #include <sys/types.h>
76 #include <sys/param.h>
77 #include <netinet/in.h>
78 #include <arpa/nameser.h>
79 #include <netdb.h>
80 #include <resolv.h>
81 #include <stdio.h>
82 #include <string.h>
83 #include "port_after.h"
84
85 /* Options.  Leave them on. */
86 #define DEBUG
87
88 extern const char *_res_opcodes[];
89
90 /*%
91  * Form all types of queries.
92  * Returns the size of the result or -1.
93  */
94 int
95 res_nmkquery(res_state statp,
96              int op,                    /*!< opcode of query  */
97              const char *dname,         /*!< domain name  */
98              int class, int type,       /*!< class and type of query  */
99              const u_char *data,        /*!< resource record data  */
100              int datalen,               /*!< length of data  */
101              const u_char *newrr_in,    /*!< new rr for modify or append  */
102              u_char *buf,               /*!< buffer to put query  */
103              int buflen)                /*!< size of buffer  */
104 {
105         HEADER *hp;
106         u_char *cp, *ep;
107         int n;
108         u_char *dnptrs[20], **dpp, **lastdnptr;
109
110         UNUSED(newrr_in);
111
112 #ifdef DEBUG
113         if (statp->options & RES_DEBUG)
114                 printf(";; res_nmkquery(%s, %s, %s, %s)\n",
115                        _res_opcodes[op], dname, p_class(class), p_type(type));
116 #endif
117         /*
118          * Initialize header fields.
119          */
120         if ((buf == NULL) || (buflen < HFIXEDSZ))
121                 return (-1);
122         memset(buf, 0, HFIXEDSZ);
123         hp = (HEADER *) buf;
124         hp->id = htons(++statp->id);
125         hp->opcode = op;
126         hp->rd = (statp->options & RES_RECURSE) != 0U;
127         hp->rcode = NOERROR;
128         cp = buf + HFIXEDSZ;
129         ep = buf + buflen;
130         dpp = dnptrs;
131         *dpp++ = buf;
132         *dpp++ = NULL;
133         lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
134         /*
135          * perform opcode specific processing
136          */
137         switch (op) {
138         case QUERY:     /*FALLTHROUGH*/
139         case NS_NOTIFY_OP:
140                 if (ep - cp < QFIXEDSZ)
141                         return (-1);
142                 if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
143                     lastdnptr)) < 0)
144                         return (-1);
145                 cp += n;
146                 ns_put16(type, cp);
147                 cp += INT16SZ;
148                 ns_put16(class, cp);
149                 cp += INT16SZ;
150                 hp->qdcount = htons(1);
151                 if (op == QUERY || data == NULL)
152                         break;
153                 /*
154                  * Make an additional record for completion domain.
155                  */
156                 if ((ep - cp) < RRFIXEDSZ)
157                         return (-1);
158                 n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
159                             dnptrs, lastdnptr);
160                 if (n < 0)
161                         return (-1);
162                 cp += n;
163                 ns_put16(T_NULL, cp);
164                 cp += INT16SZ;
165                 ns_put16(class, cp);
166                 cp += INT16SZ;
167                 ns_put32(0, cp);
168                 cp += INT32SZ;
169                 ns_put16(0, cp);
170                 cp += INT16SZ;
171                 hp->arcount = htons(1);
172                 break;
173
174         case IQUERY:
175                 /*
176                  * Initialize answer section
177                  */
178                 if (ep - cp < 1 + RRFIXEDSZ + datalen)
179                         return (-1);
180                 *cp++ = '\0';   /*%< no domain name */
181                 ns_put16(type, cp);
182                 cp += INT16SZ;
183                 ns_put16(class, cp);
184                 cp += INT16SZ;
185                 ns_put32(0, cp);
186                 cp += INT32SZ;
187                 ns_put16(datalen, cp);
188                 cp += INT16SZ;
189                 if (datalen) {
190                         memcpy(cp, data, datalen);
191                         cp += datalen;
192                 }
193                 hp->ancount = htons(1);
194                 break;
195
196         default:
197                 return (-1);
198         }
199         return (cp - buf);
200 }
201
202 #ifdef RES_USE_EDNS0
203 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
204
205 int
206 res_nopt(res_state statp,
207          int n0,                /*%< current offset in buffer */
208          u_char *buf,           /*%< buffer to put query */
209          int buflen,            /*%< size of buffer */
210          int anslen)            /*%< UDP answer buffer size */
211 {
212         HEADER *hp;
213         u_char *cp, *ep;
214         u_int16_t flags = 0;
215
216 #ifdef DEBUG
217         if ((statp->options & RES_DEBUG) != 0U)
218                 printf(";; res_nopt()\n");
219 #endif
220
221         hp = (HEADER *) buf;
222         cp = buf + n0;
223         ep = buf + buflen;
224
225         if ((ep - cp) < 1 + RRFIXEDSZ)
226                 return (-1);
227
228         *cp++ = 0;                              /*%< "." */
229         ns_put16(ns_t_opt, cp);                 /*%< TYPE */
230         cp += INT16SZ;
231         if (anslen > 0xffff)
232                 anslen = 0xffff;                /* limit to 16bit value */
233         ns_put16(anslen & 0xffff, cp);          /*%< CLASS = UDP payload size */
234         cp += INT16SZ;
235         *cp++ = NOERROR;                        /*%< extended RCODE */
236         *cp++ = 0;                              /*%< EDNS version */
237
238         if (statp->options & RES_USE_DNSSEC) {
239 #ifdef DEBUG
240                 if (statp->options & RES_DEBUG)
241                         printf(";; res_opt()... ENDS0 DNSSEC\n");
242 #endif
243                 flags |= NS_OPT_DNSSEC_OK;
244         }
245         ns_put16(flags, cp);
246         cp += INT16SZ;
247
248         ns_put16(0U, cp);                       /*%< RDLEN */
249         cp += INT16SZ;
250
251         hp->arcount = htons(ntohs(hp->arcount) + 1);
252
253         return (cp - buf);
254 }
255
256 /*
257  * Construct variable data (RDATA) block for OPT psuedo-RR, append it
258  * to the buffer, then update the RDLEN field (previously set to zero by
259  * res_nopt()) with the new RDATA length.
260  */
261 int
262 res_nopt_rdata(res_state statp,
263           int n0,               /*%< current offset in buffer */
264           u_char *buf,          /*%< buffer to put query */
265           int buflen,           /*%< size of buffer */
266           u_char *rdata,        /*%< ptr to start of opt rdata */
267           u_short code,         /*%< OPTION-CODE */
268           u_short len,          /*%< OPTION-LENGTH */
269           u_char *data)         /*%< OPTION_DATA */
270 {
271         register u_char *cp, *ep;
272
273 #ifdef DEBUG
274         if ((statp->options & RES_DEBUG) != 0U)
275                 printf(";; res_nopt_rdata()\n");
276 #endif
277
278         cp = buf + n0;
279         ep = buf + buflen;
280
281         if ((ep - cp) < (4 + len))
282                 return (-1);
283
284         if (rdata < (buf + 2) || rdata >= ep)
285                 return (-1);
286
287         ns_put16(code, cp);
288         cp += INT16SZ;
289
290         ns_put16(len, cp);
291         cp += INT16SZ;
292
293         memcpy(cp, data, len);
294         cp += len;
295
296         len = cp - rdata;
297         ns_put16(len, rdata - 2);       /* Update RDLEN field */
298
299         return (cp - buf);
300 }
301 #endif
302
303 /*! \file */