xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/tftp/main.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * TFTP User Program -- Command Interface.
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate #include <sys/types.h>
46*0Sstevel@tonic-gate #include <sys/socket.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <arpa/inet.h>
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include <signal.h>
51*0Sstevel@tonic-gate #include <stdio.h>
52*0Sstevel@tonic-gate #include <stdlib.h>
53*0Sstevel@tonic-gate #include <errno.h>
54*0Sstevel@tonic-gate #include <ctype.h>
55*0Sstevel@tonic-gate #include <netdb.h>
56*0Sstevel@tonic-gate #include <fcntl.h>
57*0Sstevel@tonic-gate #include <string.h>
58*0Sstevel@tonic-gate #include <limits.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #include "tftpcommon.h"
61*0Sstevel@tonic-gate #include "tftpprivate.h"
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate #define	NELEM(a)	(sizeof (a) / sizeof ((a)[0]))
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #define	TIMEOUT		5		/* secs between rexmt's */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate struct sockaddr_in6	sin6;
68*0Sstevel@tonic-gate int			f;
69*0Sstevel@tonic-gate int			maxtimeout = 5 * TIMEOUT;
70*0Sstevel@tonic-gate int			verbose;
71*0Sstevel@tonic-gate int			trace;
72*0Sstevel@tonic-gate int			srexmtval;
73*0Sstevel@tonic-gate int			blksize;
74*0Sstevel@tonic-gate int			rexmtval = TIMEOUT;
75*0Sstevel@tonic-gate int			tsize_opt;
76*0Sstevel@tonic-gate jmp_buf			toplevel;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static int			default_port, port;
79*0Sstevel@tonic-gate static int			connected;
80*0Sstevel@tonic-gate static char			mode[32];
81*0Sstevel@tonic-gate static char			line[200];
82*0Sstevel@tonic-gate static char			*prompt = "tftp";
83*0Sstevel@tonic-gate static char			hostname[MAXHOSTNAMELEN];
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static void		intr(int);
86*0Sstevel@tonic-gate static void		quit(int, char **);
87*0Sstevel@tonic-gate static void		help(int, char **);
88*0Sstevel@tonic-gate static void		setverbose(int, char **);
89*0Sstevel@tonic-gate static void		settrace(int, char **);
90*0Sstevel@tonic-gate static void		status(int, char **);
91*0Sstevel@tonic-gate static void		get(int, char **);
92*0Sstevel@tonic-gate static void		put(int, char **);
93*0Sstevel@tonic-gate static void		setpeer(int, char **);
94*0Sstevel@tonic-gate static void		modecmd(int, char **);
95*0Sstevel@tonic-gate static void		setrexmt(int, char **);
96*0Sstevel@tonic-gate static void		settimeout(int, char **);
97*0Sstevel@tonic-gate static void		setbinary(int, char **);
98*0Sstevel@tonic-gate static void		setascii(int, char **);
99*0Sstevel@tonic-gate static void		setblksize(int, char **);
100*0Sstevel@tonic-gate static void		setsrexmt(int, char **);
101*0Sstevel@tonic-gate static void		settsize(int, char **);
102*0Sstevel@tonic-gate static void		setmode(char *);
103*0Sstevel@tonic-gate static void		putusage(char *);
104*0Sstevel@tonic-gate static void		getusage(char *);
105*0Sstevel@tonic-gate static char		*finddelimiter(char *);
106*0Sstevel@tonic-gate static char		*removebrackets(char *);
107*0Sstevel@tonic-gate static int		prompt_for_arg(char *, int, char *);
108*0Sstevel@tonic-gate static struct cmd	*getcmd(char *);
109*0Sstevel@tonic-gate static char		*tail(char *);
110*0Sstevel@tonic-gate static void		command(int);
111*0Sstevel@tonic-gate static void		makeargv(int *, char ***);
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate #define	HELPINDENT (sizeof ("connect"))
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate struct cmd {
116*0Sstevel@tonic-gate 	char	*name;
117*0Sstevel@tonic-gate 	char	*help;
118*0Sstevel@tonic-gate 	void	(*handler)(int, char **);
119*0Sstevel@tonic-gate };
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate static char	vhelp[] =	"toggle verbose mode";
122*0Sstevel@tonic-gate static char	thelp[] =	"toggle packet tracing";
123*0Sstevel@tonic-gate static char	chelp[] =	"connect to remote tftp";
124*0Sstevel@tonic-gate static char	qhelp[] =	"exit tftp";
125*0Sstevel@tonic-gate static char	hhelp[] =	"print help information";
126*0Sstevel@tonic-gate static char	shelp[] =	"send file";
127*0Sstevel@tonic-gate static char	rhelp[] =	"receive file";
128*0Sstevel@tonic-gate static char	mhelp[] =	"set file transfer mode";
129*0Sstevel@tonic-gate static char	sthelp[] =	"show current status";
130*0Sstevel@tonic-gate static char	xhelp[] =	"set per-packet retransmission timeout";
131*0Sstevel@tonic-gate static char	ihelp[] =	"set total retransmission timeout";
132*0Sstevel@tonic-gate static char	ashelp[] =	"set mode to netascii";
133*0Sstevel@tonic-gate static char	bnhelp[] =	"set mode to octet";
134*0Sstevel@tonic-gate static char	bshelp[] =	"set transfer blocksize to negotiate with the "
135*0Sstevel@tonic-gate 				"server";
136*0Sstevel@tonic-gate static char	srhelp[] =	"set preferred per-packet retransmission "
137*0Sstevel@tonic-gate 				"timeout for server";
138*0Sstevel@tonic-gate static char	tshelp[] =	"toggle sending the transfer size option to "
139*0Sstevel@tonic-gate 				"the server";
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static struct cmd	cmdtab[] = {
142*0Sstevel@tonic-gate 	{ "connect",	chelp,		setpeer },
143*0Sstevel@tonic-gate 	{ "mode",	mhelp,		modecmd },
144*0Sstevel@tonic-gate 	{ "put",	shelp,		put },
145*0Sstevel@tonic-gate 	{ "get",	rhelp,		get },
146*0Sstevel@tonic-gate 	{ "quit",	qhelp,		quit },
147*0Sstevel@tonic-gate 	{ "verbose",	vhelp,		setverbose },
148*0Sstevel@tonic-gate 	{ "trace",	thelp,		settrace },
149*0Sstevel@tonic-gate 	{ "status",	sthelp,		status },
150*0Sstevel@tonic-gate 	{ "binary",	bnhelp,		setbinary },
151*0Sstevel@tonic-gate 	{ "ascii",	ashelp,		setascii },
152*0Sstevel@tonic-gate 	{ "rexmt",	xhelp,		setrexmt },
153*0Sstevel@tonic-gate 	{ "timeout",	ihelp,		settimeout },
154*0Sstevel@tonic-gate 	{ "blksize",	bshelp,		setblksize },
155*0Sstevel@tonic-gate 	{ "srexmt",	srhelp,		setsrexmt },
156*0Sstevel@tonic-gate 	{ "tsize",	tshelp,		settsize },
157*0Sstevel@tonic-gate 	{ "?",		hhelp,		help },
158*0Sstevel@tonic-gate 	{ NULL }
159*0Sstevel@tonic-gate };
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate #define	AMBIGCMD	(&cmdtab[NELEM(cmdtab)])
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate int
main(int argc,char ** argv)164*0Sstevel@tonic-gate main(int argc, char **argv)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	struct servent *sp;
167*0Sstevel@tonic-gate 	struct sockaddr_in6 sin6;
168*0Sstevel@tonic-gate 	int top;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	sp = getservbyname("tftp", "udp");
171*0Sstevel@tonic-gate 	default_port = (sp != NULL) ? sp->s_port : htons(IPPORT_TFTP);
172*0Sstevel@tonic-gate 	port = default_port;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	f = socket(AF_INET6, SOCK_DGRAM, 0);
175*0Sstevel@tonic-gate 	if (f < 0) {
176*0Sstevel@tonic-gate 		perror("tftp: socket");
177*0Sstevel@tonic-gate 		exit(3);
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	(void) memset(&sin6, 0, sizeof (sin6));
181*0Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
182*0Sstevel@tonic-gate 	if (bind(f, (struct sockaddr *)&sin6, sizeof (sin6)) < 0) {
183*0Sstevel@tonic-gate 		perror("tftp: bind");
184*0Sstevel@tonic-gate 		exit(1);
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	(void) strlcpy(mode, "netascii", sizeof (mode));
188*0Sstevel@tonic-gate 	(void) signal(SIGINT, intr);
189*0Sstevel@tonic-gate 	if (argc > 1) {
190*0Sstevel@tonic-gate 		if (setjmp(toplevel) != 0)
191*0Sstevel@tonic-gate 			exit(0);
192*0Sstevel@tonic-gate 		setpeer(argc, argv);
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	top = (setjmp(toplevel) == 0);
196*0Sstevel@tonic-gate 	for (;;)
197*0Sstevel@tonic-gate 		command(top);
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	/*NOTREACHED*/
200*0Sstevel@tonic-gate 	return (0);
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate /* Prompt for command argument, add to buffer with space separator */
204*0Sstevel@tonic-gate static int
prompt_for_arg(char * buffer,int buffer_size,char * prompt)205*0Sstevel@tonic-gate prompt_for_arg(char *buffer, int buffer_size, char *prompt)
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate 	int ch;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	if (strlcat(buffer, " ", buffer_size) >= buffer_size) {
210*0Sstevel@tonic-gate 		(void) fputs("?Line too long\n", stderr);
211*0Sstevel@tonic-gate 		return (-1);
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 	(void) printf("(%s) ", prompt);
214*0Sstevel@tonic-gate 	if (fgets(buffer + strlen(buffer), buffer_size - strlen(buffer),
215*0Sstevel@tonic-gate 		stdin) == NULL) {
216*0Sstevel@tonic-gate 		return (-1);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 	/* Flush what didn't fit in the buffer */
219*0Sstevel@tonic-gate 	if (buffer[strlen(buffer)-1] != '\n') {
220*0Sstevel@tonic-gate 		while (((ch = getchar()) != EOF) && (ch != '\n'))
221*0Sstevel@tonic-gate 			;
222*0Sstevel@tonic-gate 		(void) fputs("?Line too long\n", stderr);
223*0Sstevel@tonic-gate 		return (-1);
224*0Sstevel@tonic-gate 	} else {
225*0Sstevel@tonic-gate 		buffer[strlen(buffer)-1] = '\0';
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 	return (0);
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate static void
unknown_host(int error,char * hostname)231*0Sstevel@tonic-gate unknown_host(int error, char *hostname)
232*0Sstevel@tonic-gate {
233*0Sstevel@tonic-gate 	if (error == TRY_AGAIN)
234*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: Unknown host (try again later).\n",
235*0Sstevel@tonic-gate 		    hostname);
236*0Sstevel@tonic-gate 	else
237*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: Unknown host.\n", hostname);
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate static void
setpeer(int argc,char ** argv)241*0Sstevel@tonic-gate setpeer(int argc, char **argv)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 	struct hostent *host;
244*0Sstevel@tonic-gate 	int error_num;
245*0Sstevel@tonic-gate 	struct in6_addr ipv6addr;
246*0Sstevel@tonic-gate 	struct in_addr ipv4addr;
247*0Sstevel@tonic-gate 	char *hostnameinput;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	if (argc < 2) {
250*0Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "to") == -1)
251*0Sstevel@tonic-gate 			return;
252*0Sstevel@tonic-gate 		makeargv(&argc, &argv);
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 	if (argc > 3 || argc < 2) {
255*0Sstevel@tonic-gate 		(void) fprintf(stderr, "usage: %s host-name [port]\n",
256*0Sstevel@tonic-gate 		    argv[0]);
257*0Sstevel@tonic-gate 		return;
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 	hostnameinput = removebrackets(argv[1]);
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	(void) memset(&sin6, 0, sizeof (sin6));
262*0Sstevel@tonic-gate 	sin6.sin6_family = AF_INET6;
263*0Sstevel@tonic-gate 	if (host = getipnodebyname(hostnameinput, AF_INET6,
264*0Sstevel@tonic-gate 	    AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &error_num)) {
265*0Sstevel@tonic-gate 		(void) memcpy(&sin6.sin6_addr, host->h_addr_list[0],
266*0Sstevel@tonic-gate 		    host->h_length);
267*0Sstevel@tonic-gate 		/*
268*0Sstevel@tonic-gate 		 * If host->h_name is a IPv4-mapped IPv6 literal, we'll convert
269*0Sstevel@tonic-gate 		 * it to IPv4 literal address.
270*0Sstevel@tonic-gate 		 */
271*0Sstevel@tonic-gate 		if ((inet_pton(AF_INET6, host->h_name, &ipv6addr) > 0) &&
272*0Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED(&ipv6addr)) {
273*0Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(&ipv6addr, &ipv4addr);
274*0Sstevel@tonic-gate 			(void) inet_ntop(AF_INET, &ipv4addr, hostname,
275*0Sstevel@tonic-gate 			    sizeof (hostname));
276*0Sstevel@tonic-gate 		} else {
277*0Sstevel@tonic-gate 			(void) strlcpy(hostname, host->h_name,
278*0Sstevel@tonic-gate 			    sizeof (hostname));
279*0Sstevel@tonic-gate 		}
280*0Sstevel@tonic-gate 		freehostent(host);
281*0Sstevel@tonic-gate 	} else {
282*0Sstevel@tonic-gate 		/* Keeping with previous semantics */
283*0Sstevel@tonic-gate 		connected = 0;
284*0Sstevel@tonic-gate 		unknown_host(error_num, hostnameinput);
285*0Sstevel@tonic-gate 		return;
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	port = default_port;
289*0Sstevel@tonic-gate 	if (argc == 3) {
290*0Sstevel@tonic-gate 		port = atoi(argv[2]);
291*0Sstevel@tonic-gate 		if ((port < 1) || (port > 65535)) {
292*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: bad port number\n",
293*0Sstevel@tonic-gate 			    argv[2]);
294*0Sstevel@tonic-gate 			connected = 0;
295*0Sstevel@tonic-gate 			return;
296*0Sstevel@tonic-gate 		}
297*0Sstevel@tonic-gate 		port = htons(port);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 	connected = 1;
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate static struct modes {
303*0Sstevel@tonic-gate 	char *m_name;
304*0Sstevel@tonic-gate 	char *m_mode;
305*0Sstevel@tonic-gate } modes[] = {
306*0Sstevel@tonic-gate 	{ "ascii",	"netascii" },
307*0Sstevel@tonic-gate 	{ "netascii",   "netascii" },
308*0Sstevel@tonic-gate 	{ "binary",     "octet" },
309*0Sstevel@tonic-gate 	{ "image",      "octet" },
310*0Sstevel@tonic-gate 	{ "octet",     "octet" },
311*0Sstevel@tonic-gate /*      { "mail",       "mail" },       */
312*0Sstevel@tonic-gate 	{ 0,		0 }
313*0Sstevel@tonic-gate };
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate static void
modecmd(int argc,char ** argv)316*0Sstevel@tonic-gate modecmd(int argc, char **argv)
317*0Sstevel@tonic-gate {
318*0Sstevel@tonic-gate 	struct modes *p;
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	if (argc < 2) {
321*0Sstevel@tonic-gate 		(void) fprintf(stderr, "Using %s mode to transfer files.\n",
322*0Sstevel@tonic-gate 		    mode);
323*0Sstevel@tonic-gate 		return;
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 	if (argc == 2) {
326*0Sstevel@tonic-gate 		for (p = modes; p->m_name != NULL; p++)
327*0Sstevel@tonic-gate 			if (strcmp(argv[1], p->m_name) == 0) {
328*0Sstevel@tonic-gate 				setmode(p->m_mode);
329*0Sstevel@tonic-gate 				return;
330*0Sstevel@tonic-gate 			}
331*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: unknown mode\n", argv[1]);
332*0Sstevel@tonic-gate 		/* drop through and print usage message */
333*0Sstevel@tonic-gate 	}
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 	p = modes;
336*0Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: %s [ %s", argv[0], p->m_name);
337*0Sstevel@tonic-gate 	for (p++; p->m_name != NULL; p++)
338*0Sstevel@tonic-gate 		(void) fprintf(stderr, " | %s", p->m_name);
339*0Sstevel@tonic-gate 	(void) puts(" ]");
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate /*ARGSUSED*/
343*0Sstevel@tonic-gate static void
setbinary(int argc,char ** argv)344*0Sstevel@tonic-gate setbinary(int argc, char **argv)
345*0Sstevel@tonic-gate {
346*0Sstevel@tonic-gate 	setmode("octet");
347*0Sstevel@tonic-gate }
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate /*ARGSUSED*/
350*0Sstevel@tonic-gate static void
setascii(int argc,char ** argv)351*0Sstevel@tonic-gate setascii(int argc, char **argv)
352*0Sstevel@tonic-gate {
353*0Sstevel@tonic-gate 	setmode("netascii");
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate static void
setmode(char * newmode)357*0Sstevel@tonic-gate setmode(char *newmode)
358*0Sstevel@tonic-gate {
359*0Sstevel@tonic-gate 	(void) strlcpy(mode, newmode, sizeof (mode));
360*0Sstevel@tonic-gate 	if (verbose)
361*0Sstevel@tonic-gate 		(void) printf("mode set to %s\n", mode);
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate /*
365*0Sstevel@tonic-gate  * Send file(s).
366*0Sstevel@tonic-gate  */
367*0Sstevel@tonic-gate static void
put(int argc,char ** argv)368*0Sstevel@tonic-gate put(int argc, char **argv)
369*0Sstevel@tonic-gate {
370*0Sstevel@tonic-gate 	int fd;
371*0Sstevel@tonic-gate 	int n;
372*0Sstevel@tonic-gate 	char *cp, *targ;
373*0Sstevel@tonic-gate 	struct in6_addr	ipv6addr;
374*0Sstevel@tonic-gate 	struct in_addr ipv4addr;
375*0Sstevel@tonic-gate 	char buf[PATH_MAX + 1], *argtail;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	if (argc < 2) {
378*0Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "file") == -1)
379*0Sstevel@tonic-gate 			return;
380*0Sstevel@tonic-gate 		makeargv(&argc, &argv);
381*0Sstevel@tonic-gate 	}
382*0Sstevel@tonic-gate 	if (argc < 2) {
383*0Sstevel@tonic-gate 		putusage(argv[0]);
384*0Sstevel@tonic-gate 		return;
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 	targ = argv[argc - 1];
387*0Sstevel@tonic-gate 	if (finddelimiter(argv[argc - 1])) {
388*0Sstevel@tonic-gate 		char *cp;
389*0Sstevel@tonic-gate 		struct hostent *hp;
390*0Sstevel@tonic-gate 		int error_num;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 		for (n = 1; n < argc - 1; n++)
393*0Sstevel@tonic-gate 			if (finddelimiter(argv[n])) {
394*0Sstevel@tonic-gate 				putusage(argv[0]);
395*0Sstevel@tonic-gate 				return;
396*0Sstevel@tonic-gate 			}
397*0Sstevel@tonic-gate 		cp = argv[argc - 1];
398*0Sstevel@tonic-gate 		targ = finddelimiter(cp);
399*0Sstevel@tonic-gate 		*targ++ = 0;
400*0Sstevel@tonic-gate 		cp = removebrackets(cp);
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 		if ((hp = getipnodebyname(cp,
403*0Sstevel@tonic-gate 		    AF_INET6, AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED,
404*0Sstevel@tonic-gate 		    &error_num)) == NULL) {
405*0Sstevel@tonic-gate 			unknown_host(error_num, cp);
406*0Sstevel@tonic-gate 			return;
407*0Sstevel@tonic-gate 		}
408*0Sstevel@tonic-gate 		(void) memcpy(&sin6.sin6_addr, hp->h_addr_list[0],
409*0Sstevel@tonic-gate 		    hp->h_length);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 		sin6.sin6_family = AF_INET6;
412*0Sstevel@tonic-gate 		connected = 1;
413*0Sstevel@tonic-gate 		/*
414*0Sstevel@tonic-gate 		 * If hp->h_name is a IPv4-mapped IPv6 literal, we'll convert
415*0Sstevel@tonic-gate 		 * it to IPv4 literal address.
416*0Sstevel@tonic-gate 		 */
417*0Sstevel@tonic-gate 		if ((inet_pton(AF_INET6, hp->h_name, &ipv6addr) > 0) &&
418*0Sstevel@tonic-gate 		    IN6_IS_ADDR_V4MAPPED(&ipv6addr)) {
419*0Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(&ipv6addr, &ipv4addr);
420*0Sstevel@tonic-gate 			(void) inet_ntop(AF_INET, &ipv4addr, hostname,
421*0Sstevel@tonic-gate 			    sizeof (hostname));
422*0Sstevel@tonic-gate 		} else {
423*0Sstevel@tonic-gate 			(void) strlcpy(hostname, hp->h_name,
424*0Sstevel@tonic-gate 			    sizeof (hostname));
425*0Sstevel@tonic-gate 		}
426*0Sstevel@tonic-gate 	}
427*0Sstevel@tonic-gate 	if (!connected) {
428*0Sstevel@tonic-gate 		(void) fputs("No target machine specified.\n", stderr);
429*0Sstevel@tonic-gate 		return;
430*0Sstevel@tonic-gate 	}
431*0Sstevel@tonic-gate 	if (argc < 4) {
432*0Sstevel@tonic-gate 		cp = argc == 2 ? tail(targ) : argv[1];
433*0Sstevel@tonic-gate 		fd = open(cp, O_RDONLY);
434*0Sstevel@tonic-gate 		if (fd < 0) {
435*0Sstevel@tonic-gate 			(void) fprintf(stderr, "tftp: %s: %s\n", cp,
436*0Sstevel@tonic-gate 			    strerror(errno));
437*0Sstevel@tonic-gate 			return;
438*0Sstevel@tonic-gate 		}
439*0Sstevel@tonic-gate 		if (verbose)
440*0Sstevel@tonic-gate 			(void) printf("putting %s to %s:%s [%s]\n",
441*0Sstevel@tonic-gate 				cp, hostname, targ, mode);
442*0Sstevel@tonic-gate 		sin6.sin6_port = port;
443*0Sstevel@tonic-gate 		tftp_sendfile(fd, targ, mode);
444*0Sstevel@tonic-gate 		return;
445*0Sstevel@tonic-gate 	}
446*0Sstevel@tonic-gate 	/* this assumes the target is a directory */
447*0Sstevel@tonic-gate 	/* on a remote unix system.  hmmmm.  */
448*0Sstevel@tonic-gate 	if (strlen(targ) + 1 >= sizeof (buf)) {
449*0Sstevel@tonic-gate 		(void) fprintf(stderr, "tftp: filename too long: %s\n", targ);
450*0Sstevel@tonic-gate 		return;
451*0Sstevel@tonic-gate 	}
452*0Sstevel@tonic-gate 	for (n = 1; n < argc - 1; n++) {
453*0Sstevel@tonic-gate 		argtail = tail(argv[n]);
454*0Sstevel@tonic-gate 		if (snprintf(buf, sizeof (buf), "%s/%s", targ, argtail) >=
455*0Sstevel@tonic-gate 		    sizeof (buf)) {
456*0Sstevel@tonic-gate 			(void) fprintf(stderr,
457*0Sstevel@tonic-gate 			    "tftp: filename too long: %s/%s\n", targ, argtail);
458*0Sstevel@tonic-gate 			continue;
459*0Sstevel@tonic-gate 		}
460*0Sstevel@tonic-gate 		fd = open(argv[n], O_RDONLY);
461*0Sstevel@tonic-gate 		if (fd < 0) {
462*0Sstevel@tonic-gate 			(void) fprintf(stderr, "tftp: %s: %s\n", argv[n],
463*0Sstevel@tonic-gate 			    strerror(errno));
464*0Sstevel@tonic-gate 			continue;
465*0Sstevel@tonic-gate 		}
466*0Sstevel@tonic-gate 		if (verbose)
467*0Sstevel@tonic-gate 			(void) printf("putting %s to %s:%s [%s]\n",
468*0Sstevel@tonic-gate 				argv[n], hostname, buf, mode);
469*0Sstevel@tonic-gate 		sin6.sin6_port = port;
470*0Sstevel@tonic-gate 		tftp_sendfile(fd, buf, mode);
471*0Sstevel@tonic-gate 	}
472*0Sstevel@tonic-gate }
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate static void
putusage(char * s)475*0Sstevel@tonic-gate putusage(char *s)
476*0Sstevel@tonic-gate {
477*0Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: %s file ... host:target, or\n"
478*0Sstevel@tonic-gate 	    "       %s file ... target (when already connected)\n", s, s);
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate /*
482*0Sstevel@tonic-gate  * Receive file(s).
483*0Sstevel@tonic-gate  */
484*0Sstevel@tonic-gate static void
get(int argc,char ** argv)485*0Sstevel@tonic-gate get(int argc, char **argv)
486*0Sstevel@tonic-gate {
487*0Sstevel@tonic-gate 	int fd;
488*0Sstevel@tonic-gate 	int n;
489*0Sstevel@tonic-gate 	char *cp;
490*0Sstevel@tonic-gate 	char *src;
491*0Sstevel@tonic-gate 	struct in6_addr ipv6addr;
492*0Sstevel@tonic-gate 	struct in_addr ipv4addr;
493*0Sstevel@tonic-gate 	int error_num;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	if (argc < 2) {
496*0Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "files") == -1)
497*0Sstevel@tonic-gate 			return;
498*0Sstevel@tonic-gate 		makeargv(&argc, &argv);
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate 	if (argc < 2) {
501*0Sstevel@tonic-gate 		getusage(argv[0]);
502*0Sstevel@tonic-gate 		return;
503*0Sstevel@tonic-gate 	}
504*0Sstevel@tonic-gate 	if (!connected) {
505*0Sstevel@tonic-gate 		for (n = 1; n < argc; n++)
506*0Sstevel@tonic-gate 			if (finddelimiter(argv[n]) == 0) {
507*0Sstevel@tonic-gate 				getusage(argv[0]);
508*0Sstevel@tonic-gate 				return;
509*0Sstevel@tonic-gate 			}
510*0Sstevel@tonic-gate 	}
511*0Sstevel@tonic-gate 	for (n = 1; n < argc; n++) {
512*0Sstevel@tonic-gate 		src = finddelimiter(argv[n]);
513*0Sstevel@tonic-gate 		if (src == NULL)
514*0Sstevel@tonic-gate 			src = argv[n];
515*0Sstevel@tonic-gate 		else {
516*0Sstevel@tonic-gate 			struct hostent *hp;
517*0Sstevel@tonic-gate 			char *hostnameinput;
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 			*src++ = 0;
520*0Sstevel@tonic-gate 			hostnameinput = removebrackets(argv[n]);
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 			if ((hp = getipnodebyname(hostnameinput, AF_INET6,
523*0Sstevel@tonic-gate 			    AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED,
524*0Sstevel@tonic-gate 			    &error_num)) == NULL) {
525*0Sstevel@tonic-gate 				unknown_host(error_num, hostnameinput);
526*0Sstevel@tonic-gate 				continue;
527*0Sstevel@tonic-gate 			}
528*0Sstevel@tonic-gate 			(void) memcpy((caddr_t)&sin6.sin6_addr,
529*0Sstevel@tonic-gate 			    hp->h_addr_list[0], hp->h_length);
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 			sin6.sin6_family = AF_INET6;
532*0Sstevel@tonic-gate 			connected = 1;
533*0Sstevel@tonic-gate 			/*
534*0Sstevel@tonic-gate 			 * If hp->h_name is a IPv4-mapped IPv6 literal, we'll
535*0Sstevel@tonic-gate 			 * convert it to IPv4 literal address.
536*0Sstevel@tonic-gate 			 */
537*0Sstevel@tonic-gate 			if ((inet_pton(AF_INET6, hp->h_name, &ipv6addr) > 0) &&
538*0Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED(&ipv6addr)) {
539*0Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(&ipv6addr, &ipv4addr);
540*0Sstevel@tonic-gate 				(void) inet_ntop(AF_INET, &ipv4addr, hostname,
541*0Sstevel@tonic-gate 				    sizeof (hostname));
542*0Sstevel@tonic-gate 			} else {
543*0Sstevel@tonic-gate 				(void) strlcpy(hostname, hp->h_name,
544*0Sstevel@tonic-gate 				    sizeof (hostname));
545*0Sstevel@tonic-gate 			}
546*0Sstevel@tonic-gate 		}
547*0Sstevel@tonic-gate 		if (argc < 4) {
548*0Sstevel@tonic-gate 			cp = argc == 3 ? argv[2] : tail(src);
549*0Sstevel@tonic-gate 			fd = creat(cp, 0644);
550*0Sstevel@tonic-gate 			if (fd < 0) {
551*0Sstevel@tonic-gate 				(void) fprintf(stderr, "tftp: %s: %s\n", cp,
552*0Sstevel@tonic-gate 				    strerror(errno));
553*0Sstevel@tonic-gate 				return;
554*0Sstevel@tonic-gate 			}
555*0Sstevel@tonic-gate 			if (verbose)
556*0Sstevel@tonic-gate 				(void) printf("getting from %s:%s to %s [%s]\n",
557*0Sstevel@tonic-gate 					hostname, src, cp, mode);
558*0Sstevel@tonic-gate 			sin6.sin6_port = port;
559*0Sstevel@tonic-gate 			tftp_recvfile(fd, src, mode);
560*0Sstevel@tonic-gate 			break;
561*0Sstevel@tonic-gate 		}
562*0Sstevel@tonic-gate 		cp = tail(src);	/* new .. jdg */
563*0Sstevel@tonic-gate 		fd = creat(cp, 0644);
564*0Sstevel@tonic-gate 		if (fd < 0) {
565*0Sstevel@tonic-gate 			(void) fprintf(stderr, "tftp: %s: %s\n", cp,
566*0Sstevel@tonic-gate 			    strerror(errno));
567*0Sstevel@tonic-gate 			continue;
568*0Sstevel@tonic-gate 		}
569*0Sstevel@tonic-gate 		if (verbose)
570*0Sstevel@tonic-gate 			(void) printf("getting from %s:%s to %s [%s]\n",
571*0Sstevel@tonic-gate 			    hostname, src, cp, mode);
572*0Sstevel@tonic-gate 		sin6.sin6_port = port;
573*0Sstevel@tonic-gate 		tftp_recvfile(fd, src, mode);
574*0Sstevel@tonic-gate 	}
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate static void
getusage(char * s)578*0Sstevel@tonic-gate getusage(char *s)
579*0Sstevel@tonic-gate {
580*0Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: %s host:file host:file ... file, or\n"
581*0Sstevel@tonic-gate 	    "       %s file file ... file if connected\n", s, s);
582*0Sstevel@tonic-gate }
583*0Sstevel@tonic-gate 
584*0Sstevel@tonic-gate static void
setrexmt(int argc,char ** argv)585*0Sstevel@tonic-gate setrexmt(int argc, char **argv)
586*0Sstevel@tonic-gate {
587*0Sstevel@tonic-gate 	int t;
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	if (argc < 2) {
590*0Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "value") == -1)
591*0Sstevel@tonic-gate 			return;
592*0Sstevel@tonic-gate 		makeargv(&argc, &argv);
593*0Sstevel@tonic-gate 	}
594*0Sstevel@tonic-gate 	if (argc != 2) {
595*0Sstevel@tonic-gate 		(void) fprintf(stderr, "usage: %s value\n", argv[0]);
596*0Sstevel@tonic-gate 		return;
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 	t = atoi(argv[1]);
599*0Sstevel@tonic-gate 	if (t < 0)
600*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: bad value\n", argv[1]);
601*0Sstevel@tonic-gate 	else
602*0Sstevel@tonic-gate 		rexmtval = t;
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate static void
settimeout(int argc,char ** argv)606*0Sstevel@tonic-gate settimeout(int argc, char **argv)
607*0Sstevel@tonic-gate {
608*0Sstevel@tonic-gate 	int t;
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 	if (argc < 2) {
611*0Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "value") == -1)
612*0Sstevel@tonic-gate 			return;
613*0Sstevel@tonic-gate 		makeargv(&argc, &argv);
614*0Sstevel@tonic-gate 	}
615*0Sstevel@tonic-gate 	if (argc != 2) {
616*0Sstevel@tonic-gate 		(void) fprintf(stderr, "usage: %s value\n", argv[0]);
617*0Sstevel@tonic-gate 		return;
618*0Sstevel@tonic-gate 	}
619*0Sstevel@tonic-gate 	t = atoi(argv[1]);
620*0Sstevel@tonic-gate 	if (t < 0)
621*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: bad value\n", argv[1]);
622*0Sstevel@tonic-gate 	else
623*0Sstevel@tonic-gate 		maxtimeout = t;
624*0Sstevel@tonic-gate }
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate /*ARGSUSED*/
627*0Sstevel@tonic-gate static void
status(int argc,char ** argv)628*0Sstevel@tonic-gate status(int argc, char **argv)
629*0Sstevel@tonic-gate {
630*0Sstevel@tonic-gate 	if (connected)
631*0Sstevel@tonic-gate 		(void) printf("Connected to %s.\n", hostname);
632*0Sstevel@tonic-gate 	else
633*0Sstevel@tonic-gate 		(void) puts("Not connected.");
634*0Sstevel@tonic-gate 	(void) printf("Mode: %s Verbose: %s Tracing: %s\n", mode,
635*0Sstevel@tonic-gate 		verbose ? "on" : "off", trace ? "on" : "off");
636*0Sstevel@tonic-gate 	(void) printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
637*0Sstevel@tonic-gate 		rexmtval, maxtimeout);
638*0Sstevel@tonic-gate 	(void) printf("Transfer blocksize option: ");
639*0Sstevel@tonic-gate 	if (blksize == 0)
640*0Sstevel@tonic-gate 		(void) puts("off");
641*0Sstevel@tonic-gate 	else
642*0Sstevel@tonic-gate 		(void) printf("%d bytes\n", blksize);
643*0Sstevel@tonic-gate 	(void) printf("Server rexmt-interval option: ");
644*0Sstevel@tonic-gate 	if (srexmtval == 0)
645*0Sstevel@tonic-gate 		(void) puts("off");
646*0Sstevel@tonic-gate 	else
647*0Sstevel@tonic-gate 		(void) printf("%d seconds\n", srexmtval);
648*0Sstevel@tonic-gate 	(void) printf("Transfer size option: %s\n", tsize_opt ? "on" : "off");
649*0Sstevel@tonic-gate }
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate /*ARGSUSED*/
652*0Sstevel@tonic-gate static void
intr(int signum)653*0Sstevel@tonic-gate intr(int signum)
654*0Sstevel@tonic-gate {
655*0Sstevel@tonic-gate 	(void) cancel_alarm();
656*0Sstevel@tonic-gate 	longjmp(toplevel, -1);
657*0Sstevel@tonic-gate }
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate static char *
tail(char * filename)660*0Sstevel@tonic-gate tail(char *filename)
661*0Sstevel@tonic-gate {
662*0Sstevel@tonic-gate 	char *s;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	while (*filename != '\0') {
665*0Sstevel@tonic-gate 		s = strrchr(filename, '/');
666*0Sstevel@tonic-gate 		if (s == NULL)
667*0Sstevel@tonic-gate 			break;
668*0Sstevel@tonic-gate 		if (s[1] != '\0')
669*0Sstevel@tonic-gate 			return (&s[1]);
670*0Sstevel@tonic-gate 		*s = '\0';
671*0Sstevel@tonic-gate 	}
672*0Sstevel@tonic-gate 	return (filename);
673*0Sstevel@tonic-gate }
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate /*
676*0Sstevel@tonic-gate  * Command parser.
677*0Sstevel@tonic-gate  */
678*0Sstevel@tonic-gate static void
command(int top)679*0Sstevel@tonic-gate command(int top)
680*0Sstevel@tonic-gate {
681*0Sstevel@tonic-gate 	struct cmd *c;
682*0Sstevel@tonic-gate 	int ch;
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	if (!top)
685*0Sstevel@tonic-gate 		(void) putchar('\n');
686*0Sstevel@tonic-gate 	for (;;) {
687*0Sstevel@tonic-gate 		(void) printf("%s> ", prompt);
688*0Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL) {
689*0Sstevel@tonic-gate 			if (feof(stdin))
690*0Sstevel@tonic-gate 				quit(0, NULL);
691*0Sstevel@tonic-gate 			else
692*0Sstevel@tonic-gate 				continue;
693*0Sstevel@tonic-gate 		}
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 		/* Flush what didn't fit in the buffer */
696*0Sstevel@tonic-gate 		if (line[strlen(line)-1] != '\n') {
697*0Sstevel@tonic-gate 			while (((ch = getchar()) != EOF) && (ch != '\n'))
698*0Sstevel@tonic-gate 				;
699*0Sstevel@tonic-gate 			(void) fputs("?Line too long\n", stderr);
700*0Sstevel@tonic-gate 		} else {
701*0Sstevel@tonic-gate 			line[strlen(line)-1] = '\0';
702*0Sstevel@tonic-gate 			if (line[0] != '\0') {
703*0Sstevel@tonic-gate 				int	argc;
704*0Sstevel@tonic-gate 				char	**argv;
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 				makeargv(&argc, &argv);
707*0Sstevel@tonic-gate 				c = getcmd(argv[0]);
708*0Sstevel@tonic-gate 				if (c == AMBIGCMD)
709*0Sstevel@tonic-gate 					(void) fputs("?Ambiguous command\n",
710*0Sstevel@tonic-gate 					    stderr);
711*0Sstevel@tonic-gate 				else if (c == NULL)
712*0Sstevel@tonic-gate 					(void) fputs("?Invalid command\n",
713*0Sstevel@tonic-gate 					    stderr);
714*0Sstevel@tonic-gate 				else
715*0Sstevel@tonic-gate 					(*c->handler)(argc, argv);
716*0Sstevel@tonic-gate 			}
717*0Sstevel@tonic-gate 		}
718*0Sstevel@tonic-gate 	}
719*0Sstevel@tonic-gate }
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate static struct cmd *
getcmd(char * name)722*0Sstevel@tonic-gate getcmd(char *name)
723*0Sstevel@tonic-gate {
724*0Sstevel@tonic-gate 	char *p, *q;
725*0Sstevel@tonic-gate 	struct cmd *c, *found;
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 	if (name == NULL)
728*0Sstevel@tonic-gate 		return (NULL);
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	found = NULL;
731*0Sstevel@tonic-gate 	for (c = cmdtab; (p = c->name) != NULL; c++) {
732*0Sstevel@tonic-gate 		for (q = name; *q == *p++; q++)
733*0Sstevel@tonic-gate 			if (*q == '\0')		/* exact match? */
734*0Sstevel@tonic-gate 			    return (c);
735*0Sstevel@tonic-gate 		if (*q == '\0')		/* the name was a prefix */
736*0Sstevel@tonic-gate 			found = (found == NULL) ? c : AMBIGCMD;
737*0Sstevel@tonic-gate 	}
738*0Sstevel@tonic-gate 	return (found);
739*0Sstevel@tonic-gate }
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate /*
742*0Sstevel@tonic-gate  * Given a string, this function returns the pointer to the delimiting ':'.
743*0Sstevel@tonic-gate  * The string can contain an IPv6 literal address, which should be inside a
744*0Sstevel@tonic-gate  * pair of brackets, e.g. [1::2]. Any colons inside a pair of brackets are not
745*0Sstevel@tonic-gate  * accepted as delimiters. Returns NULL if delimiting ':' is not found.
746*0Sstevel@tonic-gate  */
747*0Sstevel@tonic-gate static char *
finddelimiter(char * str)748*0Sstevel@tonic-gate finddelimiter(char *str)
749*0Sstevel@tonic-gate {
750*0Sstevel@tonic-gate 	boolean_t is_bracket_open = B_FALSE;
751*0Sstevel@tonic-gate 	char *cp;
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 	for (cp = str; *cp != '\0'; cp++) {
754*0Sstevel@tonic-gate 		if (*cp == '[')
755*0Sstevel@tonic-gate 			is_bracket_open = B_TRUE;
756*0Sstevel@tonic-gate 		else if (*cp == ']')
757*0Sstevel@tonic-gate 			is_bracket_open = B_FALSE;
758*0Sstevel@tonic-gate 		else if (*cp == ':' && !is_bracket_open)
759*0Sstevel@tonic-gate 			return (cp);
760*0Sstevel@tonic-gate 	}
761*0Sstevel@tonic-gate 	return (NULL);
762*0Sstevel@tonic-gate }
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate /*
765*0Sstevel@tonic-gate  * Given a string which is possibly surrounded by brackets, e.g. [1::2], this
766*0Sstevel@tonic-gate  * function returns a string after removing those brackets. If the brackets
767*0Sstevel@tonic-gate  * don't match, it does nothing.
768*0Sstevel@tonic-gate  */
769*0Sstevel@tonic-gate static char *
removebrackets(char * str)770*0Sstevel@tonic-gate removebrackets(char *str)
771*0Sstevel@tonic-gate {
772*0Sstevel@tonic-gate 	char *newstr = str;
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	if ((str[0] == '[') && (str[strlen(str) - 1] == ']')) {
775*0Sstevel@tonic-gate 		newstr = str + 1;
776*0Sstevel@tonic-gate 		str[strlen(str) - 1] = '\0';
777*0Sstevel@tonic-gate 	}
778*0Sstevel@tonic-gate 	return (newstr);
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate #define	MARGV_INC	20
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate /*
784*0Sstevel@tonic-gate  * Slice a string up into argc/argv.
785*0Sstevel@tonic-gate  */
786*0Sstevel@tonic-gate static void
makeargv(int * argcp,char *** argvp)787*0Sstevel@tonic-gate makeargv(int *argcp, char ***argvp)
788*0Sstevel@tonic-gate {
789*0Sstevel@tonic-gate 	char *cp;
790*0Sstevel@tonic-gate 	char **argp;
791*0Sstevel@tonic-gate 	int argc;
792*0Sstevel@tonic-gate 	static char **argv;
793*0Sstevel@tonic-gate 	static int argv_size;
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 	if (argv == NULL) {
796*0Sstevel@tonic-gate 		argv_size = MARGV_INC;
797*0Sstevel@tonic-gate 		if ((argv = malloc(argv_size * sizeof (char *))) == NULL) {
798*0Sstevel@tonic-gate 			perror("tftp: malloc");
799*0Sstevel@tonic-gate 			exit(1);
800*0Sstevel@tonic-gate 		}
801*0Sstevel@tonic-gate 	}
802*0Sstevel@tonic-gate 	argc = 0;
803*0Sstevel@tonic-gate 	argp = argv;
804*0Sstevel@tonic-gate 	for (cp = line; *cp != '\0'; ) {
805*0Sstevel@tonic-gate 		while (isspace(*cp))
806*0Sstevel@tonic-gate 			cp++;
807*0Sstevel@tonic-gate 		if (*cp == '\0')
808*0Sstevel@tonic-gate 			break;
809*0Sstevel@tonic-gate 		*argp++ = cp;
810*0Sstevel@tonic-gate 		argc++;
811*0Sstevel@tonic-gate 		if (argc == argv_size) {
812*0Sstevel@tonic-gate 			argv_size += MARGV_INC;
813*0Sstevel@tonic-gate 			if ((argv = realloc(argv,
814*0Sstevel@tonic-gate 			    argv_size * sizeof (char *))) == NULL) {
815*0Sstevel@tonic-gate 				perror("tftp: realloc");
816*0Sstevel@tonic-gate 				exit(1);
817*0Sstevel@tonic-gate 			}
818*0Sstevel@tonic-gate 			argp = argv + argc;
819*0Sstevel@tonic-gate 		}
820*0Sstevel@tonic-gate 		while (*cp != '\0' && !isspace(*cp))
821*0Sstevel@tonic-gate 			cp++;
822*0Sstevel@tonic-gate 		if (*cp == '\0')
823*0Sstevel@tonic-gate 			break;
824*0Sstevel@tonic-gate 		*cp++ = '\0';
825*0Sstevel@tonic-gate 	}
826*0Sstevel@tonic-gate 	*argp = NULL;
827*0Sstevel@tonic-gate 
828*0Sstevel@tonic-gate 	*argcp = argc;
829*0Sstevel@tonic-gate 	*argvp = argv;
830*0Sstevel@tonic-gate }
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate /*ARGSUSED*/
833*0Sstevel@tonic-gate static void
quit(int argc,char ** argv)834*0Sstevel@tonic-gate quit(int argc, char **argv)
835*0Sstevel@tonic-gate {
836*0Sstevel@tonic-gate 	exit(0);
837*0Sstevel@tonic-gate }
838*0Sstevel@tonic-gate 
839*0Sstevel@tonic-gate /*
840*0Sstevel@tonic-gate  * Help command.
841*0Sstevel@tonic-gate  */
842*0Sstevel@tonic-gate static void
help(int argc,char ** argv)843*0Sstevel@tonic-gate help(int argc, char **argv)
844*0Sstevel@tonic-gate {
845*0Sstevel@tonic-gate 	struct cmd *c;
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	if (argc == 1) {
848*0Sstevel@tonic-gate 		(void) puts("Commands may be abbreviated.  Commands are:\n");
849*0Sstevel@tonic-gate 		for (c = cmdtab; c->name != NULL; c++)
850*0Sstevel@tonic-gate 			(void) printf("%-*s\t%s\n", HELPINDENT, c->name,
851*0Sstevel@tonic-gate 			    c->help);
852*0Sstevel@tonic-gate 		return;
853*0Sstevel@tonic-gate 	}
854*0Sstevel@tonic-gate 	while (--argc > 0) {
855*0Sstevel@tonic-gate 		char *arg;
856*0Sstevel@tonic-gate 		arg = *++argv;
857*0Sstevel@tonic-gate 		c = getcmd(arg);
858*0Sstevel@tonic-gate 		if (c == AMBIGCMD)
859*0Sstevel@tonic-gate 			(void) fprintf(stderr, "?Ambiguous help command %s\n",
860*0Sstevel@tonic-gate 			    arg);
861*0Sstevel@tonic-gate 		else if (c == NULL)
862*0Sstevel@tonic-gate 			(void) fprintf(stderr, "?Invalid help command %s\n",
863*0Sstevel@tonic-gate 			    arg);
864*0Sstevel@tonic-gate 		else
865*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", c->help);
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate }
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate /*ARGSUSED*/
870*0Sstevel@tonic-gate static void
settrace(int argc,char ** argv)871*0Sstevel@tonic-gate settrace(int argc, char **argv)
872*0Sstevel@tonic-gate {
873*0Sstevel@tonic-gate 	trace = !trace;
874*0Sstevel@tonic-gate 	(void) printf("Packet tracing %s.\n", trace ? "on" : "off");
875*0Sstevel@tonic-gate }
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate /*ARGSUSED*/
878*0Sstevel@tonic-gate static void
setverbose(int argc,char ** argv)879*0Sstevel@tonic-gate setverbose(int argc, char **argv)
880*0Sstevel@tonic-gate {
881*0Sstevel@tonic-gate 	verbose = !verbose;
882*0Sstevel@tonic-gate 	(void) printf("Verbose mode %s.\n", verbose ? "on" : "off");
883*0Sstevel@tonic-gate }
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate static void
setblksize(int argc,char ** argv)886*0Sstevel@tonic-gate setblksize(int argc, char **argv)
887*0Sstevel@tonic-gate {
888*0Sstevel@tonic-gate 	int b;
889*0Sstevel@tonic-gate 
890*0Sstevel@tonic-gate 	if (argc < 2) {
891*0Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "value") == -1)
892*0Sstevel@tonic-gate 			return;
893*0Sstevel@tonic-gate 		makeargv(&argc, &argv);
894*0Sstevel@tonic-gate 	}
895*0Sstevel@tonic-gate 	if (argc != 2) {
896*0Sstevel@tonic-gate 		(void) fprintf(stderr, "usage: %s value\n", argv[0]);
897*0Sstevel@tonic-gate 		return;
898*0Sstevel@tonic-gate 	}
899*0Sstevel@tonic-gate 	b = atoi(argv[1]);
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	/* RFC 2348 specifies valid blksize range, allow 0 to turn option off */
902*0Sstevel@tonic-gate 	if ((b < MIN_BLKSIZE || b > MAX_BLKSIZE) && b != 0)
903*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: bad value\n", argv[1]);
904*0Sstevel@tonic-gate 	else
905*0Sstevel@tonic-gate 		blksize = b;
906*0Sstevel@tonic-gate }
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate static void
setsrexmt(int argc,char ** argv)909*0Sstevel@tonic-gate setsrexmt(int argc, char **argv)
910*0Sstevel@tonic-gate {
911*0Sstevel@tonic-gate 	int t;
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate 	if (argc < 2) {
914*0Sstevel@tonic-gate 		if (prompt_for_arg(line, sizeof (line), "value") == -1)
915*0Sstevel@tonic-gate 			return;
916*0Sstevel@tonic-gate 		makeargv(&argc, &argv);
917*0Sstevel@tonic-gate 	}
918*0Sstevel@tonic-gate 	if (argc != 2) {
919*0Sstevel@tonic-gate 		(void) fprintf(stderr, "usage: %s value\n", argv[0]);
920*0Sstevel@tonic-gate 		return;
921*0Sstevel@tonic-gate 	}
922*0Sstevel@tonic-gate 	t = atoi(argv[1]);
923*0Sstevel@tonic-gate 
924*0Sstevel@tonic-gate 	/* RFC 2349 specifies valid timeout range, allow 0 to turn option off */
925*0Sstevel@tonic-gate 	if ((t < MIN_TIMEOUT || t > MAX_TIMEOUT) && t != 0)
926*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: bad value\n", argv[1]);
927*0Sstevel@tonic-gate 	else
928*0Sstevel@tonic-gate 		srexmtval = t;
929*0Sstevel@tonic-gate }
930*0Sstevel@tonic-gate 
931*0Sstevel@tonic-gate static void
settsize(int argc,char ** argv)932*0Sstevel@tonic-gate settsize(int argc, char **argv)
933*0Sstevel@tonic-gate {
934*0Sstevel@tonic-gate 	if (argc != 1) {
935*0Sstevel@tonic-gate 		(void) fprintf(stderr, "usage: %s\n", argv[0]);
936*0Sstevel@tonic-gate 		return;
937*0Sstevel@tonic-gate 	}
938*0Sstevel@tonic-gate 	tsize_opt = !tsize_opt;
939*0Sstevel@tonic-gate 	(void) printf("Transfer size option %s.\n", tsize_opt ? "on" : "off");
940*0Sstevel@tonic-gate }
941