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 #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */
26 #define TFTP_TIMEOUT 5 /* seconds */
27 #define TFTP_NUM_RETRIES 5 /* number of retries */
29 /* opcodes we support */
37 static const char *const tftp_bb_error_msg[] = {
41 "Disk full or allocation error",
42 "Illegal TFTP operation",
43 "Unknown transfer ID",
44 "File already exists",
48 #if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT
50 #define CMD_GET(cmd) 1
51 #define CMD_PUT(cmd) 0
52 #elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
54 #define CMD_GET(cmd) 0
55 #define CMD_PUT(cmd) 1
57 #define USE_GETPUT(a) a
58 /* masks coming from getpot32 */
59 #define CMD_GET(cmd) (cmd & 1)
60 #define CMD_PUT(cmd) (cmd & 2)
62 /* NB: in the code below
63 * CMD_GET(cmd) and CMD_GET(cmd) are mutually exclusive
67 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
69 static int tftp_blocksize_check(int blocksize, int bufsize)
71 /* Check if the blocksize is valid:
72 * RFC2348 says between 8 and 65464,
73 * but our implementation makes it impossible
74 * to use blocksizes smaller than 22 octets.
77 if ((bufsize && (blocksize > bufsize))
78 || (blocksize < 8) || (blocksize > 65564)
80 bb_error_msg("bad blocksize");
87 static char *tftp_option_get(char *buf, int len, const char * const option)
94 /* Make sure the options are terminated correctly */
96 for (k = 0; k < len; k++) {
107 if (strcasecmp(buf, option) == 0) {
130 #if ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
133 const len_and_sockaddr *peer_lsa,
134 const char *remotefile, const int localfd,
135 unsigned port, int tftp_bufsize)
143 int timeout = TFTP_NUM_RETRIES;
144 uint16_t block_nr = 1;
148 USE_FEATURE_TFTP_BLOCKSIZE(int want_option_ack = 0;)
150 /* Can't use RESERVE_CONFIG_BUFFER here since the allocation
151 * size varies meaning BUFFERS_GO_ON_STACK would fail */
152 /* We must keep the transmit and receive buffers seperate */
153 /* In case we rcv a garbage pkt and we need to rexmit the last pkt */
154 char *xbuf = xmalloc(tftp_bufsize += 4);
155 char *rbuf = xmalloc(tftp_bufsize);
159 socketfd = xsocket(peer_lsa->sa.sa_family, SOCK_DGRAM, 0);
171 /* first create the opcode part */
172 *((uint16_t*)cp) = htons(opcode);
175 /* add filename and mode */
176 if (CMD_GET(cmd) ? (opcode == TFTP_RRQ) : (opcode == TFTP_WRQ)) {
179 /* see if the filename fits into xbuf
180 * and fill in packet. */
181 len = strlen(remotefile) + 1;
183 if ((cp + len) >= &xbuf[tftp_bufsize - 1]) {
186 safe_strncpy(cp, remotefile, len);
190 if (too_long || (&xbuf[tftp_bufsize - 1] - cp) < sizeof("octet")) {
191 bb_error_msg("remote filename too long");
195 /* add "mode" part of the package */
196 memcpy(cp, "octet", sizeof("octet"));
197 cp += sizeof("octet");
199 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
201 len = tftp_bufsize - 4; /* data block size */
203 if (len != TFTP_BLOCKSIZE_DEFAULT) {
205 if ((&xbuf[tftp_bufsize - 1] - cp) < 15) {
206 bb_error_msg("remote filename too long");
210 /* add "blksize" + number of blocks */
211 memcpy(cp, "blksize", sizeof("blksize"));
212 cp += sizeof("blksize");
213 cp += snprintf(cp, 6, "%d", len) + 1;
220 /* add ack and data */
222 if (CMD_GET(cmd) ? (opcode == TFTP_ACK) : (opcode == TFTP_DATA)) {
223 *((uint16_t*)cp) = htons(block_nr);
227 if (CMD_PUT(cmd) && (opcode == TFTP_DATA)) {
228 len = full_read(localfd, cp, tftp_bufsize - 4);
231 bb_perror_msg(bb_msg_read_error);
235 if (len != (tftp_bufsize - 4)) {
245 timeout = TFTP_NUM_RETRIES; /* re-initialize */
248 #if ENABLE_DEBUG_TFTP
249 fprintf(stderr, "sending %u bytes\n", len);
250 for (cp = xbuf; cp < &xbuf[len]; cp++)
251 fprintf(stderr, "%02x ", (unsigned char) *cp);
252 fprintf(stderr, "\n");
254 if (sendto(socketfd, xbuf, len, 0,
255 &peer_lsa->sa, peer_lsa->len) < 0) {
256 bb_perror_msg("send");
261 if (finished && (opcode == TFTP_ACK)) {
267 tv.tv_sec = TFTP_TIMEOUT;
271 FD_SET(socketfd, &rfds);
273 switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) {
274 struct sockaddr *from;
278 fromlen = peer_lsa->len;
279 from = alloca(fromlen);
280 memset(from, 0, fromlen);
282 len = recvfrom(socketfd, rbuf, tftp_bufsize, 0,
285 bb_perror_msg("recvfrom");
288 #if ENABLE_FEATURE_IPV6
289 if (from->sa_family == AF_INET6)
290 if (((struct sockaddr_in6*)from)->sin6_port != port)
293 if (from->sa_family == AF_INET)
294 if (((struct sockaddr_in*)from)->sin_port != port)
299 bb_error_msg("timeout");
303 bb_error_msg("last timeout");
307 bb_perror_msg("select");
311 } while (timeout && (len >= 0));
313 if (finished || (len < 0)) {
317 /* process received packet */
319 opcode = ntohs( ((uint16_t*)rbuf)[0] );
320 tmp = ntohs( ((uint16_t*)rbuf)[1] );
322 #if ENABLE_DEBUG_TFTP
323 fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp);
326 if (opcode == TFTP_ERROR) {
327 const char *msg = NULL;
329 if (rbuf[4] != '\0') {
331 rbuf[tftp_bufsize - 1] = '\0';
332 } else if (tmp < (sizeof(tftp_bb_error_msg)
334 msg = tftp_bb_error_msg[tmp];
338 bb_error_msg("server says: %s", msg);
343 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
344 if (want_option_ack) {
348 if (opcode == TFTP_OACK) {
349 /* server seems to support options */
352 res = tftp_option_get(&rbuf[2], len - 2, "blksize");
355 int blksize = xatoi_u(res);
357 if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) {
363 #if ENABLE_DEBUG_TFTP
364 fprintf(stderr, "using blksize %u\n",
367 tftp_bufsize = blksize + 4;
373 * we should send ERROR 8 */
374 bb_error_msg("bad server option");
378 bb_error_msg("warning: blksize not supported by server"
379 " - reverting to 512");
381 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
385 if (CMD_GET(cmd) && (opcode == TFTP_DATA)) {
386 if (tmp == block_nr) {
387 len = full_write(localfd, &rbuf[4], len - 4);
390 bb_perror_msg(bb_msg_write_error);
394 if (len != (tftp_bufsize - 4)) {
401 /* in case the last ack disappeared into the ether */
402 if (tmp == (block_nr - 1)) {
406 // tmp==(block_nr-1) and (tmp+1)==block_nr is always same, I think. wtf?
407 } else if (tmp + 1 == block_nr) {
408 /* Server lost our TFTP_ACK. Resend it */
415 if (CMD_PUT(cmd) && (opcode == TFTP_ACK)) {
416 if (tmp == (uint16_t) (block_nr - 1)) {
427 if (ENABLE_FEATURE_CLEAN_UP) {
433 return finished ? EXIT_SUCCESS : EXIT_FAILURE;
436 int tftp_main(int argc, char **argv)
438 len_and_sockaddr *peer_lsa;
439 const char *localfile = NULL;
440 const char *remotefile = NULL;
441 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
442 const char *sblocksize = NULL;
449 int blocksize = TFTP_BLOCKSIZE_DEFAULT;
451 /* -p or -g is mandatory, and they are mutually exclusive */
452 opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:")
453 USE_GETPUT("?g--p:p--g");
455 USE_GETPUT(cmd =) getopt32(argc, argv,
456 USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p")
457 "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"),
458 &localfile, &remotefile
459 USE_FEATURE_TFTP_BLOCKSIZE(, &sblocksize));
463 flags = O_WRONLY | O_CREAT | O_TRUNC;
465 #if ENABLE_FEATURE_TFTP_BLOCKSIZE
467 blocksize = xatoi_u(sblocksize);
468 if (!tftp_blocksize_check(blocksize, 0)) {
474 if (localfile == NULL)
475 localfile = remotefile;
476 if (remotefile == NULL)
477 remotefile = localfile;
478 if ((localfile == NULL && remotefile == NULL) || (argv[optind] == NULL))
481 if (localfile == NULL || LONE_DASH(localfile)) {
482 fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO;
484 fd = xopen3(localfile, flags, 0644);
487 port = bb_lookup_port(argv[optind + 1], "udp", 69);
488 peer_lsa = host2sockaddr(argv[optind], port);
490 #if ENABLE_DEBUG_TFTP
491 fprintf(stderr, "using server \"%s\", "
492 "remotefile \"%s\", localfile \"%s\".\n",
493 xmalloc_sockaddr2dotted(&peer_lsa->sa, peer_lsa->len),
494 remotefile, localfile);
498 #if ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT
501 peer_lsa, remotefile, fd, port, blocksize);
504 if (ENABLE_FEATURE_CLEAN_UP)
506 if (CMD_GET(cmd) && result != EXIT_SUCCESS)