]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - support/nfs/getport.c
Now that only the Section names are case-insensitive
[nfs-utils.git] / support / nfs / getport.c
index aa9c154386d4cc14c004616e9ce64e679684f561..4bdf556e092fc5e3e33dea26b87bf1a0b3dab128 100644 (file)
@@ -65,6 +65,26 @@ static const rpcvers_t default_rpcb_version = RPCBVERS_4;
 static const rpcvers_t default_rpcb_version = PMAPVERS;
 #endif /* !HAVE_LIBTIRPC */
 
+/*
+ * Historical: Map TCP connect timeouts to timeout
+ * error code used by UDP.
+ */
+static void
+nfs_gp_map_tcp_errorcodes(const unsigned short protocol)
+{
+       if (protocol != IPPROTO_TCP)
+               return;
+
+       switch (rpc_createerr.cf_error.re_errno) {
+       case ETIMEDOUT:
+               rpc_createerr.cf_stat = RPC_TIMEDOUT;
+               break;
+       case ECONNREFUSED:
+               rpc_createerr.cf_stat = RPC_CANTRECV;
+               break;
+       }
+}
+
 /*
  * There's no easy way to tell how the local system's networking
  * and rpcbind is configured (ie. whether we want to use IPv6 or
@@ -92,24 +112,6 @@ static int nfs_gp_loopback_address(struct sockaddr *sap, socklen_t *salen)
        return ret;
 }
 
-/*
- * Plant port number in @sap.  @port is already in network byte order.
- */
-static void nfs_gp_set_port(struct sockaddr *sap, const in_port_t port)
-{
-       struct sockaddr_in *sin = (struct sockaddr_in *)sap;
-       struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
-
-       switch (sap->sa_family) {
-       case AF_INET:
-               sin->sin_port = port;
-               break;
-       case AF_INET6:
-               sin6->sin6_port = port;
-               break;
-       }
-}
-
 /*
  * Look up a network service in /etc/services and return the
  * network-order port number of that service.
@@ -188,10 +190,13 @@ static CLIENT *nfs_gp_get_rpcbclient(struct sockaddr *sap,
                NULL,
        };
        rpcprog_t rpcb_prog = nfs_getrpcbyname(RPCBPROG, rpcb_pgmtbl);
+       CLIENT *clnt;
 
-       nfs_gp_set_port(sap, nfs_gp_get_rpcb_port(transport));
-       return nfs_get_rpcclient(sap, salen, transport, rpcb_prog,
-                                       version, timeout);
+       nfs_set_port(sap, ntohs(nfs_gp_get_rpcb_port(transport)));
+       clnt = nfs_get_rpcclient(sap, salen, transport, rpcb_prog,
+                                                       version, timeout);
+       nfs_gp_map_tcp_errorcodes(transport);
+       return clnt;
 }
 
 /*
@@ -393,6 +398,10 @@ static int nfs_gp_ping(CLIENT *client, struct timeval timeout)
                           (xdrproc_t)xdr_void, NULL,
                           timeout);
 
+       if (status != RPC_SUCCESS) {
+               rpc_createerr.cf_stat = status;
+               CLNT_GETERR(client, &rpc_createerr.cf_error);
+       }
        return (int)(status == RPC_SUCCESS);
 }
 
@@ -525,7 +534,7 @@ static unsigned long nfs_gp_pmap_getport(CLIENT *client,
 
        if (status != RPC_SUCCESS) {
                rpc_createerr.cf_stat = status;
-               clnt_geterr(client, &rpc_createerr.cf_error);
+               CLNT_GETERR(client, &rpc_createerr.cf_error);
                port = 0;
        } else if (port == 0)
                rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
@@ -634,6 +643,7 @@ int nfs_rpc_ping(const struct sockaddr *sap, const socklen_t salen,
                                                program, version, &tout);
        if (client != NULL) {
                result = nfs_gp_ping(client, tout);
+               nfs_gp_map_tcp_errorcodes(protocol);
                CLNT_DESTROY(client);
        }
 
@@ -749,7 +759,7 @@ int nfs_getport_ping(struct sockaddr *sap, const socklen_t salen,
                struct sockaddr *saddr = (struct sockaddr *)&address;
 
                memcpy(saddr, sap, (size_t)salen);
-               nfs_gp_set_port(saddr, htons(port));
+               nfs_set_port(saddr, port);
 
                nfs_clear_rpc_createerr();
 
@@ -757,12 +767,13 @@ int nfs_getport_ping(struct sockaddr *sap, const socklen_t salen,
                                                program, version, &timeout);
                if (client != NULL) {
                        result = nfs_gp_ping(client, timeout);
+                       nfs_gp_map_tcp_errorcodes(protocol);
                        CLNT_DESTROY(client);
                }
        }
 
        if (result)
-               nfs_gp_set_port(sap, htons(port));
+               nfs_set_port(sap, port);
 
        return result;
 }