Add automatic reconnect after TCP session failure for the sync interface.
[deb_libnfs.git] / lib / socket.c
index a26afe1e6b194e46bea861e1974705b9d56a5bd1..503935c0a059e4dfd2da33eb356b3628f484b4e5 100644 (file)
@@ -15,6 +15,9 @@
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -25,6 +28,9 @@
 #include <rpc/rpc.h>
 #include <rpc/xdr.h>
 #include <arpa/inet.h>
+#ifdef HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -34,6 +40,8 @@
 #include "libnfs-private.h"
 #include "slist.h"
 
+static int rpc_disconnect_requeue(struct rpc_context *rpc);
+
 static void set_nonblocking(int fd)
 {
        unsigned v;
@@ -261,16 +269,16 @@ int rpc_service(struct rpc_context *rpc, int revents)
                return 0;
        }
 
-       if (revents & POLLOUT && rpc->outqueue != NULL) {
-               if (rpc_write_to_socket(rpc) != 0) {
-                       rpc_set_error(rpc, "write to socket failed");
-                       return -1;
+       if (revents & POLLIN) {
+               if (rpc_read_from_socket(rpc) != 0) {
+                       rpc_disconnect_requeue(rpc);
+                       return 0;
                }
        }
 
-       if (revents & POLLIN) {
-               if (rpc_read_from_socket(rpc) != 0) {
-                       rpc_disconnect(rpc, rpc_get_error(rpc));
+       if (revents & POLLOUT && rpc->outqueue != NULL) {
+               if (rpc_write_to_socket(rpc) != 0) {
+                       rpc_set_error(rpc, "write to socket failed");
                        return -1;
                }
        }
@@ -305,7 +313,7 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc
        switch (s.ss_family) {
        case AF_INET:
                socksize = sizeof(struct sockaddr_in);
-#ifdef HAVE_SOCK_SIN_LEN
+#ifdef HAVE_SOCKADDR_LEN
                sin->sin_len = socksize;
 #endif
                rpc->fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -344,6 +352,29 @@ int rpc_disconnect(struct rpc_context *rpc, char *error)
        return 0;
 }
 
+/* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue */
+static int rpc_disconnect_requeue(struct rpc_context *rpc)
+{
+       struct rpc_pdu *pdu;
+
+       if (rpc->fd != -1) {
+               close(rpc->fd);
+       }
+       rpc->fd  = -1;
+
+       rpc->is_connected = 0;
+
+       /* socket is closed so we will not get any replies to any commands
+        * in flight. Move them all over from the waitpdu queue back to the out queue
+        */
+       for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
+               SLIST_REMOVE(&rpc->waitpdu, pdu);
+               SLIST_ADD(&rpc->outqueue, pdu);
+       }
+
+       return 0;
+}
+
 
 int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
 {
@@ -422,3 +453,8 @@ int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int i
 
        return 0;
 }
+
+struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc)
+{
+       return (struct sockaddr *)&rpc->udp_src;
+}