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";
38 static const char rcsid[] =
42 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
45 * TFTP User Program -- Protocol Machines
47 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <netinet/in.h>
53 #include <arpa/tftp.h>
66 extern struct sockaddr_in peeraddr; /* filled in by main */
67 extern int f; /* the opened socket */
71 extern int maxtimeout;
73 #define PKTSIZE SEGSIZE+4
79 static void nak __P((int));
80 static int makerequest __P((int, const char *, struct tftphdr *, const char *));
81 static void printstats __P((const char *, unsigned long));
82 static void startclock __P((void));
83 static void stopclock __P((void));
84 static void timer __P((int));
85 static void tpacket __P((const char *, struct tftphdr *, int));
88 * Send the requested file.
91 xmitfile(fd, name, mode)
96 register struct tftphdr *ap; /* data and ack packets */
97 struct tftphdr *r_init(), *dp;
99 volatile int block, size, convert;
100 volatile unsigned long amount;
101 struct sockaddr_in from;
105 startclock(); /* start stat's clock */
106 dp = r_init(); /* reset fillbuf/read-ahead code */
107 ap = (struct tftphdr *)ackbuf;
108 file = fdopen(fd, "r");
109 convert = !strcmp(mode, "netascii");
113 signal(SIGALRM, timer);
116 size = makerequest(WRQ, name, dp, mode) - 4;
118 /* size = read(fd, dp->th_data, SEGSIZE); */
119 size = readit(file, &dp, convert);
124 dp->th_opcode = htons((u_short)DATA);
125 dp->th_block = htons((u_short)block);
128 (void) setjmp(timeoutbuf);
131 tpacket("sent", dp, size + 4);
132 n = sendto(f, dp, size + 4, 0,
133 (struct sockaddr *)&peeraddr, sizeof(peeraddr));
138 read_ahead(file, convert);
142 fromlen = sizeof(from);
143 n = recvfrom(f, ackbuf, sizeof(ackbuf), 0,
144 (struct sockaddr *)&from, &fromlen);
151 peeraddr.sin_port = from.sin_port; /* added */
153 tpacket("received", ap, n);
154 /* should verify packet came from server */
155 ap->th_opcode = ntohs(ap->th_opcode);
156 ap->th_block = ntohs(ap->th_block);
157 if (ap->th_opcode == ERROR) {
158 printf("Error code %d: %s\n", ap->th_code,
162 if (ap->th_opcode == ACK) {
165 if (ap->th_block == block) {
168 /* On an error, try to synchronize
173 printf("discarded %d packets\n",
176 if (ap->th_block == (block-1)) {
184 } while (size == SEGSIZE || block == 1);
189 printstats("Sent", amount);
196 recvfile(fd, name, mode)
201 register struct tftphdr *ap;
202 struct tftphdr *dp, *w_init();
204 volatile int block, size, firsttrip;
205 volatile unsigned long amount;
206 struct sockaddr_in from;
209 volatile int convert; /* true if converting crlf -> lf */
213 ap = (struct tftphdr *)ackbuf;
214 file = fdopen(fd, "w");
215 convert = !strcmp(mode, "netascii");
220 signal(SIGALRM, timer);
223 size = makerequest(RRQ, name, ap, mode);
226 ap->th_opcode = htons((u_short)ACK);
227 ap->th_block = htons((u_short)(block));
232 (void) setjmp(timeoutbuf);
235 tpacket("sent", ap, size);
236 if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr,
237 sizeof(peeraddr)) != size) {
242 write_behind(file, convert);
246 fromlen = sizeof(from);
247 n = recvfrom(f, dp, PKTSIZE, 0,
248 (struct sockaddr *)&from, &fromlen);
255 peeraddr.sin_port = from.sin_port; /* added */
257 tpacket("received", dp, n);
258 /* should verify client address */
259 dp->th_opcode = ntohs(dp->th_opcode);
260 dp->th_block = ntohs(dp->th_block);
261 if (dp->th_opcode == ERROR) {
262 printf("Error code %d: %s\n", dp->th_code,
266 if (dp->th_opcode == DATA) {
269 if (dp->th_block == block) {
270 break; /* have next packet */
272 /* On an error, try to synchronize
277 printf("discarded %d packets\n", j);
279 if (dp->th_block == (block-1)) {
280 goto send_ack; /* resend ack */
284 /* size = write(fd, dp->th_data, n - 4); */
285 size = writeit(file, &dp, n - 4, convert);
291 } while (size == SEGSIZE);
292 abort: /* ok to ack, since user */
293 ap->th_opcode = htons((u_short)ACK); /* has seen err msg */
294 ap->th_block = htons((u_short)block);
295 (void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
297 write_behind(file, convert); /* flush last buffer */
301 printstats("Received", amount);
305 makerequest(request, name, tp, mode)
313 tp->th_opcode = htons((u_short)request);
321 return (cp - (char *)tp);
328 { EUNDEF, "Undefined error code" },
329 { ENOTFOUND, "File not found" },
330 { EACCESS, "Access violation" },
331 { ENOSPACE, "Disk full or allocation exceeded" },
332 { EBADOP, "Illegal TFTP operation" },
333 { EBADID, "Unknown transfer ID" },
334 { EEXISTS, "File already exists" },
335 { ENOUSER, "No such user" },
340 * Send a nak packet (error message).
341 * Error code passed in is one of the
342 * standard TFTP codes, or a UNIX errno
349 register struct errmsg *pe;
350 register struct tftphdr *tp;
354 tp = (struct tftphdr *)ackbuf;
355 tp->th_opcode = htons((u_short)ERROR);
356 tp->th_code = htons((u_short)error);
357 for (pe = errmsgs; pe->e_code >= 0; pe++)
358 if (pe->e_code == error)
360 if (pe->e_code < 0) {
361 pe->e_msg = strerror(error - 100);
362 tp->th_code = EUNDEF;
364 strcpy(tp->th_msg, pe->e_msg);
365 length = strlen(pe->e_msg) + 4;
367 tpacket("sent", tp, length);
368 if (sendto(f, ackbuf, length, 0, (struct sockaddr *)&peeraddr,
369 sizeof(peeraddr)) != length)
379 static char *opcodes[] =
380 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
381 register char *cp, *file;
382 u_short op = ntohs(tp->th_opcode);
385 if (op < RRQ || op > ERROR)
386 printf("%s opcode=%x ", s, op);
388 printf("%s %s ", s, opcodes[op]);
394 file = cp = tp->th_stuff;
395 cp = index(cp, '\0');
396 printf("<file=%s, mode=%s>\n", file, cp + 1);
400 printf("<block=%d, %d bytes>\n", ntohs(tp->th_block), n - 4);
404 printf("<block=%d>\n", ntohs(tp->th_block));
408 printf("<code=%d, msg=%s>\n", ntohs(tp->th_code), tp->th_msg);
413 struct timeval tstart;
414 struct timeval tstop;
420 (void)gettimeofday(&tstart, NULL);
427 (void)gettimeofday(&tstop, NULL);
431 printstats(direction, amount)
432 const char *direction;
433 unsigned long amount;
436 /* compute delta in 1/10's second units */
437 delta = ((tstop.tv_sec*10.)+(tstop.tv_usec/100000)) -
438 ((tstart.tv_sec*10.)+(tstart.tv_usec/100000));
439 delta = delta/10.; /* back to seconds */
440 printf("%s %d bytes in %.1f seconds", direction, amount, delta);
442 printf(" [%.0f bits/sec]", (amount*8.)/delta);
452 if (timeout >= maxtimeout) {
453 printf("Transfer timed out.\n");
454 longjmp(toplevel, -1);
456 longjmp(timeoutbuf, 1);