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>
59 #include "tftp-file.h"
60 #include "tftp-utils.h"
62 #include "tftp-transfer.h"
63 #include "tftp-options.h"
66 * Send the requested file.
69 xmitfile(int peer, char *port, int fd, char *name, char *mode)
74 struct sockaddr_storage serv; /* valid server port number */
75 char recvbuffer[MAXPKTSIZE];
76 struct tftp_stats tftp_stats;
78 stats_init(&tftp_stats);
80 memset(&serv, 0, sizeof(serv));
81 rp = (struct tftphdr *)recvbuffer;
85 se = getservbyname("tftp", "udp");
86 ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port;
88 ((struct sockaddr_in *)&peer_sock)->sin_port =
91 for (i = 0; i < 12; i++) {
92 struct sockaddr_storage from;
94 /* Tell the other side what we want to do */
95 if (debug&DEBUG_SIMPLE)
96 printf("Sending %s\n", name);
98 n = send_wrq(peer, name, mode);
100 printf("Cannot send WRQ packet\n");
105 * The first packet we receive has the new destination port
106 * we have to send the next packets to.
108 n = receive_packet(peer, recvbuffer,
109 MAXPKTSIZE, &from, timeoutpacket);
111 /* We got some data! */
113 ((struct sockaddr_in *)&peer_sock)->sin_port =
114 ((struct sockaddr_in *)&from)->sin_port;
118 /* This should be retried */
119 if (n == RP_TIMEOUT) {
120 printf("Try %d, didn't receive answer from remote.\n",
125 /* Everything else is fatal */
129 printf("Transfer timed out.\n");
132 if (rp->th_opcode == ERROR) {
133 printf("Got ERROR, aborted\n");
138 * If the first packet is an OACK instead of an ACK packet,
139 * handle it different.
141 if (rp->th_opcode == OACK) {
142 if (!options_rfc_enabled) {
143 printf("Got OACK while options are not enabled!\n");
144 send_error(peer, EBADOP);
148 parse_options(peer, rp->th_stuff, n + 2);
151 if (read_init(fd, NULL, mode) < 0) {
157 tftp_send(peer, &block, &tftp_stats);
160 if (tftp_stats.amount > 0)
161 printstats("Sent", verbose, &tftp_stats);
170 recvfile(int peer, char *port, int fd, char *name, char *mode)
174 char recvbuffer[MAXPKTSIZE];
176 struct tftp_stats tftp_stats;
178 stats_init(&tftp_stats);
180 rp = (struct tftphdr *)recvbuffer;
184 se = getservbyname("tftp", "udp");
185 ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port;
187 ((struct sockaddr_in *)&peer_sock)->sin_port =
190 for (i = 0; i < 12; i++) {
191 struct sockaddr_storage from;
193 /* Tell the other side what we want to do */
194 if (debug&DEBUG_SIMPLE)
195 printf("Requesting %s\n", name);
197 n = send_rrq(peer, name, mode);
199 printf("Cannot send RRQ packet\n");
204 * The first packet we receive has the new destination port
205 * we have to send the next packets to.
207 n = receive_packet(peer, recvbuffer,
208 MAXPKTSIZE, &from, timeoutpacket);
210 /* We got something useful! */
212 ((struct sockaddr_in *)&peer_sock)->sin_port =
213 ((struct sockaddr_in *)&from)->sin_port;
217 /* We should retry if this happens */
218 if (n == RP_TIMEOUT) {
219 printf("Try %d, didn't receive answer from remote.\n",
224 /* Otherwise it is a fatal error */
228 printf("Transfer timed out.\n");
231 if (rp->th_opcode == ERROR) {
232 tftp_log(LOG_ERR, "Error code %d: %s", rp->th_code, rp->th_msg);
236 if (write_init(fd, NULL, mode) < 0) {
242 * If the first packet is an OACK packet instead of an DATA packet,
243 * handle it different.
245 if (rp->th_opcode == OACK) {
246 if (!options_rfc_enabled) {
247 printf("Got OACK while options are not enabled!\n");
248 send_error(peer, EBADOP);
252 parse_options(peer, rp->th_stuff, n + 2);
254 n = send_ack(peer, 0);
256 printf("Cannot send ACK on OACK.\n");
260 tftp_receive(peer, &block, &tftp_stats, NULL, 0);
263 tftp_receive(peer, &block, &tftp_stats, rp, n);
267 if (tftp_stats.amount > 0)
268 printstats("Received", verbose, &tftp_stats);