1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2002-03-30 14:18:15 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2002-03-30 14:18:15 +00:00
|
|
|
static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif /* not lint */
|
1997-08-14 06:47:41 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-03-30 14:18:15 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/* 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>
|
|
|
|
|
2002-03-30 14:18:15 +00:00
|
|
|
#include <arpa/inet.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <arpa/tftp.h>
|
|
|
|
|
1997-08-14 06:47:41 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
1998-02-20 04:33:03 +00:00
|
|
|
#include <string.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <unistd.h>
|
2002-04-11 17:14:22 +00:00
|
|
|
#include <netdb.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
#include "tftpsubs.h"
|
|
|
|
|
2002-04-11 17:14:22 +00:00
|
|
|
extern struct sockaddr_storage peeraddr; /* filled in by main */
|
1994-05-27 12:33:43 +00:00
|
|
|
extern int f; /* the opened socket */
|
|
|
|
extern int trace;
|
|
|
|
extern int verbose;
|
|
|
|
extern int rexmtval;
|
|
|
|
extern int maxtimeout;
|
2002-05-12 01:47:50 +00:00
|
|
|
extern volatile int txrx_error;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#define PKTSIZE SEGSIZE+4
|
|
|
|
char ackbuf[PKTSIZE];
|
|
|
|
int timeout;
|
|
|
|
jmp_buf toplevel;
|
|
|
|
jmp_buf timeoutbuf;
|
|
|
|
|
2002-04-11 17:14:22 +00:00
|
|
|
static void nak(int, struct sockaddr *);
|
2002-03-22 01:42:45 +00:00
|
|
|
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);
|
2002-04-11 17:14:22 +00:00
|
|
|
static int cmpport(struct sockaddr *, struct sockaddr *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the requested file.
|
|
|
|
*/
|
|
|
|
void
|
1998-10-30 16:17:50 +00:00
|
|
|
xmitfile(fd, name, mode)
|
1994-05-27 12:33:43 +00:00
|
|
|
int fd;
|
|
|
|
char *name;
|
|
|
|
char *mode;
|
|
|
|
{
|
2001-12-11 23:43:15 +00:00
|
|
|
struct tftphdr *ap; /* data and ack packets */
|
|
|
|
struct tftphdr *dp;
|
|
|
|
int n;
|
2001-02-02 10:53:02 +00:00
|
|
|
volatile unsigned short block;
|
|
|
|
volatile int size, convert;
|
1994-05-27 12:33:43 +00:00
|
|
|
volatile unsigned long amount;
|
2002-04-11 17:14:22 +00:00
|
|
|
struct sockaddr_storage from;
|
2005-08-05 09:58:49 +00:00
|
|
|
socklen_t fromlen;
|
1994-05-27 12:33:43 +00:00
|
|
|
FILE *file;
|
2002-04-11 17:14:22 +00:00
|
|
|
struct sockaddr_storage peer;
|
|
|
|
struct sockaddr_storage serv; /* valid server port number */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
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;
|
2002-04-11 17:14:22 +00:00
|
|
|
memcpy(&peer, &peeraddr, peeraddr.ss_len);
|
|
|
|
memset(&serv, 0, sizeof(serv));
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
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) {
|
2002-04-11 17:14:22 +00:00
|
|
|
nak(errno + 100, (struct sockaddr *)&peer);
|
1994-05-27 12:33:43 +00:00
|
|
|
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,
|
2002-04-11 17:14:22 +00:00
|
|
|
(struct sockaddr *)&peer, peer.ss_len);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (n != size + 4) {
|
1997-08-14 06:47:41 +00:00
|
|
|
warn("sendto");
|
1994-05-27 12:33:43 +00:00
|
|
|
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) {
|
1997-08-14 06:47:41 +00:00
|
|
|
warn("recvfrom");
|
1994-05-27 12:33:43 +00:00
|
|
|
goto abort;
|
|
|
|
}
|
2002-04-11 17:14:22 +00:00
|
|
|
if (!serv.ss_family)
|
|
|
|
serv = from;
|
|
|
|
else if (!cmpport((struct sockaddr *)&serv,
|
|
|
|
(struct sockaddr *)&from)) {
|
|
|
|
warn("server port mismatch");
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
peer = from;
|
1994-05-27 12:33:43 +00:00
|
|
|
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);
|
2002-05-12 01:47:50 +00:00
|
|
|
txrx_error = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
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(fd, name, mode)
|
|
|
|
int fd;
|
|
|
|
char *name;
|
|
|
|
char *mode;
|
|
|
|
{
|
2001-12-11 23:43:15 +00:00
|
|
|
struct tftphdr *ap;
|
|
|
|
struct tftphdr *dp;
|
|
|
|
int n;
|
2001-02-02 10:53:02 +00:00
|
|
|
volatile unsigned short block;
|
|
|
|
volatile int size, firsttrip;
|
1994-05-27 12:33:43 +00:00
|
|
|
volatile unsigned long amount;
|
2002-04-11 17:14:22 +00:00
|
|
|
struct sockaddr_storage from;
|
2005-08-05 09:58:49 +00:00
|
|
|
socklen_t fromlen;
|
1994-05-27 12:33:43 +00:00
|
|
|
FILE *file;
|
|
|
|
volatile int convert; /* true if converting crlf -> lf */
|
2002-04-11 17:14:22 +00:00
|
|
|
struct sockaddr_storage peer;
|
|
|
|
struct sockaddr_storage serv; /* valid server port number */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
startclock();
|
|
|
|
dp = w_init();
|
|
|
|
ap = (struct tftphdr *)ackbuf;
|
|
|
|
file = fdopen(fd, "w");
|
|
|
|
convert = !strcmp(mode, "netascii");
|
|
|
|
block = 1;
|
|
|
|
firsttrip = 1;
|
|
|
|
amount = 0;
|
2002-04-11 17:14:22 +00:00
|
|
|
memcpy(&peer, &peeraddr, peeraddr.ss_len);
|
|
|
|
memset(&serv, 0, sizeof(serv));
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
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);
|
2002-04-11 17:14:22 +00:00
|
|
|
if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peer,
|
|
|
|
peer.ss_len) != size) {
|
1994-05-27 12:33:43 +00:00
|
|
|
alarm(0);
|
1997-08-14 06:47:41 +00:00
|
|
|
warn("sendto");
|
1994-05-27 12:33:43 +00:00
|
|
|
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) {
|
1997-08-14 06:47:41 +00:00
|
|
|
warn("recvfrom");
|
1994-05-27 12:33:43 +00:00
|
|
|
goto abort;
|
|
|
|
}
|
2002-04-11 17:14:22 +00:00
|
|
|
if (!serv.ss_family)
|
|
|
|
serv = from;
|
|
|
|
else if (!cmpport((struct sockaddr *)&serv,
|
|
|
|
(struct sockaddr *)&from)) {
|
|
|
|
warn("server port mismatch");
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
peer = from;
|
1994-05-27 12:33:43 +00:00
|
|
|
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);
|
2002-05-12 01:47:50 +00:00
|
|
|
txrx_error = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
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) {
|
2002-04-11 17:14:22 +00:00
|
|
|
nak(errno + 100, (struct sockaddr *)&peer);
|
1994-05-27 12:33:43 +00:00
|
|
|
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);
|
2002-04-11 17:14:22 +00:00
|
|
|
(void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peer,
|
|
|
|
peer.ss_len);
|
1994-05-27 12:33:43 +00:00
|
|
|
write_behind(file, convert); /* flush last buffer */
|
|
|
|
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;
|
|
|
|
{
|
2001-12-11 23:43:15 +00:00
|
|
|
char *cp;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
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;
|
2001-12-11 23:43:15 +00:00
|
|
|
const char *e_msg;
|
1994-05-27 12:33:43 +00:00
|
|
|
} 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 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2002-04-11 17:14:22 +00:00
|
|
|
nak(error, peer)
|
1994-05-27 12:33:43 +00:00
|
|
|
int error;
|
2002-04-11 17:14:22 +00:00
|
|
|
struct sockaddr *peer;
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2001-12-11 23:43:15 +00:00
|
|
|
struct errmsg *pe;
|
|
|
|
struct tftphdr *tp;
|
1994-05-27 12:33:43 +00:00
|
|
|
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);
|
2002-04-11 17:14:22 +00:00
|
|
|
if (sendto(f, ackbuf, length, 0, peer, peer->sa_len) != length)
|
1997-08-14 06:47:41 +00:00
|
|
|
warn("nak");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tpacket(s, tp, n)
|
|
|
|
const char *s;
|
|
|
|
struct tftphdr *tp;
|
|
|
|
int n;
|
|
|
|
{
|
2001-12-11 23:43:15 +00:00
|
|
|
static const char *opcodes[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
{ "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
|
2001-12-11 23:43:15 +00:00
|
|
|
char *cp, *file;
|
1994-05-27 12:33:43 +00:00
|
|
|
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 = 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;
|
|
|
|
/* 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 */
|
2001-12-11 23:43:15 +00:00
|
|
|
printf("%s %ld bytes in %.1f seconds", direction, amount, delta);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (verbose)
|
|
|
|
printf(" [%.0f bits/sec]", (amount*8.)/delta);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
timer(sig)
|
2001-12-11 23:43:15 +00:00
|
|
|
int sig __unused;
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
timeout += rexmtval;
|
|
|
|
if (timeout >= maxtimeout) {
|
|
|
|
printf("Transfer timed out.\n");
|
|
|
|
longjmp(toplevel, -1);
|
|
|
|
}
|
2002-05-12 01:47:50 +00:00
|
|
|
txrx_error = 1;
|
1994-05-27 12:33:43 +00:00
|
|
|
longjmp(timeoutbuf, 1);
|
|
|
|
}
|
2002-04-11 17:14:22 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
cmpport(sa, sb)
|
|
|
|
struct sockaddr *sa;
|
|
|
|
struct sockaddr *sb;
|
|
|
|
{
|
|
|
|
char a[NI_MAXSERV], b[NI_MAXSERV];
|
|
|
|
|
|
|
|
if (getnameinfo(sa, sa->sa_len, NULL, 0, a, sizeof(a), NI_NUMERICSERV))
|
|
|
|
return 0;
|
|
|
|
if (getnameinfo(sb, sb->sa_len, NULL, 0, b, sizeof(b), NI_NUMERICSERV))
|
|
|
|
return 0;
|
|
|
|
if (strcmp(a, b) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|