1/*	$OpenBSD: pmap_clnt.c,v 1.19 2015/09/13 15:36:56 guenther 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
45static struct timeval timeout = { 5, 0 };
46static 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 */
52bool_t
53pmap_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	int save_errno;
61	u_short port = iport;
62
63	if (get_myaddress(&myaddress) != 0)
64		return (FALSE);
65	myaddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
66	client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
67	    timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
68	if (client == NULL)
69		return (FALSE);
70	parms.pm_prog = program;
71	parms.pm_vers = version;
72	parms.pm_prot = protocol;
73	parms.pm_port = port;
74	if (CLNT_CALL(client, PMAPPROC_SET, xdr_pmap, &parms, xdr_bool, &rslt,
75	    tottimeout) != RPC_SUCCESS) {
76		save_errno = errno;
77		rslt = FALSE;
78	} else
79		save_errno = errno;
80	CLNT_DESTROY(client);
81	errno = save_errno;
82	return (rslt);
83}
84DEF_WEAK(pmap_set);
85
86/*
87 * Remove the mapping between program,version and port.
88 * Calls the pmap service remotely to do the un-mapping.
89 */
90bool_t
91pmap_unset(u_long program, u_long version)
92{
93	struct sockaddr_in myaddress;
94	int sock = -1;
95	CLIENT *client;
96	struct pmap parms;
97	bool_t rslt;
98
99	if (get_myaddress(&myaddress) != 0)
100		return (FALSE);
101	myaddress.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
102	client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS,
103	    timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
104	if (client == NULL)
105		return (FALSE);
106	parms.pm_prog = program;
107	parms.pm_vers = version;
108	parms.pm_port = parms.pm_prot = 0;
109	CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt,
110	    tottimeout);
111	CLNT_DESTROY(client);
112	return (rslt);
113}
114DEF_WEAK(pmap_unset);
115