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