/* $NetBSD: pmap_getport.c,v 1.16 2000/07/06 03:10:34 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 2009, Sun Microsystems, Inc.6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions are met:10* - Redistributions of source code must retain the above copyright notice,11* this list of conditions and the following disclaimer.12* - Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15* - Neither the name of Sun Microsystems, Inc. nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"20* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE23* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR24* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF25* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS26* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN27* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE29* POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* pmap_getport.c34* Client interface to pmap rpc service.35*36* Copyright (C) 1984, Sun Microsystems, Inc.37*/3839#include "namespace.h"40#include <sys/types.h>41#include <sys/socket.h>4243#include <arpa/inet.h>44#include <net/if.h>4546#include <assert.h>47#include <unistd.h>4849#include <rpc/rpc.h>50#include <rpc/pmap_prot.h>51#include <rpc/pmap_clnt.h>52#include "un-namespace.h"5354static const struct timeval timeout = { 5, 0 };55static const struct timeval tottimeout = { 60, 0 };5657/*58* Find the mapped port for program,version.59* Calls the pmap service remotely to do the lookup.60* Returns 0 if no map exists.61*/62u_short63pmap_getport(struct sockaddr_in *address, u_long program, u_long version,64u_int protocol)65{66u_short port = 0;67int sock = -1;68CLIENT *client;69struct pmap parms;7071assert(address != NULL);7273address->sin_port = htons(PMAPPORT);74client = clntudp_bufcreate(address, PMAPPROG,75PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);76if (client != NULL) {77parms.pm_prog = program;78parms.pm_vers = version;79parms.pm_prot = protocol;80parms.pm_port = 0; /* not needed or used */81if (CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,82(xdrproc_t)xdr_pmap,83&parms, (xdrproc_t)xdr_u_short, &port, tottimeout) !=84RPC_SUCCESS){85rpc_createerr.cf_stat = RPC_PMAPFAILURE;86clnt_geterr(client, &rpc_createerr.cf_error);87} else if (port == 0) {88rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;89}90CLNT_DESTROY(client);91}92address->sin_port = 0;93return (port);94}959697