1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifndef CURL_DISABLE_TFTP
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
48 #include <curl/curl.h>
55 #include "sockaddr.h" /* required for Curl_sockaddr_storage */
59 #include "speedcheck.h"
63 /* The last 3 #include files should be in this order */
64 #include "curl_printf.h"
65 #include "curl_memory.h"
68 /* RFC2348 allows the block size to be negotiated */
69 #define TFTP_BLKSIZE_DEFAULT 512
70 #define TFTP_BLKSIZE_MIN 8
71 #define TFTP_BLKSIZE_MAX 65464
72 #define TFTP_OPTION_BLKSIZE "blksize"
75 #define TFTP_OPTION_TSIZE "tsize"
76 #define TFTP_OPTION_INTERVAL "timeout"
79 TFTP_MODE_NETASCII = 0,
110 TFTP_ERR_NOSUCHUSER, /* This will never be triggered by this code */
112 /* The remaining error codes are internal to curl */
113 TFTP_ERR_NONE = -100,
122 struct tftp_state_data {
127 struct connectdata *conn;
128 curl_socket_t sockfd;
135 unsigned short block;
136 struct Curl_sockaddr_storage local_addr;
137 struct Curl_sockaddr_storage remote_addr;
138 curl_socklen_t remote_addrlen;
142 int requested_blksize;
143 struct tftp_packet rpacket;
144 struct tftp_packet spacket;
148 /* Forward declarations */
149 static CURLcode tftp_rx(struct tftp_state_data *state, tftp_event_t event);
150 static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event);
151 static CURLcode tftp_connect(struct connectdata *conn, bool *done);
152 static CURLcode tftp_disconnect(struct connectdata *conn,
153 bool dead_connection);
154 static CURLcode tftp_do(struct connectdata *conn, bool *done);
155 static CURLcode tftp_done(struct connectdata *conn,
156 CURLcode, bool premature);
157 static CURLcode tftp_setup_connection(struct connectdata *conn);
158 static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done);
159 static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done);
160 static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks);
161 static CURLcode tftp_translate_code(tftp_error_t error);
165 * TFTP protocol handler.
168 const struct Curl_handler Curl_handler_tftp = {
170 tftp_setup_connection, /* setup_connection */
172 tftp_done, /* done */
173 ZERO_NULL, /* do_more */
174 tftp_connect, /* connect_it */
175 tftp_multi_statemach, /* connecting */
176 tftp_doing, /* doing */
177 tftp_getsock, /* proto_getsock */
178 tftp_getsock, /* doing_getsock */
179 ZERO_NULL, /* domore_getsock */
180 ZERO_NULL, /* perform_getsock */
181 tftp_disconnect, /* disconnect */
182 ZERO_NULL, /* readwrite */
183 ZERO_NULL, /* connection_check */
184 PORT_TFTP, /* defport */
185 CURLPROTO_TFTP, /* protocol */
186 CURLPROTO_TFTP, /* family */
187 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
190 /**********************************************************
192 * tftp_set_timeouts -
194 * Set timeouts based on state machine state.
195 * Use user provided connect timeouts until DATA or ACK
196 * packet is received, then use user-provided transfer timeouts
199 **********************************************************/
200 static CURLcode tftp_set_timeouts(struct tftp_state_data *state)
202 time_t maxtime, timeout;
203 timediff_t timeout_ms;
204 bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE;
206 time(&state->start_time);
208 /* Compute drop-dead time */
209 timeout_ms = Curl_timeleft(state->conn->data, NULL, start);
212 /* time-out, bail out, go home */
213 failf(state->conn->data, "Connection time-out");
214 return CURLE_OPERATION_TIMEDOUT;
219 maxtime = (time_t)(timeout_ms + 500) / 1000;
220 state->max_time = state->start_time + maxtime;
222 /* Set per-block timeout to total */
225 /* Average restart after 5 seconds */
226 state->retry_max = (int)timeout/5;
228 if(state->retry_max < 1)
229 /* avoid division by zero below */
230 state->retry_max = 1;
232 /* Compute the re-start interval to suit the timeout */
233 state->retry_time = (int)timeout/state->retry_max;
234 if(state->retry_time<1)
235 state->retry_time = 1;
240 maxtime = (time_t)(timeout_ms + 500) / 1000;
244 state->max_time = state->start_time + maxtime;
246 /* Set per-block timeout to total */
249 /* Average reposting an ACK after 5 seconds */
250 state->retry_max = (int)timeout/5;
252 /* But bound the total number */
253 if(state->retry_max<3)
254 state->retry_max = 3;
256 if(state->retry_max>50)
257 state->retry_max = 50;
259 /* Compute the re-ACK interval to suit the timeout */
260 state->retry_time = (int)(timeout/state->retry_max);
261 if(state->retry_time<1)
262 state->retry_time = 1;
264 infof(state->conn->data,
265 "set timeouts for state %d; Total %ld, retry %d maxtry %d\n",
266 (int)state->state, (long)(state->max_time-state->start_time),
267 state->retry_time, state->retry_max);
270 time(&state->rx_time);
275 /**********************************************************
277 * tftp_set_send_first
279 * Event handler for the START state
281 **********************************************************/
283 static void setpacketevent(struct tftp_packet *packet, unsigned short num)
285 packet->data[0] = (unsigned char)(num >> 8);
286 packet->data[1] = (unsigned char)(num & 0xff);
290 static void setpacketblock(struct tftp_packet *packet, unsigned short num)
292 packet->data[2] = (unsigned char)(num >> 8);
293 packet->data[3] = (unsigned char)(num & 0xff);
296 static unsigned short getrpacketevent(const struct tftp_packet *packet)
298 return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
301 static unsigned short getrpacketblock(const struct tftp_packet *packet)
303 return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
306 static size_t Curl_strnlen(const char *string, size_t maxlen)
308 const char *end = memchr(string, '\0', maxlen);
309 return end ? (size_t) (end - string) : maxlen;
312 static const char *tftp_option_get(const char *buf, size_t len,
313 const char **option, const char **value)
317 loc = Curl_strnlen(buf, len);
318 loc++; /* NULL term */
324 loc += Curl_strnlen(buf + loc, len-loc);
325 loc++; /* NULL term */
329 *value = &buf[strlen(*option) + 1];
334 static CURLcode tftp_parse_option_ack(struct tftp_state_data *state,
335 const char *ptr, int len)
337 const char *tmp = ptr;
338 struct Curl_easy *data = state->conn->data;
340 /* if OACK doesn't contain blksize option, the default (512) must be used */
341 state->blksize = TFTP_BLKSIZE_DEFAULT;
343 while(tmp < ptr + len) {
344 const char *option, *value;
346 tmp = tftp_option_get(tmp, ptr + len - tmp, &option, &value);
348 failf(data, "Malformed ACK packet, rejecting");
349 return CURLE_TFTP_ILLEGAL;
352 infof(data, "got option=(%s) value=(%s)\n", option, value);
354 if(checkprefix(option, TFTP_OPTION_BLKSIZE)) {
357 blksize = strtol(value, NULL, 10);
360 failf(data, "invalid blocksize value in OACK packet");
361 return CURLE_TFTP_ILLEGAL;
363 if(blksize > TFTP_BLKSIZE_MAX) {
364 failf(data, "%s (%d)", "blksize is larger than max supported",
366 return CURLE_TFTP_ILLEGAL;
368 else if(blksize < TFTP_BLKSIZE_MIN) {
369 failf(data, "%s (%d)", "blksize is smaller than min supported",
371 return CURLE_TFTP_ILLEGAL;
373 else if(blksize > state->requested_blksize) {
374 /* could realloc pkt buffers here, but the spec doesn't call out
375 * support for the server requesting a bigger blksize than the client
377 failf(data, "%s (%ld)",
378 "server requested blksize larger than allocated", blksize);
379 return CURLE_TFTP_ILLEGAL;
382 state->blksize = (int)blksize;
383 infof(data, "%s (%d) %s (%d)\n", "blksize parsed from OACK",
384 state->blksize, "requested", state->requested_blksize);
386 else if(checkprefix(option, TFTP_OPTION_TSIZE)) {
389 tsize = strtol(value, NULL, 10);
390 infof(data, "%s (%ld)\n", "tsize parsed from OACK", tsize);
392 /* tsize should be ignored on upload: Who cares about the size of the
394 if(!data->set.upload) {
396 failf(data, "invalid tsize -:%s:- value in OACK packet", value);
397 return CURLE_TFTP_ILLEGAL;
399 Curl_pgrsSetDownloadSize(data, tsize);
407 static CURLcode tftp_option_add(struct tftp_state_data *state, size_t *csize,
408 char *buf, const char *option)
410 if(( strlen(option) + *csize + 1) > (size_t)state->blksize)
411 return CURLE_TFTP_ILLEGAL;
413 *csize += strlen(option) + 1;
417 static CURLcode tftp_connect_for_tx(struct tftp_state_data *state,
421 #ifndef CURL_DISABLE_VERBOSE_STRINGS
422 struct Curl_easy *data = state->conn->data;
424 infof(data, "%s\n", "Connected for transmit");
426 state->state = TFTP_STATE_TX;
427 result = tftp_set_timeouts(state);
430 return tftp_tx(state, event);
433 static CURLcode tftp_connect_for_rx(struct tftp_state_data *state,
437 #ifndef CURL_DISABLE_VERBOSE_STRINGS
438 struct Curl_easy *data = state->conn->data;
440 infof(data, "%s\n", "Connected for receive");
442 state->state = TFTP_STATE_RX;
443 result = tftp_set_timeouts(state);
446 return tftp_rx(state, event);
449 static CURLcode tftp_send_first(struct tftp_state_data *state,
454 const char *mode = "octet";
456 struct Curl_easy *data = state->conn->data;
457 CURLcode result = CURLE_OK;
459 /* Set ascii mode if -B flag was used */
460 if(data->set.prefer_ascii)
465 case TFTP_EVENT_INIT: /* Send the first packet out */
466 case TFTP_EVENT_TIMEOUT: /* Resend the first packet out */
467 /* Increment the retry counter, quit if over the limit */
469 if(state->retries>state->retry_max) {
470 state->error = TFTP_ERR_NORESPONSE;
471 state->state = TFTP_STATE_FIN;
475 if(data->set.upload) {
476 /* If we are uploading, send an WRQ */
477 setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
478 state->conn->data->req.upload_fromhere =
479 (char *)state->spacket.data + 4;
480 if(data->state.infilesize != -1)
481 Curl_pgrsSetUploadSize(data, data->state.infilesize);
484 /* If we are downloading, send an RRQ */
485 setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
487 /* As RFC3617 describes the separator slash is not actually part of the
488 file name so we skip the always-present first letter of the path
490 result = Curl_urldecode(data, &state->conn->data->state.up.path[1], 0,
491 &filename, NULL, REJECT_ZERO);
495 if(strlen(filename) > (state->blksize - strlen(mode) - 4)) {
496 failf(data, "TFTP file name too long\n");
498 return CURLE_TFTP_ILLEGAL; /* too long file name field */
501 msnprintf((char *)state->spacket.data + 2,
503 "%s%c%s%c", filename, '\0', mode, '\0');
504 sbytes = 4 + strlen(filename) + strlen(mode);
506 /* optional addition of TFTP options */
507 if(!data->set.tftp_no_options) {
509 /* add tsize option */
510 if(data->set.upload && (data->state.infilesize != -1))
511 msnprintf(buf, sizeof(buf), "%" CURL_FORMAT_CURL_OFF_T,
512 data->state.infilesize);
514 strcpy(buf, "0"); /* the destination is large enough */
516 result = tftp_option_add(state, &sbytes,
517 (char *)state->spacket.data + sbytes,
519 if(result == CURLE_OK)
520 result = tftp_option_add(state, &sbytes,
521 (char *)state->spacket.data + sbytes, buf);
523 /* add blksize option */
524 msnprintf(buf, sizeof(buf), "%d", state->requested_blksize);
525 if(result == CURLE_OK)
526 result = tftp_option_add(state, &sbytes,
527 (char *)state->spacket.data + sbytes,
528 TFTP_OPTION_BLKSIZE);
529 if(result == CURLE_OK)
530 result = tftp_option_add(state, &sbytes,
531 (char *)state->spacket.data + sbytes, buf);
533 /* add timeout option */
534 msnprintf(buf, sizeof(buf), "%d", state->retry_time);
535 if(result == CURLE_OK)
536 result = tftp_option_add(state, &sbytes,
537 (char *)state->spacket.data + sbytes,
538 TFTP_OPTION_INTERVAL);
539 if(result == CURLE_OK)
540 result = tftp_option_add(state, &sbytes,
541 (char *)state->spacket.data + sbytes, buf);
543 if(result != CURLE_OK) {
544 failf(data, "TFTP buffer too small for options");
546 return CURLE_TFTP_ILLEGAL;
550 /* the typecase for the 3rd argument is mostly for systems that do
551 not have a size_t argument, like older unixes that want an 'int' */
552 senddata = sendto(state->sockfd, (void *)state->spacket.data,
553 (SEND_TYPE_ARG3)sbytes, 0,
554 state->conn->ip_addr->ai_addr,
555 state->conn->ip_addr->ai_addrlen);
556 if(senddata != (ssize_t)sbytes) {
557 char buffer[STRERROR_LEN];
558 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
563 case TFTP_EVENT_OACK:
564 if(data->set.upload) {
565 result = tftp_connect_for_tx(state, event);
568 result = tftp_connect_for_rx(state, event);
572 case TFTP_EVENT_ACK: /* Connected for transmit */
573 result = tftp_connect_for_tx(state, event);
576 case TFTP_EVENT_DATA: /* Connected for receive */
577 result = tftp_connect_for_rx(state, event);
580 case TFTP_EVENT_ERROR:
581 state->state = TFTP_STATE_FIN;
585 failf(state->conn->data, "tftp_send_first: internal error");
592 /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
594 #define NEXT_BLOCKNUM(x) (((x) + 1)&0xffff)
596 /**********************************************************
600 * Event handler for the RX state
602 **********************************************************/
603 static CURLcode tftp_rx(struct tftp_state_data *state,
608 struct Curl_easy *data = state->conn->data;
609 char buffer[STRERROR_LEN];
613 case TFTP_EVENT_DATA:
614 /* Is this the block we expect? */
615 rblock = getrpacketblock(&state->rpacket);
616 if(NEXT_BLOCKNUM(state->block) == rblock) {
617 /* This is the expected block. Reset counters and ACK it. */
620 else if(state->block == rblock) {
621 /* This is the last recently received block again. Log it and ACK it
623 infof(data, "Received last DATA packet block %d again.\n", rblock);
626 /* totally unexpected, just log it */
628 "Received unexpected DATA packet block %d, expecting block %d\n",
629 rblock, NEXT_BLOCKNUM(state->block));
633 /* ACK this block. */
634 state->block = (unsigned short)rblock;
635 setpacketevent(&state->spacket, TFTP_EVENT_ACK);
636 setpacketblock(&state->spacket, state->block);
637 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
639 (struct sockaddr *)&state->remote_addr,
640 state->remote_addrlen);
642 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
643 return CURLE_SEND_ERROR;
646 /* Check if completed (That is, a less than full packet is received) */
647 if(state->rbytes < (ssize_t)state->blksize + 4) {
648 state->state = TFTP_STATE_FIN;
651 state->state = TFTP_STATE_RX;
653 time(&state->rx_time);
656 case TFTP_EVENT_OACK:
657 /* ACK option acknowledgement so we can move on to data */
660 setpacketevent(&state->spacket, TFTP_EVENT_ACK);
661 setpacketblock(&state->spacket, state->block);
662 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
664 (struct sockaddr *)&state->remote_addr,
665 state->remote_addrlen);
667 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
668 return CURLE_SEND_ERROR;
671 /* we're ready to RX data */
672 state->state = TFTP_STATE_RX;
673 time(&state->rx_time);
676 case TFTP_EVENT_TIMEOUT:
677 /* Increment the retry count and fail if over the limit */
680 "Timeout waiting for block %d ACK. Retries = %d\n",
681 NEXT_BLOCKNUM(state->block), state->retries);
682 if(state->retries > state->retry_max) {
683 state->error = TFTP_ERR_TIMEOUT;
684 state->state = TFTP_STATE_FIN;
687 /* Resend the previous ACK */
688 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
690 (struct sockaddr *)&state->remote_addr,
691 state->remote_addrlen);
693 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
694 return CURLE_SEND_ERROR;
699 case TFTP_EVENT_ERROR:
700 setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
701 setpacketblock(&state->spacket, state->block);
702 (void)sendto(state->sockfd, (void *)state->spacket.data,
704 (struct sockaddr *)&state->remote_addr,
705 state->remote_addrlen);
706 /* don't bother with the return code, but if the socket is still up we
707 * should be a good TFTP client and let the server know we're done */
708 state->state = TFTP_STATE_FIN;
712 failf(data, "%s", "tftp_rx: internal error");
713 return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
719 /**********************************************************
723 * Event handler for the TX state
725 **********************************************************/
726 static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event)
728 struct Curl_easy *data = state->conn->data;
730 CURLcode result = CURLE_OK;
731 struct SingleRequest *k = &data->req;
732 size_t cb; /* Bytes currently read */
733 char buffer[STRERROR_LEN];
738 case TFTP_EVENT_OACK:
739 if(event == TFTP_EVENT_ACK) {
741 int rblock = getrpacketblock(&state->rpacket);
743 if(rblock != state->block &&
744 /* There's a bug in tftpd-hpa that causes it to send us an ack for
745 * 65535 when the block number wraps to 0. So when we're expecting
746 * 0, also accept 65535. See
747 * http://syslinux.zytor.com/archives/2010-September/015253.html
749 !(state->block == 0 && rblock == 65535)) {
750 /* This isn't the expected block. Log it and up the retry counter */
751 infof(data, "Received ACK for block %d, expecting %d\n",
752 rblock, state->block);
754 /* Bail out if over the maximum */
755 if(state->retries>state->retry_max) {
756 failf(data, "tftp_tx: giving up waiting for block %d ack",
758 result = CURLE_SEND_ERROR;
761 /* Re-send the data packet */
762 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
763 4 + state->sbytes, SEND_4TH_ARG,
764 (struct sockaddr *)&state->remote_addr,
765 state->remote_addrlen);
766 /* Check all sbytes were sent */
768 failf(data, "%s", Curl_strerror(SOCKERRNO,
769 buffer, sizeof(buffer)));
770 result = CURLE_SEND_ERROR;
776 /* This is the expected packet. Reset the counters and send the next
778 time(&state->rx_time);
782 state->block = 1; /* first data block is 1 when using OACK */
785 setpacketevent(&state->spacket, TFTP_EVENT_DATA);
786 setpacketblock(&state->spacket, state->block);
787 if(state->block > 1 && state->sbytes < state->blksize) {
788 state->state = TFTP_STATE_FIN;
792 /* TFTP considers data block size < 512 bytes as an end of session. So
793 * in some cases we must wait for additional data to build full (512 bytes)
797 state->conn->data->req.upload_fromhere = (char *)state->spacket.data + 4;
799 result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes,
803 state->sbytes += (int)cb;
804 state->conn->data->req.upload_fromhere += cb;
805 } while(state->sbytes < state->blksize && cb != 0);
807 sbytes = sendto(state->sockfd, (void *) state->spacket.data,
808 4 + state->sbytes, SEND_4TH_ARG,
809 (struct sockaddr *)&state->remote_addr,
810 state->remote_addrlen);
811 /* Check all sbytes were sent */
813 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
814 return CURLE_SEND_ERROR;
816 /* Update the progress meter */
817 k->writebytecount += state->sbytes;
818 Curl_pgrsSetUploadCounter(data, k->writebytecount);
821 case TFTP_EVENT_TIMEOUT:
822 /* Increment the retry counter and log the timeout */
824 infof(data, "Timeout waiting for block %d ACK. "
825 " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries);
826 /* Decide if we've had enough */
827 if(state->retries > state->retry_max) {
828 state->error = TFTP_ERR_TIMEOUT;
829 state->state = TFTP_STATE_FIN;
832 /* Re-send the data packet */
833 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
834 4 + state->sbytes, SEND_4TH_ARG,
835 (struct sockaddr *)&state->remote_addr,
836 state->remote_addrlen);
837 /* Check all sbytes were sent */
839 failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
840 return CURLE_SEND_ERROR;
842 /* since this was a re-send, we remain at the still byte position */
843 Curl_pgrsSetUploadCounter(data, k->writebytecount);
847 case TFTP_EVENT_ERROR:
848 state->state = TFTP_STATE_FIN;
849 setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
850 setpacketblock(&state->spacket, state->block);
851 (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
852 (struct sockaddr *)&state->remote_addr,
853 state->remote_addrlen);
854 /* don't bother with the return code, but if the socket is still up we
855 * should be a good TFTP client and let the server know we're done */
856 state->state = TFTP_STATE_FIN;
860 failf(data, "tftp_tx: internal error, event: %i", (int)(event));
867 /**********************************************************
869 * tftp_translate_code
871 * Translate internal error codes to CURL error codes
873 **********************************************************/
874 static CURLcode tftp_translate_code(tftp_error_t error)
876 CURLcode result = CURLE_OK;
878 if(error != TFTP_ERR_NONE) {
880 case TFTP_ERR_NOTFOUND:
881 result = CURLE_TFTP_NOTFOUND;
884 result = CURLE_TFTP_PERM;
886 case TFTP_ERR_DISKFULL:
887 result = CURLE_REMOTE_DISK_FULL;
890 case TFTP_ERR_ILLEGAL:
891 result = CURLE_TFTP_ILLEGAL;
893 case TFTP_ERR_UNKNOWNID:
894 result = CURLE_TFTP_UNKNOWNID;
896 case TFTP_ERR_EXISTS:
897 result = CURLE_REMOTE_FILE_EXISTS;
899 case TFTP_ERR_NOSUCHUSER:
900 result = CURLE_TFTP_NOSUCHUSER;
902 case TFTP_ERR_TIMEOUT:
903 result = CURLE_OPERATION_TIMEDOUT;
905 case TFTP_ERR_NORESPONSE:
906 result = CURLE_COULDNT_CONNECT;
909 result = CURLE_ABORTED_BY_CALLBACK;
919 /**********************************************************
923 * The tftp state machine event dispatcher
925 **********************************************************/
926 static CURLcode tftp_state_machine(struct tftp_state_data *state,
929 CURLcode result = CURLE_OK;
930 struct Curl_easy *data = state->conn->data;
932 switch(state->state) {
933 case TFTP_STATE_START:
934 DEBUGF(infof(data, "TFTP_STATE_START\n"));
935 result = tftp_send_first(state, event);
938 DEBUGF(infof(data, "TFTP_STATE_RX\n"));
939 result = tftp_rx(state, event);
942 DEBUGF(infof(data, "TFTP_STATE_TX\n"));
943 result = tftp_tx(state, event);
946 infof(data, "%s\n", "TFTP finished");
949 DEBUGF(infof(data, "STATE: %d\n", state->state));
950 failf(data, "%s", "Internal state machine error");
951 result = CURLE_TFTP_ILLEGAL;
958 /**********************************************************
962 * The disconnect callback
964 **********************************************************/
965 static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection)
967 struct tftp_state_data *state = conn->proto.tftpc;
968 (void) dead_connection;
970 /* done, free dynamically allocated pkt buffers */
972 Curl_safefree(state->rpacket.data);
973 Curl_safefree(state->spacket.data);
980 /**********************************************************
984 * The connect callback
986 **********************************************************/
987 static CURLcode tftp_connect(struct connectdata *conn, bool *done)
989 struct tftp_state_data *state;
993 blksize = TFTP_BLKSIZE_DEFAULT;
995 state = conn->proto.tftpc = calloc(1, sizeof(struct tftp_state_data));
997 return CURLE_OUT_OF_MEMORY;
999 /* alloc pkt buffers based on specified blksize */
1000 if(conn->data->set.tftp_blksize) {
1001 blksize = (int)conn->data->set.tftp_blksize;
1002 if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN)
1003 return CURLE_TFTP_ILLEGAL;
1006 need_blksize = blksize;
1007 /* default size is the fallback when no OACK is received */
1008 if(need_blksize < TFTP_BLKSIZE_DEFAULT)
1009 need_blksize = TFTP_BLKSIZE_DEFAULT;
1011 if(!state->rpacket.data) {
1012 state->rpacket.data = calloc(1, need_blksize + 2 + 2);
1014 if(!state->rpacket.data)
1015 return CURLE_OUT_OF_MEMORY;
1018 if(!state->spacket.data) {
1019 state->spacket.data = calloc(1, need_blksize + 2 + 2);
1021 if(!state->spacket.data)
1022 return CURLE_OUT_OF_MEMORY;
1025 /* we don't keep TFTP connections up basically because there's none or very
1026 * little gain for UDP */
1027 connclose(conn, "TFTP");
1030 state->sockfd = state->conn->sock[FIRSTSOCKET];
1031 state->state = TFTP_STATE_START;
1032 state->error = TFTP_ERR_NONE;
1033 state->blksize = TFTP_BLKSIZE_DEFAULT; /* Unless updated by OACK response */
1034 state->requested_blksize = blksize;
1036 ((struct sockaddr *)&state->local_addr)->sa_family =
1037 (CURL_SA_FAMILY_T)(conn->ip_addr->ai_family);
1039 tftp_set_timeouts(state);
1041 if(!conn->bits.bound) {
1042 /* If not already bound, bind to any interface, random UDP port. If it is
1043 * reused or a custom local port was desired, this has already been done!
1045 * We once used the size of the local_addr struct as the third argument
1046 * for bind() to better work with IPv6 or whatever size the struct could
1047 * have, but we learned that at least Tru64, AIX and IRIX *requires* the
1048 * size of that argument to match the exact size of a 'sockaddr_in' struct
1049 * when running IPv4-only.
1051 * Therefore we use the size from the address we connected to, which we
1052 * assume uses the same IP version and thus hopefully this works for both
1055 int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
1056 conn->ip_addr->ai_addrlen);
1058 char buffer[STRERROR_LEN];
1059 failf(conn->data, "bind() failed; %s",
1060 Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
1061 return CURLE_COULDNT_CONNECT;
1063 conn->bits.bound = TRUE;
1066 Curl_pgrsStartNow(conn->data);
1073 /**********************************************************
1079 **********************************************************/
1080 static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
1083 CURLcode result = CURLE_OK;
1084 struct tftp_state_data *state = conn->proto.tftpc;
1086 (void)status; /* unused */
1087 (void)premature; /* not used */
1089 if(Curl_pgrsDone(conn))
1090 return CURLE_ABORTED_BY_CALLBACK;
1092 /* If we have encountered an error */
1094 result = tftp_translate_code(state->error);
1099 /**********************************************************
1103 * The getsock callback
1105 **********************************************************/
1106 static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks)
1108 socks[0] = conn->sock[FIRSTSOCKET];
1109 return GETSOCK_READSOCK(0);
1112 /**********************************************************
1114 * tftp_receive_packet
1116 * Called once select fires and data is ready on the socket
1118 **********************************************************/
1119 static CURLcode tftp_receive_packet(struct connectdata *conn)
1121 struct Curl_sockaddr_storage fromaddr;
1122 curl_socklen_t fromlen;
1123 CURLcode result = CURLE_OK;
1124 struct Curl_easy *data = conn->data;
1125 struct tftp_state_data *state = conn->proto.tftpc;
1126 struct SingleRequest *k = &data->req;
1128 /* Receive the packet */
1129 fromlen = sizeof(fromaddr);
1130 state->rbytes = (int)recvfrom(state->sockfd,
1131 (void *)state->rpacket.data,
1134 (struct sockaddr *)&fromaddr,
1136 if(state->remote_addrlen == 0) {
1137 memcpy(&state->remote_addr, &fromaddr, fromlen);
1138 state->remote_addrlen = fromlen;
1141 /* Sanity check packet length */
1142 if(state->rbytes < 4) {
1143 failf(data, "Received too short packet");
1144 /* Not a timeout, but how best to handle it? */
1145 state->event = TFTP_EVENT_TIMEOUT;
1148 /* The event is given by the TFTP packet time */
1149 unsigned short event = getrpacketevent(&state->rpacket);
1150 state->event = (tftp_event_t)event;
1152 switch(state->event) {
1153 case TFTP_EVENT_DATA:
1154 /* Don't pass to the client empty or retransmitted packets */
1155 if(state->rbytes > 4 &&
1156 (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
1157 result = Curl_client_write(conn, CLIENTWRITE_BODY,
1158 (char *)state->rpacket.data + 4,
1161 tftp_state_machine(state, TFTP_EVENT_ERROR);
1164 k->bytecount += state->rbytes-4;
1165 Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
1168 case TFTP_EVENT_ERROR:
1170 unsigned short error = getrpacketblock(&state->rpacket);
1171 char *str = (char *)state->rpacket.data + 4;
1172 size_t strn = state->rbytes - 4;
1173 state->error = (tftp_error_t)error;
1174 if(Curl_strnlen(str, strn) < strn)
1175 infof(data, "TFTP error: %s\n", str);
1178 case TFTP_EVENT_ACK:
1180 case TFTP_EVENT_OACK:
1181 result = tftp_parse_option_ack(state,
1182 (const char *)state->rpacket.data + 2,
1187 case TFTP_EVENT_RRQ:
1188 case TFTP_EVENT_WRQ:
1190 failf(data, "%s", "Internal error: Unexpected packet");
1194 /* Update the progress meter */
1195 if(Curl_pgrsUpdate(conn)) {
1196 tftp_state_machine(state, TFTP_EVENT_ERROR);
1197 return CURLE_ABORTED_BY_CALLBACK;
1203 /**********************************************************
1205 * tftp_state_timeout
1207 * Check if timeouts have been reached
1209 **********************************************************/
1210 static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event)
1213 struct tftp_state_data *state = conn->proto.tftpc;
1216 *event = TFTP_EVENT_NONE;
1219 if(current > state->max_time) {
1220 DEBUGF(infof(conn->data, "timeout: %ld > %ld\n",
1221 (long)current, (long)state->max_time));
1222 state->error = TFTP_ERR_TIMEOUT;
1223 state->state = TFTP_STATE_FIN;
1226 if(current > state->rx_time + state->retry_time) {
1228 *event = TFTP_EVENT_TIMEOUT;
1229 time(&state->rx_time); /* update even though we received nothing */
1232 /* there's a typecast below here since 'time_t' may in fact be larger than
1233 'long', but we estimate that a 'long' will still be able to hold number
1234 of seconds even if "only" 32 bit */
1235 return (long)(state->max_time - current);
1238 /**********************************************************
1240 * tftp_multi_statemach
1242 * Handle single RX socket event and return
1244 **********************************************************/
1245 static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
1248 CURLcode result = CURLE_OK;
1249 struct Curl_easy *data = conn->data;
1250 struct tftp_state_data *state = conn->proto.tftpc;
1251 long timeout_ms = tftp_state_timeout(conn, &event);
1255 if(timeout_ms <= 0) {
1256 failf(data, "TFTP response timeout");
1257 return CURLE_OPERATION_TIMEDOUT;
1259 if(event != TFTP_EVENT_NONE) {
1260 result = tftp_state_machine(state, event);
1263 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
1265 /* Tell curl we're done */
1266 Curl_setup_transfer(data, -1, -1, FALSE, -1);
1269 /* no timeouts to handle, check our socket */
1270 int rc = SOCKET_READABLE(state->sockfd, 0);
1274 int error = SOCKERRNO;
1275 char buffer[STRERROR_LEN];
1276 failf(data, "%s", Curl_strerror(error, buffer, sizeof(buffer)));
1277 state->event = TFTP_EVENT_ERROR;
1280 result = tftp_receive_packet(conn);
1283 result = tftp_state_machine(state, state->event);
1286 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
1288 /* Tell curl we're done */
1289 Curl_setup_transfer(data, -1, -1, FALSE, -1);
1291 /* if rc == 0, then select() timed out */
1297 /**********************************************************
1301 * Called from multi.c while DOing
1303 **********************************************************/
1304 static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done)
1307 result = tftp_multi_statemach(conn, dophase_done);
1310 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1313 /* The multi code doesn't have this logic for the DOING state so we
1314 provide it for TFTP since it may do the entire transfer in this
1316 if(Curl_pgrsUpdate(conn))
1317 result = CURLE_ABORTED_BY_CALLBACK;
1319 result = Curl_speedcheck(conn->data, Curl_now());
1324 /**********************************************************
1328 * Entry point for transfer from tftp_do, sarts state mach
1330 **********************************************************/
1331 static CURLcode tftp_perform(struct connectdata *conn, bool *dophase_done)
1333 CURLcode result = CURLE_OK;
1334 struct tftp_state_data *state = conn->proto.tftpc;
1336 *dophase_done = FALSE;
1338 result = tftp_state_machine(state, TFTP_EVENT_INIT);
1340 if((state->state == TFTP_STATE_FIN) || result)
1343 tftp_multi_statemach(conn, dophase_done);
1346 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1352 /**********************************************************
1358 * This callback initiates the TFTP transfer
1360 **********************************************************/
1362 static CURLcode tftp_do(struct connectdata *conn, bool *done)
1364 struct tftp_state_data *state;
1369 if(!conn->proto.tftpc) {
1370 result = tftp_connect(conn, done);
1375 state = conn->proto.tftpc;
1377 return CURLE_TFTP_ILLEGAL;
1379 result = tftp_perform(conn, done);
1381 /* If tftp_perform() returned an error, use that for return code. If it
1382 was OK, see if tftp_translate_code() has an error. */
1384 /* If we have encountered an internal tftp error, translate it. */
1385 result = tftp_translate_code(state->error);
1390 static CURLcode tftp_setup_connection(struct connectdata *conn)
1392 struct Curl_easy *data = conn->data;
1395 conn->transport = TRNSPRT_UDP;
1397 /* TFTP URLs support an extension like ";mode=<typecode>" that
1398 * we'll try to get now! */
1399 type = strstr(data->state.up.path, ";mode=");
1402 type = strstr(conn->host.rawalloc, ";mode=");
1406 *type = 0; /* it was in the middle of the hostname */
1407 command = Curl_raw_toupper(type[6]);
1410 case 'A': /* ASCII mode */
1411 case 'N': /* NETASCII mode */
1412 data->set.prefer_ascii = TRUE;
1415 case 'O': /* octet mode */
1416 case 'I': /* binary mode */
1418 /* switch off ASCII */
1419 data->set.prefer_ascii = FALSE;