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 static const char * const MODE_OCTET = "octet";
42 #define MODE_OCTET_LEN 6 /* sizeof(MODE_OCTET)*/
44 static const char * const OPTION_BLOCKSIZE = "blksize";
45 #define OPTION_BLOCKSIZE_LEN 8 /* sizeof(OPTION_BLOCKSIZE) */
47 /* opcodes we support */
55 static const char *const tftp_bb_error_msg[] = {
59 "Disk full or allocation error",
60 "Illegal TFTP operation",
61 "Unknown transfer ID",
62 "File already exists",
66 #define tftp_cmd_get ENABLE_FEATURE_TFTP_GET
68 #if ENABLE_FEATURE_TFTP_PUT
69 # define tftp_cmd_put (tftp_cmd_get+ENABLE_FEATURE_TFTP_PUT)
71 # define tftp_cmd_put 0
75 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
77 static int tftp_blocksize_check(int blocksize, int bufsize)
79 /* Check if the blocksize is valid:
80 * RFC2348 says between 8 and 65464,
81 * but our implementation makes it impossible
82 * to use blocksizes smaller than 22 octets.
85 if ((bufsize && (blocksize > bufsize)) ||
86 (blocksize < 8) || (blocksize > 65564)) {
87 bb_error_msg("bad blocksize");
94 static char *tftp_option_get(char *buf, int len, const char * const option)
102 /* Make sure the options are terminated correctly */
104 for (k = 0; k < len; k++) {
105 if (buf[k] == '\0') {
115 if (strcasecmp(buf, option) == 0) {
137 static int tftp(const int cmd, const struct hostent *host,
138 const char *remotefile, const int localfd,
139 const unsigned short port, int tftp_bufsize)
141 struct sockaddr_in sa;
142 struct sockaddr_in from;
150 int timeout = TFTP_NUM_RETRIES;
151 unsigned short block_nr = 1;
155 USE_FEATURE_TFTP_BLOCKSIZE(int want_option_ack = 0;)
157 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
158 * size varies meaning BUFFERS_GO_ON_STACK would fail */
159 char *buf=xmalloc(tftp_bufsize += 4);
161 if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
162 /* need to unlink the localfile, so don't use bb_xsocket here. */
163 bb_perror_msg("socket");
170 bb_xbind(socketfd, (struct sockaddr *)&sa, len);
172 sa.sin_family = host->h_addrtype;
174 memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr,
175 sizeof(sa.sin_addr));
178 if (cmd & tftp_cmd_get) {
181 if (cmd & tftp_cmd_put) {
189 /* first create the opcode part */
190 *((unsigned short *) cp) = htons(opcode);
193 /* add filename and mode */
194 if (((cmd & tftp_cmd_get) && (opcode == TFTP_RRQ)) ||
195 ((cmd & tftp_cmd_put) && (opcode == TFTP_WRQ)))
199 /* see if the filename fits into buf
200 * and fill in packet. */
201 len = strlen(remotefile) + 1;
203 if ((cp + len) >= &buf[tftp_bufsize - 1]) {
206 safe_strncpy(cp, remotefile, len);
210 if (too_long || ((&buf[tftp_bufsize - 1] - cp) < MODE_OCTET_LEN)) {
211 bb_error_msg("remote filename too long");
215 /* add "mode" part of the package */
216 memcpy(cp, MODE_OCTET, MODE_OCTET_LEN);
217 cp += MODE_OCTET_LEN;
219 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
221 len = tftp_bufsize - 4; /* data block size */
223 if (len != TFTP_BLOCKSIZE_DEFAULT) {
225 if ((&buf[tftp_bufsize - 1] - cp) < 15) {
226 bb_error_msg("remote filename too long");
230 /* add "blksize" + number of blocks */
231 memcpy(cp, OPTION_BLOCKSIZE, OPTION_BLOCKSIZE_LEN);
232 cp += OPTION_BLOCKSIZE_LEN;
233 cp += snprintf(cp, 6, "%d", len) + 1;
240 /* add ack and data */
242 if (((cmd & tftp_cmd_get) && (opcode == TFTP_ACK)) ||
243 ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA))) {
245 *((unsigned short *) cp) = htons(block_nr);
251 if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) {
252 len = bb_full_read(localfd, cp, tftp_bufsize - 4);
255 bb_perror_msg(bb_msg_read_error);
259 if (len != (tftp_bufsize - 4)) {
271 timeout = TFTP_NUM_RETRIES; /* re-initialize */
276 #ifdef CONFIG_DEBUG_TFTP
277 fprintf(stderr, "sending %u bytes\n", len);
278 for (cp = buf; cp < &buf[len]; cp++)
279 fprintf(stderr, "%02x ", (unsigned char) *cp);
280 fprintf(stderr, "\n");
282 if (sendto(socketfd, buf, len, 0,
283 (struct sockaddr *) &sa, sizeof(sa)) < 0) {
284 bb_perror_msg("send");
290 if (finished && (opcode == TFTP_ACK)) {
296 memset(&from, 0, sizeof(from));
297 fromlen = sizeof(from);
299 tv.tv_sec = TFTP_TIMEOUT;
303 FD_SET(socketfd, &rfds);
305 switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
307 len = recvfrom(socketfd, buf, tftp_bufsize, 0,
308 (struct sockaddr *) &from, &fromlen);
311 bb_perror_msg("recvfrom");
317 if (sa.sin_port == port) {
318 sa.sin_port = from.sin_port;
320 if (sa.sin_port == from.sin_port) {
324 /* fall-through for bad packets! */
325 /* discard the packet - treat as timeout */
326 timeout = TFTP_NUM_RETRIES;
328 bb_error_msg("timeout");
333 bb_error_msg("last timeout");
337 bb_perror_msg("select");
341 } while (timeout && (len >= 0));
343 if ((finished) || (len < 0)) {
347 /* process received packet */
349 opcode = ntohs(*((unsigned short *) buf));
350 tmp = ntohs(*((unsigned short *) &buf[2]));
352 #ifdef CONFIG_DEBUG_TFTP
353 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp);
356 if (opcode == TFTP_ERROR) {
357 const char *msg = NULL;
359 if (buf[4] != '\0') {
361 buf[tftp_bufsize - 1] = '\0';
362 } else if (tmp < (sizeof(tftp_bb_error_msg)
365 msg = tftp_bb_error_msg[tmp];
369 bb_error_msg("server says: %s", msg);
374 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
375 if (want_option_ack) {
379 if (opcode == TFTP_OACK) {
381 /* server seems to support options */
385 res = tftp_option_get(&buf[2], len - 2, OPTION_BLOCKSIZE);
388 int blksize = atoi(res);
390 if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
392 if (cmd & tftp_cmd_put) {
397 #ifdef CONFIG_DEBUG_TFTP
398 fprintf(stderr, "using %s %u\n", OPTION_BLOCKSIZE,
401 tftp_bufsize = blksize + 4;
407 * we should send ERROR 8 */
408 bb_error_msg("bad server option");
412 bb_error_msg("warning: blksize not supported by server"
413 " - reverting to 512");
415 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
419 if ((cmd & tftp_cmd_get) && (opcode == TFTP_DATA)) {
421 if (tmp == block_nr) {
423 len = bb_full_write(localfd, &buf[4], len - 4);
426 bb_perror_msg(bb_msg_write_error);
430 if (len != (tftp_bufsize - 4)) {
437 /* in case the last ack disappeared into the ether */
438 if (tmp == (block_nr - 1)) {
442 } else if (tmp + 1 == block_nr) {
443 /* Server lost our TFTP_ACK. Resend it */
450 if ((cmd & tftp_cmd_put) && (opcode == TFTP_ACK)) {
452 if (tmp == (unsigned short) (block_nr - 1)) {
463 #ifdef CONFIG_FEATURE_CLEAN_UP
468 return finished ? EXIT_SUCCESS : EXIT_FAILURE;
471 int tftp_main(int argc, char **argv)
473 struct hostent *host = NULL;
474 const char *localfile = NULL;
475 const char *remotefile = NULL;
481 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
483 /* figure out what to pass to getopt */
485 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
486 char *sblocksize = NULL;
489 #define BS_ARG , &sblocksize
495 #ifdef CONFIG_FEATURE_TFTP_GET
497 #define GET_COMPL ":g"
503 #ifdef CONFIG_FEATURE_TFTP_PUT
505 #define PUT_COMPL ":p"
511 #if defined(CONFIG_FEATURE_TFTP_GET) && defined(CONFIG_FEATURE_TFTP_PUT)
512 bb_opt_complementally = GET_COMPL PUT_COMPL ":?g--p:p--g";
513 #elif defined(CONFIG_FEATURE_TFTP_GET) || defined(CONFIG_FEATURE_TFTP_PUT)
514 bb_opt_complementally = GET_COMPL PUT_COMPL;
518 cmd = bb_getopt_ulflags(argc, argv, GET PUT "l:r:" BS,
519 &localfile, &remotefile BS_ARG);
521 cmd &= (tftp_cmd_get | tftp_cmd_put);
522 #ifdef CONFIG_FEATURE_TFTP_GET
523 if (cmd == tftp_cmd_get)
524 flags = O_WRONLY | O_CREAT | O_TRUNC;
526 #ifdef CONFIG_FEATURE_TFTP_PUT
527 if (cmd == tftp_cmd_put)
531 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
533 blocksize = atoi(sblocksize);
534 if (!tftp_blocksize_check(blocksize, 0)) {
540 if (localfile == NULL)
541 localfile = remotefile;
542 if (remotefile == NULL)
543 remotefile = localfile;
544 if ((localfile == NULL && remotefile == NULL) || (argv[optind] == NULL))
547 if (localfile == NULL || strcmp(localfile, "-") == 0) {
548 fd = (cmd == tftp_cmd_get) ? STDOUT_FILENO : STDIN_FILENO;
550 fd = open(localfile, flags, 0644); /* fail below */
553 bb_perror_msg_and_die("local file");
556 host = xgethostbyname(argv[optind]);
557 port = bb_lookup_port(argv[optind + 1], "udp", 69);
559 #ifdef CONFIG_DEBUG_TFTP
560 fprintf(stderr, "using server \"%s\", remotefile \"%s\", "
561 "localfile \"%s\".\n",
562 inet_ntoa(*((struct in_addr *) host->h_addr)),
563 remotefile, localfile);
566 result = tftp(cmd, host, remotefile, fd, port, blocksize);
568 if (!(fd == STDOUT_FILENO || fd == STDIN_FILENO)) {
569 if (ENABLE_FEATURE_CLEAN_UP)
571 if (cmd == tftp_cmd_get && result != EXIT_SUCCESS)