tftp.c (bacb00ab408a2178d13f33d0cd9c6622913d80a9) tftp.c (92570f67c7911126ce742a3dfe1b97046091ed0e)
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 54 unchanged lines hidden (view full) ---

63#include "tftp-utils.h"
64#include "tftp-io.h"
65#include "tftp-transfer.h"
66#include "tftp-options.h"
67
68/*
69 * Send the requested file.
70 */
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 54 unchanged lines hidden (view full) ---

63#include "tftp-utils.h"
64#include "tftp-io.h"
65#include "tftp-transfer.h"
66#include "tftp-options.h"
67
68/*
69 * Send the requested file.
70 */
71void
71int
72xmitfile(int peer, char *port, int fd, char *name, char *mode)
73{
74 struct tftphdr *rp;
72xmitfile(int peer, char *port, int fd, char *name, char *mode)
73{
74 struct tftphdr *rp;
75 int n, i;
75 int n, i, ret = 0;
76 uint16_t block;
77 struct sockaddr_storage serv; /* valid server port number */
78 char recvbuffer[MAXPKTSIZE];
79 struct tftp_stats tftp_stats;
80
81 stats_init(&tftp_stats);
82
83 memset(&serv, 0, sizeof(serv));

--- 13 unchanged lines hidden (view full) ---

97
98 /* Tell the other side what we want to do */
99 if (debug & DEBUG_SIMPLE)
100 printf("Sending %s\n", name);
101
102 n = send_wrq(peer, name, mode);
103 if (n > 0) {
104 printf("Cannot send WRQ packet\n");
76 uint16_t block;
77 struct sockaddr_storage serv; /* valid server port number */
78 char recvbuffer[MAXPKTSIZE];
79 struct tftp_stats tftp_stats;
80
81 stats_init(&tftp_stats);
82
83 memset(&serv, 0, sizeof(serv));

--- 13 unchanged lines hidden (view full) ---

97
98 /* Tell the other side what we want to do */
99 if (debug & DEBUG_SIMPLE)
100 printf("Sending %s\n", name);
101
102 n = send_wrq(peer, name, mode);
103 if (n > 0) {
104 printf("Cannot send WRQ packet\n");
105 return;
105 return -1;
106 }
107
108 /*
109 * The first packet we receive has the new destination port
110 * we have to send the next packets to.
111 */
112 n = receive_packet(peer, recvbuffer,
113 MAXPKTSIZE, &from, timeoutpacket);

--- 12 unchanged lines hidden (view full) ---

126 continue;
127 }
128
129 /* Everything else is fatal */
130 break;
131 }
132 if (i == 12) {
133 printf("Transfer timed out.\n");
106 }
107
108 /*
109 * The first packet we receive has the new destination port
110 * we have to send the next packets to.
111 */
112 n = receive_packet(peer, recvbuffer,
113 MAXPKTSIZE, &from, timeoutpacket);

--- 12 unchanged lines hidden (view full) ---

126 continue;
127 }
128
129 /* Everything else is fatal */
130 break;
131 }
132 if (i == 12) {
133 printf("Transfer timed out.\n");
134 return;
134 return -1;
135 }
136 if (rp->th_opcode == ERROR) {
137 printf("Got ERROR, aborted\n");
135 }
136 if (rp->th_opcode == ERROR) {
137 printf("Got ERROR, aborted\n");
138 return;
138 return -1;
139 }
140
141 /*
142 * If the first packet is an OACK instead of an ACK packet,
143 * handle it different.
144 */
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);
139 }
140
141 /*
142 * If the first packet is an OACK instead of an ACK packet,
143 * handle it different.
144 */
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);
149 return;
149 return -1;
150 }
151
152 parse_options(peer, rp->th_stuff, n + 2);
153 }
154
155 if (read_init(fd, NULL, mode) < 0) {
156 warn("read_init()");
150 }
151
152 parse_options(peer, rp->th_stuff, n + 2);
153 }
154
155 if (read_init(fd, NULL, mode) < 0) {
156 warn("read_init()");
157 return;
157 return -1;
158 }
159
160 block = 1;
158 }
159
160 block = 1;
161 tftp_send(peer, &block, &tftp_stats);
161 if (tftp_send(peer, &block, &tftp_stats) != 0)
162 ret = -1;
162
163 read_close();
164 if (tftp_stats.amount > 0)
165 printstats("Sent", verbose, &tftp_stats);
163
164 read_close();
165 if (tftp_stats.amount > 0)
166 printstats("Sent", verbose, &tftp_stats);
166
167 txrx_error = 1;
167 return ret;
168}
169
170/*
171 * Receive a file.
172 */
168}
169
170/*
171 * Receive a file.
172 */
173void
173int
174recvfile(int peer, char *port, int fd, char *name, char *mode)
175{
176 struct tftphdr *rp;
177 uint16_t block;
178 char recvbuffer[MAXPKTSIZE];
174recvfile(int peer, char *port, int fd, char *name, char *mode)
175{
176 struct tftphdr *rp;
177 uint16_t block;
178 char recvbuffer[MAXPKTSIZE];
179 int n, i;
179 int n, i, ret = 0;
180 struct tftp_stats tftp_stats;
181
182 stats_init(&tftp_stats);
183
184 rp = (struct tftphdr *)recvbuffer;
185
186 if (port == NULL) {
187 struct servent *se;

--- 9 unchanged lines hidden (view full) ---

197
198 /* Tell the other side what we want to do */
199 if (debug & DEBUG_SIMPLE)
200 printf("Requesting %s\n", name);
201
202 n = send_rrq(peer, name, mode);
203 if (n > 0) {
204 printf("Cannot send RRQ packet\n");
180 struct tftp_stats tftp_stats;
181
182 stats_init(&tftp_stats);
183
184 rp = (struct tftphdr *)recvbuffer;
185
186 if (port == NULL) {
187 struct servent *se;

--- 9 unchanged lines hidden (view full) ---

197
198 /* Tell the other side what we want to do */
199 if (debug & DEBUG_SIMPLE)
200 printf("Requesting %s\n", name);
201
202 n = send_rrq(peer, name, mode);
203 if (n > 0) {
204 printf("Cannot send RRQ packet\n");
205 return;
205 return -1;
206 }
207
208 /*
209 * The first packet we receive has the new destination port
210 * we have to send the next packets to.
211 */
212 n = receive_packet(peer, recvbuffer,
213 MAXPKTSIZE, &from, timeoutpacket);

--- 12 unchanged lines hidden (view full) ---

226 continue;
227 }
228
229 /* Otherwise it is a fatal error */
230 break;
231 }
232 if (i == 12) {
233 printf("Transfer timed out.\n");
206 }
207
208 /*
209 * The first packet we receive has the new destination port
210 * we have to send the next packets to.
211 */
212 n = receive_packet(peer, recvbuffer,
213 MAXPKTSIZE, &from, timeoutpacket);

--- 12 unchanged lines hidden (view full) ---

226 continue;
227 }
228
229 /* Otherwise it is a fatal error */
230 break;
231 }
232 if (i == 12) {
233 printf("Transfer timed out.\n");
234 return;
234 return -1;
235 }
236 if (rp->th_opcode == ERROR) {
237 tftp_log(LOG_ERR, "Error code %d: %s", rp->th_code, rp->th_msg);
235 }
236 if (rp->th_opcode == ERROR) {
237 tftp_log(LOG_ERR, "Error code %d: %s", rp->th_code, rp->th_msg);
238 return;
238 return -1;
239 }
240
241 if (write_init(fd, NULL, mode) < 0) {
242 warn("write_init");
239 }
240
241 if (write_init(fd, NULL, mode) < 0) {
242 warn("write_init");
243 return;
243 return -1;
244 }
245
246 /*
247 * If the first packet is an OACK packet instead of an DATA packet,
248 * handle it different.
249 */
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);
244 }
245
246 /*
247 * If the first packet is an OACK packet instead of an DATA packet,
248 * handle it different.
249 */
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);
254 return;
254 return -1;
255 }
256
257 parse_options(peer, rp->th_stuff, n + 2);
258
259 n = send_ack(peer, 0);
260 if (n > 0) {
261 printf("Cannot send ACK on OACK.\n");
255 }
256
257 parse_options(peer, rp->th_stuff, n + 2);
258
259 n = send_ack(peer, 0);
260 if (n > 0) {
261 printf("Cannot send ACK on OACK.\n");
262 return;
262 return -1;
263 }
264 block = 0;
263 }
264 block = 0;
265 tftp_receive(peer, &block, &tftp_stats, NULL, 0);
265 if (tftp_receive(peer, &block, &tftp_stats, NULL, 0) != 0)
266 ret = -1;
266 } else {
267 block = 1;
267 } else {
268 block = 1;
268 tftp_receive(peer, &block, &tftp_stats, rp, n);
269 if (tftp_receive(peer, &block, &tftp_stats, rp, n) != 0)
270 ret = -1;
269 }
270
271 if (tftp_stats.amount > 0)
272 printstats("Received", verbose, &tftp_stats);
271 }
272
273 if (tftp_stats.amount > 0)
274 printstats("Received", verbose, &tftp_stats);
273 return;
275 return ret;
274}
276}