2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/tftp.h>
45 #include "tftp-file.h"
47 #include "tftp-utils.h"
48 #include "tftp-options.h"
49 #include "tftp-transfer.h"
58 * Send a file via the TFTP data session.
61 tftp_send(int peer, uint16_t *block, struct tftp_stats *ts)
64 int size, n_data, n_ack, sendtry, acktry;
66 uint16_t oldblock, windowblock;
67 char sendbuffer[MAXPKTSIZE];
68 char recvbuffer[MAXPKTSIZE];
69 struct block_data window[WINDOWSIZE_MAX];
71 rp = (struct tftphdr *)recvbuffer;
78 if (debug&DEBUG_SIMPLE)
79 tftp_log(LOG_DEBUG, "Sending block %d (window block %d)",
82 window[windowblock].offset = tell_file();
83 window[windowblock].block = *block;
84 size = read_file(sendbuffer, segsize);
86 tftp_log(LOG_ERR, "read_file returned %d", size);
87 send_error(peer, errno + 100);
90 window[windowblock].size = size;
93 for (sendtry = 0; ; sendtry++) {
94 n_data = send_data(peer, *block, sendbuffer, size);
98 if (sendtry == maxtimeouts) {
100 "Cannot send DATA packet #%d, "
101 "giving up", *block);
105 "Cannot send DATA packet #%d, trying again",
109 /* Only check for ACK for last block in window. */
110 if (windowblock == windowsize || size != segsize) {
111 n_ack = receive_packet(peer, recvbuffer,
112 MAXPKTSIZE, NULL, timeoutpacket);
114 if (n_ack == RP_TIMEOUT) {
115 if (acktry == maxtimeouts) {
117 "Timeout #%d send ACK %d "
118 "giving up", acktry, *block);
121 tftp_log(LOG_WARNING,
122 "Timeout #%d on ACK %d",
127 seek_file(window[0].offset);
128 *block = window[0].block;
133 /* Either read failure or ERROR packet */
134 if (debug&DEBUG_SIMPLE)
135 tftp_log(LOG_ERR, "Aborting: %s",
139 if (rp->th_opcode == ACK) {
141 * Look for the ACKed block in our open
144 for (i = 0; i < windowblock; i++) {
145 if (rp->th_block == window[i].block)
149 if (i == windowblock) {
150 /* Did not recognize ACK. */
151 if (debug&DEBUG_SIMPLE)
153 "ACK %d out of window",
156 /* Re-synchronize with the other side */
157 (void) synchnet(peer);
159 /* Resend the current window. */
161 seek_file(window[0].offset);
162 *block = window[0].block;
167 /* ACKed at least some data. */
169 for (j = 0; j <= i; j++) {
170 if (debug&DEBUG_SIMPLE)
175 ts->amount += window[j].size;
179 * Partial ACK. Rewind state to first
182 if (i + 1 != windowblock) {
183 if (debug&DEBUG_SIMPLE)
186 seek_file(window[i + 1].offset);
187 *block = window[i + 1].block;
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);
213 gettimeofday(&(ts->tstop), NULL);
214 } while (size == segsize);
220 * Receive a file via the TFTP data session.
222 * - It could be that the first block has already arrived while
223 * trying to figure out if we were receiving options or not. In
224 * that case it is passed to this function.
227 tftp_receive(int peer, uint16_t *block, struct tftp_stats *ts,
228 struct tftphdr *firstblock, size_t fb_size)
231 uint16_t oldblock, windowstart;
232 int n_data, n_ack, writesize, i, retry, windowblock;
233 char recvbuffer[MAXPKTSIZE];
238 if (firstblock != NULL) {
239 writesize = write_file(firstblock->th_data, fb_size);
240 ts->amount += writesize;
242 if (windowsize == 1 || fb_size != segsize) {
244 n_ack = send_ack(peer, *block);
246 if (i == maxtimeouts) {
248 "Cannot send ACK packet #%d, "
249 "giving up", *block);
253 "Cannot send ACK packet #%d, trying again",
262 if (fb_size != segsize) {
263 gettimeofday(&(ts->tstop), NULL);
268 rp = (struct tftphdr *)recvbuffer;
272 if (oldblock > *block) {
273 if (options[OPT_ROLLOVER].o_request == NULL) {
275 * "rollover" option not specified in
276 * tftp client. Default to rolling block
281 *block = atoi(options[OPT_ROLLOVER].o_request);
287 for (retry = 0; ; retry++) {
288 if (debug&DEBUG_SIMPLE)
290 "Receiving DATA block %d (window block %d)",
291 *block, windowblock);
293 n_data = receive_packet(peer, recvbuffer,
294 MAXPKTSIZE, NULL, timeoutpacket);
296 if (retry == maxtimeouts) {
298 "Timeout #%d on DATA block %d, "
299 "giving up", retry, *block);
302 if (n_data == RP_TIMEOUT) {
303 tftp_log(LOG_WARNING,
304 "Timeout #%d on DATA block %d",
306 send_ack(peer, oldblock);
311 /* Either read failure or ERROR packet */
312 if (debug&DEBUG_SIMPLE)
313 tftp_log(LOG_DEBUG, "Aborting: %s",
314 rp_strerror(n_data));
317 if (rp->th_opcode == DATA) {
320 if (rp->th_block == *block)
324 * Ignore duplicate blocks within the
327 * This does not handle duplicate
328 * blocks during a rollover as
329 * gracefully, but that should still
330 * recover eventually.
332 if (*block > windowsize)
333 windowstart = *block - windowsize;
336 if (rp->th_block > windowstart &&
337 rp->th_block < *block) {
338 if (debug&DEBUG_SIMPLE)
340 "Ignoring duplicate DATA block %d",
347 tftp_log(LOG_WARNING,
348 "Expected DATA block %d, got block %d",
349 *block, rp->th_block);
351 /* Re-synchronize with the other side */
352 (void) synchnet(peer);
354 tftp_log(LOG_INFO, "Trying to sync");
357 goto send_ack; /* rexmit */
360 tftp_log(LOG_WARNING,
361 "Expected DATA block, got %s block",
362 packettype(rp->th_opcode));
367 writesize = write_file(rp->th_data, n_data);
368 ts->amount += writesize;
369 if (writesize <= 0) {
371 "write_file returned %d", writesize);
373 send_error(peer, errno + 100);
375 send_error(peer, ENOSPACE);
378 if (n_data != segsize)
383 /* Only send ACKs for the last block in the window. */
384 if (windowblock < windowsize && n_data == segsize)
388 n_ack = send_ack(peer, *block);
391 if (i == maxtimeouts) {
393 "Cannot send ACK packet #%d, "
394 "giving up", *block);
399 "Cannot send ACK packet #%d, trying again",
404 if (debug&DEBUG_SIMPLE)
405 tftp_log(LOG_DEBUG, "Sent ACK for %d", *block);
409 gettimeofday(&(ts->tstop), NULL);
410 } while (n_data == segsize);
412 /* Don't do late packet management for the client implementation */
413 if (acting_as_client)
417 n_data = receive_packet(peer, (char *)rp, pktsize,
418 NULL, timeoutpacket);
422 rp->th_opcode == DATA && /* and got a data block */
423 *block == rp->th_block) /* then my last ack was lost */
424 send_ack(peer, *block); /* resend final ack */