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