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