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