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"; 388049f797SMark Murray #endif 399b50d902SRodney W. Grimes 408f4c8256SDavid Malone #if 0 419b50d902SRodney W. Grimes #ifndef lint 428f4c8256SDavid Malone static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; 43fd129a02SPhilippe Charnier #endif 448f4c8256SDavid Malone #endif 458f4c8256SDavid Malone 468f4c8256SDavid Malone #include <sys/cdefs.h> 478f4c8256SDavid Malone __FBSDID("$FreeBSD$"); 489b50d902SRodney W. Grimes 499b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes /* 529b50d902SRodney W. Grimes * TFTP User Program -- Command Interface. 539b50d902SRodney W. Grimes */ 54fd129a02SPhilippe Charnier #include <sys/param.h> 559b50d902SRodney W. Grimes #include <sys/types.h> 569b50d902SRodney W. Grimes #include <sys/socket.h> 579b50d902SRodney W. Grimes #include <sys/file.h> 58a716ad66SWarner Losh #include <sys/param.h> 599b50d902SRodney W. Grimes 609b50d902SRodney W. Grimes #include <netinet/in.h> 619b50d902SRodney W. Grimes 629b50d902SRodney W. Grimes #include <arpa/inet.h> 639b50d902SRodney W. Grimes 649b50d902SRodney W. Grimes #include <ctype.h> 65fd129a02SPhilippe Charnier #include <err.h> 668979160aSBruce Evans #include <histedit.h> 679b50d902SRodney W. Grimes #include <netdb.h> 689b50d902SRodney W. Grimes #include <setjmp.h> 699b50d902SRodney W. Grimes #include <signal.h> 709b50d902SRodney W. Grimes #include <stdio.h> 719b50d902SRodney W. Grimes #include <stdlib.h> 729b50d902SRodney W. Grimes #include <string.h> 739b50d902SRodney W. Grimes #include <unistd.h> 749b50d902SRodney W. Grimes 759b50d902SRodney W. Grimes #include "extern.h" 769b50d902SRodney W. Grimes 77c33ed450SMatthew N. Dodd #define MAXLINE 200 788979160aSBruce Evans #define TIMEOUT 5 /* secs between rexmt's */ 79c33ed450SMatthew N. Dodd 809b50d902SRodney W. Grimes struct sockaddr_in peeraddr; 819b50d902SRodney W. Grimes int f; 829b50d902SRodney W. Grimes short port; 839b50d902SRodney W. Grimes int trace; 849b50d902SRodney W. Grimes int verbose; 859b50d902SRodney W. Grimes int connected; 869b50d902SRodney W. Grimes char mode[32]; 87c33ed450SMatthew N. Dodd char line[MAXLINE]; 889b50d902SRodney W. Grimes int margc; 899b50d902SRodney W. Grimes char *margv[20]; 909b50d902SRodney W. Grimes jmp_buf toplevel; 919b50d902SRodney W. Grimes struct servent *sp; 929b50d902SRodney W. Grimes 933f330d7dSWarner Losh void get(int, char **); 943f330d7dSWarner Losh void help(int, char **); 953f330d7dSWarner Losh void intr(int); 963f330d7dSWarner Losh void modecmd(int, char **); 973f330d7dSWarner Losh void put(int, char **); 983f330d7dSWarner Losh void quit(int, char **); 993f330d7dSWarner Losh void setascii(int, char **); 1003f330d7dSWarner Losh void setbinary(int, char **); 1013f330d7dSWarner Losh void setpeer(int, char **); 1023f330d7dSWarner Losh void setrexmt(int, char **); 1033f330d7dSWarner Losh void settimeout(int, char **); 1043f330d7dSWarner Losh void settrace(int, char **); 1053f330d7dSWarner Losh void setverbose(int, char **); 1063f330d7dSWarner Losh void status(int, char **); 1079b50d902SRodney W. Grimes 1083f330d7dSWarner Losh static void command(void) __dead2; 1093f330d7dSWarner Losh static const char *command_prompt(void); 1109b50d902SRodney W. Grimes 1113f330d7dSWarner Losh static void getusage(char *); 1123f330d7dSWarner Losh static void makeargv(void); 1133f330d7dSWarner Losh static void putusage(char *); 1143f330d7dSWarner Losh static void settftpmode(const char *); 1158049f797SMark Murray 1163f330d7dSWarner Losh char *tail(char *); 1173f330d7dSWarner Losh struct cmd *getcmd(char *); 1189b50d902SRodney W. Grimes 1199b50d902SRodney W. Grimes #define HELPINDENT (sizeof("connect")) 1209b50d902SRodney W. Grimes 1219b50d902SRodney W. Grimes struct cmd { 1228049f797SMark Murray const char *name; 1239b50d902SRodney W. Grimes char *help; 1243f330d7dSWarner Losh void (*handler)(int, char **); 1259b50d902SRodney W. Grimes }; 1269b50d902SRodney W. Grimes 1279b50d902SRodney W. Grimes char vhelp[] = "toggle verbose mode"; 1289b50d902SRodney W. Grimes char thelp[] = "toggle packet tracing"; 1299b50d902SRodney W. Grimes char chelp[] = "connect to remote tftp"; 1309b50d902SRodney W. Grimes char qhelp[] = "exit tftp"; 1319b50d902SRodney W. Grimes char hhelp[] = "print help information"; 1329b50d902SRodney W. Grimes char shelp[] = "send file"; 1339b50d902SRodney W. Grimes char rhelp[] = "receive file"; 1349b50d902SRodney W. Grimes char mhelp[] = "set file transfer mode"; 1359b50d902SRodney W. Grimes char sthelp[] = "show current status"; 1369b50d902SRodney W. Grimes char xhelp[] = "set per-packet retransmission timeout"; 1379b50d902SRodney W. Grimes char ihelp[] = "set total retransmission timeout"; 1389b50d902SRodney W. Grimes char ashelp[] = "set mode to netascii"; 1399b50d902SRodney W. Grimes char bnhelp[] = "set mode to octet"; 1409b50d902SRodney W. Grimes 1419b50d902SRodney W. Grimes struct cmd cmdtab[] = { 1429b50d902SRodney W. Grimes { "connect", chelp, setpeer }, 1439b50d902SRodney W. Grimes { "mode", mhelp, modecmd }, 1449b50d902SRodney W. Grimes { "put", shelp, put }, 1459b50d902SRodney W. Grimes { "get", rhelp, get }, 1469b50d902SRodney W. Grimes { "quit", qhelp, quit }, 1479b50d902SRodney W. Grimes { "verbose", vhelp, setverbose }, 1489b50d902SRodney W. Grimes { "trace", thelp, settrace }, 1499b50d902SRodney W. Grimes { "status", sthelp, status }, 1509b50d902SRodney W. Grimes { "binary", bnhelp, setbinary }, 1519b50d902SRodney W. Grimes { "ascii", ashelp, setascii }, 1529b50d902SRodney W. Grimes { "rexmt", xhelp, setrexmt }, 1539b50d902SRodney W. Grimes { "timeout", ihelp, settimeout }, 1549b50d902SRodney W. Grimes { "?", hhelp, help }, 1558049f797SMark Murray { NULL, NULL, NULL } 1569b50d902SRodney W. Grimes }; 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 { 1638049f797SMark Murray struct sockaddr_in lsin; 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"); 1718049f797SMark Murray bzero((char *)&lsin, sizeof(lsin)); 1728049f797SMark Murray lsin.sin_family = AF_INET; 1738049f797SMark Murray if (bind(f, (struct sockaddr *)&lsin, sizeof(lsin)) < 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 } 204b50764b0SGuido van Rooij if ((argc < 2) || (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, 2128f4c8256SDavid Malone MIN(sizeof(peeraddr.sin_addr), (size_t)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]); 2178f4c8256SDavid Malone if (peeraddr.sin_addr.s_addr == INADDR_NONE) { 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 { 2398049f797SMark Murray const char *m_name; 2408049f797SMark Murray const 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 { 2568049f797SMark Murray struct modes *p; 2578049f797SMark Murray const 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) 2888049f797SMark Murray int argc __unused; 2898049f797SMark Murray char *argv[] __unused; 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) 2978049f797SMark Murray int argc __unused; 2988049f797SMark Murray char *argv[] __unused; 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) 3068049f797SMark Murray const 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; 3238049f797SMark Murray int n; 3248049f797SMark Murray 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], ':')) { 3408049f797SMark Murray char *lcp; 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 } 3488049f797SMark Murray lcp = argv[argc - 1]; 3498049f797SMark Murray targ = index(lcp, ':'); 3509b50d902SRodney W. Grimes *targ++ = 0; 3518049f797SMark Murray hp = gethostbyname(lcp); 3529b50d902SRodney W. Grimes if (hp == NULL) { 3538049f797SMark Murray fprintf(stderr, "tftp: %s: ", lcp); 3549b50d902SRodney W. Grimes herror((char *)NULL); 3559b50d902SRodney W. Grimes return; 3569b50d902SRodney W. Grimes } 357a716ad66SWarner Losh bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, 3588f4c8256SDavid Malone MIN(sizeof(peeraddr.sin_addr), (size_t)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; 4188049f797SMark Murray int n; 4198049f797SMark Murray 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, 4568f4c8256SDavid Malone MIN(sizeof(peeraddr.sin_addr), (size_t)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) 5568049f797SMark Murray int argc __unused; 5578049f797SMark Murray char *argv[] __unused; 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 5708049f797SMark Murray intr(dummy) 5718049f797SMark Murray int dummy __unused; 5729b50d902SRodney W. Grimes { 5739b50d902SRodney W. Grimes 5749b50d902SRodney W. Grimes signal(SIGALRM, SIG_IGN); 5759b50d902SRodney W. Grimes alarm(0); 5769b50d902SRodney W. Grimes longjmp(toplevel, -1); 5779b50d902SRodney W. Grimes } 5789b50d902SRodney W. Grimes 5799b50d902SRodney W. Grimes char * 5809b50d902SRodney W. Grimes tail(filename) 5819b50d902SRodney W. Grimes char *filename; 5829b50d902SRodney W. Grimes { 5838049f797SMark Murray char *s; 5849b50d902SRodney W. Grimes 5859b50d902SRodney W. Grimes while (*filename) { 5869b50d902SRodney W. Grimes s = rindex(filename, '/'); 5879b50d902SRodney W. Grimes if (s == NULL) 5889b50d902SRodney W. Grimes break; 5899b50d902SRodney W. Grimes if (s[1]) 5909b50d902SRodney W. Grimes return (s + 1); 5919b50d902SRodney W. Grimes *s = '\0'; 5929b50d902SRodney W. Grimes } 5939b50d902SRodney W. Grimes return (filename); 5949b50d902SRodney W. Grimes } 5959b50d902SRodney W. Grimes 596c33ed450SMatthew N. Dodd static const char * 5978979160aSBruce Evans command_prompt() 5988979160aSBruce Evans { 5998979160aSBruce Evans 600c33ed450SMatthew N. Dodd return ("tftp> "); 601c33ed450SMatthew N. Dodd } 602c33ed450SMatthew N. Dodd 6039b50d902SRodney W. Grimes /* 6049b50d902SRodney W. Grimes * Command parser. 6059b50d902SRodney W. Grimes */ 606eaa86f9dSBruce Evans static void 6079b50d902SRodney W. Grimes command() 6089b50d902SRodney W. Grimes { 609c33ed450SMatthew N. Dodd HistEvent he; 6108049f797SMark Murray struct cmd *c; 6118979160aSBruce Evans static EditLine *el; 6128979160aSBruce Evans static History *hist; 613c33ed450SMatthew N. Dodd const char *bp; 6148979160aSBruce Evans char *cp; 6158049f797SMark Murray int len, num, vrbose; 616c33ed450SMatthew N. Dodd 6178049f797SMark Murray vrbose = isatty(0); 6188049f797SMark Murray if (vrbose) { 619c33ed450SMatthew N. Dodd el = el_init("tftp", stdin, stdout, stderr); 620c33ed450SMatthew N. Dodd hist = history_init(); 621c33ed450SMatthew N. Dodd history(hist, &he, H_EVENT, 100); 622c33ed450SMatthew N. Dodd el_set(el, EL_HIST, history, hist); 623c33ed450SMatthew N. Dodd el_set(el, EL_EDITOR, "emacs"); 624c33ed450SMatthew N. Dodd el_set(el, EL_PROMPT, command_prompt); 625c33ed450SMatthew N. Dodd el_set(el, EL_SIGNAL, 1); 626c33ed450SMatthew N. Dodd el_source(el, NULL); 627c33ed450SMatthew N. Dodd } 6289b50d902SRodney W. Grimes for (;;) { 6298049f797SMark Murray if (vrbose) { 630c33ed450SMatthew N. Dodd if ((bp = el_gets(el, &num)) == NULL || num == 0) 631c33ed450SMatthew N. Dodd exit(0); 632c33ed450SMatthew N. Dodd len = (num > MAXLINE) ? MAXLINE : num; 633c33ed450SMatthew N. Dodd memcpy(line, bp, len); 634c33ed450SMatthew N. Dodd line[len] = '\0'; 635c33ed450SMatthew N. Dodd history(hist, &he, H_ENTER, bp); 636c33ed450SMatthew N. Dodd } else { 637ca22ff9eSJoerg Wunsch if (fgets(line, sizeof line , stdin) == 0) { 6389b50d902SRodney W. Grimes if (feof(stdin)) { 6399b50d902SRodney W. Grimes exit(0); 6409b50d902SRodney W. Grimes } else { 6419b50d902SRodney W. Grimes continue; 6429b50d902SRodney W. Grimes } 6439b50d902SRodney W. Grimes } 644c33ed450SMatthew N. Dodd } 645ca22ff9eSJoerg Wunsch if ((cp = strchr(line, '\n'))) 646ca22ff9eSJoerg Wunsch *cp = '\0'; 6479b50d902SRodney W. Grimes if (line[0] == 0) 6489b50d902SRodney W. Grimes continue; 6499b50d902SRodney W. Grimes makeargv(); 6509b50d902SRodney W. Grimes if (margc == 0) 6519b50d902SRodney W. Grimes continue; 6529b50d902SRodney W. Grimes c = getcmd(margv[0]); 6539b50d902SRodney W. Grimes if (c == (struct cmd *)-1) { 6549b50d902SRodney W. Grimes printf("?Ambiguous command\n"); 6559b50d902SRodney W. Grimes continue; 6569b50d902SRodney W. Grimes } 6579b50d902SRodney W. Grimes if (c == 0) { 6589b50d902SRodney W. Grimes printf("?Invalid command\n"); 6599b50d902SRodney W. Grimes continue; 6609b50d902SRodney W. Grimes } 6619b50d902SRodney W. Grimes (*c->handler)(margc, margv); 6629b50d902SRodney W. Grimes } 6639b50d902SRodney W. Grimes } 6649b50d902SRodney W. Grimes 6659b50d902SRodney W. Grimes struct cmd * 6669b50d902SRodney W. Grimes getcmd(name) 6678049f797SMark Murray char *name; 6689b50d902SRodney W. Grimes { 6698049f797SMark Murray const char *p, *q; 6708049f797SMark Murray struct cmd *c, *found; 6718049f797SMark Murray int nmatches, longest; 6729b50d902SRodney W. Grimes 6739b50d902SRodney W. Grimes longest = 0; 6749b50d902SRodney W. Grimes nmatches = 0; 6759b50d902SRodney W. Grimes found = 0; 6769b50d902SRodney W. Grimes for (c = cmdtab; (p = c->name) != NULL; c++) { 6779b50d902SRodney W. Grimes for (q = name; *q == *p++; q++) 6789b50d902SRodney W. Grimes if (*q == 0) /* exact match? */ 6799b50d902SRodney W. Grimes return (c); 6809b50d902SRodney W. Grimes if (!*q) { /* the name was a prefix */ 6819b50d902SRodney W. Grimes if (q - name > longest) { 6829b50d902SRodney W. Grimes longest = q - name; 6839b50d902SRodney W. Grimes nmatches = 1; 6849b50d902SRodney W. Grimes found = c; 6859b50d902SRodney W. Grimes } else if (q - name == longest) 6869b50d902SRodney W. Grimes nmatches++; 6879b50d902SRodney W. Grimes } 6889b50d902SRodney W. Grimes } 6899b50d902SRodney W. Grimes if (nmatches > 1) 6909b50d902SRodney W. Grimes return ((struct cmd *)-1); 6919b50d902SRodney W. Grimes return (found); 6929b50d902SRodney W. Grimes } 6939b50d902SRodney W. Grimes 6949b50d902SRodney W. Grimes /* 6959b50d902SRodney W. Grimes * Slice a string up into argc/argv. 6969b50d902SRodney W. Grimes */ 6979b50d902SRodney W. Grimes static void 6989b50d902SRodney W. Grimes makeargv() 6999b50d902SRodney W. Grimes { 7008049f797SMark Murray char *cp; 7018049f797SMark Murray char **argp = margv; 7029b50d902SRodney W. Grimes 7039b50d902SRodney W. Grimes margc = 0; 704ca22ff9eSJoerg Wunsch if ((cp = strchr(line, '\n'))) 705ca22ff9eSJoerg Wunsch *cp = '\0'; 7069b50d902SRodney W. Grimes for (cp = line; *cp;) { 7079b50d902SRodney W. Grimes while (isspace(*cp)) 7089b50d902SRodney W. Grimes cp++; 7099b50d902SRodney W. Grimes if (*cp == '\0') 7109b50d902SRodney W. Grimes break; 7119b50d902SRodney W. Grimes *argp++ = cp; 7129b50d902SRodney W. Grimes margc += 1; 7139b50d902SRodney W. Grimes while (*cp != '\0' && !isspace(*cp)) 7149b50d902SRodney W. Grimes cp++; 7159b50d902SRodney W. Grimes if (*cp == '\0') 7169b50d902SRodney W. Grimes break; 7179b50d902SRodney W. Grimes *cp++ = '\0'; 7189b50d902SRodney W. Grimes } 7199b50d902SRodney W. Grimes *argp++ = 0; 7209b50d902SRodney W. Grimes } 7219b50d902SRodney W. Grimes 7229b50d902SRodney W. Grimes void 7239b50d902SRodney W. Grimes quit(argc, argv) 7248049f797SMark Murray int argc __unused; 7258049f797SMark Murray char *argv[] __unused; 7269b50d902SRodney W. Grimes { 7279b50d902SRodney W. Grimes 7289b50d902SRodney W. Grimes exit(0); 7299b50d902SRodney W. Grimes } 7309b50d902SRodney W. Grimes 7319b50d902SRodney W. Grimes /* 7329b50d902SRodney W. Grimes * Help command. 7339b50d902SRodney W. Grimes */ 7349b50d902SRodney W. Grimes void 7359b50d902SRodney W. Grimes help(argc, argv) 7369b50d902SRodney W. Grimes int argc; 7379b50d902SRodney W. Grimes char *argv[]; 7389b50d902SRodney W. Grimes { 7398049f797SMark Murray struct cmd *c; 7409b50d902SRodney W. Grimes 7419b50d902SRodney W. Grimes if (argc == 1) { 7429b50d902SRodney W. Grimes printf("Commands may be abbreviated. Commands are:\n\n"); 7439b50d902SRodney W. Grimes for (c = cmdtab; c->name; c++) 7449b50d902SRodney W. Grimes printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help); 7459b50d902SRodney W. Grimes return; 7469b50d902SRodney W. Grimes } 7479b50d902SRodney W. Grimes while (--argc > 0) { 7488049f797SMark Murray char *arg; 7499b50d902SRodney W. Grimes arg = *++argv; 7509b50d902SRodney W. Grimes c = getcmd(arg); 7519b50d902SRodney W. Grimes if (c == (struct cmd *)-1) 7529b50d902SRodney W. Grimes printf("?Ambiguous help command %s\n", arg); 7539b50d902SRodney W. Grimes else if (c == (struct cmd *)0) 7549b50d902SRodney W. Grimes printf("?Invalid help command %s\n", arg); 7559b50d902SRodney W. Grimes else 7569b50d902SRodney W. Grimes printf("%s\n", c->help); 7579b50d902SRodney W. Grimes } 7589b50d902SRodney W. Grimes } 7599b50d902SRodney W. Grimes 7609b50d902SRodney W. Grimes void 7619b50d902SRodney W. Grimes settrace(argc, argv) 7628049f797SMark Murray int argc __unused; 7638049f797SMark Murray char **argv __unused; 7649b50d902SRodney W. Grimes { 7659b50d902SRodney W. Grimes trace = !trace; 7669b50d902SRodney W. Grimes printf("Packet tracing %s.\n", trace ? "on" : "off"); 7679b50d902SRodney W. Grimes } 7689b50d902SRodney W. Grimes 7699b50d902SRodney W. Grimes void 7709b50d902SRodney W. Grimes setverbose(argc, argv) 7718049f797SMark Murray int argc __unused; 7728049f797SMark Murray char **argv __unused; 7739b50d902SRodney W. Grimes { 7749b50d902SRodney W. Grimes verbose = !verbose; 7759b50d902SRodney W. Grimes printf("Verbose mode %s.\n", verbose ? "on" : "off"); 7769b50d902SRodney W. Grimes } 777