* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
#include <sys/param.h>
#endif
-#include "curl_urldata.h"
+#include "urldata.h"
#include <curl/curl.h>
-#include "curl_transfer.h"
-#include "curl_sendf.h"
-#include "curl_tftp.h"
-#include "curl_progress.h"
-#include "curl_connect.h"
-#include "curl_strerror.h"
-#include "curl_sockaddr.h" /* required for Curl_sockaddr_storage */
-#include "curl_multiif.h"
-#include "curl_url.h"
-#include "curl_rawstr.h"
+#include "transfer.h"
+#include "sendf.h"
+#include "tftp.h"
+#include "progress.h"
+#include "connect.h"
+#include "strerror.h"
+#include "sockaddr.h" /* required for Curl_sockaddr_storage */
+#include "multiif.h"
+#include "url.h"
+#include "rawstr.h"
+#include "speedcheck.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
#include "curl_memory.h"
-#include "curl_select.h"
+#include "select.h"
/* The last #include file should be: */
-#include "curl_memdebug.h"
+#include "memdebug.h"
/* RFC2348 allows the block size to be negotiated */
#define TFTP_BLKSIZE_DEFAULT 512
setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
state->conn->data->req.upload_fromhere =
(char *)state->spacket.data+4;
- if(data->set.infilesize != -1)
- Curl_pgrsSetUploadSize(data, data->set.infilesize);
+ if(data->state.infilesize != -1)
+ Curl_pgrsSetUploadSize(data, data->state.infilesize);
}
else {
/* If we are downloading, send an RRQ */
sbytes = 4 + strlen(filename) + strlen(mode);
/* add tsize option */
- if(data->set.upload && (data->set.infilesize != -1))
- snprintf( buf, sizeof(buf), "%" FORMAT_OFF_T, data->set.infilesize );
+ if(data->set.upload && (data->state.infilesize != -1))
+ snprintf(buf, sizeof(buf), "%" CURL_FORMAT_CURL_OFF_T,
+ data->state.infilesize);
else
strcpy(buf, "0"); /* the destination is large enough */
blksize = TFTP_BLKSIZE_DEFAULT;
- /* If there already is a protocol-specific struct allocated for this
- sessionhandle, deal with it */
- Curl_reset_reqproto(conn);
-
state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t));
if(!state)
return CURLE_OUT_OF_MEMORY;
return CURLE_OUT_OF_MEMORY;
}
- conn->bits.close = TRUE; /* we don't keep TFTP connections up bascially
- because there's none or very little gain for UDP
- */
+ /* we don't keep TFTP connections up bascially because there's none or very
+ * little gain for UDP */
+ connclose(conn, "TFTP");
state->conn = conn;
state->sockfd = state->conn->sock[FIRSTSOCKET];
return CURLE_ABORTED_BY_CALLBACK;
/* If we have encountered an error */
- code = tftp_translate_code(state->error);
+ if(state)
+ code = tftp_translate_code(state->error);
return code;
}
return (long)(state->max_time - current);
}
-
-/**********************************************************
- *
- * tftp_easy_statemach
- *
- * Handle easy request until completion
- *
- **********************************************************/
-static CURLcode tftp_easy_statemach(struct connectdata *conn)
-{
- int rc;
- int check_time = 0;
- CURLcode result = CURLE_OK;
- struct SessionHandle *data = conn->data;
- tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
- curl_socket_t fd_read;
- long timeout_ms;
- struct SingleRequest *k = &data->req;
- struct timeval transaction_start = Curl_tvnow();
-
- k->start = transaction_start;
- k->now = transaction_start;
-
- /* Run the TFTP State Machine */
- for(; (state->state != TFTP_STATE_FIN) && (result == CURLE_OK); ) {
-
- timeout_ms = state->retry_time * 1000;
-
- if(data->set.upload) {
- if(data->set.max_send_speed &&
- (data->progress.ulspeed > data->set.max_send_speed)) {
- fd_read = CURL_SOCKET_BAD;
- timeout_ms = Curl_sleep_time(data->set.max_send_speed,
- data->progress.ulspeed, state->blksize);
- }
- else {
- fd_read = state->sockfd;
- }
- }
- else {
- if(data->set.max_recv_speed &&
- (data->progress.dlspeed > data->set.max_recv_speed)) {
- fd_read = CURL_SOCKET_BAD;
- timeout_ms = Curl_sleep_time(data->set.max_recv_speed,
- data->progress.dlspeed, state->blksize);
- }
- else
- fd_read = state->sockfd;
- }
-
- if(data->set.timeout) {
- timeout_ms = data->set.timeout - Curl_tvdiff(k->now, k->start);
- if(timeout_ms > state->retry_time * 1000)
- timeout_ms = state->retry_time * 1000;
- else if(timeout_ms < 0)
- timeout_ms = 0;
- }
-
-
- /* Wait until ready to read or timeout occurs */
- rc = Curl_socket_ready(fd_read, CURL_SOCKET_BAD, timeout_ms);
-
- k->now = Curl_tvnow();
-
- /* Force a progress callback if it's been too long */
- if(Curl_tvdiff(k->now, k->start) >= data->set.timeout) {
- if(Curl_pgrsUpdate(conn)) {
- tftp_state_machine(state, TFTP_EVENT_ERROR);
- return CURLE_ABORTED_BY_CALLBACK;
- }
- k->start = k->now;
- }
-
- if(rc == -1) {
- /* bail out */
- int error = SOCKERRNO;
- failf(data, "%s", Curl_strerror(conn, error));
- state->event = TFTP_EVENT_ERROR;
- }
- else {
-
- if(rc==0) {
- /* A timeout occurred, but our timeout is variable, so maybe
- just continue? */
- long rtms = state->retry_time * 1000;
- if(Curl_tvdiff(k->now, transaction_start) > rtms) {
- state->event = TFTP_EVENT_TIMEOUT;
- /* Force a look at transfer timeouts */
- check_time = 1;
- }
- else {
- continue; /* skip state machine */
- }
- }
- else {
- result = tftp_receive_packet(conn);
- if(result == CURLE_OK)
- transaction_start = Curl_tvnow();
-
- if(k->bytecountp)
- *k->bytecountp = k->bytecount; /* read count */
- if(k->writebytecountp)
- *k->writebytecountp = k->writebytecount; /* write count */
- }
- }
-
- if(check_time) {
- tftp_state_timeout(conn, NULL);
- check_time = 0;
- }
-
- if(result)
- return(result);
-
- result = tftp_state_machine(state, state->event);
- }
-
- /* Tell curl we're done */
- Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
-
- return(result);
-}
-
/**********************************************************
*
* tftp_multi_statemach
*
* tftp_doing
*
- * Called from curl_multi.c while DOing
+ * Called from multi.c while DOing
*
**********************************************************/
static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done)
if(*dophase_done) {
DEBUGF(infof(conn->data, "DO phase is complete\n"));
}
+ else if(!result) {
+ /* The multi code doesn't have this logic for the DOING state so we
+ provide it for TFTP since it may do the entire transfer in this
+ state. */
+ if(Curl_pgrsUpdate(conn))
+ result = CURLE_ABORTED_BY_CALLBACK;
+ else
+ result = Curl_speedcheck(conn->data, Curl_tvnow());
+ }
return result;
}
if(state->state == TFTP_STATE_FIN || result != CURLE_OK)
return(result);
- if(conn->data->state.used_interface == Curl_if_multi)
- tftp_multi_statemach(conn, dophase_done);
- else {
- result = tftp_easy_statemach(conn);
- *dophase_done = TRUE; /* with the easy interface we are done here */
- }
+ tftp_multi_statemach(conn, dophase_done);
if(*dophase_done)
DEBUGF(infof(conn->data, "DO phase is complete\n"));
*done = FALSE;
- /*
- Since connections can be re-used between SessionHandles, this might be a
- connection already existing but on a fresh SessionHandle struct so we must
- make sure we have a good 'struct TFTP' to play with. For new connections,
- the struct TFTP is allocated and setup in the tftp_connect() function.
- */
- Curl_reset_reqproto(conn);
-
if(!conn->proto.tftpc) {
code = tftp_connect(conn, done);
if(code)
return code;
}
+
state = (tftp_state_data_t *)conn->proto.tftpc;
+ if(!state)
+ return CURLE_BAD_CALLING_ORDER;
code = tftp_perform(conn, done);