xref: /freebsd/usr.bin/tftp/tftp.c (revision fd129a0245ccdbed26aee4a48cd7d4b2fa74a99d)
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
35fd129a02SPhilippe Charnier #if 0
369b50d902SRodney W. Grimes static char sccsid[] = "@(#)tftp.c	8.1 (Berkeley) 6/6/93";
37fd129a02SPhilippe Charnier #endif
38fd129a02SPhilippe Charnier static const char rcsid[] =
39fd129a02SPhilippe Charnier 	"$Id$";
409b50d902SRodney W. Grimes #endif /* not lint */
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes /*
459b50d902SRodney W. Grimes  * TFTP User Program -- Protocol Machines
469b50d902SRodney W. Grimes  */
479b50d902SRodney W. Grimes #include <sys/types.h>
489b50d902SRodney W. Grimes #include <sys/socket.h>
499b50d902SRodney W. Grimes #include <sys/time.h>
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include <netinet/in.h>
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #include <arpa/tftp.h>
549b50d902SRodney W. Grimes 
55fd129a02SPhilippe Charnier #include <err.h>
569b50d902SRodney W. Grimes #include <errno.h>
579b50d902SRodney W. Grimes #include <setjmp.h>
589b50d902SRodney W. Grimes #include <signal.h>
599b50d902SRodney W. Grimes #include <stdio.h>
609b50d902SRodney W. Grimes #include <unistd.h>
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes #include "extern.h"
639b50d902SRodney W. Grimes #include "tftpsubs.h"
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes extern  struct sockaddr_in peeraddr;	/* filled in by main */
669b50d902SRodney W. Grimes extern  int     f;			/* the opened socket */
679b50d902SRodney W. Grimes extern  int     trace;
689b50d902SRodney W. Grimes extern  int     verbose;
699b50d902SRodney W. Grimes extern  int     rexmtval;
709b50d902SRodney W. Grimes extern  int     maxtimeout;
719b50d902SRodney W. Grimes 
729b50d902SRodney W. Grimes #define PKTSIZE    SEGSIZE+4
739b50d902SRodney W. Grimes char    ackbuf[PKTSIZE];
749b50d902SRodney W. Grimes int	timeout;
759b50d902SRodney W. Grimes jmp_buf	toplevel;
769b50d902SRodney W. Grimes jmp_buf	timeoutbuf;
779b50d902SRodney W. Grimes 
789b50d902SRodney W. Grimes static void nak __P((int));
799b50d902SRodney W. Grimes static int makerequest __P((int, const char *, struct tftphdr *, const char *));
809b50d902SRodney W. Grimes static void printstats __P((const char *, unsigned long));
819b50d902SRodney W. Grimes static void startclock __P((void));
829b50d902SRodney W. Grimes static void stopclock __P((void));
839b50d902SRodney W. Grimes static void timer __P((int));
849b50d902SRodney W. Grimes static void tpacket __P((const char *, struct tftphdr *, int));
859b50d902SRodney W. Grimes 
869b50d902SRodney W. Grimes /*
879b50d902SRodney W. Grimes  * Send the requested file.
889b50d902SRodney W. Grimes  */
899b50d902SRodney W. Grimes void
909b50d902SRodney W. Grimes sendfile(fd, name, mode)
919b50d902SRodney W. Grimes 	int fd;
929b50d902SRodney W. Grimes 	char *name;
939b50d902SRodney W. Grimes 	char *mode;
949b50d902SRodney W. Grimes {
959b50d902SRodney W. Grimes 	register struct tftphdr *ap;	   /* data and ack packets */
969b50d902SRodney W. Grimes 	struct tftphdr *r_init(), *dp;
979b50d902SRodney W. Grimes 	register int n;
989b50d902SRodney W. Grimes 	volatile int block, size, convert;
999b50d902SRodney W. Grimes 	volatile unsigned long amount;
1009b50d902SRodney W. Grimes 	struct sockaddr_in from;
1019b50d902SRodney W. Grimes 	int fromlen;
1029b50d902SRodney W. Grimes 	FILE *file;
1039b50d902SRodney W. Grimes 
1049b50d902SRodney W. Grimes 	startclock();		/* start stat's clock */
1059b50d902SRodney W. Grimes 	dp = r_init();		/* reset fillbuf/read-ahead code */
1069b50d902SRodney W. Grimes 	ap = (struct tftphdr *)ackbuf;
1079b50d902SRodney W. Grimes 	file = fdopen(fd, "r");
1089b50d902SRodney W. Grimes 	convert = !strcmp(mode, "netascii");
1099b50d902SRodney W. Grimes 	block = 0;
1109b50d902SRodney W. Grimes 	amount = 0;
1119b50d902SRodney W. Grimes 
1129b50d902SRodney W. Grimes 	signal(SIGALRM, timer);
1139b50d902SRodney W. Grimes 	do {
1149b50d902SRodney W. Grimes 		if (block == 0)
1159b50d902SRodney W. Grimes 			size = makerequest(WRQ, name, dp, mode) - 4;
1169b50d902SRodney W. Grimes 		else {
1179b50d902SRodney W. Grimes 		/*	size = read(fd, dp->th_data, SEGSIZE);	 */
1189b50d902SRodney W. Grimes 			size = readit(file, &dp, convert);
1199b50d902SRodney W. Grimes 			if (size < 0) {
1209b50d902SRodney W. Grimes 				nak(errno + 100);
1219b50d902SRodney W. Grimes 				break;
1229b50d902SRodney W. Grimes 			}
1239b50d902SRodney W. Grimes 			dp->th_opcode = htons((u_short)DATA);
1249b50d902SRodney W. Grimes 			dp->th_block = htons((u_short)block);
1259b50d902SRodney W. Grimes 		}
1269b50d902SRodney W. Grimes 		timeout = 0;
1279b50d902SRodney W. Grimes 		(void) setjmp(timeoutbuf);
1289b50d902SRodney W. Grimes send_data:
1299b50d902SRodney W. Grimes 		if (trace)
1309b50d902SRodney W. Grimes 			tpacket("sent", dp, size + 4);
1319b50d902SRodney W. Grimes 		n = sendto(f, dp, size + 4, 0,
1329b50d902SRodney W. Grimes 		    (struct sockaddr *)&peeraddr, sizeof(peeraddr));
1339b50d902SRodney W. Grimes 		if (n != size + 4) {
134fd129a02SPhilippe Charnier 			warn("sendto");
1359b50d902SRodney W. Grimes 			goto abort;
1369b50d902SRodney W. Grimes 		}
1379b50d902SRodney W. Grimes 		read_ahead(file, convert);
1389b50d902SRodney W. Grimes 		for ( ; ; ) {
1399b50d902SRodney W. Grimes 			alarm(rexmtval);
1409b50d902SRodney W. Grimes 			do {
1419b50d902SRodney W. Grimes 				fromlen = sizeof(from);
1429b50d902SRodney W. Grimes 				n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
1439b50d902SRodney W. Grimes 				    (struct sockaddr *)&from, &fromlen);
1449b50d902SRodney W. Grimes 			} while (n <= 0);
1459b50d902SRodney W. Grimes 			alarm(0);
1469b50d902SRodney W. Grimes 			if (n < 0) {
147fd129a02SPhilippe Charnier 				warn("recvfrom");
1489b50d902SRodney W. Grimes 				goto abort;
1499b50d902SRodney W. Grimes 			}
1509b50d902SRodney W. Grimes 			peeraddr.sin_port = from.sin_port;	/* added */
1519b50d902SRodney W. Grimes 			if (trace)
1529b50d902SRodney W. Grimes 				tpacket("received", ap, n);
1539b50d902SRodney W. Grimes 			/* should verify packet came from server */
1549b50d902SRodney W. Grimes 			ap->th_opcode = ntohs(ap->th_opcode);
1559b50d902SRodney W. Grimes 			ap->th_block = ntohs(ap->th_block);
1569b50d902SRodney W. Grimes 			if (ap->th_opcode == ERROR) {
1579b50d902SRodney W. Grimes 				printf("Error code %d: %s\n", ap->th_code,
1589b50d902SRodney W. Grimes 					ap->th_msg);
1599b50d902SRodney W. Grimes 				goto abort;
1609b50d902SRodney W. Grimes 			}
1619b50d902SRodney W. Grimes 			if (ap->th_opcode == ACK) {
1629b50d902SRodney W. Grimes 				int j;
1639b50d902SRodney W. Grimes 
1649b50d902SRodney W. Grimes 				if (ap->th_block == block) {
1659b50d902SRodney W. Grimes 					break;
1669b50d902SRodney W. Grimes 				}
1679b50d902SRodney W. Grimes 				/* On an error, try to synchronize
1689b50d902SRodney W. Grimes 				 * both sides.
1699b50d902SRodney W. Grimes 				 */
1709b50d902SRodney W. Grimes 				j = synchnet(f);
1719b50d902SRodney W. Grimes 				if (j && trace) {
1729b50d902SRodney W. Grimes 					printf("discarded %d packets\n",
1739b50d902SRodney W. Grimes 							j);
1749b50d902SRodney W. Grimes 				}
1759b50d902SRodney W. Grimes 				if (ap->th_block == (block-1)) {
1769b50d902SRodney W. Grimes 					goto send_data;
1779b50d902SRodney W. Grimes 				}
1789b50d902SRodney W. Grimes 			}
1799b50d902SRodney W. Grimes 		}
1809b50d902SRodney W. Grimes 		if (block > 0)
1819b50d902SRodney W. Grimes 			amount += size;
1829b50d902SRodney W. Grimes 		block++;
1839b50d902SRodney W. Grimes 	} while (size == SEGSIZE || block == 1);
1849b50d902SRodney W. Grimes abort:
1859b50d902SRodney W. Grimes 	fclose(file);
1869b50d902SRodney W. Grimes 	stopclock();
1879b50d902SRodney W. Grimes 	if (amount > 0)
1889b50d902SRodney W. Grimes 		printstats("Sent", amount);
1899b50d902SRodney W. Grimes }
1909b50d902SRodney W. Grimes 
1919b50d902SRodney W. Grimes /*
1929b50d902SRodney W. Grimes  * Receive a file.
1939b50d902SRodney W. Grimes  */
1949b50d902SRodney W. Grimes void
1959b50d902SRodney W. Grimes recvfile(fd, name, mode)
1969b50d902SRodney W. Grimes 	int fd;
1979b50d902SRodney W. Grimes 	char *name;
1989b50d902SRodney W. Grimes 	char *mode;
1999b50d902SRodney W. Grimes {
2009b50d902SRodney W. Grimes 	register struct tftphdr *ap;
2019b50d902SRodney W. Grimes 	struct tftphdr *dp, *w_init();
2029b50d902SRodney W. Grimes 	register int n;
2039b50d902SRodney W. Grimes 	volatile int block, size, firsttrip;
2049b50d902SRodney W. Grimes 	volatile unsigned long amount;
2059b50d902SRodney W. Grimes 	struct sockaddr_in from;
2069b50d902SRodney W. Grimes 	int fromlen;
2079b50d902SRodney W. Grimes 	FILE *file;
2089b50d902SRodney W. Grimes 	volatile int convert;		/* true if converting crlf -> lf */
2099b50d902SRodney W. Grimes 
2109b50d902SRodney W. Grimes 	startclock();
2119b50d902SRodney W. Grimes 	dp = w_init();
2129b50d902SRodney W. Grimes 	ap = (struct tftphdr *)ackbuf;
2139b50d902SRodney W. Grimes 	file = fdopen(fd, "w");
2149b50d902SRodney W. Grimes 	convert = !strcmp(mode, "netascii");
2159b50d902SRodney W. Grimes 	block = 1;
2169b50d902SRodney W. Grimes 	firsttrip = 1;
2179b50d902SRodney W. Grimes 	amount = 0;
2189b50d902SRodney W. Grimes 
2199b50d902SRodney W. Grimes 	signal(SIGALRM, timer);
2209b50d902SRodney W. Grimes 	do {
2219b50d902SRodney W. Grimes 		if (firsttrip) {
2229b50d902SRodney W. Grimes 			size = makerequest(RRQ, name, ap, mode);
2239b50d902SRodney W. Grimes 			firsttrip = 0;
2249b50d902SRodney W. Grimes 		} else {
2259b50d902SRodney W. Grimes 			ap->th_opcode = htons((u_short)ACK);
2269b50d902SRodney W. Grimes 			ap->th_block = htons((u_short)(block));
2279b50d902SRodney W. Grimes 			size = 4;
2289b50d902SRodney W. Grimes 			block++;
2299b50d902SRodney W. Grimes 		}
2309b50d902SRodney W. Grimes 		timeout = 0;
2319b50d902SRodney W. Grimes 		(void) setjmp(timeoutbuf);
2329b50d902SRodney W. Grimes send_ack:
2339b50d902SRodney W. Grimes 		if (trace)
2349b50d902SRodney W. Grimes 			tpacket("sent", ap, size);
2359b50d902SRodney W. Grimes 		if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr,
2369b50d902SRodney W. Grimes 		    sizeof(peeraddr)) != size) {
2379b50d902SRodney W. Grimes 			alarm(0);
238fd129a02SPhilippe Charnier 			warn("sendto");
2399b50d902SRodney W. Grimes 			goto abort;
2409b50d902SRodney W. Grimes 		}
2419b50d902SRodney W. Grimes 		write_behind(file, convert);
2429b50d902SRodney W. Grimes 		for ( ; ; ) {
2439b50d902SRodney W. Grimes 			alarm(rexmtval);
2449b50d902SRodney W. Grimes 			do  {
2459b50d902SRodney W. Grimes 				fromlen = sizeof(from);
2469b50d902SRodney W. Grimes 				n = recvfrom(f, dp, PKTSIZE, 0,
2479b50d902SRodney W. Grimes 				    (struct sockaddr *)&from, &fromlen);
2489b50d902SRodney W. Grimes 			} while (n <= 0);
2499b50d902SRodney W. Grimes 			alarm(0);
2509b50d902SRodney W. Grimes 			if (n < 0) {
251fd129a02SPhilippe Charnier 				warn("recvfrom");
2529b50d902SRodney W. Grimes 				goto abort;
2539b50d902SRodney W. Grimes 			}
2549b50d902SRodney W. Grimes 			peeraddr.sin_port = from.sin_port;	/* added */
2559b50d902SRodney W. Grimes 			if (trace)
2569b50d902SRodney W. Grimes 				tpacket("received", dp, n);
2579b50d902SRodney W. Grimes 			/* should verify client address */
2589b50d902SRodney W. Grimes 			dp->th_opcode = ntohs(dp->th_opcode);
2599b50d902SRodney W. Grimes 			dp->th_block = ntohs(dp->th_block);
2609b50d902SRodney W. Grimes 			if (dp->th_opcode == ERROR) {
2619b50d902SRodney W. Grimes 				printf("Error code %d: %s\n", dp->th_code,
2629b50d902SRodney W. Grimes 					dp->th_msg);
2639b50d902SRodney W. Grimes 				goto abort;
2649b50d902SRodney W. Grimes 			}
2659b50d902SRodney W. Grimes 			if (dp->th_opcode == DATA) {
2669b50d902SRodney W. Grimes 				int j;
2679b50d902SRodney W. Grimes 
2689b50d902SRodney W. Grimes 				if (dp->th_block == block) {
2699b50d902SRodney W. Grimes 					break;		/* have next packet */
2709b50d902SRodney W. Grimes 				}
2719b50d902SRodney W. Grimes 				/* On an error, try to synchronize
2729b50d902SRodney W. Grimes 				 * both sides.
2739b50d902SRodney W. Grimes 				 */
2749b50d902SRodney W. Grimes 				j = synchnet(f);
2759b50d902SRodney W. Grimes 				if (j && trace) {
2769b50d902SRodney W. Grimes 					printf("discarded %d packets\n", j);
2779b50d902SRodney W. Grimes 				}
2789b50d902SRodney W. Grimes 				if (dp->th_block == (block-1)) {
2799b50d902SRodney W. Grimes 					goto send_ack;	/* resend ack */
2809b50d902SRodney W. Grimes 				}
2819b50d902SRodney W. Grimes 			}
2829b50d902SRodney W. Grimes 		}
2839b50d902SRodney W. Grimes 	/*	size = write(fd, dp->th_data, n - 4); */
2849b50d902SRodney W. Grimes 		size = writeit(file, &dp, n - 4, convert);
2859b50d902SRodney W. Grimes 		if (size < 0) {
2869b50d902SRodney W. Grimes 			nak(errno + 100);
2879b50d902SRodney W. Grimes 			break;
2889b50d902SRodney W. Grimes 		}
2899b50d902SRodney W. Grimes 		amount += size;
2909b50d902SRodney W. Grimes 	} while (size == SEGSIZE);
2919b50d902SRodney W. Grimes abort:						/* ok to ack, since user */
2929b50d902SRodney W. Grimes 	ap->th_opcode = htons((u_short)ACK);	/* has seen err msg */
2939b50d902SRodney W. Grimes 	ap->th_block = htons((u_short)block);
2949b50d902SRodney W. Grimes 	(void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
2959b50d902SRodney W. Grimes 	    sizeof(peeraddr));
2969b50d902SRodney W. Grimes 	write_behind(file, convert);		/* flush last buffer */
2979b50d902SRodney W. Grimes 	fclose(file);
2989b50d902SRodney W. Grimes 	stopclock();
2999b50d902SRodney W. Grimes 	if (amount > 0)
3009b50d902SRodney W. Grimes 		printstats("Received", amount);
3019b50d902SRodney W. Grimes }
3029b50d902SRodney W. Grimes 
3039b50d902SRodney W. Grimes static int
3049b50d902SRodney W. Grimes makerequest(request, name, tp, mode)
3059b50d902SRodney W. Grimes 	int request;
3069b50d902SRodney W. Grimes 	const char *name;
3079b50d902SRodney W. Grimes 	struct tftphdr *tp;
3089b50d902SRodney W. Grimes 	const char *mode;
3099b50d902SRodney W. Grimes {
3109b50d902SRodney W. Grimes 	register char *cp;
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	tp->th_opcode = htons((u_short)request);
3139b50d902SRodney W. Grimes 	cp = tp->th_stuff;
3149b50d902SRodney W. Grimes 	strcpy(cp, name);
3159b50d902SRodney W. Grimes 	cp += strlen(name);
3169b50d902SRodney W. Grimes 	*cp++ = '\0';
3179b50d902SRodney W. Grimes 	strcpy(cp, mode);
3189b50d902SRodney W. Grimes 	cp += strlen(mode);
3199b50d902SRodney W. Grimes 	*cp++ = '\0';
3209b50d902SRodney W. Grimes 	return (cp - (char *)tp);
3219b50d902SRodney W. Grimes }
3229b50d902SRodney W. Grimes 
3239b50d902SRodney W. Grimes struct errmsg {
3249b50d902SRodney W. Grimes 	int	e_code;
3259b50d902SRodney W. Grimes 	char	*e_msg;
3269b50d902SRodney W. Grimes } errmsgs[] = {
3279b50d902SRodney W. Grimes 	{ EUNDEF,	"Undefined error code" },
3289b50d902SRodney W. Grimes 	{ ENOTFOUND,	"File not found" },
3299b50d902SRodney W. Grimes 	{ EACCESS,	"Access violation" },
3309b50d902SRodney W. Grimes 	{ ENOSPACE,	"Disk full or allocation exceeded" },
3319b50d902SRodney W. Grimes 	{ EBADOP,	"Illegal TFTP operation" },
3329b50d902SRodney W. Grimes 	{ EBADID,	"Unknown transfer ID" },
3339b50d902SRodney W. Grimes 	{ EEXISTS,	"File already exists" },
3349b50d902SRodney W. Grimes 	{ ENOUSER,	"No such user" },
3359b50d902SRodney W. Grimes 	{ -1,		0 }
3369b50d902SRodney W. Grimes };
3379b50d902SRodney W. Grimes 
3389b50d902SRodney W. Grimes /*
3399b50d902SRodney W. Grimes  * Send a nak packet (error message).
3409b50d902SRodney W. Grimes  * Error code passed in is one of the
3419b50d902SRodney W. Grimes  * standard TFTP codes, or a UNIX errno
3429b50d902SRodney W. Grimes  * offset by 100.
3439b50d902SRodney W. Grimes  */
3449b50d902SRodney W. Grimes static void
3459b50d902SRodney W. Grimes nak(error)
3469b50d902SRodney W. Grimes 	int error;
3479b50d902SRodney W. Grimes {
3489b50d902SRodney W. Grimes 	register struct errmsg *pe;
3499b50d902SRodney W. Grimes 	register struct tftphdr *tp;
3509b50d902SRodney W. Grimes 	int length;
3519b50d902SRodney W. Grimes 	char *strerror();
3529b50d902SRodney W. Grimes 
3539b50d902SRodney W. Grimes 	tp = (struct tftphdr *)ackbuf;
3549b50d902SRodney W. Grimes 	tp->th_opcode = htons((u_short)ERROR);
3559b50d902SRodney W. Grimes 	tp->th_code = htons((u_short)error);
3569b50d902SRodney W. Grimes 	for (pe = errmsgs; pe->e_code >= 0; pe++)
3579b50d902SRodney W. Grimes 		if (pe->e_code == error)
3589b50d902SRodney W. Grimes 			break;
3599b50d902SRodney W. Grimes 	if (pe->e_code < 0) {
3609b50d902SRodney W. Grimes 		pe->e_msg = strerror(error - 100);
3619b50d902SRodney W. Grimes 		tp->th_code = EUNDEF;
3629b50d902SRodney W. Grimes 	}
3639b50d902SRodney W. Grimes 	strcpy(tp->th_msg, pe->e_msg);
3649b50d902SRodney W. Grimes 	length = strlen(pe->e_msg) + 4;
3659b50d902SRodney W. Grimes 	if (trace)
3669b50d902SRodney W. Grimes 		tpacket("sent", tp, length);
3679b50d902SRodney W. Grimes 	if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&peeraddr,
3689b50d902SRodney W. Grimes 	    sizeof(peeraddr)) != length)
369fd129a02SPhilippe Charnier 		warn("nak");
3709b50d902SRodney W. Grimes }
3719b50d902SRodney W. Grimes 
3729b50d902SRodney W. Grimes static void
3739b50d902SRodney W. Grimes tpacket(s, tp, n)
3749b50d902SRodney W. Grimes 	const char *s;
3759b50d902SRodney W. Grimes 	struct tftphdr *tp;
3769b50d902SRodney W. Grimes 	int n;
3779b50d902SRodney W. Grimes {
3789b50d902SRodney W. Grimes 	static char *opcodes[] =
3799b50d902SRodney W. Grimes 	   { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
3809b50d902SRodney W. Grimes 	register char *cp, *file;
3819b50d902SRodney W. Grimes 	u_short op = ntohs(tp->th_opcode);
3829b50d902SRodney W. Grimes 	char *index();
3839b50d902SRodney W. Grimes 
3849b50d902SRodney W. Grimes 	if (op < RRQ || op > ERROR)
3859b50d902SRodney W. Grimes 		printf("%s opcode=%x ", s, op);
3869b50d902SRodney W. Grimes 	else
3879b50d902SRodney W. Grimes 		printf("%s %s ", s, opcodes[op]);
3889b50d902SRodney W. Grimes 	switch (op) {
3899b50d902SRodney W. Grimes 
3909b50d902SRodney W. Grimes 	case RRQ:
3919b50d902SRodney W. Grimes 	case WRQ:
3929b50d902SRodney W. Grimes 		n -= 2;
3939b50d902SRodney W. Grimes 		file = cp = tp->th_stuff;
3949b50d902SRodney W. Grimes 		cp = index(cp, '\0');
3959b50d902SRodney W. Grimes 		printf("<file=%s, mode=%s>\n", file, cp + 1);
3969b50d902SRodney W. Grimes 		break;
3979b50d902SRodney W. Grimes 
3989b50d902SRodney W. Grimes 	case DATA:
3999b50d902SRodney W. Grimes 		printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
4009b50d902SRodney W. Grimes 		break;
4019b50d902SRodney W. Grimes 
4029b50d902SRodney W. Grimes 	case ACK:
4039b50d902SRodney W. Grimes 		printf("<block=%d>\n", ntohs(tp->th_block));
4049b50d902SRodney W. Grimes 		break;
4059b50d902SRodney W. Grimes 
4069b50d902SRodney W. Grimes 	case ERROR:
4079b50d902SRodney W. Grimes 		printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
4089b50d902SRodney W. Grimes 		break;
4099b50d902SRodney W. Grimes 	}
4109b50d902SRodney W. Grimes }
4119b50d902SRodney W. Grimes 
4129b50d902SRodney W. Grimes struct timeval tstart;
4139b50d902SRodney W. Grimes struct timeval tstop;
4149b50d902SRodney W. Grimes 
4159b50d902SRodney W. Grimes static void
4169b50d902SRodney W. Grimes startclock()
4179b50d902SRodney W. Grimes {
4189b50d902SRodney W. Grimes 
4199b50d902SRodney W. Grimes 	(void)gettimeofday(&tstart, NULL);
4209b50d902SRodney W. Grimes }
4219b50d902SRodney W. Grimes 
4229b50d902SRodney W. Grimes static void
4239b50d902SRodney W. Grimes stopclock()
4249b50d902SRodney W. Grimes {
4259b50d902SRodney W. Grimes 
4269b50d902SRodney W. Grimes 	(void)gettimeofday(&tstop, NULL);
4279b50d902SRodney W. Grimes }
4289b50d902SRodney W. Grimes 
4299b50d902SRodney W. Grimes static void
4309b50d902SRodney W. Grimes printstats(direction, amount)
4319b50d902SRodney W. Grimes 	const char *direction;
4329b50d902SRodney W. Grimes 	unsigned long amount;
4339b50d902SRodney W. Grimes {
4349b50d902SRodney W. Grimes 	double delta;
4359b50d902SRodney W. Grimes 			/* compute delta in 1/10's second units */
4369b50d902SRodney W. Grimes 	delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
4379b50d902SRodney W. Grimes 		((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
4389b50d902SRodney W. Grimes 	delta = delta/10.;      /* back to seconds */
4399b50d902SRodney W. Grimes 	printf("%s %d bytes in %.1f seconds", direction, amount, delta);
4409b50d902SRodney W. Grimes 	if (verbose)
4419b50d902SRodney W. Grimes 		printf(" [%.0f bits/sec]", (amount*8.)/delta);
4429b50d902SRodney W. Grimes 	putchar('\n');
4439b50d902SRodney W. Grimes }
4449b50d902SRodney W. Grimes 
4459b50d902SRodney W. Grimes static void
4469b50d902SRodney W. Grimes timer(sig)
4479b50d902SRodney W. Grimes 	int sig;
4489b50d902SRodney W. Grimes {
4499b50d902SRodney W. Grimes 
4509b50d902SRodney W. Grimes 	timeout += rexmtval;
4519b50d902SRodney W. Grimes 	if (timeout >= maxtimeout) {
4529b50d902SRodney W. Grimes 		printf("Transfer timed out.\n");
4539b50d902SRodney W. Grimes 		longjmp(toplevel, -1);
4549b50d902SRodney W. Grimes 	}
4559b50d902SRodney W. Grimes 	longjmp(timeoutbuf, 1);
4569b50d902SRodney W. Grimes }
457