]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/pm_getport.c
Add glibc.malloc.mxfast tunable
[thirdparty/glibc.git] / sunrpc / pm_getport.c
CommitLineData
28f540f4 1/*
a7ab6ec8
UD
2 * pmap_getport.c
3 * Client interface to pmap rpc service.
ab09b221 4 *
a7ab6ec8 5 * Copyright (c) 2010, Oracle America, Inc.
ab09b221 6 *
a7ab6ec8
UD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
cb636bb2 10 *
a7ab6ec8
UD
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.
cb636bb2 20 *
a7ab6ec8
UD
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.
28f540f4
RM
33 */
34
faf10b95
UD
35#include <stdbool.h>
36#include <unistd.h>
28f540f4
RM
37#include <rpc/rpc.h>
38#include <rpc/pmap_prot.h>
39#include <rpc/pmap_clnt.h>
40#include <sys/socket.h>
82f43dd2 41#include <shlib-compat.h>
25f9784e 42
faf10b95
UD
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 */
48int
faf10b95
UD
49__get_socket (struct sockaddr_in *saddr)
50{
51 int so = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
52 if (so < 0)
53 return -1;
54
55 struct sockaddr_in laddr;
56 socklen_t namelen = sizeof (laddr);
57 laddr.sin_family = AF_INET;
58 laddr.sin_port = 0;
59 laddr.sin_addr.s_addr = htonl (INADDR_ANY);
60
61 int cc = __bind (so, (struct sockaddr *) &laddr, namelen);
a1ffb40e 62 if (__glibc_unlikely (cc < 0))
faf10b95
UD
63 {
64 fail:
65 __close (so);
66 return -1;
67 }
68
69 cc = __connect (so, (struct sockaddr *) saddr, namelen);
a1ffb40e 70 if (__glibc_unlikely (cc < 0))
faf10b95
UD
71 goto fail;
72
73 return so;
74}
75
76
28f540f4
RM
77/*
78 * Find the mapped port for program,version.
2f3e3dc7 79 * Internal version with additional parameters.
28f540f4
RM
80 * Calls the pmap service remotely to do the lookup.
81 * Returns 0 if no map exists.
82 */
83u_short
80d9be81
JM
84__libc_rpc_getport (struct sockaddr_in *address, u_long program,
85 u_long version, u_int protocol, time_t timeout_sec,
86 time_t tottimeout_sec)
28f540f4 87{
2f3e3dc7
UD
88 const struct timeval timeout = {timeout_sec, 0};
89 const struct timeval tottimeout = {tottimeout_sec, 0};
90
e7fd8a39
UD
91 u_short port = 0;
92 int socket = -1;
93 CLIENT *client;
94 struct pmap parms;
faf10b95 95 bool closeit = false;
28f540f4 96
e7fd8a39 97 address->sin_port = htons (PMAPPORT);
086311a9 98 if (protocol == IPPROTO_TCP)
faf10b95
UD
99 {
100 /* Don't need a reserved port to get ports from the portmapper. */
101 socket = __get_socket(address);
102 if (socket != -1)
103 closeit = true;
7b57bfe5
UD
104 client = clnttcp_create (address, PMAPPROG, PMAPVERS, &socket,
105 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
faf10b95 106 }
086311a9 107 else
7b57bfe5
UD
108 client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout,
109 &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
e7fd8a39
UD
110 if (client != (CLIENT *) NULL)
111 {
543cf8a9 112 struct rpc_createerr *ce = &get_rpc_createerr ();
e7fd8a39
UD
113 parms.pm_prog = program;
114 parms.pm_vers = version;
115 parms.pm_prot = protocol;
116 parms.pm_port = 0; /* not needed or used */
7b57bfe5
UD
117 if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
118 (caddr_t)&parms, (xdrproc_t)xdr_u_short,
e7fd8a39
UD
119 (caddr_t)&port, tottimeout) != RPC_SUCCESS)
120 {
543cf8a9
UD
121 ce->cf_stat = RPC_PMAPFAILURE;
122 clnt_geterr (client, &ce->cf_error);
e7fd8a39
UD
123 }
124 else if (port == 0)
125 {
543cf8a9 126 ce->cf_stat = RPC_PROGNOTREGISTERED;
28f540f4 127 }
e7fd8a39
UD
128 CLNT_DESTROY (client);
129 }
faf10b95
UD
130 /* We only need to close the socket here if we opened it. */
131 if (closeit)
132 (void) __close (socket);
e7fd8a39
UD
133 address->sin_port = 0;
134 return port;
28f540f4 135}
7b57bfe5 136#ifdef EXPORT_RPC_SYMBOLS
2f3e3dc7 137libc_hidden_def (__libc_rpc_getport)
7b57bfe5 138#else
021db4be 139libc_hidden_nolink_sunrpc (__libc_rpc_getport, GLIBC_PRIVATE)
7b57bfe5 140#endif
2f3e3dc7
UD
141
142
143/*
144 * Find the mapped port for program,version.
145 * Calls the pmap service remotely to do the lookup.
146 * Returns 0 if no map exists.
147 */
148u_short
f63f2bfd
JM
149pmap_getport (struct sockaddr_in *address, u_long program, u_long version,
150 u_int protocol)
2f3e3dc7
UD
151{
152 return __libc_rpc_getport (address, program, version, protocol, 5, 60);
153}
021db4be 154libc_hidden_nolink_sunrpc (pmap_getport, GLIBC_2_0)