Home | History | Annotate | Line # | Download | only in bootptest
bootptest.c revision 1.5
      1 /*	$NetBSD: bootptest.c,v 1.5 1998/03/14 04:39:53 lukem 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.5 1998/03/14 04:39:53 lukem Exp $");
     40 #endif
     41 
     42 char *usage = "bootptest [-h] server-name [vendor-data-template-file]";
     43 
     44 #include <sys/types.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 
     51 #include <net/if.h>
     52 #include <netinet/in.h>
     53 #include <arpa/inet.h>			/* inet_ntoa */
     54 
     55 #include <stdlib.h>
     56 #include <signal.h>
     57 #include <stdio.h>
     58 #include <string.h>
     59 #include <errno.h>
     60 #include <ctype.h>
     61 #include <netdb.h>
     62 #include <assert.h>
     63 #include <unistd.h>
     64 
     65 #include "bootp.h"
     66 #include "bootptest.h"
     67 #include "getif.h"
     68 #include "report.h"
     69 #include "patchlevel.h"
     70 
     71 #define LOG_ERR 1
     72 #define BUFLEN 1024
     73 #define WAITSECS 1
     74 #define MAXWAIT  10
     75 
     76 int vflag = 1;
     77 int tflag = 0;
     78 int thiszone;
     79 char *progname;
     80 unsigned char *packetp;
     81 unsigned char *snapend;
     82 int snaplen;
     83 
     84 
     85 /*
     86  * IP port numbers for client and server obtained from /etc/services
     87  */
     88 
     89 u_short bootps_port, bootpc_port;
     90 
     91 
     92 /*
     93  * Internet socket and interface config structures
     94  */
     95 
     96 struct sockaddr_in sin_server;	/* where to send requests */
     97 struct sockaddr_in sin_client;	/* for bind and listen */
     98 struct sockaddr_in sin_from;	/* Packet source */
     99 u_char eaddr[16];				/* Ethernet address */
    100 
    101 /*
    102  * General
    103  */
    104 
    105 int debug = 1;					/* Debugging flag (level) */
    106 char hostname[64];
    107 char *sndbuf;					/* Send packet buffer */
    108 char *rcvbuf;					/* Receive packet buffer */
    109 
    110 /*
    111  * Vendor magic cookies for CMU and RFC1048
    112  */
    113 
    114 unsigned char vm_cmu[4] = VM_CMU;
    115 unsigned char vm_rfc1048[4] = VM_RFC1048;
    116 short secs;						/* How long client has waited */
    117 
    118 
    119 #ifdef	__STDC__
    120 #define P(args) args
    121 #else
    122 #define P(args) ()
    123 #endif
    124 
    125 extern int getether P((char *, char *));
    126 int main P((int, char **));
    127 void send_request P((int));
    128 
    129 #undef P
    130 
    131 /*
    132  * Initialization such as command-line processing is done, then
    133  * the receiver loop is started.  Die when interrupted.
    134  */
    135 
    136 int
    137 main(argc, argv)
    138 	int argc;
    139 	char **argv;
    140 {
    141 	struct bootp *bp;
    142 	struct servent *sep;
    143 	struct hostent *hep;
    144 
    145 	char *servername = NULL;
    146 	char *vendor_file = NULL;
    147 	char *bp_file = NULL;
    148 	int s;				/* Socket file descriptor */
    149 	int n, fromlen, recvcnt;
    150 	int use_hwa = 0;
    151 	int32 vend_magic;
    152 	int32 xid;
    153 
    154 	progname = strrchr(argv[0], '/');
    155 	if (progname)
    156 		progname++;
    157 	else
    158 		progname = argv[0];
    159 	argc--;
    160 	argv++;
    161 
    162 	if (debug)
    163 		printf("%s: version %s.%d\n", progname, VERSION, PATCHLEVEL);
    164 
    165 	/*
    166 	 * Verify that "struct bootp" has the correct official size.
    167 	 * (Catch evil compilers that do struct padding.)
    168 	 */
    169 	assert(sizeof(struct bootp) == BP_MINPKTSZ);
    170 
    171 	sndbuf = malloc(BUFLEN);
    172 	rcvbuf = malloc(BUFLEN);
    173 	if (!sndbuf || !rcvbuf) {
    174 		printf("malloc failed\n");
    175 		exit(1);
    176 	}
    177 
    178 	/* default magic number */
    179 	bcopy(vm_rfc1048, (char*)&vend_magic, 4);
    180 
    181 	/* Handle option switches. */
    182 	while (argc > 0) {
    183 		if (argv[0][0] != '-')
    184 			break;
    185 		switch (argv[0][1]) {
    186 
    187 		case 'f':				/* File name to reqest. */
    188 			if (argc < 2)
    189 				goto error;
    190 			argc--; argv++;
    191 			bp_file = *argv;
    192 			break;
    193 
    194 		case 'h':				/* Use hardware address. */
    195 			use_hwa = 1;
    196 			break;
    197 
    198 		case 'm':				/* Magic number value. */
    199 			if (argc < 2)
    200 				goto error;
    201 			argc--; argv++;
    202 			vend_magic = inet_addr(*argv);
    203 			break;
    204 
    205 		error:
    206 		default:
    207 			puts(usage);
    208 			exit(1);
    209 
    210 		}
    211 		argc--;
    212 		argv++;
    213 	}
    214 
    215 	/* Get server name (or address) for query. */
    216 	if (argc > 0) {
    217 		servername = *argv;
    218 		argc--;
    219 		argv++;
    220 	}
    221 	/* Get optional vendor-data-template-file. */
    222 	if (argc > 0) {
    223 		vendor_file = *argv;
    224 		argc--;
    225 		argv++;
    226 	}
    227 	if (!servername) {
    228 		printf("missing server name.\n");
    229 		puts(usage);
    230 		exit(1);
    231 	}
    232 	/*
    233 	 * Create a socket.
    234 	 */
    235 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    236 		perror("socket");
    237 		exit(1);
    238 	}
    239 	/*
    240 	 * Get server's listening port number
    241 	 */
    242 	sep = getservbyname("bootps", "udp");
    243 	if (sep) {
    244 		bootps_port = ntohs((u_short) sep->s_port);
    245 	} else {
    246 		fprintf(stderr, "udp/bootps: unknown service -- using port %d\n",
    247 				IPPORT_BOOTPS);
    248 		bootps_port = (u_short) IPPORT_BOOTPS;
    249 	}
    250 
    251 	/*
    252 	 * Set up server socket address (for send)
    253 	 */
    254 	if (servername) {
    255 		if (inet_aton(servername, &sin_server.sin_addr) == 0) {
    256 			hep = gethostbyname(servername);
    257 			if (!hep) {
    258 				fprintf(stderr, "%s: unknown host\n", servername);
    259 				exit(1);
    260 			}
    261 			memcpy(&sin_server.sin_addr, hep->h_addr,
    262 			    sizeof(sin_server.sin_addr));
    263 		}
    264 	} else {
    265 		/* Get broadcast address */
    266 		/* XXX - not yet */
    267 		sin_server.sin_addr.s_addr = INADDR_ANY;
    268 	}
    269 	sin_server.sin_family = AF_INET;
    270 	sin_server.sin_port = htons(bootps_port);
    271 
    272 	/*
    273 	 * Get client's listening port number
    274 	 */
    275 	sep = getservbyname("bootpc", "udp");
    276 	if (sep) {
    277 		bootpc_port = ntohs(sep->s_port);
    278 	} else {
    279 		fprintf(stderr, "udp/bootpc: unknown service -- using port %d\n",
    280 				IPPORT_BOOTPC);
    281 		bootpc_port = (u_short) IPPORT_BOOTPC;
    282 	}
    283 
    284 	/*
    285 	 * Set up client socket address (for listen)
    286 	 */
    287 	sin_client.sin_family = AF_INET;
    288 	sin_client.sin_port = htons(bootpc_port);
    289 	sin_client.sin_addr.s_addr = INADDR_ANY;
    290 
    291 	/*
    292 	 * Bind client socket to BOOTPC port.
    293 	 */
    294 	if (bind(s, (struct sockaddr *) &sin_client, sizeof(sin_client)) < 0) {
    295 		perror("bind BOOTPC port");
    296 		if (errno == EACCES)
    297 			fprintf(stderr, "You need to run this as root\n");
    298 		exit(1);
    299 	}
    300 	/*
    301 	 * Build a request.
    302 	 */
    303 	bp = (struct bootp *) sndbuf;
    304 	bzero(bp, sizeof(*bp));
    305 	bp->bp_op = BOOTREQUEST;
    306 	xid = (int32) getpid();
    307 	bp->bp_xid = (u_int32) htonl(xid);
    308 	if (bp_file)
    309 		strncpy(bp->bp_file, bp_file, BP_FILE_LEN);
    310 
    311 	/*
    312 	 * Fill in the hardware address (or client IP address)
    313 	 */
    314 	if (use_hwa) {
    315 		struct ifreq *ifr;
    316 
    317 		ifr = getif(s, &sin_server.sin_addr);
    318 		if (!ifr) {
    319 			printf("No interface for %s\n", servername);
    320 			exit(1);
    321 		}
    322 		if (getether(ifr->ifr_name, eaddr)) {
    323 			printf("Can not get ether addr for %s\n", ifr->ifr_name);
    324 			exit(1);
    325 		}
    326 		/* Copy Ethernet address into request packet. */
    327 		bp->bp_htype = 1;
    328 		bp->bp_hlen = 6;
    329 		bcopy(eaddr, bp->bp_chaddr, bp->bp_hlen);
    330 	} else {
    331 		/* Fill in the client IP address. */
    332 		gethostname(hostname, sizeof(hostname));
    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 	fprintf(stderr, "no response from %s\n", servername);
    439 	exit(1);
    440 }
    441 
    442 void
    443 send_request(s)
    444 	int s;
    445 {
    446 	/* Print the request packet. */
    447 	printf("Sending to %s", inet_ntoa(sin_server.sin_addr));
    448 	bootp_print((struct bootp *)sndbuf, snaplen, sin_from.sin_port, 0);
    449 	putchar('\n');
    450 
    451 	/* Send the request packet. */
    452 	if (sendto(s, sndbuf, snaplen, 0,
    453 			   (struct sockaddr *) &sin_server,
    454 			   sizeof(sin_server)) < 0)
    455 	{
    456 		perror("sendto server");
    457 		exit(1);
    458 	}
    459 }
    460 
    461 /*
    462  * Print out a filename (or other ascii string).
    463  * Return true if truncated.
    464  */
    465 int
    466 printfn(s, ep)
    467 	register u_char *s, *ep;
    468 {
    469 	register u_char c;
    470 
    471 	putchar('"');
    472 	while ((c = *s++) != 0) {
    473 		if (s > ep) {
    474 			putchar('"');
    475 			return (1);
    476 		}
    477 		if (!isascii(c)) {
    478 			c = toascii(c);
    479 			putchar('M');
    480 			putchar('-');
    481 		}
    482 		if (!isprint(c)) {
    483 			c ^= 0x40;			/* DEL to ?, others to alpha */
    484 			putchar('^');
    485 		}
    486 		putchar(c);
    487 	}
    488 	putchar('"');
    489 	return (0);
    490 }
    491 
    492 /*
    493  * Convert an IP addr to a string.
    494  * (like inet_ntoa, but ina is a pointer)
    495  */
    496 char *
    497 ipaddr_string(ina)
    498 	struct in_addr *ina;
    499 {
    500 	static char b[24];
    501 	u_char *p;
    502 
    503 	p = (u_char *) ina;
    504 	sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
    505 	return (b);
    506 }
    507 
    508 /*
    509  * Local Variables:
    510  * tab-width: 4
    511  * c-indent-level: 4
    512  * c-argdecl-indent: 4
    513  * c-continued-statement-offset: 4
    514  * c-continued-brace-offset: -4
    515  * c-label-offset: -4
    516  * c-brace-offset: 0
    517  * End:
    518  */
    519