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.
45 #include <rpc/pmap_prot.h>
46 #include <rpc/pmap_clnt.h>
47 #include <rpc/pmap_rmt.h>
48 #include <sys/socket.h>
51 #undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
52 #include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
54 #include <sys/ioctl.h>
55 #include <arpa/inet.h>
56 #define MAX_BROADCAST_SIZE 1400
58 static struct timeval timeout
= {3, 0};
61 * pmapper remote-call-service interface.
62 * This routine is used to call the pmapper remote call service
63 * which will look up a service program in the port maps, and then
64 * remotely call that routine with the given parameters. This allows
65 * programs to do a lookup and call in one step.
68 pmap_rmtcall (addr
, prog
, vers
, proc
, xdrargs
, argsp
, xdrres
, resp
, tout
, port_ptr
)
69 struct sockaddr_in
*addr
;
70 u_long prog
, vers
, proc
;
71 xdrproc_t xdrargs
, xdrres
;
82 addr
->sin_port
= htons (PMAPPORT
);
83 client
= clntudp_create (addr
, PMAPPROG
, PMAPVERS
, timeout
, &socket
);
84 if (client
!= (CLIENT
*) NULL
)
91 r
.port_ptr
= port_ptr
;
93 r
.xdr_results
= xdrres
;
94 stat
= CLNT_CALL (client
, PMAPPROC_CALLIT
, (xdrproc_t
)xdr_rmtcall_args
,
95 (caddr_t
)&a
, (xdrproc_t
)xdr_rmtcallres
,
97 CLNT_DESTROY (client
);
103 /* (void)close(socket); CLNT_DESTROY already closed it */
110 * XDR remote call arguments
111 * written for XDR_ENCODE direction only
114 xdr_rmtcall_args (xdrs
, cap
)
116 struct rmtcallargs
*cap
;
118 u_int lenposition
, argposition
, position
;
120 if (xdr_u_long (xdrs
, &(cap
->prog
)) &&
121 xdr_u_long (xdrs
, &(cap
->vers
)) &&
122 xdr_u_long (xdrs
, &(cap
->proc
)))
124 lenposition
= XDR_GETPOS (xdrs
);
125 if (!xdr_u_long (xdrs
, &(cap
->arglen
)))
127 argposition
= XDR_GETPOS (xdrs
);
128 if (!(*(cap
->xdr_args
)) (xdrs
, cap
->args_ptr
))
130 position
= XDR_GETPOS (xdrs
);
131 cap
->arglen
= (u_long
) position
- (u_long
) argposition
;
132 XDR_SETPOS (xdrs
, lenposition
);
133 if (!xdr_u_long (xdrs
, &(cap
->arglen
)))
135 XDR_SETPOS (xdrs
, position
);
142 * XDR remote call results
143 * written for XDR_DECODE direction only
146 xdr_rmtcallres (xdrs
, crp
)
148 struct rmtcallres
*crp
;
152 port_ptr
= (caddr_t
) crp
->port_ptr
;
153 if (xdr_reference (xdrs
, &port_ptr
, sizeof (u_long
), (xdrproc_t
) xdr_u_long
)
154 && xdr_u_long (xdrs
, &crp
->resultslen
))
156 crp
->port_ptr
= (u_long
*) port_ptr
;
157 return (*(crp
->xdr_results
)) (xdrs
, crp
->results_ptr
);
164 * The following is kludged-up support for simple rpc broadcasts.
165 * Someday a large, complicated system will replace these trivial
166 * routines which only support udp/ip .
171 getbroadcastnets (struct in_addr
*addrs
, int sock
, char *buf
)
172 /* int sock: any valid socket will do */
173 /* char *buf: why allocate more when we can use existing... */
176 struct ifreq ifreq
, *ifr
;
177 struct sockaddr_in
*sin
;
180 ifc
.ifc_len
= UDPMSGSIZE
;
182 if (ioctl (sock
, SIOCGIFCONF
, (char *) &ifc
) < 0)
184 perror (_("broadcast: ioctl (get interface configuration)"));
188 for (i
= 0, n
= ifc
.ifc_len
/ sizeof (struct ifreq
); n
> 0; n
--, ifr
++)
191 if (ioctl (sock
, SIOCGIFFLAGS
, (char *) &ifreq
) < 0)
193 perror (_("broadcast: ioctl (get interface flags)"));
196 if ((ifreq
.ifr_flags
& IFF_BROADCAST
) &&
197 (ifreq
.ifr_flags
& IFF_UP
) &&
198 ifr
->ifr_addr
.sa_family
== AF_INET
)
200 sin
= (struct sockaddr_in
*) &ifr
->ifr_addr
;
201 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
202 if (ioctl (sock
, SIOCGIFBRDADDR
, (char *) &ifreq
) < 0)
204 addrs
[i
++] = inet_makeaddr (inet_netof
205 /* Changed to pass struct instead of s_addr member
207 (sin
->sin_addr
), INADDR_ANY
);
211 addrs
[i
++] = ((struct sockaddr_in
*)
212 &ifreq
.ifr_addr
)->sin_addr
;
215 addrs
[i
++] = inet_makeaddr (inet_netof
216 (sin
->sin_addr
.s_addr
), INADDR_ANY
);
225 clnt_broadcast (prog
, vers
, proc
, xargs
, argsp
, xresults
, resultsp
, eachresult
)
226 u_long prog
; /* program number */
227 u_long vers
; /* version number */
228 u_long proc
; /* procedure number */
229 xdrproc_t xargs
; /* xdr routine for args */
230 caddr_t argsp
; /* pointer to args */
231 xdrproc_t xresults
; /* xdr routine for results */
232 caddr_t resultsp
; /* pointer to results */
233 resultproc_t eachresult
; /* call with each result obtained */
236 AUTH
*unix_auth
= authunix_create_default ();
238 XDR
*xdrs
= &xdr_stream
;
239 int outlen
, inlen
, nets
;
249 #endif /* def FD_SETSIZE */
254 struct in_addr addrs
[20];
255 struct sockaddr_in baddr
, raddr
; /* broadcast and response addresses */
256 struct rmtcallargs a
;
259 struct timeval t
, t1
;
260 char outbuf
[MAX_BROADCAST_SIZE
], inbuf
[UDPMSGSIZE
];
263 * initialization: create a socket, a broadcast address, and
264 * preserialize the arguments into a send buffer.
266 if ((sock
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0)
268 perror (_("Cannot create socket for broadcast rpc"));
273 if (setsockopt (sock
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof (on
)) < 0)
275 perror (_("Cannot set socket option SO_BROADCAST"));
279 #endif /* def SO_BROADCAST */
282 FD_SET (sock
, &mask
);
285 #endif /* def FD_SETSIZE */
286 nets
= getbroadcastnets (addrs
, sock
, inbuf
);
287 bzero ((char *) &baddr
, sizeof (baddr
));
288 baddr
.sin_family
= AF_INET
;
289 baddr
.sin_port
= htons (PMAPPORT
);
290 baddr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
291 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
292 (void) gettimeofday (&t
, (struct timezone
*) 0);
293 msg
.rm_xid
= xid
= getpid () ^ t
.tv_sec
^ t
.tv_usec
;
295 msg
.rm_direction
= CALL
;
296 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
297 msg
.rm_call
.cb_prog
= PMAPPROG
;
298 msg
.rm_call
.cb_vers
= PMAPVERS
;
299 msg
.rm_call
.cb_proc
= PMAPPROC_CALLIT
;
300 msg
.rm_call
.cb_cred
= unix_auth
->ah_cred
;
301 msg
.rm_call
.cb_verf
= unix_auth
->ah_verf
;
308 r
.xdr_results
= xresults
;
309 r
.results_ptr
= resultsp
;
310 xdrmem_create (xdrs
, outbuf
, MAX_BROADCAST_SIZE
, XDR_ENCODE
);
311 if ((!xdr_callmsg (xdrs
, &msg
)) || (!xdr_rmtcall_args (xdrs
, &a
)))
313 stat
= RPC_CANTENCODEARGS
;
316 outlen
= (int) xdr_getpos (xdrs
);
319 * Basic loop: broadcast a packet and wait a while for response(s).
320 * The response timeout grows larger per iteration.
322 for (t
.tv_sec
= 4; t
.tv_sec
<= 14; t
.tv_sec
+= 2)
324 for (i
= 0; i
< nets
; i
++)
326 baddr
.sin_addr
= addrs
[i
];
327 if (sendto (sock
, outbuf
, outlen
, 0,
328 (struct sockaddr
*) &baddr
,
329 sizeof (struct sockaddr
)) != outlen
)
331 perror (_("Cannot send broadcast packet"));
336 if (eachresult
== NULL
)
342 msg
.acpted_rply
.ar_verf
= _null_auth
;
343 msg
.acpted_rply
.ar_results
.where
= (caddr_t
) & r
;
344 msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_rmtcallres
;
347 switch (select (_rpc_dtablesize (), &readfds
, (fd_set
*) NULL
,
348 (fd_set
*) NULL
, &t1
))
351 case 0: /* timed out */
355 case -1: /* some kind of error */
358 perror (_("Broadcast select problem"));
362 } /* end of select results switch */
364 fromlen
= sizeof (struct sockaddr
);
365 inlen
= recvfrom (sock
, inbuf
, UDPMSGSIZE
, 0,
366 (struct sockaddr
*) &raddr
, &fromlen
);
371 perror (_("Cannot receive reply to broadcast"));
375 if ((size_t) inlen
< sizeof (u_long
))
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
))
384 if ((msg
.rm_xid
== xid
) &&
385 (msg
.rm_reply
.rp_stat
== MSG_ACCEPTED
) &&
386 (msg
.acpted_rply
.ar_stat
== SUCCESS
))
388 raddr
.sin_port
= htons ((u_short
) port
);
389 done
= (*eachresult
) (resultsp
, &raddr
);
391 /* otherwise, we just ignore the errors ... */
396 /* some kind of deserialization problem ... */
397 if (msg
.rm_xid
== xid
)
398 fprintf (stderr
, "Broadcast deserialization problem");
399 /* otherwise, just random garbage */
402 xdrs
->x_op
= XDR_FREE
;
403 msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
)xdr_void
;
404 (void) xdr_replymsg (xdrs
, &msg
);
405 (void) (*xresults
) (xdrs
, resultsp
);
419 AUTH_DESTROY (unix_auth
);