2 * ++Copyright++ 1985, 1993
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * 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
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid
[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
54 static char rcsid
[] = "$Id$";
55 #endif /* LIBC_SCCS and not lint */
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/nameser.h>
65 #if defined(BSD) && (BSD >= 199103)
68 # include "../conf/portability.h"
71 #if defined(USE_OPTIONS_H)
72 # include <../conf/options.h>
76 * Form all types of queries.
77 * Returns the size of the result or -1.
80 res_mkquery(op
, dname
, class, type
, data
, datalen
, newrr_in
, buf
, buflen
)
81 int op
; /* opcode of query */
82 const char *dname
; /* domain name */
83 int class, type
; /* class and type of query */
84 const u_char
*data
; /* resource record data */
85 int datalen
; /* length of data */
86 const u_char
*newrr_in
; /* new rr for modify or append */
87 u_char
*buf
; /* buffer to put query */
88 int buflen
; /* size of buffer */
93 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
95 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
96 __set_h_errno (NETDB_INTERNAL
);
100 if (_res
.options
& RES_DEBUG
)
101 printf(";; res_mkquery(%d, %s, %d, %d)\n",
102 op
, dname
, class, type
);
105 * Initialize header fields.
107 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
109 bzero(buf
, HFIXEDSZ
);
111 hp
->id
= htons(++_res
.id
);
113 hp
->rd
= (_res
.options
& RES_RECURSE
) != 0;
120 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
122 * perform opcode specific processing
125 case QUERY
: /*FALLTHROUGH*/
127 if ((buflen
-= QFIXEDSZ
) < 0)
129 if ((n
= dn_comp(dname
, cp
, buflen
, dnptrs
, lastdnptr
)) < 0)
133 __putshort(type
, cp
);
135 __putshort(class, cp
);
137 hp
->qdcount
= htons(1);
138 if (op
== QUERY
|| data
== NULL
)
141 * Make an additional record for completion domain.
144 n
= dn_comp((char *)data
, cp
, buflen
, dnptrs
, lastdnptr
);
149 __putshort(T_NULL
, cp
);
151 __putshort(class, cp
);
157 hp
->arcount
= htons(1);
162 * Initialize answer section
164 if (buflen
< 1 + RRFIXEDSZ
+ datalen
)
166 *cp
++ = '\0'; /* no domain name */
167 __putshort(type
, cp
);
169 __putshort(class, cp
);
173 __putshort(datalen
, cp
);
176 bcopy(data
, cp
, datalen
);
179 hp
->ancount
= htons(1);