a9992a38ea17ffe9ee31cd0af25baf1e5c898bfb
[platform/upstream/glibc.git] / sunrpc / pm_getport.c
1 /*
2  * pmap_getport.c
3  * Client interface to pmap rpc service.
4  *
5  * Copyright (c) 2010, Oracle America, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials
16  *       provided with the distribution.
17  *     * Neither the name of the "Oracle America, Inc." nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <stdbool.h>
36 #include <unistd.h>
37 #include <rpc/rpc.h>
38 #include <rpc/pmap_prot.h>
39 #include <rpc/pmap_clnt.h>
40 #include <sys/socket.h>
41 #include <shlib-compat.h>
42
43 /*
44  * Create a socket that is locally bound to a non-reserve port. For
45  * any failures, -1 is returned which will cause the RPC code to
46  * create the socket.
47  */
48 int
49 internal_function
50 __get_socket (struct sockaddr_in *saddr)
51 {
52   int so = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
53   if (so < 0)
54     return -1;
55
56   struct sockaddr_in laddr;
57   socklen_t namelen = sizeof (laddr);
58   laddr.sin_family = AF_INET;
59   laddr.sin_port = 0;
60   laddr.sin_addr.s_addr = htonl (INADDR_ANY);
61
62   int cc = __bind (so, (struct sockaddr *) &laddr, namelen);
63   if (__glibc_unlikely (cc < 0))
64     {
65     fail:
66       __close (so);
67       return -1;
68     }
69
70   cc = __connect (so, (struct sockaddr *) saddr, namelen);
71   if (__glibc_unlikely (cc < 0))
72     goto fail;
73
74   return so;
75 }
76
77
78 /*
79  * Find the mapped port for program,version.
80  * Internal version with additional parameters.
81  * Calls the pmap service remotely to do the lookup.
82  * Returns 0 if no map exists.
83  */
84 u_short
85 __libc_rpc_getport (struct sockaddr_in *address, u_long program,
86                     u_long version, u_int protocol, time_t timeout_sec,
87                     time_t tottimeout_sec)
88 {
89   const struct timeval timeout = {timeout_sec, 0};
90   const struct timeval tottimeout = {tottimeout_sec, 0};
91
92   u_short port = 0;
93   int socket = -1;
94   CLIENT *client;
95   struct pmap parms;
96   bool closeit = false;
97
98   address->sin_port = htons (PMAPPORT);
99   if (protocol == IPPROTO_TCP)
100     {
101       /* Don't need a reserved port to get ports from the portmapper.  */
102       socket = __get_socket(address);
103       if (socket != -1)
104         closeit = true;
105       client = clnttcp_create (address, PMAPPROG, PMAPVERS, &socket,
106                                RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
107     }
108   else
109     client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout,
110                                 &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
111   if (client != (CLIENT *) NULL)
112     {
113       struct rpc_createerr *ce = &get_rpc_createerr ();
114       parms.pm_prog = program;
115       parms.pm_vers = version;
116       parms.pm_prot = protocol;
117       parms.pm_port = 0;        /* not needed or used */
118       if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
119                      (caddr_t)&parms, (xdrproc_t)xdr_u_short,
120                      (caddr_t)&port, tottimeout) != RPC_SUCCESS)
121         {
122           ce->cf_stat = RPC_PMAPFAILURE;
123           clnt_geterr (client, &ce->cf_error);
124         }
125       else if (port == 0)
126         {
127           ce->cf_stat = RPC_PROGNOTREGISTERED;
128         }
129       CLNT_DESTROY (client);
130     }
131   /* We only need to close the socket here if we opened  it.  */
132   if (closeit)
133     (void) __close (socket);
134   address->sin_port = 0;
135   return port;
136 }
137 #ifdef EXPORT_RPC_SYMBOLS
138 libc_hidden_def (__libc_rpc_getport)
139 #else
140 libc_hidden_nolink_sunrpc (__libc_rpc_getport, GLIBC_PRIVATE)
141 #endif
142
143
144 /*
145  * Find the mapped port for program,version.
146  * Calls the pmap service remotely to do the lookup.
147  * Returns 0 if no map exists.
148  */
149 u_short
150 pmap_getport (struct sockaddr_in *address, u_long program, u_long version,
151               u_int protocol)
152 {
153   return __libc_rpc_getport (address, program, version, protocol, 5, 60);
154 }
155 libc_hidden_nolink_sunrpc (pmap_getport, GLIBC_2_0)