changeset 820:435b11d2d628

2006-10-1 Brian Masney <masneyb@gftp.org> * lib/ftpcommon.h lib/ftps.c lib/rfc959.c - automatically reconnect to the server if a timeout occurs. * src/uicommon/gftpui.c (_gftpui_common_thread_callback) - removed checks for the timeouts. This is now transparently handled in the FTP[S]* protocols. I need to check for timeouts in the other protocols.
author masneyb
date Sun, 01 Oct 2006 18:25:16 +0000
parents d6fdfcbdb056
children b282e346bd25
files ChangeLog lib/ftpcommon.h lib/ftps.c lib/rfc959.c src/uicommon/gftpui.c
diffstat 5 files changed, 83 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Oct 01 17:57:46 2006 +0000
+++ b/ChangeLog	Sun Oct 01 18:25:16 2006 +0000
@@ -1,4 +1,11 @@
 2006-10-1 Brian Masney <masneyb@gftp.org>
+	* lib/ftpcommon.h lib/ftps.c lib/rfc959.c - automatically reconnect to
+	the server if a timeout occurs.
+
+	* src/uicommon/gftpui.c (_gftpui_common_thread_callback) - removed
+	checks for the timeouts. This is now transparently handled in the
+	FTP[S]* protocols. I need to check for timeouts in the other protocols.
+
 	* src/gtk/transfer.c src/uicommon/gftpui.c src/uicommon/gftpui.h - added
 	new functions for canceling and skipping file transfers
 
@@ -3597,7 +3604,7 @@
 
 	* cvsclean - added this script
 
-	* *.[ch] - added $Id: ChangeLog,v 1.477 2006/10/01 17:57:45 masneyb Exp $ tags
+	* *.[ch] - added $Id: ChangeLog,v 1.478 2006/10/01 18:25:00 masneyb Exp $ tags
 
 	* debian/* - updated files from Debian maintainer
 
--- a/lib/ftpcommon.h	Sun Oct 01 17:57:46 2006 +0000
+++ b/lib/ftpcommon.h	Sun Oct 01 18:25:16 2006 +0000
@@ -39,5 +39,6 @@
 
 int rfc959_send_command 		( gftp_request * request,
 					  const char *command,
-					  int read_response );
+					  int read_response,
+					  int dont_try_to_reconnect );
 
--- a/lib/ftps.c	Sun Oct 01 17:57:46 2006 +0000
+++ b/lib/ftps.c	Sun Oct 01 18:25:16 2006 +0000
@@ -60,7 +60,7 @@
 
   params = request->protocol_data;
 
-  ret = rfc959_send_command (request, "AUTH TLS\r\n", 1);
+  ret = rfc959_send_command (request, "AUTH TLS\r\n", 1, 0);
   if (ret < 0)
     return (ret);
   else if (ret != '2')
@@ -72,12 +72,12 @@
   request->read_function = gftp_ssl_read;
   request->write_function = gftp_ssl_write;
 
-  ret = rfc959_send_command (request, "PBSZ 0\r\n", 1);
+  ret = rfc959_send_command (request, "PBSZ 0\r\n", 1, 0);
   if (ret < 0)
     return (ret);
 
   ret = '5'; /* FIXME */
-  /* ret = rfc959_send_command (request, "PROT P\r\n", 1); */
+  /* ret = rfc959_send_command (request, "PROT P\r\n", 1, 0); */
   if (ret < 0)
     return (ret);
   else if (ret == '2')
@@ -87,7 +87,7 @@
     }
   else
     {
-      ret = rfc959_send_command (request, "PROT C\r\n", 1);
+      ret = rfc959_send_command (request, "PROT C\r\n", 1, 0);
       if (ret < 0)
         return (ret);
       else if (ret != '2')
--- a/lib/rfc959.c	Sun Oct 01 17:57:46 2006 +0000
+++ b/lib/rfc959.c	Sun Oct 01 18:25:16 2006 +0000
@@ -146,7 +146,7 @@
 
 int
 rfc959_send_command (gftp_request * request, const char *command, 
-                     int read_response)
+                     int read_response, int dont_try_to_reconnect)
 {
   int ret;
 
@@ -175,7 +175,19 @@
     return (ret);
 
   if (read_response)
-    return (rfc959_read_response (request, 1));
+    {
+      ret = rfc959_read_response (request, 1);
+      if (ret == GFTP_ETIMEDOUT && !dont_try_to_reconnect)
+        {
+          ret = gftp_connect (request);
+          if (ret < 0)
+            return (ret);
+
+          return (rfc959_send_command (request, command, 1, 1));
+        }
+      else
+        return (ret);
+    }
   else
     return (0);
 }
@@ -313,7 +325,7 @@
   char *pos, *dir;
   int ret;
 
-  ret = rfc959_send_command (request, "PWD\r\n", 1);
+  ret = rfc959_send_command (request, "PWD\r\n", 1, 0);
   if (ret < 0)
     return (ret);
   else if (ret != '2')
@@ -365,11 +377,11 @@
   g_return_val_if_fail (directory != NULL, GFTP_EFATAL);
 
   if (strcmp (directory, "..") == 0)
-    ret = rfc959_send_command (request, "CDUP\r\n", 1);
+    ret = rfc959_send_command (request, "CDUP\r\n", 1, 0);
   else
     {
       tempstr = g_strconcat ("CWD ", directory, "\r\n", NULL);
-      ret = rfc959_send_command (request, tempstr, 1);
+      ret = rfc959_send_command (request, tempstr, 1, 0);
       g_free (tempstr);
     }
 
@@ -396,7 +408,7 @@
   g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
 
   parms = request->protocol_data;
-  ret = rfc959_send_command (request, "SYST\r\n", 1);
+  ret = rfc959_send_command (request, "SYST\r\n", 1, 0);
 
   if (ret < 0)
     return (ret);
@@ -509,7 +521,7 @@
 	      tempchar = *(endpos + 1);
 	      if (*endpos != '\0')
 		*(endpos + 1) = '\0';
-	      if ((resp = rfc959_send_command (request, startpos, 1)) < 0)
+	      if ((resp = rfc959_send_command (request, startpos, 1, 0)) < 0)
                 return (resp);
 	      if (*endpos != '\0')
 		*(endpos + 1) = tempchar;
@@ -524,7 +536,7 @@
   else
     {
       tempstr = g_strconcat ("USER ", request->username, "\r\n", NULL);
-      resp = rfc959_send_command (request, tempstr, 1);
+      resp = rfc959_send_command (request, tempstr, 1, 0);
       g_free (tempstr);
       if (resp < 0)
         return (resp);
@@ -540,7 +552,7 @@
           else
             tempstr = g_strconcat ("PASS ", request->password, "\r\n", NULL);
 
-	  resp = rfc959_send_command (request, tempstr, 1);
+	  resp = rfc959_send_command (request, tempstr, 1, 0);
 	  g_free (tempstr);
           if (resp < 0)
             return (resp);
@@ -549,7 +561,7 @@
       if (resp == '3' && request->account != NULL)
 	{
 	  tempstr = g_strconcat ("ACCT ", request->account, "\r\n", NULL);
-	  resp = rfc959_send_command (request, tempstr, 1);
+	  resp = rfc959_send_command (request, tempstr, 1, 0);
 	  g_free (tempstr);
           if (resp < 0)
             return (resp);
@@ -581,7 +593,7 @@
       parms->is_ascii_transfer = 0;
     }
 
-  if ((ret = rfc959_send_command (request, tempstr, 1)) < 0)
+  if ((ret = rfc959_send_command (request, tempstr, 1, 0)) < 0)
     return (ret);
 
   ret = -1;
@@ -653,7 +665,8 @@
 
   parms = request->protocol_data;
 
-  if ((parms->data_connection = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
+  parms->data_connection = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
+  if (parms->data_connection == -1)
     {
       request->logging_function (gftp_logging_error, request,
 				 _("Failed to create a IPv4 socket: %s\n"),
@@ -678,7 +691,7 @@
   gftp_lookup_request_option (request, "passive_transfer", &passive_transfer);
   if (passive_transfer)
     {
-      resp = rfc959_send_command (request, "PASV\r\n", 1);
+      resp = rfc959_send_command (request, "PASV\r\n", 1, 1);
       if (resp < 0)
         return (resp);
       else if (resp != '2')
@@ -797,7 +810,7 @@
 				 pos[0] & 0xff, pos[1] & 0xff, pos[2] & 0xff,
 				 pos[3] & 0xff, pos1[0] & 0xff,
 				 pos1[1] & 0xff);
-      resp = rfc959_send_command (request, command, 1);
+      resp = rfc959_send_command (request, command, 1, 1);
       g_free (command);
 
       if (resp < 0)
@@ -864,7 +877,7 @@
   gftp_lookup_request_option (request, "passive_transfer", &passive_transfer);
   if (passive_transfer)
     {
-      resp = rfc959_send_command (request, "EPSV\r\n", 1);
+      resp = rfc959_send_command (request, "EPSV\r\n", 1, 1);
       if (resp < 0)
         return (resp);
       else if (resp != '2')
@@ -956,7 +969,7 @@
       command = g_strdup_printf ("EPRT |2|%s|%d|\n", buf,
                                  ntohs (data_addr.sin6_port));
 
-      resp = rfc959_send_command (request, command, 1);
+      resp = rfc959_send_command (request, command, 1, 1);
       g_free (command);
 
       if (resp < 0)
@@ -975,19 +988,32 @@
 
 
 static int
-rfc959_data_connection_new (gftp_request * request)
+rfc959_data_connection_new (gftp_request * request, int dont_try_to_reconnect)
 {
   g_return_val_if_fail (request != NULL, GFTP_EFATAL);
   g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
 
+  int ret;
+
 #ifdef HAVE_IPV6
   if (request->ai_family == AF_INET6)
-    return (rfc959_ipv6_data_connection_new (request));
+    ret = rfc959_ipv6_data_connection_new (request);
   else
-    return (rfc959_ipv4_data_connection_new (request));
+    ret = rfc959_ipv4_data_connection_new (request);
 #else
-  return (rfc959_ipv4_data_connection_new (request));
+  ret = rfc959_ipv4_data_connection_new (request);
 #endif
+
+  if (ret == GFTP_ETIMEDOUT && !dont_try_to_reconnect)
+    {
+      ret = gftp_connect (request);
+      if (ret < 0)
+        return (ret);
+
+      return (rfc959_data_connection_new (request, 1));
+    }
+  else
+    return (ret);
 }
 
 
@@ -1095,7 +1121,7 @@
           parms->is_ascii_transfer = 0;
         }
 
-      if ((ret = rfc959_send_command (request, tempstr, 1)) < 0)
+      if ((ret = rfc959_send_command (request, tempstr, 1, 0)) < 0)
         return (ret);
     }
 
@@ -1124,7 +1150,7 @@
     return (ret);
 
   if (parms->data_connection < 0 && 
-      (ret = rfc959_data_connection_new (request)) < 0)
+      (ret = rfc959_data_connection_new (request, 0)) < 0)
     return (ret);
 
   if ((ret = gftp_fd_set_sockblocking (request, parms->data_connection, 1)) < 0)
@@ -1134,7 +1160,7 @@
     {
       command = g_strdup_printf ("REST " GFTP_OFF_T_PRINTF_MOD "\r\n",
                                  startsize);
-      ret = rfc959_send_command (request, command, 1);
+      ret = rfc959_send_command (request, command, 1, 0);
       g_free (command);
 
       if (ret < 0)
@@ -1147,7 +1173,7 @@
     }
 
   tempstr = g_strconcat ("RETR ", filename, "\r\n", NULL);
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1197,7 +1223,7 @@
     return (ret);
 
   if (parms->data_connection < 0 && 
-      (ret = rfc959_data_connection_new (request)) < 0)
+      (ret = rfc959_data_connection_new (request, 0)) < 0)
     return (ret);
 
   if ((ret = gftp_fd_set_sockblocking (request, parms->data_connection, 1)) < 0)
@@ -1207,7 +1233,7 @@
     {
       command = g_strdup_printf ("REST " GFTP_OFF_T_PRINTF_MOD "\r\n",
                                  startsize);
-      ret = rfc959_send_command (request, command, 1);
+      ret = rfc959_send_command (request, command, 1, 0);
       g_free (command);
       if (ret < 0)
         return (ret);
@@ -1219,7 +1245,7 @@
     }
 
   tempstr = g_strconcat ("STOR ", filename, "\r\n", NULL);
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
   if (ret < 0)
     return (ret);
@@ -1254,7 +1280,7 @@
   g_return_val_if_fail (fromreq->datafd > 0, GFTP_EFATAL);
   g_return_val_if_fail (toreq->datafd > 0, GFTP_EFATAL);
 
-  if ((ret = rfc959_send_command (fromreq, "PASV\r\n", 1)) < 0)
+  if ((ret = rfc959_send_command (fromreq, "PASV\r\n", 1, 0)) < 0)
     return (ret);
   else if (ret != '2')
     return (GFTP_ERETRYABLE);
@@ -1272,7 +1298,7 @@
     *endpos = '\0';
 
   tempstr = g_strconcat ("PORT ", pos, "\r\n", NULL);
-  ret = rfc959_send_command (toreq, tempstr, 1);
+  ret = rfc959_send_command (toreq, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1281,14 +1307,14 @@
     return (GFTP_ERETRYABLE);
 
   tempstr = g_strconcat ("RETR ", fromfile, "\r\n", NULL);
-  ret = rfc959_send_command (fromreq, tempstr, 0);
+  ret = rfc959_send_command (fromreq, tempstr, 0, 0);
   g_free (tempstr);
 
   if (ret < 0)
     return (ret);
 
   tempstr = g_strconcat ("STOR ", tofile, "\r\n", NULL);
-  ret = rfc959_send_command (toreq, tempstr, 0);
+  ret = rfc959_send_command (toreq, tempstr, 0, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1343,7 +1369,7 @@
   g_return_val_if_fail (request != NULL, GFTP_EFATAL);
   g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
 
-  if ((ret = rfc959_send_command (request, "ABOR\r\n", 0)) < 0)
+  if ((ret = rfc959_send_command (request, "ABOR\r\n", 0, 0)) < 0)
     return (ret);
 
   rfc959_close_data_connection (request);
@@ -1368,7 +1394,7 @@
   g_return_val_if_fail (request != NULL, GFTP_EFATAL);
   g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
 
-  if ((ret = rfc959_data_connection_new (request)) < 0)
+  if ((ret = rfc959_data_connection_new (request, 0)) < 0)
     return (ret);
 
   gftp_lookup_request_option (request, "show_hidden_files", &show_hidden_files);
@@ -1381,7 +1407,7 @@
   tempstr = g_strconcat ("LIST", *parms != '\0' ? " -" : "", parms, "\r\n", 
                          NULL); 
 
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1592,7 +1618,7 @@
   g_return_val_if_fail (request->datafd > 0, 0);
 
   tempstr = g_strconcat ("SIZE ", filename, "\r\n", NULL);
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
   if (ret < 0)
     return (ret);
@@ -1614,7 +1640,7 @@
   g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
 
   tempstr = g_strconcat ("RMD ", directory, "\r\n", NULL);
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1637,7 +1663,7 @@
   g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
 
   tempstr = g_strconcat ("DELE ", file, "\r\n", NULL);
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1660,7 +1686,7 @@
   g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
 
   tempstr = g_strconcat ("MKD ", directory, "\r\n", NULL);
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1685,7 +1711,7 @@
   g_return_val_if_fail (request->datafd > 0, GFTP_EFATAL);
 
   tempstr = g_strconcat ("RNFR ", oldname, "\r\n", NULL);
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1694,7 +1720,7 @@
     return (GFTP_ERETRYABLE);
 
   tempstr = g_strconcat ("RNTO ", newname, "\r\n", NULL);
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1720,7 +1746,7 @@
   parms = request->protocol_data;
   tempstr = g_strdup_printf ("SITE CHMOD %o %s\r\n", mode, file);
 
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
@@ -1747,7 +1773,7 @@
   else
     tempstr = g_strconcat (command, "\r\n", NULL);
 
-  ret = rfc959_send_command (request, tempstr, 1);
+  ret = rfc959_send_command (request, tempstr, 1, 0);
   g_free (tempstr);
 
   if (ret < 0)
--- a/src/uicommon/gftpui.c	Sun Oct 01 17:57:46 2006 +0000
+++ b/src/uicommon/gftpui.c	Sun Oct 01 18:25:16 2006 +0000
@@ -53,8 +53,8 @@
 { 
   intptr_t network_timeout, sleep_time;
   gftpui_callback_data * cdata;
-  int success, sj, num_timeouts;
   struct timespec ts;
+  int success, sj;
 
   cdata = data;
   gftp_lookup_request_option (cdata->request, "network_timeout",
@@ -65,7 +65,6 @@
   sj = sigsetjmp (gftpui_common_jmp_environment, 1);
   gftpui_common_use_jmp_environment = 1;
 
-  num_timeouts = 0;
   success = GFTP_ERETRYABLE;
   if (sj != 1)
     {
@@ -76,14 +75,6 @@
           success = cdata->run_function (cdata);
           alarm (0);
 
-          if (success == GFTP_ETIMEDOUT && num_timeouts == 0)
-            {
-              _gftpui_cb_disconnect (cdata);
-              num_timeouts++;
-              if (_gftpui_cb_connect (cdata) == 0)
-                continue;
-            }
-
           if (success == GFTP_EFATAL || success == 0 || cdata->retries == 0)
             break;