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 * ------------------------------------------------------------------------- */
25 #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT
27 #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 */
31 /* opcodes we support */
39 static const char *const tftp_bb_error_msg[] = {
43 "Disk full or allocation error",
44 "Illegal TFTP operation",
45 "Unknown transfer ID",
46 "File already exists",
50 #if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT
52 #define CMD_GET(cmd) 1
53 #define CMD_PUT(cmd) 0
54 #elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
56 #define CMD_GET(cmd) 0
57 #define CMD_PUT(cmd) 1
59 #define USE_GETPUT(a) a
60 /* masks coming from getpot32 */
61 #define CMD_GET(cmd) ((cmd) & 1)
62 #define CMD_PUT(cmd) ((cmd) & 2)
64 /* NB: in the code below
65 * CMD_GET(cmd) and CMD_GET(cmd) are mutually exclusive
69 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
71 static int tftp_blocksize_check(int blocksize, int bufsize)
73 /* Check if the blocksize is valid:
74 * RFC2348 says between 8 and 65464,
75 * but our implementation makes it impossible
76 * to use blocksizes smaller than 22 octets.
79 if ((bufsize && (blocksize > bufsize))
80 || (blocksize < 8) || (blocksize > 65564)
82 bb_error_msg("bad blocksize");
89 static char *tftp_option_get(char *buf, int len, const char * const option)
96 /* Make sure the options are terminated correctly */
98 for (k = 0; k < len; k++) {
109 if (strcasecmp(buf, option) == 0) {
132 #if ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
135 const len_and_sockaddr *peer_lsa,
136 const char *remotefile, const int localfd,
137 unsigned port, int tftp_bufsize)
145 int timeout = TFTP_NUM_RETRIES;
146 uint16_t block_nr = 1;
150 USE_FEATURE_TFTP_BLOCKSIZE(int want_option_ack = 0;)
152 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
153 * size varies meaning BUFFERS_GO_ON_STACK would fail */
154 /* We must keep the transmit and receive buffers seperate */
155 /* In case we rcv a garbage pkt and we need to rexmit the last pkt */
156 char *xbuf = xmalloc(tftp_bufsize += 4);
157 char *rbuf = xmalloc(tftp_bufsize);
161 socketfd = xsocket(peer_lsa->sa.sa_family, SOCK_DGRAM, 0);
173 /* first create the opcode part */
174 *((uint16_t*)cp) = htons(opcode);
177 /* add filename and mode */
178 if (CMD_GET(cmd) ? (opcode == TFTP_RRQ) : (opcode == TFTP_WRQ)) {
181 /* see if the filename fits into xbuf
182 * and fill in packet. */
183 len = strlen(remotefile) + 1;
185 if ((cp + len) >= &xbuf[tftp_bufsize - 1]) {
188 safe_strncpy(cp, remotefile, len);
192 if (too_long || (&xbuf[tftp_bufsize - 1] - cp) < sizeof("octet")) {
193 bb_error_msg("remote filename too long");
197 /* add "mode" part of the package */
198 memcpy(cp, "octet", sizeof("octet"));
199 cp += sizeof("octet");
201 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
203 len = tftp_bufsize - 4; /* data block size */
205 if (len != TFTP_BLOCKSIZE_DEFAULT) {
207 if ((&xbuf[tftp_bufsize - 1] - cp) < 15) {
208 bb_error_msg("remote filename too long");
212 /* add "blksize" + number of blocks */
213 memcpy(cp, "blksize", sizeof("blksize"));
214 cp += sizeof("blksize");
215 cp += snprintf(cp, 6, "%d", len) + 1;
222 /* add ack and data */
224 if (CMD_GET(cmd) ? (opcode == TFTP_ACK) : (opcode == TFTP_DATA)) {
225 *((uint16_t*)cp) = htons(block_nr);
229 if (CMD_PUT(cmd) && (opcode == TFTP_DATA)) {
230 len = full_read(localfd, cp, tftp_bufsize - 4);
233 bb_perror_msg(bb_msg_read_error);
237 if (len != (tftp_bufsize - 4)) {
247 timeout = TFTP_NUM_RETRIES; /* re-initialize */
250 #if ENABLE_DEBUG_TFTP
251 fprintf(stderr, "sending %u bytes\n", len);
252 for (cp = xbuf; cp < &xbuf[len]; cp++)
253 fprintf(stderr, "%02x ", (unsigned char) *cp);
254 fprintf(stderr, "\n");
256 if (sendto(socketfd, xbuf, len, 0,
257 &peer_lsa->sa, peer_lsa->len) < 0) {
258 bb_perror_msg("send");
263 if (finished && (opcode == TFTP_ACK)) {
269 tv.tv_sec = TFTP_TIMEOUT;
273 FD_SET(socketfd, &rfds);
275 switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
276 struct sockaddr *from;
280 fromlen = peer_lsa->len;
281 from = alloca(fromlen);
282 memset(from, 0, fromlen);
284 len = recvfrom(socketfd, rbuf, tftp_bufsize, 0,
287 bb_perror_msg("recvfrom");
290 #if ENABLE_FEATURE_IPV6
291 if (from->sa_family == AF_INET6)
292 if (((struct sockaddr_in6*)from)->sin6_port != port)
295 if (from->sa_family == AF_INET)
296 if (((struct sockaddr_in*)from)->sin_port != port)
301 bb_error_msg("timeout");
305 bb_error_msg("last timeout");
309 bb_perror_msg("select");
313 } while (timeout && (len >= 0));
315 if (finished || (len < 0)) {
319 /* process received packet */
321 opcode = ntohs( ((uint16_t*)rbuf)[0] );
322 tmp = ntohs( ((uint16_t*)rbuf)[1] );
324 #if ENABLE_DEBUG_TFTP
325 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp);
328 if (opcode == TFTP_ERROR) {
329 const char *msg = NULL;
331 if (rbuf[4] != '\0') {
333 rbuf[tftp_bufsize - 1] = '\0';
334 } else if (tmp < (sizeof(tftp_bb_error_msg)
336 msg = tftp_bb_error_msg[tmp];
340 bb_error_msg("server says: %s", msg);
345 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
346 if (want_option_ack) {
350 if (opcode == TFTP_OACK) {
351 /* server seems to support options */
354 res = tftp_option_get(&rbuf[2], len - 2, "blksize");
357 int blksize = xatoi_u(res);
359 if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
365 #if ENABLE_DEBUG_TFTP
366 fprintf(stderr, "using blksize %u\n",
369 tftp_bufsize = blksize + 4;
375 * we should send ERROR 8 */
376 bb_error_msg("bad server option");
380 bb_error_msg("warning: blksize not supported by server"
381 " - reverting to 512");
383 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
387 if (CMD_GET(cmd) && (opcode == TFTP_DATA)) {
388 if (tmp == block_nr) {
389 len = full_write(localfd, &rbuf[4], len - 4);
392 bb_perror_msg(bb_msg_write_error);
396 if (len != (tftp_bufsize - 4)) {
403 /* in case the last ack disappeared into the ether */
404 if (tmp == (block_nr - 1)) {
408 // tmp==(block_nr-1) and (tmp+1)==block_nr is always same, I think. wtf?
409 } else if (tmp + 1 == block_nr) {
410 /* Server lost our TFTP_ACK. Resend it */
417 if (CMD_PUT(cmd) && (opcode == TFTP_ACK)) {
418 if (tmp == (uint16_t) (block_nr - 1)) {
429 if (ENABLE_FEATURE_CLEAN_UP) {
435 return finished ? EXIT_SUCCESS : EXIT_FAILURE;
438 int tftp_main(int argc, char **argv);
439 int tftp_main(int argc, char **argv)
441 len_and_sockaddr *peer_lsa;
442 const char *localfile = NULL;
443 const char *remotefile = NULL;
444 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
445 const char *sblocksize = NULL;
452 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
454 /* -p or -g is mandatory, and they are mutually exclusive */
455 opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:")
456 USE_GETPUT("?g--p:p--g");
458 USE_GETPUT(cmd =) getopt32(argc, argv,
459 USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p")
460 "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"),
461 &localfile, &remotefile
462 USE_FEATURE_TFTP_BLOCKSIZE(, &sblocksize));
466 flags = O_WRONLY | O_CREAT | O_TRUNC;
468 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
470 blocksize = xatoi_u(sblocksize);
471 if (!tftp_blocksize_check(blocksize, 0)) {
477 if (localfile == NULL)
478 localfile = remotefile;
479 if (remotefile == NULL)
480 remotefile = localfile;
481 if ((localfile == NULL && remotefile == NULL) || (argv[optind] == NULL))
484 if (localfile == NULL || LONE_DASH(localfile)) {
485 fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO;
487 fd = xopen3(localfile, flags, 0644);
490 port = bb_lookup_port(argv[optind + 1], "udp", 69);
491 peer_lsa = host2sockaddr(argv[optind], port);
493 #if ENABLE_DEBUG_TFTP
494 fprintf(stderr, "using server \"%s\", "
495 "remotefile \"%s\", localfile \"%s\".\n",
496 xmalloc_sockaddr2dotted(&peer_lsa->sa, peer_lsa->len),
497 remotefile, localfile);
501 #if ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
504 peer_lsa, remotefile, fd, port, blocksize);
507 if (ENABLE_FEATURE_CLEAN_UP)
509 if (CMD_GET(cmd) && result != EXIT_SUCCESS)
515 #endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */