xref: /csrg-svn/usr.bin/tftp/tftp.c (revision 42770)
122404Sdist /*
233821Sbostic  * Copyright (c) 1983 Regents of the University of California.
333821Sbostic  * All rights reserved.
433821Sbostic  *
5*42770Sbostic  * %sccs.include.redist.c%
622404Sdist  */
722404Sdist 
814554Ssam #ifndef lint
9*42770Sbostic static char sccsid[] = "@(#)tftp.c	5.9 (Berkeley) 06/01/90";
1033821Sbostic #endif /* not lint */
117770Ssam 
1226094Sminshall /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
1326094Sminshall 
147770Ssam /*
157770Ssam  * TFTP User Program -- Protocol Machines
167770Ssam  */
177770Ssam #include <sys/types.h>
187770Ssam #include <sys/socket.h>
1926094Sminshall #include <sys/time.h>
209220Ssam 
219220Ssam #include <netinet/in.h>
2226094Sminshall 
2312399Ssam #include <arpa/tftp.h>
2412399Ssam 
257770Ssam #include <signal.h>
267770Ssam #include <stdio.h>
277770Ssam #include <errno.h>
287770Ssam #include <setjmp.h>
299220Ssam 
307770Ssam extern	int errno;
3126094Sminshall 
3226094Sminshall extern  struct sockaddr_in sin;         /* filled in by main */
3326094Sminshall extern  int     f;                      /* the opened socket */
3426094Sminshall extern  int     trace;
3526094Sminshall extern  int     verbose;
3626094Sminshall extern  int     rexmtval;
3726094Sminshall extern  int     maxtimeout;
3826094Sminshall 
3926094Sminshall #define PKTSIZE    SEGSIZE+4
4026094Sminshall char    ackbuf[PKTSIZE];
417770Ssam int	timeout;
427770Ssam jmp_buf	toplevel;
4313018Ssam jmp_buf	timeoutbuf;
447770Ssam 
457770Ssam timer()
467770Ssam {
4713018Ssam 
4813018Ssam 	timeout += rexmtval;
4913018Ssam 	if (timeout >= maxtimeout) {
507770Ssam 		printf("Transfer timed out.\n");
517770Ssam 		longjmp(toplevel, -1);
527770Ssam 	}
5313018Ssam 	longjmp(timeoutbuf, 1);
547770Ssam }
557770Ssam 
567770Ssam /*
577770Ssam  * Send the requested file.
587770Ssam  */
5926094Sminshall sendfile(fd, name, mode)
607770Ssam 	int fd;
617770Ssam 	char *name;
6226094Sminshall 	char *mode;
637770Ssam {
6426094Sminshall 	register struct tftphdr *ap;       /* data and ack packets */
6526094Sminshall 	struct tftphdr *r_init(), *dp;
6626094Sminshall 	register int block = 0, size, n;
6726094Sminshall 	register unsigned long amount = 0;
6826094Sminshall 	struct sockaddr_in from;
6926094Sminshall 	int fromlen;
7026094Sminshall 	int convert;            /* true if doing nl->crlf conversion */
7126094Sminshall 	FILE *file;
727770Ssam 
7326094Sminshall 	startclock();           /* start stat's clock */
7426094Sminshall 	dp = r_init();          /* reset fillbuf/read-ahead code */
7526094Sminshall 	ap = (struct tftphdr *)ackbuf;
7626094Sminshall 	file = fdopen(fd, "r");
7726094Sminshall 	convert = !strcmp(mode, "netascii");
7826094Sminshall 
7913018Ssam 	signal(SIGALRM, timer);
807770Ssam 	do {
8113018Ssam 		if (block == 0)
8226094Sminshall 			size = makerequest(WRQ, name, dp, mode) - 4;
8313018Ssam 		else {
8426094Sminshall 		/*      size = read(fd, dp->th_data, SEGSIZE);   */
8526094Sminshall 			size = readit(file, &dp, convert);
867770Ssam 			if (size < 0) {
8726094Sminshall 				nak(errno + 100);
887770Ssam 				break;
897770Ssam 			}
9026094Sminshall 			dp->th_opcode = htons((u_short)DATA);
9126094Sminshall 			dp->th_block = htons((u_short)block);
927770Ssam 		}
937770Ssam 		timeout = 0;
9413018Ssam 		(void) setjmp(timeoutbuf);
9526104Sminshall send_data:
967770Ssam 		if (trace)
9726094Sminshall 			tpacket("sent", dp, size + 4);
9826094Sminshall 		n = sendto(f, dp, size + 4, 0, (caddr_t)&sin, sizeof (sin));
999220Ssam 		if (n != size + 4) {
10013018Ssam 			perror("tftp: sendto");
10126094Sminshall 			goto abort;
1027770Ssam 		}
10326094Sminshall 		read_ahead(file, convert);
10426104Sminshall 		for ( ; ; ) {
10513018Ssam 			alarm(rexmtval);
10613018Ssam 			do {
10713018Ssam 				fromlen = sizeof (from);
10826094Sminshall 				n = recvfrom(f, ackbuf, sizeof (ackbuf), 0,
10913018Ssam 				    (caddr_t)&from, &fromlen);
11013018Ssam 			} while (n <= 0);
1117770Ssam 			alarm(0);
11213018Ssam 			if (n < 0) {
11313018Ssam 				perror("tftp: recvfrom");
11426094Sminshall 				goto abort;
11513018Ssam 			}
11626094Sminshall 			sin.sin_port = from.sin_port;   /* added */
11713018Ssam 			if (trace)
11826094Sminshall 				tpacket("received", ap, n);
11913018Ssam 			/* should verify packet came from server */
12026094Sminshall 			ap->th_opcode = ntohs(ap->th_opcode);
12126094Sminshall 			ap->th_block = ntohs(ap->th_block);
12226094Sminshall 			if (ap->th_opcode == ERROR) {
12326094Sminshall 				printf("Error code %d: %s\n", ap->th_code,
12426094Sminshall 					ap->th_msg);
12526094Sminshall 				goto abort;
12613018Ssam 			}
12726104Sminshall 			if (ap->th_opcode == ACK) {
12826113Sminshall 				int j;
12926113Sminshall 
13026104Sminshall 				if (ap->th_block == block) {
13126104Sminshall 					break;
13226104Sminshall 				}
13326113Sminshall 				/* On an error, try to synchronize
13426113Sminshall 				 * both sides.
13526113Sminshall 				 */
13626113Sminshall 				j = synchnet(f);
13726113Sminshall 				if (j && trace) {
13826113Sminshall 					printf("discarded %d packets\n",
13926113Sminshall 							j);
14026113Sminshall 				}
14126113Sminshall 				if (ap->th_block == (block-1)) {
14226113Sminshall 					goto send_data;
14326113Sminshall 				}
14426104Sminshall 			}
14526104Sminshall 		}
1467770Ssam 		if (block > 0)
1477770Ssam 			amount += size;
1487770Ssam 		block++;
1497770Ssam 	} while (size == SEGSIZE || block == 1);
15026094Sminshall abort:
15126094Sminshall 	fclose(file);
15226094Sminshall 	stopclock();
15326094Sminshall 	if (amount > 0)
15426094Sminshall 		printstats("Sent", amount);
1557770Ssam }
1567770Ssam 
1577770Ssam /*
1587770Ssam  * Receive a file.
1597770Ssam  */
16026094Sminshall recvfile(fd, name, mode)
1617770Ssam 	int fd;
1627770Ssam 	char *name;
16326094Sminshall 	char *mode;
1647770Ssam {
16526094Sminshall 	register struct tftphdr *ap;
16626094Sminshall 	struct tftphdr *dp, *w_init();
16726094Sminshall 	register int block = 1, n, size;
16826094Sminshall 	unsigned long amount = 0;
16926094Sminshall 	struct sockaddr_in from;
17026094Sminshall 	int fromlen, firsttrip = 1;
17126094Sminshall 	FILE *file;
17226094Sminshall 	int convert;                    /* true if converting crlf -> lf */
1737770Ssam 
17426094Sminshall 	startclock();
17526094Sminshall 	dp = w_init();
17626094Sminshall 	ap = (struct tftphdr *)ackbuf;
17726094Sminshall 	file = fdopen(fd, "w");
17826094Sminshall 	convert = !strcmp(mode, "netascii");
17926094Sminshall 
18013018Ssam 	signal(SIGALRM, timer);
1817770Ssam 	do {
18213018Ssam 		if (firsttrip) {
18326094Sminshall 			size = makerequest(RRQ, name, ap, mode);
18413018Ssam 			firsttrip = 0;
18513018Ssam 		} else {
18626094Sminshall 			ap->th_opcode = htons((u_short)ACK);
18726094Sminshall 			ap->th_block = htons((u_short)(block));
18813018Ssam 			size = 4;
18913018Ssam 			block++;
19013018Ssam 		}
1917770Ssam 		timeout = 0;
19213018Ssam 		(void) setjmp(timeoutbuf);
19326094Sminshall send_ack:
1947770Ssam 		if (trace)
19526094Sminshall 			tpacket("sent", ap, size);
19626094Sminshall 		if (sendto(f, ackbuf, size, 0, (caddr_t)&sin,
19726094Sminshall 		    sizeof (sin)) != size) {
19813018Ssam 			alarm(0);
19913018Ssam 			perror("tftp: sendto");
20026094Sminshall 			goto abort;
2017770Ssam 		}
20226094Sminshall 		write_behind(file, convert);
20326094Sminshall 		for ( ; ; ) {
20413018Ssam 			alarm(rexmtval);
20526094Sminshall 			do  {
20616384Ssam 				fromlen = sizeof (from);
20726094Sminshall 				n = recvfrom(f, dp, PKTSIZE, 0,
20813018Ssam 				    (caddr_t)&from, &fromlen);
20916384Ssam 			} while (n <= 0);
2107770Ssam 			alarm(0);
21113018Ssam 			if (n < 0) {
21213018Ssam 				perror("tftp: recvfrom");
21326094Sminshall 				goto abort;
21413018Ssam 			}
21526094Sminshall 			sin.sin_port = from.sin_port;   /* added */
21626094Sminshall 			if (trace)
21726094Sminshall 				tpacket("received", dp, n);
21826094Sminshall 			/* should verify client address */
21926094Sminshall 			dp->th_opcode = ntohs(dp->th_opcode);
22026094Sminshall 			dp->th_block = ntohs(dp->th_block);
22126094Sminshall 			if (dp->th_opcode == ERROR) {
22226094Sminshall 				printf("Error code %d: %s\n", dp->th_code,
22326094Sminshall 					dp->th_msg);
22426094Sminshall 				goto abort;
22516384Ssam 			}
22626094Sminshall 			if (dp->th_opcode == DATA) {
22726113Sminshall 				int j;
22826113Sminshall 
22926113Sminshall 				if (dp->th_block == block) {
23026094Sminshall 					break;          /* have next packet */
23126113Sminshall 				}
23226113Sminshall 				/* On an error, try to synchronize
23326113Sminshall 				 * both sides.
23426113Sminshall 				 */
23526113Sminshall 				j = synchnet(f);
23626113Sminshall 				if (j && trace) {
23726113Sminshall 					printf("discarded %d packets\n", j);
23826113Sminshall 				}
23926113Sminshall 				if (dp->th_block == (block-1)) {
24026094Sminshall 					goto send_ack;  /* resend ack */
24126113Sminshall 				}
24216384Ssam 			}
24326094Sminshall 		}
24426094Sminshall 	/*      size = write(fd, dp->th_data, n - 4); */
24526094Sminshall 		size = writeit(file, &dp, n - 4, convert);
2467770Ssam 		if (size < 0) {
24726094Sminshall 			nak(errno + 100);
24826094Sminshall 			break;
2497770Ssam 		}
2507770Ssam 		amount += size;
2517770Ssam 	} while (size == SEGSIZE);
25226094Sminshall abort:                                          /* ok to ack, since user */
25326094Sminshall 	ap->th_opcode = htons((u_short)ACK);    /* has seen err msg */
25426094Sminshall 	ap->th_block = htons((u_short)block);
25526094Sminshall 	(void) sendto(f, ackbuf, 4, 0, &sin, sizeof (sin));
25626094Sminshall 	write_behind(file, convert);            /* flush last buffer */
25726094Sminshall 	fclose(file);
25826094Sminshall 	stopclock();
25926094Sminshall 	if (amount > 0)
26026094Sminshall 		printstats("Received", amount);
2617770Ssam }
2627770Ssam 
26326094Sminshall makerequest(request, name, tp, mode)
2647770Ssam 	int request;
26526094Sminshall 	char *name, *mode;
26626094Sminshall 	struct tftphdr *tp;
2677770Ssam {
2687770Ssam 	register char *cp;
2697770Ssam 
27026094Sminshall 	tp->th_opcode = htons((u_short)request);
27126094Sminshall 	cp = tp->th_stuff;
27226094Sminshall 	strcpy(cp, name);
27326094Sminshall 	cp += strlen(name);
2747770Ssam 	*cp++ = '\0';
2757770Ssam 	strcpy(cp, mode);
27626094Sminshall 	cp += strlen(mode);
2777770Ssam 	*cp++ = '\0';
27826094Sminshall 	return (cp - (char *)tp);
2797770Ssam }
2807770Ssam 
2817770Ssam struct errmsg {
2827770Ssam 	int	e_code;
2837770Ssam 	char	*e_msg;
2847770Ssam } errmsgs[] = {
2857770Ssam 	{ EUNDEF,	"Undefined error code" },
2867770Ssam 	{ ENOTFOUND,	"File not found" },
2877770Ssam 	{ EACCESS,	"Access violation" },
2887770Ssam 	{ ENOSPACE,	"Disk full or allocation exceeded" },
2897770Ssam 	{ EBADOP,	"Illegal TFTP operation" },
2907770Ssam 	{ EBADID,	"Unknown transfer ID" },
2917770Ssam 	{ EEXISTS,	"File already exists" },
2927770Ssam 	{ ENOUSER,	"No such user" },
2937770Ssam 	{ -1,		0 }
2947770Ssam };
2957770Ssam 
2967770Ssam /*
2977770Ssam  * Send a nak packet (error message).
2987770Ssam  * Error code passed in is one of the
2997770Ssam  * standard TFTP codes, or a UNIX errno
3007770Ssam  * offset by 100.
3017770Ssam  */
30226094Sminshall nak(error)
3037770Ssam 	int error;
3047770Ssam {
30542423Sbostic 	register struct errmsg *pe;
30626094Sminshall 	register struct tftphdr *tp;
3077770Ssam 	int length;
30842423Sbostic 	char *strerror();
3097770Ssam 
31026094Sminshall 	tp = (struct tftphdr *)ackbuf;
31126094Sminshall 	tp->th_opcode = htons((u_short)ERROR);
31226094Sminshall 	tp->th_code = htons((u_short)error);
3137770Ssam 	for (pe = errmsgs; pe->e_code >= 0; pe++)
3147770Ssam 		if (pe->e_code == error)
3157770Ssam 			break;
31626094Sminshall 	if (pe->e_code < 0) {
31742423Sbostic 		pe->e_msg = strerror(error - 100);
31826094Sminshall 		tp->th_code = EUNDEF;
31926094Sminshall 	}
32026094Sminshall 	strcpy(tp->th_msg, pe->e_msg);
3217770Ssam 	length = strlen(pe->e_msg) + 4;
3227770Ssam 	if (trace)
32326094Sminshall 		tpacket("sent", tp, length);
32426094Sminshall 	if (sendto(f, ackbuf, length, 0, &sin, sizeof (sin)) != length)
32526094Sminshall 		perror("nak");
3267770Ssam }
3277770Ssam 
32826094Sminshall tpacket(s, tp, n)
32926111Sminshall 	char *s;
3307770Ssam 	struct tftphdr *tp;
3317770Ssam 	int n;
3327770Ssam {
3337770Ssam 	static char *opcodes[] =
3347770Ssam 	   { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
3357770Ssam 	register char *cp, *file;
3367770Ssam 	u_short op = ntohs(tp->th_opcode);
3377770Ssam 	char *index();
3387770Ssam 
3397770Ssam 	if (op < RRQ || op > ERROR)
34026094Sminshall 		printf("%s opcode=%x ", s, op);
3417770Ssam 	else
34226094Sminshall 		printf("%s %s ", s, opcodes[op]);
3437770Ssam 	switch (op) {
3447770Ssam 
3457770Ssam 	case RRQ:
3467770Ssam 	case WRQ:
3477770Ssam 		n -= 2;
3487770Ssam 		file = cp = tp->th_stuff;
3497770Ssam 		cp = index(cp, '\0');
3507770Ssam 		printf("<file=%s, mode=%s>\n", file, cp + 1);
3517770Ssam 		break;
3527770Ssam 
3537770Ssam 	case DATA:
3547770Ssam 		printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
3557770Ssam 		break;
3567770Ssam 
3577770Ssam 	case ACK:
3587770Ssam 		printf("<block=%d>\n", ntohs(tp->th_block));
3597770Ssam 		break;
3607770Ssam 
3617770Ssam 	case ERROR:
3627770Ssam 		printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
3637770Ssam 		break;
3647770Ssam 	}
3657770Ssam }
36626094Sminshall 
36726094Sminshall struct timeval tstart;
36826094Sminshall struct timeval tstop;
36926094Sminshall struct timezone zone;
37026094Sminshall 
37126094Sminshall startclock() {
37226094Sminshall 	gettimeofday(&tstart, &zone);
37326094Sminshall }
37426094Sminshall 
37526094Sminshall stopclock() {
37626094Sminshall 	gettimeofday(&tstop, &zone);
37726094Sminshall }
37826094Sminshall 
37926094Sminshall printstats(direction, amount)
38026094Sminshall char *direction;
38126094Sminshall unsigned long amount;
38226094Sminshall {
38326094Sminshall 	double delta;
38426094Sminshall 			/* compute delta in 1/10's second units */
38526094Sminshall 	delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
38626094Sminshall 		((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
38726094Sminshall 	delta = delta/10.;      /* back to seconds */
38826094Sminshall 	printf("%s %d bytes in %.1f seconds", direction, amount, delta);
38926094Sminshall 	if (verbose)
39026094Sminshall 		printf(" [%.0f bits/sec]", (amount*8.)/delta);
39126094Sminshall 	putchar('\n');
39226094Sminshall }
39326094Sminshall 
394