2 * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/tftp.h>
43 #include "tftp-file.h"
45 #include "tftp-utils.h"
46 #include "tftp-options.h"
47 #include "tftp-transfer.h"
50 * Send a file via the TFTP data session.
53 tftp_send(int peer, uint16_t *block, struct tftp_stats *ts)
56 int size, n_data, n_ack, try;
58 char sendbuffer[MAXPKTSIZE];
59 char recvbuffer[MAXPKTSIZE];
61 rp = (struct tftphdr *)recvbuffer;
65 if (debug&DEBUG_SIMPLE)
66 tftp_log(LOG_DEBUG, "Sending block %d", *block);
68 size = read_file(sendbuffer, segsize);
70 tftp_log(LOG_ERR, "read_file returned %d", size);
71 send_error(peer, errno + 100);
75 for (try = 0; ; try++) {
76 n_data = send_data(peer, *block, sendbuffer, size);
78 if (try == maxtimeouts) {
80 "Cannot send DATA packet #%d, "
85 "Cannot send DATA packet #%d, trying again",
90 n_ack = receive_packet(peer, recvbuffer,
91 MAXPKTSIZE, NULL, timeoutpacket);
93 if (n_ack == RP_TIMEOUT) {
94 if (try == maxtimeouts) {
96 "Timeout #%d send ACK %d "
97 "giving up", try, *block);
100 tftp_log(LOG_WARNING,
101 "Timeout #%d on ACK %d",
106 /* Either read failure or ERROR packet */
107 if (debug&DEBUG_SIMPLE)
108 tftp_log(LOG_ERR, "Aborting: %s",
112 if (rp->th_opcode == ACK) {
114 if (rp->th_block == *block) {
119 /* Re-synchronize with the other side */
120 (void) synchnet(peer);
121 if (rp->th_block == (*block - 1)) {
130 if (oldblock > *block) {
131 if (options[OPT_ROLLOVER].o_request == NULL) {
133 * "rollover" option not specified in
134 * tftp client. Default to rolling block
139 *block = atoi(options[OPT_ROLLOVER].o_request);
144 gettimeofday(&(ts->tstop), NULL);
145 } while (size == segsize);
151 * Receive a file via the TFTP data session.
153 * - It could be that the first block has already arrived while
154 * trying to figure out if we were receiving options or not. In
155 * that case it is passed to this function.
158 tftp_receive(int peer, uint16_t *block, struct tftp_stats *ts,
159 struct tftphdr *firstblock, size_t fb_size)
163 int n_data, n_ack, writesize, i, retry;
164 char recvbuffer[MAXPKTSIZE];
168 if (firstblock != NULL) {
169 writesize = write_file(firstblock->th_data, fb_size);
170 ts->amount += writesize;
172 n_ack = send_ack(peer, *block);
174 if (i == maxtimeouts) {
176 "Cannot send ACK packet #%d, "
177 "giving up", *block);
181 "Cannot send ACK packet #%d, trying again",
189 if (fb_size != segsize) {
190 gettimeofday(&(ts->tstop), NULL);
195 rp = (struct tftphdr *)recvbuffer;
199 if (oldblock > *block) {
200 if (options[OPT_ROLLOVER].o_request == NULL) {
202 * "rollover" option not specified in
203 * tftp client. Default to rolling block
208 *block = atoi(options[OPT_ROLLOVER].o_request);
214 for (retry = 0; ; retry++) {
215 if (debug&DEBUG_SIMPLE)
217 "Receiving DATA block %d", *block);
219 n_data = receive_packet(peer, recvbuffer,
220 MAXPKTSIZE, NULL, timeoutpacket);
222 if (retry == maxtimeouts) {
224 "Timeout #%d on DATA block %d, "
225 "giving up", retry, *block);
228 if (n_data == RP_TIMEOUT) {
229 tftp_log(LOG_WARNING,
230 "Timeout #%d on DATA block %d",
232 send_ack(peer, oldblock);
236 /* Either read failure or ERROR packet */
237 if (debug&DEBUG_SIMPLE)
238 tftp_log(LOG_DEBUG, "Aborting: %s",
239 rp_strerror(n_data));
242 if (rp->th_opcode == DATA) {
245 if (rp->th_block == *block)
248 tftp_log(LOG_WARNING,
249 "Expected DATA block %d, got block %d",
250 *block, rp->th_block);
252 /* Re-synchronize with the other side */
253 (void) synchnet(peer);
254 if (rp->th_block == (*block-1)) {
255 tftp_log(LOG_INFO, "Trying to sync");
258 goto send_ack; /* rexmit */
262 tftp_log(LOG_WARNING,
263 "Expected DATA block, got %s block",
264 packettype(rp->th_opcode));
269 writesize = write_file(rp->th_data, n_data);
270 ts->amount += writesize;
271 if (writesize <= 0) {
273 "write_file returned %d", writesize);
275 send_error(peer, errno + 100);
277 send_error(peer, ENOSPACE);
284 n_ack = send_ack(peer, *block);
287 if (i == maxtimeouts) {
289 "Cannot send ACK packet #%d, "
290 "giving up", *block);
295 "Cannot send ACK packet #%d, trying again",
302 gettimeofday(&(ts->tstop), NULL);
303 } while (n_data == segsize);
305 /* Don't do late packet management for the client implementation */
306 if (acting_as_client)
310 n_data = receive_packet(peer, (char *)rp, pktsize,
311 NULL, timeoutpacket);
315 rp->th_opcode == DATA && /* and got a data block */
316 *block == rp->th_block) /* then my last ack was lost */
317 send_ack(peer, *block); /* resend final ack */