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