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