pmap_rmt.c revision 1.30.6.1 1 /* $NetBSD: pmap_rmt.c,v 1.30.6.1 2012/04/17 00:05:23 yamt Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: pmap_rmt.c,v 1.30.6.1 2012/04/17 00:05:23 yamt Exp $");
39 #endif
40 #endif
41
42 /*
43 * pmap_rmt.c
44 * Client interface to pmap rpc service.
45 * remote call and broadcast service
46 *
47 * Copyright (C) 1984, Sun Microsystems, Inc.
48 */
49
50 #include "namespace.h"
51
52 #include <sys/types.h>
53 #include <sys/ioctl.h>
54 #include <sys/poll.h>
55 #include <sys/socket.h>
56
57 #include <net/if.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60
61 #include <assert.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <unistd.h>
67
68 #include <rpc/rpc.h>
69 #include <rpc/pmap_prot.h>
70 #include <rpc/pmap_clnt.h>
71 #include <rpc/pmap_rmt.h>
72
73 #ifdef __weak_alias
74 __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args)
75 __weak_alias(xdr_rmtcallres,_xdr_rmtcallres)
76 #endif
77
78 static const struct timeval timeout = { 3, 0 };
79
80 /*
81 * pmapper remote-call-service interface.
82 * This routine is used to call the pmapper remote call service
83 * which will look up a service program in the port maps, and then
84 * remotely call that routine with the given parameters. This allows
85 * programs to do a lookup and call in one step.
86 */
87 enum clnt_stat
88 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
89 port_ptr)
90 struct sockaddr_in *addr;
91 u_long prog, vers, proc;
92 xdrproc_t xdrargs, xdrres;
93 caddr_t argsp, resp;
94 struct timeval tout;
95 u_long *port_ptr;
96 {
97 int sock = -1;
98 CLIENT *client;
99 struct rmtcallargs a;
100 struct rmtcallres r;
101 enum clnt_stat stat;
102
103 _DIAGASSERT(addr != NULL);
104 _DIAGASSERT(port_ptr != NULL);
105
106 addr->sin_port = htons(PMAPPORT);
107 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
108 if (client != NULL) {
109 a.prog = prog;
110 a.vers = vers;
111 a.proc = proc;
112 a.args_ptr = argsp;
113 a.xdr_args = xdrargs;
114 r.port_ptr = port_ptr;
115 r.results_ptr = resp;
116 r.xdr_results = xdrres;
117 stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
118 (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
119 &r, tout);
120 CLNT_DESTROY(client);
121 } else {
122 stat = RPC_FAILED;
123 }
124 addr->sin_port = 0;
125 return (stat);
126 }
127
128
129 /*
130 * XDR remote call arguments
131 * written for XDR_ENCODE direction only
132 */
133 bool_t
134 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
135 {
136 u_int lenposition, argposition, position;
137
138 _DIAGASSERT(xdrs != NULL);
139 _DIAGASSERT(cap != NULL);
140
141 if (xdr_u_long(xdrs, &(cap->prog)) &&
142 xdr_u_long(xdrs, &(cap->vers)) &&
143 xdr_u_long(xdrs, &(cap->proc))) {
144 lenposition = XDR_GETPOS(xdrs);
145 if (! xdr_u_long(xdrs, &(cap->arglen)))
146 return (FALSE);
147 argposition = XDR_GETPOS(xdrs);
148 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
149 return (FALSE);
150 position = XDR_GETPOS(xdrs);
151 cap->arglen = (u_long)position - (u_long)argposition;
152 XDR_SETPOS(xdrs, lenposition);
153 if (! xdr_u_long(xdrs, &(cap->arglen)))
154 return (FALSE);
155 XDR_SETPOS(xdrs, position);
156 return (TRUE);
157 }
158 return (FALSE);
159 }
160
161 /*
162 * XDR remote call results
163 * written for XDR_DECODE direction only
164 */
165 bool_t
166 xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
167 {
168 caddr_t port_ptr;
169
170 _DIAGASSERT(xdrs != NULL);
171 _DIAGASSERT(crp != NULL);
172
173 port_ptr = (caddr_t)(void *)crp->port_ptr;
174 if (xdr_reference(xdrs, &port_ptr, (u_int)sizeof(u_long),
175 (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
176 crp->port_ptr = (u_long *)(void *)port_ptr;
177 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
178 }
179 return (FALSE);
180 }
181