* include/rpc/key_prot.h: Maek all _internal functions as hidden.
[glibc.git] / sunrpc / pmap_rmt.c
blob7de7f4147c0fa88344359ba9f8c94ebd24f4835b
1 /* @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
32 #endif
35 * pmap_rmt.c
36 * Client interface to pmap rpc service.
37 * remote call and broadcast service
39 * Copyright (C) 1984, Sun Microsystems, Inc.
42 #include <unistd.h>
43 #include <string.h>
44 #include <libintl.h>
45 #include <rpc/rpc.h>
46 #include <rpc/pmap_prot.h>
47 #include <rpc/pmap_clnt.h>
48 #include <rpc/pmap_rmt.h>
49 #include <sys/poll.h>
50 #include <sys/socket.h>
51 #include <stdio.h>
52 #include <errno.h>
53 #undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
54 #include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
55 #include <net/if.h>
56 #include <ifaddrs.h>
57 #include <sys/ioctl.h>
58 #include <arpa/inet.h>
59 #define MAX_BROADCAST_SIZE 1400
61 extern u_long _create_xid (void);
63 static const struct timeval timeout = {3, 0};
65 bool_t xdr_rmtcall_args_internal (XDR *xdrs, struct rmtcallargs *cap)
66 attribute_hidden;
70 * pmapper remote-call-service interface.
71 * This routine is used to call the pmapper remote call service
72 * which will look up a service program in the port maps, and then
73 * remotely call that routine with the given parameters. This allows
74 * programs to do a lookup and call in one step.
76 enum clnt_stat
77 pmap_rmtcall (addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
78 struct sockaddr_in *addr;
79 u_long prog, vers, proc;
80 xdrproc_t xdrargs, xdrres;
81 caddr_t argsp, resp;
82 struct timeval tout;
83 u_long *port_ptr;
85 int socket = -1;
86 CLIENT *client;
87 struct rmtcallargs a;
88 struct rmtcallres r;
89 enum clnt_stat stat;
91 addr->sin_port = htons (PMAPPORT);
92 client = INTUSE(clntudp_create) (addr, PMAPPROG, PMAPVERS, timeout, &socket);
93 if (client != (CLIENT *) NULL)
95 a.prog = prog;
96 a.vers = vers;
97 a.proc = proc;
98 a.args_ptr = argsp;
99 a.xdr_args = xdrargs;
100 r.port_ptr = port_ptr;
101 r.results_ptr = resp;
102 r.xdr_results = xdrres;
103 stat = CLNT_CALL (client, PMAPPROC_CALLIT,
104 (xdrproc_t)INTUSE(xdr_rmtcall_args),
105 (caddr_t)&a, (xdrproc_t)INTUSE(xdr_rmtcallres),
106 (caddr_t)&r, tout);
107 CLNT_DESTROY (client);
109 else
111 stat = RPC_FAILED;
113 /* (void)__close(socket); CLNT_DESTROY already closed it */
114 addr->sin_port = 0;
115 return stat;
120 * XDR remote call arguments
121 * written for XDR_ENCODE direction only
123 bool_t
124 xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
126 u_int lenposition, argposition, position;
128 if (INTUSE(xdr_u_long) (xdrs, &(cap->prog)) &&
129 INTUSE(xdr_u_long) (xdrs, &(cap->vers)) &&
130 INTUSE(xdr_u_long) (xdrs, &(cap->proc)))
132 u_long dummy_arglen = 0;
133 lenposition = XDR_GETPOS (xdrs);
134 if (!INTUSE(xdr_u_long) (xdrs, &dummy_arglen))
135 return FALSE;
136 argposition = XDR_GETPOS (xdrs);
137 if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
138 return FALSE;
139 position = XDR_GETPOS (xdrs);
140 cap->arglen = (u_long) position - (u_long) argposition;
141 XDR_SETPOS (xdrs, lenposition);
142 if (!INTUSE(xdr_u_long) (xdrs, &(cap->arglen)))
143 return FALSE;
144 XDR_SETPOS (xdrs, position);
145 return TRUE;
147 return FALSE;
149 INTDEF(xdr_rmtcall_args)
152 * XDR remote call results
153 * written for XDR_DECODE direction only
155 bool_t
156 xdr_rmtcallres (xdrs, crp)
157 XDR *xdrs;
158 struct rmtcallres *crp;
160 caddr_t port_ptr;
162 port_ptr = (caddr_t) crp->port_ptr;
163 if (INTUSE(xdr_reference) (xdrs, &port_ptr, sizeof (u_long),
164 (xdrproc_t) INTUSE(xdr_u_long))
165 && INTUSE(xdr_u_long) (xdrs, &crp->resultslen))
167 crp->port_ptr = (u_long *) port_ptr;
168 return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
170 return FALSE;
172 INTDEF(xdr_rmtcallres)
176 * The following is kludged-up support for simple rpc broadcasts.
177 * Someday a large, complicated system will replace these trivial
178 * routines which only support udp/ip .
181 static int
182 internal_function
183 getbroadcastnets (struct in_addr *addrs, int naddrs)
185 struct ifaddrs *ifa;
187 if (getifaddrs (&ifa) != 0)
189 perror ("broadcast: getifaddrs");
190 return 0;
193 int i = 0;
194 struct ifaddrs *run = ifa;
195 while (run != NULL && i < naddrs)
197 if ((run->ifa_flags & IFF_BROADCAST) != 0
198 && (run->ifa_flags & IFF_UP) != 0
199 && run->ifa_addr != NULL
200 && run->ifa_addr->sa_family == AF_INET)
201 /* Copy the broadcast address. */
202 addrs[i++] = ((struct sockaddr_in *) run->ifa_broadaddr)->sin_addr;
204 run = run->ifa_next;
207 freeifaddrs (ifa);
209 return i;
213 enum clnt_stat
214 clnt_broadcast (prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
215 u_long prog; /* program number */
216 u_long vers; /* version number */
217 u_long proc; /* procedure number */
218 xdrproc_t xargs; /* xdr routine for args */
219 caddr_t argsp; /* pointer to args */
220 xdrproc_t xresults; /* xdr routine for results */
221 caddr_t resultsp; /* pointer to results */
222 resultproc_t eachresult; /* call with each result obtained */
224 enum clnt_stat stat = RPC_FAILED;
225 AUTH *unix_auth = INTUSE(authunix_create_default) ();
226 XDR xdr_stream;
227 XDR *xdrs = &xdr_stream;
228 struct timeval t;
229 int outlen, inlen, nets;
230 socklen_t fromlen;
231 int sock;
232 int on = 1;
233 struct pollfd fd;
234 int milliseconds;
235 int i;
236 bool_t done = FALSE;
237 u_long xid;
238 u_long port;
239 struct in_addr addrs[20];
240 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
241 struct rmtcallargs a;
242 struct rmtcallres r;
243 struct rpc_msg msg;
244 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
247 * initialization: create a socket, a broadcast address, and
248 * preserialize the arguments into a send buffer.
250 if ((sock = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
252 perror (_("Cannot create socket for broadcast rpc"));
253 stat = RPC_CANTSEND;
254 goto done_broad;
256 #ifdef SO_BROADCAST
257 if (__setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
259 perror (_("Cannot set socket option SO_BROADCAST"));
260 stat = RPC_CANTSEND;
261 goto done_broad;
263 #endif /* def SO_BROADCAST */
264 fd.fd = sock;
265 fd.events = POLLIN;
266 nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0]));
267 __bzero ((char *) &baddr, sizeof (baddr));
268 baddr.sin_family = AF_INET;
269 baddr.sin_port = htons (PMAPPORT);
270 baddr.sin_addr.s_addr = htonl (INADDR_ANY);
271 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
272 msg.rm_xid = xid = _create_xid ();
273 t.tv_usec = 0;
274 msg.rm_direction = CALL;
275 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
276 msg.rm_call.cb_prog = PMAPPROG;
277 msg.rm_call.cb_vers = PMAPVERS;
278 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
279 msg.rm_call.cb_cred = unix_auth->ah_cred;
280 msg.rm_call.cb_verf = unix_auth->ah_verf;
281 a.prog = prog;
282 a.vers = vers;
283 a.proc = proc;
284 a.xdr_args = xargs;
285 a.args_ptr = argsp;
286 r.port_ptr = &port;
287 r.xdr_results = xresults;
288 r.results_ptr = resultsp;
289 INTUSE(xdrmem_create) (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
290 if ((!INTUSE(xdr_callmsg) (xdrs, &msg))
291 || (!INTUSE(xdr_rmtcall_args) (xdrs, &a)))
293 stat = RPC_CANTENCODEARGS;
294 goto done_broad;
296 outlen = (int) xdr_getpos (xdrs);
297 xdr_destroy (xdrs);
299 * Basic loop: broadcast a packet and wait a while for response(s).
300 * The response timeout grows larger per iteration.
302 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
304 for (i = 0; i < nets; i++)
306 baddr.sin_addr = addrs[i];
307 if (__sendto (sock, outbuf, outlen, 0,
308 (struct sockaddr *) &baddr,
309 sizeof (struct sockaddr)) != outlen)
311 perror (_("Cannot send broadcast packet"));
312 stat = RPC_CANTSEND;
313 goto done_broad;
316 if (eachresult == NULL)
318 stat = RPC_SUCCESS;
319 goto done_broad;
321 recv_again:
322 msg.acpted_rply.ar_verf = _null_auth;
323 msg.acpted_rply.ar_results.where = (caddr_t) & r;
324 msg.acpted_rply.ar_results.proc = (xdrproc_t) INTUSE(xdr_rmtcallres);
325 milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
326 switch (__poll(&fd, 1, milliseconds))
329 case 0: /* timed out */
330 stat = RPC_TIMEDOUT;
331 continue;
333 case -1: /* some kind of error */
334 if (errno == EINTR)
335 goto recv_again;
336 perror (_("Broadcast poll problem"));
337 stat = RPC_CANTRECV;
338 goto done_broad;
340 } /* end of poll results switch */
341 try_again:
342 fromlen = sizeof (struct sockaddr);
343 inlen = __recvfrom (sock, inbuf, UDPMSGSIZE, 0,
344 (struct sockaddr *) &raddr, &fromlen);
345 if (inlen < 0)
347 if (errno == EINTR)
348 goto try_again;
349 perror (_("Cannot receive reply to broadcast"));
350 stat = RPC_CANTRECV;
351 goto done_broad;
353 if ((size_t) inlen < sizeof (u_long))
354 goto recv_again;
356 * see if reply transaction id matches sent id.
357 * If so, decode the results.
359 INTUSE(xdrmem_create) (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
360 if (INTUSE(xdr_replymsg) (xdrs, &msg))
362 if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
363 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
364 (msg.acpted_rply.ar_stat == SUCCESS))
366 raddr.sin_port = htons ((u_short) port);
367 done = (*eachresult) (resultsp, &raddr);
369 /* otherwise, we just ignore the errors ... */
371 else
373 #ifdef notdef
374 /* some kind of deserialization problem ... */
375 if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
376 fprintf (stderr, "Broadcast deserialization problem");
377 /* otherwise, just random garbage */
378 #endif
380 xdrs->x_op = XDR_FREE;
381 msg.acpted_rply.ar_results.proc = (xdrproc_t)INTUSE(xdr_void);
382 (void) INTUSE(xdr_replymsg) (xdrs, &msg);
383 (void) (*xresults) (xdrs, resultsp);
384 xdr_destroy (xdrs);
385 if (done)
387 stat = RPC_SUCCESS;
388 goto done_broad;
390 else
392 goto recv_again;
395 done_broad:
396 (void) __close (sock);
397 AUTH_DESTROY (unix_auth);
398 return stat;