bootptest.c revision 1.9 1 /* $NetBSD: bootptest.c,v 1.9 2002/07/14 00:30:02 wiz 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.9 2002/07/14 00:30:02 wiz Exp $");
40 #endif
41
42 char *usage = "bootptest [-h] server-name [vendor-data-template-file]";
43
44 #include <sys/param.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[MAXHOSTNAMELEN + 1];
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 extern int getether(char *, char *);
120 int main(int, char **);
121 void send_request(int);
122
123 /*
124 * Initialization such as command-line processing is done, then
125 * the receiver loop is started. Die when interrupted.
126 */
127
128 int
129 main(int argc, char **argv)
130 {
131 struct bootp *bp;
132 struct servent *sep;
133 struct hostent *hep;
134
135 char *servername = NULL;
136 char *vendor_file = NULL;
137 char *bp_file = NULL;
138 int s; /* Socket file descriptor */
139 int n, fromlen, recvcnt;
140 int use_hwa = 0;
141 int32 vend_magic;
142 int32 xid;
143
144 progname = strrchr(argv[0], '/');
145 if (progname)
146 progname++;
147 else
148 progname = argv[0];
149 argc--;
150 argv++;
151
152 if (debug)
153 printf("%s: version %s.%d\n", progname, VERSION, PATCHLEVEL);
154
155 /*
156 * Verify that "struct bootp" has the correct official size.
157 * (Catch evil compilers that do struct padding.)
158 */
159 assert(sizeof(struct bootp) == BP_MINPKTSZ);
160
161 sndbuf = malloc(BUFLEN);
162 rcvbuf = malloc(BUFLEN);
163 if (!sndbuf || !rcvbuf) {
164 printf("malloc failed\n");
165 exit(1);
166 }
167
168 /* default magic number */
169 bcopy(vm_rfc1048, (char*)&vend_magic, 4);
170
171 /* Handle option switches. */
172 while (argc > 0) {
173 if (argv[0][0] != '-')
174 break;
175 switch (argv[0][1]) {
176
177 case 'f': /* File name to reqest. */
178 if (argc < 2)
179 goto error;
180 argc--; argv++;
181 bp_file = *argv;
182 break;
183
184 case 'h': /* Use hardware address. */
185 use_hwa = 1;
186 break;
187
188 case 'm': /* Magic number value. */
189 if (argc < 2)
190 goto error;
191 argc--; argv++;
192 vend_magic = inet_addr(*argv);
193 break;
194
195 error:
196 default:
197 puts(usage);
198 exit(1);
199
200 }
201 argc--;
202 argv++;
203 }
204
205 /* Get server name (or address) for query. */
206 if (argc > 0) {
207 servername = *argv;
208 argc--;
209 argv++;
210 }
211 /* Get optional vendor-data-template-file. */
212 if (argc > 0) {
213 vendor_file = *argv;
214 argc--;
215 argv++;
216 }
217 if (!servername) {
218 printf("missing server name.\n");
219 puts(usage);
220 exit(1);
221 }
222 /*
223 * Create a socket.
224 */
225 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
226 perror("socket");
227 exit(1);
228 }
229 /*
230 * Get server's listening port number
231 */
232 sep = getservbyname("bootps", "udp");
233 if (sep) {
234 bootps_port = ntohs((u_short) sep->s_port);
235 } else {
236 fprintf(stderr, "udp/bootps: unknown service -- using port %d\n",
237 IPPORT_BOOTPS);
238 bootps_port = (u_short) IPPORT_BOOTPS;
239 }
240
241 /*
242 * Set up server socket address (for send)
243 */
244 if (servername) {
245 if (inet_aton(servername, &sin_server.sin_addr) == 0) {
246 hep = gethostbyname(servername);
247 if (!hep) {
248 fprintf(stderr, "%s: unknown host\n", servername);
249 exit(1);
250 }
251 memcpy(&sin_server.sin_addr, hep->h_addr,
252 sizeof(sin_server.sin_addr));
253 }
254 } else {
255 /* Get broadcast address */
256 /* XXX - not yet */
257 sin_server.sin_addr.s_addr = INADDR_ANY;
258 }
259 sin_server.sin_family = AF_INET;
260 sin_server.sin_port = htons(bootps_port);
261
262 /*
263 * Get client's listening port number
264 */
265 sep = getservbyname("bootpc", "udp");
266 if (sep) {
267 bootpc_port = ntohs(sep->s_port);
268 } else {
269 fprintf(stderr, "udp/bootpc: unknown service -- using port %d\n",
270 IPPORT_BOOTPC);
271 bootpc_port = (u_short) IPPORT_BOOTPC;
272 }
273
274 /*
275 * Set up client socket address (for listen)
276 */
277 sin_client.sin_family = AF_INET;
278 sin_client.sin_port = htons(bootpc_port);
279 sin_client.sin_addr.s_addr = INADDR_ANY;
280
281 /*
282 * Bind client socket to BOOTPC port.
283 */
284 if (bind(s, (struct sockaddr *) &sin_client, sizeof(sin_client)) < 0) {
285 perror("bind BOOTPC port");
286 if (errno == EACCES)
287 fprintf(stderr, "You need to run this as root\n");
288 exit(1);
289 }
290 /*
291 * Build a request.
292 */
293 bp = (struct bootp *) sndbuf;
294 bzero(bp, sizeof(*bp));
295 bp->bp_op = BOOTREQUEST;
296 xid = (int32) getpid();
297 bp->bp_xid = (u_int32) htonl(xid);
298 if (bp_file)
299 strncpy(bp->bp_file, bp_file, BP_FILE_LEN);
300
301 /*
302 * Fill in the hardware address (or client IP address)
303 */
304 if (use_hwa) {
305 struct ifreq *ifr;
306
307 ifr = getif(s, &sin_server.sin_addr);
308 if (!ifr) {
309 printf("No interface for %s\n", servername);
310 exit(1);
311 }
312 if (getether(ifr->ifr_name, eaddr)) {
313 printf("Can not get ether addr for %s\n", ifr->ifr_name);
314 exit(1);
315 }
316 /* Copy Ethernet address into request packet. */
317 bp->bp_htype = 1;
318 bp->bp_hlen = 6;
319 bcopy(eaddr, bp->bp_chaddr, bp->bp_hlen);
320 } else {
321 /* Fill in the client IP address. */
322 gethostname(hostname, sizeof(hostname));
323 hostname[sizeof(hostname) - 1] = '\0';
324 hep = gethostbyname(hostname);
325 if (!hep) {
326 printf("Can not get my IP address\n");
327 exit(1);
328 }
329 bcopy(hep->h_addr, &bp->bp_ciaddr, hep->h_length);
330 }
331
332 /*
333 * Copy in the default vendor data.
334 */
335 bcopy((char*)&vend_magic, bp->bp_vend, 4);
336 if (vend_magic)
337 bp->bp_vend[4] = TAG_END;
338
339 /*
340 * Read in the "options" part of the request.
341 * This also determines the size of the packet.
342 */
343 snaplen = sizeof(*bp);
344 if (vendor_file) {
345 int fd = open(vendor_file, 0);
346 if (fd < 0) {
347 perror(vendor_file);
348 exit(1);
349 }
350 /* Compute actual space for options. */
351 n = BUFLEN - sizeof(*bp) + BP_VEND_LEN;
352 n = read(fd, bp->bp_vend, n);
353 close(fd);
354 if (n < 0) {
355 perror(vendor_file);
356 exit(1);
357 }
358 printf("read %d bytes of vendor template\n", n);
359 if (n > BP_VEND_LEN) {
360 printf("warning: extended options in use (len > %d)\n",
361 BP_VEND_LEN);
362 snaplen += (n - BP_VEND_LEN);
363 }
364 }
365 /*
366 * Set globals needed by print_bootp
367 * (called by send_request)
368 */
369 packetp = (unsigned char *) eaddr;
370 snapend = (unsigned char *) sndbuf + snaplen;
371
372 /* Send a request once per second while waiting for replies. */
373 recvcnt = 0;
374 bp->bp_secs = secs = 0;
375 send_request(s);
376 while (1) {
377 struct timeval tv;
378 int readfds;
379
380 tv.tv_sec = WAITSECS;
381 tv.tv_usec = 0L;
382 readfds = (1 << s);
383 n = select(s + 1, (fd_set *) & readfds, NULL, NULL, &tv);
384 if (n < 0) {
385 perror("select");
386 break;
387 }
388 if (n == 0) {
389 /*
390 * We have not received a response in the last second.
391 * If we have ever received any responses, exit now.
392 * Otherwise, bump the "wait time" field and re-send.
393 */
394 if (recvcnt > 0)
395 exit(0);
396 secs += WAITSECS;
397 if (secs > MAXWAIT)
398 break;
399 bp->bp_secs = htons(secs);
400 send_request(s);
401 continue;
402 }
403 fromlen = sizeof(sin_from);
404 n = recvfrom(s, rcvbuf, BUFLEN, 0,
405 (struct sockaddr *) &sin_from, &fromlen);
406 if (n <= 0) {
407 continue;
408 }
409 if (n < sizeof(struct bootp)) {
410 printf("received short packet\n");
411 continue;
412 }
413 recvcnt++;
414
415 /* Print the received packet. */
416 printf("Recvd from %s", inet_ntoa(sin_from.sin_addr));
417 /* set globals needed by bootp_print() */
418 snaplen = n;
419 snapend = (unsigned char *) rcvbuf + snaplen;
420 bootp_print((struct bootp *)rcvbuf, n, sin_from.sin_port, 0);
421 putchar('\n');
422 /*
423 * This no longer exits immediately after receiving
424 * one response because it is useful to know if the
425 * client might get multiple responses. This code
426 * will now listen for one second after a response.
427 */
428 }
429 fprintf(stderr, "no response from %s\n", servername);
430 exit(1);
431 }
432
433 void
434 send_request(int s)
435 {
436 /* Print the request packet. */
437 printf("Sending to %s", inet_ntoa(sin_server.sin_addr));
438 bootp_print((struct bootp *)sndbuf, snaplen, sin_from.sin_port, 0);
439 putchar('\n');
440
441 /* Send the request packet. */
442 if (sendto(s, sndbuf, snaplen, 0,
443 (struct sockaddr *) &sin_server,
444 sizeof(sin_server)) < 0)
445 {
446 perror("sendto server");
447 exit(1);
448 }
449 }
450
451 /*
452 * Print out a filename (or other ascii string).
453 * Return true if truncated.
454 */
455 int
456 printfn(u_char *s, u_char *ep)
457 {
458 u_char c;
459
460 putchar('"');
461 while ((c = *s++) != 0) {
462 if (s > ep) {
463 putchar('"');
464 return (1);
465 }
466 if (!isascii(c)) {
467 c = toascii(c);
468 putchar('M');
469 putchar('-');
470 }
471 if (!isprint(c)) {
472 c ^= 0x40; /* DEL to ?, others to alpha */
473 putchar('^');
474 }
475 putchar(c);
476 }
477 putchar('"');
478 return (0);
479 }
480
481 /*
482 * Convert an IP addr to a string.
483 * (like inet_ntoa, but ina is a pointer)
484 */
485 char *
486 ipaddr_string(struct in_addr *ina)
487 {
488 static char b[24];
489 u_char *p;
490
491 p = (u_char *) ina;
492 sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
493 return (b);
494 }
495
496 /*
497 * Local Variables:
498 * tab-width: 4
499 * c-indent-level: 4
500 * c-argdecl-indent: 4
501 * c-continued-statement-offset: 4
502 * c-continued-brace-offset: -4
503 * c-label-offset: -4
504 * c-brace-offset: 0
505 * End:
506 */
507