xref: /freebsd/usr.bin/tftp/tftp.c (revision 9b50d9027575220cb6dd09b3e62f03f511e908b8)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)tftp.c	8.1 (Berkeley) 6/6/93";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes /*
419b50d902SRodney W. Grimes  * TFTP User Program -- Protocol Machines
429b50d902SRodney W. Grimes  */
439b50d902SRodney W. Grimes #include <sys/types.h>
449b50d902SRodney W. Grimes #include <sys/socket.h>
459b50d902SRodney W. Grimes #include <sys/time.h>
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <netinet/in.h>
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #include <arpa/tftp.h>
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include <errno.h>
529b50d902SRodney W. Grimes #include <setjmp.h>
539b50d902SRodney W. Grimes #include <signal.h>
549b50d902SRodney W. Grimes #include <stdio.h>
559b50d902SRodney W. Grimes #include <unistd.h>
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes #include "extern.h"
589b50d902SRodney W. Grimes #include "tftpsubs.h"
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes extern	int errno;
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes extern  struct sockaddr_in peeraddr;	/* filled in by main */
639b50d902SRodney W. Grimes extern  int     f;			/* the opened socket */
649b50d902SRodney W. Grimes extern  int     trace;
659b50d902SRodney W. Grimes extern  int     verbose;
669b50d902SRodney W. Grimes extern  int     rexmtval;
679b50d902SRodney W. Grimes extern  int     maxtimeout;
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes #define PKTSIZE    SEGSIZE+4
709b50d902SRodney W. Grimes char    ackbuf[PKTSIZE];
719b50d902SRodney W. Grimes int	timeout;
729b50d902SRodney W. Grimes jmp_buf	toplevel;
739b50d902SRodney W. Grimes jmp_buf	timeoutbuf;
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes static void nak __P((int));
769b50d902SRodney W. Grimes static int makerequest __P((int, const char *, struct tftphdr *, const char *));
779b50d902SRodney W. Grimes static void printstats __P((const char *, unsigned long));
789b50d902SRodney W. Grimes static void startclock __P((void));
799b50d902SRodney W. Grimes static void stopclock __P((void));
809b50d902SRodney W. Grimes static void timer __P((int));
819b50d902SRodney W. Grimes static void tpacket __P((const char *, struct tftphdr *, int));
829b50d902SRodney W. Grimes 
839b50d902SRodney W. Grimes /*
849b50d902SRodney W. Grimes  * Send the requested file.
859b50d902SRodney W. Grimes  */
869b50d902SRodney W. Grimes void
879b50d902SRodney W. Grimes sendfile(fd, name, mode)
889b50d902SRodney W. Grimes 	int fd;
899b50d902SRodney W. Grimes 	char *name;
909b50d902SRodney W. Grimes 	char *mode;
919b50d902SRodney W. Grimes {
929b50d902SRodney W. Grimes 	register struct tftphdr *ap;	   /* data and ack packets */
939b50d902SRodney W. Grimes 	struct tftphdr *r_init(), *dp;
949b50d902SRodney W. Grimes 	register int n;
959b50d902SRodney W. Grimes 	volatile int block, size, convert;
969b50d902SRodney W. Grimes 	volatile unsigned long amount;
979b50d902SRodney W. Grimes 	struct sockaddr_in from;
989b50d902SRodney W. Grimes 	int fromlen;
999b50d902SRodney W. Grimes 	FILE *file;
1009b50d902SRodney W. Grimes 
1019b50d902SRodney W. Grimes 	startclock();		/* start stat's clock */
1029b50d902SRodney W. Grimes 	dp = r_init();		/* reset fillbuf/read-ahead code */
1039b50d902SRodney W. Grimes 	ap = (struct tftphdr *)ackbuf;
1049b50d902SRodney W. Grimes 	file = fdopen(fd, "r");
1059b50d902SRodney W. Grimes 	convert = !strcmp(mode, "netascii");
1069b50d902SRodney W. Grimes 	block = 0;
1079b50d902SRodney W. Grimes 	amount = 0;
1089b50d902SRodney W. Grimes 
1099b50d902SRodney W. Grimes 	signal(SIGALRM, timer);
1109b50d902SRodney W. Grimes 	do {
1119b50d902SRodney W. Grimes 		if (block == 0)
1129b50d902SRodney W. Grimes 			size = makerequest(WRQ, name, dp, mode) - 4;
1139b50d902SRodney W. Grimes 		else {
1149b50d902SRodney W. Grimes 		/*	size = read(fd, dp->th_data, SEGSIZE);	 */
1159b50d902SRodney W. Grimes 			size = readit(file, &dp, convert);
1169b50d902SRodney W. Grimes 			if (size < 0) {
1179b50d902SRodney W. Grimes 				nak(errno + 100);
1189b50d902SRodney W. Grimes 				break;
1199b50d902SRodney W. Grimes 			}
1209b50d902SRodney W. Grimes 			dp->th_opcode = htons((u_short)DATA);
1219b50d902SRodney W. Grimes 			dp->th_block = htons((u_short)block);
1229b50d902SRodney W. Grimes 		}
1239b50d902SRodney W. Grimes 		timeout = 0;
1249b50d902SRodney W. Grimes 		(void) setjmp(timeoutbuf);
1259b50d902SRodney W. Grimes send_data:
1269b50d902SRodney W. Grimes 		if (trace)
1279b50d902SRodney W. Grimes 			tpacket("sent", dp, size + 4);
1289b50d902SRodney W. Grimes 		n = sendto(f, dp, size + 4, 0,
1299b50d902SRodney W. Grimes 		    (struct sockaddr *)&peeraddr, sizeof(peeraddr));
1309b50d902SRodney W. Grimes 		if (n != size + 4) {
1319b50d902SRodney W. Grimes 			perror("tftp: sendto");
1329b50d902SRodney W. Grimes 			goto abort;
1339b50d902SRodney W. Grimes 		}
1349b50d902SRodney W. Grimes 		read_ahead(file, convert);
1359b50d902SRodney W. Grimes 		for ( ; ; ) {
1369b50d902SRodney W. Grimes 			alarm(rexmtval);
1379b50d902SRodney W. Grimes 			do {
1389b50d902SRodney W. Grimes 				fromlen = sizeof(from);
1399b50d902SRodney W. Grimes 				n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
1409b50d902SRodney W. Grimes 				    (struct sockaddr *)&from, &fromlen);
1419b50d902SRodney W. Grimes 			} while (n <= 0);
1429b50d902SRodney W. Grimes 			alarm(0);
1439b50d902SRodney W. Grimes 			if (n < 0) {
1449b50d902SRodney W. Grimes 				perror("tftp: recvfrom");
1459b50d902SRodney W. Grimes 				goto abort;
1469b50d902SRodney W. Grimes 			}
1479b50d902SRodney W. Grimes 			peeraddr.sin_port = from.sin_port;	/* added */
1489b50d902SRodney W. Grimes 			if (trace)
1499b50d902SRodney W. Grimes 				tpacket("received", ap, n);
1509b50d902SRodney W. Grimes 			/* should verify packet came from server */
1519b50d902SRodney W. Grimes 			ap->th_opcode = ntohs(ap->th_opcode);
1529b50d902SRodney W. Grimes 			ap->th_block = ntohs(ap->th_block);
1539b50d902SRodney W. Grimes 			if (ap->th_opcode == ERROR) {
1549b50d902SRodney W. Grimes 				printf("Error code %d: %s\n", ap->th_code,
1559b50d902SRodney W. Grimes 					ap->th_msg);
1569b50d902SRodney W. Grimes 				goto abort;
1579b50d902SRodney W. Grimes 			}
1589b50d902SRodney W. Grimes 			if (ap->th_opcode == ACK) {
1599b50d902SRodney W. Grimes 				int j;
1609b50d902SRodney W. Grimes 
1619b50d902SRodney W. Grimes 				if (ap->th_block == block) {
1629b50d902SRodney W. Grimes 					break;
1639b50d902SRodney W. Grimes 				}
1649b50d902SRodney W. Grimes 				/* On an error, try to synchronize
1659b50d902SRodney W. Grimes 				 * both sides.
1669b50d902SRodney W. Grimes 				 */
1679b50d902SRodney W. Grimes 				j = synchnet(f);
1689b50d902SRodney W. Grimes 				if (j && trace) {
1699b50d902SRodney W. Grimes 					printf("discarded %d packets\n",
1709b50d902SRodney W. Grimes 							j);
1719b50d902SRodney W. Grimes 				}
1729b50d902SRodney W. Grimes 				if (ap->th_block == (block-1)) {
1739b50d902SRodney W. Grimes 					goto send_data;
1749b50d902SRodney W. Grimes 				}
1759b50d902SRodney W. Grimes 			}
1769b50d902SRodney W. Grimes 		}
1779b50d902SRodney W. Grimes 		if (block > 0)
1789b50d902SRodney W. Grimes 			amount += size;
1799b50d902SRodney W. Grimes 		block++;
1809b50d902SRodney W. Grimes 	} while (size == SEGSIZE || block == 1);
1819b50d902SRodney W. Grimes abort:
1829b50d902SRodney W. Grimes 	fclose(file);
1839b50d902SRodney W. Grimes 	stopclock();
1849b50d902SRodney W. Grimes 	if (amount > 0)
1859b50d902SRodney W. Grimes 		printstats("Sent", amount);
1869b50d902SRodney W. Grimes }
1879b50d902SRodney W. Grimes 
1889b50d902SRodney W. Grimes /*
1899b50d902SRodney W. Grimes  * Receive a file.
1909b50d902SRodney W. Grimes  */
1919b50d902SRodney W. Grimes void
1929b50d902SRodney W. Grimes recvfile(fd, name, mode)
1939b50d902SRodney W. Grimes 	int fd;
1949b50d902SRodney W. Grimes 	char *name;
1959b50d902SRodney W. Grimes 	char *mode;
1969b50d902SRodney W. Grimes {
1979b50d902SRodney W. Grimes 	register struct tftphdr *ap;
1989b50d902SRodney W. Grimes 	struct tftphdr *dp, *w_init();
1999b50d902SRodney W. Grimes 	register int n;
2009b50d902SRodney W. Grimes 	volatile int block, size, firsttrip;
2019b50d902SRodney W. Grimes 	volatile unsigned long amount;
2029b50d902SRodney W. Grimes 	struct sockaddr_in from;
2039b50d902SRodney W. Grimes 	int fromlen;
2049b50d902SRodney W. Grimes 	FILE *file;
2059b50d902SRodney W. Grimes 	volatile int convert;		/* true if converting crlf -> lf */
2069b50d902SRodney W. Grimes 
2079b50d902SRodney W. Grimes 	startclock();
2089b50d902SRodney W. Grimes 	dp = w_init();
2099b50d902SRodney W. Grimes 	ap = (struct tftphdr *)ackbuf;
2109b50d902SRodney W. Grimes 	file = fdopen(fd, "w");
2119b50d902SRodney W. Grimes 	convert = !strcmp(mode, "netascii");
2129b50d902SRodney W. Grimes 	block = 1;
2139b50d902SRodney W. Grimes 	firsttrip = 1;
2149b50d902SRodney W. Grimes 	amount = 0;
2159b50d902SRodney W. Grimes 
2169b50d902SRodney W. Grimes 	signal(SIGALRM, timer);
2179b50d902SRodney W. Grimes 	do {
2189b50d902SRodney W. Grimes 		if (firsttrip) {
2199b50d902SRodney W. Grimes 			size = makerequest(RRQ, name, ap, mode);
2209b50d902SRodney W. Grimes 			firsttrip = 0;
2219b50d902SRodney W. Grimes 		} else {
2229b50d902SRodney W. Grimes 			ap->th_opcode = htons((u_short)ACK);
2239b50d902SRodney W. Grimes 			ap->th_block = htons((u_short)(block));
2249b50d902SRodney W. Grimes 			size = 4;
2259b50d902SRodney W. Grimes 			block++;
2269b50d902SRodney W. Grimes 		}
2279b50d902SRodney W. Grimes 		timeout = 0;
2289b50d902SRodney W. Grimes 		(void) setjmp(timeoutbuf);
2299b50d902SRodney W. Grimes send_ack:
2309b50d902SRodney W. Grimes 		if (trace)
2319b50d902SRodney W. Grimes 			tpacket("sent", ap, size);
2329b50d902SRodney W. Grimes 		if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr,
2339b50d902SRodney W. Grimes 		    sizeof(peeraddr)) != size) {
2349b50d902SRodney W. Grimes 			alarm(0);
2359b50d902SRodney W. Grimes 			perror("tftp: sendto");
2369b50d902SRodney W. Grimes 			goto abort;
2379b50d902SRodney W. Grimes 		}
2389b50d902SRodney W. Grimes 		write_behind(file, convert);
2399b50d902SRodney W. Grimes 		for ( ; ; ) {
2409b50d902SRodney W. Grimes 			alarm(rexmtval);
2419b50d902SRodney W. Grimes 			do  {
2429b50d902SRodney W. Grimes 				fromlen = sizeof(from);
2439b50d902SRodney W. Grimes 				n = recvfrom(f, dp, PKTSIZE, 0,
2449b50d902SRodney W. Grimes 				    (struct sockaddr *)&from, &fromlen);
2459b50d902SRodney W. Grimes 			} while (n <= 0);
2469b50d902SRodney W. Grimes 			alarm(0);
2479b50d902SRodney W. Grimes 			if (n < 0) {
2489b50d902SRodney W. Grimes 				perror("tftp: recvfrom");
2499b50d902SRodney W. Grimes 				goto abort;
2509b50d902SRodney W. Grimes 			}
2519b50d902SRodney W. Grimes 			peeraddr.sin_port = from.sin_port;	/* added */
2529b50d902SRodney W. Grimes 			if (trace)
2539b50d902SRodney W. Grimes 				tpacket("received", dp, n);
2549b50d902SRodney W. Grimes 			/* should verify client address */
2559b50d902SRodney W. Grimes 			dp->th_opcode = ntohs(dp->th_opcode);
2569b50d902SRodney W. Grimes 			dp->th_block = ntohs(dp->th_block);
2579b50d902SRodney W. Grimes 			if (dp->th_opcode == ERROR) {
2589b50d902SRodney W. Grimes 				printf("Error code %d: %s\n", dp->th_code,
2599b50d902SRodney W. Grimes 					dp->th_msg);
2609b50d902SRodney W. Grimes 				goto abort;
2619b50d902SRodney W. Grimes 			}
2629b50d902SRodney W. Grimes 			if (dp->th_opcode == DATA) {
2639b50d902SRodney W. Grimes 				int j;
2649b50d902SRodney W. Grimes 
2659b50d902SRodney W. Grimes 				if (dp->th_block == block) {
2669b50d902SRodney W. Grimes 					break;		/* have next packet */
2679b50d902SRodney W. Grimes 				}
2689b50d902SRodney W. Grimes 				/* On an error, try to synchronize
2699b50d902SRodney W. Grimes 				 * both sides.
2709b50d902SRodney W. Grimes 				 */
2719b50d902SRodney W. Grimes 				j = synchnet(f);
2729b50d902SRodney W. Grimes 				if (j && trace) {
2739b50d902SRodney W. Grimes 					printf("discarded %d packets\n", j);
2749b50d902SRodney W. Grimes 				}
2759b50d902SRodney W. Grimes 				if (dp->th_block == (block-1)) {
2769b50d902SRodney W. Grimes 					goto send_ack;	/* resend ack */
2779b50d902SRodney W. Grimes 				}
2789b50d902SRodney W. Grimes 			}
2799b50d902SRodney W. Grimes 		}
2809b50d902SRodney W. Grimes 	/*	size = write(fd, dp->th_data, n - 4); */
2819b50d902SRodney W. Grimes 		size = writeit(file, &dp, n - 4, convert);
2829b50d902SRodney W. Grimes 		if (size < 0) {
2839b50d902SRodney W. Grimes 			nak(errno + 100);
2849b50d902SRodney W. Grimes 			break;
2859b50d902SRodney W. Grimes 		}
2869b50d902SRodney W. Grimes 		amount += size;
2879b50d902SRodney W. Grimes 	} while (size == SEGSIZE);
2889b50d902SRodney W. Grimes abort:						/* ok to ack, since user */
2899b50d902SRodney W. Grimes 	ap->th_opcode = htons((u_short)ACK);	/* has seen err msg */
2909b50d902SRodney W. Grimes 	ap->th_block = htons((u_short)block);
2919b50d902SRodney W. Grimes 	(void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
2929b50d902SRodney W. Grimes 	    sizeof(peeraddr));
2939b50d902SRodney W. Grimes 	write_behind(file, convert);		/* flush last buffer */
2949b50d902SRodney W. Grimes 	fclose(file);
2959b50d902SRodney W. Grimes 	stopclock();
2969b50d902SRodney W. Grimes 	if (amount > 0)
2979b50d902SRodney W. Grimes 		printstats("Received", amount);
2989b50d902SRodney W. Grimes }
2999b50d902SRodney W. Grimes 
3009b50d902SRodney W. Grimes static int
3019b50d902SRodney W. Grimes makerequest(request, name, tp, mode)
3029b50d902SRodney W. Grimes 	int request;
3039b50d902SRodney W. Grimes 	const char *name;
3049b50d902SRodney W. Grimes 	struct tftphdr *tp;
3059b50d902SRodney W. Grimes 	const char *mode;
3069b50d902SRodney W. Grimes {
3079b50d902SRodney W. Grimes 	register char *cp;
3089b50d902SRodney W. Grimes 
3099b50d902SRodney W. Grimes 	tp->th_opcode = htons((u_short)request);
3109b50d902SRodney W. Grimes 	cp = tp->th_stuff;
3119b50d902SRodney W. Grimes 	strcpy(cp, name);
3129b50d902SRodney W. Grimes 	cp += strlen(name);
3139b50d902SRodney W. Grimes 	*cp++ = '\0';
3149b50d902SRodney W. Grimes 	strcpy(cp, mode);
3159b50d902SRodney W. Grimes 	cp += strlen(mode);
3169b50d902SRodney W. Grimes 	*cp++ = '\0';
3179b50d902SRodney W. Grimes 	return (cp - (char *)tp);
3189b50d902SRodney W. Grimes }
3199b50d902SRodney W. Grimes 
3209b50d902SRodney W. Grimes struct errmsg {
3219b50d902SRodney W. Grimes 	int	e_code;
3229b50d902SRodney W. Grimes 	char	*e_msg;
3239b50d902SRodney W. Grimes } errmsgs[] = {
3249b50d902SRodney W. Grimes 	{ EUNDEF,	"Undefined error code" },
3259b50d902SRodney W. Grimes 	{ ENOTFOUND,	"File not found" },
3269b50d902SRodney W. Grimes 	{ EACCESS,	"Access violation" },
3279b50d902SRodney W. Grimes 	{ ENOSPACE,	"Disk full or allocation exceeded" },
3289b50d902SRodney W. Grimes 	{ EBADOP,	"Illegal TFTP operation" },
3299b50d902SRodney W. Grimes 	{ EBADID,	"Unknown transfer ID" },
3309b50d902SRodney W. Grimes 	{ EEXISTS,	"File already exists" },
3319b50d902SRodney W. Grimes 	{ ENOUSER,	"No such user" },
3329b50d902SRodney W. Grimes 	{ -1,		0 }
3339b50d902SRodney W. Grimes };
3349b50d902SRodney W. Grimes 
3359b50d902SRodney W. Grimes /*
3369b50d902SRodney W. Grimes  * Send a nak packet (error message).
3379b50d902SRodney W. Grimes  * Error code passed in is one of the
3389b50d902SRodney W. Grimes  * standard TFTP codes, or a UNIX errno
3399b50d902SRodney W. Grimes  * offset by 100.
3409b50d902SRodney W. Grimes  */
3419b50d902SRodney W. Grimes static void
3429b50d902SRodney W. Grimes nak(error)
3439b50d902SRodney W. Grimes 	int error;
3449b50d902SRodney W. Grimes {
3459b50d902SRodney W. Grimes 	register struct errmsg *pe;
3469b50d902SRodney W. Grimes 	register struct tftphdr *tp;
3479b50d902SRodney W. Grimes 	int length;
3489b50d902SRodney W. Grimes 	char *strerror();
3499b50d902SRodney W. Grimes 
3509b50d902SRodney W. Grimes 	tp = (struct tftphdr *)ackbuf;
3519b50d902SRodney W. Grimes 	tp->th_opcode = htons((u_short)ERROR);
3529b50d902SRodney W. Grimes 	tp->th_code = htons((u_short)error);
3539b50d902SRodney W. Grimes 	for (pe = errmsgs; pe->e_code >= 0; pe++)
3549b50d902SRodney W. Grimes 		if (pe->e_code == error)
3559b50d902SRodney W. Grimes 			break;
3569b50d902SRodney W. Grimes 	if (pe->e_code < 0) {
3579b50d902SRodney W. Grimes 		pe->e_msg = strerror(error - 100);
3589b50d902SRodney W. Grimes 		tp->th_code = EUNDEF;
3599b50d902SRodney W. Grimes 	}
3609b50d902SRodney W. Grimes 	strcpy(tp->th_msg, pe->e_msg);
3619b50d902SRodney W. Grimes 	length = strlen(pe->e_msg) + 4;
3629b50d902SRodney W. Grimes 	if (trace)
3639b50d902SRodney W. Grimes 		tpacket("sent", tp, length);
3649b50d902SRodney W. Grimes 	if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&peeraddr,
3659b50d902SRodney W. Grimes 	    sizeof(peeraddr)) != length)
3669b50d902SRodney W. Grimes 		perror("nak");
3679b50d902SRodney W. Grimes }
3689b50d902SRodney W. Grimes 
3699b50d902SRodney W. Grimes static void
3709b50d902SRodney W. Grimes tpacket(s, tp, n)
3719b50d902SRodney W. Grimes 	const char *s;
3729b50d902SRodney W. Grimes 	struct tftphdr *tp;
3739b50d902SRodney W. Grimes 	int n;
3749b50d902SRodney W. Grimes {
3759b50d902SRodney W. Grimes 	static char *opcodes[] =
3769b50d902SRodney W. Grimes 	   { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
3779b50d902SRodney W. Grimes 	register char *cp, *file;
3789b50d902SRodney W. Grimes 	u_short op = ntohs(tp->th_opcode);
3799b50d902SRodney W. Grimes 	char *index();
3809b50d902SRodney W. Grimes 
3819b50d902SRodney W. Grimes 	if (op < RRQ || op > ERROR)
3829b50d902SRodney W. Grimes 		printf("%s opcode=%x ", s, op);
3839b50d902SRodney W. Grimes 	else
3849b50d902SRodney W. Grimes 		printf("%s %s ", s, opcodes[op]);
3859b50d902SRodney W. Grimes 	switch (op) {
3869b50d902SRodney W. Grimes 
3879b50d902SRodney W. Grimes 	case RRQ:
3889b50d902SRodney W. Grimes 	case WRQ:
3899b50d902SRodney W. Grimes 		n -= 2;
3909b50d902SRodney W. Grimes 		file = cp = tp->th_stuff;
3919b50d902SRodney W. Grimes 		cp = index(cp, '\0');
3929b50d902SRodney W. Grimes 		printf("<file=%s, mode=%s>\n", file, cp + 1);
3939b50d902SRodney W. Grimes 		break;
3949b50d902SRodney W. Grimes 
3959b50d902SRodney W. Grimes 	case DATA:
3969b50d902SRodney W. Grimes 		printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
3979b50d902SRodney W. Grimes 		break;
3989b50d902SRodney W. Grimes 
3999b50d902SRodney W. Grimes 	case ACK:
4009b50d902SRodney W. Grimes 		printf("<block=%d>\n", ntohs(tp->th_block));
4019b50d902SRodney W. Grimes 		break;
4029b50d902SRodney W. Grimes 
4039b50d902SRodney W. Grimes 	case ERROR:
4049b50d902SRodney W. Grimes 		printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
4059b50d902SRodney W. Grimes 		break;
4069b50d902SRodney W. Grimes 	}
4079b50d902SRodney W. Grimes }
4089b50d902SRodney W. Grimes 
4099b50d902SRodney W. Grimes struct timeval tstart;
4109b50d902SRodney W. Grimes struct timeval tstop;
4119b50d902SRodney W. Grimes 
4129b50d902SRodney W. Grimes static void
4139b50d902SRodney W. Grimes startclock()
4149b50d902SRodney W. Grimes {
4159b50d902SRodney W. Grimes 
4169b50d902SRodney W. Grimes 	(void)gettimeofday(&tstart, NULL);
4179b50d902SRodney W. Grimes }
4189b50d902SRodney W. Grimes 
4199b50d902SRodney W. Grimes static void
4209b50d902SRodney W. Grimes stopclock()
4219b50d902SRodney W. Grimes {
4229b50d902SRodney W. Grimes 
4239b50d902SRodney W. Grimes 	(void)gettimeofday(&tstop, NULL);
4249b50d902SRodney W. Grimes }
4259b50d902SRodney W. Grimes 
4269b50d902SRodney W. Grimes static void
4279b50d902SRodney W. Grimes printstats(direction, amount)
4289b50d902SRodney W. Grimes 	const char *direction;
4299b50d902SRodney W. Grimes 	unsigned long amount;
4309b50d902SRodney W. Grimes {
4319b50d902SRodney W. Grimes 	double delta;
4329b50d902SRodney W. Grimes 			/* compute delta in 1/10's second units */
4339b50d902SRodney W. Grimes 	delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
4349b50d902SRodney W. Grimes 		((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
4359b50d902SRodney W. Grimes 	delta = delta/10.;      /* back to seconds */
4369b50d902SRodney W. Grimes 	printf("%s %d bytes in %.1f seconds", direction, amount, delta);
4379b50d902SRodney W. Grimes 	if (verbose)
4389b50d902SRodney W. Grimes 		printf(" [%.0f bits/sec]", (amount*8.)/delta);
4399b50d902SRodney W. Grimes 	putchar('\n');
4409b50d902SRodney W. Grimes }
4419b50d902SRodney W. Grimes 
4429b50d902SRodney W. Grimes static void
4439b50d902SRodney W. Grimes timer(sig)
4449b50d902SRodney W. Grimes 	int sig;
4459b50d902SRodney W. Grimes {
4469b50d902SRodney W. Grimes 
4479b50d902SRodney W. Grimes 	timeout += rexmtval;
4489b50d902SRodney W. Grimes 	if (timeout >= maxtimeout) {
4499b50d902SRodney W. Grimes 		printf("Transfer timed out.\n");
4509b50d902SRodney W. Grimes 		longjmp(toplevel, -1);
4519b50d902SRodney W. Grimes 	}
4529b50d902SRodney W. Grimes 	longjmp(timeoutbuf, 1);
4539b50d902SRodney W. Grimes }
454