]> git.saurik.com Git - apple/libinfo.git/blame - rpc.subproj/pmap_rmt.c
Libinfo-542.40.3.tar.gz
[apple/libinfo.git] / rpc.subproj / pmap_rmt.c
CommitLineData
03fb6eb0 1/*
f475de6c 2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
03fb6eb0
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ad21edcc
A
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.
03fb6eb0
A
13 *
14 * The Original Code and all software distributed under the License are
ad21edcc 15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
03fb6eb0
A
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ad21edcc
A
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.
03fb6eb0
A
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";*/
c29f2fcc 56static char *rcsid = "$Id: pmap_rmt.c,v 1.6 2004/12/19 22:45:44 zarzycki Exp $";
03fb6eb0
A
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
f475de6c
A
67#include "libinfo_common.h"
68
3b7c7bd7
A
69#include <string.h>
70#include <unistd.h>
03fb6eb0
A
71#include <rpc/rpc.h>
72#include <rpc/pmap_prot.h>
73#include <rpc/pmap_clnt.h>
74#include <rpc/pmap_rmt.h>
75#include <sys/socket.h>
3b7c7bd7 76#include <sys/fcntl.h>
03fb6eb0
A
77#include <stdio.h>
78#include <errno.h>
79#include <net/if.h>
80#include <sys/ioctl.h>
81#include <arpa/inet.h>
82#define MAX_BROADCAST_SIZE 1400
83
03fb6eb0
A
84static 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*/
f475de6c 94LIBINFO_EXPORT
03fb6eb0
A
95enum clnt_stat
96pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr)
b3dd680f
A
97#ifdef __LP64__
98 struct sockaddr_in *addr;
99 uint32_t prog, vers, proc;
100 xdrproc_t xdrargs, xdrres;
101 caddr_t argsp, resp;
102 struct timeval tout;
103 uint32_t *port_ptr;
104#else
03fb6eb0
A
105 struct sockaddr_in *addr;
106 u_long prog, vers, proc;
107 xdrproc_t xdrargs, xdrres;
108 caddr_t argsp, resp;
109 struct timeval tout;
110 u_long *port_ptr;
b3dd680f 111#endif
03fb6eb0
A
112{
113 int socket = -1;
114 register CLIENT *client;
115 struct rmtcallargs a;
116 struct rmtcallres r;
117 enum clnt_stat stat;
118
119 addr->sin_port = htons(PMAPPORT);
120 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
121 if (client != (CLIENT *)NULL) {
122 a.prog = prog;
123 a.vers = vers;
124 a.proc = proc;
125 a.args_ptr = argsp;
126 a.xdr_args = xdrargs;
127 r.port_ptr = port_ptr;
128 r.results_ptr = resp;
129 r.xdr_results = xdrres;
b3dd680f 130 stat = CLNT_CALL(client, PMAPPROC_CALLIT, (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres, &r, tout);
03fb6eb0
A
131 CLNT_DESTROY(client);
132 } else {
133 stat = RPC_FAILED;
134 }
135 (void)close(socket);
136 addr->sin_port = 0;
137 return (stat);
138}
139
140
141/*
142 * XDR remote call arguments
143 * written for XDR_ENCODE direction only
144 */
f475de6c 145LIBINFO_EXPORT
03fb6eb0
A
146bool_t
147xdr_rmtcall_args(xdrs, cap)
148 register XDR *xdrs;
149 register struct rmtcallargs *cap;
150{
151 u_int lenposition, argposition, position;
152
153 if (xdr_u_long(xdrs, &(cap->prog)) &&
154 xdr_u_long(xdrs, &(cap->vers)) &&
155 xdr_u_long(xdrs, &(cap->proc))) {
156 lenposition = XDR_GETPOS(xdrs);
157 if (! xdr_u_long(xdrs, &(cap->arglen)))
158 return (FALSE);
159 argposition = XDR_GETPOS(xdrs);
0eb52ff2 160 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr, 0))
03fb6eb0
A
161 return (FALSE);
162 position = XDR_GETPOS(xdrs);
1c0d47b0
A
163#ifdef __LP64__
164 cap->arglen = (uint32_t)position - (uint32_t)argposition;
165#else
03fb6eb0 166 cap->arglen = (u_long)position - (u_long)argposition;
1c0d47b0 167#endif
03fb6eb0
A
168 XDR_SETPOS(xdrs, lenposition);
169 if (! xdr_u_long(xdrs, &(cap->arglen)))
170 return (FALSE);
171 XDR_SETPOS(xdrs, position);
172 return (TRUE);
173 }
174 return (FALSE);
175}
176
177/*
178 * XDR remote call results
179 * written for XDR_DECODE direction only
180 */
f475de6c 181LIBINFO_EXPORT
03fb6eb0
A
182bool_t
183xdr_rmtcallres(xdrs, crp)
184 register XDR *xdrs;
185 register struct rmtcallres *crp;
186{
187 caddr_t port_ptr;
188
189 port_ptr = (caddr_t)crp->port_ptr;
b3dd680f 190#ifdef __LP64__
1c0d47b0 191 if (xdr_reference(xdrs, &port_ptr, sizeof (uint32_t), (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
b3dd680f 192 crp->port_ptr = (unsigned int *)port_ptr;
0eb52ff2 193 return ((*(crp->xdr_results))(xdrs, crp->results_ptr, 0));
1c0d47b0 194 }
b3dd680f 195#else
1c0d47b0 196 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long), (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
b3dd680f 197 crp->port_ptr = (unsigned long *)port_ptr;
0eb52ff2 198 return ((*(crp->xdr_results))(xdrs, crp->results_ptr, 0));
03fb6eb0 199 }
1c0d47b0 200#endif
03fb6eb0
A
201 return (FALSE);
202}
203
204
205/*
206 * The following is kludged-up support for simple rpc broadcasts.
207 * Someday a large, complicated system will replace these trivial
208 * routines which only support udp/ip .
209 */
210
211static int
212getbroadcastnets(addrs, sock, buf)
213 struct in_addr *addrs;
214 int sock; /* any valid socket will do */
215 char *buf; /* why allocxate more when we can use existing... */
216{
217 struct ifconf ifc;
218 struct ifreq ifreq, *ifr;
219 struct sockaddr_in *sin;
220 char *cp, *cplim;
3b7c7bd7 221 int i = 0;
03fb6eb0
A
222
223 ifc.ifc_len = UDPMSGSIZE;
224 ifc.ifc_buf = buf;
225 if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
226 perror("broadcast: ioctl (get interface configuration)");
227 return (0);
228 }
229#define max(a, b) (a > b ? a : b)
230#define size(p) max((p).sa_len, sizeof(p))
231 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
232 for (cp = buf; cp < cplim;
233 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
234 ifr = (struct ifreq *)cp;
235 if (ifr->ifr_addr.sa_family != AF_INET)
236 continue;
237 ifreq = *ifr;
238 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
239 perror("broadcast: ioctl (get interface flags)");
240 continue;
241 }
242 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
243 (ifreq.ifr_flags & IFF_UP)) {
244 sin = (struct sockaddr_in *)&ifr->ifr_addr;
245#ifdef SIOCGIFBRDADDR /* 4.3BSD */
246 if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
247 addrs[i++] =
248 inet_makeaddr(inet_netof(sin->sin_addr),
249 INADDR_ANY);
250 } else {
251 addrs[i++] = ((struct sockaddr_in*)
252 &ifreq.ifr_addr)->sin_addr;
253 }
254#else /* 4.2 BSD */
255 addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr),
256 INADDR_ANY);
257#endif
258 }
259 }
260 return (i);
261}
262
263typedef bool_t (*resultproc_t)();
264
f475de6c 265LIBINFO_EXPORT
03fb6eb0
A
266enum clnt_stat
267clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
b3dd680f
A
268#ifdef __LP64__
269 uint32_t prog; /* program number */
270 uint32_t vers; /* version number */
271 uint32_t proc; /* procedure number */
272 xdrproc_t xargs; /* xdr routine for args */
273 caddr_t argsp; /* pointer to args */
274 xdrproc_t xresults; /* xdr routine for results */
275 caddr_t resultsp; /* pointer to results */
276 resultproc_t eachresult; /* call with each result obtained */
277#else
03fb6eb0
A
278 u_long prog; /* program number */
279 u_long vers; /* version number */
280 u_long proc; /* procedure number */
281 xdrproc_t xargs; /* xdr routine for args */
282 caddr_t argsp; /* pointer to args */
283 xdrproc_t xresults; /* xdr routine for results */
284 caddr_t resultsp; /* pointer to results */
285 resultproc_t eachresult; /* call with each result obtained */
b3dd680f 286#endif
03fb6eb0
A
287{
288 enum clnt_stat stat;
289 AUTH *unix_auth = authunix_create_default();
290 XDR xdr_stream;
291 register XDR *xdrs = &xdr_stream;
b3dd680f
A
292 int outlen, inlen, nets;
293 unsigned int fromlen;
03fb6eb0
A
294 register int sock;
295 int on = 1;
296 fd_set mask;
297 fd_set readfds;
298 register int i;
299 bool_t done = FALSE;
b3dd680f
A
300 uint32_t xid;
301#ifdef __LP64__
302 unsigned int port;
303#else
304 unsigned long port;
305#endif
03fb6eb0
A
306 struct in_addr addrs[20];
307 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
308 struct rmtcallargs a;
309 struct rmtcallres r;
310 struct rpc_msg msg;
311 struct timeval t;
312 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
3b7c7bd7 313 int rfd;
03fb6eb0 314
b3dd680f
A
315 stat = RPC_SUCCESS;
316
03fb6eb0
A
317 /*
318 * initialization: create a socket, a broadcast address, and
319 * preserialize the arguments into a send buffer.
320 */
321 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
322 perror("Cannot create socket for broadcast rpc");
323 stat = RPC_CANTSEND;
324 goto done_broad;
325 }
326#ifdef SO_BROADCAST
327 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
328 perror("Cannot set socket option SO_BROADCAST");
329 stat = RPC_CANTSEND;
330 goto done_broad;
331 }
332#endif /* def SO_BROADCAST */
333 FD_ZERO(&mask);
334 FD_SET(sock, &mask);
335 nets = getbroadcastnets(addrs, sock, inbuf);
336 bzero((char *)&baddr, sizeof (baddr));
337 baddr.sin_family = AF_INET;
338 baddr.sin_port = htons(PMAPPORT);
339 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
340/* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
3b7c7bd7
A
341
342 rfd = open("/dev/random", O_RDONLY, 0);
ccd4a120 343 if ((rfd < 0) || (read(rfd, &xid, sizeof(xid)) != sizeof(xid)))
3b7c7bd7
A
344 {
345 gettimeofday(&t, (struct timezone *)0);
ccd4a120 346 xid = getpid() ^ t.tv_sec ^ t.tv_usec;
3b7c7bd7
A
347 }
348 if (rfd > 0) close(rfd);
349
03fb6eb0 350 t.tv_usec = 0;
ccd4a120 351 msg.rm_xid = xid;
03fb6eb0
A
352 msg.rm_direction = CALL;
353 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
354 msg.rm_call.cb_prog = PMAPPROG;
355 msg.rm_call.cb_vers = PMAPVERS;
356 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
357 msg.rm_call.cb_cred = unix_auth->ah_cred;
358 msg.rm_call.cb_verf = unix_auth->ah_verf;
359 a.prog = prog;
360 a.vers = vers;
361 a.proc = proc;
362 a.xdr_args = xargs;
363 a.args_ptr = argsp;
364 r.port_ptr = &port;
365 r.xdr_results = xresults;
366 r.results_ptr = resultsp;
367 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
368 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
369 stat = RPC_CANTENCODEARGS;
370 goto done_broad;
371 }
372 outlen = (int)xdr_getpos(xdrs);
373 xdr_destroy(xdrs);
374 /*
375 * Basic loop: broadcast a packet and wait a while for response(s).
376 * The response timeout grows larger per iteration.
377 */
378 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
379 for (i = 0; i < nets; i++) {
380 baddr.sin_addr = addrs[i];
381 if (sendto(sock, outbuf, outlen, 0,
382 (struct sockaddr *)&baddr,
383 sizeof (struct sockaddr)) != outlen) {
384 perror("Cannot send broadcast packet");
385 stat = RPC_CANTSEND;
386 goto done_broad;
387 }
388 }
389 if (eachresult == NULL) {
390 stat = RPC_SUCCESS;
391 goto done_broad;
392 }
393 recv_again:
394 msg.acpted_rply.ar_verf = _null_auth;
395 msg.acpted_rply.ar_results.where = (caddr_t)&r;
b3dd680f 396 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
03fb6eb0 397 readfds = mask;
3b7c7bd7 398 switch (select(sock+1, &readfds, NULL, NULL, &t)) {
03fb6eb0
A
399
400 case 0: /* timed out */
401 stat = RPC_TIMEDOUT;
402 continue;
403
404 case -1: /* some kind of error */
405 if (errno == EINTR)
406 goto recv_again;
407 perror("Broadcast select problem");
408 stat = RPC_CANTRECV;
409 goto done_broad;
410
411 } /* end of select results switch */
412 try_again:
413 fromlen = sizeof(struct sockaddr);
b3dd680f 414 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0, (struct sockaddr *)&raddr, &fromlen);
03fb6eb0
A
415 if (inlen < 0) {
416 if (errno == EINTR)
417 goto try_again;
418 perror("Cannot receive reply to broadcast");
419 stat = RPC_CANTRECV;
420 goto done_broad;
421 }
1c0d47b0
A
422#ifdef __LP64__
423 if (inlen < sizeof(uint32_t))
424 goto recv_again;
425#else
03fb6eb0
A
426 if (inlen < sizeof(u_long))
427 goto recv_again;
1c0d47b0 428#endif
03fb6eb0
A
429 /*
430 * see if reply transaction id matches sent id.
431 * If so, decode the results.
432 */
433 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
434 if (xdr_replymsg(xdrs, &msg)) {
435 if ((msg.rm_xid == xid) &&
436 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
437 (msg.acpted_rply.ar_stat == SUCCESS)) {
438 raddr.sin_port = htons((u_short)port);
439 done = (*eachresult)(resultsp, &raddr);
440 }
441 /* otherwise, we just ignore the errors ... */
442 } else {
443#ifdef notdef
444 /* some kind of deserialization problem ... */
445 if (msg.rm_xid == xid)
446 fprintf(stderr, "Broadcast deserialization problem");
447 /* otherwise, just random garbage */
448#endif
449 }
450 xdrs->x_op = XDR_FREE;
b3dd680f 451 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
03fb6eb0 452 (void)xdr_replymsg(xdrs, &msg);
0eb52ff2 453 (void)(*xresults)(xdrs, resultsp, 0);
03fb6eb0
A
454 xdr_destroy(xdrs);
455 if (done) {
456 stat = RPC_SUCCESS;
457 goto done_broad;
458 } else {
459 goto recv_again;
460 }
461 }
462done_broad:
463 (void)close(sock);
464 AUTH_DESTROY(unix_auth);
465 return (stat);
466}
467