1 /* ------------------------------------------------------------------------- */
4 /* A simple tftp client for busybox. */
5 /* Tries to follow RFC1350. */
6 /* Only "octet" mode supported. */
7 /* Optional blocksize negotiation (RFC2347 + RFC2348) */
9 /* Copyright (C) 2001 Magnus Damm <damm@opensource.se> */
11 /* Parts of the code based on: */
13 /* atftp: Copyright (C) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca> */
14 /* and Remi Lefebvre <remi@debian.org> */
16 /* utftp: Copyright (C) 1999 Uwe Ohse <uwe@ohse.de> */
18 /* This program is free software; you can redistribute it and/or modify */
19 /* it under the terms of the GNU General Public License as published by */
20 /* the Free Software Foundation; either version 2 of the License, or */
21 /* (at your option) any later version. */
23 /* This program is distributed in the hope that it will be useful, */
24 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
25 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
26 /* General Public License for more details. */
28 /* You should have received a copy of the GNU General Public License */
29 /* along with this program; if not, write to the Free Software */
30 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
32 /* ------------------------------------------------------------------------- */
37 #include <sys/types.h>
38 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
49 //#define CONFIG_FEATURE_TFTP_DEBUG
51 #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
52 #define TFTP_TIMEOUT 5 /* seconds */
54 /* opcodes we support */
63 static const char *tftp_bb_error_msg[] = {
67 "Disk full or allocation error",
68 "Illegal TFTP operation",
69 "Unknown transfer ID",
70 "File already exists",
74 const int tftp_cmd_get = 1;
75 const int tftp_cmd_put = 2;
77 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
79 static int tftp_blocksize_check(int blocksize, int bufsize)
81 /* Check if the blocksize is valid:
82 * RFC2348 says between 8 and 65464,
83 * but our implementation makes it impossible
84 * to use blocksizes smaller than 22 octets.
87 if ((bufsize && (blocksize > bufsize)) ||
88 (blocksize < 8) || (blocksize > 65464)) {
89 bb_error_msg("bad blocksize");
96 static char *tftp_option_get(char *buf, int len, char *option)
104 /* Make sure the options are terminated correctly */
106 for (k = 0; k < len; k++) {
107 if (buf[k] == '\0') {
117 if (strcasecmp(buf, option) == 0) {
140 static inline int tftp(const int cmd, const struct hostent *host,
141 const char *remotefile, int localfd, const int port, int tftp_bufsize)
143 const int cmd_get = cmd & tftp_cmd_get;
144 const int cmd_put = cmd & tftp_cmd_put;
145 const int bb_tftp_num_retries = 5;
147 struct sockaddr_in sa;
148 struct sockaddr_in from;
158 int timeout = bb_tftp_num_retries;
161 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
162 int want_option_ack = 0;
165 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
166 * size varies meaning BUFFERS_GO_ON_STACK would fail */
167 char *buf=xmalloc(tftp_bufsize + 4);
171 if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
172 bb_perror_msg("socket");
179 bind(socketfd, (struct sockaddr *)&sa, len);
181 sa.sin_family = host->h_addrtype;
182 sa.sin_port = htons(port);
183 memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr,
184 sizeof(sa.sin_addr));
200 /* first create the opcode part */
202 *((unsigned short *) cp) = htons(opcode);
206 /* add filename and mode */
208 if ((cmd_get && (opcode == TFTP_RRQ)) ||
209 (cmd_put && (opcode == TFTP_WRQ))) {
212 /* see if the filename fits into buf */
213 /* and fill in packet */
215 len = strlen(remotefile) + 1;
217 if ((cp + len) >= &buf[tftp_bufsize - 1]) {
221 safe_strncpy(cp, remotefile, len);
225 if (too_long || ((&buf[tftp_bufsize - 1] - cp) < 6)) {
226 bb_error_msg("too long remote-filename");
230 /* add "mode" part of the package */
232 memcpy(cp, "octet", 6);
235 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
237 len = tftp_bufsize - 4; /* data block size */
239 if (len != TFTP_BLOCKSIZE_DEFAULT) {
241 if ((&buf[tftp_bufsize - 1] - cp) < 15) {
242 bb_error_msg("too long remote-filename");
246 /* add "blksize" + number of blocks */
248 memcpy(cp, "blksize", 8);
251 cp += snprintf(cp, 6, "%d", len) + 1;
258 /* add ack and data */
260 if ((cmd_get && (opcode == TFTP_ACK)) ||
261 (cmd_put && (opcode == TFTP_DATA))) {
263 *((unsigned short *) cp) = htons(block_nr);
269 if (cmd_put && (opcode == TFTP_DATA)) {
270 len = read(localfd, cp, tftp_bufsize - 4);
273 bb_perror_msg("read");
277 if (len != (tftp_bufsize - 4)) {
289 timeout = bb_tftp_num_retries; /* re-initialize */
294 #ifdef CONFIG_FEATURE_TFTP_DEBUG
295 printf("sending %u bytes\n", len);
296 for (cp = buf; cp < &buf[len]; cp++)
297 printf("%02x ", *cp);
300 if (sendto(socketfd, buf, len, 0,
301 (struct sockaddr *) &sa, sizeof(sa)) < 0) {
302 bb_perror_msg("send");
308 if (finished && (opcode == TFTP_ACK)) {
314 memset(&from, 0, sizeof(from));
315 fromlen = sizeof(from);
317 tv.tv_sec = TFTP_TIMEOUT;
321 FD_SET(socketfd, &rfds);
323 switch (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) {
325 len = recvfrom(socketfd, buf, tftp_bufsize, 0,
326 (struct sockaddr *) &from, &fromlen);
329 bb_perror_msg("recvfrom");
335 if (sa.sin_port == htons(port)) {
336 sa.sin_port = from.sin_port;
338 if (sa.sin_port == from.sin_port) {
342 /* fall-through for bad packets! */
343 /* discard the packet - treat as timeout */
344 timeout = bb_tftp_num_retries;
347 bb_error_msg("timeout");
352 bb_error_msg("last timeout");
357 bb_perror_msg("select");
361 } while (timeout && (len >= 0));
363 if ((finished) || (len < 0)) {
367 /* process received packet */
370 opcode = ntohs(*((unsigned short *) buf));
371 tmp = ntohs(*((unsigned short *) &buf[2]));
373 #ifdef CONFIG_FEATURE_TFTP_DEBUG
374 printf("received %d bytes: %04x %04x\n", len, opcode, tmp);
377 if (opcode == TFTP_ERROR) {
380 if (buf[4] != '\0') {
382 buf[tftp_bufsize - 1] = '\0';
383 } else if (tmp < (sizeof(tftp_bb_error_msg)
386 msg = (char *) tftp_bb_error_msg[tmp];
390 bb_error_msg("server says: %s", msg);
396 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
397 if (want_option_ack) {
401 if (opcode == TFTP_OACK) {
403 /* server seems to support options */
407 res = tftp_option_get(&buf[2], len-2,
411 int blksize = atoi(res);
413 if (tftp_blocksize_check(blksize,
422 #ifdef CONFIG_FEATURE_TFTP_DEBUG
423 printf("using blksize %u\n", blksize);
425 tftp_bufsize = blksize + 4;
431 * we should send ERROR 8 */
432 bb_error_msg("bad server option");
436 bb_error_msg("warning: blksize not supported by server"
437 " - reverting to 512");
439 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
443 if (cmd_get && (opcode == TFTP_DATA)) {
445 if (tmp == block_nr) {
447 len = write(localfd, &buf[4], len - 4);
450 bb_perror_msg("write");
454 if (len != (tftp_bufsize - 4)) {
463 if (cmd_put && (opcode == TFTP_ACK)) {
465 if (tmp == (block_nr - 1)) {
476 #ifdef CONFIG_FEATURE_CLEAN_UP
482 return finished ? EXIT_SUCCESS : EXIT_FAILURE;
485 int tftp_main(int argc, char **argv)
487 struct hostent *host = NULL;
488 char *localfile = NULL;
489 char *remotefile = NULL;
496 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
498 /* figure out what to pass to getopt */
500 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
506 #ifdef CONFIG_FEATURE_TFTP_GET
512 #ifdef CONFIG_FEATURE_TFTP_PUT
518 while ((opt = getopt(argc, argv, BS GET PUT "l:r:")) != -1) {
520 #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE
522 blocksize = atoi(optarg);
523 if (!tftp_blocksize_check(blocksize, 0)) {
528 #ifdef CONFIG_FEATURE_TFTP_GET
531 flags = O_WRONLY | O_CREAT;
534 #ifdef CONFIG_FEATURE_TFTP_PUT
541 localfile = bb_xstrdup(optarg);
544 remotefile = bb_xstrdup(optarg);
549 if ((cmd == 0) || (optind == argc)) {
552 if(localfile && strcmp(localfile, "-") == 0) {
553 fd = fileno((cmd==tftp_cmd_get)? stdout : stdin);
555 if(localfile == NULL)
556 localfile = remotefile;
557 if(remotefile == NULL)
558 remotefile = localfile;
560 fd = open(localfile, flags, 0644);
563 bb_perror_msg_and_die("local file");
566 host = xgethostbyname(argv[optind]);
568 if (optind + 2 == argc) {
569 port = atoi(argv[optind + 1]);
572 #ifdef CONFIG_FEATURE_TFTP_DEBUG
573 printf("using server \"%s\", remotefile \"%s\", "
574 "localfile \"%s\".\n",
575 inet_ntoa(*((struct in_addr *) host->h_addr)),
576 remotefile, localfile);
579 result = tftp(cmd, host, remotefile, fd, port, blocksize);
581 #ifdef CONFIG_FEATURE_CLEAN_UP
582 if (!(fd == fileno(stdout) || fd == fileno(stdin))) {