2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. 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.
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
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
51 * Portions Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52 * Portions Copyright (c) 1996-2003 by Internet Software Consortium
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.
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.
66 * Internet Systems Consortium, Inc.
68 * Redwood City, CA 94063
73 #if defined(LIBC_SCCS) && !defined(lint)
74 static const char sccsid
[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
75 static const char rcsid
[] = "$Id: res_mkquery.c,v 1.3 2005/08/11 17:13:26 drochner Exp $";
76 #endif /* LIBC_SCCS and not lint */
78 #include <sys/types.h>
79 #include <sys/param.h>
80 #include <netinet/in.h>
84 #include <sys/socket.h>
86 #include "minires/minires.h"
87 #include "arpa/nameser.h"
89 extern const char *_res_opcodes
[];
92 * Form all types of queries.
93 * Returns the size of the result or -1.
96 res_nmkquery(res_state statp
,
97 int op
, /* opcode of query */
98 const char *dname
, /* domain name */
99 ns_class
class, ns_type type
, /* class and type of query */
100 const u_char
*data
, /* resource record data */
101 unsigned datalen
, /* length of data */
102 const u_char
*newrr_in
, /* new rr for modify or append */
103 double *buf
, /* buffer to put query */
104 unsigned buflen
, /* size of buffer */
105 unsigned *rbuflen
) /* returned size of buffer */
110 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
113 * Initialize header fields.
115 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
116 return ISC_R_INVALIDARG
;
117 memset(buf
, 0, HFIXEDSZ
);
119 hp
->id
= htons(++statp
->id
);
121 hp
->rd
= (statp
->options
& RES_RECURSE
) != 0;
123 cp
= ((u_char
*)buf
) + HFIXEDSZ
;
126 *dpp
++ = (u_char
*)buf
;
128 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
130 * perform opcode specific processing
133 case QUERY
: /*FALLTHROUGH*/
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
;
143 putUShort(cp
, class);
145 hp
->qdcount
= htons(1);
146 if (op
== QUERY
|| data
== NULL
)
149 * Make an additional record for completion domain.
152 n
= dn_comp((const char *)data
, cp
, buflen
, dnptrs
, lastdnptr
);
154 return ISC_R_NOSPACE
;
157 putUShort(cp
, T_NULL
);
159 putUShort(cp
, class);
165 hp
->arcount
= htons(1);
170 * Initialize answer section
172 if (buflen
< 1 + RRFIXEDSZ
+ datalen
)
173 return ISC_R_NOSPACE
;
174 *cp
++ = '\0'; /* no domain name */
177 putUShort(cp
, class);
181 putUShort(cp
, datalen
);
184 memcpy(cp
, data
, datalen
);
187 hp
->ancount
= htons(1);
191 return ISC_R_NOTIMPLEMENTED
;
193 *rbuflen
= cp
- ((u_char
*)buf
);
194 return ISC_R_SUCCESS
;