xref: /freebsd/usr.bin/tftp/main.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes /*
359b50d902SRodney W. Grimes  * TFTP User Program -- Command Interface.
369b50d902SRodney W. Grimes  */
37fd129a02SPhilippe Charnier #include <sys/param.h>
389b50d902SRodney W. Grimes #include <sys/file.h>
39b9fabe99SDag-Erling Smørgrav #include <sys/socket.h>
40752fa694SWarner Losh #include <sys/stat.h>
41b9fabe99SDag-Erling Smørgrav #include <sys/sysctl.h>
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <netinet/in.h>
449b50d902SRodney W. Grimes #include <arpa/inet.h>
45752fa694SWarner Losh #include <arpa/tftp.h>
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <ctype.h>
48fd129a02SPhilippe Charnier #include <err.h>
498979160aSBruce Evans #include <histedit.h>
509b50d902SRodney W. Grimes #include <netdb.h>
519b50d902SRodney W. Grimes #include <setjmp.h>
529b50d902SRodney W. Grimes #include <signal.h>
5349fb0615SConrad Meyer #include <stdbool.h>
54b9fabe99SDag-Erling Smørgrav #include <stddef.h>
559b50d902SRodney W. Grimes #include <stdio.h>
569b50d902SRodney W. Grimes #include <stdlib.h>
579b50d902SRodney W. Grimes #include <string.h>
589b50d902SRodney W. Grimes #include <unistd.h>
599b50d902SRodney W. Grimes 
60752fa694SWarner Losh #include "tftp-utils.h"
61752fa694SWarner Losh #include "tftp-io.h"
62752fa694SWarner Losh #include "tftp-options.h"
63752fa694SWarner Losh #include "tftp.h"
649b50d902SRodney W. Grimes 
650f8d6cfcSMarcel Moolenaar #define	MAXLINE		(2 * MAXPATHLEN)
668979160aSBruce Evans #define	TIMEOUT		5		/* secs between rexmt's */
67c33ed450SMatthew N. Dodd 
687c9acc77SXin LI typedef struct	sockaddr_storage peeraddr;
69752fa694SWarner Losh static int	connected;
70752fa694SWarner Losh static char	mode[32];
71bf70beceSEd Schouten static jmp_buf	toplevel;
72*92570f67SDag-Erling Smørgrav static int	txrx_error;
73752fa694SWarner Losh static int	peer;
749b50d902SRodney W. Grimes 
75752fa694SWarner Losh #define	MAX_MARGV	20
76752fa694SWarner Losh static int	margc;
77752fa694SWarner Losh static char	*margv[MAX_MARGV];
78752fa694SWarner Losh 
79752fa694SWarner Losh int		verbose;
80bf70beceSEd Schouten static char	*port = NULL;
81752fa694SWarner Losh 
82752fa694SWarner Losh static void	get(int, char **);
83752fa694SWarner Losh static void	help(int, char **);
84752fa694SWarner Losh static void	intr(int);
85752fa694SWarner Losh static void	modecmd(int, char **);
86752fa694SWarner Losh static void	put(int, char **);
87752fa694SWarner Losh static void	quit(int, char **);
88752fa694SWarner Losh static void	setascii(int, char **);
89752fa694SWarner Losh static void	setbinary(int, char **);
90752fa694SWarner Losh static void	setpeer0(char *, const char *);
91752fa694SWarner Losh static void	setpeer(int, char **);
92752fa694SWarner Losh static void	settimeoutpacket(int, char **);
93752fa694SWarner Losh static void	settimeoutnetwork(int, char **);
94752fa694SWarner Losh static void	setdebug(int, char **);
95752fa694SWarner Losh static void	setverbose(int, char **);
96752fa694SWarner Losh static void	showstatus(int, char **);
97752fa694SWarner Losh static void	setblocksize(int, char **);
98752fa694SWarner Losh static void	setblocksize2(int, char **);
99752fa694SWarner Losh static void	setoptions(int, char **);
100752fa694SWarner Losh static void	setrollover(int, char **);
101752fa694SWarner Losh static void	setpacketdrop(int, char **);
102fdf929ffSJohn Baldwin static void	setwindowsize(int, char **);
1039b50d902SRodney W. Grimes 
10449fb0615SConrad Meyer static void command(bool, EditLine *, History *, HistEvent *) __dead2;
1053f330d7dSWarner Losh static const char *command_prompt(void);
1069b50d902SRodney W. Grimes 
107752fa694SWarner Losh static void urihandling(char *URI);
108752fa694SWarner Losh static void getusage(char *);
109752fa694SWarner Losh static void makeargv(char *line);
110752fa694SWarner Losh static void putusage(char *);
1113f330d7dSWarner Losh static void settftpmode(const char *);
1128049f797SMark Murray 
113752fa694SWarner Losh static char	*tail(char *);
114b9fabe99SDag-Erling Smørgrav static const struct cmd *getcmd(const char *);
1159b50d902SRodney W. Grimes 
1169b50d902SRodney W. Grimes #define HELPINDENT (sizeof("connect"))
1179b50d902SRodney W. Grimes 
1189b50d902SRodney W. Grimes struct cmd {
1198049f797SMark Murray 	const char	*name;
1203f330d7dSWarner Losh 	void	(*handler)(int, char **);
121752fa694SWarner Losh 	const char	*help;
1229b50d902SRodney W. Grimes };
1239b50d902SRodney W. Grimes 
124752fa694SWarner Losh static struct cmd cmdtab[] = {
125752fa694SWarner Losh 	{ "connect",	setpeer,	"connect to remote tftp"	},
126752fa694SWarner Losh 	{ "mode",	modecmd,	"set file transfer mode"	},
127752fa694SWarner Losh 	{ "put",	put,		"send file"			},
128752fa694SWarner Losh 	{ "get",	get,		"receive file"			},
129752fa694SWarner Losh 	{ "quit",	quit,		"exit tftp"			},
130752fa694SWarner Losh 	{ "verbose",	setverbose,	"toggle verbose mode"		},
131752fa694SWarner Losh 	{ "status",	showstatus,	"show current status"		},
132752fa694SWarner Losh 	{ "binary",     setbinary,	"set mode to octet"		},
133752fa694SWarner Losh 	{ "ascii",      setascii,	"set mode to netascii"		},
134752fa694SWarner Losh 	{ "rexmt",	settimeoutpacket,
135752fa694SWarner Losh 	  "set per-packet retransmission timeout[-]" },
136752fa694SWarner Losh 	{ "timeout",	settimeoutnetwork,
137752fa694SWarner Losh 	  "set total retransmission timeout" },
138752fa694SWarner Losh 	{ "trace",	setdebug,	"enable 'debug packet'[-]"	},
139752fa694SWarner Losh 	{ "debug",	setdebug,	"enable verbose output"		},
140752fa694SWarner Losh 	{ "blocksize",	setblocksize,	"set blocksize[*]"		},
141752fa694SWarner Losh 	{ "blocksize2",	setblocksize2,	"set blocksize as a power of 2[**]" },
142752fa694SWarner Losh 	{ "rollover",	setrollover,	"rollover after 64K packets[**]" },
143752fa694SWarner Losh 	{ "options",	setoptions,
144752fa694SWarner Losh 	  "enable or disable RFC2347 style options" },
145752fa694SWarner Losh 	{ "help",	help,		"print help information"	},
146f471745aSWarner Losh 	{ "packetdrop",	setpacketdrop,	"artificial packetloss feature"	},
147fdf929ffSJohn Baldwin 	{ "windowsize",	setwindowsize,	"set windowsize[*]"		},
148752fa694SWarner Losh 	{ "?",		help,		"print help information"	},
1498049f797SMark Murray 	{ NULL,		NULL,		NULL				}
1509b50d902SRodney W. Grimes };
1519b50d902SRodney W. Grimes 
152752fa694SWarner Losh static struct	modes {
1538049f797SMark Murray 	const char *m_name;
1548049f797SMark Murray 	const char *m_mode;
1559b50d902SRodney W. Grimes } modes[] = {
1569b50d902SRodney W. Grimes 	{ "ascii",	"netascii" },
1579b50d902SRodney W. Grimes 	{ "netascii",	"netascii" },
1589b50d902SRodney W. Grimes 	{ "binary",	"octet" },
1599b50d902SRodney W. Grimes 	{ "image",	"octet" },
1609b50d902SRodney W. Grimes 	{ "octet",	"octet" },
161752fa694SWarner Losh 	{ NULL,		NULL }
1629b50d902SRodney W. Grimes };
1639b50d902SRodney W. Grimes 
164752fa694SWarner Losh int
main(int argc,char * argv[])165752fa694SWarner Losh main(int argc, char *argv[])
166752fa694SWarner Losh {
16749fb0615SConrad Meyer 	HistEvent he;
168e4803b1cSJustin Hibbits 	static EditLine *el;
169e4803b1cSJustin Hibbits 	static History *hist;
17049fb0615SConrad Meyer 	bool interactive;
171752fa694SWarner Losh 
172752fa694SWarner Losh 	acting_as_client = 1;
173752fa694SWarner Losh 	peer = -1;
1747dbe228cSAlan Somers 	strcpy(mode, "octet");
175752fa694SWarner Losh 	signal(SIGINT, intr);
17649fb0615SConrad Meyer 
17749fb0615SConrad Meyer 	interactive = isatty(STDIN_FILENO);
17849fb0615SConrad Meyer 	if (interactive) {
17949fb0615SConrad Meyer 		el = el_init("tftp", stdin, stdout, stderr);
18049fb0615SConrad Meyer 		hist = history_init();
18149fb0615SConrad Meyer 		history(hist, &he, H_SETSIZE, 100);
18249fb0615SConrad Meyer 		el_set(el, EL_HIST, history, hist);
18349fb0615SConrad Meyer 		el_set(el, EL_EDITOR, "emacs");
18449fb0615SConrad Meyer 		el_set(el, EL_PROMPT, command_prompt);
18549fb0615SConrad Meyer 		el_set(el, EL_SIGNAL, 1);
18649fb0615SConrad Meyer 		el_source(el, NULL);
18749fb0615SConrad Meyer 	}
18849fb0615SConrad Meyer 
189752fa694SWarner Losh 	if (argc > 1) {
190752fa694SWarner Losh 		if (setjmp(toplevel) != 0)
191752fa694SWarner Losh 			exit(txrx_error);
192752fa694SWarner Losh 
193752fa694SWarner Losh 		if (strncmp(argv[1], "tftp://", 7) == 0) {
194752fa694SWarner Losh 			urihandling(argv[1]);
195752fa694SWarner Losh 			exit(txrx_error);
196752fa694SWarner Losh 		}
197752fa694SWarner Losh 
198752fa694SWarner Losh 		setpeer(argc, argv);
199752fa694SWarner Losh 	}
20049fb0615SConrad Meyer 
20149fb0615SConrad Meyer 	if (setjmp(toplevel) != 0) {
20249fb0615SConrad Meyer 		if (interactive)
20349fb0615SConrad Meyer 			el_reset(el);
204752fa694SWarner Losh 		(void)putchar('\n');
20549fb0615SConrad Meyer 	}
206752fa694SWarner Losh 
207752fa694SWarner Losh 	init_options();
20849fb0615SConrad Meyer 	command(interactive, el, hist, &he);
209752fa694SWarner Losh }
210752fa694SWarner Losh 
211752fa694SWarner Losh /*
212752fa694SWarner Losh  * RFC3617 handling of TFTP URIs:
213752fa694SWarner Losh  *
214752fa694SWarner Losh  *    tftpURI         = "tftp://" host "/" file [ mode ]
215752fa694SWarner Losh  *    mode            = ";"  "mode=" ( "netascii" / "octet" )
216752fa694SWarner Losh  *    file            = *( unreserved / escaped )
217752fa694SWarner Losh  *    host            = <as specified by RFC 2732>
218752fa694SWarner Losh  *    unreserved      = <as specified in RFC 2396>
219752fa694SWarner Losh  *    escaped         = <as specified in RFC 2396>
220752fa694SWarner Losh  *
221752fa694SWarner Losh  * We are cheating a little bit by allowing any mode as specified in the
222752fa694SWarner Losh  * modes table defined earlier on in this file and mapping it on the real
223752fa694SWarner Losh  * mode.
224752fa694SWarner Losh  */
225752fa694SWarner Losh static void
urihandling(char * URI)226752fa694SWarner Losh urihandling(char *URI)
227752fa694SWarner Losh {
228752fa694SWarner Losh 	char	uri[ARG_MAX];
229752fa694SWarner Losh 	char	*host = NULL;
230752fa694SWarner Losh 	char	*path = NULL;
23104ebad38SMarius Strobl 	char	*opts = NULL;
23204ebad38SMarius Strobl 	const char *tmode = "octet";
233752fa694SWarner Losh 	char	*s;
234752fa694SWarner Losh 	char	line[MAXLINE];
235752fa694SWarner Losh 	int	i;
236752fa694SWarner Losh 
23732e49abeSXin LI 	strlcpy(uri, URI, ARG_MAX);
238752fa694SWarner Losh 	host = uri + 7;
239752fa694SWarner Losh 
240752fa694SWarner Losh 	if ((s = strchr(host, '/')) == NULL) {
241752fa694SWarner Losh 		fprintf(stderr,
242752fa694SWarner Losh 		    "Invalid URI: Couldn't find / after hostname\n");
243752fa694SWarner Losh 		exit(1);
244752fa694SWarner Losh 	}
245752fa694SWarner Losh 	*s = '\0';
246752fa694SWarner Losh 	path = s + 1;
247752fa694SWarner Losh 
248752fa694SWarner Losh 	if ((s = strchr(path, ';')) != NULL) {
249752fa694SWarner Losh 		*s = '\0';
25004ebad38SMarius Strobl 		opts = s + 1;
251752fa694SWarner Losh 
25204ebad38SMarius Strobl 		if (strncmp(opts, "mode=", 5) == 0) {
25304ebad38SMarius Strobl 			tmode = opts;
25404ebad38SMarius Strobl 			tmode += 5;
255752fa694SWarner Losh 
256752fa694SWarner Losh 			for (i = 0; modes[i].m_name != NULL; i++) {
25704ebad38SMarius Strobl 				if (strcmp(modes[i].m_name, tmode) == 0)
258752fa694SWarner Losh 					break;
259752fa694SWarner Losh 			}
260752fa694SWarner Losh 			if (modes[i].m_name == NULL) {
261752fa694SWarner Losh 				fprintf(stderr, "Invalid mode: '%s'\n", mode);
262752fa694SWarner Losh 				exit(1);
263752fa694SWarner Losh 			}
264752fa694SWarner Losh 			settftpmode(modes[i].m_mode);
265752fa694SWarner Losh 		}
266752fa694SWarner Losh 	} else {
267752fa694SWarner Losh 		settftpmode("octet");
268752fa694SWarner Losh 	}
269752fa694SWarner Losh 
270752fa694SWarner Losh 	setpeer0(host, NULL);
271752fa694SWarner Losh 
272752fa694SWarner Losh 	sprintf(line, "get %s", path);
273752fa694SWarner Losh 	makeargv(line);
274752fa694SWarner Losh 	get(margc, margv);
275752fa694SWarner Losh }
276752fa694SWarner Losh 
277752fa694SWarner Losh static char    hostname[MAXHOSTNAMELEN];
278752fa694SWarner Losh 
279752fa694SWarner Losh static void
setpeer0(char * host,const char * lport)280752fa694SWarner Losh setpeer0(char *host, const char *lport)
281752fa694SWarner Losh {
282752fa694SWarner Losh 	struct addrinfo hints, *res0, *res;
283752fa694SWarner Losh 	int error;
284752fa694SWarner Losh 	const char *cause = "unknown";
285752fa694SWarner Losh 
286752fa694SWarner Losh 	if (connected) {
287752fa694SWarner Losh 		close(peer);
288752fa694SWarner Losh 		peer = -1;
289752fa694SWarner Losh 	}
290752fa694SWarner Losh 	connected = 0;
291752fa694SWarner Losh 
292752fa694SWarner Losh 	memset(&hints, 0, sizeof(hints));
293752fa694SWarner Losh 	hints.ai_family = PF_UNSPEC;
294752fa694SWarner Losh 	hints.ai_socktype = SOCK_DGRAM;
295752fa694SWarner Losh 	hints.ai_protocol = IPPROTO_UDP;
296752fa694SWarner Losh 	hints.ai_flags = AI_CANONNAME;
297752fa694SWarner Losh 	if (!lport)
298752fa694SWarner Losh 		lport = "tftp";
299752fa694SWarner Losh 	error = getaddrinfo(host, lport, &hints, &res0);
300752fa694SWarner Losh 	if (error) {
301752fa694SWarner Losh 		warnx("%s", gai_strerror(error));
302752fa694SWarner Losh 		return;
303752fa694SWarner Losh 	}
304752fa694SWarner Losh 
305752fa694SWarner Losh 	for (res = res0; res; res = res->ai_next) {
306752fa694SWarner Losh 		if (res->ai_addrlen > sizeof(peeraddr))
307752fa694SWarner Losh 			continue;
308752fa694SWarner Losh 		peer = socket(res->ai_family, res->ai_socktype,
309752fa694SWarner Losh 			res->ai_protocol);
310752fa694SWarner Losh 		if (peer < 0) {
311752fa694SWarner Losh 			cause = "socket";
312752fa694SWarner Losh 			continue;
313752fa694SWarner Losh 		}
314752fa694SWarner Losh 
315752fa694SWarner Losh 		memset(&peer_sock, 0, sizeof(peer_sock));
316752fa694SWarner Losh 		peer_sock.ss_family = res->ai_family;
317752fa694SWarner Losh 		peer_sock.ss_len = res->ai_addrlen;
318752fa694SWarner Losh 		if (bind(peer, (struct sockaddr *)&peer_sock, peer_sock.ss_len) < 0) {
319752fa694SWarner Losh 			cause = "bind";
320752fa694SWarner Losh 			close(peer);
321752fa694SWarner Losh 			peer = -1;
322752fa694SWarner Losh 			continue;
323752fa694SWarner Losh 		}
324752fa694SWarner Losh 
325752fa694SWarner Losh 		break;
326752fa694SWarner Losh 	}
327752fa694SWarner Losh 
328752fa694SWarner Losh 	if (peer < 0)
329752fa694SWarner Losh 		warn("%s", cause);
330752fa694SWarner Losh 	else {
331752fa694SWarner Losh 		/* res->ai_addr <= sizeof(peeraddr) is guaranteed */
332752fa694SWarner Losh 		memcpy(&peer_sock, res->ai_addr, res->ai_addrlen);
333752fa694SWarner Losh 		if (res->ai_canonname) {
33432e49abeSXin LI 			(void) strlcpy(hostname, res->ai_canonname,
335752fa694SWarner Losh 				sizeof(hostname));
336752fa694SWarner Losh 		} else
33732e49abeSXin LI 			(void) strlcpy(hostname, host, sizeof(hostname));
338752fa694SWarner Losh 		connected = 1;
339752fa694SWarner Losh 	}
340752fa694SWarner Losh 
341752fa694SWarner Losh 	freeaddrinfo(res0);
342752fa694SWarner Losh }
343752fa694SWarner Losh 
344752fa694SWarner Losh static void
setpeer(int argc,char * argv[])345752fa694SWarner Losh setpeer(int argc, char *argv[])
346752fa694SWarner Losh {
347752fa694SWarner Losh 	char	line[MAXLINE];
348752fa694SWarner Losh 
349752fa694SWarner Losh 	if (argc < 2) {
350752fa694SWarner Losh 		strcpy(line, "Connect ");
351752fa694SWarner Losh 		printf("(to) ");
352752fa694SWarner Losh 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
353752fa694SWarner Losh 		makeargv(line);
354752fa694SWarner Losh 		argc = margc;
355752fa694SWarner Losh 		argv = margv;
356752fa694SWarner Losh 	}
357752fa694SWarner Losh 	if ((argc < 2) || (argc > 3)) {
358752fa694SWarner Losh 		printf("usage: %s [host [port]]\n", argv[0]);
359752fa694SWarner Losh 		return;
360752fa694SWarner Losh 	}
361752fa694SWarner Losh 	if (argc == 3) {
362752fa694SWarner Losh 		port = argv[2];
363752fa694SWarner Losh 		setpeer0(argv[1], argv[2]);
364752fa694SWarner Losh 	} else
365752fa694SWarner Losh 		setpeer0(argv[1], NULL);
366752fa694SWarner Losh }
367752fa694SWarner Losh 
368752fa694SWarner Losh static void
modecmd(int argc,char * argv[])3691b0fa6faSXin LI modecmd(int argc, char *argv[])
3709b50d902SRodney W. Grimes {
3718049f797SMark Murray 	struct modes *p;
3728049f797SMark Murray 	const char *sep;
3739b50d902SRodney W. Grimes 
3749b50d902SRodney W. Grimes 	if (argc < 2) {
3759b50d902SRodney W. Grimes 		printf("Using %s mode to transfer files.\n", mode);
3769b50d902SRodney W. Grimes 		return;
3779b50d902SRodney W. Grimes 	}
3789b50d902SRodney W. Grimes 	if (argc == 2) {
3799b50d902SRodney W. Grimes 		for (p = modes; p->m_name; p++)
3809b50d902SRodney W. Grimes 			if (strcmp(argv[1], p->m_name) == 0)
3819b50d902SRodney W. Grimes 				break;
3829b50d902SRodney W. Grimes 		if (p->m_name) {
3839b50d902SRodney W. Grimes 			settftpmode(p->m_mode);
3849b50d902SRodney W. Grimes 			return;
3859b50d902SRodney W. Grimes 		}
3869b50d902SRodney W. Grimes 		printf("%s: unknown mode\n", argv[1]);
3879b50d902SRodney W. Grimes 		/* drop through and print usage message */
3889b50d902SRodney W. Grimes 	}
3899b50d902SRodney W. Grimes 
3909b50d902SRodney W. Grimes 	printf("usage: %s [", argv[0]);
3919b50d902SRodney W. Grimes 	sep = " ";
392752fa694SWarner Losh 	for (p = modes; p->m_name != NULL; p++) {
3939b50d902SRodney W. Grimes 		printf("%s%s", sep, p->m_name);
3949b50d902SRodney W. Grimes 		if (*sep == ' ')
3959b50d902SRodney W. Grimes 			sep = " | ";
3969b50d902SRodney W. Grimes 	}
3979b50d902SRodney W. Grimes 	printf(" ]\n");
3989b50d902SRodney W. Grimes 	return;
3999b50d902SRodney W. Grimes }
4009b50d902SRodney W. Grimes 
401752fa694SWarner Losh static void
setbinary(int argc __unused,char * argv[]__unused)4021b0fa6faSXin LI setbinary(int argc __unused, char *argv[] __unused)
4039b50d902SRodney W. Grimes {
4049b50d902SRodney W. Grimes 
4059b50d902SRodney W. Grimes 	settftpmode("octet");
4069b50d902SRodney W. Grimes }
4079b50d902SRodney W. Grimes 
408752fa694SWarner Losh static void
setascii(int argc __unused,char * argv[]__unused)4091b0fa6faSXin LI setascii(int argc __unused, char *argv[] __unused)
4109b50d902SRodney W. Grimes {
4119b50d902SRodney W. Grimes 
4129b50d902SRodney W. Grimes 	settftpmode("netascii");
4139b50d902SRodney W. Grimes }
4149b50d902SRodney W. Grimes 
4159b50d902SRodney W. Grimes static void
settftpmode(const char * newmode)4161b0fa6faSXin LI settftpmode(const char *newmode)
4179b50d902SRodney W. Grimes {
418752fa694SWarner Losh 
419ca2d3691SAlan Somers 	strlcpy(mode, newmode, sizeof(mode));
4209b50d902SRodney W. Grimes 	if (verbose)
4219b50d902SRodney W. Grimes 		printf("mode set to %s\n", mode);
4229b50d902SRodney W. Grimes }
4239b50d902SRodney W. Grimes 
4249b50d902SRodney W. Grimes 
4259b50d902SRodney W. Grimes /*
4269b50d902SRodney W. Grimes  * Send file(s).
4279b50d902SRodney W. Grimes  */
428752fa694SWarner Losh static void
put(int argc,char * argv[])4291b0fa6faSXin LI put(int argc, char *argv[])
4309b50d902SRodney W. Grimes {
4319b50d902SRodney W. Grimes 	int	fd;
4328049f797SMark Murray 	int	n;
4339f9544fdSDag-Erling Smørgrav 	char	*cp, *targ, *path;
434752fa694SWarner Losh 	char	line[MAXLINE];
435752fa694SWarner Losh 	struct stat sb;
4369b50d902SRodney W. Grimes 
4379b50d902SRodney W. Grimes 	if (argc < 2) {
4389b50d902SRodney W. Grimes 		strcpy(line, "send ");
4399b50d902SRodney W. Grimes 		printf("(file) ");
440ca22ff9eSJoerg Wunsch 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
441752fa694SWarner Losh 		makeargv(line);
4429b50d902SRodney W. Grimes 		argc = margc;
4439b50d902SRodney W. Grimes 		argv = margv;
4449b50d902SRodney W. Grimes 	}
4459b50d902SRodney W. Grimes 	if (argc < 2) {
4469b50d902SRodney W. Grimes 		putusage(argv[0]);
4479b50d902SRodney W. Grimes 		return;
4489b50d902SRodney W. Grimes 	}
4499b50d902SRodney W. Grimes 	targ = argv[argc - 1];
450b3608ae1SEd Schouten 	if (strrchr(argv[argc - 1], ':')) {
4518049f797SMark Murray 		char *lcp;
4529b50d902SRodney W. Grimes 
4539b50d902SRodney W. Grimes 		for (n = 1; n < argc - 1; n++)
454b3608ae1SEd Schouten 			if (strchr(argv[n], ':')) {
4559b50d902SRodney W. Grimes 				putusage(argv[0]);
4569b50d902SRodney W. Grimes 				return;
4579b50d902SRodney W. Grimes 			}
4588049f797SMark Murray 		lcp = argv[argc - 1];
459b3608ae1SEd Schouten 		targ = strrchr(lcp, ':');
4609b50d902SRodney W. Grimes 		*targ++ = 0;
4614dac6235SHajimu UMEMOTO 		if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') {
4624dac6235SHajimu UMEMOTO 			lcp[strlen(lcp) - 1] = '\0';
4634dac6235SHajimu UMEMOTO 			lcp++;
4649b50d902SRodney W. Grimes 		}
4654dac6235SHajimu UMEMOTO 		setpeer0(lcp, NULL);
4669b50d902SRodney W. Grimes 	}
4679b50d902SRodney W. Grimes 	if (!connected) {
4689b50d902SRodney W. Grimes 		printf("No target machine specified.\n");
4699b50d902SRodney W. Grimes 		return;
4709b50d902SRodney W. Grimes 	}
4719b50d902SRodney W. Grimes 	if (argc < 4) {
4729b50d902SRodney W. Grimes 		cp = argc == 2 ? tail(targ) : argv[1];
4739b50d902SRodney W. Grimes 		fd = open(cp, O_RDONLY);
4749b50d902SRodney W. Grimes 		if (fd < 0) {
475fd129a02SPhilippe Charnier 			warn("%s", cp);
4769b50d902SRodney W. Grimes 			return;
4779b50d902SRodney W. Grimes 		}
478752fa694SWarner Losh 
479ca2d3691SAlan Somers 		if (fstat(fd, &sb) < 0) {
480ca2d3691SAlan Somers 			warn("%s", cp);
481123d18b5SAlan Somers 			close(fd);
482ca2d3691SAlan Somers 			return;
483ca2d3691SAlan Somers 		}
484b15e052eSDag-Erling Smørgrav 		options_set_request(OPT_TSIZE, "%ju", (uintmax_t)sb.st_size);
485752fa694SWarner Losh 
4869b50d902SRodney W. Grimes 		if (verbose)
4879b50d902SRodney W. Grimes 			printf("putting %s to %s:%s [%s]\n",
4889b50d902SRodney W. Grimes 			    cp, hostname, targ, mode);
489*92570f67SDag-Erling Smørgrav 		if (xmitfile(peer, port, fd, targ, mode))
490*92570f67SDag-Erling Smørgrav 			txrx_error = 1;
491e4036974SAlan Somers 		close(fd);
4929b50d902SRodney W. Grimes 		return;
4939b50d902SRodney W. Grimes 	}
4949b50d902SRodney W. Grimes 				/* this assumes the target is a directory */
4959b50d902SRodney W. Grimes 				/* on a remote unix system.  hmmmm.  */
4969b50d902SRodney W. Grimes 	for (n = 1; n < argc - 1; n++) {
4979f9544fdSDag-Erling Smørgrav 		if (asprintf(&path, "%s/%s", targ, tail(argv[n])) < 0)
4989f9544fdSDag-Erling Smørgrav 			err(1, "malloc");
4999f9544fdSDag-Erling Smørgrav 
5009b50d902SRodney W. Grimes 		fd = open(argv[n], O_RDONLY);
5019b50d902SRodney W. Grimes 		if (fd < 0) {
502fd129a02SPhilippe Charnier 			warn("%s", argv[n]);
5039f9544fdSDag-Erling Smørgrav 			free(path);
5049b50d902SRodney W. Grimes 			continue;
5059b50d902SRodney W. Grimes 		}
506752fa694SWarner Losh 
507ca2d3691SAlan Somers 		if (fstat(fd, &sb) < 0) {
508ca2d3691SAlan Somers 			warn("%s", argv[n]);
5099f9544fdSDag-Erling Smørgrav 			close(fd);
5109f9544fdSDag-Erling Smørgrav 			free(path);
511ca2d3691SAlan Somers 			continue;
512ca2d3691SAlan Somers 		}
513b15e052eSDag-Erling Smørgrav 		options_set_request(OPT_TSIZE, "%ju", (uintmax_t)sb.st_size);
514752fa694SWarner Losh 
5159b50d902SRodney W. Grimes 		if (verbose)
5169b50d902SRodney W. Grimes 			printf("putting %s to %s:%s [%s]\n",
5179f9544fdSDag-Erling Smørgrav 			    argv[n], hostname, path, mode);
518*92570f67SDag-Erling Smørgrav 		if (xmitfile(peer, port, fd, path, mode) != 0)
519*92570f67SDag-Erling Smørgrav 			txrx_error = 1;
5209f9544fdSDag-Erling Smørgrav 		close(fd);
5219f9544fdSDag-Erling Smørgrav 
5229f9544fdSDag-Erling Smørgrav 		free(path);
5239b50d902SRodney W. Grimes 	}
5249b50d902SRodney W. Grimes }
5259b50d902SRodney W. Grimes 
5269b50d902SRodney W. Grimes static void
putusage(char * s)527752fa694SWarner Losh putusage(char *s)
5289b50d902SRodney W. Grimes {
529752fa694SWarner Losh 
530752fa694SWarner Losh 	printf("usage: %s file [remotename]\n", s);
531752fa694SWarner Losh 	printf("       %s file host:remotename\n", s);
532891ca8cfSSimon L. B. Nielsen 	printf("       %s file1 file2 ... fileN [[host:]remote-directory]\n", s);
5339b50d902SRodney W. Grimes }
5349b50d902SRodney W. Grimes 
5359b50d902SRodney W. Grimes /*
5369b50d902SRodney W. Grimes  * Receive file(s).
5379b50d902SRodney W. Grimes  */
538752fa694SWarner Losh static void
get(int argc,char * argv[])5391b0fa6faSXin LI get(int argc, char *argv[])
5409b50d902SRodney W. Grimes {
5419b50d902SRodney W. Grimes 	int fd;
5428049f797SMark Murray 	int n;
5438049f797SMark Murray 	char *cp;
5449b50d902SRodney W. Grimes 	char *src;
545752fa694SWarner Losh 	char	line[MAXLINE];
5469b50d902SRodney W. Grimes 
5479b50d902SRodney W. Grimes 	if (argc < 2) {
5489b50d902SRodney W. Grimes 		strcpy(line, "get ");
5499b50d902SRodney W. Grimes 		printf("(files) ");
550ca22ff9eSJoerg Wunsch 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
551752fa694SWarner Losh 		makeargv(line);
5529b50d902SRodney W. Grimes 		argc = margc;
5539b50d902SRodney W. Grimes 		argv = margv;
5549b50d902SRodney W. Grimes 	}
5559b50d902SRodney W. Grimes 	if (argc < 2) {
5569b50d902SRodney W. Grimes 		getusage(argv[0]);
5579b50d902SRodney W. Grimes 		return;
5589b50d902SRodney W. Grimes 	}
5599b50d902SRodney W. Grimes 	if (!connected) {
5609b50d902SRodney W. Grimes 		for (n = 1; n < argc ; n++)
561b3608ae1SEd Schouten 			if (strrchr(argv[n], ':') == 0) {
562752fa694SWarner Losh 				printf("No remote host specified and "
563752fa694SWarner Losh 				    "no host given for file '%s'\n", argv[n]);
5649b50d902SRodney W. Grimes 				getusage(argv[0]);
5659b50d902SRodney W. Grimes 				return;
5669b50d902SRodney W. Grimes 			}
5679b50d902SRodney W. Grimes 	}
5689b50d902SRodney W. Grimes 	for (n = 1; n < argc ; n++) {
569b3608ae1SEd Schouten 		src = strrchr(argv[n], ':');
5709b50d902SRodney W. Grimes 		if (src == NULL)
5719b50d902SRodney W. Grimes 			src = argv[n];
5729b50d902SRodney W. Grimes 		else {
5734dac6235SHajimu UMEMOTO 			char *lcp;
5749b50d902SRodney W. Grimes 
5759b50d902SRodney W. Grimes 			*src++ = 0;
5764dac6235SHajimu UMEMOTO 			lcp = argv[n];
5774dac6235SHajimu UMEMOTO 			if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') {
5784dac6235SHajimu UMEMOTO 				lcp[strlen(lcp) - 1] = '\0';
5794dac6235SHajimu UMEMOTO 				lcp++;
5809b50d902SRodney W. Grimes 			}
5814dac6235SHajimu UMEMOTO 			setpeer0(lcp, NULL);
5824dac6235SHajimu UMEMOTO 			if (!connected)
5834dac6235SHajimu UMEMOTO 				continue;
5849b50d902SRodney W. Grimes 		}
5859b50d902SRodney W. Grimes 		if (argc < 4) {
5869b50d902SRodney W. Grimes 			cp = argc == 3 ? argv[2] : tail(src);
5879b50d902SRodney W. Grimes 			fd = creat(cp, 0644);
5889b50d902SRodney W. Grimes 			if (fd < 0) {
589fd129a02SPhilippe Charnier 				warn("%s", cp);
5909b50d902SRodney W. Grimes 				return;
5919b50d902SRodney W. Grimes 			}
5929b50d902SRodney W. Grimes 			if (verbose)
5939b50d902SRodney W. Grimes 				printf("getting from %s:%s to %s [%s]\n",
5949b50d902SRodney W. Grimes 				    hostname, src, cp, mode);
595*92570f67SDag-Erling Smørgrav 			if (recvfile(peer, port, fd, src, mode) != 0)
596*92570f67SDag-Erling Smørgrav 				txrx_error = 1;
5979b50d902SRodney W. Grimes 			break;
5989b50d902SRodney W. Grimes 		}
5999b50d902SRodney W. Grimes 		cp = tail(src);         /* new .. jdg */
6009b50d902SRodney W. Grimes 		fd = creat(cp, 0644);
6019b50d902SRodney W. Grimes 		if (fd < 0) {
602fd129a02SPhilippe Charnier 			warn("%s", cp);
6039b50d902SRodney W. Grimes 			continue;
6049b50d902SRodney W. Grimes 		}
6059b50d902SRodney W. Grimes 		if (verbose)
6069b50d902SRodney W. Grimes 			printf("getting from %s:%s to %s [%s]\n",
6079b50d902SRodney W. Grimes 			    hostname, src, cp, mode);
608*92570f67SDag-Erling Smørgrav 		if (recvfile(peer, port, fd, src, mode) != 0)
609*92570f67SDag-Erling Smørgrav 			txrx_error = 1;
6109b50d902SRodney W. Grimes 	}
6119b50d902SRodney W. Grimes }
6129b50d902SRodney W. Grimes 
6139b50d902SRodney W. Grimes static void
getusage(char * s)614752fa694SWarner Losh getusage(char *s)
6159b50d902SRodney W. Grimes {
616752fa694SWarner Losh 
617752fa694SWarner Losh 	printf("usage: %s file [localname]\n", s);
618752fa694SWarner Losh 	printf("       %s [host:]file [localname]\n", s);
619891ca8cfSSimon L. B. Nielsen 	printf("       %s [host1:]file1 [host2:]file2 ... [hostN:]fileN\n", s);
6209b50d902SRodney W. Grimes }
6219b50d902SRodney W. Grimes 
622752fa694SWarner Losh static void
settimeoutpacket(int argc,char * argv[])623752fa694SWarner Losh settimeoutpacket(int argc, char *argv[])
6249b50d902SRodney W. Grimes {
6259b50d902SRodney W. Grimes 	int t;
626752fa694SWarner Losh 	char	line[MAXLINE];
6279b50d902SRodney W. Grimes 
6289b50d902SRodney W. Grimes 	if (argc < 2) {
629752fa694SWarner Losh 		strcpy(line, "Packet timeout ");
6309b50d902SRodney W. Grimes 		printf("(value) ");
631ca22ff9eSJoerg Wunsch 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
632752fa694SWarner Losh 		makeargv(line);
6339b50d902SRodney W. Grimes 		argc = margc;
6349b50d902SRodney W. Grimes 		argv = margv;
6359b50d902SRodney W. Grimes 	}
6369b50d902SRodney W. Grimes 	if (argc != 2) {
6379b50d902SRodney W. Grimes 		printf("usage: %s value\n", argv[0]);
6389b50d902SRodney W. Grimes 		return;
6399b50d902SRodney W. Grimes 	}
6409b50d902SRodney W. Grimes 	t = atoi(argv[1]);
641752fa694SWarner Losh 	if (t < 0) {
6429b50d902SRodney W. Grimes 		printf("%s: bad value\n", argv[1]);
643752fa694SWarner Losh 		return;
6449b50d902SRodney W. Grimes 	}
6459b50d902SRodney W. Grimes 
646752fa694SWarner Losh 	settimeouts(t, timeoutnetwork, maxtimeouts);
647752fa694SWarner Losh }
6489b50d902SRodney W. Grimes 
649752fa694SWarner Losh static void
settimeoutnetwork(int argc,char * argv[])650752fa694SWarner Losh settimeoutnetwork(int argc, char *argv[])
6519b50d902SRodney W. Grimes {
6529b50d902SRodney W. Grimes 	int t;
653752fa694SWarner Losh 	char	line[MAXLINE];
6549b50d902SRodney W. Grimes 
6559b50d902SRodney W. Grimes 	if (argc < 2) {
656752fa694SWarner Losh 		strcpy(line, "Network timeout ");
6579b50d902SRodney W. Grimes 		printf("(value) ");
658ca22ff9eSJoerg Wunsch 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
659752fa694SWarner Losh 		makeargv(line);
6609b50d902SRodney W. Grimes 		argc = margc;
6619b50d902SRodney W. Grimes 		argv = margv;
6629b50d902SRodney W. Grimes 	}
6639b50d902SRodney W. Grimes 	if (argc != 2) {
6649b50d902SRodney W. Grimes 		printf("usage: %s value\n", argv[0]);
6659b50d902SRodney W. Grimes 		return;
6669b50d902SRodney W. Grimes 	}
6679b50d902SRodney W. Grimes 	t = atoi(argv[1]);
668752fa694SWarner Losh 	if (t < 0) {
6699b50d902SRodney W. Grimes 		printf("%s: bad value\n", argv[1]);
670752fa694SWarner Losh 		return;
6719b50d902SRodney W. Grimes 	}
6729b50d902SRodney W. Grimes 
673752fa694SWarner Losh 	settimeouts(timeoutpacket, t, maxtimeouts);
674752fa694SWarner Losh }
675752fa694SWarner Losh 
676752fa694SWarner Losh static void
showstatus(int argc __unused,char * argv[]__unused)677752fa694SWarner Losh showstatus(int argc __unused, char *argv[] __unused)
6789b50d902SRodney W. Grimes {
679752fa694SWarner Losh 
680752fa694SWarner Losh 	printf("Remote host: %s\n",
681752fa694SWarner Losh 	    connected ? hostname : "none specified yet");
682752fa694SWarner Losh 	printf("RFC2347 Options support: %s\n",
683752fa694SWarner Losh 	    options_rfc_enabled ? "enabled" : "disabled");
684752fa694SWarner Losh 	printf("Non-RFC defined options support: %s\n",
685752fa694SWarner Losh 	    options_extra_enabled ? "enabled" : "disabled");
686752fa694SWarner Losh 	printf("Mode: %s\n", mode);
687752fa694SWarner Losh 	printf("Verbose: %s\n", verbose ? "on" : "off");
688752fa694SWarner Losh 	printf("Debug: %s\n", debug_show(debug));
689752fa694SWarner Losh 	printf("Artificial packetloss: %d in 100 packets\n",
690752fa694SWarner Losh 	    packetdroppercentage);
691752fa694SWarner Losh 	printf("Segment size: %d bytes\n", segsize);
692752fa694SWarner Losh 	printf("Network timeout: %d seconds\n", timeoutpacket);
693752fa694SWarner Losh 	printf("Maximum network timeout: %d seconds\n", timeoutnetwork);
694752fa694SWarner Losh 	printf("Maximum timeouts: %d \n", maxtimeouts);
6959b50d902SRodney W. Grimes }
6969b50d902SRodney W. Grimes 
697752fa694SWarner Losh static void
intr(int dummy __unused)6981b0fa6faSXin LI intr(int dummy __unused)
6999b50d902SRodney W. Grimes {
7009b50d902SRodney W. Grimes 
7019b50d902SRodney W. Grimes 	signal(SIGALRM, SIG_IGN);
7029b50d902SRodney W. Grimes 	alarm(0);
7039b50d902SRodney W. Grimes 	longjmp(toplevel, -1);
7049b50d902SRodney W. Grimes }
7059b50d902SRodney W. Grimes 
706752fa694SWarner Losh static char *
tail(char * filename)7071b0fa6faSXin LI tail(char *filename)
7089b50d902SRodney W. Grimes {
7098049f797SMark Murray 	char *s;
7109b50d902SRodney W. Grimes 
7119b50d902SRodney W. Grimes 	while (*filename) {
712b3608ae1SEd Schouten 		s = strrchr(filename, '/');
7139b50d902SRodney W. Grimes 		if (s == NULL)
7149b50d902SRodney W. Grimes 			break;
7159b50d902SRodney W. Grimes 		if (s[1])
7169b50d902SRodney W. Grimes 			return (s + 1);
7179b50d902SRodney W. Grimes 		*s = '\0';
7189b50d902SRodney W. Grimes 	}
7199b50d902SRodney W. Grimes 	return (filename);
7209b50d902SRodney W. Grimes }
7219b50d902SRodney W. Grimes 
722c33ed450SMatthew N. Dodd static const char *
command_prompt(void)72304ebad38SMarius Strobl command_prompt(void)
7248979160aSBruce Evans {
7258979160aSBruce Evans 
726c33ed450SMatthew N. Dodd 	return ("tftp> ");
727c33ed450SMatthew N. Dodd }
728c33ed450SMatthew N. Dodd 
7299b50d902SRodney W. Grimes /*
7309b50d902SRodney W. Grimes  * Command parser.
7319b50d902SRodney W. Grimes  */
732eaa86f9dSBruce Evans static void
command(bool interactive,EditLine * el,History * hist,HistEvent * hep)73349fb0615SConrad Meyer command(bool interactive, EditLine *el, History *hist, HistEvent *hep)
7349b50d902SRodney W. Grimes {
735b9fabe99SDag-Erling Smørgrav 	const struct cmd *c;
736c33ed450SMatthew N. Dodd 	const char *bp;
7378979160aSBruce Evans 	char *cp;
73849fb0615SConrad Meyer 	int len, num;
739752fa694SWarner Losh 	char	line[MAXLINE];
740c33ed450SMatthew N. Dodd 
7419b50d902SRodney W. Grimes 	for (;;) {
74249fb0615SConrad Meyer 		if (interactive) {
743c33ed450SMatthew N. Dodd 			if ((bp = el_gets(el, &num)) == NULL || num == 0)
744c33ed450SMatthew N. Dodd 				exit(0);
745a9a46bd9SMarcelo Araujo 			len = MIN(MAXLINE, num);
746c33ed450SMatthew N. Dodd 			memcpy(line, bp, len);
747123d18b5SAlan Somers 			line[len - 1] = '\0';
74849fb0615SConrad Meyer 			history(hist, hep, H_ENTER, bp);
749c33ed450SMatthew N. Dodd 		} else {
750752fa694SWarner Losh 			line[0] = 0;
751a3a2bf4bSKevin Lo 			if (fgets(line, sizeof line , stdin) == NULL) {
7529b50d902SRodney W. Grimes 				if (feof(stdin)) {
75373f899caSBrian S. Dean 					exit(txrx_error);
7549b50d902SRodney W. Grimes 				} else {
7559b50d902SRodney W. Grimes 					continue;
7569b50d902SRodney W. Grimes 				}
7579b50d902SRodney W. Grimes 			}
758c33ed450SMatthew N. Dodd 		}
759ca22ff9eSJoerg Wunsch 		if ((cp = strchr(line, '\n')))
760ca22ff9eSJoerg Wunsch 			*cp = '\0';
7619b50d902SRodney W. Grimes 		if (line[0] == 0)
7629b50d902SRodney W. Grimes 			continue;
763752fa694SWarner Losh 		makeargv(line);
7649b50d902SRodney W. Grimes 		if (margc == 0)
7659b50d902SRodney W. Grimes 			continue;
7669b50d902SRodney W. Grimes 		c = getcmd(margv[0]);
7679b50d902SRodney W. Grimes 		if (c == (struct cmd *)-1) {
7689b50d902SRodney W. Grimes 			printf("?Ambiguous command\n");
7699b50d902SRodney W. Grimes 			continue;
7709b50d902SRodney W. Grimes 		}
771b4b3d271SMarcelo Araujo 		if (c == NULL) {
7729b50d902SRodney W. Grimes 			printf("?Invalid command\n");
7739b50d902SRodney W. Grimes 			continue;
7749b50d902SRodney W. Grimes 		}
7759b50d902SRodney W. Grimes 		(*c->handler)(margc, margv);
7769b50d902SRodney W. Grimes 	}
7779b50d902SRodney W. Grimes }
7789b50d902SRodney W. Grimes 
779b9fabe99SDag-Erling Smørgrav static const struct cmd *
getcmd(const char * name)780b9fabe99SDag-Erling Smørgrav getcmd(const char *name)
7819b50d902SRodney W. Grimes {
7828049f797SMark Murray 	const char *p, *q;
783b9fabe99SDag-Erling Smørgrav 	const struct cmd *c, *found;
784b9fabe99SDag-Erling Smørgrav 	ptrdiff_t longest;
785b9fabe99SDag-Erling Smørgrav 	int nmatches;
7869b50d902SRodney W. Grimes 
7879b50d902SRodney W. Grimes 	longest = 0;
7889b50d902SRodney W. Grimes 	nmatches = 0;
7899b50d902SRodney W. Grimes 	found = 0;
7909b50d902SRodney W. Grimes 	for (c = cmdtab; (p = c->name) != NULL; c++) {
7919b50d902SRodney W. Grimes 		for (q = name; *q == *p++; q++)
792b9fabe99SDag-Erling Smørgrav 			if (*q == '\0')		/* exact match? */
7939b50d902SRodney W. Grimes 				return (c);
794b9fabe99SDag-Erling Smørgrav 		if (*q == '\0') {		/* the name was a prefix */
7959b50d902SRodney W. Grimes 			if (q - name > longest) {
7969b50d902SRodney W. Grimes 				longest = q - name;
7979b50d902SRodney W. Grimes 				nmatches = 1;
7989b50d902SRodney W. Grimes 				found = c;
7999b50d902SRodney W. Grimes 			} else if (q - name == longest)
8009b50d902SRodney W. Grimes 				nmatches++;
8019b50d902SRodney W. Grimes 		}
8029b50d902SRodney W. Grimes 	}
8039b50d902SRodney W. Grimes 	if (nmatches > 1)
8049b50d902SRodney W. Grimes 		return ((struct cmd *)-1);
8059b50d902SRodney W. Grimes 	return (found);
8069b50d902SRodney W. Grimes }
8079b50d902SRodney W. Grimes 
8089b50d902SRodney W. Grimes /*
8099b50d902SRodney W. Grimes  * Slice a string up into argc/argv.
8109b50d902SRodney W. Grimes  */
8119b50d902SRodney W. Grimes static void
makeargv(char * line)812752fa694SWarner Losh makeargv(char *line)
8139b50d902SRodney W. Grimes {
8148049f797SMark Murray 	char *cp;
8158049f797SMark Murray 	char **argp = margv;
8169b50d902SRodney W. Grimes 
8179b50d902SRodney W. Grimes 	margc = 0;
818752fa694SWarner Losh 	if ((cp = strchr(line, '\n')) != NULL)
819ca22ff9eSJoerg Wunsch 		*cp = '\0';
820752fa694SWarner Losh 	for (cp = line; margc < MAX_MARGV - 1 && *cp != '\0';) {
8219b50d902SRodney W. Grimes 		while (isspace(*cp))
8229b50d902SRodney W. Grimes 			cp++;
8239b50d902SRodney W. Grimes 		if (*cp == '\0')
8249b50d902SRodney W. Grimes 			break;
8259b50d902SRodney W. Grimes 		*argp++ = cp;
8269b50d902SRodney W. Grimes 		margc += 1;
8279b50d902SRodney W. Grimes 		while (*cp != '\0' && !isspace(*cp))
8289b50d902SRodney W. Grimes 			cp++;
8299b50d902SRodney W. Grimes 		if (*cp == '\0')
8309b50d902SRodney W. Grimes 			break;
8319b50d902SRodney W. Grimes 		*cp++ = '\0';
8329b50d902SRodney W. Grimes 	}
8339b50d902SRodney W. Grimes 	*argp++ = 0;
8349b50d902SRodney W. Grimes }
8359b50d902SRodney W. Grimes 
836752fa694SWarner Losh static void
quit(int argc __unused,char * argv[]__unused)8371b0fa6faSXin LI quit(int argc __unused, char *argv[] __unused)
8389b50d902SRodney W. Grimes {
839752fa694SWarner Losh 
84073f899caSBrian S. Dean 	exit(txrx_error);
8419b50d902SRodney W. Grimes }
8429b50d902SRodney W. Grimes 
8439b50d902SRodney W. Grimes /*
8449b50d902SRodney W. Grimes  * Help command.
8459b50d902SRodney W. Grimes  */
846752fa694SWarner Losh static void
help(int argc,char * argv[])8471b0fa6faSXin LI help(int argc, char *argv[])
8489b50d902SRodney W. Grimes {
849b9fabe99SDag-Erling Smørgrav 	const struct cmd *c;
8509b50d902SRodney W. Grimes 
8519b50d902SRodney W. Grimes 	if (argc == 1) {
8529b50d902SRodney W. Grimes 		printf("Commands may be abbreviated.  Commands are:\n\n");
8539b50d902SRodney W. Grimes 		for (c = cmdtab; c->name; c++)
8549b50d902SRodney W. Grimes 			printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help);
855752fa694SWarner Losh 
856752fa694SWarner Losh 		printf("\n[-] : You shouldn't use these ones anymore.\n");
857f4b7e64dSCraig Rodrigues 		printf("[*] : RFC2347 options support required.\n");
858f4b7e64dSCraig Rodrigues 		printf("[**] : Non-standard RFC2347 option.\n");
8599b50d902SRodney W. Grimes 		return;
8609b50d902SRodney W. Grimes 	}
8619b50d902SRodney W. Grimes 	while (--argc > 0) {
8628049f797SMark Murray 		char *arg;
8639b50d902SRodney W. Grimes 		arg = *++argv;
8649b50d902SRodney W. Grimes 		c = getcmd(arg);
8659b50d902SRodney W. Grimes 		if (c == (struct cmd *)-1)
866752fa694SWarner Losh 			printf("?Ambiguous help command: %s\n", arg);
8679b50d902SRodney W. Grimes 		else if (c == (struct cmd *)0)
868752fa694SWarner Losh 			printf("?Invalid help command: %s\n", arg);
8699b50d902SRodney W. Grimes 		else
8709b50d902SRodney W. Grimes 			printf("%s\n", c->help);
8719b50d902SRodney W. Grimes 	}
8729b50d902SRodney W. Grimes }
8739b50d902SRodney W. Grimes 
874752fa694SWarner Losh static void
setverbose(int argc __unused,char * argv[]__unused)875752fa694SWarner Losh setverbose(int argc __unused, char *argv[] __unused)
8769b50d902SRodney W. Grimes {
8779b50d902SRodney W. Grimes 
8789b50d902SRodney W. Grimes 	verbose = !verbose;
8799b50d902SRodney W. Grimes 	printf("Verbose mode %s.\n", verbose ? "on" : "off");
8809b50d902SRodney W. Grimes }
881752fa694SWarner Losh 
882752fa694SWarner Losh static void
setoptions(int argc,char * argv[])883752fa694SWarner Losh setoptions(int argc, char *argv[])
884752fa694SWarner Losh {
885752fa694SWarner Losh 
886752fa694SWarner Losh 	if (argc == 2) {
887752fa694SWarner Losh 		if (strcasecmp(argv[1], "enable") == 0 ||
888752fa694SWarner Losh 		    strcasecmp(argv[1], "on") == 0) {
889752fa694SWarner Losh 			options_extra_enabled = 1;
890752fa694SWarner Losh 			options_rfc_enabled = 1;
891752fa694SWarner Losh 		}
892752fa694SWarner Losh 		if (strcasecmp(argv[1], "disable") == 0 ||
893752fa694SWarner Losh 		    strcasecmp(argv[1], "off") == 0) {
894752fa694SWarner Losh 			options_extra_enabled = 0;
895752fa694SWarner Losh 			options_rfc_enabled = 0;
896752fa694SWarner Losh 		}
897752fa694SWarner Losh 		if (strcasecmp(argv[1], "extra") == 0)
898752fa694SWarner Losh 			options_extra_enabled = !options_extra_enabled;
899752fa694SWarner Losh 	}
900752fa694SWarner Losh 	printf("Support for RFC2347 style options are now %s.\n",
901752fa694SWarner Losh 	    options_rfc_enabled ? "enabled" : "disabled");
902752fa694SWarner Losh 	printf("Support for non-RFC defined options are now %s.\n",
903752fa694SWarner Losh 	    options_extra_enabled ? "enabled" : "disabled");
904752fa694SWarner Losh 
905752fa694SWarner Losh 	printf("\nThe following options are available:\n"
906752fa694SWarner Losh 	    "\toptions on	: enable support for RFC2347 style options\n"
907752fa694SWarner Losh 	    "\toptions off	: disable support for RFC2347 style options\n"
908752fa694SWarner Losh 	    "\toptions extra	: toggle support for non-RFC defined options\n"
909752fa694SWarner Losh 	);
910752fa694SWarner Losh }
911752fa694SWarner Losh 
912752fa694SWarner Losh static void
setrollover(int argc,char * argv[])913752fa694SWarner Losh setrollover(int argc, char *argv[])
914752fa694SWarner Losh {
915752fa694SWarner Losh 
916752fa694SWarner Losh 	if (argc == 2) {
917752fa694SWarner Losh 		if (strcasecmp(argv[1], "never") == 0 ||
918752fa694SWarner Losh 		    strcasecmp(argv[1], "none") == 0) {
919b15e052eSDag-Erling Smørgrav 			options_set_request(OPT_ROLLOVER, NULL);
920752fa694SWarner Losh 		}
921752fa694SWarner Losh 		if (strcasecmp(argv[1], "1") == 0) {
922b15e052eSDag-Erling Smørgrav 			options_set_request(OPT_ROLLOVER, "1");
923752fa694SWarner Losh 		}
924752fa694SWarner Losh 		if (strcasecmp(argv[1], "0") == 0) {
925b15e052eSDag-Erling Smørgrav 			options_set_request(OPT_ROLLOVER, "0");
926752fa694SWarner Losh 		}
927752fa694SWarner Losh 	}
928752fa694SWarner Losh 	printf("Support for the rollover options is %s.\n",
929752fa694SWarner Losh 	    options[OPT_ROLLOVER].o_request != NULL ? "enabled" : "disabled");
930752fa694SWarner Losh 	if (options[OPT_ROLLOVER].o_request != NULL)
931752fa694SWarner Losh 		printf("Block rollover will be to block %s.\n",
932752fa694SWarner Losh 		    options[OPT_ROLLOVER].o_request);
933752fa694SWarner Losh 
934752fa694SWarner Losh 
935752fa694SWarner Losh 	printf("\nThe following rollover options are available:\n"
936752fa694SWarner Losh 	    "\trollover 0	: rollover to block zero (default)\n"
937752fa694SWarner Losh 	    "\trollover 1	: rollover to block one\n"
938752fa694SWarner Losh 	    "\trollover never	: do not support the rollover option\n"
939752fa694SWarner Losh 	    "\trollover none	: do not support the rollover option\n"
940752fa694SWarner Losh 	);
941752fa694SWarner Losh }
942752fa694SWarner Losh 
943752fa694SWarner Losh static void
setdebug(int argc,char * argv[])944752fa694SWarner Losh setdebug(int argc, char *argv[])
945752fa694SWarner Losh {
946752fa694SWarner Losh 	int i;
947752fa694SWarner Losh 
948752fa694SWarner Losh 	if (argc != 1) {
949752fa694SWarner Losh 		i = 1;
950752fa694SWarner Losh 		while (i < argc)
951752fa694SWarner Losh 			debug ^= debug_find(argv[i++]);
952752fa694SWarner Losh 	}
953752fa694SWarner Losh 	printf("The following debugging is enabled: %s\n", debug_show(debug));
954752fa694SWarner Losh 
955752fa694SWarner Losh 	printf("\nThe following debugs are available:\n");
956752fa694SWarner Losh 	i = 0;
957752fa694SWarner Losh 	while (debugs[i].name != NULL) {
958752fa694SWarner Losh 		printf("\t%s\t%s\n", debugs[i].name, debugs[i].desc);
959752fa694SWarner Losh 		i++;
960752fa694SWarner Losh 	}
961752fa694SWarner Losh }
962752fa694SWarner Losh 
963752fa694SWarner Losh static void
setblocksize(int argc,char * argv[])964752fa694SWarner Losh setblocksize(int argc, char *argv[])
965752fa694SWarner Losh {
966752fa694SWarner Losh 
967752fa694SWarner Losh 	if (!options_rfc_enabled)
968752fa694SWarner Losh 		printf("RFC2347 style options are not enabled "
969f471745aSWarner Losh 		    "(but proceeding anyway)\n");
970752fa694SWarner Losh 
971752fa694SWarner Losh 	if (argc != 1) {
972752fa694SWarner Losh 		int size = atoi(argv[1]);
973752fa694SWarner Losh 		size_t max;
97404ebad38SMarius Strobl 		u_long maxdgram;
975752fa694SWarner Losh 
97604ebad38SMarius Strobl 		max = sizeof(maxdgram);
977752fa694SWarner Losh 		if (sysctlbyname("net.inet.udp.maxdgram",
97804ebad38SMarius Strobl 			&maxdgram, &max, NULL, 0) < 0) {
979752fa694SWarner Losh 			perror("sysctl: net.inet.udp.maxdgram");
980752fa694SWarner Losh 			return;
981752fa694SWarner Losh 		}
982752fa694SWarner Losh 
983752fa694SWarner Losh 		if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) {
984752fa694SWarner Losh 			printf("Blocksize should be between %d and %d bytes.\n",
985752fa694SWarner Losh 				BLKSIZE_MIN, BLKSIZE_MAX);
986752fa694SWarner Losh 			return;
98704ebad38SMarius Strobl 		} else if (size > (int)maxdgram - 4) {
98804ebad38SMarius Strobl 			printf("Blocksize can't be bigger than %ld bytes due "
989752fa694SWarner Losh 			    "to the net.inet.udp.maxdgram sysctl limitation.\n",
99004ebad38SMarius Strobl 			    maxdgram - 4);
991b15e052eSDag-Erling Smørgrav 			options_set_request(OPT_BLKSIZE, "%ld", maxdgram - 4);
992752fa694SWarner Losh 		} else {
993b15e052eSDag-Erling Smørgrav 			options_set_request(OPT_BLKSIZE, "%d", size);
994752fa694SWarner Losh 		}
995752fa694SWarner Losh 	}
996752fa694SWarner Losh 	printf("Blocksize is now %s bytes.\n", options[OPT_BLKSIZE].o_request);
997752fa694SWarner Losh }
998752fa694SWarner Losh 
999752fa694SWarner Losh static void
setblocksize2(int argc,char * argv[])1000752fa694SWarner Losh setblocksize2(int argc, char *argv[])
1001752fa694SWarner Losh {
1002752fa694SWarner Losh 
1003752fa694SWarner Losh 	if (!options_rfc_enabled || !options_extra_enabled)
1004752fa694SWarner Losh 		printf(
1005752fa694SWarner Losh 		    "RFC2347 style or non-RFC defined options are not enabled "
1006f471745aSWarner Losh 		    "(but proceeding anyway)\n");
1007752fa694SWarner Losh 
1008752fa694SWarner Losh 	if (argc != 1) {
1009752fa694SWarner Losh 		int size = atoi(argv[1]);
1010752fa694SWarner Losh 		int i;
1011752fa694SWarner Losh 		size_t max;
101204ebad38SMarius Strobl 		u_long maxdgram;
1013752fa694SWarner Losh 
1014752fa694SWarner Losh 		int sizes[] = {
1015752fa694SWarner Losh 			8, 16, 32, 64, 128, 256, 512, 1024,
1016752fa694SWarner Losh 			2048, 4096, 8192, 16384, 32768, 0
1017752fa694SWarner Losh 		};
1018752fa694SWarner Losh 
101904ebad38SMarius Strobl 		max = sizeof(maxdgram);
1020752fa694SWarner Losh 		if (sysctlbyname("net.inet.udp.maxdgram",
102104ebad38SMarius Strobl 			&maxdgram, &max, NULL, 0) < 0) {
1022752fa694SWarner Losh 			perror("sysctl: net.inet.udp.maxdgram");
1023752fa694SWarner Losh 			return;
1024752fa694SWarner Losh 		}
1025752fa694SWarner Losh 
1026752fa694SWarner Losh 		for (i = 0; sizes[i] != 0; i++) {
1027752fa694SWarner Losh 			if (sizes[i] == size) break;
1028752fa694SWarner Losh 		}
1029752fa694SWarner Losh 		if (sizes[i] == 0) {
1030752fa694SWarner Losh 			printf("Blocksize2 should be a power of two between "
1031752fa694SWarner Losh 			    "8 and 32768.\n");
1032752fa694SWarner Losh 			return;
1033752fa694SWarner Losh 		}
1034752fa694SWarner Losh 
1035752fa694SWarner Losh 		if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) {
1036752fa694SWarner Losh 			printf("Blocksize2 should be between "
1037752fa694SWarner Losh 			    "%d and %d bytes.\n", BLKSIZE_MIN, BLKSIZE_MAX);
1038752fa694SWarner Losh 			return;
103904ebad38SMarius Strobl 		} else if (size > (int)maxdgram - 4) {
104004ebad38SMarius Strobl 			printf("Blocksize2 can't be bigger than %ld bytes due "
1041752fa694SWarner Losh 			    "to the net.inet.udp.maxdgram sysctl limitation.\n",
104204ebad38SMarius Strobl 			    maxdgram - 4);
1043752fa694SWarner Losh 			for (i = 0; sizes[i+1] != 0; i++) {
104404ebad38SMarius Strobl 				if ((int)maxdgram < sizes[i+1]) break;
1045752fa694SWarner Losh 			}
1046b15e052eSDag-Erling Smørgrav 			options_set_request(OPT_BLKSIZE2, "%d", sizes[i]);
1047752fa694SWarner Losh 		} else {
1048b15e052eSDag-Erling Smørgrav 			options_set_request(OPT_BLKSIZE2, "%d", size);
1049752fa694SWarner Losh 		}
1050752fa694SWarner Losh 	}
1051752fa694SWarner Losh 	printf("Blocksize2 is now %s bytes.\n",
1052752fa694SWarner Losh 	    options[OPT_BLKSIZE2].o_request);
1053752fa694SWarner Losh }
1054752fa694SWarner Losh 
1055752fa694SWarner Losh static void
setpacketdrop(int argc,char * argv[])1056752fa694SWarner Losh setpacketdrop(int argc, char *argv[])
1057752fa694SWarner Losh {
1058752fa694SWarner Losh 
1059752fa694SWarner Losh 	if (argc != 1)
1060752fa694SWarner Losh 		packetdroppercentage = atoi(argv[1]);
1061752fa694SWarner Losh 
1062752fa694SWarner Losh 	printf("Randomly %d in 100 packets will be dropped\n",
1063752fa694SWarner Losh 	    packetdroppercentage);
1064752fa694SWarner Losh }
1065fdf929ffSJohn Baldwin 
1066fdf929ffSJohn Baldwin static void
setwindowsize(int argc,char * argv[])1067fdf929ffSJohn Baldwin setwindowsize(int argc, char *argv[])
1068fdf929ffSJohn Baldwin {
1069fdf929ffSJohn Baldwin 
1070fdf929ffSJohn Baldwin 	if (!options_rfc_enabled)
1071fdf929ffSJohn Baldwin 		printf("RFC2347 style options are not enabled "
1072fdf929ffSJohn Baldwin 		    "(but proceeding anyway)\n");
1073fdf929ffSJohn Baldwin 
1074fdf929ffSJohn Baldwin 	if (argc != 1) {
1075fdf929ffSJohn Baldwin 		int size = atoi(argv[1]);
1076fdf929ffSJohn Baldwin 
1077fdf929ffSJohn Baldwin 		if (size < WINDOWSIZE_MIN || size > WINDOWSIZE_MAX) {
1078fdf929ffSJohn Baldwin 			printf("Windowsize should be between %d and %d "
1079fdf929ffSJohn Baldwin 			    "blocks.\n", WINDOWSIZE_MIN, WINDOWSIZE_MAX);
1080fdf929ffSJohn Baldwin 			return;
1081fdf929ffSJohn Baldwin 		} else {
1082b15e052eSDag-Erling Smørgrav 			options_set_request(OPT_WINDOWSIZE, "%d", size);
1083fdf929ffSJohn Baldwin 		}
1084fdf929ffSJohn Baldwin 	}
1085fdf929ffSJohn Baldwin 	printf("Windowsize is now %s blocks.\n",
1086fdf929ffSJohn Baldwin 	    options[OPT_WINDOWSIZE].o_request);
1087fdf929ffSJohn Baldwin }
1088