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