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