1 /* vi: set sw=4 ts=4: */
2 /* -------------------------------------------------------------------------
5 * A simple tftp client for busybox.
6 * Tries to follow RFC1350.
7 * Only "octet" mode supported.
8 * Optional blocksize negotiation (RFC2347 + RFC2348)
10 * Copyright (C) 2001 Magnus Damm <damm@opensource.se>
12 * Parts of the code based on:
14 * atftp: Copyright (C) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca>
15 * and Remi Lefebvre <remi@debian.org>
17 * utftp: Copyright (C) 1999 Uwe Ohse <uwe@ohse.de>
19 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
20 * ------------------------------------------------------------------------- */
24 #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
26 #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
27 #define TFTP_TIMEOUT 50000 /* 50ms, in microseconds */
28 #define TFTP_MAXTIMEOUT 999000 /* about 1 second, in microseconds */
29 #define TFTP_NUM_RETRIES 12 /* number of backed-off retries */
31 /* opcodes we support */
39 #if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT
40 #define USE_GETPUT(...)
41 #define CMD_GET(cmd) 1
42 #define CMD_PUT(cmd) 0
43 #elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
44 #define USE_GETPUT(...)
45 #define CMD_GET(cmd) 0
46 #define CMD_PUT(cmd) 1
48 #define USE_GETPUT(...) __VA_ARGS__
49 /* masks coming from getpot32 */
50 #define CMD_GET(cmd) ((cmd) & 1)
51 #define CMD_PUT(cmd) ((cmd) & 2)
53 /* NB: in the code below
54 * CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive
58 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
60 static int tftp_blocksize_check(int blocksize, int bufsize)
62 /* Check if the blocksize is valid:
63 * RFC2348 says between 8 and 65464,
64 * but our implementation makes it impossible
65 * to use blocksizes smaller than 22 octets.
68 if ((bufsize && (blocksize > bufsize))
69 || (blocksize < 8) || (blocksize > 65564)
71 bb_error_msg("bad blocksize");
78 static char *tftp_option_get(char *buf, int len, const char *option)
85 /* Make sure the options are terminated correctly */
86 for (k = 0; k < len; k++) {
94 if (strcasecmp(buf, option) == 0) {
97 } else if (opt_found) {
112 static int tftp( USE_GETPUT(const int cmd,)
113 len_and_sockaddr *peer_lsa,
114 const char *remotefile, const int localfd,
115 unsigned port, int tftp_bufsize)
122 USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;)
123 smallint finished = 0;
125 uint16_t block_nr = 1;
127 int retries, waittime;
131 len_and_sockaddr *const from = alloca(offsetof(len_and_sockaddr, sa) + peer_lsa->len);
133 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
134 * size varies meaning BUFFERS_GO_ON_STACK would fail */
135 /* 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);
140 port = org_port = htons(port);
142 socketfd = xsocket(peer_lsa->sa.sa_family, SOCK_DGRAM, 0);
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");
157 strcpy(cp, remotefile);
159 /* add "mode" part of the package */
161 cp += sizeof("octet");
163 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
164 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");
171 /* add "blksize", <nul>, blocksize */
172 strcpy(cp, "blksize");
173 cp += sizeof("blksize");
174 cp += snprintf(cp, 6, "%d", len) + 1;
178 /* First packet is built, so skip packet generation */
181 /* Using mostly goto's - continue/break will be less clear
182 * in where we actually jump to */
185 /* Build ACK or DATA */
187 *((uint16_t*)cp) = htons(block_nr);
193 len = full_read(localfd, cp, tftp_bufsize - 4);
195 bb_perror_msg(bb_msg_read_error);
198 if (len != (tftp_bufsize - 4)) {
205 *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */
206 send_len = cp - xbuf;
207 /* NB: send_len value is preserved in code below
208 * for potential resend */
210 retries = TFTP_NUM_RETRIES; /* re-initialize */
211 waittime = TFTP_TIMEOUT;
214 #if ENABLE_DEBUG_TFTP
215 fprintf(stderr, "sending %u bytes\n", send_len);
216 for (cp = xbuf; cp < &xbuf[send_len]; cp++)
217 fprintf(stderr, "%02x ", (unsigned char) *cp);
218 fprintf(stderr, "\n");
220 xsendto(socketfd, xbuf, send_len, &peer_lsa->sa, peer_lsa->len);
221 /* Was it final ACK? then exit */
222 if (finished && (opcode == TFTP_ACK))
228 tv.tv_usec = waittime;
230 FD_SET(socketfd, &rfds);
231 switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
234 from->len = peer_lsa->len;
235 memset(&from->sa, 0, peer_lsa->len);
236 len = recvfrom(socketfd, rbuf, tftp_bufsize, 0,
237 &from->sa, &from->len);
239 bb_perror_msg("recvfrom");
242 from_port = get_nport(&from->sa);
243 if (port == org_port) {
244 /* Our first query went to port 69
245 * but reply will come from different one.
246 * Remember and use this new port */
248 set_nport(peer_lsa, from_port);
250 if (port != from_port)
256 bb_error_msg("timeout");
260 /* exponential backoff with limit */
261 waittime += waittime/2;
262 if (waittime > TFTP_MAXTIMEOUT) {
263 waittime = TFTP_MAXTIMEOUT;
266 goto send_again; /* resend last sent pkt */
268 bb_perror_msg("select");
272 /* Process recv'ed packet */
273 opcode = ntohs( ((uint16_t*)rbuf)[0] );
274 recv_blk = ntohs( ((uint16_t*)rbuf)[1] );
276 #if ENABLE_DEBUG_TFTP
277 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk);
280 if (opcode == TFTP_ERROR) {
281 static const char *const errcode_str[] = {
286 "illegal TFTP operation",
287 "unknown transfer id",
288 "file already exists",
293 const char *msg = "";
295 if (rbuf[4] != '\0') {
297 rbuf[tftp_bufsize - 1] = '\0';
298 } else if (recv_blk < ARRAY_SIZE(errcode_str)) {
299 msg = errcode_str[recv_blk];
301 bb_error_msg("server error: (%u) %s", recv_blk, msg);
305 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
306 if (want_option_ack) {
309 if (opcode == TFTP_OACK) {
310 /* server seems to support options */
313 res = tftp_option_get(&rbuf[2], len - 2, "blksize");
315 int blksize = xatoi_u(res);
316 if (!tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
317 /* send ERROR 8 to server... */
318 /* htons can be impossible to use in const initializer: */
319 /*static const uint16_t error_8[2] = { htons(TFTP_ERROR), htons(8) };*/
320 /* thus we open-code big-endian layout */
321 static const uint8_t error_8[4] = { 0,TFTP_ERROR, 0,8 };
322 xsendto(socketfd, error_8, 4, &peer_lsa->sa, peer_lsa->len);
323 bb_error_msg("server proposes bad blksize %d, exiting", blksize);
326 #if ENABLE_DEBUG_TFTP
327 fprintf(stderr, "using blksize %u\n",
330 tftp_bufsize = blksize + 4;
331 /* Send ACK for OACK ("block" no: 0) */
336 * "An option not acknowledged by the server
337 * must be ignored by the client and server
338 * as if it were never requested." */
341 bb_error_msg("blksize is not supported by server"
342 " - reverting to 512");
343 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
346 /* block_nr is already advanced to next block# we expect
347 * to get / block# we are about to send next time */
349 if (CMD_GET(cmd) && (opcode == TFTP_DATA)) {
350 if (recv_blk == block_nr) {
351 len = full_write(localfd, &rbuf[4], len - 4);
353 bb_perror_msg(bb_msg_write_error);
356 if (len != (tftp_bufsize - 4)) {
359 continue; /* send ACK */
361 if (recv_blk == (block_nr - 1)) {
362 /* Server lost our TFTP_ACK. Resend it */
368 if (CMD_PUT(cmd) && (opcode == TFTP_ACK)) {
369 /* did server ACK our last DATA pkt? */
370 if (recv_blk == (uint16_t) (block_nr - 1)) {
373 continue; /* send next block */
376 /* Awww... recv'd packet is not recognized! */
378 /* why recv_again? - rfc1123 says:
379 * "The sender (i.e., the side originating the DATA packets)
380 * must never resend the current DATA packet on receipt
381 * of a duplicate ACK".
382 * DATA pkts are resent ONLY on timeout.
383 * Thus "goto send_again" will ba a bad mistake above.
385 * http://en.wikipedia.org/wiki/Sorcerer's_Apprentice_Syndrome
389 if (ENABLE_FEATURE_CLEAN_UP) {
394 return finished == 0; /* returns 1 on failure */
397 int tftp_main(int argc, char **argv);
398 int tftp_main(int argc, char **argv)
400 len_and_sockaddr *peer_lsa;
401 const char *localfile = NULL;
402 const char *remotefile = NULL;
403 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
404 const char *sblocksize = NULL;
411 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
413 /* -p or -g is mandatory, and they are mutually exclusive */
414 opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:")
415 USE_GETPUT("?g--p:p--g");
417 USE_GETPUT(cmd =) getopt32(argv,
418 USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p")
419 "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"),
420 &localfile, &remotefile
421 USE_FEATURE_TFTP_BLOCKSIZE(, &sblocksize));
426 flags = O_WRONLY | O_CREAT | O_TRUNC;
428 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
430 blocksize = xatoi_u(sblocksize);
431 if (!tftp_blocksize_check(blocksize, 0)) {
438 localfile = remotefile;
440 remotefile = localfile;
441 /* Error if filename or host is not known */
442 if (!remotefile || !argv[0])
445 fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO;
446 if (!LONE_DASH(localfile)) {
447 fd = xopen(localfile, flags);
450 port = bb_lookup_port(argv[1], "udp", 69);
451 peer_lsa = xhost2sockaddr(argv[0], port);
453 #if ENABLE_DEBUG_TFTP
454 fprintf(stderr, "using server '%s', remotefile '%s', localfile '%s'\n",
455 xmalloc_sockaddr2dotted(&peer_lsa->sa),
456 remotefile, localfile);
459 result = tftp( USE_GETPUT(cmd,) peer_lsa, remotefile, fd, port, blocksize);
461 if (ENABLE_FEATURE_CLEAN_UP)
463 if (result != EXIT_SUCCESS && !LONE_DASH(localfile) && CMD_GET(cmd)) {
469 #endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */