19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 35fd129a02SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 41fd129a02SPhilippe Charnier #if 0 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 43fd129a02SPhilippe Charnier #endif 44fd129a02SPhilippe Charnier static const char rcsid[] = 45c3aac50fSPeter Wemm "$FreeBSD$"; 469b50d902SRodney W. Grimes #endif /* not lint */ 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ 499b50d902SRodney W. Grimes 509b50d902SRodney W. Grimes /* 519b50d902SRodney W. Grimes * TFTP User Program -- Command Interface. 529b50d902SRodney W. Grimes */ 53fd129a02SPhilippe Charnier #include <sys/param.h> 549b50d902SRodney W. Grimes #include <sys/types.h> 559b50d902SRodney W. Grimes #include <sys/socket.h> 569b50d902SRodney W. Grimes #include <sys/file.h> 57a716ad66SWarner Losh #include <sys/param.h> 589b50d902SRodney W. Grimes 599b50d902SRodney W. Grimes #include <netinet/in.h> 609b50d902SRodney W. Grimes 619b50d902SRodney W. Grimes #include <arpa/inet.h> 629b50d902SRodney W. Grimes 639b50d902SRodney W. Grimes #include <ctype.h> 64fd129a02SPhilippe Charnier #include <err.h> 658979160aSBruce Evans #include <histedit.h> 669b50d902SRodney W. Grimes #include <netdb.h> 679b50d902SRodney W. Grimes #include <setjmp.h> 689b50d902SRodney W. Grimes #include <signal.h> 699b50d902SRodney W. Grimes #include <stdio.h> 709b50d902SRodney W. Grimes #include <stdlib.h> 719b50d902SRodney W. Grimes #include <string.h> 729b50d902SRodney W. Grimes #include <unistd.h> 739b50d902SRodney W. Grimes 749b50d902SRodney W. Grimes #include "extern.h" 759b50d902SRodney W. Grimes 76c33ed450SMatthew N. Dodd #define MAXLINE 200 778979160aSBruce Evans #define TIMEOUT 5 /* secs between rexmt's */ 78c33ed450SMatthew N. Dodd 799b50d902SRodney W. Grimes struct sockaddr_in peeraddr; 809b50d902SRodney W. Grimes int f; 819b50d902SRodney W. Grimes short port; 829b50d902SRodney W. Grimes int trace; 839b50d902SRodney W. Grimes int verbose; 849b50d902SRodney W. Grimes int connected; 859b50d902SRodney W. Grimes char mode[32]; 86c33ed450SMatthew N. Dodd char line[MAXLINE]; 879b50d902SRodney W. Grimes int margc; 889b50d902SRodney W. Grimes char *margv[20]; 899b50d902SRodney W. Grimes jmp_buf toplevel; 909b50d902SRodney W. Grimes void intr(); 919b50d902SRodney W. Grimes struct servent *sp; 929b50d902SRodney W. Grimes 939b50d902SRodney W. Grimes void get __P((int, char **)); 949b50d902SRodney W. Grimes void help __P((int, char **)); 959b50d902SRodney W. Grimes void modecmd __P((int, char **)); 969b50d902SRodney W. Grimes void put __P((int, char **)); 979b50d902SRodney W. Grimes void quit __P((int, char **)); 989b50d902SRodney W. Grimes void setascii __P((int, char **)); 999b50d902SRodney W. Grimes void setbinary __P((int, char **)); 1009b50d902SRodney W. Grimes void setpeer __P((int, char **)); 1019b50d902SRodney W. Grimes void setrexmt __P((int, char **)); 1029b50d902SRodney W. Grimes void settimeout __P((int, char **)); 1039b50d902SRodney W. Grimes void settrace __P((int, char **)); 1049b50d902SRodney W. Grimes void setverbose __P((int, char **)); 1059b50d902SRodney W. Grimes void status __P((int, char **)); 1069b50d902SRodney W. Grimes 107eaa86f9dSBruce Evans static void command __P((void)) __dead2; 1088979160aSBruce Evans static const char *command_prompt __P((void)); 1099b50d902SRodney W. Grimes 1109b50d902SRodney W. Grimes static void getusage __P((char *)); 1119b50d902SRodney W. Grimes static void makeargv __P((void)); 1129b50d902SRodney W. Grimes static void putusage __P((char *)); 1139b50d902SRodney W. Grimes static void settftpmode __P((char *)); 1149b50d902SRodney W. Grimes 1159b50d902SRodney W. Grimes #define HELPINDENT (sizeof("connect")) 1169b50d902SRodney W. Grimes 1179b50d902SRodney W. Grimes struct cmd { 1189b50d902SRodney W. Grimes char *name; 1199b50d902SRodney W. Grimes char *help; 1209b50d902SRodney W. Grimes void (*handler) __P((int, char **)); 1219b50d902SRodney W. Grimes }; 1229b50d902SRodney W. Grimes 1239b50d902SRodney W. Grimes char vhelp[] = "toggle verbose mode"; 1249b50d902SRodney W. Grimes char thelp[] = "toggle packet tracing"; 1259b50d902SRodney W. Grimes char chelp[] = "connect to remote tftp"; 1269b50d902SRodney W. Grimes char qhelp[] = "exit tftp"; 1279b50d902SRodney W. Grimes char hhelp[] = "print help information"; 1289b50d902SRodney W. Grimes char shelp[] = "send file"; 1299b50d902SRodney W. Grimes char rhelp[] = "receive file"; 1309b50d902SRodney W. Grimes char mhelp[] = "set file transfer mode"; 1319b50d902SRodney W. Grimes char sthelp[] = "show current status"; 1329b50d902SRodney W. Grimes char xhelp[] = "set per-packet retransmission timeout"; 1339b50d902SRodney W. Grimes char ihelp[] = "set total retransmission timeout"; 1349b50d902SRodney W. Grimes char ashelp[] = "set mode to netascii"; 1359b50d902SRodney W. Grimes char bnhelp[] = "set mode to octet"; 1369b50d902SRodney W. Grimes 1379b50d902SRodney W. Grimes struct cmd cmdtab[] = { 1389b50d902SRodney W. Grimes { "connect", chelp, setpeer }, 1399b50d902SRodney W. Grimes { "mode", mhelp, modecmd }, 1409b50d902SRodney W. Grimes { "put", shelp, put }, 1419b50d902SRodney W. Grimes { "get", rhelp, get }, 1429b50d902SRodney W. Grimes { "quit", qhelp, quit }, 1439b50d902SRodney W. Grimes { "verbose", vhelp, setverbose }, 1449b50d902SRodney W. Grimes { "trace", thelp, settrace }, 1459b50d902SRodney W. Grimes { "status", sthelp, status }, 1469b50d902SRodney W. Grimes { "binary", bnhelp, setbinary }, 1479b50d902SRodney W. Grimes { "ascii", ashelp, setascii }, 1489b50d902SRodney W. Grimes { "rexmt", xhelp, setrexmt }, 1499b50d902SRodney W. Grimes { "timeout", ihelp, settimeout }, 1509b50d902SRodney W. Grimes { "?", hhelp, help }, 1519b50d902SRodney W. Grimes { 0 } 1529b50d902SRodney W. Grimes }; 1539b50d902SRodney W. Grimes 1549b50d902SRodney W. Grimes struct cmd *getcmd(); 1559b50d902SRodney W. Grimes char *tail(); 1569b50d902SRodney W. Grimes 1579b50d902SRodney W. Grimes int 1589b50d902SRodney W. Grimes main(argc, argv) 1599b50d902SRodney W. Grimes int argc; 1609b50d902SRodney W. Grimes char *argv[]; 1619b50d902SRodney W. Grimes { 1629b50d902SRodney W. Grimes struct sockaddr_in sin; 1639b50d902SRodney W. Grimes 1649b50d902SRodney W. Grimes sp = getservbyname("tftp", "udp"); 165fd129a02SPhilippe Charnier if (sp == 0) 166fd129a02SPhilippe Charnier errx(1, "udp/tftp: unknown service"); 1679b50d902SRodney W. Grimes f = socket(AF_INET, SOCK_DGRAM, 0); 168fd129a02SPhilippe Charnier if (f < 0) 169fd129a02SPhilippe Charnier err(3, "socket"); 1709b50d902SRodney W. Grimes bzero((char *)&sin, sizeof(sin)); 1719b50d902SRodney W. Grimes sin.sin_family = AF_INET; 172fd129a02SPhilippe Charnier if (bind(f, (struct sockaddr *)&sin, sizeof(sin)) < 0) 173fd129a02SPhilippe Charnier err(1, "bind"); 1749b50d902SRodney W. Grimes strcpy(mode, "netascii"); 1759b50d902SRodney W. Grimes signal(SIGINT, intr); 1769b50d902SRodney W. Grimes if (argc > 1) { 1779b50d902SRodney W. Grimes if (setjmp(toplevel) != 0) 1789b50d902SRodney W. Grimes exit(0); 1799b50d902SRodney W. Grimes setpeer(argc, argv); 1809b50d902SRodney W. Grimes } 1819b50d902SRodney W. Grimes if (setjmp(toplevel) != 0) 1829b50d902SRodney W. Grimes (void)putchar('\n'); 1839b50d902SRodney W. Grimes command(); 1849b50d902SRodney W. Grimes } 1859b50d902SRodney W. Grimes 186fd129a02SPhilippe Charnier char hostname[MAXHOSTNAMELEN]; 1879b50d902SRodney W. Grimes 1889b50d902SRodney W. Grimes void 1899b50d902SRodney W. Grimes setpeer(argc, argv) 1909b50d902SRodney W. Grimes int argc; 1919b50d902SRodney W. Grimes char *argv[]; 1929b50d902SRodney W. Grimes { 1939b50d902SRodney W. Grimes struct hostent *host; 1949b50d902SRodney W. Grimes 1959b50d902SRodney W. Grimes if (argc < 2) { 1969b50d902SRodney W. Grimes strcpy(line, "Connect "); 1979b50d902SRodney W. Grimes printf("(to) "); 198ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 1999b50d902SRodney W. Grimes makeargv(); 2009b50d902SRodney W. Grimes argc = margc; 2019b50d902SRodney W. Grimes argv = margv; 2029b50d902SRodney W. Grimes } 2039b50d902SRodney W. Grimes if (argc > 3) { 2049b50d902SRodney W. Grimes printf("usage: %s host-name [port]\n", argv[0]); 2059b50d902SRodney W. Grimes return; 2069b50d902SRodney W. Grimes } 2079b50d902SRodney W. Grimes host = gethostbyname(argv[1]); 2089b50d902SRodney W. Grimes if (host) { 2099b50d902SRodney W. Grimes peeraddr.sin_family = host->h_addrtype; 210a716ad66SWarner Losh bcopy(host->h_addr, &peeraddr.sin_addr, 211a716ad66SWarner Losh MIN(sizeof(peeraddr.sin_addr), host->h_length)); 212a716ad66SWarner Losh strncpy(hostname, host->h_name, sizeof(hostname)); 2139b50d902SRodney W. Grimes } else { 2149b50d902SRodney W. Grimes peeraddr.sin_family = AF_INET; 2159b50d902SRodney W. Grimes peeraddr.sin_addr.s_addr = inet_addr(argv[1]); 2169b50d902SRodney W. Grimes if (peeraddr.sin_addr.s_addr == -1) { 2179b50d902SRodney W. Grimes connected = 0; 2189b50d902SRodney W. Grimes printf("%s: unknown host\n", argv[1]); 2199b50d902SRodney W. Grimes return; 2209b50d902SRodney W. Grimes } 221a716ad66SWarner Losh strncpy(hostname, argv[1], sizeof(hostname)); 2229b50d902SRodney W. Grimes } 223a716ad66SWarner Losh hostname[sizeof(hostname) - 1] = '\0'; 2249b50d902SRodney W. Grimes port = sp->s_port; 2259b50d902SRodney W. Grimes if (argc == 3) { 2269b50d902SRodney W. Grimes port = atoi(argv[2]); 2279b50d902SRodney W. Grimes if (port < 0) { 2289b50d902SRodney W. Grimes printf("%s: bad port number\n", argv[2]); 2299b50d902SRodney W. Grimes connected = 0; 2309b50d902SRodney W. Grimes return; 2319b50d902SRodney W. Grimes } 2329b50d902SRodney W. Grimes port = htons(port); 2339b50d902SRodney W. Grimes } 2349b50d902SRodney W. Grimes connected = 1; 2359b50d902SRodney W. Grimes } 2369b50d902SRodney W. Grimes 2379b50d902SRodney W. Grimes struct modes { 2389b50d902SRodney W. Grimes char *m_name; 2399b50d902SRodney W. Grimes char *m_mode; 2409b50d902SRodney W. Grimes } modes[] = { 2419b50d902SRodney W. Grimes { "ascii", "netascii" }, 2429b50d902SRodney W. Grimes { "netascii", "netascii" }, 2439b50d902SRodney W. Grimes { "binary", "octet" }, 2449b50d902SRodney W. Grimes { "image", "octet" }, 2459b50d902SRodney W. Grimes { "octet", "octet" }, 2469b50d902SRodney W. Grimes /* { "mail", "mail" }, */ 2479b50d902SRodney W. Grimes { 0, 0 } 2489b50d902SRodney W. Grimes }; 2499b50d902SRodney W. Grimes 2509b50d902SRodney W. Grimes void 2519b50d902SRodney W. Grimes modecmd(argc, argv) 2529b50d902SRodney W. Grimes int argc; 2539b50d902SRodney W. Grimes char *argv[]; 2549b50d902SRodney W. Grimes { 2559b50d902SRodney W. Grimes register struct modes *p; 2569b50d902SRodney W. Grimes char *sep; 2579b50d902SRodney W. Grimes 2589b50d902SRodney W. Grimes if (argc < 2) { 2599b50d902SRodney W. Grimes printf("Using %s mode to transfer files.\n", mode); 2609b50d902SRodney W. Grimes return; 2619b50d902SRodney W. Grimes } 2629b50d902SRodney W. Grimes if (argc == 2) { 2639b50d902SRodney W. Grimes for (p = modes; p->m_name; p++) 2649b50d902SRodney W. Grimes if (strcmp(argv[1], p->m_name) == 0) 2659b50d902SRodney W. Grimes break; 2669b50d902SRodney W. Grimes if (p->m_name) { 2679b50d902SRodney W. Grimes settftpmode(p->m_mode); 2689b50d902SRodney W. Grimes return; 2699b50d902SRodney W. Grimes } 2709b50d902SRodney W. Grimes printf("%s: unknown mode\n", argv[1]); 2719b50d902SRodney W. Grimes /* drop through and print usage message */ 2729b50d902SRodney W. Grimes } 2739b50d902SRodney W. Grimes 2749b50d902SRodney W. Grimes printf("usage: %s [", argv[0]); 2759b50d902SRodney W. Grimes sep = " "; 2769b50d902SRodney W. Grimes for (p = modes; p->m_name; p++) { 2779b50d902SRodney W. Grimes printf("%s%s", sep, p->m_name); 2789b50d902SRodney W. Grimes if (*sep == ' ') 2799b50d902SRodney W. Grimes sep = " | "; 2809b50d902SRodney W. Grimes } 2819b50d902SRodney W. Grimes printf(" ]\n"); 2829b50d902SRodney W. Grimes return; 2839b50d902SRodney W. Grimes } 2849b50d902SRodney W. Grimes 2859b50d902SRodney W. Grimes void 2869b50d902SRodney W. Grimes setbinary(argc, argv) 2879b50d902SRodney W. Grimes int argc; 2889b50d902SRodney W. Grimes char *argv[]; 2899b50d902SRodney W. Grimes { 2909b50d902SRodney W. Grimes 2919b50d902SRodney W. Grimes settftpmode("octet"); 2929b50d902SRodney W. Grimes } 2939b50d902SRodney W. Grimes 2949b50d902SRodney W. Grimes void 2959b50d902SRodney W. Grimes setascii(argc, argv) 2969b50d902SRodney W. Grimes int argc; 2979b50d902SRodney W. Grimes char *argv[]; 2989b50d902SRodney W. Grimes { 2999b50d902SRodney W. Grimes 3009b50d902SRodney W. Grimes settftpmode("netascii"); 3019b50d902SRodney W. Grimes } 3029b50d902SRodney W. Grimes 3039b50d902SRodney W. Grimes static void 3049b50d902SRodney W. Grimes settftpmode(newmode) 3059b50d902SRodney W. Grimes char *newmode; 3069b50d902SRodney W. Grimes { 3079b50d902SRodney W. Grimes strcpy(mode, newmode); 3089b50d902SRodney W. Grimes if (verbose) 3099b50d902SRodney W. Grimes printf("mode set to %s\n", mode); 3109b50d902SRodney W. Grimes } 3119b50d902SRodney W. Grimes 3129b50d902SRodney W. Grimes 3139b50d902SRodney W. Grimes /* 3149b50d902SRodney W. Grimes * Send file(s). 3159b50d902SRodney W. Grimes */ 3169b50d902SRodney W. Grimes void 3179b50d902SRodney W. Grimes put(argc, argv) 3189b50d902SRodney W. Grimes int argc; 3199b50d902SRodney W. Grimes char *argv[]; 3209b50d902SRodney W. Grimes { 3219b50d902SRodney W. Grimes int fd; 3229b50d902SRodney W. Grimes register int n; 3239b50d902SRodney W. Grimes register char *cp, *targ; 3249b50d902SRodney W. Grimes 3259b50d902SRodney W. Grimes if (argc < 2) { 3269b50d902SRodney W. Grimes strcpy(line, "send "); 3279b50d902SRodney W. Grimes printf("(file) "); 328ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 3299b50d902SRodney W. Grimes makeargv(); 3309b50d902SRodney W. Grimes argc = margc; 3319b50d902SRodney W. Grimes argv = margv; 3329b50d902SRodney W. Grimes } 3339b50d902SRodney W. Grimes if (argc < 2) { 3349b50d902SRodney W. Grimes putusage(argv[0]); 3359b50d902SRodney W. Grimes return; 3369b50d902SRodney W. Grimes } 3379b50d902SRodney W. Grimes targ = argv[argc - 1]; 3389b50d902SRodney W. Grimes if (index(argv[argc - 1], ':')) { 3399b50d902SRodney W. Grimes char *cp; 3409b50d902SRodney W. Grimes struct hostent *hp; 3419b50d902SRodney W. Grimes 3429b50d902SRodney W. Grimes for (n = 1; n < argc - 1; n++) 3439b50d902SRodney W. Grimes if (index(argv[n], ':')) { 3449b50d902SRodney W. Grimes putusage(argv[0]); 3459b50d902SRodney W. Grimes return; 3469b50d902SRodney W. Grimes } 3479b50d902SRodney W. Grimes cp = argv[argc - 1]; 3489b50d902SRodney W. Grimes targ = index(cp, ':'); 3499b50d902SRodney W. Grimes *targ++ = 0; 3509b50d902SRodney W. Grimes hp = gethostbyname(cp); 3519b50d902SRodney W. Grimes if (hp == NULL) { 3529b50d902SRodney W. Grimes fprintf(stderr, "tftp: %s: ", cp); 3539b50d902SRodney W. Grimes herror((char *)NULL); 3549b50d902SRodney W. Grimes return; 3559b50d902SRodney W. Grimes } 356a716ad66SWarner Losh bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, 357a716ad66SWarner Losh MIN(sizeof(peeraddr.sin_addr), hp->h_length)); 3589b50d902SRodney W. Grimes peeraddr.sin_family = hp->h_addrtype; 3599b50d902SRodney W. Grimes connected = 1; 360a716ad66SWarner Losh strncpy(hostname, hp->h_name, sizeof(hostname)); 361a716ad66SWarner Losh hostname[sizeof(hostname) - 1] = '\0'; 3629b50d902SRodney W. Grimes } 3639b50d902SRodney W. Grimes if (!connected) { 3649b50d902SRodney W. Grimes printf("No target machine specified.\n"); 3659b50d902SRodney W. Grimes return; 3669b50d902SRodney W. Grimes } 3679b50d902SRodney W. Grimes if (argc < 4) { 3689b50d902SRodney W. Grimes cp = argc == 2 ? tail(targ) : argv[1]; 3699b50d902SRodney W. Grimes fd = open(cp, O_RDONLY); 3709b50d902SRodney W. Grimes if (fd < 0) { 371fd129a02SPhilippe Charnier warn("%s", cp); 3729b50d902SRodney W. Grimes return; 3739b50d902SRodney W. Grimes } 3749b50d902SRodney W. Grimes if (verbose) 3759b50d902SRodney W. Grimes printf("putting %s to %s:%s [%s]\n", 3769b50d902SRodney W. Grimes cp, hostname, targ, mode); 3779b50d902SRodney W. Grimes peeraddr.sin_port = port; 3788692ad46SDavid Greenman xmitfile(fd, targ, mode); 3799b50d902SRodney W. Grimes return; 3809b50d902SRodney W. Grimes } 3819b50d902SRodney W. Grimes /* this assumes the target is a directory */ 3829b50d902SRodney W. Grimes /* on a remote unix system. hmmmm. */ 3839b50d902SRodney W. Grimes cp = index(targ, '\0'); 3849b50d902SRodney W. Grimes *cp++ = '/'; 3859b50d902SRodney W. Grimes for (n = 1; n < argc - 1; n++) { 3869b50d902SRodney W. Grimes strcpy(cp, tail(argv[n])); 3879b50d902SRodney W. Grimes fd = open(argv[n], O_RDONLY); 3889b50d902SRodney W. Grimes if (fd < 0) { 389fd129a02SPhilippe Charnier warn("%s", argv[n]); 3909b50d902SRodney W. Grimes continue; 3919b50d902SRodney W. Grimes } 3929b50d902SRodney W. Grimes if (verbose) 3939b50d902SRodney W. Grimes printf("putting %s to %s:%s [%s]\n", 3949b50d902SRodney W. Grimes argv[n], hostname, targ, mode); 3959b50d902SRodney W. Grimes peeraddr.sin_port = port; 3968692ad46SDavid Greenman xmitfile(fd, targ, mode); 3979b50d902SRodney W. Grimes } 3989b50d902SRodney W. Grimes } 3999b50d902SRodney W. Grimes 4009b50d902SRodney W. Grimes static void 4019b50d902SRodney W. Grimes putusage(s) 4029b50d902SRodney W. Grimes char *s; 4039b50d902SRodney W. Grimes { 4049b50d902SRodney W. Grimes printf("usage: %s file ... host:target, or\n", s); 4059b50d902SRodney W. Grimes printf(" %s file ... target (when already connected)\n", s); 4069b50d902SRodney W. Grimes } 4079b50d902SRodney W. Grimes 4089b50d902SRodney W. Grimes /* 4099b50d902SRodney W. Grimes * Receive file(s). 4109b50d902SRodney W. Grimes */ 4119b50d902SRodney W. Grimes void 4129b50d902SRodney W. Grimes get(argc, argv) 4139b50d902SRodney W. Grimes int argc; 4149b50d902SRodney W. Grimes char *argv[]; 4159b50d902SRodney W. Grimes { 4169b50d902SRodney W. Grimes int fd; 4179b50d902SRodney W. Grimes register int n; 4189b50d902SRodney W. Grimes register char *cp; 4199b50d902SRodney W. Grimes char *src; 4209b50d902SRodney W. Grimes 4219b50d902SRodney W. Grimes if (argc < 2) { 4229b50d902SRodney W. Grimes strcpy(line, "get "); 4239b50d902SRodney W. Grimes printf("(files) "); 424ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 4259b50d902SRodney W. Grimes makeargv(); 4269b50d902SRodney W. Grimes argc = margc; 4279b50d902SRodney W. Grimes argv = margv; 4289b50d902SRodney W. Grimes } 4299b50d902SRodney W. Grimes if (argc < 2) { 4309b50d902SRodney W. Grimes getusage(argv[0]); 4319b50d902SRodney W. Grimes return; 4329b50d902SRodney W. Grimes } 4339b50d902SRodney W. Grimes if (!connected) { 4349b50d902SRodney W. Grimes for (n = 1; n < argc ; n++) 4359b50d902SRodney W. Grimes if (index(argv[n], ':') == 0) { 4369b50d902SRodney W. Grimes getusage(argv[0]); 4379b50d902SRodney W. Grimes return; 4389b50d902SRodney W. Grimes } 4399b50d902SRodney W. Grimes } 4409b50d902SRodney W. Grimes for (n = 1; n < argc ; n++) { 4419b50d902SRodney W. Grimes src = index(argv[n], ':'); 4429b50d902SRodney W. Grimes if (src == NULL) 4439b50d902SRodney W. Grimes src = argv[n]; 4449b50d902SRodney W. Grimes else { 4459b50d902SRodney W. Grimes struct hostent *hp; 4469b50d902SRodney W. Grimes 4479b50d902SRodney W. Grimes *src++ = 0; 4489b50d902SRodney W. Grimes hp = gethostbyname(argv[n]); 4499b50d902SRodney W. Grimes if (hp == NULL) { 4509b50d902SRodney W. Grimes fprintf(stderr, "tftp: %s: ", argv[n]); 4519b50d902SRodney W. Grimes herror((char *)NULL); 4529b50d902SRodney W. Grimes continue; 4539b50d902SRodney W. Grimes } 4549b50d902SRodney W. Grimes bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, 455a716ad66SWarner Losh MIN(sizeof(peeraddr.sin_addr), hp->h_length)); 4569b50d902SRodney W. Grimes peeraddr.sin_family = hp->h_addrtype; 4579b50d902SRodney W. Grimes connected = 1; 458a716ad66SWarner Losh strncpy(hostname, hp->h_name, sizeof(hostname)); 459a716ad66SWarner Losh hostname[sizeof(hostname) - 1] = '\0'; 4609b50d902SRodney W. Grimes } 4619b50d902SRodney W. Grimes if (argc < 4) { 4629b50d902SRodney W. Grimes cp = argc == 3 ? argv[2] : tail(src); 4639b50d902SRodney W. Grimes fd = creat(cp, 0644); 4649b50d902SRodney W. Grimes if (fd < 0) { 465fd129a02SPhilippe Charnier warn("%s", cp); 4669b50d902SRodney W. Grimes return; 4679b50d902SRodney W. Grimes } 4689b50d902SRodney W. Grimes if (verbose) 4699b50d902SRodney W. Grimes printf("getting from %s:%s to %s [%s]\n", 4709b50d902SRodney W. Grimes hostname, src, cp, mode); 4719b50d902SRodney W. Grimes peeraddr.sin_port = port; 4729b50d902SRodney W. Grimes recvfile(fd, src, mode); 4739b50d902SRodney W. Grimes break; 4749b50d902SRodney W. Grimes } 4759b50d902SRodney W. Grimes cp = tail(src); /* new .. jdg */ 4769b50d902SRodney W. Grimes fd = creat(cp, 0644); 4779b50d902SRodney W. Grimes if (fd < 0) { 478fd129a02SPhilippe Charnier warn("%s", cp); 4799b50d902SRodney W. Grimes continue; 4809b50d902SRodney W. Grimes } 4819b50d902SRodney W. Grimes if (verbose) 4829b50d902SRodney W. Grimes printf("getting from %s:%s to %s [%s]\n", 4839b50d902SRodney W. Grimes hostname, src, cp, mode); 4849b50d902SRodney W. Grimes peeraddr.sin_port = port; 4859b50d902SRodney W. Grimes recvfile(fd, src, mode); 4869b50d902SRodney W. Grimes } 4879b50d902SRodney W. Grimes } 4889b50d902SRodney W. Grimes 4899b50d902SRodney W. Grimes static void 4909b50d902SRodney W. Grimes getusage(s) 4919b50d902SRodney W. Grimes char *s; 4929b50d902SRodney W. Grimes { 4939b50d902SRodney W. Grimes printf("usage: %s host:file host:file ... file, or\n", s); 4949b50d902SRodney W. Grimes printf(" %s file file ... file if connected\n", s); 4959b50d902SRodney W. Grimes } 4969b50d902SRodney W. Grimes 4979b50d902SRodney W. Grimes int rexmtval = TIMEOUT; 4989b50d902SRodney W. Grimes 4999b50d902SRodney W. Grimes void 5009b50d902SRodney W. Grimes setrexmt(argc, argv) 5019b50d902SRodney W. Grimes int argc; 5029b50d902SRodney W. Grimes char *argv[]; 5039b50d902SRodney W. Grimes { 5049b50d902SRodney W. Grimes int t; 5059b50d902SRodney W. Grimes 5069b50d902SRodney W. Grimes if (argc < 2) { 5079b50d902SRodney W. Grimes strcpy(line, "Rexmt-timeout "); 5089b50d902SRodney W. Grimes printf("(value) "); 509ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 5109b50d902SRodney W. Grimes makeargv(); 5119b50d902SRodney W. Grimes argc = margc; 5129b50d902SRodney W. Grimes argv = margv; 5139b50d902SRodney W. Grimes } 5149b50d902SRodney W. Grimes if (argc != 2) { 5159b50d902SRodney W. Grimes printf("usage: %s value\n", argv[0]); 5169b50d902SRodney W. Grimes return; 5179b50d902SRodney W. Grimes } 5189b50d902SRodney W. Grimes t = atoi(argv[1]); 5199b50d902SRodney W. Grimes if (t < 0) 5209b50d902SRodney W. Grimes printf("%s: bad value\n", argv[1]); 5219b50d902SRodney W. Grimes else 5229b50d902SRodney W. Grimes rexmtval = t; 5239b50d902SRodney W. Grimes } 5249b50d902SRodney W. Grimes 5259b50d902SRodney W. Grimes int maxtimeout = 5 * TIMEOUT; 5269b50d902SRodney W. Grimes 5279b50d902SRodney W. Grimes void 5289b50d902SRodney W. Grimes settimeout(argc, argv) 5299b50d902SRodney W. Grimes int argc; 5309b50d902SRodney W. Grimes char *argv[]; 5319b50d902SRodney W. Grimes { 5329b50d902SRodney W. Grimes int t; 5339b50d902SRodney W. Grimes 5349b50d902SRodney W. Grimes if (argc < 2) { 5359b50d902SRodney W. Grimes strcpy(line, "Maximum-timeout "); 5369b50d902SRodney W. Grimes printf("(value) "); 537ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 5389b50d902SRodney W. Grimes makeargv(); 5399b50d902SRodney W. Grimes argc = margc; 5409b50d902SRodney W. Grimes argv = margv; 5419b50d902SRodney W. Grimes } 5429b50d902SRodney W. Grimes if (argc != 2) { 5439b50d902SRodney W. Grimes printf("usage: %s value\n", argv[0]); 5449b50d902SRodney W. Grimes return; 5459b50d902SRodney W. Grimes } 5469b50d902SRodney W. Grimes t = atoi(argv[1]); 5479b50d902SRodney W. Grimes if (t < 0) 5489b50d902SRodney W. Grimes printf("%s: bad value\n", argv[1]); 5499b50d902SRodney W. Grimes else 5509b50d902SRodney W. Grimes maxtimeout = t; 5519b50d902SRodney W. Grimes } 5529b50d902SRodney W. Grimes 5539b50d902SRodney W. Grimes void 5549b50d902SRodney W. Grimes status(argc, argv) 5559b50d902SRodney W. Grimes int argc; 5569b50d902SRodney W. Grimes char *argv[]; 5579b50d902SRodney W. Grimes { 5589b50d902SRodney W. Grimes if (connected) 5599b50d902SRodney W. Grimes printf("Connected to %s.\n", hostname); 5609b50d902SRodney W. Grimes else 5619b50d902SRodney W. Grimes printf("Not connected.\n"); 5629b50d902SRodney W. Grimes printf("Mode: %s Verbose: %s Tracing: %s\n", mode, 5639b50d902SRodney W. Grimes verbose ? "on" : "off", trace ? "on" : "off"); 5649b50d902SRodney W. Grimes printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n", 5659b50d902SRodney W. Grimes rexmtval, maxtimeout); 5669b50d902SRodney W. Grimes } 5679b50d902SRodney W. Grimes 5689b50d902SRodney W. Grimes void 5699b50d902SRodney W. Grimes intr() 5709b50d902SRodney W. Grimes { 5719b50d902SRodney W. Grimes 5729b50d902SRodney W. Grimes signal(SIGALRM, SIG_IGN); 5739b50d902SRodney W. Grimes alarm(0); 5749b50d902SRodney W. Grimes longjmp(toplevel, -1); 5759b50d902SRodney W. Grimes } 5769b50d902SRodney W. Grimes 5779b50d902SRodney W. Grimes char * 5789b50d902SRodney W. Grimes tail(filename) 5799b50d902SRodney W. Grimes char *filename; 5809b50d902SRodney W. Grimes { 5819b50d902SRodney W. Grimes register char *s; 5829b50d902SRodney W. Grimes 5839b50d902SRodney W. Grimes while (*filename) { 5849b50d902SRodney W. Grimes s = rindex(filename, '/'); 5859b50d902SRodney W. Grimes if (s == NULL) 5869b50d902SRodney W. Grimes break; 5879b50d902SRodney W. Grimes if (s[1]) 5889b50d902SRodney W. Grimes return (s + 1); 5899b50d902SRodney W. Grimes *s = '\0'; 5909b50d902SRodney W. Grimes } 5919b50d902SRodney W. Grimes return (filename); 5929b50d902SRodney W. Grimes } 5939b50d902SRodney W. Grimes 594c33ed450SMatthew N. Dodd static const char * 5958979160aSBruce Evans command_prompt() 5968979160aSBruce Evans { 5978979160aSBruce Evans 598c33ed450SMatthew N. Dodd return ("tftp> "); 599c33ed450SMatthew N. Dodd } 600c33ed450SMatthew N. Dodd 6019b50d902SRodney W. Grimes /* 6029b50d902SRodney W. Grimes * Command parser. 6039b50d902SRodney W. Grimes */ 604eaa86f9dSBruce Evans static void 6059b50d902SRodney W. Grimes command() 6069b50d902SRodney W. Grimes { 607c33ed450SMatthew N. Dodd HistEvent he; 6088979160aSBruce Evans register struct cmd *c; 6098979160aSBruce Evans static EditLine *el; 6108979160aSBruce Evans static History *hist; 611c33ed450SMatthew N. Dodd const char *bp; 6128979160aSBruce Evans char *cp; 6138979160aSBruce Evans int len, num, verbose; 614c33ed450SMatthew N. Dodd 615c33ed450SMatthew N. Dodd verbose = isatty(0); 616c33ed450SMatthew N. Dodd if (verbose) { 617c33ed450SMatthew N. Dodd el = el_init("tftp", stdin, stdout, stderr); 618c33ed450SMatthew N. Dodd hist = history_init(); 619c33ed450SMatthew N. Dodd history(hist, &he, H_EVENT, 100); 620c33ed450SMatthew N. Dodd el_set(el, EL_HIST, history, hist); 621c33ed450SMatthew N. Dodd el_set(el, EL_EDITOR, "emacs"); 622c33ed450SMatthew N. Dodd el_set(el, EL_PROMPT, command_prompt); 623c33ed450SMatthew N. Dodd el_set(el, EL_SIGNAL, 1); 624c33ed450SMatthew N. Dodd el_source(el, NULL); 625c33ed450SMatthew N. Dodd } 6269b50d902SRodney W. Grimes for (;;) { 627c33ed450SMatthew N. Dodd if (verbose) { 628c33ed450SMatthew N. Dodd if ((bp = el_gets(el, &num)) == NULL || num == 0) 629c33ed450SMatthew N. Dodd exit(0); 630c33ed450SMatthew N. Dodd len = (num > MAXLINE) ? MAXLINE : num; 631c33ed450SMatthew N. Dodd memcpy(line, bp, len); 632c33ed450SMatthew N. Dodd line[len] = '\0'; 633c33ed450SMatthew N. Dodd history(hist, &he, H_ENTER, bp); 634c33ed450SMatthew N. Dodd } else { 635ca22ff9eSJoerg Wunsch if (fgets(line, sizeof line , stdin) == 0) { 6369b50d902SRodney W. Grimes if (feof(stdin)) { 6379b50d902SRodney W. Grimes exit(0); 6389b50d902SRodney W. Grimes } else { 6399b50d902SRodney W. Grimes continue; 6409b50d902SRodney W. Grimes } 6419b50d902SRodney W. Grimes } 642c33ed450SMatthew N. Dodd } 643ca22ff9eSJoerg Wunsch if ((cp = strchr(line, '\n'))) 644ca22ff9eSJoerg Wunsch *cp = '\0'; 6459b50d902SRodney W. Grimes if (line[0] == 0) 6469b50d902SRodney W. Grimes continue; 6479b50d902SRodney W. Grimes makeargv(); 6489b50d902SRodney W. Grimes if (margc == 0) 6499b50d902SRodney W. Grimes continue; 6509b50d902SRodney W. Grimes c = getcmd(margv[0]); 6519b50d902SRodney W. Grimes if (c == (struct cmd *)-1) { 6529b50d902SRodney W. Grimes printf("?Ambiguous command\n"); 6539b50d902SRodney W. Grimes continue; 6549b50d902SRodney W. Grimes } 6559b50d902SRodney W. Grimes if (c == 0) { 6569b50d902SRodney W. Grimes printf("?Invalid command\n"); 6579b50d902SRodney W. Grimes continue; 6589b50d902SRodney W. Grimes } 6599b50d902SRodney W. Grimes (*c->handler)(margc, margv); 6609b50d902SRodney W. Grimes } 6619b50d902SRodney W. Grimes } 6629b50d902SRodney W. Grimes 6639b50d902SRodney W. Grimes struct cmd * 6649b50d902SRodney W. Grimes getcmd(name) 6659b50d902SRodney W. Grimes register char *name; 6669b50d902SRodney W. Grimes { 6679b50d902SRodney W. Grimes register char *p, *q; 6689b50d902SRodney W. Grimes register struct cmd *c, *found; 6699b50d902SRodney W. Grimes register int nmatches, longest; 6709b50d902SRodney W. Grimes 6719b50d902SRodney W. Grimes longest = 0; 6729b50d902SRodney W. Grimes nmatches = 0; 6739b50d902SRodney W. Grimes found = 0; 6749b50d902SRodney W. Grimes for (c = cmdtab; (p = c->name) != NULL; c++) { 6759b50d902SRodney W. Grimes for (q = name; *q == *p++; q++) 6769b50d902SRodney W. Grimes if (*q == 0) /* exact match? */ 6779b50d902SRodney W. Grimes return (c); 6789b50d902SRodney W. Grimes if (!*q) { /* the name was a prefix */ 6799b50d902SRodney W. Grimes if (q - name > longest) { 6809b50d902SRodney W. Grimes longest = q - name; 6819b50d902SRodney W. Grimes nmatches = 1; 6829b50d902SRodney W. Grimes found = c; 6839b50d902SRodney W. Grimes } else if (q - name == longest) 6849b50d902SRodney W. Grimes nmatches++; 6859b50d902SRodney W. Grimes } 6869b50d902SRodney W. Grimes } 6879b50d902SRodney W. Grimes if (nmatches > 1) 6889b50d902SRodney W. Grimes return ((struct cmd *)-1); 6899b50d902SRodney W. Grimes return (found); 6909b50d902SRodney W. Grimes } 6919b50d902SRodney W. Grimes 6929b50d902SRodney W. Grimes /* 6939b50d902SRodney W. Grimes * Slice a string up into argc/argv. 6949b50d902SRodney W. Grimes */ 6959b50d902SRodney W. Grimes static void 6969b50d902SRodney W. Grimes makeargv() 6979b50d902SRodney W. Grimes { 6989b50d902SRodney W. Grimes register char *cp; 6999b50d902SRodney W. Grimes register char **argp = margv; 7009b50d902SRodney W. Grimes 7019b50d902SRodney W. Grimes margc = 0; 702ca22ff9eSJoerg Wunsch if ((cp = strchr(line, '\n'))) 703ca22ff9eSJoerg Wunsch *cp = '\0'; 7049b50d902SRodney W. Grimes for (cp = line; *cp;) { 7059b50d902SRodney W. Grimes while (isspace(*cp)) 7069b50d902SRodney W. Grimes cp++; 7079b50d902SRodney W. Grimes if (*cp == '\0') 7089b50d902SRodney W. Grimes break; 7099b50d902SRodney W. Grimes *argp++ = cp; 7109b50d902SRodney W. Grimes margc += 1; 7119b50d902SRodney W. Grimes while (*cp != '\0' && !isspace(*cp)) 7129b50d902SRodney W. Grimes cp++; 7139b50d902SRodney W. Grimes if (*cp == '\0') 7149b50d902SRodney W. Grimes break; 7159b50d902SRodney W. Grimes *cp++ = '\0'; 7169b50d902SRodney W. Grimes } 7179b50d902SRodney W. Grimes *argp++ = 0; 7189b50d902SRodney W. Grimes } 7199b50d902SRodney W. Grimes 7209b50d902SRodney W. Grimes void 7219b50d902SRodney W. Grimes quit(argc, argv) 7229b50d902SRodney W. Grimes int argc; 7239b50d902SRodney W. Grimes char *argv[]; 7249b50d902SRodney W. Grimes { 7259b50d902SRodney W. Grimes 7269b50d902SRodney W. Grimes exit(0); 7279b50d902SRodney W. Grimes } 7289b50d902SRodney W. Grimes 7299b50d902SRodney W. Grimes /* 7309b50d902SRodney W. Grimes * Help command. 7319b50d902SRodney W. Grimes */ 7329b50d902SRodney W. Grimes void 7339b50d902SRodney W. Grimes help(argc, argv) 7349b50d902SRodney W. Grimes int argc; 7359b50d902SRodney W. Grimes char *argv[]; 7369b50d902SRodney W. Grimes { 7379b50d902SRodney W. Grimes register struct cmd *c; 7389b50d902SRodney W. Grimes 7399b50d902SRodney W. Grimes if (argc == 1) { 7409b50d902SRodney W. Grimes printf("Commands may be abbreviated. Commands are:\n\n"); 7419b50d902SRodney W. Grimes for (c = cmdtab; c->name; c++) 7429b50d902SRodney W. Grimes printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help); 7439b50d902SRodney W. Grimes return; 7449b50d902SRodney W. Grimes } 7459b50d902SRodney W. Grimes while (--argc > 0) { 7469b50d902SRodney W. Grimes register char *arg; 7479b50d902SRodney W. Grimes arg = *++argv; 7489b50d902SRodney W. Grimes c = getcmd(arg); 7499b50d902SRodney W. Grimes if (c == (struct cmd *)-1) 7509b50d902SRodney W. Grimes printf("?Ambiguous help command %s\n", arg); 7519b50d902SRodney W. Grimes else if (c == (struct cmd *)0) 7529b50d902SRodney W. Grimes printf("?Invalid help command %s\n", arg); 7539b50d902SRodney W. Grimes else 7549b50d902SRodney W. Grimes printf("%s\n", c->help); 7559b50d902SRodney W. Grimes } 7569b50d902SRodney W. Grimes } 7579b50d902SRodney W. Grimes 7589b50d902SRodney W. Grimes void 7599b50d902SRodney W. Grimes settrace(argc, argv) 7609b50d902SRodney W. Grimes int argc; 7619b50d902SRodney W. Grimes char **argv; 7629b50d902SRodney W. Grimes { 7639b50d902SRodney W. Grimes trace = !trace; 7649b50d902SRodney W. Grimes printf("Packet tracing %s.\n", trace ? "on" : "off"); 7659b50d902SRodney W. Grimes } 7669b50d902SRodney W. Grimes 7679b50d902SRodney W. Grimes void 7689b50d902SRodney W. Grimes setverbose(argc, argv) 7699b50d902SRodney W. Grimes int argc; 7709b50d902SRodney W. Grimes char **argv; 7719b50d902SRodney W. Grimes { 7729b50d902SRodney W. Grimes verbose = !verbose; 7739b50d902SRodney W. Grimes printf("Verbose mode %s.\n", verbose ? "on" : "off"); 7749b50d902SRodney W. Grimes } 775