Home | History | Annotate | Line # | Download | only in tftp
tftp.c revision 1.32
      1 /*	$NetBSD: tftp.c,v 1.32 2011/09/17 15:15:46 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)tftp.c	8.1 (Berkeley) 6/6/93";
     36 #else
     37 __RCSID("$NetBSD: tftp.c,v 1.32 2011/09/17 15:15:46 christos Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
     42 
     43 /*
     44  * TFTP User Program -- Protocol Machines
     45  */
     46 #include <sys/types.h>
     47 #include <sys/param.h>
     48 #include <sys/socket.h>
     49 #include <sys/stat.h>
     50 #include <sys/time.h>
     51 
     52 #include <netinet/in.h>
     53 
     54 #include <arpa/tftp.h>
     55 #include <arpa/inet.h>
     56 
     57 #include <err.h>
     58 #include <errno.h>
     59 #include <setjmp.h>
     60 #include <signal.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 #include <netdb.h>
     66 
     67 #include "extern.h"
     68 #include "tftpsubs.h"
     69 
     70 char    ackbuf[PKTSIZE];
     71 int	timeout;
     72 jmp_buf	toplevel;
     73 jmp_buf	timeoutbuf;
     74 
     75 static void nak __P((int, struct sockaddr *));
     76 static int makerequest __P((int, const char *, struct tftphdr *, const char *, off_t));
     77 static void printstats __P((const char *, unsigned long));
     78 static void startclock __P((void));
     79 static void stopclock __P((void));
     80 __dead static void timer __P((int));
     81 static void tpacket __P((const char *, struct tftphdr *, int));
     82 static int cmpport __P((struct sockaddr *, struct sockaddr *));
     83 
     84 static void get_options(struct tftphdr *, int);
     85 static int tftp_igmp_join(void);
     86 static void tftp_igmp_leave(int);
     87 
     88 static void
     89 get_options(struct tftphdr *ap, int size)
     90 {
     91 	unsigned long val;
     92 	char *opt, *endp, *nextopt, *valp;
     93 	int l;
     94 
     95 	size -= 2;	/* skip over opcode */
     96 	opt = ap->th_stuff;
     97 	endp = opt + size - 1;
     98 	*endp = '\0';
     99 
    100 	while (opt < endp) {
    101 		int ismulticast;
    102 		l = strlen(opt) + 1;
    103 		valp = opt + l;
    104 		ismulticast = !strcasecmp(opt, "multicast");
    105 		if (valp < endp) {
    106 			val = strtoul(valp, NULL, 10);
    107 			l = strlen(valp) + 1;
    108 			nextopt = valp + l;
    109 			if (!ismulticast) {
    110 				if (val == ULONG_MAX && errno == ERANGE) {
    111 					/* Report illegal value */
    112 					opt = nextopt;
    113 					continue;
    114 				}
    115 			}
    116 		} else {
    117 			/* Badly formed OACK */
    118 			break;
    119 		}
    120 		if (strcmp(opt, "tsize") == 0) {
    121 			/* cool, but we'll ignore it */
    122 		} else if (strcmp(opt, "timeout") == 0) {
    123 			if (val >= 1 && val <= 255) {
    124 				rexmtval = val;
    125 			} else {
    126 				/* Report error? */
    127 			}
    128 		} else if (strcmp(opt, "blksize") == 0) {
    129 			if (val >= 8 && val <= MAXSEGSIZE) {
    130 				blksize = val;
    131 			} else {
    132 				/* Report error? */
    133 			}
    134 		} else if (ismulticast) {
    135 			char multicast[24];
    136 			char *pmulticast;
    137 			char *addr;
    138 
    139 			strlcpy(multicast, valp, sizeof(multicast));
    140 			pmulticast = multicast;
    141 			addr = strsep(&pmulticast, ",");
    142 			if (pmulticast == NULL)
    143 				continue; /* Report error? */
    144 			mcport = atoi(strsep(&pmulticast, ","));
    145 			if (pmulticast == NULL)
    146 				continue; /* Report error? */
    147 			mcmasterslave = atoi(pmulticast);
    148 			mcaddr = inet_addr(addr);
    149 			if (mcaddr == INADDR_NONE)
    150 				continue; /* Report error? */
    151 		} else {
    152 			/* unknown option */
    153 		}
    154 		opt = nextopt;
    155 	}
    156 }
    157 
    158 static int
    159 tftp_igmp_join(void)
    160 {
    161 	struct ip_mreq req;
    162 	struct sockaddr_in s;
    163 	int fd, rv;
    164 
    165 	memset(&req, 0, sizeof(struct ip_mreq));
    166 	req.imr_multiaddr.s_addr = mcaddr;
    167 	req.imr_interface.s_addr = INADDR_ANY;
    168 
    169 	fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    170 	if (fd < 0) {
    171 		perror("socket");
    172 		return fd;
    173 	}
    174 
    175 	memset(&s, 0, sizeof(struct sockaddr_in));
    176 	s.sin_family = AF_INET;
    177 	s.sin_port = htons(mcport);
    178 	s.sin_len = sizeof(struct sockaddr_in);
    179 	rv = bind(fd, (struct sockaddr *)&s, sizeof(struct sockaddr_in));
    180 	if (rv < 0) {
    181 		perror("bind");
    182 		close(fd);
    183 		return rv;
    184 	}
    185 
    186 	rv = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req,
    187 	    sizeof(struct ip_mreq));
    188 	if (rv < 0) {
    189 		perror("setsockopt");
    190 		close(fd);
    191 		return rv;
    192 	}
    193 
    194 	return fd;
    195 }
    196 
    197 static void
    198 tftp_igmp_leave(int fd)
    199 {
    200 	struct ip_mreq req;
    201 	int rv;
    202 
    203 	memset(&req, 0, sizeof(struct ip_mreq));
    204 	req.imr_multiaddr.s_addr = mcaddr;
    205 	req.imr_interface.s_addr = INADDR_ANY;
    206 
    207 	rv = setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &req,
    208 	    sizeof(struct ip_mreq));
    209 	if (rv < 0)
    210 		perror("setsockopt");
    211 
    212 	close(fd);
    213 
    214 	return;
    215 }
    216 
    217 /*
    218  * Send the requested file.
    219  */
    220 void
    221 sendfile(int fd, const char *name, const char *mode)
    222 {
    223 	struct tftphdr *ap;	   /* data and ack packets */
    224 	struct tftphdr *dp;
    225 	int j, n;
    226 	volatile unsigned int block;
    227 	volatile int size, convert;
    228 	volatile unsigned long amount;
    229 	struct sockaddr_storage from;
    230 	struct stat sbuf;
    231 	volatile off_t filesize = 0;
    232 	socklen_t fromlen;
    233 	FILE *file;
    234 	struct sockaddr_storage peer;
    235 	struct sockaddr_storage serv;	/* valid server port number */
    236 
    237 	startclock();		/* start stat's clock */
    238 	dp = r_init();		/* reset fillbuf/read-ahead code */
    239 	ap = (struct tftphdr *)(void *)ackbuf;
    240 	if (tsize) {
    241 		if (fstat(fd, &sbuf) == 0) {
    242 			filesize = sbuf.st_size;
    243 		} else {
    244 			filesize = -1ULL;
    245 		}
    246 	}
    247 	file = fdopen(fd, "r");
    248 	convert = !strcmp(mode, "netascii");
    249 	block = 0;
    250 	amount = 0;
    251 	(void)memcpy(&peer, &peeraddr, (size_t)peeraddr.ss_len);
    252 	(void)memset(&serv, 0, sizeof(serv));
    253 
    254 	(void)signal(SIGALRM, timer);
    255 	do {
    256 		if (block == 0)
    257 			size = makerequest(WRQ, name, dp, mode, filesize) - 4;
    258 		else {
    259 		/*	size = read(fd, dp->th_data, SEGSIZE);	 */
    260 			size = readit(file, &dp, blksize, convert);
    261 			if (size < 0) {
    262 				nak(errno + 100, (struct sockaddr *)(void *)&peer);
    263 				break;
    264 			}
    265 			dp->th_opcode = htons((u_short)DATA);
    266 			dp->th_block = htons((u_short)block);
    267 		}
    268 		timeout = 0;
    269 		(void) setjmp(timeoutbuf);
    270 send_data:
    271 		if (trace)
    272 			tpacket("sent", dp, size + 4);
    273 		n = sendto(f, dp, (socklen_t)(size + 4), 0,
    274 		    (struct sockaddr *)(void *)&peer, (socklen_t)peer.ss_len);
    275 		if (n != size + 4) {
    276 			warn("sendto");
    277 			goto abort;
    278 		}
    279 		if (block)
    280 			read_ahead(file, blksize, convert);
    281 		for ( ; ; ) {
    282 			(void)alarm(rexmtval);
    283 			do {
    284 				int curf;
    285 				fromlen = sizeof(from);
    286 				if (mcaddr != INADDR_NONE)
    287 					curf = mf;
    288 				else
    289 					curf = f;
    290 				n = recvfrom(curf, ackbuf, sizeof(ackbuf), 0,
    291 				    (struct sockaddr *)(void *)&from, &fromlen);
    292 			} while (n <= 0);
    293 			(void)alarm(0);
    294 			if (n < 0) {
    295 				warn("recvfrom");
    296 				goto abort;
    297 			}
    298 			if (!serv.ss_family)
    299 				serv = from;
    300 			else if (!cmpport((struct sockaddr *)(void *)&serv,
    301 			    (struct sockaddr *)(void *)&from)) {
    302 				warn("server port mismatch");
    303 				goto abort;
    304 			}
    305 			peer = from;
    306 			if (trace)
    307 				tpacket("received", ap, n);
    308 			/* should verify packet came from server */
    309 			ap->th_opcode = ntohs(ap->th_opcode);
    310 			if (ap->th_opcode == ERROR) {
    311 				(void)printf("Error code %d: %s\n", ap->th_code,
    312 					ap->th_msg);
    313 				goto abort;
    314 			}
    315 			if (ap->th_opcode == ACK) {
    316 				ap->th_block = ntohs(ap->th_block);
    317 
    318 				if (ap->th_block == 0) {
    319 					/*
    320 					 * If the extended options are enabled,
    321 					 * the server just refused 'em all.
    322 					 * The only one that _really_
    323 					 * matters is blksize, but we'll
    324 					 * clear timeout and mcaddr, too.
    325 					 */
    326 					blksize = def_blksize;
    327 					rexmtval = def_rexmtval;
    328 					mcaddr = INADDR_NONE;
    329 				}
    330 				if (ap->th_block == block) {
    331 					break;
    332 				}
    333 				/* On an error, try to synchronize
    334 				 * both sides.
    335 				 */
    336 				j = synchnet(f, blksize+4);
    337 				if (j && trace) {
    338 					(void)printf("discarded %d packets\n",
    339 							j);
    340 				}
    341 				if (ap->th_block == (block-1)) {
    342 					goto send_data;
    343 				}
    344 			}
    345 			if (ap->th_opcode == OACK) {
    346 				if (block == 0) {
    347 					blksize = def_blksize;
    348 					rexmtval = def_rexmtval;
    349 					mcaddr = INADDR_NONE;
    350 					get_options(ap, n);
    351 					break;
    352 				}
    353 			}
    354 		}
    355 		if (block > 0)
    356 			amount += size;
    357 		block++;
    358 	} while ((size_t)size == blksize || block == 1);
    359 abort:
    360 	(void)fclose(file);
    361 	stopclock();
    362 	if (amount > 0)
    363 		printstats("Sent", amount);
    364 }
    365 
    366 /*
    367  * Receive a file.
    368  */
    369 void
    370 recvfile(int fd, const char *name, const char *mode)
    371 {
    372 	struct tftphdr *ap;
    373 	struct tftphdr *dp;
    374 	int j, n;
    375 	volatile int oack = 0;
    376 	volatile unsigned int block;
    377 	volatile int size, firsttrip;
    378 	volatile unsigned long amount;
    379 	struct sockaddr_storage from;
    380 	socklen_t fromlen;
    381 	volatile size_t readlen;
    382 	FILE *file;
    383 	volatile int convert;		/* true if converting crlf -> lf */
    384 	struct sockaddr_storage peer;
    385 	struct sockaddr_storage serv;	/* valid server port number */
    386 
    387 	startclock();
    388 	dp = w_init();
    389 	ap = (struct tftphdr *)(void *)ackbuf;
    390 	file = fdopen(fd, "w");
    391 	convert = !strcmp(mode, "netascii");
    392 	block = 1;
    393 	firsttrip = 1;
    394 	amount = 0;
    395 	(void)memcpy(&peer, &peeraddr, (size_t)peeraddr.ss_len);
    396 	(void)memset(&serv, 0, sizeof(serv));
    397 
    398 	(void)signal(SIGALRM, timer);
    399 	do {
    400 		if (firsttrip) {
    401 			size = makerequest(RRQ, name, ap, mode, (off_t)0);
    402 			readlen = PKTSIZE;
    403 			firsttrip = 0;
    404 		} else {
    405 			ap->th_opcode = htons((u_short)ACK);
    406 			ap->th_block = htons((u_short)(block));
    407 			readlen = blksize+4;
    408 			size = 4;
    409 			block++;
    410 		}
    411 		timeout = 0;
    412 		(void) setjmp(timeoutbuf);
    413 send_ack:
    414 		if (trace)
    415 			tpacket("sent", ap, size);
    416 		if (sendto(f, ackbuf, (socklen_t)size, 0,
    417 		    (struct sockaddr *)(void *)&peer,
    418 		    (socklen_t)peer.ss_len) != size) {
    419 			(void)alarm(0);
    420 			warn("sendto");
    421 			goto abort;
    422 		}
    423 skip_ack:
    424 		if (write_behind(file, convert) == -1)
    425 			goto abort;
    426 		for ( ; ; ) {
    427 			(void)alarm(rexmtval);
    428 			do  {
    429 				int readfd;
    430 				if (mf > 0)
    431 					readfd = mf;
    432 				else
    433 					readfd = f;
    434 				fromlen = sizeof(from);
    435 				n = recvfrom(readfd, dp, readlen, 0,
    436 				    (struct sockaddr *)(void *)&from, &fromlen);
    437 			} while (n <= 0);
    438 			(void)alarm(0);
    439 			if (n < 0) {
    440 				warn("recvfrom");
    441 				goto abort;
    442 			}
    443 			if (!serv.ss_family)
    444 				serv = from;
    445 			else if (!cmpport((struct sockaddr *)(void *)&serv,
    446 			    (struct sockaddr *)(void *)&from)) {
    447 				warn("server port mismatch");
    448 				goto abort;
    449 			}
    450 			peer = from;
    451 			if (trace)
    452 				tpacket("received", dp, n);
    453 			/* should verify client address */
    454 			dp->th_opcode = ntohs(dp->th_opcode);
    455 			if (dp->th_opcode == ERROR) {
    456 				(void)printf("Error code %d: %s\n", dp->th_code,
    457 					dp->th_msg);
    458 				goto abort;
    459 			}
    460 			if (dp->th_opcode == DATA) {
    461 				dp->th_block = ntohs(dp->th_block);
    462 
    463 				if (dp->th_block == 1 && !oack) {
    464 					/* no OACK, revert to defaults */
    465 					blksize = def_blksize;
    466 					rexmtval = def_rexmtval;
    467 				}
    468 				if (dp->th_block == block) {
    469 					break;		/* have next packet */
    470 				}
    471 				/* On an error, try to synchronize
    472 				 * both sides.
    473 				 */
    474 				j = synchnet(f, blksize);
    475 				if (j && trace) {
    476 					(void)printf("discarded %d packets\n", j);
    477 				}
    478 				if (dp->th_block == (block-1)) {
    479 					goto send_ack;	/* resend ack */
    480 				}
    481 			}
    482 			if (dp->th_opcode == OACK) {
    483 				if (block == 1) {
    484 					oack = 1;
    485 					blksize = def_blksize;
    486 					rexmtval = def_rexmtval;
    487 					get_options(dp, n);
    488 					ap->th_opcode = htons(ACK);
    489 					ap->th_block = 0;
    490 					readlen = blksize+4;
    491 					size = 4;
    492 					if (mcaddr != INADDR_NONE) {
    493 						mf = tftp_igmp_join();
    494 						if (mf < 0)
    495 							goto abort;
    496 						if (mcmasterslave == 0)
    497 							goto skip_ack;
    498 					}
    499 					goto send_ack;
    500 				}
    501 			}
    502 		}
    503 	/*	size = write(fd, dp->th_data, n - 4); */
    504 		size = writeit(file, &dp, n - 4, convert);
    505 		if (size < 0) {
    506 			nak(errno + 100, (struct sockaddr *)(void *)&peer);
    507 			break;
    508 		}
    509 		amount += size;
    510 	} while ((size_t)size == blksize);
    511 abort:						/* ok to ack, since user */
    512 	ap->th_opcode = htons((u_short)ACK);	/* has seen err msg */
    513 	ap->th_block = htons((u_short)block);
    514 	if (mcaddr != INADDR_NONE && mf >= 0) {
    515 		tftp_igmp_leave(mf);
    516 		mf = -1;
    517 	}
    518 	(void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)(void *)&peer,
    519 	    (socklen_t)peer.ss_len);
    520 	/*
    521 	 * flush last buffer
    522 	 * We do not check for failure because last buffer
    523 	 * can be empty, thus returning an error.
    524 	 * XXX maybe we should fix 'write_behind' instead.
    525 	 */
    526 	(void)write_behind(file, convert);
    527 	(void)fclose(file);
    528 	stopclock();
    529 	if (amount > 0)
    530 		printstats("Received", amount);
    531 }
    532 
    533 static int
    534 makerequest(request, name, tp, mode, filesize)
    535 	int request;
    536 	const char *name;
    537 	struct tftphdr *tp;
    538 	const char *mode;
    539 	off_t filesize;
    540 {
    541 	char *cp;
    542 
    543 	tp->th_opcode = htons((u_short)request);
    544 #ifndef __SVR4
    545 	cp = tp->th_stuff;
    546 #else
    547 	cp = (void *)&tp->th_stuff;
    548 #endif
    549 	(void)strcpy(cp, name);
    550 	cp += strlen(name);
    551 	*cp++ = '\0';
    552 	(void)strcpy(cp, mode);
    553 	cp += strlen(mode);
    554 	*cp++ = '\0';
    555 	if (tsize) {
    556 		(void)strcpy(cp, "tsize");
    557 		cp += strlen(cp);
    558 		*cp++ = '\0';
    559 		(void)sprintf(cp, "%lu", (unsigned long) filesize);
    560 		cp += strlen(cp);
    561 		*cp++ = '\0';
    562 	}
    563 	if (tout) {
    564 		(void)strcpy(cp, "timeout");
    565 		cp += strlen(cp);
    566 		*cp++ = '\0';
    567 		(void)sprintf(cp, "%d", rexmtval);
    568 		cp += strlen(cp);
    569 		*cp++ = '\0';
    570 	}
    571 	if (blksize != SEGSIZE) {
    572 		(void)strcpy(cp, "blksize");
    573 		cp += strlen(cp);
    574 		*cp++ = '\0';
    575 		(void)sprintf(cp, "%zd", blksize);
    576 		cp += strlen(cp);
    577 		*cp++ = '\0';
    578 	}
    579 	return (cp - (char *)(void *)tp);
    580 }
    581 
    582 const struct errmsg {
    583 	int	e_code;
    584 	const char *e_msg;
    585 } errmsgs[] = {
    586 	{ EUNDEF,	"Undefined error code" },
    587 	{ ENOTFOUND,	"File not found" },
    588 	{ EACCESS,	"Access violation" },
    589 	{ ENOSPACE,	"Disk full or allocation exceeded" },
    590 	{ EBADOP,	"Illegal TFTP operation" },
    591 	{ EBADID,	"Unknown transfer ID" },
    592 	{ EEXISTS,	"File already exists" },
    593 	{ ENOUSER,	"No such user" },
    594 	{ EOPTNEG,	"Option negotiation failed" },
    595 	{ -1,		0 }
    596 };
    597 
    598 /*
    599  * Send a nak packet (error message).
    600  * Error code passed in is one of the
    601  * standard TFTP codes, or a UNIX errno
    602  * offset by 100.
    603  */
    604 static void
    605 nak(error, peer)
    606 	int error;
    607 	struct sockaddr *peer;
    608 {
    609 	const struct errmsg *pe;
    610 	struct tftphdr *tp;
    611 	int length;
    612 	size_t msglen;
    613 
    614 	tp = (struct tftphdr *)(void *)ackbuf;
    615 	tp->th_opcode = htons((u_short)ERROR);
    616 	msglen = sizeof(ackbuf) - (&tp->th_msg[0] - ackbuf);
    617 	for (pe = errmsgs; pe->e_code >= 0; pe++)
    618 		if (pe->e_code == error)
    619 			break;
    620 	if (pe->e_code < 0) {
    621 		tp->th_code = EUNDEF;
    622 		(void)strlcpy(tp->th_msg, strerror(error - 100), msglen);
    623 	} else {
    624 		tp->th_code = htons((u_short)error);
    625 		(void)strlcpy(tp->th_msg, pe->e_msg, msglen);
    626 	}
    627 	length = strlen(tp->th_msg);
    628 	msglen = &tp->th_msg[length + 1] - ackbuf;
    629 	if (trace)
    630 		tpacket("sent", tp, (int)msglen);
    631 	if ((size_t)sendto(f, ackbuf, msglen, 0, peer, (socklen_t)peer->sa_len) != msglen)
    632 		warn("nak");
    633 }
    634 
    635 static void
    636 tpacket(s, tp, n)
    637 	const char *s;
    638 	struct tftphdr *tp;
    639 	int n;
    640 {
    641 	static const char *opcodes[] =
    642 	   { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
    643 	char *cp, *file, *endp, *opt = NULL;
    644 	const char *spc;
    645 	u_short op = ntohs(tp->th_opcode);
    646 	int i, o;
    647 
    648 	if (op < RRQ || op > OACK)
    649 		(void)printf("%s opcode=%x ", s, op);
    650 	else
    651 		(void)printf("%s %s ", s, opcodes[op]);
    652 	switch (op) {
    653 
    654 	case RRQ:
    655 	case WRQ:
    656 		n -= 2;
    657 #ifndef __SVR4
    658 		cp = tp->th_stuff;
    659 #else
    660 		cp = (void *) &tp->th_stuff;
    661 #endif
    662 		endp = cp + n - 1;
    663 		if (*endp != '\0') {	/* Shouldn't happen, but... */
    664 			*endp = '\0';
    665 		}
    666 		file = cp;
    667 		cp = strchr(cp, '\0') + 1;
    668 		(void)printf("<file=%s, mode=%s", file, cp);
    669 		cp = strchr(cp, '\0') + 1;
    670 		o = 0;
    671 		while (cp < endp) {
    672 			i = strlen(cp) + 1;
    673 			if (o) {
    674 				(void)printf(", %s=%s", opt, cp);
    675 			} else {
    676 				opt = cp;
    677 			}
    678 			o = (o+1) % 2;
    679 			cp += i;
    680 		}
    681 		(void)printf(">\n");
    682 		break;
    683 
    684 	case DATA:
    685 		(void)printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
    686 		break;
    687 
    688 	case ACK:
    689 		(void)printf("<block=%d>\n", ntohs(tp->th_block));
    690 		break;
    691 
    692 	case ERROR:
    693 		(void)printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
    694 		break;
    695 
    696 	case OACK:
    697 		o = 0;
    698 		n -= 2;
    699 		cp = tp->th_stuff;
    700 		endp = cp + n - 1;
    701 		if (*endp != '\0') {	/* Shouldn't happen, but... */
    702 			*endp = '\0';
    703 		}
    704 		(void)printf("<");
    705 		spc = "";
    706 		while (cp < endp) {
    707 			i = strlen(cp) + 1;
    708 			if (o) {
    709 				(void)printf("%s%s=%s", spc, opt, cp);
    710 				spc = ", ";
    711 			} else {
    712 				opt = cp;
    713 			}
    714 			o = (o+1) % 2;
    715 			cp += i;
    716 		}
    717 		(void)printf(">\n");
    718 		break;
    719 	}
    720 }
    721 
    722 struct timeval tstart;
    723 struct timeval tstop;
    724 
    725 static void
    726 startclock()
    727 {
    728 
    729 	(void)gettimeofday(&tstart, NULL);
    730 }
    731 
    732 static void
    733 stopclock()
    734 {
    735 
    736 	(void)gettimeofday(&tstop, NULL);
    737 }
    738 
    739 static void
    740 printstats(direction, amount)
    741 	const char *direction;
    742 	unsigned long amount;
    743 {
    744 	double delta;
    745 
    746 	/* compute delta in 1/10's second units */
    747 	delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
    748 		((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
    749 	delta = delta/10.;      /* back to seconds */
    750 	(void)printf("%s %ld bytes in %.1f seconds", direction, amount, delta);
    751 	if (verbose)
    752 		(void)printf(" [%.0f bits/sec]", (amount*8.)/delta);
    753 	(void)putchar('\n');
    754 }
    755 
    756 static void
    757 /*ARGSUSED*/
    758 timer(sig)
    759 	int sig;
    760 {
    761 
    762 	timeout += rexmtval;
    763 	if (timeout >= maxtimeout) {
    764 		(void)printf("Transfer timed out.\n");
    765 		longjmp(toplevel, -1);
    766 	}
    767 	longjmp(timeoutbuf, 1);
    768 }
    769 
    770 static int
    771 cmpport(sa, sb)
    772 	struct sockaddr *sa;
    773 	struct sockaddr *sb;
    774 {
    775 	char a[NI_MAXSERV], b[NI_MAXSERV];
    776 
    777 	if (getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0, a, sizeof(a), NI_NUMERICSERV))
    778 		return 0;
    779 	if (getnameinfo(sb, (socklen_t)sb->sa_len, NULL, 0, b, sizeof(b), NI_NUMERICSERV))
    780 		return 0;
    781 	if (strcmp(a, b) != 0)
    782 		return 0;
    783 
    784 	return 1;
    785 }
    786