xref: /dragonfly/lib/libc/rpc/pmap_rmt.c (revision ba96d07f7c72db2df2f4855fd9cef54db7111960)
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro
29  * @(#)pmap_rmt.c   2.2 88/08/01 4.0 RPCSRC
30  * $NetBSD: pmap_rmt.c,v 1.29 2000/07/06 03:10:34 christos Exp $
31  * $FreeBSD: src/lib/libc/rpc/pmap_rmt.c,v 1.20 2004/10/16 06:11:35 obrien Exp $
32  * $DragonFly: src/lib/libc/rpc/pmap_rmt.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
33  */
34 
35 /*
36  * pmap_rmt.c
37  * Client interface to pmap rpc service.
38  * remote call and broadcast service
39  *
40  * Copyright (C) 1984, Sun Microsystems, Inc.
41  */
42 
43 #include "namespace.h"
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <sys/poll.h>
47 #include <sys/socket.h>
48 
49 #include <net/if.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 
53 #include <assert.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include <rpc/rpc.h>
61 #include <rpc/pmap_prot.h>
62 #include <rpc/pmap_clnt.h>
63 #include <rpc/pmap_rmt.h>
64 #include "un-namespace.h"
65 
66 static const struct timeval timeout = { 3, 0 };
67 
68 /*
69  * pmapper remote-call-service interface.
70  * This routine is used to call the pmapper remote call service
71  * which will look up a service program in the port maps, and then
72  * remotely call that routine with the given parameters.  This allows
73  * programs to do a lookup and call in one step.
74 */
75 enum clnt_stat
pmap_rmtcall(struct sockaddr_in * addr,u_long prog,u_long vers,u_long proc,xdrproc_t xdrargs,caddr_t argsp,xdrproc_t xdrres,caddr_t resp,struct timeval tout,u_long * port_ptr)76 pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
77                xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
78                struct timeval tout, u_long *port_ptr)
79 {
80           int sock = -1;
81           CLIENT *client;
82           struct rmtcallargs a;
83           struct rmtcallres r;
84           enum clnt_stat stat;
85 
86           assert(addr != NULL);
87           assert(port_ptr != NULL);
88 
89           addr->sin_port = htons(PMAPPORT);
90           client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
91           if (client != NULL) {
92                     a.prog = prog;
93                     a.vers = vers;
94                     a.proc = proc;
95                     a.args_ptr = argsp;
96                     a.xdr_args = xdrargs;
97                     r.port_ptr = port_ptr;
98                     r.results_ptr = resp;
99                     r.xdr_results = xdrres;
100                     stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
101                         (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
102                         &r, tout);
103                     CLNT_DESTROY(client);
104           } else {
105                     stat = RPC_FAILED;
106           }
107           addr->sin_port = 0;
108           return (stat);
109 }
110 
111 
112 /*
113  * XDR remote call arguments
114  * written for XDR_ENCODE direction only
115  */
116 bool_t
xdr_rmtcall_args(XDR * xdrs,struct rmtcallargs * cap)117 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
118 {
119           u_int lenposition, argposition, position;
120 
121           assert(xdrs != NULL);
122           assert(cap != NULL);
123 
124           if (xdr_u_long(xdrs, &(cap->prog)) &&
125               xdr_u_long(xdrs, &(cap->vers)) &&
126               xdr_u_long(xdrs, &(cap->proc))) {
127                     lenposition = XDR_GETPOS(xdrs);
128                     if (! xdr_u_long(xdrs, &(cap->arglen)))
129                         return (FALSE);
130                     argposition = XDR_GETPOS(xdrs);
131                     if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
132                         return (FALSE);
133                     position = XDR_GETPOS(xdrs);
134                     cap->arglen = (u_long)position - (u_long)argposition;
135                     XDR_SETPOS(xdrs, lenposition);
136                     if (! xdr_u_long(xdrs, &(cap->arglen)))
137                         return (FALSE);
138                     XDR_SETPOS(xdrs, position);
139                     return (TRUE);
140           }
141           return (FALSE);
142 }
143 
144 /*
145  * XDR remote call results
146  * written for XDR_DECODE direction only
147  */
148 bool_t
xdr_rmtcallres(XDR * xdrs,struct rmtcallres * crp)149 xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
150 {
151           caddr_t port_ptr;
152 
153           assert(xdrs != NULL);
154           assert(crp != NULL);
155 
156           port_ptr = (caddr_t)(void *)crp->port_ptr;
157           if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
158               (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
159                     crp->port_ptr = (u_long *)(void *)port_ptr;
160                     return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
161           }
162           return (FALSE);
163 }
164