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