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"
52 * Send a file via the TFTP data session.
55 tftp_send(int peer, uint16_t *block, struct tftp_stats *ts)
58 int size, n_data, n_ack, try;
60 char sendbuffer[MAXPKTSIZE];
61 char recvbuffer[MAXPKTSIZE];
63 rp = (struct tftphdr *)recvbuffer;
67 if (debug&DEBUG_SIMPLE)
68 tftp_log(LOG_DEBUG, "Sending block %d", *block);
70 size = read_file(sendbuffer, segsize);
72 tftp_log(LOG_ERR, "read_file returned %d", size);
73 send_error(peer, errno + 100);
77 for (try = 0; ; try++) {
78 n_data = send_data(peer, *block, sendbuffer, size);
80 if (try == maxtimeouts) {
82 "Cannot send DATA packet #%d, "
87 "Cannot send DATA packet #%d, trying again",
92 n_ack = receive_packet(peer, recvbuffer,
93 MAXPKTSIZE, NULL, timeoutpacket);
95 if (n_ack == RP_TIMEOUT) {
96 if (try == maxtimeouts) {
98 "Timeout #%d send ACK %d "
99 "giving up", try, *block);
102 tftp_log(LOG_WARNING,
103 "Timeout #%d on ACK %d",
108 /* Either read failure or ERROR packet */
109 if (debug&DEBUG_SIMPLE)
110 tftp_log(LOG_ERR, "Aborting: %s",
114 if (rp->th_opcode == ACK) {
116 if (rp->th_block == *block) {
121 /* Re-synchronize with the other side */
122 (void) synchnet(peer);
123 if (rp->th_block == (*block - 1)) {
132 if (oldblock > *block) {
133 if (options[OPT_ROLLOVER].o_request == NULL) {
135 * "rollover" option not specified in
136 * tftp client. Default to rolling block
141 *block = atoi(options[OPT_ROLLOVER].o_request);
146 gettimeofday(&(ts->tstop), NULL);
147 } while (size == segsize);
153 * Receive a file via the TFTP data session.
155 * - It could be that the first block has already arrived while
156 * trying to figure out if we were receiving options or not. In
157 * that case it is passed to this function.
160 tftp_receive(int peer, uint16_t *block, struct tftp_stats *ts,
161 struct tftphdr *firstblock, size_t fb_size)
165 int n_data, n_ack, writesize, i, retry;
166 char recvbuffer[MAXPKTSIZE];
170 if (firstblock != NULL) {
171 writesize = write_file(firstblock->th_data, fb_size);
172 ts->amount += writesize;
174 n_ack = send_ack(peer, *block);
176 if (i == maxtimeouts) {
178 "Cannot send ACK packet #%d, "
179 "giving up", *block);
183 "Cannot send ACK packet #%d, trying again",
191 if (fb_size != segsize) {
192 gettimeofday(&(ts->tstop), NULL);
197 rp = (struct tftphdr *)recvbuffer;
201 if (oldblock > *block) {
202 if (options[OPT_ROLLOVER].o_request == NULL) {
204 * "rollover" option not specified in
205 * tftp client. Default to rolling block
210 *block = atoi(options[OPT_ROLLOVER].o_request);
216 for (retry = 0; ; retry++) {
217 if (debug&DEBUG_SIMPLE)
219 "Receiving DATA block %d", *block);
221 n_data = receive_packet(peer, recvbuffer,
222 MAXPKTSIZE, NULL, timeoutpacket);
224 if (retry == maxtimeouts) {
226 "Timeout #%d on DATA block %d, "
227 "giving up", retry, *block);
230 if (n_data == RP_TIMEOUT) {
231 tftp_log(LOG_WARNING,
232 "Timeout #%d on DATA block %d",
234 send_ack(peer, oldblock);
238 /* Either read failure or ERROR packet */
239 if (debug&DEBUG_SIMPLE)
240 tftp_log(LOG_DEBUG, "Aborting: %s",
241 rp_strerror(n_data));
244 if (rp->th_opcode == DATA) {
247 if (rp->th_block == *block)
250 tftp_log(LOG_WARNING,
251 "Expected DATA block %d, got block %d",
252 *block, rp->th_block);
254 /* Re-synchronize with the other side */
255 (void) synchnet(peer);
256 if (rp->th_block == (*block-1)) {
257 tftp_log(LOG_INFO, "Trying to sync");
260 goto send_ack; /* rexmit */
264 tftp_log(LOG_WARNING,
265 "Expected DATA block, got %s block",
266 packettype(rp->th_opcode));
271 writesize = write_file(rp->th_data, n_data);
272 ts->amount += writesize;
273 if (writesize <= 0) {
275 "write_file returned %d", writesize);
277 send_error(peer, errno + 100);
279 send_error(peer, ENOSPACE);
286 n_ack = send_ack(peer, *block);
289 if (i == maxtimeouts) {
291 "Cannot send ACK packet #%d, "
292 "giving up", *block);
297 "Cannot send ACK packet #%d, trying again",
304 gettimeofday(&(ts->tstop), NULL);
305 } while (n_data == segsize);
307 /* Don't do late packet management for the client implementation */
308 if (acting_as_client)
312 n_data = receive_packet(peer, (char *)rp, pktsize,
313 NULL, timeoutpacket);
317 rp->th_opcode == DATA && /* and got a data block */
318 *block == rp->th_block) /* then my last ack was lost */
319 send_ack(peer, *block); /* resend final ack */