#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 <unistd.h>
#include "extern.h"
#include "tftpsubs.h"
extern int errno;
extern struct sockaddr_in peeraddr;
extern int f;
extern int trace;
extern int verbose;
extern int rexmtval;
extern int maxtimeout;
extern jmp_buf toplevel;
#define PKTSIZE SEGSIZE+4
char ackbuf[PKTSIZE];
int timeout;
jmp_buf timeoutbuf;
static void nak __P((int));
static int makerequest __P((int, const char *, struct tftphdr *, const char *));
static void printstats __P((const char *, unsigned long));
static void startclock __P((void));
static void stopclock __P((void));
static void timer __P((int));
static void tpacket __P((const char *, struct tftphdr *, int));
void
tftp_sendfile(fd, name, mode)
int fd;
char *name;
char *mode;
{
register struct tftphdr *ap;
struct tftphdr *r_init(), *dp;
register int n;
volatile int block, size, convert;
volatile unsigned long amount;
struct sockaddr_in from;
int fromlen;
FILE *file;
startclock();
dp = r_init();
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 = 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;
if (trace)
tpacket("received", ap, n);
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;
}
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);
}
void
recvfile(fd, name, mode)
int fd;
char *name;
char *mode;
{
register struct tftphdr *ap;
struct tftphdr *dp, *w_init();
register int n;
volatile int block, size, firsttrip;
volatile unsigned long amount;
struct sockaddr_in from;
int fromlen;
FILE *file;
volatile int convert;
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;
if (trace)
tpacket("received", dp, n);
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;
}
j = synchnet(f);
if (j && trace) {
printf("discarded %d packets\n", j);
}
if (dp->th_block == (block-1)) {
goto send_ack;
}
}
}
size = writeit(file, &dp, n - 4, convert);
if (size < 0) {
nak(errno + 100);
break;
}
amount += size;
} while (size == SEGSIZE);
abort:
ap->th_opcode = htons((u_short)ACK);
ap->th_block = htons((u_short)block);
(void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
sizeof(peeraddr));
write_behind(file, convert);
fclose(file);
stopclock();
if (amount > 0)
printstats("Received", amount);
}
static int
makerequest(request, name, tp, mode)
int request;
const char *name;
struct tftphdr *tp;
const char *mode;
{
register 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" },
{ -1, 0 }
};
static void
nak(error)
int error;
{
register struct errmsg *pe;
register struct tftphdr *tp;
int length;
char *strerror();
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(s, tp, n)
const char *s;
struct tftphdr *tp;
int n;
{
static char *opcodes[] =
{ "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
register char *cp, *file;
u_short op = ntohs(tp->th_opcode);
char *index();
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 = index(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)gettimeofday(&tstart, NULL);
}
static void
stopclock()
{
(void)gettimeofday(&tstop, NULL);
}
static void
printstats(direction, amount)
const char *direction;
unsigned long amount;
{
double delta;
delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
delta = delta/10.;
printf("%s %d bytes in %.1f seconds", direction, amount, delta);
if (verbose)
printf(" [%.0f bits/sec]", (amount*8.)/delta);
putchar('\n');
}
static void
timer(sig)
int sig;
{
timeout += rexmtval;
if (timeout >= maxtimeout) {
printf("Transfer timed out.\n");
longjmp(toplevel, -1);
}
longjmp(timeoutbuf, 1);
}