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