1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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"
61 #define _MPRINTF_REPLACE /* use our functions only */
62 #include <curl/mprintf.h>
64 #include "curl_memory.h"
67 /* The last #include file should be: */
70 /* RFC2348 allows the block size to be negotiated */
71 #define TFTP_BLKSIZE_DEFAULT 512
72 #define TFTP_BLKSIZE_MIN 8
73 #define TFTP_BLKSIZE_MAX 65464
74 #define TFTP_OPTION_BLKSIZE "blksize"
77 #define TFTP_OPTION_TSIZE "tsize"
78 #define TFTP_OPTION_INTERVAL "timeout"
112 TFTP_ERR_NOSUCHUSER, /* This will never be triggered by this code */
114 /* The remaining error codes are internal to curl */
115 TFTP_ERR_NONE = -100,
120 typedef struct tftp_packet {
124 typedef struct tftp_state_data {
129 struct connectdata *conn;
130 curl_socket_t sockfd;
137 unsigned short block;
138 struct Curl_sockaddr_storage local_addr;
139 struct Curl_sockaddr_storage remote_addr;
140 curl_socklen_t remote_addrlen;
144 int requested_blksize;
145 tftp_packet_t rpacket;
146 tftp_packet_t spacket;
150 /* Forward declarations */
151 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) ;
152 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) ;
153 static CURLcode tftp_connect(struct connectdata *conn, bool *done);
154 static CURLcode tftp_disconnect(struct connectdata *conn,
155 bool dead_connection);
156 static CURLcode tftp_do(struct connectdata *conn, bool *done);
157 static CURLcode tftp_done(struct connectdata *conn,
158 CURLcode, bool premature);
159 static CURLcode tftp_setup_connection(struct connectdata * conn);
160 static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done);
161 static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done);
162 static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks,
164 static CURLcode tftp_translate_code(tftp_error_t error);
168 * TFTP protocol handler.
171 const struct Curl_handler Curl_handler_tftp = {
173 tftp_setup_connection, /* setup_connection */
175 tftp_done, /* done */
176 ZERO_NULL, /* do_more */
177 tftp_connect, /* connect_it */
178 tftp_multi_statemach, /* connecting */
179 tftp_doing, /* doing */
180 tftp_getsock, /* proto_getsock */
181 tftp_getsock, /* doing_getsock */
182 ZERO_NULL, /* domore_getsock */
183 ZERO_NULL, /* perform_getsock */
184 tftp_disconnect, /* disconnect */
185 ZERO_NULL, /* readwrite */
186 PORT_TFTP, /* defport */
187 CURLPROTO_TFTP, /* protocol */
188 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
191 /**********************************************************
193 * tftp_set_timeouts -
195 * Set timeouts based on state machine state.
196 * Use user provided connect timeouts until DATA or ACK
197 * packet is received, then use user-provided transfer timeouts
200 **********************************************************/
201 static CURLcode tftp_set_timeouts(tftp_state_data_t *state)
203 time_t maxtime, timeout;
205 bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE;
207 time(&state->start_time);
209 /* Compute drop-dead time */
210 timeout_ms = Curl_timeleft(state->conn->data, NULL, start);
213 /* time-out, bail out, go home */
214 failf(state->conn->data, "Connection time-out");
215 return CURLE_OPERATION_TIMEDOUT;
220 maxtime = (time_t)(timeout_ms + 500) / 1000;
221 state->max_time = state->start_time+maxtime;
223 /* Set per-block timeout to total */
226 /* Average restart after 5 seconds */
227 state->retry_max = (int)timeout/5;
229 if(state->retry_max < 1)
230 /* avoid division by zero below */
231 state->retry_max = 1;
233 /* Compute the re-start interval to suit the timeout */
234 state->retry_time = (int)timeout/state->retry_max;
235 if(state->retry_time<1)
241 maxtime = (time_t)(timeout_ms + 500) / 1000;
245 state->max_time = state->start_time+maxtime;
247 /* Set per-block timeout to total */
250 /* Average reposting an ACK after 5 seconds */
251 state->retry_max = (int)timeout/5;
253 /* But bound the total number */
254 if(state->retry_max<3)
257 if(state->retry_max>50)
260 /* Compute the re-ACK interval to suit the timeout */
261 state->retry_time = (int)(timeout/state->retry_max);
262 if(state->retry_time<1)
265 infof(state->conn->data,
266 "set timeouts for state %d; Total %ld, retry %d maxtry %d\n",
267 (int)state->state, (long)(state->max_time-state->start_time),
268 state->retry_time, state->retry_max);
271 time(&state->rx_time);
276 /**********************************************************
278 * tftp_set_send_first
280 * Event handler for the START state
282 **********************************************************/
284 static void setpacketevent(tftp_packet_t *packet, unsigned short num)
286 packet->data[0] = (unsigned char)(num >> 8);
287 packet->data[1] = (unsigned char)(num & 0xff);
291 static void setpacketblock(tftp_packet_t *packet, unsigned short num)
293 packet->data[2] = (unsigned char)(num >> 8);
294 packet->data[3] = (unsigned char)(num & 0xff);
297 static unsigned short getrpacketevent(const tftp_packet_t *packet)
299 return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
302 static unsigned short getrpacketblock(const tftp_packet_t *packet)
304 return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
307 static size_t Curl_strnlen(const char *string, size_t maxlen)
309 const char *end = memchr (string, '\0', maxlen);
310 return end ? (size_t) (end - string) : maxlen;
313 static const char *tftp_option_get(const char *buf, size_t len,
314 const char **option, const char **value)
318 loc = Curl_strnlen( buf, len );
319 loc++; /* NULL term */
325 loc += Curl_strnlen( buf+loc, len-loc );
326 loc++; /* NULL term */
330 *value = &buf[strlen(*option) + 1];
335 static CURLcode tftp_parse_option_ack(tftp_state_data_t *state,
336 const char *ptr, int len)
338 const char *tmp = ptr;
339 struct SessionHandle *data = state->conn->data;
341 /* if OACK doesn't contain blksize option, the default (512) must be used */
342 state->blksize = TFTP_BLKSIZE_DEFAULT;
344 while(tmp < ptr + len) {
345 const char *option, *value;
347 tmp = tftp_option_get(tmp, ptr + len - tmp, &option, &value);
349 failf(data, "Malformed ACK packet, rejecting");
350 return CURLE_TFTP_ILLEGAL;
353 infof(data, "got option=(%s) value=(%s)\n", option, value);
355 if(checkprefix(option, TFTP_OPTION_BLKSIZE)) {
358 blksize = strtol( value, NULL, 10 );
361 failf(data, "invalid blocksize value in OACK packet");
362 return CURLE_TFTP_ILLEGAL;
364 else if(blksize > TFTP_BLKSIZE_MAX) {
365 failf(data, "%s (%d)", "blksize is larger than max supported",
367 return CURLE_TFTP_ILLEGAL;
369 else if(blksize < TFTP_BLKSIZE_MIN) {
370 failf(data, "%s (%d)", "blksize is smaller than min supported",
372 return CURLE_TFTP_ILLEGAL;
374 else if(blksize > state->requested_blksize) {
375 /* could realloc pkt buffers here, but the spec doesn't call out
376 * support for the server requesting a bigger blksize than the client
378 failf(data, "%s (%ld)",
379 "server requested blksize larger than allocated", blksize);
380 return CURLE_TFTP_ILLEGAL;
383 state->blksize = (int)blksize;
384 infof(data, "%s (%d) %s (%d)\n", "blksize parsed from OACK",
385 state->blksize, "requested", state->requested_blksize);
387 else if(checkprefix(option, TFTP_OPTION_TSIZE)) {
390 tsize = strtol( value, NULL, 10 );
391 infof(data, "%s (%ld)\n", "tsize parsed from OACK", tsize);
393 /* tsize should be ignored on upload: Who cares about the size of the
395 if(!data->set.upload) {
397 failf(data, "invalid tsize -:%s:- value in OACK packet", value);
398 return CURLE_TFTP_ILLEGAL;
400 Curl_pgrsSetDownloadSize(data, tsize);
408 static size_t tftp_option_add(tftp_state_data_t *state, size_t csize,
409 char *buf, const char *option)
411 if(( strlen(option) + csize + 1 ) > (size_t)state->blksize)
414 return( strlen(option) + 1 );
417 static CURLcode tftp_connect_for_tx(tftp_state_data_t *state,
421 #ifndef CURL_DISABLE_VERBOSE_STRINGS
422 struct SessionHandle *data = state->conn->data;
424 infof(data, "%s\n", "Connected for transmit");
426 state->state = TFTP_STATE_TX;
427 res = tftp_set_timeouts(state);
430 return tftp_tx(state, event);
433 static CURLcode tftp_connect_for_rx(tftp_state_data_t *state,
437 #ifndef CURL_DISABLE_VERBOSE_STRINGS
438 struct SessionHandle *data = state->conn->data;
440 infof(data, "%s\n", "Connected for receive");
442 state->state = TFTP_STATE_RX;
443 res = tftp_set_timeouts(state);
446 return tftp_rx(state, event);
449 static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
453 const char *mode = "octet";
456 struct SessionHandle *data = state->conn->data;
457 CURLcode res = 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 filename = curl_easy_unescape(data, &state->conn->data->state.path[1], 0,
493 return CURLE_OUT_OF_MEMORY;
495 snprintf((char *)state->spacket.data+2,
497 "%s%c%s%c", filename, '\0', mode, '\0');
498 sbytes = 4 + strlen(filename) + strlen(mode);
500 /* add tsize option */
501 if(data->set.upload && (data->state.infilesize != -1))
502 snprintf(buf, sizeof(buf), "%" CURL_FORMAT_CURL_OFF_T,
503 data->state.infilesize);
505 strcpy(buf, "0"); /* the destination is large enough */
507 sbytes += tftp_option_add(state, sbytes,
508 (char *)state->spacket.data+sbytes,
510 sbytes += tftp_option_add(state, sbytes,
511 (char *)state->spacket.data+sbytes, buf);
512 /* add blksize option */
513 snprintf( buf, sizeof(buf), "%d", state->requested_blksize );
514 sbytes += tftp_option_add(state, sbytes,
515 (char *)state->spacket.data+sbytes,
516 TFTP_OPTION_BLKSIZE);
517 sbytes += tftp_option_add(state, sbytes,
518 (char *)state->spacket.data+sbytes, buf );
520 /* add timeout option */
521 snprintf( buf, sizeof(buf), "%d", state->retry_time);
522 sbytes += tftp_option_add(state, sbytes,
523 (char *)state->spacket.data+sbytes,
524 TFTP_OPTION_INTERVAL);
525 sbytes += tftp_option_add(state, sbytes,
526 (char *)state->spacket.data+sbytes, buf );
528 /* the typecase for the 3rd argument is mostly for systems that do
529 not have a size_t argument, like older unixes that want an 'int' */
530 senddata = sendto(state->sockfd, (void *)state->spacket.data,
531 (SEND_TYPE_ARG3)sbytes, 0,
532 state->conn->ip_addr->ai_addr,
533 state->conn->ip_addr->ai_addrlen);
534 if(senddata != (ssize_t)sbytes) {
535 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
537 Curl_safefree(filename);
540 case TFTP_EVENT_OACK:
541 if(data->set.upload) {
542 res = tftp_connect_for_tx(state, event);
545 res = tftp_connect_for_rx(state, event);
549 case TFTP_EVENT_ACK: /* Connected for transmit */
550 res = tftp_connect_for_tx(state, event);
553 case TFTP_EVENT_DATA: /* Connected for receive */
554 res = tftp_connect_for_rx(state, event);
557 case TFTP_EVENT_ERROR:
558 state->state = TFTP_STATE_FIN;
562 failf(state->conn->data, "tftp_send_first: internal error");
568 /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
570 #define NEXT_BLOCKNUM(x) (((x)+1)&0xffff)
572 /**********************************************************
576 * Event handler for the RX state
578 **********************************************************/
579 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event)
583 struct SessionHandle *data = state->conn->data;
587 case TFTP_EVENT_DATA:
588 /* Is this the block we expect? */
589 rblock = getrpacketblock(&state->rpacket);
590 if(NEXT_BLOCKNUM(state->block) == rblock) {
591 /* This is the expected block. Reset counters and ACK it. */
594 else if(state->block == rblock) {
595 /* This is the last recently received block again. Log it and ACK it
597 infof(data, "Received last DATA packet block %d again.\n", rblock);
600 /* totally unexpected, just log it */
602 "Received unexpected DATA packet block %d, expecting block %d\n",
603 rblock, NEXT_BLOCKNUM(state->block));
607 /* ACK this block. */
608 state->block = (unsigned short)rblock;
609 setpacketevent(&state->spacket, TFTP_EVENT_ACK);
610 setpacketblock(&state->spacket, state->block);
611 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
613 (struct sockaddr *)&state->remote_addr,
614 state->remote_addrlen);
616 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
617 return CURLE_SEND_ERROR;
620 /* Check if completed (That is, a less than full packet is received) */
621 if(state->rbytes < (ssize_t)state->blksize+4) {
622 state->state = TFTP_STATE_FIN;
625 state->state = TFTP_STATE_RX;
627 time(&state->rx_time);
630 case TFTP_EVENT_OACK:
631 /* ACK option acknowledgement so we can move on to data */
634 setpacketevent(&state->spacket, TFTP_EVENT_ACK);
635 setpacketblock(&state->spacket, state->block);
636 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
638 (struct sockaddr *)&state->remote_addr,
639 state->remote_addrlen);
641 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
642 return CURLE_SEND_ERROR;
645 /* we're ready to RX data */
646 state->state = TFTP_STATE_RX;
647 time(&state->rx_time);
650 case TFTP_EVENT_TIMEOUT:
651 /* Increment the retry count and fail if over the limit */
654 "Timeout waiting for block %d ACK. Retries = %d\n",
655 NEXT_BLOCKNUM(state->block), state->retries);
656 if(state->retries > state->retry_max) {
657 state->error = TFTP_ERR_TIMEOUT;
658 state->state = TFTP_STATE_FIN;
661 /* Resend the previous ACK */
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(state->conn, SOCKERRNO));
668 return CURLE_SEND_ERROR;
673 case TFTP_EVENT_ERROR:
674 setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
675 setpacketblock(&state->spacket, state->block);
676 (void)sendto(state->sockfd, (void *)state->spacket.data,
678 (struct sockaddr *)&state->remote_addr,
679 state->remote_addrlen);
680 /* don't bother with the return code, but if the socket is still up we
681 * should be a good TFTP client and let the server know we're done */
682 state->state = TFTP_STATE_FIN;
686 failf(data, "%s", "tftp_rx: internal error");
687 return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
693 /**********************************************************
697 * Event handler for the TX state
699 **********************************************************/
700 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
702 struct SessionHandle *data = state->conn->data;
705 CURLcode res = CURLE_OK;
706 struct SingleRequest *k = &data->req;
711 case TFTP_EVENT_OACK:
712 if(event == TFTP_EVENT_ACK) {
714 rblock = getrpacketblock(&state->rpacket);
716 if(rblock != state->block &&
717 /* There's a bug in tftpd-hpa that causes it to send us an ack for
718 * 65535 when the block number wraps to 0. So when we're expecting
719 * 0, also accept 65535. See
720 * http://syslinux.zytor.com/archives/2010-September/015253.html
722 !(state->block == 0 && rblock == 65535)) {
723 /* This isn't the expected block. Log it and up the retry counter */
724 infof(data, "Received ACK for block %d, expecting %d\n",
725 rblock, state->block);
727 /* Bail out if over the maximum */
728 if(state->retries>state->retry_max) {
729 failf(data, "tftp_tx: giving up waiting for block %d ack",
731 res = CURLE_SEND_ERROR;
734 /* Re-send the data packet */
735 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
736 4+state->sbytes, SEND_4TH_ARG,
737 (struct sockaddr *)&state->remote_addr,
738 state->remote_addrlen);
739 /* Check all sbytes were sent */
741 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
742 res = CURLE_SEND_ERROR;
747 /* This is the expected packet. Reset the counters and send the next
749 time(&state->rx_time);
753 state->block = 1; /* first data block is 1 when using OACK */
756 setpacketevent(&state->spacket, TFTP_EVENT_DATA);
757 setpacketblock(&state->spacket, state->block);
758 if(state->block > 1 && state->sbytes < (int)state->blksize) {
759 state->state = TFTP_STATE_FIN;
762 res = Curl_fillreadbuffer(state->conn, state->blksize, &state->sbytes);
765 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
766 4+state->sbytes, SEND_4TH_ARG,
767 (struct sockaddr *)&state->remote_addr,
768 state->remote_addrlen);
769 /* Check all sbytes were sent */
771 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
772 return CURLE_SEND_ERROR;
774 /* Update the progress meter */
775 k->writebytecount += state->sbytes;
776 Curl_pgrsSetUploadCounter(data, k->writebytecount);
779 case TFTP_EVENT_TIMEOUT:
780 /* Increment the retry counter and log the timeout */
782 infof(data, "Timeout waiting for block %d ACK. "
783 " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries);
784 /* Decide if we've had enough */
785 if(state->retries > state->retry_max) {
786 state->error = TFTP_ERR_TIMEOUT;
787 state->state = TFTP_STATE_FIN;
790 /* Re-send the data packet */
791 sbytes = sendto(state->sockfd, (void *)state->spacket.data,
792 4+state->sbytes, SEND_4TH_ARG,
793 (struct sockaddr *)&state->remote_addr,
794 state->remote_addrlen);
795 /* Check all sbytes were sent */
797 failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
798 return CURLE_SEND_ERROR;
800 /* since this was a re-send, we remain at the still byte position */
801 Curl_pgrsSetUploadCounter(data, k->writebytecount);
805 case TFTP_EVENT_ERROR:
806 state->state = TFTP_STATE_FIN;
807 setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
808 setpacketblock(&state->spacket, state->block);
809 (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
810 (struct sockaddr *)&state->remote_addr,
811 state->remote_addrlen);
812 /* don't bother with the return code, but if the socket is still up we
813 * should be a good TFTP client and let the server know we're done */
814 state->state = TFTP_STATE_FIN;
818 failf(data, "tftp_tx: internal error, event: %i", (int)(event));
825 /**********************************************************
827 * tftp_translate_code
829 * Translate internal error codes to CURL error codes
831 **********************************************************/
832 static CURLcode tftp_translate_code(tftp_error_t error)
834 CURLcode code = CURLE_OK;
836 if(error != TFTP_ERR_NONE) {
838 case TFTP_ERR_NOTFOUND:
839 code = CURLE_TFTP_NOTFOUND;
842 code = CURLE_TFTP_PERM;
844 case TFTP_ERR_DISKFULL:
845 code = CURLE_REMOTE_DISK_FULL;
848 case TFTP_ERR_ILLEGAL:
849 code = CURLE_TFTP_ILLEGAL;
851 case TFTP_ERR_UNKNOWNID:
852 code = CURLE_TFTP_UNKNOWNID;
854 case TFTP_ERR_EXISTS:
855 code = CURLE_REMOTE_FILE_EXISTS;
857 case TFTP_ERR_NOSUCHUSER:
858 code = CURLE_TFTP_NOSUCHUSER;
860 case TFTP_ERR_TIMEOUT:
861 code = CURLE_OPERATION_TIMEDOUT;
863 case TFTP_ERR_NORESPONSE:
864 code = CURLE_COULDNT_CONNECT;
867 code= CURLE_ABORTED_BY_CALLBACK;
878 /**********************************************************
882 * The tftp state machine event dispatcher
884 **********************************************************/
885 static CURLcode tftp_state_machine(tftp_state_data_t *state,
888 CURLcode res = CURLE_OK;
889 struct SessionHandle *data = state->conn->data;
890 switch(state->state) {
891 case TFTP_STATE_START:
892 DEBUGF(infof(data, "TFTP_STATE_START\n"));
893 res = tftp_send_first(state, event);
896 DEBUGF(infof(data, "TFTP_STATE_RX\n"));
897 res = tftp_rx(state, event);
900 DEBUGF(infof(data, "TFTP_STATE_TX\n"));
901 res = tftp_tx(state, event);
904 infof(data, "%s\n", "TFTP finished");
907 DEBUGF(infof(data, "STATE: %d\n", state->state));
908 failf(data, "%s", "Internal state machine error");
909 res = CURLE_TFTP_ILLEGAL;
915 /**********************************************************
919 * The disconnect callback
921 **********************************************************/
922 static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection)
924 tftp_state_data_t *state = conn->proto.tftpc;
925 (void) dead_connection;
927 /* done, free dynamically allocated pkt buffers */
929 Curl_safefree(state->rpacket.data);
930 Curl_safefree(state->spacket.data);
937 /**********************************************************
941 * The connect callback
943 **********************************************************/
944 static CURLcode tftp_connect(struct connectdata *conn, bool *done)
947 tftp_state_data_t *state;
950 blksize = TFTP_BLKSIZE_DEFAULT;
952 state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t));
954 return CURLE_OUT_OF_MEMORY;
956 /* alloc pkt buffers based on specified blksize */
957 if(conn->data->set.tftp_blksize) {
958 blksize = (int)conn->data->set.tftp_blksize;
959 if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN )
960 return CURLE_TFTP_ILLEGAL;
963 if(!state->rpacket.data) {
964 state->rpacket.data = calloc(1, blksize + 2 + 2);
966 if(!state->rpacket.data)
967 return CURLE_OUT_OF_MEMORY;
970 if(!state->spacket.data) {
971 state->spacket.data = calloc(1, blksize + 2 + 2);
973 if(!state->spacket.data)
974 return CURLE_OUT_OF_MEMORY;
977 /* we don't keep TFTP connections up bascially because there's none or very
978 * little gain for UDP */
979 connclose(conn, "TFTP");
982 state->sockfd = state->conn->sock[FIRSTSOCKET];
983 state->state = TFTP_STATE_START;
984 state->error = TFTP_ERR_NONE;
985 state->blksize = TFTP_BLKSIZE_DEFAULT;
986 state->requested_blksize = blksize;
988 ((struct sockaddr *)&state->local_addr)->sa_family =
989 (unsigned short)(conn->ip_addr->ai_family);
991 tftp_set_timeouts(state);
993 if(!conn->bits.bound) {
994 /* If not already bound, bind to any interface, random UDP port. If it is
995 * reused or a custom local port was desired, this has already been done!
997 * We once used the size of the local_addr struct as the third argument
998 * for bind() to better work with IPv6 or whatever size the struct could
999 * have, but we learned that at least Tru64, AIX and IRIX *requires* the
1000 * size of that argument to match the exact size of a 'sockaddr_in' struct
1001 * when running IPv4-only.
1003 * Therefore we use the size from the address we connected to, which we
1004 * assume uses the same IP version and thus hopefully this works for both
1007 rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
1008 conn->ip_addr->ai_addrlen);
1010 failf(conn->data, "bind() failed; %s",
1011 Curl_strerror(conn, SOCKERRNO));
1012 return CURLE_COULDNT_CONNECT;
1014 conn->bits.bound = TRUE;
1017 Curl_pgrsStartNow(conn->data);
1024 /**********************************************************
1030 **********************************************************/
1031 static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
1034 CURLcode code = CURLE_OK;
1035 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
1037 (void)status; /* unused */
1038 (void)premature; /* not used */
1040 if(Curl_pgrsDone(conn))
1041 return CURLE_ABORTED_BY_CALLBACK;
1043 /* If we have encountered an error */
1045 code = tftp_translate_code(state->error);
1050 /**********************************************************
1054 * The getsock callback
1056 **********************************************************/
1057 static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks,
1061 return GETSOCK_BLANK;
1063 socks[0] = conn->sock[FIRSTSOCKET];
1065 return GETSOCK_READSOCK(0);
1068 /**********************************************************
1070 * tftp_receive_packet
1072 * Called once select fires and data is ready on the socket
1074 **********************************************************/
1075 static CURLcode tftp_receive_packet(struct connectdata *conn)
1077 struct Curl_sockaddr_storage fromaddr;
1078 curl_socklen_t fromlen;
1079 CURLcode result = CURLE_OK;
1080 struct SessionHandle *data = conn->data;
1081 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
1082 struct SingleRequest *k = &data->req;
1084 /* Receive the packet */
1085 fromlen = sizeof(fromaddr);
1086 state->rbytes = (int)recvfrom(state->sockfd,
1087 (void *)state->rpacket.data,
1090 (struct sockaddr *)&fromaddr,
1092 if(state->remote_addrlen==0) {
1093 memcpy(&state->remote_addr, &fromaddr, fromlen);
1094 state->remote_addrlen = fromlen;
1097 /* Sanity check packet length */
1098 if(state->rbytes < 4) {
1099 failf(data, "Received too short packet");
1100 /* Not a timeout, but how best to handle it? */
1101 state->event = TFTP_EVENT_TIMEOUT;
1104 /* The event is given by the TFTP packet time */
1105 state->event = (tftp_event_t)getrpacketevent(&state->rpacket);
1107 switch(state->event) {
1108 case TFTP_EVENT_DATA:
1109 /* Don't pass to the client empty or retransmitted packets */
1110 if(state->rbytes > 4 &&
1111 (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
1112 result = Curl_client_write(conn, CLIENTWRITE_BODY,
1113 (char *)state->rpacket.data+4,
1116 tftp_state_machine(state, TFTP_EVENT_ERROR);
1119 k->bytecount += state->rbytes-4;
1120 Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
1123 case TFTP_EVENT_ERROR:
1124 state->error = (tftp_error_t)getrpacketblock(&state->rpacket);
1125 infof(data, "%s\n", (const char *)state->rpacket.data+4);
1127 case TFTP_EVENT_ACK:
1129 case TFTP_EVENT_OACK:
1130 result = tftp_parse_option_ack(state,
1131 (const char *)state->rpacket.data+2,
1136 case TFTP_EVENT_RRQ:
1137 case TFTP_EVENT_WRQ:
1139 failf(data, "%s", "Internal error: Unexpected packet");
1143 /* Update the progress meter */
1144 if(Curl_pgrsUpdate(conn)) {
1145 tftp_state_machine(state, TFTP_EVENT_ERROR);
1146 return CURLE_ABORTED_BY_CALLBACK;
1152 /**********************************************************
1154 * tftp_state_timeout
1156 * Check if timeouts have been reached
1158 **********************************************************/
1159 static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event)
1162 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
1165 *event = TFTP_EVENT_NONE;
1168 if(current > state->max_time) {
1169 DEBUGF(infof(conn->data, "timeout: %ld > %ld\n",
1170 (long)current, (long)state->max_time));
1171 state->error = TFTP_ERR_TIMEOUT;
1172 state->state = TFTP_STATE_FIN;
1175 else if(current > state->rx_time+state->retry_time) {
1177 *event = TFTP_EVENT_TIMEOUT;
1178 time(&state->rx_time); /* update even though we received nothing */
1181 /* there's a typecast below here since 'time_t' may in fact be larger than
1182 'long', but we estimate that a 'long' will still be able to hold number
1183 of seconds even if "only" 32 bit */
1184 return (long)(state->max_time - current);
1187 /**********************************************************
1189 * tftp_multi_statemach
1191 * Handle single RX socket event and return
1193 **********************************************************/
1194 static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
1198 CURLcode result = CURLE_OK;
1199 struct SessionHandle *data = conn->data;
1200 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
1201 long timeout_ms = tftp_state_timeout(conn, &event);
1205 if(timeout_ms <= 0) {
1206 failf(data, "TFTP response timeout");
1207 return CURLE_OPERATION_TIMEDOUT;
1209 else if(event != TFTP_EVENT_NONE) {
1210 result = tftp_state_machine(state, event);
1211 if(result != CURLE_OK)
1213 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
1215 /* Tell curl we're done */
1216 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
1219 /* no timeouts to handle, check our socket */
1220 rc = Curl_socket_ready(state->sockfd, CURL_SOCKET_BAD, 0);
1224 int error = SOCKERRNO;
1225 failf(data, "%s", Curl_strerror(conn, error));
1226 state->event = TFTP_EVENT_ERROR;
1229 result = tftp_receive_packet(conn);
1230 if(result != CURLE_OK)
1232 result = tftp_state_machine(state, state->event);
1233 if(result != CURLE_OK)
1235 *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
1237 /* Tell curl we're done */
1238 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
1240 /* if rc == 0, then select() timed out */
1246 /**********************************************************
1250 * Called from multi.c while DOing
1252 **********************************************************/
1253 static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done)
1256 result = tftp_multi_statemach(conn, dophase_done);
1259 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1262 /* The multi code doesn't have this logic for the DOING state so we
1263 provide it for TFTP since it may do the entire transfer in this
1265 if(Curl_pgrsUpdate(conn))
1266 result = CURLE_ABORTED_BY_CALLBACK;
1268 result = Curl_speedcheck(conn->data, Curl_tvnow());
1273 /**********************************************************
1277 * Entry point for transfer from tftp_do, sarts state mach
1279 **********************************************************/
1280 static CURLcode tftp_perform(struct connectdata *conn, bool *dophase_done)
1282 CURLcode result = CURLE_OK;
1283 tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
1285 *dophase_done = FALSE;
1287 result = tftp_state_machine(state, TFTP_EVENT_INIT);
1289 if(state->state == TFTP_STATE_FIN || result != CURLE_OK)
1292 tftp_multi_statemach(conn, dophase_done);
1295 DEBUGF(infof(conn->data, "DO phase is complete\n"));
1301 /**********************************************************
1307 * This callback initiates the TFTP transfer
1309 **********************************************************/
1311 static CURLcode tftp_do(struct connectdata *conn, bool *done)
1313 tftp_state_data_t *state;
1318 if(!conn->proto.tftpc) {
1319 code = tftp_connect(conn, done);
1324 state = (tftp_state_data_t *)conn->proto.tftpc;
1326 return CURLE_BAD_CALLING_ORDER;
1328 code = tftp_perform(conn, done);
1330 /* If tftp_perform() returned an error, use that for return code. If it
1331 was OK, see if tftp_translate_code() has an error. */
1332 if(code == CURLE_OK)
1333 /* If we have encountered an internal tftp error, translate it. */
1334 code = tftp_translate_code(state->error);
1339 static CURLcode tftp_setup_connection(struct connectdata * conn)
1341 struct SessionHandle *data = conn->data;
1345 conn->socktype = SOCK_DGRAM; /* UDP datagram based */
1347 /* TFTP URLs support an extension like ";mode=<typecode>" that
1348 * we'll try to get now! */
1349 type = strstr(data->state.path, ";mode=");
1352 type = strstr(conn->host.rawalloc, ";mode=");
1355 *type = 0; /* it was in the middle of the hostname */
1356 command = Curl_raw_toupper(type[6]);
1359 case 'A': /* ASCII mode */
1360 case 'N': /* NETASCII mode */
1361 data->set.prefer_ascii = TRUE;
1364 case 'O': /* octet mode */
1365 case 'I': /* binary mode */
1367 /* switch off ASCII */
1368 data->set.prefer_ascii = FALSE;