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
34 #include "common/tftpsubs.h"
37 * TFTP User Program -- Protocol Machines
41 extern struct sockaddr_in peeraddr
; /* filled in by main */
42 extern int f
; /* the opened socket */
46 extern int maxtimeout
;
48 #define PKTSIZE SEGSIZE+4
52 sigjmp_buf timeoutbuf
;
54 static void nak(int, const char *);
55 static int makerequest(int, const char *, struct tftphdr
*, const char *);
56 static void printstats(const char *, unsigned long);
57 static void startclock(void);
58 static void stopclock(void);
59 static void timer(int);
60 static void tpacket(const char *, struct tftphdr
*, int);
63 * Send the requested file.
65 void tftp_sendfile(int fd
, const char *name
, const char *mode
)
67 struct tftphdr
*ap
; /* data and ack packets */
70 volatile int is_request
;
71 volatile u_short block
;
72 volatile int size
, convert
;
73 volatile off_t amount
;
74 struct sockaddr_in from
;
77 u_short ap_opcode
, ap_block
;
79 startclock(); /* start stat's clock */
80 dp
= r_init(); /* reset fillbuf/read-ahead code */
81 ap
= (struct tftphdr
*)ackbuf
;
82 convert
= !strcmp(mode
, "netascii");
83 file
= fdopen(fd
, convert
? "rt" : "rb");
85 is_request
= 1; /* First packet is the actual WRQ */
88 bsd_signal(SIGALRM
, timer
);
91 size
= makerequest(WRQ
, name
, dp
, mode
) - 4;
93 /* size = read(fd, dp->th_data, SEGSIZE); */
94 size
= readit(file
, &dp
, convert
);
96 nak(errno
+ 100, NULL
);
99 dp
->th_opcode
= htons((u_short
) DATA
);
100 dp
->th_block
= htons((u_short
) block
);
103 (void)sigsetjmp(timeoutbuf
, 1);
106 tpacket("sent", dp
, size
+ 4);
107 n
= sendto(f
, dp
, size
+ 4, 0,
108 (struct sockaddr
*)&peeraddr
, sizeof(peeraddr
));
110 perror("tftp: sendto");
113 read_ahead(file
, convert
);
117 fromlen
= sizeof(from
);
118 n
= recvfrom(f
, ackbuf
, sizeof(ackbuf
), 0,
119 (struct sockaddr
*)&from
, &fromlen
);
123 perror("tftp: recvfrom");
126 peeraddr
.sin_port
= from
.sin_port
; /* added */
128 tpacket("received", ap
, n
);
129 /* should verify packet came from server */
130 ap_opcode
= ntohs((u_short
) ap
->th_opcode
);
131 ap_block
= ntohs((u_short
) ap
->th_block
);
132 if (ap_opcode
== ERROR
) {
133 printf("Error code %d: %s\n", ap_block
, ap
->th_msg
);
136 if (ap_opcode
== ACK
) {
139 if (ap_block
== block
) {
142 /* On an error, try to synchronize
147 printf("discarded %d packets\n", j
);
150 * RFC1129/RFC1350: We MUST NOT re-send the DATA
151 * packet in response to an invalid ACK. Doing so
152 * would cause the Sorcerer's Apprentice bug.
160 } while (size
== SEGSIZE
|| block
== 1);
165 printstats("Sent", amount
);
171 void tftp_recvfile(int fd
, const char *name
, const char *mode
)
176 volatile u_short block
;
177 volatile int size
, firsttrip
;
178 volatile unsigned long amount
;
179 struct sockaddr_in from
;
182 volatile int convert
; /* true if converting crlf -> lf */
183 u_short dp_opcode
, dp_block
;
187 ap
= (struct tftphdr
*)ackbuf
;
188 convert
= !strcmp(mode
, "netascii");
189 file
= fdopen(fd
, convert
? "wt" : "wb");
194 bsd_signal(SIGALRM
, timer
);
197 size
= makerequest(RRQ
, name
, ap
, mode
);
200 ap
->th_opcode
= htons((u_short
) ACK
);
201 ap
->th_block
= htons((u_short
) block
);
206 (void)sigsetjmp(timeoutbuf
, 1);
209 tpacket("sent", ap
, size
);
210 if (sendto(f
, ackbuf
, size
, 0, (struct sockaddr
*)&peeraddr
,
211 sizeof(peeraddr
)) != size
) {
213 perror("tftp: sendto");
216 write_behind(file
, convert
);
220 fromlen
= sizeof(from
);
221 n
= recvfrom(f
, dp
, PKTSIZE
, 0,
222 (struct sockaddr
*)&from
, &fromlen
);
226 perror("tftp: recvfrom");
229 peeraddr
.sin_port
= from
.sin_port
; /* added */
231 tpacket("received", dp
, n
);
232 /* should verify client address */
233 dp_opcode
= ntohs((u_short
) dp
->th_opcode
);
234 dp_block
= ntohs((u_short
) dp
->th_block
);
235 if (dp_opcode
== ERROR
) {
236 printf("Error code %d: %s\n", dp_block
, dp
->th_msg
);
239 if (dp_opcode
== DATA
) {
242 if (dp_block
== block
) {
243 break; /* have next packet */
245 /* On an error, try to synchronize
250 printf("discarded %d packets\n", j
);
252 if (dp_block
== (block
- 1)) {
253 goto send_ack
; /* resend ack */
257 /* size = write(fd, dp->th_data, n - 4); */
258 size
= writeit(file
, &dp
, n
- 4, convert
);
260 nak(errno
+ 100, NULL
);
264 } while (size
== SEGSIZE
);
265 abort
: /* ok to ack, since user */
266 ap
->th_opcode
= htons((u_short
) ACK
); /* has seen err msg */
267 ap
->th_block
= htons((u_short
) block
);
268 (void)sendto(f
, ackbuf
, 4, 0, (struct sockaddr
*)&peeraddr
,
270 write_behind(file
, convert
); /* flush last buffer */
274 printstats("Received", amount
);
278 makerequest(int request
, const char *name
,
279 struct tftphdr
*tp
, const char *mode
)
283 tp
->th_opcode
= htons((u_short
) request
);
284 cp
= (char *)&(tp
->th_stuff
);
291 return (cp
- (char *)tp
);
294 static const char *const errmsgs
[] = {
295 "Undefined error code", /* 0 - EUNDEF */
296 "File not found", /* 1 - ENOTFOUND */
297 "Access denied", /* 2 - EACCESS */
298 "Disk full or allocation exceeded", /* 3 - ENOSPACE */
299 "Illegal TFTP operation", /* 4 - EBADOP */
300 "Unknown transfer ID", /* 5 - EBADID */
301 "File already exists", /* 6 - EEXISTS */
302 "No such user", /* 7 - ENOUSER */
303 "Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
306 #define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
309 * Send a nak packet (error message).
310 * Error code passed in is one of the
311 * standard TFTP codes, or a UNIX errno
314 static void nak(int error
, const char *msg
)
319 tp
= (struct tftphdr
*)ackbuf
;
320 tp
->th_opcode
= htons((u_short
) ERROR
);
321 tp
->th_code
= htons((u_short
) error
);
324 /* This is a Unix errno+100 */
326 msg
= strerror(error
- 100);
329 if ((unsigned)error
>= ERR_CNT
)
333 msg
= errmsgs
[error
];
336 tp
->th_code
= htons((u_short
) error
);
338 length
= strlen(msg
) + 1;
339 memcpy(tp
->th_msg
, msg
, length
);
340 length
+= 4; /* Add space for header */
343 tpacket("sent", tp
, length
);
344 if (sendto(f
, ackbuf
, length
, 0, (struct sockaddr
*)&peeraddr
,
345 sizeof(peeraddr
)) != length
)
349 static void tpacket(const char *s
, struct tftphdr
*tp
, int n
)
351 static const char *opcodes
[] =
352 { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
354 u_short op
= ntohs((u_short
) tp
->th_opcode
);
356 if (op
< RRQ
|| op
> ERROR
)
357 printf("%s opcode=%x ", s
, op
);
359 printf("%s %s ", s
, opcodes
[op
]);
365 file
= cp
= (char *)&(tp
->th_stuff
);
366 cp
= strchr(cp
, '\0');
367 printf("<file=%s, mode=%s>\n", file
, cp
+ 1);
371 printf("<block=%d, %d bytes>\n", ntohs(tp
->th_block
), n
- 4);
375 printf("<block=%d>\n", ntohs(tp
->th_block
));
379 printf("<code=%d, msg=%s>\n", ntohs(tp
->th_code
), tp
->th_msg
);
384 struct timeval tstart
;
385 struct timeval tstop
;
387 static void startclock(void)
389 (void)gettimeofday(&tstart
, NULL
);
392 static void stopclock(void)
395 (void)gettimeofday(&tstop
, NULL
);
398 static void printstats(const char *direction
, unsigned long amount
)
402 delta
= (tstop
.tv_sec
+ (tstop
.tv_usec
/ 100000.0)) -
403 (tstart
.tv_sec
+ (tstart
.tv_usec
/ 100000.0));
405 printf("%s %lu bytes in %.1f seconds", direction
, amount
, delta
);
406 printf(" [%.0f bit/s]", (amount
* 8.) / delta
);
411 static void timer(int sig
)
413 int save_errno
= errno
;
415 (void)sig
; /* Shut up unused warning */
418 if (timeout
>= maxtimeout
) {
419 printf("Transfer timed out.\n");
421 siglongjmp(toplevel
, -1);
424 siglongjmp(timeoutbuf
, 1);