1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#ifndef lint 31static const char copyright[] = 32"@(#) Copyright (c) 1983, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34#endif 35 36#if 0 37#ifndef lint 38static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 39#endif 40#endif 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: stable/10/usr.bin/tftp/main.c 339061 2018-10-01 16:10:27Z asomers $"); 44 45/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ 46 47/* 48 * TFTP User Program -- Command Interface. 49 */ 50#include <sys/param.h> 51#include <sys/types.h> 52#include <sys/socket.h> 53#include <sys/sysctl.h> 54#include <sys/file.h> 55#include <sys/stat.h> 56 57#include <netinet/in.h> 58#include <arpa/inet.h> 59#include <arpa/tftp.h> 60 61#include <ctype.h> 62#include <err.h> 63#include <histedit.h> 64#include <netdb.h> 65#include <setjmp.h> 66#include <signal.h> 67#include <stdio.h> 68#include <stdlib.h> 69#include <string.h> 70#include <unistd.h> 71 72#include "tftp-utils.h" 73#include "tftp-io.h" 74#include "tftp-options.h" 75#include "tftp.h" 76 77#define MAXLINE 200 78#define TIMEOUT 5 /* secs between rexmt's */ 79 80typedef struct sockaddr_storage peeraddr; 81static int connected; 82static char mode[32]; 83static jmp_buf toplevel; 84volatile int txrx_error; 85static int peer; 86 87#define MAX_MARGV 20 88static int margc; 89static char *margv[MAX_MARGV]; 90 91int verbose; 92static char *port = NULL; 93 94static void get(int, char **); 95static void help(int, char **); 96static void intr(int); 97static void modecmd(int, char **); 98static void put(int, char **); 99static void quit(int, char **); 100static void setascii(int, char **); 101static void setbinary(int, char **); 102static void setpeer0(char *, const char *); 103static void setpeer(int, char **); 104static void settimeoutpacket(int, char **); 105static void settimeoutnetwork(int, char **); 106static void setdebug(int, char **); 107static void setverbose(int, char **); 108static void showstatus(int, char **); 109static void setblocksize(int, char **); 110static void setblocksize2(int, char **); 111static void setoptions(int, char **); 112static void setrollover(int, char **); 113static void setpacketdrop(int, char **); 114 115static void command(void) __dead2; 116static const char *command_prompt(void); 117 118static void urihandling(char *URI); 119static void getusage(char *); 120static void makeargv(char *line); 121static void putusage(char *); 122static void settftpmode(const char *); 123 124static char *tail(char *); 125static struct cmd *getcmd(char *); 126 127#define HELPINDENT (sizeof("connect")) 128 129struct cmd { 130 const char *name; 131 void (*handler)(int, char **); 132 const char *help; 133}; 134 135static struct cmd cmdtab[] = { 136 { "connect", setpeer, "connect to remote tftp" }, 137 { "mode", modecmd, "set file transfer mode" }, 138 { "put", put, "send file" }, 139 { "get", get, "receive file" }, 140 { "quit", quit, "exit tftp" }, 141 { "verbose", setverbose, "toggle verbose mode" }, 142 { "status", showstatus, "show current status" }, 143 { "binary", setbinary, "set mode to octet" }, 144 { "ascii", setascii, "set mode to netascii" }, 145 { "rexmt", settimeoutpacket, 146 "set per-packet retransmission timeout[-]" }, 147 { "timeout", settimeoutnetwork, 148 "set total retransmission timeout" }, 149 { "trace", setdebug, "enable 'debug packet'[-]" }, 150 { "debug", setdebug, "enable verbose output" }, 151 { "blocksize", setblocksize, "set blocksize[*]" }, 152 { "blocksize2", setblocksize2, "set blocksize as a power of 2[**]" }, 153 { "rollover", setrollover, "rollover after 64K packets[**]" }, 154 { "options", setoptions, 155 "enable or disable RFC2347 style options" }, 156 { "help", help, "print help information" }, 157 { "packetdrop", setpacketdrop, "artificial packetloss feature" }, 158 { "?", help, "print help information" }, 159 { NULL, NULL, NULL } 160}; 161 162static struct modes { 163 const char *m_name; 164 const char *m_mode; 165} modes[] = { 166 { "ascii", "netascii" }, 167 { "netascii", "netascii" }, 168 { "binary", "octet" }, 169 { "image", "octet" }, 170 { "octet", "octet" }, 171 { NULL, NULL } 172}; 173 174int 175main(int argc, char *argv[]) 176{ 177 178 acting_as_client = 1; 179 peer = -1; 180 strcpy(mode, "netascii"); 181 signal(SIGINT, intr); 182 if (argc > 1) { 183 if (setjmp(toplevel) != 0) 184 exit(txrx_error); 185 186 if (strncmp(argv[1], "tftp://", 7) == 0) { 187 urihandling(argv[1]); 188 exit(txrx_error); 189 } 190 191 setpeer(argc, argv); 192 } 193 if (setjmp(toplevel) != 0) 194 (void)putchar('\n'); 195 196 init_options(); 197 command(); 198} 199 200/* 201 * RFC3617 handling of TFTP URIs: 202 * 203 * tftpURI = "tftp://" host "/" file [ mode ] 204 * mode = ";" "mode=" ( "netascii" / "octet" ) 205 * file = *( unreserved / escaped ) 206 * host = <as specified by RFC 2732> 207 * unreserved = <as specified in RFC 2396> 208 * escaped = <as specified in RFC 2396> 209 * 210 * We are cheating a little bit by allowing any mode as specified in the 211 * modes table defined earlier on in this file and mapping it on the real 212 * mode. 213 */ 214static void 215urihandling(char *URI) 216{ 217 char uri[ARG_MAX]; 218 char *host = NULL; 219 char *path = NULL; 220 char *opts = NULL; 221 const char *tmode = "octet"; 222 char *s; 223 char line[MAXLINE]; 224 int i; 225 226 strlcpy(uri, URI, ARG_MAX); 227 host = uri + 7; 228 229 if ((s = strchr(host, '/')) == NULL) { 230 fprintf(stderr, 231 "Invalid URI: Couldn't find / after hostname\n"); 232 exit(1); 233 } 234 *s = '\0'; 235 path = s + 1; 236 237 if ((s = strchr(path, ';')) != NULL) { 238 *s = '\0'; 239 opts = s + 1; 240 241 if (strncmp(opts, "mode=", 5) == 0) { 242 tmode = opts; 243 tmode += 5; 244 245 for (i = 0; modes[i].m_name != NULL; i++) { 246 if (strcmp(modes[i].m_name, tmode) == 0) 247 break; 248 } 249 if (modes[i].m_name == NULL) { 250 fprintf(stderr, "Invalid mode: '%s'\n", mode); 251 exit(1); 252 } 253 settftpmode(modes[i].m_mode); 254 } 255 } else { 256 settftpmode("octet"); 257 } 258 259 setpeer0(host, NULL); 260 261 sprintf(line, "get %s", path); 262 makeargv(line); 263 get(margc, margv); 264} 265 266static char hostname[MAXHOSTNAMELEN]; 267 268static void 269setpeer0(char *host, const char *lport) 270{ 271 struct addrinfo hints, *res0, *res; 272 int error; 273 const char *cause = "unknown"; 274 275 if (connected) { 276 close(peer); 277 peer = -1; 278 } 279 connected = 0; 280 281 memset(&hints, 0, sizeof(hints)); 282 hints.ai_family = PF_UNSPEC; 283 hints.ai_socktype = SOCK_DGRAM; 284 hints.ai_protocol = IPPROTO_UDP; 285 hints.ai_flags = AI_CANONNAME; 286 if (!lport) 287 lport = "tftp"; 288 error = getaddrinfo(host, lport, &hints, &res0); 289 if (error) { 290 warnx("%s", gai_strerror(error)); 291 return; 292 } 293 294 for (res = res0; res; res = res->ai_next) { 295 if (res->ai_addrlen > sizeof(peeraddr)) 296 continue; 297 peer = socket(res->ai_family, res->ai_socktype, 298 res->ai_protocol); 299 if (peer < 0) { 300 cause = "socket"; 301 continue; 302 } 303 304 memset(&peer_sock, 0, sizeof(peer_sock)); 305 peer_sock.ss_family = res->ai_family; 306 peer_sock.ss_len = res->ai_addrlen; 307 if (bind(peer, (struct sockaddr *)&peer_sock, peer_sock.ss_len) < 0) { 308 cause = "bind"; 309 close(peer); 310 peer = -1; 311 continue; 312 } 313 314 break; 315 } 316 317 if (peer < 0) 318 warn("%s", cause); 319 else { 320 /* res->ai_addr <= sizeof(peeraddr) is guaranteed */ 321 memcpy(&peer_sock, res->ai_addr, res->ai_addrlen); 322 if (res->ai_canonname) { 323 (void) strlcpy(hostname, res->ai_canonname, 324 sizeof(hostname)); 325 } else 326 (void) strlcpy(hostname, host, sizeof(hostname)); 327 connected = 1; 328 } 329 330 freeaddrinfo(res0); 331} 332 333static void 334setpeer(int argc, char *argv[]) 335{ 336 char line[MAXLINE]; 337 338 if (argc < 2) { 339 strcpy(line, "Connect "); 340 printf("(to) "); 341 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 342 makeargv(line); 343 argc = margc; 344 argv = margv; 345 } 346 if ((argc < 2) || (argc > 3)) { 347 printf("usage: %s [host [port]]\n", argv[0]); 348 return; 349 } 350 if (argc == 3) { 351 port = argv[2]; 352 setpeer0(argv[1], argv[2]); 353 } else 354 setpeer0(argv[1], NULL); 355} 356 357static void 358modecmd(int argc, char *argv[]) 359{ 360 struct modes *p; 361 const char *sep; 362 363 if (argc < 2) { 364 printf("Using %s mode to transfer files.\n", mode); 365 return; 366 } 367 if (argc == 2) { 368 for (p = modes; p->m_name; p++) 369 if (strcmp(argv[1], p->m_name) == 0) 370 break; 371 if (p->m_name) { 372 settftpmode(p->m_mode); 373 return; 374 } 375 printf("%s: unknown mode\n", argv[1]); 376 /* drop through and print usage message */ 377 } 378 379 printf("usage: %s [", argv[0]); 380 sep = " "; 381 for (p = modes; p->m_name != NULL; p++) { 382 printf("%s%s", sep, p->m_name); 383 if (*sep == ' ') 384 sep = " | "; 385 } 386 printf(" ]\n"); 387 return; 388} 389 390static void 391setbinary(int argc __unused, char *argv[] __unused) 392{ 393 394 settftpmode("octet"); 395} 396 397static void 398setascii(int argc __unused, char *argv[] __unused) 399{ 400 401 settftpmode("netascii"); 402} 403 404static void 405settftpmode(const char *newmode) 406{ 407 408 strlcpy(mode, newmode, sizeof(mode)); 409 if (verbose) 410 printf("mode set to %s\n", mode); 411} 412 413 414/* 415 * Send file(s). 416 */ 417static void 418put(int argc, char *argv[]) 419{ 420 int fd; 421 int n; 422 char *cp, *targ; 423 char line[MAXLINE]; 424 struct stat sb; 425 426 if (argc < 2) { 427 strcpy(line, "send "); 428 printf("(file) "); 429 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 430 makeargv(line); 431 argc = margc; 432 argv = margv; 433 } 434 if (argc < 2) { 435 putusage(argv[0]); 436 return; 437 } 438 targ = argv[argc - 1]; 439 if (strrchr(argv[argc - 1], ':')) { 440 char *lcp; 441 442 for (n = 1; n < argc - 1; n++) 443 if (strchr(argv[n], ':')) { 444 putusage(argv[0]); 445 return; 446 } 447 lcp = argv[argc - 1]; 448 targ = strrchr(lcp, ':'); 449 *targ++ = 0; 450 if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') { 451 lcp[strlen(lcp) - 1] = '\0'; 452 lcp++; 453 } 454 setpeer0(lcp, NULL); 455 } 456 if (!connected) { 457 printf("No target machine specified.\n"); 458 return; 459 } 460 if (argc < 4) { 461 cp = argc == 2 ? tail(targ) : argv[1]; 462 fd = open(cp, O_RDONLY); 463 if (fd < 0) { 464 warn("%s", cp); 465 return; 466 } 467 468 if (fstat(fd, &sb) < 0) { 469 warn("%s", cp); 470 return; 471 } 472 asprintf(&options[OPT_TSIZE].o_request, "%ju", sb.st_size); 473 474 if (verbose) 475 printf("putting %s to %s:%s [%s]\n", 476 cp, hostname, targ, mode); 477 xmitfile(peer, port, fd, targ, mode); 478 close(fd); 479 return; 480 } 481 /* this assumes the target is a directory */ 482 /* on a remote unix system. hmmmm. */ 483 cp = strchr(targ, '\0'); 484 *cp++ = '/'; 485 for (n = 1; n < argc - 1; n++) { 486 strcpy(cp, tail(argv[n])); 487 fd = open(argv[n], O_RDONLY); 488 if (fd < 0) { 489 warn("%s", argv[n]); 490 continue; 491 } 492 493 if (fstat(fd, &sb) < 0) { 494 warn("%s", argv[n]); 495 continue; 496 } 497 asprintf(&options[OPT_TSIZE].o_request, "%ju", sb.st_size); 498 499 if (verbose) 500 printf("putting %s to %s:%s [%s]\n", 501 argv[n], hostname, targ, mode); 502 xmitfile(peer, port, fd, targ, mode); 503 } 504} 505 506static void 507putusage(char *s) 508{ 509 510 printf("usage: %s file [remotename]\n", s); 511 printf(" %s file host:remotename\n", s); 512 printf(" %s file1 file2 ... fileN [[host:]remote-directory]\n", s); 513} 514 515/* 516 * Receive file(s). 517 */ 518static void 519get(int argc, char *argv[]) 520{ 521 int fd; 522 int n; 523 char *cp; 524 char *src; 525 char line[MAXLINE]; 526 527 if (argc < 2) { 528 strcpy(line, "get "); 529 printf("(files) "); 530 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 531 makeargv(line); 532 argc = margc; 533 argv = margv; 534 } 535 if (argc < 2) { 536 getusage(argv[0]); 537 return; 538 } 539 if (!connected) { 540 for (n = 1; n < argc ; n++) 541 if (strrchr(argv[n], ':') == 0) { 542 printf("No remote host specified and " 543 "no host given for file '%s'\n", argv[n]); 544 getusage(argv[0]); 545 return; 546 } 547 } 548 for (n = 1; n < argc ; n++) { 549 src = strrchr(argv[n], ':'); 550 if (src == NULL) 551 src = argv[n]; 552 else { 553 char *lcp; 554 555 *src++ = 0; 556 lcp = argv[n]; 557 if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') { 558 lcp[strlen(lcp) - 1] = '\0'; 559 lcp++; 560 } 561 setpeer0(lcp, NULL); 562 if (!connected) 563 continue; 564 } 565 if (argc < 4) { 566 cp = argc == 3 ? argv[2] : tail(src); 567 fd = creat(cp, 0644); 568 if (fd < 0) { 569 warn("%s", cp); 570 return; 571 } 572 if (verbose) 573 printf("getting from %s:%s to %s [%s]\n", 574 hostname, src, cp, mode); 575 recvfile(peer, port, fd, src, mode); 576 break; 577 } 578 cp = tail(src); /* new .. jdg */ 579 fd = creat(cp, 0644); 580 if (fd < 0) { 581 warn("%s", cp); 582 continue; 583 } 584 if (verbose) 585 printf("getting from %s:%s to %s [%s]\n", 586 hostname, src, cp, mode); 587 recvfile(peer, port, fd, src, mode); 588 } 589} 590 591static void 592getusage(char *s) 593{ 594 595 printf("usage: %s file [localname]\n", s); 596 printf(" %s [host:]file [localname]\n", s); 597 printf(" %s [host1:]file1 [host2:]file2 ... [hostN:]fileN\n", s); 598} 599 600static void 601settimeoutpacket(int argc, char *argv[]) 602{ 603 int t; 604 char line[MAXLINE]; 605 606 if (argc < 2) { 607 strcpy(line, "Packet timeout "); 608 printf("(value) "); 609 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 610 makeargv(line); 611 argc = margc; 612 argv = margv; 613 } 614 if (argc != 2) { 615 printf("usage: %s value\n", argv[0]); 616 return; 617 } 618 t = atoi(argv[1]); 619 if (t < 0) { 620 printf("%s: bad value\n", argv[1]); 621 return; 622 } 623 624 settimeouts(t, timeoutnetwork, maxtimeouts); 625} 626 627static void 628settimeoutnetwork(int argc, char *argv[]) 629{ 630 int t; 631 char line[MAXLINE]; 632 633 if (argc < 2) { 634 strcpy(line, "Network timeout "); 635 printf("(value) "); 636 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 637 makeargv(line); 638 argc = margc; 639 argv = margv; 640 } 641 if (argc != 2) { 642 printf("usage: %s value\n", argv[0]); 643 return; 644 } 645 t = atoi(argv[1]); 646 if (t < 0) { 647 printf("%s: bad value\n", argv[1]); 648 return; 649 } 650 651 settimeouts(timeoutpacket, t, maxtimeouts); 652} 653 654static void 655showstatus(int argc __unused, char *argv[] __unused) 656{ 657 658 printf("Remote host: %s\n", 659 connected ? hostname : "none specified yet"); 660 printf("RFC2347 Options support: %s\n", 661 options_rfc_enabled ? "enabled" : "disabled"); 662 printf("Non-RFC defined options support: %s\n", 663 options_extra_enabled ? "enabled" : "disabled"); 664 printf("Mode: %s\n", mode); 665 printf("Verbose: %s\n", verbose ? "on" : "off"); 666 printf("Debug: %s\n", debug_show(debug)); 667 printf("Artificial packetloss: %d in 100 packets\n", 668 packetdroppercentage); 669 printf("Segment size: %d bytes\n", segsize); 670 printf("Network timeout: %d seconds\n", timeoutpacket); 671 printf("Maximum network timeout: %d seconds\n", timeoutnetwork); 672 printf("Maximum timeouts: %d \n", maxtimeouts); 673} 674 675static void 676intr(int dummy __unused) 677{ 678 679 signal(SIGALRM, SIG_IGN); 680 alarm(0); 681 longjmp(toplevel, -1); 682} 683 684static char * 685tail(char *filename) 686{ 687 char *s; 688 689 while (*filename) { 690 s = strrchr(filename, '/'); 691 if (s == NULL) 692 break; 693 if (s[1]) 694 return (s + 1); 695 *s = '\0'; 696 } 697 return (filename); 698} 699 700static const char * 701command_prompt(void) 702{ 703 704 return ("tftp> "); 705} 706 707/* 708 * Command parser. 709 */ 710static void 711command(void) 712{ 713 HistEvent he; 714 struct cmd *c; 715 static EditLine *el; 716 static History *hist; 717 const char *bp; 718 char *cp; 719 int len, num, vrbose; 720 char line[MAXLINE]; 721 722 vrbose = isatty(0); 723 if (vrbose) { 724 el = el_init("tftp", stdin, stdout, stderr); 725 hist = history_init(); 726 history(hist, &he, H_SETSIZE, 100); 727 el_set(el, EL_HIST, history, hist); 728 el_set(el, EL_EDITOR, "emacs"); 729 el_set(el, EL_PROMPT, command_prompt); 730 el_set(el, EL_SIGNAL, 1); 731 el_source(el, NULL); 732 } 733 for (;;) { 734 if (vrbose) { 735 if ((bp = el_gets(el, &num)) == NULL || num == 0) 736 exit(0); 737 len = (num > MAXLINE) ? MAXLINE : num; 738 memcpy(line, bp, len); 739 line[len] = '\0'; 740 history(hist, &he, H_ENTER, bp); 741 } else { 742 line[0] = 0; 743 if (fgets(line, sizeof line , stdin) == NULL) { 744 if (feof(stdin)) { 745 exit(txrx_error); 746 } else { 747 continue; 748 } 749 } 750 } 751 if ((cp = strchr(line, '\n'))) 752 *cp = '\0'; 753 if (line[0] == 0) 754 continue; 755 makeargv(line); 756 if (margc == 0) 757 continue; 758 c = getcmd(margv[0]); 759 if (c == (struct cmd *)-1) { 760 printf("?Ambiguous command\n"); 761 continue; 762 } 763 if (c == 0) { 764 printf("?Invalid command\n"); 765 continue; 766 } 767 (*c->handler)(margc, margv); 768 } 769} 770 771static struct cmd * 772getcmd(char *name) 773{ 774 const char *p, *q; 775 struct cmd *c, *found; 776 int nmatches, longest; 777 778 longest = 0; 779 nmatches = 0; 780 found = 0; 781 for (c = cmdtab; (p = c->name) != NULL; c++) { 782 for (q = name; *q == *p++; q++) 783 if (*q == 0) /* exact match? */ 784 return (c); 785 if (!*q) { /* the name was a prefix */ 786 if (q - name > longest) { 787 longest = q - name; 788 nmatches = 1; 789 found = c; 790 } else if (q - name == longest) 791 nmatches++; 792 } 793 } 794 if (nmatches > 1) 795 return ((struct cmd *)-1); 796 return (found); 797} 798 799/* 800 * Slice a string up into argc/argv. 801 */ 802static void 803makeargv(char *line) 804{ 805 char *cp; 806 char **argp = margv; 807 808 margc = 0; 809 if ((cp = strchr(line, '\n')) != NULL) 810 *cp = '\0'; 811 for (cp = line; margc < MAX_MARGV - 1 && *cp != '\0';) { 812 while (isspace(*cp)) 813 cp++; 814 if (*cp == '\0') 815 break; 816 *argp++ = cp; 817 margc += 1; 818 while (*cp != '\0' && !isspace(*cp)) 819 cp++; 820 if (*cp == '\0') 821 break; 822 *cp++ = '\0'; 823 } 824 *argp++ = 0; 825} 826 827static void 828quit(int argc __unused, char *argv[] __unused) 829{ 830 831 exit(txrx_error); 832} 833 834/* 835 * Help command. 836 */ 837static void 838help(int argc, char *argv[]) 839{ 840 struct cmd *c; 841 842 if (argc == 1) { 843 printf("Commands may be abbreviated. Commands are:\n\n"); 844 for (c = cmdtab; c->name; c++) 845 printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help); 846 847 printf("\n[-] : You shouldn't use these ones anymore.\n"); 848 printf("[*] : RFC2347 options support required.\n"); 849 printf("[**] : Non-standard RFC2347 option.\n"); 850 return; 851 } 852 while (--argc > 0) { 853 char *arg; 854 arg = *++argv; 855 c = getcmd(arg); 856 if (c == (struct cmd *)-1) 857 printf("?Ambiguous help command: %s\n", arg); 858 else if (c == (struct cmd *)0) 859 printf("?Invalid help command: %s\n", arg); 860 else 861 printf("%s\n", c->help); 862 } 863} 864 865static void 866setverbose(int argc __unused, char *argv[] __unused) 867{ 868 869 verbose = !verbose; 870 printf("Verbose mode %s.\n", verbose ? "on" : "off"); 871} 872 873static void 874setoptions(int argc, char *argv[]) 875{ 876 877 if (argc == 2) { 878 if (strcasecmp(argv[1], "enable") == 0 || 879 strcasecmp(argv[1], "on") == 0) { 880 options_extra_enabled = 1; 881 options_rfc_enabled = 1; 882 } 883 if (strcasecmp(argv[1], "disable") == 0 || 884 strcasecmp(argv[1], "off") == 0) { 885 options_extra_enabled = 0; 886 options_rfc_enabled = 0; 887 } 888 if (strcasecmp(argv[1], "extra") == 0) 889 options_extra_enabled = !options_extra_enabled; 890 } 891 printf("Support for RFC2347 style options are now %s.\n", 892 options_rfc_enabled ? "enabled" : "disabled"); 893 printf("Support for non-RFC defined options are now %s.\n", 894 options_extra_enabled ? "enabled" : "disabled"); 895 896 printf("\nThe following options are available:\n" 897 "\toptions on : enable support for RFC2347 style options\n" 898 "\toptions off : disable support for RFC2347 style options\n" 899 "\toptions extra : toggle support for non-RFC defined options\n" 900 ); 901} 902 903static void 904setrollover(int argc, char *argv[]) 905{ 906 907 if (argc == 2) { 908 if (strcasecmp(argv[1], "never") == 0 || 909 strcasecmp(argv[1], "none") == 0) { 910 free(options[OPT_ROLLOVER].o_request); 911 options[OPT_ROLLOVER].o_request = NULL; 912 } 913 if (strcasecmp(argv[1], "1") == 0) { 914 free(options[OPT_ROLLOVER].o_request); 915 options[OPT_ROLLOVER].o_request = strdup("1"); 916 } 917 if (strcasecmp(argv[1], "0") == 0) { 918 free(options[OPT_ROLLOVER].o_request); 919 options[OPT_ROLLOVER].o_request = strdup("0"); 920 } 921 } 922 printf("Support for the rollover options is %s.\n", 923 options[OPT_ROLLOVER].o_request != NULL ? "enabled" : "disabled"); 924 if (options[OPT_ROLLOVER].o_request != NULL) 925 printf("Block rollover will be to block %s.\n", 926 options[OPT_ROLLOVER].o_request); 927 928 929 printf("\nThe following rollover options are available:\n" 930 "\trollover 0 : rollover to block zero (default)\n" 931 "\trollover 1 : rollover to block one\n" 932 "\trollover never : do not support the rollover option\n" 933 "\trollover none : do not support the rollover option\n" 934 ); 935} 936 937static void 938setdebug(int argc, char *argv[]) 939{ 940 int i; 941 942 if (argc != 1) { 943 i = 1; 944 while (i < argc) 945 debug ^= debug_find(argv[i++]); 946 } 947 printf("The following debugging is enabled: %s\n", debug_show(debug)); 948 949 printf("\nThe following debugs are available:\n"); 950 i = 0; 951 while (debugs[i].name != NULL) { 952 printf("\t%s\t%s\n", debugs[i].name, debugs[i].desc); 953 i++; 954 } 955} 956 957static void 958setblocksize(int argc, char *argv[]) 959{ 960 961 if (!options_rfc_enabled) 962 printf("RFC2347 style options are not enabled " 963 "(but proceeding anyway)\n"); 964 965 if (argc != 1) { 966 int size = atoi(argv[1]); 967 size_t max; 968 u_long maxdgram; 969 970 max = sizeof(maxdgram); 971 if (sysctlbyname("net.inet.udp.maxdgram", 972 &maxdgram, &max, NULL, 0) < 0) { 973 perror("sysctl: net.inet.udp.maxdgram"); 974 return; 975 } 976 977 if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) { 978 printf("Blocksize should be between %d and %d bytes.\n", 979 BLKSIZE_MIN, BLKSIZE_MAX); 980 return; 981 } else if (size > (int)maxdgram - 4) { 982 printf("Blocksize can't be bigger than %ld bytes due " 983 "to the net.inet.udp.maxdgram sysctl limitation.\n", 984 maxdgram - 4); 985 asprintf(&options[OPT_BLKSIZE].o_request, 986 "%ld", maxdgram - 4); 987 } else { 988 asprintf(&options[OPT_BLKSIZE].o_request, "%d", size); 989 } 990 } 991 printf("Blocksize is now %s bytes.\n", options[OPT_BLKSIZE].o_request); 992} 993 994static void 995setblocksize2(int argc, char *argv[]) 996{ 997 998 if (!options_rfc_enabled || !options_extra_enabled) 999 printf( 1000 "RFC2347 style or non-RFC defined options are not enabled " 1001 "(but proceeding anyway)\n"); 1002 1003 if (argc != 1) { 1004 int size = atoi(argv[1]); 1005 int i; 1006 size_t max; 1007 u_long maxdgram; 1008 1009 int sizes[] = { 1010 8, 16, 32, 64, 128, 256, 512, 1024, 1011 2048, 4096, 8192, 16384, 32768, 0 1012 }; 1013 1014 max = sizeof(maxdgram); 1015 if (sysctlbyname("net.inet.udp.maxdgram", 1016 &maxdgram, &max, NULL, 0) < 0) { 1017 perror("sysctl: net.inet.udp.maxdgram"); 1018 return; 1019 } 1020 1021 for (i = 0; sizes[i] != 0; i++) { 1022 if (sizes[i] == size) break; 1023 } 1024 if (sizes[i] == 0) { 1025 printf("Blocksize2 should be a power of two between " 1026 "8 and 32768.\n"); 1027 return; 1028 } 1029 1030 if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) { 1031 printf("Blocksize2 should be between " 1032 "%d and %d bytes.\n", BLKSIZE_MIN, BLKSIZE_MAX); 1033 return; 1034 } else if (size > (int)maxdgram - 4) { 1035 printf("Blocksize2 can't be bigger than %ld bytes due " 1036 "to the net.inet.udp.maxdgram sysctl limitation.\n", 1037 maxdgram - 4); 1038 for (i = 0; sizes[i+1] != 0; i++) { 1039 if ((int)maxdgram < sizes[i+1]) break; 1040 } 1041 asprintf(&options[OPT_BLKSIZE2].o_request, 1042 "%d", sizes[i]); 1043 } else { 1044 asprintf(&options[OPT_BLKSIZE2].o_request, "%d", size); 1045 } 1046 } 1047 printf("Blocksize2 is now %s bytes.\n", 1048 options[OPT_BLKSIZE2].o_request); 1049} 1050 1051static void 1052setpacketdrop(int argc, char *argv[]) 1053{ 1054 1055 if (argc != 1) 1056 packetdroppercentage = atoi(argv[1]); 1057 1058 printf("Randomly %d in 100 packets will be dropped\n", 1059 packetdroppercentage); 1060} 1061