1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1983, 1993\n\
33	The Regents of the University of California.  All rights reserved.\n";
34#endif
35
36#if 0
37#ifndef lint
38static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
39#endif
40#endif
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD$");
44
45/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
46
47/*
48 * TFTP User Program -- Command Interface.
49 */
50#include <sys/param.h>
51#include <sys/types.h>
52#include <sys/socket.h>
53#include <sys/sysctl.h>
54#include <sys/file.h>
55#include <sys/stat.h>
56
57#include <netinet/in.h>
58#include <arpa/inet.h>
59#include <arpa/tftp.h>
60
61#include <ctype.h>
62#include <err.h>
63#include <histedit.h>
64#include <netdb.h>
65#include <setjmp.h>
66#include <signal.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70#include <unistd.h>
71
72#include "tftp-utils.h"
73#include "tftp-io.h"
74#include "tftp-options.h"
75#include "tftp.h"
76
77#define	MAXLINE		200
78#define	TIMEOUT		5		/* secs between rexmt's */
79
80typedef struct	sockaddr_storage peeraddr;
81static int	connected;
82static char	mode[32];
83static jmp_buf	toplevel;
84volatile int	txrx_error;
85static int	peer;
86
87#define	MAX_MARGV	20
88static int	margc;
89static char	*margv[MAX_MARGV];
90
91int		verbose;
92static char	*port = NULL;
93
94static void	get(int, char **);
95static void	help(int, char **);
96static void	intr(int);
97static void	modecmd(int, char **);
98static void	put(int, char **);
99static void	quit(int, char **);
100static void	setascii(int, char **);
101static void	setbinary(int, char **);
102static void	setpeer0(char *, const char *);
103static void	setpeer(int, char **);
104static void	settimeoutpacket(int, char **);
105static void	settimeoutnetwork(int, char **);
106static void	setdebug(int, char **);
107static void	setverbose(int, char **);
108static void	showstatus(int, char **);
109static void	setblocksize(int, char **);
110static void	setblocksize2(int, char **);
111static void	setoptions(int, char **);
112static void	setrollover(int, char **);
113static void	setpacketdrop(int, char **);
114
115static void command(void) __dead2;
116static const char *command_prompt(void);
117
118static void urihandling(char *URI);
119static void getusage(char *);
120static void makeargv(char *line);
121static void putusage(char *);
122static void settftpmode(const char *);
123
124static char	*tail(char *);
125static struct	cmd *getcmd(char *);
126
127#define HELPINDENT (sizeof("connect"))
128
129struct cmd {
130	const char	*name;
131	void	(*handler)(int, char **);
132	const char	*help;
133};
134
135static struct cmd cmdtab[] = {
136	{ "connect",	setpeer,	"connect to remote tftp"	},
137	{ "mode",	modecmd,	"set file transfer mode"	},
138	{ "put",	put,		"send file"			},
139	{ "get",	get,		"receive file"			},
140	{ "quit",	quit,		"exit tftp"			},
141	{ "verbose",	setverbose,	"toggle verbose mode"		},
142	{ "status",	showstatus,	"show current status"		},
143	{ "binary",     setbinary,	"set mode to octet"		},
144	{ "ascii",      setascii,	"set mode to netascii"		},
145	{ "rexmt",	settimeoutpacket,
146	  "set per-packet retransmission timeout[-]" },
147	{ "timeout",	settimeoutnetwork,
148	  "set total retransmission timeout" },
149	{ "trace",	setdebug,	"enable 'debug packet'[-]"	},
150	{ "debug",	setdebug,	"enable verbose output"		},
151	{ "blocksize",	setblocksize,	"set blocksize[*]"		},
152	{ "blocksize2",	setblocksize2,	"set blocksize as a power of 2[**]" },
153	{ "rollover",	setrollover,	"rollover after 64K packets[**]" },
154	{ "options",	setoptions,
155	  "enable or disable RFC2347 style options" },
156	{ "help",	help,		"print help information"	},
157	{ "packetdrop",	setpacketdrop,	"artificial packetloss feature"	},
158	{ "?",		help,		"print help information"	},
159	{ NULL,		NULL,		NULL				}
160};
161
162static struct	modes {
163	const char *m_name;
164	const char *m_mode;
165} modes[] = {
166	{ "ascii",	"netascii" },
167	{ "netascii",	"netascii" },
168	{ "binary",	"octet" },
169	{ "image",	"octet" },
170	{ "octet",	"octet" },
171	{ NULL,		NULL }
172};
173
174int
175main(int argc, char *argv[])
176{
177
178	acting_as_client = 1;
179	peer = -1;
180	strcpy(mode, "netascii");
181	signal(SIGINT, intr);
182	if (argc > 1) {
183		if (setjmp(toplevel) != 0)
184			exit(txrx_error);
185
186		if (strncmp(argv[1], "tftp://", 7) == 0) {
187			urihandling(argv[1]);
188			exit(txrx_error);
189		}
190
191		setpeer(argc, argv);
192	}
193	if (setjmp(toplevel) != 0)
194		(void)putchar('\n');
195
196	init_options();
197	command();
198}
199
200/*
201 * RFC3617 handling of TFTP URIs:
202 *
203 *    tftpURI         = "tftp://" host "/" file [ mode ]
204 *    mode            = ";"  "mode=" ( "netascii" / "octet" )
205 *    file            = *( unreserved / escaped )
206 *    host            = <as specified by RFC 2732>
207 *    unreserved      = <as specified in RFC 2396>
208 *    escaped         = <as specified in RFC 2396>
209 *
210 * We are cheating a little bit by allowing any mode as specified in the
211 * modes table defined earlier on in this file and mapping it on the real
212 * mode.
213 */
214static void
215urihandling(char *URI)
216{
217	char	uri[ARG_MAX];
218	char	*host = NULL;
219	char	*path = NULL;
220	char	*opts = NULL;
221	const char *tmode = "octet";
222	char	*s;
223	char	line[MAXLINE];
224	int	i;
225
226	strncpy(uri, URI, ARG_MAX);
227	host = uri + 7;
228
229	if ((s = strchr(host, '/')) == NULL) {
230		fprintf(stderr,
231		    "Invalid URI: Couldn't find / after hostname\n");
232		exit(1);
233	}
234	*s = '\0';
235	path = s + 1;
236
237	if ((s = strchr(path, ';')) != NULL) {
238		*s = '\0';
239		opts = s + 1;
240
241		if (strncmp(opts, "mode=", 5) == 0) {
242			tmode = opts;
243			tmode += 5;
244
245			for (i = 0; modes[i].m_name != NULL; i++) {
246				if (strcmp(modes[i].m_name, tmode) == 0)
247					break;
248			}
249			if (modes[i].m_name == NULL) {
250				fprintf(stderr, "Invalid mode: '%s'\n", mode);
251				exit(1);
252			}
253			settftpmode(modes[i].m_mode);
254		}
255	} else {
256		settftpmode("octet");
257	}
258
259	setpeer0(host, NULL);
260
261	sprintf(line, "get %s", path);
262	makeargv(line);
263	get(margc, margv);
264}
265
266static char    hostname[MAXHOSTNAMELEN];
267
268static void
269setpeer0(char *host, const char *lport)
270{
271	struct addrinfo hints, *res0, *res;
272	int error;
273	const char *cause = "unknown";
274
275	if (connected) {
276		close(peer);
277		peer = -1;
278	}
279	connected = 0;
280
281	memset(&hints, 0, sizeof(hints));
282	hints.ai_family = PF_UNSPEC;
283	hints.ai_socktype = SOCK_DGRAM;
284	hints.ai_protocol = IPPROTO_UDP;
285	hints.ai_flags = AI_CANONNAME;
286	if (!lport)
287		lport = "tftp";
288	error = getaddrinfo(host, lport, &hints, &res0);
289	if (error) {
290		warnx("%s", gai_strerror(error));
291		return;
292	}
293
294	for (res = res0; res; res = res->ai_next) {
295		if (res->ai_addrlen > sizeof(peeraddr))
296			continue;
297		peer = socket(res->ai_family, res->ai_socktype,
298			res->ai_protocol);
299		if (peer < 0) {
300			cause = "socket";
301			continue;
302		}
303
304		memset(&peer_sock, 0, sizeof(peer_sock));
305		peer_sock.ss_family = res->ai_family;
306		peer_sock.ss_len = res->ai_addrlen;
307		if (bind(peer, (struct sockaddr *)&peer_sock, peer_sock.ss_len) < 0) {
308			cause = "bind";
309			close(peer);
310			peer = -1;
311			continue;
312		}
313
314		break;
315	}
316
317	if (peer < 0)
318		warn("%s", cause);
319	else {
320		/* res->ai_addr <= sizeof(peeraddr) is guaranteed */
321		memcpy(&peer_sock, res->ai_addr, res->ai_addrlen);
322		if (res->ai_canonname) {
323			(void) strncpy(hostname, res->ai_canonname,
324				sizeof(hostname));
325		} else
326			(void) strncpy(hostname, host, sizeof(hostname));
327		hostname[sizeof(hostname)-1] = 0;
328		connected = 1;
329	}
330
331	freeaddrinfo(res0);
332}
333
334static void
335setpeer(int argc, char *argv[])
336{
337	char	line[MAXLINE];
338
339	if (argc < 2) {
340		strcpy(line, "Connect ");
341		printf("(to) ");
342		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
343		makeargv(line);
344		argc = margc;
345		argv = margv;
346	}
347	if ((argc < 2) || (argc > 3)) {
348		printf("usage: %s [host [port]]\n", argv[0]);
349		return;
350	}
351	if (argc == 3) {
352		port = argv[2];
353		setpeer0(argv[1], argv[2]);
354	} else
355		setpeer0(argv[1], NULL);
356}
357
358static void
359modecmd(int argc, char *argv[])
360{
361	struct modes *p;
362	const char *sep;
363
364	if (argc < 2) {
365		printf("Using %s mode to transfer files.\n", mode);
366		return;
367	}
368	if (argc == 2) {
369		for (p = modes; p->m_name; p++)
370			if (strcmp(argv[1], p->m_name) == 0)
371				break;
372		if (p->m_name) {
373			settftpmode(p->m_mode);
374			return;
375		}
376		printf("%s: unknown mode\n", argv[1]);
377		/* drop through and print usage message */
378	}
379
380	printf("usage: %s [", argv[0]);
381	sep = " ";
382	for (p = modes; p->m_name != NULL; p++) {
383		printf("%s%s", sep, p->m_name);
384		if (*sep == ' ')
385			sep = " | ";
386	}
387	printf(" ]\n");
388	return;
389}
390
391static void
392setbinary(int argc __unused, char *argv[] __unused)
393{
394
395	settftpmode("octet");
396}
397
398static void
399setascii(int argc __unused, char *argv[] __unused)
400{
401
402	settftpmode("netascii");
403}
404
405static void
406settftpmode(const char *newmode)
407{
408
409	strcpy(mode, newmode);
410	if (verbose)
411		printf("mode set to %s\n", mode);
412}
413
414
415/*
416 * Send file(s).
417 */
418static void
419put(int argc, char *argv[])
420{
421	int	fd;
422	int	n;
423	char	*cp, *targ;
424	char	line[MAXLINE];
425	struct stat sb;
426
427	if (argc < 2) {
428		strcpy(line, "send ");
429		printf("(file) ");
430		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
431		makeargv(line);
432		argc = margc;
433		argv = margv;
434	}
435	if (argc < 2) {
436		putusage(argv[0]);
437		return;
438	}
439	targ = argv[argc - 1];
440	if (strrchr(argv[argc - 1], ':')) {
441		char *lcp;
442
443		for (n = 1; n < argc - 1; n++)
444			if (strchr(argv[n], ':')) {
445				putusage(argv[0]);
446				return;
447			}
448		lcp = argv[argc - 1];
449		targ = strrchr(lcp, ':');
450		*targ++ = 0;
451		if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') {
452			lcp[strlen(lcp) - 1] = '\0';
453			lcp++;
454		}
455		setpeer0(lcp, NULL);
456	}
457	if (!connected) {
458		printf("No target machine specified.\n");
459		return;
460	}
461	if (argc < 4) {
462		cp = argc == 2 ? tail(targ) : argv[1];
463		fd = open(cp, O_RDONLY);
464		if (fd < 0) {
465			warn("%s", cp);
466			return;
467		}
468
469		stat(cp, &sb);
470		asprintf(&options[OPT_TSIZE].o_request, "%ju", sb.st_size);
471
472		if (verbose)
473			printf("putting %s to %s:%s [%s]\n",
474			    cp, hostname, targ, mode);
475		xmitfile(peer, port, fd, targ, mode);
476		return;
477	}
478				/* this assumes the target is a directory */
479				/* on a remote unix system.  hmmmm.  */
480	cp = strchr(targ, '\0');
481	*cp++ = '/';
482	for (n = 1; n < argc - 1; n++) {
483		strcpy(cp, tail(argv[n]));
484		fd = open(argv[n], O_RDONLY);
485		if (fd < 0) {
486			warn("%s", argv[n]);
487			continue;
488		}
489
490		stat(cp, &sb);
491		asprintf(&options[OPT_TSIZE].o_request, "%ju", sb.st_size);
492
493		if (verbose)
494			printf("putting %s to %s:%s [%s]\n",
495			    argv[n], hostname, targ, mode);
496		xmitfile(peer, port, fd, targ, mode);
497	}
498}
499
500static void
501putusage(char *s)
502{
503
504	printf("usage: %s file [remotename]\n", s);
505	printf("       %s file host:remotename\n", s);
506	printf("       %s file1 file2 ... fileN [[host:]remote-directory]\n", s);
507}
508
509/*
510 * Receive file(s).
511 */
512static void
513get(int argc, char *argv[])
514{
515	int fd;
516	int n;
517	char *cp;
518	char *src;
519	char	line[MAXLINE];
520
521	if (argc < 2) {
522		strcpy(line, "get ");
523		printf("(files) ");
524		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
525		makeargv(line);
526		argc = margc;
527		argv = margv;
528	}
529	if (argc < 2) {
530		getusage(argv[0]);
531		return;
532	}
533	if (!connected) {
534		for (n = 1; n < argc ; n++)
535			if (strrchr(argv[n], ':') == 0) {
536				printf("No remote host specified and "
537				    "no host given for file '%s'\n", argv[n]);
538				getusage(argv[0]);
539				return;
540			}
541	}
542	for (n = 1; n < argc ; n++) {
543		src = strrchr(argv[n], ':');
544		if (src == NULL)
545			src = argv[n];
546		else {
547			char *lcp;
548
549			*src++ = 0;
550			lcp = argv[n];
551			if (lcp[0] == '[' && lcp[strlen(lcp) - 1] == ']') {
552				lcp[strlen(lcp) - 1] = '\0';
553				lcp++;
554			}
555			setpeer0(lcp, NULL);
556			if (!connected)
557				continue;
558		}
559		if (argc < 4) {
560			cp = argc == 3 ? argv[2] : tail(src);
561			fd = creat(cp, 0644);
562			if (fd < 0) {
563				warn("%s", cp);
564				return;
565			}
566			if (verbose)
567				printf("getting from %s:%s to %s [%s]\n",
568				    hostname, src, cp, mode);
569			recvfile(peer, port, fd, src, mode);
570			break;
571		}
572		cp = tail(src);         /* new .. jdg */
573		fd = creat(cp, 0644);
574		if (fd < 0) {
575			warn("%s", cp);
576			continue;
577		}
578		if (verbose)
579			printf("getting from %s:%s to %s [%s]\n",
580			    hostname, src, cp, mode);
581		recvfile(peer, port, fd, src, mode);
582	}
583}
584
585static void
586getusage(char *s)
587{
588
589	printf("usage: %s file [localname]\n", s);
590	printf("       %s [host:]file [localname]\n", s);
591	printf("       %s [host1:]file1 [host2:]file2 ... [hostN:]fileN\n", s);
592}
593
594static void
595settimeoutpacket(int argc, char *argv[])
596{
597	int t;
598	char	line[MAXLINE];
599
600	if (argc < 2) {
601		strcpy(line, "Packet timeout ");
602		printf("(value) ");
603		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
604		makeargv(line);
605		argc = margc;
606		argv = margv;
607	}
608	if (argc != 2) {
609		printf("usage: %s value\n", argv[0]);
610		return;
611	}
612	t = atoi(argv[1]);
613	if (t < 0) {
614		printf("%s: bad value\n", argv[1]);
615		return;
616	}
617
618	settimeouts(t, timeoutnetwork, maxtimeouts);
619}
620
621static void
622settimeoutnetwork(int argc, char *argv[])
623{
624	int t;
625	char	line[MAXLINE];
626
627	if (argc < 2) {
628		strcpy(line, "Network timeout ");
629		printf("(value) ");
630		fgets(&line[strlen(line)], sizeof line - strlen(line), stdin);
631		makeargv(line);
632		argc = margc;
633		argv = margv;
634	}
635	if (argc != 2) {
636		printf("usage: %s value\n", argv[0]);
637		return;
638	}
639	t = atoi(argv[1]);
640	if (t < 0) {
641		printf("%s: bad value\n", argv[1]);
642		return;
643	}
644
645	settimeouts(timeoutpacket, t, maxtimeouts);
646}
647
648static void
649showstatus(int argc __unused, char *argv[] __unused)
650{
651
652	printf("Remote host: %s\n",
653	    connected ? hostname : "none specified yet");
654	printf("RFC2347 Options support: %s\n",
655	    options_rfc_enabled ? "enabled" : "disabled");
656	printf("Non-RFC defined options support: %s\n",
657	    options_extra_enabled ? "enabled" : "disabled");
658	printf("Mode: %s\n", mode);
659	printf("Verbose: %s\n", verbose ? "on" : "off");
660	printf("Debug: %s\n", debug_show(debug));
661	printf("Artificial packetloss: %d in 100 packets\n",
662	    packetdroppercentage);
663	printf("Segment size: %d bytes\n", segsize);
664	printf("Network timeout: %d seconds\n", timeoutpacket);
665	printf("Maximum network timeout: %d seconds\n", timeoutnetwork);
666	printf("Maximum timeouts: %d \n", maxtimeouts);
667}
668
669static void
670intr(int dummy __unused)
671{
672
673	signal(SIGALRM, SIG_IGN);
674	alarm(0);
675	longjmp(toplevel, -1);
676}
677
678static char *
679tail(char *filename)
680{
681	char *s;
682
683	while (*filename) {
684		s = strrchr(filename, '/');
685		if (s == NULL)
686			break;
687		if (s[1])
688			return (s + 1);
689		*s = '\0';
690	}
691	return (filename);
692}
693
694static const char *
695command_prompt(void)
696{
697
698	return ("tftp> ");
699}
700
701/*
702 * Command parser.
703 */
704static void
705command(void)
706{
707	HistEvent he;
708	struct cmd *c;
709	static EditLine *el;
710	static History *hist;
711	const char *bp;
712	char *cp;
713	int len, num, vrbose;
714	char	line[MAXLINE];
715
716	vrbose = isatty(0);
717	if (vrbose) {
718		el = el_init("tftp", stdin, stdout, stderr);
719		hist = history_init();
720		history(hist, &he, H_SETSIZE, 100);
721		el_set(el, EL_HIST, history, hist);
722		el_set(el, EL_EDITOR, "emacs");
723		el_set(el, EL_PROMPT, command_prompt);
724		el_set(el, EL_SIGNAL, 1);
725		el_source(el, NULL);
726	}
727	for (;;) {
728		if (vrbose) {
729                        if ((bp = el_gets(el, &num)) == NULL || num == 0)
730                                exit(0);
731                        len = (num > MAXLINE) ? MAXLINE : num;
732                        memcpy(line, bp, len);
733                        line[len] = '\0';
734                        history(hist, &he, H_ENTER, bp);
735		} else {
736			line[0] = 0;
737			if (fgets(line, sizeof line , stdin) == NULL) {
738				if (feof(stdin)) {
739					exit(txrx_error);
740				} else {
741					continue;
742				}
743			}
744		}
745		if ((cp = strchr(line, '\n')))
746			*cp = '\0';
747		if (line[0] == 0)
748			continue;
749		makeargv(line);
750		if (margc == 0)
751			continue;
752		c = getcmd(margv[0]);
753		if (c == (struct cmd *)-1) {
754			printf("?Ambiguous command\n");
755			continue;
756		}
757		if (c == 0) {
758			printf("?Invalid command\n");
759			continue;
760		}
761		(*c->handler)(margc, margv);
762	}
763}
764
765static struct cmd *
766getcmd(char *name)
767{
768	const char *p, *q;
769	struct cmd *c, *found;
770	int nmatches, longest;
771
772	longest = 0;
773	nmatches = 0;
774	found = 0;
775	for (c = cmdtab; (p = c->name) != NULL; c++) {
776		for (q = name; *q == *p++; q++)
777			if (*q == 0)		/* exact match? */
778				return (c);
779		if (!*q) {			/* the name was a prefix */
780			if (q - name > longest) {
781				longest = q - name;
782				nmatches = 1;
783				found = c;
784			} else if (q - name == longest)
785				nmatches++;
786		}
787	}
788	if (nmatches > 1)
789		return ((struct cmd *)-1);
790	return (found);
791}
792
793/*
794 * Slice a string up into argc/argv.
795 */
796static void
797makeargv(char *line)
798{
799	char *cp;
800	char **argp = margv;
801
802	margc = 0;
803	if ((cp = strchr(line, '\n')) != NULL)
804		*cp = '\0';
805	for (cp = line; margc < MAX_MARGV - 1 && *cp != '\0';) {
806		while (isspace(*cp))
807			cp++;
808		if (*cp == '\0')
809			break;
810		*argp++ = cp;
811		margc += 1;
812		while (*cp != '\0' && !isspace(*cp))
813			cp++;
814		if (*cp == '\0')
815			break;
816		*cp++ = '\0';
817	}
818	*argp++ = 0;
819}
820
821static void
822quit(int argc __unused, char *argv[] __unused)
823{
824
825	exit(txrx_error);
826}
827
828/*
829 * Help command.
830 */
831static void
832help(int argc, char *argv[])
833{
834	struct cmd *c;
835
836	if (argc == 1) {
837		printf("Commands may be abbreviated.  Commands are:\n\n");
838		for (c = cmdtab; c->name; c++)
839			printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help);
840
841		printf("\n[-] : You shouldn't use these ones anymore.\n");
842		printf("[*] : RFC2347 options support required.\n");
843		printf("[**] : Non-standard RFC2347 option.\n");
844		return;
845	}
846	while (--argc > 0) {
847		char *arg;
848		arg = *++argv;
849		c = getcmd(arg);
850		if (c == (struct cmd *)-1)
851			printf("?Ambiguous help command: %s\n", arg);
852		else if (c == (struct cmd *)0)
853			printf("?Invalid help command: %s\n", arg);
854		else
855			printf("%s\n", c->help);
856	}
857}
858
859static void
860setverbose(int argc __unused, char *argv[] __unused)
861{
862
863	verbose = !verbose;
864	printf("Verbose mode %s.\n", verbose ? "on" : "off");
865}
866
867static void
868setoptions(int argc, char *argv[])
869{
870
871	if (argc == 2) {
872		if (strcasecmp(argv[1], "enable") == 0 ||
873		    strcasecmp(argv[1], "on") == 0) {
874			options_extra_enabled = 1;
875			options_rfc_enabled = 1;
876		}
877		if (strcasecmp(argv[1], "disable") == 0 ||
878		    strcasecmp(argv[1], "off") == 0) {
879			options_extra_enabled = 0;
880			options_rfc_enabled = 0;
881		}
882		if (strcasecmp(argv[1], "extra") == 0)
883			options_extra_enabled = !options_extra_enabled;
884	}
885	printf("Support for RFC2347 style options are now %s.\n",
886	    options_rfc_enabled ? "enabled" : "disabled");
887	printf("Support for non-RFC defined options are now %s.\n",
888	    options_extra_enabled ? "enabled" : "disabled");
889
890	printf("\nThe following options are available:\n"
891	    "\toptions on	: enable support for RFC2347 style options\n"
892	    "\toptions off	: disable support for RFC2347 style options\n"
893	    "\toptions extra	: toggle support for non-RFC defined options\n"
894	);
895}
896
897static void
898setrollover(int argc, char *argv[])
899{
900
901	if (argc == 2) {
902		if (strcasecmp(argv[1], "never") == 0 ||
903		    strcasecmp(argv[1], "none") == 0) {
904			free(options[OPT_ROLLOVER].o_request);
905			options[OPT_ROLLOVER].o_request = NULL;
906		}
907		if (strcasecmp(argv[1], "1") == 0) {
908			free(options[OPT_ROLLOVER].o_request);
909			options[OPT_ROLLOVER].o_request = strdup("1");
910		}
911		if (strcasecmp(argv[1], "0") == 0) {
912			free(options[OPT_ROLLOVER].o_request);
913			options[OPT_ROLLOVER].o_request = strdup("0");
914		}
915	}
916	printf("Support for the rollover options is %s.\n",
917	    options[OPT_ROLLOVER].o_request != NULL ? "enabled" : "disabled");
918	if (options[OPT_ROLLOVER].o_request != NULL)
919		printf("Block rollover will be to block %s.\n",
920		    options[OPT_ROLLOVER].o_request);
921
922
923	printf("\nThe following rollover options are available:\n"
924	    "\trollover 0	: rollover to block zero (default)\n"
925	    "\trollover 1	: rollover to block one\n"
926	    "\trollover never	: do not support the rollover option\n"
927	    "\trollover none	: do not support the rollover option\n"
928	);
929}
930
931static void
932setdebug(int argc, char *argv[])
933{
934	int i;
935
936	if (argc != 1) {
937		i = 1;
938		while (i < argc)
939			debug ^= debug_find(argv[i++]);
940	}
941	printf("The following debugging is enabled: %s\n", debug_show(debug));
942
943	printf("\nThe following debugs are available:\n");
944	i = 0;
945	while (debugs[i].name != NULL) {
946		printf("\t%s\t%s\n", debugs[i].name, debugs[i].desc);
947		i++;
948	}
949}
950
951static void
952setblocksize(int argc, char *argv[])
953{
954
955	if (!options_rfc_enabled)
956		printf("RFC2347 style options are not enabled "
957		    "(but proceeding anyway)\n");
958
959	if (argc != 1) {
960		int size = atoi(argv[1]);
961		size_t max;
962		u_long maxdgram;
963
964		max = sizeof(maxdgram);
965		if (sysctlbyname("net.inet.udp.maxdgram",
966			&maxdgram, &max, NULL, 0) < 0) {
967			perror("sysctl: net.inet.udp.maxdgram");
968			return;
969		}
970
971		if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) {
972			printf("Blocksize should be between %d and %d bytes.\n",
973				BLKSIZE_MIN, BLKSIZE_MAX);
974			return;
975		} else if (size > (int)maxdgram - 4) {
976			printf("Blocksize can't be bigger than %ld bytes due "
977			    "to the net.inet.udp.maxdgram sysctl limitation.\n",
978			    maxdgram - 4);
979			asprintf(&options[OPT_BLKSIZE].o_request,
980			    "%ld", maxdgram - 4);
981		} else {
982			asprintf(&options[OPT_BLKSIZE].o_request, "%d", size);
983		}
984	}
985	printf("Blocksize is now %s bytes.\n", options[OPT_BLKSIZE].o_request);
986}
987
988static void
989setblocksize2(int argc, char *argv[])
990{
991
992	if (!options_rfc_enabled || !options_extra_enabled)
993		printf(
994		    "RFC2347 style or non-RFC defined options are not enabled "
995		    "(but proceeding anyway)\n");
996
997	if (argc != 1) {
998		int size = atoi(argv[1]);
999		int i;
1000		size_t max;
1001		u_long maxdgram;
1002
1003		int sizes[] = {
1004			8, 16, 32, 64, 128, 256, 512, 1024,
1005			2048, 4096, 8192, 16384, 32768, 0
1006		};
1007
1008		max = sizeof(maxdgram);
1009		if (sysctlbyname("net.inet.udp.maxdgram",
1010			&maxdgram, &max, NULL, 0) < 0) {
1011			perror("sysctl: net.inet.udp.maxdgram");
1012			return;
1013		}
1014
1015		for (i = 0; sizes[i] != 0; i++) {
1016			if (sizes[i] == size) break;
1017		}
1018		if (sizes[i] == 0) {
1019			printf("Blocksize2 should be a power of two between "
1020			    "8 and 32768.\n");
1021			return;
1022		}
1023
1024		if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) {
1025			printf("Blocksize2 should be between "
1026			    "%d and %d bytes.\n", BLKSIZE_MIN, BLKSIZE_MAX);
1027			return;
1028		} else if (size > (int)maxdgram - 4) {
1029			printf("Blocksize2 can't be bigger than %ld bytes due "
1030			    "to the net.inet.udp.maxdgram sysctl limitation.\n",
1031			    maxdgram - 4);
1032			for (i = 0; sizes[i+1] != 0; i++) {
1033				if ((int)maxdgram < sizes[i+1]) break;
1034			}
1035			asprintf(&options[OPT_BLKSIZE2].o_request,
1036			    "%d", sizes[i]);
1037		} else {
1038			asprintf(&options[OPT_BLKSIZE2].o_request, "%d", size);
1039		}
1040	}
1041	printf("Blocksize2 is now %s bytes.\n",
1042	    options[OPT_BLKSIZE2].o_request);
1043}
1044
1045static void
1046setpacketdrop(int argc, char *argv[])
1047{
1048
1049	if (argc != 1)
1050		packetdroppercentage = atoi(argv[1]);
1051
1052	printf("Randomly %d in 100 packets will be dropped\n",
1053	    packetdroppercentage);
1054}
1055