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> 659b50d902SRodney W. Grimes #include <netdb.h> 669b50d902SRodney W. Grimes #include <setjmp.h> 679b50d902SRodney W. Grimes #include <signal.h> 689b50d902SRodney W. Grimes #include <stdio.h> 699b50d902SRodney W. Grimes #include <stdlib.h> 709b50d902SRodney W. Grimes #include <string.h> 719b50d902SRodney W. Grimes #include <unistd.h> 729b50d902SRodney W. Grimes 73c33ed450SMatthew N. Dodd #include <histedit.h> 74c33ed450SMatthew N. Dodd 759b50d902SRodney W. Grimes #include "extern.h" 769b50d902SRodney W. Grimes 779b50d902SRodney W. Grimes #define TIMEOUT 5 /* secs between rexmt's */ 789b50d902SRodney W. Grimes 79c33ed450SMatthew N. Dodd #define MAXLINE 200 80c33ed450SMatthew N. Dodd 819b50d902SRodney W. Grimes struct sockaddr_in peeraddr; 829b50d902SRodney W. Grimes int f; 839b50d902SRodney W. Grimes short port; 849b50d902SRodney W. Grimes int trace; 859b50d902SRodney W. Grimes int verbose; 869b50d902SRodney W. Grimes int connected; 879b50d902SRodney W. Grimes char mode[32]; 88c33ed450SMatthew N. Dodd char line[MAXLINE]; 899b50d902SRodney W. Grimes int margc; 909b50d902SRodney W. Grimes char *margv[20]; 919b50d902SRodney W. Grimes jmp_buf toplevel; 929b50d902SRodney W. Grimes void intr(); 939b50d902SRodney W. Grimes struct servent *sp; 949b50d902SRodney W. Grimes 959b50d902SRodney W. Grimes void get __P((int, char **)); 969b50d902SRodney W. Grimes void help __P((int, char **)); 979b50d902SRodney W. Grimes void modecmd __P((int, char **)); 989b50d902SRodney W. Grimes void put __P((int, char **)); 999b50d902SRodney W. Grimes void quit __P((int, char **)); 1009b50d902SRodney W. Grimes void setascii __P((int, char **)); 1019b50d902SRodney W. Grimes void setbinary __P((int, char **)); 1029b50d902SRodney W. Grimes void setpeer __P((int, char **)); 1039b50d902SRodney W. Grimes void setrexmt __P((int, char **)); 1049b50d902SRodney W. Grimes void settimeout __P((int, char **)); 1059b50d902SRodney W. Grimes void settrace __P((int, char **)); 1069b50d902SRodney W. Grimes void setverbose __P((int, char **)); 1079b50d902SRodney W. Grimes void status __P((int, char **)); 1089b50d902SRodney W. Grimes 109eaa86f9dSBruce Evans static void command __P((void)) __dead2; 1109b50d902SRodney W. Grimes 1119b50d902SRodney W. Grimes static void getusage __P((char *)); 1129b50d902SRodney W. Grimes static void makeargv __P((void)); 1139b50d902SRodney W. Grimes static void putusage __P((char *)); 1149b50d902SRodney W. Grimes static void settftpmode __P((char *)); 1159b50d902SRodney W. Grimes 1169b50d902SRodney W. Grimes #define HELPINDENT (sizeof("connect")) 1179b50d902SRodney W. Grimes 1189b50d902SRodney W. Grimes struct cmd { 1199b50d902SRodney W. Grimes char *name; 1209b50d902SRodney W. Grimes char *help; 1219b50d902SRodney W. Grimes void (*handler) __P((int, char **)); 1229b50d902SRodney W. Grimes }; 1239b50d902SRodney W. Grimes 1249b50d902SRodney W. Grimes char vhelp[] = "toggle verbose mode"; 1259b50d902SRodney W. Grimes char thelp[] = "toggle packet tracing"; 1269b50d902SRodney W. Grimes char chelp[] = "connect to remote tftp"; 1279b50d902SRodney W. Grimes char qhelp[] = "exit tftp"; 1289b50d902SRodney W. Grimes char hhelp[] = "print help information"; 1299b50d902SRodney W. Grimes char shelp[] = "send file"; 1309b50d902SRodney W. Grimes char rhelp[] = "receive file"; 1319b50d902SRodney W. Grimes char mhelp[] = "set file transfer mode"; 1329b50d902SRodney W. Grimes char sthelp[] = "show current status"; 1339b50d902SRodney W. Grimes char xhelp[] = "set per-packet retransmission timeout"; 1349b50d902SRodney W. Grimes char ihelp[] = "set total retransmission timeout"; 1359b50d902SRodney W. Grimes char ashelp[] = "set mode to netascii"; 1369b50d902SRodney W. Grimes char bnhelp[] = "set mode to octet"; 1379b50d902SRodney W. Grimes 1389b50d902SRodney W. Grimes struct cmd cmdtab[] = { 1399b50d902SRodney W. Grimes { "connect", chelp, setpeer }, 1409b50d902SRodney W. Grimes { "mode", mhelp, modecmd }, 1419b50d902SRodney W. Grimes { "put", shelp, put }, 1429b50d902SRodney W. Grimes { "get", rhelp, get }, 1439b50d902SRodney W. Grimes { "quit", qhelp, quit }, 1449b50d902SRodney W. Grimes { "verbose", vhelp, setverbose }, 1459b50d902SRodney W. Grimes { "trace", thelp, settrace }, 1469b50d902SRodney W. Grimes { "status", sthelp, status }, 1479b50d902SRodney W. Grimes { "binary", bnhelp, setbinary }, 1489b50d902SRodney W. Grimes { "ascii", ashelp, setascii }, 1499b50d902SRodney W. Grimes { "rexmt", xhelp, setrexmt }, 1509b50d902SRodney W. Grimes { "timeout", ihelp, settimeout }, 1519b50d902SRodney W. Grimes { "?", hhelp, help }, 1529b50d902SRodney W. Grimes { 0 } 1539b50d902SRodney W. Grimes }; 1549b50d902SRodney W. Grimes 1559b50d902SRodney W. Grimes struct cmd *getcmd(); 1569b50d902SRodney W. Grimes char *tail(); 1579b50d902SRodney W. Grimes 1589b50d902SRodney W. Grimes int 1599b50d902SRodney W. Grimes main(argc, argv) 1609b50d902SRodney W. Grimes int argc; 1619b50d902SRodney W. Grimes char *argv[]; 1629b50d902SRodney W. Grimes { 1639b50d902SRodney W. Grimes struct sockaddr_in sin; 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes sp = getservbyname("tftp", "udp"); 166fd129a02SPhilippe Charnier if (sp == 0) 167fd129a02SPhilippe Charnier errx(1, "udp/tftp: unknown service"); 1689b50d902SRodney W. Grimes f = socket(AF_INET, SOCK_DGRAM, 0); 169fd129a02SPhilippe Charnier if (f < 0) 170fd129a02SPhilippe Charnier err(3, "socket"); 1719b50d902SRodney W. Grimes bzero((char *)&sin, sizeof(sin)); 1729b50d902SRodney W. Grimes sin.sin_family = AF_INET; 173fd129a02SPhilippe Charnier if (bind(f, (struct sockaddr *)&sin, sizeof(sin)) < 0) 174fd129a02SPhilippe Charnier err(1, "bind"); 1759b50d902SRodney W. Grimes strcpy(mode, "netascii"); 1769b50d902SRodney W. Grimes signal(SIGINT, intr); 1779b50d902SRodney W. Grimes if (argc > 1) { 1789b50d902SRodney W. Grimes if (setjmp(toplevel) != 0) 1799b50d902SRodney W. Grimes exit(0); 1809b50d902SRodney W. Grimes setpeer(argc, argv); 1819b50d902SRodney W. Grimes } 1829b50d902SRodney W. Grimes if (setjmp(toplevel) != 0) 1839b50d902SRodney W. Grimes (void)putchar('\n'); 1849b50d902SRodney W. Grimes command(); 1859b50d902SRodney W. Grimes } 1869b50d902SRodney W. Grimes 187fd129a02SPhilippe Charnier char hostname[MAXHOSTNAMELEN]; 1889b50d902SRodney W. Grimes 1899b50d902SRodney W. Grimes void 1909b50d902SRodney W. Grimes setpeer(argc, argv) 1919b50d902SRodney W. Grimes int argc; 1929b50d902SRodney W. Grimes char *argv[]; 1939b50d902SRodney W. Grimes { 1949b50d902SRodney W. Grimes struct hostent *host; 1959b50d902SRodney W. Grimes 1969b50d902SRodney W. Grimes if (argc < 2) { 1979b50d902SRodney W. Grimes strcpy(line, "Connect "); 1989b50d902SRodney W. Grimes printf("(to) "); 199ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 2009b50d902SRodney W. Grimes makeargv(); 2019b50d902SRodney W. Grimes argc = margc; 2029b50d902SRodney W. Grimes argv = margv; 2039b50d902SRodney W. Grimes } 2049b50d902SRodney W. Grimes if (argc > 3) { 2059b50d902SRodney W. Grimes printf("usage: %s host-name [port]\n", argv[0]); 2069b50d902SRodney W. Grimes return; 2079b50d902SRodney W. Grimes } 2089b50d902SRodney W. Grimes host = gethostbyname(argv[1]); 2099b50d902SRodney W. Grimes if (host) { 2109b50d902SRodney W. Grimes peeraddr.sin_family = host->h_addrtype; 211a716ad66SWarner Losh bcopy(host->h_addr, &peeraddr.sin_addr, 212a716ad66SWarner Losh MIN(sizeof(peeraddr.sin_addr), host->h_length)); 213a716ad66SWarner Losh strncpy(hostname, host->h_name, sizeof(hostname)); 2149b50d902SRodney W. Grimes } else { 2159b50d902SRodney W. Grimes peeraddr.sin_family = AF_INET; 2169b50d902SRodney W. Grimes peeraddr.sin_addr.s_addr = inet_addr(argv[1]); 2179b50d902SRodney W. Grimes if (peeraddr.sin_addr.s_addr == -1) { 2189b50d902SRodney W. Grimes connected = 0; 2199b50d902SRodney W. Grimes printf("%s: unknown host\n", argv[1]); 2209b50d902SRodney W. Grimes return; 2219b50d902SRodney W. Grimes } 222a716ad66SWarner Losh strncpy(hostname, argv[1], sizeof(hostname)); 2239b50d902SRodney W. Grimes } 224a716ad66SWarner Losh hostname[sizeof(hostname) - 1] = '\0'; 2259b50d902SRodney W. Grimes port = sp->s_port; 2269b50d902SRodney W. Grimes if (argc == 3) { 2279b50d902SRodney W. Grimes port = atoi(argv[2]); 2289b50d902SRodney W. Grimes if (port < 0) { 2299b50d902SRodney W. Grimes printf("%s: bad port number\n", argv[2]); 2309b50d902SRodney W. Grimes connected = 0; 2319b50d902SRodney W. Grimes return; 2329b50d902SRodney W. Grimes } 2339b50d902SRodney W. Grimes port = htons(port); 2349b50d902SRodney W. Grimes } 2359b50d902SRodney W. Grimes connected = 1; 2369b50d902SRodney W. Grimes } 2379b50d902SRodney W. Grimes 2389b50d902SRodney W. Grimes struct modes { 2399b50d902SRodney W. Grimes char *m_name; 2409b50d902SRodney W. Grimes char *m_mode; 2419b50d902SRodney W. Grimes } modes[] = { 2429b50d902SRodney W. Grimes { "ascii", "netascii" }, 2439b50d902SRodney W. Grimes { "netascii", "netascii" }, 2449b50d902SRodney W. Grimes { "binary", "octet" }, 2459b50d902SRodney W. Grimes { "image", "octet" }, 2469b50d902SRodney W. Grimes { "octet", "octet" }, 2479b50d902SRodney W. Grimes /* { "mail", "mail" }, */ 2489b50d902SRodney W. Grimes { 0, 0 } 2499b50d902SRodney W. Grimes }; 2509b50d902SRodney W. Grimes 2519b50d902SRodney W. Grimes void 2529b50d902SRodney W. Grimes modecmd(argc, argv) 2539b50d902SRodney W. Grimes int argc; 2549b50d902SRodney W. Grimes char *argv[]; 2559b50d902SRodney W. Grimes { 2569b50d902SRodney W. Grimes register struct modes *p; 2579b50d902SRodney W. Grimes char *sep; 2589b50d902SRodney W. Grimes 2599b50d902SRodney W. Grimes if (argc < 2) { 2609b50d902SRodney W. Grimes printf("Using %s mode to transfer files.\n", mode); 2619b50d902SRodney W. Grimes return; 2629b50d902SRodney W. Grimes } 2639b50d902SRodney W. Grimes if (argc == 2) { 2649b50d902SRodney W. Grimes for (p = modes; p->m_name; p++) 2659b50d902SRodney W. Grimes if (strcmp(argv[1], p->m_name) == 0) 2669b50d902SRodney W. Grimes break; 2679b50d902SRodney W. Grimes if (p->m_name) { 2689b50d902SRodney W. Grimes settftpmode(p->m_mode); 2699b50d902SRodney W. Grimes return; 2709b50d902SRodney W. Grimes } 2719b50d902SRodney W. Grimes printf("%s: unknown mode\n", argv[1]); 2729b50d902SRodney W. Grimes /* drop through and print usage message */ 2739b50d902SRodney W. Grimes } 2749b50d902SRodney W. Grimes 2759b50d902SRodney W. Grimes printf("usage: %s [", argv[0]); 2769b50d902SRodney W. Grimes sep = " "; 2779b50d902SRodney W. Grimes for (p = modes; p->m_name; p++) { 2789b50d902SRodney W. Grimes printf("%s%s", sep, p->m_name); 2799b50d902SRodney W. Grimes if (*sep == ' ') 2809b50d902SRodney W. Grimes sep = " | "; 2819b50d902SRodney W. Grimes } 2829b50d902SRodney W. Grimes printf(" ]\n"); 2839b50d902SRodney W. Grimes return; 2849b50d902SRodney W. Grimes } 2859b50d902SRodney W. Grimes 2869b50d902SRodney W. Grimes void 2879b50d902SRodney W. Grimes setbinary(argc, argv) 2889b50d902SRodney W. Grimes int argc; 2899b50d902SRodney W. Grimes char *argv[]; 2909b50d902SRodney W. Grimes { 2919b50d902SRodney W. Grimes 2929b50d902SRodney W. Grimes settftpmode("octet"); 2939b50d902SRodney W. Grimes } 2949b50d902SRodney W. Grimes 2959b50d902SRodney W. Grimes void 2969b50d902SRodney W. Grimes setascii(argc, argv) 2979b50d902SRodney W. Grimes int argc; 2989b50d902SRodney W. Grimes char *argv[]; 2999b50d902SRodney W. Grimes { 3009b50d902SRodney W. Grimes 3019b50d902SRodney W. Grimes settftpmode("netascii"); 3029b50d902SRodney W. Grimes } 3039b50d902SRodney W. Grimes 3049b50d902SRodney W. Grimes static void 3059b50d902SRodney W. Grimes settftpmode(newmode) 3069b50d902SRodney W. Grimes char *newmode; 3079b50d902SRodney W. Grimes { 3089b50d902SRodney W. Grimes strcpy(mode, newmode); 3099b50d902SRodney W. Grimes if (verbose) 3109b50d902SRodney W. Grimes printf("mode set to %s\n", mode); 3119b50d902SRodney W. Grimes } 3129b50d902SRodney W. Grimes 3139b50d902SRodney W. Grimes 3149b50d902SRodney W. Grimes /* 3159b50d902SRodney W. Grimes * Send file(s). 3169b50d902SRodney W. Grimes */ 3179b50d902SRodney W. Grimes void 3189b50d902SRodney W. Grimes put(argc, argv) 3199b50d902SRodney W. Grimes int argc; 3209b50d902SRodney W. Grimes char *argv[]; 3219b50d902SRodney W. Grimes { 3229b50d902SRodney W. Grimes int fd; 3239b50d902SRodney W. Grimes register int n; 3249b50d902SRodney W. Grimes register char *cp, *targ; 3259b50d902SRodney W. Grimes 3269b50d902SRodney W. Grimes if (argc < 2) { 3279b50d902SRodney W. Grimes strcpy(line, "send "); 3289b50d902SRodney W. Grimes printf("(file) "); 329ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 3309b50d902SRodney W. Grimes makeargv(); 3319b50d902SRodney W. Grimes argc = margc; 3329b50d902SRodney W. Grimes argv = margv; 3339b50d902SRodney W. Grimes } 3349b50d902SRodney W. Grimes if (argc < 2) { 3359b50d902SRodney W. Grimes putusage(argv[0]); 3369b50d902SRodney W. Grimes return; 3379b50d902SRodney W. Grimes } 3389b50d902SRodney W. Grimes targ = argv[argc - 1]; 3399b50d902SRodney W. Grimes if (index(argv[argc - 1], ':')) { 3409b50d902SRodney W. Grimes char *cp; 3419b50d902SRodney W. Grimes struct hostent *hp; 3429b50d902SRodney W. Grimes 3439b50d902SRodney W. Grimes for (n = 1; n < argc - 1; n++) 3449b50d902SRodney W. Grimes if (index(argv[n], ':')) { 3459b50d902SRodney W. Grimes putusage(argv[0]); 3469b50d902SRodney W. Grimes return; 3479b50d902SRodney W. Grimes } 3489b50d902SRodney W. Grimes cp = argv[argc - 1]; 3499b50d902SRodney W. Grimes targ = index(cp, ':'); 3509b50d902SRodney W. Grimes *targ++ = 0; 3519b50d902SRodney W. Grimes hp = gethostbyname(cp); 3529b50d902SRodney W. Grimes if (hp == NULL) { 3539b50d902SRodney W. Grimes fprintf(stderr, "tftp: %s: ", cp); 3549b50d902SRodney W. Grimes herror((char *)NULL); 3559b50d902SRodney W. Grimes return; 3569b50d902SRodney W. Grimes } 357a716ad66SWarner Losh bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, 358a716ad66SWarner Losh MIN(sizeof(peeraddr.sin_addr), hp->h_length)); 3599b50d902SRodney W. Grimes peeraddr.sin_family = hp->h_addrtype; 3609b50d902SRodney W. Grimes connected = 1; 361a716ad66SWarner Losh strncpy(hostname, hp->h_name, sizeof(hostname)); 362a716ad66SWarner Losh hostname[sizeof(hostname) - 1] = '\0'; 3639b50d902SRodney W. Grimes } 3649b50d902SRodney W. Grimes if (!connected) { 3659b50d902SRodney W. Grimes printf("No target machine specified.\n"); 3669b50d902SRodney W. Grimes return; 3679b50d902SRodney W. Grimes } 3689b50d902SRodney W. Grimes if (argc < 4) { 3699b50d902SRodney W. Grimes cp = argc == 2 ? tail(targ) : argv[1]; 3709b50d902SRodney W. Grimes fd = open(cp, O_RDONLY); 3719b50d902SRodney W. Grimes if (fd < 0) { 372fd129a02SPhilippe Charnier warn("%s", cp); 3739b50d902SRodney W. Grimes return; 3749b50d902SRodney W. Grimes } 3759b50d902SRodney W. Grimes if (verbose) 3769b50d902SRodney W. Grimes printf("putting %s to %s:%s [%s]\n", 3779b50d902SRodney W. Grimes cp, hostname, targ, mode); 3789b50d902SRodney W. Grimes peeraddr.sin_port = port; 3798692ad46SDavid Greenman xmitfile(fd, targ, mode); 3809b50d902SRodney W. Grimes return; 3819b50d902SRodney W. Grimes } 3829b50d902SRodney W. Grimes /* this assumes the target is a directory */ 3839b50d902SRodney W. Grimes /* on a remote unix system. hmmmm. */ 3849b50d902SRodney W. Grimes cp = index(targ, '\0'); 3859b50d902SRodney W. Grimes *cp++ = '/'; 3869b50d902SRodney W. Grimes for (n = 1; n < argc - 1; n++) { 3879b50d902SRodney W. Grimes strcpy(cp, tail(argv[n])); 3889b50d902SRodney W. Grimes fd = open(argv[n], O_RDONLY); 3899b50d902SRodney W. Grimes if (fd < 0) { 390fd129a02SPhilippe Charnier warn("%s", argv[n]); 3919b50d902SRodney W. Grimes continue; 3929b50d902SRodney W. Grimes } 3939b50d902SRodney W. Grimes if (verbose) 3949b50d902SRodney W. Grimes printf("putting %s to %s:%s [%s]\n", 3959b50d902SRodney W. Grimes argv[n], hostname, targ, mode); 3969b50d902SRodney W. Grimes peeraddr.sin_port = port; 3978692ad46SDavid Greenman xmitfile(fd, targ, mode); 3989b50d902SRodney W. Grimes } 3999b50d902SRodney W. Grimes } 4009b50d902SRodney W. Grimes 4019b50d902SRodney W. Grimes static void 4029b50d902SRodney W. Grimes putusage(s) 4039b50d902SRodney W. Grimes char *s; 4049b50d902SRodney W. Grimes { 4059b50d902SRodney W. Grimes printf("usage: %s file ... host:target, or\n", s); 4069b50d902SRodney W. Grimes printf(" %s file ... target (when already connected)\n", s); 4079b50d902SRodney W. Grimes } 4089b50d902SRodney W. Grimes 4099b50d902SRodney W. Grimes /* 4109b50d902SRodney W. Grimes * Receive file(s). 4119b50d902SRodney W. Grimes */ 4129b50d902SRodney W. Grimes void 4139b50d902SRodney W. Grimes get(argc, argv) 4149b50d902SRodney W. Grimes int argc; 4159b50d902SRodney W. Grimes char *argv[]; 4169b50d902SRodney W. Grimes { 4179b50d902SRodney W. Grimes int fd; 4189b50d902SRodney W. Grimes register int n; 4199b50d902SRodney W. Grimes register char *cp; 4209b50d902SRodney W. Grimes char *src; 4219b50d902SRodney W. Grimes 4229b50d902SRodney W. Grimes if (argc < 2) { 4239b50d902SRodney W. Grimes strcpy(line, "get "); 4249b50d902SRodney W. Grimes printf("(files) "); 425ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 4269b50d902SRodney W. Grimes makeargv(); 4279b50d902SRodney W. Grimes argc = margc; 4289b50d902SRodney W. Grimes argv = margv; 4299b50d902SRodney W. Grimes } 4309b50d902SRodney W. Grimes if (argc < 2) { 4319b50d902SRodney W. Grimes getusage(argv[0]); 4329b50d902SRodney W. Grimes return; 4339b50d902SRodney W. Grimes } 4349b50d902SRodney W. Grimes if (!connected) { 4359b50d902SRodney W. Grimes for (n = 1; n < argc ; n++) 4369b50d902SRodney W. Grimes if (index(argv[n], ':') == 0) { 4379b50d902SRodney W. Grimes getusage(argv[0]); 4389b50d902SRodney W. Grimes return; 4399b50d902SRodney W. Grimes } 4409b50d902SRodney W. Grimes } 4419b50d902SRodney W. Grimes for (n = 1; n < argc ; n++) { 4429b50d902SRodney W. Grimes src = index(argv[n], ':'); 4439b50d902SRodney W. Grimes if (src == NULL) 4449b50d902SRodney W. Grimes src = argv[n]; 4459b50d902SRodney W. Grimes else { 4469b50d902SRodney W. Grimes struct hostent *hp; 4479b50d902SRodney W. Grimes 4489b50d902SRodney W. Grimes *src++ = 0; 4499b50d902SRodney W. Grimes hp = gethostbyname(argv[n]); 4509b50d902SRodney W. Grimes if (hp == NULL) { 4519b50d902SRodney W. Grimes fprintf(stderr, "tftp: %s: ", argv[n]); 4529b50d902SRodney W. Grimes herror((char *)NULL); 4539b50d902SRodney W. Grimes continue; 4549b50d902SRodney W. Grimes } 4559b50d902SRodney W. Grimes bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, 456a716ad66SWarner Losh MIN(sizeof(peeraddr.sin_addr), hp->h_length)); 4579b50d902SRodney W. Grimes peeraddr.sin_family = hp->h_addrtype; 4589b50d902SRodney W. Grimes connected = 1; 459a716ad66SWarner Losh strncpy(hostname, hp->h_name, sizeof(hostname)); 460a716ad66SWarner Losh hostname[sizeof(hostname) - 1] = '\0'; 4619b50d902SRodney W. Grimes } 4629b50d902SRodney W. Grimes if (argc < 4) { 4639b50d902SRodney W. Grimes cp = argc == 3 ? argv[2] : tail(src); 4649b50d902SRodney W. Grimes fd = creat(cp, 0644); 4659b50d902SRodney W. Grimes if (fd < 0) { 466fd129a02SPhilippe Charnier warn("%s", cp); 4679b50d902SRodney W. Grimes return; 4689b50d902SRodney W. Grimes } 4699b50d902SRodney W. Grimes if (verbose) 4709b50d902SRodney W. Grimes printf("getting from %s:%s to %s [%s]\n", 4719b50d902SRodney W. Grimes hostname, src, cp, mode); 4729b50d902SRodney W. Grimes peeraddr.sin_port = port; 4739b50d902SRodney W. Grimes recvfile(fd, src, mode); 4749b50d902SRodney W. Grimes break; 4759b50d902SRodney W. Grimes } 4769b50d902SRodney W. Grimes cp = tail(src); /* new .. jdg */ 4779b50d902SRodney W. Grimes fd = creat(cp, 0644); 4789b50d902SRodney W. Grimes if (fd < 0) { 479fd129a02SPhilippe Charnier warn("%s", cp); 4809b50d902SRodney W. Grimes continue; 4819b50d902SRodney W. Grimes } 4829b50d902SRodney W. Grimes if (verbose) 4839b50d902SRodney W. Grimes printf("getting from %s:%s to %s [%s]\n", 4849b50d902SRodney W. Grimes hostname, src, cp, mode); 4859b50d902SRodney W. Grimes peeraddr.sin_port = port; 4869b50d902SRodney W. Grimes recvfile(fd, src, mode); 4879b50d902SRodney W. Grimes } 4889b50d902SRodney W. Grimes } 4899b50d902SRodney W. Grimes 4909b50d902SRodney W. Grimes static void 4919b50d902SRodney W. Grimes getusage(s) 4929b50d902SRodney W. Grimes char *s; 4939b50d902SRodney W. Grimes { 4949b50d902SRodney W. Grimes printf("usage: %s host:file host:file ... file, or\n", s); 4959b50d902SRodney W. Grimes printf(" %s file file ... file if connected\n", s); 4969b50d902SRodney W. Grimes } 4979b50d902SRodney W. Grimes 4989b50d902SRodney W. Grimes int rexmtval = TIMEOUT; 4999b50d902SRodney W. Grimes 5009b50d902SRodney W. Grimes void 5019b50d902SRodney W. Grimes setrexmt(argc, argv) 5029b50d902SRodney W. Grimes int argc; 5039b50d902SRodney W. Grimes char *argv[]; 5049b50d902SRodney W. Grimes { 5059b50d902SRodney W. Grimes int t; 5069b50d902SRodney W. Grimes 5079b50d902SRodney W. Grimes if (argc < 2) { 5089b50d902SRodney W. Grimes strcpy(line, "Rexmt-timeout "); 5099b50d902SRodney W. Grimes printf("(value) "); 510ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 5119b50d902SRodney W. Grimes makeargv(); 5129b50d902SRodney W. Grimes argc = margc; 5139b50d902SRodney W. Grimes argv = margv; 5149b50d902SRodney W. Grimes } 5159b50d902SRodney W. Grimes if (argc != 2) { 5169b50d902SRodney W. Grimes printf("usage: %s value\n", argv[0]); 5179b50d902SRodney W. Grimes return; 5189b50d902SRodney W. Grimes } 5199b50d902SRodney W. Grimes t = atoi(argv[1]); 5209b50d902SRodney W. Grimes if (t < 0) 5219b50d902SRodney W. Grimes printf("%s: bad value\n", argv[1]); 5229b50d902SRodney W. Grimes else 5239b50d902SRodney W. Grimes rexmtval = t; 5249b50d902SRodney W. Grimes } 5259b50d902SRodney W. Grimes 5269b50d902SRodney W. Grimes int maxtimeout = 5 * TIMEOUT; 5279b50d902SRodney W. Grimes 5289b50d902SRodney W. Grimes void 5299b50d902SRodney W. Grimes settimeout(argc, argv) 5309b50d902SRodney W. Grimes int argc; 5319b50d902SRodney W. Grimes char *argv[]; 5329b50d902SRodney W. Grimes { 5339b50d902SRodney W. Grimes int t; 5349b50d902SRodney W. Grimes 5359b50d902SRodney W. Grimes if (argc < 2) { 5369b50d902SRodney W. Grimes strcpy(line, "Maximum-timeout "); 5379b50d902SRodney W. Grimes printf("(value) "); 538ca22ff9eSJoerg Wunsch fgets(&line[strlen(line)], sizeof line - strlen(line), stdin); 5399b50d902SRodney W. Grimes makeargv(); 5409b50d902SRodney W. Grimes argc = margc; 5419b50d902SRodney W. Grimes argv = margv; 5429b50d902SRodney W. Grimes } 5439b50d902SRodney W. Grimes if (argc != 2) { 5449b50d902SRodney W. Grimes printf("usage: %s value\n", argv[0]); 5459b50d902SRodney W. Grimes return; 5469b50d902SRodney W. Grimes } 5479b50d902SRodney W. Grimes t = atoi(argv[1]); 5489b50d902SRodney W. Grimes if (t < 0) 5499b50d902SRodney W. Grimes printf("%s: bad value\n", argv[1]); 5509b50d902SRodney W. Grimes else 5519b50d902SRodney W. Grimes maxtimeout = t; 5529b50d902SRodney W. Grimes } 5539b50d902SRodney W. Grimes 5549b50d902SRodney W. Grimes void 5559b50d902SRodney W. Grimes status(argc, argv) 5569b50d902SRodney W. Grimes int argc; 5579b50d902SRodney W. Grimes char *argv[]; 5589b50d902SRodney W. Grimes { 5599b50d902SRodney W. Grimes if (connected) 5609b50d902SRodney W. Grimes printf("Connected to %s.\n", hostname); 5619b50d902SRodney W. Grimes else 5629b50d902SRodney W. Grimes printf("Not connected.\n"); 5639b50d902SRodney W. Grimes printf("Mode: %s Verbose: %s Tracing: %s\n", mode, 5649b50d902SRodney W. Grimes verbose ? "on" : "off", trace ? "on" : "off"); 5659b50d902SRodney W. Grimes printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n", 5669b50d902SRodney W. Grimes rexmtval, maxtimeout); 5679b50d902SRodney W. Grimes } 5689b50d902SRodney W. Grimes 5699b50d902SRodney W. Grimes void 5709b50d902SRodney W. Grimes intr() 5719b50d902SRodney W. Grimes { 5729b50d902SRodney W. Grimes 5739b50d902SRodney W. Grimes signal(SIGALRM, SIG_IGN); 5749b50d902SRodney W. Grimes alarm(0); 5759b50d902SRodney W. Grimes longjmp(toplevel, -1); 5769b50d902SRodney W. Grimes } 5779b50d902SRodney W. Grimes 5789b50d902SRodney W. Grimes char * 5799b50d902SRodney W. Grimes tail(filename) 5809b50d902SRodney W. Grimes char *filename; 5819b50d902SRodney W. Grimes { 5829b50d902SRodney W. Grimes register char *s; 5839b50d902SRodney W. Grimes 5849b50d902SRodney W. Grimes while (*filename) { 5859b50d902SRodney W. Grimes s = rindex(filename, '/'); 5869b50d902SRodney W. Grimes if (s == NULL) 5879b50d902SRodney W. Grimes break; 5889b50d902SRodney W. Grimes if (s[1]) 5899b50d902SRodney W. Grimes return (s + 1); 5909b50d902SRodney W. Grimes *s = '\0'; 5919b50d902SRodney W. Grimes } 5929b50d902SRodney W. Grimes return (filename); 5939b50d902SRodney W. Grimes } 5949b50d902SRodney W. Grimes 595c33ed450SMatthew N. Dodd static const char * 596c33ed450SMatthew N. Dodd command_prompt() { 597c33ed450SMatthew N. Dodd return ("tftp> "); 598c33ed450SMatthew N. Dodd } 599c33ed450SMatthew N. Dodd 6009b50d902SRodney W. Grimes /* 6019b50d902SRodney W. Grimes * Command parser. 6029b50d902SRodney W. Grimes */ 603eaa86f9dSBruce Evans static void 6049b50d902SRodney W. Grimes command() 6059b50d902SRodney W. Grimes { 6069b50d902SRodney W. Grimes register struct cmd *c; 607ca22ff9eSJoerg Wunsch char *cp; 608c33ed450SMatthew N. Dodd static EditLine *el = NULL; 609c33ed450SMatthew N. Dodd static History *hist = NULL; 610c33ed450SMatthew N. Dodd HistEvent he; 611c33ed450SMatthew N. Dodd const char * bp; 612c33ed450SMatthew N. Dodd int len, num; 613c33ed450SMatthew N. Dodd int verbose; 614c33ed450SMatthew N. Dodd 615c33ed450SMatthew N. Dodd verbose = isatty(0); 616c33ed450SMatthew N. Dodd 617c33ed450SMatthew N. Dodd if (verbose) { 618c33ed450SMatthew N. Dodd el = el_init("tftp", stdin, stdout, stderr); 619c33ed450SMatthew N. Dodd hist = history_init(); 620c33ed450SMatthew N. Dodd history(hist, &he, H_EVENT, 100); 621c33ed450SMatthew N. Dodd el_set(el, EL_HIST, history, hist); 622c33ed450SMatthew N. Dodd el_set(el, EL_EDITOR, "emacs"); 623c33ed450SMatthew N. Dodd el_set(el, EL_PROMPT, command_prompt); 624c33ed450SMatthew N. Dodd el_set(el, EL_SIGNAL, 1); 625c33ed450SMatthew N. Dodd el_source(el, NULL); 626c33ed450SMatthew N. Dodd } 6279b50d902SRodney W. Grimes 6289b50d902SRodney W. Grimes for (;;) { 629c33ed450SMatthew N. Dodd if (verbose) { 630c33ed450SMatthew N. Dodd if ((bp = el_gets(el, &num)) == NULL || num == 0) 631c33ed450SMatthew N. Dodd exit(0); 632c33ed450SMatthew N. Dodd 633c33ed450SMatthew N. Dodd len = (num > MAXLINE) ? MAXLINE : num; 634c33ed450SMatthew N. Dodd memcpy(line, bp, len); 635c33ed450SMatthew N. Dodd line[len] = '\0'; 636c33ed450SMatthew N. Dodd history(hist, &he, H_ENTER, bp); 637c33ed450SMatthew N. Dodd } else { 638ca22ff9eSJoerg Wunsch if (fgets(line, sizeof line , stdin) == 0) { 6399b50d902SRodney W. Grimes if (feof(stdin)) { 6409b50d902SRodney W. Grimes exit(0); 6419b50d902SRodney W. Grimes } else { 6429b50d902SRodney W. Grimes continue; 6439b50d902SRodney W. Grimes } 6449b50d902SRodney W. Grimes } 645c33ed450SMatthew N. Dodd } 646ca22ff9eSJoerg Wunsch if ((cp = strchr(line, '\n'))) 647ca22ff9eSJoerg Wunsch *cp = '\0'; 6489b50d902SRodney W. Grimes if (line[0] == 0) 6499b50d902SRodney W. Grimes continue; 6509b50d902SRodney W. Grimes makeargv(); 6519b50d902SRodney W. Grimes if (margc == 0) 6529b50d902SRodney W. Grimes continue; 6539b50d902SRodney W. Grimes c = getcmd(margv[0]); 6549b50d902SRodney W. Grimes if (c == (struct cmd *)-1) { 6559b50d902SRodney W. Grimes printf("?Ambiguous command\n"); 6569b50d902SRodney W. Grimes continue; 6579b50d902SRodney W. Grimes } 6589b50d902SRodney W. Grimes if (c == 0) { 6599b50d902SRodney W. Grimes printf("?Invalid command\n"); 6609b50d902SRodney W. Grimes continue; 6619b50d902SRodney W. Grimes } 6629b50d902SRodney W. Grimes (*c->handler)(margc, margv); 6639b50d902SRodney W. Grimes } 6649b50d902SRodney W. Grimes } 6659b50d902SRodney W. Grimes 6669b50d902SRodney W. Grimes struct cmd * 6679b50d902SRodney W. Grimes getcmd(name) 6689b50d902SRodney W. Grimes register char *name; 6699b50d902SRodney W. Grimes { 6709b50d902SRodney W. Grimes register char *p, *q; 6719b50d902SRodney W. Grimes register struct cmd *c, *found; 6729b50d902SRodney W. Grimes register int nmatches, longest; 6739b50d902SRodney W. Grimes 6749b50d902SRodney W. Grimes longest = 0; 6759b50d902SRodney W. Grimes nmatches = 0; 6769b50d902SRodney W. Grimes found = 0; 6779b50d902SRodney W. Grimes for (c = cmdtab; (p = c->name) != NULL; c++) { 6789b50d902SRodney W. Grimes for (q = name; *q == *p++; q++) 6799b50d902SRodney W. Grimes if (*q == 0) /* exact match? */ 6809b50d902SRodney W. Grimes return (c); 6819b50d902SRodney W. Grimes if (!*q) { /* the name was a prefix */ 6829b50d902SRodney W. Grimes if (q - name > longest) { 6839b50d902SRodney W. Grimes longest = q - name; 6849b50d902SRodney W. Grimes nmatches = 1; 6859b50d902SRodney W. Grimes found = c; 6869b50d902SRodney W. Grimes } else if (q - name == longest) 6879b50d902SRodney W. Grimes nmatches++; 6889b50d902SRodney W. Grimes } 6899b50d902SRodney W. Grimes } 6909b50d902SRodney W. Grimes if (nmatches > 1) 6919b50d902SRodney W. Grimes return ((struct cmd *)-1); 6929b50d902SRodney W. Grimes return (found); 6939b50d902SRodney W. Grimes } 6949b50d902SRodney W. Grimes 6959b50d902SRodney W. Grimes /* 6969b50d902SRodney W. Grimes * Slice a string up into argc/argv. 6979b50d902SRodney W. Grimes */ 6989b50d902SRodney W. Grimes static void 6999b50d902SRodney W. Grimes makeargv() 7009b50d902SRodney W. Grimes { 7019b50d902SRodney W. Grimes register char *cp; 7029b50d902SRodney W. Grimes register char **argp = margv; 7039b50d902SRodney W. Grimes 7049b50d902SRodney W. Grimes margc = 0; 705ca22ff9eSJoerg Wunsch if ((cp = strchr(line, '\n'))) 706ca22ff9eSJoerg Wunsch *cp = '\0'; 7079b50d902SRodney W. Grimes for (cp = line; *cp;) { 7089b50d902SRodney W. Grimes while (isspace(*cp)) 7099b50d902SRodney W. Grimes cp++; 7109b50d902SRodney W. Grimes if (*cp == '\0') 7119b50d902SRodney W. Grimes break; 7129b50d902SRodney W. Grimes *argp++ = cp; 7139b50d902SRodney W. Grimes margc += 1; 7149b50d902SRodney W. Grimes while (*cp != '\0' && !isspace(*cp)) 7159b50d902SRodney W. Grimes cp++; 7169b50d902SRodney W. Grimes if (*cp == '\0') 7179b50d902SRodney W. Grimes break; 7189b50d902SRodney W. Grimes *cp++ = '\0'; 7199b50d902SRodney W. Grimes } 7209b50d902SRodney W. Grimes *argp++ = 0; 7219b50d902SRodney W. Grimes } 7229b50d902SRodney W. Grimes 7239b50d902SRodney W. Grimes void 7249b50d902SRodney W. Grimes quit(argc, argv) 7259b50d902SRodney W. Grimes int argc; 7269b50d902SRodney W. Grimes char *argv[]; 7279b50d902SRodney W. Grimes { 7289b50d902SRodney W. Grimes 7299b50d902SRodney W. Grimes exit(0); 7309b50d902SRodney W. Grimes } 7319b50d902SRodney W. Grimes 7329b50d902SRodney W. Grimes /* 7339b50d902SRodney W. Grimes * Help command. 7349b50d902SRodney W. Grimes */ 7359b50d902SRodney W. Grimes void 7369b50d902SRodney W. Grimes help(argc, argv) 7379b50d902SRodney W. Grimes int argc; 7389b50d902SRodney W. Grimes char *argv[]; 7399b50d902SRodney W. Grimes { 7409b50d902SRodney W. Grimes register struct cmd *c; 7419b50d902SRodney W. Grimes 7429b50d902SRodney W. Grimes if (argc == 1) { 7439b50d902SRodney W. Grimes printf("Commands may be abbreviated. Commands are:\n\n"); 7449b50d902SRodney W. Grimes for (c = cmdtab; c->name; c++) 7459b50d902SRodney W. Grimes printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help); 7469b50d902SRodney W. Grimes return; 7479b50d902SRodney W. Grimes } 7489b50d902SRodney W. Grimes while (--argc > 0) { 7499b50d902SRodney W. Grimes register char *arg; 7509b50d902SRodney W. Grimes arg = *++argv; 7519b50d902SRodney W. Grimes c = getcmd(arg); 7529b50d902SRodney W. Grimes if (c == (struct cmd *)-1) 7539b50d902SRodney W. Grimes printf("?Ambiguous help command %s\n", arg); 7549b50d902SRodney W. Grimes else if (c == (struct cmd *)0) 7559b50d902SRodney W. Grimes printf("?Invalid help command %s\n", arg); 7569b50d902SRodney W. Grimes else 7579b50d902SRodney W. Grimes printf("%s\n", c->help); 7589b50d902SRodney W. Grimes } 7599b50d902SRodney W. Grimes } 7609b50d902SRodney W. Grimes 7619b50d902SRodney W. Grimes void 7629b50d902SRodney W. Grimes settrace(argc, argv) 7639b50d902SRodney W. Grimes int argc; 7649b50d902SRodney W. Grimes char **argv; 7659b50d902SRodney W. Grimes { 7669b50d902SRodney W. Grimes trace = !trace; 7679b50d902SRodney W. Grimes printf("Packet tracing %s.\n", trace ? "on" : "off"); 7689b50d902SRodney W. Grimes } 7699b50d902SRodney W. Grimes 7709b50d902SRodney W. Grimes void 7719b50d902SRodney W. Grimes setverbose(argc, argv) 7729b50d902SRodney W. Grimes int argc; 7739b50d902SRodney W. Grimes char **argv; 7749b50d902SRodney W. Grimes { 7759b50d902SRodney W. Grimes verbose = !verbose; 7769b50d902SRodney W. Grimes printf("Verbose mode %s.\n", verbose ? "on" : "off"); 7779b50d902SRodney W. Grimes } 778