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(int s);
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 /*
126  * Initialization such as command-line processing is done, then
127  * the receiver loop is started.  Die when interrupted.
128  */
129 
130 int
131 main(argc, argv)
132 	int argc;
133 	char **argv;
134 {
135 	struct bootp *bp;
136 	struct servent *sep;
137 	struct hostent *hep;
138 
139 	char *servername = NULL;
140 	char *vendor_file = NULL;
141 	char *bp_file = NULL;
142 	int32 server_addr;			/* inet addr, network order */
143 	int s;						/* Socket file descriptor */
144 	int n, fromlen, recvcnt;
145 	int use_hwa = 0;
146 	int32 vend_magic;
147 	int32 xid;
148 
149 	progname = strrchr(argv[0], '/');
150 	if (progname)
151 		progname++;
152 	else
153 		progname = argv[0];
154 	argc--;
155 	argv++;
156 
157 	if (debug)
158 		printf("%s: version %s.%d\n", progname, VERSION, PATCHLEVEL);
159 
160 	/*
161 	 * Verify that "struct bootp" has the correct official size.
162 	 * (Catch evil compilers that do struct padding.)
163 	 */
164 	assert(sizeof(struct bootp) == BP_MINPKTSZ);
165 
166 	if (uname(&my_uname) < 0)
167 		errx(1, "can't get hostname");
168 	hostname = my_uname.nodename;
169 
170 	sndbuf = malloc(BUFLEN);
171 	rcvbuf = malloc(BUFLEN);
172 	if (!sndbuf || !rcvbuf) {
173 		printf("malloc failed\n");
174 		exit(1);
175 	}
176 
177 	/* default magic number */
178 	bcopy(vm_rfc1048, (char*)&vend_magic, 4);
179 
180 	/* Handle option switches. */
181 	while (argc > 0) {
182 		if (argv[0][0] != '-')
183 			break;
184 		switch (argv[0][1]) {
185 
186 		case 'f':				/* File name to request. */
187 			if (argc < 2)
188 				goto error;
189 			argc--; argv++;
190 			bp_file = *argv;
191 			break;
192 
193 		case 'h':				/* Use hardware address. */
194 			use_hwa = 1;
195 			break;
196 
197 		case 'm':				/* Magic number value. */
198 			if (argc < 2)
199 				goto error;
200 			argc--; argv++;
201 			vend_magic = inet_addr(*argv);
202 			break;
203 
204 		error:
205 		default:
206 			puts(usage);
207 			exit(1);
208 
209 		}
210 		argc--;
211 		argv++;
212 	}
213 
214 	/* Get server name (or address) for query. */
215 	if (argc > 0) {
216 		servername = *argv;
217 		argc--;
218 		argv++;
219 	}
220 	/* Get optional vendor-data-template-file. */
221 	if (argc > 0) {
222 		vendor_file = *argv;
223 		argc--;
224 		argv++;
225 	}
226 	if (!servername) {
227 		printf("missing server name.\n");
228 		puts(usage);
229 		exit(1);
230 	}
231 	/*
232 	 * Create a socket.
233 	 */
234 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
235 		perror("socket");
236 		exit(1);
237 	}
238 	/*
239 	 * Get server's listening port number
240 	 */
241 	sep = getservbyname("bootps", "udp");
242 	if (sep) {
243 		bootps_port = ntohs((u_short) sep->s_port);
244 	} else {
245 		warnx("bootps/udp: unknown service -- using port %d",
246 				IPPORT_BOOTPS);
247 		bootps_port = (u_short) IPPORT_BOOTPS;
248 	}
249 
250 	/*
251 	 * Set up server socket address (for send)
252 	 */
253 	if (servername) {
254 		if (isdigit(servername[0]))
255 			server_addr = inet_addr(servername);
256 		else {
257 			hep = gethostbyname(servername);
258 			if (!hep)
259 				errx(1, "%s: unknown host", servername);
260 			bcopy(hep->h_addr, &server_addr, sizeof(server_addr));
261 		}
262 	} else {
263 		/* Get broadcast address */
264 		/* XXX - not yet */
265 		server_addr = INADDR_ANY;
266 	}
267 	sin_server.sin_family = AF_INET;
268 	sin_server.sin_port = htons(bootps_port);
269 	sin_server.sin_addr.s_addr = server_addr;
270 
271 	/*
272 	 * Get client's listening port number
273 	 */
274 	sep = getservbyname("bootpc", "udp");
275 	if (sep) {
276 		bootpc_port = ntohs(sep->s_port);
277 	} else {
278 		warnx("bootpc/udp: unknown service -- using port %d",
279 				IPPORT_BOOTPC);
280 		bootpc_port = (u_short) IPPORT_BOOTPC;
281 	}
282 
283 	/*
284 	 * Set up client socket address (for listen)
285 	 */
286 	sin_client.sin_family = AF_INET;
287 	sin_client.sin_port = htons(bootpc_port);
288 	sin_client.sin_addr.s_addr = INADDR_ANY;
289 
290 	/*
291 	 * Bind client socket to BOOTPC port.
292 	 */
293 	if (bind(s, (struct sockaddr *) &sin_client, sizeof(sin_client)) < 0) {
294 		if (errno == EACCES) {
295 			warn("bind BOOTPC port");
296 			errx(1, "you need to run this as root");
297 		}
298 		else
299 			err(1, "bind BOOTPC port");
300 	}
301 	/*
302 	 * Build a request.
303 	 */
304 	bp = (struct bootp *) sndbuf;
305 	bzero(bp, sizeof(*bp));
306 	bp->bp_op = BOOTREQUEST;
307 	xid = (int32) getpid();
308 	bp->bp_xid = (u_int32) htonl(xid);
309 	if (bp_file)
310 		strncpy(bp->bp_file, bp_file, BP_FILE_LEN);
311 
312 	/*
313 	 * Fill in the hardware address (or client IP address)
314 	 */
315 	if (use_hwa) {
316 		struct ifreq *ifr;
317 
318 		ifr = getif(s, &sin_server.sin_addr);
319 		if (!ifr) {
320 			printf("No interface for %s\n", servername);
321 			exit(1);
322 		}
323 		if (getether(ifr->ifr_name, (char*)eaddr)) {
324 			printf("Can not get ether addr for %s\n", ifr->ifr_name);
325 			exit(1);
326 		}
327 		/* Copy Ethernet address into request packet. */
328 		bp->bp_htype = 1;
329 		bp->bp_hlen = 6;
330 		bcopy(eaddr, bp->bp_chaddr, bp->bp_hlen);
331 	} else {
332 		/* Fill in the client IP address. */
333 		hep = gethostbyname(hostname);
334 		if (!hep) {
335 			printf("Can not get my IP address\n");
336 			exit(1);
337 		}
338 		bcopy(hep->h_addr, &bp->bp_ciaddr, hep->h_length);
339 	}
340 
341 	/*
342 	 * Copy in the default vendor data.
343 	 */
344 	bcopy((char*)&vend_magic, bp->bp_vend, 4);
345 	if (vend_magic)
346 		bp->bp_vend[4] = TAG_END;
347 
348 	/*
349 	 * Read in the "options" part of the request.
350 	 * This also determines the size of the packet.
351 	 */
352 	snaplen = sizeof(*bp);
353 	if (vendor_file) {
354 		int fd = open(vendor_file, 0);
355 		if (fd < 0) {
356 			perror(vendor_file);
357 			exit(1);
358 		}
359 		/* Compute actual space for options. */
360 		n = BUFLEN - sizeof(*bp) + BP_VEND_LEN;
361 		n = read(fd, bp->bp_vend, n);
362 		close(fd);
363 		if (n < 0) {
364 			perror(vendor_file);
365 			exit(1);
366 		}
367 		printf("read %d bytes of vendor template\n", n);
368 		if (n > BP_VEND_LEN) {
369 			printf("warning: extended options in use (len > %d)\n",
370 				   BP_VEND_LEN);
371 			snaplen += (n - BP_VEND_LEN);
372 		}
373 	}
374 	/*
375 	 * Set globals needed by print_bootp
376 	 * (called by send_request)
377 	 */
378 	packetp = (unsigned char *) eaddr;
379 	snapend = (unsigned char *) sndbuf + snaplen;
380 
381 	/* Send a request once per second while waiting for replies. */
382 	recvcnt = 0;
383 	bp->bp_secs = secs = 0;
384 	send_request(s);
385 	while (1) {
386 		struct timeval tv;
387 		int readfds;
388 
389 		tv.tv_sec = WAITSECS;
390 		tv.tv_usec = 0L;
391 		readfds = (1 << s);
392 		n = select(s + 1, (fd_set *) & readfds, NULL, NULL, &tv);
393 		if (n < 0) {
394 			perror("select");
395 			break;
396 		}
397 		if (n == 0) {
398 			/*
399 			 * We have not received a response in the last second.
400 			 * If we have ever received any responses, exit now.
401 			 * Otherwise, bump the "wait time" field and re-send.
402 			 */
403 			if (recvcnt > 0)
404 				exit(0);
405 			secs += WAITSECS;
406 			if (secs > MAXWAIT)
407 				break;
408 			bp->bp_secs = htons(secs);
409 			send_request(s);
410 			continue;
411 		}
412 		fromlen = sizeof(sin_from);
413 		n = recvfrom(s, rcvbuf, BUFLEN, 0,
414 					 (struct sockaddr *) &sin_from, &fromlen);
415 		if (n <= 0) {
416 			continue;
417 		}
418 		if (n < sizeof(struct bootp)) {
419 			printf("received short packet\n");
420 			continue;
421 		}
422 		recvcnt++;
423 
424 		/* Print the received packet. */
425 		printf("Recvd from %s", inet_ntoa(sin_from.sin_addr));
426 		/* set globals needed by bootp_print() */
427 		snaplen = n;
428 		snapend = (unsigned char *) rcvbuf + snaplen;
429 		bootp_print((struct bootp *)rcvbuf, n, sin_from.sin_port, 0);
430 		putchar('\n');
431 		/*
432 		 * This no longer exits immediately after receiving
433 		 * one response because it is useful to know if the
434 		 * client might get multiple responses.  This code
435 		 * will now listen for one second after a response.
436 		 */
437 	}
438 	errx(1, "no response from %s", servername);
439 }
440 
441 static void
442 send_request(s)
443 	int s;
444 {
445 	/* Print the request packet. */
446 	printf("Sending to %s", inet_ntoa(sin_server.sin_addr));
447 	bootp_print((struct bootp *)sndbuf, snaplen, sin_from.sin_port, 0);
448 	putchar('\n');
449 
450 	/* Send the request packet. */
451 	if (sendto(s, sndbuf, snaplen, 0,
452 			   (struct sockaddr *) &sin_server,
453 			   sizeof(sin_server)) < 0)
454 	{
455 		perror("sendto server");
456 		exit(1);
457 	}
458 }
459 
460 /*
461  * Print out a filename (or other ascii string).
462  * Return true if truncated.
463  */
464 int
465 printfn(s, ep)
466 	u_char *s, *ep;
467 {
468 	u_char c;
469 
470 	putchar('"');
471 	while ((c = *s++) != '\0') {
472 		if (s > ep) {
473 			putchar('"');
474 			return (1);
475 		}
476 		if (!isascii(c)) {
477 			c = toascii(c);
478 			putchar('M');
479 			putchar('-');
480 		}
481 		if (!isprint(c)) {
482 			c ^= 0x40;			/* DEL to ?, others to alpha */
483 			putchar('^');
484 		}
485 		putchar(c);
486 	}
487 	putchar('"');
488 	return (0);
489 }
490 
491 /*
492  * Convert an IP addr to a string.
493  * (like inet_ntoa, but ina is a pointer)
494  */
495 char *
496 ipaddr_string(ina)
497 	struct in_addr *ina;
498 {
499 	static char b[24];
500 	u_char *p;
501 
502 	p = (u_char *) ina;
503 	snprintf(b, sizeof(b), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
504 	return (b);
505 }
506 
507 /*
508  * Local Variables:
509  * tab-width: 4
510  * c-indent-level: 4
511  * c-argdecl-indent: 4
512  * c-continued-statement-offset: 4
513  * c-continued-brace-offset: -4
514  * c-label-offset: -4
515  * c-brace-offset: 0
516  * End:
517  */
518