VirtualBox

source: vbox/trunk/src/libs/curl-8.7.1/lib/tftp.c@ 105284

Last change on this file since 105284 was 104083, checked in by vboxsync, 11 months ago

curl-8.7.1: Applied and adjusted our curl changes to 8.4.0. bugref:10639

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette