2 * bootptest.c - Test out a bootp server.
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
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)
18 * The remainder of this program is public domain. You may do
19 * whatever you like with it except claim that you wrote it.
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.
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>
34 * $FreeBSD: src/libexec/bootpd/tools/bootptest/bootptest.c,v 1.7 1999/08/28 00:09:25 peter Exp $
35 * $DragonFly: src/libexec/bootpd/tools/bootptest/bootptest.c,v 1.2 2003/06/17 04:27:07 dillon Exp $
38 char *usage = "bootptest [-h] server-name [vendor-data-template-file]";
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
46 #include <sys/utsname.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h> /* inet_ntoa */
66 #include "bootptest.h"
70 #include "patchlevel.h"
72 static void send_request();
83 unsigned char *packetp;
84 unsigned char *snapend;
89 * IP port numbers for client and server obtained from /etc/services
92 u_short bootps_port, bootpc_port;
96 * Internet socket and interface config structures
99 struct sockaddr_in sin_server; /* where to send requests */
100 struct sockaddr_in sin_client; /* for bind and listen */
101 struct sockaddr_in sin_from; /* Packet source */
102 u_char eaddr[16]; /* Ethernet address */
108 int debug = 1; /* Debugging flag (level) */
109 char *sndbuf; /* Send packet buffer */
110 char *rcvbuf; /* Receive packet buffer */
112 struct utsname my_uname;
116 * Vendor magic cookies for CMU and RFC1048
119 unsigned char vm_cmu[4] = VM_CMU;
120 unsigned char vm_rfc1048[4] = VM_RFC1048;
121 short secs; /* How long client has waited */
124 extern void bootp_print();
127 * Initialization such as command-line processing is done, then
128 * the receiver loop is started. Die when interrupted.
140 char *servername = NULL;
141 char *vendor_file = NULL;
142 char *bp_file = NULL;
143 int32 server_addr; /* inet addr, network order */
144 int s; /* Socket file descriptor */
145 int n, fromlen, recvcnt;
150 progname = strrchr(argv[0], '/');
159 printf("%s: version %s.%d\n", progname, VERSION, PATCHLEVEL);
162 * Verify that "struct bootp" has the correct official size.
163 * (Catch evil compilers that do struct padding.)
165 assert(sizeof(struct bootp) == BP_MINPKTSZ);
167 if (uname(&my_uname) < 0) {
168 fprintf(stderr, "%s: can't get hostname\n", argv[0]);
171 hostname = my_uname.nodename;
173 sndbuf = malloc(BUFLEN);
174 rcvbuf = malloc(BUFLEN);
175 if (!sndbuf || !rcvbuf) {
176 printf("malloc failed\n");
180 /* default magic number */
181 bcopy(vm_rfc1048, (char*)&vend_magic, 4);
183 /* Handle option switches. */
185 if (argv[0][0] != '-')
187 switch (argv[0][1]) {
189 case 'f': /* File name to reqest. */
196 case 'h': /* Use hardware address. */
200 case 'm': /* Magic number value. */
204 vend_magic = inet_addr(*argv);
217 /* Get server name (or address) for query. */
223 /* Get optional vendor-data-template-file. */
230 printf("missing server name.\n");
237 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
242 * Get server's listening port number
244 sep = getservbyname("bootps", "udp");
246 bootps_port = ntohs((u_short) sep->s_port);
248 fprintf(stderr, "udp/bootps: unknown service -- using port %d\n",
250 bootps_port = (u_short) IPPORT_BOOTPS;
254 * Set up server socket address (for send)
257 if (isdigit(servername[0]))
258 server_addr = inet_addr(servername);
260 hep = gethostbyname(servername);
262 fprintf(stderr, "%s: unknown host\n", servername);
265 bcopy(hep->h_addr, &server_addr, sizeof(server_addr));
268 /* Get broadcast address */
270 server_addr = INADDR_ANY;
272 sin_server.sin_family = AF_INET;
273 sin_server.sin_port = htons(bootps_port);
274 sin_server.sin_addr.s_addr = server_addr;
277 * Get client's listening port number
279 sep = getservbyname("bootpc", "udp");
281 bootpc_port = ntohs(sep->s_port);
283 fprintf(stderr, "udp/bootpc: unknown service -- using port %d\n",
285 bootpc_port = (u_short) IPPORT_BOOTPC;
289 * Set up client socket address (for listen)
291 sin_client.sin_family = AF_INET;
292 sin_client.sin_port = htons(bootpc_port);
293 sin_client.sin_addr.s_addr = INADDR_ANY;
296 * Bind client socket to BOOTPC port.
298 if (bind(s, (struct sockaddr *) &sin_client, sizeof(sin_client)) < 0) {
299 perror("bind BOOTPC port");
301 fprintf(stderr, "You need to run this as root\n");
307 bp = (struct bootp *) sndbuf;
308 bzero(bp, sizeof(*bp));
309 bp->bp_op = BOOTREQUEST;
310 xid = (int32) getpid();
311 bp->bp_xid = (u_int32) htonl(xid);
313 strncpy(bp->bp_file, bp_file, BP_FILE_LEN);
316 * Fill in the hardware address (or client IP address)
321 ifr = getif(s, &sin_server.sin_addr);
323 printf("No interface for %s\n", servername);
326 if (getether(ifr->ifr_name, (char*)eaddr)) {
327 printf("Can not get ether addr for %s\n", ifr->ifr_name);
330 /* Copy Ethernet address into request packet. */
333 bcopy(eaddr, bp->bp_chaddr, bp->bp_hlen);
335 /* Fill in the client IP address. */
336 hep = gethostbyname(hostname);
338 printf("Can not get my IP address\n");
341 bcopy(hep->h_addr, &bp->bp_ciaddr, hep->h_length);
345 * Copy in the default vendor data.
347 bcopy((char*)&vend_magic, bp->bp_vend, 4);
349 bp->bp_vend[4] = TAG_END;
352 * Read in the "options" part of the request.
353 * This also determines the size of the packet.
355 snaplen = sizeof(*bp);
357 int fd = open(vendor_file, 0);
362 /* Compute actual space for options. */
363 n = BUFLEN - sizeof(*bp) + BP_VEND_LEN;
364 n = read(fd, bp->bp_vend, n);
370 printf("read %d bytes of vendor template\n", n);
371 if (n > BP_VEND_LEN) {
372 printf("warning: extended options in use (len > %d)\n",
374 snaplen += (n - BP_VEND_LEN);
378 * Set globals needed by print_bootp
379 * (called by send_request)
381 packetp = (unsigned char *) eaddr;
382 snapend = (unsigned char *) sndbuf + snaplen;
384 /* Send a request once per second while waiting for replies. */
386 bp->bp_secs = secs = 0;
392 tv.tv_sec = WAITSECS;
395 n = select(s + 1, (fd_set *) & readfds, NULL, NULL, &tv);
402 * We have not received a response in the last second.
403 * If we have ever received any responses, exit now.
404 * Otherwise, bump the "wait time" field and re-send.
411 bp->bp_secs = htons(secs);
415 fromlen = sizeof(sin_from);
416 n = recvfrom(s, rcvbuf, BUFLEN, 0,
417 (struct sockaddr *) &sin_from, &fromlen);
421 if (n < sizeof(struct bootp)) {
422 printf("received short packet\n");
427 /* Print the received packet. */
428 printf("Recvd from %s", inet_ntoa(sin_from.sin_addr));
429 /* set globals needed by bootp_print() */
431 snapend = (unsigned char *) rcvbuf + snaplen;
432 bootp_print(rcvbuf, n, sin_from.sin_port, 0);
435 * This no longer exits immediately after receiving
436 * one response because it is useful to know if the
437 * client might get multiple responses. This code
438 * will now listen for one second after a response.
441 fprintf(stderr, "no response from %s\n", servername);
449 /* Print the request packet. */
450 printf("Sending to %s", inet_ntoa(sin_server.sin_addr));
451 bootp_print(sndbuf, snaplen, sin_from.sin_port, 0);
454 /* Send the request packet. */
455 if (sendto(s, sndbuf, snaplen, 0,
456 (struct sockaddr *) &sin_server,
457 sizeof(sin_server)) < 0)
459 perror("sendto server");
465 * Print out a filename (or other ascii string).
466 * Return true if truncated.
470 register u_char *s, *ep;
475 while ((c = *s++) != '\0') {
486 c ^= 0x40; /* DEL to ?, others to alpha */
496 * Convert an IP addr to a string.
497 * (like inet_ntoa, but ina is a pointer)
507 snprintf(b, sizeof(b), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
515 * c-argdecl-indent: 4
516 * c-continued-statement-offset: 4
517 * c-continued-brace-offset: -4