174462Salfred/*	$NetBSD: pmap_getport.c,v 1.16 2000/07/06 03:10:34 christos Exp $	*/
274462Salfred
3261057Smav/*-
4261057Smav * Copyright (c) 2009, Sun Microsystems, Inc.
5261057Smav * All rights reserved.
68870Srgrimes *
7261057Smav * Redistribution and use in source and binary forms, with or without
8261057Smav * modification, are permitted provided that the following conditions are met:
9261057Smav * - Redistributions of source code must retain the above copyright notice,
10261057Smav *   this list of conditions and the following disclaimer.
11261057Smav * - Redistributions in binary form must reproduce the above copyright notice,
12261057Smav *   this list of conditions and the following disclaimer in the documentation
13261057Smav *   and/or other materials provided with the distribution.
14261057Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its
15261057Smav *   contributors may be used to endorse or promote products derived
16261057Smav *   from this software without specific prior written permission.
17261057Smav *
18261057Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19261057Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20261057Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21261057Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22261057Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23261057Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24261057Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25261057Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26261057Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27261057Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28261057Smav * POSSIBILITY OF SUCH DAMAGE.
291901Swollman */
301901Swollman
311901Swollman#if defined(LIBC_SCCS) && !defined(lint)
32136581Sobrienstatic char *sccsid2 = "from: @(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro";
3392990Sobrienstatic char *sccsid = "from: @(#)pmap_getport.c	2.2 88/08/01 4.0 RPCSRC";
341901Swollman#endif
3592990Sobrien#include <sys/cdefs.h>
3692990Sobrien__FBSDID("$FreeBSD$");
371901Swollman
381901Swollman/*
391901Swollman * pmap_getport.c
401901Swollman * Client interface to pmap rpc service.
411901Swollman *
421901Swollman * Copyright (C) 1984, Sun Microsystems, Inc.
431901Swollman */
441901Swollman
4571579Sdeischen#include "namespace.h"
4674462Salfred#include <sys/types.h>
4774462Salfred#include <sys/socket.h>
4874462Salfred
4990868Smike#include <arpa/inet.h>
5074462Salfred#include <net/if.h>
5174462Salfred
5274462Salfred#include <assert.h>
5374462Salfred#include <unistd.h>
5474462Salfred
551901Swollman#include <rpc/rpc.h>
561901Swollman#include <rpc/pmap_prot.h>
571901Swollman#include <rpc/pmap_clnt.h>
5871579Sdeischen#include "un-namespace.h"
591901Swollman
6074462Salfredstatic const struct timeval timeout = { 5, 0 };
6174462Salfredstatic const struct timeval tottimeout = { 60, 0 };
621901Swollman
631901Swollman/*
641901Swollman * Find the mapped port for program,version.
651901Swollman * Calls the pmap service remotely to do the lookup.
661901Swollman * Returns 0 if no map exists.
671901Swollman */
681901Swollmanu_short
691901Swollmanpmap_getport(address, program, version, protocol)
701901Swollman	struct sockaddr_in *address;
711901Swollman	u_long program;
721901Swollman	u_long version;
731901Swollman	u_int protocol;
741901Swollman{
751901Swollman	u_short port = 0;
7674462Salfred	int sock = -1;
7774462Salfred	CLIENT *client;
781901Swollman	struct pmap parms;
791901Swollman
8074462Salfred	assert(address != NULL);
8174462Salfred
821901Swollman	address->sin_port = htons(PMAPPORT);
831901Swollman	client = clntudp_bufcreate(address, PMAPPROG,
8474462Salfred	    PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
8574462Salfred	if (client != NULL) {
861901Swollman		parms.pm_prog = program;
871901Swollman		parms.pm_vers = version;
881901Swollman		parms.pm_prot = protocol;
891901Swollman		parms.pm_port = 0;  /* not needed or used */
9074462Salfred		if (CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
9174462Salfred		    (xdrproc_t)xdr_pmap,
9274462Salfred		    &parms, (xdrproc_t)xdr_u_short, &port, tottimeout) !=
9374462Salfred		    RPC_SUCCESS){
941901Swollman			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
951901Swollman			clnt_geterr(client, &rpc_createerr.cf_error);
961901Swollman		} else if (port == 0) {
971901Swollman			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
981901Swollman		}
991901Swollman		CLNT_DESTROY(client);
1001901Swollman	}
1011901Swollman	address->sin_port = 0;
1021901Swollman	return (port);
1031901Swollman}
104