xref: /netbsd-src/lib/libc/rpc/pmap_getport.c (revision 47c0e0c312cd964546204bd0febab9a56fa11df9)
1*47c0e0c3Stron /*	$NetBSD: pmap_getport.c,v 1.19 2013/03/11 20:19:29 tron Exp $	*/
29e15c989Scgd 
363d7b677Scgd /*
4*47c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd  *
6*47c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
7*47c0e0c3Stron  * modification, are permitted provided that the following conditions are
8*47c0e0c3Stron  * met:
963d7b677Scgd  *
10*47c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
11*47c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
12*47c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
13*47c0e0c3Stron  *       copyright notice, this list of conditions and the following
14*47c0e0c3Stron  *       disclaimer in the documentation and/or other materials
15*47c0e0c3Stron  *       provided with the distribution.
16*47c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*47c0e0c3Stron  *       contributors may be used to endorse or promote products derived
18*47c0e0c3Stron  *       from this software without specific prior written permission.
1963d7b677Scgd  *
20*47c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*47c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*47c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*47c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*47c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*47c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*47c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*47c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*47c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*47c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*47c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*47c0e0c3Stron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd  */
3363d7b677Scgd 
34c63c52b2Schristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36c63c52b2Schristos #if 0
37c63c52b2Schristos static char *sccsid = "@(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro";
38c63c52b2Schristos static char *sccsid = "@(#)pmap_getport.c	2.2 88/08/01 4.0 RPCSRC";
39c63c52b2Schristos #else
40*47c0e0c3Stron __RCSID("$NetBSD: pmap_getport.c,v 1.19 2013/03/11 20:19:29 tron Exp $");
41c63c52b2Schristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd 
4463d7b677Scgd /*
4563d7b677Scgd  * pmap_getport.c
4663d7b677Scgd  * Client interface to pmap rpc service.
4763d7b677Scgd  *
4863d7b677Scgd  * Copyright (C) 1984, Sun Microsystems, Inc.
4963d7b677Scgd  */
5063d7b677Scgd 
5143fa6fe3Sjtc #include "namespace.h"
5246e6c5e8Slukem 
5346e6c5e8Slukem #include <sys/types.h>
5446e6c5e8Slukem #include <sys/socket.h>
5546e6c5e8Slukem 
5646e6c5e8Slukem #include <net/if.h>
5746e6c5e8Slukem 
58b48252f3Slukem #include <assert.h>
5946e6c5e8Slukem #include <unistd.h>
6046e6c5e8Slukem 
6163d7b677Scgd #include <rpc/rpc.h>
6263d7b677Scgd #include <rpc/pmap_prot.h>
6363d7b677Scgd #include <rpc/pmap_clnt.h>
6463d7b677Scgd 
6543fa6fe3Sjtc #ifdef __weak_alias
6660549036Smycroft __weak_alias(pmap_getport,_pmap_getport)
6743fa6fe3Sjtc #endif
6843fa6fe3Sjtc 
6900eb97ddSmycroft static const struct timeval timeout = { 5, 0 };
7000eb97ddSmycroft static const struct timeval tottimeout = { 60, 0 };
7163d7b677Scgd 
7263d7b677Scgd /*
7363d7b677Scgd  * Find the mapped port for program,version.
7463d7b677Scgd  * Calls the pmap service remotely to do the lookup.
7563d7b677Scgd  * Returns 0 if no map exists.
7663d7b677Scgd  */
77a1e4f75aSchristos 
78a1e4f75aSchristos static void
remote_pmap_getport(CLIENT * client,struct pmap * parms,u_short * port)79a1e4f75aSchristos remote_pmap_getport(CLIENT *client, struct pmap *parms, u_short *port)
80a1e4f75aSchristos {
81a1e4f75aSchristos 	if (CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
82a1e4f75aSchristos 	    parms, (xdrproc_t)xdr_u_short, port, tottimeout) != RPC_SUCCESS) {
83a1e4f75aSchristos 		rpc_createerr.cf_stat = RPC_PMAPFAILURE;
84a1e4f75aSchristos 		clnt_geterr(client, &rpc_createerr.cf_error);
85a1e4f75aSchristos 	} else if (*port == 0) {
86a1e4f75aSchristos 		rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
87a1e4f75aSchristos 		clnt_geterr(client, &rpc_createerr.cf_error);
88a1e4f75aSchristos 	}
89a1e4f75aSchristos 	CLNT_DESTROY(client);
90a1e4f75aSchristos }
91a1e4f75aSchristos 
92ef1144e6Schristos static CLIENT *
get_client(struct sockaddr_in * address,int tcp)93ef1144e6Schristos get_client(struct sockaddr_in *address, int tcp)
94ef1144e6Schristos {
95ef1144e6Schristos 	int sock = -1;
96ef1144e6Schristos 	if (tcp)
97ef1144e6Schristos 		return clnttcp_create(address, PMAPPROG, PMAPVERS, &sock, 0, 0);
98ef1144e6Schristos 	else
99ef1144e6Schristos 		return clntudp_bufcreate(address, PMAPPROG, PMAPVERS, timeout,
100ef1144e6Schristos 		    &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
101ef1144e6Schristos }
102ef1144e6Schristos 
103ce147c1cSlukem u_short
pmap_getport(struct sockaddr_in * address,u_long program,u_long version,u_int protocol)104ef1144e6Schristos pmap_getport(struct sockaddr_in *address, u_long program, u_long version,
105ef1144e6Schristos     u_int protocol)
10663d7b677Scgd {
107ce147c1cSlukem 	u_short port = 0;
10846e6c5e8Slukem 	CLIENT *client;
10963d7b677Scgd 	struct pmap parms;
11063d7b677Scgd 
111b48252f3Slukem 	_DIAGASSERT(address != NULL);
112b48252f3Slukem 
11363d7b677Scgd 	parms.pm_prog = program;
11463d7b677Scgd 	parms.pm_vers = version;
11563d7b677Scgd 	parms.pm_prot = protocol;
11663d7b677Scgd 	parms.pm_port = 0;  /* not needed or used */
117a1e4f75aSchristos 
118a1e4f75aSchristos 	address->sin_port = htons(PMAPPORT);
119ef1144e6Schristos 
120ef1144e6Schristos 	client = get_client(address, protocol == IPPROTO_TCP);
121a1e4f75aSchristos 	if (client != NULL)
122a1e4f75aSchristos 		remote_pmap_getport(client, &parms, &port);
123ef1144e6Schristos 
124a1e4f75aSchristos 	if (port == 0) {
125ef1144e6Schristos 		client = get_client(address, protocol != IPPROTO_TCP);
126a1e4f75aSchristos 		if (client != NULL)
127a1e4f75aSchristos 			remote_pmap_getport(client, &parms, &port);
12863d7b677Scgd 	}
129ef1144e6Schristos 
13063d7b677Scgd 	address->sin_port = 0;
131ef1144e6Schristos 	return port;
13263d7b677Scgd }
133