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