Blame src/pmap_getport.c

Packit Service 4f68e0
/*
Packit Service 4f68e0
 * Copyright (c) 2009, Sun Microsystems, Inc.
Packit Service 4f68e0
 * All rights reserved.
Packit Service 4f68e0
 *
Packit Service 4f68e0
 * Redistribution and use in source and binary forms, with or without
Packit Service 4f68e0
 * modification, are permitted provided that the following conditions are met:
Packit Service 4f68e0
 * - Redistributions of source code must retain the above copyright notice,
Packit Service 4f68e0
 *   this list of conditions and the following disclaimer.
Packit Service 4f68e0
 * - Redistributions in binary form must reproduce the above copyright notice,
Packit Service 4f68e0
 *   this list of conditions and the following disclaimer in the documentation
Packit Service 4f68e0
 *   and/or other materials provided with the distribution.
Packit Service 4f68e0
 * - Neither the name of Sun Microsystems, Inc. nor the names of its
Packit Service 4f68e0
 *   contributors may be used to endorse or promote products derived
Packit Service 4f68e0
 *   from this software without specific prior written permission.
Packit Service 4f68e0
 *
Packit Service 4f68e0
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit Service 4f68e0
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit Service 4f68e0
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit Service 4f68e0
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
Packit Service 4f68e0
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit Service 4f68e0
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit Service 4f68e0
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit Service 4f68e0
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit Service 4f68e0
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit Service 4f68e0
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit Service 4f68e0
 * POSSIBILITY OF SUCH DAMAGE.
Packit Service 4f68e0
 */
Packit Service 4f68e0
/*
Packit Service 4f68e0
 * pmap_getport.c
Packit Service 4f68e0
 * Client interface to pmap rpc service.
Packit Service 4f68e0
 *
Packit Service 4f68e0
 * Copyright (C) 1984, Sun Microsystems, Inc.
Packit Service 4f68e0
 */
Packit Service 4f68e0
Packit Service 4f68e0
#include <sys/types.h>
Packit Service 4f68e0
#include <sys/socket.h>
Packit Service 4f68e0
Packit Service 4f68e0
#include <arpa/inet.h>
Packit Service 4f68e0
#include <net/if.h>
Packit Service 4f68e0
Packit Service 4f68e0
#include <assert.h>
Packit Service 4f68e0
#include <unistd.h>
Packit Service 4f68e0
Packit Service 4f68e0
#include <rpc/rpc.h>
Packit Service 4f68e0
#include <rpc/pmap_prot.h>
Packit Service 4f68e0
#include <rpc/pmap_clnt.h>
Packit Service 4f68e0
Packit Service 4f68e0
static const struct timeval timeout = { 5, 0 };
Packit Service 4f68e0
static const struct timeval tottimeout = { 60, 0 };
Packit Service 4f68e0
Packit Service 4f68e0
/*
Packit Service 4f68e0
 * Find the mapped port for program,version.
Packit Service 4f68e0
 * Calls the pmap service remotely to do the lookup.
Packit Service 4f68e0
 * Returns 0 if no map exists.
Packit Service 4f68e0
 */
Packit Service 4f68e0
u_short
Packit Service 4f68e0
pmap_getport(address, program, version, protocol)
Packit Service 4f68e0
	struct sockaddr_in *address;
Packit Service 4f68e0
	u_long program;
Packit Service 4f68e0
	u_long version;
Packit Service 4f68e0
	u_int protocol;
Packit Service 4f68e0
{
Packit Service 4f68e0
	u_short port = 0;
Packit Service 4f68e0
	int sock = -1;
Packit Service 4f68e0
	CLIENT *client;
Packit Service 4f68e0
	struct pmap parms;
Packit Service 4f68e0
Packit Service 4f68e0
	assert(address != NULL);
Packit Service 4f68e0
Packit Service 4f68e0
	address->sin_port = htons(PMAPPORT);
Packit Service 4f68e0
	client = clntudp_bufcreate(address, PMAPPROG,
Packit Service 4f68e0
	    PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
Packit Service 4f68e0
	if (client != NULL) {
Packit Service 4f68e0
		parms.pm_prog = program;
Packit Service 4f68e0
		parms.pm_vers = version;
Packit Service 4f68e0
		parms.pm_prot = protocol;
Packit Service 4f68e0
		parms.pm_port = 0;  /* not needed or used */
Packit Service 4f68e0
		if (CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
Packit Service 4f68e0
		    (xdrproc_t)xdr_pmap,
Packit Service 4f68e0
		    &parms, (xdrproc_t)xdr_u_short, &port, tottimeout) !=
Packit Service 4f68e0
		    RPC_SUCCESS){
Packit Service 4f68e0
			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
Packit Service 4f68e0
			clnt_geterr(client, &rpc_createerr.cf_error);
Packit Service 4f68e0
		} else if (port == 0) {
Packit Service 4f68e0
			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
Packit Service 4f68e0
		}
Packit Service 4f68e0
		CLNT_DESTROY(client);
Packit Service 4f68e0
	}
Packit Service 4f68e0
	address->sin_port = 0;
Packit Service 4f68e0
	return (port);
Packit Service 4f68e0
}