1/* $NetBSD: pmap_getport.c,v 1.18 2008/01/28 01:10:22 christos Exp $ */ 2 3/* 4 * Copyright (c) 2010, Oracle America, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * * Neither the name of the "Oracle America, Inc." nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> 35#if defined(LIBC_SCCS) && !defined(lint) 36#if 0 37static char *sccsid = "@(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro"; 38static char *sccsid = "@(#)pmap_getport.c 2.2 88/08/01 4.0 RPCSRC"; 39#else 40__RCSID("$NetBSD: pmap_getport.c,v 1.18 2008/01/28 01:10:22 christos Exp $"); 41#endif 42#endif 43 44/* 45 * pmap_getport.c 46 * Client interface to pmap rpc service. 47 * 48 * Copyright (C) 1984, Sun Microsystems, Inc. 49 */ 50 51#include "namespace.h" 52 53#include <sys/types.h> 54#include <sys/socket.h> 55 56#include <net/if.h> 57 58#include <assert.h> 59#include <unistd.h> 60 61#include <rpc/rpc.h> 62#include <rpc/pmap_prot.h> 63#include <rpc/pmap_clnt.h> 64 65#ifdef __weak_alias 66__weak_alias(pmap_getport,_pmap_getport) 67#endif 68 69static const struct timeval timeout = { 5, 0 }; 70static const struct timeval tottimeout = { 60, 0 }; 71 72/* 73 * Find the mapped port for program,version. 74 * Calls the pmap service remotely to do the lookup. 75 * Returns 0 if no map exists. 76 */ 77 78static void 79remote_pmap_getport(CLIENT *client, struct pmap *parms, u_short *port) 80{ 81 if (CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap, 82 parms, (xdrproc_t)xdr_u_short, port, tottimeout) != RPC_SUCCESS) { 83 rpc_createerr.cf_stat = RPC_PMAPFAILURE; 84 clnt_geterr(client, &rpc_createerr.cf_error); 85 } else if (*port == 0) { 86 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED; 87 clnt_geterr(client, &rpc_createerr.cf_error); 88 } 89 CLNT_DESTROY(client); 90} 91 92static CLIENT * 93get_client(struct sockaddr_in *address, int tcp) 94{ 95 int sock = -1; 96 if (tcp) 97 return clnttcp_create(address, PMAPPROG, PMAPVERS, &sock, 0, 0); 98 else 99 return clntudp_bufcreate(address, PMAPPROG, PMAPVERS, timeout, 100 &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE); 101} 102 103u_short 104pmap_getport(struct sockaddr_in *address, u_long program, u_long version, 105 u_int protocol) 106{ 107 u_short port = 0; 108 CLIENT *client; 109 struct pmap parms; 110 111 _DIAGASSERT(address != NULL); 112 113 parms.pm_prog = program; 114 parms.pm_vers = version; 115 parms.pm_prot = protocol; 116 parms.pm_port = 0; /* not needed or used */ 117 118 address->sin_port = htons(PMAPPORT); 119 120 client = get_client(address, protocol == IPPROTO_TCP); 121 if (client != NULL) 122 remote_pmap_getport(client, &parms, &port); 123 124 if (port == 0) { 125 client = get_client(address, protocol != IPPROTO_TCP); 126 if (client != NULL) 127 remote_pmap_getport(client, &parms, &port); 128 } 129 130 address->sin_port = 0; 131 return port; 132} 133