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
36 static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
46 * TFTP User Program -- Protocol Machines
48 #include <sys/types.h>
49 #include <sys/socket.h>
52 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <arpa/tftp.h>
69 extern struct sockaddr_storage peeraddr; /* filled in by main */
70 extern int f; /* the opened socket */
74 extern int maxtimeout;
75 extern volatile int txrx_error;
77 #define PKTSIZE SEGSIZE+4
83 static void nak(int, const struct sockaddr *);
84 static int makerequest(int, const char *, struct tftphdr *, const char *);
85 static void printstats(const char *, unsigned long);
86 static void startclock(void);
87 static void stopclock(void);
88 static void timer(int);
89 static void tpacket(const char *, struct tftphdr *, int);
90 static int cmpport(const struct sockaddr *, const struct sockaddr *);
93 * Send the requested file.
96 xmitfile(int fd, const char *name, const char *mode)
98 struct tftphdr *ap; /* data and ack packets */
101 volatile unsigned short block;
102 volatile int size, convert;
103 volatile unsigned long amount;
104 struct sockaddr_storage from;
107 struct sockaddr_storage peer;
108 struct sockaddr_storage serv; /* valid server port number */
110 startclock(); /* start stat's clock */
111 dp = r_init(); /* reset fillbuf/read-ahead code */
112 ap = (struct tftphdr *)ackbuf;
113 file = fdopen(fd, "r");
114 convert = !strcmp(mode, "netascii");
117 memcpy(&peer, &peeraddr, peeraddr.ss_len);
118 memset(&serv, 0, sizeof(serv));
120 signal(SIGALRM, timer);
123 size = makerequest(WRQ, name, dp, mode) - 4;
125 /* size = read(fd, dp->th_data, SEGSIZE); */
126 size = readit(file, &dp, convert);
128 nak(errno + 100, (struct sockaddr *)&peer);
131 dp->th_opcode = htons((u_short)DATA);
132 dp->th_block = htons((u_short)block);
135 (void) setjmp(timeoutbuf);
138 tpacket("sent", dp, size + 4);
139 n = sendto(f, dp, size + 4, 0,
140 (struct sockaddr *)&peer, peer.ss_len);
146 read_ahead(file, convert);
150 fromlen = sizeof(from);
151 n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
152 (struct sockaddr *)&from, &fromlen);
162 else if (!cmpport((struct sockaddr *)&serv,
163 (struct sockaddr *)&from)) {
164 warn("server port mismatch");
170 tpacket("received", ap, n);
171 /* should verify packet came from server */
172 ap->th_opcode = ntohs(ap->th_opcode);
173 ap->th_block = ntohs(ap->th_block);
174 if (ap->th_opcode == ERROR) {
175 printf("Error code %d: %s\n", ap->th_code,
180 if (ap->th_opcode == ACK) {
183 if (ap->th_block == block) {
186 /* On an error, try to synchronize
191 printf("discarded %d packets\n",
194 if (ap->th_block == (block-1)) {
202 } while (size == SEGSIZE || block == 1);
207 printstats("Sent", amount);
214 recvfile(int fd, const char *name, const char *mode)
219 volatile unsigned short block;
220 volatile int size, firsttrip;
221 volatile unsigned long amount;
222 struct sockaddr_storage from;
225 volatile int convert; /* true if converting crlf -> lf */
226 struct sockaddr_storage peer;
227 struct sockaddr_storage serv; /* valid server port number */
231 ap = (struct tftphdr *)ackbuf;
232 file = fdopen(fd, "w");
233 convert = !strcmp(mode, "netascii");
237 memcpy(&peer, &peeraddr, peeraddr.ss_len);
238 memset(&serv, 0, sizeof(serv));
240 signal(SIGALRM, timer);
243 size = makerequest(RRQ, name, ap, mode);
246 ap->th_opcode = htons((u_short)ACK);
247 ap->th_block = htons((u_short)(block));
252 (void) setjmp(timeoutbuf);
255 tpacket("sent", ap, size);
256 if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peer,
257 peer.ss_len) != size) {
263 write_behind(file, convert);
267 fromlen = sizeof(from);
268 n = recvfrom(f, dp, PKTSIZE, 0,
269 (struct sockaddr *)&from, &fromlen);
279 else if (!cmpport((struct sockaddr *)&serv,
280 (struct sockaddr *)&from)) {
281 warn("server port mismatch");
287 tpacket("received", dp, n);
288 /* should verify client address */
289 dp->th_opcode = ntohs(dp->th_opcode);
290 dp->th_block = ntohs(dp->th_block);
291 if (dp->th_opcode == ERROR) {
292 printf("Error code %d: %s\n", dp->th_code,
297 if (dp->th_opcode == DATA) {
300 if (dp->th_block == block) {
301 break; /* have next packet */
303 /* On an error, try to synchronize
308 printf("discarded %d packets\n", j);
310 if (dp->th_block == (block-1)) {
311 goto send_ack; /* resend ack */
315 /* size = write(fd, dp->th_data, n - 4); */
316 size = writeit(file, &dp, n - 4, convert);
318 nak(errno + 100, (struct sockaddr *)&peer);
322 } while (size == SEGSIZE);
323 abort: /* ok to ack, since user */
324 ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
325 ap->th_block = htons((u_short)block);
326 (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peer,
328 write_behind(file, convert); /* flush last buffer */
332 printstats("Received", amount);
336 makerequest(int request, const char *name, struct tftphdr *tp, const char *mode)
340 tp->th_opcode = htons((u_short)request);
348 return (cp - (char *)tp);
355 { EUNDEF, "Undefined error code" },
356 { ENOTFOUND, "File not found" },
357 { EACCESS, "Access violation" },
358 { ENOSPACE, "Disk full or allocation exceeded" },
359 { EBADOP, "Illegal TFTP operation" },
360 { EBADID, "Unknown transfer ID" },
361 { EEXISTS, "File already exists" },
362 { ENOUSER, "No such user" },
367 * Send a nak packet (error message).
368 * Error code passed in is one of the
369 * standard TFTP codes, or a UNIX errno
373 nak(int error, const struct sockaddr *peer)
379 tp = (struct tftphdr *)ackbuf;
380 tp->th_opcode = htons((u_short)ERROR);
381 tp->th_code = htons((u_short)error);
382 for (pe = errmsgs; pe->e_code >= 0; pe++)
383 if (pe->e_code == error)
385 if (pe->e_code < 0) {
386 pe->e_msg = strerror(error - 100);
387 tp->th_code = EUNDEF;
389 strcpy(tp->th_msg, pe->e_msg);
390 length = strlen(pe->e_msg) + 4;
392 tpacket("sent", tp, length);
393 if (sendto(f, ackbuf, length, 0, peer, peer->sa_len) != length)
398 tpacket(const char *s, struct tftphdr *tp, int n)
400 static const char *opcodes[] =
401 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
403 u_short op = ntohs(tp->th_opcode);
405 if (op < RRQ || op > ERROR)
406 printf("%s opcode=%x ", s, op);
408 printf("%s %s ", s, opcodes[op]);
414 file = cp = tp->th_stuff;
415 cp = index(cp, '\0');
416 printf("<file=%s, mode=%s>\n", file, cp + 1);
420 printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
424 printf("<block=%d>\n", ntohs(tp->th_block));
428 printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
433 struct timeval tstart;
434 struct timeval tstop;
440 (void)gettimeofday(&tstart, NULL);
447 (void)gettimeofday(&tstop, NULL);
451 printstats(const char *direction, unsigned long amount)
454 /* compute delta in 1/10's second units */
455 delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
456 ((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
457 delta = delta/10.; /* back to seconds */
458 printf("%s %ld bytes in %.1f seconds", direction, amount, delta);
460 printf(" [%.0f bits/sec]", (amount*8.)/delta);
465 timer(int sig __unused)
469 if (timeout >= maxtimeout) {
470 printf("Transfer timed out.\n");
471 longjmp(toplevel, -1);
474 longjmp(timeoutbuf, 1);
478 cmpport(const struct sockaddr *sa, const struct sockaddr *sb)
480 char a[NI_MAXSERV], b[NI_MAXSERV];
482 if (getnameinfo(sa, sa->sa_len, NULL, 0, a, sizeof(a), NI_NUMERICSERV))
484 if (getnameinfo(sb, sb->sa_len, NULL, 0, b, sizeof(b), NI_NUMERICSERV))
486 if (strcmp(a, b) != 0)