xref: /openbsd-src/lib/libc/rpc/pmap_clnt.c (revision 85858ec2de0a5bfea0d5f098a39cf144ad74f89a)
1*85858ec2Sguenther /*	$OpenBSD: pmap_clnt.c,v 1.19 2015/09/13 15:36:56 guenther Exp $ */
2cb7760d1Smillert 
3df930be7Sderaadt /*
4cb7760d1Smillert  * Copyright (c) 2010, Oracle America, Inc.
5df930be7Sderaadt  *
6cb7760d1Smillert  * Redistribution and use in source and binary forms, with or without
7cb7760d1Smillert  * modification, are permitted provided that the following conditions are
8cb7760d1Smillert  * met:
9df930be7Sderaadt  *
10cb7760d1Smillert  *     * Redistributions of source code must retain the above copyright
11cb7760d1Smillert  *       notice, this list of conditions and the following disclaimer.
12cb7760d1Smillert  *     * Redistributions in binary form must reproduce the above
13cb7760d1Smillert  *       copyright notice, this list of conditions and the following
14cb7760d1Smillert  *       disclaimer in the documentation and/or other materials
15cb7760d1Smillert  *       provided with the distribution.
16cb7760d1Smillert  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17cb7760d1Smillert  *       contributors may be used to endorse or promote products derived
18cb7760d1Smillert  *       from this software without specific prior written permission.
19df930be7Sderaadt  *
20cb7760d1Smillert  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21cb7760d1Smillert  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22cb7760d1Smillert  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23cb7760d1Smillert  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24cb7760d1Smillert  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25cb7760d1Smillert  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26cb7760d1Smillert  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27cb7760d1Smillert  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28cb7760d1Smillert  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29cb7760d1Smillert  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30cb7760d1Smillert  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31cb7760d1Smillert  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32df930be7Sderaadt  */
33df930be7Sderaadt 
34df930be7Sderaadt /*
35df930be7Sderaadt  * pmap_clnt.c
36df930be7Sderaadt  * Client interface to pmap rpc service.
37df930be7Sderaadt  */
38df930be7Sderaadt 
398e26201bSmillert #include <unistd.h>
40530550bcSderaadt #include <errno.h>
41df930be7Sderaadt #include <rpc/rpc.h>
42df930be7Sderaadt #include <rpc/pmap_prot.h>
43df930be7Sderaadt #include <rpc/pmap_clnt.h>
44df930be7Sderaadt 
45df930be7Sderaadt static struct timeval timeout = { 5, 0 };
46df930be7Sderaadt static struct timeval tottimeout = { 60, 0 };
47df930be7Sderaadt 
48df930be7Sderaadt /*
49df930be7Sderaadt  * Set a mapping between program,version and port.
50df930be7Sderaadt  * Calls the pmap service remotely to do the mapping.
51df930be7Sderaadt  */
52df930be7Sderaadt bool_t
pmap_set(u_long program,u_long version,u_int protocol,int iport)53384ec30aSotto pmap_set(u_long program, u_long version, u_int protocol, int iport)
54df930be7Sderaadt {
55df930be7Sderaadt 	struct sockaddr_in myaddress;
5679a9d2e3Sderaadt 	int sock = -1;
5741aa8645Sderaadt 	CLIENT *client;
58df930be7Sderaadt 	struct pmap parms;
59df930be7Sderaadt 	bool_t rslt;
60cc062249Sguenther 	int save_errno;
61384ec30aSotto 	u_short port = iport;
62df930be7Sderaadt 
63794b87d7Sderaadt 	if (get_myaddress(&myaddress) != 0)
64794b87d7Sderaadt 		return (FALSE);
653003ca2cSderaadt 	myaddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
66df930be7Sderaadt 	client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
6779a9d2e3Sderaadt 	    timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
683f820ea1Skrw 	if (client == NULL)
69df930be7Sderaadt 		return (FALSE);
70df930be7Sderaadt 	parms.pm_prog = program;
71df930be7Sderaadt 	parms.pm_vers = version;
72df930be7Sderaadt 	parms.pm_prot = protocol;
73df930be7Sderaadt 	parms.pm_port = port;
74df930be7Sderaadt 	if (CLNT_CALL(client, PMAPPROC_SET, xdr_pmap, &parms, xdr_bool, &rslt,
75df930be7Sderaadt 	    tottimeout) != RPC_SUCCESS) {
76cc062249Sguenther 		save_errno = errno;
77cc062249Sguenther 		rslt = FALSE;
78cc062249Sguenther 	} else
79cc062249Sguenther 		save_errno = errno;
80df930be7Sderaadt 	CLNT_DESTROY(client);
81cc062249Sguenther 	errno = save_errno;
82df930be7Sderaadt 	return (rslt);
83df930be7Sderaadt }
84*85858ec2Sguenther DEF_WEAK(pmap_set);
85df930be7Sderaadt 
86df930be7Sderaadt /*
87df930be7Sderaadt  * Remove the mapping between program,version and port.
88df930be7Sderaadt  * Calls the pmap service remotely to do the un-mapping.
89df930be7Sderaadt  */
90df930be7Sderaadt bool_t
pmap_unset(u_long program,u_long version)91384ec30aSotto pmap_unset(u_long program, u_long version)
92df930be7Sderaadt {
93df930be7Sderaadt 	struct sockaddr_in myaddress;
9479a9d2e3Sderaadt 	int sock = -1;
9541aa8645Sderaadt 	CLIENT *client;
96df930be7Sderaadt 	struct pmap parms;
97df930be7Sderaadt 	bool_t rslt;
98df930be7Sderaadt 
99794b87d7Sderaadt 	if (get_myaddress(&myaddress) != 0)
100794b87d7Sderaadt 		return (FALSE);
1013003ca2cSderaadt 	myaddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
102df930be7Sderaadt 	client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
10379a9d2e3Sderaadt 	    timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1043f820ea1Skrw 	if (client == NULL)
105df930be7Sderaadt 		return (FALSE);
106df930be7Sderaadt 	parms.pm_prog = program;
107df930be7Sderaadt 	parms.pm_vers = version;
108df930be7Sderaadt 	parms.pm_port = parms.pm_prot = 0;
109df930be7Sderaadt 	CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt,
110df930be7Sderaadt 	    tottimeout);
111df930be7Sderaadt 	CLNT_DESTROY(client);
112df930be7Sderaadt 	return (rslt);
113df930be7Sderaadt }
114*85858ec2Sguenther DEF_WEAK(pmap_unset);
115