Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ***************************************************************************/ |
| 22 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 23 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 24 | |
| 25 | #ifndef CURL_DISABLE_TFTP |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 26 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 27 | #ifdef HAVE_NETINET_IN_H |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 28 | #include <netinet/in.h> |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 29 | #endif |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 30 | #ifdef HAVE_NETDB_H |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 31 | #include <netdb.h> |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 32 | #endif |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 33 | #ifdef HAVE_ARPA_INET_H |
| 34 | #include <arpa/inet.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_NET_IF_H |
| 37 | #include <net/if.h> |
| 38 | #endif |
| 39 | #ifdef HAVE_SYS_IOCTL_H |
| 40 | #include <sys/ioctl.h> |
| 41 | #endif |
| 42 | |
| 43 | #ifdef HAVE_SYS_PARAM_H |
| 44 | #include <sys/param.h> |
| 45 | #endif |
| 46 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 47 | #include "urldata.h" |
| 48 | #include <curl/curl.h> |
| 49 | #include "transfer.h" |
| 50 | #include "sendf.h" |
| 51 | #include "tftp.h" |
| 52 | #include "progress.h" |
| 53 | #include "connect.h" |
| 54 | #include "strerror.h" |
| 55 | #include "sockaddr.h" /* required for Curl_sockaddr_storage */ |
| 56 | #include "multiif.h" |
| 57 | #include "url.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 58 | #include "strcase.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 59 | #include "speedcheck.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 60 | #include "select.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 61 | #include "escape.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 62 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 63 | /* The last 3 #include files should be in this order */ |
| 64 | #include "curl_printf.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 65 | #include "curl_memory.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 66 | #include "memdebug.h" |
| 67 | |
| 68 | /* RFC2348 allows the block size to be negotiated */ |
| 69 | #define TFTP_BLKSIZE_DEFAULT 512 |
| 70 | #define TFTP_BLKSIZE_MIN 8 |
| 71 | #define TFTP_BLKSIZE_MAX 65464 |
| 72 | #define TFTP_OPTION_BLKSIZE "blksize" |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 73 | |
| 74 | /* from RFC2349: */ |
| 75 | #define TFTP_OPTION_TSIZE "tsize" |
| 76 | #define TFTP_OPTION_INTERVAL "timeout" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 77 | |
| 78 | typedef enum { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 79 | TFTP_MODE_NETASCII = 0, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 80 | TFTP_MODE_OCTET |
| 81 | } tftp_mode_t; |
| 82 | |
| 83 | typedef enum { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 84 | TFTP_STATE_START = 0, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 85 | TFTP_STATE_RX, |
| 86 | TFTP_STATE_TX, |
| 87 | TFTP_STATE_FIN |
| 88 | } tftp_state_t; |
| 89 | |
| 90 | typedef enum { |
| 91 | TFTP_EVENT_NONE = -1, |
| 92 | TFTP_EVENT_INIT = 0, |
| 93 | TFTP_EVENT_RRQ = 1, |
| 94 | TFTP_EVENT_WRQ = 2, |
| 95 | TFTP_EVENT_DATA = 3, |
| 96 | TFTP_EVENT_ACK = 4, |
| 97 | TFTP_EVENT_ERROR = 5, |
| 98 | TFTP_EVENT_OACK = 6, |
| 99 | TFTP_EVENT_TIMEOUT |
| 100 | } tftp_event_t; |
| 101 | |
| 102 | typedef enum { |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 103 | TFTP_ERR_UNDEF = 0, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 104 | TFTP_ERR_NOTFOUND, |
| 105 | TFTP_ERR_PERM, |
| 106 | TFTP_ERR_DISKFULL, |
| 107 | TFTP_ERR_ILLEGAL, |
| 108 | TFTP_ERR_UNKNOWNID, |
| 109 | TFTP_ERR_EXISTS, |
| 110 | TFTP_ERR_NOSUCHUSER, /* This will never be triggered by this code */ |
| 111 | |
| 112 | /* The remaining error codes are internal to curl */ |
| 113 | TFTP_ERR_NONE = -100, |
| 114 | TFTP_ERR_TIMEOUT, |
| 115 | TFTP_ERR_NORESPONSE |
| 116 | } tftp_error_t; |
| 117 | |
| 118 | typedef struct tftp_packet { |
| 119 | unsigned char *data; |
| 120 | } tftp_packet_t; |
| 121 | |
| 122 | typedef struct tftp_state_data { |
| 123 | tftp_state_t state; |
| 124 | tftp_mode_t mode; |
| 125 | tftp_error_t error; |
| 126 | tftp_event_t event; |
| 127 | struct connectdata *conn; |
| 128 | curl_socket_t sockfd; |
| 129 | int retries; |
| 130 | int retry_time; |
| 131 | int retry_max; |
| 132 | time_t start_time; |
| 133 | time_t max_time; |
| 134 | time_t rx_time; |
| 135 | unsigned short block; |
| 136 | struct Curl_sockaddr_storage local_addr; |
| 137 | struct Curl_sockaddr_storage remote_addr; |
| 138 | curl_socklen_t remote_addrlen; |
| 139 | int rbytes; |
| 140 | int sbytes; |
| 141 | int blksize; |
| 142 | int requested_blksize; |
| 143 | tftp_packet_t rpacket; |
| 144 | tftp_packet_t spacket; |
| 145 | } tftp_state_data_t; |
| 146 | |
| 147 | |
| 148 | /* Forward declarations */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 149 | static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event); |
| 150 | static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 151 | static CURLcode tftp_connect(struct connectdata *conn, bool *done); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 152 | static CURLcode tftp_disconnect(struct connectdata *conn, |
| 153 | bool dead_connection); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 154 | static CURLcode tftp_do(struct connectdata *conn, bool *done); |
| 155 | static CURLcode tftp_done(struct connectdata *conn, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 156 | CURLcode, bool premature); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 157 | static CURLcode tftp_setup_connection(struct connectdata * conn); |
| 158 | static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done); |
| 159 | static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done); |
| 160 | static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks, |
| 161 | int numsocks); |
| 162 | static CURLcode tftp_translate_code(tftp_error_t error); |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | * TFTP protocol handler. |
| 167 | */ |
| 168 | |
| 169 | const struct Curl_handler Curl_handler_tftp = { |
| 170 | "TFTP", /* scheme */ |
| 171 | tftp_setup_connection, /* setup_connection */ |
| 172 | tftp_do, /* do_it */ |
| 173 | tftp_done, /* done */ |
| 174 | ZERO_NULL, /* do_more */ |
| 175 | tftp_connect, /* connect_it */ |
| 176 | tftp_multi_statemach, /* connecting */ |
| 177 | tftp_doing, /* doing */ |
| 178 | tftp_getsock, /* proto_getsock */ |
| 179 | tftp_getsock, /* doing_getsock */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 180 | ZERO_NULL, /* domore_getsock */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 181 | ZERO_NULL, /* perform_getsock */ |
| 182 | tftp_disconnect, /* disconnect */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 183 | ZERO_NULL, /* readwrite */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 184 | ZERO_NULL, /* connection_check */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 185 | PORT_TFTP, /* defport */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 186 | CURLPROTO_TFTP, /* protocol */ |
| 187 | PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | /********************************************************** |
| 191 | * |
| 192 | * tftp_set_timeouts - |
| 193 | * |
| 194 | * Set timeouts based on state machine state. |
| 195 | * Use user provided connect timeouts until DATA or ACK |
| 196 | * packet is received, then use user-provided transfer timeouts |
| 197 | * |
| 198 | * |
| 199 | **********************************************************/ |
| 200 | static CURLcode tftp_set_timeouts(tftp_state_data_t *state) |
| 201 | { |
| 202 | time_t maxtime, timeout; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 203 | timediff_t timeout_ms; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 204 | bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 205 | |
| 206 | time(&state->start_time); |
| 207 | |
| 208 | /* Compute drop-dead time */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 209 | timeout_ms = Curl_timeleft(state->conn->data, NULL, start); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 210 | |
| 211 | if(timeout_ms < 0) { |
| 212 | /* time-out, bail out, go home */ |
| 213 | failf(state->conn->data, "Connection time-out"); |
| 214 | return CURLE_OPERATION_TIMEDOUT; |
| 215 | } |
| 216 | |
| 217 | if(start) { |
| 218 | |
| 219 | maxtime = (time_t)(timeout_ms + 500) / 1000; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 220 | state->max_time = state->start_time + maxtime; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 221 | |
| 222 | /* Set per-block timeout to total */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 223 | timeout = maxtime; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 224 | |
| 225 | /* Average restart after 5 seconds */ |
| 226 | state->retry_max = (int)timeout/5; |
| 227 | |
| 228 | if(state->retry_max < 1) |
| 229 | /* avoid division by zero below */ |
| 230 | state->retry_max = 1; |
| 231 | |
| 232 | /* Compute the re-start interval to suit the timeout */ |
| 233 | state->retry_time = (int)timeout/state->retry_max; |
| 234 | if(state->retry_time<1) |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 235 | state->retry_time = 1; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 236 | |
| 237 | } |
| 238 | else { |
| 239 | if(timeout_ms > 0) |
| 240 | maxtime = (time_t)(timeout_ms + 500) / 1000; |
| 241 | else |
| 242 | maxtime = 3600; |
| 243 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 244 | state->max_time = state->start_time + maxtime; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 245 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 246 | /* Set per-block timeout to total */ |
| 247 | timeout = maxtime; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 248 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 249 | /* Average reposting an ACK after 5 seconds */ |
| 250 | state->retry_max = (int)timeout/5; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 251 | } |
| 252 | /* But bound the total number */ |
| 253 | if(state->retry_max<3) |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 254 | state->retry_max = 3; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 255 | |
| 256 | if(state->retry_max>50) |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 257 | state->retry_max = 50; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 258 | |
| 259 | /* Compute the re-ACK interval to suit the timeout */ |
| 260 | state->retry_time = (int)(timeout/state->retry_max); |
| 261 | if(state->retry_time<1) |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 262 | state->retry_time = 1; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 263 | |
| 264 | infof(state->conn->data, |
| 265 | "set timeouts for state %d; Total %ld, retry %d maxtry %d\n", |
| 266 | (int)state->state, (long)(state->max_time-state->start_time), |
| 267 | state->retry_time, state->retry_max); |
| 268 | |
| 269 | /* init RX time */ |
| 270 | time(&state->rx_time); |
| 271 | |
| 272 | return CURLE_OK; |
| 273 | } |
| 274 | |
| 275 | /********************************************************** |
| 276 | * |
| 277 | * tftp_set_send_first |
| 278 | * |
| 279 | * Event handler for the START state |
| 280 | * |
| 281 | **********************************************************/ |
| 282 | |
| 283 | static void setpacketevent(tftp_packet_t *packet, unsigned short num) |
| 284 | { |
| 285 | packet->data[0] = (unsigned char)(num >> 8); |
| 286 | packet->data[1] = (unsigned char)(num & 0xff); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | static void setpacketblock(tftp_packet_t *packet, unsigned short num) |
| 291 | { |
| 292 | packet->data[2] = (unsigned char)(num >> 8); |
| 293 | packet->data[3] = (unsigned char)(num & 0xff); |
| 294 | } |
| 295 | |
| 296 | static unsigned short getrpacketevent(const tftp_packet_t *packet) |
| 297 | { |
| 298 | return (unsigned short)((packet->data[0] << 8) | packet->data[1]); |
| 299 | } |
| 300 | |
| 301 | static unsigned short getrpacketblock(const tftp_packet_t *packet) |
| 302 | { |
| 303 | return (unsigned short)((packet->data[2] << 8) | packet->data[3]); |
| 304 | } |
| 305 | |
| 306 | static size_t Curl_strnlen(const char *string, size_t maxlen) |
| 307 | { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 308 | const char *end = memchr(string, '\0', maxlen); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 309 | return end ? (size_t) (end - string) : maxlen; |
| 310 | } |
| 311 | |
| 312 | static const char *tftp_option_get(const char *buf, size_t len, |
| 313 | const char **option, const char **value) |
| 314 | { |
| 315 | size_t loc; |
| 316 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 317 | loc = Curl_strnlen(buf, len); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 318 | loc++; /* NULL term */ |
| 319 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 320 | if(loc >= len) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 321 | return NULL; |
| 322 | *option = buf; |
| 323 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 324 | loc += Curl_strnlen(buf + loc, len-loc); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 325 | loc++; /* NULL term */ |
| 326 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 327 | if(loc > len) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 328 | return NULL; |
| 329 | *value = &buf[strlen(*option) + 1]; |
| 330 | |
| 331 | return &buf[loc]; |
| 332 | } |
| 333 | |
| 334 | static CURLcode tftp_parse_option_ack(tftp_state_data_t *state, |
| 335 | const char *ptr, int len) |
| 336 | { |
| 337 | const char *tmp = ptr; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 338 | struct Curl_easy *data = state->conn->data; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 339 | |
| 340 | /* if OACK doesn't contain blksize option, the default (512) must be used */ |
| 341 | state->blksize = TFTP_BLKSIZE_DEFAULT; |
| 342 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 343 | while(tmp < ptr + len) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 344 | const char *option, *value; |
| 345 | |
| 346 | tmp = tftp_option_get(tmp, ptr + len - tmp, &option, &value); |
| 347 | if(tmp == NULL) { |
| 348 | failf(data, "Malformed ACK packet, rejecting"); |
| 349 | return CURLE_TFTP_ILLEGAL; |
| 350 | } |
| 351 | |
| 352 | infof(data, "got option=(%s) value=(%s)\n", option, value); |
| 353 | |
| 354 | if(checkprefix(option, TFTP_OPTION_BLKSIZE)) { |
| 355 | long blksize; |
| 356 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 357 | blksize = strtol(value, NULL, 10); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 358 | |
| 359 | if(!blksize) { |
| 360 | failf(data, "invalid blocksize value in OACK packet"); |
| 361 | return CURLE_TFTP_ILLEGAL; |
| 362 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 363 | if(blksize > TFTP_BLKSIZE_MAX) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 364 | failf(data, "%s (%d)", "blksize is larger than max supported", |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 365 | TFTP_BLKSIZE_MAX); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 366 | return CURLE_TFTP_ILLEGAL; |
| 367 | } |
| 368 | else if(blksize < TFTP_BLKSIZE_MIN) { |
| 369 | failf(data, "%s (%d)", "blksize is smaller than min supported", |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 370 | TFTP_BLKSIZE_MIN); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 371 | return CURLE_TFTP_ILLEGAL; |
| 372 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 373 | else if(blksize > state->requested_blksize) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 374 | /* could realloc pkt buffers here, but the spec doesn't call out |
| 375 | * support for the server requesting a bigger blksize than the client |
| 376 | * requests */ |
| 377 | failf(data, "%s (%ld)", |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 378 | "server requested blksize larger than allocated", blksize); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 379 | return CURLE_TFTP_ILLEGAL; |
| 380 | } |
| 381 | |
| 382 | state->blksize = (int)blksize; |
| 383 | infof(data, "%s (%d) %s (%d)\n", "blksize parsed from OACK", |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 384 | state->blksize, "requested", state->requested_blksize); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 385 | } |
| 386 | else if(checkprefix(option, TFTP_OPTION_TSIZE)) { |
| 387 | long tsize = 0; |
| 388 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 389 | tsize = strtol(value, NULL, 10); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 390 | infof(data, "%s (%ld)\n", "tsize parsed from OACK", tsize); |
| 391 | |
| 392 | /* tsize should be ignored on upload: Who cares about the size of the |
| 393 | remote file? */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 394 | if(!data->set.upload) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 395 | if(!tsize) { |
| 396 | failf(data, "invalid tsize -:%s:- value in OACK packet", value); |
| 397 | return CURLE_TFTP_ILLEGAL; |
| 398 | } |
| 399 | Curl_pgrsSetDownloadSize(data, tsize); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return CURLE_OK; |
| 405 | } |
| 406 | |
| 407 | static size_t tftp_option_add(tftp_state_data_t *state, size_t csize, |
| 408 | char *buf, const char *option) |
| 409 | { |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 410 | if(( strlen(option) + csize + 1) > (size_t)state->blksize) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 411 | return 0; |
| 412 | strcpy(buf, option); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 413 | return strlen(option) + 1; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static CURLcode tftp_connect_for_tx(tftp_state_data_t *state, |
| 417 | tftp_event_t event) |
| 418 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 419 | CURLcode result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 420 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 421 | struct Curl_easy *data = state->conn->data; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 422 | |
| 423 | infof(data, "%s\n", "Connected for transmit"); |
| 424 | #endif |
| 425 | state->state = TFTP_STATE_TX; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 426 | result = tftp_set_timeouts(state); |
| 427 | if(result) |
| 428 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 429 | return tftp_tx(state, event); |
| 430 | } |
| 431 | |
| 432 | static CURLcode tftp_connect_for_rx(tftp_state_data_t *state, |
| 433 | tftp_event_t event) |
| 434 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 435 | CURLcode result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 436 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 437 | struct Curl_easy *data = state->conn->data; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 438 | |
| 439 | infof(data, "%s\n", "Connected for receive"); |
| 440 | #endif |
| 441 | state->state = TFTP_STATE_RX; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 442 | result = tftp_set_timeouts(state); |
| 443 | if(result) |
| 444 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 445 | return tftp_rx(state, event); |
| 446 | } |
| 447 | |
| 448 | static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event) |
| 449 | { |
| 450 | size_t sbytes; |
| 451 | ssize_t senddata; |
| 452 | const char *mode = "octet"; |
| 453 | char *filename; |
| 454 | char buf[64]; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 455 | struct Curl_easy *data = state->conn->data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 456 | CURLcode result = CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 457 | |
| 458 | /* Set ascii mode if -B flag was used */ |
| 459 | if(data->set.prefer_ascii) |
| 460 | mode = "netascii"; |
| 461 | |
| 462 | switch(event) { |
| 463 | |
| 464 | case TFTP_EVENT_INIT: /* Send the first packet out */ |
| 465 | case TFTP_EVENT_TIMEOUT: /* Resend the first packet out */ |
| 466 | /* Increment the retry counter, quit if over the limit */ |
| 467 | state->retries++; |
| 468 | if(state->retries>state->retry_max) { |
| 469 | state->error = TFTP_ERR_NORESPONSE; |
| 470 | state->state = TFTP_STATE_FIN; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 471 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if(data->set.upload) { |
| 475 | /* If we are uploading, send an WRQ */ |
| 476 | setpacketevent(&state->spacket, TFTP_EVENT_WRQ); |
| 477 | state->conn->data->req.upload_fromhere = |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 478 | (char *)state->spacket.data + 4; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 479 | if(data->state.infilesize != -1) |
| 480 | Curl_pgrsSetUploadSize(data, data->state.infilesize); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 481 | } |
| 482 | else { |
| 483 | /* If we are downloading, send an RRQ */ |
| 484 | setpacketevent(&state->spacket, TFTP_EVENT_RRQ); |
| 485 | } |
| 486 | /* As RFC3617 describes the separator slash is not actually part of the |
| 487 | file name so we skip the always-present first letter of the path |
| 488 | string. */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 489 | result = Curl_urldecode(data, &state->conn->data->state.path[1], 0, |
| 490 | &filename, NULL, FALSE); |
| 491 | if(result) |
| 492 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 493 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 494 | if(strlen(filename) > (state->blksize - strlen(mode) - 4)) { |
| 495 | failf(data, "TFTP file name too long\n"); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 496 | free(filename); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 497 | return CURLE_TFTP_ILLEGAL; /* too long file name field */ |
| 498 | } |
| 499 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 500 | snprintf((char *)state->spacket.data + 2, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 501 | state->blksize, |
| 502 | "%s%c%s%c", filename, '\0', mode, '\0'); |
| 503 | sbytes = 4 + strlen(filename) + strlen(mode); |
| 504 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 505 | /* optional addition of TFTP options */ |
| 506 | if(!data->set.tftp_no_options) { |
| 507 | /* add tsize option */ |
| 508 | if(data->set.upload && (data->state.infilesize != -1)) |
| 509 | snprintf(buf, sizeof(buf), "%" CURL_FORMAT_CURL_OFF_T, |
| 510 | data->state.infilesize); |
| 511 | else |
| 512 | strcpy(buf, "0"); /* the destination is large enough */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 513 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 514 | sbytes += tftp_option_add(state, sbytes, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 515 | (char *)state->spacket.data + sbytes, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 516 | TFTP_OPTION_TSIZE); |
| 517 | sbytes += tftp_option_add(state, sbytes, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 518 | (char *)state->spacket.data + sbytes, buf); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 519 | /* add blksize option */ |
| 520 | snprintf(buf, sizeof(buf), "%d", state->requested_blksize); |
| 521 | sbytes += tftp_option_add(state, sbytes, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 522 | (char *)state->spacket.data + sbytes, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 523 | TFTP_OPTION_BLKSIZE); |
| 524 | sbytes += tftp_option_add(state, sbytes, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 525 | (char *)state->spacket.data + sbytes, buf); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 526 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 527 | /* add timeout option */ |
| 528 | snprintf(buf, sizeof(buf), "%d", state->retry_time); |
| 529 | sbytes += tftp_option_add(state, sbytes, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 530 | (char *)state->spacket.data + sbytes, |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 531 | TFTP_OPTION_INTERVAL); |
| 532 | sbytes += tftp_option_add(state, sbytes, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 533 | (char *)state->spacket.data + sbytes, buf); |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 534 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 535 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 536 | /* the typecase for the 3rd argument is mostly for systems that do |
| 537 | not have a size_t argument, like older unixes that want an 'int' */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 538 | senddata = sendto(state->sockfd, (void *)state->spacket.data, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 539 | (SEND_TYPE_ARG3)sbytes, 0, |
| 540 | state->conn->ip_addr->ai_addr, |
| 541 | state->conn->ip_addr->ai_addrlen); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 542 | if(senddata != (ssize_t)sbytes) { |
| 543 | failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); |
| 544 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 545 | free(filename); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 546 | break; |
| 547 | |
| 548 | case TFTP_EVENT_OACK: |
| 549 | if(data->set.upload) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 550 | result = tftp_connect_for_tx(state, event); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 551 | } |
| 552 | else { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 553 | result = tftp_connect_for_rx(state, event); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 554 | } |
| 555 | break; |
| 556 | |
| 557 | case TFTP_EVENT_ACK: /* Connected for transmit */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 558 | result = tftp_connect_for_tx(state, event); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 559 | break; |
| 560 | |
| 561 | case TFTP_EVENT_DATA: /* Connected for receive */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 562 | result = tftp_connect_for_rx(state, event); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 563 | break; |
| 564 | |
| 565 | case TFTP_EVENT_ERROR: |
| 566 | state->state = TFTP_STATE_FIN; |
| 567 | break; |
| 568 | |
| 569 | default: |
| 570 | failf(state->conn->data, "tftp_send_first: internal error"); |
| 571 | break; |
| 572 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 573 | |
| 574 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 575 | } |
| 576 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 577 | /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit |
| 578 | boundary */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 579 | #define NEXT_BLOCKNUM(x) (((x) + 1)&0xffff) |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 580 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 581 | /********************************************************** |
| 582 | * |
| 583 | * tftp_rx |
| 584 | * |
| 585 | * Event handler for the RX state |
| 586 | * |
| 587 | **********************************************************/ |
| 588 | static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) |
| 589 | { |
| 590 | ssize_t sbytes; |
| 591 | int rblock; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 592 | struct Curl_easy *data = state->conn->data; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 593 | |
| 594 | switch(event) { |
| 595 | |
| 596 | case TFTP_EVENT_DATA: |
| 597 | /* Is this the block we expect? */ |
| 598 | rblock = getrpacketblock(&state->rpacket); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 599 | if(NEXT_BLOCKNUM(state->block) == rblock) { |
| 600 | /* This is the expected block. Reset counters and ACK it. */ |
| 601 | state->retries = 0; |
| 602 | } |
| 603 | else if(state->block == rblock) { |
| 604 | /* This is the last recently received block again. Log it and ACK it |
| 605 | again. */ |
| 606 | infof(data, "Received last DATA packet block %d again.\n", rblock); |
| 607 | } |
| 608 | else { |
| 609 | /* totally unexpected, just log it */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 610 | infof(data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 611 | "Received unexpected DATA packet block %d, expecting block %d\n", |
| 612 | rblock, NEXT_BLOCKNUM(state->block)); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 613 | break; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 614 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 615 | |
| 616 | /* ACK this block. */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 617 | state->block = (unsigned short)rblock; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 618 | setpacketevent(&state->spacket, TFTP_EVENT_ACK); |
| 619 | setpacketblock(&state->spacket, state->block); |
| 620 | sbytes = sendto(state->sockfd, (void *)state->spacket.data, |
| 621 | 4, SEND_4TH_ARG, |
| 622 | (struct sockaddr *)&state->remote_addr, |
| 623 | state->remote_addrlen); |
| 624 | if(sbytes < 0) { |
| 625 | failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); |
| 626 | return CURLE_SEND_ERROR; |
| 627 | } |
| 628 | |
| 629 | /* Check if completed (That is, a less than full packet is received) */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 630 | if(state->rbytes < (ssize_t)state->blksize + 4) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 631 | state->state = TFTP_STATE_FIN; |
| 632 | } |
| 633 | else { |
| 634 | state->state = TFTP_STATE_RX; |
| 635 | } |
| 636 | time(&state->rx_time); |
| 637 | break; |
| 638 | |
| 639 | case TFTP_EVENT_OACK: |
| 640 | /* ACK option acknowledgement so we can move on to data */ |
| 641 | state->block = 0; |
| 642 | state->retries = 0; |
| 643 | setpacketevent(&state->spacket, TFTP_EVENT_ACK); |
| 644 | setpacketblock(&state->spacket, state->block); |
| 645 | sbytes = sendto(state->sockfd, (void *)state->spacket.data, |
| 646 | 4, SEND_4TH_ARG, |
| 647 | (struct sockaddr *)&state->remote_addr, |
| 648 | state->remote_addrlen); |
| 649 | if(sbytes < 0) { |
| 650 | failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); |
| 651 | return CURLE_SEND_ERROR; |
| 652 | } |
| 653 | |
| 654 | /* we're ready to RX data */ |
| 655 | state->state = TFTP_STATE_RX; |
| 656 | time(&state->rx_time); |
| 657 | break; |
| 658 | |
| 659 | case TFTP_EVENT_TIMEOUT: |
| 660 | /* Increment the retry count and fail if over the limit */ |
| 661 | state->retries++; |
| 662 | infof(data, |
| 663 | "Timeout waiting for block %d ACK. Retries = %d\n", |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 664 | NEXT_BLOCKNUM(state->block), state->retries); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 665 | if(state->retries > state->retry_max) { |
| 666 | state->error = TFTP_ERR_TIMEOUT; |
| 667 | state->state = TFTP_STATE_FIN; |
| 668 | } |
| 669 | else { |
| 670 | /* Resend the previous ACK */ |
| 671 | sbytes = sendto(state->sockfd, (void *)state->spacket.data, |
| 672 | 4, SEND_4TH_ARG, |
| 673 | (struct sockaddr *)&state->remote_addr, |
| 674 | state->remote_addrlen); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 675 | if(sbytes<0) { |
| 676 | failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); |
| 677 | return CURLE_SEND_ERROR; |
| 678 | } |
| 679 | } |
| 680 | break; |
| 681 | |
| 682 | case TFTP_EVENT_ERROR: |
| 683 | setpacketevent(&state->spacket, TFTP_EVENT_ERROR); |
| 684 | setpacketblock(&state->spacket, state->block); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 685 | (void)sendto(state->sockfd, (void *)state->spacket.data, |
| 686 | 4, SEND_4TH_ARG, |
| 687 | (struct sockaddr *)&state->remote_addr, |
| 688 | state->remote_addrlen); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 689 | /* don't bother with the return code, but if the socket is still up we |
| 690 | * should be a good TFTP client and let the server know we're done */ |
| 691 | state->state = TFTP_STATE_FIN; |
| 692 | break; |
| 693 | |
| 694 | default: |
| 695 | failf(data, "%s", "tftp_rx: internal error"); |
| 696 | return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for |
| 697 | this */ |
| 698 | } |
| 699 | return CURLE_OK; |
| 700 | } |
| 701 | |
| 702 | /********************************************************** |
| 703 | * |
| 704 | * tftp_tx |
| 705 | * |
| 706 | * Event handler for the TX state |
| 707 | * |
| 708 | **********************************************************/ |
| 709 | static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) |
| 710 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 711 | struct Curl_easy *data = state->conn->data; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 712 | ssize_t sbytes; |
| 713 | int rblock; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 714 | CURLcode result = CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 715 | struct SingleRequest *k = &data->req; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 716 | int cb; /* Bytes currently read */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 717 | |
| 718 | switch(event) { |
| 719 | |
| 720 | case TFTP_EVENT_ACK: |
| 721 | case TFTP_EVENT_OACK: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 722 | if(event == TFTP_EVENT_ACK) { |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 723 | /* Ack the packet */ |
| 724 | rblock = getrpacketblock(&state->rpacket); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 725 | |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 726 | if(rblock != state->block && |
| 727 | /* There's a bug in tftpd-hpa that causes it to send us an ack for |
| 728 | * 65535 when the block number wraps to 0. So when we're expecting |
| 729 | * 0, also accept 65535. See |
| 730 | * http://syslinux.zytor.com/archives/2010-September/015253.html |
| 731 | * */ |
| 732 | !(state->block == 0 && rblock == 65535)) { |
| 733 | /* This isn't the expected block. Log it and up the retry counter */ |
| 734 | infof(data, "Received ACK for block %d, expecting %d\n", |
| 735 | rblock, state->block); |
| 736 | state->retries++; |
| 737 | /* Bail out if over the maximum */ |
| 738 | if(state->retries>state->retry_max) { |
| 739 | failf(data, "tftp_tx: giving up waiting for block %d ack", |
| 740 | state->block); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 741 | result = CURLE_SEND_ERROR; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 742 | } |
| 743 | else { |
| 744 | /* Re-send the data packet */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 745 | sbytes = sendto(state->sockfd, (void *)state->spacket.data, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 746 | 4 + state->sbytes, SEND_4TH_ARG, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 747 | (struct sockaddr *)&state->remote_addr, |
| 748 | state->remote_addrlen); |
| 749 | /* Check all sbytes were sent */ |
| 750 | if(sbytes<0) { |
| 751 | failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 752 | result = CURLE_SEND_ERROR; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 753 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 754 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 755 | |
| 756 | return result; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 757 | } |
| 758 | /* This is the expected packet. Reset the counters and send the next |
| 759 | block */ |
| 760 | time(&state->rx_time); |
| 761 | state->block++; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 762 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 763 | else |
| 764 | state->block = 1; /* first data block is 1 when using OACK */ |
| 765 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 766 | state->retries = 0; |
| 767 | setpacketevent(&state->spacket, TFTP_EVENT_DATA); |
| 768 | setpacketblock(&state->spacket, state->block); |
| 769 | if(state->block > 1 && state->sbytes < (int)state->blksize) { |
| 770 | state->state = TFTP_STATE_FIN; |
| 771 | return CURLE_OK; |
| 772 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 773 | |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 774 | /* TFTP considers data block size < 512 bytes as an end of session. So |
| 775 | * in some cases we must wait for additional data to build full (512 bytes) |
| 776 | * data block. |
| 777 | * */ |
| 778 | state->sbytes = 0; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 779 | state->conn->data->req.upload_fromhere = (char *)state->spacket.data + 4; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 780 | do { |
| 781 | result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes, |
| 782 | &cb); |
| 783 | if(result) |
| 784 | return result; |
| 785 | state->sbytes += cb; |
| 786 | state->conn->data->req.upload_fromhere += cb; |
| 787 | } while(state->sbytes < state->blksize && cb != 0); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 788 | |
| 789 | sbytes = sendto(state->sockfd, (void *) state->spacket.data, |
| 790 | 4 + state->sbytes, SEND_4TH_ARG, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 791 | (struct sockaddr *)&state->remote_addr, |
| 792 | state->remote_addrlen); |
| 793 | /* Check all sbytes were sent */ |
| 794 | if(sbytes<0) { |
| 795 | failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); |
| 796 | return CURLE_SEND_ERROR; |
| 797 | } |
| 798 | /* Update the progress meter */ |
| 799 | k->writebytecount += state->sbytes; |
| 800 | Curl_pgrsSetUploadCounter(data, k->writebytecount); |
| 801 | break; |
| 802 | |
| 803 | case TFTP_EVENT_TIMEOUT: |
| 804 | /* Increment the retry counter and log the timeout */ |
| 805 | state->retries++; |
| 806 | infof(data, "Timeout waiting for block %d ACK. " |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 807 | " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 808 | /* Decide if we've had enough */ |
| 809 | if(state->retries > state->retry_max) { |
| 810 | state->error = TFTP_ERR_TIMEOUT; |
| 811 | state->state = TFTP_STATE_FIN; |
| 812 | } |
| 813 | else { |
| 814 | /* Re-send the data packet */ |
| 815 | sbytes = sendto(state->sockfd, (void *)state->spacket.data, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 816 | 4 + state->sbytes, SEND_4TH_ARG, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 817 | (struct sockaddr *)&state->remote_addr, |
| 818 | state->remote_addrlen); |
| 819 | /* Check all sbytes were sent */ |
| 820 | if(sbytes<0) { |
| 821 | failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO)); |
| 822 | return CURLE_SEND_ERROR; |
| 823 | } |
| 824 | /* since this was a re-send, we remain at the still byte position */ |
| 825 | Curl_pgrsSetUploadCounter(data, k->writebytecount); |
| 826 | } |
| 827 | break; |
| 828 | |
| 829 | case TFTP_EVENT_ERROR: |
| 830 | state->state = TFTP_STATE_FIN; |
| 831 | setpacketevent(&state->spacket, TFTP_EVENT_ERROR); |
| 832 | setpacketblock(&state->spacket, state->block); |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 833 | (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG, |
| 834 | (struct sockaddr *)&state->remote_addr, |
| 835 | state->remote_addrlen); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 836 | /* don't bother with the return code, but if the socket is still up we |
| 837 | * should be a good TFTP client and let the server know we're done */ |
| 838 | state->state = TFTP_STATE_FIN; |
| 839 | break; |
| 840 | |
| 841 | default: |
| 842 | failf(data, "tftp_tx: internal error, event: %i", (int)(event)); |
| 843 | break; |
| 844 | } |
| 845 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 846 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /********************************************************** |
| 850 | * |
| 851 | * tftp_translate_code |
| 852 | * |
| 853 | * Translate internal error codes to CURL error codes |
| 854 | * |
| 855 | **********************************************************/ |
| 856 | static CURLcode tftp_translate_code(tftp_error_t error) |
| 857 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 858 | CURLcode result = CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 859 | |
| 860 | if(error != TFTP_ERR_NONE) { |
| 861 | switch(error) { |
| 862 | case TFTP_ERR_NOTFOUND: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 863 | result = CURLE_TFTP_NOTFOUND; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 864 | break; |
| 865 | case TFTP_ERR_PERM: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 866 | result = CURLE_TFTP_PERM; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 867 | break; |
| 868 | case TFTP_ERR_DISKFULL: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 869 | result = CURLE_REMOTE_DISK_FULL; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 870 | break; |
| 871 | case TFTP_ERR_UNDEF: |
| 872 | case TFTP_ERR_ILLEGAL: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 873 | result = CURLE_TFTP_ILLEGAL; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 874 | break; |
| 875 | case TFTP_ERR_UNKNOWNID: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 876 | result = CURLE_TFTP_UNKNOWNID; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 877 | break; |
| 878 | case TFTP_ERR_EXISTS: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 879 | result = CURLE_REMOTE_FILE_EXISTS; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 880 | break; |
| 881 | case TFTP_ERR_NOSUCHUSER: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 882 | result = CURLE_TFTP_NOSUCHUSER; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 883 | break; |
| 884 | case TFTP_ERR_TIMEOUT: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 885 | result = CURLE_OPERATION_TIMEDOUT; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 886 | break; |
| 887 | case TFTP_ERR_NORESPONSE: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 888 | result = CURLE_COULDNT_CONNECT; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 889 | break; |
| 890 | default: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 891 | result = CURLE_ABORTED_BY_CALLBACK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 892 | break; |
| 893 | } |
| 894 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 895 | else |
| 896 | result = CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 897 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 898 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /********************************************************** |
| 902 | * |
| 903 | * tftp_state_machine |
| 904 | * |
| 905 | * The tftp state machine event dispatcher |
| 906 | * |
| 907 | **********************************************************/ |
| 908 | static CURLcode tftp_state_machine(tftp_state_data_t *state, |
| 909 | tftp_event_t event) |
| 910 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 911 | CURLcode result = CURLE_OK; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 912 | struct Curl_easy *data = state->conn->data; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 913 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 914 | switch(state->state) { |
| 915 | case TFTP_STATE_START: |
| 916 | DEBUGF(infof(data, "TFTP_STATE_START\n")); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 917 | result = tftp_send_first(state, event); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 918 | break; |
| 919 | case TFTP_STATE_RX: |
| 920 | DEBUGF(infof(data, "TFTP_STATE_RX\n")); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 921 | result = tftp_rx(state, event); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 922 | break; |
| 923 | case TFTP_STATE_TX: |
| 924 | DEBUGF(infof(data, "TFTP_STATE_TX\n")); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 925 | result = tftp_tx(state, event); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 926 | break; |
| 927 | case TFTP_STATE_FIN: |
| 928 | infof(data, "%s\n", "TFTP finished"); |
| 929 | break; |
| 930 | default: |
| 931 | DEBUGF(infof(data, "STATE: %d\n", state->state)); |
| 932 | failf(data, "%s", "Internal state machine error"); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 933 | result = CURLE_TFTP_ILLEGAL; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 934 | break; |
| 935 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 936 | |
| 937 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /********************************************************** |
| 941 | * |
| 942 | * tftp_disconnect |
| 943 | * |
| 944 | * The disconnect callback |
| 945 | * |
| 946 | **********************************************************/ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 947 | static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 948 | { |
| 949 | tftp_state_data_t *state = conn->proto.tftpc; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 950 | (void) dead_connection; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 951 | |
| 952 | /* done, free dynamically allocated pkt buffers */ |
| 953 | if(state) { |
| 954 | Curl_safefree(state->rpacket.data); |
| 955 | Curl_safefree(state->spacket.data); |
| 956 | free(state); |
| 957 | } |
| 958 | |
| 959 | return CURLE_OK; |
| 960 | } |
| 961 | |
| 962 | /********************************************************** |
| 963 | * |
| 964 | * tftp_connect |
| 965 | * |
| 966 | * The connect callback |
| 967 | * |
| 968 | **********************************************************/ |
| 969 | static CURLcode tftp_connect(struct connectdata *conn, bool *done) |
| 970 | { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 971 | tftp_state_data_t *state; |
| 972 | int blksize, rc; |
| 973 | |
| 974 | blksize = TFTP_BLKSIZE_DEFAULT; |
| 975 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 976 | state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t)); |
| 977 | if(!state) |
| 978 | return CURLE_OUT_OF_MEMORY; |
| 979 | |
| 980 | /* alloc pkt buffers based on specified blksize */ |
| 981 | if(conn->data->set.tftp_blksize) { |
| 982 | blksize = (int)conn->data->set.tftp_blksize; |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 983 | if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 984 | return CURLE_TFTP_ILLEGAL; |
| 985 | } |
| 986 | |
| 987 | if(!state->rpacket.data) { |
| 988 | state->rpacket.data = calloc(1, blksize + 2 + 2); |
| 989 | |
| 990 | if(!state->rpacket.data) |
| 991 | return CURLE_OUT_OF_MEMORY; |
| 992 | } |
| 993 | |
| 994 | if(!state->spacket.data) { |
| 995 | state->spacket.data = calloc(1, blksize + 2 + 2); |
| 996 | |
| 997 | if(!state->spacket.data) |
| 998 | return CURLE_OUT_OF_MEMORY; |
| 999 | } |
| 1000 | |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 1001 | /* we don't keep TFTP connections up basically because there's none or very |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1002 | * little gain for UDP */ |
| 1003 | connclose(conn, "TFTP"); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1004 | |
| 1005 | state->conn = conn; |
| 1006 | state->sockfd = state->conn->sock[FIRSTSOCKET]; |
| 1007 | state->state = TFTP_STATE_START; |
| 1008 | state->error = TFTP_ERR_NONE; |
| 1009 | state->blksize = TFTP_BLKSIZE_DEFAULT; |
| 1010 | state->requested_blksize = blksize; |
| 1011 | |
| 1012 | ((struct sockaddr *)&state->local_addr)->sa_family = |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 1013 | (CURL_SA_FAMILY_T)(conn->ip_addr->ai_family); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1014 | |
| 1015 | tftp_set_timeouts(state); |
| 1016 | |
| 1017 | if(!conn->bits.bound) { |
| 1018 | /* If not already bound, bind to any interface, random UDP port. If it is |
| 1019 | * reused or a custom local port was desired, this has already been done! |
| 1020 | * |
| 1021 | * We once used the size of the local_addr struct as the third argument |
| 1022 | * for bind() to better work with IPv6 or whatever size the struct could |
| 1023 | * have, but we learned that at least Tru64, AIX and IRIX *requires* the |
| 1024 | * size of that argument to match the exact size of a 'sockaddr_in' struct |
| 1025 | * when running IPv4-only. |
| 1026 | * |
| 1027 | * Therefore we use the size from the address we connected to, which we |
| 1028 | * assume uses the same IP version and thus hopefully this works for both |
| 1029 | * IPv4 and IPv6... |
| 1030 | */ |
| 1031 | rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr, |
| 1032 | conn->ip_addr->ai_addrlen); |
| 1033 | if(rc) { |
| 1034 | failf(conn->data, "bind() failed; %s", |
| 1035 | Curl_strerror(conn, SOCKERRNO)); |
| 1036 | return CURLE_COULDNT_CONNECT; |
| 1037 | } |
| 1038 | conn->bits.bound = TRUE; |
| 1039 | } |
| 1040 | |
| 1041 | Curl_pgrsStartNow(conn->data); |
| 1042 | |
| 1043 | *done = TRUE; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1044 | |
| 1045 | return CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /********************************************************** |
| 1049 | * |
| 1050 | * tftp_done |
| 1051 | * |
| 1052 | * The done callback |
| 1053 | * |
| 1054 | **********************************************************/ |
| 1055 | static CURLcode tftp_done(struct connectdata *conn, CURLcode status, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1056 | bool premature) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1057 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1058 | CURLcode result = CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1059 | tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; |
| 1060 | |
| 1061 | (void)status; /* unused */ |
| 1062 | (void)premature; /* not used */ |
| 1063 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1064 | if(Curl_pgrsDone(conn)) |
| 1065 | return CURLE_ABORTED_BY_CALLBACK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1066 | |
| 1067 | /* If we have encountered an error */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1068 | if(state) |
| 1069 | result = tftp_translate_code(state->error); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1070 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1071 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | /********************************************************** |
| 1075 | * |
| 1076 | * tftp_getsock |
| 1077 | * |
| 1078 | * The getsock callback |
| 1079 | * |
| 1080 | **********************************************************/ |
| 1081 | static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1082 | int numsocks) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1083 | { |
| 1084 | if(!numsocks) |
| 1085 | return GETSOCK_BLANK; |
| 1086 | |
| 1087 | socks[0] = conn->sock[FIRSTSOCKET]; |
| 1088 | |
| 1089 | return GETSOCK_READSOCK(0); |
| 1090 | } |
| 1091 | |
| 1092 | /********************************************************** |
| 1093 | * |
| 1094 | * tftp_receive_packet |
| 1095 | * |
| 1096 | * Called once select fires and data is ready on the socket |
| 1097 | * |
| 1098 | **********************************************************/ |
| 1099 | static CURLcode tftp_receive_packet(struct connectdata *conn) |
| 1100 | { |
| 1101 | struct Curl_sockaddr_storage fromaddr; |
| 1102 | curl_socklen_t fromlen; |
| 1103 | CURLcode result = CURLE_OK; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1104 | struct Curl_easy *data = conn->data; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1105 | tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; |
| 1106 | struct SingleRequest *k = &data->req; |
| 1107 | |
| 1108 | /* Receive the packet */ |
| 1109 | fromlen = sizeof(fromaddr); |
| 1110 | state->rbytes = (int)recvfrom(state->sockfd, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1111 | (void *)state->rpacket.data, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1112 | state->blksize + 4, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1113 | 0, |
| 1114 | (struct sockaddr *)&fromaddr, |
| 1115 | &fromlen); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1116 | if(state->remote_addrlen == 0) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1117 | memcpy(&state->remote_addr, &fromaddr, fromlen); |
| 1118 | state->remote_addrlen = fromlen; |
| 1119 | } |
| 1120 | |
| 1121 | /* Sanity check packet length */ |
| 1122 | if(state->rbytes < 4) { |
| 1123 | failf(data, "Received too short packet"); |
| 1124 | /* Not a timeout, but how best to handle it? */ |
| 1125 | state->event = TFTP_EVENT_TIMEOUT; |
| 1126 | } |
| 1127 | else { |
| 1128 | /* The event is given by the TFTP packet time */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1129 | unsigned short event = getrpacketevent(&state->rpacket); |
| 1130 | state->event = (tftp_event_t)event; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1131 | |
| 1132 | switch(state->event) { |
| 1133 | case TFTP_EVENT_DATA: |
| 1134 | /* Don't pass to the client empty or retransmitted packets */ |
| 1135 | if(state->rbytes > 4 && |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1136 | (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1137 | result = Curl_client_write(conn, CLIENTWRITE_BODY, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1138 | (char *)state->rpacket.data + 4, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1139 | state->rbytes-4); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1140 | if(result) { |
| 1141 | tftp_state_machine(state, TFTP_EVENT_ERROR); |
| 1142 | return result; |
| 1143 | } |
| 1144 | k->bytecount += state->rbytes-4; |
| 1145 | Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount); |
| 1146 | } |
| 1147 | break; |
| 1148 | case TFTP_EVENT_ERROR: |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1149 | { |
| 1150 | unsigned short error = getrpacketblock(&state->rpacket); |
| 1151 | state->error = (tftp_error_t)error; |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1152 | infof(data, "%s\n", (const char *)state->rpacket.data + 4); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1153 | break; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1154 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1155 | case TFTP_EVENT_ACK: |
| 1156 | break; |
| 1157 | case TFTP_EVENT_OACK: |
| 1158 | result = tftp_parse_option_ack(state, |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1159 | (const char *)state->rpacket.data + 2, |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1160 | state->rbytes-2); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1161 | if(result) |
| 1162 | return result; |
| 1163 | break; |
| 1164 | case TFTP_EVENT_RRQ: |
| 1165 | case TFTP_EVENT_WRQ: |
| 1166 | default: |
| 1167 | failf(data, "%s", "Internal error: Unexpected packet"); |
| 1168 | break; |
| 1169 | } |
| 1170 | |
| 1171 | /* Update the progress meter */ |
| 1172 | if(Curl_pgrsUpdate(conn)) { |
| 1173 | tftp_state_machine(state, TFTP_EVENT_ERROR); |
| 1174 | return CURLE_ABORTED_BY_CALLBACK; |
| 1175 | } |
| 1176 | } |
| 1177 | return result; |
| 1178 | } |
| 1179 | |
| 1180 | /********************************************************** |
| 1181 | * |
| 1182 | * tftp_state_timeout |
| 1183 | * |
| 1184 | * Check if timeouts have been reached |
| 1185 | * |
| 1186 | **********************************************************/ |
| 1187 | static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event) |
| 1188 | { |
| 1189 | time_t current; |
| 1190 | tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; |
| 1191 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1192 | if(event) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1193 | *event = TFTP_EVENT_NONE; |
| 1194 | |
| 1195 | time(¤t); |
| 1196 | if(current > state->max_time) { |
| 1197 | DEBUGF(infof(conn->data, "timeout: %ld > %ld\n", |
| 1198 | (long)current, (long)state->max_time)); |
| 1199 | state->error = TFTP_ERR_TIMEOUT; |
| 1200 | state->state = TFTP_STATE_FIN; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1201 | return 0; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1202 | } |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1203 | if(current > state->rx_time + state->retry_time) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1204 | if(event) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1205 | *event = TFTP_EVENT_TIMEOUT; |
| 1206 | time(&state->rx_time); /* update even though we received nothing */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1207 | } |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1208 | |
| 1209 | /* there's a typecast below here since 'time_t' may in fact be larger than |
| 1210 | 'long', but we estimate that a 'long' will still be able to hold number |
| 1211 | of seconds even if "only" 32 bit */ |
| 1212 | return (long)(state->max_time - current); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1215 | /********************************************************** |
| 1216 | * |
| 1217 | * tftp_multi_statemach |
| 1218 | * |
| 1219 | * Handle single RX socket event and return |
| 1220 | * |
| 1221 | **********************************************************/ |
| 1222 | static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done) |
| 1223 | { |
| 1224 | int rc; |
| 1225 | tftp_event_t event; |
| 1226 | CURLcode result = CURLE_OK; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1227 | struct Curl_easy *data = conn->data; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1228 | tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; |
| 1229 | long timeout_ms = tftp_state_timeout(conn, &event); |
| 1230 | |
| 1231 | *done = FALSE; |
| 1232 | |
| 1233 | if(timeout_ms <= 0) { |
| 1234 | failf(data, "TFTP response timeout"); |
| 1235 | return CURLE_OPERATION_TIMEDOUT; |
| 1236 | } |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1237 | if(event != TFTP_EVENT_NONE) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1238 | result = tftp_state_machine(state, event); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1239 | if(result) |
| 1240 | return result; |
| 1241 | *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1242 | if(*done) |
| 1243 | /* Tell curl we're done */ |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1244 | Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1245 | } |
| 1246 | else { |
| 1247 | /* no timeouts to handle, check our socket */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1248 | rc = SOCKET_READABLE(state->sockfd, 0); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1249 | |
| 1250 | if(rc == -1) { |
| 1251 | /* bail out */ |
| 1252 | int error = SOCKERRNO; |
| 1253 | failf(data, "%s", Curl_strerror(conn, error)); |
| 1254 | state->event = TFTP_EVENT_ERROR; |
| 1255 | } |
| 1256 | else if(rc != 0) { |
| 1257 | result = tftp_receive_packet(conn); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1258 | if(result) |
| 1259 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1260 | result = tftp_state_machine(state, state->event); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1261 | if(result) |
| 1262 | return result; |
| 1263 | *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1264 | if(*done) |
| 1265 | /* Tell curl we're done */ |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 1266 | Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1267 | } |
| 1268 | /* if rc == 0, then select() timed out */ |
| 1269 | } |
| 1270 | |
| 1271 | return result; |
| 1272 | } |
| 1273 | |
| 1274 | /********************************************************** |
| 1275 | * |
| 1276 | * tftp_doing |
| 1277 | * |
| 1278 | * Called from multi.c while DOing |
| 1279 | * |
| 1280 | **********************************************************/ |
| 1281 | static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done) |
| 1282 | { |
| 1283 | CURLcode result; |
| 1284 | result = tftp_multi_statemach(conn, dophase_done); |
| 1285 | |
| 1286 | if(*dophase_done) { |
| 1287 | DEBUGF(infof(conn->data, "DO phase is complete\n")); |
| 1288 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1289 | else if(!result) { |
| 1290 | /* The multi code doesn't have this logic for the DOING state so we |
| 1291 | provide it for TFTP since it may do the entire transfer in this |
| 1292 | state. */ |
| 1293 | if(Curl_pgrsUpdate(conn)) |
| 1294 | result = CURLE_ABORTED_BY_CALLBACK; |
| 1295 | else |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 1296 | result = Curl_speedcheck(conn->data, Curl_now()); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1297 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1298 | return result; |
| 1299 | } |
| 1300 | |
| 1301 | /********************************************************** |
| 1302 | * |
| 1303 | * tftp_peform |
| 1304 | * |
| 1305 | * Entry point for transfer from tftp_do, sarts state mach |
| 1306 | * |
| 1307 | **********************************************************/ |
| 1308 | static CURLcode tftp_perform(struct connectdata *conn, bool *dophase_done) |
| 1309 | { |
| 1310 | CURLcode result = CURLE_OK; |
| 1311 | tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc; |
| 1312 | |
| 1313 | *dophase_done = FALSE; |
| 1314 | |
| 1315 | result = tftp_state_machine(state, TFTP_EVENT_INIT); |
| 1316 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1317 | if((state->state == TFTP_STATE_FIN) || result) |
| 1318 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1319 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1320 | tftp_multi_statemach(conn, dophase_done); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1321 | |
| 1322 | if(*dophase_done) |
| 1323 | DEBUGF(infof(conn->data, "DO phase is complete\n")); |
| 1324 | |
| 1325 | return result; |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | /********************************************************** |
| 1330 | * |
| 1331 | * tftp_do |
| 1332 | * |
| 1333 | * The do callback |
| 1334 | * |
| 1335 | * This callback initiates the TFTP transfer |
| 1336 | * |
| 1337 | **********************************************************/ |
| 1338 | |
| 1339 | static CURLcode tftp_do(struct connectdata *conn, bool *done) |
| 1340 | { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1341 | tftp_state_data_t *state; |
| 1342 | CURLcode result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1343 | |
| 1344 | *done = FALSE; |
| 1345 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1346 | if(!conn->proto.tftpc) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1347 | result = tftp_connect(conn, done); |
| 1348 | if(result) |
| 1349 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1350 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1351 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1352 | state = (tftp_state_data_t *)conn->proto.tftpc; |
| 1353 | if(!state) |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1354 | return CURLE_TFTP_ILLEGAL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1355 | |
| 1356 | result = tftp_perform(conn, done); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1357 | |
| 1358 | /* If tftp_perform() returned an error, use that for return code. If it |
| 1359 | was OK, see if tftp_translate_code() has an error. */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1360 | if(!result) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1361 | /* If we have encountered an internal tftp error, translate it. */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1362 | result = tftp_translate_code(state->error); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1363 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1364 | return result; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | static CURLcode tftp_setup_connection(struct connectdata * conn) |
| 1368 | { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 1369 | struct Curl_easy *data = conn->data; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 1370 | char *type; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1371 | char command; |
| 1372 | |
| 1373 | conn->socktype = SOCK_DGRAM; /* UDP datagram based */ |
| 1374 | |
| 1375 | /* TFTP URLs support an extension like ";mode=<typecode>" that |
| 1376 | * we'll try to get now! */ |
| 1377 | type = strstr(data->state.path, ";mode="); |
| 1378 | |
| 1379 | if(!type) |
| 1380 | type = strstr(conn->host.rawalloc, ";mode="); |
| 1381 | |
| 1382 | if(type) { |
| 1383 | *type = 0; /* it was in the middle of the hostname */ |
| 1384 | command = Curl_raw_toupper(type[6]); |
| 1385 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 1386 | switch(command) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1387 | case 'A': /* ASCII mode */ |
| 1388 | case 'N': /* NETASCII mode */ |
| 1389 | data->set.prefer_ascii = TRUE; |
| 1390 | break; |
| 1391 | |
| 1392 | case 'O': /* octet mode */ |
| 1393 | case 'I': /* binary mode */ |
| 1394 | default: |
| 1395 | /* switch off ASCII */ |
| 1396 | data->set.prefer_ascii = FALSE; |
| 1397 | break; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | return CURLE_OK; |
| 1402 | } |
| 1403 | #endif |