NetBSD/usr.bin/tftp/tftp.c

684 lines
16 KiB
C
Raw Normal View History

2006-04-09 22:45:19 +04:00
/* $NetBSD: tftp.c,v 1.25 2006/04/09 18:45:19 christos Exp $ */
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* 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.
*/
1997-10-07 13:19:40 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93";
1997-10-07 13:19:40 +04:00
#else
2006-04-09 22:45:19 +04:00
__RCSID("$NetBSD: tftp.c,v 1.25 2006/04/09 18:45:19 christos Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
/*
* TFTP User Program -- Protocol Machines
*/
#include <sys/types.h>
#include <sys/param.h>
1993-03-21 12:45:37 +03:00
#include <sys/socket.h>
#include <sys/stat.h>
1993-03-21 12:45:37 +03:00
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/tftp.h>
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
1995-04-29 09:55:25 +04:00
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include "extern.h"
#include "tftpsubs.h"
1993-03-21 12:45:37 +03:00
char ackbuf[PKTSIZE];
int timeout;
jmp_buf toplevel;
jmp_buf timeoutbuf;
static void nak __P((int, struct sockaddr *));
static int makerequest __P((int, const char *, struct tftphdr *, const char *, off_t));
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));
static int cmpport __P((struct sockaddr *, struct sockaddr *));
1993-03-21 12:45:37 +03:00
static void get_options(struct tftphdr *, int);
static void
get_options(struct tftphdr *ap, int size)
{
unsigned long val;
char *opt, *endp, *nextopt, *valp;
int l;
size -= 2; /* skip over opcode */
opt = ap->th_stuff;
endp = opt + size - 1;
*endp = '\0';
while (opt < endp) {
l = strlen(opt) + 1;
valp = opt + l;
if (valp < endp) {
val = strtoul(valp, NULL, 10);
l = strlen(valp) + 1;
nextopt = valp + l;
if (val == ULONG_MAX && errno == ERANGE) {
/* Report illegal value */
opt = nextopt;
continue;
}
} else {
/* Badly formed OACK */
break;
}
if (strcmp(opt, "tsize") == 0) {
/* cool, but we'll ignore it */
} else if (strcmp(opt, "timeout") == 0) {
if (val >= 1 && val <= 255) {
rexmtval = val;
} else {
/* Report error? */
}
} else if (strcmp(opt, "blksize") == 0) {
if (val >= 8 && val <= MAXSEGSIZE) {
blksize = val;
} else {
/* Report error? */
}
} else {
/* unknown option */
}
opt = nextopt;
}
}
1993-03-21 12:45:37 +03:00
/*
* Send the requested file.
*/
void
1993-03-21 12:45:37 +03:00
sendfile(fd, name, mode)
int fd;
char *name;
char *mode;
{
struct tftphdr *ap; /* data and ack packets */
1997-10-07 13:19:40 +04:00
struct tftphdr *dp;
int j, n;
volatile unsigned int block;
volatile int size, convert;
volatile unsigned long amount;
struct sockaddr_storage from;
struct stat sbuf;
off_t filesize=0;
socklen_t fromlen;
1993-03-21 12:45:37 +03:00
FILE *file;
struct sockaddr_storage peer;
struct sockaddr_storage serv; /* valid server port number */
1993-03-21 12:45:37 +03:00
startclock(); /* start stat's clock */
dp = r_init(); /* reset fillbuf/read-ahead code */
ap = (struct tftphdr *)(void *)ackbuf;
if (tsize) {
if (fstat(fd, &sbuf) == 0) {
filesize = sbuf.st_size;
} else {
filesize = -1ULL;
}
}
1993-03-21 12:45:37 +03:00
file = fdopen(fd, "r");
convert = !strcmp(mode, "netascii");
block = 0;
amount = 0;
2006-04-09 22:45:19 +04:00
(void)memcpy(&peer, &peeraddr, (size_t)peeraddr.ss_len);
(void)memset(&serv, 0, sizeof(serv));
1993-03-21 12:45:37 +03:00
2006-04-09 22:45:19 +04:00
(void)signal(SIGALRM, timer);
1993-03-21 12:45:37 +03:00
do {
if (block == 0)
size = makerequest(WRQ, name, dp, mode, filesize) - 4;
1993-03-21 12:45:37 +03:00
else {
/* size = read(fd, dp->th_data, SEGSIZE); */
size = readit(file, &dp, blksize, convert);
1993-03-21 12:45:37 +03:00
if (size < 0) {
nak(errno + 100, (struct sockaddr *)(void *)&peer);
1993-03-21 12:45:37 +03: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, (socklen_t)(size + 4), 0,
(struct sockaddr *)(void *)&peer, (socklen_t)peer.ss_len);
1993-03-21 12:45:37 +03:00
if (n != size + 4) {
warn("sendto");
1993-03-21 12:45:37 +03:00
goto abort;
}
if (block)
read_ahead(file, blksize, convert);
1993-03-21 12:45:37 +03:00
for ( ; ; ) {
2006-04-09 22:45:19 +04:00
(void)alarm(rexmtval);
1993-03-21 12:45:37 +03:00
do {
fromlen = sizeof(from);
n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
(struct sockaddr *)(void *)&from, &fromlen);
1993-03-21 12:45:37 +03:00
} while (n <= 0);
2006-04-09 22:45:19 +04:00
(void)alarm(0);
1993-03-21 12:45:37 +03:00
if (n < 0) {
warn("recvfrom");
1993-03-21 12:45:37 +03:00
goto abort;
}
if (!serv.ss_family)
serv = from;
else if (!cmpport((struct sockaddr *)(void *)&serv,
(struct sockaddr *)(void *)&from)) {
warn("server port mismatch");
goto abort;
}
peer = from;
1993-03-21 12:45:37 +03:00
if (trace)
tpacket("received", ap, n);
/* should verify packet came from server */
ap->th_opcode = ntohs(ap->th_opcode);
if (ap->th_opcode == ERROR) {
2006-04-09 22:45:19 +04:00
(void)printf("Error code %d: %s\n", ap->th_code,
1993-03-21 12:45:37 +03:00
ap->th_msg);
goto abort;
}
if (ap->th_opcode == ACK) {
ap->th_block = ntohs(ap->th_block);
1993-03-21 12:45:37 +03:00
if (ap->th_block == 0) {
/*
* If the extended options are enabled,
* the server just refused 'em all.
* The only one that _really_
* matters is blksize, but we'll
* clear timeout, too.
*/
blksize = def_blksize;
rexmtval = def_rexmtval;
}
1993-03-21 12:45:37 +03:00
if (ap->th_block == block) {
break;
}
/* On an error, try to synchronize
* both sides.
*/
j = synchnet(f, blksize+4);
1993-03-21 12:45:37 +03:00
if (j && trace) {
2006-04-09 22:45:19 +04:00
(void)printf("discarded %d packets\n",
1993-03-21 12:45:37 +03:00
j);
}
if (ap->th_block == (block-1)) {
goto send_data;
}
}
if (ap->th_opcode == OACK) {
if (block == 0) {
blksize = def_blksize;
rexmtval = def_rexmtval;
get_options(ap, n);
break;
}
}
1993-03-21 12:45:37 +03:00
}
if (block > 0)
amount += size;
block++;
} while (size == blksize || block == 1);
1993-03-21 12:45:37 +03:00
abort:
2006-04-09 22:45:19 +04:00
(void)fclose(file);
1993-03-21 12:45:37 +03:00
stopclock();
if (amount > 0)
printstats("Sent", amount);
}
/*
* Receive a file.
*/
void
1993-03-21 12:45:37 +03:00
recvfile(fd, name, mode)
int fd;
char *name;
char *mode;
{
struct tftphdr *ap;
1997-10-07 13:19:40 +04:00
struct tftphdr *dp;
int j, n, oack=0;
volatile unsigned int block;
volatile int size, firsttrip;
volatile unsigned long amount;
struct sockaddr_storage from;
socklen_t fromlen;
size_t readlen;
1993-03-21 12:45:37 +03:00
FILE *file;
volatile int convert; /* true if converting crlf -> lf */
struct sockaddr_storage peer;
struct sockaddr_storage serv; /* valid server port number */
1993-03-21 12:45:37 +03:00
startclock();
dp = w_init();
ap = (struct tftphdr *)(void *)ackbuf;
1993-03-21 12:45:37 +03:00
file = fdopen(fd, "w");
convert = !strcmp(mode, "netascii");
block = 1;
firsttrip = 1;
amount = 0;
2006-04-09 22:45:19 +04:00
(void)memcpy(&peer, &peeraddr, (size_t)peeraddr.ss_len);
(void)memset(&serv, 0, sizeof(serv));
1993-03-21 12:45:37 +03:00
2006-04-09 22:45:19 +04:00
(void)signal(SIGALRM, timer);
1993-03-21 12:45:37 +03:00
do {
if (firsttrip) {
size = makerequest(RRQ, name, ap, mode, (off_t)0);
readlen = PKTSIZE;
1993-03-21 12:45:37 +03:00
firsttrip = 0;
} else {
ap->th_opcode = htons((u_short)ACK);
ap->th_block = htons((u_short)(block));
readlen = blksize+4;
1993-03-21 12:45:37 +03:00
size = 4;
block++;
}
timeout = 0;
(void) setjmp(timeoutbuf);
send_ack:
if (trace)
tpacket("sent", ap, size);
if (sendto(f, ackbuf, (socklen_t)size, 0,
(struct sockaddr *)(void *)&peer,
(socklen_t)peer.ss_len) != size) {
2006-04-09 22:45:19 +04:00
(void)alarm(0);
warn("sendto");
1993-03-21 12:45:37 +03:00
goto abort;
}
if (write_behind(file, convert) == -1)
goto abort;
1993-03-21 12:45:37 +03:00
for ( ; ; ) {
2006-04-09 22:45:19 +04:00
(void)alarm(rexmtval);
1993-03-21 12:45:37 +03:00
do {
fromlen = sizeof(from);
n = recvfrom(f, dp, readlen, 0,
(struct sockaddr *)(void *)&from, &fromlen);
1993-03-21 12:45:37 +03:00
} while (n <= 0);
2006-04-09 22:45:19 +04:00
(void)alarm(0);
1993-03-21 12:45:37 +03:00
if (n < 0) {
warn("recvfrom");
1993-03-21 12:45:37 +03:00
goto abort;
}
if (!serv.ss_family)
serv = from;
else if (!cmpport((struct sockaddr *)(void *)&serv,
(struct sockaddr *)(void *)&from)) {
warn("server port mismatch");
goto abort;
}
peer = from;
1993-03-21 12:45:37 +03:00
if (trace)
tpacket("received", dp, n);
/* should verify client address */
dp->th_opcode = ntohs(dp->th_opcode);
if (dp->th_opcode == ERROR) {
2006-04-09 22:45:19 +04:00
(void)printf("Error code %d: %s\n", dp->th_code,
1993-03-21 12:45:37 +03:00
dp->th_msg);
goto abort;
}
if (dp->th_opcode == DATA) {
dp->th_block = ntohs(dp->th_block);
1993-03-21 12:45:37 +03:00
if (dp->th_block == 1 && !oack) {
/* no OACK, revert to defaults */
blksize = def_blksize;
rexmtval = def_rexmtval;
}
1993-03-21 12:45:37 +03:00
if (dp->th_block == block) {
break; /* have next packet */
1993-03-21 12:45:37 +03:00
}
/* On an error, try to synchronize
* both sides.
*/
j = synchnet(f, blksize);
1993-03-21 12:45:37 +03:00
if (j && trace) {
2006-04-09 22:45:19 +04:00
(void)printf("discarded %d packets\n", j);
1993-03-21 12:45:37 +03:00
}
if (dp->th_block == (block-1)) {
goto send_ack; /* resend ack */
1993-03-21 12:45:37 +03:00
}
}
if (dp->th_opcode == OACK) {
if (block == 1) {
oack = 1;
blksize = def_blksize;
rexmtval = def_rexmtval;
get_options(dp, n);
ap->th_opcode = htons(ACK);
ap->th_block = 0;
readlen = blksize+4;
size = 4;
goto send_ack;
}
}
1993-03-21 12:45:37 +03:00
}
/* size = write(fd, dp->th_data, n - 4); */
1993-03-21 12:45:37 +03:00
size = writeit(file, &dp, n - 4, convert);
if (size < 0) {
nak(errno + 100, (struct sockaddr *)(void *)&peer);
1993-03-21 12:45:37 +03:00
break;
}
amount += size;
} while (size == blksize);
abort: /* ok to ack, since user */
ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
1993-03-21 12:45:37 +03:00
ap->th_block = htons((u_short)block);
(void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)(void *)&peer,
(socklen_t)peer.ss_len);
/*
* flush last buffer
* We do not check for failure because last buffer
* can be empty, thus returning an error.
* XXX maybe we should fix 'write_behind' instead.
*/
(void)write_behind(file, convert);
2006-04-09 22:45:19 +04:00
(void)fclose(file);
1993-03-21 12:45:37 +03:00
stopclock();
if (amount > 0)
printstats("Received", amount);
}
static int
makerequest(request, name, tp, mode, filesize)
1993-03-21 12:45:37 +03:00
int request;
const char *name;
1993-03-21 12:45:37 +03:00
struct tftphdr *tp;
const char *mode;
off_t filesize;
1993-03-21 12:45:37 +03:00
{
char *cp;
1993-03-21 12:45:37 +03:00
tp->th_opcode = htons((u_short)request);
#ifndef __SVR4
1993-03-21 12:45:37 +03:00
cp = tp->th_stuff;
#else
cp = (void *)&tp->th_stuff;
#endif
2006-04-09 22:45:19 +04:00
(void)strcpy(cp, name);
1993-03-21 12:45:37 +03:00
cp += strlen(name);
*cp++ = '\0';
2006-04-09 22:45:19 +04:00
(void)strcpy(cp, mode);
1993-03-21 12:45:37 +03:00
cp += strlen(mode);
*cp++ = '\0';
if (tsize) {
2006-04-09 22:45:19 +04:00
(void)strcpy(cp, "tsize");
cp += strlen(cp);
*cp++ = '\0';
2006-04-09 22:45:19 +04:00
(void)sprintf(cp, "%lu", (unsigned long) filesize);
cp += strlen(cp);
*cp++ = '\0';
}
if (tout) {
2006-04-09 22:45:19 +04:00
(void)strcpy(cp, "timeout");
cp += strlen(cp);
*cp++ = '\0';
2006-04-09 22:45:19 +04:00
(void)sprintf(cp, "%d", rexmtval);
cp += strlen(cp);
*cp++ = '\0';
}
if (blksize != SEGSIZE) {
2006-04-09 22:45:19 +04:00
(void)strcpy(cp, "blksize");
cp += strlen(cp);
*cp++ = '\0';
2006-04-09 22:45:19 +04:00
(void)sprintf(cp, "%zd", blksize);
cp += strlen(cp);
*cp++ = '\0';
}
return (cp - (char *)(void *)tp);
1993-03-21 12:45:37 +03:00
}
1998-07-26 19:23:39 +04:00
const struct errmsg {
1993-03-21 12:45:37 +03:00
int e_code;
1998-07-26 19:23:39 +04:00
const char *e_msg;
1993-03-21 12:45:37 +03: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" },
{ EOPTNEG, "Option negotiation failed" },
1993-03-21 12:45:37 +03:00
{ -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(error, peer)
1993-03-21 12:45:37 +03:00
int error;
struct sockaddr *peer;
1993-03-21 12:45:37 +03:00
{
1998-07-26 19:23:39 +04:00
const struct errmsg *pe;
struct tftphdr *tp;
1993-03-21 12:45:37 +03:00
int length;
size_t msglen;
1993-03-21 12:45:37 +03:00
tp = (struct tftphdr *)(void *)ackbuf;
1993-03-21 12:45:37 +03:00
tp->th_opcode = htons((u_short)ERROR);
msglen = sizeof(ackbuf) - (&tp->th_msg[0] - ackbuf);
1993-03-21 12:45:37 +03:00
for (pe = errmsgs; pe->e_code >= 0; pe++)
if (pe->e_code == error)
break;
if (pe->e_code < 0) {
tp->th_code = EUNDEF;
2006-04-09 22:45:19 +04:00
(void)strlcpy(tp->th_msg, strerror(error - 100), msglen);
1998-07-26 19:23:39 +04:00
} else {
tp->th_code = htons((u_short)error);
2006-04-09 22:45:19 +04:00
(void)strlcpy(tp->th_msg, pe->e_msg, msglen);
1993-03-21 12:45:37 +03:00
}
length = strlen(tp->th_msg);
msglen = &tp->th_msg[length + 1] - ackbuf;
1993-03-21 12:45:37 +03:00
if (trace)
tpacket("sent", tp, (int)msglen);
if (sendto(f, ackbuf, msglen, 0, peer, (socklen_t)peer->sa_len) != msglen)
warn("nak");
1993-03-21 12:45:37 +03:00
}
static void
1993-03-21 12:45:37 +03:00
tpacket(s, tp, n)
const char *s;
1993-03-21 12:45:37 +03:00
struct tftphdr *tp;
int n;
{
static const char *opcodes[] =
{ "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
char *cp, *file, *endp, *opt = NULL;
const char *spc;
1993-03-21 12:45:37 +03:00
u_short op = ntohs(tp->th_opcode);
int i, o;
1993-03-21 12:45:37 +03:00
if (op < RRQ || op > OACK)
2006-04-09 22:45:19 +04:00
(void)printf("%s opcode=%x ", s, op);
1993-03-21 12:45:37 +03:00
else
2006-04-09 22:45:19 +04:00
(void)printf("%s %s ", s, opcodes[op]);
1993-03-21 12:45:37 +03:00
switch (op) {
case RRQ:
case WRQ:
n -= 2;
#ifndef __SVR4
cp = tp->th_stuff;
#else
cp = (void *) &tp->th_stuff;
#endif
endp = cp + n - 1;
if (*endp != '\0') { /* Shouldn't happen, but... */
*endp = '\0';
}
file = cp;
cp = strchr(cp, '\0') + 1;
2006-04-09 22:45:19 +04:00
(void)printf("<file=%s, mode=%s", file, cp);
cp = strchr(cp, '\0') + 1;
o = 0;
while (cp < endp) {
i = strlen(cp) + 1;
if (o) {
2006-04-09 22:45:19 +04:00
(void)printf(", %s=%s", opt, cp);
} else {
opt = cp;
}
o = (o+1) % 2;
cp += i;
}
2006-04-09 22:45:19 +04:00
(void)printf(">\n");
1993-03-21 12:45:37 +03:00
break;
case DATA:
2006-04-09 22:45:19 +04:00
(void)printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
1993-03-21 12:45:37 +03:00
break;
case ACK:
2006-04-09 22:45:19 +04:00
(void)printf("<block=%d>\n", ntohs(tp->th_block));
1993-03-21 12:45:37 +03:00
break;
case ERROR:
2006-04-09 22:45:19 +04:00
(void)printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
1993-03-21 12:45:37 +03:00
break;
case OACK:
o = 0;
n -= 2;
cp = tp->th_stuff;
endp = cp + n - 1;
if (*endp != '\0') { /* Shouldn't happen, but... */
*endp = '\0';
}
2006-04-09 22:45:19 +04:00
(void)printf("<");
spc = "";
while (cp < endp) {
i = strlen(cp) + 1;
if (o) {
2006-04-09 22:45:19 +04:00
(void)printf("%s%s=%s", spc, opt, cp);
spc = ", ";
} else {
opt = cp;
}
o = (o+1) % 2;
cp += i;
}
2006-04-09 22:45:19 +04:00
(void)printf(">\n");
break;
1993-03-21 12:45:37 +03:00
}
}
struct timeval tstart;
struct timeval tstop;
static void
startclock()
{
(void)gettimeofday(&tstart, NULL);
1993-03-21 12:45:37 +03:00
}
static void
stopclock()
{
(void)gettimeofday(&tstop, NULL);
1993-03-21 12:45:37 +03:00
}
static void
1993-03-21 12:45:37 +03:00
printstats(direction, amount)
const char *direction;
unsigned long amount;
1993-03-21 12:45:37 +03:00
{
double delta;
1997-10-07 13:19:40 +04:00
/* compute delta in 1/10's second units */
1993-03-21 12:45:37 +03:00
delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
delta = delta/10.; /* back to seconds */
2006-04-09 22:45:19 +04:00
(void)printf("%s %ld bytes in %.1f seconds", direction, amount, delta);
1993-03-21 12:45:37 +03:00
if (verbose)
2006-04-09 22:45:19 +04:00
(void)printf(" [%.0f bits/sec]", (amount*8.)/delta);
(void)putchar('\n');
1993-03-21 12:45:37 +03:00
}
static void
/*ARGSUSED*/
timer(sig)
int sig;
{
timeout += rexmtval;
if (timeout >= maxtimeout) {
2006-04-09 22:45:19 +04:00
(void)printf("Transfer timed out.\n");
longjmp(toplevel, -1);
}
longjmp(timeoutbuf, 1);
}
static int
cmpport(sa, sb)
struct sockaddr *sa;
struct sockaddr *sb;
{
char a[NI_MAXSERV], b[NI_MAXSERV];
if (getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0, a, sizeof(a), NI_NUMERICSERV))
return 0;
if (getnameinfo(sb, (socklen_t)sb->sa_len, NULL, 0, b, sizeof(b), NI_NUMERICSERV))
return 0;
if (strcmp(a, b) != 0)
return 0;
return 1;
}