Libinfo-221.tar.gz
[apple/libinfo.git] / rpc.subproj / pmap_rmt.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
31 *
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 *
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
39 *
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
43 *
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
47 *
48 * Sun Microsystems, Inc.
49 * 2550 Garcia Avenue
50 * Mountain View, California 94043
51 */
52
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";*/
56 static char *rcsid = "$Id: pmap_rmt.c,v 1.6 2004/12/19 22:45:44 zarzycki Exp $";
57 #endif
58
59 /*
60 * pmap_rmt.c
61 * Client interface to pmap rpc service.
62 * remote call and broadcast service
63 *
64 * Copyright (C) 1984, Sun Microsystems, Inc.
65 */
66
67 #include <string.h>
68 #include <unistd.h>
69 #include <rpc/rpc.h>
70 #include <rpc/pmap_prot.h>
71 #include <rpc/pmap_clnt.h>
72 #include <rpc/pmap_rmt.h>
73 #include <sys/socket.h>
74 #include <sys/fcntl.h>
75 #include <stdio.h>
76 #include <errno.h>
77 #include <net/if.h>
78 #include <sys/ioctl.h>
79 #include <arpa/inet.h>
80 #define MAX_BROADCAST_SIZE 1400
81
82 #include "pmap_wakeup.h"
83
84 static struct timeval timeout = { 3, 0 };
85
86
87 /*
88 * pmapper remote-call-service interface.
89 * This routine is used to call the pmapper remote call service
90 * which will look up a service program in the port maps, and then
91 * remotely call that routine with the given parameters. This allows
92 * programs to do a lookup and call in one step.
93 */
94 enum clnt_stat
95 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
96 struct sockaddr_in *addr;
97 u_long prog, vers, proc;
98 xdrproc_t xdrargs, xdrres;
99 caddr_t argsp, resp;
100 struct timeval tout;
101 u_long *port_ptr;
102 {
103 int socket = -1;
104 register CLIENT *client;
105 struct rmtcallargs a;
106 struct rmtcallres r;
107 enum clnt_stat stat;
108
109 pmap_wakeup();
110
111 addr->sin_port = htons(PMAPPORT);
112 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
113 if (client != (CLIENT *)NULL) {
114 a.prog = prog;
115 a.vers = vers;
116 a.proc = proc;
117 a.args_ptr = argsp;
118 a.xdr_args = xdrargs;
119 r.port_ptr = port_ptr;
120 r.results_ptr = resp;
121 r.xdr_results = xdrres;
122 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
123 xdr_rmtcallres, &r, tout);
124 CLNT_DESTROY(client);
125 } else {
126 stat = RPC_FAILED;
127 }
128 (void)close(socket);
129 addr->sin_port = 0;
130 return (stat);
131 }
132
133
134 /*
135 * XDR remote call arguments
136 * written for XDR_ENCODE direction only
137 */
138 bool_t
139 xdr_rmtcall_args(xdrs, cap)
140 register XDR *xdrs;
141 register struct rmtcallargs *cap;
142 {
143 u_int lenposition, argposition, position;
144
145 if (xdr_u_long(xdrs, &(cap->prog)) &&
146 xdr_u_long(xdrs, &(cap->vers)) &&
147 xdr_u_long(xdrs, &(cap->proc))) {
148 lenposition = XDR_GETPOS(xdrs);
149 if (! xdr_u_long(xdrs, &(cap->arglen)))
150 return (FALSE);
151 argposition = XDR_GETPOS(xdrs);
152 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
153 return (FALSE);
154 position = XDR_GETPOS(xdrs);
155 cap->arglen = (u_long)position - (u_long)argposition;
156 XDR_SETPOS(xdrs, lenposition);
157 if (! xdr_u_long(xdrs, &(cap->arglen)))
158 return (FALSE);
159 XDR_SETPOS(xdrs, position);
160 return (TRUE);
161 }
162 return (FALSE);
163 }
164
165 /*
166 * XDR remote call results
167 * written for XDR_DECODE direction only
168 */
169 bool_t
170 xdr_rmtcallres(xdrs, crp)
171 register XDR *xdrs;
172 register struct rmtcallres *crp;
173 {
174 caddr_t port_ptr;
175
176 port_ptr = (caddr_t)crp->port_ptr;
177 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
178 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
179 crp->port_ptr = (u_long *)port_ptr;
180 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
181 }
182 return (FALSE);
183 }
184
185
186 /*
187 * The following is kludged-up support for simple rpc broadcasts.
188 * Someday a large, complicated system will replace these trivial
189 * routines which only support udp/ip .
190 */
191
192 static int
193 getbroadcastnets(addrs, sock, buf)
194 struct in_addr *addrs;
195 int sock; /* any valid socket will do */
196 char *buf; /* why allocxate more when we can use existing... */
197 {
198 struct ifconf ifc;
199 struct ifreq ifreq, *ifr;
200 struct sockaddr_in *sin;
201 char *cp, *cplim;
202 int i = 0;
203
204 ifc.ifc_len = UDPMSGSIZE;
205 ifc.ifc_buf = buf;
206 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
207 perror("broadcast: ioctl (get interface configuration)");
208 return (0);
209 }
210 #define max(a, b) (a > b ? a : b)
211 #define size(p) max((p).sa_len, sizeof(p))
212 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
213 for (cp = buf; cp < cplim;
214 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
215 ifr = (struct ifreq *)cp;
216 if (ifr->ifr_addr.sa_family != AF_INET)
217 continue;
218 ifreq = *ifr;
219 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
220 perror("broadcast: ioctl (get interface flags)");
221 continue;
222 }
223 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
224 (ifreq.ifr_flags & IFF_UP)) {
225 sin = (struct sockaddr_in *)&ifr->ifr_addr;
226 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
227 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
228 addrs[i++] =
229 inet_makeaddr(inet_netof(sin->sin_addr),
230 INADDR_ANY);
231 } else {
232 addrs[i++] = ((struct sockaddr_in*)
233 &ifreq.ifr_addr)->sin_addr;
234 }
235 #else /* 4.2 BSD */
236 addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr),
237 INADDR_ANY);
238 #endif
239 }
240 }
241 return (i);
242 }
243
244 typedef bool_t (*resultproc_t)();
245
246 enum clnt_stat
247 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
248 u_long prog; /* program number */
249 u_long vers; /* version number */
250 u_long proc; /* procedure number */
251 xdrproc_t xargs; /* xdr routine for args */
252 caddr_t argsp; /* pointer to args */
253 xdrproc_t xresults; /* xdr routine for results */
254 caddr_t resultsp; /* pointer to results */
255 resultproc_t eachresult; /* call with each result obtained */
256 {
257 enum clnt_stat stat;
258 AUTH *unix_auth = authunix_create_default();
259 XDR xdr_stream;
260 register XDR *xdrs = &xdr_stream;
261 int outlen, inlen, fromlen, nets;
262 register int sock;
263 int on = 1;
264 fd_set mask;
265 fd_set readfds;
266 register int i;
267 bool_t done = FALSE;
268 u_long xid;
269 u_long port;
270 struct in_addr addrs[20];
271 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
272 struct rmtcallargs a;
273 struct rmtcallres r;
274 struct rpc_msg msg;
275 struct timeval t;
276 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
277 int rfd;
278
279 /*
280 * initialization: create a socket, a broadcast address, and
281 * preserialize the arguments into a send buffer.
282 */
283 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
284 perror("Cannot create socket for broadcast rpc");
285 stat = RPC_CANTSEND;
286 goto done_broad;
287 }
288 #ifdef SO_BROADCAST
289 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
290 perror("Cannot set socket option SO_BROADCAST");
291 stat = RPC_CANTSEND;
292 goto done_broad;
293 }
294 #endif /* def SO_BROADCAST */
295 FD_ZERO(&mask);
296 FD_SET(sock, &mask);
297 nets = getbroadcastnets(addrs, sock, inbuf);
298 bzero((char *)&baddr, sizeof (baddr));
299 baddr.sin_family = AF_INET;
300 baddr.sin_port = htons(PMAPPORT);
301 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
302 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
303
304 rfd = open("/dev/random", O_RDONLY, 0);
305 if ((rfd < 0) || (read(rfd, &xid, sizeof(xid)) != sizeof(xid)))
306 {
307 gettimeofday(&t, (struct timezone *)0);
308 xid = getpid() ^ t.tv_sec ^ t.tv_usec;
309 }
310 if (rfd > 0) close(rfd);
311
312 t.tv_usec = 0;
313 msg.rm_xid = xid;
314 msg.rm_direction = CALL;
315 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
316 msg.rm_call.cb_prog = PMAPPROG;
317 msg.rm_call.cb_vers = PMAPVERS;
318 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
319 msg.rm_call.cb_cred = unix_auth->ah_cred;
320 msg.rm_call.cb_verf = unix_auth->ah_verf;
321 a.prog = prog;
322 a.vers = vers;
323 a.proc = proc;
324 a.xdr_args = xargs;
325 a.args_ptr = argsp;
326 r.port_ptr = &port;
327 r.xdr_results = xresults;
328 r.results_ptr = resultsp;
329 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
330 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
331 stat = RPC_CANTENCODEARGS;
332 goto done_broad;
333 }
334 outlen = (int)xdr_getpos(xdrs);
335 xdr_destroy(xdrs);
336 /*
337 * Basic loop: broadcast a packet and wait a while for response(s).
338 * The response timeout grows larger per iteration.
339 */
340 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
341 for (i = 0; i < nets; i++) {
342 baddr.sin_addr = addrs[i];
343 if (sendto(sock, outbuf, outlen, 0,
344 (struct sockaddr *)&baddr,
345 sizeof (struct sockaddr)) != outlen) {
346 perror("Cannot send broadcast packet");
347 stat = RPC_CANTSEND;
348 goto done_broad;
349 }
350 }
351 if (eachresult == NULL) {
352 stat = RPC_SUCCESS;
353 goto done_broad;
354 }
355 recv_again:
356 msg.acpted_rply.ar_verf = _null_auth;
357 msg.acpted_rply.ar_results.where = (caddr_t)&r;
358 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
359 readfds = mask;
360 switch (select(sock+1, &readfds, NULL, NULL, &t)) {
361
362 case 0: /* timed out */
363 stat = RPC_TIMEDOUT;
364 continue;
365
366 case -1: /* some kind of error */
367 if (errno == EINTR)
368 goto recv_again;
369 perror("Broadcast select problem");
370 stat = RPC_CANTRECV;
371 goto done_broad;
372
373 } /* end of select results switch */
374 try_again:
375 fromlen = sizeof(struct sockaddr);
376 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
377 (struct sockaddr *)&raddr, &fromlen);
378 if (inlen < 0) {
379 if (errno == EINTR)
380 goto try_again;
381 perror("Cannot receive reply to broadcast");
382 stat = RPC_CANTRECV;
383 goto done_broad;
384 }
385 if (inlen < sizeof(u_long))
386 goto recv_again;
387 /*
388 * see if reply transaction id matches sent id.
389 * If so, decode the results.
390 */
391 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
392 if (xdr_replymsg(xdrs, &msg)) {
393 if ((msg.rm_xid == xid) &&
394 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
395 (msg.acpted_rply.ar_stat == SUCCESS)) {
396 raddr.sin_port = htons((u_short)port);
397 done = (*eachresult)(resultsp, &raddr);
398 }
399 /* otherwise, we just ignore the errors ... */
400 } else {
401 #ifdef notdef
402 /* some kind of deserialization problem ... */
403 if (msg.rm_xid == xid)
404 fprintf(stderr, "Broadcast deserialization problem");
405 /* otherwise, just random garbage */
406 #endif
407 }
408 xdrs->x_op = XDR_FREE;
409 msg.acpted_rply.ar_results.proc = xdr_void;
410 (void)xdr_replymsg(xdrs, &msg);
411 (void)(*xresults)(xdrs, resultsp);
412 xdr_destroy(xdrs);
413 if (done) {
414 stat = RPC_SUCCESS;
415 goto done_broad;
416 } else {
417 goto recv_again;
418 }
419 }
420 done_broad:
421 (void)close(sock);
422 AUTH_DESTROY(unix_auth);
423 return (stat);
424 }
425