2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
42 * TFTP User Program -- Protocol Machines
44 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #include <arpa/tftp.h>
60 #include "tftp-file.h"
61 #include "tftp-utils.h"
63 #include "tftp-transfer.h"
64 #include "tftp-options.h"
67 * Send the requested file.
70 xmitfile(int peer, char *port, int fd, char *name, char *mode)
75 struct sockaddr_storage serv; /* valid server port number */
76 char recvbuffer[MAXPKTSIZE];
77 struct tftp_stats tftp_stats;
79 stats_init(&tftp_stats);
81 memset(&serv, 0, sizeof(serv));
82 rp = (struct tftphdr *)recvbuffer;
86 se = getservbyname("tftp", "udp");
88 ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port;
90 ((struct sockaddr_in *)&peer_sock)->sin_port =
93 for (i = 0; i < 12; i++) {
94 struct sockaddr_storage from;
96 /* Tell the other side what we want to do */
97 if (debug&DEBUG_SIMPLE)
98 printf("Sending %s\n", name);
100 n = send_wrq(peer, name, mode);
102 printf("Cannot send WRQ packet\n");
107 * The first packet we receive has the new destination port
108 * we have to send the next packets to.
110 n = receive_packet(peer, recvbuffer,
111 MAXPKTSIZE, &from, timeoutpacket);
113 /* We got some data! */
115 ((struct sockaddr_in *)&peer_sock)->sin_port =
116 ((struct sockaddr_in *)&from)->sin_port;
120 /* This should be retried */
121 if (n == RP_TIMEOUT) {
122 printf("Try %d, didn't receive answer from remote.\n",
127 /* Everything else is fatal */
131 printf("Transfer timed out.\n");
134 if (rp->th_opcode == ERROR) {
135 printf("Got ERROR, aborted\n");
140 * If the first packet is an OACK instead of an ACK packet,
141 * handle it different.
143 if (rp->th_opcode == OACK) {
144 if (!options_rfc_enabled) {
145 printf("Got OACK while options are not enabled!\n");
146 send_error(peer, EBADOP);
150 parse_options(peer, rp->th_stuff, n + 2);
153 if (read_init(fd, NULL, mode) < 0) {
159 tftp_send(peer, &block, &tftp_stats);
162 if (tftp_stats.amount > 0)
163 printstats("Sent", verbose, &tftp_stats);
172 recvfile(int peer, char *port, int fd, char *name, char *mode)
176 char recvbuffer[MAXPKTSIZE];
178 struct tftp_stats tftp_stats;
180 stats_init(&tftp_stats);
182 rp = (struct tftphdr *)recvbuffer;
186 se = getservbyname("tftp", "udp");
188 ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port;
190 ((struct sockaddr_in *)&peer_sock)->sin_port =
193 for (i = 0; i < 12; i++) {
194 struct sockaddr_storage from;
196 /* Tell the other side what we want to do */
197 if (debug&DEBUG_SIMPLE)
198 printf("Requesting %s\n", name);
200 n = send_rrq(peer, name, mode);
202 printf("Cannot send RRQ packet\n");
207 * The first packet we receive has the new destination port
208 * we have to send the next packets to.
210 n = receive_packet(peer, recvbuffer,
211 MAXPKTSIZE, &from, timeoutpacket);
213 /* We got something useful! */
215 ((struct sockaddr_in *)&peer_sock)->sin_port =
216 ((struct sockaddr_in *)&from)->sin_port;
220 /* We should retry if this happens */
221 if (n == RP_TIMEOUT) {
222 printf("Try %d, didn't receive answer from remote.\n",
227 /* Otherwise it is a fatal error */
231 printf("Transfer timed out.\n");
234 if (rp->th_opcode == ERROR) {
235 tftp_log(LOG_ERR, "Error code %d: %s", rp->th_code, rp->th_msg);
239 if (write_init(fd, NULL, mode) < 0) {
245 * If the first packet is an OACK packet instead of an DATA packet,
246 * handle it different.
248 if (rp->th_opcode == OACK) {
249 if (!options_rfc_enabled) {
250 printf("Got OACK while options are not enabled!\n");
251 send_error(peer, EBADOP);
255 parse_options(peer, rp->th_stuff, n + 2);
257 n = send_ack(peer, 0);
259 printf("Cannot send ACK on OACK.\n");
263 tftp_receive(peer, &block, &tftp_stats, NULL, 0);
266 tftp_receive(peer, &block, &tftp_stats, rp, n);
269 if (tftp_stats.amount > 0)
270 printstats("Received", verbose, &tftp_stats);