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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)tftp.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/tftp/tftp.c,v 1.5.2.3 2002/05/14 22:08:07 bsd Exp $
31 * $DragonFly: src/usr.bin/tftp/tftp.c,v 1.6 2008/10/16 01:52:33 swildner Exp $
34 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
37 * TFTP User Program -- Protocol Machines
39 #include <sys/types.h>
40 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include <arpa/tftp.h>
59 extern struct sockaddr_storage peeraddr; /* filled in by main */
60 extern int f; /* the opened socket */
64 extern int maxtimeout;
65 extern volatile int txrx_error;
67 #define PKTSIZE SEGSIZE+4
73 static void nak(int, struct sockaddr *);
74 static int makerequest(int, const char *, struct tftphdr *, const char *);
75 static void printstats(const char *, unsigned long);
76 static void startclock(void);
77 static void stopclock(void);
78 static void timer(int);
79 static void tpacket(const char *, struct tftphdr *, int);
80 static int cmpport(struct sockaddr *, struct sockaddr *);
83 * Send the requested file.
86 xmitfile(int fd, char *name, char *mode)
88 struct tftphdr *ap; /* data and ack packets */
91 volatile unsigned short block;
92 volatile int size, convert;
93 volatile unsigned long amount;
94 struct sockaddr_storage from;
97 struct sockaddr_storage peer;
98 struct sockaddr_storage serv; /* valid server port number */
100 startclock(); /* start stat's clock */
101 dp = r_init(); /* reset fillbuf/read-ahead code */
102 ap = (struct tftphdr *)ackbuf;
103 file = fdopen(fd, "r");
104 convert = !strcmp(mode, "netascii");
107 memcpy(&peer, &peeraddr, peeraddr.ss_len);
108 memset(&serv, 0, sizeof(serv));
110 signal(SIGALRM, timer);
113 size = makerequest(WRQ, name, dp, mode) - 4;
115 /* size = read(fd, dp->th_data, SEGSIZE); */
116 size = readit(file, &dp, convert);
118 nak(errno + 100, (struct sockaddr *)&peer);
121 dp->th_opcode = htons((u_short)DATA);
122 dp->th_block = htons((u_short)block);
125 (void) setjmp(timeoutbuf);
128 tpacket("sent", dp, size + 4);
129 n = sendto(f, dp, size + 4, 0,
130 (struct sockaddr *)&peer, peer.ss_len);
135 read_ahead(file, convert);
139 fromlen = sizeof(from);
140 n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
141 (struct sockaddr *)&from, &fromlen);
150 else if (!cmpport((struct sockaddr *)&serv,
151 (struct sockaddr *)&from)) {
152 warn("server port mismatch");
157 tpacket("received", ap, n);
158 /* should verify packet came from server */
159 ap->th_opcode = ntohs(ap->th_opcode);
160 ap->th_block = ntohs(ap->th_block);
161 if (ap->th_opcode == ERROR) {
162 printf("Error code %d: %s\n", ap->th_code,
167 if (ap->th_opcode == ACK) {
170 if (ap->th_block == block) {
173 /* On an error, try to synchronize
178 printf("discarded %d packets\n",
181 if (ap->th_block == (block-1)) {
189 } while (size == SEGSIZE || block == 1);
194 printstats("Sent", amount);
201 recvfile(int fd, char *name, char *mode)
206 volatile unsigned short block;
207 volatile int size, firsttrip;
208 volatile unsigned long amount;
209 struct sockaddr_storage from;
212 volatile int convert; /* true if converting crlf -> lf */
213 struct sockaddr_storage peer;
214 struct sockaddr_storage serv; /* valid server port number */
218 ap = (struct tftphdr *)ackbuf;
219 file = fdopen(fd, "w");
220 convert = !strcmp(mode, "netascii");
224 memcpy(&peer, &peeraddr, peeraddr.ss_len);
225 memset(&serv, 0, sizeof(serv));
227 signal(SIGALRM, timer);
230 size = makerequest(RRQ, name, ap, mode);
233 ap->th_opcode = htons((u_short)ACK);
234 ap->th_block = htons((u_short)(block));
239 (void) setjmp(timeoutbuf);
242 tpacket("sent", ap, size);
243 if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peer,
244 peer.ss_len) != size) {
249 write_behind(file, convert);
253 fromlen = sizeof(from);
254 n = recvfrom(f, dp, PKTSIZE, 0,
255 (struct sockaddr *)&from, &fromlen);
264 else if (!cmpport((struct sockaddr *)&serv,
265 (struct sockaddr *)&from)) {
266 warn("server port mismatch");
271 tpacket("received", dp, n);
272 /* should verify client address */
273 dp->th_opcode = ntohs(dp->th_opcode);
274 dp->th_block = ntohs(dp->th_block);
275 if (dp->th_opcode == ERROR) {
276 printf("Error code %d: %s\n", dp->th_code,
281 if (dp->th_opcode == DATA) {
284 if (dp->th_block == block) {
285 break; /* have next packet */
287 /* On an error, try to synchronize
292 printf("discarded %d packets\n", j);
294 if (dp->th_block == (block-1)) {
295 goto send_ack; /* resend ack */
299 /* size = write(fd, dp->th_data, n - 4); */
300 size = writeit(file, &dp, n - 4, convert);
302 nak(errno + 100, (struct sockaddr *)&peer);
306 } while (size == SEGSIZE);
307 abort: /* ok to ack, since user */
308 ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
309 ap->th_block = htons((u_short)block);
310 (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peer,
312 write_behind(file, convert); /* flush last buffer */
316 printstats("Received", amount);
320 makerequest(int request, const char *name, struct tftphdr *tp, const char *mode)
324 tp->th_opcode = htons((u_short)request);
332 return (cp - (char *)tp);
339 { EUNDEF, "Undefined error code" },
340 { ENOTFOUND, "File not found" },
341 { EACCESS, "Access violation" },
342 { ENOSPACE, "Disk full or allocation exceeded" },
343 { EBADOP, "Illegal TFTP operation" },
344 { EBADID, "Unknown transfer ID" },
345 { EEXISTS, "File already exists" },
346 { ENOUSER, "No such user" },
351 * Send a nak packet (error message).
352 * Error code passed in is one of the
353 * standard TFTP codes, or a UNIX errno
357 nak(int error, struct sockaddr *peer)
363 tp = (struct tftphdr *)ackbuf;
364 tp->th_opcode = htons((u_short)ERROR);
365 tp->th_code = htons((u_short)error);
366 for (pe = errmsgs; pe->e_code >= 0; pe++)
367 if (pe->e_code == error)
369 if (pe->e_code < 0) {
370 pe->e_msg = strerror(error - 100);
371 tp->th_code = EUNDEF;
373 strcpy(tp->th_msg, pe->e_msg);
374 length = strlen(pe->e_msg) + 4;
376 tpacket("sent", tp, length);
377 if (sendto(f, ackbuf, length, 0, peer, peer->sa_len) != length)
382 tpacket(const char *s, struct tftphdr *tp, int n)
384 static const char *opcodes[] =
385 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
387 u_short op = ntohs(tp->th_opcode);
389 if (op < RRQ || op > ERROR)
390 printf("%s opcode=%x ", s, op);
392 printf("%s %s ", s, opcodes[op]);
398 file = cp = tp->th_stuff;
399 cp = strchr(cp, '\0');
400 printf("<file=%s, mode=%s>\n", file, cp + 1);
404 printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
408 printf("<block=%d>\n", ntohs(tp->th_block));
412 printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
417 struct timeval tstart;
418 struct timeval tstop;
424 (void)gettimeofday(&tstart, NULL);
431 (void)gettimeofday(&tstop, NULL);
435 printstats(const char *direction, unsigned long amount)
438 /* compute delta in 1/10's second units */
439 delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
440 ((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
441 delta = delta/10.; /* back to seconds */
442 printf("%s %lu bytes in %.1f seconds", direction, amount, delta);
444 printf(" [%.0f bits/sec]", (amount*8.)/delta);
449 timer(int sig __unused)
453 if (timeout >= maxtimeout) {
454 printf("Transfer timed out.\n");
455 longjmp(toplevel, -1);
458 longjmp(timeoutbuf, 1);
462 cmpport(struct sockaddr *sa, struct sockaddr *sb)
464 char a[NI_MAXSERV], b[NI_MAXSERV];
466 if (getnameinfo(sa, sa->sa_len, NULL, 0, a, sizeof(a), NI_NUMERICSERV))
468 if (getnameinfo(sb, sb->sa_len, NULL, 0, b, sizeof(b), NI_NUMERICSERV))
470 if (strcmp(a, b) != 0)