blob: 8517830b7152c4289dce908ad77741cd84f6805d [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
2/* -------------------------------------------------------------------------
3 * tftp.c
4 *
5 * A simple tftp client for busybox.
6 * Tries to follow RFC1350.
7 * Only "octet" mode supported.
8 * Optional blocksize negotiation (RFC2347 + RFC2348)
9 *
10 * Copyright (C) 2001 Magnus Damm <damm@opensource.se>
11 *
12 * Parts of the code based on:
13 *
14 * atftp: Copyright (C) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca>
15 * and Remi Lefebvre <remi@debian.org>
16 *
17 * utftp: Copyright (C) 1999 Uwe Ohse <uwe@ohse.de>
18 *
19 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
20 * ------------------------------------------------------------------------- */
Mark Whitley450736c2001-03-02 19:08:50 +000021
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Mark Whitley450736c2001-03-02 19:08:50 +000023
Mark Whitley450736c2001-03-02 19:08:50 +000024
Denis Vlasenko31635552007-01-20 16:54:19 +000025#if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
26
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +000027#define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
28#define TFTP_TIMEOUT 5 /* seconds */
29#define TFTP_NUM_RETRIES 5 /* number of retries */
30
Glenn L McGrathad117d82001-10-05 04:40:37 +000031/* opcodes we support */
Glenn L McGrathad117d82001-10-05 04:40:37 +000032#define TFTP_RRQ 1
33#define TFTP_WRQ 2
34#define TFTP_DATA 3
35#define TFTP_ACK 4
36#define TFTP_ERROR 5
37#define TFTP_OACK 6
38
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000039#if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT
Denis Vlasenkobf678d52007-05-09 12:50:08 +000040#define USE_GETPUT(...)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000041#define CMD_GET(cmd) 1
42#define CMD_PUT(cmd) 0
43#elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
Denis Vlasenkobf678d52007-05-09 12:50:08 +000044#define USE_GETPUT(...)
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000045#define CMD_GET(cmd) 0
46#define CMD_PUT(cmd) 1
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000047#else
Denis Vlasenkobf678d52007-05-09 12:50:08 +000048#define USE_GETPUT(...) __VA_ARGS__
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000049/* masks coming from getpot32 */
Denis Vlasenko31635552007-01-20 16:54:19 +000050#define CMD_GET(cmd) ((cmd) & 1)
51#define CMD_PUT(cmd) ((cmd) & 2)
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000052#endif
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000053/* NB: in the code below
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000054 * CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000055 */
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +000056
Eric Andersen76fa8ea2001-08-20 17:47:49 +000057
Denis Vlasenko04291bc2006-11-21 10:15:25 +000058#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Glenn L McGrathad117d82001-10-05 04:40:37 +000059
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060static int tftp_blocksize_check(int blocksize, int bufsize)
Glenn L McGrathad117d82001-10-05 04:40:37 +000061{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000062 /* Check if the blocksize is valid:
Glenn L McGrathad117d82001-10-05 04:40:37 +000063 * RFC2348 says between 8 and 65464,
64 * but our implementation makes it impossible
65 * to use blocksizes smaller than 22 octets.
66 */
67
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000068 if ((bufsize && (blocksize > bufsize))
69 || (blocksize < 8) || (blocksize > 65564)
70 ) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +000071 bb_error_msg("bad blocksize");
72 return 0;
Glenn L McGrathad117d82001-10-05 04:40:37 +000073 }
74
75 return blocksize;
76}
77
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000078static char *tftp_option_get(char *buf, int len, const char *option)
Glenn L McGrathad117d82001-10-05 04:40:37 +000079{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000080 int opt_val = 0;
Glenn L McGrathad117d82001-10-05 04:40:37 +000081 int opt_found = 0;
82 int k;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083
Glenn L McGrathad117d82001-10-05 04:40:37 +000084 while (len > 0) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +000085 /* Make sure the options are terminated correctly */
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +000086 for (k = 0; k < len; k++) {
87 if (buf[k] == '\0') {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000088 goto nul_found;
Glenn L McGrathad117d82001-10-05 04:40:37 +000089 }
90 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000091 return NULL;
92 nul_found:
Glenn L McGrathad117d82001-10-05 04:40:37 +000093 if (opt_val == 0) {
94 if (strcasecmp(buf, option) == 0) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +000095 opt_found = 1;
Glenn L McGrathad117d82001-10-05 04:40:37 +000096 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +000097 } else if (opt_found) {
98 return buf;
Glenn L McGrathad117d82001-10-05 04:40:37 +000099 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000100
Glenn L McGrathad117d82001-10-05 04:40:37 +0000101 k++;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000102 buf += k;
103 len -= k;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000104 opt_val ^= 1;
105 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000106
Glenn L McGrathad117d82001-10-05 04:40:37 +0000107 return NULL;
108}
109
110#endif
111
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000112static int tftp( USE_GETPUT(const int cmd,)
Denis Vlasenko0850cda2007-02-07 23:20:32 +0000113 len_and_sockaddr *peer_lsa,
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000114 const char *remotefile, const int localfd,
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000115 unsigned port, int tftp_bufsize)
Mark Whitley450736c2001-03-02 19:08:50 +0000116{
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000117 struct timeval tv;
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000118 fd_set rfds;
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000119 int socketfd;
Bernhard Reutner-Fischer62f98562006-06-10 14:32:56 +0000120 int len;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000121 int send_len;
122 USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;)
123 smallint finished = 0;
124 uint16_t opcode;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000125 uint16_t block_nr = 1;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000126 uint16_t recv_blk;
127 int timeout = TFTP_NUM_RETRIES;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000128 char *cp;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000129
Denis Vlasenko0850cda2007-02-07 23:20:32 +0000130 unsigned org_port;
131 len_and_sockaddr *const from = alloca(offsetof(len_and_sockaddr, sa) + peer_lsa->len);
132
Eric Andersen744a1942001-11-10 11:16:39 +0000133 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
134 * size varies meaning BUFFERS_GO_ON_STACK would fail */
Denis Vlasenko10f7dd12006-12-17 01:14:08 +0000135 /* We must keep the transmit and receive buffers seperate */
136 /* In case we rcv a garbage pkt and we need to rexmit the last pkt */
137 char *xbuf = xmalloc(tftp_bufsize += 4);
138 char *rbuf = xmalloc(tftp_bufsize);
Mark Whitley450736c2001-03-02 19:08:50 +0000139
Denis Vlasenko0850cda2007-02-07 23:20:32 +0000140 port = org_port = htons(port);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000141
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000142 socketfd = xsocket(peer_lsa->sa.sa_family, SOCK_DGRAM, 0);
Mark Whitley450736c2001-03-02 19:08:50 +0000143
144 /* build opcode */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000145 opcode = TFTP_WRQ;
146 if (CMD_GET(cmd)) {
Glenn L McGrathad117d82001-10-05 04:40:37 +0000147 opcode = TFTP_RRQ;
Mark Whitley450736c2001-03-02 19:08:50 +0000148 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000149 cp = xbuf + 2;
150 /* add filename and mode */
151 /* fill in packet if the filename fits into xbuf */
152 len = strlen(remotefile) + 1;
153 if (2 + len + sizeof("octet") >= tftp_bufsize) {
154 bb_error_msg("remote filename is too long");
155 goto ret;
156 }
157 strcpy(cp, remotefile);
158 cp += len;
159 /* add "mode" part of the package */
160 strcpy(cp, "octet");
161 cp += sizeof("octet");
Glenn L McGrathad117d82001-10-05 04:40:37 +0000162
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000163#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000164 len = tftp_bufsize - 4; /* data block size */
165 if (len != TFTP_BLOCKSIZE_DEFAULT) {
166 /* rfc2348 says that 65464 is a max allowed value */
167 if ((&xbuf[tftp_bufsize - 1] - cp) < sizeof("blksize NNNNN")) {
168 bb_error_msg("remote filename is too long");
169 goto ret;
170 }
171 /* add "blksize", <nul>, blocksize */
172 strcpy(cp, "blksize");
173 cp += sizeof("blksize");
174 cp += snprintf(cp, 6, "%d", len) + 1;
175 want_option_ack = 1;
176 }
Glenn L McGrathad117d82001-10-05 04:40:37 +0000177#endif
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000178 /* First packet is built, so skip packet generation */
179 goto send_pkt;
Mark Whitley450736c2001-03-02 19:08:50 +0000180
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000181 /* Using mostly goto's - continue/break will be less clear
182 * in where we actually jump to */
183
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000184 while (1) {
185 /* Build ACK or DATA */
186 cp = xbuf + 2;
187 *((uint16_t*)cp) = htons(block_nr);
188 cp += 2;
189 block_nr++;
190 opcode = TFTP_ACK;
191 if (CMD_PUT(cmd)) {
192 opcode = TFTP_DATA;
193 len = full_read(localfd, cp, tftp_bufsize - 4);
194 if (len < 0) {
195 bb_perror_msg(bb_msg_read_error);
196 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000197 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000198 if (len != (tftp_bufsize - 4)) {
199 finished = 1;
200 }
201 cp += len;
Mark Whitley450736c2001-03-02 19:08:50 +0000202 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000203 send_pkt:
204 /* Send packet */
205 *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000206 send_len = cp - xbuf;
207 /* NB: send_len value is preserved in code below
208 * for potential resend */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000209 send_again:
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000210#if ENABLE_DEBUG_TFTP
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000211 fprintf(stderr, "sending %u bytes\n", send_len);
212 for (cp = xbuf; cp < &xbuf[send_len]; cp++)
213 fprintf(stderr, "%02x ", (unsigned char) *cp);
214 fprintf(stderr, "\n");
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000215#endif
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000216 xsendto(socketfd, xbuf, send_len, &peer_lsa->sa, peer_lsa->len);
217 /* Was it final ACK? then exit */
218 if (finished && (opcode == TFTP_ACK))
219 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000220
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000221 timeout = TFTP_NUM_RETRIES; /* re-initialize */
Denis Vlasenko2c916522007-01-12 14:57:37 +0000222 recv_again:
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000223 /* Receive packet */
224 tv.tv_sec = TFTP_TIMEOUT;
225 tv.tv_usec = 0;
226 FD_ZERO(&rfds);
227 FD_SET(socketfd, &rfds);
228 switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
229 unsigned from_port;
230 case 1:
231 from->len = peer_lsa->len;
232 memset(&from->sa, 0, peer_lsa->len);
233 len = recvfrom(socketfd, rbuf, tftp_bufsize, 0,
234 &from->sa, &from->len);
235 if (len < 0) {
236 bb_perror_msg("recvfrom");
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000237 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000238 }
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000239 from_port = get_nport(&from->sa);
240 if (port == org_port) {
241 /* Our first query went to port 69
242 * but reply will come from different one.
243 * Remember and use this new port */
244 port = from_port;
245 set_nport(peer_lsa, from_port);
246 }
247 if (port != from_port)
248 goto recv_again;
249 goto process_pkt;
250 case 0:
251 timeout--;
252 if (timeout == 0) {
253 bb_error_msg("last timeout");
254 goto ret;
255 }
256 bb_error_msg("last timeout" + 5);
257 goto send_again; /* resend last sent pkt */
258 default:
259 bb_perror_msg("select");
260 goto ret;
261 }
262 process_pkt:
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000263 /* Process recv'ed packet */
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000264 opcode = ntohs( ((uint16_t*)rbuf)[0] );
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000265 recv_blk = ntohs( ((uint16_t*)rbuf)[1] );
Mark Whitley450736c2001-03-02 19:08:50 +0000266
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000267#if ENABLE_DEBUG_TFTP
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000268 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000269#endif
Mark Whitley450736c2001-03-02 19:08:50 +0000270
Glenn L McGrathad117d82001-10-05 04:40:37 +0000271 if (opcode == TFTP_ERROR) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000272 static const char *const errcode_str[] = {
273 "",
274 "file not found",
275 "access violation",
276 "disk full",
277 "illegal TFTP operation",
278 "unknown transfer id",
279 "file already exists",
280 "no such user",
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000281 "bad option",
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000282 };
283 enum { NUM_ERRCODE = sizeof(errcode_str) / sizeof(errcode_str[0]) };
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000284 const char *msg = "";
Glenn L McGrathad117d82001-10-05 04:40:37 +0000285
Denis Vlasenko10f7dd12006-12-17 01:14:08 +0000286 if (rbuf[4] != '\0') {
287 msg = &rbuf[4];
288 rbuf[tftp_bufsize - 1] = '\0';
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000289 } else if (recv_blk < NUM_ERRCODE) {
290 msg = errcode_str[recv_blk];
Glenn L McGrathad117d82001-10-05 04:40:37 +0000291 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000292 bb_error_msg("server error: (%u) %s", recv_blk, msg);
293 goto ret;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000294 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000295
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000296#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Glenn L McGrathad117d82001-10-05 04:40:37 +0000297 if (want_option_ack) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000298 want_option_ack = 0;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000299
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000300 if (opcode == TFTP_OACK) {
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000301 /* server seems to support options */
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000302 char *res;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000303
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000304 res = tftp_option_get(&rbuf[2], len - 2, "blksize");
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000305 if (res) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000306 int blksize = xatoi_u(res);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000307 if (!tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000308 /* send ERROR 8 to server... */
309 /* htons can be impossible to use in const initializer: */
310 /*static const uint16_t error_8[2] = { htons(TFTP_ERROR), htons(8) };*/
311 /* thus we open-code big-endian layout */
312 static const char error_8[4] = { 0,TFTP_ERROR, 0,8 };
313 xsendto(socketfd, error_8, 4, &peer_lsa->sa, peer_lsa->len);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000314 bb_error_msg("server proposes bad blksize %d, exiting", blksize);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000315 goto ret;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000316 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000317#if ENABLE_DEBUG_TFTP
318 fprintf(stderr, "using blksize %u\n",
319 blksize);
320#endif
321 tftp_bufsize = blksize + 4;
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000322 /* Send ACK for OACK ("block" no: 0) */
323 block_nr = 0;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000324 continue;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000325 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000326 /* rfc2347:
327 * "An option not acknowledged by the server
328 * must be ignored by the client and server
329 * as if it were never requested." */
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000330 }
Glenn L McGrathad117d82001-10-05 04:40:37 +0000331
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000332 bb_error_msg("blksize is not supported by server"
333 " - reverting to 512");
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000334 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000335 }
336#endif
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000337 /* block_nr is already advanced to next block# we expect
338 * to get / block# we are about to send next time */
Glenn L McGrathad117d82001-10-05 04:40:37 +0000339
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000340 if (CMD_GET(cmd) && (opcode == TFTP_DATA)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000341 if (recv_blk == block_nr) {
Denis Vlasenko10f7dd12006-12-17 01:14:08 +0000342 len = full_write(localfd, &rbuf[4], len - 4);
Mark Whitley450736c2001-03-02 19:08:50 +0000343 if (len < 0) {
Bernhard Reutner-Fischer1b9d7c92006-06-03 22:45:37 +0000344 bb_perror_msg(bb_msg_write_error);
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000345 goto ret;
Mark Whitley450736c2001-03-02 19:08:50 +0000346 }
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000347 if (len != (tftp_bufsize - 4)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000348 finished = 1;
Mark Whitley450736c2001-03-02 19:08:50 +0000349 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000350 continue; /* send ACK */
Mark Whitley450736c2001-03-02 19:08:50 +0000351 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000352 if (recv_blk == (block_nr - 1)) {
Paul Fox1d4c88c2005-07-20 19:49:15 +0000353 /* Server lost our TFTP_ACK. Resend it */
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000354 block_nr = recv_blk;
Paul Fox1d4c88c2005-07-20 19:49:15 +0000355 continue;
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000356 }
Mark Whitley450736c2001-03-02 19:08:50 +0000357 }
358
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000359 if (CMD_PUT(cmd) && (opcode == TFTP_ACK)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000360 /* did server ACK our last DATA pkt? */
361 if (recv_blk == (uint16_t) (block_nr - 1)) {
362 if (finished)
363 goto ret;
364 continue; /* send next block */
Mark Whitley450736c2001-03-02 19:08:50 +0000365 }
366 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000367 /* Awww... recv'd packet is not recognized! */
368 goto recv_again;
369 /* why recv_again? - rfc1123 says:
370 * "The sender (i.e., the side originating the DATA packets)
371 * must never resend the current DATA packet on receipt
372 * of a duplicate ACK".
373 * DATA pkts are resent ONLY on timeout.
374 * Thus "goto send_again" will ba a bad mistake above.
375 * See:
376 * http://en.wikipedia.org/wiki/Sorcerer's_Apprentice_Syndrome
377 */
Mark Whitley450736c2001-03-02 19:08:50 +0000378 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000379 ret:
Denis Vlasenko2c916522007-01-12 14:57:37 +0000380 if (ENABLE_FEATURE_CLEAN_UP) {
381 close(socketfd);
382 free(xbuf);
383 free(rbuf);
384 }
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000385 return finished == 0; /* returns 1 on failure */
Mark Whitley450736c2001-03-02 19:08:50 +0000386}
387
Denis Vlasenko06af2162007-02-03 17:28:39 +0000388int tftp_main(int argc, char **argv);
Mark Whitley450736c2001-03-02 19:08:50 +0000389int tftp_main(int argc, char **argv)
390{
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000391 len_and_sockaddr *peer_lsa;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000392 const char *localfile = NULL;
393 const char *remotefile = NULL;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000394#if ENABLE_FEATURE_TFTP_BLOCKSIZE
395 const char *sblocksize = NULL;
396#endif
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000397 int port;
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000398 USE_GETPUT(int cmd;)
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000399 int fd = -1;
400 int flags = 0;
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000401 int result;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000402 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
Mark Whitley450736c2001-03-02 19:08:50 +0000403
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000404 /* -p or -g is mandatory, and they are mutually exclusive */
405 opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:")
406 USE_GETPUT("?g--p:p--g");
Glenn L McGrathad117d82001-10-05 04:40:37 +0000407
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000408 USE_GETPUT(cmd =) getopt32(argc, argv,
409 USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p")
410 "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"),
411 &localfile, &remotefile
412 USE_FEATURE_TFTP_BLOCKSIZE(, &sblocksize));
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000413 argv += optind;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000414
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000415 flags = O_RDONLY;
416 if (CMD_GET(cmd))
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000417 flags = O_WRONLY | O_CREAT | O_TRUNC;
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000418
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000419#if ENABLE_FEATURE_TFTP_BLOCKSIZE
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000420 if (sblocksize) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000421 blocksize = xatoi_u(sblocksize);
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +0000422 if (!tftp_blocksize_check(blocksize, 0)) {
423 return EXIT_FAILURE;
Mark Whitley450736c2001-03-02 19:08:50 +0000424 }
Mark Whitley450736c2001-03-02 19:08:50 +0000425 }
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +0000426#endif
Mark Whitley450736c2001-03-02 19:08:50 +0000427
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000428 if (!localfile)
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000429 localfile = remotefile;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000430 if (!remotefile)
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000431 remotefile = localfile;
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000432 /* Error if filename or host is not known */
433 if (!remotefile || !argv[0])
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000434 bb_show_usage();
"Vladimir N. Oleynik"86ac0722005-10-17 10:47:19 +0000435
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000436 fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO;
437 if (!LONE_DASH(localfile)) {
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000438 fd = xopen(localfile, flags);
Mark Whitley450736c2001-03-02 19:08:50 +0000439 }
440
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000441 port = bb_lookup_port(argv[1], "udp", 69);
442 peer_lsa = xhost2sockaddr(argv[0], port);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000443
Denis Vlasenko04291bc2006-11-21 10:15:25 +0000444#if ENABLE_DEBUG_TFTP
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000445 fprintf(stderr, "using server '%s', remotefile '%s', localfile '%s'\n",
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +0000446 xmalloc_sockaddr2dotted(&peer_lsa->sa, peer_lsa->len),
Bernhard Reutner-Fischerb25f98a2006-06-10 14:15:03 +0000447 remotefile, localfile);
Eric Andersen76fa8ea2001-08-20 17:47:49 +0000448#endif
449
Denis Vlasenkobf678d52007-05-09 12:50:08 +0000450 result = tftp( USE_GETPUT(cmd,) peer_lsa, remotefile, fd, port, blocksize);
Mark Whitley450736c2001-03-02 19:08:50 +0000451
Denis Vlasenkoa04561f2007-05-08 23:12:21 +0000452 if (ENABLE_FEATURE_CLEAN_UP)
453 close(fd);
454 if (result != EXIT_SUCCESS && !LONE_DASH(localfile) && CMD_GET(cmd)) {
455 unlink(localfile);
Eric Andersena66a43e2002-04-13 09:30:25 +0000456 }
Denis Vlasenko000b9ba2006-10-05 23:12:49 +0000457 return result;
Glenn L McGrathad117d82001-10-05 04:40:37 +0000458}
Denis Vlasenko31635552007-01-20 16:54:19 +0000459
460#endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */