Fri Aug 2 01:04:02 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / sunrpc / pmap_rmt.c
bloba944f6ccb6c3cb27d65cb65cd02e83f13e61e94a
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 <rpc/rpc.h>
43 #include <rpc/pmap_prot.h>
44 #include <rpc/pmap_clnt.h>
45 #include <rpc/pmap_rmt.h>
46 #include <sys/socket.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
50 #include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
51 #include <net/if.h>
52 #include <sys/ioctl.h>
53 #include <arpa/inet.h>
54 #define MAX_BROADCAST_SIZE 1400
56 extern int errno;
57 static struct timeval timeout = { 3, 0 };
61 * pmapper remote-call-service interface.
62 * This routine is used to call the pmapper remote call service
63 * which will look up a service program in the port maps, and then
64 * remotely call that routine with the given parameters. This allows
65 * programs to do a lookup and call in one step.
67 enum clnt_stat
68 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
69 struct sockaddr_in *addr;
70 u_long prog, vers, proc;
71 xdrproc_t xdrargs, xdrres;
72 caddr_t argsp, resp;
73 struct timeval tout;
74 u_long *port_ptr;
76 int socket = -1;
77 register CLIENT *client;
78 struct rmtcallargs a;
79 struct rmtcallres r;
80 enum clnt_stat stat;
82 addr->sin_port = htons(PMAPPORT);
83 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
84 if (client != (CLIENT *)NULL) {
85 a.prog = prog;
86 a.vers = vers;
87 a.proc = proc;
88 a.args_ptr = argsp;
89 a.xdr_args = xdrargs;
90 r.port_ptr = port_ptr;
91 r.results_ptr = resp;
92 r.xdr_results = xdrres;
93 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
94 xdr_rmtcallres, &r, tout);
95 CLNT_DESTROY(client);
96 } else {
97 stat = RPC_FAILED;
99 /* (void)close(socket); CLNT_DESTROY already closed it */
100 addr->sin_port = 0;
101 return (stat);
106 * XDR remote call arguments
107 * written for XDR_ENCODE direction only
109 bool_t
110 xdr_rmtcall_args(xdrs, cap)
111 register XDR *xdrs;
112 register struct rmtcallargs *cap;
114 u_int lenposition, argposition, position;
116 if (xdr_u_long(xdrs, &(cap->prog)) &&
117 xdr_u_long(xdrs, &(cap->vers)) &&
118 xdr_u_long(xdrs, &(cap->proc))) {
119 lenposition = XDR_GETPOS(xdrs);
120 if (! xdr_u_long(xdrs, &(cap->arglen)))
121 return (FALSE);
122 argposition = XDR_GETPOS(xdrs);
123 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
124 return (FALSE);
125 position = XDR_GETPOS(xdrs);
126 cap->arglen = (u_long)position - (u_long)argposition;
127 XDR_SETPOS(xdrs, lenposition);
128 if (! xdr_u_long(xdrs, &(cap->arglen)))
129 return (FALSE);
130 XDR_SETPOS(xdrs, position);
131 return (TRUE);
133 return (FALSE);
137 * XDR remote call results
138 * written for XDR_DECODE direction only
140 bool_t
141 xdr_rmtcallres(xdrs, crp)
142 register XDR *xdrs;
143 register struct rmtcallres *crp;
145 caddr_t port_ptr;
147 port_ptr = (caddr_t)crp->port_ptr;
148 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
149 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
150 crp->port_ptr = (u_long *)port_ptr;
151 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
153 return (FALSE);
158 * The following is kludged-up support for simple rpc broadcasts.
159 * Someday a large, complicated system will replace these trivial
160 * routines which only support udp/ip .
163 static int
164 getbroadcastnets(addrs, sock, buf)
165 struct in_addr *addrs;
166 int sock; /* any valid socket will do */
167 char *buf; /* why allocxate more when we can use existing... */
169 struct ifconf ifc;
170 struct ifreq ifreq, *ifr;
171 struct sockaddr_in *sin;
172 int n, i;
174 ifc.ifc_len = UDPMSGSIZE;
175 ifc.ifc_buf = buf;
176 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
177 perror(_("broadcast: ioctl (get interface configuration)"));
178 return (0);
180 ifr = ifc.ifc_req;
181 for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) {
182 ifreq = *ifr;
183 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
184 perror(_("broadcast: ioctl (get interface flags)"));
185 continue;
187 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
188 (ifreq.ifr_flags & IFF_UP) &&
189 ifr->ifr_addr.sa_family == AF_INET) {
190 sin = (struct sockaddr_in *)&ifr->ifr_addr;
191 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
192 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
193 addrs[i++] = inet_makeaddr(inet_netof
194 /* Changed to pass struct instead of s_addr member
195 by roland@gnu. */
196 (sin->sin_addr), INADDR_ANY);
197 } else {
198 addrs[i++] = ((struct sockaddr_in*)
199 &ifreq.ifr_addr)->sin_addr;
201 #else /* 4.2 BSD */
202 addrs[i++] = inet_makeaddr(inet_netof
203 (sin->sin_addr.s_addr), INADDR_ANY);
204 #endif
207 return (i);
211 enum clnt_stat
212 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
213 u_long prog; /* program number */
214 u_long vers; /* version number */
215 u_long proc; /* procedure number */
216 xdrproc_t xargs; /* xdr routine for args */
217 caddr_t argsp; /* pointer to args */
218 xdrproc_t xresults; /* xdr routine for results */
219 caddr_t resultsp; /* pointer to results */
220 resultproc_t eachresult; /* call with each result obtained */
222 enum clnt_stat stat;
223 AUTH *unix_auth = authunix_create_default();
224 XDR xdr_stream;
225 register XDR *xdrs = &xdr_stream;
226 int outlen, inlen, fromlen, nets;
227 register int sock;
228 int on = 1;
229 #ifdef FD_SETSIZE
230 fd_set mask;
231 fd_set readfds;
232 #else
233 int readfds;
234 register int mask;
235 #endif /* def FD_SETSIZE */
236 register int i;
237 bool_t done = FALSE;
238 register u_long xid;
239 u_long port;
240 struct in_addr addrs[20];
241 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
242 struct rmtcallargs a;
243 struct rmtcallres r;
244 struct rpc_msg msg;
245 struct timeval t;
246 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
249 * initialization: create a socket, a broadcast address, and
250 * preserialize the arguments into a send buffer.
252 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
253 perror(_("Cannot create socket for broadcast rpc"));
254 stat = RPC_CANTSEND;
255 goto done_broad;
257 #ifdef SO_BROADCAST
258 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 #ifdef FD_SETSIZE
265 FD_ZERO(&mask);
266 FD_SET(sock, &mask);
267 #else
268 mask = (1 << sock);
269 #endif /* def FD_SETSIZE */
270 nets = getbroadcastnets(addrs, sock, inbuf);
271 bzero((char *)&baddr, sizeof (baddr));
272 baddr.sin_family = AF_INET;
273 baddr.sin_port = htons(PMAPPORT);
274 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
275 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
276 (void)gettimeofday(&t, (struct timezone *)0);
277 msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec;
278 t.tv_usec = 0;
279 msg.rm_direction = CALL;
280 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
281 msg.rm_call.cb_prog = PMAPPROG;
282 msg.rm_call.cb_vers = PMAPVERS;
283 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
284 msg.rm_call.cb_cred = unix_auth->ah_cred;
285 msg.rm_call.cb_verf = unix_auth->ah_verf;
286 a.prog = prog;
287 a.vers = vers;
288 a.proc = proc;
289 a.xdr_args = xargs;
290 a.args_ptr = argsp;
291 r.port_ptr = &port;
292 r.xdr_results = xresults;
293 r.results_ptr = resultsp;
294 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
295 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
296 stat = RPC_CANTENCODEARGS;
297 goto done_broad;
299 outlen = (int)xdr_getpos(xdrs);
300 xdr_destroy(xdrs);
302 * Basic loop: broadcast a packet and wait a while for response(s).
303 * The response timeout grows larger per iteration.
305 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
306 for (i = 0; i < nets; i++) {
307 baddr.sin_addr = addrs[i];
308 if (sendto(sock, outbuf, outlen, 0,
309 (struct sockaddr *)&baddr,
310 sizeof (struct sockaddr)) != outlen) {
311 perror(_("Cannot send broadcast packet"));
312 stat = RPC_CANTSEND;
313 goto done_broad;
316 if (eachresult == NULL) {
317 stat = RPC_SUCCESS;
318 goto done_broad;
320 recv_again:
321 msg.acpted_rply.ar_verf = _null_auth;
322 msg.acpted_rply.ar_results.where = (caddr_t)&r;
323 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
324 readfds = mask;
325 switch (select(_rpc_dtablesize(), &readfds, (int *)NULL,
326 (int *)NULL, &t)) {
328 case 0: /* timed out */
329 stat = RPC_TIMEDOUT;
330 continue;
332 case -1: /* some kind of error */
333 if (errno == EINTR)
334 goto recv_again;
335 perror(_("Broadcast select problem"));
336 stat = RPC_CANTRECV;
337 goto done_broad;
339 } /* end of select results switch */
340 try_again:
341 fromlen = sizeof(struct sockaddr);
342 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
343 (struct sockaddr *)&raddr, &fromlen);
344 if (inlen < 0) {
345 if (errno == EINTR)
346 goto try_again;
347 perror(_("Cannot receive reply to broadcast"));
348 stat = RPC_CANTRECV;
349 goto done_broad;
351 if (inlen < sizeof(u_long))
352 goto recv_again;
354 * see if reply transaction id matches sent id.
355 * If so, decode the results.
357 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
358 if (xdr_replymsg(xdrs, &msg)) {
359 if ((msg.rm_xid == xid) &&
360 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
361 (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 ... */
366 } else {
367 #ifdef notdef
368 /* some kind of deserialization problem ... */
369 if (msg.rm_xid == xid)
370 fprintf(stderr, "Broadcast deserialization problem");
371 /* otherwise, just random garbage */
372 #endif
374 xdrs->x_op = XDR_FREE;
375 msg.acpted_rply.ar_results.proc = xdr_void;
376 (void)xdr_replymsg(xdrs, &msg);
377 (void)(*xresults)(xdrs, resultsp);
378 xdr_destroy(xdrs);
379 if (done) {
380 stat = RPC_SUCCESS;
381 goto done_broad;
382 } else {
383 goto recv_again;
386 done_broad:
387 (void)close(sock);
388 AUTH_DESTROY(unix_auth);
389 return (stat);