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