source: trunk/minix/lib/ip/res_mkquery.c@ 12

Last change on this file since 12 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 1985 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21static char sccsid[] = "@(#)res_mkquery.c 6.12 (Berkeley) 6/1/90";
22#endif /* LIBC_SCCS and not lint */
23
24#if _MINIX
25#include <sys/types.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include <net/hton.h>
31#include <net/gen/in.h>
32#include <net/gen/nameser.h>
33#include <net/gen/resolv.h>
34
35#define bzero(b,l) memset(b,0,l)
36#define bcopy(s,d,l) memcpy(d,s,l)
37
38#define putshort __putshort
39#define putlong __putlong
40#else
41#include <stdio.h>
42#include <sys/types.h>
43#include <netinet/in.h>
44#include <arpa/nameser.h>
45#include <resolv.h>
46#endif
47
48#ifdef __STDC__
49#define _CONST const
50#else
51#define _CONST
52#endif
53
54/*
55 * Form all types of queries.
56 * Returns the size of the result or -1.
57 */
58res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
59 int op; /* opcode of query */
60 _CONST char *dname; /* domain name */
61 int class, type; /* class and type of query */
62 _CONST char *data; /* resource record data */
63 int datalen; /* length of data */
64 _CONST struct rrec *newrr; /* new rr for modify or append */
65 char *buf; /* buffer to put query */
66 int buflen; /* size of buffer */
67{
68 register dns_hdr_t *hp;
69 register char *cp;
70 register int n;
71 char *dnptrs[10], **dpp, **lastdnptr;
72
73#ifdef DEBUG
74 if (_res.options & RES_DEBUG)
75 printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
76#endif /* DEBUG */
77 /*
78 * Initialize header fields.
79 */
80 if ((buf == NULL) || (buflen < sizeof(dns_hdr_t)))
81 return(-1);
82 bzero(buf, sizeof(dns_hdr_t));
83 hp = (dns_hdr_t *) buf;
84 hp->dh_id = htons(++_res.id);
85 hp->dh_flag1= 0;
86 hp->dh_flag2= 0;
87 hp->dh_flag1 |= (op << 3) & DHF_OPCODE;
88 hp->dh_flag2 |= ((_res.options & RES_PRIMARY) != 0 ? 1 : 0) << 6;
89 hp->dh_flag1 |= (_res.options & RES_RECURSE) != 0 ? 1 : 0;
90 hp->dh_flag2 |= NOERROR & DHF_RCODE;
91 cp = buf + sizeof(dns_hdr_t);
92 buflen -= sizeof(dns_hdr_t);
93 dpp = dnptrs;
94 *dpp++ = buf;
95 *dpp++ = NULL;
96 lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]);
97 /*
98 * perform opcode specific processing
99 */
100 switch (op) {
101 case QUERY:
102 if ((buflen -= QFIXEDSZ) < 0)
103 return(-1);
104 if ((n = dn_comp((u8_t *)dname, (u8_t *)cp, buflen,
105 (u8_t **)dnptrs, (u8_t **)lastdnptr)) < 0)
106 return (-1);
107 cp += n;
108 buflen -= n;
109 putshort(type, (u8_t *)cp);
110 cp += sizeof(u_short);
111 putshort(class, (u8_t *)cp);
112 cp += sizeof(u_short);
113 hp->dh_qdcount = HTONS(1);
114 if (op == QUERY || data == NULL)
115 break;
116 /*
117 * Make an additional record for completion domain.
118 */
119 buflen -= RRFIXEDSZ;
120 if ((n = dn_comp((u8_t *)data, (u8_t *)cp, buflen,
121 (u8_t **)dnptrs, (u8_t **)lastdnptr)) < 0)
122 return (-1);
123 cp += n;
124 buflen -= n;
125 putshort(T_NULL, (u8_t *)cp);
126 cp += sizeof(u_short);
127 putshort(class, (u8_t *)cp);
128 cp += sizeof(u_short);
129 putlong(0, (u8_t *)cp);
130 cp += sizeof(u_long);
131 putshort(0, (u8_t *)cp);
132 cp += sizeof(u_short);
133 hp->dh_arcount = HTONS(1);
134 break;
135
136 case IQUERY:
137 /*
138 * Initialize answer section
139 */
140 if (buflen < 1 + RRFIXEDSZ + datalen)
141 return (-1);
142 *cp++ = '\0'; /* no domain name */
143 putshort(type, (u8_t *)cp);
144 cp += sizeof(u_short);
145 putshort(class, (u8_t *)cp);
146 cp += sizeof(u_short);
147 putlong(0, (u8_t *)cp);
148 cp += sizeof(u_long);
149 putshort(datalen, (u8_t *)cp);
150 cp += sizeof(u_short);
151 if (datalen) {
152 bcopy(data, cp, datalen);
153 cp += datalen;
154 }
155 hp->dh_ancount = HTONS(1);
156 break;
157
158#ifdef ALLOW_UPDATES
159 /*
160 * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
161 * (Record to be modified is followed by its replacement in msg.)
162 */
163 case UPDATEM:
164 case UPDATEMA:
165
166 case UPDATED:
167 /*
168 * The res code for UPDATED and UPDATEDA is the same; user
169 * calls them differently: specifies data for UPDATED; server
170 * ignores data if specified for UPDATEDA.
171 */
172 case UPDATEDA:
173 buflen -= RRFIXEDSZ + datalen;
174 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
175 return (-1);
176 cp += n;
177 putshort(type, cp);
178 cp += sizeof(u_short);
179 putshort(class, cp);
180 cp += sizeof(u_short);
181 putlong(0, cp);
182 cp += sizeof(u_long);
183 putshort(datalen, cp);
184 cp += sizeof(u_short);
185 if (datalen) {
186 bcopy(data, cp, datalen);
187 cp += datalen;
188 }
189 if ( (op == UPDATED) || (op == UPDATEDA) ) {
190 hp->ancount = HTONS(0);
191 break;
192 }
193 /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
194
195 case UPDATEA: /* Add new resource record */
196 buflen -= RRFIXEDSZ + datalen;
197 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
198 return (-1);
199 cp += n;
200 putshort(newrr->r_type, cp);
201 cp += sizeof(u_short);
202 putshort(newrr->r_class, cp);
203 cp += sizeof(u_short);
204 putlong(0, cp);
205 cp += sizeof(u_long);
206 putshort(newrr->r_size, cp);
207 cp += sizeof(u_short);
208 if (newrr->r_size) {
209 bcopy(newrr->r_data, cp, newrr->r_size);
210 cp += newrr->r_size;
211 }
212 hp->ancount = HTONS(0);
213 break;
214
215#endif /* ALLOW_UPDATES */
216 }
217 return (cp - buf);
218}
Note: See TracBrowser for help on using the repository browser.