Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/pmap_rmt.c

raw symbols SCIP
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
29 /*
30 * pmap_rmt.c
31 * Client interface to pmap rpc service.
32 * remote call and broadcast service
33 *
34 * Copyright (C) 1984, Sun Microsystems, Inc.
35 */
36
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <poll.h>
40 #include <sys/socket.h>
41
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45
46 #include <assert.h>
47 #include <err.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include <rpc/rpc.h>
54 #include <rpc/pmap_prot.h>
55 #include <rpc/pmap_clnt.h>
56 #include <rpc/pmap_rmt.h>
57
58 // For the clnttcp_create function
59 //#include <clnt_soc.h>
60
61
62 static const struct timeval timeout = { 3, 0 };
63
64 /*
65 * pmapper remote-call-service interface.
66 * This routine is used to call the pmapper remote call service
67 * which will look up a service program in the port maps, and then
68 * remotely call that routine with the given parameters. This allows
69 * programs to do a lookup and call in one step.
70 */
71 enum clnt_stat
72 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
73 port_ptr)
74 struct sockaddr_in *addr;
75 u_long prog, vers, proc;
76 xdrproc_t xdrargs, xdrres;
77 caddr_t argsp, resp;
78 struct timeval tout;
79 u_long *port_ptr;
80 {
81 int sock = -1;
82 CLIENT *client;
83 struct rmtcallargs a;
84 struct rmtcallres r;
85 enum clnt_stat stat;
86
87 assert(addr != NULL);
88 assert(port_ptr != NULL);
89
90 addr->sin_port = htons(PMAPPORT);
91 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
92 if (client != NULL) {
93 a.prog = prog;
94 a.vers = vers;
95 a.proc = proc;
96 a.args_ptr = argsp;
97 a.xdr_args = xdrargs;
98 r.port_ptr = port_ptr;
99 r.results_ptr = resp;
100 r.xdr_results = xdrres;
101 stat = CLNT_CALL(client, (rpcproc_t)PMAPPROC_CALLIT,
102 (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
103 &r, tout);
104 CLNT_DESTROY(client);
105 } else {
106 stat = RPC_FAILED;
107 }
108 addr->sin_port = 0;
109 return (stat);
110 }
111
112
113 /*
114 * XDR remote call arguments
115 * written for XDR_ENCODE direction only
116 */
117 bool_t
118 xdr_rmtcall_args(xdrs, cap)
119 XDR *xdrs;
120 struct rmtcallargs *cap;
121 {
122 u_int lenposition, argposition, position;
123
124 assert(xdrs != NULL);
125 assert(cap != NULL);
126
127 if (xdr_u_long(xdrs, &(cap->prog)) &&
128 xdr_u_long(xdrs, &(cap->vers)) &&
129 xdr_u_long(xdrs, &(cap->proc))) {
130 lenposition = XDR_GETPOS(xdrs);
131 if (! xdr_u_long(xdrs, &(cap->arglen)))
132 return (FALSE);
133 argposition = XDR_GETPOS(xdrs);
134 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
135 return (FALSE);
136 position = XDR_GETPOS(xdrs);
137 cap->arglen = (u_long)position - (u_long)argposition;
138 XDR_SETPOS(xdrs, lenposition);
139 if (! xdr_u_long(xdrs, &(cap->arglen)))
140 return (FALSE);
141 XDR_SETPOS(xdrs, position);
142 return (TRUE);
143 }
144 return (FALSE);
145 }
146
147 /*
148 * XDR remote call results
149 * written for XDR_DECODE direction only
150 */
151 bool_t
152 xdr_rmtcallres(xdrs, crp)
153 XDR *xdrs;
154 struct rmtcallres *crp;
155 {
156 caddr_t port_ptr;
157
158 assert(xdrs != NULL);
159 assert(crp != NULL);
160
161 port_ptr = (caddr_t)(void *)crp->port_ptr;
162 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
163 (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
164 crp->port_ptr = (u_long *)(void *)port_ptr;
165 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
166 }
167 return (FALSE);
168 }