Sync zoneinfo database with tzdata2010g from elsie.
[dragonfly.git] / libexec / bootpd / dovend.c
blobfecc20423e3e745a6bbc13f9cf1a3e806f419a44
1 /*
2 * dovend.c : Inserts all but the first few vendor options.
4 * $FreeBSD: src/libexec/bootpd/dovend.c,v 1.5.2.1 2003/02/15 05:36:01 kris Exp $
5 * $DragonFly: src/libexec/bootpd/dovend.c,v 1.2 2003/06/17 04:27:07 dillon Exp $
6 */
8 #include <sys/types.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h> /* inet_ntoa */
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <syslog.h>
19 #ifndef USE_BFUNCS
20 # include <memory.h>
21 /* Yes, memcpy is OK here (no overlapped copies). */
22 # define bcopy(a,b,c) memcpy(b,a,c)
23 # define bzero(p,l) memset(p,0,l)
24 # define bcmp(a,b,c) memcmp(a,b,c)
25 # define index strchr
26 #endif
28 #include "bootp.h"
29 #include "bootpd.h"
30 #include "report.h"
31 #include "dovend.h"
33 PRIVATE int insert_generic(struct shared_bindata *, byte **, int *);
36 * Insert the 2nd part of the options into an option buffer.
37 * Return amount of space used.
39 * This inserts everything EXCEPT:
40 * magic cookie, subnet mask, gateway, bootsize, extension file
41 * Those are handled separately (in bootpd.c) to allow this function
42 * to be shared between bootpd and bootpef.
44 * When an "extension file" is in use, the options inserted by
45 * this function go into the exten_file, not the bootp response.
48 int
49 dovend_rfc1497(struct host *hp, byte *buf, int len)
51 int bytesleft = len;
52 byte *vp = buf;
54 static const char noroom[] = "%s: No room for \"%s\" option";
55 #define NEED(LEN, MSG) do \
56 if (bytesleft < (LEN)) { \
57 report(LOG_NOTICE, noroom, \
58 hp->hostname->string, MSG); \
59 return (vp - buf); \
60 } while (0)
63 * Note that the following have already been inserted:
64 * magic_cookie, subnet_mask, gateway, bootsize
66 * The remaining options are inserted in order of importance.
67 * (Of course the importance of each is a matter of opinion.)
68 * The option insertion order should probably be configurable.
70 * This is the order used in the NetBSD version. Can anyone
71 * explain why the time_offset and swap_server are first?
72 * Also, why is the hostname so far down the list? -gwr
75 if (hp->flags.time_offset) {
76 NEED(6, "to");
77 *vp++ = TAG_TIME_OFFSET;/* -1 byte */
78 *vp++ = 4; /* -1 byte */
79 insert_u_long(htonl(hp->time_offset), &vp); /* -4 bytes */
80 bytesleft -= 6;
83 * swap server, root path, dump path
85 if (hp->flags.swap_server) {
86 NEED(6, "sw");
87 /* There is just one SWAP_SERVER, so it is not an iplist. */
88 *vp++ = TAG_SWAP_SERVER;/* -1 byte */
89 *vp++ = 4; /* -1 byte */
90 insert_u_long(hp->swap_server.s_addr, &vp); /* -4 bytes */
91 bytesleft -= 6; /* Fix real count */
93 if (hp->flags.root_path) {
95 * Check for room for root_path. Add 2 to account for
96 * TAG_ROOT_PATH and length.
98 len = strlen(hp->root_path->string);
99 NEED((len + 2), "rp");
100 *vp++ = TAG_ROOT_PATH;
101 *vp++ = (byte) (len & 0xFF);
102 bcopy(hp->root_path->string, vp, len);
103 vp += len;
104 bytesleft -= len + 2;
106 if (hp->flags.dump_file) {
108 * Check for room for dump_file. Add 2 to account for
109 * TAG_DUMP_FILE and length.
111 len = strlen(hp->dump_file->string);
112 NEED((len + 2), "df");
113 *vp++ = TAG_DUMP_FILE;
114 *vp++ = (byte) (len & 0xFF);
115 bcopy(hp->dump_file->string, vp, len);
116 vp += len;
117 bytesleft -= len + 2;
120 * DNS server and domain
122 if (hp->flags.domain_server) {
123 if (insert_ip(TAG_DOMAIN_SERVER,
124 hp->domain_server,
125 &vp, &bytesleft))
126 NEED(8, "ds");
128 if (hp->flags.domain_name) {
130 * Check for room for domain_name. Add 2 to account for
131 * TAG_DOMAIN_NAME and length.
133 len = strlen(hp->domain_name->string);
134 NEED((len + 2), "dn");
135 *vp++ = TAG_DOMAIN_NAME;
136 *vp++ = (byte) (len & 0xFF);
137 bcopy(hp->domain_name->string, vp, len);
138 vp += len;
139 bytesleft -= len + 2;
142 * NIS (YP) server and domain
144 if (hp->flags.nis_server) {
145 if (insert_ip(TAG_NIS_SERVER,
146 hp->nis_server,
147 &vp, &bytesleft))
148 NEED(8, "ds");
150 if (hp->flags.nis_domain) {
152 * Check for room for nis_domain. Add 2 to account for
153 * TAG_NIS_DOMAIN and length.
155 len = strlen(hp->nis_domain->string);
156 NEED((len + 2), "dn");
157 *vp++ = TAG_NIS_DOMAIN;
158 *vp++ = (byte) (len & 0xFF);
159 bcopy(hp->nis_domain->string, vp, len);
160 vp += len;
161 bytesleft -= len + 2;
163 /* IEN 116 name server */
164 if (hp->flags.name_server) {
165 if (insert_ip(TAG_NAME_SERVER,
166 hp->name_server,
167 &vp, &bytesleft))
168 NEED(8, "ns");
170 if (hp->flags.rlp_server) {
171 if (insert_ip(TAG_RLP_SERVER,
172 hp->rlp_server,
173 &vp, &bytesleft))
174 NEED(8, "rl");
176 /* Time server (RFC 868) */
177 if (hp->flags.time_server) {
178 if (insert_ip(TAG_TIME_SERVER,
179 hp->time_server,
180 &vp, &bytesleft))
181 NEED(8, "ts");
183 /* NTP (time) Server (RFC 1129) */
184 if (hp->flags.ntp_server) {
185 if (insert_ip(TAG_NTP_SERVER,
186 hp->ntp_server,
187 &vp, &bytesleft))
188 NEED(8, "ts");
191 * I wonder: If the hostname were "promoted" into the BOOTP
192 * response part, might these "extension" files possibly be
193 * shared between several clients?
195 * Also, why not just use longer BOOTP packets with all the
196 * additional length used as option data. This bootpd version
197 * already supports that feature by replying with the same
198 * packet length as the client request packet. -gwr
200 if (hp->flags.name_switch && hp->flags.send_name) {
202 * Check for room for hostname. Add 2 to account for
203 * TAG_HOST_NAME and length.
205 len = strlen(hp->hostname->string);
206 #if 0
208 * XXX - Too much magic. The user can always set the hostname
209 * to the short version in the bootptab file. -gwr
211 if ((len + 2) > bytesleft) {
213 * Not enough room for full (domain-qualified) hostname, try
214 * stripping it down to just the first field (host).
216 char *tmpstr = hp->hostname->string;
217 len = 0;
218 while (*tmpstr && (*tmpstr != '.')) {
219 tmpstr++;
220 len++;
223 #endif
224 NEED((len + 2), "hn");
225 *vp++ = TAG_HOST_NAME;
226 *vp++ = (byte) (len & 0xFF);
227 bcopy(hp->hostname->string, vp, len);
228 vp += len;
229 bytesleft -= len + 2;
232 * The rest of these are less important, so they go last.
234 if (hp->flags.lpr_server) {
235 if (insert_ip(TAG_LPR_SERVER,
236 hp->lpr_server,
237 &vp, &bytesleft))
238 NEED(8, "lp");
240 if (hp->flags.cookie_server) {
241 if (insert_ip(TAG_COOKIE_SERVER,
242 hp->cookie_server,
243 &vp, &bytesleft))
244 NEED(8, "cs");
246 if (hp->flags.log_server) {
247 if (insert_ip(TAG_LOG_SERVER,
248 hp->log_server,
249 &vp, &bytesleft))
250 NEED(8, "lg");
253 * XXX - Add new tags here (to insert options)
255 if (hp->flags.generic) {
256 if (insert_generic(hp->generic, &vp, &bytesleft))
257 NEED(64, "(generic)");
260 * The end marker is inserted by the caller.
262 return (vp - buf);
263 #undef NEED
264 } /* dovend_rfc1497 */
269 * Insert a tag value, a length value, and a list of IP addresses into the
270 * memory buffer indirectly pointed to by "dest". "tag" is the RFC1048 tag
271 * number to use, "iplist" is a pointer to a list of IP addresses
272 * (struct in_addr_list), and "bytesleft" points to an integer which
273 * indicates the size of the "dest" buffer.
275 * Return zero if everything fits.
277 * This is used to fill the vendor-specific area of a bootp packet in
278 * conformance to RFC1048.
282 insert_ip(byte tag, struct in_addr_list *iplist, byte **dest, int *bytesleft)
284 struct in_addr *addrptr;
285 unsigned addrcount = 1;
286 byte *d;
288 if (iplist == NULL)
289 return (0);
291 if (*bytesleft >= 6) {
292 d = *dest; /* Save pointer for later */
293 **dest = tag;
294 (*dest) += 2;
295 (*bytesleft) -= 2; /* Account for tag and length */
296 addrptr = iplist->addr;
297 addrcount = iplist->addrcount;
298 while ((*bytesleft >= 4) && (addrcount > 0)) {
299 insert_u_long(addrptr->s_addr, dest);
300 addrptr++;
301 addrcount--;
302 (*bytesleft) -= 4; /* Four bytes per address */
304 d[1] = (byte) ((*dest - d - 2) & 0xFF);
306 return (addrcount);
312 * Insert generic data into a bootp packet. The data is assumed to already
313 * be in RFC1048 format. It is inserted using a first-fit algorithm which
314 * attempts to insert as many tags as possible. Tags and data which are
315 * too large to fit are skipped; any remaining tags are tried until they
316 * have all been exhausted.
317 * Return zero if everything fits.
320 static int
321 insert_generic(struct shared_bindata *gendata, byte **buff, int *bytesleft)
323 byte *srcptr;
324 int length, numbytes;
325 int skipped = 0;
327 if (gendata == NULL)
328 return (0);
330 srcptr = gendata->data;
331 length = gendata->length;
332 while ((length > 0) && (*bytesleft > 0)) {
333 switch (*srcptr) {
334 case TAG_END:
335 length = 0; /* Force an exit on next iteration */
336 break;
337 case TAG_PAD:
338 *(*buff)++ = *srcptr++;
339 (*bytesleft)--;
340 length--;
341 break;
342 default:
343 numbytes = srcptr[1] + 2;
344 if (*bytesleft < numbytes)
345 skipped += numbytes;
346 else {
347 bcopy(srcptr, *buff, numbytes);
348 (*buff) += numbytes;
349 (*bytesleft) -= numbytes;
351 srcptr += numbytes;
352 length -= numbytes;
353 break;
355 } /* while */
356 return (skipped);
360 * Insert the unsigned long "value" into memory starting at the byte
361 * pointed to by the byte pointer (*dest). (*dest) is updated to
362 * point to the next available byte.
364 * Since it is desirable to internally store network addresses in network
365 * byte order (in struct in_addr's), this routine expects longs to be
366 * passed in network byte order.
368 * However, due to the nature of the main algorithm, the long must be in
369 * host byte order, thus necessitating the use of ntohl() first.
372 void
373 insert_u_long(u_int32 value, byte **dest)
375 byte *temp;
376 int n;
378 value = ntohl(value); /* Must use host byte order here */
379 temp = (*dest += 4);
380 for (n = 4; n > 0; n--) {
381 *--temp = (byte) (value & 0xFF);
382 value >>= 8;
384 /* Final result is network byte order */
388 * Local Variables:
389 * tab-width: 4
390 * c-indent-level: 4
391 * c-argdecl-indent: 4
392 * c-continued-statement-offset: 4
393 * c-continued-brace-offset: -4
394 * c-label-offset: -4
395 * c-brace-offset: 0
396 * End: