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