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