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 $
36 * $DragonFly: src/usr.bin/tftp/main.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
39 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
42 * TFTP User Program -- Command Interface.
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
48 #include <sys/param.h>
50 #include <netinet/in.h>
52 #include <arpa/inet.h>
66 #define TIMEOUT 5 /* secs between rexmt's */
68 struct sockaddr_storage peeraddr;
77 char *prompt = "tftp";
79 volatile int txrx_error;
82 void get __P((int, char **));
83 void help __P((int, char **));
84 void modecmd __P((int, char **));
85 void put __P((int, char **));
86 void quit __P((int, char **));
87 void setascii __P((int, char **));
88 void setbinary __P((int, char **));
89 void setpeer0 __P((char *, char *));
90 void setpeer __P((int, char **));
91 void setrexmt __P((int, char **));
92 void settimeout __P((int, char **));
93 void settrace __P((int, char **));
94 void setverbose __P((int, char **));
95 void status __P((int, char **));
97 static void command __P((void)) __dead2;
99 static void getusage __P((char *));
100 static void makeargv __P((void));
101 static void putusage __P((char *));
102 static void settftpmode __P((char *));
104 #define HELPINDENT (sizeof("connect"))
109 void (*handler) __P((int, char **));
112 char vhelp[] = "toggle verbose mode";
113 char thelp[] = "toggle packet tracing";
114 char chelp[] = "connect to remote tftp";
115 char qhelp[] = "exit tftp";
116 char hhelp[] = "print help information";
117 char shelp[] = "send file";
118 char rhelp[] = "receive file";
119 char mhelp[] = "set file transfer mode";
120 char sthelp[] = "show current status";
121 char xhelp[] = "set per-packet retransmission timeout";
122 char ihelp[] = "set total retransmission timeout";
123 char ashelp[] = "set mode to netascii";
124 char bnhelp[] = "set mode to octet";
126 struct cmd cmdtab[] = {
127 { "connect", chelp, setpeer },
128 { "mode", mhelp, modecmd },
129 { "put", shelp, put },
130 { "get", rhelp, get },
131 { "quit", qhelp, quit },
132 { "verbose", vhelp, setverbose },
133 { "trace", thelp, settrace },
134 { "status", sthelp, status },
135 { "binary", bnhelp, setbinary },
136 { "ascii", ashelp, setascii },
137 { "rexmt", xhelp, setrexmt },
138 { "timeout", ihelp, settimeout },
139 { "?", hhelp, help },
143 struct cmd *getcmd();
152 strcpy(mode, "netascii");
153 signal(SIGINT, intr);
155 if (setjmp(toplevel) != 0)
159 if (setjmp(toplevel) != 0)
164 char hostname[MAXHOSTNAMELEN];
171 struct addrinfo hints, *res0, *res;
173 struct sockaddr_storage ss;
174 char *cause = "unknown";
182 memset(&hints, 0, sizeof(hints));
183 hints.ai_family = PF_UNSPEC;
184 hints.ai_socktype = SOCK_DGRAM;
185 hints.ai_protocol = IPPROTO_UDP;
186 hints.ai_flags = AI_CANONNAME;
189 error = getaddrinfo(host, port, &hints, &res0);
191 warnx("%s", gai_strerror(error));
195 for (res = res0; res; res = res->ai_next) {
196 if (res->ai_addrlen > sizeof(peeraddr))
198 f = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
204 memset(&ss, 0, sizeof(ss));
205 ss.ss_family = res->ai_family;
206 ss.ss_len = res->ai_addrlen;
207 if (bind(f, (struct sockaddr *)&ss, ss.ss_len) < 0) {
220 /* res->ai_addr <= sizeof(peeraddr) is guaranteed */
221 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
222 if (res->ai_canonname) {
223 (void) strncpy(hostname, res->ai_canonname,
226 (void) strncpy(hostname, host, sizeof(hostname));
227 hostname[sizeof(hostname)-1] = 0;
241 strcpy(line, "Connect ");
243 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
248 if ((argc < 2) || (argc > 3)) {
249 printf("usage: %s host-name [port]\n", argv[0]);
253 setpeer0(argv[1], NULL);
255 setpeer0(argv[1], argv[2]);
262 { "ascii", "netascii" },
263 { "netascii", "netascii" },
264 { "binary", "octet" },
265 { "image", "octet" },
266 { "octet", "octet" },
267 /* { "mail", "mail" }, */
276 register struct modes *p;
280 printf("Using %s mode to transfer files.\n", mode);
284 for (p = modes; p->m_name; p++)
285 if (strcmp(argv[1], p->m_name) == 0)
288 settftpmode(p->m_mode);
291 printf("%s: unknown mode\n", argv[1]);
292 /* drop through and print usage message */
295 printf("usage: %s [", argv[0]);
297 for (p = modes; p->m_name; p++) {
298 printf("%s%s", sep, p->m_name);
307 setbinary(argc, argv)
312 settftpmode("octet");
321 settftpmode("netascii");
328 strcpy(mode, newmode);
330 printf("mode set to %s\n", mode);
344 register char *cp, *targ;
347 strcpy(line, "send ");
349 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
358 targ = argv[argc - 1];
359 if (rindex(argv[argc - 1], ':')) {
362 for (n = 1; n < argc - 1; n++)
363 if (index(argv[n], ':')) {
368 targ = rindex(cp, ':');
370 if (cp[0] == '[' && cp[strlen(cp) - 1] == ']') {
371 cp[strlen(cp) - 1] = '\0';
377 printf("No target machine specified.\n");
381 cp = argc == 2 ? tail(targ) : argv[1];
382 fd = open(cp, O_RDONLY);
388 printf("putting %s to %s:%s [%s]\n",
389 cp, hostname, targ, mode);
390 xmitfile(fd, targ, mode);
393 /* this assumes the target is a directory */
394 /* on a remote unix system. hmmmm. */
395 cp = index(targ, '\0');
397 for (n = 1; n < argc - 1; n++) {
398 strcpy(cp, tail(argv[n]));
399 fd = open(argv[n], O_RDONLY);
405 printf("putting %s to %s:%s [%s]\n",
406 argv[n], hostname, targ, mode);
407 xmitfile(fd, targ, mode);
415 printf("usage: %s file ... host:target, or\n", s);
416 printf(" %s file ... target (when already connected)\n", s);
433 strcpy(line, "get ");
435 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
445 for (n = 1; n < argc ; n++)
446 if (rindex(argv[n], ':') == 0) {
451 for (n = 1; n < argc ; n++) {
452 src = rindex(argv[n], ':');
459 if (cp[0] == '[' && cp[strlen(cp) - 1] == ']') {
460 cp[strlen(cp) - 1] = '\0';
468 cp = argc == 3 ? argv[2] : tail(src);
469 fd = creat(cp, 0644);
475 printf("getting from %s:%s to %s [%s]\n",
476 hostname, src, cp, mode);
477 recvfile(fd, src, mode);
480 cp = tail(src); /* new .. jdg */
481 fd = creat(cp, 0644);
487 printf("getting from %s:%s to %s [%s]\n",
488 hostname, src, cp, mode);
489 recvfile(fd, src, mode);
497 printf("usage: %s host:file host:file ... file, or\n", s);
498 printf(" %s file file ... file if connected\n", s);
501 int rexmtval = TIMEOUT;
511 strcpy(line, "Rexmt-timeout ");
513 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
519 printf("usage: %s value\n", argv[0]);
524 printf("%s: bad value\n", argv[1]);
529 int maxtimeout = 5 * TIMEOUT;
532 settimeout(argc, argv)
539 strcpy(line, "Maximum-timeout ");
541 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
547 printf("usage: %s value\n", argv[0]);
552 printf("%s: bad value\n", argv[1]);
563 printf("Connected to %s.\n", hostname);
565 printf("Not connected.\n");
566 printf("Mode: %s Verbose: %s Tracing: %s\n", mode,
567 verbose ? "on" : "off", trace ? "on" : "off");
568 printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
569 rexmtval, maxtimeout);
576 signal(SIGALRM, SIG_IGN);
578 longjmp(toplevel, -1);
588 s = rindex(filename, '/');
604 register struct cmd *c;
608 printf("%s> ", prompt);
609 if (fgets(line, sizeof line , stdin) == 0) {
616 if ((cp = strchr(line, '\n')))
623 c = getcmd(margv[0]);
624 if (c == (struct cmd *)-1) {
625 printf("?Ambiguous command\n");
629 printf("?Invalid command\n");
632 (*c->handler)(margc, margv);
640 register char *p, *q;
641 register struct cmd *c, *found;
642 register int nmatches, longest;
647 for (c = cmdtab; (p = c->name) != NULL; c++) {
648 for (q = name; *q == *p++; q++)
649 if (*q == 0) /* exact match? */
651 if (!*q) { /* the name was a prefix */
652 if (q - name > longest) {
656 } else if (q - name == longest)
661 return ((struct cmd *)-1);
666 * Slice a string up into argc/argv.
672 register char **argp = margv;
675 if ((cp = strchr(line, '\n')))
677 for (cp = line; *cp;) {
684 while (*cp != '\0' && !isspace(*cp))
710 register struct cmd *c;
713 printf("Commands may be abbreviated. Commands are:\n\n");
714 for (c = cmdtab; c->name; c++)
715 printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help);
722 if (c == (struct cmd *)-1)
723 printf("?Ambiguous help command %s\n", arg);
724 else if (c == (struct cmd *)0)
725 printf("?Invalid help command %s\n", arg);
727 printf("%s\n", c->help);
737 printf("Packet tracing %s.\n", trace ? "on" : "off");
741 setverbose(argc, argv)
746 printf("Verbose mode %s.\n", verbose ? "on" : "off");