/* $NetBSD: pmap_rmt.c,v 1.29 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_rmt.c34* Client interface to pmap rpc service.35* remote call and broadcast service36*37* Copyright (C) 1984, Sun Microsystems, Inc.38*/3940#include "namespace.h"41#include <sys/types.h>42#include <sys/ioctl.h>43#include <sys/poll.h>44#include <sys/socket.h>4546#include <net/if.h>47#include <netinet/in.h>48#include <arpa/inet.h>4950#include <assert.h>51#include <err.h>52#include <errno.h>53#include <stdio.h>54#include <string.h>55#include <unistd.h>5657#include <rpc/rpc.h>58#include <rpc/pmap_prot.h>59#include <rpc/pmap_clnt.h>60#include <rpc/pmap_rmt.h>61#include "un-namespace.h"6263static const struct timeval timeout = { 3, 0 };6465/*66* pmapper remote-call-service interface.67* This routine is used to call the pmapper remote call service68* which will look up a service program in the port maps, and then69* remotely call that routine with the given parameters. This allows70* programs to do a lookup and call in one step.71*/72enum clnt_stat73pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,74xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,75struct timeval tout, u_long *port_ptr)76{77int sock = -1;78CLIENT *client;79struct rmtcallargs a;80struct rmtcallres r;81enum clnt_stat stat;8283assert(addr != NULL);84assert(port_ptr != NULL);8586addr->sin_port = htons(PMAPPORT);87client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);88if (client != NULL) {89a.prog = prog;90a.vers = vers;91a.proc = proc;92a.args_ptr = argsp;93a.xdr_args = xdrargs;94r.port_ptr = port_ptr;95r.results_ptr = resp;96r.xdr_results = xdrres;97stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,98(xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,99&r, tout);100CLNT_DESTROY(client);101} else {102stat = RPC_FAILED;103}104addr->sin_port = 0;105return (stat);106}107108109/*110* XDR remote call arguments111* written for XDR_ENCODE direction only112*/113bool_t114xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)115{116u_int lenposition, argposition, position;117118assert(xdrs != NULL);119assert(cap != NULL);120121if (xdr_u_long(xdrs, &(cap->prog)) &&122xdr_u_long(xdrs, &(cap->vers)) &&123xdr_u_long(xdrs, &(cap->proc))) {124lenposition = XDR_GETPOS(xdrs);125if (! xdr_u_long(xdrs, &(cap->arglen)))126return (FALSE);127argposition = XDR_GETPOS(xdrs);128if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))129return (FALSE);130position = XDR_GETPOS(xdrs);131cap->arglen = (u_long)position - (u_long)argposition;132XDR_SETPOS(xdrs, lenposition);133if (! xdr_u_long(xdrs, &(cap->arglen)))134return (FALSE);135XDR_SETPOS(xdrs, position);136return (TRUE);137}138return (FALSE);139}140141/*142* XDR remote call results143* written for XDR_DECODE direction only144*/145bool_t146xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)147{148caddr_t port_ptr;149150assert(xdrs != NULL);151assert(crp != NULL);152153port_ptr = (caddr_t)(void *)crp->port_ptr;154if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),155(xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {156crp->port_ptr = (u_long *)(void *)port_ptr;157return ((*(crp->xdr_results))(xdrs, crp->results_ptr));158}159return (FALSE);160}161162163