2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro
30 * @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/pmap_rmt.c,v 1.16.2.1 2002/06/30 23:34:58 iedowse Exp $
32 * $DragonFly: src/lib/libc/rpc/pmap_rmt.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
37 * Client interface to pmap rpc service.
38 * remote call and broadcast service
40 * Copyright (C) 1984, Sun Microsystems, Inc.
43 #include "namespace.h"
45 #include <rpc/pmap_prot.h>
46 #include <rpc/pmap_clnt.h>
47 #include <rpc/pmap_rmt.h>
48 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <arpa/inet.h>
57 #include "un-namespace.h"
59 #define MAX_BROADCAST_SIZE 1400
61 static struct timeval timeout
= { 3, 0 };
64 * pmapper remote-call-service interface.
65 * This routine is used to call the pmapper remote call service
66 * which will look up a service program in the port maps, and then
67 * remotely call that routine with the given parameters. This allows
68 * programs to do a lookup and call in one step.
71 pmap_rmtcall(struct sockaddr_in
*addr
, u_long prog
, u_long vers
, u_long proc
,
72 xdrproc_t xdrargs
, caddr_t argsp
, xdrproc_t xdrres
, caddr_t resp
,
73 struct timeval tout
, u_long
*port_ptr
)
81 addr
->sin_port
= htons(PMAPPORT
);
82 client
= clntudp_create(addr
, PMAPPROG
, PMAPVERS
, timeout
, &socket
);
83 if (client
!= (CLIENT
*)NULL
) {
89 r
.port_ptr
= port_ptr
;
91 r
.xdr_results
= xdrres
;
92 stat
= CLNT_CALL(client
, PMAPPROC_CALLIT
, xdr_rmtcall_args
, &a
,
93 xdr_rmtcallres
, &r
, tout
);
106 * XDR remote call arguments
107 * written for XDR_ENCODE direction only
110 xdr_rmtcall_args(XDR
*xdrs
, struct rmtcallargs
*cap
)
112 u_int lenposition
, argposition
, position
;
114 if (xdr_u_long(xdrs
, &(cap
->prog
)) &&
115 xdr_u_long(xdrs
, &(cap
->vers
)) &&
116 xdr_u_long(xdrs
, &(cap
->proc
))) {
117 lenposition
= XDR_GETPOS(xdrs
);
118 if (! xdr_u_long(xdrs
, &(cap
->arglen
)))
120 argposition
= XDR_GETPOS(xdrs
);
121 if (! (*(cap
->xdr_args
))(xdrs
, cap
->args_ptr
))
123 position
= XDR_GETPOS(xdrs
);
124 cap
->arglen
= (u_long
)position
- (u_long
)argposition
;
125 XDR_SETPOS(xdrs
, lenposition
);
126 if (! xdr_u_long(xdrs
, &(cap
->arglen
)))
128 XDR_SETPOS(xdrs
, position
);
135 * XDR remote call results
136 * written for XDR_DECODE direction only
139 xdr_rmtcallres(XDR
*xdrs
, struct rmtcallres
*crp
)
143 port_ptr
= (caddr_t
)crp
->port_ptr
;
144 if (xdr_reference(xdrs
, &port_ptr
, sizeof (u_long
),
145 xdr_u_long
) && xdr_u_long(xdrs
, &crp
->resultslen
)) {
146 crp
->port_ptr
= (u_long
*)port_ptr
;
147 return ((*(crp
->xdr_results
))(xdrs
, crp
->results_ptr
));
154 * The following is kludged-up support for simple rpc broadcasts.
155 * Someday a large, complicated system will replace these trivial
156 * routines which only support udp/ip .
160 getbroadcastnets(struct in_addr
*addrs
,
161 int sock
, /* any valid socket will do */
162 char *buf
) /* why allocxate more when we can use existing... */
165 struct ifreq ifreq
, *ifr
;
166 struct sockaddr_in
*sin
;
171 ifc
.ifc_len
= UDPMSGSIZE
;
173 if (_ioctl(sock
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
174 perror("broadcast: ioctl (get interface configuration)");
177 #define max(a, b) (a > b ? a : b)
178 #define size(p) max((p).sa_len, sizeof(p))
179 cplim
= buf
+ ifc
.ifc_len
; /*skip over if's with big ifr_addr's */
180 for (cp
= buf
; cp
< cplim
;
181 cp
+= sizeof (ifr
->ifr_name
) + size(ifr
->ifr_addr
)) {
182 ifr
= (struct ifreq
*)cp
;
183 if (ifr
->ifr_addr
.sa_family
!= AF_INET
)
185 memcpy(&ifreq
, ifr
, sizeof(ifreq
));
186 if (_ioctl(sock
, SIOCGIFFLAGS
, (char *)&ifreq
) < 0) {
187 perror("broadcast: ioctl (get interface flags)");
190 if ((ifreq
.ifr_flags
& IFF_BROADCAST
) &&
191 (ifreq
.ifr_flags
& IFF_UP
)) {
192 sin
= (struct sockaddr_in
*)&ifr
->ifr_addr
;
193 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
194 if (_ioctl(sock
, SIOCGIFBRDADDR
, (char *)&ifreq
) < 0) {
196 inet_makeaddr(inet_netof(sin
->sin_addr
),
199 addr
= ((struct sockaddr_in
*)
200 &ifreq
.ifr_addr
)->sin_addr
;
203 addr
= inet_makeaddr(inet_netof(sin
->sin_addr
),
206 for (n
=i
-1; n
>=0; n
--) {
207 if (addr
.s_addr
== addrs
[n
].s_addr
)
218 typedef bool_t (*resultproc_t
)();
221 clnt_broadcast(u_long prog
, /* program number */
222 u_long vers
, /* version number */
223 u_long proc
, /* procedure number */
224 xdrproc_t xargs
, /* xdr routine for args */
225 caddr_t argsp
, /* pointer to args */
226 xdrproc_t xresults
, /* xdr routine for results */
227 caddr_t resultsp
, /* pointer to results */
228 resultproc_t eachresult
) /* call with each result obtained */
231 AUTH
*unix_auth
= authunix_create_default();
233 XDR
*xdrs
= &xdr_stream
;
234 int outlen
, inlen
, fromlen
, nets
;
237 fd_set
*fds
, readfds
;
242 struct in_addr addrs
[20];
243 struct sockaddr_in baddr
, raddr
; /* broadcast and response addresses */
244 struct rmtcallargs a
;
247 struct timeval t
, tv
;
248 char outbuf
[MAX_BROADCAST_SIZE
], inbuf
[UDPMSGSIZE
];
249 static u_int32_t disrupt
;
252 disrupt
= (u_int32_t
)(long)resultsp
;
255 * initialization: create a socket, a broadcast address, and
256 * preserialize the arguments into a send buffer.
258 if ((sock
= _socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0) {
259 perror("Cannot create socket for broadcast rpc");
264 if (_setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof (on
)) < 0) {
265 perror("Cannot set socket option SO_BROADCAST");
269 #endif /* def SO_BROADCAST */
270 if (sock
+ 1 > FD_SETSIZE
) {
271 int bytes
= howmany(sock
+ 1, NFDBITS
) * sizeof(fd_mask
);
272 fds
= (fd_set
*)malloc(bytes
);
277 memset(fds
, 0, bytes
);
283 nets
= getbroadcastnets(addrs
, sock
, inbuf
);
284 memset(&baddr
, 0, sizeof (baddr
));
285 baddr
.sin_len
= sizeof(struct sockaddr_in
);
286 baddr
.sin_family
= AF_INET
;
287 baddr
.sin_port
= htons(PMAPPORT
);
288 baddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
289 gettimeofday(&t
, (struct timezone
*)0);
290 msg
.rm_xid
= xid
= (++disrupt
) ^ getpid() ^ t
.tv_sec
^ t
.tv_usec
;
292 msg
.rm_direction
= CALL
;
293 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
294 msg
.rm_call
.cb_prog
= PMAPPROG
;
295 msg
.rm_call
.cb_vers
= PMAPVERS
;
296 msg
.rm_call
.cb_proc
= PMAPPROC_CALLIT
;
297 msg
.rm_call
.cb_cred
= unix_auth
->ah_cred
;
298 msg
.rm_call
.cb_verf
= unix_auth
->ah_verf
;
305 r
.xdr_results
= xresults
;
306 r
.results_ptr
= resultsp
;
307 xdrmem_create(xdrs
, outbuf
, MAX_BROADCAST_SIZE
, XDR_ENCODE
);
308 if ((! xdr_callmsg(xdrs
, &msg
)) || (! xdr_rmtcall_args(xdrs
, &a
))) {
309 stat
= RPC_CANTENCODEARGS
;
312 outlen
= (int)xdr_getpos(xdrs
);
315 * Basic loop: broadcast a packet and wait a while for response(s).
316 * The response timeout grows larger per iteration.
318 * XXX This will loop about 5 times the stop. If there are
319 * lots of signals being received by the process it will quit
320 * send them all in one quick burst, not paying attention to
321 * the intended function of sending them slowly over half a
324 for (t
.tv_sec
= 4; t
.tv_sec
<= 14; t
.tv_sec
+= 2) {
326 for (i
= 0; i
< nets
; i
++) {
327 baddr
.sin_addr
= addrs
[i
];
328 if (_sendto(sock
, outbuf
, outlen
, 0,
329 (struct sockaddr
*)&baddr
,
330 sizeof (struct sockaddr
)) == outlen
) {
335 perror("Cannot send broadcast packet");
339 if (eachresult
== NULL
) {
344 msg
.acpted_rply
.ar_verf
= _null_auth
;
345 msg
.acpted_rply
.ar_results
.where
= (caddr_t
)&r
;
346 msg
.acpted_rply
.ar_results
.proc
= xdr_rmtcallres
;
347 /* XXX we know the other bits are still clear */
349 tv
= t
; /* for _select() that copies back */
350 switch (_select(sock
+ 1, fds
, NULL
, NULL
, &tv
)) {
352 case 0: /* timed out */
356 case -1: /* some kind of error */
359 perror("Broadcast select problem");
363 } /* end of select results switch */
365 fromlen
= sizeof(struct sockaddr
);
366 inlen
= _recvfrom(sock
, inbuf
, UDPMSGSIZE
, 0,
367 (struct sockaddr
*)&raddr
, &fromlen
);
371 perror("Cannot receive reply to broadcast");
375 if (inlen
< sizeof(u_int32_t
))
378 * see if reply transaction id matches sent id.
379 * If so, decode the results.
381 xdrmem_create(xdrs
, inbuf
, (u_int
)inlen
, XDR_DECODE
);
382 if (xdr_replymsg(xdrs
, &msg
)) {
383 if ((msg
.rm_xid
== xid
) &&
384 (msg
.rm_reply
.rp_stat
== MSG_ACCEPTED
) &&
385 (msg
.acpted_rply
.ar_stat
== SUCCESS
)) {
386 raddr
.sin_port
= htons((u_short
)port
);
387 done
= (*eachresult
)(resultsp
, &raddr
);
389 /* otherwise, we just ignore the errors ... */
391 xdrs
->x_op
= XDR_FREE
;
392 msg
.acpted_rply
.ar_results
.proc
= xdr_void
;
393 xdr_replymsg(xdrs
, &msg
);
394 (*xresults
)(xdrs
, resultsp
);
408 AUTH_DESTROY(unix_auth
);