* shlib-versions (sparc.*-.*-.*, sparc64.*-.*-.*): Add ABI lines.
[glibc.git] / sunrpc / pmap_rmt.c
blobe06884891991320a069263e10b917017a9d83193
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};
66 * pmapper remote-call-service interface.
67 * This routine is used to call the pmapper remote call service
68 * which will look up a service program in the port maps, and then
69 * remotely call that routine with the given parameters. This allows
70 * programs to do a lookup and call in one step.
72 enum clnt_stat
73 pmap_rmtcall (addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, 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;
81 int socket = -1;
82 CLIENT *client;
83 struct rmtcallargs a;
84 struct rmtcallres r;
85 enum clnt_stat stat;
87 addr->sin_port = htons (PMAPPORT);
88 client = INTUSE(clntudp_create) (addr, PMAPPROG, PMAPVERS, timeout, &socket);
89 if (client != (CLIENT *) NULL)
91 a.prog = prog;
92 a.vers = vers;
93 a.proc = proc;
94 a.args_ptr = argsp;
95 a.xdr_args = xdrargs;
96 r.port_ptr = port_ptr;
97 r.results_ptr = resp;
98 r.xdr_results = xdrres;
99 stat = CLNT_CALL (client, PMAPPROC_CALLIT,
100 (xdrproc_t)INTUSE(xdr_rmtcall_args),
101 (caddr_t)&a, (xdrproc_t)INTUSE(xdr_rmtcallres),
102 (caddr_t)&r, tout);
103 CLNT_DESTROY (client);
105 else
107 stat = RPC_FAILED;
109 /* (void)__close(socket); CLNT_DESTROY already closed it */
110 addr->sin_port = 0;
111 return stat;
116 * XDR remote call arguments
117 * written for XDR_ENCODE direction only
119 bool_t
120 xdr_rmtcall_args (XDR *xdrs, struct rmtcallargs *cap)
122 u_int lenposition, argposition, position;
124 if (INTUSE(xdr_u_long) (xdrs, &(cap->prog)) &&
125 INTUSE(xdr_u_long) (xdrs, &(cap->vers)) &&
126 INTUSE(xdr_u_long) (xdrs, &(cap->proc)))
128 u_long dummy_arglen = 0;
129 lenposition = XDR_GETPOS (xdrs);
130 if (!INTUSE(xdr_u_long) (xdrs, &dummy_arglen))
131 return FALSE;
132 argposition = XDR_GETPOS (xdrs);
133 if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr))
134 return FALSE;
135 position = XDR_GETPOS (xdrs);
136 cap->arglen = (u_long) position - (u_long) argposition;
137 XDR_SETPOS (xdrs, lenposition);
138 if (!INTUSE(xdr_u_long) (xdrs, &(cap->arglen)))
139 return FALSE;
140 XDR_SETPOS (xdrs, position);
141 return TRUE;
143 return FALSE;
145 INTDEF(xdr_rmtcall_args)
148 * XDR remote call results
149 * written for XDR_DECODE direction only
151 bool_t
152 xdr_rmtcallres (xdrs, crp)
153 XDR *xdrs;
154 struct rmtcallres *crp;
156 caddr_t port_ptr;
158 port_ptr = (caddr_t) crp->port_ptr;
159 if (INTUSE(xdr_reference) (xdrs, &port_ptr, sizeof (u_long),
160 (xdrproc_t) INTUSE(xdr_u_long))
161 && INTUSE(xdr_u_long) (xdrs, &crp->resultslen))
163 crp->port_ptr = (u_long *) port_ptr;
164 return (*(crp->xdr_results)) (xdrs, crp->results_ptr);
166 return FALSE;
168 INTDEF(xdr_rmtcallres)
172 * The following is kludged-up support for simple rpc broadcasts.
173 * Someday a large, complicated system will replace these trivial
174 * routines which only support udp/ip .
177 static int
178 internal_function
179 getbroadcastnets (struct in_addr *addrs, int naddrs)
181 struct ifaddrs *ifa;
183 if (getifaddrs (&ifa) != 0)
185 perror ("broadcast: getifaddrs");
186 return 0;
189 int i = 0;
190 struct ifaddrs *run = ifa;
191 while (run != NULL && i < naddrs)
193 if ((run->ifa_flags & IFF_BROADCAST) != 0
194 && (run->ifa_flags & IFF_UP) != 0
195 && run->ifa_addr != NULL
196 && run->ifa_addr->sa_family == AF_INET)
197 /* Copy the broadcast address. */
198 addrs[i++] = ((struct sockaddr_in *) run->ifa_broadaddr)->sin_addr;
200 run = run->ifa_next;
203 freeifaddrs (ifa);
205 return i;
209 enum clnt_stat
210 clnt_broadcast (prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
211 u_long prog; /* program number */
212 u_long vers; /* version number */
213 u_long proc; /* procedure number */
214 xdrproc_t xargs; /* xdr routine for args */
215 caddr_t argsp; /* pointer to args */
216 xdrproc_t xresults; /* xdr routine for results */
217 caddr_t resultsp; /* pointer to results */
218 resultproc_t eachresult; /* call with each result obtained */
220 enum clnt_stat stat = RPC_FAILED;
221 AUTH *unix_auth = INTUSE(authunix_create_default) ();
222 XDR xdr_stream;
223 XDR *xdrs = &xdr_stream;
224 struct timeval t;
225 int outlen, inlen, nets;
226 socklen_t fromlen;
227 int sock;
228 int on = 1;
229 struct pollfd fd;
230 int milliseconds;
231 int i;
232 bool_t done = FALSE;
233 u_long xid;
234 u_long port;
235 struct in_addr addrs[20];
236 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
237 struct rmtcallargs a;
238 struct rmtcallres r;
239 struct rpc_msg msg;
240 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
243 * initialization: create a socket, a broadcast address, and
244 * preserialize the arguments into a send buffer.
246 if ((sock = __socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
248 perror (_("Cannot create socket for broadcast rpc"));
249 stat = RPC_CANTSEND;
250 goto done_broad;
252 #ifdef SO_BROADCAST
253 if (__setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0)
255 perror (_("Cannot set socket option SO_BROADCAST"));
256 stat = RPC_CANTSEND;
257 goto done_broad;
259 #endif /* def SO_BROADCAST */
260 fd.fd = sock;
261 fd.events = POLLIN;
262 nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0]));
263 __bzero ((char *) &baddr, sizeof (baddr));
264 baddr.sin_family = AF_INET;
265 baddr.sin_port = htons (PMAPPORT);
266 baddr.sin_addr.s_addr = htonl (INADDR_ANY);
267 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
268 msg.rm_xid = xid = _create_xid ();
269 t.tv_usec = 0;
270 msg.rm_direction = CALL;
271 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
272 msg.rm_call.cb_prog = PMAPPROG;
273 msg.rm_call.cb_vers = PMAPVERS;
274 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
275 msg.rm_call.cb_cred = unix_auth->ah_cred;
276 msg.rm_call.cb_verf = unix_auth->ah_verf;
277 a.prog = prog;
278 a.vers = vers;
279 a.proc = proc;
280 a.xdr_args = xargs;
281 a.args_ptr = argsp;
282 r.port_ptr = &port;
283 r.xdr_results = xresults;
284 r.results_ptr = resultsp;
285 INTUSE(xdrmem_create) (xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
286 if ((!INTUSE(xdr_callmsg) (xdrs, &msg))
287 || (!INTUSE(xdr_rmtcall_args) (xdrs, &a)))
289 stat = RPC_CANTENCODEARGS;
290 goto done_broad;
292 outlen = (int) xdr_getpos (xdrs);
293 xdr_destroy (xdrs);
295 * Basic loop: broadcast a packet and wait a while for response(s).
296 * The response timeout grows larger per iteration.
298 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2)
300 for (i = 0; i < nets; i++)
302 baddr.sin_addr = addrs[i];
303 if (__sendto (sock, outbuf, outlen, 0,
304 (struct sockaddr *) &baddr,
305 sizeof (struct sockaddr)) != outlen)
307 perror (_("Cannot send broadcast packet"));
308 stat = RPC_CANTSEND;
309 goto done_broad;
312 if (eachresult == NULL)
314 stat = RPC_SUCCESS;
315 goto done_broad;
317 recv_again:
318 msg.acpted_rply.ar_verf = _null_auth;
319 msg.acpted_rply.ar_results.where = (caddr_t) & r;
320 msg.acpted_rply.ar_results.proc = (xdrproc_t) INTUSE(xdr_rmtcallres);
321 milliseconds = t.tv_sec * 1000 + t.tv_usec / 1000;
322 switch (__poll(&fd, 1, milliseconds))
325 case 0: /* timed out */
326 stat = RPC_TIMEDOUT;
327 continue;
329 case -1: /* some kind of error */
330 if (errno == EINTR)
331 goto recv_again;
332 perror (_("Broadcast poll problem"));
333 stat = RPC_CANTRECV;
334 goto done_broad;
336 } /* end of poll results switch */
337 try_again:
338 fromlen = sizeof (struct sockaddr);
339 inlen = __recvfrom (sock, inbuf, UDPMSGSIZE, 0,
340 (struct sockaddr *) &raddr, &fromlen);
341 if (inlen < 0)
343 if (errno == EINTR)
344 goto try_again;
345 perror (_("Cannot receive reply to broadcast"));
346 stat = RPC_CANTRECV;
347 goto done_broad;
349 if ((size_t) inlen < sizeof (u_long))
350 goto recv_again;
352 * see if reply transaction id matches sent id.
353 * If so, decode the results.
355 INTUSE(xdrmem_create) (xdrs, inbuf, (u_int) inlen, XDR_DECODE);
356 if (INTUSE(xdr_replymsg) (xdrs, &msg))
358 if (((u_int32_t) msg.rm_xid == (u_int32_t) xid) &&
359 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
360 (msg.acpted_rply.ar_stat == SUCCESS))
362 raddr.sin_port = htons ((u_short) port);
363 done = (*eachresult) (resultsp, &raddr);
365 /* otherwise, we just ignore the errors ... */
367 else
369 #ifdef notdef
370 /* some kind of deserialization problem ... */
371 if ((u_int32_t) msg.rm_xid == (u_int32_t) xid)
372 fprintf (stderr, "Broadcast deserialization problem");
373 /* otherwise, just random garbage */
374 #endif
376 xdrs->x_op = XDR_FREE;
377 msg.acpted_rply.ar_results.proc = (xdrproc_t)INTUSE(xdr_void);
378 (void) INTUSE(xdr_replymsg) (xdrs, &msg);
379 (void) (*xresults) (xdrs, resultsp);
380 xdr_destroy (xdrs);
381 if (done)
383 stat = RPC_SUCCESS;
384 goto done_broad;
386 else
388 goto recv_again;
391 done_broad:
392 (void) __close (sock);
393 AUTH_DESTROY (unix_auth);
394 return stat;