bootptest.c revision 1.10 1 /* $NetBSD: bootptest.c,v 1.10 2002/09/18 23:13:40 mycroft Exp $ */
2
3 /*
4 * bootptest.c - Test out a bootp server.
5 *
6 * This simple program was put together from pieces taken from
7 * various places, including the CMU BOOTP client and server.
8 * The packet printing routine is from the Berkeley "tcpdump"
9 * program with some enhancements I added. The print-bootp.c
10 * file was shared with my copy of "tcpdump" and therefore uses
11 * some unusual utility routines that would normally be provided
12 * by various parts of the tcpdump program. Gordon W. Ross
13 *
14 * Boilerplate:
15 *
16 * This program includes software developed by the University of
17 * California, Lawrence Berkeley Laboratory and its contributors.
18 * (See the copyright notice in print-bootp.c)
19 *
20 * The remainder of this program is public domain. You may do
21 * whatever you like with it except claim that you wrote it.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * HISTORY:
28 *
29 * 12/02/93 Released version 1.4 (with bootp-2.3.2)
30 * 11/05/93 Released version 1.3
31 * 10/14/93 Released version 1.2
32 * 10/11/93 Released version 1.1
33 * 09/28/93 Released version 1.0
34 * 09/93 Original developed by Gordon W. Ross <gwr (at) mc.com>
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: bootptest.c,v 1.10 2002/09/18 23:13:40 mycroft Exp $");
40 #endif
41
42 char *usage = "bootptest [-h] server-name [vendor-data-template-file]";
43
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <sys/file.h>
48 #include <sys/time.h>
49 #include <sys/stat.h>
50 #include <sys/poll.h>
51
52 #include <net/if.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h> /* inet_ntoa */
55
56 #include <stdlib.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <string.h>
60 #include <errno.h>
61 #include <ctype.h>
62 #include <netdb.h>
63 #include <assert.h>
64 #include <unistd.h>
65
66 #include "bootp.h"
67 #include "bootptest.h"
68 #include "getif.h"
69 #include "report.h"
70 #include "patchlevel.h"
71
72 #define LOG_ERR 1
73 #define BUFLEN 1024
74 #define WAITSECS 1
75 #define MAXWAIT 10
76
77 int vflag = 1;
78 int tflag = 0;
79 int thiszone;
80 char *progname;
81 unsigned char *packetp;
82 unsigned char *snapend;
83 int snaplen;
84
85
86 /*
87 * IP port numbers for client and server obtained from /etc/services
88 */
89
90 u_short bootps_port, bootpc_port;
91
92
93 /*
94 * Internet socket and interface config structures
95 */
96
97 struct sockaddr_in sin_server; /* where to send requests */
98 struct sockaddr_in sin_client; /* for bind and listen */
99 struct sockaddr_in sin_from; /* Packet source */
100 u_char eaddr[16]; /* Ethernet address */
101
102 /*
103 * General
104 */
105
106 int debug = 1; /* Debugging flag (level) */
107 char hostname[MAXHOSTNAMELEN + 1];
108 char *sndbuf; /* Send packet buffer */
109 char *rcvbuf; /* Receive packet buffer */
110
111 /*
112 * Vendor magic cookies for CMU and RFC1048
113 */
114
115 unsigned char vm_cmu[4] = VM_CMU;
116 unsigned char vm_rfc1048[4] = VM_RFC1048;
117 short secs; /* How long client has waited */
118
119
120 extern int getether(char *, char *);
121 int main(int, char **);
122 void send_request(int);
123
124 /*
125 * Initialization such as command-line processing is done, then
126 * the receiver loop is started. Die when interrupted.
127 */
128
129 int
130 main(int argc, char **argv)
131 {
132 struct bootp *bp;
133 struct servent *sep;
134 struct hostent *hep;
135
136 char *servername = NULL;
137 char *vendor_file = NULL;
138 char *bp_file = NULL;
139 int s; /* Socket file descriptor */
140 int n, fromlen, recvcnt;
141 int use_hwa = 0;
142 int32 vend_magic;
143 int32 xid;
144
145 progname = strrchr(argv[0], '/');
146 if (progname)
147 progname++;
148 else
149 progname = argv[0];
150 argc--;
151 argv++;
152
153 if (debug)
154 printf("%s: version %s.%d\n", progname, VERSION, PATCHLEVEL);
155
156 /*
157 * Verify that "struct bootp" has the correct official size.
158 * (Catch evil compilers that do struct padding.)
159 */
160 assert(sizeof(struct bootp) == BP_MINPKTSZ);
161
162 sndbuf = malloc(BUFLEN);
163 rcvbuf = malloc(BUFLEN);
164 if (!sndbuf || !rcvbuf) {
165 printf("malloc failed\n");
166 exit(1);
167 }
168
169 /* default magic number */
170 bcopy(vm_rfc1048, (char*)&vend_magic, 4);
171
172 /* Handle option switches. */
173 while (argc > 0) {
174 if (argv[0][0] != '-')
175 break;
176 switch (argv[0][1]) {
177
178 case 'f': /* File name to reqest. */
179 if (argc < 2)
180 goto error;
181 argc--; argv++;
182 bp_file = *argv;
183 break;
184
185 case 'h': /* Use hardware address. */
186 use_hwa = 1;
187 break;
188
189 case 'm': /* Magic number value. */
190 if (argc < 2)
191 goto error;
192 argc--; argv++;
193 vend_magic = inet_addr(*argv);
194 break;
195
196 error:
197 default:
198 puts(usage);
199 exit(1);
200
201 }
202 argc--;
203 argv++;
204 }
205
206 /* Get server name (or address) for query. */
207 if (argc > 0) {
208 servername = *argv;
209 argc--;
210 argv++;
211 }
212 /* Get optional vendor-data-template-file. */
213 if (argc > 0) {
214 vendor_file = *argv;
215 argc--;
216 argv++;
217 }
218 if (!servername) {
219 printf("missing server name.\n");
220 puts(usage);
221 exit(1);
222 }
223 /*
224 * Create a socket.
225 */
226 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
227 perror("socket");
228 exit(1);
229 }
230 /*
231 * Get server's listening port number
232 */
233 sep = getservbyname("bootps", "udp");
234 if (sep) {
235 bootps_port = ntohs((u_short) sep->s_port);
236 } else {
237 fprintf(stderr, "udp/bootps: unknown service -- using port %d\n",
238 IPPORT_BOOTPS);
239 bootps_port = (u_short) IPPORT_BOOTPS;
240 }
241
242 /*
243 * Set up server socket address (for send)
244 */
245 if (servername) {
246 if (inet_aton(servername, &sin_server.sin_addr) == 0) {
247 hep = gethostbyname(servername);
248 if (!hep) {
249 fprintf(stderr, "%s: unknown host\n", servername);
250 exit(1);
251 }
252 memcpy(&sin_server.sin_addr, hep->h_addr,
253 sizeof(sin_server.sin_addr));
254 }
255 } else {
256 /* Get broadcast address */
257 /* XXX - not yet */
258 sin_server.sin_addr.s_addr = INADDR_ANY;
259 }
260 sin_server.sin_family = AF_INET;
261 sin_server.sin_port = htons(bootps_port);
262
263 /*
264 * Get client's listening port number
265 */
266 sep = getservbyname("bootpc", "udp");
267 if (sep) {
268 bootpc_port = ntohs(sep->s_port);
269 } else {
270 fprintf(stderr, "udp/bootpc: unknown service -- using port %d\n",
271 IPPORT_BOOTPC);
272 bootpc_port = (u_short) IPPORT_BOOTPC;
273 }
274
275 /*
276 * Set up client socket address (for listen)
277 */
278 sin_client.sin_family = AF_INET;
279 sin_client.sin_port = htons(bootpc_port);
280 sin_client.sin_addr.s_addr = INADDR_ANY;
281
282 /*
283 * Bind client socket to BOOTPC port.
284 */
285 if (bind(s, (struct sockaddr *) &sin_client, sizeof(sin_client)) < 0) {
286 perror("bind BOOTPC port");
287 if (errno == EACCES)
288 fprintf(stderr, "You need to run this as root\n");
289 exit(1);
290 }
291 /*
292 * Build a request.
293 */
294 bp = (struct bootp *) sndbuf;
295 bzero(bp, sizeof(*bp));
296 bp->bp_op = BOOTREQUEST;
297 xid = (int32) getpid();
298 bp->bp_xid = (u_int32) htonl(xid);
299 if (bp_file)
300 strncpy(bp->bp_file, bp_file, BP_FILE_LEN);
301
302 /*
303 * Fill in the hardware address (or client IP address)
304 */
305 if (use_hwa) {
306 struct ifreq *ifr;
307
308 ifr = getif(s, &sin_server.sin_addr);
309 if (!ifr) {
310 printf("No interface for %s\n", servername);
311 exit(1);
312 }
313 if (getether(ifr->ifr_name, eaddr)) {
314 printf("Can not get ether addr for %s\n", ifr->ifr_name);
315 exit(1);
316 }
317 /* Copy Ethernet address into request packet. */
318 bp->bp_htype = 1;
319 bp->bp_hlen = 6;
320 bcopy(eaddr, bp->bp_chaddr, bp->bp_hlen);
321 } else {
322 /* Fill in the client IP address. */
323 gethostname(hostname, sizeof(hostname));
324 hostname[sizeof(hostname) - 1] = '\0';
325 hep = gethostbyname(hostname);
326 if (!hep) {
327 printf("Can not get my IP address\n");
328 exit(1);
329 }
330 bcopy(hep->h_addr, &bp->bp_ciaddr, hep->h_length);
331 }
332
333 /*
334 * Copy in the default vendor data.
335 */
336 bcopy((char*)&vend_magic, bp->bp_vend, 4);
337 if (vend_magic)
338 bp->bp_vend[4] = TAG_END;
339
340 /*
341 * Read in the "options" part of the request.
342 * This also determines the size of the packet.
343 */
344 snaplen = sizeof(*bp);
345 if (vendor_file) {
346 int fd = open(vendor_file, 0);
347 if (fd < 0) {
348 perror(vendor_file);
349 exit(1);
350 }
351 /* Compute actual space for options. */
352 n = BUFLEN - sizeof(*bp) + BP_VEND_LEN;
353 n = read(fd, bp->bp_vend, n);
354 close(fd);
355 if (n < 0) {
356 perror(vendor_file);
357 exit(1);
358 }
359 printf("read %d bytes of vendor template\n", n);
360 if (n > BP_VEND_LEN) {
361 printf("warning: extended options in use (len > %d)\n",
362 BP_VEND_LEN);
363 snaplen += (n - BP_VEND_LEN);
364 }
365 }
366 /*
367 * Set globals needed by print_bootp
368 * (called by send_request)
369 */
370 packetp = (unsigned char *) eaddr;
371 snapend = (unsigned char *) sndbuf + snaplen;
372
373 /* Send a request once per second while waiting for replies. */
374 recvcnt = 0;
375 bp->bp_secs = secs = 0;
376 send_request(s);
377 while (1) {
378 struct pollfd set[1];
379
380 set[0].fd = s;
381 set[0].events = POLLIN;
382 n = poll(set, 1, WAITSECS * 1000);
383 if (n < 0) {
384 perror("poll");
385 break;
386 }
387 if (n == 0) {
388 /*
389 * We have not received a response in the last second.
390 * If we have ever received any responses, exit now.
391 * Otherwise, bump the "wait time" field and re-send.
392 */
393 if (recvcnt > 0)
394 exit(0);
395 secs += WAITSECS;
396 if (secs > MAXWAIT)
397 break;
398 bp->bp_secs = htons(secs);
399 send_request(s);
400 continue;
401 }
402 fromlen = sizeof(sin_from);
403 n = recvfrom(s, rcvbuf, BUFLEN, 0,
404 (struct sockaddr *) &sin_from, &fromlen);
405 if (n <= 0) {
406 continue;
407 }
408 if (n < sizeof(struct bootp)) {
409 printf("received short packet\n");
410 continue;
411 }
412 recvcnt++;
413
414 /* Print the received packet. */
415 printf("Recvd from %s", inet_ntoa(sin_from.sin_addr));
416 /* set globals needed by bootp_print() */
417 snaplen = n;
418 snapend = (unsigned char *) rcvbuf + snaplen;
419 bootp_print((struct bootp *)rcvbuf, n, sin_from.sin_port, 0);
420 putchar('\n');
421 /*
422 * This no longer exits immediately after receiving
423 * one response because it is useful to know if the
424 * client might get multiple responses. This code
425 * will now listen for one second after a response.
426 */
427 }
428 fprintf(stderr, "no response from %s\n", servername);
429 exit(1);
430 }
431
432 void
433 send_request(int s)
434 {
435 /* Print the request packet. */
436 printf("Sending to %s", inet_ntoa(sin_server.sin_addr));
437 bootp_print((struct bootp *)sndbuf, snaplen, sin_from.sin_port, 0);
438 putchar('\n');
439
440 /* Send the request packet. */
441 if (sendto(s, sndbuf, snaplen, 0,
442 (struct sockaddr *) &sin_server,
443 sizeof(sin_server)) < 0)
444 {
445 perror("sendto server");
446 exit(1);
447 }
448 }
449
450 /*
451 * Print out a filename (or other ascii string).
452 * Return true if truncated.
453 */
454 int
455 printfn(u_char *s, u_char *ep)
456 {
457 u_char c;
458
459 putchar('"');
460 while ((c = *s++) != 0) {
461 if (s > ep) {
462 putchar('"');
463 return (1);
464 }
465 if (!isascii(c)) {
466 c = toascii(c);
467 putchar('M');
468 putchar('-');
469 }
470 if (!isprint(c)) {
471 c ^= 0x40; /* DEL to ?, others to alpha */
472 putchar('^');
473 }
474 putchar(c);
475 }
476 putchar('"');
477 return (0);
478 }
479
480 /*
481 * Convert an IP addr to a string.
482 * (like inet_ntoa, but ina is a pointer)
483 */
484 char *
485 ipaddr_string(struct in_addr *ina)
486 {
487 static char b[24];
488 u_char *p;
489
490 p = (u_char *) ina;
491 sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
492 return (b);
493 }
494
495 /*
496 * Local Variables:
497 * tab-width: 4
498 * c-indent-level: 4
499 * c-argdecl-indent: 4
500 * c-continued-statement-offset: 4
501 * c-continued-brace-offset: -4
502 * c-label-offset: -4
503 * c-brace-offset: 0
504 * End:
505 */
506