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