1 /* @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid
[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
36 * Client interface to pmap rpc service.
37 * remote call and broadcast service
39 * Copyright (C) 1984, Sun Microsystems, Inc.
43 #include <rpc/pmap_prot.h>
44 #include <rpc/pmap_clnt.h>
45 #include <rpc/pmap_rmt.h>
46 #include <sys/socket.h>
49 #undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
50 #include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
52 #include <sys/ioctl.h>
53 #include <arpa/inet.h>
54 #define MAX_BROADCAST_SIZE 1400
59 static struct timeval timeout
= { 3, 0 };
63 * pmapper remote-call-service interface.
64 * This routine is used to call the pmapper remote call service
65 * which will look up a service program in the port maps, and then
66 * remotely call that routine with the given parameters. This allows
67 * programs to do a lookup and call in one step.
70 pmap_rmtcall(addr
, prog
, vers
, proc
, xdrargs
, argsp
, xdrres
, resp
, tout
, port_ptr
)
71 struct sockaddr_in
*addr
;
72 u_long prog
, vers
, proc
;
73 xdrproc_t xdrargs
, xdrres
;
79 register CLIENT
*client
;
84 addr
->sin_port
= htons(PMAPPORT
);
85 client
= clntudp_create(addr
, PMAPPROG
, PMAPVERS
, timeout
, &socket
);
86 if (client
!= (CLIENT
*)NULL
) {
92 r
.port_ptr
= port_ptr
;
94 r
.xdr_results
= xdrres
;
95 stat
= CLNT_CALL(client
, PMAPPROC_CALLIT
, xdr_rmtcall_args
, &a
,
96 xdr_rmtcallres
, &r
, tout
);
101 /* (void)close(socket); CLNT_DESTROY already closed it */
108 * XDR remote call arguments
109 * written for XDR_ENCODE direction only
112 xdr_rmtcall_args(xdrs
, cap
)
114 register struct rmtcallargs
*cap
;
116 u_int lenposition
, argposition
, position
;
118 if (xdr_u_long(xdrs
, &(cap
->prog
)) &&
119 xdr_u_long(xdrs
, &(cap
->vers
)) &&
120 xdr_u_long(xdrs
, &(cap
->proc
))) {
121 lenposition
= XDR_GETPOS(xdrs
);
122 if (! xdr_u_long(xdrs
, &(cap
->arglen
)))
124 argposition
= XDR_GETPOS(xdrs
);
125 if (! (*(cap
->xdr_args
))(xdrs
, cap
->args_ptr
))
127 position
= XDR_GETPOS(xdrs
);
128 cap
->arglen
= (u_long
)position
- (u_long
)argposition
;
129 XDR_SETPOS(xdrs
, lenposition
);
130 if (! xdr_u_long(xdrs
, &(cap
->arglen
)))
132 XDR_SETPOS(xdrs
, position
);
139 * XDR remote call results
140 * written for XDR_DECODE direction only
143 xdr_rmtcallres(xdrs
, crp
)
145 register struct rmtcallres
*crp
;
149 port_ptr
= (caddr_t
)crp
->port_ptr
;
150 if (xdr_reference(xdrs
, &port_ptr
, sizeof (u_long
),
151 xdr_u_long
) && xdr_u_long(xdrs
, &crp
->resultslen
)) {
152 crp
->port_ptr
= (u_long
*)port_ptr
;
153 return ((*(crp
->xdr_results
))(xdrs
, crp
->results_ptr
));
160 * The following is kludged-up support for simple rpc broadcasts.
161 * Someday a large, complicated system will replace these trivial
162 * routines which only support udp/ip .
166 getbroadcastnets(addrs
, sock
, buf
)
167 struct in_addr
*addrs
;
168 int sock
; /* any valid socket will do */
169 char *buf
; /* why allocate more when we can use existing... */
172 struct ifreq ifreq
, *ifr
;
173 struct sockaddr_in
*sin
;
176 ifc
.ifc_len
= UDPMSGSIZE
;
178 if (ioctl(sock
, SIOCGIFCONF
, (char *)&ifc
) < 0) {
179 perror(_("broadcast: ioctl (get interface configuration)"));
183 for (i
= 0, n
= ifc
.ifc_len
/sizeof (struct ifreq
); n
> 0; n
--, ifr
++) {
185 if (ioctl(sock
, SIOCGIFFLAGS
, (char *)&ifreq
) < 0) {
186 perror(_("broadcast: ioctl (get interface flags)"));
189 if ((ifreq
.ifr_flags
& IFF_BROADCAST
) &&
190 (ifreq
.ifr_flags
& IFF_UP
) &&
191 ifr
->ifr_addr
.sa_family
== AF_INET
) {
192 sin
= (struct sockaddr_in
*)&ifr
->ifr_addr
;
193 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
194 if (ioctl(sock
, SIOCGIFBRDADDR
, (char *)&ifreq
) < 0) {
195 addrs
[i
++] = inet_makeaddr(inet_netof
196 /* Changed to pass struct instead of s_addr member
198 (sin
->sin_addr
), INADDR_ANY
);
200 addrs
[i
++] = ((struct sockaddr_in
*)
201 &ifreq
.ifr_addr
)->sin_addr
;
204 addrs
[i
++] = inet_makeaddr(inet_netof
205 (sin
->sin_addr
.s_addr
), INADDR_ANY
);
214 clnt_broadcast(prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
, eachresult
)
215 u_long prog
; /* program number */
216 u_long vers
; /* version number */
217 u_long proc
; /* procedure number */
218 xdrproc_t xargs
; /* xdr routine for args */
219 caddr_t argsp
; /* pointer to args */
220 xdrproc_t xresults
; /* xdr routine for results */
221 caddr_t resultsp
; /* pointer to results */
222 resultproc_t eachresult
; /* call with each result obtained */
225 AUTH
*unix_auth
= authunix_create_default();
227 register XDR
*xdrs
= &xdr_stream
;
228 int outlen
, inlen
, fromlen
, nets
;
237 #endif /* def FD_SETSIZE */
242 struct in_addr addrs
[20];
243 struct sockaddr_in baddr
, raddr
; /* broadcast and response addresses */
244 struct rmtcallargs a
;
247 struct timeval t
, t1
;
248 char outbuf
[MAX_BROADCAST_SIZE
], inbuf
[UDPMSGSIZE
];
251 * initialization: create a socket, a broadcast address, and
252 * preserialize the arguments into a send buffer.
254 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0) {
255 perror(_("Cannot create socket for broadcast rpc"));
260 if (setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof (on
)) < 0) {
261 perror(_("Cannot set socket option SO_BROADCAST"));
265 #endif /* def SO_BROADCAST */
271 #endif /* def FD_SETSIZE */
272 nets
= getbroadcastnets(addrs
, sock
, inbuf
);
273 bzero((char *)&baddr
, sizeof (baddr
));
274 baddr
.sin_family
= AF_INET
;
275 baddr
.sin_port
= htons(PMAPPORT
);
276 baddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
277 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
278 (void)gettimeofday(&t
, (struct timezone
*)0);
279 msg
.rm_xid
= xid
= getpid() ^ t
.tv_sec
^ t
.tv_usec
;
281 msg
.rm_direction
= CALL
;
282 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
283 msg
.rm_call
.cb_prog
= PMAPPROG
;
284 msg
.rm_call
.cb_vers
= PMAPVERS
;
285 msg
.rm_call
.cb_proc
= PMAPPROC_CALLIT
;
286 msg
.rm_call
.cb_cred
= unix_auth
->ah_cred
;
287 msg
.rm_call
.cb_verf
= unix_auth
->ah_verf
;
294 r
.xdr_results
= xresults
;
295 r
.results_ptr
= resultsp
;
296 xdrmem_create(xdrs
, outbuf
, MAX_BROADCAST_SIZE
, XDR_ENCODE
);
297 if ((! xdr_callmsg(xdrs
, &msg
)) || (! xdr_rmtcall_args(xdrs
, &a
))) {
298 stat
= RPC_CANTENCODEARGS
;
301 outlen
= (int)xdr_getpos(xdrs
);
304 * Basic loop: broadcast a packet and wait a while for response(s).
305 * The response timeout grows larger per iteration.
307 for (t
.tv_sec
= 4; t
.tv_sec
<= 14; t
.tv_sec
+= 2) {
308 for (i
= 0; i
< nets
; i
++) {
309 baddr
.sin_addr
= addrs
[i
];
310 if (sendto(sock
, outbuf
, outlen
, 0,
311 (struct sockaddr
*)&baddr
,
312 sizeof (struct sockaddr
)) != outlen
) {
313 perror(_("Cannot send broadcast packet"));
318 if (eachresult
== NULL
) {
323 msg
.acpted_rply
.ar_verf
= _null_auth
;
324 msg
.acpted_rply
.ar_results
.where
= (caddr_t
)&r
;
325 msg
.acpted_rply
.ar_results
.proc
= xdr_rmtcallres
;
328 switch (select(_rpc_dtablesize(), &readfds
, (int *)NULL
,
331 case 0: /* timed out */
335 case -1: /* some kind of error */
338 perror(_("Broadcast select problem"));
342 } /* end of select results switch */
344 fromlen
= sizeof(struct sockaddr
);
345 inlen
= recvfrom(sock
, inbuf
, UDPMSGSIZE
, 0,
346 (struct sockaddr
*)&raddr
, &fromlen
);
350 perror(_("Cannot receive reply to broadcast"));
354 if (inlen
< sizeof(u_long
))
357 * see if reply transaction id matches sent id.
358 * If so, decode the results.
360 xdrmem_create(xdrs
, inbuf
, (u_int
)inlen
, XDR_DECODE
);
361 if (xdr_replymsg(xdrs
, &msg
)) {
362 if ((msg
.rm_xid
== xid
) &&
363 (msg
.rm_reply
.rp_stat
== MSG_ACCEPTED
) &&
364 (msg
.acpted_rply
.ar_stat
== SUCCESS
)) {
365 raddr
.sin_port
= htons((u_short
)port
);
366 done
= (*eachresult
)(resultsp
, &raddr
);
368 /* otherwise, we just ignore the errors ... */
371 /* some kind of deserialization problem ... */
372 if (msg
.rm_xid
== xid
)
373 fprintf(stderr
, "Broadcast deserialization problem");
374 /* otherwise, just random garbage */
377 xdrs
->x_op
= XDR_FREE
;
378 msg
.acpted_rply
.ar_results
.proc
= xdr_void
;
379 (void)xdr_replymsg(xdrs
, &msg
);
380 (void)(*xresults
)(xdrs
, resultsp
);
391 AUTH_DESTROY(unix_auth
);