2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
44 * TFTP User Program -- Protocol Machines
46 #include <sys/socket.h>
49 #include <netinet/in.h>
51 #include <arpa/tftp.h>
62 #include "tftp-file.h"
63 #include "tftp-utils.h"
65 #include "tftp-transfer.h"
66 #include "tftp-options.h"
69 * Send the requested file.
72 xmitfile(int peer, char *port, int fd, char *name, char *mode)
77 struct sockaddr_storage serv; /* valid server port number */
78 char recvbuffer[MAXPKTSIZE];
79 struct tftp_stats tftp_stats;
81 stats_init(&tftp_stats);
83 memset(&serv, 0, sizeof(serv));
84 rp = (struct tftphdr *)recvbuffer;
88 se = getservbyname("tftp", "udp");
90 ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port;
92 ((struct sockaddr_in *)&peer_sock)->sin_port =
95 for (i = 0; i < 12; i++) {
96 struct sockaddr_storage from;
98 /* Tell the other side what we want to do */
99 if (debug&DEBUG_SIMPLE)
100 printf("Sending %s\n", name);
102 n = send_wrq(peer, name, mode);
104 printf("Cannot send WRQ packet\n");
109 * The first packet we receive has the new destination port
110 * we have to send the next packets to.
112 n = receive_packet(peer, recvbuffer,
113 MAXPKTSIZE, &from, timeoutpacket);
115 /* We got some data! */
117 ((struct sockaddr_in *)&peer_sock)->sin_port =
118 ((struct sockaddr_in *)&from)->sin_port;
122 /* This should be retried */
123 if (n == RP_TIMEOUT) {
124 printf("Try %d, didn't receive answer from remote.\n",
129 /* Everything else is fatal */
133 printf("Transfer timed out.\n");
136 if (rp->th_opcode == ERROR) {
137 printf("Got ERROR, aborted\n");
142 * If the first packet is an OACK instead of an ACK packet,
143 * handle it different.
145 if (rp->th_opcode == OACK) {
146 if (!options_rfc_enabled) {
147 printf("Got OACK while options are not enabled!\n");
148 send_error(peer, EBADOP);
152 parse_options(peer, rp->th_stuff, n + 2);
155 if (read_init(fd, NULL, mode) < 0) {
161 tftp_send(peer, &block, &tftp_stats);
164 if (tftp_stats.amount > 0)
165 printstats("Sent", verbose, &tftp_stats);
174 recvfile(int peer, char *port, int fd, char *name, char *mode)
178 char recvbuffer[MAXPKTSIZE];
180 struct tftp_stats tftp_stats;
182 stats_init(&tftp_stats);
184 rp = (struct tftphdr *)recvbuffer;
188 se = getservbyname("tftp", "udp");
190 ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port;
192 ((struct sockaddr_in *)&peer_sock)->sin_port =
195 for (i = 0; i < 12; i++) {
196 struct sockaddr_storage from;
198 /* Tell the other side what we want to do */
199 if (debug&DEBUG_SIMPLE)
200 printf("Requesting %s\n", name);
202 n = send_rrq(peer, name, mode);
204 printf("Cannot send RRQ packet\n");
209 * The first packet we receive has the new destination port
210 * we have to send the next packets to.
212 n = receive_packet(peer, recvbuffer,
213 MAXPKTSIZE, &from, timeoutpacket);
215 /* We got something useful! */
217 ((struct sockaddr_in *)&peer_sock)->sin_port =
218 ((struct sockaddr_in *)&from)->sin_port;
222 /* We should retry if this happens */
223 if (n == RP_TIMEOUT) {
224 printf("Try %d, didn't receive answer from remote.\n",
229 /* Otherwise it is a fatal error */
233 printf("Transfer timed out.\n");
236 if (rp->th_opcode == ERROR) {
237 tftp_log(LOG_ERR, "Error code %d: %s", rp->th_code, rp->th_msg);
241 if (write_init(fd, NULL, mode) < 0) {
247 * If the first packet is an OACK packet instead of an DATA packet,
248 * handle it different.
250 if (rp->th_opcode == OACK) {
251 if (!options_rfc_enabled) {
252 printf("Got OACK while options are not enabled!\n");
253 send_error(peer, EBADOP);
257 parse_options(peer, rp->th_stuff, n + 2);
259 n = send_ack(peer, 0);
261 printf("Cannot send ACK on OACK.\n");
265 tftp_receive(peer, &block, &tftp_stats, NULL, 0);
268 tftp_receive(peer, &block, &tftp_stats, rp, n);
271 if (tftp_stats.amount > 0)
272 printstats("Received", verbose, &tftp_stats);