Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------- */ |
| 2 | /* tftp.c */ |
| 3 | /* */ |
| 4 | /* A simple tftp client for busybox. */ |
| 5 | /* Tries to follow RFC1350. */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 6 | /* Only "octet" mode supported. */ |
| 7 | /* Optional blocksize negotiation (RFC2347 + RFC2348) */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 8 | /* */ |
| 9 | /* Copyright (C) 2001 Magnus Damm <damm@opensource.se> */ |
| 10 | /* */ |
| 11 | /* Parts of the code based on: */ |
| 12 | /* */ |
| 13 | /* atftp: Copyright (C) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca> */ |
| 14 | /* and Remi Lefebvre <remi@debian.org> */ |
| 15 | /* */ |
| 16 | /* utftp: Copyright (C) 1999 Uwe Ohse <uwe@ohse.de> */ |
| 17 | /* */ |
| 18 | /* This program is free software; you can redistribute it and/or modify */ |
| 19 | /* it under the terms of the GNU General Public License as published by */ |
| 20 | /* the Free Software Foundation; either version 2 of the License, or */ |
| 21 | /* (at your option) any later version. */ |
| 22 | /* */ |
| 23 | /* This program is distributed in the hope that it will be useful, */ |
| 24 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 25 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ |
| 26 | /* General Public License for more details. */ |
| 27 | /* */ |
| 28 | /* You should have received a copy of the GNU General Public License */ |
| 29 | /* along with this program; if not, write to the Free Software */ |
| 30 | /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
| 31 | /* */ |
| 32 | /* ------------------------------------------------------------------------- */ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/time.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <netdb.h> |
| 42 | #include <netinet/in.h> |
| 43 | #include <arpa/inet.h> |
| 44 | #include <unistd.h> |
| 45 | #include <fcntl.h> |
| 46 | |
| 47 | #include "busybox.h" |
| 48 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 49 | //#define CONFIG_FEATURE_TFTP_DEBUG |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 51 | #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ |
| 52 | #define TFTP_TIMEOUT 5 /* seconds */ |
| 53 | |
| 54 | /* opcodes we support */ |
| 55 | |
| 56 | #define TFTP_RRQ 1 |
| 57 | #define TFTP_WRQ 2 |
| 58 | #define TFTP_DATA 3 |
| 59 | #define TFTP_ACK 4 |
| 60 | #define TFTP_ERROR 5 |
| 61 | #define TFTP_OACK 6 |
| 62 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | static const char *tftp_bb_error_msg[] = { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 64 | "Undefined error", |
| 65 | "File not found", |
| 66 | "Access violation", |
| 67 | "Disk full or allocation error", |
| 68 | "Illegal TFTP operation", |
| 69 | "Unknown transfer ID", |
| 70 | "File already exists", |
| 71 | "No such user" |
| 72 | }; |
| 73 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 74 | const int tftp_cmd_get = 1; |
| 75 | const int tftp_cmd_put = 2; |
| 76 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 77 | #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 78 | |
| 79 | static int tftp_blocksize_check(int blocksize, int bufsize) |
| 80 | { |
| 81 | /* Check if the blocksize is valid: |
| 82 | * RFC2348 says between 8 and 65464, |
| 83 | * but our implementation makes it impossible |
| 84 | * to use blocksizes smaller than 22 octets. |
| 85 | */ |
| 86 | |
| 87 | if ((bufsize && (blocksize > bufsize)) || |
| 88 | (blocksize < 8) || (blocksize > 65464)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 89 | bb_error_msg("bad blocksize"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | return blocksize; |
| 94 | } |
| 95 | |
| 96 | static char *tftp_option_get(char *buf, int len, char *option) |
| 97 | { |
| 98 | int opt_val = 0; |
| 99 | int opt_found = 0; |
| 100 | int k; |
| 101 | |
| 102 | while (len > 0) { |
| 103 | |
| 104 | /* Make sure the options are terminated correctly */ |
| 105 | |
| 106 | for (k = 0; k < len; k++) { |
| 107 | if (buf[k] == '\0') { |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (k >= len) { |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | if (opt_val == 0) { |
| 117 | if (strcasecmp(buf, option) == 0) { |
| 118 | opt_found = 1; |
| 119 | } |
| 120 | } |
| 121 | else { |
| 122 | if (opt_found) { |
| 123 | return buf; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | k++; |
| 128 | |
| 129 | buf += k; |
| 130 | len -= k; |
| 131 | |
| 132 | opt_val ^= 1; |
| 133 | } |
| 134 | |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | #endif |
| 139 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 140 | static inline int tftp(const int cmd, const struct hostent *host, |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 141 | const char *remotefile, int localfd, const int port, int tftp_bufsize) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 142 | { |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 143 | const int cmd_get = cmd & tftp_cmd_get; |
| 144 | const int cmd_put = cmd & tftp_cmd_put; |
| 145 | const int bb_tftp_num_retries = 5; |
| 146 | |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 147 | struct sockaddr_in sa; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 148 | struct sockaddr_in from; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 149 | struct timeval tv; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 150 | socklen_t fromlen; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 151 | fd_set rfds; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 152 | char *cp; |
| 153 | unsigned short tmp; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 154 | int socketfd; |
| 155 | int len; |
| 156 | int opcode = 0; |
| 157 | int finished = 0; |
| 158 | int timeout = bb_tftp_num_retries; |
| 159 | int block_nr = 1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 160 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 161 | #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 162 | int want_option_ack = 0; |
| 163 | #endif |
| 164 | |
Eric Andersen | 744a194 | 2001-11-10 11:16:39 +0000 | [diff] [blame] | 165 | /* Can't use RESERVE_CONFIG_BUFFER here since the allocation |
| 166 | * size varies meaning BUFFERS_GO_ON_STACK would fail */ |
| 167 | char *buf=xmalloc(tftp_bufsize + 4); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 168 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 169 | tftp_bufsize += 4; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 170 | |
| 171 | if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 172 | bb_perror_msg("socket"); |
Mark Whitley | 8bb7df4 | 2001-03-06 20:58:48 +0000 | [diff] [blame] | 173 | return EXIT_FAILURE; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | len = sizeof(sa); |
| 177 | |
| 178 | memset(&sa, 0, len); |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 179 | bind(socketfd, (struct sockaddr *)&sa, len); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 180 | |
| 181 | sa.sin_family = host->h_addrtype; |
| 182 | sa.sin_port = htons(port); |
| 183 | memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr, |
| 184 | sizeof(sa.sin_addr)); |
| 185 | |
| 186 | /* build opcode */ |
| 187 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 188 | if (cmd_get) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 189 | opcode = TFTP_RRQ; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 192 | if (cmd_put) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 193 | opcode = TFTP_WRQ; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | while (1) { |
| 197 | |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 198 | cp = buf; |
| 199 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 200 | /* first create the opcode part */ |
| 201 | |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 202 | *((unsigned short *) cp) = htons(opcode); |
| 203 | |
| 204 | cp += 2; |
| 205 | |
| 206 | /* add filename and mode */ |
| 207 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 208 | if ((cmd_get && (opcode == TFTP_RRQ)) || |
| 209 | (cmd_put && (opcode == TFTP_WRQ))) { |
| 210 | int too_long = 0; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 211 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 212 | /* see if the filename fits into buf */ |
| 213 | /* and fill in packet */ |
| 214 | |
| 215 | len = strlen(remotefile) + 1; |
| 216 | |
| 217 | if ((cp + len) >= &buf[tftp_bufsize - 1]) { |
| 218 | too_long = 1; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 219 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 220 | else { |
| 221 | safe_strncpy(cp, remotefile, len); |
| 222 | cp += len; |
| 223 | } |
| 224 | |
| 225 | if (too_long || ((&buf[tftp_bufsize - 1] - cp) < 6)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 226 | bb_error_msg("too long remote-filename"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 230 | /* add "mode" part of the package */ |
| 231 | |
| 232 | memcpy(cp, "octet", 6); |
| 233 | cp += 6; |
| 234 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 235 | #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 236 | |
| 237 | len = tftp_bufsize - 4; /* data block size */ |
| 238 | |
| 239 | if (len != TFTP_BLOCKSIZE_DEFAULT) { |
| 240 | |
| 241 | if ((&buf[tftp_bufsize - 1] - cp) < 15) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 242 | bb_error_msg("too long remote-filename"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | |
| 246 | /* add "blksize" + number of blocks */ |
| 247 | |
| 248 | memcpy(cp, "blksize", 8); |
| 249 | cp += 8; |
| 250 | |
| 251 | cp += snprintf(cp, 6, "%d", len) + 1; |
| 252 | |
| 253 | want_option_ack = 1; |
| 254 | } |
| 255 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* add ack and data */ |
| 259 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 260 | if ((cmd_get && (opcode == TFTP_ACK)) || |
| 261 | (cmd_put && (opcode == TFTP_DATA))) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 262 | |
| 263 | *((unsigned short *) cp) = htons(block_nr); |
| 264 | |
| 265 | cp += 2; |
| 266 | |
| 267 | block_nr++; |
| 268 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 269 | if (cmd_put && (opcode == TFTP_DATA)) { |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 270 | len = read(localfd, cp, tftp_bufsize - 4); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 271 | |
| 272 | if (len < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 273 | bb_perror_msg("read"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 277 | if (len != (tftp_bufsize - 4)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 278 | finished++; |
| 279 | } |
| 280 | |
| 281 | cp += len; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /* send packet */ |
| 287 | |
| 288 | |
Eric Andersen | 05e662a | 2003-07-26 08:16:10 +0000 | [diff] [blame] | 289 | timeout = bb_tftp_num_retries; /* re-initialize */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 290 | do { |
| 291 | |
| 292 | len = cp - buf; |
| 293 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 294 | #ifdef CONFIG_FEATURE_TFTP_DEBUG |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 295 | printf("sending %u bytes\n", len); |
| 296 | for (cp = buf; cp < &buf[len]; cp++) |
| 297 | printf("%02x ", *cp); |
| 298 | printf("\n"); |
| 299 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 300 | if (sendto(socketfd, buf, len, 0, |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 301 | (struct sockaddr *) &sa, sizeof(sa)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 302 | bb_perror_msg("send"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 303 | len = -1; |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | |
Eric Andersen | b99aec0 | 2003-07-30 07:16:39 +0000 | [diff] [blame] | 308 | if (finished && (opcode == TFTP_ACK)) { |
Glenn L McGrath | 0f18271 | 2002-12-19 20:16:22 +0000 | [diff] [blame] | 309 | break; |
| 310 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 311 | |
Glenn L McGrath | 0f18271 | 2002-12-19 20:16:22 +0000 | [diff] [blame] | 312 | /* receive packet */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 313 | |
| 314 | memset(&from, 0, sizeof(from)); |
| 315 | fromlen = sizeof(from); |
| 316 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 317 | tv.tv_sec = TFTP_TIMEOUT; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 318 | tv.tv_usec = 0; |
| 319 | |
| 320 | FD_ZERO(&rfds); |
| 321 | FD_SET(socketfd, &rfds); |
| 322 | |
| 323 | switch (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) { |
| 324 | case 1: |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 325 | len = recvfrom(socketfd, buf, tftp_bufsize, 0, |
| 326 | (struct sockaddr *) &from, &fromlen); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 327 | |
| 328 | if (len < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 329 | bb_perror_msg("recvfrom"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | |
| 333 | timeout = 0; |
| 334 | |
| 335 | if (sa.sin_port == htons(port)) { |
| 336 | sa.sin_port = from.sin_port; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 337 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 338 | if (sa.sin_port == from.sin_port) { |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | /* fall-through for bad packets! */ |
| 343 | /* discard the packet - treat as timeout */ |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 344 | timeout = bb_tftp_num_retries; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 345 | |
| 346 | case 0: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 347 | bb_error_msg("timeout"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 348 | |
Eric Andersen | b99aec0 | 2003-07-30 07:16:39 +0000 | [diff] [blame] | 349 | timeout--; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 350 | if (timeout == 0) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 351 | len = -1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 352 | bb_error_msg("last timeout"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 353 | } |
| 354 | break; |
| 355 | |
| 356 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 357 | bb_perror_msg("select"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 358 | len = -1; |
| 359 | } |
| 360 | |
| 361 | } while (timeout && (len >= 0)); |
| 362 | |
Glenn L McGrath | 0f18271 | 2002-12-19 20:16:22 +0000 | [diff] [blame] | 363 | if ((finished) || (len < 0)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | |
| 367 | /* process received packet */ |
| 368 | |
| 369 | |
| 370 | opcode = ntohs(*((unsigned short *) buf)); |
| 371 | tmp = ntohs(*((unsigned short *) &buf[2])); |
| 372 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 373 | #ifdef CONFIG_FEATURE_TFTP_DEBUG |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 374 | printf("received %d bytes: %04x %04x\n", len, opcode, tmp); |
| 375 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 376 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 377 | if (opcode == TFTP_ERROR) { |
| 378 | char *msg = NULL; |
| 379 | |
| 380 | if (buf[4] != '\0') { |
| 381 | msg = &buf[4]; |
| 382 | buf[tftp_bufsize - 1] = '\0'; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 383 | } else if (tmp < (sizeof(tftp_bb_error_msg) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 384 | / sizeof(char *))) { |
| 385 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 386 | msg = (char *) tftp_bb_error_msg[tmp]; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (msg) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 390 | bb_error_msg("server says: %s", msg); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | break; |
| 394 | } |
| 395 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 396 | #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 397 | if (want_option_ack) { |
| 398 | |
| 399 | want_option_ack = 0; |
| 400 | |
| 401 | if (opcode == TFTP_OACK) { |
| 402 | |
| 403 | /* server seems to support options */ |
| 404 | |
| 405 | char *res; |
| 406 | |
| 407 | res = tftp_option_get(&buf[2], len-2, |
| 408 | "blksize"); |
| 409 | |
| 410 | if (res) { |
Glenn L McGrath | fad90db | 2002-12-09 21:05:40 +0000 | [diff] [blame] | 411 | int blksize = atoi(res); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 412 | |
Glenn L McGrath | fad90db | 2002-12-09 21:05:40 +0000 | [diff] [blame] | 413 | if (tftp_blocksize_check(blksize, |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 414 | tftp_bufsize - 4)) { |
| 415 | |
| 416 | if (cmd_put) { |
| 417 | opcode = TFTP_DATA; |
| 418 | } |
| 419 | else { |
| 420 | opcode = TFTP_ACK; |
| 421 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 422 | #ifdef CONFIG_FEATURE_TFTP_DEBUG |
Glenn L McGrath | fad90db | 2002-12-09 21:05:40 +0000 | [diff] [blame] | 423 | printf("using blksize %u\n", blksize); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 424 | #endif |
Glenn L McGrath | 9bf9f1e | 2002-12-09 21:52:29 +0000 | [diff] [blame] | 425 | tftp_bufsize = blksize + 4; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 426 | block_nr = 0; |
| 427 | continue; |
| 428 | } |
| 429 | } |
| 430 | /* FIXME: |
| 431 | * we should send ERROR 8 */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 432 | bb_error_msg("bad server option"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 436 | bb_error_msg("warning: blksize not supported by server" |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 437 | " - reverting to 512"); |
| 438 | |
| 439 | tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4; |
| 440 | } |
| 441 | #endif |
| 442 | |
| 443 | if (cmd_get && (opcode == TFTP_DATA)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 444 | |
| 445 | if (tmp == block_nr) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 446 | |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 447 | len = write(localfd, &buf[4], len - 4); |
| 448 | |
| 449 | if (len < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 450 | bb_perror_msg("write"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 454 | if (len != (tftp_bufsize - 4)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 455 | finished++; |
| 456 | } |
| 457 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 458 | opcode = TFTP_ACK; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 459 | continue; |
| 460 | } |
| 461 | } |
| 462 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 463 | if (cmd_put && (opcode == TFTP_ACK)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 464 | |
| 465 | if (tmp == (block_nr - 1)) { |
| 466 | if (finished) { |
| 467 | break; |
| 468 | } |
| 469 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 470 | opcode = TFTP_DATA; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 471 | continue; |
| 472 | } |
| 473 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 476 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 477 | close(socketfd); |
| 478 | |
Eric Andersen | 5ad22c9 | 2002-10-25 12:14:02 +0000 | [diff] [blame] | 479 | free(buf); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 480 | #endif |
| 481 | |
Mark Whitley | 8bb7df4 | 2001-03-06 20:58:48 +0000 | [diff] [blame] | 482 | return finished ? EXIT_SUCCESS : EXIT_FAILURE; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | int tftp_main(int argc, char **argv) |
| 486 | { |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 487 | struct hostent *host = NULL; |
| 488 | char *localfile = NULL; |
| 489 | char *remotefile = NULL; |
| 490 | int port = 69; |
| 491 | int cmd = 0; |
| 492 | int fd = -1; |
| 493 | int flags = 0; |
| 494 | int opt; |
| 495 | int result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 496 | int blocksize = TFTP_BLOCKSIZE_DEFAULT; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 497 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 498 | /* figure out what to pass to getopt */ |
| 499 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 500 | #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 501 | #define BS "b:" |
| 502 | #else |
| 503 | #define BS |
| 504 | #endif |
| 505 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 506 | #ifdef CONFIG_FEATURE_TFTP_GET |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 507 | #define GET "g" |
| 508 | #else |
| 509 | #define GET |
| 510 | #endif |
| 511 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 512 | #ifdef CONFIG_FEATURE_TFTP_PUT |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 513 | #define PUT "p" |
| 514 | #else |
| 515 | #define PUT |
| 516 | #endif |
| 517 | |
| 518 | while ((opt = getopt(argc, argv, BS GET PUT "l:r:")) != -1) { |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 519 | switch (opt) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 520 | #ifdef CONFIG_FEATURE_TFTP_BLOCKSIZE |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 521 | case 'b': |
| 522 | blocksize = atoi(optarg); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 523 | if (!tftp_blocksize_check(blocksize, 0)) { |
| 524 | return EXIT_FAILURE; |
| 525 | } |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 526 | break; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 527 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 528 | #ifdef CONFIG_FEATURE_TFTP_GET |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 529 | case 'g': |
| 530 | cmd = tftp_cmd_get; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 531 | flags = O_WRONLY | O_CREAT; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 532 | break; |
| 533 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 534 | #ifdef CONFIG_FEATURE_TFTP_PUT |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 535 | case 'p': |
| 536 | cmd = tftp_cmd_put; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 537 | flags = O_RDONLY; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 538 | break; |
| 539 | #endif |
| 540 | case 'l': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 541 | localfile = bb_xstrdup(optarg); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 542 | break; |
| 543 | case 'r': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 544 | remotefile = bb_xstrdup(optarg); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 545 | break; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 546 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 549 | if ((cmd == 0) || (optind == argc)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 550 | bb_show_usage(); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 551 | } |
Eric Andersen | 744ec1d | 2002-04-15 07:40:27 +0000 | [diff] [blame] | 552 | if(localfile && strcmp(localfile, "-") == 0) { |
Eric Andersen | 7829b8d | 2002-09-10 06:03:31 +0000 | [diff] [blame] | 553 | fd = fileno((cmd==tftp_cmd_get)? stdout : stdin); |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 554 | } |
Eric Andersen | 744ec1d | 2002-04-15 07:40:27 +0000 | [diff] [blame] | 555 | if(localfile == NULL) |
| 556 | localfile = remotefile; |
| 557 | if(remotefile == NULL) |
| 558 | remotefile = localfile; |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 559 | if (fd==-1) { |
| 560 | fd = open(localfile, flags, 0644); |
| 561 | } |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 562 | if (fd < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 563 | bb_perror_msg_and_die("local file"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 566 | host = xgethostbyname(argv[optind]); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 567 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 568 | if (optind + 2 == argc) { |
| 569 | port = atoi(argv[optind + 1]); |
| 570 | } |
| 571 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 572 | #ifdef CONFIG_FEATURE_TFTP_DEBUG |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 573 | printf("using server \"%s\", remotefile \"%s\", " |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 574 | "localfile \"%s\".\n", |
| 575 | inet_ntoa(*((struct in_addr *) host->h_addr)), |
| 576 | remotefile, localfile); |
| 577 | #endif |
| 578 | |
| 579 | result = tftp(cmd, host, remotefile, fd, port, blocksize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 580 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 581 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Eric Andersen | 744ec1d | 2002-04-15 07:40:27 +0000 | [diff] [blame] | 582 | if (!(fd == fileno(stdout) || fd == fileno(stdin))) { |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 583 | close(fd); |
| 584 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 585 | #endif |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 586 | return(result); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 587 | } |