Watt-32 tcp/ip  2.2 dev-rel.10
res_mkqu.c
Go to the documentation of this file.
1 
5 /* ++Copyright++ 1985, 1993
6  * -
7  * Copyright (c) 1985, 1993
8  * The Regents of the University of California. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  * must display the following acknowledgement:
20  * This product includes software developed by the University of
21  * California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  * may be used to endorse or promote products derived from this software
24  * without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  * -
38  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
39  *
40  * Permission to use, copy, modify, and distribute this software for any
41  * purpose with or without fee is hereby granted, provided that the above
42  * copyright notice and this permission notice appear in all copies, and that
43  * the name of Digital Equipment Corporation not be used in advertising or
44  * publicity pertaining to distribution of the document or software without
45  * specific, written prior permission.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
48  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
50  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
51  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
52  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
53  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
54  * SOFTWARE.
55  * -
56  * --Copyright--
57  */
58 
59 #include "resolver.h"
60 
61 #if defined(USE_BIND)
62 
63 /*
64  * Form all types of queries.
65  * Returns the size of the result or -1.
66  */
67 int W32_CALL res_mkquery (
68  int op, /* opcode of query */
69  const char *dname, /* domain name */
70  int Class, /* class of query */
71  int type, /* type of query */
72  const u_char *data, /* resource record data */
73  int datalen, /* length of data */
74  const u_char *newrr_in, /* new rr for modify or append */
75  u_char *buf, /* buffer to put query */
76  int buflen) /* size of buffer */
77 {
78  struct rrec *newrr = (struct rrec *) newrr_in;
79  HEADER *hp;
80  u_char *cp;
81  u_char *dnptrs[20];
82  u_char **dpp, **lastdnptr;
83  int n;
84 
85  if ((_res.options & RES_INIT) == 0 && res_init() == -1)
86  {
87  h_errno = NETDB_INTERNAL;
88  return (-1);
89  }
90 
91  if (_res.options & RES_DEBUG)
92  (*_printf) (";; res_mkquery(%d, %s, %d, %d)\n", op, dname, Class, type);
93 
94  /* Initialize header fields.
95  */
96  if (!buf || buflen < HFIXEDSZ)
97  return (-1);
98 
99  memset (buf, 0, HFIXEDSZ);
100  hp = (HEADER*) buf;
101  hp->id = htons (++_res.id);
102  hp->opcode = op;
103  hp->rd = (_res.options & RES_RECURSE) != 0;
104  hp->rcode = NOERROR;
105  cp = buf + HFIXEDSZ;
106  buflen -= HFIXEDSZ;
107  dpp = dnptrs;
108  *dpp++ = buf;
109  *dpp++ = NULL;
110  lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
111 
112  /* perform opcode specific processing
113  */
114  switch (op)
115  {
116  case STATUS:
117  return (-1);
118 
119  case QUERY:
120  /* fallthrough */
121 
122  case NS_NOTIFY_OP: /* Notify secondary server of SOA change */
123  buflen -= QFIXEDSZ;
124  if (buflen < 0)
125  return (-1);
126 
127  if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
128  return (-1);
129  cp += n;
130  buflen -= n;
131  __putshort (type, cp);
132  cp += INT16SZ;
133  __putshort (Class, cp);
134  cp += INT16SZ;
135  hp->qdcount = htons (1);
136  if (op == QUERY || !data)
137  break;
138  /*
139  * Make an additional record for completion domain.
140  */
141  buflen -= RRFIXEDSZ;
142  n = dn_comp ((char*)data, cp, buflen, dnptrs, lastdnptr);
143  if (n < 0)
144  return (-1);
145  cp += n;
146  buflen -= n;
147  __putshort (T_NULL, cp);
148  cp += INT16SZ;
149  __putshort (Class, cp);
150  cp += INT16SZ;
151  __putlong (0, cp);
152  cp += INT32SZ;
153  __putshort (0, cp);
154  cp += INT16SZ;
155  hp->arcount = htons (1);
156  break;
157 
158  case IQUERY:
159  /*
160  * Initialize answer section
161  */
162  if (buflen < 1 + RRFIXEDSZ + datalen)
163  return (-1);
164  *cp++ = '\0'; /* no domain name */
165  __putshort (type, cp);
166  cp += INT16SZ;
167  __putshort (Class, cp);
168  cp += INT16SZ;
169  __putlong (0, cp);
170  cp += INT32SZ;
171  __putshort (datalen, cp);
172  cp += INT16SZ;
173  if (datalen)
174  {
175  memcpy (cp,data,datalen);
176  cp += datalen;
177  }
178  hp->ancount = htons (1);
179  break;
180 
181 #if ALLOW_UPDATES
182  /*
183  * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
184  * (Record to be modified is followed by its replacement in msg.)
185  */
186  case UPDATEM:
187  case UPDATEMA:
188  case UPDATED:
189  /*
190  * The res code for UPDATED and UPDATEDA is the same; user
191  * calls them differently: specifies data for UPDATED; server
192  * ignores data if specified for UPDATEDA.
193  */
194  case UPDATEDA:
195  buflen -= RRFIXEDSZ + datalen;
196  if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
197  return (-1);
198  cp += n;
199  __putshort (type, cp);
200  cp += INT16SZ;
201  __putshort (Class, cp);
202  cp += INT16SZ;
203  __putlong (0, cp);
204  cp += INT32SZ;
205  __putshort (datalen, cp);
206  cp += INT16SZ;
207  if (datalen)
208  {
209  memcpy (cp,data,datalen);
210  cp += datalen;
211  }
212  if (op == UPDATED || op == UPDATEDA)
213  {
214  hp->ancount = 0;
215  break;
216  }
217  /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA
218  */
219 
220  case UPDATEA: /* Add new resource record */
221  buflen -= RRFIXEDSZ + datalen;
222  if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
223  return (-1);
224  cp += n;
225  __putshort (newrr->r_type, cp);
226  cp += INT16SZ;
227  __putshort (newrr->r_class, cp);
228  cp += INT16SZ;
229  __putlong (0, cp);
230  cp += INT32SZ;
231  __putshort (newrr->r_size, cp);
232  cp += INT16SZ;
233  if (newrr->r_size)
234  {
235  memcpy (cp,newrr->r_data,newrr->r_size);
236  cp += newrr->r_size;
237  }
238  hp->ancount = 0;
239  break;
240 #endif
241  default:
242  return (-1);
243  }
244 
245 #if ALLOW_UPDATES == 0
246  ARGSUSED (newrr);
247  ARGSUSED (newrr_in);
248 #endif
249 
250  return (cp - buf);
251 }
252 #endif /* USE_BIND */
253 
Definition: nameser.h:264
int W32_CALL res_mkquery(int op, const char *dname, int Class, int type, const u_char *data, int datalen, const u_char *newrr_in, u_char *buf, int buflen)
Definition: res_mkqu.c:67