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>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
37 #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
38 #define TFTP_TIMEOUT 5 /* seconds */
39 #define TFTP_NUM_RETRIES 5 /* number of retries */
41 /* RFC2348 says between 8 and 65464 */
42 #define TFTP_OCTECTS_MIN 8
43 #define TFTP_OCTECTS_MAX 65464
45 static const char * const MODE_OCTET = "octet";
46 #define MODE_OCTET_LEN 6 /* sizeof(MODE_OCTET)*/
48 static const char * const OPTION_BLOCKSIZE = "blksize";
49 #define OPTION_BLOCKSIZE_LEN 8 /* sizeof(OPTION_BLOCKSIZE) */
51 /* opcodes we support */
59 static const char *const tftp_bb_error_msg[] = {
63 "Disk full or allocation error",
64 "Illegal TFTP operation",
65 "Unknown transfer ID",
66 "File already exists",
70 #define tftp_cmd_get ENABLE_FEATURE_TFTP_GET
72 #if ENABLE_FEATURE_TFTP_PUT
73 # define tftp_cmd_put (tftp_cmd_get+ENABLE_FEATURE_TFTP_PUT)
75 # define tftp_cmd_put 0
79 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
81 static int tftp_blocksize_check(int blocksize, int bufsize)
83 /* Check if the blocksize is valid:
84 * RFC2348 says between 8 and 65464,
85 * but our implementation makes it impossible
86 * to use blocksizes smaller than 22 octets.
89 if ((bufsize && (blocksize > bufsize)) ||
90 (blocksize < TFTP_OCTECTS_MIN) || (blocksize > TFTP_OCTECTS_MAX)) {
91 bb_error_msg("bad blocksize");
98 static char *tftp_option_get(char *buf, int len, const char * const option)
106 /* Make sure the options are terminated correctly */
108 for (k = 0; k < len; k++) {
109 if (buf[k] == '\0') {
119 if (strcasecmp(buf, option) == 0) {
141 static int tftp(const int cmd, const struct hostent *host,
142 const char *remotefile, const int localfd,
143 const unsigned short port, int tftp_bufsize)
145 struct sockaddr_in sa;
146 struct sockaddr_in from;
154 int timeout = TFTP_NUM_RETRIES;
155 unsigned short block_nr = 1;
159 USE_FEATURE_TFTP_BLOCKSIZE(int want_option_ack = 0;)
161 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
162 * size varies meaning BUFFERS_GO_ON_STACK would fail */
163 char *buf=xmalloc(tftp_bufsize += 4);
165 if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
166 /* need to unlink the localfile, so don't use bb_xsocket here. */
167 bb_perror_msg("socket");
174 bb_xbind(socketfd, (struct sockaddr *)&sa, len);
176 sa.sin_family = host->h_addrtype;
178 memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr,
179 sizeof(sa.sin_addr));
182 if (cmd & tftp_cmd_get) {
185 if (cmd & tftp_cmd_put) {
193 /* first create the opcode part */
194 *((unsigned short *) cp) = htons(opcode);
197 /* add filename and mode */
198 if (((cmd & tftp_cmd_get) && (opcode == TFTP_RRQ)) ||
199 ((cmd & tftp_cmd_put) && (opcode == TFTP_WRQ)))
203 /* see if the filename fits into buf
204 * and fill in packet. */
205 len = strlen(remotefile) + 1;
207 if ((cp + len) >= &buf[tftp_bufsize - 1]) {
210 safe_strncpy(cp, remotefile, len);
214 if (too_long || ((&buf[tftp_bufsize - 1] - cp) < MODE_OCTET_LEN)) {
215 bb_error_msg("remote filename too long");
219 /* add "mode" part of the package */
220 memcpy(cp, MODE_OCTET, MODE_OCTET_LEN);
221 cp += MODE_OCTET_LEN;
223 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
225 len = tftp_bufsize - 4; /* data block size */
227 if (len != TFTP_BLOCKSIZE_DEFAULT) {
229 if ((&buf[tftp_bufsize - 1] - cp) < 15) {
230 bb_error_msg("remote filename too long");
234 /* add "blksize" + number of blocks */
235 memcpy(cp, OPTION_BLOCKSIZE, OPTION_BLOCKSIZE_LEN);
236 cp += OPTION_BLOCKSIZE_LEN;
237 cp += snprintf(cp, 6, "%d", len) + 1;
244 /* add ack and data */
246 if (((cmd & tftp_cmd_get) && (opcode == TFTP_ACK)) ||
247 ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA))) {
249 *((unsigned short *) cp) = htons(block_nr);
255 if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) {
256 len = bb_full_read(localfd, cp, tftp_bufsize - 4);
259 bb_perror_msg(bb_msg_read_error);
263 if (len != (tftp_bufsize - 4)) {
275 timeout = TFTP_NUM_RETRIES; /* re-initialize */
280 #ifdef CONFIG_DEBUG_TFTP
281 fprintf(stderr, "sending %u bytes\n", len);
282 for (cp = buf; cp < &buf[len]; cp++)
283 fprintf(stderr, "%02x ", (unsigned char) *cp);
284 fprintf(stderr, "\n");
286 if (sendto(socketfd, buf, len, 0,
287 (struct sockaddr *) &sa, sizeof(sa)) < 0) {
288 bb_perror_msg("send");
294 if (finished && (opcode == TFTP_ACK)) {
300 memset(&from, 0, sizeof(from));
301 fromlen = sizeof(from);
303 tv.tv_sec = TFTP_TIMEOUT;
307 FD_SET(socketfd, &rfds);
309 switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
311 len = recvfrom(socketfd, buf, tftp_bufsize, 0,
312 (struct sockaddr *) &from, &fromlen);
315 bb_perror_msg("recvfrom");
321 if (sa.sin_port == port) {
322 sa.sin_port = from.sin_port;
324 if (sa.sin_port == from.sin_port) {
328 /* fall-through for bad packets! */
329 /* discard the packet - treat as timeout */
330 timeout = TFTP_NUM_RETRIES;
332 bb_error_msg("timeout");
337 bb_error_msg("last timeout");
341 bb_perror_msg("select");
345 } while (timeout && (len >= 0));
347 if ((finished) || (len < 0)) {
351 /* process received packet */
353 opcode = ntohs(*((unsigned short *) buf));
354 tmp = ntohs(*((unsigned short *) &buf[2]));
356 #ifdef CONFIG_DEBUG_TFTP
357 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp);
360 if (opcode == TFTP_ERROR) {
361 const char *msg = NULL;
363 if (buf[4] != '\0') {
365 buf[tftp_bufsize - 1] = '\0';
366 } else if (tmp < (sizeof(tftp_bb_error_msg)
369 msg = tftp_bb_error_msg[tmp];
373 bb_error_msg("server says: %s", msg);
378 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
379 if (want_option_ack) {
383 if (opcode == TFTP_OACK) {
385 /* server seems to support options */
389 res = tftp_option_get(&buf[2], len - 2, OPTION_BLOCKSIZE);
392 int blksize = atoi(res);
394 if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
396 if (cmd & tftp_cmd_put) {
401 #ifdef CONFIG_DEBUG_TFTP
402 fprintf(stderr, "using %s %u\n", OPTION_BLOCKSIZE,
405 tftp_bufsize = blksize + 4;
411 * we should send ERROR 8 */
412 bb_error_msg("bad server option");
416 bb_error_msg("warning: blksize not supported by server"
417 " - reverting to 512");
419 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
423 if ((cmd & tftp_cmd_get) && (opcode == TFTP_DATA)) {
425 if (tmp == block_nr) {
427 len = bb_full_write(localfd, &buf[4], len - 4);
430 bb_perror_msg(bb_msg_write_error);
434 if (len != (tftp_bufsize - 4)) {
441 /* in case the last ack disappeared into the ether */
442 if (tmp == (block_nr - 1)) {
446 } else if (tmp + 1 == block_nr) {
447 /* Server lost our TFTP_ACK. Resend it */
454 if ((cmd & tftp_cmd_put) && (opcode == TFTP_ACK)) {
456 if (tmp == (unsigned short) (block_nr - 1)) {
467 #ifdef CONFIG_FEATURE_CLEAN_UP
472 return finished ? EXIT_SUCCESS : EXIT_FAILURE;
475 int tftp_main(int argc, char **argv)
477 struct hostent *host = NULL;
478 const char *localfile = NULL;
479 const char *remotefile = NULL;
485 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
487 /* figure out what to pass to getopt */
489 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
490 char *sblocksize = NULL;
493 #define BS_ARG , &sblocksize
499 #ifdef CONFIG_FEATURE_TFTP_GET
501 #define GET_COMPL ":g"
507 #ifdef CONFIG_FEATURE_TFTP_PUT
509 #define PUT_COMPL ":p"
515 #if defined(CONFIG_FEATURE_TFTP_GET) && defined(CONFIG_FEATURE_TFTP_PUT)
516 bb_opt_complementally = GET_COMPL PUT_COMPL ":?g--p:p--g";
517 #elif defined(CONFIG_FEATURE_TFTP_GET) || defined(CONFIG_FEATURE_TFTP_PUT)
518 bb_opt_complementally = GET_COMPL PUT_COMPL;
522 cmd = bb_getopt_ulflags(argc, argv, GET PUT "l:r:" BS,
523 &localfile, &remotefile BS_ARG);
525 cmd &= (tftp_cmd_get | tftp_cmd_put);
526 #ifdef CONFIG_FEATURE_TFTP_GET
527 if (cmd == tftp_cmd_get)
528 flags = O_WRONLY | O_CREAT | O_TRUNC;
530 #ifdef CONFIG_FEATURE_TFTP_PUT
531 if (cmd == tftp_cmd_put)
535 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
537 blocksize = atoi(sblocksize);
538 if (!tftp_blocksize_check(blocksize, 0)) {
544 if (localfile == NULL)
545 localfile = remotefile;
546 if (remotefile == NULL)
547 remotefile = localfile;
548 if ((localfile == NULL && remotefile == NULL) || (argv[optind] == NULL))
551 if (localfile == NULL || strcmp(localfile, "-") == 0) {
552 fd = (cmd == tftp_cmd_get) ? STDOUT_FILENO : STDIN_FILENO;
554 fd = open(localfile, flags, 0644); /* fail below */
557 bb_perror_msg_and_die("local file");
560 host = xgethostbyname(argv[optind]);
561 port = bb_lookup_port(argv[optind + 1], "udp", 69);
563 #ifdef CONFIG_DEBUG_TFTP
564 fprintf(stderr, "using server \"%s\", remotefile \"%s\", "
565 "localfile \"%s\".\n",
566 inet_ntoa(*((struct in_addr *) host->h_addr)),
567 remotefile, localfile);
570 result = tftp(cmd, host, remotefile, fd, port, blocksize);
572 if (!(fd == STDOUT_FILENO || fd == STDIN_FILENO)) {
573 if (ENABLE_FEATURE_CLEAN_UP)
575 if (cmd == tftp_cmd_get && result != EXIT_SUCCESS)