Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* ------------------------------------------------------------------------- |
| 3 | * tftp.c |
| 4 | * |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 5 | * A simple tftp client/server for busybox. |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 6 | * Tries to follow RFC1350. |
| 7 | * Only "octet" mode supported. |
| 8 | * Optional blocksize negotiation (RFC2347 + RFC2348) |
| 9 | * |
| 10 | * Copyright (C) 2001 Magnus Damm <damm@opensource.se> |
| 11 | * |
| 12 | * Parts of the code based on: |
| 13 | * |
| 14 | * atftp: Copyright (C) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca> |
| 15 | * and Remi Lefebvre <remi@debian.org> |
| 16 | * |
| 17 | * utftp: Copyright (C) 1999 Uwe Ohse <uwe@ohse.de> |
| 18 | * |
Denis Vlasenko | 78c5656 | 2008-03-18 00:11:46 +0000 | [diff] [blame] | 19 | * tftpd added by Denys Vlasenko & Vladimir Dronnikov |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 20 | * |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 21 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 22 | * ------------------------------------------------------------------------- */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 24 | #include "libbb.h" |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 26 | #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT |
| 27 | |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 28 | #define TFTP_BLKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ |
| 29 | #define TFTP_BLKSIZE_DEFAULT_STR "512" |
| 30 | #define TFTP_TIMEOUT_MS 50 |
| 31 | #define TFTP_MAXTIMEOUT_MS 2000 |
| 32 | #define TFTP_NUM_RETRIES 12 /* number of backed-off retries */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 33 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 34 | /* opcodes we support */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 35 | #define TFTP_RRQ 1 |
| 36 | #define TFTP_WRQ 2 |
| 37 | #define TFTP_DATA 3 |
| 38 | #define TFTP_ACK 4 |
| 39 | #define TFTP_ERROR 5 |
| 40 | #define TFTP_OACK 6 |
| 41 | |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 42 | /* error codes sent over network (we use only 0, 1, 3 and 8) */ |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 43 | /* generic (error message is included in the packet) */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 44 | #define ERR_UNSPEC 0 |
| 45 | #define ERR_NOFILE 1 |
| 46 | #define ERR_ACCESS 2 |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 47 | /* disk full or allocation exceeded */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 48 | #define ERR_WRITE 3 |
| 49 | #define ERR_OP 4 |
| 50 | #define ERR_BAD_ID 5 |
| 51 | #define ERR_EXIST 6 |
| 52 | #define ERR_BAD_USER 7 |
| 53 | #define ERR_BAD_OPT 8 |
| 54 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 55 | /* masks coming from getopt32 */ |
| 56 | enum { |
| 57 | TFTP_OPT_GET = (1 << 0), |
| 58 | TFTP_OPT_PUT = (1 << 1), |
| 59 | /* pseudo option: if set, it's tftpd */ |
| 60 | TFTPD_OPT = (1 << 7) * ENABLE_TFTPD, |
| 61 | TFTPD_OPT_r = (1 << 8) * ENABLE_TFTPD, |
| 62 | TFTPD_OPT_c = (1 << 9) * ENABLE_TFTPD, |
| 63 | TFTPD_OPT_u = (1 << 10) * ENABLE_TFTPD, |
| 64 | }; |
| 65 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 66 | #if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 67 | #define IF_GETPUT(...) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 68 | #define CMD_GET(cmd) 1 |
| 69 | #define CMD_PUT(cmd) 0 |
| 70 | #elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 71 | #define IF_GETPUT(...) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 72 | #define CMD_GET(cmd) 0 |
| 73 | #define CMD_PUT(cmd) 1 |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 74 | #else |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 75 | #define IF_GETPUT(...) __VA_ARGS__ |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 76 | #define CMD_GET(cmd) ((cmd) & TFTP_OPT_GET) |
| 77 | #define CMD_PUT(cmd) ((cmd) & TFTP_OPT_PUT) |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 78 | #endif |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 79 | /* NB: in the code below |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 80 | * CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 81 | */ |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 83 | |
| 84 | struct globals { |
| 85 | /* u16 TFTP_ERROR; u16 reason; both network-endian, then error text: */ |
| 86 | uint8_t error_pkt[4 + 32]; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 87 | char *user_opt; |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 88 | /* used in tftpd_main(), a bit big for stack: */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 89 | char block_buf[TFTP_BLKSIZE_DEFAULT]; |
| 90 | }; |
| 91 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 92 | #define block_buf (G.block_buf ) |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 93 | #define user_opt (G.user_opt ) |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 94 | #define error_pkt (G.error_pkt ) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 95 | #define INIT_G() do { } while (0) |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 96 | |
| 97 | #define error_pkt_reason (error_pkt[3]) |
| 98 | #define error_pkt_str (error_pkt + 4) |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 99 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 101 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 103 | static int tftp_blksize_check(const char *blksize_str, int maxsize) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 104 | { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 105 | /* Check if the blksize is valid: |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 106 | * RFC2348 says between 8 and 65464, |
| 107 | * but our implementation makes it impossible |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 108 | * to use blksizes smaller than 22 octets. */ |
| 109 | unsigned blksize = bb_strtou(blksize_str, NULL, 10); |
| 110 | if (errno |
| 111 | || (blksize < 24) || (blksize > maxsize) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 112 | ) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 113 | bb_error_msg("bad blocksize '%s'", blksize_str); |
| 114 | return -1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 115 | } |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 116 | #if ENABLE_TFTP_DEBUG |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 117 | bb_error_msg("using blksize %u", blksize); |
| 118 | #endif |
| 119 | return blksize; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 122 | static char *tftp_get_option(const char *option, char *buf, int len) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 123 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 124 | int opt_val = 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 125 | int opt_found = 0; |
| 126 | int k; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 128 | /* buf points to: |
| 129 | * "opt_name<NUL>opt_val<NUL>opt_name2<NUL>opt_val2<NUL>..." */ |
| 130 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 131 | while (len > 0) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 132 | /* Make sure options are terminated correctly */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 133 | for (k = 0; k < len; k++) { |
| 134 | if (buf[k] == '\0') { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 135 | goto nul_found; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 138 | return NULL; |
| 139 | nul_found: |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 140 | if (opt_val == 0) { /* it's "name" part */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 141 | if (strcasecmp(buf, option) == 0) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 142 | opt_found = 1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 143 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 144 | } else if (opt_found) { |
| 145 | return buf; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 146 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 147 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 148 | k++; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 149 | buf += k; |
| 150 | len -= k; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 151 | opt_val ^= 1; |
| 152 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 153 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | #endif |
| 158 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 159 | static int tftp_protocol( |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 160 | len_and_sockaddr *our_lsa, |
Denis Vlasenko | 0850cda | 2007-02-07 23:20:32 +0000 | [diff] [blame] | 161 | len_and_sockaddr *peer_lsa, |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 162 | const char *local_file |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 163 | IF_TFTP(, const char *remote_file) |
| 164 | IF_FEATURE_TFTP_BLOCKSIZE(IF_TFTPD(, void *tsize)) |
| 165 | IF_FEATURE_TFTP_BLOCKSIZE(, int blksize)) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 166 | { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 167 | #if !ENABLE_TFTP |
| 168 | #define remote_file NULL |
| 169 | #endif |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 170 | #if !(ENABLE_FEATURE_TFTP_BLOCKSIZE && ENABLE_TFTPD) |
| 171 | #define tsize NULL |
| 172 | #endif |
| 173 | #if !ENABLE_FEATURE_TFTP_BLOCKSIZE |
| 174 | enum { blksize = TFTP_BLKSIZE_DEFAULT }; |
| 175 | #endif |
| 176 | |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 177 | struct pollfd pfd[1]; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 178 | #define socket_fd (pfd[0].fd) |
Bernhard Reutner-Fischer | 62f9856 | 2006-06-10 14:32:56 +0000 | [diff] [blame] | 179 | int len; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 180 | int send_len; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 181 | IF_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;) |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 182 | smallint finished = 0; |
| 183 | uint16_t opcode; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 184 | uint16_t block_nr; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 185 | uint16_t recv_blk; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 186 | int open_mode, local_fd; |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 187 | int retries, waittime_ms; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 188 | int io_bufsize = blksize + 4; |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 189 | char *cp; |
Eric Andersen | 744a194 | 2001-11-10 11:16:39 +0000 | [diff] [blame] | 190 | /* Can't use RESERVE_CONFIG_BUFFER here since the allocation |
| 191 | * size varies meaning BUFFERS_GO_ON_STACK would fail */ |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 192 | /* We must keep the transmit and receive buffers seperate */ |
| 193 | /* In case we rcv a garbage pkt and we need to rexmit the last pkt */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 194 | char *xbuf = xmalloc(io_bufsize); |
| 195 | char *rbuf = xmalloc(io_bufsize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 197 | socket_fd = xsocket(peer_lsa->u.sa.sa_family, SOCK_DGRAM, 0); |
| 198 | setsockopt_reuseaddr(socket_fd); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 199 | |
Denis Vlasenko | 78c5656 | 2008-03-18 00:11:46 +0000 | [diff] [blame] | 200 | block_nr = 1; |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 201 | cp = xbuf + 2; |
Denis Vlasenko | 7a60133 | 2008-03-19 13:24:13 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 203 | if (!ENABLE_TFTP || our_lsa) { |
| 204 | /* tftpd */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 205 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 206 | /* Create a socket which is: |
| 207 | * 1. bound to IP:port peer sent 1st datagram to, |
| 208 | * 2. connected to peer's IP:port |
| 209 | * This way we will answer from the IP:port peer |
| 210 | * expects, will not get any other packets on |
| 211 | * the socket, and also plain read/write will work. */ |
| 212 | xbind(socket_fd, &our_lsa->u.sa, our_lsa->len); |
| 213 | xconnect(socket_fd, &peer_lsa->u.sa, peer_lsa->len); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 214 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 215 | /* Is there an error already? Send pkt and bail out */ |
| 216 | if (error_pkt_reason || error_pkt_str[0]) |
| 217 | goto send_err_pkt; |
| 218 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 219 | if (CMD_GET(option_mask32)) { |
Denis Vlasenko | 78c5656 | 2008-03-18 00:11:46 +0000 | [diff] [blame] | 220 | /* it's upload - we must ACK 1st packet (with filename) |
| 221 | * as if it's "block 0" */ |
| 222 | block_nr = 0; |
| 223 | } |
| 224 | |
Denis Vlasenko | 7a60133 | 2008-03-19 13:24:13 +0000 | [diff] [blame] | 225 | if (user_opt) { |
Denis Vlasenko | d7a805e | 2008-12-03 19:05:55 +0000 | [diff] [blame] | 226 | struct passwd *pw = xgetpwnam(user_opt); |
Denis Vlasenko | 7a60133 | 2008-03-19 13:24:13 +0000 | [diff] [blame] | 227 | change_identity(pw); /* initgroups, setgid, setuid */ |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* Open local file (must be after changing user) */ |
| 232 | if (CMD_PUT(option_mask32)) { |
| 233 | open_mode = O_RDONLY; |
| 234 | } else { |
| 235 | open_mode = O_WRONLY | O_TRUNC | O_CREAT; |
| 236 | #if ENABLE_TFTPD |
| 237 | if ((option_mask32 & (TFTPD_OPT+TFTPD_OPT_c)) == TFTPD_OPT) { |
| 238 | /* tftpd without -c */ |
| 239 | open_mode = O_WRONLY | O_TRUNC; |
| 240 | } |
| 241 | #endif |
| 242 | } |
| 243 | if (!(option_mask32 & TFTPD_OPT)) { |
| 244 | local_fd = CMD_GET(option_mask32) ? STDOUT_FILENO : STDIN_FILENO; |
| 245 | if (NOT_LONE_DASH(local_file)) |
| 246 | local_fd = xopen(local_file, open_mode); |
| 247 | } else { |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 248 | local_fd = open(local_file, open_mode); |
Denis Vlasenko | 7a60133 | 2008-03-19 13:24:13 +0000 | [diff] [blame] | 249 | if (local_fd < 0) { |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 250 | error_pkt_reason = ERR_NOFILE; |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 251 | strcpy((char*)error_pkt_str, "can't open file"); |
Denis Vlasenko | 7a60133 | 2008-03-19 13:24:13 +0000 | [diff] [blame] | 252 | goto send_err_pkt; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (!ENABLE_TFTP || our_lsa) { |
Denis Vlasenko | 31e1286 | 2008-06-16 07:32:40 +0000 | [diff] [blame] | 257 | /* gcc 4.3.1 would NOT optimize it out as it should! */ |
| 258 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 259 | if (blksize != TFTP_BLKSIZE_DEFAULT || tsize) { |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 260 | /* Create and send OACK packet. */ |
| 261 | /* For the download case, block_nr is still 1 - |
| 262 | * we expect 1st ACK from peer to be for (block_nr-1), |
| 263 | * that is, for "block 0" which is our OACK pkt */ |
| 264 | opcode = TFTP_OACK; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 265 | goto add_blksize_opt; |
| 266 | } |
Denis Vlasenko | 31e1286 | 2008-06-16 07:32:40 +0000 | [diff] [blame] | 267 | #endif |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 268 | } else { |
| 269 | /* Removing it, or using if() statement instead of #if may lead to |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 270 | * "warning: null argument where non-null required": */ |
| 271 | #if ENABLE_TFTP |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 272 | /* tftp */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 273 | |
| 274 | /* We can't (and don't really need to) bind the socket: |
| 275 | * we don't know from which local IP datagrams will be sent, |
| 276 | * but kernel will pick the same IP every time (unless routing |
| 277 | * table is changed), thus peer will see dgrams consistently |
| 278 | * coming from the same IP. |
| 279 | * We would like to connect the socket, but since peer's |
| 280 | * UDP code can be less perfect than ours, _peer's_ IP:port |
| 281 | * in replies may differ from IP:port we used to send |
| 282 | * our first packet. We can connect() only when we get |
| 283 | * first reply. */ |
| 284 | |
| 285 | /* build opcode */ |
| 286 | opcode = TFTP_WRQ; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 287 | if (CMD_GET(option_mask32)) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 288 | opcode = TFTP_RRQ; |
| 289 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 290 | /* add filename and mode */ |
| 291 | /* fill in packet if the filename fits into xbuf */ |
| 292 | len = strlen(remote_file) + 1; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 293 | if (2 + len + sizeof("octet") >= io_bufsize) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 294 | bb_error_msg("remote filename is too long"); |
| 295 | goto ret; |
| 296 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 297 | strcpy(cp, remote_file); |
| 298 | cp += len; |
| 299 | /* add "mode" part of the package */ |
| 300 | strcpy(cp, "octet"); |
| 301 | cp += sizeof("octet"); |
| 302 | |
Denis Vlasenko | 31e1286 | 2008-06-16 07:32:40 +0000 | [diff] [blame] | 303 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 304 | if (blksize == TFTP_BLKSIZE_DEFAULT) |
| 305 | goto send_pkt; |
| 306 | |
| 307 | /* Non-standard blocksize: add option to pkt */ |
| 308 | if ((&xbuf[io_bufsize - 1] - cp) < sizeof("blksize NNNNN")) { |
| 309 | bb_error_msg("remote filename is too long"); |
| 310 | goto ret; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 311 | } |
Denis Vlasenko | 31e1286 | 2008-06-16 07:32:40 +0000 | [diff] [blame] | 312 | want_option_ack = 1; |
| 313 | #endif |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 314 | #endif /* ENABLE_TFTP */ |
| 315 | |
Denis Vlasenko | 31e1286 | 2008-06-16 07:32:40 +0000 | [diff] [blame] | 316 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 317 | add_blksize_opt: |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 318 | #if ENABLE_TFTPD |
| 319 | if (tsize) { |
| 320 | struct stat st; |
| 321 | /* add "tsize", <nul>, size, <nul> */ |
| 322 | strcpy(cp, "tsize"); |
| 323 | cp += sizeof("tsize"); |
| 324 | fstat(local_fd, &st); |
| 325 | cp += snprintf(cp, 10, "%u", (int) st.st_size) + 1; |
| 326 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 327 | #endif |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 328 | if (blksize != TFTP_BLKSIZE_DEFAULT) { |
| 329 | /* add "blksize", <nul>, blksize, <nul> */ |
| 330 | strcpy(cp, "blksize"); |
| 331 | cp += sizeof("blksize"); |
| 332 | cp += snprintf(cp, 6, "%d", blksize) + 1; |
| 333 | } |
Denis Vlasenko | 31e1286 | 2008-06-16 07:32:40 +0000 | [diff] [blame] | 334 | #endif |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 335 | /* First packet is built, so skip packet generation */ |
| 336 | goto send_pkt; |
| 337 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 338 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 339 | /* Using mostly goto's - continue/break will be less clear |
| 340 | * in where we actually jump to */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 341 | while (1) { |
| 342 | /* Build ACK or DATA */ |
| 343 | cp = xbuf + 2; |
| 344 | *((uint16_t*)cp) = htons(block_nr); |
| 345 | cp += 2; |
| 346 | block_nr++; |
| 347 | opcode = TFTP_ACK; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 348 | if (CMD_PUT(option_mask32)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 349 | opcode = TFTP_DATA; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 350 | len = full_read(local_fd, cp, blksize); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 351 | if (len < 0) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 352 | goto send_read_err_pkt; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 353 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 354 | if (len != blksize) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 355 | finished = 1; |
| 356 | } |
| 357 | cp += len; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 358 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 359 | send_pkt: |
| 360 | /* Send packet */ |
| 361 | *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */ |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 362 | send_len = cp - xbuf; |
| 363 | /* NB: send_len value is preserved in code below |
| 364 | * for potential resend */ |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 365 | |
| 366 | retries = TFTP_NUM_RETRIES; /* re-initialize */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 367 | waittime_ms = TFTP_TIMEOUT_MS; |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 368 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 369 | send_again: |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 370 | #if ENABLE_TFTP_DEBUG |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 371 | fprintf(stderr, "sending %u bytes\n", send_len); |
| 372 | for (cp = xbuf; cp < &xbuf[send_len]; cp++) |
| 373 | fprintf(stderr, "%02x ", (unsigned char) *cp); |
| 374 | fprintf(stderr, "\n"); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 375 | #endif |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 376 | xsendto(socket_fd, xbuf, send_len, &peer_lsa->u.sa, peer_lsa->len); |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 377 | /* Was it final ACK? then exit */ |
| 378 | if (finished && (opcode == TFTP_ACK)) |
| 379 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 380 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 381 | recv_again: |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 382 | /* Receive packet */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 383 | /*pfd[0].fd = socket_fd;*/ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 384 | pfd[0].events = POLLIN; |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 385 | switch (safe_poll(pfd, 1, waittime_ms)) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 386 | default: |
| 387 | /*bb_perror_msg("poll"); - done in safe_poll */ |
| 388 | goto ret; |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 389 | case 0: |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 390 | retries--; |
| 391 | if (retries == 0) { |
| 392 | bb_error_msg("timeout"); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 393 | goto ret; /* no err packet sent */ |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 394 | } |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 395 | |
| 396 | /* exponential backoff with limit */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 397 | waittime_ms += waittime_ms/2; |
| 398 | if (waittime_ms > TFTP_MAXTIMEOUT_MS) { |
| 399 | waittime_ms = TFTP_MAXTIMEOUT_MS; |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 402 | goto send_again; /* resend last sent pkt */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 403 | case 1: |
| 404 | if (!our_lsa) { |
| 405 | /* tftp (not tftpd!) receiving 1st packet */ |
| 406 | our_lsa = ((void*)(ptrdiff_t)-1); /* not NULL */ |
| 407 | len = recvfrom(socket_fd, rbuf, io_bufsize, 0, |
| 408 | &peer_lsa->u.sa, &peer_lsa->len); |
| 409 | /* Our first dgram went to port 69 |
| 410 | * but reply may come from different one. |
| 411 | * Remember and use this new port (and IP) */ |
| 412 | if (len >= 0) |
| 413 | xconnect(socket_fd, &peer_lsa->u.sa, peer_lsa->len); |
| 414 | } else { |
| 415 | /* tftpd, or not the very first packet: |
| 416 | * socket is connect()ed, can just read from it. */ |
| 417 | /* Don't full_read()! |
| 418 | * This is not TCP, one read == one pkt! */ |
| 419 | len = safe_read(socket_fd, rbuf, io_bufsize); |
| 420 | } |
| 421 | if (len < 0) { |
| 422 | goto send_read_err_pkt; |
| 423 | } |
| 424 | if (len < 4) { /* too small? */ |
| 425 | goto recv_again; |
| 426 | } |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 427 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 428 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 429 | /* Process recv'ed packet */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 430 | opcode = ntohs( ((uint16_t*)rbuf)[0] ); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 431 | recv_blk = ntohs( ((uint16_t*)rbuf)[1] ); |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 432 | #if ENABLE_TFTP_DEBUG |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 433 | fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 434 | #endif |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 435 | if (opcode == TFTP_ERROR) { |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 436 | static const char errcode_str[] ALIGN1 = |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 437 | "\0" |
| 438 | "file not found\0" |
| 439 | "access violation\0" |
| 440 | "disk full\0" |
| 441 | "bad operation\0" |
| 442 | "unknown transfer id\0" |
| 443 | "file already exists\0" |
| 444 | "no such user\0" |
| 445 | "bad option"; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 446 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 447 | const char *msg = ""; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 448 | |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 449 | if (len > 4 && rbuf[4] != '\0') { |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 450 | msg = &rbuf[4]; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 451 | rbuf[io_bufsize - 1] = '\0'; /* paranoia */ |
| 452 | } else if (recv_blk <= 8) { |
| 453 | msg = nth_string(errcode_str, recv_blk); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 454 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 455 | bb_error_msg("server error: (%u) %s", recv_blk, msg); |
| 456 | goto ret; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 457 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 458 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 459 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 460 | if (want_option_ack) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 461 | want_option_ack = 0; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 462 | if (opcode == TFTP_OACK) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 463 | /* server seems to support options */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 464 | char *res; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 465 | |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 466 | res = tftp_get_option("blksize", &rbuf[2], len - 2); |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 467 | if (res) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 468 | blksize = tftp_blksize_check(res, blksize); |
| 469 | if (blksize < 0) { |
| 470 | error_pkt_reason = ERR_BAD_OPT; |
| 471 | goto send_err_pkt; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 472 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 473 | io_bufsize = blksize + 4; |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 474 | /* Send ACK for OACK ("block" no: 0) */ |
| 475 | block_nr = 0; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 476 | continue; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 477 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 478 | /* rfc2347: |
| 479 | * "An option not acknowledged by the server |
| 480 | * must be ignored by the client and server |
| 481 | * as if it were never requested." */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 482 | } |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 483 | bb_error_msg("server only supports blocksize of 512"); |
| 484 | blksize = TFTP_BLKSIZE_DEFAULT; |
| 485 | io_bufsize = TFTP_BLKSIZE_DEFAULT + 4; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 486 | } |
| 487 | #endif |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 488 | /* block_nr is already advanced to next block# we expect |
| 489 | * to get / block# we are about to send next time */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 490 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 491 | if (CMD_GET(option_mask32) && (opcode == TFTP_DATA)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 492 | if (recv_blk == block_nr) { |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 493 | int sz = full_write(local_fd, &rbuf[4], len - 4); |
| 494 | if (sz != len - 4) { |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 495 | strcpy((char*)error_pkt_str, bb_msg_write_error); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 496 | error_pkt_reason = ERR_WRITE; |
| 497 | goto send_err_pkt; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 498 | } |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 499 | if (sz != blksize) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 500 | finished = 1; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 501 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 502 | continue; /* send ACK */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 503 | } |
Denys Vlasenko | c8ab67c | 2009-05-10 23:27:43 +0200 | [diff] [blame] | 504 | /* Disabled to cope with servers with Sorcerer's Apprentice Syndrome */ |
| 505 | #if 0 |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 506 | if (recv_blk == (block_nr - 1)) { |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 507 | /* Server lost our TFTP_ACK. Resend it */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 508 | block_nr = recv_blk; |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 509 | continue; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 510 | } |
Denys Vlasenko | c8ab67c | 2009-05-10 23:27:43 +0200 | [diff] [blame] | 511 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 514 | if (CMD_PUT(option_mask32) && (opcode == TFTP_ACK)) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 515 | /* did peer ACK our last DATA pkt? */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 516 | if (recv_blk == (uint16_t) (block_nr - 1)) { |
| 517 | if (finished) |
| 518 | goto ret; |
| 519 | continue; /* send next block */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 522 | /* Awww... recv'd packet is not recognized! */ |
| 523 | goto recv_again; |
| 524 | /* why recv_again? - rfc1123 says: |
| 525 | * "The sender (i.e., the side originating the DATA packets) |
| 526 | * must never resend the current DATA packet on receipt |
| 527 | * of a duplicate ACK". |
| 528 | * DATA pkts are resent ONLY on timeout. |
| 529 | * Thus "goto send_again" will ba a bad mistake above. |
| 530 | * See: |
| 531 | * http://en.wikipedia.org/wiki/Sorcerer's_Apprentice_Syndrome |
| 532 | */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 533 | } /* end of "while (1)" */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 534 | ret: |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 535 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 536 | close(local_fd); |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 537 | close(socket_fd); |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 538 | free(xbuf); |
| 539 | free(rbuf); |
| 540 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 541 | return finished == 0; /* returns 1 on failure */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 542 | |
| 543 | send_read_err_pkt: |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 544 | strcpy((char*)error_pkt_str, bb_msg_read_error); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 545 | send_err_pkt: |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 546 | if (error_pkt_str[0]) |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 547 | bb_error_msg((char*)error_pkt_str); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 548 | error_pkt[1] = TFTP_ERROR; |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 549 | xsendto(socket_fd, error_pkt, 4 + 1 + strlen((char*)error_pkt_str), |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 550 | &peer_lsa->u.sa, peer_lsa->len); |
| 551 | return EXIT_FAILURE; |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 552 | #undef remote_file |
| 553 | #undef tsize |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 556 | #if ENABLE_TFTP |
| 557 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 558 | int tftp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 559 | int tftp_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 560 | { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 561 | len_and_sockaddr *peer_lsa; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 562 | const char *local_file = NULL; |
| 563 | const char *remote_file = NULL; |
Denis Vlasenko | 4824cca | 2008-03-21 18:29:01 +0000 | [diff] [blame] | 564 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
| 565 | const char *blksize_str = TFTP_BLKSIZE_DEFAULT_STR; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 566 | int blksize; |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 567 | #endif |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 568 | int result; |
| 569 | int port; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 570 | IF_GETPUT(int opt;) |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 571 | |
| 572 | INIT_G(); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 573 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 574 | /* -p or -g is mandatory, and they are mutually exclusive */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 575 | opt_complementary = "" IF_FEATURE_TFTP_GET("g:") IF_FEATURE_TFTP_PUT("p:") |
| 576 | IF_GETPUT("g--p:p--g:"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 577 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 578 | IF_GETPUT(opt =) getopt32(argv, |
| 579 | IF_FEATURE_TFTP_GET("g") IF_FEATURE_TFTP_PUT("p") |
| 580 | "l:r:" IF_FEATURE_TFTP_BLOCKSIZE("b:"), |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 581 | &local_file, &remote_file |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 582 | IF_FEATURE_TFTP_BLOCKSIZE(, &blksize_str)); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 583 | argv += optind; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 584 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 585 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 586 | /* Check if the blksize is valid: |
| 587 | * RFC2348 says between 8 and 65464 */ |
| 588 | blksize = tftp_blksize_check(blksize_str, 65564); |
| 589 | if (blksize < 0) { |
| 590 | //bb_error_msg("bad block size"); |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 591 | return EXIT_FAILURE; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 592 | } |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 593 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 594 | |
Denis Vlasenko | f9beb61 | 2009-03-25 03:55:53 +0000 | [diff] [blame] | 595 | if (remote_file) { |
| 596 | if (!local_file) { |
| 597 | const char *slash = strrchr(remote_file, '/'); |
| 598 | local_file = slash ? slash + 1 : remote_file; |
| 599 | } |
| 600 | } else { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 601 | remote_file = local_file; |
Denis Vlasenko | f9beb61 | 2009-03-25 03:55:53 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 604 | /* Error if filename or host is not known */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 605 | if (!remote_file || !argv[0]) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 606 | bb_show_usage(); |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 607 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 608 | port = bb_lookup_port(argv[1], "udp", 69); |
| 609 | peer_lsa = xhost2sockaddr(argv[0], port); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 611 | #if ENABLE_TFTP_DEBUG |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 612 | fprintf(stderr, "using server '%s', remote_file '%s', local_file '%s'\n", |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 613 | xmalloc_sockaddr2dotted(&peer_lsa->u.sa), |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 614 | remote_file, local_file); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 615 | #endif |
| 616 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 617 | result = tftp_protocol( |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 618 | NULL /*our_lsa*/, peer_lsa, |
| 619 | local_file, remote_file |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 620 | IF_FEATURE_TFTP_BLOCKSIZE(IF_TFTPD(, NULL /*tsize*/)) |
| 621 | IF_FEATURE_TFTP_BLOCKSIZE(, blksize) |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 622 | ); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 623 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 624 | if (result != EXIT_SUCCESS && NOT_LONE_DASH(local_file) && CMD_GET(opt)) { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 625 | unlink(local_file); |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 626 | } |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 627 | return result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 628 | } |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 629 | |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 630 | #endif /* ENABLE_TFTP */ |
| 631 | |
| 632 | #if ENABLE_TFTPD |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 633 | int tftpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 634 | int tftpd_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 635 | { |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 636 | len_and_sockaddr *our_lsa; |
| 637 | len_and_sockaddr *peer_lsa; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 638 | char *local_file, *mode; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 639 | const char *error_msg; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 640 | int opt, result, opcode; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 641 | IF_FEATURE_TFTP_BLOCKSIZE(int blksize = TFTP_BLKSIZE_DEFAULT;) |
| 642 | IF_FEATURE_TFTP_BLOCKSIZE(char *tsize = NULL;) |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 643 | |
| 644 | INIT_G(); |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 645 | |
| 646 | our_lsa = get_sock_lsa(STDIN_FILENO); |
Denis Vlasenko | 5a89763 | 2008-11-01 00:22:24 +0000 | [diff] [blame] | 647 | if (!our_lsa) { |
| 648 | /* This is confusing: |
| 649 | *bb_error_msg_and_die("stdin is not a socket"); |
| 650 | * Better: */ |
| 651 | bb_show_usage(); |
| 652 | /* Help text says that tftpd must be used as inetd service, |
| 653 | * which is by far the most usual cause of get_sock_lsa |
| 654 | * failure */ |
| 655 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 656 | peer_lsa = xzalloc(LSA_LEN_SIZE + our_lsa->len); |
| 657 | peer_lsa->len = our_lsa->len; |
| 658 | |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 659 | /* Shifting to not collide with TFTP_OPTs */ |
| 660 | opt = option_mask32 = TFTPD_OPT | (getopt32(argv, "rcu:", &user_opt) << 8); |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 661 | argv += optind; |
| 662 | if (argv[0]) |
| 663 | xchdir(argv[0]); |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 664 | |
| 665 | result = recv_from_to(STDIN_FILENO, block_buf, sizeof(block_buf), |
| 666 | 0 /* flags */, |
| 667 | &peer_lsa->u.sa, &our_lsa->u.sa, our_lsa->len); |
| 668 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 669 | error_msg = "malformed packet"; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 670 | opcode = ntohs(*(uint16_t*)block_buf); |
| 671 | if (result < 4 || result >= sizeof(block_buf) |
| 672 | || block_buf[result-1] != '\0' |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 673 | || (IF_FEATURE_TFTP_PUT(opcode != TFTP_RRQ) /* not download */ |
| 674 | IF_GETPUT(&&) |
| 675 | IF_FEATURE_TFTP_GET(opcode != TFTP_WRQ) /* not upload */ |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 676 | ) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 677 | ) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 678 | goto err; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 679 | } |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 680 | local_file = block_buf + 2; |
| 681 | if (local_file[0] == '.' || strstr(local_file, "/.")) { |
Denis Vlasenko | 0a0180c | 2008-03-19 23:37:32 +0000 | [diff] [blame] | 682 | error_msg = "dot in file name"; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 683 | goto err; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 684 | } |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 685 | mode = local_file + strlen(local_file) + 1; |
| 686 | if (mode >= block_buf + result || strcmp(mode, "octet") != 0) { |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 687 | goto err; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 688 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 689 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 690 | { |
| 691 | char *res; |
| 692 | char *opt_str = mode + sizeof("octet"); |
| 693 | int opt_len = block_buf + result - opt_str; |
| 694 | if (opt_len > 0) { |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 695 | res = tftp_get_option("blksize", opt_str, opt_len); |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 696 | if (res) { |
| 697 | blksize = tftp_blksize_check(res, 65564); |
| 698 | if (blksize < 0) { |
| 699 | error_pkt_reason = ERR_BAD_OPT; |
| 700 | /* will just send error pkt */ |
| 701 | goto do_proto; |
| 702 | } |
| 703 | } |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 704 | /* did client ask us about file size? */ |
| 705 | tsize = tftp_get_option("tsize", opt_str, opt_len); |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | #endif |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 709 | |
Denis Vlasenko | d7e6af2 | 2008-03-18 01:13:11 +0000 | [diff] [blame] | 710 | if (!ENABLE_FEATURE_TFTP_PUT || opcode == TFTP_WRQ) { |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 711 | if (opt & TFTPD_OPT_r) { |
Denis Vlasenko | 71c9f01 | 2008-03-19 09:43:50 +0000 | [diff] [blame] | 712 | /* This would mean "disk full" - not true */ |
| 713 | /*error_pkt_reason = ERR_WRITE;*/ |
| 714 | error_msg = bb_msg_write_error; |
| 715 | goto err; |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 716 | } |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 717 | IF_GETPUT(option_mask32 |= TFTP_OPT_GET;) /* will receive file's data */ |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 718 | } else { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 719 | IF_GETPUT(option_mask32 |= TFTP_OPT_PUT;) /* will send file's data */ |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 720 | } |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 721 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 722 | /* NB: if error_pkt_str or error_pkt_reason is set up, |
| 723 | * tftp_protocol() just sends one error pkt and returns */ |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 724 | |
Denis Vlasenko | dd9228b | 2008-03-19 05:00:05 +0000 | [diff] [blame] | 725 | do_proto: |
Denis Vlasenko | 8474cd3 | 2008-06-16 07:12:19 +0000 | [diff] [blame] | 726 | close(STDIN_FILENO); /* close old, possibly wildcard socket */ |
| 727 | /* tftp_protocol() will create new one, bound to particular local IP */ |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 728 | result = tftp_protocol( |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 729 | our_lsa, peer_lsa, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 730 | local_file IF_TFTP(, NULL /*remote_file*/) |
| 731 | IF_FEATURE_TFTP_BLOCKSIZE(, tsize) |
| 732 | IF_FEATURE_TFTP_BLOCKSIZE(, blksize) |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 733 | ); |
| 734 | |
| 735 | return result; |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 736 | err: |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 737 | strcpy((char*)error_pkt_str, error_msg); |
Denis Vlasenko | 403a5a2 | 2008-03-19 13:07:00 +0000 | [diff] [blame] | 738 | goto do_proto; |
Denis Vlasenko | aa9b182 | 2008-03-17 09:10:39 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | #endif /* ENABLE_TFTPD */ |
| 742 | |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 743 | #endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */ |