2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/tftp.h>
43 #include "tftp-file.h"
45 #include "tftp-utils.h"
46 #include "tftp-options.h"
47 #include "tftp-transfer.h"
56 * Send a file via the TFTP data session.
59 tftp_send(int peer, uint16_t *block, struct tftp_stats *ts)
62 int size, n_data, n_ack, sendtry, acktry;
64 uint16_t oldblock, windowblock;
65 char sendbuffer[MAXPKTSIZE];
66 char recvbuffer[MAXPKTSIZE];
67 struct block_data window[WINDOWSIZE_MAX];
69 rp = (struct tftphdr *)recvbuffer;
76 if (debug & DEBUG_SIMPLE)
77 tftp_log(LOG_DEBUG, "Sending block %d (window block %d)",
80 window[windowblock].offset = tell_file();
81 window[windowblock].block = *block;
82 size = read_file(sendbuffer, segsize);
84 tftp_log(LOG_ERR, "read_file returned %d", size);
85 send_error(peer, errno + 100);
88 window[windowblock].size = size;
91 for (sendtry = 0; ; sendtry++) {
92 n_data = send_data(peer, *block, sendbuffer, size);
96 if (sendtry == maxtimeouts) {
98 "Cannot send DATA packet #%d, "
103 "Cannot send DATA packet #%d, trying again",
107 /* Only check for ACK for last block in window. */
108 if (windowblock == windowsize || size != segsize) {
109 n_ack = receive_packet(peer, recvbuffer,
110 MAXPKTSIZE, NULL, timeoutpacket);
112 if (n_ack == RP_TIMEOUT) {
113 if (acktry == maxtimeouts) {
115 "Timeout #%d send ACK %d "
116 "giving up", acktry, *block);
119 tftp_log(LOG_WARNING,
120 "Timeout #%d on ACK %d",
125 if (seek_file(window[0].offset) != 0) {
127 "seek_file failed: %s",
129 send_error(peer, errno + 100);
132 *block = window[0].block;
137 /* Either read failure or ERROR packet */
138 if (debug & DEBUG_SIMPLE)
139 tftp_log(LOG_ERR, "Aborting: %s",
143 if (rp->th_opcode == ACK) {
145 * Look for the ACKed block in our open
148 for (i = 0; i < windowblock; i++) {
149 if (rp->th_block == window[i].block)
153 if (i == windowblock) {
154 /* Did not recognize ACK. */
155 if (debug & DEBUG_SIMPLE)
157 "ACK %d out of window",
160 /* Re-synchronize with the other side */
161 (void) synchnet(peer);
163 /* Resend the current window. */
165 if (seek_file(window[0].offset) != 0) {
167 "seek_file failed: %s",
169 send_error(peer, errno + 100);
172 *block = window[0].block;
177 /* ACKed at least some data. */
179 for (j = 0; j <= i; j++) {
180 if (debug & DEBUG_SIMPLE)
185 ts->amount += window[j].size;
189 * Partial ACK. Rewind state to first
192 if (i + 1 != windowblock) {
193 if (debug & DEBUG_SIMPLE)
196 if (seek_file(window[i + 1].offset) !=
199 "seek_file failed: %s",
201 send_error(peer, errno + 100);
204 *block = window[i + 1].block;
216 if (oldblock > *block) {
217 if (options[OPT_ROLLOVER].o_request == NULL) {
219 * "rollover" option not specified in
220 * tftp client. Default to rolling block
225 *block = atoi(options[OPT_ROLLOVER].o_request);
230 gettimeofday(&(ts->tstop), NULL);
231 } while (size == segsize);
236 * Receive a file via the TFTP data session.
238 * - It could be that the first block has already arrived while
239 * trying to figure out if we were receiving options or not. In
240 * that case it is passed to this function.
243 tftp_receive(int peer, uint16_t *block, struct tftp_stats *ts,
244 struct tftphdr *firstblock, size_t fb_size)
247 uint16_t oldblock, windowstart;
248 int n_data, n_ack, writesize, i, retry, windowblock;
249 char recvbuffer[MAXPKTSIZE];
254 if (firstblock != NULL) {
255 writesize = write_file(firstblock->th_data, fb_size);
256 ts->amount += writesize;
259 if (windowsize == 1 || fb_size != segsize) {
261 n_ack = send_ack(peer, *block);
263 if (i == maxtimeouts) {
265 "Cannot send ACK packet #%d, "
266 "giving up", *block);
270 "Cannot send ACK packet #%d, trying again",
279 if (fb_size != segsize) {
281 gettimeofday(&(ts->tstop), NULL);
286 rp = (struct tftphdr *)recvbuffer;
290 if (oldblock > *block) {
291 if (options[OPT_ROLLOVER].o_request == NULL) {
293 * "rollover" option not specified in
294 * tftp client. Default to rolling block
299 *block = atoi(options[OPT_ROLLOVER].o_request);
305 for (retry = 0; ; retry++) {
306 if (debug & DEBUG_SIMPLE)
308 "Receiving DATA block %d (window block %d)",
309 *block, windowblock);
311 n_data = receive_packet(peer, recvbuffer,
312 MAXPKTSIZE, NULL, timeoutpacket);
314 if (retry == maxtimeouts) {
316 "Timeout #%d on DATA block %d, "
317 "giving up", retry, *block);
320 if (n_data == RP_TIMEOUT) {
321 tftp_log(LOG_WARNING,
322 "Timeout #%d on DATA block %d",
324 send_ack(peer, oldblock);
329 /* Either read failure or ERROR packet */
330 if (debug & DEBUG_SIMPLE)
331 tftp_log(LOG_DEBUG, "Aborting: %s",
332 rp_strerror(n_data));
335 if (rp->th_opcode == DATA) {
338 if (rp->th_block == *block)
342 * Ignore duplicate blocks within the
345 * This does not handle duplicate
346 * blocks during a rollover as
347 * gracefully, but that should still
348 * recover eventually.
350 if (*block > windowsize)
351 windowstart = *block - windowsize;
354 if (rp->th_block > windowstart &&
355 rp->th_block < *block) {
356 if (debug & DEBUG_SIMPLE)
358 "Ignoring duplicate DATA block %d",
365 tftp_log(LOG_WARNING,
366 "Expected DATA block %d, got block %d",
367 *block, rp->th_block);
369 /* Re-synchronize with the other side */
370 (void) synchnet(peer);
372 tftp_log(LOG_INFO, "Trying to sync");
375 goto send_ack; /* rexmit */
378 tftp_log(LOG_WARNING,
379 "Expected DATA block, got %s block",
380 packettype(rp->th_opcode));
385 writesize = write_file(rp->th_data, n_data);
386 ts->amount += writesize;
387 if (writesize <= 0) {
389 "write_file returned %d", writesize);
391 send_error(peer, errno + 100);
393 send_error(peer, ENOSPACE);
397 if (n_data != segsize)
401 /* Only send ACKs for the last block in the window. */
402 if (windowblock < windowsize && n_data == segsize)
406 n_ack = send_ack(peer, *block);
409 if (i == maxtimeouts) {
411 "Cannot send ACK packet #%d, "
412 "giving up", *block);
417 "Cannot send ACK packet #%d, trying again",
422 if (debug & DEBUG_SIMPLE)
423 tftp_log(LOG_DEBUG, "Sent ACK for %d", *block);
427 gettimeofday(&(ts->tstop), NULL);
428 } while (n_data == segsize);
430 /* Don't do late packet management for the client implementation */
431 if (acting_as_client)
435 n_data = receive_packet(peer, (char *)rp, pktsize,
436 NULL, -timeoutpacket);
440 rp->th_opcode == DATA && /* and got a data block */
441 *block == rp->th_block) /* then my last ack was lost */
442 send_ack(peer, *block); /* resend final ack */