Revert "Update to 7.44.0"
[platform/upstream/curl.git] / lib / tftp.c
index 4c5796f..b9723c0 100644 (file)
@@ -5,7 +5,7 @@
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
- * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
 #include "url.h"
 #include "rawstr.h"
 #include "speedcheck.h"
-#include "curl_printf.h"
-#include "select.h"
 
-/* The last #include files should be: */
+#define _MPRINTF_REPLACE /* use our functions only */
+#include <curl/mprintf.h>
+
 #include "curl_memory.h"
+#include "select.h"
+
+/* The last #include file should be: */
 #include "memdebug.h"
 
 /* RFC2348 allows the block size to be negotiated */
@@ -145,8 +148,8 @@ typedef struct tftp_state_data {
 
 
 /* Forward declarations */
-static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event);
-static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event);
+static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) ;
+static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) ;
 static CURLcode tftp_connect(struct connectdata *conn, bool *done);
 static CURLcode tftp_disconnect(struct connectdata *conn,
                                 bool dead_connection);
@@ -218,7 +221,7 @@ static CURLcode tftp_set_timeouts(tftp_state_data_t *state)
     state->max_time = state->start_time+maxtime;
 
     /* Set per-block timeout to total */
-    timeout = maxtime;
+    timeout = maxtime ;
 
     /* Average restart after 5 seconds */
     state->retry_max = (int)timeout/5;
@@ -408,7 +411,7 @@ static size_t tftp_option_add(tftp_state_data_t *state, size_t csize,
   if(( strlen(option) + csize + 1 ) > (size_t)state->blksize)
     return 0;
   strcpy(buf, option);
-  return strlen(option) + 1;
+  return( strlen(option) + 1 );
 }
 
 static CURLcode tftp_connect_for_tx(tftp_state_data_t *state,
@@ -423,7 +426,7 @@ static CURLcode tftp_connect_for_tx(tftp_state_data_t *state,
   state->state = TFTP_STATE_TX;
   result = tftp_set_timeouts(state);
   if(result)
-    return result;
+    return(result);
   return tftp_tx(state, event);
 }
 
@@ -439,7 +442,7 @@ static CURLcode tftp_connect_for_rx(tftp_state_data_t *state,
   state->state = TFTP_STATE_RX;
   result = tftp_set_timeouts(state);
   if(result)
-    return result;
+    return(result);
   return tftp_rx(state, event);
 }
 
@@ -451,7 +454,7 @@ static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
   char *filename;
   char buf[64];
   struct SessionHandle *data = state->conn->data;
-  CURLcode result = CURLE_OK;
+  CURLcode res = CURLE_OK;
 
   /* Set ascii mode if -B flag was used */
   if(data->set.prefer_ascii)
@@ -466,7 +469,7 @@ static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
     if(state->retries>state->retry_max) {
       state->error = TFTP_ERR_NORESPONSE;
       state->state = TFTP_STATE_FIN;
-      return result;
+      return res;
     }
 
     if(data->set.upload) {
@@ -531,24 +534,24 @@ static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
     if(senddata != (ssize_t)sbytes) {
       failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
     }
-    free(filename);
+    Curl_safefree(filename);
     break;
 
   case TFTP_EVENT_OACK:
     if(data->set.upload) {
-      result = tftp_connect_for_tx(state, event);
+      res = tftp_connect_for_tx(state, event);
     }
     else {
-      result = tftp_connect_for_rx(state, event);
+      res = tftp_connect_for_rx(state, event);
     }
     break;
 
   case TFTP_EVENT_ACK: /* Connected for transmit */
-    result = tftp_connect_for_tx(state, event);
+    res = tftp_connect_for_tx(state, event);
     break;
 
   case TFTP_EVENT_DATA: /* Connected for receive */
-    result = tftp_connect_for_rx(state, event);
+    res = tftp_connect_for_rx(state, event);
     break;
 
   case TFTP_EVENT_ERROR:
@@ -559,8 +562,7 @@ static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
     failf(state->conn->data, "tftp_send_first: internal error");
     break;
   }
-
-  return result;
+  return res;
 }
 
 /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
@@ -700,7 +702,7 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
   struct SessionHandle *data = state->conn->data;
   ssize_t sbytes;
   int rblock;
-  CURLcode result = CURLE_OK;
+  CURLcode res = CURLE_OK;
   struct SingleRequest *k = &data->req;
 
   switch(event) {
@@ -726,7 +728,7 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
         if(state->retries>state->retry_max) {
           failf(data, "tftp_tx: giving up waiting for block %d ack",
                 state->block);
-          result = CURLE_SEND_ERROR;
+          res = CURLE_SEND_ERROR;
         }
         else {
           /* Re-send the data packet */
@@ -737,11 +739,10 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
           /* Check all sbytes were sent */
           if(sbytes<0) {
             failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
-            result = CURLE_SEND_ERROR;
+            res = CURLE_SEND_ERROR;
           }
         }
-
-        return result;
+        return res;
       }
       /* This is the expected packet.  Reset the counters and send the next
          block */
@@ -758,13 +759,11 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
       state->state = TFTP_STATE_FIN;
       return CURLE_OK;
     }
-
-    result = Curl_fillreadbuffer(state->conn, state->blksize, &state->sbytes);
-    if(result)
-      return result;
-
-    sbytes = sendto(state->sockfd, (void *) state->spacket.data,
-                    4 + state->sbytes, SEND_4TH_ARG,
+    res = Curl_fillreadbuffer(state->conn, state->blksize, &state->sbytes);
+    if(res)
+      return res;
+    sbytes = sendto(state->sockfd, (void *)state->spacket.data,
+                    4+state->sbytes, SEND_4TH_ARG,
                     (struct sockaddr *)&state->remote_addr,
                     state->remote_addrlen);
     /* Check all sbytes were sent */
@@ -820,7 +819,7 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
     break;
   }
 
-  return result;
+  return res;
 }
 
 /**********************************************************
@@ -832,47 +831,48 @@ static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
  **********************************************************/
 static CURLcode tftp_translate_code(tftp_error_t error)
 {
-  CURLcode result = CURLE_OK;
+  CURLcode code = CURLE_OK;
 
   if(error != TFTP_ERR_NONE) {
     switch(error) {
     case TFTP_ERR_NOTFOUND:
-      result = CURLE_TFTP_NOTFOUND;
+      code = CURLE_TFTP_NOTFOUND;
       break;
     case TFTP_ERR_PERM:
-      result = CURLE_TFTP_PERM;
+      code = CURLE_TFTP_PERM;
       break;
     case TFTP_ERR_DISKFULL:
-      result = CURLE_REMOTE_DISK_FULL;
+      code = CURLE_REMOTE_DISK_FULL;
       break;
     case TFTP_ERR_UNDEF:
     case TFTP_ERR_ILLEGAL:
-      result = CURLE_TFTP_ILLEGAL;
+      code = CURLE_TFTP_ILLEGAL;
       break;
     case TFTP_ERR_UNKNOWNID:
-      result = CURLE_TFTP_UNKNOWNID;
+      code = CURLE_TFTP_UNKNOWNID;
       break;
     case TFTP_ERR_EXISTS:
-      result = CURLE_REMOTE_FILE_EXISTS;
+      code = CURLE_REMOTE_FILE_EXISTS;
       break;
     case TFTP_ERR_NOSUCHUSER:
-      result = CURLE_TFTP_NOSUCHUSER;
+      code = CURLE_TFTP_NOSUCHUSER;
       break;
     case TFTP_ERR_TIMEOUT:
-      result = CURLE_OPERATION_TIMEDOUT;
+      code = CURLE_OPERATION_TIMEDOUT;
       break;
     case TFTP_ERR_NORESPONSE:
-      result = CURLE_COULDNT_CONNECT;
+      code = CURLE_COULDNT_CONNECT;
       break;
     default:
-      result = CURLE_ABORTED_BY_CALLBACK;
+      code= CURLE_ABORTED_BY_CALLBACK;
       break;
     }
   }
-  else
-    result = CURLE_OK;
+  else {
+    code = CURLE_OK;
+  }
 
-  return result;
+  return(code);
 }
 
 /**********************************************************
@@ -885,21 +885,20 @@ static CURLcode tftp_translate_code(tftp_error_t error)
 static CURLcode tftp_state_machine(tftp_state_data_t *state,
                                    tftp_event_t event)
 {
-  CURLcode result = CURLE_OK;
+  CURLcode res = CURLE_OK;
   struct SessionHandle *data = state->conn->data;
-
   switch(state->state) {
   case TFTP_STATE_START:
     DEBUGF(infof(data, "TFTP_STATE_START\n"));
-    result = tftp_send_first(state, event);
+    res = tftp_send_first(state, event);
     break;
   case TFTP_STATE_RX:
     DEBUGF(infof(data, "TFTP_STATE_RX\n"));
-    result = tftp_rx(state, event);
+    res = tftp_rx(state, event);
     break;
   case TFTP_STATE_TX:
     DEBUGF(infof(data, "TFTP_STATE_TX\n"));
-    result = tftp_tx(state, event);
+    res = tftp_tx(state, event);
     break;
   case TFTP_STATE_FIN:
     infof(data, "%s\n", "TFTP finished");
@@ -907,11 +906,10 @@ static CURLcode tftp_state_machine(tftp_state_data_t *state,
   default:
     DEBUGF(infof(data, "STATE: %d\n", state->state));
     failf(data, "%s", "Internal state machine error");
-    result = CURLE_TFTP_ILLEGAL;
+    res = CURLE_TFTP_ILLEGAL;
     break;
   }
-
-  return result;
+  return res;
 }
 
 /**********************************************************
@@ -945,6 +943,7 @@ static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection)
  **********************************************************/
 static CURLcode tftp_connect(struct connectdata *conn, bool *done)
 {
+  CURLcode code;
   tftp_state_data_t *state;
   int blksize, rc;
 
@@ -1018,8 +1017,8 @@ static CURLcode tftp_connect(struct connectdata *conn, bool *done)
   Curl_pgrsStartNow(conn->data);
 
   *done = TRUE;
-
-  return CURLE_OK;
+  code = CURLE_OK;
+  return(code);
 }
 
 /**********************************************************
@@ -1032,7 +1031,7 @@ static CURLcode tftp_connect(struct connectdata *conn, bool *done)
 static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
                           bool premature)
 {
-  CURLcode result = CURLE_OK;
+  CURLcode code = CURLE_OK;
   tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
 
   (void)status; /* unused */
@@ -1043,9 +1042,9 @@ static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
 
   /* If we have encountered an error */
   if(state)
-    result = tftp_translate_code(state->error);
+    code = tftp_translate_code(state->error);
 
-  return result;
+  return code;
 }
 
 /**********************************************************
@@ -1210,7 +1209,7 @@ static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
   else if(event != TFTP_EVENT_NONE) {
     result = tftp_state_machine(state, event);
     if(result)
-      return result;
+      return(result);
     *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
     if(*done)
       /* Tell curl we're done */
@@ -1229,10 +1228,10 @@ static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
     else if(rc != 0) {
       result = tftp_receive_packet(conn);
       if(result)
-        return result;
+        return(result);
       result = tftp_state_machine(state, state->event);
       if(result)
-        return result;
+        return(result);
       *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
       if(*done)
         /* Tell curl we're done */