xref: /freebsd/usr.bin/tftp/main.c (revision f4b7e64d7ae3dac903c90b56f1844d307fafd924)
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  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
31fd129a02SPhilippe Charnier static const char copyright[] =
329b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\
339b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
348049f797SMark Murray #endif
359b50d902SRodney W. Grimes 
368f4c8256SDavid Malone #if 0
379b50d902SRodney W. Grimes #ifndef lint
388f4c8256SDavid Malone static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
39fd129a02SPhilippe Charnier #endif
408f4c8256SDavid Malone #endif
418f4c8256SDavid Malone 
428f4c8256SDavid Malone #include <sys/cdefs.h>
438f4c8256SDavid Malone __FBSDID("$FreeBSD$");
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes /*
489b50d902SRodney W. Grimes  * TFTP User Program -- Command Interface.
499b50d902SRodney W. Grimes  */
50fd129a02SPhilippe Charnier #include <sys/param.h>
519b50d902SRodney W. Grimes #include <sys/types.h>
529b50d902SRodney W. Grimes #include <sys/socket.h>
53752fa694SWarner Losh #include <sys/sysctl.h>
549b50d902SRodney W. Grimes #include <sys/file.h>
55a716ad66SWarner Losh #include <sys/param.h>
56752fa694SWarner Losh #include <sys/stat.h>
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes #include <netinet/in.h>
599b50d902SRodney W. Grimes #include <arpa/inet.h>
60752fa694SWarner Losh #include <arpa/tftp.h>
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes #include <ctype.h>
63fd129a02SPhilippe Charnier #include <err.h>
648979160aSBruce Evans #include <histedit.h>
659b50d902SRodney W. Grimes #include <netdb.h>
669b50d902SRodney W. Grimes #include <setjmp.h>
679b50d902SRodney W. Grimes #include <signal.h>
689b50d902SRodney W. Grimes #include <stdio.h>
699b50d902SRodney W. Grimes #include <stdlib.h>
709b50d902SRodney W. Grimes #include <string.h>
719b50d902SRodney W. Grimes #include <unistd.h>
729b50d902SRodney W. Grimes 
73752fa694SWarner Losh #include "tftp-utils.h"
74752fa694SWarner Losh #include "tftp-io.h"
75752fa694SWarner Losh #include "tftp-options.h"
76752fa694SWarner Losh #include "tftp.h"
779b50d902SRodney W. Grimes 
78c33ed450SMatthew N. Dodd #define	MAXLINE		200
798979160aSBruce Evans #define	TIMEOUT		5		/* secs between rexmt's */
80c33ed450SMatthew N. Dodd 
81752fa694SWarner Losh static struct	sockaddr_storage peeraddr;
82752fa694SWarner Losh static int	connected;
83752fa694SWarner Losh static char	mode[32];
849b50d902SRodney W. Grimes jmp_buf		toplevel;
8573f899caSBrian S. Dean volatile int	txrx_error;
86752fa694SWarner Losh static int	peer;
879b50d902SRodney W. Grimes 
88752fa694SWarner Losh #define	MAX_MARGV	20
89752fa694SWarner Losh static int	margc;
90752fa694SWarner Losh static char	*margv[MAX_MARGV];
91752fa694SWarner Losh 
92752fa694SWarner Losh int		verbose;
93752fa694SWarner Losh char		*port = NULL;
94752fa694SWarner Losh 
95752fa694SWarner Losh static void	get(int, char **);
96752fa694SWarner Losh static void	help(int, char **);
97752fa694SWarner Losh static void	intr(int);
98752fa694SWarner Losh static void	modecmd(int, char **);
99752fa694SWarner Losh static void	put(int, char **);
100752fa694SWarner Losh static void	quit(int, char **);
101752fa694SWarner Losh static void	setascii(int, char **);
102752fa694SWarner Losh static void	setbinary(int, char **);
103752fa694SWarner Losh static void	setpeer0(char *, const char *);
104752fa694SWarner Losh static void	setpeer(int, char **);
105752fa694SWarner Losh static void	settimeoutpacket(int, char **);
106752fa694SWarner Losh static void	settimeoutnetwork(int, char **);
107752fa694SWarner Losh static void	setdebug(int, char **);
108752fa694SWarner Losh static void	setverbose(int, char **);
109752fa694SWarner Losh static void	showstatus(int, char **);
110752fa694SWarner Losh static void	setblocksize(int, char **);
111752fa694SWarner Losh static void	setblocksize2(int, char **);
112752fa694SWarner Losh static void	setoptions(int, char **);
113752fa694SWarner Losh static void	setrollover(int, char **);
114752fa694SWarner Losh static void	setpacketdrop(int, char **);
1159b50d902SRodney W. Grimes 
1163f330d7dSWarner Losh static void command(void) __dead2;
1173f330d7dSWarner Losh static const char *command_prompt(void);
1189b50d902SRodney W. Grimes 
119752fa694SWarner Losh static void urihandling(char *URI);
120752fa694SWarner Losh static void getusage(char *);
121752fa694SWarner Losh static void makeargv(char *line);
122752fa694SWarner Losh static void putusage(char *);
1233f330d7dSWarner Losh static void settftpmode(const char *);
1248049f797SMark Murray 
125752fa694SWarner Losh static char	*tail(char *);
126752fa694SWarner Losh static struct	cmd *getcmd(char *);
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes #define HELPINDENT (sizeof("connect"))
1299b50d902SRodney W. Grimes 
1309b50d902SRodney W. Grimes struct cmd {
1318049f797SMark Murray 	const char	*name;
1323f330d7dSWarner Losh 	void	(*handler)(int, char **);
133752fa694SWarner Losh 	const char	*help;
1349b50d902SRodney W. Grimes };
1359b50d902SRodney W. Grimes 
136752fa694SWarner Losh static struct cmd cmdtab[] = {
137752fa694SWarner Losh 	{ "connect",	setpeer,	"connect to remote tftp"	},
138752fa694SWarner Losh 	{ "mode",	modecmd,	"set file transfer mode"	},
139752fa694SWarner Losh 	{ "put",	put,		"send file"			},
140752fa694SWarner Losh 	{ "get",	get,		"receive file"			},
141752fa694SWarner Losh 	{ "quit",	quit,		"exit tftp"			},
142752fa694SWarner Losh 	{ "verbose",	setverbose,	"toggle verbose mode"		},
143752fa694SWarner Losh 	{ "status",	showstatus,	"show current status"		},
144752fa694SWarner Losh 	{ "binary",     setbinary,	"set mode to octet"		},
145752fa694SWarner Losh 	{ "ascii",      setascii,	"set mode to netascii"		},
146752fa694SWarner Losh 	{ "rexmt",	settimeoutpacket,
147752fa694SWarner Losh 	  "set per-packet retransmission timeout[-]" },
148752fa694SWarner Losh 	{ "timeout",	settimeoutnetwork,
149752fa694SWarner Losh 	  "set total retransmission timeout" },
150752fa694SWarner Losh 	{ "trace",	setdebug,	"enable 'debug packet'[-]"	},
151752fa694SWarner Losh 	{ "debug",	setdebug,	"enable verbose output"		},
152752fa694SWarner Losh 	{ "blocksize",	setblocksize,	"set blocksize[*]"		},
153752fa694SWarner Losh 	{ "blocksize2",	setblocksize2,	"set blocksize as a power of 2[**]" },
154752fa694SWarner Losh 	{ "rollover",	setrollover,	"rollover after 64K packets[**]" },
155752fa694SWarner Losh 	{ "options",	setoptions,
156752fa694SWarner Losh 	  "enable or disable RFC2347 style options" },
157752fa694SWarner Losh 	{ "help",	help,		"print help information"	},
158f471745aSWarner Losh 	{ "packetdrop",	setpacketdrop,	"artificial packetloss feature"	},
159752fa694SWarner Losh 	{ "?",		help,		"print help information"	},
1608049f797SMark Murray 	{ NULL,		NULL,		NULL				}
1619b50d902SRodney W. Grimes };
1629b50d902SRodney W. Grimes 
163752fa694SWarner Losh static struct	modes {
1648049f797SMark Murray 	const char *m_name;
1658049f797SMark Murray 	const char *m_mode;
1669b50d902SRodney W. Grimes } modes[] = {
1679b50d902SRodney W. Grimes 	{ "ascii",	"netascii" },
1689b50d902SRodney W. Grimes 	{ "netascii",	"netascii" },
1699b50d902SRodney W. Grimes 	{ "binary",	"octet" },
1709b50d902SRodney W. Grimes 	{ "image",	"octet" },
1719b50d902SRodney W. Grimes 	{ "octet",	"octet" },
172752fa694SWarner Losh 	{ NULL,		NULL }
1739b50d902SRodney W. Grimes };
1749b50d902SRodney W. Grimes 
175752fa694SWarner Losh int
176752fa694SWarner Losh main(int argc, char *argv[])
177752fa694SWarner Losh {
178752fa694SWarner Losh 
179752fa694SWarner Losh 	acting_as_client = 1;
180752fa694SWarner Losh 	peer = -1;
181752fa694SWarner Losh 	strcpy(mode, "netascii");
182752fa694SWarner Losh 	signal(SIGINT, intr);
183752fa694SWarner Losh 	if (argc > 1) {
184752fa694SWarner Losh 		if (setjmp(toplevel) != 0)
185752fa694SWarner Losh 			exit(txrx_error);
186752fa694SWarner Losh 
187752fa694SWarner Losh 		if (strncmp(argv[1], "tftp://", 7) == 0) {
188752fa694SWarner Losh 			urihandling(argv[1]);
189752fa694SWarner Losh 			exit(txrx_error);
190752fa694SWarner Losh 		}
191752fa694SWarner Losh 
192752fa694SWarner Losh 		setpeer(argc, argv);
193752fa694SWarner Losh 	}
194752fa694SWarner Losh 	if (setjmp(toplevel) != 0)
195752fa694SWarner Losh 		(void)putchar('\n');
196752fa694SWarner Losh 
197752fa694SWarner Losh 	init_options();
198752fa694SWarner Losh 	command();
199752fa694SWarner Losh }
200752fa694SWarner Losh 
201752fa694SWarner Losh /*
202752fa694SWarner Losh  * RFC3617 handling of TFTP URIs:
203752fa694SWarner Losh  *
204752fa694SWarner Losh  *    tftpURI         = "tftp://" host "/" file [ mode ]
205752fa694SWarner Losh  *    mode            = ";"  "mode=" ( "netascii" / "octet" )
206752fa694SWarner Losh  *    file            = *( unreserved / escaped )
207752fa694SWarner Losh  *    host            = <as specified by RFC 2732>
208752fa694SWarner Losh  *    unreserved      = <as specified in RFC 2396>
209752fa694SWarner Losh  *    escaped         = <as specified in RFC 2396>
210752fa694SWarner Losh  *
211752fa694SWarner Losh  * We are cheating a little bit by allowing any mode as specified in the
212752fa694SWarner Losh  * modes table defined earlier on in this file and mapping it on the real
213752fa694SWarner Losh  * mode.
214752fa694SWarner Losh  */
215752fa694SWarner Losh static void
216752fa694SWarner Losh urihandling(char *URI)
217752fa694SWarner Losh {
218752fa694SWarner Losh 	char	uri[ARG_MAX];
219752fa694SWarner Losh 	char	*host = NULL;
220752fa694SWarner Losh 	char	*path = NULL;
22104ebad38SMarius Strobl 	char	*opts = NULL;
22204ebad38SMarius Strobl 	const char *tmode = "octet";
223752fa694SWarner Losh 	char	*s;
224752fa694SWarner Losh 	char	line[MAXLINE];
225752fa694SWarner Losh 	int	i;
226752fa694SWarner Losh 
227752fa694SWarner Losh 	strncpy(uri, URI, ARG_MAX);
228752fa694SWarner Losh 	host = uri + 7;
229752fa694SWarner Losh 
230752fa694SWarner Losh 	if ((s = strchr(host, '/')) == NULL) {
231752fa694SWarner Losh 		fprintf(stderr,
232752fa694SWarner Losh 		    "Invalid URI: Couldn't find / after hostname\n");
233752fa694SWarner Losh 		exit(1);
234752fa694SWarner Losh 	}
235752fa694SWarner Losh 	*s = '\0';
236752fa694SWarner Losh 	path = s + 1;
237752fa694SWarner Losh 
238752fa694SWarner Losh 	if ((s = strchr(path, ';')) != NULL) {
239752fa694SWarner Losh 		*s = '\0';
24004ebad38SMarius Strobl 		opts = s + 1;
241752fa694SWarner Losh 
24204ebad38SMarius Strobl 		if (strncmp(opts, "mode=", 5) == 0) {
24304ebad38SMarius Strobl 			tmode = opts;
24404ebad38SMarius Strobl 			tmode += 5;
245752fa694SWarner Losh 
246752fa694SWarner Losh 			for (i = 0; modes[i].m_name != NULL; i++) {
24704ebad38SMarius Strobl 				if (strcmp(modes[i].m_name, tmode) == 0)
248752fa694SWarner Losh 					break;
249752fa694SWarner Losh 			}
250752fa694SWarner Losh 			if (modes[i].m_name == NULL) {
251752fa694SWarner Losh 				fprintf(stderr, "Invalid mode: '%s'\n", mode);
252752fa694SWarner Losh 				exit(1);
253752fa694SWarner Losh 			}
254752fa694SWarner Losh 			settftpmode(modes[i].m_mode);
255752fa694SWarner Losh 		}
256752fa694SWarner Losh 	} else {
257752fa694SWarner Losh 		settftpmode("octet");
258752fa694SWarner Losh 	}
259752fa694SWarner Losh 
260752fa694SWarner Losh 	setpeer0(host, NULL);
261752fa694SWarner Losh 
262752fa694SWarner Losh 	sprintf(line, "get %s", path);
263752fa694SWarner Losh 	makeargv(line);
264752fa694SWarner Losh 	get(margc, margv);
265752fa694SWarner Losh }
266752fa694SWarner Losh 
267752fa694SWarner Losh static char    hostname[MAXHOSTNAMELEN];
268752fa694SWarner Losh 
269752fa694SWarner Losh static void
270752fa694SWarner Losh setpeer0(char *host, const char *lport)
271752fa694SWarner Losh {
272752fa694SWarner Losh 	struct addrinfo hints, *res0, *res;
273752fa694SWarner Losh 	int error;
274752fa694SWarner Losh 	const char *cause = "unknown";
275752fa694SWarner Losh 
276752fa694SWarner Losh 	if (connected) {
277752fa694SWarner Losh 		close(peer);
278752fa694SWarner Losh 		peer = -1;
279752fa694SWarner Losh 	}
280752fa694SWarner Losh 	connected = 0;
281752fa694SWarner Losh 
282752fa694SWarner Losh 	memset(&hints, 0, sizeof(hints));
283752fa694SWarner Losh 	hints.ai_family = PF_UNSPEC;
284752fa694SWarner Losh 	hints.ai_socktype = SOCK_DGRAM;
285752fa694SWarner Losh 	hints.ai_protocol = IPPROTO_UDP;
286752fa694SWarner Losh 	hints.ai_flags = AI_CANONNAME;
287752fa694SWarner Losh 	if (!lport)
288752fa694SWarner Losh 		lport = "tftp";
289752fa694SWarner Losh 	error = getaddrinfo(host, lport, &hints, &res0);
290752fa694SWarner Losh 	if (error) {
291752fa694SWarner Losh 		warnx("%s", gai_strerror(error));
292752fa694SWarner Losh 		return;
293752fa694SWarner Losh 	}
294752fa694SWarner Losh 
295752fa694SWarner Losh 	for (res = res0; res; res = res->ai_next) {
296752fa694SWarner Losh 		if (res->ai_addrlen > sizeof(peeraddr))
297752fa694SWarner Losh 			continue;
298752fa694SWarner Losh 		peer = socket(res->ai_family, res->ai_socktype,
299752fa694SWarner Losh 			res->ai_protocol);
300752fa694SWarner Losh 		if (peer < 0) {
301752fa694SWarner Losh 			cause = "socket";
302752fa694SWarner Losh 			continue;
303752fa694SWarner Losh 		}
304752fa694SWarner Losh 
305752fa694SWarner Losh 		memset(&peer_sock, 0, sizeof(peer_sock));
306752fa694SWarner Losh 		peer_sock.ss_family = res->ai_family;
307752fa694SWarner Losh 		peer_sock.ss_len = res->ai_addrlen;
308752fa694SWarner Losh 		if (bind(peer, (struct sockaddr *)&peer_sock, peer_sock.ss_len) < 0) {
309752fa694SWarner Losh 			cause = "bind";
310752fa694SWarner Losh 			close(peer);
311752fa694SWarner Losh 			peer = -1;
312752fa694SWarner Losh 			continue;
313752fa694SWarner Losh 		}
314752fa694SWarner Losh 
315752fa694SWarner Losh 		break;
316752fa694SWarner Losh 	}
317752fa694SWarner Losh 
318752fa694SWarner Losh 	if (peer < 0)
319752fa694SWarner Losh 		warn("%s", cause);
320752fa694SWarner Losh 	else {
321752fa694SWarner Losh 		/* res->ai_addr <= sizeof(peeraddr) is guaranteed */
322752fa694SWarner Losh 		memcpy(&peer_sock, res->ai_addr, res->ai_addrlen);
323752fa694SWarner Losh 		if (res->ai_canonname) {
324752fa694SWarner Losh 			(void) strncpy(hostname, res->ai_canonname,
325752fa694SWarner Losh 				sizeof(hostname));
326752fa694SWarner Losh 		} else
327752fa694SWarner Losh 			(void) strncpy(hostname, host, sizeof(hostname));
328752fa694SWarner Losh 		hostname[sizeof(hostname)-1] = 0;
329752fa694SWarner Losh 		connected = 1;
330752fa694SWarner Losh 	}
331752fa694SWarner Losh 
332752fa694SWarner Losh 	freeaddrinfo(res0);
333752fa694SWarner Losh }
334752fa694SWarner Losh 
335752fa694SWarner Losh static void
336752fa694SWarner Losh setpeer(int argc, char *argv[])
337752fa694SWarner Losh {
338752fa694SWarner Losh 	char	line[MAXLINE];
339752fa694SWarner Losh 
340752fa694SWarner Losh 	if (argc < 2) {
341752fa694SWarner Losh 		strcpy(line, "Connect ");
342752fa694SWarner Losh 		printf("(to) ");
343752fa694SWarner Losh 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
344752fa694SWarner Losh 		makeargv(line);
345752fa694SWarner Losh 		argc = margc;
346752fa694SWarner Losh 		argv = margv;
347752fa694SWarner Losh 	}
348752fa694SWarner Losh 	if ((argc < 2) || (argc > 3)) {
349752fa694SWarner Losh 		printf("usage: %s [host [port]]\n", argv[0]);
350752fa694SWarner Losh 		return;
351752fa694SWarner Losh 	}
352752fa694SWarner Losh 	if (argc == 3) {
353752fa694SWarner Losh 		port = argv[2];
354752fa694SWarner Losh 		setpeer0(argv[1], argv[2]);
355752fa694SWarner Losh 	} else
356752fa694SWarner Losh 		setpeer0(argv[1], NULL);
357752fa694SWarner Losh }
358752fa694SWarner Losh 
359752fa694SWarner Losh static void
3601b0fa6faSXin LI modecmd(int argc, char *argv[])
3619b50d902SRodney W. Grimes {
3628049f797SMark Murray 	struct modes *p;
3638049f797SMark Murray 	const char *sep;
3649b50d902SRodney W. Grimes 
3659b50d902SRodney W. Grimes 	if (argc < 2) {
3669b50d902SRodney W. Grimes 		printf("Using %s mode to transfer files.\n", mode);
3679b50d902SRodney W. Grimes 		return;
3689b50d902SRodney W. Grimes 	}
3699b50d902SRodney W. Grimes 	if (argc == 2) {
3709b50d902SRodney W. Grimes 		for (p = modes; p->m_name; p++)
3719b50d902SRodney W. Grimes 			if (strcmp(argv[1], p->m_name) == 0)
3729b50d902SRodney W. Grimes 				break;
3739b50d902SRodney W. Grimes 		if (p->m_name) {
3749b50d902SRodney W. Grimes 			settftpmode(p->m_mode);
3759b50d902SRodney W. Grimes 			return;
3769b50d902SRodney W. Grimes 		}
3779b50d902SRodney W. Grimes 		printf("%s: unknown mode\n", argv[1]);
3789b50d902SRodney W. Grimes 		/* drop through and print usage message */
3799b50d902SRodney W. Grimes 	}
3809b50d902SRodney W. Grimes 
3819b50d902SRodney W. Grimes 	printf("usage: %s [", argv[0]);
3829b50d902SRodney W. Grimes 	sep = " ";
383752fa694SWarner Losh 	for (p = modes; p->m_name != NULL; p++) {
3849b50d902SRodney W. Grimes 		printf("%s%s", sep, p->m_name);
3859b50d902SRodney W. Grimes 		if (*sep == ' ')
3869b50d902SRodney W. Grimes 			sep = " | ";
3879b50d902SRodney W. Grimes 	}
3889b50d902SRodney W. Grimes 	printf(" ]\n");
3899b50d902SRodney W. Grimes 	return;
3909b50d902SRodney W. Grimes }
3919b50d902SRodney W. Grimes 
392752fa694SWarner Losh static void
3931b0fa6faSXin LI setbinary(int argc __unused, char *argv[] __unused)
3949b50d902SRodney W. Grimes {
3959b50d902SRodney W. Grimes 
3969b50d902SRodney W. Grimes 	settftpmode("octet");
3979b50d902SRodney W. Grimes }
3989b50d902SRodney W. Grimes 
399752fa694SWarner Losh static void
4001b0fa6faSXin LI setascii(int argc __unused, char *argv[] __unused)
4019b50d902SRodney W. Grimes {
4029b50d902SRodney W. Grimes 
4039b50d902SRodney W. Grimes 	settftpmode("netascii");
4049b50d902SRodney W. Grimes }
4059b50d902SRodney W. Grimes 
4069b50d902SRodney W. Grimes static void
4071b0fa6faSXin LI settftpmode(const char *newmode)
4089b50d902SRodney W. Grimes {
409752fa694SWarner Losh 
4109b50d902SRodney W. Grimes 	strcpy(mode, newmode);
4119b50d902SRodney W. Grimes 	if (verbose)
4129b50d902SRodney W. Grimes 		printf("mode set to %s\n", mode);
4139b50d902SRodney W. Grimes }
4149b50d902SRodney W. Grimes 
4159b50d902SRodney W. Grimes 
4169b50d902SRodney W. Grimes /*
4179b50d902SRodney W. Grimes  * Send file(s).
4189b50d902SRodney W. Grimes  */
419752fa694SWarner Losh static void
4201b0fa6faSXin LI put(int argc, char *argv[])
4219b50d902SRodney W. Grimes {
4229b50d902SRodney W. Grimes 	int	fd;
4238049f797SMark Murray 	int	n;
4248049f797SMark Murray 	char	*cp, *targ;
425752fa694SWarner Losh 	char	line[MAXLINE];
426752fa694SWarner Losh 	struct stat sb;
4279b50d902SRodney W. Grimes 
4289b50d902SRodney W. Grimes 	if (argc < 2) {
4299b50d902SRodney W. Grimes 		strcpy(line, "send ");
4309b50d902SRodney W. Grimes 		printf("(file) ");
431ca22ff9eSJoerg Wunsch 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
432752fa694SWarner Losh 		makeargv(line);
4339b50d902SRodney W. Grimes 		argc = margc;
4349b50d902SRodney W. Grimes 		argv = margv;
4359b50d902SRodney W. Grimes 	}
4369b50d902SRodney W. Grimes 	if (argc < 2) {
4379b50d902SRodney W. Grimes 		putusage(argv[0]);
4389b50d902SRodney W. Grimes 		return;
4399b50d902SRodney W. Grimes 	}
4409b50d902SRodney W. Grimes 	targ = argv[argc - 1];
4414dac6235SHajimu UMEMOTO 	if (rindex(argv[argc - 1], ':')) {
4428049f797SMark Murray 		char *lcp;
4439b50d902SRodney W. Grimes 
4449b50d902SRodney W. Grimes 		for (n = 1; n < argc - 1; n++)
4459b50d902SRodney W. Grimes 			if (index(argv[n], ':')) {
4469b50d902SRodney W. Grimes 				putusage(argv[0]);
4479b50d902SRodney W. Grimes 				return;
4489b50d902SRodney W. Grimes 			}
4498049f797SMark Murray 		lcp = argv[argc - 1];
4504dac6235SHajimu UMEMOTO 		targ = rindex(lcp, ':');
4519b50d902SRodney W. Grimes 		*targ++ = 0;
4524dac6235SHajimu UMEMOTO 		if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') {
4534dac6235SHajimu UMEMOTO 			lcp[strlen(lcp) - 1] = '\0';
4544dac6235SHajimu UMEMOTO 			lcp++;
4559b50d902SRodney W. Grimes 		}
4564dac6235SHajimu UMEMOTO 		setpeer0(lcp, NULL);
4579b50d902SRodney W. Grimes 	}
4589b50d902SRodney W. Grimes 	if (!connected) {
4599b50d902SRodney W. Grimes 		printf("No target machine specified.\n");
4609b50d902SRodney W. Grimes 		return;
4619b50d902SRodney W. Grimes 	}
4629b50d902SRodney W. Grimes 	if (argc < 4) {
4639b50d902SRodney W. Grimes 		cp = argc == 2 ? tail(targ) : argv[1];
4649b50d902SRodney W. Grimes 		fd = open(cp, O_RDONLY);
4659b50d902SRodney W. Grimes 		if (fd < 0) {
466fd129a02SPhilippe Charnier 			warn("%s", cp);
4679b50d902SRodney W. Grimes 			return;
4689b50d902SRodney W. Grimes 		}
469752fa694SWarner Losh 
470752fa694SWarner Losh 		stat(cp, &sb);
471752fa694SWarner Losh 		asprintf(&options[OPT_TSIZE].o_request, "%ju", sb.st_size);
472752fa694SWarner Losh 
4739b50d902SRodney W. Grimes 		if (verbose)
4749b50d902SRodney W. Grimes 			printf("putting %s to %s:%s [%s]\n",
4759b50d902SRodney W. Grimes 			    cp, hostname, targ, mode);
476752fa694SWarner Losh 		xmitfile(peer, port, fd, targ, mode);
4779b50d902SRodney W. Grimes 		return;
4789b50d902SRodney W. Grimes 	}
4799b50d902SRodney W. Grimes 				/* this assumes the target is a directory */
4809b50d902SRodney W. Grimes 				/* on a remote unix system.  hmmmm.  */
4819b50d902SRodney W. Grimes 	cp = index(targ, '\0');
4829b50d902SRodney W. Grimes 	*cp++ = '/';
4839b50d902SRodney W. Grimes 	for (n = 1; n < argc - 1; n++) {
4849b50d902SRodney W. Grimes 		strcpy(cp, tail(argv[n]));
4859b50d902SRodney W. Grimes 		fd = open(argv[n], O_RDONLY);
4869b50d902SRodney W. Grimes 		if (fd < 0) {
487fd129a02SPhilippe Charnier 			warn("%s", argv[n]);
4889b50d902SRodney W. Grimes 			continue;
4899b50d902SRodney W. Grimes 		}
490752fa694SWarner Losh 
491752fa694SWarner Losh 		stat(cp, &sb);
492752fa694SWarner Losh 		asprintf(&options[OPT_TSIZE].o_request, "%ju", sb.st_size);
493752fa694SWarner Losh 
4949b50d902SRodney W. Grimes 		if (verbose)
4959b50d902SRodney W. Grimes 			printf("putting %s to %s:%s [%s]\n",
4969b50d902SRodney W. Grimes 			    argv[n], hostname, targ, mode);
497752fa694SWarner Losh 		xmitfile(peer, port, fd, targ, mode);
4989b50d902SRodney W. Grimes 	}
4999b50d902SRodney W. Grimes }
5009b50d902SRodney W. Grimes 
5019b50d902SRodney W. Grimes static void
502752fa694SWarner Losh putusage(char *s)
5039b50d902SRodney W. Grimes {
504752fa694SWarner Losh 
505752fa694SWarner Losh 	printf("usage: %s file [remotename]\n", s);
506752fa694SWarner Losh 	printf("       %s file host:remotename\n", s);
507891ca8cfSSimon L. B. Nielsen 	printf("       %s file1 file2 ... fileN [[host:]remote-directory]\n", s);
5089b50d902SRodney W. Grimes }
5099b50d902SRodney W. Grimes 
5109b50d902SRodney W. Grimes /*
5119b50d902SRodney W. Grimes  * Receive file(s).
5129b50d902SRodney W. Grimes  */
513752fa694SWarner Losh static void
5141b0fa6faSXin LI get(int argc, char *argv[])
5159b50d902SRodney W. Grimes {
5169b50d902SRodney W. Grimes 	int fd;
5178049f797SMark Murray 	int n;
5188049f797SMark Murray 	char *cp;
5199b50d902SRodney W. Grimes 	char *src;
520752fa694SWarner Losh 	char	line[MAXLINE];
5219b50d902SRodney W. Grimes 
5229b50d902SRodney W. Grimes 	if (argc < 2) {
5239b50d902SRodney W. Grimes 		strcpy(line, "get ");
5249b50d902SRodney W. Grimes 		printf("(files) ");
525ca22ff9eSJoerg Wunsch 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
526752fa694SWarner Losh 		makeargv(line);
5279b50d902SRodney W. Grimes 		argc = margc;
5289b50d902SRodney W. Grimes 		argv = margv;
5299b50d902SRodney W. Grimes 	}
5309b50d902SRodney W. Grimes 	if (argc < 2) {
5319b50d902SRodney W. Grimes 		getusage(argv[0]);
5329b50d902SRodney W. Grimes 		return;
5339b50d902SRodney W. Grimes 	}
5349b50d902SRodney W. Grimes 	if (!connected) {
5359b50d902SRodney W. Grimes 		for (n = 1; n < argc ; n++)
5364dac6235SHajimu UMEMOTO 			if (rindex(argv[n], ':') == 0) {
537752fa694SWarner Losh 				printf("No remote host specified and "
538752fa694SWarner Losh 				    "no host given for file '%s'\n", argv[n]);
5399b50d902SRodney W. Grimes 				getusage(argv[0]);
5409b50d902SRodney W. Grimes 				return;
5419b50d902SRodney W. Grimes 			}
5429b50d902SRodney W. Grimes 	}
5439b50d902SRodney W. Grimes 	for (n = 1; n < argc ; n++) {
5444dac6235SHajimu UMEMOTO 		src = rindex(argv[n], ':');
5459b50d902SRodney W. Grimes 		if (src == NULL)
5469b50d902SRodney W. Grimes 			src = argv[n];
5479b50d902SRodney W. Grimes 		else {
5484dac6235SHajimu UMEMOTO 			char *lcp;
5499b50d902SRodney W. Grimes 
5509b50d902SRodney W. Grimes 			*src++ = 0;
5514dac6235SHajimu UMEMOTO 			lcp = argv[n];
5524dac6235SHajimu UMEMOTO 			if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') {
5534dac6235SHajimu UMEMOTO 				lcp[strlen(lcp) - 1] = '\0';
5544dac6235SHajimu UMEMOTO 				lcp++;
5559b50d902SRodney W. Grimes 			}
5564dac6235SHajimu UMEMOTO 			setpeer0(lcp, NULL);
5574dac6235SHajimu UMEMOTO 			if (!connected)
5584dac6235SHajimu UMEMOTO 				continue;
5599b50d902SRodney W. Grimes 		}
5609b50d902SRodney W. Grimes 		if (argc < 4) {
5619b50d902SRodney W. Grimes 			cp = argc == 3 ? argv[2] : tail(src);
5629b50d902SRodney W. Grimes 			fd = creat(cp, 0644);
5639b50d902SRodney W. Grimes 			if (fd < 0) {
564fd129a02SPhilippe Charnier 				warn("%s", cp);
5659b50d902SRodney W. Grimes 				return;
5669b50d902SRodney W. Grimes 			}
5679b50d902SRodney W. Grimes 			if (verbose)
5689b50d902SRodney W. Grimes 				printf("getting from %s:%s to %s [%s]\n",
5699b50d902SRodney W. Grimes 				    hostname, src, cp, mode);
570752fa694SWarner Losh 			recvfile(peer, port, fd, src, mode);
5719b50d902SRodney W. Grimes 			break;
5729b50d902SRodney W. Grimes 		}
5739b50d902SRodney W. Grimes 		cp = tail(src);         /* new .. jdg */
5749b50d902SRodney W. Grimes 		fd = creat(cp, 0644);
5759b50d902SRodney W. Grimes 		if (fd < 0) {
576fd129a02SPhilippe Charnier 			warn("%s", cp);
5779b50d902SRodney W. Grimes 			continue;
5789b50d902SRodney W. Grimes 		}
5799b50d902SRodney W. Grimes 		if (verbose)
5809b50d902SRodney W. Grimes 			printf("getting from %s:%s to %s [%s]\n",
5819b50d902SRodney W. Grimes 			    hostname, src, cp, mode);
582752fa694SWarner Losh 		recvfile(peer, port, fd, src, mode);
5839b50d902SRodney W. Grimes 	}
5849b50d902SRodney W. Grimes }
5859b50d902SRodney W. Grimes 
5869b50d902SRodney W. Grimes static void
587752fa694SWarner Losh getusage(char *s)
5889b50d902SRodney W. Grimes {
589752fa694SWarner Losh 
590752fa694SWarner Losh 	printf("usage: %s file [localname]\n", s);
591752fa694SWarner Losh 	printf("       %s [host:]file [localname]\n", s);
592891ca8cfSSimon L. B. Nielsen 	printf("       %s [host1:]file1 [host2:]file2 ... [hostN:]fileN\n", s);
5939b50d902SRodney W. Grimes }
5949b50d902SRodney W. Grimes 
595752fa694SWarner Losh static void
596752fa694SWarner Losh settimeoutpacket(int argc, char *argv[])
5979b50d902SRodney W. Grimes {
5989b50d902SRodney W. Grimes 	int t;
599752fa694SWarner Losh 	char	line[MAXLINE];
6009b50d902SRodney W. Grimes 
6019b50d902SRodney W. Grimes 	if (argc < 2) {
602752fa694SWarner Losh 		strcpy(line, "Packet timeout ");
6039b50d902SRodney W. Grimes 		printf("(value) ");
604ca22ff9eSJoerg Wunsch 		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
605752fa694SWarner Losh 		makeargv(line);
6069b50d902SRodney W. Grimes 		argc = margc;
6079b50d902SRodney W. Grimes 		argv = margv;
6089b50d902SRodney W. Grimes 	}
6099b50d902SRodney W. Grimes 	if (argc != 2) {
6109b50d902SRodney W. Grimes 		printf("usage: %s value\n", argv[0]);
6119b50d902SRodney W. Grimes 		return;
6129b50d902SRodney W. Grimes 	}
6139b50d902SRodney W. Grimes 	t = atoi(argv[1]);
614752fa694SWarner Losh 	if (t < 0) {
6159b50d902SRodney W. Grimes 		printf("%s: bad value\n", argv[1]);
616752fa694SWarner Losh 		return;
6179b50d902SRodney W. Grimes 	}
6189b50d902SRodney W. Grimes 
619752fa694SWarner Losh 	settimeouts(t, timeoutnetwork, maxtimeouts);
620752fa694SWarner Losh }
6219b50d902SRodney W. Grimes 
622752fa694SWarner Losh static void
623752fa694SWarner Losh settimeoutnetwork(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, "Network 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(timeoutpacket, t, maxtimeouts);
647752fa694SWarner Losh }
648752fa694SWarner Losh 
649752fa694SWarner Losh static void
650752fa694SWarner Losh showstatus(int argc __unused, char *argv[] __unused)
6519b50d902SRodney W. Grimes {
652752fa694SWarner Losh 
653752fa694SWarner Losh 	printf("Remote host: %s\n",
654752fa694SWarner Losh 	    connected ? hostname : "none specified yet");
655752fa694SWarner Losh 	printf("RFC2347 Options support: %s\n",
656752fa694SWarner Losh 	    options_rfc_enabled ? "enabled" : "disabled");
657752fa694SWarner Losh 	printf("Non-RFC defined options support: %s\n",
658752fa694SWarner Losh 	    options_extra_enabled ? "enabled" : "disabled");
659752fa694SWarner Losh 	printf("Mode: %s\n", mode);
660752fa694SWarner Losh 	printf("Verbose: %s\n", verbose ? "on" : "off");
661752fa694SWarner Losh 	printf("Debug: %s\n", debug_show(debug));
662752fa694SWarner Losh 	printf("Artificial packetloss: %d in 100 packets\n",
663752fa694SWarner Losh 	    packetdroppercentage);
664752fa694SWarner Losh 	printf("Segment size: %d bytes\n", segsize);
665752fa694SWarner Losh 	printf("Network timeout: %d seconds\n", timeoutpacket);
666752fa694SWarner Losh 	printf("Maximum network timeout: %d seconds\n", timeoutnetwork);
667752fa694SWarner Losh 	printf("Maximum timeouts: %d \n", maxtimeouts);
6689b50d902SRodney W. Grimes }
6699b50d902SRodney W. Grimes 
670752fa694SWarner Losh static void
6711b0fa6faSXin LI intr(int dummy __unused)
6729b50d902SRodney W. Grimes {
6739b50d902SRodney W. Grimes 
6749b50d902SRodney W. Grimes 	signal(SIGALRM, SIG_IGN);
6759b50d902SRodney W. Grimes 	alarm(0);
6769b50d902SRodney W. Grimes 	longjmp(toplevel, -1);
6779b50d902SRodney W. Grimes }
6789b50d902SRodney W. Grimes 
679752fa694SWarner Losh static char *
6801b0fa6faSXin LI tail(char *filename)
6819b50d902SRodney W. Grimes {
6828049f797SMark Murray 	char *s;
6839b50d902SRodney W. Grimes 
6849b50d902SRodney W. Grimes 	while (*filename) {
6859b50d902SRodney W. Grimes 		s = rindex(filename, '/');
6869b50d902SRodney W. Grimes 		if (s == NULL)
6879b50d902SRodney W. Grimes 			break;
6889b50d902SRodney W. Grimes 		if (s[1])
6899b50d902SRodney W. Grimes 			return (s + 1);
6909b50d902SRodney W. Grimes 		*s = '\0';
6919b50d902SRodney W. Grimes 	}
6929b50d902SRodney W. Grimes 	return (filename);
6939b50d902SRodney W. Grimes }
6949b50d902SRodney W. Grimes 
695c33ed450SMatthew N. Dodd static const char *
69604ebad38SMarius Strobl command_prompt(void)
6978979160aSBruce Evans {
6988979160aSBruce Evans 
699c33ed450SMatthew N. Dodd 	return ("tftp> ");
700c33ed450SMatthew N. Dodd }
701c33ed450SMatthew N. Dodd 
7029b50d902SRodney W. Grimes /*
7039b50d902SRodney W. Grimes  * Command parser.
7049b50d902SRodney W. Grimes  */
705eaa86f9dSBruce Evans static void
7061b0fa6faSXin LI command(void)
7079b50d902SRodney W. Grimes {
708c33ed450SMatthew N. Dodd 	HistEvent he;
7098049f797SMark Murray 	struct cmd *c;
7108979160aSBruce Evans 	static EditLine *el;
7118979160aSBruce Evans 	static History *hist;
712c33ed450SMatthew N. Dodd 	const char *bp;
7138979160aSBruce Evans 	char *cp;
7148049f797SMark Murray 	int len, num, vrbose;
715752fa694SWarner Losh 	char	line[MAXLINE];
716c33ed450SMatthew N. Dodd 
7178049f797SMark Murray 	vrbose = isatty(0);
7188049f797SMark Murray 	if (vrbose) {
719c33ed450SMatthew N. Dodd 		el = el_init("tftp", stdin, stdout, stderr);
720c33ed450SMatthew N. Dodd 		hist = history_init();
7212110d9c3SStefan Farfeleder 		history(hist, &he, H_SETSIZE, 100);
722c33ed450SMatthew N. Dodd 		el_set(el, EL_HIST, history, hist);
723c33ed450SMatthew N. Dodd 		el_set(el, EL_EDITOR, "emacs");
724c33ed450SMatthew N. Dodd 		el_set(el, EL_PROMPT, command_prompt);
725c33ed450SMatthew N. Dodd 		el_set(el, EL_SIGNAL, 1);
726c33ed450SMatthew N. Dodd 		el_source(el, NULL);
727c33ed450SMatthew N. Dodd 	}
7289b50d902SRodney W. Grimes 	for (;;) {
7298049f797SMark Murray 		if (vrbose) {
730c33ed450SMatthew N. Dodd                         if ((bp = el_gets(el, &num)) == NULL || num == 0)
731c33ed450SMatthew N. Dodd                                 exit(0);
732c33ed450SMatthew N. Dodd                         len = (num > MAXLINE) ? MAXLINE : num;
733c33ed450SMatthew N. Dodd                         memcpy(line, bp, len);
734c33ed450SMatthew N. Dodd                         line[len] = '\0';
735c33ed450SMatthew N. Dodd                         history(hist, &he, H_ENTER, bp);
736c33ed450SMatthew N. Dodd 		} else {
737752fa694SWarner Losh 			line[0] = 0;
738ca22ff9eSJoerg Wunsch 			if (fgets(line, sizeof line , stdin) == 0) {
7399b50d902SRodney W. Grimes 				if (feof(stdin)) {
74073f899caSBrian S. Dean 					exit(txrx_error);
7419b50d902SRodney W. Grimes 				} else {
7429b50d902SRodney W. Grimes 					continue;
7439b50d902SRodney W. Grimes 				}
7449b50d902SRodney W. Grimes 			}
745c33ed450SMatthew N. Dodd 		}
746ca22ff9eSJoerg Wunsch 		if ((cp = strchr(line, '\n')))
747ca22ff9eSJoerg Wunsch 			*cp = '\0';
7489b50d902SRodney W. Grimes 		if (line[0] == 0)
7499b50d902SRodney W. Grimes 			continue;
750752fa694SWarner Losh 		makeargv(line);
7519b50d902SRodney W. Grimes 		if (margc == 0)
7529b50d902SRodney W. Grimes 			continue;
7539b50d902SRodney W. Grimes 		c = getcmd(margv[0]);
7549b50d902SRodney W. Grimes 		if (c == (struct cmd *)-1) {
7559b50d902SRodney W. Grimes 			printf("?Ambiguous command\n");
7569b50d902SRodney W. Grimes 			continue;
7579b50d902SRodney W. Grimes 		}
7589b50d902SRodney W. Grimes 		if (c == 0) {
7599b50d902SRodney W. Grimes 			printf("?Invalid command\n");
7609b50d902SRodney W. Grimes 			continue;
7619b50d902SRodney W. Grimes 		}
7629b50d902SRodney W. Grimes 		(*c->handler)(margc, margv);
7639b50d902SRodney W. Grimes 	}
7649b50d902SRodney W. Grimes }
7659b50d902SRodney W. Grimes 
766752fa694SWarner Losh static struct cmd *
7671b0fa6faSXin LI getcmd(char *name)
7689b50d902SRodney W. Grimes {
7698049f797SMark Murray 	const char *p, *q;
7708049f797SMark Murray 	struct cmd *c, *found;
7718049f797SMark Murray 	int nmatches, longest;
7729b50d902SRodney W. Grimes 
7739b50d902SRodney W. Grimes 	longest = 0;
7749b50d902SRodney W. Grimes 	nmatches = 0;
7759b50d902SRodney W. Grimes 	found = 0;
7769b50d902SRodney W. Grimes 	for (c = cmdtab; (p = c->name) != NULL; c++) {
7779b50d902SRodney W. Grimes 		for (q = name; *q == *p++; q++)
7789b50d902SRodney W. Grimes 			if (*q == 0)		/* exact match? */
7799b50d902SRodney W. Grimes 				return (c);
7809b50d902SRodney W. Grimes 		if (!*q) {			/* the name was a prefix */
7819b50d902SRodney W. Grimes 			if (q - name > longest) {
7829b50d902SRodney W. Grimes 				longest = q - name;
7839b50d902SRodney W. Grimes 				nmatches = 1;
7849b50d902SRodney W. Grimes 				found = c;
7859b50d902SRodney W. Grimes 			} else if (q - name == longest)
7869b50d902SRodney W. Grimes 				nmatches++;
7879b50d902SRodney W. Grimes 		}
7889b50d902SRodney W. Grimes 	}
7899b50d902SRodney W. Grimes 	if (nmatches > 1)
7909b50d902SRodney W. Grimes 		return ((struct cmd *)-1);
7919b50d902SRodney W. Grimes 	return (found);
7929b50d902SRodney W. Grimes }
7939b50d902SRodney W. Grimes 
7949b50d902SRodney W. Grimes /*
7959b50d902SRodney W. Grimes  * Slice a string up into argc/argv.
7969b50d902SRodney W. Grimes  */
7979b50d902SRodney W. Grimes static void
798752fa694SWarner Losh makeargv(char *line)
7999b50d902SRodney W. Grimes {
8008049f797SMark Murray 	char *cp;
8018049f797SMark Murray 	char **argp = margv;
8029b50d902SRodney W. Grimes 
8039b50d902SRodney W. Grimes 	margc = 0;
804752fa694SWarner Losh 	if ((cp = strchr(line, '\n')) != NULL)
805ca22ff9eSJoerg Wunsch 		*cp = '\0';
806752fa694SWarner Losh 	for (cp = line; margc < MAX_MARGV - 1 && *cp != '\0';) {
8079b50d902SRodney W. Grimes 		while (isspace(*cp))
8089b50d902SRodney W. Grimes 			cp++;
8099b50d902SRodney W. Grimes 		if (*cp == '\0')
8109b50d902SRodney W. Grimes 			break;
8119b50d902SRodney W. Grimes 		*argp++ = cp;
8129b50d902SRodney W. Grimes 		margc += 1;
8139b50d902SRodney W. Grimes 		while (*cp != '\0' && !isspace(*cp))
8149b50d902SRodney W. Grimes 			cp++;
8159b50d902SRodney W. Grimes 		if (*cp == '\0')
8169b50d902SRodney W. Grimes 			break;
8179b50d902SRodney W. Grimes 		*cp++ = '\0';
8189b50d902SRodney W. Grimes 	}
8199b50d902SRodney W. Grimes 	*argp++ = 0;
8209b50d902SRodney W. Grimes }
8219b50d902SRodney W. Grimes 
822752fa694SWarner Losh static void
8231b0fa6faSXin LI quit(int argc __unused, char *argv[] __unused)
8249b50d902SRodney W. Grimes {
825752fa694SWarner Losh 
82673f899caSBrian S. Dean 	exit(txrx_error);
8279b50d902SRodney W. Grimes }
8289b50d902SRodney W. Grimes 
8299b50d902SRodney W. Grimes /*
8309b50d902SRodney W. Grimes  * Help command.
8319b50d902SRodney W. Grimes  */
832752fa694SWarner Losh static void
8331b0fa6faSXin LI help(int argc, char *argv[])
8349b50d902SRodney W. Grimes {
8358049f797SMark Murray 	struct cmd *c;
8369b50d902SRodney W. Grimes 
8379b50d902SRodney W. Grimes 	if (argc == 1) {
8389b50d902SRodney W. Grimes 		printf("Commands may be abbreviated.  Commands are:\n\n");
8399b50d902SRodney W. Grimes 		for (c = cmdtab; c->name; c++)
8409b50d902SRodney W. Grimes 			printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help);
841752fa694SWarner Losh 
842752fa694SWarner Losh 		printf("\n[-] : You shouldn't use these ones anymore.\n");
843*f4b7e64dSCraig Rodrigues 		printf("[*] : RFC2347 options support required.\n");
844*f4b7e64dSCraig Rodrigues 		printf("[**] : Non-standard RFC2347 option.\n");
8459b50d902SRodney W. Grimes 		return;
8469b50d902SRodney W. Grimes 	}
8479b50d902SRodney W. Grimes 	while (--argc > 0) {
8488049f797SMark Murray 		char *arg;
8499b50d902SRodney W. Grimes 		arg = *++argv;
8509b50d902SRodney W. Grimes 		c = getcmd(arg);
8519b50d902SRodney W. Grimes 		if (c == (struct cmd *)-1)
852752fa694SWarner Losh 			printf("?Ambiguous help command: %s\n", arg);
8539b50d902SRodney W. Grimes 		else if (c == (struct cmd *)0)
854752fa694SWarner Losh 			printf("?Invalid help command: %s\n", arg);
8559b50d902SRodney W. Grimes 		else
8569b50d902SRodney W. Grimes 			printf("%s\n", c->help);
8579b50d902SRodney W. Grimes 	}
8589b50d902SRodney W. Grimes }
8599b50d902SRodney W. Grimes 
860752fa694SWarner Losh static void
861752fa694SWarner Losh setverbose(int argc __unused, char *argv[] __unused)
8629b50d902SRodney W. Grimes {
8639b50d902SRodney W. Grimes 
8649b50d902SRodney W. Grimes 	verbose = !verbose;
8659b50d902SRodney W. Grimes 	printf("Verbose mode %s.\n", verbose ? "on" : "off");
8669b50d902SRodney W. Grimes }
867752fa694SWarner Losh 
868752fa694SWarner Losh static void
869752fa694SWarner Losh setoptions(int argc, char *argv[])
870752fa694SWarner Losh {
871752fa694SWarner Losh 
872752fa694SWarner Losh 	if (argc == 2) {
873752fa694SWarner Losh 		if (strcasecmp(argv[1], "enable") == 0 ||
874752fa694SWarner Losh 		    strcasecmp(argv[1], "on") == 0) {
875752fa694SWarner Losh 			options_extra_enabled = 1;
876752fa694SWarner Losh 			options_rfc_enabled = 1;
877752fa694SWarner Losh 		}
878752fa694SWarner Losh 		if (strcasecmp(argv[1], "disable") == 0 ||
879752fa694SWarner Losh 		    strcasecmp(argv[1], "off") == 0) {
880752fa694SWarner Losh 			options_extra_enabled = 0;
881752fa694SWarner Losh 			options_rfc_enabled = 0;
882752fa694SWarner Losh 		}
883752fa694SWarner Losh 		if (strcasecmp(argv[1], "extra") == 0)
884752fa694SWarner Losh 			options_extra_enabled = !options_extra_enabled;
885752fa694SWarner Losh 	}
886752fa694SWarner Losh 	printf("Support for RFC2347 style options are now %s.\n",
887752fa694SWarner Losh 	    options_rfc_enabled ? "enabled" : "disabled");
888752fa694SWarner Losh 	printf("Support for non-RFC defined options are now %s.\n",
889752fa694SWarner Losh 	    options_extra_enabled ? "enabled" : "disabled");
890752fa694SWarner Losh 
891752fa694SWarner Losh 	printf("\nThe following options are available:\n"
892752fa694SWarner Losh 	    "\toptions on	: enable support for RFC2347 style options\n"
893752fa694SWarner Losh 	    "\toptions off	: disable support for RFC2347 style options\n"
894752fa694SWarner Losh 	    "\toptions extra	: toggle support for non-RFC defined options\n"
895752fa694SWarner Losh 	);
896752fa694SWarner Losh }
897752fa694SWarner Losh 
898752fa694SWarner Losh static void
899752fa694SWarner Losh setrollover(int argc, char *argv[])
900752fa694SWarner Losh {
901752fa694SWarner Losh 
902752fa694SWarner Losh 	if (argc == 2) {
903752fa694SWarner Losh 		if (strcasecmp(argv[1], "never") == 0 ||
904752fa694SWarner Losh 		    strcasecmp(argv[1], "none") == 0) {
905752fa694SWarner Losh 			free(options[OPT_ROLLOVER].o_request);
906752fa694SWarner Losh 			options[OPT_ROLLOVER].o_request = NULL;
907752fa694SWarner Losh 		}
908752fa694SWarner Losh 		if (strcasecmp(argv[1], "1") == 0) {
909752fa694SWarner Losh 			free(options[OPT_ROLLOVER].o_request);
910752fa694SWarner Losh 			options[OPT_ROLLOVER].o_request = strdup("1");
911752fa694SWarner Losh 		}
912752fa694SWarner Losh 		if (strcasecmp(argv[1], "0") == 0) {
913752fa694SWarner Losh 			free(options[OPT_ROLLOVER].o_request);
914752fa694SWarner Losh 			options[OPT_ROLLOVER].o_request = strdup("0");
915752fa694SWarner Losh 		}
916752fa694SWarner Losh 	}
917752fa694SWarner Losh 	printf("Support for the rollover options is %s.\n",
918752fa694SWarner Losh 	    options[OPT_ROLLOVER].o_request != NULL ? "enabled" : "disabled");
919752fa694SWarner Losh 	if (options[OPT_ROLLOVER].o_request != NULL)
920752fa694SWarner Losh 		printf("Block rollover will be to block %s.\n",
921752fa694SWarner Losh 		    options[OPT_ROLLOVER].o_request);
922752fa694SWarner Losh 
923752fa694SWarner Losh 
924752fa694SWarner Losh 	printf("\nThe following rollover options are available:\n"
925752fa694SWarner Losh 	    "\trollover 0	: rollover to block zero (default)\n"
926752fa694SWarner Losh 	    "\trollover 1	: rollover to block one\n"
927752fa694SWarner Losh 	    "\trollover never	: do not support the rollover option\n"
928752fa694SWarner Losh 	    "\trollover none	: do not support the rollover option\n"
929752fa694SWarner Losh 	);
930752fa694SWarner Losh }
931752fa694SWarner Losh 
932752fa694SWarner Losh static void
933752fa694SWarner Losh setdebug(int argc, char *argv[])
934752fa694SWarner Losh {
935752fa694SWarner Losh 	int i;
936752fa694SWarner Losh 
937752fa694SWarner Losh 	if (argc != 1) {
938752fa694SWarner Losh 		i = 1;
939752fa694SWarner Losh 		while (i < argc)
940752fa694SWarner Losh 			debug ^= debug_find(argv[i++]);
941752fa694SWarner Losh 	}
942752fa694SWarner Losh 	printf("The following debugging is enabled: %s\n", debug_show(debug));
943752fa694SWarner Losh 
944752fa694SWarner Losh 	printf("\nThe following debugs are available:\n");
945752fa694SWarner Losh 	i = 0;
946752fa694SWarner Losh 	while (debugs[i].name != NULL) {
947752fa694SWarner Losh 		printf("\t%s\t%s\n", debugs[i].name, debugs[i].desc);
948752fa694SWarner Losh 		i++;
949752fa694SWarner Losh 	}
950752fa694SWarner Losh }
951752fa694SWarner Losh 
952752fa694SWarner Losh static void
953752fa694SWarner Losh setblocksize(int argc, char *argv[])
954752fa694SWarner Losh {
955752fa694SWarner Losh 
956752fa694SWarner Losh 	if (!options_rfc_enabled)
957752fa694SWarner Losh 		printf("RFC2347 style options are not enabled "
958f471745aSWarner Losh 		    "(but proceeding anyway)\n");
959752fa694SWarner Losh 
960752fa694SWarner Losh 	if (argc != 1) {
961752fa694SWarner Losh 		int size = atoi(argv[1]);
962752fa694SWarner Losh 		size_t max;
96304ebad38SMarius Strobl 		u_long maxdgram;
964752fa694SWarner Losh 
96504ebad38SMarius Strobl 		max = sizeof(maxdgram);
966752fa694SWarner Losh 		if (sysctlbyname("net.inet.udp.maxdgram",
96704ebad38SMarius Strobl 			&maxdgram, &max, NULL, 0) < 0) {
968752fa694SWarner Losh 			perror("sysctl: net.inet.udp.maxdgram");
969752fa694SWarner Losh 			return;
970752fa694SWarner Losh 		}
971752fa694SWarner Losh 
972752fa694SWarner Losh 		if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) {
973752fa694SWarner Losh 			printf("Blocksize should be between %d and %d bytes.\n",
974752fa694SWarner Losh 				BLKSIZE_MIN, BLKSIZE_MAX);
975752fa694SWarner Losh 			return;
97604ebad38SMarius Strobl 		} else if (size > (int)maxdgram - 4) {
97704ebad38SMarius Strobl 			printf("Blocksize can't be bigger than %ld bytes due "
978752fa694SWarner Losh 			    "to the net.inet.udp.maxdgram sysctl limitation.\n",
97904ebad38SMarius Strobl 			    maxdgram - 4);
980752fa694SWarner Losh 			asprintf(&options[OPT_BLKSIZE].o_request,
98104ebad38SMarius Strobl 			    "%ld", maxdgram - 4);
982752fa694SWarner Losh 		} else {
983752fa694SWarner Losh 			asprintf(&options[OPT_BLKSIZE].o_request, "%d", size);
984752fa694SWarner Losh 		}
985752fa694SWarner Losh 	}
986752fa694SWarner Losh 	printf("Blocksize is now %s bytes.\n", options[OPT_BLKSIZE].o_request);
987752fa694SWarner Losh }
988752fa694SWarner Losh 
989752fa694SWarner Losh static void
990752fa694SWarner Losh setblocksize2(int argc, char *argv[])
991752fa694SWarner Losh {
992752fa694SWarner Losh 
993752fa694SWarner Losh 	if (!options_rfc_enabled || !options_extra_enabled)
994752fa694SWarner Losh 		printf(
995752fa694SWarner Losh 		    "RFC2347 style or non-RFC defined options are not enabled "
996f471745aSWarner Losh 		    "(but proceeding anyway)\n");
997752fa694SWarner Losh 
998752fa694SWarner Losh 	if (argc != 1) {
999752fa694SWarner Losh 		int size = atoi(argv[1]);
1000752fa694SWarner Losh 		int i;
1001752fa694SWarner Losh 		size_t max;
100204ebad38SMarius Strobl 		u_long maxdgram;
1003752fa694SWarner Losh 
1004752fa694SWarner Losh 		int sizes[] = {
1005752fa694SWarner Losh 			8, 16, 32, 64, 128, 256, 512, 1024,
1006752fa694SWarner Losh 			2048, 4096, 8192, 16384, 32768, 0
1007752fa694SWarner Losh 		};
1008752fa694SWarner Losh 
100904ebad38SMarius Strobl 		max = sizeof(maxdgram);
1010752fa694SWarner Losh 		if (sysctlbyname("net.inet.udp.maxdgram",
101104ebad38SMarius Strobl 			&maxdgram, &max, NULL, 0) < 0) {
1012752fa694SWarner Losh 			perror("sysctl: net.inet.udp.maxdgram");
1013752fa694SWarner Losh 			return;
1014752fa694SWarner Losh 		}
1015752fa694SWarner Losh 
1016752fa694SWarner Losh 		for (i = 0; sizes[i] != 0; i++) {
1017752fa694SWarner Losh 			if (sizes[i] == size) break;
1018752fa694SWarner Losh 		}
1019752fa694SWarner Losh 		if (sizes[i] == 0) {
1020752fa694SWarner Losh 			printf("Blocksize2 should be a power of two between "
1021752fa694SWarner Losh 			    "8 and 32768.\n");
1022752fa694SWarner Losh 			return;
1023752fa694SWarner Losh 		}
1024752fa694SWarner Losh 
1025752fa694SWarner Losh 		if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) {
1026752fa694SWarner Losh 			printf("Blocksize2 should be between "
1027752fa694SWarner Losh 			    "%d and %d bytes.\n", BLKSIZE_MIN, BLKSIZE_MAX);
1028752fa694SWarner Losh 			return;
102904ebad38SMarius Strobl 		} else if (size > (int)maxdgram - 4) {
103004ebad38SMarius Strobl 			printf("Blocksize2 can't be bigger than %ld bytes due "
1031752fa694SWarner Losh 			    "to the net.inet.udp.maxdgram sysctl limitation.\n",
103204ebad38SMarius Strobl 			    maxdgram - 4);
1033752fa694SWarner Losh 			for (i = 0; sizes[i+1] != 0; i++) {
103404ebad38SMarius Strobl 				if ((int)maxdgram < sizes[i+1]) break;
1035752fa694SWarner Losh 			}
1036752fa694SWarner Losh 			asprintf(&options[OPT_BLKSIZE2].o_request,
1037752fa694SWarner Losh 			    "%d", sizes[i]);
1038752fa694SWarner Losh 		} else {
1039752fa694SWarner Losh 			asprintf(&options[OPT_BLKSIZE2].o_request, "%d", size);
1040752fa694SWarner Losh 		}
1041752fa694SWarner Losh 	}
1042752fa694SWarner Losh 	printf("Blocksize2 is now %s bytes.\n",
1043752fa694SWarner Losh 	    options[OPT_BLKSIZE2].o_request);
1044752fa694SWarner Losh }
1045752fa694SWarner Losh 
1046752fa694SWarner Losh static void
1047752fa694SWarner Losh setpacketdrop(int argc, char *argv[])
1048752fa694SWarner Losh {
1049752fa694SWarner Losh 
1050752fa694SWarner Losh 	if (argc != 1)
1051752fa694SWarner Losh 		packetdroppercentage = atoi(argv[1]);
1052752fa694SWarner Losh 
1053752fa694SWarner Losh 	printf("Randomly %d in 100 packets will be dropped\n",
1054752fa694SWarner Losh 	    packetdroppercentage);
1055752fa694SWarner Losh }
1056