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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
52 * TFTP User Program -- Command Interface.
54 #include <sys/param.h>
55 #include <sys/types.h>
56 #include <sys/socket.h>
58 #include <sys/param.h>
60 #include <netinet/in.h>
62 #include <arpa/inet.h>
78 #define TIMEOUT 5 /* secs between rexmt's */
80 struct sockaddr_storage peeraddr;
89 char *margv[MAX_MARGV];
91 volatile int txrx_error;
93 void get(int, char **);
94 void help(int, char **);
96 void modecmd(int, char **);
97 void put(int, char **);
98 void quit(int, char **);
99 void setascii(int, char **);
100 void setbinary(int, char **);
101 void setpeer0(char *, const char *);
102 void setpeer(int, char **);
103 void setrexmt(int, char **);
104 void settimeout(int, char **);
105 void settrace(int, char **);
106 void setverbose(int, char **);
107 void status(int, char **);
109 static void command(void) __dead2;
110 static const char *command_prompt(void);
112 static void getusage(const char *);
113 static void makeargv(void);
114 static void putusage(const char *);
115 static void settftpmode(const char *);
118 struct cmd *getcmd(char *);
120 #define HELPINDENT (sizeof("connect"))
125 void (*handler)(int, char **);
128 char vhelp[] = "toggle verbose mode";
129 char thelp[] = "toggle packet tracing";
130 char chelp[] = "connect to remote tftp";
131 char qhelp[] = "exit tftp";
132 char hhelp[] = "print help information";
133 char shelp[] = "send file";
134 char rhelp[] = "receive file";
135 char mhelp[] = "set file transfer mode";
136 char sthelp[] = "show current status";
137 char xhelp[] = "set per-packet retransmission timeout";
138 char ihelp[] = "set total retransmission timeout";
139 char ashelp[] = "set mode to netascii";
140 char bnhelp[] = "set mode to octet";
142 struct cmd cmdtab[] = {
143 { "connect", chelp, setpeer },
144 { "mode", mhelp, modecmd },
145 { "put", shelp, put },
146 { "get", rhelp, get },
147 { "quit", qhelp, quit },
148 { "verbose", vhelp, setverbose },
149 { "trace", thelp, settrace },
150 { "status", sthelp, status },
151 { "binary", bnhelp, setbinary },
152 { "ascii", ashelp, setascii },
153 { "rexmt", xhelp, setrexmt },
154 { "timeout", ihelp, settimeout },
155 { "?", hhelp, help },
160 main(int argc, char *argv[])
163 strcpy(mode, "netascii");
164 signal(SIGINT, intr);
166 if (setjmp(toplevel) != 0)
170 if (setjmp(toplevel) != 0)
175 char hostname[MAXHOSTNAMELEN];
178 setpeer0(char *host, const char *port)
180 struct addrinfo hints, *res0, *res;
182 struct sockaddr_storage ss;
183 const char *cause = "unknown";
191 memset(&hints, 0, sizeof(hints));
192 hints.ai_family = PF_UNSPEC;
193 hints.ai_socktype = SOCK_DGRAM;
194 hints.ai_protocol = IPPROTO_UDP;
195 hints.ai_flags = AI_CANONNAME;
198 error = getaddrinfo(host, port, &hints, &res0);
200 warnx("%s", gai_strerror(error));
204 for (res = res0; res; res = res->ai_next) {
205 if (res->ai_addrlen > sizeof(peeraddr))
207 f = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
213 memset(&ss, 0, sizeof(ss));
214 ss.ss_family = res->ai_family;
215 ss.ss_len = res->ai_addrlen;
216 if (bind(f, (struct sockaddr *)&ss, ss.ss_len) < 0) {
229 /* res->ai_addr <= sizeof(peeraddr) is guaranteed */
230 memcpy(&peeraddr, res->ai_addr, res->ai_addrlen);
231 if (res->ai_canonname) {
232 (void) strlcpy(hostname, res->ai_canonname,
235 (void) strlcpy(hostname, host, sizeof(hostname));
243 setpeer(int argc, char *argv[])
247 strcpy(line, "Connect ");
249 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
254 if ((argc < 2) || (argc > 3)) {
255 printf("usage: %s [host [port]]\n", argv[0]);
259 setpeer0(argv[1], argv[2]);
261 setpeer0(argv[1], NULL);
268 { "ascii", "netascii" },
269 { "netascii", "netascii" },
270 { "binary", "octet" },
271 { "image", "octet" },
272 { "octet", "octet" },
273 /* { "mail", "mail" }, */
278 modecmd(int argc, char *argv[])
284 printf("Using %s mode to transfer files.\n", mode);
288 for (p = modes; p->m_name; p++)
289 if (strcmp(argv[1], p->m_name) == 0)
292 settftpmode(p->m_mode);
295 printf("%s: unknown mode\n", argv[1]);
296 /* drop through and print usage message */
299 printf("usage: %s [", argv[0]);
301 for (p = modes; p->m_name; p++) {
302 printf("%s%s", sep, p->m_name);
311 setbinary(int argc __unused, char *argv[] __unused)
314 settftpmode("octet");
318 setascii(int argc __unused, char *argv[] __unused)
321 settftpmode("netascii");
325 settftpmode(const char *newmode)
327 strcpy(mode, newmode);
329 printf("mode set to %s\n", mode);
337 put(int argc, char *argv[])
344 strcpy(line, "send ");
346 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
355 targ = argv[argc - 1];
356 if (rindex(argv[argc - 1], ':')) {
359 for (n = 1; n < argc - 1; n++)
360 if (index(argv[n], ':')) {
364 lcp = argv[argc - 1];
365 targ = rindex(lcp, ':');
367 if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') {
368 lcp[strlen(lcp) - 1] = '\0';
374 printf("No target machine specified.\n");
378 cp = argc == 2 ? tail(targ) : argv[1];
379 fd = open(cp, O_RDONLY);
385 printf("putting %s to %s:%s [%s]\n",
386 cp, hostname, targ, mode);
387 xmitfile(fd, targ, mode);
390 /* this assumes the target is a directory */
391 /* on a remote unix system. hmmmm. */
392 cp = index(targ, '\0');
394 for (n = 1; n < argc - 1; n++) {
395 strcpy(cp, tail(argv[n]));
396 fd = open(argv[n], O_RDONLY);
402 printf("putting %s to %s:%s [%s]\n",
403 argv[n], hostname, targ, mode);
404 xmitfile(fd, targ, mode);
409 putusage(const char *s)
411 printf("usage: %s file [[host:]remotename]\n", s);
412 printf(" %s file1 file2 ... fileN [[host:]remote-directory]\n", s);
419 get(int argc, char *argv[])
427 strcpy(line, "get ");
429 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
439 for (n = 1; n < argc ; n++)
440 if (rindex(argv[n], ':') == 0) {
445 for (n = 1; n < argc ; n++) {
446 src = rindex(argv[n], ':');
454 if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') {
455 lcp[strlen(lcp) - 1] = '\0';
463 cp = argc == 3 ? argv[2] : tail(src);
464 fd = creat(cp, 0644);
470 printf("getting from %s:%s to %s [%s]\n",
471 hostname, src, cp, mode);
472 recvfile(fd, src, mode);
475 cp = tail(src); /* new .. jdg */
476 fd = creat(cp, 0644);
482 printf("getting from %s:%s to %s [%s]\n",
483 hostname, src, cp, mode);
484 recvfile(fd, src, mode);
489 getusage(const char *s)
491 printf("usage: %s [host:]file [localname]\n", s);
492 printf(" %s [host1:]file1 [host2:]file2 ... [hostN:]fileN\n", s);
495 int rexmtval = TIMEOUT;
498 setrexmt(int argc, char *argv[])
503 strcpy(line, "Rexmt-timeout ");
505 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
511 printf("usage: %s value\n", argv[0]);
516 printf("%s: bad value\n", argv[1]);
521 int maxtimeout = 5 * TIMEOUT;
524 settimeout(int argc, char *argv[])
529 strcpy(line, "Maximum-timeout ");
531 fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
537 printf("usage: %s value\n", argv[0]);
542 printf("%s: bad value\n", argv[1]);
548 status(int argc __unused, char *argv[] __unused)
551 printf("Connected to %s.\n", hostname);
553 printf("Not connected.\n");
554 printf("Mode: %s Verbose: %s Tracing: %s\n", mode,
555 verbose ? "on" : "off", trace ? "on" : "off");
556 printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
557 rexmtval, maxtimeout);
561 intr(int dummy __unused)
564 signal(SIGALRM, SIG_IGN);
566 longjmp(toplevel, -1);
575 s = rindex(filename, '/');
601 static History *hist;
604 int len, num, vrbose;
608 el = el_init("tftp", stdin, stdout, stderr);
609 hist = history_init();
610 history(hist, &he, H_SETSIZE, 100);
611 el_set(el, EL_HIST, history, hist);
612 el_set(el, EL_EDITOR, "emacs");
613 el_set(el, EL_PROMPT, command_prompt);
614 el_set(el, EL_SIGNAL, 1);
619 if ((bp = el_gets(el, &num)) == NULL || num == 0)
621 len = (num > MAXLINE) ? MAXLINE : num;
622 memcpy(line, bp, len);
624 history(hist, &he, H_ENTER, bp);
626 if (fgets(line, sizeof line , stdin) == 0) {
634 if ((cp = strchr(line, '\n')))
641 c = getcmd(margv[0]);
642 if (c == (struct cmd *)-1) {
643 printf("?Ambiguous command\n");
647 printf("?Invalid command\n");
650 (*c->handler)(margc, margv);
658 struct cmd *c, *found;
659 int nmatches, longest;
664 for (c = cmdtab; (p = c->name) != NULL; c++) {
665 for (q = name; *q == *p++; q++)
666 if (*q == 0) /* exact match? */
668 if (!*q) { /* the name was a prefix */
669 if (q - name > longest) {
673 } else if (q - name == longest)
678 return ((struct cmd *)-1);
683 * Slice a string up into argc/argv.
692 if ((cp = strchr(line, '\n')))
694 for (cp = line; margc < MAX_MARGV - 1 && *cp;) {
701 while (*cp != '\0' && !isspace(*cp))
711 quit(int argc __unused, char *argv[] __unused)
720 help(int argc, char *argv[])
725 printf("Commands may be abbreviated. Commands are:\n\n");
726 for (c = cmdtab; c->name; c++)
727 printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help);
734 if (c == (struct cmd *)-1)
735 printf("?Ambiguous help command %s\n", arg);
736 else if (c == (struct cmd *)0)
737 printf("?Invalid help command %s\n", arg);
739 printf("%s\n", c->help);
744 settrace(int argc __unused, char **argv __unused)
747 printf("Packet tracing %s.\n", trace ? "on" : "off");
751 setverbose(int argc __unused, char **argv __unused)
754 printf("Verbose mode %s.\n", verbose ? "on" : "off");