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