Only in curl-7.19.2: CVS
Only in curl-7.19.2: .cvsignore
Only in curl-7.19.2/docs: CVS
Only in curl-7.19.2/docs: .cvsignore
diff -ur orig/curl-7.19.2/docs/libcurl/curl_easy_setopt.3 curl-7.19.2/docs/libcurl/curl_easy_setopt.3
--- orig/curl-7.19.2/docs/libcurl/curl_easy_setopt.3	2008-11-05 13:48:00.000000000 -0800
+++ curl-7.19.2/docs/libcurl/curl_easy_setopt.3	2009-01-16 14:56:25.000000000 -0800
@@ -18,7 +18,7 @@
 .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 .\" * KIND, either express or implied.
 .\" *
-.\" * $Id: curl_easy_setopt.3,v 1.238 2008-11-05 21:48:00 bagder Exp $
+.\" * $Id: curl_easy_setopt.3,v 1.4 2009/01/16 22:56:25 cmonroe Exp $
 .\" **************************************************************************
 .\"
 .TH curl_easy_setopt 3 "28 Oct 2008" "libcurl 7.19.1" "libcurl Manual"
@@ -989,6 +989,14 @@
 transfer decoding will be disabled, if set to 1 it is enabled
 (default). libcurl does chunked transfer decoding by default unless this
 option is set to zero. (added in 7.16.2)
+.SH TFTP OPTIONS
+.IP CURLOPT_TFTPBLKSIZE
+Specify block size to use for TFTP data transmission. Valid range as per 
+RFC 2348 is 8-65464 bytes. The default of 512 bytes will be used if this 
+option is not specified. The specified block size will only be used pending
+support by the remote server. If the server does not return an option 
+acknowledgement or returns an option acknowledgement with no blksize, the
+default of 512 bytes will be used.
 .SH FTP OPTIONS
 .IP CURLOPT_FTPPORT
 Pass a pointer to a zero terminated string as parameter. It will be used to
Only in curl-7.19.2/docs/libcurl: CVS
Only in curl-7.19.2/docs/libcurl: .cvsignore
diff -ur orig/curl-7.19.2/include/curl/curl.h curl-7.19.2/include/curl/curl.h
--- orig/curl-7.19.2/include/curl/curl.h	2008-10-16 22:58:26.000000000 -0700
+++ curl-7.19.2/include/curl/curl.h	2009-01-16 14:56:25.000000000 -0800
@@ -20,7 +20,7 @@
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  * KIND, either express or implied.
  *
- * $Id: curl.h,v 1.370 2008-10-17 03:59:02 yangtse Exp $
+ * $Id: curl.h,v 1.3 2009/01/16 22:56:25 cmonroe Exp $
  ***************************************************************************/
 
 /*
@@ -1149,6 +1149,9 @@
   CINIT(PROXYUSERNAME, OBJECTPOINT, 175),
   CINIT(PROXYPASSWORD, OBJECTPOINT, 176),
 
+  /* block size for TFTP transfers */
+  CINIT(TFTP_BLKSIZE, LONG, 177),
+
   CURLOPT_LASTENTRY /* the last unused */
 } CURLoption;
 
Only in curl-7.19.2/include/curl: CVS
Only in curl-7.19.2/include/curl: .cvsignore
Only in curl-7.19.2/include: CVS
Only in curl-7.19.2/include: .cvsignore
Only in curl-7.19.2/lib: CVS
Only in curl-7.19.2/lib: .cvsignore
diff -ur orig/curl-7.19.2/lib/tftp.c curl-7.19.2/lib/tftp.c
--- orig/curl-7.19.2/lib/tftp.c	2008-09-29 14:45:38.000000000 -0700
+++ curl-7.19.2/lib/tftp.c	2009-02-10 23:50:31.000000000 -0800
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
@@ -18,7 +18,7 @@
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  * KIND, either express or implied.
  *
- * $Id: tftp.c,v 1.73 2008-09-29 21:44:50 danf Exp $
+ * $Id: tftp.c,v 1.13 2009/02/11 07:50:31 cmonroe Exp $
  ***************************************************************************/
 
 #include "setup.h"
@@ -26,7 +26,6 @@
 #ifndef CURL_DISABLE_TFTP
 /* -- WIN32 approved -- */
 #include <stdio.h>
-#include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <ctype.h>
@@ -52,7 +51,9 @@
 #ifdef HAVE_NET_IF_H
 #include <net/if.h>
 #endif
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
+#endif
 
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
@@ -69,7 +70,9 @@
 #include "connect.h"
 #include "strerror.h"
 #include "sockaddr.h" /* required for Curl_sockaddr_storage */
+#include "multiif.h"
 #include "url.h"
+#include "rawstr.h"
 
 #define _MPRINTF_REPLACE /* use our functions only */
 #include <curl/mprintf.h>
@@ -80,8 +83,13 @@
 /* The last #include file should be: */
 #include "memdebug.h"
 
-/* RFC2348 allows the block size to be negotiated, but we don't support that */
-#define TFTP_BLOCKSIZE 512
+/* 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"
+#define TFTP_OPTION_TSIZE "tsize"
+#define TFTP_OPTION_INTERVAL "interval"
 
 typedef enum {
   TFTP_MODE_NETASCII=0,
@@ -96,12 +104,14 @@
 } tftp_state_t;
 
 typedef enum {
-  TFTP_EVENT_INIT=0,
+  TFTP_EVENT_NONE = -1,
+  TFTP_EVENT_INIT = 0,
   TFTP_EVENT_RRQ = 1,
   TFTP_EVENT_WRQ = 2,
   TFTP_EVENT_DATA = 3,
   TFTP_EVENT_ACK = 4,
   TFTP_EVENT_ERROR = 5,
+  TFTP_EVENT_OACK = 6,
   TFTP_EVENT_TIMEOUT
 } tftp_event_t;
 
@@ -122,13 +132,14 @@
 } tftp_error_t;
 
 typedef struct tftp_packet {
-  unsigned char data[2 + 2 + TFTP_BLOCKSIZE];
+  unsigned char *data;
 } tftp_packet_t;
 
 typedef struct tftp_state_data {
   tftp_state_t    state;
   tftp_mode_t     mode;
   tftp_error_t    error;
+  tftp_event_t    event;
   struct connectdata      *conn;
   curl_socket_t   sockfd;
   int             retries;
@@ -136,12 +147,15 @@
   int             retry_max;
   time_t          start_time;
   time_t          max_time;
+  time_t	  rx_time;
   unsigned short  block;
   struct Curl_sockaddr_storage   local_addr;
   struct Curl_sockaddr_storage   remote_addr;
   socklen_t       remote_addrlen;
   ssize_t         rbytes;
-  int             sbytes;
+  size_t          sbytes;
+  size_t          blksize;
+  int             requested_blksize;
   tftp_packet_t   rpacket;
   tftp_packet_t   spacket;
 } tftp_state_data_t;
@@ -151,10 +165,16 @@
 static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) ;
 static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) ;
 static CURLcode tftp_connect(struct connectdata *conn, bool *done);
+static CURLcode tftp_disconnect(struct connectdata *conn);
 static CURLcode tftp_do(struct connectdata *conn, bool *done);
 static CURLcode tftp_done(struct connectdata *conn,
                                CURLcode, bool premature);
 static CURLcode tftp_setup_connection(struct connectdata * conn);
+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);
+
 
 
 /*
@@ -168,11 +188,11 @@
   tftp_done,                            /* done */
   ZERO_NULL,                            /* do_more */
   tftp_connect,                         /* connect_it */
-  ZERO_NULL,                            /* connecting */
-  ZERO_NULL,                            /* doing */
-  ZERO_NULL,                            /* proto_getsock */
-  ZERO_NULL,                            /* doing_getsock */
-  ZERO_NULL,                            /* disconnect */
+  tftp_multi_statemach,                 /* connecting */
+  tftp_doing,                           /* doing */
+  tftp_getsock,                         /* proto_getsock */
+  tftp_getsock,                         /* doing_getsock */
+  tftp_disconnect,                      /* disconnect */
   PORT_TFTP,                            /* defport */
   PROT_TFTP                             /* protocol */
 };
@@ -192,11 +212,12 @@
 {
   time_t maxtime, timeout;
   long timeout_ms;
+  bool start = (bool)(state->state == TFTP_STATE_START);
 
   time(&state->start_time);
 
   /* Compute drop-dead time */
-  timeout_ms = Curl_timeleft(state->conn, NULL, TRUE);
+  timeout_ms = Curl_timeleft(state->conn, NULL, start);
 
   if(timeout_ms < 0) {
     /* time-out, bail out, go home */
@@ -204,7 +225,7 @@
     return CURLE_OPERATION_TIMEDOUT;
   }
 
-  if(state->state == TFTP_STATE_START) {
+  if(start) {
 
     maxtime = (time_t)(timeout_ms + 500) / 1000;
     state->max_time = state->start_time+maxtime;
@@ -239,23 +260,27 @@
     /* Average reposting an ACK after 15 seconds */
     state->retry_max = timeout/15;
   }
-  /* But bound the total number  */
+  /* But bound the total number */
   if(state->retry_max<3)
     state->retry_max=3;
 
   if(state->retry_max>50)
     state->retry_max=50;
 
-  /* Compute the re-ACK interval to suit the timeout */
+  /* Compute the re-ACK interval to suit the timeout 
   state->retry_time = timeout/state->retry_max;
   if(state->retry_time<1)
     state->retry_time=1;
+  */
 
   infof(state->conn->data,
-  	"set timeouts for state %d; Total %d, retry %d maxtry %d\n",
+        "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);
 
+  /* init RX time */
+  time(&state->rx_time);
+
   return CURLE_OK;
 }
 
@@ -290,11 +315,143 @@
   return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
 }
 
+static size_t Curl_strnlen(const char *string, size_t maxlen)
+{
+  const char *end = memchr (string, '\0', maxlen);
+  return end ? (size_t) (end - string) : maxlen;
+}
+
+static const char *tftp_option_get(const char *buf, size_t len,
+                                   const char **option, const char **value)
+{
+  size_t loc;
+
+  loc = Curl_strnlen( buf, len );
+  loc++; /* NULL term */
+
+  if (loc >= len)
+    return NULL;
+  *option = buf;
+
+  loc += Curl_strnlen( buf+loc, len-loc );
+  loc++; /* NULL term */
+
+  if (loc > len)
+    return NULL;
+  *value = &buf[strlen(*option) + 1];
+
+  return &buf[loc];
+}
+
+static CURLcode tftp_parse_option_ack(tftp_state_data_t *state,
+                                      const char *ptr, int len)
+{
+  const char *tmp = ptr;
+  struct SessionHandle *data = state->conn->data;
+
+  /* if OACK doesn't contain blksize option, the default (512) must be used */
+  state->blksize = TFTP_BLKSIZE_DEFAULT;
+
+  while (tmp < ptr + len) {
+    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)) {
+      int blksize;
+
+      blksize = strtol( value, NULL, 10 );
+
+      if(!blksize) {
+        failf(data, "invalid blocksize value in OACK packet");
+        return CURLE_TFTP_ILLEGAL;
+      }
+      else if(blksize > TFTP_BLKSIZE_MAX) {
+        failf(data, "%s (%d)", "blksize is larger than max supported",
+                TFTP_BLKSIZE_MAX);
+        return CURLE_TFTP_ILLEGAL;
+      }
+      else if(blksize < TFTP_BLKSIZE_MIN) {
+        failf(data, "%s (%d)", "blksize is smaller than min supported",
+                TFTP_BLKSIZE_MIN);
+        return CURLE_TFTP_ILLEGAL;
+      }
+      else if (blksize > state->requested_blksize) {
+        /* could realloc pkt buffers here, but the spec doesn't call out
+         * support for the server requesting a bigger blksize than the client
+         * requests */
+        failf(data, "%s (%d)",
+                "server requested blksize larger than allocated", blksize);
+        return CURLE_TFTP_ILLEGAL;
+      }
+
+      state->blksize = blksize;
+      infof(data, "%s (%d) %s (%d)\n", "blksize parsed from OACK",
+        state->blksize, "requested", state->requested_blksize);
+    }
+    else if(checkprefix(option, TFTP_OPTION_TSIZE)) {
+      long tsize = 0;
+
+      tsize = strtol( value, NULL, 10 );
+      if(!tsize) {
+        failf(data, "invalid tsize value in OACK packet");
+        return CURLE_TFTP_ILLEGAL;
+      }
+      Curl_pgrsSetDownloadSize(data, tsize);
+      infof(data, "%s (%d)\n", "tsize parsed from OACK", tsize);
+    }
+  }
+
+  return CURLE_OK;
+}
+
+static size_t tftp_option_add(tftp_state_data_t *state, size_t csize,
+                              char *buf, const char *option)
+{
+  if( ( strlen(option) + csize + 1U ) > state->blksize )
+        return 0;
+  strcpy(buf, option);
+  return( strlen(option) + 1 );
+}
+
+static CURLcode tftp_connect_for_tx(tftp_state_data_t *state, tftp_event_t event)
+{
+  CURLcode res;
+  struct SessionHandle *data = state->conn->data;
+
+  infof(data, "%s\n", "Connected for transmit");
+  state->state = TFTP_STATE_TX;
+  res = tftp_set_timeouts(state);
+  if(res != CURLE_OK)
+    return(res);
+  return tftp_tx(state, event);
+}
+
+static CURLcode tftp_connect_for_rx(tftp_state_data_t *state, tftp_event_t event)
+{
+  CURLcode res;
+  struct SessionHandle *data = state->conn->data;
+
+  infof(data, "%s\n", "Connected for receive");
+  state->state = TFTP_STATE_RX;
+  res = tftp_set_timeouts(state);
+  if(res != CURLE_OK)
+    return(res);
+  return tftp_rx(state, event);
+}
+
 static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
 {
   int sbytes;
   const char *mode = "octet";
   char *filename;
+  char buf[8];
   struct SessionHandle *data = state->conn->data;
   CURLcode res = CURLE_OK;
 
@@ -318,7 +475,7 @@
       /* If we are uploading, send an WRQ */
       setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
       state->conn->data->req.upload_fromhere =
-        (char *)&state->spacket.data[4];
+        (char *)state->spacket.data+4;
       if(data->set.infilesize != -1)
         Curl_pgrsSetUploadSize(data, data->set.infilesize);
     }
@@ -327,17 +484,40 @@
       setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
     }
     /* 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. */
+       file name so we skip the always-present first letter of the path
+       string. */
     filename = curl_easy_unescape(data, &state->conn->data->state.path[1], 0,
                                   NULL);
     if(!filename)
       return CURLE_OUT_OF_MEMORY;
 
-    snprintf((char *)&state->spacket.data[2],
-             TFTP_BLOCKSIZE,
+    snprintf((char *)state->spacket.data+2,
+             state->blksize,
              "%s%c%s%c", filename, '\0',  mode, '\0');
-    sbytes = 4 + (int)strlen(filename) + (int)strlen(mode);
-    sbytes = sendto(state->sockfd, (void *)&state->spacket,
+    sbytes = 4 + strlen(filename) + strlen(mode);
+
+    /* add tsize option */
+    sbytes += tftp_option_add(state, sbytes,
+                              (char *)state->spacket.data+sbytes,
+                              TFTP_OPTION_TSIZE);
+    sbytes += tftp_option_add(state, sbytes,
+                              (char *)state->spacket.data+sbytes, "0");
+    /* add blksize option */
+    snprintf( buf, sizeof(buf), "%d", state->requested_blksize );
+    sbytes += tftp_option_add(state, sbytes,
+                              (char *)state->spacket.data+sbytes,
+                              TFTP_OPTION_BLKSIZE);
+    sbytes += tftp_option_add(state, sbytes,
+                              (char *)state->spacket.data+sbytes, buf );
+    /* add timeout option, this is the max time the session may live */
+    snprintf( buf, sizeof(buf), "%d", state->retry_time*state->retry_max );
+    sbytes += tftp_option_add(state, sbytes,
+                              (char *)state->spacket.data+sbytes,
+                              TFTP_OPTION_INTERVAL);
+    sbytes += tftp_option_add(state, sbytes,
+                              (char *)state->spacket.data+sbytes, buf );
+
+    sbytes = sendto(state->sockfd, (void *)state->spacket.data,
                     sbytes, 0,
                     state->conn->ip_addr->ai_addr,
                     state->conn->ip_addr->ai_addrlen);
@@ -347,21 +527,22 @@
     Curl_safefree(filename);
     break;
 
+  case TFTP_EVENT_OACK:
+    if(data->set.upload) {
+      res = tftp_connect_for_tx(state, event);
+    }
+    else {
+      res = tftp_connect_for_rx(state, event);
+    }
+    break;
+
   case TFTP_EVENT_ACK: /* Connected for transmit */
-    infof(data, "%s\n", "Connected for transmit");
-    state->state = TFTP_STATE_TX;
-    res = tftp_set_timeouts(state);
-    if(res)
-      break;
-    return tftp_tx(state, event);
+    res = tftp_connect_for_tx(state, event);
+    break;
 
-  case TFTP_EVENT_DATA: /* connected for receive */
-    infof(data, "%s\n", "Connected for receive");
-    state->state = TFTP_STATE_RX;
-    res = tftp_set_timeouts(state);
-    if(res)
-      break;
-    return tftp_rx(state, event);
+  case TFTP_EVENT_DATA: /* Connected for receive */
+    res = tftp_connect_for_rx(state, event);
+    break;
 
   case TFTP_EVENT_ERROR:
     state->state = TFTP_STATE_FIN;
@@ -390,7 +571,6 @@
   switch(event) {
 
   case TFTP_EVENT_DATA:
-
     /* Is this the block we expect? */
     rblock = getrpacketblock(&state->rpacket);
     if((state->block+1) != rblock) {
@@ -419,12 +599,33 @@
     }
 
     /* Check if completed (That is, a less than full packet is received) */
-    if(state->rbytes < (ssize_t)sizeof(state->spacket)){
+    if(state->rbytes < (ssize_t)state->blksize+4){
       state->state = TFTP_STATE_FIN;
     }
     else {
       state->state = TFTP_STATE_RX;
     }
+    time(&state->rx_time);
+    break;
+
+  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);
+    sbytes = sendto(state->sockfd, (void *)state->spacket.data,
+                    4, SEND_4TH_ARG,
+                    (struct sockaddr *)&state->remote_addr,
+                    state->remote_addrlen);
+    if(sbytes < 0) {
+      failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
+      return CURLE_SEND_ERROR;
+    }
+
+    /* we're ready to RX data */
+    state->state = TFTP_STATE_RX;
+    time(&state->rx_time);
     break;
 
   case TFTP_EVENT_TIMEOUT:
@@ -438,7 +639,7 @@
     }
     else {
       /* Resend the previous ACK */
-      sbytes = sendto(state->sockfd, (void *)&state->spacket,
+      sbytes = sendto(state->sockfd, (void *)state->spacket.data,
                       4, SEND_4TH_ARG,
                       (struct sockaddr *)&state->remote_addr,
                       state->remote_addrlen);
@@ -451,6 +652,14 @@
     break;
 
   case TFTP_EVENT_ERROR:
+    setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
+    setpacketblock(&state->spacket, state->block);
+    sbytes = sendto(state->sockfd, (void *)state->spacket.data,
+                    4, SEND_4TH_ARG,
+                    (struct sockaddr *)&state->remote_addr,
+                    state->remote_addrlen);
+    /* don't bother with the return code, but if the socket is still up we
+     * should be a good TFTP client and let the server know we're done */
     state->state = TFTP_STATE_FIN;
     break;
 
@@ -510,15 +719,17 @@
     }
     /* This is the expected packet.  Reset the counters and send the next
        block */
+    time(&state->rx_time);
     state->block++;
     state->retries = 0;
     setpacketevent(&state->spacket, TFTP_EVENT_DATA);
     setpacketblock(&state->spacket, state->block);
-    if(state->block > 1 && state->sbytes < TFTP_BLOCKSIZE) {
+    if(state->block > 1 && state->sbytes < state->blksize) {
       state->state = TFTP_STATE_FIN;
       return CURLE_OK;
     }
-    res = Curl_fillreadbuffer(state->conn, TFTP_BLOCKSIZE, &state->sbytes);
+    res = Curl_fillreadbuffer(state->conn, state->blksize,
+                              (int *)&state->sbytes);
     if(res)
       return res;
     sbytes = sendto(state->sockfd, (void *)state->spacket.data,
@@ -547,7 +758,7 @@
     }
     else {
       /* Re-send the data packet */
-      sbytes = sendto(state->sockfd, (void *)&state->spacket,
+      sbytes = sendto(state->sockfd, (void *)state->spacket.data,
                       4+state->sbytes, SEND_4TH_ARG,
                       (struct sockaddr *)&state->remote_addr,
                       state->remote_addrlen);
@@ -563,6 +774,15 @@
 
   case TFTP_EVENT_ERROR:
     state->state = TFTP_STATE_FIN;
+    setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
+    setpacketblock(&state->spacket, state->block);
+    sbytes = sendto(state->sockfd, (void *)state->spacket.data,
+                    4, SEND_4TH_ARG,
+                    (struct sockaddr *)&state->remote_addr,
+                    state->remote_addrlen);
+    /* don't bother with the return code, but if the socket is still up we
+     * should be a good TFTP client and let the server know we're done */
+    state->state = TFTP_STATE_FIN;
     break;
 
   default:
@@ -575,6 +795,59 @@
 
 /**********************************************************
  *
+ * tftp_translate_code
+ *
+ * Translate internal error codes to CURL error codes 
+ *
+ **********************************************************/
+CURLcode tftp_translate_code(tftp_error_t error)
+{
+  CURLcode code = CURLE_OK;
+
+  if(error != TFTP_ERR_NONE) {
+    switch(error) {
+    case TFTP_ERR_NOTFOUND:
+      code = CURLE_TFTP_NOTFOUND;
+      break;
+    case TFTP_ERR_PERM:
+      code = CURLE_TFTP_PERM;
+      break;
+    case TFTP_ERR_DISKFULL:
+      code = CURLE_REMOTE_DISK_FULL;
+      break;
+    case TFTP_ERR_UNDEF:
+    case TFTP_ERR_ILLEGAL:
+      code = CURLE_TFTP_ILLEGAL;
+      break;
+    case TFTP_ERR_UNKNOWNID:
+      code = CURLE_TFTP_UNKNOWNID;
+      break;
+    case TFTP_ERR_EXISTS:
+      code = CURLE_REMOTE_FILE_EXISTS;
+      break;
+    case TFTP_ERR_NOSUCHUSER:
+      code = CURLE_TFTP_NOSUCHUSER;
+      break;
+    case TFTP_ERR_TIMEOUT:
+      code = CURLE_OPERATION_TIMEDOUT;
+      break;
+    case TFTP_ERR_NORESPONSE:
+      code = CURLE_COULDNT_CONNECT;
+      break;
+    default:
+      code= CURLE_ABORTED_BY_CALLBACK;
+      break;
+    }
+  }
+  else {
+    code = CURLE_OK;
+  }
+
+  return(code);
+}
+
+/**********************************************************
+ *
  * tftp_state_machine
  *
  * The tftp state machine event dispatcher
@@ -610,6 +883,26 @@
   return res;
 }
 
+/**********************************************************
+ *
+ * tftp_disconnect
+ *
+ * The disconnect callback
+ *
+ **********************************************************/
+static CURLcode tftp_disconnect(struct connectdata *conn)
+{
+  tftp_state_data_t *state = conn->proto.tftpc;
+
+  /* done, free dynamically allocated pkt buffers */
+  if(state) {
+    Curl_safefree(state->rpacket.data);
+    Curl_safefree(state->spacket.data);
+    free(state);
+  }
+
+  return CURLE_OK;
+}
 
 /**********************************************************
  *
@@ -622,26 +915,50 @@
 {
   CURLcode code;
   tftp_state_data_t *state;
-  int rc;
+  int blksize, rc;
+
+  blksize = TFTP_BLKSIZE_DEFAULT;
 
   /* If there already is a protocol-specific struct allocated for this
      sessionhandle, deal with it */
   Curl_reset_reqproto(conn);
 
-  state = conn->data->state.proto.tftp;
+  state = conn->proto.tftpc;
   if(!state) {
-    state = conn->data->state.proto.tftp = calloc(sizeof(tftp_state_data_t),
-                                                  1);
+    state = conn->proto.tftpc = calloc(sizeof(tftp_state_data_t), 1);
     if(!state)
       return CURLE_OUT_OF_MEMORY;
   }
 
+  /* alloc pkt buffers based on specified blksize */
+  if(conn->data->set.tftp_blksize) {
+    blksize = conn->data->set.tftp_blksize;
+    if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN )
+      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)
+      return CURLE_OUT_OF_MEMORY;
+  }
+
   conn->bits.close = FALSE; /* keep it open if possible */
 
   state->conn = conn;
   state->sockfd = state->conn->sock[FIRSTSOCKET];
   state->state = TFTP_STATE_START;
   state->error = TFTP_ERR_NONE;
+  state->blksize = TFTP_BLKSIZE_DEFAULT;
+  state->requested_blksize = blksize;
 
   ((struct sockaddr *)&state->local_addr)->sa_family =
     (unsigned short)(conn->ip_addr->ai_family);
@@ -689,58 +1006,176 @@
 static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
                                bool premature)
 {
+  CURLcode code = CURLE_OK;
+  tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
+
   (void)status; /* unused */
   (void)premature; /* not used */
 
   Curl_pgrsDone(conn);
 
-  return CURLE_OK;
-}
+  /* If we have encountered an error */
+  code = tftp_translate_code(state->error);
 
+  return code;
+}
 
 /**********************************************************
  *
- * tftp
+ * tftp_getsock
  *
- * The do callback
- *
- * This callback handles the entire TFTP transfer
+ * The getsock callback
  *
  **********************************************************/
+static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks,
+                       int numsocks)
+{
+  if(!numsocks)
+    return GETSOCK_BLANK;
 
-static CURLcode tftp_do(struct connectdata *conn, bool *done)
+  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) 
 {
-  struct SessionHandle  *data = conn->data;
-  tftp_state_data_t     *state;
-  tftp_event_t          event;
-  CURLcode              code;
-  int                   rc;
   struct Curl_sockaddr_storage fromaddr;
   socklen_t             fromlen;
-  int                   check_time = 0;
-  struct SingleRequest *k = &data->req;
+  CURLcode 		result = CURLE_OK;
+  struct SessionHandle  *data = conn->data;
+  tftp_state_data_t	*state = (tftp_state_data_t *)conn->proto.tftpc;
+  struct SingleRequest	*k = &data->req;
 
-  *done = TRUE;
+  /* Receive the packet */
+  fromlen = sizeof(fromaddr);
+  state->rbytes = (ssize_t)recvfrom(state->sockfd,
+                                   (void *)state->rpacket.data,
+                                   state->blksize+4,
+                                   0,
+                                   (struct sockaddr *)&fromaddr,
+                                   &fromlen);
+  if(state->remote_addrlen==0) {
+    memcpy(&state->remote_addr, &fromaddr, fromlen);
+    state->remote_addrlen = fromlen;
+  }
 
-  /*
-    Since connections can be re-used between SessionHandles, this might be a
-    connection already existing but on a fresh SessionHandle struct so we must
-    make sure we have a good 'struct TFTP' to play with. For new connections,
-    the struct TFTP is allocated and setup in the tftp_connect() function.
-  */
-  Curl_reset_reqproto(conn);
+  /* 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 */
+    state->event = (tftp_event_t)getrpacketevent(&state->rpacket);
 
-  if(!data->state.proto.tftp) {
-    code = tftp_connect(conn, done);
-    if(code)
-      return code;
+    switch(state->event) {
+    case TFTP_EVENT_DATA:
+      /* Don't pass to the client empty or retransmitted packets */
+      if(state->rbytes > 4 &&
+          ((state->block+1) == getrpacketblock(&state->rpacket))) {
+        result = Curl_client_write(conn, CLIENTWRITE_BODY,
+                                 (char *)state->rpacket.data+4,
+                                 state->rbytes-4);
+        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:
+      state->error = (tftp_error_t)getrpacketblock(&state->rpacket);
+      infof(data, "%s\n", (const char *)state->rpacket.data+4);
+      break;
+    case TFTP_EVENT_ACK:
+      break;
+    case TFTP_EVENT_OACK:
+      result = tftp_parse_option_ack(state,
+                                   (const char *)state->rpacket.data+2,
+                                   state->rbytes-2);
+      if(result)
+        return result;
+      break;
+    case TFTP_EVENT_RRQ:
+    case TFTP_EVENT_WRQ:
+    default:
+      failf(data, "%s", "Internal error: Unexpected packet");
+      break;
+    }
+
+    /* Update the progress meter */
+    if(Curl_pgrsUpdate(conn)) {
+      tftp_state_machine(state, TFTP_EVENT_ERROR);
+      return CURLE_ABORTED_BY_CALLBACK;
+    }
+  }
+  return result;
+}
+
+/**********************************************************
+ *
+ * tftp_state_timeout 
+ *
+ * Check if timeouts have been reached 
+ *
+ **********************************************************/
+static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event)
+{
+  time_t 		current;
+  struct SessionHandle  *data = conn->data;
+  tftp_state_data_t	*state = (tftp_state_data_t *)conn->proto.tftpc;
+
+  if (event)
+    *event = TFTP_EVENT_NONE;
+
+  time(&current);
+  if(current > state->max_time) {
+    DEBUGF(infof(data, "timeout: %d > %d\n",
+                 current, state->max_time));
+    state->error = TFTP_ERR_TIMEOUT;
+    state->state = TFTP_STATE_FIN;
+    return(0);
+  } 
+  else if (current > state->rx_time+state->retry_time) {
+    if (event)
+      *event = TFTP_EVENT_TIMEOUT;
+    time(&state->rx_time); /* update even though we received nothing */
+    return(state->max_time-current);
+  }
+  else {
+    return(state->max_time-current);
   }
-  state = (tftp_state_data_t *)data->state.proto.tftp;
+}
+
+/**********************************************************
+ *
+ * tftp_easy_statemach 
+ *
+ * Handle easy request until completion 
+ *
+ **********************************************************/
+static CURLcode tftp_easy_statemach(struct connectdata *conn)
+{
+  int			rc;
+  int			check_time = 0;
+  CURLcode		result = CURLE_OK;
+  struct SessionHandle  *data = conn->data;
+  tftp_state_data_t	*state = (tftp_state_data_t *)conn->proto.tftpc;
 
   /* Run the TFTP State Machine */
-  for(code=tftp_state_machine(state, TFTP_EVENT_INIT);
-      (state->state != TFTP_STATE_FIN) && (code == CURLE_OK);
-      code=tftp_state_machine(state, event) ) {
+  for(;
+      (state->state != TFTP_STATE_FIN) && (result == CURLE_OK);
+      result=tftp_state_machine(state, state->event) ) {
 
     /* Wait until ready to read or timeout occurs */
     rc=Curl_socket_ready(state->sockfd, CURL_SOCKET_BAD,
@@ -750,136 +1185,184 @@
       /* bail out */
       int error = SOCKERRNO;
       failf(data, "%s", Curl_strerror(conn, error));
-      event = TFTP_EVENT_ERROR;
+      state->event = TFTP_EVENT_ERROR;
     }
     else if(rc==0) {
       /* A timeout occured */
-      event = TFTP_EVENT_TIMEOUT;
+      state->event = TFTP_EVENT_TIMEOUT;
 
       /* Force a look at transfer timeouts */
       check_time = 0;
 
     }
     else {
-
-      /* Receive the packet */
-      fromlen = sizeof(fromaddr);
-      state->rbytes = (ssize_t)recvfrom(state->sockfd,
-                                        (void *)&state->rpacket,
-                                        sizeof(state->rpacket),
-                                        0,
-                                        (struct sockaddr *)&fromaddr,
-                                        &fromlen);
-      if(state->remote_addrlen==0) {
-        memcpy(&state->remote_addr, &fromaddr, fromlen);
-        state->remote_addrlen = fromlen;
-      }
-
-      /* Sanity check packet length */
-      if(state->rbytes < 4) {
-        failf(data, "Received too short packet");
-        /* Not a timeout, but how best to handle it? */
-        event = TFTP_EVENT_TIMEOUT;
-      }
-      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 */
-          if(state->rbytes > 4 &&
-              ((state->block+1) == getrpacketblock(&state->rpacket))) {
-            code = Curl_client_write(conn, CLIENTWRITE_BODY,
-                                     (char *)&state->rpacket.data[4],
-                                     state->rbytes-4);
-            if(code)
-              return code;
-            k->bytecount += state->rbytes-4;
-            Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
-          }
-          break;
-        case TFTP_EVENT_ERROR:
-          state->error = (tftp_error_t)getrpacketblock(&state->rpacket);
-          infof(data, "%s\n", (const char *)&state->rpacket.data[4]);
-          break;
-        case TFTP_EVENT_ACK:
-          break;
-        case TFTP_EVENT_RRQ:
-        case TFTP_EVENT_WRQ:
-        default:
-          failf(data, "%s", "Internal error: Unexpected packet");
-          break;
-        }
-
-        /* Update the progress meter */
-        if(Curl_pgrsUpdate(conn))
-          return CURLE_ABORTED_BY_CALLBACK;
-      }
+        result = tftp_receive_packet(conn);
     }
 
     /* Check for transfer timeout every 10 blocks, or after timeout */
     if(check_time%10==0) {
-      time_t current;
-      time(&current);
-      if(current>state->max_time) {
-        DEBUGF(infof(data, "timeout: %d > %d\n",
-                     current, state->max_time));
-        state->error = TFTP_ERR_TIMEOUT;
-        state->state = TFTP_STATE_FIN;
-      }
+      /* ignore the event here as Curl_socket_ready() handles
+       * retransmission timeouts inside the easy state mach */
+      tftp_state_timeout(conn, NULL);
     }
 
+    if(result)
+      return(result);
   }
-  if(code)
-    return code;
 
   /* Tell curl we're done */
-  code = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
-  if(code)
-    return code;
+  result = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
 
-  /* If we have encountered an error */
-  if(state->error != TFTP_ERR_NONE) {
+  return(result);
+}
 
-    /* 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:
-      code = CURLE_REMOTE_DISK_FULL;
-      break;
-    case TFTP_ERR_UNDEF:
-    case TFTP_ERR_ILLEGAL:
-      code = CURLE_TFTP_ILLEGAL;
-      break;
-    case TFTP_ERR_UNKNOWNID:
-      code = CURLE_TFTP_UNKNOWNID;
-      break;
-    case TFTP_ERR_EXISTS:
-      code = CURLE_REMOTE_FILE_EXISTS;
-      break;
-    case TFTP_ERR_NOSUCHUSER:
-      code = CURLE_TFTP_NOSUCHUSER;
-      break;
-    case TFTP_ERR_TIMEOUT:
-      code = CURLE_OPERATION_TIMEDOUT;
-      break;
-    case TFTP_ERR_NORESPONSE:
-      code = CURLE_COULDNT_CONNECT;
-      break;
-    default:
-      code= CURLE_ABORTED_BY_CALLBACK;
-      break;
+/**********************************************************
+ *
+ * tftp_multi_statemach 
+ *
+ * Handle single RX socket event and return 
+ *
+ **********************************************************/
+static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
+{
+  int			rc;
+  tftp_event_t		event;
+  CURLcode		result = CURLE_OK;
+  struct SessionHandle  *data = conn->data;
+  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;
+  }
+  else if (event != TFTP_EVENT_NONE) {
+    result = tftp_state_machine(state, event);
+    if(result != CURLE_OK)
+      return(result);
+    *done = (bool)(state->state == TFTP_STATE_FIN);
+    if(*done)
+      /* Tell curl we're done */
+      result = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
+  }
+  else {
+    /* no timeouts to handle, check our socket */	
+    rc = Curl_socket_ready(state->sockfd, CURL_SOCKET_BAD, 0);
+
+    if(rc == -1) {
+      /* bail out */
+      int error = SOCKERRNO;
+      failf(data, "%s", Curl_strerror(conn, error));
+      state->event = TFTP_EVENT_ERROR;
+    }
+    else if(rc != 0) {
+      result = tftp_receive_packet(conn); 
+      if(result != CURLE_OK)
+        return(result);
+      result = tftp_state_machine(state, state->event);
+      if(result != CURLE_OK)
+        return(result);
+      *done = (bool)(state->state == TFTP_STATE_FIN);
+      if(*done)
+        /* Tell curl we're done */
+        result = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
     }
+    /* if rc == 0, then select() timed out */
   }
-  else
-    code = CURLE_OK;
+
+  return result;
+}
+
+/**********************************************************
+ *
+ * tftp_doing 
+ *
+ * Called from multi.c while DOing 
+ *
+ **********************************************************/
+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"));
+  }
+  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);
+
+  if(state->state == TFTP_STATE_FIN || result != CURLE_OK)
+    return(result);
+
+  if(conn->data->state.used_interface == Curl_if_multi)
+    tftp_multi_statemach(conn, dophase_done);
+  else {
+    result = tftp_easy_statemach(conn);
+    *dophase_done = TRUE; /* with the easy interface we are done here */
+  }
+
+  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)
+{
+  tftp_state_data_t     *state;
+  CURLcode              code;
+
+  *done = FALSE;
+
+  /*
+    Since connections can be re-used between SessionHandles, this might be a
+    connection already existing but on a fresh SessionHandle struct so we must
+    make sure we have a good 'struct TFTP' to play with. For new connections,
+    the struct TFTP is allocated and setup in the tftp_connect() function.
+  */
+  Curl_reset_reqproto(conn);
+
+  if(!conn->proto.tftpc) {
+    code = tftp_connect(conn, done);
+    if(code)
+      return code;
+  }
+  state = (tftp_state_data_t *)conn->proto.tftpc;
+
+  code = tftp_perform(conn, done);
+
+  /* If we have encountered an error */
+  code = tftp_translate_code(state->error);
+
   return code;
 }
 
@@ -901,6 +1384,7 @@
   if(type) {
     *type = 0;                   /* it was in the middle of the hostname */
     command = (char) toupper((int) type[6]);
+    //command = Curl_raw_toupper(type[6]);
 
     switch (command) {
     case 'A': /* ASCII mode */
diff -ur orig/curl-7.19.2/lib/url.c curl-7.19.2/lib/url.c
--- orig/curl-7.19.2/lib/url.c	2008-11-11 05:20:02.000000000 -0800
+++ curl-7.19.2/lib/url.c	2009-01-16 14:56:26.000000000 -0800
@@ -18,7 +18,7 @@
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  * KIND, either express or implied.
  *
- * $Id: url.c,v 1.771 2008-11-05 21:46:40 bagder Exp $
+ * $Id: url.c,v 1.3 2009/01/16 22:56:26 cmonroe Exp $
  ***************************************************************************/
 
 /* -- WIN32 approved -- */
@@ -903,6 +903,12 @@
      */
     data->set.ftp_response_timeout = va_arg( param , long ) * 1000;
     break;
+  case CURLOPT_TFTP_BLKSIZE:
+    /*
+     * TFTP option that specifies the block size to use for data transmission
+     */
+    data->set.tftp_blksize = va_arg(param, long);
+    break;
   case CURLOPT_DIRLISTONLY:
     /*
      * An option that changes the command to one that asks for a list
diff -ur orig/curl-7.19.2/lib/urldata.h curl-7.19.2/lib/urldata.h
--- orig/curl-7.19.2/lib/urldata.h	2008-11-11 11:19:56.000000000 -0800
+++ curl-7.19.2/lib/urldata.h	2009-02-10 23:19:22.000000000 -0800
@@ -20,7 +20,7 @@
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  * KIND, either express or implied.
  *
- * $Id: urldata.h,v 1.395 2008-11-03 16:24:56 bagder Exp $
+ * $Id: urldata.h,v 1.5 2009/02/11 07:19:22 cmonroe Exp $
  ***************************************************************************/
 
 /* This file is for lib internal stuff */
@@ -894,8 +894,8 @@
 #define PROT_SCP     (1<<12)
 #define PROT_SFTP    (1<<13)
 
-#define PROT_CLOSEACTION PROT_FTP /* these ones need action before socket
-                                     close */
+/* these ones need action before socket close */
+#define PROT_CLOSEACTION (PROT_FTP | PROT_TFTP) 
 
   /* 'dns_entry' is the particular host we use. This points to an entry in the
      DNS cache and it will not get pruned while locked. It gets unlocked in
@@ -1029,6 +1029,7 @@
   union {
     struct ftp_conn ftpc;
     struct ssh_conn sshc;
+    struct tftp_state_data *tftpc;
   } proto;
 
   int cselect_bits; /* bitmask of socket events */
@@ -1404,6 +1405,7 @@
   long timeout;         /* in milliseconds, 0 means no timeout */
   long connecttimeout;  /* in milliseconds, 0 means no timeout */
   long ftp_response_timeout; /* in milliseconds, 0 means no timeout */
+  long tftp_blksize ; /* in bytes, 0 means use default */
   curl_off_t infilesize;      /* size of file to upload, -1 means unknown */
   long low_speed_limit; /* bytes/second */
   long low_speed_time;  /* number of seconds */
Only in orig/curl-7.19.2: Makefile

