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 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
37 //#define CONFIG_FEATURE_TFTP_DEBUG
39 #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
40 #define TFTP_TIMEOUT 5 /* seconds */
42 /* opcodes we support */
51 static const char * const tftp_bb_error_msg[] = {
55 "Disk full or allocation error",
56 "Illegal TFTP operation",
57 "Unknown transfer ID",
58 "File already exists",
62 #ifdef CONFIG_FEATURE_TFTP_GET
63 # define tftp_cmd_get 1
65 # define tftp_cmd_get 0
67 #ifdef CONFIG_FEATURE_TFTP_PUT
68 # define tftp_cmd_put (tftp_cmd_get+1)
70 # define tftp_cmd_put 0
74 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
76 static int tftp_blocksize_check(int blocksize, int bufsize)
78 /* Check if the blocksize is valid:
79 * RFC2348 says between 8 and 65464,
80 * but our implementation makes it impossible
81 * to use blocksizes smaller than 22 octets.
84 if ((bufsize && (blocksize > bufsize)) ||
85 (blocksize < 8) || (blocksize > 65464)) {
86 bb_error_msg("bad blocksize");
93 static char *tftp_option_get(char *buf, int len, char *option)
101 /* Make sure the options are terminated correctly */
103 for (k = 0; k < len; k++) {
104 if (buf[k] == '\0') {
114 if (strcasecmp(buf, option) == 0) {
137 static inline int tftp(const int cmd, const struct hostent *host,
138 const char *remotefile, int localfd, const unsigned short port, int tftp_bufsize)
140 const int cmd_get = cmd & tftp_cmd_get;
141 const int cmd_put = cmd & tftp_cmd_put;
142 const int bb_tftp_num_retries = 5;
144 struct sockaddr_in sa;
145 struct sockaddr_in from;
155 int timeout = bb_tftp_num_retries;
156 unsigned short block_nr = 1;
158 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
159 int want_option_ack = 0;
162 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
163 * size varies meaning BUFFERS_GO_ON_STACK would fail */
164 char *buf=xmalloc(tftp_bufsize + 4);
168 if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { /* bb_xsocket? */
169 bb_perror_msg("socket");
176 bind(socketfd, (struct sockaddr *)&sa, len);
178 sa.sin_family = host->h_addrtype;
180 memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr,
181 sizeof(sa.sin_addr));
197 /* first create the opcode part */
199 *((unsigned short *) cp) = htons(opcode);
203 /* add filename and mode */
205 if ((cmd_get && (opcode == TFTP_RRQ)) ||
206 (cmd_put && (opcode == TFTP_WRQ))) {
209 /* see if the filename fits into buf */
210 /* and fill in packet */
212 len = strlen(remotefile) + 1;
214 if ((cp + len) >= &buf[tftp_bufsize - 1]) {
218 safe_strncpy(cp, remotefile, len);
222 if (too_long || ((&buf[tftp_bufsize - 1] - cp) < 6)) {
223 bb_error_msg("too long remote-filename");
227 /* add "mode" part of the package */
229 memcpy(cp, "octet", 6);
232 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
234 len = tftp_bufsize - 4; /* data block size */
236 if (len != TFTP_BLOCKSIZE_DEFAULT) {
238 if ((&buf[tftp_bufsize - 1] - cp) < 15) {
239 bb_error_msg("too long remote-filename");
243 /* add "blksize" + number of blocks */
245 memcpy(cp, "blksize", 8);
248 cp += snprintf(cp, 6, "%d", len) + 1;
255 /* add ack and data */
257 if ((cmd_get && (opcode == TFTP_ACK)) ||
258 (cmd_put && (opcode == TFTP_DATA))) {
260 *((unsigned short *) cp) = htons(block_nr);
266 if (cmd_put && (opcode == TFTP_DATA)) {
267 len = bb_full_read(localfd, cp, tftp_bufsize - 4);
270 bb_perror_msg("read");
274 if (len != (tftp_bufsize - 4)) {
286 timeout = bb_tftp_num_retries; /* re-initialize */
291 #ifdef CONFIG_FEATURE_TFTP_DEBUG
292 fprintf(stderr, "sending %u bytes\n", len);
293 for (cp = buf; cp < &buf[len]; cp++)
294 fprintf(stderr, "%02x ", (unsigned char)*cp);
295 fprintf(stderr, "\n");
297 if (sendto(socketfd, buf, len, 0,
298 (struct sockaddr *) &sa, sizeof(sa)) < 0) {
299 bb_perror_msg("send");
305 if (finished && (opcode == TFTP_ACK)) {
311 memset(&from, 0, sizeof(from));
312 fromlen = sizeof(from);
314 tv.tv_sec = TFTP_TIMEOUT;
318 FD_SET(socketfd, &rfds);
320 switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
322 len = recvfrom(socketfd, buf, tftp_bufsize, 0,
323 (struct sockaddr *) &from, &fromlen);
326 bb_perror_msg("recvfrom");
332 if (sa.sin_port == port) {
333 sa.sin_port = from.sin_port;
335 if (sa.sin_port == from.sin_port) {
339 /* fall-through for bad packets! */
340 /* discard the packet - treat as timeout */
341 timeout = bb_tftp_num_retries;
344 bb_error_msg("timeout");
349 bb_error_msg("last timeout");
354 bb_perror_msg("select");
358 } while (timeout && (len >= 0));
360 if ((finished) || (len < 0)) {
364 /* process received packet */
367 opcode = ntohs(*((unsigned short *) buf));
368 tmp = ntohs(*((unsigned short *) &buf[2]));
370 #ifdef CONFIG_FEATURE_TFTP_DEBUG
371 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp);
374 if (opcode == TFTP_ERROR) {
375 const char *msg = NULL;
377 if (buf[4] != '\0') {
379 buf[tftp_bufsize - 1] = '\0';
380 } else if (tmp < (sizeof(tftp_bb_error_msg)
383 msg = tftp_bb_error_msg[tmp];
387 bb_error_msg("server says: %s", msg);
393 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
394 if (want_option_ack) {
398 if (opcode == TFTP_OACK) {
400 /* server seems to support options */
404 res = tftp_option_get(&buf[2], len-2,
408 int blksize = atoi(res);
410 if (tftp_blocksize_check(blksize,
419 #ifdef CONFIG_FEATURE_TFTP_DEBUG
420 fprintf(stderr, "using blksize %u\n", blksize);
422 tftp_bufsize = blksize + 4;
428 * we should send ERROR 8 */
429 bb_error_msg("bad server option");
433 bb_error_msg("warning: blksize not supported by server"
434 " - reverting to 512");
436 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
440 if (cmd_get && (opcode == TFTP_DATA)) {
442 if (tmp == block_nr) {
444 len = bb_full_write(localfd, &buf[4], len - 4);
447 bb_perror_msg("write");
451 if (len != (tftp_bufsize - 4)) {
458 /* in case the last ack disappeared into the ether */
459 if ( tmp == (block_nr - 1) ) {
463 } else if (tmp + 1 == block_nr) {
464 /* Server lost our TFTP_ACK. Resend it */
471 if (cmd_put && (opcode == TFTP_ACK)) {
473 if (tmp == (unsigned short)(block_nr - 1)) {
484 #ifdef CONFIG_FEATURE_CLEAN_UP
490 return finished ? EXIT_SUCCESS : EXIT_FAILURE;
493 int tftp_main(int argc, char **argv)
495 struct hostent *host = NULL;
496 const char *localfile = NULL;
497 const char *remotefile = NULL;
503 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
505 /* figure out what to pass to getopt */
507 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
508 char *sblocksize = NULL;
510 #define BS_ARG , &sblocksize
516 #ifdef CONFIG_FEATURE_TFTP_GET
518 #define GET_COMPL ":g"
524 #ifdef CONFIG_FEATURE_TFTP_PUT
526 #define PUT_COMPL ":p"
532 #if defined(CONFIG_FEATURE_TFTP_GET) && defined(CONFIG_FEATURE_TFTP_PUT)
533 bb_opt_complementally = GET_COMPL PUT_COMPL ":?g--p:p--g";
534 #elif defined(CONFIG_FEATURE_TFTP_GET) || defined(CONFIG_FEATURE_TFTP_PUT)
535 bb_opt_complementally = GET_COMPL PUT_COMPL;
537 /* XXX: may be should #error ? */
541 cmd = bb_getopt_ulflags(argc, argv, GET PUT "l:r:" BS,
542 &localfile, &remotefile BS_ARG);
543 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
545 blocksize = atoi(sblocksize);
546 if (!tftp_blocksize_check(blocksize, 0)) {
552 cmd &= (tftp_cmd_get | tftp_cmd_put);
553 #ifdef CONFIG_FEATURE_TFTP_GET
554 if(cmd == tftp_cmd_get)
555 flags = O_WRONLY | O_CREAT | O_TRUNC;
557 #ifdef CONFIG_FEATURE_TFTP_PUT
558 if(cmd == tftp_cmd_put)
562 if(localfile == NULL)
563 localfile = remotefile;
564 if(remotefile == NULL)
565 remotefile = localfile;
566 /* XXX: I corrected this, but may be wrong too. vodz */
567 if(localfile==NULL || strcmp(localfile, "-") == 0) {
568 fd = fileno((cmd==tftp_cmd_get)? stdout : stdin);
570 fd = open(localfile, flags, 0644);
573 bb_perror_msg_and_die("local file");
576 /* XXX: argv[optind] and/or argv[optind + 1] may be NULL! */
577 host = xgethostbyname(argv[optind]);
578 port = bb_lookup_port(argv[optind + 1], "udp", 69);
580 #ifdef CONFIG_FEATURE_TFTP_DEBUG
581 fprintf(stderr, "using server \"%s\", remotefile \"%s\", "
582 "localfile \"%s\".\n",
583 inet_ntoa(*((struct in_addr *) host->h_addr)),
584 remotefile, localfile);
587 result = tftp(cmd, host, remotefile, fd, port, blocksize);
589 #ifdef CONFIG_FEATURE_CLEAN_UP
590 if (!(fd == STDOUT_FILENO || fd == STDIN_FILENO)) {