2005-09-02 23:11:08 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2007-01-17 06:22:10 +08:00
|
|
|
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2005-09-02 23:11:08 +08:00
|
|
|
*
|
|
|
|
* 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 http://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.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
|
|
|
#ifndef CURL_DISABLE_TFTP
|
|
|
|
/* -- WIN32 approved -- */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
#include <time.h>
|
|
|
|
#include <io.h>
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#include <netinet/in.h>
|
2006-08-31 00:17:06 +08:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2005-09-02 23:11:08 +08:00
|
|
|
#include <sys/time.h>
|
2006-08-30 02:45:55 +08:00
|
|
|
#endif
|
2005-09-02 23:11:08 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <netdb.h>
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NET_IF_H
|
|
|
|
#include <net/if.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
|
2007-02-26 12:24:26 +08:00
|
|
|
#endif /* WIN32 */
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
#include "urldata.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "transfer.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "tftp.h"
|
|
|
|
#include "progress.h"
|
2005-09-06 23:43:14 +08:00
|
|
|
#include "connect.h"
|
|
|
|
#include "strerror.h"
|
2005-11-13 06:10:42 +08:00
|
|
|
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
|
2007-01-17 06:22:10 +08:00
|
|
|
#include "url.h"
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
#include "select.h"
|
|
|
|
|
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2006-05-09 03:41:26 +08:00
|
|
|
/* RFC2348 allows the block size to be negotiated, but we don't support that */
|
|
|
|
#define TFTP_BLOCKSIZE 512
|
|
|
|
|
2005-09-02 23:11:08 +08:00
|
|
|
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 {
|
|
|
|
TFTP_EVENT_INIT=0,
|
|
|
|
TFTP_EVENT_RRQ = 1,
|
|
|
|
TFTP_EVENT_WRQ = 2,
|
|
|
|
TFTP_EVENT_DATA = 3,
|
|
|
|
TFTP_EVENT_ACK = 4,
|
|
|
|
TFTP_EVENT_ERROR = 5,
|
|
|
|
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,
|
2007-08-29 13:36:53 +08:00
|
|
|
TFTP_ERR_NOSUCHUSER, /* This will never be triggered by this code */
|
2007-09-08 04:35:37 +08:00
|
|
|
|
|
|
|
/* The remaining error codes are internal to curl */
|
|
|
|
TFTP_ERR_NONE = -100,
|
2005-09-02 23:11:08 +08:00
|
|
|
TFTP_ERR_TIMEOUT,
|
|
|
|
TFTP_ERR_NORESPONSE
|
|
|
|
} tftp_error_t;
|
|
|
|
|
|
|
|
typedef struct tftp_packet {
|
2006-05-09 03:41:26 +08:00
|
|
|
unsigned char data[2 + 2 + TFTP_BLOCKSIZE];
|
2005-09-02 23:11:08 +08:00
|
|
|
} tftp_packet_t;
|
|
|
|
|
|
|
|
typedef struct tftp_state_data {
|
|
|
|
tftp_state_t state;
|
|
|
|
tftp_mode_t mode;
|
|
|
|
tftp_error_t error;
|
|
|
|
struct connectdata *conn;
|
2005-09-06 18:39:01 +08:00
|
|
|
curl_socket_t sockfd;
|
2005-09-02 23:11:08 +08:00
|
|
|
int retries;
|
|
|
|
int retry_time;
|
|
|
|
int retry_max;
|
|
|
|
time_t start_time;
|
|
|
|
time_t max_time;
|
|
|
|
unsigned short block;
|
2005-11-13 06:10:42 +08:00
|
|
|
struct Curl_sockaddr_storage local_addr;
|
|
|
|
struct Curl_sockaddr_storage remote_addr;
|
2005-09-02 23:11:08 +08:00
|
|
|
socklen_t remote_addrlen;
|
|
|
|
int rbytes;
|
|
|
|
int sbytes;
|
|
|
|
tftp_packet_t rpacket;
|
|
|
|
tftp_packet_t spacket;
|
|
|
|
} tftp_state_data_t;
|
|
|
|
|
|
|
|
|
|
|
|
/* Forward declarations */
|
2006-05-09 06:23:33 +08: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-10-12 21:36:37 +08:00
|
|
|
static CURLcode Curl_tftp_connect(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode Curl_tftp(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode Curl_tftp_done(struct connectdata *conn,
|
|
|
|
CURLcode, bool premature);
|
|
|
|
static CURLcode Curl_tftp_setup_connection(struct connectdata * conn);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TFTP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_tftp = {
|
|
|
|
"TFTP", /* scheme */
|
|
|
|
Curl_tftp_setup_connection, /* setup_connection */
|
|
|
|
Curl_tftp, /* do_it */
|
|
|
|
Curl_tftp_done, /* done */
|
2007-10-18 00:58:32 +08:00
|
|
|
ZERO_NULL, /* do_more */
|
2007-10-12 21:36:37 +08:00
|
|
|
Curl_tftp_connect, /* connect_it */
|
2007-10-18 00:58:32 +08:00
|
|
|
ZERO_NULL, /* connecting */
|
|
|
|
ZERO_NULL, /* doing */
|
|
|
|
ZERO_NULL, /* proto_getsock */
|
|
|
|
ZERO_NULL, /* doing_getsock */
|
|
|
|
ZERO_NULL, /* disconnect */
|
2007-10-12 21:36:37 +08:00
|
|
|
PORT_TFTP, /* defport */
|
|
|
|
PROT_TFTP /* protocol */
|
|
|
|
};
|
|
|
|
|
2005-09-02 23:11:08 +08: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
|
|
|
|
*
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2007-08-29 13:36:53 +08:00
|
|
|
static void tftp_set_timeouts(tftp_state_data_t *state)
|
2005-09-02 23:11:08 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
struct SessionHandle *data = state->conn->data;
|
2005-09-21 19:28:40 +08:00
|
|
|
time_t maxtime, timeout;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
time(&state->start_time);
|
|
|
|
if(state->state == TFTP_STATE_START) {
|
|
|
|
/* Compute drop-dead time */
|
2007-02-07 02:08:11 +08:00
|
|
|
maxtime = (time_t)(data->set.connecttimeout/1000L?
|
|
|
|
data->set.connecttimeout/1000L:30);
|
2005-09-02 23:11:08 +08:00
|
|
|
state->max_time = state->start_time+maxtime;
|
|
|
|
|
|
|
|
/* Set per-block timeout to total */
|
|
|
|
timeout = maxtime ;
|
|
|
|
|
|
|
|
/* Average restart after 5 seconds */
|
|
|
|
state->retry_max = timeout/5;
|
|
|
|
|
2007-05-18 05:40:08 +08:00
|
|
|
if(state->retry_max < 1)
|
|
|
|
/* avoid division by zero below */
|
|
|
|
state->retry_max = 1;
|
|
|
|
|
2005-09-02 23:11:08 +08:00
|
|
|
/* Compute the re-start interval to suit the timeout */
|
|
|
|
state->retry_time = timeout/state->retry_max;
|
2006-05-08 23:09:50 +08:00
|
|
|
if(state->retry_time<1)
|
|
|
|
state->retry_time=1;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
/* Compute drop-dead time */
|
2007-02-07 02:08:11 +08:00
|
|
|
maxtime = (time_t)(data->set.timeout/1000L?
|
|
|
|
data->set.timeout/1000L:3600);
|
2005-09-02 23:11:08 +08:00
|
|
|
state->max_time = state->start_time+maxtime;
|
|
|
|
|
|
|
|
/* Set per-block timeout to 10% of total */
|
|
|
|
timeout = maxtime/10 ;
|
|
|
|
|
|
|
|
/* Average reposting an ACK after 15 seconds */
|
|
|
|
state->retry_max = timeout/15;
|
|
|
|
}
|
|
|
|
/* But bound the total number */
|
2006-05-08 23:09:50 +08:00
|
|
|
if(state->retry_max<3)
|
|
|
|
state->retry_max=3;
|
|
|
|
|
|
|
|
if(state->retry_max>50)
|
|
|
|
state->retry_max=50;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
/* Compute the re-ACK interval to suit the timeout */
|
|
|
|
state->retry_time = timeout/state->retry_max;
|
2006-05-08 23:09:50 +08:00
|
|
|
if(state->retry_time<1)
|
|
|
|
state->retry_time=1;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
infof(data, "set timeouts for state %d; Total %d, retry %d maxtry %d\n",
|
|
|
|
state->state, (state->max_time-state->start_time),
|
|
|
|
state->retry_time, state->retry_max);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_set_send_first
|
|
|
|
*
|
|
|
|
* Event handler for the START state
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
|
2006-05-08 23:09:50 +08:00
|
|
|
static void setpacketevent(tftp_packet_t *packet, unsigned short num)
|
|
|
|
{
|
2006-07-20 05:14:02 +08:00
|
|
|
packet->data[0] = (unsigned char)(num >> 8);
|
|
|
|
packet->data[1] = (unsigned char)(num & 0xff);
|
2006-05-08 23:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setpacketblock(tftp_packet_t *packet, unsigned short num)
|
|
|
|
{
|
2006-07-20 05:14:02 +08:00
|
|
|
packet->data[2] = (unsigned char)(num >> 8);
|
|
|
|
packet->data[3] = (unsigned char)(num & 0xff);
|
2006-05-08 23:09:50 +08:00
|
|
|
}
|
|
|
|
|
2007-08-29 13:36:53 +08:00
|
|
|
static unsigned short getrpacketevent(const tftp_packet_t *packet)
|
2006-05-08 23:09:50 +08:00
|
|
|
{
|
2006-07-20 05:14:02 +08:00
|
|
|
return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
|
2006-05-08 23:09:50 +08:00
|
|
|
}
|
|
|
|
|
2007-08-29 13:36:53 +08:00
|
|
|
static unsigned short getrpacketblock(const tftp_packet_t *packet)
|
2006-05-08 23:09:50 +08:00
|
|
|
{
|
2006-07-20 05:14:02 +08:00
|
|
|
return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
|
2006-05-08 23:09:50 +08:00
|
|
|
}
|
|
|
|
|
2006-05-09 06:23:33 +08:00
|
|
|
static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
|
2005-09-02 23:11:08 +08:00
|
|
|
{
|
|
|
|
int sbytes;
|
|
|
|
const char *mode = "octet";
|
2007-01-17 02:33:25 +08:00
|
|
|
char *filename;
|
2005-09-02 23:11:08 +08:00
|
|
|
struct SessionHandle *data = state->conn->data;
|
2006-05-09 21:02:53 +08:00
|
|
|
CURLcode res = CURLE_OK;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
/* Set ascii mode if -B flag was used */
|
2006-08-20 05:18:36 +08:00
|
|
|
if(data->set.prefer_ascii)
|
2005-09-02 23:11:08 +08: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;
|
2006-05-09 06:23:33 +08:00
|
|
|
return res;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(data->set.upload) {
|
|
|
|
/* If we are uploading, send an WRQ */
|
2006-05-08 23:09:50 +08:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
|
2007-11-25 07:16:55 +08:00
|
|
|
state->conn->data->req.upload_fromhere =
|
2007-11-15 06:44:30 +08:00
|
|
|
(char *)&state->spacket.data[4];
|
2006-04-08 05:50:47 +08:00
|
|
|
if(data->set.infilesize != -1)
|
2005-09-02 23:11:08 +08:00
|
|
|
Curl_pgrsSetUploadSize(data, data->set.infilesize);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* If we are downloading, send an RRQ */
|
2006-05-08 23:09:50 +08:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
2007-01-17 02:33:25 +08:00
|
|
|
/* As RFC3617 describes the separator slash is not actually part of the
|
|
|
|
file name so we skip the always-present first letter of the path string. */
|
2007-11-25 07:16:55 +08:00
|
|
|
filename = curl_easy_unescape(data, &state->conn->data->state.path[1], 0,
|
2007-01-17 06:22:10 +08:00
|
|
|
NULL);
|
2007-11-07 17:21:35 +08:00
|
|
|
if(!filename)
|
2007-04-11 08:25:41 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2006-05-08 23:09:50 +08:00
|
|
|
snprintf((char *)&state->spacket.data[2],
|
2006-05-09 03:41:26 +08:00
|
|
|
TFTP_BLOCKSIZE,
|
2006-03-20 15:32:50 +08:00
|
|
|
"%s%c%s%c", filename, '\0', mode, '\0');
|
2005-12-05 22:10:48 +08:00
|
|
|
sbytes = 4 + (int)strlen(filename) + (int)strlen(mode);
|
2005-11-24 06:59:24 +08:00
|
|
|
sbytes = sendto(state->sockfd, (void *)&state->spacket,
|
|
|
|
sbytes, 0,
|
2005-09-02 23:11:08 +08:00
|
|
|
state->conn->ip_addr->ai_addr,
|
|
|
|
state->conn->ip_addr->ai_addrlen);
|
|
|
|
if(sbytes < 0) {
|
2007-02-17 02:19:35 +08:00
|
|
|
failf(data, "%s\n", Curl_strerror(state->conn, SOCKERRNO));
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
2007-01-17 02:33:25 +08:00
|
|
|
Curl_safefree(filename);
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_EVENT_ACK: /* Connected for transmit */
|
|
|
|
infof(data, "%s\n", "Connected for transmit");
|
|
|
|
state->state = TFTP_STATE_TX;
|
|
|
|
tftp_set_timeouts(state);
|
2006-05-09 06:23:33 +08:00
|
|
|
return tftp_tx(state, event);
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
case TFTP_EVENT_DATA: /* connected for receive */
|
|
|
|
infof(data, "%s\n", "Connected for receive");
|
|
|
|
state->state = TFTP_STATE_RX;
|
|
|
|
tftp_set_timeouts(state);
|
2006-05-09 06:23:33 +08:00
|
|
|
return tftp_rx(state, event);
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
case TFTP_EVENT_ERROR:
|
|
|
|
state->state = TFTP_STATE_FIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
failf(state->conn->data, "tftp_send_first: internal error\n");
|
|
|
|
break;
|
|
|
|
}
|
2006-05-09 06:23:33 +08:00
|
|
|
return res;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_rx
|
|
|
|
*
|
|
|
|
* Event handler for the RX state
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2006-05-09 06:23:33 +08:00
|
|
|
static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event)
|
2005-09-02 23:11:08 +08:00
|
|
|
{
|
|
|
|
int sbytes;
|
|
|
|
int rblock;
|
2005-09-16 04:21:27 +08:00
|
|
|
struct SessionHandle *data = state->conn->data;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
switch(event) {
|
|
|
|
|
|
|
|
case TFTP_EVENT_DATA:
|
|
|
|
|
|
|
|
/* Is this the block we expect? */
|
2006-05-08 23:09:50 +08:00
|
|
|
rblock = getrpacketblock(&state->rpacket);
|
2007-11-07 17:21:35 +08:00
|
|
|
if((state->block+1) != rblock) {
|
2005-09-02 23:11:08 +08:00
|
|
|
/* No, log it, up the retry count and fail if over the limit */
|
2005-09-16 04:21:27 +08:00
|
|
|
infof(data,
|
2005-09-02 23:11:08 +08:00
|
|
|
"Received unexpected DATA packet block %d\n", rblock);
|
|
|
|
state->retries++;
|
2007-11-07 17:21:35 +08:00
|
|
|
if(state->retries>state->retry_max) {
|
2005-09-16 04:21:27 +08:00
|
|
|
failf(data, "tftp_rx: giving up waiting for block %d\n",
|
2005-09-02 23:11:08 +08:00
|
|
|
state->block+1);
|
2006-05-09 06:23:33 +08:00
|
|
|
return CURLE_TFTP_ILLEGAL;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* This is the expected block. Reset counters and ACK it. */
|
2006-07-20 05:14:02 +08:00
|
|
|
state->block = (unsigned short)rblock;
|
2005-09-02 23:11:08 +08:00
|
|
|
state->retries = 0;
|
2006-05-08 23:09:50 +08:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_ACK);
|
|
|
|
setpacketblock(&state->spacket, state->block);
|
|
|
|
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
|
2005-12-03 07:22:00 +08:00
|
|
|
4, SEND_4TH_ARG,
|
2005-11-11 06:25:07 +08:00
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
2005-09-02 23:11:08 +08:00
|
|
|
if(sbytes < 0) {
|
2007-02-17 02:19:35 +08:00
|
|
|
failf(data, "%s\n", Curl_strerror(state->conn, SOCKERRNO));
|
2007-05-18 18:12:57 +08:00
|
|
|
return CURLE_SEND_ERROR;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
|
2006-05-09 03:41:26 +08:00
|
|
|
/* Check if completed (That is, a less than full packet is received) */
|
2007-11-07 17:21:35 +08:00
|
|
|
if(state->rbytes < (int)sizeof(state->spacket)){
|
2005-09-02 23:11:08 +08:00
|
|
|
state->state = TFTP_STATE_FIN;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state->state = TFTP_STATE_RX;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_EVENT_TIMEOUT:
|
|
|
|
/* Increment the retry count and fail if over the limit */
|
|
|
|
state->retries++;
|
2005-09-16 04:21:27 +08:00
|
|
|
infof(data,
|
2005-09-02 23:11:08 +08:00
|
|
|
"Timeout waiting for block %d ACK. Retries = %d\n", state->retries);
|
|
|
|
if(state->retries > state->retry_max) {
|
|
|
|
state->error = TFTP_ERR_TIMEOUT;
|
|
|
|
state->state = TFTP_STATE_FIN;
|
2006-05-09 06:23:33 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-09-02 23:11:08 +08:00
|
|
|
/* Resend the previous ACK */
|
2005-11-24 06:59:24 +08:00
|
|
|
sbytes = sendto(state->sockfd, (void *)&state->spacket,
|
2005-12-03 07:22:00 +08:00
|
|
|
4, SEND_4TH_ARG,
|
2005-11-11 06:25:07 +08:00
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
2005-09-02 23:11:08 +08:00
|
|
|
/* Check all sbytes were sent */
|
|
|
|
if(sbytes<0) {
|
2007-02-17 02:19:35 +08:00
|
|
|
failf(data, "%s\n", Curl_strerror(state->conn, SOCKERRNO));
|
2007-05-18 18:12:57 +08:00
|
|
|
return CURLE_SEND_ERROR;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_EVENT_ERROR:
|
|
|
|
state->state = TFTP_STATE_FIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2005-09-16 04:21:27 +08:00
|
|
|
failf(data, "%s\n", "tftp_rx: internal error");
|
2007-05-18 18:12:57 +08:00
|
|
|
return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
|
|
|
|
this */
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
2006-05-09 06:23:33 +08:00
|
|
|
return CURLE_OK;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_tx
|
|
|
|
*
|
|
|
|
* Event handler for the TX state
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2006-05-09 06:23:33 +08:00
|
|
|
static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
|
2005-09-02 23:11:08 +08:00
|
|
|
{
|
2005-09-16 04:21:27 +08:00
|
|
|
struct SessionHandle *data = state->conn->data;
|
2005-09-02 23:11:08 +08:00
|
|
|
int sbytes;
|
|
|
|
int rblock;
|
2006-05-09 06:23:33 +08:00
|
|
|
CURLcode res = CURLE_OK;
|
2007-11-25 07:16:55 +08:00
|
|
|
struct SingleRequest *k = &data->req;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
switch(event) {
|
|
|
|
|
|
|
|
case TFTP_EVENT_ACK:
|
|
|
|
/* Ack the packet */
|
2006-05-08 23:09:50 +08:00
|
|
|
rblock = getrpacketblock(&state->rpacket);
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
if(rblock != state->block) {
|
|
|
|
/* This isn't the expected block. Log it and up the retry counter */
|
2005-09-16 04:21:27 +08:00
|
|
|
infof(data, "Received ACK for block %d, expecting %d\n",
|
2005-09-02 23:11:08 +08:00
|
|
|
rblock, state->block);
|
|
|
|
state->retries++;
|
|
|
|
/* Bail out if over the maximum */
|
|
|
|
if(state->retries>state->retry_max) {
|
2006-05-09 06:23:33 +08:00
|
|
|
failf(data, "tftp_tx: giving up waiting for block %d ack",
|
2005-09-02 23:11:08 +08:00
|
|
|
state->block);
|
2006-05-09 06:23:33 +08:00
|
|
|
res = CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
else {
|
2006-05-09 05:00:44 +08:00
|
|
|
/* Re-send the data packet */
|
|
|
|
sbytes = sendto(state->sockfd, (void *)&state->spacket,
|
|
|
|
4+state->sbytes, SEND_4TH_ARG,
|
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
|
|
|
/* Check all sbytes were sent */
|
|
|
|
if(sbytes<0) {
|
2007-02-17 02:19:35 +08:00
|
|
|
failf(data, "%s\n", Curl_strerror(state->conn, SOCKERRNO));
|
2006-05-09 06:23:33 +08:00
|
|
|
res = CURLE_SEND_ERROR;
|
2006-05-09 05:00:44 +08:00
|
|
|
}
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
2006-05-09 06:23:33 +08:00
|
|
|
return res;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
/* This is the expected packet. Reset the counters and send the next
|
|
|
|
block */
|
|
|
|
state->block++;
|
|
|
|
state->retries = 0;
|
2006-05-08 23:09:50 +08:00
|
|
|
setpacketevent(&state->spacket, TFTP_EVENT_DATA);
|
|
|
|
setpacketblock(&state->spacket, state->block);
|
2006-05-09 03:41:26 +08:00
|
|
|
if(state->block > 1 && state->sbytes < TFTP_BLOCKSIZE) {
|
2005-09-02 23:11:08 +08:00
|
|
|
state->state = TFTP_STATE_FIN;
|
2006-05-09 06:23:33 +08:00
|
|
|
return CURLE_OK;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
2006-05-09 06:23:33 +08:00
|
|
|
res = Curl_fillreadbuffer(state->conn, TFTP_BLOCKSIZE, &state->sbytes);
|
|
|
|
if(res)
|
|
|
|
return res;
|
2006-05-08 23:09:50 +08:00
|
|
|
sbytes = sendto(state->sockfd, (void *)state->spacket.data,
|
2005-12-03 07:22:00 +08:00
|
|
|
4+state->sbytes, SEND_4TH_ARG,
|
2005-11-11 06:25:07 +08:00
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
2005-09-02 23:11:08 +08:00
|
|
|
/* Check all sbytes were sent */
|
|
|
|
if(sbytes<0) {
|
2007-02-17 02:19:35 +08:00
|
|
|
failf(data, "%s\n", Curl_strerror(state->conn, SOCKERRNO));
|
2007-05-18 18:32:48 +08:00
|
|
|
return CURLE_SEND_ERROR;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
2007-05-18 18:40:43 +08:00
|
|
|
/* Update the progress meter */
|
|
|
|
k->writebytecount += state->sbytes;
|
|
|
|
Curl_pgrsSetUploadCounter(data, k->writebytecount);
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_EVENT_TIMEOUT:
|
|
|
|
/* Increment the retry counter and log the timeout */
|
|
|
|
state->retries++;
|
2005-09-16 04:21:27 +08:00
|
|
|
infof(data, "Timeout waiting for block %d ACK. "
|
2005-09-02 23:11:08 +08:00
|
|
|
" Retries = %d\n", state->retries);
|
|
|
|
/* 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 18:32:48 +08:00
|
|
|
}
|
|
|
|
else {
|
2005-09-02 23:11:08 +08:00
|
|
|
/* Re-send the data packet */
|
2005-11-24 06:59:24 +08:00
|
|
|
sbytes = sendto(state->sockfd, (void *)&state->spacket,
|
2005-12-03 07:22:00 +08:00
|
|
|
4+state->sbytes, SEND_4TH_ARG,
|
2005-11-11 06:25:07 +08:00
|
|
|
(struct sockaddr *)&state->remote_addr,
|
|
|
|
state->remote_addrlen);
|
2005-09-02 23:11:08 +08:00
|
|
|
/* Check all sbytes were sent */
|
|
|
|
if(sbytes<0) {
|
2007-02-17 02:19:35 +08:00
|
|
|
failf(data, "%s\n", Curl_strerror(state->conn, SOCKERRNO));
|
2007-05-18 18:32:48 +08:00
|
|
|
return CURLE_SEND_ERROR;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
2007-05-18 18:40:43 +08:00
|
|
|
/* since this was a re-send, we remain at the still byte position */
|
|
|
|
Curl_pgrsSetUploadCounter(data, k->writebytecount);
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_EVENT_ERROR:
|
|
|
|
state->state = TFTP_STATE_FIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2005-09-16 04:21:27 +08:00
|
|
|
failf(data, "%s\n", "tftp_tx: internal error");
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-05-09 06:23:33 +08:00
|
|
|
return res;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* tftp_state_machine
|
|
|
|
*
|
|
|
|
* The tftp state machine event dispatcher
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
static CURLcode tftp_state_machine(tftp_state_data_t *state,
|
|
|
|
tftp_event_t event)
|
|
|
|
{
|
2006-05-09 06:23:33 +08:00
|
|
|
CURLcode res = CURLE_OK;
|
2005-09-16 04:21:27 +08:00
|
|
|
struct SessionHandle *data = state->conn->data;
|
2005-09-02 23:11:08 +08:00
|
|
|
switch(state->state) {
|
|
|
|
case TFTP_STATE_START:
|
2005-09-16 04:21:27 +08:00
|
|
|
DEBUGF(infof(data, "TFTP_STATE_START\n"));
|
2006-05-09 06:23:33 +08:00
|
|
|
res = tftp_send_first(state, event);
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
case TFTP_STATE_RX:
|
2005-09-16 04:21:27 +08:00
|
|
|
DEBUGF(infof(data, "TFTP_STATE_RX\n"));
|
2006-05-09 06:23:33 +08:00
|
|
|
res = tftp_rx(state, event);
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
case TFTP_STATE_TX:
|
2005-09-16 04:21:27 +08:00
|
|
|
DEBUGF(infof(data, "TFTP_STATE_TX\n"));
|
2006-05-09 06:23:33 +08:00
|
|
|
res = tftp_tx(state, event);
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
case TFTP_STATE_FIN:
|
2005-09-16 04:21:27 +08:00
|
|
|
infof(data, "%s\n", "TFTP finished");
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
default:
|
2005-09-16 04:21:27 +08:00
|
|
|
DEBUGF(infof(data, "STATE: %d\n", state->state));
|
|
|
|
failf(data, "%s\n", "Internal state machine error");
|
2006-05-09 06:23:33 +08:00
|
|
|
res = CURLE_TFTP_ILLEGAL;
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
}
|
2006-05-09 06:23:33 +08:00
|
|
|
return res;
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* Curl_tftp_connect
|
|
|
|
*
|
|
|
|
* The connect callback
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2007-10-12 21:36:37 +08:00
|
|
|
static CURLcode Curl_tftp_connect(struct connectdata *conn, bool *done)
|
2005-09-02 23:11:08 +08:00
|
|
|
{
|
|
|
|
CURLcode code;
|
2007-10-25 15:47:38 +08:00
|
|
|
tftp_state_data_t *state;
|
2005-09-02 23:11:08 +08:00
|
|
|
int rc;
|
|
|
|
|
2007-10-25 15:47:38 +08:00
|
|
|
/* If there already is a protocol-specific struct allocated for this
|
|
|
|
sessionhandle, deal with it */
|
|
|
|
Curl_reset_reqproto(conn);
|
|
|
|
|
2007-11-25 07:16:55 +08:00
|
|
|
if(!(state = conn->data->state.proto.tftp)) {
|
|
|
|
state = conn->data->state.proto.tftp = calloc(sizeof(tftp_state_data_t),
|
|
|
|
1);
|
2007-10-25 17:34:16 +08:00
|
|
|
if(!state)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
2005-09-02 23:11:08 +08:00
|
|
|
|
2006-11-10 05:36:18 +08:00
|
|
|
conn->bits.close = FALSE; /* keep it open if possible */
|
|
|
|
|
2005-09-02 23:11:08 +08:00
|
|
|
state->conn = conn;
|
|
|
|
state->sockfd = state->conn->sock[FIRSTSOCKET];
|
|
|
|
state->state = TFTP_STATE_START;
|
2007-09-08 04:35:37 +08:00
|
|
|
state->error = TFTP_ERR_NONE;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
2006-07-30 00:17:36 +08:00
|
|
|
((struct sockaddr *)&state->local_addr)->sa_family =
|
2007-02-05 12:10:32 +08:00
|
|
|
(unsigned short)(conn->ip_addr->ai_family);
|
2005-09-06 23:43:14 +08:00
|
|
|
|
2005-09-02 23:11:08 +08:00
|
|
|
tftp_set_timeouts(state);
|
|
|
|
|
2007-10-04 18:01:41 +08: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 05:36:18 +08: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.
|
|
|
|
*
|
|
|
|
* 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...
|
|
|
|
*/
|
|
|
|
rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
|
|
|
|
conn->ip_addr->ai_addrlen);
|
|
|
|
if(rc) {
|
|
|
|
failf(conn->data, "bind() failed; %s\n",
|
2007-02-17 02:19:35 +08:00
|
|
|
Curl_strerror(conn, SOCKERRNO));
|
2006-11-10 05:36:18 +08:00
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
2007-10-04 18:01:41 +08:00
|
|
|
conn->bits.bound = TRUE;
|
2005-09-05 02:33:20 +08:00
|
|
|
}
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
Curl_pgrsStartNow(conn->data);
|
|
|
|
|
|
|
|
*done = TRUE;
|
|
|
|
code = CURLE_OK;
|
|
|
|
return(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* Curl_tftp_done
|
|
|
|
*
|
|
|
|
* The done callback
|
|
|
|
*
|
|
|
|
**********************************************************/
|
2007-10-12 21:36:37 +08:00
|
|
|
static CURLcode Curl_tftp_done(struct connectdata *conn, CURLcode status,
|
|
|
|
bool premature)
|
2005-09-02 23:11:08 +08:00
|
|
|
{
|
|
|
|
(void)status; /* unused */
|
2007-01-17 06:22:10 +08:00
|
|
|
(void)premature; /* not used */
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
Curl_pgrsDone(conn);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************
|
|
|
|
*
|
|
|
|
* Curl_tftp
|
|
|
|
*
|
|
|
|
* The do callback
|
|
|
|
*
|
|
|
|
* This callback handles the entire TFTP transfer
|
|
|
|
*
|
|
|
|
**********************************************************/
|
|
|
|
|
2007-10-12 21:36:37 +08:00
|
|
|
static CURLcode Curl_tftp(struct connectdata *conn, bool *done)
|
2005-09-02 23:11:08 +08:00
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
2007-11-15 06:41:42 +08:00
|
|
|
tftp_state_data_t *state;
|
2005-09-02 23:11:08 +08:00
|
|
|
tftp_event_t event;
|
|
|
|
CURLcode code;
|
|
|
|
int rc;
|
2005-11-13 06:10:42 +08:00
|
|
|
struct Curl_sockaddr_storage fromaddr;
|
2005-09-02 23:11:08 +08:00
|
|
|
socklen_t fromlen;
|
|
|
|
int check_time = 0;
|
2007-11-25 07:16:55 +08:00
|
|
|
struct SingleRequest *k = &data->req;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
2006-11-10 05:36:18 +08:00
|
|
|
*done = TRUE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Since connections can be re-used between SessionHandles, this might be a
|
|
|
|
connection already existing but on a fresh SessionHandle struct so we must
|
|
|
|
make sure we have a good 'struct TFTP' to play with. For new connections,
|
|
|
|
the struct TFTP is allocated and setup in the Curl_tftp_connect() function.
|
|
|
|
*/
|
2007-11-15 06:41:42 +08:00
|
|
|
Curl_reset_reqproto(conn);
|
|
|
|
|
2007-11-25 07:16:55 +08:00
|
|
|
if(!data->state.proto.tftp) {
|
2006-11-10 05:36:18 +08:00
|
|
|
code = Curl_tftp_connect(conn, done);
|
|
|
|
if(code)
|
|
|
|
return code;
|
|
|
|
}
|
2007-11-25 07:16:55 +08:00
|
|
|
state = (tftp_state_data_t *)data->state.proto.tftp;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
/* Run the TFTP State Machine */
|
2007-04-11 08:25:41 +08:00
|
|
|
for(code=tftp_state_machine(state, TFTP_EVENT_INIT);
|
|
|
|
(state->state != TFTP_STATE_FIN) && (code == CURLE_OK);
|
|
|
|
code=tftp_state_machine(state, event) ) {
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
/* Wait until ready to read or timeout occurs */
|
2007-05-18 18:12:57 +08:00
|
|
|
rc=Curl_socket_ready(state->sockfd, CURL_SOCKET_BAD,
|
|
|
|
state->retry_time * 1000);
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
if(rc == -1) {
|
2005-09-16 04:21:27 +08:00
|
|
|
/* bail out */
|
2007-02-17 02:19:35 +08:00
|
|
|
int error = SOCKERRNO;
|
2005-09-16 04:21:27 +08:00
|
|
|
failf(data, "%s\n", Curl_strerror(conn, error));
|
2005-09-02 23:11:08 +08:00
|
|
|
event = TFTP_EVENT_ERROR;
|
|
|
|
}
|
2007-11-07 17:21:35 +08:00
|
|
|
else if(rc==0) {
|
2005-09-02 23:11:08 +08:00
|
|
|
/* A timeout occured */
|
|
|
|
event = TFTP_EVENT_TIMEOUT;
|
|
|
|
|
|
|
|
/* Force a look at transfer timeouts */
|
|
|
|
check_time = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
/* Receive the packet */
|
|
|
|
fromlen=sizeof(fromaddr);
|
|
|
|
state->rbytes = recvfrom(state->sockfd,
|
|
|
|
(void *)&state->rpacket, sizeof(state->rpacket),
|
2005-11-11 06:25:07 +08:00
|
|
|
0, (struct sockaddr *)&fromaddr, &fromlen);
|
2005-09-02 23:11:08 +08:00
|
|
|
if(state->remote_addrlen==0) {
|
|
|
|
memcpy(&state->remote_addr, &fromaddr, fromlen);
|
|
|
|
state->remote_addrlen = fromlen;
|
|
|
|
}
|
|
|
|
|
2006-03-21 06:15:22 +08:00
|
|
|
/* Sanity check packet length */
|
2007-11-07 17:21:35 +08:00
|
|
|
if(state->rbytes < 4) {
|
2007-11-15 06:44:30 +08:00
|
|
|
failf(data, "Received too short packet\n");
|
2006-03-21 06:15:22 +08:00
|
|
|
/* Not a timeout, but how best to handle it? */
|
|
|
|
event = TFTP_EVENT_TIMEOUT;
|
2006-05-09 05:00:44 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
/* The event is given by the TFTP packet time */
|
|
|
|
event = (tftp_event_t)getrpacketevent(&state->rpacket);
|
|
|
|
|
|
|
|
switch(event) {
|
|
|
|
case TFTP_EVENT_DATA:
|
|
|
|
/* Don't pass to the client empty or retransmitted packets */
|
2007-11-07 17:21:35 +08:00
|
|
|
if(state->rbytes > 4 &&
|
2006-05-09 05:00:44 +08:00
|
|
|
((state->block+1) == getrpacketblock(&state->rpacket))) {
|
2006-08-20 05:18:36 +08:00
|
|
|
code = Curl_client_write(conn, CLIENTWRITE_BODY,
|
2006-05-09 05:00:44 +08:00
|
|
|
(char *)&state->rpacket.data[4],
|
|
|
|
state->rbytes-4);
|
|
|
|
if(code)
|
|
|
|
return code;
|
2007-05-18 18:40:43 +08:00
|
|
|
k->bytecount += state->rbytes-4;
|
|
|
|
Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
|
2006-05-09 05:00:44 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TFTP_EVENT_ERROR:
|
|
|
|
state->error = (tftp_error_t)getrpacketblock(&state->rpacket);
|
2007-11-15 06:44:30 +08:00
|
|
|
infof(data, "%s\n", (char *)&state->rpacket.data[4]);
|
2006-05-09 05:00:44 +08:00
|
|
|
break;
|
|
|
|
case TFTP_EVENT_ACK:
|
|
|
|
break;
|
|
|
|
case TFTP_EVENT_RRQ:
|
|
|
|
case TFTP_EVENT_WRQ:
|
|
|
|
default:
|
2007-11-15 06:44:30 +08:00
|
|
|
failf(data, "%s\n", "Internal error: Unexpected packet");
|
2006-05-09 05:00:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the progress meter */
|
2006-05-09 06:23:33 +08:00
|
|
|
if(Curl_pgrsUpdate(conn))
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
2006-03-21 06:15:22 +08:00
|
|
|
}
|
2005-09-02 23:11:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for transfer timeout every 10 blocks, or after timeout */
|
|
|
|
if(check_time%10==0) {
|
|
|
|
time_t current;
|
|
|
|
time(¤t);
|
|
|
|
if(current>state->max_time) {
|
2005-09-16 04:21:27 +08:00
|
|
|
DEBUGF(infof(data, "timeout: %d > %d\n",
|
|
|
|
current, state->max_time));
|
2005-09-02 23:11:08 +08:00
|
|
|
state->error = TFTP_ERR_TIMEOUT;
|
|
|
|
state->state = TFTP_STATE_FIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-04-11 08:25:41 +08:00
|
|
|
if(code)
|
|
|
|
return code;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
/* Tell curl we're done */
|
2006-09-08 05:49:20 +08:00
|
|
|
code = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
|
2006-05-09 06:23:33 +08:00
|
|
|
if(code)
|
|
|
|
return code;
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
/* If we have encountered an error */
|
2007-09-08 04:35:37 +08:00
|
|
|
if(state->error != TFTP_ERR_NONE) {
|
2005-09-02 23:11:08 +08:00
|
|
|
|
|
|
|
/* Translate internal error codes to curl error codes */
|
|
|
|
switch(state->error) {
|
|
|
|
case TFTP_ERR_NOTFOUND:
|
|
|
|
code = CURLE_TFTP_NOTFOUND;
|
|
|
|
break;
|
|
|
|
case TFTP_ERR_PERM:
|
|
|
|
code = CURLE_TFTP_PERM;
|
|
|
|
break;
|
|
|
|
case TFTP_ERR_DISKFULL:
|
2007-08-31 04:34:57 +08:00
|
|
|
code = CURLE_REMOTE_DISK_FULL;
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
2007-09-08 04:35:37 +08:00
|
|
|
case TFTP_ERR_UNDEF:
|
2005-09-02 23:11:08 +08:00
|
|
|
case TFTP_ERR_ILLEGAL:
|
|
|
|
code = CURLE_TFTP_ILLEGAL;
|
|
|
|
break;
|
|
|
|
case TFTP_ERR_UNKNOWNID:
|
|
|
|
code = CURLE_TFTP_UNKNOWNID;
|
|
|
|
break;
|
|
|
|
case TFTP_ERR_EXISTS:
|
2007-08-31 04:34:57 +08:00
|
|
|
code = CURLE_REMOTE_FILE_EXISTS;
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_NOSUCHUSER:
|
|
|
|
code = CURLE_TFTP_NOSUCHUSER;
|
|
|
|
break;
|
|
|
|
case TFTP_ERR_TIMEOUT:
|
2007-08-31 04:34:57 +08:00
|
|
|
code = CURLE_OPERATION_TIMEDOUT;
|
2005-09-02 23:11:08 +08:00
|
|
|
break;
|
|
|
|
case TFTP_ERR_NORESPONSE:
|
|
|
|
code = CURLE_COULDNT_CONNECT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
code= CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
code = CURLE_OK;
|
|
|
|
return code;
|
|
|
|
}
|
2007-10-12 21:36:37 +08:00
|
|
|
|
|
|
|
static CURLcode Curl_tftp_setup_connection(struct connectdata * conn)
|
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
char * type;
|
|
|
|
char command;
|
|
|
|
|
|
|
|
conn->socktype = SOCK_DGRAM; /* UDP datagram based */
|
|
|
|
|
|
|
|
/* TFTP URLs support an extension like ";mode=<typecode>" that
|
|
|
|
* we'll try to get now! */
|
2007-11-25 07:16:55 +08:00
|
|
|
type = strstr(data->state.path, ";mode=");
|
2007-10-12 21:36:37 +08:00
|
|
|
|
2007-11-07 17:21:35 +08:00
|
|
|
if(!type)
|
2007-10-12 21:36:37 +08:00
|
|
|
type = strstr(conn->host.rawalloc, ";mode=");
|
|
|
|
|
2007-11-07 17:21:35 +08:00
|
|
|
if(type) {
|
2007-10-12 21:36:37 +08:00
|
|
|
*type = 0; /* it was in the middle of the hostname */
|
|
|
|
command = (char) toupper((int) type[6]);
|
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
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 23:11:08 +08:00
|
|
|
#endif
|