forked from mirrors/tftp-hpa-google
Initial revision
This commit is contained in:
commit
09f975fa4c
18 changed files with 4696 additions and 0 deletions
21
tftp/Makefile
Normal file
21
tftp/Makefile
Normal file
|
@ -0,0 +1,21 @@
|
|||
all: tftp
|
||||
|
||||
include ../MCONFIG
|
||||
include ../MRULES
|
||||
|
||||
OBJS = tftp.o main.o tftpsubs.o
|
||||
|
||||
tftp: $(OBJS)
|
||||
$(CC) $(LDFLAGS) $^ $(LIBS) -o $@
|
||||
|
||||
$(OBJS): tftpsubs.h
|
||||
|
||||
install: tftp
|
||||
$(INSTALL_PROGRAM) -s tftp $(INSTALLROOT)$(BINDIR)
|
||||
$(INSTALL_DATA) tftp.1 $(INSTALLROOT)$(MANDIR)/man1
|
||||
|
||||
clean:
|
||||
rm -f *.o tftp
|
||||
|
||||
spotless: clean
|
||||
rm -f *~
|
42
tftp/extern.h
Normal file
42
tftp/extern.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
/* $Id$ */
|
||||
|
||||
/* $OpenBSD: extern.h,v 1.2 1996/06/26 05:40:33 deraadt Exp $ */
|
||||
/* $NetBSD: extern.h,v 1.2 1994/12/08 09:51:24 jtc Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)extern.h 8.1 (Berkeley) 6/6/93
|
||||
*/
|
||||
|
||||
void recvfile (int, char *, char *);
|
||||
void sendfile (int, char *, char *);
|
704
tftp/main.c
Normal file
704
tftp/main.c
Normal file
|
@ -0,0 +1,704 @@
|
|||
/* $OpenBSD: main.c,v 1.4 1997/01/17 07:13:30 millert Exp $ */
|
||||
/* $NetBSD: main.c,v 1.6 1995/05/21 16:54:10 mycroft Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1983, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
static const char *copyright =
|
||||
"@(#) Copyright (c) 1983, 1993\n\
|
||||
The Regents of the University of California. All rights reserved.\n";
|
||||
/* static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; */
|
||||
/* static char rcsid[] = "$OpenBSD: main.c,v 1.4 1997/01/17 07:13:30 millert Exp $"; */
|
||||
static const char *rcsid = "tftp-hpa $Id$";
|
||||
#endif /* not lint */
|
||||
|
||||
/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
|
||||
|
||||
/*
|
||||
* TFTP User Program -- Command Interface.
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/file.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "extern.h"
|
||||
|
||||
#define TIMEOUT 5 /* secs between rexmt's */
|
||||
#define LBUFLEN 200 /* size of input buffer */
|
||||
|
||||
struct sockaddr_in peeraddr;
|
||||
int f;
|
||||
short port;
|
||||
int trace;
|
||||
int verbose;
|
||||
int connected;
|
||||
char mode[32];
|
||||
char line[LBUFLEN];
|
||||
int margc;
|
||||
char *margv[20];
|
||||
char *prompt = "tftp";
|
||||
jmp_buf toplevel;
|
||||
void intr(int);
|
||||
struct servent *sp;
|
||||
|
||||
void get (int, char **);
|
||||
void help (int, char **);
|
||||
void modecmd (int, char **);
|
||||
void put (int, char **);
|
||||
void quit (int, char **);
|
||||
void setascii (int, char **);
|
||||
void setbinary (int, char **);
|
||||
void setpeer (int, char **);
|
||||
void setrexmt (int, char **);
|
||||
void settimeout (int, char **);
|
||||
void settrace (int, char **);
|
||||
void setverbose (int, char **);
|
||||
void status (int, char **);
|
||||
|
||||
static void command (void);
|
||||
|
||||
static void getusage (char *);
|
||||
static void makeargv (void);
|
||||
static void putusage (char *);
|
||||
static void settftpmode (char *);
|
||||
|
||||
#define HELPINDENT (sizeof("connect"))
|
||||
|
||||
struct cmd {
|
||||
char *name;
|
||||
char *help;
|
||||
void (*handler) (int, char **);
|
||||
};
|
||||
|
||||
char vhelp[] = "toggle verbose mode";
|
||||
char thelp[] = "toggle packet tracing";
|
||||
char chelp[] = "connect to remote tftp";
|
||||
char qhelp[] = "exit tftp";
|
||||
char hhelp[] = "print help information";
|
||||
char shelp[] = "send file";
|
||||
char rhelp[] = "receive file";
|
||||
char mhelp[] = "set file transfer mode";
|
||||
char sthelp[] = "show current status";
|
||||
char xhelp[] = "set per-packet retransmission timeout";
|
||||
char ihelp[] = "set total retransmission timeout";
|
||||
char ashelp[] = "set mode to netascii";
|
||||
char bnhelp[] = "set mode to octet";
|
||||
|
||||
struct cmd cmdtab[] = {
|
||||
{ "connect", chelp, setpeer },
|
||||
{ "mode", mhelp, modecmd },
|
||||
{ "put", shelp, put },
|
||||
{ "get", rhelp, get },
|
||||
{ "quit", qhelp, quit },
|
||||
{ "verbose", vhelp, setverbose },
|
||||
{ "trace", thelp, settrace },
|
||||
{ "status", sthelp, status },
|
||||
{ "binary", bnhelp, setbinary },
|
||||
{ "ascii", ashelp, setascii },
|
||||
{ "rexmt", xhelp, setrexmt },
|
||||
{ "timeout", ihelp, settimeout },
|
||||
{ "?", hhelp, help },
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
struct cmd *getcmd(char *);
|
||||
char *tail(char *);
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct sockaddr_in s_in;
|
||||
|
||||
sp = getservbyname("tftp", "udp");
|
||||
if (sp == 0) {
|
||||
fprintf(stderr, "tftp: udp/tftp: unknown service\n");
|
||||
exit(1);
|
||||
}
|
||||
f = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (f < 0) {
|
||||
perror("tftp: socket");
|
||||
exit(3);
|
||||
}
|
||||
bzero((char *)&s_in, sizeof (s_in));
|
||||
s_in.sin_family = AF_INET;
|
||||
if (bind(f, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
|
||||
perror("tftp: bind");
|
||||
exit(1);
|
||||
}
|
||||
strcpy(mode, "netascii");
|
||||
signal(SIGINT, intr);
|
||||
if (argc > 1) {
|
||||
if (setjmp(toplevel) != 0)
|
||||
exit(0);
|
||||
setpeer(argc, argv);
|
||||
}
|
||||
if (setjmp(toplevel) != 0)
|
||||
(void)putchar('\n');
|
||||
|
||||
command();
|
||||
|
||||
return 0; /* Never reached */
|
||||
}
|
||||
|
||||
char hostname[MAXHOSTNAMELEN];
|
||||
|
||||
void
|
||||
setpeer(int argc, char *argv[])
|
||||
{
|
||||
struct hostent *host;
|
||||
|
||||
if (argc < 2) {
|
||||
strcpy(line, "Connect ");
|
||||
printf("(to) ");
|
||||
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
|
||||
makeargv();
|
||||
argc = margc;
|
||||
argv = margv;
|
||||
}
|
||||
if ((argc < 2) || (argc > 3)) {
|
||||
printf("usage: %s host-name [port]\n", argv[0]);
|
||||
return;
|
||||
}
|
||||
if (inet_aton(argv[1], &peeraddr.sin_addr) != 0) {
|
||||
peeraddr.sin_family = AF_INET;
|
||||
(void) strncpy(hostname, argv[1], sizeof hostname);
|
||||
hostname[sizeof(hostname)-1] = '\0';
|
||||
} else {
|
||||
host = gethostbyname(argv[1]);
|
||||
if (host == 0) {
|
||||
connected = 0;
|
||||
printf("%s: unknown host\n", argv[1]);
|
||||
return;
|
||||
}
|
||||
peeraddr.sin_family = host->h_addrtype;
|
||||
bcopy(host->h_addr, &peeraddr.sin_addr, host->h_length);
|
||||
(void) strcpy(hostname, host->h_name);
|
||||
}
|
||||
port = sp->s_port;
|
||||
if (argc == 3) {
|
||||
port = atoi(argv[2]);
|
||||
if (port < 0) {
|
||||
printf("%s: bad port number\n", argv[2]);
|
||||
connected = 0;
|
||||
return;
|
||||
}
|
||||
port = htons(port);
|
||||
}
|
||||
connected = 1;
|
||||
}
|
||||
|
||||
struct modes {
|
||||
char *m_name;
|
||||
char *m_mode;
|
||||
} modes[] = {
|
||||
{ "ascii", "netascii" },
|
||||
{ "netascii", "netascii" },
|
||||
{ "binary", "octet" },
|
||||
{ "image", "octet" },
|
||||
{ "octet", "octet" },
|
||||
/* { "mail", "mail" }, */
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
void
|
||||
modecmd(int argc, char *argv[])
|
||||
{
|
||||
struct modes *p;
|
||||
char *sep;
|
||||
|
||||
if (argc < 2) {
|
||||
printf("Using %s mode to transfer files.\n", mode);
|
||||
return;
|
||||
}
|
||||
if (argc == 2) {
|
||||
for (p = modes; p->m_name; p++)
|
||||
if (strcmp(argv[1], p->m_name) == 0)
|
||||
break;
|
||||
if (p->m_name) {
|
||||
settftpmode(p->m_mode);
|
||||
return;
|
||||
}
|
||||
printf("%s: unknown mode\n", argv[1]);
|
||||
/* drop through and print usage message */
|
||||
}
|
||||
|
||||
printf("usage: %s [", argv[0]);
|
||||
sep = " ";
|
||||
for (p = modes; p->m_name; p++) {
|
||||
printf("%s%s", sep, p->m_name);
|
||||
if (*sep == ' ')
|
||||
sep = " | ";
|
||||
}
|
||||
printf(" ]\n");
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
setbinary(int argc, char *argv[])
|
||||
{
|
||||
|
||||
settftpmode("octet");
|
||||
}
|
||||
|
||||
void
|
||||
setascii(int argc, char *argv[])
|
||||
{
|
||||
|
||||
settftpmode("netascii");
|
||||
}
|
||||
|
||||
static void
|
||||
settftpmode(char *newmode)
|
||||
{
|
||||
strcpy(mode, newmode);
|
||||
if (verbose)
|
||||
printf("mode set to %s\n", mode);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Send file(s).
|
||||
*/
|
||||
void
|
||||
put(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
int n;
|
||||
char *cp, *targ;
|
||||
|
||||
if (argc < 2) {
|
||||
strcpy(line, "send ");
|
||||
printf("(file) ");
|
||||
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
|
||||
makeargv();
|
||||
argc = margc;
|
||||
argv = margv;
|
||||
}
|
||||
if (argc < 2) {
|
||||
putusage(argv[0]);
|
||||
return;
|
||||
}
|
||||
targ = argv[argc - 1];
|
||||
if (strchr(argv[argc - 1], ':')) {
|
||||
char *cp;
|
||||
struct hostent *hp;
|
||||
|
||||
for (n = 1; n < argc - 1; n++)
|
||||
if (strchr(argv[n], ':')) {
|
||||
putusage(argv[0]);
|
||||
return;
|
||||
}
|
||||
cp = argv[argc - 1];
|
||||
targ = strchr(cp, ':');
|
||||
*targ++ = 0;
|
||||
hp = gethostbyname(cp);
|
||||
if (hp == NULL) {
|
||||
fprintf(stderr, "tftp: %s: ", cp);
|
||||
herror((char *)NULL);
|
||||
return;
|
||||
}
|
||||
bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, hp->h_length);
|
||||
peeraddr.sin_family = hp->h_addrtype;
|
||||
connected = 1;
|
||||
strcpy(hostname, hp->h_name);
|
||||
}
|
||||
if (!connected) {
|
||||
printf("No target machine specified.\n");
|
||||
return;
|
||||
}
|
||||
if (argc < 4) {
|
||||
cp = argc == 2 ? tail(targ) : argv[1];
|
||||
fd = open(cp, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "tftp: "); perror(cp);
|
||||
return;
|
||||
}
|
||||
if (verbose)
|
||||
printf("putting %s to %s:%s [%s]\n",
|
||||
cp, hostname, targ, mode);
|
||||
peeraddr.sin_port = port;
|
||||
sendfile(fd, targ, mode);
|
||||
return;
|
||||
}
|
||||
/* this assumes the target is a directory */
|
||||
/* on a remote unix system. hmmmm. */
|
||||
cp = strchr(targ, '\0');
|
||||
*cp++ = '/';
|
||||
for (n = 1; n < argc - 1; n++) {
|
||||
strcpy(cp, tail(argv[n]));
|
||||
fd = open(argv[n], O_RDONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "tftp: "); perror(argv[n]);
|
||||
continue;
|
||||
}
|
||||
if (verbose)
|
||||
printf("putting %s to %s:%s [%s]\n",
|
||||
argv[n], hostname, targ, mode);
|
||||
peeraddr.sin_port = port;
|
||||
sendfile(fd, targ, mode);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
putusage(char *s)
|
||||
{
|
||||
printf("usage: %s file ... host:target, or\n", s);
|
||||
printf(" %s file ... target (when already connected)\n", s);
|
||||
}
|
||||
|
||||
/*
|
||||
* Receive file(s).
|
||||
*/
|
||||
void
|
||||
get(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
int n;
|
||||
char *cp;
|
||||
char *src;
|
||||
|
||||
if (argc < 2) {
|
||||
strcpy(line, "get ");
|
||||
printf("(files) ");
|
||||
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
|
||||
makeargv();
|
||||
argc = margc;
|
||||
argv = margv;
|
||||
}
|
||||
if (argc < 2) {
|
||||
getusage(argv[0]);
|
||||
return;
|
||||
}
|
||||
if (!connected) {
|
||||
for (n = 1; n < argc ; n++)
|
||||
if (strchr(argv[n], ':') == 0) {
|
||||
getusage(argv[0]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (n = 1; n < argc ; n++) {
|
||||
src = strchr(argv[n], ':');
|
||||
if (src == NULL)
|
||||
src = argv[n];
|
||||
else {
|
||||
struct hostent *hp;
|
||||
|
||||
*src++ = 0;
|
||||
hp = gethostbyname(argv[n]);
|
||||
if (hp == NULL) {
|
||||
fprintf(stderr, "tftp: %s: ", argv[n]);
|
||||
herror((char *)NULL);
|
||||
continue;
|
||||
}
|
||||
bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr,
|
||||
hp->h_length);
|
||||
peeraddr.sin_family = hp->h_addrtype;
|
||||
connected = 1;
|
||||
strcpy(hostname, hp->h_name);
|
||||
}
|
||||
if (argc < 4) {
|
||||
cp = argc == 3 ? argv[2] : tail(src);
|
||||
fd = creat(cp, 0644);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "tftp: "); perror(cp);
|
||||
return;
|
||||
}
|
||||
if (verbose)
|
||||
printf("getting from %s:%s to %s [%s]\n",
|
||||
hostname, src, cp, mode);
|
||||
peeraddr.sin_port = port;
|
||||
recvfile(fd, src, mode);
|
||||
break;
|
||||
}
|
||||
cp = tail(src); /* new .. jdg */
|
||||
fd = creat(cp, 0644);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "tftp: "); perror(cp);
|
||||
continue;
|
||||
}
|
||||
if (verbose)
|
||||
printf("getting from %s:%s to %s [%s]\n",
|
||||
hostname, src, cp, mode);
|
||||
peeraddr.sin_port = port;
|
||||
recvfile(fd, src, mode);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
getusage(char *s)
|
||||
{
|
||||
printf("usage: %s host:file host:file ... file, or\n", s);
|
||||
printf(" %s file file ... file if connected\n", s);
|
||||
}
|
||||
|
||||
int rexmtval = TIMEOUT;
|
||||
|
||||
void
|
||||
setrexmt(int argc, char *argv[])
|
||||
{
|
||||
int t;
|
||||
|
||||
if (argc < 2) {
|
||||
strcpy(line, "Rexmt-timeout ");
|
||||
printf("(value) ");
|
||||
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
|
||||
makeargv();
|
||||
argc = margc;
|
||||
argv = margv;
|
||||
}
|
||||
if (argc != 2) {
|
||||
printf("usage: %s value\n", argv[0]);
|
||||
return;
|
||||
}
|
||||
t = atoi(argv[1]);
|
||||
if (t < 0)
|
||||
printf("%s: bad value\n", argv[1]);
|
||||
else
|
||||
rexmtval = t;
|
||||
}
|
||||
|
||||
int maxtimeout = 5 * TIMEOUT;
|
||||
|
||||
void
|
||||
settimeout(int argc, char *argv[])
|
||||
{
|
||||
int t;
|
||||
|
||||
if (argc < 2) {
|
||||
strcpy(line, "Maximum-timeout ");
|
||||
printf("(value) ");
|
||||
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
|
||||
makeargv();
|
||||
argc = margc;
|
||||
argv = margv;
|
||||
}
|
||||
if (argc != 2) {
|
||||
printf("usage: %s value\n", argv[0]);
|
||||
return;
|
||||
}
|
||||
t = atoi(argv[1]);
|
||||
if (t < 0)
|
||||
printf("%s: bad value\n", argv[1]);
|
||||
else
|
||||
maxtimeout = t;
|
||||
}
|
||||
|
||||
void
|
||||
status(int argc, char *argv[])
|
||||
{
|
||||
if (connected)
|
||||
printf("Connected to %s.\n", hostname);
|
||||
else
|
||||
printf("Not connected.\n");
|
||||
printf("Mode: %s Verbose: %s Tracing: %s\n", mode,
|
||||
verbose ? "on" : "off", trace ? "on" : "off");
|
||||
printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
|
||||
rexmtval, maxtimeout);
|
||||
}
|
||||
|
||||
void
|
||||
intr(int sig)
|
||||
{
|
||||
|
||||
signal(SIGALRM, SIG_IGN);
|
||||
alarm(0);
|
||||
longjmp(toplevel, -1);
|
||||
}
|
||||
|
||||
char *
|
||||
tail(char *filename)
|
||||
{
|
||||
char *s;
|
||||
|
||||
while (*filename) {
|
||||
s = strrchr(filename, '/');
|
||||
if (s == NULL)
|
||||
break;
|
||||
if (s[1])
|
||||
return (s + 1);
|
||||
*s = '\0';
|
||||
}
|
||||
return (filename);
|
||||
}
|
||||
|
||||
/*
|
||||
* Command parser.
|
||||
*/
|
||||
static void
|
||||
command(void)
|
||||
{
|
||||
struct cmd *c;
|
||||
|
||||
for (;;) {
|
||||
printf("%s> ", prompt);
|
||||
if (fgets(line, LBUFLEN, stdin) == 0) {
|
||||
if (feof(stdin)) {
|
||||
exit(0);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ((line[0] == 0) || (line[0] == '\n'))
|
||||
continue;
|
||||
makeargv();
|
||||
if (margc == 0)
|
||||
continue;
|
||||
c = getcmd(margv[0]);
|
||||
if (c == (struct cmd *)-1) {
|
||||
printf("?Ambiguous command\n");
|
||||
continue;
|
||||
}
|
||||
if (c == 0) {
|
||||
printf("?Invalid command\n");
|
||||
continue;
|
||||
}
|
||||
(*c->handler)(margc, margv);
|
||||
}
|
||||
}
|
||||
|
||||
struct cmd *
|
||||
getcmd(char *name)
|
||||
{
|
||||
char *p, *q;
|
||||
struct cmd *c, *found;
|
||||
int nmatches, longest;
|
||||
|
||||
longest = 0;
|
||||
nmatches = 0;
|
||||
found = 0;
|
||||
for (c = cmdtab; (p = c->name) != NULL; c++) {
|
||||
for (q = name; *q == *p++; q++)
|
||||
if (*q == 0) /* exact match? */
|
||||
return (c);
|
||||
if (!*q) { /* the name was a prefix */
|
||||
if (q - name > longest) {
|
||||
longest = q - name;
|
||||
nmatches = 1;
|
||||
found = c;
|
||||
} else if (q - name == longest)
|
||||
nmatches++;
|
||||
}
|
||||
}
|
||||
if (nmatches > 1)
|
||||
return ((struct cmd *)-1);
|
||||
return (found);
|
||||
}
|
||||
|
||||
/*
|
||||
* Slice a string up into argc/argv.
|
||||
*/
|
||||
static void
|
||||
makeargv(void)
|
||||
{
|
||||
char *cp;
|
||||
char **argp = margv;
|
||||
|
||||
margc = 0;
|
||||
for (cp = line; *cp;) {
|
||||
while (isspace(*cp))
|
||||
cp++;
|
||||
if (*cp == '\0')
|
||||
break;
|
||||
*argp++ = cp;
|
||||
margc += 1;
|
||||
while (*cp != '\0' && !isspace(*cp))
|
||||
cp++;
|
||||
if (*cp == '\0')
|
||||
break;
|
||||
*cp++ = '\0';
|
||||
}
|
||||
*argp++ = 0;
|
||||
}
|
||||
|
||||
void
|
||||
quit(int argc, char *argv[])
|
||||
{
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Help command.
|
||||
*/
|
||||
void
|
||||
help(int argc, char *argv[])
|
||||
{
|
||||
struct cmd *c;
|
||||
|
||||
if (argc == 1) {
|
||||
printf("Commands may be abbreviated. Commands are:\n\n");
|
||||
for (c = cmdtab; c->name; c++)
|
||||
printf("%-*s\t%s\n", (int)HELPINDENT, c->name, c->help);
|
||||
return;
|
||||
}
|
||||
while (--argc > 0) {
|
||||
char *arg;
|
||||
arg = *++argv;
|
||||
c = getcmd(arg);
|
||||
if (c == (struct cmd *)-1)
|
||||
printf("?Ambiguous help command %s\n", arg);
|
||||
else if (c == (struct cmd *)0)
|
||||
printf("?Invalid help command %s\n", arg);
|
||||
else
|
||||
printf("%s\n", c->help);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
settrace(int argc, char *argv[])
|
||||
{
|
||||
trace = !trace;
|
||||
printf("Packet tracing %s.\n", trace ? "on" : "off");
|
||||
}
|
||||
|
||||
void
|
||||
setverbose(int argc, char *argv[])
|
||||
{
|
||||
verbose = !verbose;
|
||||
printf("Verbose mode %s.\n", verbose ? "on" : "off");
|
||||
}
|
177
tftp/tftp.1
Normal file
177
tftp/tftp.1
Normal file
|
@ -0,0 +1,177 @@
|
|||
.\" $OpenBSD: tftp.1,v 1.4 1999/06/05 01:21:43 aaron Exp $
|
||||
.\" $NetBSD: tftp.1,v 1.5 1995/08/18 14:45:44 pk Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1993, 1994
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" Redistribution and use in source and binary forms, with or without
|
||||
.\" modification, are permitted provided that the following conditions
|
||||
.\" are met:
|
||||
.\" 1. Redistributions of source code must retain the above copyright
|
||||
.\" notice, this list of conditions and the following disclaimer.
|
||||
.\" 2. Redistributions in binary form must reproduce the above copyright
|
||||
.\" notice, this list of conditions and the following disclaimer in the
|
||||
.\" documentation and/or other materials provided with the distribution.
|
||||
.\" 3. All advertising materials mentioning features or use of this software
|
||||
.\" must display the following acknowledgement:
|
||||
.\" This product includes software developed by the University of
|
||||
.\" California, Berkeley and its contributors.
|
||||
.\" 4. Neither the name of the University nor the names of its contributors
|
||||
.\" may be used to endorse or promote products derived from this software
|
||||
.\" without specific prior written permission.
|
||||
.\"
|
||||
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
.\" SUCH DAMAGE.
|
||||
.\"
|
||||
.\" @(#)tftp.1 8.2 (Berkeley) 4/18/94
|
||||
.\"
|
||||
.Dd April 18, 1994
|
||||
.Dt TFTP 1
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm tftp
|
||||
.Nd trivial file transfer program
|
||||
.Sh SYNOPSIS
|
||||
.Nm tftp
|
||||
.Op Ar host
|
||||
.Sh DESCRIPTION
|
||||
.Nm tftp
|
||||
is the user interface to the Internet
|
||||
.Tn TFTP
|
||||
(Trivial File Transfer Protocol),
|
||||
which allows users to transfer files to and from a remote machine.
|
||||
The remote
|
||||
.Ar host
|
||||
may be specified on the command line, in which case
|
||||
.Nm tftp
|
||||
uses
|
||||
.Ar host
|
||||
as the default host for future transfers (see the
|
||||
.Ic connect
|
||||
command below).
|
||||
.Sh COMMANDS
|
||||
Once
|
||||
.Nm tftp
|
||||
is running, it issues the prompt
|
||||
.Ql tftp>
|
||||
and recognizes the following commands:
|
||||
.Pp
|
||||
.Bl -tag -width verbose -compact
|
||||
.It Ic \&? Ar command-name Op Ar ...
|
||||
Print help information.
|
||||
.Pp
|
||||
.It Ic ascii
|
||||
Shorthand for
|
||||
.Ic mode ascii .
|
||||
.Pp
|
||||
.It Ic binary
|
||||
Shorthand for
|
||||
.Ic mode binary .
|
||||
.Pp
|
||||
.It Ic connect Ar host Op Ar port
|
||||
Set the
|
||||
.Ar host
|
||||
(and optionally
|
||||
.Ar port )
|
||||
for transfers.
|
||||
Note that the
|
||||
.Tn TFTP
|
||||
protocol, unlike the
|
||||
.Tn FTP
|
||||
protocol,
|
||||
does not maintain connections between transfers; thus, the
|
||||
.Ic connect
|
||||
command does not actually create a connection,
|
||||
but merely remembers what host is to be used for transfers.
|
||||
You do not have to use the
|
||||
.Ic connect
|
||||
command; the remote host can be specified as part of the
|
||||
.Ic get
|
||||
or
|
||||
.Ic put
|
||||
commands.
|
||||
.Pp
|
||||
.It Ic get Ar filename
|
||||
.It Ic get Ar remotename localname
|
||||
.It Ic get Ar file Op Ar ...
|
||||
Get a file or set of files from the specified
|
||||
.Ar sources .
|
||||
.Ar source
|
||||
can be in one of two forms:
|
||||
a filename on the remote host, if the host has already been specified,
|
||||
or a string of the form
|
||||
.Ar hosts:filename
|
||||
to specify both a host and filename at the same time.
|
||||
If the latter form is used,
|
||||
the last hostname specified becomes the default for future transfers.
|
||||
.Pp
|
||||
.It Ic mode Ar transfer-mode
|
||||
Set the mode for transfers;
|
||||
.Ar transfer-mode
|
||||
may be one of
|
||||
.Ic ascii
|
||||
or
|
||||
.Ic binary .
|
||||
The default is
|
||||
.Ic ascii .
|
||||
.Pp
|
||||
.It Ic put Ar file
|
||||
.It Ic put Ar localfile remotefile
|
||||
.It Ic put Ar file1 file2 ... fileN remote-directory
|
||||
Put a file or set of files to the specified
|
||||
remote file or directory.
|
||||
The destination
|
||||
can be in one of two forms:
|
||||
a filename on the remote host, if the host has already been specified,
|
||||
or a string of the form
|
||||
.Ar hosts:filename
|
||||
to specify both a host and filename at the same time.
|
||||
If the latter form is used,
|
||||
the hostname specified becomes the default for future transfers.
|
||||
If the remote-directory form is used, the remote host is
|
||||
assumed to be a
|
||||
.Tn UNIX
|
||||
machine.
|
||||
.Pp
|
||||
.It Ic quit
|
||||
Exit
|
||||
.Nm tftp .
|
||||
An end-of-file also exits.
|
||||
.Pp
|
||||
.It Ic rexmt Ar retransmission-timeout
|
||||
Set the per-packet retransmission timeout, in seconds.
|
||||
.Pp
|
||||
.It Ic status
|
||||
Show current status.
|
||||
.Pp
|
||||
.It Ic timeout Ar total-transmission-timeout
|
||||
Set the total transmission timeout, in seconds.
|
||||
.Pp
|
||||
.It Ic trace
|
||||
Toggle packet tracing.
|
||||
.Pp
|
||||
.It Ic verbose
|
||||
Toggle verbose mode.
|
||||
.El
|
||||
.Sh BUGS
|
||||
Because there is no user login or validation within
|
||||
the
|
||||
.Tn TFTP
|
||||
protocol, the remote site will probably have some
|
||||
sort of file access restrictions in place. The
|
||||
exact methods are specific to each site and therefore
|
||||
difficult to document here.
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Nm
|
||||
command appeared in
|
||||
.Bx 4.3 .
|
453
tftp/tftp.c
Normal file
453
tftp/tftp.c
Normal file
|
@ -0,0 +1,453 @@
|
|||
/* $Id$ */
|
||||
|
||||
/* $OpenBSD: tftp.c,v 1.4 1997/08/06 06:43:45 deraadt Exp $ */
|
||||
/* $NetBSD: tftp.c,v 1.5 1995/04/29 05:55:25 cgd Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1983, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
/* static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; */
|
||||
/* static char rcsid[] = "$OpenBSD: tftp.c,v 1.4 1997/08/06 06:43:45 deraadt Exp $"; */
|
||||
static const char *rcsid = "tftp-hpa $Id$";
|
||||
#endif /* not lint */
|
||||
|
||||
/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
|
||||
|
||||
/*
|
||||
* TFTP User Program -- Protocol Machines
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <arpa/tftp.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "extern.h"
|
||||
#include "tftpsubs.h"
|
||||
|
||||
extern int errno;
|
||||
|
||||
extern struct sockaddr_in peeraddr; /* filled in by main */
|
||||
extern int f; /* the opened socket */
|
||||
extern int trace;
|
||||
extern int verbose;
|
||||
extern int rexmtval;
|
||||
extern int maxtimeout;
|
||||
|
||||
#ifndef EOPTNEG
|
||||
#define EOPTNEG 8
|
||||
#endif
|
||||
#ifndef OACK
|
||||
#define OACK 6
|
||||
#endif
|
||||
|
||||
#define PKTSIZE SEGSIZE+4
|
||||
char ackbuf[PKTSIZE];
|
||||
int timeout;
|
||||
jmp_buf toplevel;
|
||||
jmp_buf timeoutbuf;
|
||||
|
||||
static void nak(int);
|
||||
static int makerequest(int, const char *, struct tftphdr *, const char *);
|
||||
static void printstats(const char *, unsigned long);
|
||||
static void startclock(void);
|
||||
static void stopclock(void);
|
||||
static void timer(int);
|
||||
static void tpacket(const char *, struct tftphdr *, int);
|
||||
|
||||
/*
|
||||
* Send the requested file.
|
||||
*/
|
||||
void
|
||||
sendfile(int fd, char *name, char *mode)
|
||||
{
|
||||
struct tftphdr *ap; /* data and ack packets */
|
||||
struct tftphdr *dp;
|
||||
int n;
|
||||
volatile int block, size, convert;
|
||||
volatile unsigned long amount;
|
||||
struct sockaddr_in from;
|
||||
int fromlen;
|
||||
FILE *file;
|
||||
|
||||
startclock(); /* start stat's clock */
|
||||
dp = r_init(); /* reset fillbuf/read-ahead code */
|
||||
ap = (struct tftphdr *)ackbuf;
|
||||
file = fdopen(fd, "r");
|
||||
convert = !strcmp(mode, "netascii");
|
||||
block = 0;
|
||||
amount = 0;
|
||||
|
||||
signal(SIGALRM, timer);
|
||||
do {
|
||||
if (block == 0)
|
||||
size = makerequest(WRQ, name, dp, mode) - 4;
|
||||
else {
|
||||
/* size = read(fd, dp->th_data, SEGSIZE); */
|
||||
size = readit(file, &dp, convert);
|
||||
if (size < 0) {
|
||||
nak(errno + 100);
|
||||
break;
|
||||
}
|
||||
dp->th_opcode = htons((u_short)DATA);
|
||||
dp->th_block = htons((u_short)block);
|
||||
}
|
||||
timeout = 0;
|
||||
(void) setjmp(timeoutbuf);
|
||||
send_data:
|
||||
if (trace)
|
||||
tpacket("sent", dp, size + 4);
|
||||
n = sendto(f, dp, size + 4, 0,
|
||||
(struct sockaddr *)&peeraddr, sizeof(peeraddr));
|
||||
if (n != size + 4) {
|
||||
perror("tftp: sendto");
|
||||
goto abort;
|
||||
}
|
||||
read_ahead(file, convert);
|
||||
for ( ; ; ) {
|
||||
alarm(rexmtval);
|
||||
do {
|
||||
fromlen = sizeof(from);
|
||||
n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
|
||||
(struct sockaddr *)&from, &fromlen);
|
||||
} while (n <= 0);
|
||||
alarm(0);
|
||||
if (n < 0) {
|
||||
perror("tftp: recvfrom");
|
||||
goto abort;
|
||||
}
|
||||
peeraddr.sin_port = from.sin_port; /* added */
|
||||
if (trace)
|
||||
tpacket("received", ap, n);
|
||||
/* should verify packet came from server */
|
||||
ap->th_opcode = ntohs(ap->th_opcode);
|
||||
ap->th_block = ntohs(ap->th_block);
|
||||
if (ap->th_opcode == ERROR) {
|
||||
printf("Error code %d: %s\n", ap->th_code,
|
||||
ap->th_msg);
|
||||
goto abort;
|
||||
}
|
||||
if (ap->th_opcode == ACK) {
|
||||
int j;
|
||||
|
||||
if (ap->th_block == block) {
|
||||
break;
|
||||
}
|
||||
/* On an error, try to synchronize
|
||||
* both sides.
|
||||
*/
|
||||
j = synchnet(f);
|
||||
if (j && trace) {
|
||||
printf("discarded %d packets\n",
|
||||
j);
|
||||
}
|
||||
if (ap->th_block == (block-1)) {
|
||||
goto send_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (block > 0)
|
||||
amount += size;
|
||||
block++;
|
||||
} while (size == SEGSIZE || block == 1);
|
||||
abort:
|
||||
fclose(file);
|
||||
stopclock();
|
||||
if (amount > 0)
|
||||
printstats("Sent", amount);
|
||||
}
|
||||
|
||||
/*
|
||||
* Receive a file.
|
||||
*/
|
||||
void
|
||||
recvfile(int fd, char *name, char *mode)
|
||||
{
|
||||
struct tftphdr *ap;
|
||||
struct tftphdr *dp;
|
||||
int n;
|
||||
volatile int block, size, firsttrip;
|
||||
volatile unsigned long amount;
|
||||
struct sockaddr_in from;
|
||||
int fromlen;
|
||||
FILE *file;
|
||||
volatile int convert; /* true if converting crlf -> lf */
|
||||
|
||||
startclock();
|
||||
dp = w_init();
|
||||
ap = (struct tftphdr *)ackbuf;
|
||||
file = fdopen(fd, "w");
|
||||
convert = !strcmp(mode, "netascii");
|
||||
block = 1;
|
||||
firsttrip = 1;
|
||||
amount = 0;
|
||||
|
||||
signal(SIGALRM, timer);
|
||||
do {
|
||||
if (firsttrip) {
|
||||
size = makerequest(RRQ, name, ap, mode);
|
||||
firsttrip = 0;
|
||||
} else {
|
||||
ap->th_opcode = htons((u_short)ACK);
|
||||
ap->th_block = htons((u_short)(block));
|
||||
size = 4;
|
||||
block++;
|
||||
}
|
||||
timeout = 0;
|
||||
(void) setjmp(timeoutbuf);
|
||||
send_ack:
|
||||
if (trace)
|
||||
tpacket("sent", ap, size);
|
||||
if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr,
|
||||
sizeof(peeraddr)) != size) {
|
||||
alarm(0);
|
||||
perror("tftp: sendto");
|
||||
goto abort;
|
||||
}
|
||||
write_behind(file, convert);
|
||||
for ( ; ; ) {
|
||||
alarm(rexmtval);
|
||||
do {
|
||||
fromlen = sizeof(from);
|
||||
n = recvfrom(f, dp, PKTSIZE, 0,
|
||||
(struct sockaddr *)&from, &fromlen);
|
||||
} while (n <= 0);
|
||||
alarm(0);
|
||||
if (n < 0) {
|
||||
perror("tftp: recvfrom");
|
||||
goto abort;
|
||||
}
|
||||
peeraddr.sin_port = from.sin_port; /* added */
|
||||
if (trace)
|
||||
tpacket("received", dp, n);
|
||||
/* should verify client address */
|
||||
dp->th_opcode = ntohs(dp->th_opcode);
|
||||
dp->th_block = ntohs(dp->th_block);
|
||||
if (dp->th_opcode == ERROR) {
|
||||
printf("Error code %d: %s\n", dp->th_code,
|
||||
dp->th_msg);
|
||||
goto abort;
|
||||
}
|
||||
if (dp->th_opcode == DATA) {
|
||||
int j;
|
||||
|
||||
if (dp->th_block == block) {
|
||||
break; /* have next packet */
|
||||
}
|
||||
/* On an error, try to synchronize
|
||||
* both sides.
|
||||
*/
|
||||
j = synchnet(f);
|
||||
if (j && trace) {
|
||||
printf("discarded %d packets\n", j);
|
||||
}
|
||||
if (dp->th_block == (block-1)) {
|
||||
goto send_ack; /* resend ack */
|
||||
}
|
||||
}
|
||||
}
|
||||
/* size = write(fd, dp->th_data, n - 4); */
|
||||
size = writeit(file, &dp, n - 4, convert);
|
||||
if (size < 0) {
|
||||
nak(errno + 100);
|
||||
break;
|
||||
}
|
||||
amount += size;
|
||||
} while (size == SEGSIZE);
|
||||
abort: /* ok to ack, since user */
|
||||
ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
|
||||
ap->th_block = htons((u_short)block);
|
||||
(void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
|
||||
sizeof(peeraddr));
|
||||
write_behind(file, convert); /* flush last buffer */
|
||||
fclose(file);
|
||||
stopclock();
|
||||
if (amount > 0)
|
||||
printstats("Received", amount);
|
||||
}
|
||||
|
||||
static int
|
||||
makerequest(int request, const char *name,
|
||||
struct tftphdr *tp, const char *mode)
|
||||
{
|
||||
char *cp;
|
||||
|
||||
tp->th_opcode = htons((u_short)request);
|
||||
cp = tp->th_stuff;
|
||||
strcpy(cp, name);
|
||||
cp += strlen(name);
|
||||
*cp++ = '\0';
|
||||
strcpy(cp, mode);
|
||||
cp += strlen(mode);
|
||||
*cp++ = '\0';
|
||||
return (cp - (char *)tp);
|
||||
}
|
||||
|
||||
struct errmsg {
|
||||
int e_code;
|
||||
char *e_msg;
|
||||
} errmsgs[] = {
|
||||
{ EUNDEF, "Undefined error code" },
|
||||
{ ENOTFOUND, "File not found" },
|
||||
{ EACCESS, "Access violation" },
|
||||
{ ENOSPACE, "Disk full or allocation exceeded" },
|
||||
{ EBADOP, "Illegal TFTP operation" },
|
||||
{ EBADID, "Unknown transfer ID" },
|
||||
{ EEXISTS, "File already exists" },
|
||||
{ ENOUSER, "No such user" },
|
||||
{ EOPTNEG, "Failure to negotiate RFC2347 options" },
|
||||
{ -1, 0 }
|
||||
};
|
||||
|
||||
/*
|
||||
* Send a nak packet (error message).
|
||||
* Error code passed in is one of the
|
||||
* standard TFTP codes, or a UNIX errno
|
||||
* offset by 100.
|
||||
*/
|
||||
static void
|
||||
nak(int error)
|
||||
{
|
||||
struct errmsg *pe;
|
||||
struct tftphdr *tp;
|
||||
int length;
|
||||
|
||||
tp = (struct tftphdr *)ackbuf;
|
||||
tp->th_opcode = htons((u_short)ERROR);
|
||||
tp->th_code = htons((u_short)error);
|
||||
for (pe = errmsgs; pe->e_code >= 0; pe++)
|
||||
if (pe->e_code == error)
|
||||
break;
|
||||
if (pe->e_code < 0) {
|
||||
pe->e_msg = strerror(error - 100);
|
||||
tp->th_code = EUNDEF;
|
||||
}
|
||||
strcpy(tp->th_msg, pe->e_msg);
|
||||
length = strlen(pe->e_msg) + 4;
|
||||
if (trace)
|
||||
tpacket("sent", tp, length);
|
||||
if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&peeraddr,
|
||||
sizeof(peeraddr)) != length)
|
||||
perror("nak");
|
||||
}
|
||||
|
||||
static void
|
||||
tpacket(const char *s, struct tftphdr *tp, int n)
|
||||
{
|
||||
static char *opcodes[] =
|
||||
{ "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
|
||||
char *cp, *file;
|
||||
u_short op = ntohs(tp->th_opcode);
|
||||
|
||||
if (op < RRQ || op > ERROR)
|
||||
printf("%s opcode=%x ", s, op);
|
||||
else
|
||||
printf("%s %s ", s, opcodes[op]);
|
||||
switch (op) {
|
||||
|
||||
case RRQ:
|
||||
case WRQ:
|
||||
n -= 2;
|
||||
file = cp = tp->th_stuff;
|
||||
cp = strchr(cp, '\0');
|
||||
printf("<file=%s, mode=%s>\n", file, cp + 1);
|
||||
break;
|
||||
|
||||
case DATA:
|
||||
printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
|
||||
break;
|
||||
|
||||
case ACK:
|
||||
printf("<block=%d>\n", ntohs(tp->th_block));
|
||||
break;
|
||||
|
||||
case ERROR:
|
||||
printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
struct timeval tstart;
|
||||
struct timeval tstop;
|
||||
|
||||
static void
|
||||
startclock(void)
|
||||
{
|
||||
(void)gettimeofday(&tstart, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
stopclock(void)
|
||||
{
|
||||
|
||||
(void)gettimeofday(&tstop, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
printstats(const char *direction, unsigned long amount)
|
||||
{
|
||||
double delta;
|
||||
/* compute delta in 1/10's second units */
|
||||
delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
|
||||
((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
|
||||
delta = delta/10.; /* back to seconds */
|
||||
printf("%s %lu bytes in %.1f seconds", direction, amount, delta);
|
||||
if (verbose)
|
||||
printf(" [%.0f bits/sec]", (amount*8.)/delta);
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
static void
|
||||
timer(int sig)
|
||||
{
|
||||
int save_errno = errno;
|
||||
|
||||
timeout += rexmtval;
|
||||
if (timeout >= maxtimeout) {
|
||||
printf("Transfer timed out.\n");
|
||||
errno = save_errno;
|
||||
longjmp(toplevel, -1);
|
||||
}
|
||||
errno = save_errno;
|
||||
longjmp(timeoutbuf, 1);
|
||||
}
|
271
tftp/tftpsubs.c
Normal file
271
tftp/tftpsubs.c
Normal file
|
@ -0,0 +1,271 @@
|
|||
/* $OpenBSD: tftpsubs.c,v 1.2 1996/06/26 05:40:36 deraadt Exp $ */
|
||||
/* $NetBSD: tftpsubs.c,v 1.3 1994/12/08 09:51:31 jtc Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1983, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 6/6/93";
|
||||
#endif
|
||||
static char rcsid[] = "$OpenBSD: tftpsubs.c,v 1.2 1996/06/26 05:40:36 deraadt Exp $";
|
||||
#endif /* not lint */
|
||||
|
||||
/* Simple minded read-ahead/write-behind subroutines for tftp user and
|
||||
server. Written originally with multiple buffers in mind, but current
|
||||
implementation has two buffer logic wired in.
|
||||
|
||||
Todo: add some sort of final error check so when the write-buffer
|
||||
is finally flushed, the caller can detect if the disk filled up
|
||||
(or had an i/o error) and return a nak to the other side.
|
||||
|
||||
Jim Guyton 10/85
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/tftp.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "tftpsubs.h"
|
||||
|
||||
#define PKTSIZE MAX_SEGSIZE+4 /* should be moved to tftp.h */
|
||||
|
||||
int segsize = SEGSIZE; /* Default segsize */
|
||||
|
||||
struct bf {
|
||||
int counter; /* size of data in buffer, or flag */
|
||||
char buf[PKTSIZE]; /* room for data packet */
|
||||
} bfs[2];
|
||||
|
||||
/* Values for bf.counter */
|
||||
#define BF_ALLOC -3 /* alloc'd but not yet filled */
|
||||
#define BF_FREE -2 /* free */
|
||||
/* [-1 .. segsize] = size of data in the data buffer */
|
||||
|
||||
static int nextone; /* index of next buffer to use */
|
||||
static int current; /* index of buffer in use */
|
||||
|
||||
/* control flags for crlf conversions */
|
||||
int newline = 0; /* fillbuf: in middle of newline expansion */
|
||||
int prevchar = -1; /* putbuf: previous char (cr check) */
|
||||
|
||||
static struct tftphdr *rw_init(int);
|
||||
|
||||
struct tftphdr *w_init() { return rw_init(0); } /* write-behind */
|
||||
struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */
|
||||
|
||||
/* init for either read-ahead or write-behind */
|
||||
/* x == zero for write-behind, one for read-head */
|
||||
static struct tftphdr *
|
||||
rw_init(int x)
|
||||
{
|
||||
newline = 0; /* init crlf flag */
|
||||
prevchar = -1;
|
||||
bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
|
||||
current = 0;
|
||||
bfs[1].counter = BF_FREE;
|
||||
nextone = x; /* ahead or behind? */
|
||||
return (struct tftphdr *)bfs[0].buf;
|
||||
}
|
||||
|
||||
|
||||
/* Have emptied current buffer by sending to net and getting ack.
|
||||
Free it and return next buffer filled with data.
|
||||
*/
|
||||
int
|
||||
readit(FILE *file, struct tftphdr **dpp, int convert)
|
||||
{
|
||||
struct bf *b;
|
||||
|
||||
bfs[current].counter = BF_FREE; /* free old one */
|
||||
current = !current; /* "incr" current */
|
||||
|
||||
b = &bfs[current]; /* look at new buffer */
|
||||
if (b->counter == BF_FREE) /* if it's empty */
|
||||
read_ahead(file, convert); /* fill it */
|
||||
/* assert(b->counter != BF_FREE);*//* check */
|
||||
*dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
|
||||
return b->counter;
|
||||
}
|
||||
|
||||
/*
|
||||
* fill the input buffer, doing ascii conversions if requested
|
||||
* conversions are lf -> cr,lf and cr -> cr, nul
|
||||
*/
|
||||
void
|
||||
read_ahead(FILE *file, int convert)
|
||||
{
|
||||
int i;
|
||||
char *p;
|
||||
int c;
|
||||
struct bf *b;
|
||||
struct tftphdr *dp;
|
||||
|
||||
b = &bfs[nextone]; /* look at "next" buffer */
|
||||
if (b->counter != BF_FREE) /* nop if not free */
|
||||
return;
|
||||
nextone = !nextone; /* "incr" next buffer ptr */
|
||||
|
||||
dp = (struct tftphdr *)b->buf;
|
||||
|
||||
if (convert == 0) {
|
||||
b->counter = read(fileno(file), dp->th_data, segsize);
|
||||
return;
|
||||
}
|
||||
|
||||
p = dp->th_data;
|
||||
for (i = 0 ; i < segsize; i++) {
|
||||
if (newline) {
|
||||
if (prevchar == '\n')
|
||||
c = '\n'; /* lf to cr,lf */
|
||||
else c = '\0'; /* cr to cr,nul */
|
||||
newline = 0;
|
||||
}
|
||||
else {
|
||||
c = getc(file);
|
||||
if (c == EOF) break;
|
||||
if (c == '\n' || c == '\r') {
|
||||
prevchar = c;
|
||||
c = '\r';
|
||||
newline = 1;
|
||||
}
|
||||
}
|
||||
*p++ = c;
|
||||
}
|
||||
b->counter = (int)(p - dp->th_data);
|
||||
}
|
||||
|
||||
/* Update count associated with the buffer, get new buffer
|
||||
from the queue. Calls write_behind only if next buffer not
|
||||
available.
|
||||
*/
|
||||
int
|
||||
writeit(FILE *file, struct tftphdr **dpp, int ct, int convert)
|
||||
{
|
||||
bfs[current].counter = ct; /* set size of data to write */
|
||||
current = !current; /* switch to other buffer */
|
||||
if (bfs[current].counter != BF_FREE) /* if not free */
|
||||
(void)write_behind(file, convert); /* flush it */
|
||||
bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
|
||||
*dpp = (struct tftphdr *)bfs[current].buf;
|
||||
return ct; /* this is a lie of course */
|
||||
}
|
||||
|
||||
/*
|
||||
* Output a buffer to a file, converting from netascii if requested.
|
||||
* CR,NUL -> CR and CR,LF => LF.
|
||||
* Note spec is undefined if we get CR as last byte of file or a
|
||||
* CR followed by anything else. In this case we leave it alone.
|
||||
*/
|
||||
int
|
||||
write_behind(FILE *file, int convert)
|
||||
{
|
||||
char *buf;
|
||||
int count;
|
||||
int ct;
|
||||
char *p;
|
||||
int c; /* current character */
|
||||
struct bf *b;
|
||||
struct tftphdr *dp;
|
||||
|
||||
b = &bfs[nextone];
|
||||
if (b->counter < -1) /* anything to flush? */
|
||||
return 0; /* just nop if nothing to do */
|
||||
|
||||
count = b->counter; /* remember byte count */
|
||||
b->counter = BF_FREE; /* reset flag */
|
||||
dp = (struct tftphdr *)b->buf;
|
||||
nextone = !nextone; /* incr for next time */
|
||||
buf = dp->th_data;
|
||||
|
||||
if (count <= 0) return -1; /* nak logic? */
|
||||
|
||||
if (convert == 0)
|
||||
return write(fileno(file), buf, count);
|
||||
|
||||
p = buf;
|
||||
ct = count;
|
||||
while (ct--) { /* loop over the buffer */
|
||||
c = *p++; /* pick up a character */
|
||||
if (prevchar == '\r') { /* if prev char was cr */
|
||||
if (c == '\n') /* if have cr,lf then just */
|
||||
fseek(file, -1, 1); /* smash lf on top of the cr */
|
||||
else
|
||||
if (c == '\0') /* if have cr,nul then */
|
||||
goto skipit; /* just skip over the putc */
|
||||
/* else just fall through and allow it */
|
||||
}
|
||||
putc(c, file);
|
||||
skipit:
|
||||
prevchar = c;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
/* When an error has occurred, it is possible that the two sides
|
||||
* are out of synch. Ie: that what I think is the other side's
|
||||
* response to packet N is really their response to packet N-1.
|
||||
*
|
||||
* So, to try to prevent that, we flush all the input queued up
|
||||
* for us on the network connection on our host.
|
||||
*
|
||||
* We return the number of packets we flushed (mostly for reporting
|
||||
* when trace is active).
|
||||
*/
|
||||
|
||||
int
|
||||
synchnet(int f) /* socket to flush */
|
||||
{
|
||||
int i, j = 0;
|
||||
char rbuf[PKTSIZE];
|
||||
struct sockaddr_in from;
|
||||
int fromlen;
|
||||
|
||||
while (1) {
|
||||
(void) ioctl(f, FIONREAD, &i);
|
||||
if (i) {
|
||||
j++;
|
||||
fromlen = sizeof from;
|
||||
(void) recvfrom(f, rbuf, sizeof (rbuf), 0,
|
||||
(struct sockaddr *)&from, &fromlen);
|
||||
} else {
|
||||
return(j);
|
||||
}
|
||||
}
|
||||
}
|
57
tftp/tftpsubs.h
Normal file
57
tftp/tftpsubs.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
/* $Id$ */
|
||||
|
||||
/* $OpenBSD: tftpsubs.h,v 1.2 1996/06/26 05:40:37 deraadt Exp $ */
|
||||
/* $NetBSD: tftpsubs.h,v 1.2 1994/12/08 09:51:32 jtc Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)tftpsubs.h 8.1 (Berkeley) 6/6/93
|
||||
*/
|
||||
|
||||
/*
|
||||
* Prototypes for read-ahead/write-behind subroutines for tftp user and
|
||||
* server.
|
||||
*/
|
||||
struct tftphdr *r_init(void);
|
||||
void read_ahead(FILE *, int);
|
||||
int readit(FILE *, struct tftphdr **, int);
|
||||
|
||||
int synchnet(int);
|
||||
|
||||
struct tftphdr *w_init(void);
|
||||
int write_behind(FILE *, int);
|
||||
int writeit(FILE *, struct tftphdr **, int, int);
|
||||
|
||||
extern int segsize;
|
||||
#define MAX_SEGSIZE 65464
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue