]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/res_mkquery.c
Update.
[thirdparty/glibc.git] / resolv / res_mkquery.c
1 /*
2 * ++Copyright++ 1985, 1993
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 * 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.
18 *
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
29 * SUCH DAMAGE.
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 * --Copyright--
50 */
51
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 */
56
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/nameser.h>
61
62 #include <stdio.h>
63 #include <netdb.h>
64 #include <resolv.h>
65 #if defined(BSD) && (BSD >= 199103)
66 # include <string.h>
67 #else
68 # include "../conf/portability.h"
69 #endif
70
71 #if defined(USE_OPTIONS_H)
72 # include <../conf/options.h>
73 #endif
74
75 /*
76 * Form all types of queries.
77 * Returns the size of the result or -1.
78 */
79 int
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 */
89 {
90 register HEADER *hp;
91 register u_char *cp;
92 register int n;
93 u_char *dnptrs[20], **dpp, **lastdnptr;
94
95 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
96 __set_h_errno (NETDB_INTERNAL);
97 return (-1);
98 }
99 #ifdef DEBUG
100 if (_res.options & RES_DEBUG)
101 printf(";; res_mkquery(%d, %s, %d, %d)\n",
102 op, dname, class, type);
103 #endif
104 /*
105 * Initialize header fields.
106 */
107 if ((buf == NULL) || (buflen < HFIXEDSZ))
108 return (-1);
109 bzero(buf, HFIXEDSZ);
110 hp = (HEADER *) buf;
111 hp->id = htons(++_res.id);
112 hp->opcode = op;
113 hp->rd = (_res.options & RES_RECURSE) != 0;
114 hp->rcode = NOERROR;
115 cp = buf + HFIXEDSZ;
116 buflen -= HFIXEDSZ;
117 dpp = dnptrs;
118 *dpp++ = buf;
119 *dpp++ = NULL;
120 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
121 /*
122 * perform opcode specific processing
123 */
124 switch (op) {
125 case QUERY: /*FALLTHROUGH*/
126 case NS_NOTIFY_OP:
127 if ((buflen -= QFIXEDSZ) < 0)
128 return (-1);
129 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
130 return (-1);
131 cp += n;
132 buflen -= n;
133 __putshort(type, cp);
134 cp += INT16SZ;
135 __putshort(class, cp);
136 cp += INT16SZ;
137 hp->qdcount = htons(1);
138 if (op == QUERY || data == NULL)
139 break;
140 /*
141 * Make an additional record for completion domain.
142 */
143 buflen -= RRFIXEDSZ;
144 n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr);
145 if (n < 0)
146 return (-1);
147 cp += n;
148 buflen -= n;
149 __putshort(T_NULL, cp);
150 cp += INT16SZ;
151 __putshort(class, cp);
152 cp += INT16SZ;
153 __putlong(0, cp);
154 cp += INT32SZ;
155 __putshort(0, cp);
156 cp += INT16SZ;
157 hp->arcount = htons(1);
158 break;
159
160 case IQUERY:
161 /*
162 * Initialize answer section
163 */
164 if (buflen < 1 + RRFIXEDSZ + datalen)
165 return (-1);
166 *cp++ = '\0'; /* no domain name */
167 __putshort(type, cp);
168 cp += INT16SZ;
169 __putshort(class, cp);
170 cp += INT16SZ;
171 __putlong(0, cp);
172 cp += INT32SZ;
173 __putshort(datalen, cp);
174 cp += INT16SZ;
175 if (datalen) {
176 bcopy(data, cp, datalen);
177 cp += datalen;
178 }
179 hp->ancount = htons(1);
180 break;
181
182 default:
183 return (-1);
184 }
185 return (cp - buf);
186 }