Reset the receive buffer processing the received PDU.
[deb_libnfs.git] / lib / socket.c
index b14231e84129f2d67980a93177e4a415a2023b03..c5068bae24ea754e4bbbf787a2fa44ca17d721e6 100644 (file)
 
 #ifdef WIN32
 #include "win32_compat.h"
-#else
+#endif
+
+#ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#endif/*WIN32*/
+#endif
 
 #ifdef HAVE_POLL_H
 #include <poll.h>
 #include <sys/ioctl.h>
 #endif
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-#include <fcntl.h>
-#include <string.h>
-#include <errno.h>
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
+
 #ifdef HAVE_SYS_FILIO_H
 #include <sys/filio.h>
 #endif
+
 #ifdef HAVE_SYS_SOCKIO_H
 #include <sys/sockio.h>
 #endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
 #include <sys/types.h>
 #include "libnfs-zdr.h"
 #include "libnfs.h"
@@ -153,8 +163,6 @@ static int rpc_read_from_socket(struct rpc_context *rpc)
 
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
-       assert(rpc->magic == RPC_CONTEXT_MAGIC);
-
        if (ioctl(rpc->fd, FIONREAD, &available) != 0) {
                rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno);
                return -1;
@@ -248,14 +256,17 @@ static int rpc_read_from_socket(struct rpc_context *rpc)
        rpc->inpos += count;
 
        if (rpc->inpos == rpc->insize) {
-               if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) {
+               char *buf = rpc->inbuf;
+
+               rpc->inbuf  = NULL;
+               rpc->insize = 0;
+               rpc->inpos  = 0;
+
+               if (rpc_process_pdu(rpc, buf, pdu_size) != 0) {
                        rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
                        return -1;
                }
                free(rpc->inbuf);
-               rpc->inbuf  = NULL;
-               rpc->insize = 0;
-               rpc->inpos  = 0;
        }
 
        return 0;
@@ -401,8 +412,12 @@ static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_s
                static int portOfs = 0;
                const int firstPort = 512;      /* >= 512 according to Sun docs */
                const int portCount = IPPORT_RESERVED - firstPort;
-               int startOfs = portOfs, port, rc;
+               int startOfs, port, rc;
 
+               if (portOfs == 0) {
+                       portOfs = time(NULL) % 400;
+               }
+               startOfs = portOfs;
                do {
                        rc = -1;
                        port = htons(firstPort + portOfs);