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