Newer
Older
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2016, 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
* are also available at https://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "curl_setup.h"
#ifndef CURL_DISABLE_TFTP
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <curl/curl.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"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
Daniel Stenberg
committed
/* RFC2348 allows the block size to be negotiated */
#define TFTP_BLKSIZE_DEFAULT 512
#define TFTP_BLKSIZE_MIN 8
#define TFTP_BLKSIZE_MAX 65464
#define TFTP_OPTION_BLKSIZE "blksize"
/* from RFC2349: */
#define TFTP_OPTION_TSIZE "tsize"
#define TFTP_OPTION_INTERVAL "timeout"
typedef enum {
TFTP_MODE_NETASCII=0,
TFTP_MODE_OCTET
} tftp_mode_t;
typedef enum {
TFTP_STATE_START=0,
TFTP_STATE_RX,
TFTP_STATE_TX,
TFTP_STATE_FIN
} tftp_state_t;
typedef enum {
Daniel Stenberg
committed
TFTP_EVENT_NONE = -1,
TFTP_EVENT_INIT = 0,
TFTP_EVENT_RRQ = 1,
TFTP_EVENT_WRQ = 2,
TFTP_EVENT_DATA = 3,
TFTP_EVENT_ACK = 4,
TFTP_EVENT_ERROR = 5,
Daniel Stenberg
committed
TFTP_EVENT_OACK = 6,
TFTP_EVENT_TIMEOUT
} tftp_event_t;
typedef enum {
TFTP_ERR_UNDEF=0,
TFTP_ERR_NOTFOUND,
TFTP_ERR_PERM,
TFTP_ERR_DISKFULL,
TFTP_ERR_ILLEGAL,
TFTP_ERR_UNKNOWNID,
TFTP_ERR_EXISTS,
Daniel Stenberg
committed
TFTP_ERR_NOSUCHUSER, /* This will never be triggered by this code */
/* The remaining error codes are internal to curl */
TFTP_ERR_NONE = -100,
TFTP_ERR_TIMEOUT,
TFTP_ERR_NORESPONSE
} tftp_error_t;
typedef struct tftp_packet {
Daniel Stenberg
committed
unsigned char *data;
} tftp_packet_t;
typedef struct tftp_state_data {
tftp_state_t state;
tftp_mode_t mode;
tftp_error_t error;
Daniel Stenberg
committed
tftp_event_t event;
struct connectdata *conn;
int retries;
Daniel Stenberg
committed
int retry_time;
int retry_max;
time_t start_time;
time_t max_time;
Daniel Stenberg
committed
time_t rx_time;
unsigned short block;
Daniel Stenberg
committed
struct Curl_sockaddr_storage local_addr;
struct Curl_sockaddr_storage remote_addr;
Daniel Stenberg
committed
int rbytes;
int sbytes;
int blksize;
Daniel Stenberg
committed
int requested_blksize;
tftp_packet_t rpacket;
tftp_packet_t spacket;
} tftp_state_data_t;
/* Forward declarations */
static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event);
static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event);
static CURLcode tftp_connect(struct connectdata *conn, bool *done);
static CURLcode tftp_disconnect(struct connectdata *conn,
bool dead_connection);
static CURLcode tftp_do(struct connectdata *conn, bool *done);
static CURLcode tftp_done(struct connectdata *conn,
static CURLcode tftp_setup_connection(struct connectdata * conn);
Daniel Stenberg
committed
static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done);
static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done);
static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks,
int numsocks);
static CURLcode tftp_translate_code(tftp_error_t error);
Patrick Monnerat
committed
/*
* TFTP protocol handler.
*/
const struct Curl_handler Curl_handler_tftp = {
"TFTP", /* scheme */
tftp_setup_connection, /* setup_connection */
tftp_do, /* do_it */
tftp_done, /* done */
ZERO_NULL, /* do_more */
tftp_connect, /* connect_it */
Daniel Stenberg
committed
tftp_multi_statemach, /* connecting */
tftp_doing, /* doing */
tftp_getsock, /* proto_getsock */
tftp_getsock, /* doing_getsock */
ZERO_NULL, /* domore_getsock */
Daniel Stenberg
committed
ZERO_NULL, /* perform_getsock */
Daniel Stenberg
committed
tftp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
Patrick Monnerat
committed
PORT_TFTP, /* defport */
PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
Patrick Monnerat
committed
};
/**********************************************************
*
* tftp_set_timeouts -
*
* Set timeouts based on state machine state.
* Use user provided connect timeouts until DATA or ACK
* packet is received, then use user-provided transfer timeouts
*
*
**********************************************************/
Daniel Stenberg
committed
static CURLcode tftp_set_timeouts(tftp_state_data_t *state)
{
time_t maxtime, timeout;
Daniel Stenberg
committed
long timeout_ms;
bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE;
time(&state->start_time);
Daniel Stenberg
committed
Daniel Stenberg
committed
/* Compute drop-dead time */
timeout_ms = Curl_timeleft(state->conn->data, NULL, start);
Daniel Stenberg
committed
if(timeout_ms < 0) {
/* time-out, bail out, go home */
failf(state->conn->data, "Connection time-out");
return CURLE_OPERATION_TIMEDOUT;
}
Daniel Stenberg
committed
Daniel Stenberg
committed
maxtime = (time_t)(timeout_ms + 500) / 1000;
state->max_time = state->start_time+maxtime;
/* Set per-block timeout to total */
timeout = maxtime;
/* Average restart after 5 seconds */
Daniel Stenberg
committed
state->retry_max = (int)timeout/5;
if(state->retry_max < 1)
/* avoid division by zero below */
state->retry_max = 1;
/* Compute the re-start interval to suit the timeout */
Daniel Stenberg
committed
state->retry_time = (int)timeout/state->retry_max;
if(state->retry_time<1)
state->retry_time=1;
}
else {
Daniel Stenberg
committed
if(timeout_ms > 0)
maxtime = (time_t)(timeout_ms + 500) / 1000;
else
maxtime = 3600;
state->max_time = state->start_time+maxtime;
/* Set per-block timeout to total */
timeout = maxtime;
/* Average reposting an ACK after 5 seconds */
state->retry_max = (int)timeout/5;
}
Daniel Stenberg
committed
/* But bound the total number */
if(state->retry_max<3)
state->retry_max=3;
if(state->retry_max>50)
state->retry_max=50;
/* Compute the re-ACK interval to suit the timeout */
state->retry_time = (int)(timeout/state->retry_max);
if(state->retry_time<1)
state->retry_time=1;
state->retry_time, state->retry_max);
Daniel Stenberg
committed
Daniel Stenberg
committed
/* init RX time */
time(&state->rx_time);
Daniel Stenberg
committed
return CURLE_OK;
}
/**********************************************************
*
* tftp_set_send_first
*
* Event handler for the START state
*
**********************************************************/
static void setpacketevent(tftp_packet_t *packet, unsigned short num)
{
packet->data[0] = (unsigned char)(num >> 8);
packet->data[1] = (unsigned char)(num & 0xff);
}
static void setpacketblock(tftp_packet_t *packet, unsigned short num)
{
packet->data[2] = (unsigned char)(num >> 8);
packet->data[3] = (unsigned char)(num & 0xff);
}
static unsigned short getrpacketevent(const tftp_packet_t *packet)
{
return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
}
static unsigned short getrpacketblock(const tftp_packet_t *packet)
{
return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
}
Daniel Stenberg
committed
static size_t Curl_strnlen(const char *string, size_t maxlen)
{
const char *end = memchr (string, '\0', maxlen);
return end ? (size_t) (end - string) : maxlen;
}
static const char *tftp_option_get(const char *buf, size_t len,
const char **option, const char **value)
{
size_t loc;
Daniel Stenberg
committed
loc++; /* NULL term */
Daniel Stenberg
committed
return NULL;
*option = buf;
Daniel Stenberg
committed
loc++; /* NULL term */
Daniel Stenberg
committed
return NULL;
*value = &buf[strlen(*option) + 1];
return &buf[loc];
}
static CURLcode tftp_parse_option_ack(tftp_state_data_t *state,
const char *ptr, int len)
{
const char *tmp = ptr;
struct Curl_easy *data = state->conn->data;
Daniel Stenberg
committed
/* if OACK doesn't contain blksize option, the default (512) must be used */
state->blksize = TFTP_BLKSIZE_DEFAULT;
while(tmp < ptr + len) {
Daniel Stenberg
committed
const char *option, *value;
tmp = tftp_option_get(tmp, ptr + len - tmp, &option, &value);
if(tmp == NULL) {
failf(data, "Malformed ACK packet, rejecting");
return CURLE_TFTP_ILLEGAL;
}
infof(data, "got option=(%s) value=(%s)\n", option, value);
if(checkprefix(option, TFTP_OPTION_BLKSIZE)) {
Daniel Stenberg
committed
long blksize;
Daniel Stenberg
committed
Daniel Stenberg
committed
if(!blksize) {
failf(data, "invalid blocksize value in OACK packet");
return CURLE_TFTP_ILLEGAL;
}
else if(blksize > TFTP_BLKSIZE_MAX) {
failf(data, "%s (%d)", "blksize is larger than max supported",
Daniel Stenberg
committed
return CURLE_TFTP_ILLEGAL;
}
else if(blksize < TFTP_BLKSIZE_MIN) {
failf(data, "%s (%d)", "blksize is smaller than min supported",
Daniel Stenberg
committed
return CURLE_TFTP_ILLEGAL;
}
else if(blksize > state->requested_blksize) {
Daniel Stenberg
committed
/* could realloc pkt buffers here, but the spec doesn't call out
* support for the server requesting a bigger blksize than the client
* requests */
"server requested blksize larger than allocated", blksize);
Daniel Stenberg
committed
return CURLE_TFTP_ILLEGAL;
}
Daniel Stenberg
committed
state->blksize = (int)blksize;
Daniel Stenberg
committed
infof(data, "%s (%d) %s (%d)\n", "blksize parsed from OACK",
state->blksize, "requested", state->requested_blksize);
Daniel Stenberg
committed
}
else if(checkprefix(option, TFTP_OPTION_TSIZE)) {
long tsize = 0;
/* tsize should be ignored on upload: Who cares about the size of the
remote file? */
if(!data->set.upload) {
if(!tsize) {
failf(data, "invalid tsize -:%s:- value in OACK packet", value);
return CURLE_TFTP_ILLEGAL;
}
Curl_pgrsSetDownloadSize(data, tsize);
}
Daniel Stenberg
committed
}
}
return CURLE_OK;
}
static size_t tftp_option_add(tftp_state_data_t *state, size_t csize,
char *buf, const char *option)
{
if(( strlen(option) + csize + 1) > (size_t)state->blksize)
Daniel Stenberg
committed
return 0;
Daniel Stenberg
committed
strcpy(buf, option);
return strlen(option) + 1;
Daniel Stenberg
committed
}
Daniel Stenberg
committed
static CURLcode tftp_connect_for_tx(tftp_state_data_t *state,
tftp_event_t event)
Daniel Stenberg
committed
{
struct Curl_easy *data = state->conn->data;
Daniel Stenberg
committed
infof(data, "%s\n", "Connected for transmit");
Daniel Stenberg
committed
state->state = TFTP_STATE_TX;
result = tftp_set_timeouts(state);
if(result)
Daniel Stenberg
committed
return tftp_tx(state, event);
}
Daniel Stenberg
committed
static CURLcode tftp_connect_for_rx(tftp_state_data_t *state,
tftp_event_t event)
Daniel Stenberg
committed
{
struct Curl_easy *data = state->conn->data;
Daniel Stenberg
committed
infof(data, "%s\n", "Connected for receive");
Daniel Stenberg
committed
state->state = TFTP_STATE_RX;
result = tftp_set_timeouts(state);
if(result)
Daniel Stenberg
committed
return tftp_rx(state, event);
}
static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
{
Daniel Stenberg
committed
ssize_t senddata;
const char *mode = "octet";
char *filename;
Daniel Stenberg
committed
char buf[64];
struct Curl_easy *data = state->conn->data;
CURLcode result = CURLE_OK;
/* Set ascii mode if -B flag was used */
Daniel Stenberg
committed
if(data->set.prefer_ascii)
mode = "netascii";
switch(event) {
case TFTP_EVENT_INIT: /* Send the first packet out */
case TFTP_EVENT_TIMEOUT: /* Resend the first packet out */
/* Increment the retry counter, quit if over the limit */
state->retries++;
if(state->retries>state->retry_max) {
state->error = TFTP_ERR_NORESPONSE;
state->state = TFTP_STATE_FIN;
}
if(data->set.upload) {
/* If we are uploading, send an WRQ */
setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
Daniel Stenberg
committed
state->conn->data->req.upload_fromhere =
Daniel Stenberg
committed
(char *)state->spacket.data+4;
if(data->state.infilesize != -1)
Curl_pgrsSetUploadSize(data, data->state.infilesize);
}
else {
/* If we are downloading, send an RRQ */
setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
}
/* As RFC3617 describes the separator slash is not actually part of the
Daniel Stenberg
committed
file name so we skip the always-present first letter of the path
string. */
Daniel Stenberg
committed
filename = curl_easy_unescape(data, &state->conn->data->state.path[1], 0,
Daniel Stenberg
committed
NULL);
return CURLE_OUT_OF_MEMORY;
Daniel Stenberg
committed
snprintf((char *)state->spacket.data+2,
state->blksize,
"%s%c%s%c", filename, '\0', mode, '\0');
Daniel Stenberg
committed
sbytes = 4 + strlen(filename) + strlen(mode);
/* optional addition of TFTP options */
if(!data->set.tftp_no_options) {
/* add tsize option */
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 */
sbytes += tftp_option_add(state, sbytes,
(char *)state->spacket.data+sbytes,
TFTP_OPTION_TSIZE);
sbytes += tftp_option_add(state, sbytes,
(char *)state->spacket.data+sbytes, buf);
/* add blksize option */
snprintf(buf, sizeof(buf), "%d", state->requested_blksize);
sbytes += tftp_option_add(state, sbytes,
(char *)state->spacket.data+sbytes,
TFTP_OPTION_BLKSIZE);
sbytes += tftp_option_add(state, sbytes,
/* add timeout option */
snprintf(buf, sizeof(buf), "%d", state->retry_time);
sbytes += tftp_option_add(state, sbytes,
(char *)state->spacket.data+sbytes,
TFTP_OPTION_INTERVAL);
sbytes += tftp_option_add(state, sbytes,
Daniel Stenberg
committed
/* the typecase for the 3rd argument is mostly for systems that do
not have a size_t argument, like older unixes that want an 'int' */
Daniel Stenberg
committed
senddata = sendto(state->sockfd, (void *)state->spacket.data,
(SEND_TYPE_ARG3)sbytes, 0,
state->conn->ip_addr->ai_addr,
state->conn->ip_addr->ai_addrlen);
Daniel Stenberg
committed
if(senddata != (ssize_t)sbytes) {
Daniel Stenberg
committed
failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
}
break;
Daniel Stenberg
committed
case TFTP_EVENT_OACK:
if(data->set.upload) {
result = tftp_connect_for_tx(state, event);
Daniel Stenberg
committed
}
else {
result = tftp_connect_for_rx(state, event);
Daniel Stenberg
committed
}
break;
case TFTP_EVENT_ACK: /* Connected for transmit */
result = tftp_connect_for_tx(state, event);
Daniel Stenberg
committed
break;
Daniel Stenberg
committed
case TFTP_EVENT_DATA: /* Connected for receive */
result = tftp_connect_for_rx(state, event);
Daniel Stenberg
committed
break;
case TFTP_EVENT_ERROR:
state->state = TFTP_STATE_FIN;
break;
default:
Daniel Stenberg
committed
failf(state->conn->data, "tftp_send_first: internal error");
break;
}
}
/* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
boundary */
#define NEXT_BLOCKNUM(x) (((x)+1)&0xffff)
/**********************************************************
*
* tftp_rx
*
* Event handler for the RX state
*
**********************************************************/
static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event)
{
Daniel Stenberg
committed
ssize_t sbytes;
int rblock;
struct Curl_easy *data = state->conn->data;
switch(event) {
case TFTP_EVENT_DATA:
/* Is this the block we expect? */
rblock = getrpacketblock(&state->rpacket);
if(NEXT_BLOCKNUM(state->block) == rblock) {
/* This is the expected block. Reset counters and ACK it. */
state->retries = 0;
}
else if(state->block == rblock) {
/* This is the last recently received block again. Log it and ACK it
again. */
infof(data, "Received last DATA packet block %d again.\n", rblock);
}
else {
/* totally unexpected, just log it */
"Received unexpected DATA packet block %d, expecting block %d\n",
rblock, NEXT_BLOCKNUM(state->block));
}
setpacketevent(&state->spacket, TFTP_EVENT_ACK);
setpacketblock(&state->spacket, state->block);
Daniel Stenberg
committed
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
4, SEND_4TH_ARG,
(struct sockaddr *)&state->remote_addr,
state->remote_addrlen);
if(sbytes < 0) {
Daniel Stenberg
committed
failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
}
/* Check if completed (That is, a less than full packet is received) */
if(state->rbytes < (ssize_t)state->blksize+4) {
state->state = TFTP_STATE_FIN;
}
else {
state->state = TFTP_STATE_RX;
}
Daniel Stenberg
committed
time(&state->rx_time);
break;
Daniel Stenberg
committed
case TFTP_EVENT_OACK:
/* ACK option acknowledgement so we can move on to data */
state->block = 0;
state->retries = 0;
setpacketevent(&state->spacket, TFTP_EVENT_ACK);
setpacketblock(&state->spacket, state->block);
Daniel Stenberg
committed
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
4, SEND_4TH_ARG,
(struct sockaddr *)&state->remote_addr,
state->remote_addrlen);
if(sbytes < 0) {
Daniel Stenberg
committed
failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
return CURLE_SEND_ERROR;
}
/* we're ready to RX data */
state->state = TFTP_STATE_RX;
Daniel Stenberg
committed
time(&state->rx_time);
Daniel Stenberg
committed
break;
case TFTP_EVENT_TIMEOUT:
/* Increment the retry count and fail if over the limit */
state->retries++;
NEXT_BLOCKNUM(state->block), state->retries);
if(state->retries > state->retry_max) {
state->error = TFTP_ERR_TIMEOUT;
state->state = TFTP_STATE_FIN;
}
else {
Daniel Stenberg
committed
/* Resend the previous ACK */
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
4, SEND_4TH_ARG,
(struct sockaddr *)&state->remote_addr,
state->remote_addrlen);
if(sbytes<0) {
Daniel Stenberg
committed
failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
}
}
break;
case TFTP_EVENT_ERROR:
Daniel Stenberg
committed
setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
setpacketblock(&state->spacket, state->block);
(void)sendto(state->sockfd, (void *)state->spacket.data,
4, SEND_4TH_ARG,
(struct sockaddr *)&state->remote_addr,
state->remote_addrlen);
Daniel Stenberg
committed
/* don't bother with the return code, but if the socket is still up we
* should be a good TFTP client and let the server know we're done */
state->state = TFTP_STATE_FIN;
break;
default:
Daniel Stenberg
committed
failf(data, "%s", "tftp_rx: internal error");
return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
this */
}
return CURLE_OK;
}
/**********************************************************
*
* tftp_tx
*
* Event handler for the TX state
*
**********************************************************/
static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
{
struct Curl_easy *data = state->conn->data;
Daniel Stenberg
committed
ssize_t sbytes;
int rblock;
CURLcode result = CURLE_OK;
Daniel Stenberg
committed
struct SingleRequest *k = &data->req;
int cb; /* Bytes currently read */
switch(event) {
case TFTP_EVENT_ACK:
case TFTP_EVENT_OACK:
if(event == TFTP_EVENT_ACK) {
/* Ack the packet */
rblock = getrpacketblock(&state->rpacket);
if(rblock != state->block &&
/* There's a bug in tftpd-hpa that causes it to send us an ack for
* 65535 when the block number wraps to 0. So when we're expecting
* 0, also accept 65535. See
* http://syslinux.zytor.com/archives/2010-September/015253.html
* */
!(state->block == 0 && rblock == 65535)) {
/* This isn't the expected block. Log it and up the retry counter */
infof(data, "Received ACK for block %d, expecting %d\n",
rblock, state->block);
state->retries++;
/* Bail out if over the maximum */
if(state->retries>state->retry_max) {
failf(data, "tftp_tx: giving up waiting for block %d ack",
state->block);
result = CURLE_SEND_ERROR;
}
else {
/* Re-send the data packet */
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
4+state->sbytes, SEND_4TH_ARG,
(struct sockaddr *)&state->remote_addr,
state->remote_addrlen);
/* Check all sbytes were sent */
if(sbytes<0) {
failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
result = CURLE_SEND_ERROR;
}
/* This is the expected packet. Reset the counters and send the next
block */
time(&state->rx_time);
state->block++;
}
else
state->block = 1; /* first data block is 1 when using OACK */
state->retries = 0;
setpacketevent(&state->spacket, TFTP_EVENT_DATA);
setpacketblock(&state->spacket, state->block);
Daniel Stenberg
committed
if(state->block > 1 && state->sbytes < (int)state->blksize) {
state->state = TFTP_STATE_FIN;
return CURLE_OK;
}
/* TFTP considers data block size < 512 bytes as an end of session. So
* in some cases we must wait for additional data to build full (512 bytes)
* data block.
* */
state->sbytes = 0;
state->conn->data->req.upload_fromhere = (char *)state->spacket.data+4;
do {
result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes,
&cb);
if(result)
return result;
state->sbytes += cb;
state->conn->data->req.upload_fromhere += cb;
} while(state->sbytes < state->blksize && cb != 0);
sbytes = sendto(state->sockfd, (void *) state->spacket.data,
4 + state->sbytes, SEND_4TH_ARG,
Daniel Stenberg
committed
(struct sockaddr *)&state->remote_addr,
state->remote_addrlen);
/* Check all sbytes were sent */
if(sbytes<0) {
Daniel Stenberg
committed
failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
return CURLE_SEND_ERROR;
}
/* Update the progress meter */
k->writebytecount += state->sbytes;
Curl_pgrsSetUploadCounter(data, k->writebytecount);
break;
case TFTP_EVENT_TIMEOUT:
/* Increment the retry counter and log the timeout */
state->retries++;
infof(data, "Timeout waiting for block %d ACK. "
" Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries);
/* Decide if we've had enough */
if(state->retries > state->retry_max) {
state->error = TFTP_ERR_TIMEOUT;
state->state = TFTP_STATE_FIN;
}
else {
Daniel Stenberg
committed
/* Re-send the data packet */
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
4+state->sbytes, SEND_4TH_ARG,
(struct sockaddr *)&state->remote_addr,
state->remote_addrlen);
/* Check all sbytes were sent */
if(sbytes<0) {
Daniel Stenberg
committed
failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
return CURLE_SEND_ERROR;
}
/* since this was a re-send, we remain at the still byte position */
Curl_pgrsSetUploadCounter(data, k->writebytecount);
}
break;
case TFTP_EVENT_ERROR:
state->state = TFTP_STATE_FIN;
Daniel Stenberg
committed
setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
setpacketblock(&state->spacket, state->block);
(void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
(struct sockaddr *)&state->remote_addr,
state->remote_addrlen);
Daniel Stenberg
committed
/* don't bother with the return code, but if the socket is still up we
* should be a good TFTP client and let the server know we're done */
state->state = TFTP_STATE_FIN;
break;
default:
failf(data, "tftp_tx: internal error, event: %i", (int)(event));
break;
}
}
Daniel Stenberg
committed
/**********************************************************
*
* tftp_translate_code
*
* Translate internal error codes to CURL error codes
*
**********************************************************/
static CURLcode tftp_translate_code(tftp_error_t error)
Daniel Stenberg
committed
{
CURLcode result = CURLE_OK;
Daniel Stenberg
committed
if(error != TFTP_ERR_NONE) {
switch(error) {
case TFTP_ERR_NOTFOUND:
result = CURLE_TFTP_NOTFOUND;
Daniel Stenberg
committed
break;
case TFTP_ERR_PERM:
result = CURLE_TFTP_PERM;
Daniel Stenberg
committed
break;
case TFTP_ERR_DISKFULL:
result = CURLE_REMOTE_DISK_FULL;
Daniel Stenberg
committed
break;
case TFTP_ERR_UNDEF:
case TFTP_ERR_ILLEGAL:
result = CURLE_TFTP_ILLEGAL;
Daniel Stenberg
committed
break;
case TFTP_ERR_UNKNOWNID:
result = CURLE_TFTP_UNKNOWNID;
Daniel Stenberg
committed
break;
case TFTP_ERR_EXISTS:
result = CURLE_REMOTE_FILE_EXISTS;
Daniel Stenberg
committed
break;
case TFTP_ERR_NOSUCHUSER:
result = CURLE_TFTP_NOSUCHUSER;
Daniel Stenberg
committed
break;
case TFTP_ERR_TIMEOUT:
result = CURLE_OPERATION_TIMEDOUT;
Daniel Stenberg
committed
break;
case TFTP_ERR_NORESPONSE:
result = CURLE_COULDNT_CONNECT;
Daniel Stenberg
committed
break;
default:
result = CURLE_ABORTED_BY_CALLBACK;
Daniel Stenberg
committed
break;
}
}
else
result = CURLE_OK;
Daniel Stenberg
committed
Daniel Stenberg
committed
}
/**********************************************************
*
* tftp_state_machine
*
* The tftp state machine event dispatcher
*
**********************************************************/
static CURLcode tftp_state_machine(tftp_state_data_t *state,
tftp_event_t event)
{
CURLcode result = CURLE_OK;
struct Curl_easy *data = state->conn->data;
switch(state->state) {
case TFTP_STATE_START:
DEBUGF(infof(data, "TFTP_STATE_START\n"));
result = tftp_send_first(state, event);
break;
case TFTP_STATE_RX:
DEBUGF(infof(data, "TFTP_STATE_RX\n"));
result = tftp_rx(state, event);
break;
case TFTP_STATE_TX:
DEBUGF(infof(data, "TFTP_STATE_TX\n"));
result = tftp_tx(state, event);
break;
case TFTP_STATE_FIN:
infof(data, "%s\n", "TFTP finished");
break;
default:
DEBUGF(infof(data, "STATE: %d\n", state->state));
Daniel Stenberg
committed
failf(data, "%s", "Internal state machine error");
result = CURLE_TFTP_ILLEGAL;
break;
}
}
Daniel Stenberg
committed
/**********************************************************
*
* tftp_disconnect
*
* The disconnect callback
*
**********************************************************/
static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection)
Daniel Stenberg
committed
{
tftp_state_data_t *state = conn->proto.tftpc;
(void) dead_connection;
Daniel Stenberg
committed
/* done, free dynamically allocated pkt buffers */
if(state) {
Curl_safefree(state->rpacket.data);
Curl_safefree(state->spacket.data);
free(state);
}
return CURLE_OK;
}
/**********************************************************
*
* tftp_connect
*
* The connect callback
*
**********************************************************/
static CURLcode tftp_connect(struct connectdata *conn, bool *done)
{
tftp_state_data_t *state;
Daniel Stenberg
committed
int blksize, rc;
blksize = TFTP_BLKSIZE_DEFAULT;
state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t));
Daniel Stenberg
committed
if(!state)
return CURLE_OUT_OF_MEMORY;
/* alloc pkt buffers based on specified blksize */
if(conn->data->set.tftp_blksize) {
blksize = (int)conn->data->set.tftp_blksize;
if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN)
Daniel Stenberg
committed
return CURLE_TFTP_ILLEGAL;
}
if(!state->rpacket.data) {
state->rpacket.data = calloc(1, blksize + 2 + 2);
if(!state->rpacket.data)
return CURLE_OUT_OF_MEMORY;
}
if(!state->spacket.data) {
state->spacket.data = calloc(1, blksize + 2 + 2);
if(!state->spacket.data)
/* we don't keep TFTP connections up basically because there's none or very
* little gain for UDP */
connclose(conn, "TFTP");
Daniel Stenberg
committed
state->conn = conn;
state->sockfd = state->conn->sock[FIRSTSOCKET];
state->state = TFTP_STATE_START;
state->error = TFTP_ERR_NONE;