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