xref: /netbsd-src/usr.sbin/bootp/bootptest/bootptest.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: bootptest.c,v 1.14 2004/11/17 13:28:31 hira 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@mc.com>
35  */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: bootptest.c,v 1.14 2004/11/17 13:28:31 hira Exp $");
40 #endif
41 
42 char *usage = "usage: %s [-f bootfile] [-h] [-m magic_number] server-name\n"
43 	      "                 [vendor-data-template-file]\n";
44 
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/file.h>
49 #include <sys/time.h>
50 #include <sys/stat.h>
51 #include <sys/poll.h>
52 
53 #include <net/if.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>			/* inet_ntoa */
56 
57 #include <stdlib.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <string.h>
61 #include <errno.h>
62 #include <ctype.h>
63 #include <netdb.h>
64 #include <assert.h>
65 #include <unistd.h>
66 
67 #include "bootp.h"
68 #include "bootptest.h"
69 #include "getif.h"
70 #include "report.h"
71 #include "patchlevel.h"
72 
73 #define LOG_ERR 1
74 #define BUFLEN 1024
75 #define WAITSECS 1
76 #define MAXWAIT  10
77 
78 int vflag = 1;
79 int tflag = 0;
80 int thiszone;
81 char *progname;
82 unsigned char *packetp;
83 unsigned char *snapend;
84 int snaplen;
85 
86 
87 /*
88  * IP port numbers for client and server obtained from /etc/services
89  */
90 
91 u_short bootps_port, bootpc_port;
92 
93 
94 /*
95  * Internet socket and interface config structures
96  */
97 
98 struct sockaddr_in sin_server;	/* where to send requests */
99 struct sockaddr_in sin_client;	/* for bind and listen */
100 struct sockaddr_in sin_from;	/* Packet source */
101 u_char eaddr[16];				/* Ethernet address */
102 
103 /*
104  * General
105  */
106 
107 int debug = 1;					/* Debugging flag (level) */
108 char hostname[MAXHOSTNAMELEN + 1];
109 char *sndbuf;					/* Send packet buffer */
110 char *rcvbuf;					/* Receive packet buffer */
111 
112 /*
113  * Vendor magic cookies for CMU and RFC1048
114  */
115 
116 unsigned char vm_cmu[4] = VM_CMU;
117 unsigned char vm_rfc1048[4] = VM_RFC1048;
118 short secs;						/* How long client has waited */
119 
120 
121 extern int getether(char *, char *);
122 int main(int, char **);
123 void send_request(int);
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(int argc, char **argv)
132 {
133 	struct bootp *bp;
134 	struct servent *sep;
135 	struct hostent *hep;
136 
137 	char *servername = NULL;
138 	char *vendor_file = NULL;
139 	char *bp_file = NULL;
140 	int s;				/* Socket file descriptor */
141 	int n, fromlen, recvcnt;
142 	int use_hwa = 0;
143 	int32 vend_magic;
144 	int32 xid;
145 	struct pollfd set[1];
146 
147 	progname = strrchr(argv[0], '/');
148 	if (progname)
149 		progname++;
150 	else
151 		progname = argv[0];
152 	argc--;
153 	argv++;
154 
155 	if (debug)
156 		printf("%s: version %s.%d\n", progname, VERSION, PATCHLEVEL);
157 
158 	/*
159 	 * Verify that "struct bootp" has the correct official size.
160 	 * (Catch evil compilers that do struct padding.)
161 	 */
162 	assert(sizeof(struct bootp) == BP_MINPKTSZ);
163 
164 	sndbuf = malloc(BUFLEN);
165 	rcvbuf = malloc(BUFLEN);
166 	if (!sndbuf || !rcvbuf) {
167 		printf("malloc failed\n");
168 		exit(1);
169 	}
170 
171 	/* default magic number */
172 	bcopy(vm_rfc1048, (char*)&vend_magic, 4);
173 
174 	/* Handle option switches. */
175 	while (argc > 0) {
176 		if (argv[0][0] != '-')
177 			break;
178 		switch (argv[0][1]) {
179 
180 		case 'f':				/* File name to reqest. */
181 			if (argc < 2)
182 				goto error;
183 			argc--; argv++;
184 			bp_file = *argv;
185 			break;
186 
187 		case 'h':				/* Use hardware address. */
188 			use_hwa = 1;
189 			break;
190 
191 		case 'm':				/* Magic number value. */
192 			if (argc < 2)
193 				goto error;
194 			argc--; argv++;
195 			vend_magic = inet_addr(*argv);
196 			break;
197 
198 		error:
199 		default:
200 			(void)fprintf(stderr, usage, getprogname());
201 			exit(1);
202 
203 		}
204 		argc--;
205 		argv++;
206 	}
207 
208 	/* Get server name (or address) for query. */
209 	if (argc > 0) {
210 		servername = *argv;
211 		argc--;
212 		argv++;
213 	}
214 	/* Get optional vendor-data-template-file. */
215 	if (argc > 0) {
216 		vendor_file = *argv;
217 		argc--;
218 		argv++;
219 	}
220 	if (!servername) {
221 		printf("missing server name.\n");
222 		(void)fprintf(stderr, usage, getprogname());
223 		exit(1);
224 	}
225 	/*
226 	 * Create a socket.
227 	 */
228 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
229 		perror("socket");
230 		exit(1);
231 	}
232 	/*
233 	 * Get server's listening port number
234 	 */
235 	sep = getservbyname("bootps", "udp");
236 	if (sep) {
237 		bootps_port = ntohs((u_short) sep->s_port);
238 	} else {
239 		fprintf(stderr, "udp/bootps: unknown service -- using port %d\n",
240 				IPPORT_BOOTPS);
241 		bootps_port = (u_short) IPPORT_BOOTPS;
242 	}
243 
244 	/*
245 	 * Set up server socket address (for send)
246 	 */
247 	if (servername) {
248 		if (inet_aton(servername, &sin_server.sin_addr) == 0) {
249 			hep = gethostbyname(servername);
250 			if (!hep) {
251 				fprintf(stderr, "%s: unknown host\n", servername);
252 				exit(1);
253 			}
254 			memcpy(&sin_server.sin_addr, hep->h_addr,
255 			    sizeof(sin_server.sin_addr));
256 		}
257 	} else {
258 		/* Get broadcast address */
259 		/* XXX - not yet */
260 		sin_server.sin_addr.s_addr = INADDR_ANY;
261 	}
262 	sin_server.sin_family = AF_INET;
263 	sin_server.sin_port = htons(bootps_port);
264 
265 	/*
266 	 * Get client's listening port number
267 	 */
268 	sep = getservbyname("bootpc", "udp");
269 	if (sep) {
270 		bootpc_port = ntohs(sep->s_port);
271 	} else {
272 		fprintf(stderr, "udp/bootpc: unknown service -- using port %d\n",
273 				IPPORT_BOOTPC);
274 		bootpc_port = (u_short) IPPORT_BOOTPC;
275 	}
276 
277 	/*
278 	 * Set up client socket address (for listen)
279 	 */
280 	sin_client.sin_family = AF_INET;
281 	sin_client.sin_port = htons(bootpc_port);
282 	sin_client.sin_addr.s_addr = INADDR_ANY;
283 
284 	/*
285 	 * Bind client socket to BOOTPC port.
286 	 */
287 	if (bind(s, (struct sockaddr *) &sin_client, sizeof(sin_client)) < 0) {
288 		perror("bind BOOTPC port");
289 		if (errno == EACCES)
290 			fprintf(stderr, "You need to run this as root\n");
291 		exit(1);
292 	}
293 	/*
294 	 * Build a request.
295 	 */
296 	bp = (struct bootp *) sndbuf;
297 	bzero(bp, sizeof(*bp));
298 	bp->bp_op = BOOTREQUEST;
299 	xid = (int32) getpid();
300 	bp->bp_xid = (u_int32) htonl(xid);
301 	if (bp_file)
302 		strlcpy(bp->bp_file, bp_file, sizeof(bp->bp_file));
303 
304 	/*
305 	 * Fill in the hardware address (or client IP address)
306 	 */
307 	if (use_hwa) {
308 		struct ifreq *ifr;
309 
310 		ifr = getif(s, &sin_server.sin_addr);
311 		if (!ifr) {
312 			printf("No interface for %s\n", servername);
313 			exit(1);
314 		}
315 		if (getether(ifr->ifr_name, eaddr)) {
316 			printf("Can not get ether addr for %s\n", ifr->ifr_name);
317 			exit(1);
318 		}
319 		/* Copy Ethernet address into request packet. */
320 		bp->bp_htype = 1;
321 		bp->bp_hlen = 6;
322 		bcopy(eaddr, bp->bp_chaddr, bp->bp_hlen);
323 	} else {
324 		/* Fill in the client IP address. */
325 		gethostname(hostname, sizeof(hostname));
326 		hostname[sizeof(hostname) - 1] = '\0';
327 		hep = gethostbyname(hostname);
328 		if (!hep) {
329 			printf("Can not get my IP address\n");
330 			exit(1);
331 		}
332 		bcopy(hep->h_addr, &bp->bp_ciaddr, hep->h_length);
333 	}
334 
335 	/*
336 	 * Copy in the default vendor data.
337 	 */
338 	bcopy((char*)&vend_magic, bp->bp_vend, 4);
339 	if (vend_magic)
340 		bp->bp_vend[4] = TAG_END;
341 
342 	/*
343 	 * Read in the "options" part of the request.
344 	 * This also determines the size of the packet.
345 	 */
346 	snaplen = sizeof(*bp);
347 	if (vendor_file) {
348 		int fd = open(vendor_file, 0);
349 		if (fd < 0) {
350 			perror(vendor_file);
351 			exit(1);
352 		}
353 		/* Compute actual space for options. */
354 		n = BUFLEN - sizeof(*bp) + BP_VEND_LEN;
355 		n = read(fd, bp->bp_vend, n);
356 		close(fd);
357 		if (n < 0) {
358 			perror(vendor_file);
359 			exit(1);
360 		}
361 		printf("read %d bytes of vendor template\n", n);
362 		if (n > BP_VEND_LEN) {
363 			printf("warning: extended options in use (len > %d)\n",
364 				   BP_VEND_LEN);
365 			snaplen += (n - BP_VEND_LEN);
366 		}
367 	}
368 	/*
369 	 * Set globals needed by print_bootp
370 	 * (called by send_request)
371 	 */
372 	packetp = (unsigned char *) eaddr;
373 	snapend = (unsigned char *) sndbuf + snaplen;
374 
375 	/* Send a request once per second while waiting for replies. */
376 	recvcnt = 0;
377 	bp->bp_secs = secs = 0;
378 	send_request(s);
379 	set[0].fd = s;
380 	set[0].events = POLLIN;
381 	while (1) {
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 	snprintf(b, sizeof(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