Get rid of unused __swblk_t type.
[glibc.git] / sunrpc / pm_getport.c
blobf10f87250d2784d72e8964e2319912fa18366bea
1 /*
2 * pmap_getport.c
3 * Client interface to pmap rpc service.
5 * Copyright (c) 2010, Oracle America, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
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.
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.
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>
43 * Create a socket that is locally bound to a non-reserve port. For
44 * any failures, -1 is returned which will cause the RPC code to
45 * create the socket.
47 int
48 internal_function
49 __get_socket (struct sockaddr_in *saddr)
51 int so = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
52 if (so < 0)
53 return -1;
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);
61 int cc = __bind (so, (struct sockaddr *) &laddr, namelen);
62 if (__builtin_expect (cc < 0, 0))
64 fail:
65 __close (so);
66 return -1;
69 cc = __connect (so, (struct sockaddr *) saddr, namelen);
70 if (__builtin_expect (cc < 0, 0))
71 goto fail;
73 return so;
78 * Find the mapped port for program,version.
79 * Internal version with additional parameters.
80 * Calls the pmap service remotely to do the lookup.
81 * Returns 0 if no map exists.
83 u_short
84 internal_function
85 __libc_rpc_getport (address, program, version, protocol, timeout_sec,
86 tottimeout_sec)
87 struct sockaddr_in *address;
88 u_long program;
89 u_long version;
90 u_int protocol;
91 time_t timeout_sec;
92 time_t tottimeout_sec;
94 const struct timeval timeout = {timeout_sec, 0};
95 const struct timeval tottimeout = {tottimeout_sec, 0};
97 u_short port = 0;
98 int socket = -1;
99 CLIENT *client;
100 struct pmap parms;
101 bool closeit = false;
103 address->sin_port = htons (PMAPPORT);
104 if (protocol == IPPROTO_TCP)
106 /* Don't need a reserved port to get ports from the portmapper. */
107 socket = __get_socket(address);
108 if (socket != -1)
109 closeit = true;
110 client = clnttcp_create (address, PMAPPROG, PMAPVERS, &socket,
111 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
113 else
114 client = clntudp_bufcreate (address, PMAPPROG, PMAPVERS, timeout,
115 &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
116 if (client != (CLIENT *) NULL)
118 struct rpc_createerr *ce = &get_rpc_createerr ();
119 parms.pm_prog = program;
120 parms.pm_vers = version;
121 parms.pm_prot = protocol;
122 parms.pm_port = 0; /* not needed or used */
123 if (CLNT_CALL (client, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
124 (caddr_t)&parms, (xdrproc_t)xdr_u_short,
125 (caddr_t)&port, tottimeout) != RPC_SUCCESS)
127 ce->cf_stat = RPC_PMAPFAILURE;
128 clnt_geterr (client, &ce->cf_error);
130 else if (port == 0)
132 ce->cf_stat = RPC_PROGNOTREGISTERED;
134 CLNT_DESTROY (client);
136 /* We only need to close the socket here if we opened it. */
137 if (closeit)
138 (void) __close (socket);
139 address->sin_port = 0;
140 return port;
142 #ifdef EXPORT_RPC_SYMBOLS
143 libc_hidden_def (__libc_rpc_getport)
144 #else
145 libc_hidden_nolink_sunrpc (__libc_rpc_getport, GLIBC_PRIVATE)
146 #endif
150 * Find the mapped port for program,version.
151 * Calls the pmap service remotely to do the lookup.
152 * Returns 0 if no map exists.
154 u_short
155 pmap_getport (address, program, version, protocol)
156 struct sockaddr_in *address;
157 u_long program;
158 u_long version;
159 u_int protocol;
161 return __libc_rpc_getport (address, program, version, protocol, 5, 60);
163 libc_hidden_nolink_sunrpc (pmap_getport, GLIBC_2_0)