]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/res_mkquery.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * ++Copyright++ 1985, 1993
28 * Copyright (c) 1985, 1993
29 * The Regents of the University of California. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
61 * Permission to use, copy, modify, and distribute this software for any
62 * purpose with or without fee is hereby granted, provided that the above
63 * copyright notice and this permission notice appear in all copies, and that
64 * the name of Digital Equipment Corporation not be used in advertising or
65 * publicity pertaining to distribution of the document or software without
66 * specific, written prior permission.
68 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
69 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
70 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
71 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
72 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
73 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
74 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
80 #if defined(LIBC_SCCS) && !defined(lint)
81 static char sccsid
[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
82 static char rcsid
[] = "$Id: res_mkquery.c,v 1.3 2003/02/18 17:29:24 majka Exp $";
83 #endif /* LIBC_SCCS and not lint */
85 #include <sys/param.h>
86 #include <netinet/in.h>
87 #include <arpa/nameser8_compat.h>
91 #include <resolv8_compat.h>
92 #if defined(BSD) && (BSD >= 199103)
95 # include "portability.h"
98 #if defined(USE_OPTIONS_H)
103 * Form all types of queries.
104 * Returns the size of the result or -1.
107 res_mkquery(op
, dname
, class, type
, data
, datalen
, newrr_in
, buf
, buflen
)
108 int op
; /* opcode of query */
109 const char *dname
; /* domain name */
110 int class, type
; /* class and type of query */
111 const u_char
*data
; /* resource record data */
112 int datalen
; /* length of data */
113 const u_char
*newrr_in
; /* new rr for modify or append */
114 u_char
*buf
; /* buffer to put query */
115 int buflen
; /* size of buffer */
121 struct rrec
*newrr
= (struct rrec
*) newrr_in
;
123 u_char
*dnptrs
[20], **dpp
, **lastdnptr
;
125 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
126 h_errno
= NETDB_INTERNAL
;
130 if (_res
.options
& RES_DEBUG
)
131 printf(";; res_mkquery(%d, %s, %d, %d)\n",
132 op
, dname
, class, type
);
135 * Initialize header fields.
137 if ((buf
== NULL
) || (buflen
< HFIXEDSZ
))
139 bzero(buf
, HFIXEDSZ
);
141 hp
->id
= htons(++_res
.id
);
143 hp
->rd
= (_res
.options
& RES_RECURSE
) != 0;
150 lastdnptr
= dnptrs
+ sizeof dnptrs
/ sizeof dnptrs
[0];
152 * perform opcode specific processing
155 case QUERY
: /*FALLTHROUGH*/
157 if ((buflen
-= QFIXEDSZ
) < 0)
159 if ((n
= dn_comp(dname
, cp
, buflen
, dnptrs
, lastdnptr
)) < 0)
163 __putshort(type
, cp
);
165 __putshort(class, cp
);
167 hp
->qdcount
= htons(1);
168 if (op
== QUERY
|| data
== NULL
)
171 * Make an additional record for completion domain.
174 n
= dn_comp((char *)data
, cp
, buflen
, dnptrs
, lastdnptr
);
179 __putshort(T_NULL
, cp
);
181 __putshort(class, cp
);
187 hp
->arcount
= htons(1);
192 * Initialize answer section
194 if (buflen
< 1 + RRFIXEDSZ
+ datalen
)
196 *cp
++ = '\0'; /* no domain name */
197 __putshort(type
, cp
);
199 __putshort(class, cp
);
203 __putshort(datalen
, cp
);
206 bcopy(data
, cp
, datalen
);
209 hp
->ancount
= htons(1);
214 * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
215 * (Record to be modified is followed by its replacement in msg.)
222 * The res code for UPDATED and UPDATEDA is the same; user
223 * calls them differently: specifies data for UPDATED; server
224 * ignores data if specified for UPDATEDA.
227 buflen
-= RRFIXEDSZ
+ datalen
;
228 if ((n
= dn_comp(dname
, cp
, buflen
, dnptrs
, lastdnptr
)) < 0)
231 __putshort(type
, cp
);
233 __putshort(class, cp
);
237 __putshort(datalen
, cp
);
240 bcopy(data
, cp
, datalen
);
243 if ( (op
== UPDATED
) || (op
== UPDATEDA
) ) {
244 hp
->ancount
= htons(0);
247 /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
249 case UPDATEA
: /* Add new resource record */
250 buflen
-= RRFIXEDSZ
+ datalen
;
251 if ((n
= dn_comp(dname
, cp
, buflen
, dnptrs
, lastdnptr
)) < 0)
254 __putshort(newrr
->r_type
, cp
);
256 __putshort(newrr
->r_class
, cp
);
260 __putshort(newrr
->r_size
, cp
);
263 bcopy(newrr
->r_data
, cp
, newrr
->r_size
);
266 hp
->ancount
= htons(0);
268 #endif /* ALLOW_UPDATES */