18360efbdSAlfred Perlstein /* $NetBSD: pmap_getport.c,v 1.16 2000/07/06 03:10:34 christos Exp $ */
28360efbdSAlfred Perlstein
32e322d37SHiroki Sato /*-
4*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni *
62e322d37SHiroki Sato * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato * All rights reserved.
899064799SGarrett Wollman *
92e322d37SHiroki Sato * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato * this list of conditions and the following disclaimer.
132e322d37SHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato * this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato * and/or other materials provided with the distribution.
162e322d37SHiroki Sato * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato * contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato * from this software without specific prior written permission.
1999064799SGarrett Wollman *
202e322d37SHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato * POSSIBILITY OF SUCH DAMAGE.
3199064799SGarrett Wollman */
3299064799SGarrett Wollman
3399064799SGarrett Wollman /*
3499064799SGarrett Wollman * pmap_getport.c
3599064799SGarrett Wollman * Client interface to pmap rpc service.
3699064799SGarrett Wollman *
3799064799SGarrett Wollman * Copyright (C) 1984, Sun Microsystems, Inc.
3899064799SGarrett Wollman */
3999064799SGarrett Wollman
40d201fe46SDaniel Eischen #include "namespace.h"
418360efbdSAlfred Perlstein #include <sys/types.h>
428360efbdSAlfred Perlstein #include <sys/socket.h>
438360efbdSAlfred Perlstein
44fd8e4ebcSMike Barcroft #include <arpa/inet.h>
458360efbdSAlfred Perlstein #include <net/if.h>
468360efbdSAlfred Perlstein
478360efbdSAlfred Perlstein #include <assert.h>
488360efbdSAlfred Perlstein #include <unistd.h>
498360efbdSAlfred Perlstein
5099064799SGarrett Wollman #include <rpc/rpc.h>
5199064799SGarrett Wollman #include <rpc/pmap_prot.h>
5299064799SGarrett Wollman #include <rpc/pmap_clnt.h>
53d201fe46SDaniel Eischen #include "un-namespace.h"
5499064799SGarrett Wollman
558360efbdSAlfred Perlstein static const struct timeval timeout = { 5, 0 };
568360efbdSAlfred Perlstein static const struct timeval tottimeout = { 60, 0 };
5799064799SGarrett Wollman
5899064799SGarrett Wollman /*
5999064799SGarrett Wollman * Find the mapped port for program,version.
6099064799SGarrett Wollman * Calls the pmap service remotely to do the lookup.
6199064799SGarrett Wollman * Returns 0 if no map exists.
6299064799SGarrett Wollman */
6399064799SGarrett Wollman u_short
pmap_getport(struct sockaddr_in * address,u_long program,u_long version,u_int protocol)64587cf682SCraig Rodrigues pmap_getport(struct sockaddr_in *address, u_long program, u_long version,
65587cf682SCraig Rodrigues u_int protocol)
6699064799SGarrett Wollman {
6799064799SGarrett Wollman u_short port = 0;
688360efbdSAlfred Perlstein int sock = -1;
698360efbdSAlfred Perlstein CLIENT *client;
7099064799SGarrett Wollman struct pmap parms;
7199064799SGarrett Wollman
728360efbdSAlfred Perlstein assert(address != NULL);
738360efbdSAlfred Perlstein
7499064799SGarrett Wollman address->sin_port = htons(PMAPPORT);
7599064799SGarrett Wollman client = clntudp_bufcreate(address, PMAPPROG,
768360efbdSAlfred Perlstein PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
778360efbdSAlfred Perlstein if (client != NULL) {
7899064799SGarrett Wollman parms.pm_prog = program;
7999064799SGarrett Wollman parms.pm_vers = version;
8099064799SGarrett Wollman parms.pm_prot = protocol;
8199064799SGarrett Wollman parms.pm_port = 0; /* not needed or used */
828360efbdSAlfred Perlstein if (CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
838360efbdSAlfred Perlstein (xdrproc_t)xdr_pmap,
848360efbdSAlfred Perlstein &parms, (xdrproc_t)xdr_u_short, &port, tottimeout) !=
858360efbdSAlfred Perlstein RPC_SUCCESS){
8699064799SGarrett Wollman rpc_createerr.cf_stat = RPC_PMAPFAILURE;
8799064799SGarrett Wollman clnt_geterr(client, &rpc_createerr.cf_error);
8899064799SGarrett Wollman } else if (port == 0) {
8999064799SGarrett Wollman rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
9099064799SGarrett Wollman }
9199064799SGarrett Wollman CLNT_DESTROY(client);
9299064799SGarrett Wollman }
9399064799SGarrett Wollman address->sin_port = 0;
9499064799SGarrett Wollman return (port);
9599064799SGarrett Wollman }
96