2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)main.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/tftp/main.c,v 1.8.2.3 2002/05/14 22:08:07 bsd Exp $
38 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
41 * TFTP User Program -- Command Interface.
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
48 #include <netinet/in.h>
50 #include <arpa/inet.h>
64 #define TIMEOUT 5 /* secs between rexmt's */
66 struct sockaddr_storage peeraddr;
75 char *margv[MAX_MARGV];
76 const char *prompt = "tftp";
78 volatile int txrx_error;
81 void get(int, char **);
82 void help(int, char **);
83 void modecmd(int, char **);
84 void put(int, char **);
85 void quit(int, char **);
86 void setascii(int, char **);
87 void setbinary(int, char **);
88 void setpeer0(char *, const char *);
89 void setpeer(int, char **);
90 void setrexmt(int, char **);
91 void settimeout(int, char **);
92 void settrace(int, char **);
93 void setverbose(int, char **);
94 void status(int, char **);
96 static void command(void) __dead2;
98 static void getusage(char *);
99 static void makeargv(void);
100 static void putusage(char *);
101 static void settftpmode(const char *);
103 #define HELPINDENT (sizeof("connect"))
108 void (*handler)(int, char **);
111 const char vhelp[] = "toggle verbose mode";
112 const char thelp[] = "toggle packet tracing";
113 const char chelp[] = "connect to remote tftp";
114 const char qhelp[] = "exit tftp";
115 const char hhelp[] = "print help information";
116 const char shelp[] = "send file";
117 const char rhelp[] = "receive file";
118 const char mhelp[] = "set file transfer mode";
119 const char sthelp[] = "show current status";
120 const char xhelp[] = "set per-packet retransmission timeout";
121 const char ihelp[] = "set total retransmission timeout";
122 const char ashelp[] = "set mode to netascii";
123 const char bnhelp[] = "set mode to octet";
125 struct cmd cmdtab[] = {
126 { "connect", chelp, setpeer },
127 { "mode", mhelp, modecmd },
128 { "put", shelp, put },
129 { "get", rhelp, get },
130 { "quit", qhelp, quit },
131 { "verbose", vhelp, setverbose },
132 { "trace", thelp, settrace },
133 { "status", sthelp, status },
134 { "binary", bnhelp, setbinary },
135 { "ascii", ashelp, setascii },
136 { "rexmt", xhelp, setrexmt },
137 { "timeout", ihelp, settimeout },
138 { "?", hhelp, help },
142 struct cmd *getcmd(char *);
146 main(int argc, char **argv)
149 strcpy(mode, "netascii");
150 signal(SIGINT, intr);
152 if (setjmp(toplevel) != 0)
156 if (setjmp(toplevel) != 0)
161 char hostname[MAXHOSTNAMELEN];
164 setpeer0(char *host, const char *port)
166 struct addrinfo hints, *res0, *res;
168 struct sockaddr_storage ss;
169 const char *cause = "unknown";
177 memset(&hints, 0, sizeof(hints));
178 hints.ai_family = PF_UNSPEC;
179 hints.ai_socktype = SOCK_DGRAM;
180 hints.ai_protocol = IPPROTO_UDP;
181 hints.ai_flags = AI_CANONNAME;
184 error = getaddrinfo(host, port, &hints, &res0);
186 warnx("%s", gai_strerror(error));
190 for (res = res0; res; res = res->ai_next) {
191 if (res->ai_addrlen > sizeof(peeraddr))
193 f = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
199 memset(&ss, 0, sizeof(ss));
200 ss.ss_family = res->ai_family;
201 ss.ss_len = res->ai_addrlen;
202 if (bind(f, (struct sockaddr *)&ss, ss.ss_len) < 0) {
215 /* res->ai_addr <= sizeof(peeraddr) is guaranteed */
216 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
217 if (res->ai_canonname) {
218 (void) strncpy(hostname, res->ai_canonname,
221 (void) strncpy(hostname, host, sizeof(hostname));
222 hostname[sizeof(hostname)-1] = 0;
230 setpeer(int argc, char **argv)
234 strcpy(line, "Connect ");
236 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
241 if ((argc < 2) || (argc > 3)) {
242 printf("usage: %s host-name [port]\n", argv[0]);
246 setpeer0(argv[1], NULL);
248 setpeer0(argv[1], argv[2]);
255 { "ascii", "netascii" },
256 { "netascii", "netascii" },
257 { "binary", "octet" },
258 { "image", "octet" },
259 { "octet", "octet" },
260 /* { "mail", "mail" }, */
265 modecmd(int argc, char **argv)
271 printf("Using %s mode to transfer files.\n", mode);
275 for (p = modes; p->m_name; p++)
276 if (strcmp(argv[1], p->m_name) == 0)
279 settftpmode(p->m_mode);
282 printf("%s: unknown mode\n", argv[1]);
283 /* drop through and print usage message */
286 printf("usage: %s [", argv[0]);
288 for (p = modes; p->m_name; p++) {
289 printf("%s%s", sep, p->m_name);
298 setbinary(int argc __unused, char **argv __unused)
301 settftpmode("octet");
305 setascii(int argc __unused, char **argv __unused)
308 settftpmode("netascii");
312 settftpmode(const char *newmode)
314 strcpy(mode, newmode);
316 printf("mode set to %s\n", mode);
324 put(int argc, char **argv)
331 strcpy(line, "send ");
333 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
342 targ = argv[argc - 1];
343 if (strrchr(argv[argc - 1], ':')) {
344 for (n = 1; n < argc - 1; n++)
345 if (strchr(argv[n], ':')) {
350 targ = strrchr(cp, ':');
352 if (cp[0] == '[' && cp[strlen(cp) - 1] == ']') {
353 cp[strlen(cp) - 1] = '\0';
359 printf("No target machine specified.\n");
363 cp = argc == 2 ? tail(targ) : argv[1];
364 fd = open(cp, O_RDONLY);
370 printf("putting %s to %s:%s [%s]\n",
371 cp, hostname, targ, mode);
372 xmitfile(fd, targ, mode);
375 /* this assumes the target is a directory */
376 /* on a remote unix system. hmmmm. */
377 cp = strchr(targ, '\0');
379 for (n = 1; n < argc - 1; n++) {
380 strcpy(cp, tail(argv[n]));
381 fd = open(argv[n], O_RDONLY);
387 printf("putting %s to %s:%s [%s]\n",
388 argv[n], hostname, targ, mode);
389 xmitfile(fd, targ, mode);
396 printf("usage: %s file ... host:target, or\n", s);
397 printf(" %s file ... target (when already connected)\n", s);
404 get(int argc, char **argv)
412 strcpy(line, "get ");
414 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
424 for (n = 1; n < argc ; n++)
425 if (strrchr(argv[n], ':') == 0) {
430 for (n = 1; n < argc ; n++) {
431 src = strrchr(argv[n], ':');
437 if (cp[0] == '[' && cp[strlen(cp) - 1] == ']') {
438 cp[strlen(cp) - 1] = '\0';
446 cp = argc == 3 ? argv[2] : tail(src);
447 fd = creat(cp, 0644);
453 printf("getting from %s:%s to %s [%s]\n",
454 hostname, src, cp, mode);
455 recvfile(fd, src, mode);
458 cp = tail(src); /* new .. jdg */
459 fd = creat(cp, 0644);
465 printf("getting from %s:%s to %s [%s]\n",
466 hostname, src, cp, mode);
467 recvfile(fd, src, mode);
474 printf("usage: %s host:file host:file ... file, or\n", s);
475 printf(" %s file file ... file if connected\n", s);
478 int rexmtval = TIMEOUT;
481 setrexmt(int argc, char **argv)
486 strcpy(line, "Rexmt-timeout ");
488 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
494 printf("usage: %s value\n", argv[0]);
499 printf("%s: bad value\n", argv[1]);
504 int maxtimeout = 5 * TIMEOUT;
507 settimeout(int argc, char **argv)
512 strcpy(line, "Maximum-timeout ");
514 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
520 printf("usage: %s value\n", argv[0]);
525 printf("%s: bad value\n", argv[1]);
531 status(int argc __unused, char **argv __unused)
534 printf("Connected to %s.\n", hostname);
536 printf("Not connected.\n");
537 printf("Mode: %s Verbose: %s Tracing: %s\n", mode,
538 verbose ? "on" : "off", trace ? "on" : "off");
539 printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
540 rexmtval, maxtimeout);
544 intr(int signo __unused)
546 signal(SIGALRM, SIG_IGN);
548 longjmp(toplevel, -1);
557 s = strrchr(filename, '/');
577 printf("%s> ", prompt);
578 if (fgets(line, sizeof line , stdin) == 0) {
585 if ((cp = strchr(line, '\n')))
592 c = getcmd(margv[0]);
593 if (c == (struct cmd *)-1) {
594 printf("?Ambiguous command\n");
598 printf("?Invalid command\n");
601 (*c->handler)(margc, margv);
610 struct cmd *c, *found;
611 int nmatches, longest;
616 for (c = cmdtab; (p = c->name) != NULL; c++) {
617 for (q = name; *q == *p++; q++)
618 if (*q == 0) /* exact match? */
620 if (!*q) { /* the name was a prefix */
621 if (q - name > longest) {
625 } else if (q - name == longest)
630 return ((struct cmd *)-1);
635 * Slice a string up into argc/argv.
644 if ((cp = strchr(line, '\n')))
646 for (cp = line; margc < MAX_MARGV -1 && *cp;) {
653 while (*cp != '\0' && !isspace(*cp))
663 quit(int argc __unused, char **argv __unused)
673 help(int argc, char **argv)
678 printf("Commands may be abbreviated. Commands are:\n\n");
679 for (c = cmdtab; c->name; c++)
680 printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help);
687 if (c == (struct cmd *)-1)
688 printf("?Ambiguous help command %s\n", arg);
690 printf("?Invalid help command %s\n", arg);
692 printf("%s\n", c->help);
697 settrace(int argc __unused, char **argv __unused)
700 printf("Packet tracing %s.\n", trace ? "on" : "off");
704 setverbose(int argc __unused, char **argv __unused)
707 printf("Verbose mode %s.\n", verbose ? "on" : "off");