pmap_getport.c revision 1.12
1/*	$OpenBSD: pmap_getport.c,v 1.12 2014/11/11 04:51:49 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_getport.c
36 * Client interface to pmap rpc service.
37 */
38
39#include <unistd.h>
40#include <rpc/rpc.h>
41#include <rpc/pmap_prot.h>
42#include <rpc/pmap_clnt.h>
43#include <sys/socket.h>
44#include <net/if.h>
45
46static struct timeval timeout = { 5, 0 };
47static struct timeval tottimeout = { 60, 0 };
48
49/*
50 * Find the mapped port for program,version.
51 * Calls the pmap service remotely to do the lookup.
52 * Returns 0 if no map exists.
53 */
54u_short
55pmap_getport(struct sockaddr_in *address, u_long program, u_long version,
56    u_int protocol)
57{
58	u_short port = 0;
59	int sock = -1;
60	CLIENT *client;
61	struct pmap parms;
62
63	address->sin_port = htons(PMAPPORT);
64	client = clntudp_bufcreate(address, PMAPPROG,
65	    PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
66	if (client != NULL) {
67		parms.pm_prog = program;
68		parms.pm_vers = version;
69		parms.pm_prot = protocol;
70		parms.pm_port = 0;  /* not needed or used */
71		if (CLNT_CALL(client, PMAPPROC_GETPORT, xdr_pmap, &parms,
72		    xdr_u_short, &port, tottimeout) != RPC_SUCCESS){
73			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
74			clnt_geterr(client, &rpc_createerr.cf_error);
75		} else if (port == 0) {
76			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
77		}
78		CLNT_DESTROY(client);
79	}
80	address->sin_port = 0;
81	return (port);
82}
83