2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)tftp.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/tftp/tftp.c,v 1.5.2.3 2002/05/14 22:08:07 bsd Exp $
35 * $DragonFly: src/usr.bin/tftp/tftp.c,v 1.5 2004/09/03 20:38:01 dillon Exp $
38 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
41 * TFTP User Program -- Protocol Machines
43 #include <sys/types.h>
44 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #include <arpa/tftp.h>
63 extern struct sockaddr_storage peeraddr; /* filled in by main */
64 extern int f; /* the opened socket */
68 extern int maxtimeout;
69 extern volatile int txrx_error;
71 #define PKTSIZE SEGSIZE+4
77 static void nak(int, struct sockaddr *);
78 static int makerequest(int, const char *, struct tftphdr *, const char *);
79 static void printstats(const char *, unsigned long);
80 static void startclock(void);
81 static void stopclock(void);
82 static void timer(int);
83 static void tpacket(const char *, struct tftphdr *, int);
84 static int cmpport(struct sockaddr *, struct sockaddr *);
87 * Send the requested file.
90 xmitfile(int fd, char *name, char *mode)
92 register struct tftphdr *ap; /* data and ack packets */
93 struct tftphdr *r_init(), *dp;
95 volatile unsigned short block;
96 volatile int size, convert;
97 volatile unsigned long amount;
98 struct sockaddr_storage from;
101 struct sockaddr_storage peer;
102 struct sockaddr_storage serv; /* valid server port number */
104 startclock(); /* start stat's clock */
105 dp = r_init(); /* reset fillbuf/read-ahead code */
106 ap = (struct tftphdr *)ackbuf;
107 file = fdopen(fd, "r");
108 convert = !strcmp(mode, "netascii");
111 memcpy(&peer, &peeraddr, peeraddr.ss_len);
112 memset(&serv, 0, sizeof(serv));
114 signal(SIGALRM, timer);
117 size = makerequest(WRQ, name, dp, mode) - 4;
119 /* size = read(fd, dp->th_data, SEGSIZE); */
120 size = readit(file, &dp, convert);
122 nak(errno + 100, (struct sockaddr *)&peer);
125 dp->th_opcode = htons((u_short)DATA);
126 dp->th_block = htons((u_short)block);
129 (void) setjmp(timeoutbuf);
132 tpacket("sent", dp, size + 4);
133 n = sendto(f, dp, size + 4, 0,
134 (struct sockaddr *)&peer, peer.ss_len);
139 read_ahead(file, convert);
143 fromlen = sizeof(from);
144 n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
145 (struct sockaddr *)&from, &fromlen);
154 else if (!cmpport((struct sockaddr *)&serv,
155 (struct sockaddr *)&from)) {
156 warn("server port mismatch");
161 tpacket("received", ap, n);
162 /* should verify packet came from server */
163 ap->th_opcode = ntohs(ap->th_opcode);
164 ap->th_block = ntohs(ap->th_block);
165 if (ap->th_opcode == ERROR) {
166 printf("Error code %d: %s\n", ap->th_code,
171 if (ap->th_opcode == ACK) {
174 if (ap->th_block == block) {
177 /* On an error, try to synchronize
182 printf("discarded %d packets\n",
185 if (ap->th_block == (block-1)) {
193 } while (size == SEGSIZE || block == 1);
198 printstats("Sent", amount);
205 recvfile(int fd, char *name, char *mode)
207 register struct tftphdr *ap;
208 struct tftphdr *dp, *w_init();
210 volatile unsigned short block;
211 volatile int size, firsttrip;
212 volatile unsigned long amount;
213 struct sockaddr_storage from;
216 volatile int convert; /* true if converting crlf -> lf */
217 struct sockaddr_storage peer;
218 struct sockaddr_storage serv; /* valid server port number */
222 ap = (struct tftphdr *)ackbuf;
223 file = fdopen(fd, "w");
224 convert = !strcmp(mode, "netascii");
228 memcpy(&peer, &peeraddr, peeraddr.ss_len);
229 memset(&serv, 0, sizeof(serv));
231 signal(SIGALRM, timer);
234 size = makerequest(RRQ, name, ap, mode);
237 ap->th_opcode = htons((u_short)ACK);
238 ap->th_block = htons((u_short)(block));
243 (void) setjmp(timeoutbuf);
246 tpacket("sent", ap, size);
247 if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peer,
248 peer.ss_len) != size) {
253 write_behind(file, convert);
257 fromlen = sizeof(from);
258 n = recvfrom(f, dp, PKTSIZE, 0,
259 (struct sockaddr *)&from, &fromlen);
268 else if (!cmpport((struct sockaddr *)&serv,
269 (struct sockaddr *)&from)) {
270 warn("server port mismatch");
275 tpacket("received", dp, n);
276 /* should verify client address */
277 dp->th_opcode = ntohs(dp->th_opcode);
278 dp->th_block = ntohs(dp->th_block);
279 if (dp->th_opcode == ERROR) {
280 printf("Error code %d: %s\n", dp->th_code,
285 if (dp->th_opcode == DATA) {
288 if (dp->th_block == block) {
289 break; /* have next packet */
291 /* On an error, try to synchronize
296 printf("discarded %d packets\n", j);
298 if (dp->th_block == (block-1)) {
299 goto send_ack; /* resend ack */
303 /* size = write(fd, dp->th_data, n - 4); */
304 size = writeit(file, &dp, n - 4, convert);
306 nak(errno + 100, (struct sockaddr *)&peer);
310 } while (size == SEGSIZE);
311 abort: /* ok to ack, since user */
312 ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
313 ap->th_block = htons((u_short)block);
314 (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peer,
316 write_behind(file, convert); /* flush last buffer */
320 printstats("Received", amount);
324 makerequest(int request, const char *name, struct tftphdr *tp, const char *mode)
328 tp->th_opcode = htons((u_short)request);
336 return (cp - (char *)tp);
343 { EUNDEF, "Undefined error code" },
344 { ENOTFOUND, "File not found" },
345 { EACCESS, "Access violation" },
346 { ENOSPACE, "Disk full or allocation exceeded" },
347 { EBADOP, "Illegal TFTP operation" },
348 { EBADID, "Unknown transfer ID" },
349 { EEXISTS, "File already exists" },
350 { ENOUSER, "No such user" },
355 * Send a nak packet (error message).
356 * Error code passed in is one of the
357 * standard TFTP codes, or a UNIX errno
361 nak(int error, struct sockaddr *peer)
363 register struct errmsg *pe;
364 register struct tftphdr *tp;
368 tp = (struct tftphdr *)ackbuf;
369 tp->th_opcode = htons((u_short)ERROR);
370 tp->th_code = htons((u_short)error);
371 for (pe = errmsgs; pe->e_code >= 0; pe++)
372 if (pe->e_code == error)
374 if (pe->e_code < 0) {
375 pe->e_msg = strerror(error - 100);
376 tp->th_code = EUNDEF;
378 strcpy(tp->th_msg, pe->e_msg);
379 length = strlen(pe->e_msg) + 4;
381 tpacket("sent", tp, length);
382 if (sendto(f, ackbuf, length, 0, peer, peer->sa_len) != length)
387 tpacket(const char *s, struct tftphdr *tp, int n)
389 static char *opcodes[] =
390 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
391 register char *cp, *file;
392 u_short op = ntohs(tp->th_opcode);
394 if (op < RRQ || op > ERROR)
395 printf("%s opcode=%x ", s, op);
397 printf("%s %s ", s, opcodes[op]);
403 file = cp = tp->th_stuff;
404 cp = strchr(cp, '\0');
405 printf("<file=%s, mode=%s>\n", file, cp + 1);
409 printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
413 printf("<block=%d>\n", ntohs(tp->th_block));
417 printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
422 struct timeval tstart;
423 struct timeval tstop;
429 (void)gettimeofday(&tstart, NULL);
436 (void)gettimeofday(&tstop, NULL);
440 printstats(const char *direction, unsigned long amount)
443 /* compute delta in 1/10's second units */
444 delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
445 ((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
446 delta = delta/10.; /* back to seconds */
447 printf("%s %d bytes in %.1f seconds", direction, amount, delta);
449 printf(" [%.0f bits/sec]", (amount*8.)/delta);
458 if (timeout >= maxtimeout) {
459 printf("Transfer timed out.\n");
460 longjmp(toplevel, -1);
463 longjmp(timeoutbuf, 1);
467 cmpport(struct sockaddr *sa, struct sockaddr *sb)
469 char a[NI_MAXSERV], b[NI_MAXSERV];
471 if (getnameinfo(sa, sa->sa_len, NULL, 0, a, sizeof(a), NI_NUMERICSERV))
473 if (getnameinfo(sb, sb->sa_len, NULL, 0, b, sizeof(b), NI_NUMERICSERV))
475 if (strcmp(a, b) != 0)