2005-09-02 19:11:08 +04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2019-02-25 20:12:51 +03:00
|
|
|
* Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-09-02 19:11:08 +04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-03 02:19:02 +03:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2005-09-02 19:11:08 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 22:06:49 +04:00
|
|
|
#include "curl_setup.h"
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
#ifndef CURL_DISABLE_TFTP
|
|
|
|
|
2011-07-25 07:30:14 +04:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2005-09-02 19:11:08 +04:00
|
|
|
#include <netinet/in.h>
|
2011-07-25 07:30:14 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2005-09-02 19:11:08 +04:00
|
|
|
#include <netdb.h>
|
2011-07-25 07:30:14 +04:00
|
|
|
#endif
|
2005-09-02 19:11:08 +04:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NET_IF_H
|
|
|
|
#include <net/if.h>
|
|
|
|
#endif
|
2008-11-15 02:17:32 +03:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2005-09-02 19:11:08 +04:00
|
|
|
#include <sys/ioctl.h>
|
2008-11-15 02:17:32 +03:00
|
|
|
#endif
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "urldata.h"
|
2005-09-02 19:11:08 +04:00
|
|
|
#include <curl/curl.h>
|
2013-01-04 05:50:28 +04:00
|
|
|
#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"
|
2016-09-30 19:54:02 +03:00
|
|
|
#include "strcase.h"
|
2013-08-23 00:40:38 +04:00
|
|
|
#include "speedcheck.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "select.h"
|
2016-10-08 12:21:38 +03:00
|
|
|
#include "escape.h"
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2016-04-29 16:46:40 +03:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
2015-03-25 01:12:03 +03:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "memdebug.h"
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2009-01-27 01:43:06 +03:00
|
|
|
/* 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"
|
2010-05-19 01:14:00 +04:00
|
|
|
|
|
|
|
/* from RFC2349: */
|
|
|
|
#define TFTP_OPTION_TSIZE "tsize"
|
|
|
|
#define TFTP_OPTION_INTERVAL "timeout"
|
2006-05-08 23:41:26 +04:00
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
typedef enum {
|
2017-09-10 00:09:06 +03:00
|
|
|
TFTP_MODE_NETASCII = 0,
|
2005-09-02 19:11:08 +04:00
|
|
|
TFTP_MODE_OCTET
|
|
|
|
} tftp_mode_t;
|
|
|
|
|
|
|
|
typedef enum {
|
2017-09-10 00:09:06 +03:00
|
|
|
TFTP_STATE_START = 0,
|
2005-09-02 19:11:08 +04:00
|
|
|
TFTP_STATE_RX,
|
|
|
|
TFTP_STATE_TX,
|
|
|
|
TFTP_STATE_FIN
|
|
|
|
} tftp_state_t;
|
|
|
|
|
|
|
|
typedef enum {
|
2009-11-28 02:46:29 +03:00
|
|
|
TFTP_EVENT_NONE = -1,
|
|
|
|
TFTP_EVENT_INIT = 0,
|
2005-09-02 19:11:08 +04:00
|
|
|
TFTP_EVENT_RRQ = 1,
|
|
|
|
TFTP_EVENT_WRQ = 2,
|
|
|
|
TFTP_EVENT_DATA = 3,
|
|
|
|
TFTP_EVENT_ACK = 4,
|
|
|
|
TFTP_EVENT_ERROR = 5,
|
2009-01-27 01:43:06 +03:00
|
|
|
TFTP_EVENT_OACK = 6,
|
2005-09-02 19:11:08 +04:00
|
|
|
TFTP_EVENT_TIMEOUT
|
|
|
|
} tftp_event_t;
|
|
|
|
|
|
|
|
typedef enum {
|
2017-09-10 00:09:06 +03:00
|
|
|
TFTP_ERR_UNDEF = 0,
|
2005-09-02 19:11:08 +04:00
|
|
|
TFTP_ERR_NOTFOUND,
|
|
|
|
TFTP_ERR_PERM,
|
|
|
|
TFTP_ERR_DISKFULL,
|
|
|
|
TFTP_ERR_ILLEGAL,
|
|
|
|
TFTP_ERR_UNKNOWNID,
|
|
|
|
TFTP_ERR_EXISTS,
|
2008-02-08 01:25:04 +03:00
|
|
|
TFTP_ERR_NOSUCHUSER, /* This will never be triggered by this code */
|
2007-09-08 00:35:37 +04:00
|
|
|
|
|
|
|
/* The remaining error codes are internal to curl */
|
|
|
|
TFTP_ERR_NONE = -100,
|
2005-09-02 19:11:08 +04:00
|
|
|
TFTP_ERR_TIMEOUT,
|
|
|
|
TFTP_ERR_NORESPONSE
|
|
|
|
} tftp_error_t;
|
|
|
|
|
|
|
|
typedef struct tftp_packet {
|
2009-01-27 01:43:06 +03:00
|
|
|
unsigned char *data;
|
2005-09-02 19:11:08 +04:00
|
|
|
} tftp_packet_t;
|
|
|
|
|
|
|
|
typedef struct tftp_state_data {
|
|
|
|
tftp_state_t state;
|
|
|
|
tftp_mode_t mode;
|
|
|
|
tftp_error_t error;
|
2009-11-28 02:46:29 +03:00
|
|
|
tftp_event_t event;
|
2005-09-02 19:11:08 +04:00
|
|
|
struct connectdata *conn;
|
2005-09-06 14:39:01 +04:00
|
|
|
curl_socket_t sockfd;
|
2005-09-02 19:11:08 +04:00
|
|
|
int retries;
|
2009-11-28 02:46:29 +03:00
|
|
|
int retry_time;
|
|
|
|
int retry_max;
|
2005-09-02 19:11:08 +04:00
|
|
|
time_t start_time;
|
|
|
|
time_t max_time;
|
2009-11-28 02:46:29 +03:00
|
|
|
time_t rx_time;
|
2005-09-02 19:11:08 +04:00
|
|
|
unsigned short block;
|
2005-11-13 01:10:42 +03:00
|
|
|
struct Curl_sockaddr_storage local_addr;
|
|
|
|
struct Curl_sockaddr_storage remote_addr;
|
2009-11-28 07:34:46 +03:00
|
|
|
curl_socklen_t remote_addrlen;
|
2009-11-28 02:46:29 +03:00
|
|
|
int rbytes;
|
|
|
|
int sbytes;
|
|
|
|
int blksize;
|
2009-01-27 01:43:06 +03:00
|
|
|
int requested_blksize;
|
2005-09-02 19:11:08 +04:00
|
|
|
tftp_packet_t rpacket;
|
|
|
|
tftp_packet_t spacket;
|
|
|
|
} tftp_state_data_t;
|
|
|
|
|
|
|
|
|
|
|
|
/* Forward declarations */
|
2015-03-17 16:06:48 +03:00
|
|
|
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);
|
2007-12-09 01:50:55 +03:00
|
|
|
static CURLcode tftp_connect(struct connectdata *conn, bool *done);
|
2011-04-20 17:17:42 +04:00
|
|
|
static CURLcode tftp_disconnect(struct connectdata *conn,
|
|
|
|
bool dead_connection);
|
2007-12-09 01:50:55 +03:00
|
|
|
static CURLcode tftp_do(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode tftp_done(struct connectdata *conn,
|
2010-09-27 01:38:00 +04:00
|
|
|
CURLcode, bool premature);
|
2007-12-09 01:50:55 +03:00
|
|
|
static CURLcode tftp_setup_connection(struct connectdata * conn);
|
2009-11-28 02:46:29 +03:00
|
|
|
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);
|
2010-01-21 17:44:28 +03:00
|
|
|
static CURLcode tftp_translate_code(tftp_error_t error);
|
2007-10-12 17:36:37 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TFTP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_tftp = {
|
|
|
|
"TFTP", /* scheme */
|
2007-12-09 01:50:55 +03:00
|
|
|
tftp_setup_connection, /* setup_connection */
|
|
|
|
tftp_do, /* do_it */
|
|
|
|
tftp_done, /* done */
|
2007-10-17 20:58:32 +04:00
|
|
|
ZERO_NULL, /* do_more */
|
2007-12-09 01:50:55 +03:00
|
|
|
tftp_connect, /* connect_it */
|
2009-11-28 02:46:29 +03:00
|
|
|
tftp_multi_statemach, /* connecting */
|
|
|
|
tftp_doing, /* doing */
|
|
|
|
tftp_getsock, /* proto_getsock */
|
|
|
|
tftp_getsock, /* doing_getsock */
|
2011-10-22 01:36:54 +04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2008-12-20 00:14:52 +03:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
2009-01-27 01:43:06 +03:00
|
|
|
tftp_disconnect, /* disconnect */
|
2011-05-05 18:27:03 +04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 14:09:56 +03:00
|
|
|
ZERO_NULL, /* connection_check */
|
2007-10-12 17:36:37 +04:00
|
|
|
PORT_TFTP, /* defport */
|
2011-03-15 00:52:14 +03:00
|
|
|
CURLPROTO_TFTP, /* protocol */
|
2011-11-25 02:28:54 +04:00
|
|
|
PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
|
2007-10-12 17:36:37 +04:00
|
|
|
};
|
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2008-02-12 01:03:31 +03:00
|
|
|
static CURLcode tftp_set_timeouts(tftp_state_data_t *state)
|
2005-09-02 19:11:08 +04:00
|
|
|
{
|
2005-09-21 15:28:40 +04:00
|
|
|
time_t maxtime, timeout;
|
2017-10-26 16:24:50 +03:00
|
|
|
timediff_t timeout_ms;
|
2011-09-05 22:46:09 +04:00
|
|
|
bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
time(&state->start_time);
|
2008-02-08 01:25:04 +03:00
|
|
|
|
2008-02-12 01:03:31 +03:00
|
|
|
/* Compute drop-dead time */
|
2011-01-05 01:07:58 +03:00
|
|
|
timeout_ms = Curl_timeleft(state->conn->data, NULL, start);
|
2008-02-12 01:03:31 +03:00
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* time-out, bail out, go home */
|
|
|
|
failf(state->conn->data, "Connection time-out");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2009-01-14 02:24:06 +03:00
|
|
|
if(start) {
|
2008-02-12 01:03:31 +03:00
|
|
|
|
2008-02-08 01:25:04 +03:00
|
|
|
maxtime = (time_t)(timeout_ms + 500) / 1000;
|
2017-09-10 00:55:08 +03:00
|
|
|
state->max_time = state->start_time + maxtime;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
/* Set per-block timeout to total */
|
2015-03-17 16:06:48 +03:00
|
|
|
timeout = maxtime;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
/* Average restart after 5 seconds */
|
2009-11-28 02:46:29 +03:00
|
|
|
state->retry_max = (int)timeout/5;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2007-05-18 01:40:08 +04:00
|
|
|
if(state->retry_max < 1)
|
|
|
|
/* avoid division by zero below */
|
|
|
|
state->retry_max = 1;
|
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
/* Compute the re-start interval to suit the timeout */
|
2009-11-28 02:46:29 +03:00
|
|
|
state->retry_time = (int)timeout/state->retry_max;
|
2006-05-08 19:09:50 +04:00
|
|
|
if(state->retry_time<1)
|
2017-09-10 00:09:06 +03:00
|
|
|
state->retry_time = 1;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2008-02-08 01:25:04 +03:00
|
|
|
if(timeout_ms > 0)
|
|
|
|
maxtime = (time_t)(timeout_ms + 500) / 1000;
|
|
|
|
else
|
|
|
|
maxtime = 3600;
|
|
|
|
|
2017-09-10 00:55:08 +03:00
|
|
|
state->max_time = state->start_time + maxtime;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2011-10-13 21:45:36 +04:00
|
|
|
/* Set per-block timeout to total */
|
|
|
|
timeout = maxtime;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2011-10-13 21:45:36 +04:00
|
|
|
/* Average reposting an ACK after 5 seconds */
|
|
|
|
state->retry_max = (int)timeout/5;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2009-11-28 02:46:29 +03:00
|
|
|
/* But bound the total number */
|
2006-05-08 19:09:50 +04:00
|
|
|
if(state->retry_max<3)
|
2017-09-10 00:09:06 +03:00
|
|
|
state->retry_max = 3;
|
2006-05-08 19:09:50 +04:00
|
|
|
|
|
|
|
if(state->retry_max>50)
|
2017-09-10 00:09:06 +03:00
|
|
|
state->retry_max = 50;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
/* Compute the re-ACK interval to suit the timeout */
|
2010-03-27 01:33:02 +03:00
|
|
|
state->retry_time = (int)(timeout/state->retry_max);
|
2006-05-08 19:09:50 +04:00
|
|
|
if(state->retry_time<1)
|
2017-09-10 00:09:06 +03:00
|
|
|
state->retry_time = 1;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2008-02-11 21:27:36 +03:00
|
|
|
infof(state->conn->data,
|
2010-01-28 18:34:18 +03:00
|
|
|
"set timeouts for state %d; Total %ld, retry %d maxtry %d\n",
|
2010-02-04 22:44:31 +03:00
|
|
|
(int)state->state, (long)(state->max_time-state->start_time),
|
2005-09-02 19:11:08 +04:00
|
|
|
state->retry_time, state->retry_max);
|
2008-02-12 01:03:31 +03:00
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
/* init RX time */
|
|
|
|
time(&state->rx_time);
|
|
|
|
|
2008-02-12 01:03:31 +03:00
|
|
|
return CURLE_OK;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_set_send_first
|
|
|
|
*
|
|
|
|
* Event handler for the START state
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
|
2006-05-08 19:09:50 +04:00
|
|
|
static void setpacketevent(tftp_packet_t *packet, unsigned short num)
|
|
|
|
{
|
2006-07-20 01:14:02 +04:00
|
|
|
packet->data[0] = (unsigned char)(num >> 8);
|
|
|
|
packet->data[1] = (unsigned char)(num & 0xff);
|
2006-05-08 19:09:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setpacketblock(tftp_packet_t *packet, unsigned short num)
|
|
|
|
{
|
2006-07-20 01:14:02 +04:00
|
|
|
packet->data[2] = (unsigned char)(num >> 8);
|
|
|
|
packet->data[3] = (unsigned char)(num & 0xff);
|
2006-05-08 19:09:50 +04:00
|
|
|
}
|
|
|
|
|
2007-08-29 09:36:53 +04:00
|
|
|
static unsigned short getrpacketevent(const tftp_packet_t *packet)
|
2006-05-08 19:09:50 +04:00
|
|
|
{
|
2006-07-20 01:14:02 +04:00
|
|
|
return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
|
2006-05-08 19:09:50 +04:00
|
|
|
}
|
|
|
|
|
2007-08-29 09:36:53 +04:00
|
|
|
static unsigned short getrpacketblock(const tftp_packet_t *packet)
|
2006-05-08 19:09:50 +04:00
|
|
|
{
|
2006-07-20 01:14:02 +04:00
|
|
|
return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
|
2006-05-08 19:09:50 +04:00
|
|
|
}
|
|
|
|
|
2009-01-27 01:43:06 +03:00
|
|
|
static size_t Curl_strnlen(const char *string, size_t maxlen)
|
|
|
|
{
|
2016-12-14 01:34:59 +03:00
|
|
|
const char *end = memchr(string, '\0', maxlen);
|
2009-01-27 01:43:06 +03:00
|
|
|
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;
|
|
|
|
|
2016-04-03 21:28:34 +03:00
|
|
|
loc = Curl_strnlen(buf, len);
|
2009-01-27 01:43:06 +03:00
|
|
|
loc++; /* NULL term */
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
if(loc >= len)
|
2009-01-27 01:43:06 +03:00
|
|
|
return NULL;
|
|
|
|
*option = buf;
|
|
|
|
|
2017-09-10 00:55:08 +03:00
|
|
|
loc += Curl_strnlen(buf + loc, len-loc);
|
2009-01-27 01:43:06 +03:00
|
|
|
loc++; /* NULL term */
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
if(loc > len)
|
2009-01-27 01:43:06 +03:00
|
|
|
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;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = state->conn->data;
|
2009-01-27 01:43:06 +03:00
|
|
|
|
|
|
|
/* if OACK doesn't contain blksize option, the default (512) must be used */
|
|
|
|
state->blksize = TFTP_BLKSIZE_DEFAULT;
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
while(tmp < ptr + len) {
|
2009-01-27 01:43:06 +03:00
|
|
|
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)) {
|
2009-11-28 02:46:29 +03:00
|
|
|
long blksize;
|
2009-01-27 01:43:06 +03:00
|
|
|
|
2016-04-03 21:28:34 +03:00
|
|
|
blksize = strtol(value, NULL, 10);
|
2009-01-27 01:43:06 +03:00
|
|
|
|
|
|
|
if(!blksize) {
|
|
|
|
failf(data, "invalid blocksize value in OACK packet");
|
|
|
|
return CURLE_TFTP_ILLEGAL;
|
|
|
|
}
|
2017-03-10 16:28:37 +03:00
|
|
|
if(blksize > TFTP_BLKSIZE_MAX) {
|
2009-01-27 01:43:06 +03:00
|
|
|
failf(data, "%s (%d)", "blksize is larger than max supported",
|
2010-09-27 01:38:00 +04:00
|
|
|
TFTP_BLKSIZE_MAX);
|
2009-01-27 01:43:06 +03:00
|
|
|
return CURLE_TFTP_ILLEGAL;
|
|
|
|
}
|
|
|
|
else if(blksize < TFTP_BLKSIZE_MIN) {
|
|
|
|
failf(data, "%s (%d)", "blksize is smaller than min supported",
|
2010-09-27 01:38:00 +04:00
|
|
|
TFTP_BLKSIZE_MIN);
|
2009-01-27 01:43:06 +03:00
|
|
|
return CURLE_TFTP_ILLEGAL;
|
|
|
|
}
|
2011-04-20 17:17:42 +04:00
|
|
|
else if(blksize > state->requested_blksize) {
|
2009-01-27 01:43:06 +03:00
|
|
|
/* could realloc pkt buffers here, but the spec doesn't call out
|
|
|
|
* support for the server requesting a bigger blksize than the client
|
|
|
|
* requests */
|
2010-01-28 18:34:18 +03:00
|
|
|
failf(data, "%s (%ld)",
|
2010-09-27 01:38:00 +04:00
|
|
|
"server requested blksize larger than allocated", blksize);
|
2009-01-27 01:43:06 +03:00
|
|
|
return CURLE_TFTP_ILLEGAL;
|
|
|
|
}
|
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
state->blksize = (int)blksize;
|
2009-01-27 01:43:06 +03:00
|
|
|
infof(data, "%s (%d) %s (%d)\n", "blksize parsed from OACK",
|
2010-09-27 01:38:00 +04:00
|
|
|
state->blksize, "requested", state->requested_blksize);
|
2009-01-27 01:43:06 +03:00
|
|
|
}
|
|
|
|
else if(checkprefix(option, TFTP_OPTION_TSIZE)) {
|
|
|
|
long tsize = 0;
|
|
|
|
|
2016-04-03 21:28:34 +03:00
|
|
|
tsize = strtol(value, NULL, 10);
|
2010-01-28 18:34:18 +03:00
|
|
|
infof(data, "%s (%ld)\n", "tsize parsed from OACK", tsize);
|
2010-03-22 01:24:36 +03:00
|
|
|
|
|
|
|
/* tsize should be ignored on upload: Who cares about the size of the
|
|
|
|
remote file? */
|
2011-04-20 17:17:42 +04:00
|
|
|
if(!data->set.upload) {
|
2010-03-22 01:24:36 +03:00
|
|
|
if(!tsize) {
|
|
|
|
failf(data, "invalid tsize -:%s:- value in OACK packet", value);
|
|
|
|
return CURLE_TFTP_ILLEGAL;
|
|
|
|
}
|
|
|
|
Curl_pgrsSetDownloadSize(data, tsize);
|
|
|
|
}
|
2009-01-27 01:43:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t tftp_option_add(tftp_state_data_t *state, size_t csize,
|
|
|
|
char *buf, const char *option)
|
|
|
|
{
|
2016-04-03 21:28:34 +03:00
|
|
|
if(( strlen(option) + csize + 1) > (size_t)state->blksize)
|
2009-11-28 02:46:29 +03:00
|
|
|
return 0;
|
2009-01-27 01:43:06 +03:00
|
|
|
strcpy(buf, option);
|
2015-03-17 15:05:01 +03:00
|
|
|
return strlen(option) + 1;
|
2009-01-27 01:43:06 +03:00
|
|
|
}
|
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
static CURLcode tftp_connect_for_tx(tftp_state_data_t *state,
|
|
|
|
tftp_event_t event)
|
2009-01-27 01:43:06 +03:00
|
|
|
{
|
2014-10-24 00:56:35 +04:00
|
|
|
CURLcode result;
|
2009-05-28 20:18:25 +04:00
|
|
|
#ifndef CURL_DISABLE_VERBOSE_STRINGS
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = state->conn->data;
|
2009-01-27 01:43:06 +03:00
|
|
|
|
|
|
|
infof(data, "%s\n", "Connected for transmit");
|
2009-05-28 20:18:25 +04:00
|
|
|
#endif
|
2009-01-27 01:43:06 +03:00
|
|
|
state->state = TFTP_STATE_TX;
|
2014-10-24 00:56:35 +04:00
|
|
|
result = tftp_set_timeouts(state);
|
|
|
|
if(result)
|
2015-03-17 15:05:01 +03:00
|
|
|
return result;
|
2009-01-27 01:43:06 +03:00
|
|
|
return tftp_tx(state, event);
|
|
|
|
}
|
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
static CURLcode tftp_connect_for_rx(tftp_state_data_t *state,
|
|
|
|
tftp_event_t event)
|
2009-01-27 01:43:06 +03:00
|
|
|
{
|
2014-10-24 00:56:35 +04:00
|
|
|
CURLcode result;
|
2009-05-28 20:18:25 +04:00
|
|
|
#ifndef CURL_DISABLE_VERBOSE_STRINGS
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = state->conn->data;
|
2009-01-27 01:43:06 +03:00
|
|
|
|
|
|
|
infof(data, "%s\n", "Connected for receive");
|
2009-05-28 20:18:25 +04:00
|
|
|
#endif
|
2009-01-27 01:43:06 +03:00
|
|
|
state->state = TFTP_STATE_RX;
|
2014-10-24 00:56:35 +04:00
|
|
|
result = tftp_set_timeouts(state);
|
|
|
|
if(result)
|
2015-03-17 15:05:01 +03:00
|
|
|
return result;
|
2009-01-27 01:43:06 +03:00
|
|
|
return tftp_rx(state, event);
|
|
|
|
}
|
|
|
|
|
2006-05-09 02:23:33 +04:00
|
|
|
static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
|
2005-09-02 19:11:08 +04:00
|
|
|
{
|
2009-07-23 06:48:05 +04:00
|
|
|
size_t sbytes;
|
2009-11-28 02:46:29 +03:00
|
|
|
ssize_t senddata;
|
2005-09-02 19:11:08 +04:00
|
|
|
const char *mode = "octet";
|
2007-01-16 21:33:25 +03:00
|
|
|
char *filename;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = state->conn->data;
|
2015-02-05 23:31:12 +03:00
|
|
|
CURLcode result = CURLE_OK;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
/* Set ascii mode if -B flag was used */
|
2006-08-20 01:18:36 +04:00
|
|
|
if(data->set.prefer_ascii)
|
2005-09-02 19:11:08 +04:00
|
|
|
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;
|
2015-02-05 23:31:12 +03:00
|
|
|
return result;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(data->set.upload) {
|
|
|
|
/* If we are uploading, send an WRQ */
|
2006-05-08 19:09:50 +04:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
|
2007-11-25 02:16:55 +03:00
|
|
|
state->conn->data->req.upload_fromhere =
|
2017-09-10 00:55:08 +03:00
|
|
|
(char *)state->spacket.data + 4;
|
2014-04-15 15:49:18 +04:00
|
|
|
if(data->state.infilesize != -1)
|
|
|
|
Curl_pgrsSetUploadSize(data, data->state.infilesize);
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* If we are downloading, send an RRQ */
|
2006-05-08 19:09:50 +04:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2007-01-16 21:33:25 +03:00
|
|
|
/* As RFC3617 describes the separator slash is not actually part of the
|
2009-01-27 01:43:06 +03:00
|
|
|
file name so we skip the always-present first letter of the path
|
|
|
|
string. */
|
2018-09-15 00:33:28 +03:00
|
|
|
result = Curl_urldecode(data, &state->conn->data->state.up.path[1], 0,
|
2016-10-08 12:21:38 +03:00
|
|
|
&filename, NULL, FALSE);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2007-04-11 04:25:41 +04:00
|
|
|
|
2017-08-01 18:16:46 +03:00
|
|
|
if(strlen(filename) > (state->blksize - strlen(mode) - 4)) {
|
|
|
|
failf(data, "TFTP file name too long\n");
|
2017-08-19 17:33:32 +03:00
|
|
|
free(filename);
|
2017-08-01 18:16:46 +03:00
|
|
|
return CURLE_TFTP_ILLEGAL; /* too long file name field */
|
|
|
|
}
|
|
|
|
|
2018-11-22 11:01:24 +03:00
|
|
|
msnprintf((char *)state->spacket.data + 2,
|
|
|
|
state->blksize,
|
|
|
|
"%s%c%s%c", filename, '\0', mode, '\0');
|
2009-01-27 01:43:06 +03:00
|
|
|
sbytes = 4 + strlen(filename) + strlen(mode);
|
|
|
|
|
2015-08-17 18:54:47 +03:00
|
|
|
/* optional addition of TFTP options */
|
|
|
|
if(!data->set.tftp_no_options) {
|
2018-06-02 23:52:56 +03:00
|
|
|
char buf[64];
|
2015-08-17 18:54:47 +03:00
|
|
|
/* add tsize option */
|
|
|
|
if(data->set.upload && (data->state.infilesize != -1))
|
2018-11-22 11:01:24 +03:00
|
|
|
msnprintf(buf, sizeof(buf), "%" CURL_FORMAT_CURL_OFF_T,
|
|
|
|
data->state.infilesize);
|
2015-08-17 18:54:47 +03:00
|
|
|
else
|
|
|
|
strcpy(buf, "0"); /* the destination is large enough */
|
|
|
|
|
|
|
|
sbytes += tftp_option_add(state, sbytes,
|
2017-09-10 00:55:08 +03:00
|
|
|
(char *)state->spacket.data + sbytes,
|
2015-08-17 18:54:47 +03:00
|
|
|
TFTP_OPTION_TSIZE);
|
|
|
|
sbytes += tftp_option_add(state, sbytes,
|
2017-09-10 00:55:08 +03:00
|
|
|
(char *)state->spacket.data + sbytes, buf);
|
2015-08-17 18:54:47 +03:00
|
|
|
/* add blksize option */
|
2018-11-22 11:01:24 +03:00
|
|
|
msnprintf(buf, sizeof(buf), "%d", state->requested_blksize);
|
2015-08-17 18:54:47 +03:00
|
|
|
sbytes += tftp_option_add(state, sbytes,
|
2017-09-10 00:55:08 +03:00
|
|
|
(char *)state->spacket.data + sbytes,
|
2015-08-17 18:54:47 +03:00
|
|
|
TFTP_OPTION_BLKSIZE);
|
|
|
|
sbytes += tftp_option_add(state, sbytes,
|
2017-09-10 00:55:08 +03:00
|
|
|
(char *)state->spacket.data + sbytes, buf);
|
2015-08-17 18:54:47 +03:00
|
|
|
|
|
|
|
/* add timeout option */
|
2018-11-22 11:01:24 +03:00
|
|
|
msnprintf(buf, sizeof(buf), "%d", state->retry_time);
|
2015-08-17 18:54:47 +03:00
|
|
|
sbytes += tftp_option_add(state, sbytes,
|
2017-09-10 00:55:08 +03:00
|
|
|
(char *)state->spacket.data + sbytes,
|
2015-08-17 18:54:47 +03:00
|
|
|
TFTP_OPTION_INTERVAL);
|
|
|
|
sbytes += tftp_option_add(state, sbytes,
|
2017-09-10 00:55:08 +03:00
|
|
|
(char *)state->spacket.data + sbytes, buf);
|
2015-08-17 18:54:47 +03:00
|
|
|
}
|
2009-01-27 01:43:06 +03:00
|
|
|
|
2010-06-08 12:19:39 +04:00
|
|
|
/* 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' */
|
2009-11-28 02:46:29 +03:00
|
|
|
senddata = sendto(state->sockfd, (void *)state->spacket.data,
|
2010-06-08 12:19:39 +04:00
|
|
|
(SEND_TYPE_ARG3)sbytes, 0,
|
|
|
|
state->conn->ip_addr->ai_addr,
|
|
|
|
state->conn->ip_addr->ai_addrlen);
|
2009-11-28 02:46:29 +03:00
|
|
|
if(senddata != (ssize_t)sbytes) {
|
2019-02-25 20:12:51 +03:00
|
|
|
char buffer[STRERROR_LEN];
|
|
|
|
failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2015-03-16 17:01:15 +03:00
|
|
|
free(filename);
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
|
2009-01-27 01:43:06 +03:00
|
|
|
case TFTP_EVENT_OACK:
|
|
|
|
if(data->set.upload) {
|
2015-02-05 23:31:12 +03:00
|
|
|
result = tftp_connect_for_tx(state, event);
|
2009-01-27 01:43:06 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-02-05 23:31:12 +03:00
|
|
|
result = tftp_connect_for_rx(state, event);
|
2009-01-27 01:43:06 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
case TFTP_EVENT_ACK: /* Connected for transmit */
|
2015-02-05 23:31:12 +03:00
|
|
|
result = tftp_connect_for_tx(state, event);
|
2009-01-27 01:43:06 +03:00
|
|
|
break;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2009-01-27 01:43:06 +03:00
|
|
|
case TFTP_EVENT_DATA: /* Connected for receive */
|
2015-02-05 23:31:12 +03:00
|
|
|
result = tftp_connect_for_rx(state, event);
|
2009-01-27 01:43:06 +03:00
|
|
|
break;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
case TFTP_EVENT_ERROR:
|
|
|
|
state->state = TFTP_STATE_FIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-01-16 02:19:02 +03:00
|
|
|
failf(state->conn->data, "tftp_send_first: internal error");
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-02-05 23:31:12 +03:00
|
|
|
|
|
|
|
return result;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
|
2010-05-22 01:04:15 +04:00
|
|
|
/* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
|
|
|
|
boundary */
|
2017-09-10 00:55:08 +03:00
|
|
|
#define NEXT_BLOCKNUM(x) (((x) + 1)&0xffff)
|
2010-05-22 01:04:15 +04:00
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_rx
|
|
|
|
*
|
|
|
|
* Event handler for the RX state
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2006-05-09 02:23:33 +04:00
|
|
|
static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event)
|
2005-09-02 19:11:08 +04:00
|
|
|
{
|
2009-11-28 02:46:29 +03:00
|
|
|
ssize_t sbytes;
|
2005-09-02 19:11:08 +04:00
|
|
|
int rblock;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = state->conn->data;
|
2019-02-25 20:12:51 +03:00
|
|
|
char buffer[STRERROR_LEN];
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
switch(event) {
|
|
|
|
|
|
|
|
case TFTP_EVENT_DATA:
|
|
|
|
/* Is this the block we expect? */
|
2006-05-08 19:09:50 +04:00
|
|
|
rblock = getrpacketblock(&state->rpacket);
|
2012-11-17 01:00:17 +04:00
|
|
|
if(NEXT_BLOCKNUM(state->block) == rblock) {
|
2012-11-16 18:30:52 +04:00
|
|
|
/* This is the expected block. Reset counters and ACK it. */
|
|
|
|
state->retries = 0;
|
|
|
|
}
|
2012-11-17 01:00:17 +04:00
|
|
|
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);
|
2012-11-16 18:30:52 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* totally unexpected, just log it */
|
2005-09-16 00:21:27 +04:00
|
|
|
infof(data,
|
2011-10-13 21:45:36 +04:00
|
|
|
"Received unexpected DATA packet block %d, expecting block %d\n",
|
|
|
|
rblock, NEXT_BLOCKNUM(state->block));
|
2010-05-22 01:07:59 +04:00
|
|
|
break;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2012-11-16 18:30:52 +04:00
|
|
|
|
|
|
|
/* ACK this block. */
|
2006-07-20 01:14:02 +04:00
|
|
|
state->block = (unsigned short)rblock;
|
2006-05-08 19:09:50 +04:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_ACK);
|
|
|
|
setpacketblock(&state->spacket, state->block);
|
2009-11-28 02:46:29 +03:00
|
|
|
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
|
|
|
|
4, SEND_4TH_ARG,
|
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
|
|
|
if(sbytes < 0) {
|
2019-02-25 20:12:51 +03:00
|
|
|
failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2007-05-18 14:12:57 +04:00
|
|
|
return CURLE_SEND_ERROR;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
|
2006-05-08 23:41:26 +04:00
|
|
|
/* Check if completed (That is, a less than full packet is received) */
|
2017-09-10 00:55:08 +03:00
|
|
|
if(state->rbytes < (ssize_t)state->blksize + 4) {
|
2005-09-02 19:11:08 +04:00
|
|
|
state->state = TFTP_STATE_FIN;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state->state = TFTP_STATE_RX;
|
|
|
|
}
|
2009-11-28 02:46:29 +03:00
|
|
|
time(&state->rx_time);
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
|
2009-01-27 01:43:06 +03:00
|
|
|
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);
|
2009-11-28 02:46:29 +03:00
|
|
|
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
|
|
|
|
4, SEND_4TH_ARG,
|
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
|
|
|
if(sbytes < 0) {
|
2019-02-25 20:12:51 +03:00
|
|
|
failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2009-01-27 01:43:06 +03:00
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we're ready to RX data */
|
|
|
|
state->state = TFTP_STATE_RX;
|
2009-11-28 02:46:29 +03:00
|
|
|
time(&state->rx_time);
|
2009-01-27 01:43:06 +03:00
|
|
|
break;
|
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
case TFTP_EVENT_TIMEOUT:
|
|
|
|
/* Increment the retry count and fail if over the limit */
|
|
|
|
state->retries++;
|
2005-09-16 00:21:27 +04:00
|
|
|
infof(data,
|
2010-01-28 18:34:18 +03:00
|
|
|
"Timeout waiting for block %d ACK. Retries = %d\n",
|
2010-05-22 01:04:15 +04:00
|
|
|
NEXT_BLOCKNUM(state->block), state->retries);
|
2005-09-02 19:11:08 +04:00
|
|
|
if(state->retries > state->retry_max) {
|
|
|
|
state->error = TFTP_ERR_TIMEOUT;
|
|
|
|
state->state = TFTP_STATE_FIN;
|
2006-05-09 02:23:33 +04:00
|
|
|
}
|
|
|
|
else {
|
2009-11-28 02:46:29 +03:00
|
|
|
/* 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) {
|
2019-02-25 20:12:51 +03:00
|
|
|
failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2007-05-18 14:12:57 +04:00
|
|
|
return CURLE_SEND_ERROR;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_EVENT_ERROR:
|
2009-11-28 02:46:29 +03:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
|
|
|
|
setpacketblock(&state->spacket, state->block);
|
2010-04-17 00:55:07 +04:00
|
|
|
(void)sendto(state->sockfd, (void *)state->spacket.data,
|
|
|
|
4, SEND_4TH_ARG,
|
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
2009-11-28 02:46:29 +03:00
|
|
|
/* 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 */
|
2005-09-02 19:11:08 +04:00
|
|
|
state->state = TFTP_STATE_FIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-01-16 02:19:02 +03:00
|
|
|
failf(data, "%s", "tftp_rx: internal error");
|
2007-05-18 14:12:57 +04:00
|
|
|
return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
|
|
|
|
this */
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2006-05-09 02:23:33 +04:00
|
|
|
return CURLE_OK;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_tx
|
|
|
|
*
|
|
|
|
* Event handler for the TX state
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2006-05-09 02:23:33 +04:00
|
|
|
static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
|
2005-09-02 19:11:08 +04:00
|
|
|
{
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = state->conn->data;
|
2009-11-28 02:46:29 +03:00
|
|
|
ssize_t sbytes;
|
2015-02-05 23:31:12 +03:00
|
|
|
CURLcode result = CURLE_OK;
|
2007-11-25 02:16:55 +03:00
|
|
|
struct SingleRequest *k = &data->req;
|
2018-08-31 11:17:40 +03:00
|
|
|
size_t cb; /* Bytes currently read */
|
2019-02-25 20:12:51 +03:00
|
|
|
char buffer[STRERROR_LEN];
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
switch(event) {
|
|
|
|
|
|
|
|
case TFTP_EVENT_ACK:
|
2010-03-02 01:14:47 +03:00
|
|
|
case TFTP_EVENT_OACK:
|
2011-04-20 17:17:42 +04:00
|
|
|
if(event == TFTP_EVENT_ACK) {
|
2010-09-27 01:38:00 +04:00
|
|
|
/* Ack the packet */
|
2018-06-02 23:52:56 +03:00
|
|
|
int rblock = getrpacketblock(&state->rpacket);
|
2010-09-27 01:38:00 +04:00
|
|
|
|
|
|
|
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);
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_SEND_ERROR;
|
2010-09-27 01:38:00 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Re-send the data packet */
|
2012-01-13 02:13:19 +04:00
|
|
|
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
|
2017-09-10 00:55:08 +03:00
|
|
|
4 + state->sbytes, SEND_4TH_ARG,
|
2010-09-27 01:38:00 +04:00
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
|
|
|
/* Check all sbytes were sent */
|
|
|
|
if(sbytes<0) {
|
2019-02-25 20:12:51 +03:00
|
|
|
failf(data, "%s", Curl_strerror(SOCKERRNO,
|
|
|
|
buffer, sizeof(buffer)));
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_SEND_ERROR;
|
2010-03-02 01:14:47 +03:00
|
|
|
}
|
2010-09-27 01:38:00 +04:00
|
|
|
}
|
2015-02-05 23:31:12 +03:00
|
|
|
|
|
|
|
return result;
|
2010-09-27 01:38:00 +04:00
|
|
|
}
|
|
|
|
/* This is the expected packet. Reset the counters and send the next
|
|
|
|
block */
|
|
|
|
time(&state->rx_time);
|
|
|
|
state->block++;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2010-09-27 01:38:00 +04:00
|
|
|
else
|
|
|
|
state->block = 1; /* first data block is 1 when using OACK */
|
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
state->retries = 0;
|
2006-05-08 19:09:50 +04:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_DATA);
|
|
|
|
setpacketblock(&state->spacket, state->block);
|
2018-08-31 11:17:40 +03:00
|
|
|
if(state->block > 1 && state->sbytes < state->blksize) {
|
2005-09-02 19:11:08 +04:00
|
|
|
state->state = TFTP_STATE_FIN;
|
2006-05-09 02:23:33 +04:00
|
|
|
return CURLE_OK;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2015-02-05 23:31:12 +03:00
|
|
|
|
2016-08-03 01:29:09 +03:00
|
|
|
/* 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;
|
2017-09-10 00:55:08 +03:00
|
|
|
state->conn->data->req.upload_fromhere = (char *)state->spacket.data + 4;
|
2016-08-03 01:29:09 +03:00
|
|
|
do {
|
|
|
|
result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes,
|
|
|
|
&cb);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2018-08-31 11:17:40 +03:00
|
|
|
state->sbytes += (int)cb;
|
2016-08-03 01:29:09 +03:00
|
|
|
state->conn->data->req.upload_fromhere += cb;
|
|
|
|
} while(state->sbytes < state->blksize && cb != 0);
|
2015-02-05 23:31:12 +03:00
|
|
|
|
|
|
|
sbytes = sendto(state->sockfd, (void *) state->spacket.data,
|
|
|
|
4 + state->sbytes, SEND_4TH_ARG,
|
2009-11-28 02:46:29 +03:00
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
|
|
|
/* Check all sbytes were sent */
|
|
|
|
if(sbytes<0) {
|
2019-02-25 20:12:51 +03:00
|
|
|
failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2007-05-18 14:32:48 +04:00
|
|
|
return CURLE_SEND_ERROR;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2007-05-18 14:40:43 +04:00
|
|
|
/* Update the progress meter */
|
|
|
|
k->writebytecount += state->sbytes;
|
|
|
|
Curl_pgrsSetUploadCounter(data, k->writebytecount);
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_EVENT_TIMEOUT:
|
|
|
|
/* Increment the retry counter and log the timeout */
|
|
|
|
state->retries++;
|
2005-09-16 00:21:27 +04:00
|
|
|
infof(data, "Timeout waiting for block %d ACK. "
|
2010-05-22 01:04:15 +04:00
|
|
|
" Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries);
|
2005-09-02 19:11:08 +04:00
|
|
|
/* Decide if we've had enough */
|
|
|
|
if(state->retries > state->retry_max) {
|
|
|
|
state->error = TFTP_ERR_TIMEOUT;
|
|
|
|
state->state = TFTP_STATE_FIN;
|
2007-05-18 14:32:48 +04:00
|
|
|
}
|
|
|
|
else {
|
2009-11-28 02:46:29 +03:00
|
|
|
/* Re-send the data packet */
|
|
|
|
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
|
2017-09-10 00:55:08 +03:00
|
|
|
4 + state->sbytes, SEND_4TH_ARG,
|
2009-11-28 02:46:29 +03:00
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
|
|
|
/* Check all sbytes were sent */
|
|
|
|
if(sbytes<0) {
|
2019-02-25 20:12:51 +03:00
|
|
|
failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2007-05-18 14:32:48 +04:00
|
|
|
return CURLE_SEND_ERROR;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2007-05-18 14:40:43 +04:00
|
|
|
/* since this was a re-send, we remain at the still byte position */
|
|
|
|
Curl_pgrsSetUploadCounter(data, k->writebytecount);
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_EVENT_ERROR:
|
|
|
|
state->state = TFTP_STATE_FIN;
|
2009-11-28 02:46:29 +03:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
|
|
|
|
setpacketblock(&state->spacket, state->block);
|
2010-04-17 01:43:04 +04:00
|
|
|
(void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
|
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
2009-11-28 02:46:29 +03:00
|
|
|
/* 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;
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-03-02 01:14:47 +03:00
|
|
|
failf(data, "tftp_tx: internal error, event: %i", (int)(event));
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-02-05 23:31:12 +03:00
|
|
|
return result;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_translate_code
|
|
|
|
*
|
|
|
|
* Translate internal error codes to CURL error codes
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2010-01-21 17:44:28 +03:00
|
|
|
static CURLcode tftp_translate_code(tftp_error_t error)
|
2009-11-28 02:46:29 +03:00
|
|
|
{
|
2015-02-05 23:31:12 +03:00
|
|
|
CURLcode result = CURLE_OK;
|
2009-11-28 02:46:29 +03:00
|
|
|
|
|
|
|
if(error != TFTP_ERR_NONE) {
|
|
|
|
switch(error) {
|
|
|
|
case TFTP_ERR_NOTFOUND:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_TFTP_NOTFOUND;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_PERM:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_TFTP_PERM;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_DISKFULL:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_REMOTE_DISK_FULL;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_UNDEF:
|
|
|
|
case TFTP_ERR_ILLEGAL:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_TFTP_ILLEGAL;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_UNKNOWNID:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_TFTP_UNKNOWNID;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_EXISTS:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_REMOTE_FILE_EXISTS;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_NOSUCHUSER:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_TFTP_NOSUCHUSER;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_TIMEOUT:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_OPERATION_TIMEDOUT;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_NORESPONSE:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_COULDNT_CONNECT;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
default:
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_ABORTED_BY_CALLBACK;
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 23:31:12 +03:00
|
|
|
else
|
|
|
|
result = CURLE_OK;
|
2009-11-28 02:46:29 +03:00
|
|
|
|
2015-02-05 23:31:12 +03:00
|
|
|
return result;
|
2009-11-28 02:46:29 +03:00
|
|
|
}
|
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_state_machine
|
|
|
|
*
|
|
|
|
* The tftp state machine event dispatcher
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
static CURLcode tftp_state_machine(tftp_state_data_t *state,
|
|
|
|
tftp_event_t event)
|
|
|
|
{
|
2015-02-05 23:31:12 +03:00
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = state->conn->data;
|
2015-02-05 23:31:12 +03:00
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
switch(state->state) {
|
|
|
|
case TFTP_STATE_START:
|
2005-09-16 00:21:27 +04:00
|
|
|
DEBUGF(infof(data, "TFTP_STATE_START\n"));
|
2015-02-05 23:31:12 +03:00
|
|
|
result = tftp_send_first(state, event);
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
case TFTP_STATE_RX:
|
2005-09-16 00:21:27 +04:00
|
|
|
DEBUGF(infof(data, "TFTP_STATE_RX\n"));
|
2015-02-05 23:31:12 +03:00
|
|
|
result = tftp_rx(state, event);
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
case TFTP_STATE_TX:
|
2005-09-16 00:21:27 +04:00
|
|
|
DEBUGF(infof(data, "TFTP_STATE_TX\n"));
|
2015-02-05 23:31:12 +03:00
|
|
|
result = tftp_tx(state, event);
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
case TFTP_STATE_FIN:
|
2005-09-16 00:21:27 +04:00
|
|
|
infof(data, "%s\n", "TFTP finished");
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
default:
|
2005-09-16 00:21:27 +04:00
|
|
|
DEBUGF(infof(data, "STATE: %d\n", state->state));
|
2008-01-16 02:19:02 +03:00
|
|
|
failf(data, "%s", "Internal state machine error");
|
2015-02-05 23:31:12 +03:00
|
|
|
result = CURLE_TFTP_ILLEGAL;
|
2005-09-02 19:11:08 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-02-05 23:31:12 +03:00
|
|
|
|
|
|
|
return result;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
|
2009-01-27 01:43:06 +03:00
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_disconnect
|
|
|
|
*
|
|
|
|
* The disconnect callback
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2010-11-19 15:43:20 +03:00
|
|
|
static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection)
|
2009-01-27 01:43:06 +03:00
|
|
|
{
|
|
|
|
tftp_state_data_t *state = conn->proto.tftpc;
|
2010-11-19 15:43:20 +03:00
|
|
|
(void) dead_connection;
|
2009-01-27 01:43:06 +03:00
|
|
|
|
|
|
|
/* done, free dynamically allocated pkt buffers */
|
|
|
|
if(state) {
|
|
|
|
Curl_safefree(state->rpacket.data);
|
|
|
|
Curl_safefree(state->spacket.data);
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
2007-12-09 01:50:55 +03:00
|
|
|
* tftp_connect
|
2005-09-02 19:11:08 +04:00
|
|
|
*
|
|
|
|
* The connect callback
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2007-12-09 01:50:55 +03:00
|
|
|
static CURLcode tftp_connect(struct connectdata *conn, bool *done)
|
2005-09-02 19:11:08 +04:00
|
|
|
{
|
2007-10-25 11:47:38 +04:00
|
|
|
tftp_state_data_t *state;
|
2018-06-02 23:52:56 +03:00
|
|
|
int blksize;
|
2009-01-27 01:43:06 +03:00
|
|
|
|
|
|
|
blksize = TFTP_BLKSIZE_DEFAULT;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2009-11-18 13:33:54 +03:00
|
|
|
state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t));
|
2009-01-27 01:43:06 +03:00
|
|
|
if(!state)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* alloc pkt buffers based on specified blksize */
|
|
|
|
if(conn->data->set.tftp_blksize) {
|
2009-07-25 02:06:19 +04:00
|
|
|
blksize = (int)conn->data->set.tftp_blksize;
|
2016-04-03 21:28:34 +03:00
|
|
|
if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN)
|
2009-01-27 01:43:06 +03:00
|
|
|
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)
|
2007-10-25 13:34:16 +04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2015-08-24 07:15:01 +03:00
|
|
|
/* we don't keep TFTP connections up basically because there's none or very
|
2014-05-20 12:32:23 +04:00
|
|
|
* little gain for UDP */
|
|
|
|
connclose(conn, "TFTP");
|
2006-11-10 00:36:18 +03:00
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
state->conn = conn;
|
|
|
|
state->sockfd = state->conn->sock[FIRSTSOCKET];
|
|
|
|
state->state = TFTP_STATE_START;
|
2007-09-08 00:35:37 +04:00
|
|
|
state->error = TFTP_ERR_NONE;
|
2009-01-27 01:43:06 +03:00
|
|
|
state->blksize = TFTP_BLKSIZE_DEFAULT;
|
|
|
|
state->requested_blksize = blksize;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2006-07-29 20:17:36 +04:00
|
|
|
((struct sockaddr *)&state->local_addr)->sa_family =
|
2018-04-06 16:26:04 +03:00
|
|
|
(CURL_SA_FAMILY_T)(conn->ip_addr->ai_family);
|
2005-09-06 19:43:14 +04:00
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
tftp_set_timeouts(state);
|
|
|
|
|
2007-10-04 14:01:41 +04:00
|
|
|
if(!conn->bits.bound) {
|
|
|
|
/* If not already bound, bind to any interface, random UDP port. If it is
|
|
|
|
* reused or a custom local port was desired, this has already been done!
|
2006-11-10 00:36:18 +03:00
|
|
|
*
|
2009-11-28 02:46:29 +03:00
|
|
|
* We once used the size of the local_addr struct as the third argument
|
|
|
|
* for bind() to better work with IPv6 or whatever size the struct could
|
|
|
|
* have, but we learned that at least Tru64, AIX and IRIX *requires* the
|
|
|
|
* size of that argument to match the exact size of a 'sockaddr_in' struct
|
|
|
|
* when running IPv4-only.
|
2006-11-10 00:36:18 +03:00
|
|
|
*
|
|
|
|
* Therefore we use the size from the address we connected to, which we
|
|
|
|
* assume uses the same IP version and thus hopefully this works for both
|
|
|
|
* IPv4 and IPv6...
|
|
|
|
*/
|
2018-06-02 23:52:56 +03:00
|
|
|
int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
|
|
|
|
conn->ip_addr->ai_addrlen);
|
2006-11-10 00:36:18 +03:00
|
|
|
if(rc) {
|
2019-02-25 20:12:51 +03:00
|
|
|
char buffer[STRERROR_LEN];
|
2008-01-16 02:19:02 +03:00
|
|
|
failf(conn->data, "bind() failed; %s",
|
2019-02-25 20:12:51 +03:00
|
|
|
Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2006-11-10 00:36:18 +03:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2007-10-04 14:01:41 +04:00
|
|
|
conn->bits.bound = TRUE;
|
2005-09-04 22:33:20 +04:00
|
|
|
}
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
Curl_pgrsStartNow(conn->data);
|
|
|
|
|
|
|
|
*done = TRUE;
|
2015-02-05 23:31:12 +03:00
|
|
|
|
|
|
|
return CURLE_OK;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
2007-12-09 01:50:55 +03:00
|
|
|
* tftp_done
|
2005-09-02 19:11:08 +04:00
|
|
|
*
|
|
|
|
* The done callback
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2007-12-09 01:50:55 +03:00
|
|
|
static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
|
2010-09-27 01:38:00 +04:00
|
|
|
bool premature)
|
2005-09-02 19:11:08 +04:00
|
|
|
{
|
2015-02-05 23:31:12 +03:00
|
|
|
CURLcode result = CURLE_OK;
|
2009-11-28 02:46:29 +03:00
|
|
|
tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
|
|
|
|
|
2005-09-02 19:11:08 +04:00
|
|
|
(void)status; /* unused */
|
2007-01-17 01:22:10 +03:00
|
|
|
(void)premature; /* not used */
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2012-06-11 01:39:04 +04:00
|
|
|
if(Curl_pgrsDone(conn))
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
/* If we have encountered an error */
|
2014-01-28 23:39:25 +04:00
|
|
|
if(state)
|
2015-02-05 23:31:12 +03:00
|
|
|
result = tftp_translate_code(state->error);
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2015-02-05 23:31:12 +03:00
|
|
|
return result;
|
2009-11-28 02:46:29 +03:00
|
|
|
}
|
2005-09-02 19:11:08 +04:00
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
2009-11-28 02:46:29 +03:00
|
|
|
* tftp_getsock
|
2005-09-02 19:11:08 +04:00
|
|
|
*
|
2009-11-28 02:46:29 +03:00
|
|
|
* The getsock callback
|
2005-09-02 19:11:08 +04:00
|
|
|
*
|
|
|
|
**********************************************************/
|
2009-11-28 02:46:29 +03:00
|
|
|
static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks,
|
2010-09-27 01:38:00 +04:00
|
|
|
int numsocks)
|
2009-11-28 02:46:29 +03:00
|
|
|
{
|
|
|
|
if(!numsocks)
|
|
|
|
return GETSOCK_BLANK;
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
socks[0] = conn->sock[FIRSTSOCKET];
|
|
|
|
|
|
|
|
return GETSOCK_READSOCK(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_receive_packet
|
|
|
|
*
|
|
|
|
* Called once select fires and data is ready on the socket
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
static CURLcode tftp_receive_packet(struct connectdata *conn)
|
2005-09-02 19:11:08 +04:00
|
|
|
{
|
2005-11-13 01:10:42 +03:00
|
|
|
struct Curl_sockaddr_storage fromaddr;
|
2009-11-28 07:34:46 +03:00
|
|
|
curl_socklen_t fromlen;
|
2009-11-28 02:46:29 +03:00
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = conn->data;
|
2009-11-28 02:46:29 +03:00
|
|
|
tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
|
|
|
|
struct SingleRequest *k = &data->req;
|
|
|
|
|
|
|
|
/* Receive the packet */
|
|
|
|
fromlen = sizeof(fromaddr);
|
|
|
|
state->rbytes = (int)recvfrom(state->sockfd,
|
2010-09-27 01:38:00 +04:00
|
|
|
(void *)state->rpacket.data,
|
2017-09-10 00:55:08 +03:00
|
|
|
state->blksize + 4,
|
2010-09-27 01:38:00 +04:00
|
|
|
0,
|
|
|
|
(struct sockaddr *)&fromaddr,
|
|
|
|
&fromlen);
|
2017-09-10 00:09:06 +03:00
|
|
|
if(state->remote_addrlen == 0) {
|
2009-11-28 02:46:29 +03:00
|
|
|
memcpy(&state->remote_addr, &fromaddr, fromlen);
|
|
|
|
state->remote_addrlen = fromlen;
|
|
|
|
}
|
2005-09-02 19:11:08 +04:00
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
/* Sanity check packet length */
|
|
|
|
if(state->rbytes < 4) {
|
|
|
|
failf(data, "Received too short packet");
|
|
|
|
/* Not a timeout, but how best to handle it? */
|
|
|
|
state->event = TFTP_EVENT_TIMEOUT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* The event is given by the TFTP packet time */
|
2017-05-07 17:26:19 +03:00
|
|
|
unsigned short event = getrpacketevent(&state->rpacket);
|
|
|
|
state->event = (tftp_event_t)event;
|
2009-11-28 02:46:29 +03:00
|
|
|
|
|
|
|
switch(state->event) {
|
|
|
|
case TFTP_EVENT_DATA:
|
|
|
|
/* Don't pass to the client empty or retransmitted packets */
|
|
|
|
if(state->rbytes > 4 &&
|
2010-05-22 01:04:15 +04:00
|
|
|
(NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
|
2009-11-28 02:46:29 +03:00
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_BODY,
|
2017-09-10 00:55:08 +03:00
|
|
|
(char *)state->rpacket.data + 4,
|
2010-05-22 01:04:15 +04:00
|
|
|
state->rbytes-4);
|
2009-11-28 02:46:29 +03:00
|
|
|
if(result) {
|
|
|
|
tftp_state_machine(state, TFTP_EVENT_ERROR);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
k->bytecount += state->rbytes-4;
|
|
|
|
Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TFTP_EVENT_ERROR:
|
2017-05-07 17:26:19 +03:00
|
|
|
{
|
|
|
|
unsigned short error = getrpacketblock(&state->rpacket);
|
2018-05-22 11:10:39 +03:00
|
|
|
char *str = (char *)state->rpacket.data + 4;
|
|
|
|
size_t strn = state->rbytes - 4;
|
2017-05-07 17:26:19 +03:00
|
|
|
state->error = (tftp_error_t)error;
|
2018-05-22 11:10:39 +03:00
|
|
|
if(Curl_strnlen(str, strn) < strn)
|
|
|
|
infof(data, "TFTP error: %s\n", str);
|
2009-11-28 02:46:29 +03:00
|
|
|
break;
|
2017-05-07 17:26:19 +03:00
|
|
|
}
|
2009-11-28 02:46:29 +03:00
|
|
|
case TFTP_EVENT_ACK:
|
|
|
|
break;
|
|
|
|
case TFTP_EVENT_OACK:
|
|
|
|
result = tftp_parse_option_ack(state,
|
2017-09-10 00:55:08 +03:00
|
|
|
(const char *)state->rpacket.data + 2,
|
2010-09-27 01:38:00 +04:00
|
|
|
state->rbytes-2);
|
2009-11-28 02:46:29 +03:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
break;
|
|
|
|
case TFTP_EVENT_RRQ:
|
|
|
|
case TFTP_EVENT_WRQ:
|
|
|
|
default:
|
|
|
|
failf(data, "%s", "Internal error: Unexpected packet");
|
|
|
|
break;
|
|
|
|
}
|
2006-11-10 00:36:18 +03:00
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
/* Update the progress meter */
|
|
|
|
if(Curl_pgrsUpdate(conn)) {
|
|
|
|
tftp_state_machine(state, TFTP_EVENT_ERROR);
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2007-11-15 01:41:42 +03:00
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_state_timeout
|
|
|
|
*
|
|
|
|
* Check if timeouts have been reached
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event)
|
|
|
|
{
|
|
|
|
time_t current;
|
|
|
|
tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
if(event)
|
2009-11-28 02:46:29 +03:00
|
|
|
*event = TFTP_EVENT_NONE;
|
|
|
|
|
|
|
|
time(¤t);
|
|
|
|
if(current > state->max_time) {
|
2010-01-28 18:34:18 +03:00
|
|
|
DEBUGF(infof(conn->data, "timeout: %ld > %ld\n",
|
|
|
|
(long)current, (long)state->max_time));
|
2009-11-28 02:46:29 +03:00
|
|
|
state->error = TFTP_ERR_TIMEOUT;
|
|
|
|
state->state = TFTP_STATE_FIN;
|
2010-08-11 02:06:20 +04:00
|
|
|
return 0;
|
2006-11-10 00:36:18 +03:00
|
|
|
}
|
2017-09-10 00:55:08 +03:00
|
|
|
if(current > state->rx_time + state->retry_time) {
|
2011-04-20 17:17:42 +04:00
|
|
|
if(event)
|
2009-11-28 02:46:29 +03:00
|
|
|
*event = TFTP_EVENT_TIMEOUT;
|
|
|
|
time(&state->rx_time); /* update even though we received nothing */
|
|
|
|
}
|
2010-08-11 02:06:20 +04:00
|
|
|
|
|
|
|
/* there's a typecast below here since 'time_t' may in fact be larger than
|
|
|
|
'long', but we estimate that a 'long' will still be able to hold number
|
|
|
|
of seconds even if "only" 32 bit */
|
2010-09-03 22:35:34 +04:00
|
|
|
return (long)(state->max_time - current);
|
2009-11-28 02:46:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_multi_statemach
|
|
|
|
*
|
|
|
|
* Handle single RX socket event and return
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
|
|
|
|
{
|
|
|
|
tftp_event_t event;
|
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = conn->data;
|
2009-11-28 02:46:29 +03:00
|
|
|
tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
|
|
|
|
long timeout_ms = tftp_state_timeout(conn, &event);
|
|
|
|
|
|
|
|
*done = FALSE;
|
|
|
|
|
|
|
|
if(timeout_ms <= 0) {
|
|
|
|
failf(data, "TFTP response timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
2017-03-10 16:28:37 +03:00
|
|
|
if(event != TFTP_EVENT_NONE) {
|
2009-11-28 02:46:29 +03:00
|
|
|
result = tftp_state_machine(state, event);
|
2014-10-24 00:56:35 +04:00
|
|
|
if(result)
|
2015-03-17 15:05:01 +03:00
|
|
|
return result;
|
2011-09-05 22:46:09 +04:00
|
|
|
*done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
|
2009-11-28 02:46:29 +03:00
|
|
|
if(*done)
|
|
|
|
/* Tell curl we're done */
|
2019-02-28 13:36:26 +03:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2009-11-28 02:46:29 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* no timeouts to handle, check our socket */
|
2018-06-02 23:52:56 +03:00
|
|
|
int rc = SOCKET_READABLE(state->sockfd, 0);
|
2009-11-28 02:46:29 +03:00
|
|
|
|
|
|
|
if(rc == -1) {
|
|
|
|
/* bail out */
|
|
|
|
int error = SOCKERRNO;
|
2019-02-25 20:12:51 +03:00
|
|
|
char buffer[STRERROR_LEN];
|
|
|
|
failf(data, "%s", Curl_strerror(error, buffer, sizeof(buffer)));
|
2009-11-28 02:46:29 +03:00
|
|
|
state->event = TFTP_EVENT_ERROR;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2009-11-28 02:46:29 +03:00
|
|
|
else if(rc != 0) {
|
|
|
|
result = tftp_receive_packet(conn);
|
2014-10-24 00:56:35 +04:00
|
|
|
if(result)
|
2015-03-17 15:05:01 +03:00
|
|
|
return result;
|
2009-11-28 02:46:29 +03:00
|
|
|
result = tftp_state_machine(state, state->event);
|
2014-10-24 00:56:35 +04:00
|
|
|
if(result)
|
2015-03-17 15:05:01 +03:00
|
|
|
return result;
|
2011-09-05 22:46:09 +04:00
|
|
|
*done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
|
2009-11-28 02:46:29 +03:00
|
|
|
if(*done)
|
|
|
|
/* Tell curl we're done */
|
2019-02-28 13:36:26 +03:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2009-11-28 02:46:29 +03:00
|
|
|
}
|
|
|
|
/* if rc == 0, then select() timed out */
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2009-11-28 02:46:29 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_doing
|
|
|
|
*
|
2013-01-04 05:50:28 +04:00
|
|
|
* Called from multi.c while DOing
|
2009-11-28 02:46:29 +03:00
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
result = tftp_multi_statemach(conn, dophase_done);
|
|
|
|
|
|
|
|
if(*dophase_done) {
|
|
|
|
DEBUGF(infof(conn->data, "DO phase is complete\n"));
|
|
|
|
}
|
2013-12-07 18:53:08 +04:00
|
|
|
else if(!result) {
|
2013-08-23 00:40:38 +04:00
|
|
|
/* 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
|
2017-10-25 12:59:43 +03:00
|
|
|
result = Curl_speedcheck(conn->data, Curl_now());
|
2013-08-23 00:40:38 +04:00
|
|
|
}
|
2009-11-28 02:46:29 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_peform
|
|
|
|
*
|
|
|
|
* Entry point for transfer from tftp_do, sarts state mach
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
static CURLcode tftp_perform(struct connectdata *conn, bool *dophase_done)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
|
|
|
|
|
|
|
|
*dophase_done = FALSE;
|
|
|
|
|
|
|
|
result = tftp_state_machine(state, TFTP_EVENT_INIT);
|
|
|
|
|
2014-10-24 00:56:35 +04:00
|
|
|
if((state->state == TFTP_STATE_FIN) || result)
|
|
|
|
return result;
|
2009-11-28 02:46:29 +03:00
|
|
|
|
2013-01-17 15:59:23 +04:00
|
|
|
tftp_multi_statemach(conn, dophase_done);
|
2009-11-28 02:46:29 +03:00
|
|
|
|
|
|
|
if(*dophase_done)
|
|
|
|
DEBUGF(infof(conn->data, "DO phase is complete\n"));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_do
|
|
|
|
*
|
|
|
|
* The do callback
|
|
|
|
*
|
|
|
|
* This callback initiates the TFTP transfer
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
|
|
|
|
static CURLcode tftp_do(struct connectdata *conn, bool *done)
|
|
|
|
{
|
2014-10-24 00:56:35 +04:00
|
|
|
tftp_state_data_t *state;
|
|
|
|
CURLcode result;
|
2009-11-28 02:46:29 +03:00
|
|
|
|
|
|
|
*done = FALSE;
|
|
|
|
|
|
|
|
if(!conn->proto.tftpc) {
|
2014-10-24 00:56:35 +04:00
|
|
|
result = tftp_connect(conn, done);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2009-11-28 02:46:29 +03:00
|
|
|
}
|
2014-04-19 01:53:48 +04:00
|
|
|
|
2009-11-28 02:46:29 +03:00
|
|
|
state = (tftp_state_data_t *)conn->proto.tftpc;
|
2014-04-19 01:53:48 +04:00
|
|
|
if(!state)
|
2017-07-31 08:52:04 +03:00
|
|
|
return CURLE_TFTP_ILLEGAL;
|
2009-11-28 02:46:29 +03:00
|
|
|
|
2014-10-24 00:56:35 +04:00
|
|
|
result = tftp_perform(conn, done);
|
2009-11-28 02:46:29 +03:00
|
|
|
|
2010-03-22 01:24:36 +03:00
|
|
|
/* If tftp_perform() returned an error, use that for return code. If it
|
|
|
|
was OK, see if tftp_translate_code() has an error. */
|
2014-10-24 00:56:35 +04:00
|
|
|
if(!result)
|
2010-03-22 01:24:36 +03:00
|
|
|
/* If we have encountered an internal tftp error, translate it. */
|
2014-10-24 00:56:35 +04:00
|
|
|
result = tftp_translate_code(state->error);
|
2009-11-28 02:46:29 +03:00
|
|
|
|
2014-10-24 00:56:35 +04:00
|
|
|
return result;
|
2005-09-02 19:11:08 +04:00
|
|
|
}
|
2007-10-12 17:36:37 +04:00
|
|
|
|
2007-12-09 01:50:55 +03:00
|
|
|
static CURLcode tftp_setup_connection(struct connectdata * conn)
|
2007-10-12 17:36:37 +04:00
|
|
|
{
|
2016-06-21 16:47:12 +03:00
|
|
|
struct Curl_easy *data = conn->data;
|
2016-11-23 09:53:24 +03:00
|
|
|
char *type;
|
2007-10-12 17:36:37 +04:00
|
|
|
|
|
|
|
conn->socktype = SOCK_DGRAM; /* UDP datagram based */
|
|
|
|
|
|
|
|
/* TFTP URLs support an extension like ";mode=<typecode>" that
|
|
|
|
* we'll try to get now! */
|
2018-09-15 00:33:28 +03:00
|
|
|
type = strstr(data->state.up.path, ";mode=");
|
2007-10-12 17:36:37 +04:00
|
|
|
|
2007-11-07 12:21:35 +03:00
|
|
|
if(!type)
|
2007-10-12 17:36:37 +04:00
|
|
|
type = strstr(conn->host.rawalloc, ";mode=");
|
|
|
|
|
2007-11-07 12:21:35 +03:00
|
|
|
if(type) {
|
2018-06-02 23:52:56 +03:00
|
|
|
char command;
|
2007-10-12 17:36:37 +04:00
|
|
|
*type = 0; /* it was in the middle of the hostname */
|
2009-01-21 07:42:47 +03:00
|
|
|
command = Curl_raw_toupper(type[6]);
|
2007-10-12 17:36:37 +04:00
|
|
|
|
2016-12-14 01:34:59 +03:00
|
|
|
switch(command) {
|
2007-10-12 17:36:37 +04:00
|
|
|
case 'A': /* ASCII mode */
|
|
|
|
case 'N': /* NETASCII mode */
|
|
|
|
data->set.prefer_ascii = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O': /* octet mode */
|
|
|
|
case 'I': /* binary mode */
|
|
|
|
default:
|
|
|
|
/* switch off ASCII */
|
|
|
|
data->set.prefer_ascii = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2005-09-02 19:11:08 +04:00
|
|
|
#endif
|