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 | * |
| 5 | * A simple tftp client for busybox. |
| 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 | * |
| 19 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 20 | * ------------------------------------------------------------------------- */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 22 | #include "libbb.h" |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 24 | #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT |
| 25 | |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 26 | #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ |
| 27 | #define TFTP_TIMEOUT_MS 50 |
| 28 | #define TFTP_MAXTIMEOUT_MS 2000 |
| 29 | #define TFTP_NUM_RETRIES 12 /* number of backed-off retries */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 30 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 31 | /* opcodes we support */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 32 | #define TFTP_RRQ 1 |
| 33 | #define TFTP_WRQ 2 |
| 34 | #define TFTP_DATA 3 |
| 35 | #define TFTP_ACK 4 |
| 36 | #define TFTP_ERROR 5 |
| 37 | #define TFTP_OACK 6 |
| 38 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 39 | #if ENABLE_FEATURE_TFTP_GET && !ENABLE_FEATURE_TFTP_PUT |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 40 | #define USE_GETPUT(...) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 41 | #define CMD_GET(cmd) 1 |
| 42 | #define CMD_PUT(cmd) 0 |
| 43 | #elif !ENABLE_FEATURE_TFTP_GET && ENABLE_FEATURE_TFTP_PUT |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 44 | #define USE_GETPUT(...) |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 45 | #define CMD_GET(cmd) 0 |
| 46 | #define CMD_PUT(cmd) 1 |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 47 | #else |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 48 | #define USE_GETPUT(...) __VA_ARGS__ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 49 | /* masks coming from getpot32 */ |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 50 | #define CMD_GET(cmd) ((cmd) & 1) |
| 51 | #define CMD_PUT(cmd) ((cmd) & 2) |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 52 | #endif |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 53 | /* NB: in the code below |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 54 | * CMD_GET(cmd) and CMD_PUT(cmd) are mutually exclusive |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 55 | */ |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 56 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 58 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 59 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 60 | static int tftp_blocksize_check(int blocksize, int bufsize) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 61 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 62 | /* Check if the blocksize is valid: |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 63 | * RFC2348 says between 8 and 65464, |
| 64 | * but our implementation makes it impossible |
| 65 | * to use blocksizes smaller than 22 octets. |
| 66 | */ |
| 67 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 68 | if ((bufsize && (blocksize > bufsize)) |
| 69 | || (blocksize < 8) || (blocksize > 65564) |
| 70 | ) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 71 | bb_error_msg("bad blocksize"); |
| 72 | return 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return blocksize; |
| 76 | } |
| 77 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 78 | static char *tftp_option_get(char *buf, int len, const char *option) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 79 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 80 | int opt_val = 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 81 | int opt_found = 0; |
| 82 | int k; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 83 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 84 | while (len > 0) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 85 | /* Make sure the options are terminated correctly */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 86 | for (k = 0; k < len; k++) { |
| 87 | if (buf[k] == '\0') { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 88 | goto nul_found; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 91 | return NULL; |
| 92 | nul_found: |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 93 | if (opt_val == 0) { |
| 94 | if (strcasecmp(buf, option) == 0) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 95 | opt_found = 1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 96 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 97 | } else if (opt_found) { |
| 98 | return buf; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 99 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 100 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 101 | k++; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 102 | buf += k; |
| 103 | len -= k; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 104 | opt_val ^= 1; |
| 105 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 106 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 112 | static int tftp( USE_GETPUT(const int cmd,) |
Denis Vlasenko | 0850cda | 2007-02-07 23:20:32 +0000 | [diff] [blame] | 113 | len_and_sockaddr *peer_lsa, |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 114 | const char *remotefile, const int localfd, |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 115 | unsigned port, int tftp_bufsize) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 116 | { |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 117 | struct pollfd pfd[1]; |
| 118 | #define socketfd (pfd[0].fd) |
Bernhard Reutner-Fischer | 62f9856 | 2006-06-10 14:32:56 +0000 | [diff] [blame] | 119 | int len; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 120 | int send_len; |
| 121 | USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;) |
| 122 | smallint finished = 0; |
| 123 | uint16_t opcode; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 124 | uint16_t block_nr = 1; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 125 | uint16_t recv_blk; |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 126 | int retries, waittime_ms; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 127 | char *cp; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | 0850cda | 2007-02-07 23:20:32 +0000 | [diff] [blame] | 129 | unsigned org_port; |
| 130 | len_and_sockaddr *const from = alloca(offsetof(len_and_sockaddr, sa) + peer_lsa->len); |
| 131 | |
Eric Andersen | 744a194 | 2001-11-10 11:16:39 +0000 | [diff] [blame] | 132 | /* Can't use RESERVE_CONFIG_BUFFER here since the allocation |
| 133 | * size varies meaning BUFFERS_GO_ON_STACK would fail */ |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 134 | /* We must keep the transmit and receive buffers seperate */ |
| 135 | /* In case we rcv a garbage pkt and we need to rexmit the last pkt */ |
| 136 | char *xbuf = xmalloc(tftp_bufsize += 4); |
| 137 | char *rbuf = xmalloc(tftp_bufsize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | 0850cda | 2007-02-07 23:20:32 +0000 | [diff] [blame] | 139 | port = org_port = htons(port); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 141 | socketfd = xsocket(peer_lsa->sa.sa_family, SOCK_DGRAM, 0); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 142 | |
| 143 | /* build opcode */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 144 | opcode = TFTP_WRQ; |
| 145 | if (CMD_GET(cmd)) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 146 | opcode = TFTP_RRQ; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 147 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 148 | cp = xbuf + 2; |
| 149 | /* add filename and mode */ |
| 150 | /* fill in packet if the filename fits into xbuf */ |
| 151 | len = strlen(remotefile) + 1; |
| 152 | if (2 + len + sizeof("octet") >= tftp_bufsize) { |
| 153 | bb_error_msg("remote filename is too long"); |
| 154 | goto ret; |
| 155 | } |
| 156 | strcpy(cp, remotefile); |
| 157 | cp += len; |
| 158 | /* add "mode" part of the package */ |
| 159 | strcpy(cp, "octet"); |
| 160 | cp += sizeof("octet"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 161 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 162 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 163 | len = tftp_bufsize - 4; /* data block size */ |
| 164 | if (len != TFTP_BLOCKSIZE_DEFAULT) { |
| 165 | /* rfc2348 says that 65464 is a max allowed value */ |
| 166 | if ((&xbuf[tftp_bufsize - 1] - cp) < sizeof("blksize NNNNN")) { |
| 167 | bb_error_msg("remote filename is too long"); |
| 168 | goto ret; |
| 169 | } |
| 170 | /* add "blksize", <nul>, blocksize */ |
| 171 | strcpy(cp, "blksize"); |
| 172 | cp += sizeof("blksize"); |
| 173 | cp += snprintf(cp, 6, "%d", len) + 1; |
| 174 | want_option_ack = 1; |
| 175 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 176 | #endif |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 177 | /* First packet is built, so skip packet generation */ |
| 178 | goto send_pkt; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 179 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 180 | /* Using mostly goto's - continue/break will be less clear |
| 181 | * in where we actually jump to */ |
| 182 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 183 | while (1) { |
| 184 | /* Build ACK or DATA */ |
| 185 | cp = xbuf + 2; |
| 186 | *((uint16_t*)cp) = htons(block_nr); |
| 187 | cp += 2; |
| 188 | block_nr++; |
| 189 | opcode = TFTP_ACK; |
| 190 | if (CMD_PUT(cmd)) { |
| 191 | opcode = TFTP_DATA; |
| 192 | len = full_read(localfd, cp, tftp_bufsize - 4); |
| 193 | if (len < 0) { |
| 194 | bb_perror_msg(bb_msg_read_error); |
| 195 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 196 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 197 | if (len != (tftp_bufsize - 4)) { |
| 198 | finished = 1; |
| 199 | } |
| 200 | cp += len; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 201 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 202 | send_pkt: |
| 203 | /* Send packet */ |
| 204 | *((uint16_t*)xbuf) = htons(opcode); /* fill in opcode part */ |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 205 | send_len = cp - xbuf; |
| 206 | /* NB: send_len value is preserved in code below |
| 207 | * for potential resend */ |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 208 | |
| 209 | retries = TFTP_NUM_RETRIES; /* re-initialize */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 210 | waittime_ms = TFTP_TIMEOUT_MS; |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 211 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 212 | send_again: |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 213 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 214 | fprintf(stderr, "sending %u bytes\n", send_len); |
| 215 | for (cp = xbuf; cp < &xbuf[send_len]; cp++) |
| 216 | fprintf(stderr, "%02x ", (unsigned char) *cp); |
| 217 | fprintf(stderr, "\n"); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 218 | #endif |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 219 | xsendto(socketfd, xbuf, send_len, &peer_lsa->sa, peer_lsa->len); |
| 220 | /* Was it final ACK? then exit */ |
| 221 | if (finished && (opcode == TFTP_ACK)) |
| 222 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 223 | |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 224 | recv_again: |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 225 | /* Receive packet */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 226 | /*pfd[0].fd = socketfd;*/ |
| 227 | pfd[0].events = POLLIN; |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 228 | switch (safe_poll(pfd, 1, waittime_ms)) { |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 229 | unsigned from_port; |
| 230 | case 1: |
| 231 | from->len = peer_lsa->len; |
| 232 | memset(&from->sa, 0, peer_lsa->len); |
| 233 | len = recvfrom(socketfd, rbuf, tftp_bufsize, 0, |
| 234 | &from->sa, &from->len); |
| 235 | if (len < 0) { |
| 236 | bb_perror_msg("recvfrom"); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 237 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 238 | } |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 239 | from_port = get_nport(&from->sa); |
| 240 | if (port == org_port) { |
| 241 | /* Our first query went to port 69 |
| 242 | * but reply will come from different one. |
| 243 | * Remember and use this new port */ |
| 244 | port = from_port; |
| 245 | set_nport(peer_lsa, from_port); |
| 246 | } |
| 247 | if (port != from_port) |
| 248 | goto recv_again; |
| 249 | goto process_pkt; |
| 250 | case 0: |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 251 | retries--; |
| 252 | if (retries == 0) { |
| 253 | bb_error_msg("timeout"); |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 254 | goto ret; |
| 255 | } |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 256 | |
| 257 | /* exponential backoff with limit */ |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 258 | waittime_ms += waittime_ms/2; |
| 259 | if (waittime_ms > TFTP_MAXTIMEOUT_MS) { |
| 260 | waittime_ms = TFTP_MAXTIMEOUT_MS; |
Paul Fox | 40f0bcf | 2007-09-06 17:52:22 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 263 | goto send_again; /* resend last sent pkt */ |
| 264 | default: |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 265 | /*bb_perror_msg("poll"); - done in safe_poll */ |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 266 | goto ret; |
| 267 | } |
| 268 | process_pkt: |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 269 | /* Process recv'ed packet */ |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 270 | opcode = ntohs( ((uint16_t*)rbuf)[0] ); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 271 | recv_blk = ntohs( ((uint16_t*)rbuf)[1] ); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 272 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 273 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 274 | fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, recv_blk); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 275 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 276 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 277 | if (opcode == TFTP_ERROR) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 278 | static const char *const errcode_str[] = { |
| 279 | "", |
| 280 | "file not found", |
| 281 | "access violation", |
| 282 | "disk full", |
| 283 | "illegal TFTP operation", |
| 284 | "unknown transfer id", |
| 285 | "file already exists", |
| 286 | "no such user", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 287 | "bad option" |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 288 | }; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 289 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 290 | const char *msg = ""; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 291 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 292 | if (rbuf[4] != '\0') { |
| 293 | msg = &rbuf[4]; |
| 294 | rbuf[tftp_bufsize - 1] = '\0'; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 295 | } else if (recv_blk < ARRAY_SIZE(errcode_str)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 296 | msg = errcode_str[recv_blk]; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 297 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 298 | bb_error_msg("server error: (%u) %s", recv_blk, msg); |
| 299 | goto ret; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 300 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 301 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 302 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 303 | if (want_option_ack) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 304 | want_option_ack = 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 305 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 306 | if (opcode == TFTP_OACK) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 307 | /* server seems to support options */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 308 | char *res; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 309 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 310 | res = tftp_option_get(&rbuf[2], len - 2, "blksize"); |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 311 | if (res) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 312 | int blksize = xatoi_u(res); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 313 | if (!tftp_blocksize_check(blksize, tftp_bufsize - 4)) { |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 314 | /* send ERROR 8 to server... */ |
| 315 | /* htons can be impossible to use in const initializer: */ |
| 316 | /*static const uint16_t error_8[2] = { htons(TFTP_ERROR), htons(8) };*/ |
| 317 | /* thus we open-code big-endian layout */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 318 | static const uint8_t error_8[4] = { 0,TFTP_ERROR, 0,8 }; |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 319 | xsendto(socketfd, error_8, 4, &peer_lsa->sa, peer_lsa->len); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 320 | bb_error_msg("server proposes bad blksize %d, exiting", blksize); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 321 | goto ret; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 322 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 323 | #if ENABLE_DEBUG_TFTP |
| 324 | fprintf(stderr, "using blksize %u\n", |
| 325 | blksize); |
| 326 | #endif |
| 327 | tftp_bufsize = blksize + 4; |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 328 | /* Send ACK for OACK ("block" no: 0) */ |
| 329 | block_nr = 0; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 330 | continue; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 331 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 332 | /* rfc2347: |
| 333 | * "An option not acknowledged by the server |
| 334 | * must be ignored by the client and server |
| 335 | * as if it were never requested." */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 336 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 338 | bb_error_msg("blksize is not supported by server" |
| 339 | " - reverting to 512"); |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 340 | tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 341 | } |
| 342 | #endif |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 343 | /* block_nr is already advanced to next block# we expect |
| 344 | * to get / block# we are about to send next time */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 345 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 346 | if (CMD_GET(cmd) && (opcode == TFTP_DATA)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 347 | if (recv_blk == block_nr) { |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 348 | len = full_write(localfd, &rbuf[4], len - 4); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 349 | if (len < 0) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 350 | bb_perror_msg(bb_msg_write_error); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 351 | goto ret; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 352 | } |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 353 | if (len != (tftp_bufsize - 4)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 354 | finished = 1; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 355 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 356 | continue; /* send ACK */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 357 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 358 | if (recv_blk == (block_nr - 1)) { |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 359 | /* Server lost our TFTP_ACK. Resend it */ |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 360 | block_nr = recv_blk; |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 361 | continue; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 362 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 365 | if (CMD_PUT(cmd) && (opcode == TFTP_ACK)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 366 | /* did server ACK our last DATA pkt? */ |
| 367 | if (recv_blk == (uint16_t) (block_nr - 1)) { |
| 368 | if (finished) |
| 369 | goto ret; |
| 370 | continue; /* send next block */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 373 | /* Awww... recv'd packet is not recognized! */ |
| 374 | goto recv_again; |
| 375 | /* why recv_again? - rfc1123 says: |
| 376 | * "The sender (i.e., the side originating the DATA packets) |
| 377 | * must never resend the current DATA packet on receipt |
| 378 | * of a duplicate ACK". |
| 379 | * DATA pkts are resent ONLY on timeout. |
| 380 | * Thus "goto send_again" will ba a bad mistake above. |
| 381 | * See: |
| 382 | * http://en.wikipedia.org/wiki/Sorcerer's_Apprentice_Syndrome |
| 383 | */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 384 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 385 | ret: |
Denis Vlasenko | 2c91652 | 2007-01-12 14:57:37 +0000 | [diff] [blame] | 386 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 387 | close(socketfd); |
| 388 | free(xbuf); |
| 389 | free(rbuf); |
| 390 | } |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 391 | return finished == 0; /* returns 1 on failure */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 394 | int tftp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 395 | int tftp_main(int argc, char **argv) |
| 396 | { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 397 | len_and_sockaddr *peer_lsa; |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 398 | const char *localfile = NULL; |
| 399 | const char *remotefile = NULL; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 400 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
| 401 | const char *sblocksize = NULL; |
| 402 | #endif |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 403 | int port; |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 404 | USE_GETPUT(int cmd;) |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 405 | int fd = -1; |
| 406 | int flags = 0; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 407 | int result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 408 | int blocksize = TFTP_BLOCKSIZE_DEFAULT; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 409 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 410 | /* -p or -g is mandatory, and they are mutually exclusive */ |
| 411 | opt_complementary = "" USE_FEATURE_TFTP_GET("g:") USE_FEATURE_TFTP_PUT("p:") |
| 412 | USE_GETPUT("?g--p:p--g"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 413 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 414 | USE_GETPUT(cmd =) getopt32(argv, |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 415 | USE_FEATURE_TFTP_GET("g") USE_FEATURE_TFTP_PUT("p") |
| 416 | "l:r:" USE_FEATURE_TFTP_BLOCKSIZE("b:"), |
| 417 | &localfile, &remotefile |
| 418 | USE_FEATURE_TFTP_BLOCKSIZE(, &sblocksize)); |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 419 | argv += optind; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 420 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 421 | flags = O_RDONLY; |
| 422 | if (CMD_GET(cmd)) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 423 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 424 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 425 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 426 | if (sblocksize) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 427 | blocksize = xatoi_u(sblocksize); |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 428 | if (!tftp_blocksize_check(blocksize, 0)) { |
| 429 | return EXIT_FAILURE; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 430 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 431 | } |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 432 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 433 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 434 | if (!localfile) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 435 | localfile = remotefile; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 436 | if (!remotefile) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 437 | remotefile = localfile; |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 438 | /* Error if filename or host is not known */ |
| 439 | if (!remotefile || !argv[0]) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 440 | bb_show_usage(); |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 441 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 442 | fd = CMD_GET(cmd) ? STDOUT_FILENO : STDIN_FILENO; |
| 443 | if (!LONE_DASH(localfile)) { |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 444 | fd = xopen(localfile, flags); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 447 | port = bb_lookup_port(argv[1], "udp", 69); |
| 448 | peer_lsa = xhost2sockaddr(argv[0], port); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 449 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 450 | #if ENABLE_DEBUG_TFTP |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 451 | fprintf(stderr, "using server '%s', remotefile '%s', localfile '%s'\n", |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 452 | xmalloc_sockaddr2dotted(&peer_lsa->sa), |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 453 | remotefile, localfile); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 454 | #endif |
| 455 | |
Denis Vlasenko | bf678d5 | 2007-05-09 12:50:08 +0000 | [diff] [blame] | 456 | result = tftp( USE_GETPUT(cmd,) peer_lsa, remotefile, fd, port, blocksize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | a04561f | 2007-05-08 23:12:21 +0000 | [diff] [blame] | 458 | if (ENABLE_FEATURE_CLEAN_UP) |
| 459 | close(fd); |
| 460 | if (result != EXIT_SUCCESS && !LONE_DASH(localfile) && CMD_GET(cmd)) { |
| 461 | unlink(localfile); |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 462 | } |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 463 | return result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 464 | } |
Denis Vlasenko | 3163555 | 2007-01-20 16:54:19 +0000 | [diff] [blame] | 465 | |
| 466 | #endif /* ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT */ |