2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
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.
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.
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.
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.
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.
49 * Sun Microsystems, Inc.
51 * Mountain View, California 94043
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 $";
62 * Client interface to pmap rpc service.
63 * remote call and broadcast service
65 * Copyright (C) 1984, Sun Microsystems, Inc.
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>
79 #include <sys/ioctl.h>
80 #include <arpa/inet.h>
81 #define MAX_BROADCAST_SIZE 1400
84 static struct timeval timeout
= { 3, 0 };
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.
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
;
104 register CLIENT
*client
;
105 struct rmtcallargs a
;
109 addr
->sin_port
= htons(PMAPPORT
);
110 client
= clntudp_create(addr
, PMAPPROG
, PMAPVERS
, timeout
, &socket
);
111 if (client
!= (CLIENT
*)NULL
) {
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
);
133 * XDR remote call arguments
134 * written for XDR_ENCODE direction only
137 xdr_rmtcall_args(xdrs
, cap
)
139 register struct rmtcallargs
*cap
;
141 u_int lenposition
, argposition
, position
;
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
)))
149 argposition
= XDR_GETPOS(xdrs
);
150 if (! (*(cap
->xdr_args
))(xdrs
, cap
->args_ptr
))
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
)))
157 XDR_SETPOS(xdrs
, position
);
164 * XDR remote call results
165 * written for XDR_DECODE direction only
168 xdr_rmtcallres(xdrs
, crp
)
170 register struct rmtcallres
*crp
;
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
));
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 .
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... */
197 struct ifreq ifreq
, *ifr
;
198 struct sockaddr_in
*sin
;
202 ifc
.ifc_len
= UDPMSGSIZE
;
204 if (ioctl(sock
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
205 perror("broadcast: ioctl (get interface configuration)");
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
)
217 if (ioctl(sock
, SIOCGIFFLAGS
, (char *)&ifreq
) < 0) {
218 perror("broadcast: ioctl (get interface flags)");
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) {
227 inet_makeaddr(inet_netof(sin
->sin_addr
),
230 addrs
[i
++] = ((struct sockaddr_in
*)
231 &ifreq
.ifr_addr
)->sin_addr
;
234 addrs
[i
++] = inet_makeaddr(inet_netof(sin
->sin_addr
),
242 typedef bool_t (*resultproc_t
)();
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 */
256 AUTH
*unix_auth
= authunix_create_default();
258 register XDR
*xdrs
= &xdr_stream
;
259 int outlen
, inlen
, fromlen
, nets
;
268 struct in_addr addrs
[20];
269 struct sockaddr_in baddr
, raddr
; /* broadcast and response addresses */
270 struct rmtcallargs a
;
274 char outbuf
[MAX_BROADCAST_SIZE
], inbuf
[UDPMSGSIZE
];
278 * initialization: create a socket, a broadcast address, and
279 * preserialize the arguments into a send buffer.
281 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0) {
282 perror("Cannot create socket for broadcast rpc");
287 if (setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof (on
)) < 0) {
288 perror("Cannot set socket option SO_BROADCAST");
292 #endif /* def SO_BROADCAST */
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); */
302 rfd
= open("/dev/random", O_RDONLY
, 0);
303 if ((rfd
< 0) || (read(rfd
, &xid
, sizeof(xid
)) != sizeof(xid
)))
305 gettimeofday(&t
, (struct timezone
*)0);
306 xid
= getpid() ^ t
.tv_sec
^ t
.tv_usec
;
308 if (rfd
> 0) close(rfd
);
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
;
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
;
332 outlen
= (int)xdr_getpos(xdrs
);
335 * Basic loop: broadcast a packet and wait a while for response(s).
336 * The response timeout grows larger per iteration.
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");
349 if (eachresult
== NULL
) {
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
;
358 switch (select(sock
+1, &readfds
, NULL
, NULL
, &t
)) {
360 case 0: /* timed out */
364 case -1: /* some kind of error */
367 perror("Broadcast select problem");
371 } /* end of select results switch */
373 fromlen
= sizeof(struct sockaddr
);
374 inlen
= recvfrom(sock
, inbuf
, UDPMSGSIZE
, 0,
375 (struct sockaddr
*)&raddr
, &fromlen
);
379 perror("Cannot receive reply to broadcast");
383 if (inlen
< sizeof(u_long
))
386 * see if reply transaction id matches sent id.
387 * If so, decode the results.
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
);
397 /* otherwise, we just ignore the errors ... */
400 /* some kind of deserialization problem ... */
401 if (msg
.rm_xid
== xid
)
402 fprintf(stderr
, "Broadcast deserialization problem");
403 /* otherwise, just random garbage */
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
);
420 AUTH_DESTROY(unix_auth
);