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
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.
48 #include <rpc/pmap_prot.h>
49 #include <rpc/pmap_clnt.h>
50 #include <rpc/pmap_rmt.h>
52 #include <sys/socket.h>
55 #undef _POSIX_SOURCE /* Ultrix <sys/param.h> needs --roland@gnu */
56 #include <sys/param.h> /* Ultrix needs before net/if --roland@gnu */
58 #include <sys/ioctl.h>
59 #include <arpa/inet.h>
60 #define MAX_BROADCAST_SIZE 1400
64 extern u_long
_create_xid (void) attribute_hidden
;
66 static const struct timeval timeout
= {3, 0};
69 * pmapper remote-call-service interface.
70 * This routine is used to call the pmapper remote call service
71 * which will look up a service program in the port maps, and then
72 * remotely call that routine with the given parameters. This allows
73 * programs to do a lookup and call in one step.
76 pmap_rmtcall (struct sockaddr_in
*addr
, u_long prog
, u_long vers
, u_long proc
,
77 xdrproc_t xdrargs
, caddr_t argsp
, xdrproc_t xdrres
, caddr_t resp
,
78 struct timeval tout
, u_long
*port_ptr
)
86 addr
->sin_port
= htons (PMAPPORT
);
87 client
= clntudp_create (addr
, PMAPPROG
, PMAPVERS
, timeout
, &_socket
);
88 if (client
!= (CLIENT
*) NULL
)
95 r
.port_ptr
= port_ptr
;
97 r
.xdr_results
= xdrres
;
98 stat
= CLNT_CALL (client
, PMAPPROC_CALLIT
, (xdrproc_t
)xdr_rmtcall_args
,
99 (caddr_t
)&a
, (xdrproc_t
)xdr_rmtcallres
,
101 CLNT_DESTROY (client
);
107 /* (void)close(_socket); CLNT_DESTROY already closed it */
114 * XDR remote call arguments
115 * written for XDR_ENCODE direction only
118 xdr_rmtcall_args (XDR
*xdrs
, struct rmtcallargs
*cap
)
120 u_int lenposition
, argposition
, position
;
122 if (xdr_u_long (xdrs
, &(cap
->prog
)) &&
123 xdr_u_long (xdrs
, &(cap
->vers
)) &&
124 xdr_u_long (xdrs
, &(cap
->proc
)))
126 u_long dummy_arglen
= 0;
127 lenposition
= XDR_GETPOS (xdrs
);
128 if (!xdr_u_long (xdrs
, &dummy_arglen
))
130 argposition
= XDR_GETPOS (xdrs
);
131 if (!(*(cap
->xdr_args
)) (xdrs
, cap
->args_ptr
))
133 position
= XDR_GETPOS (xdrs
);
134 cap
->arglen
= (u_long
) position
- (u_long
) argposition
;
135 XDR_SETPOS (xdrs
, lenposition
);
136 if (!xdr_u_long (xdrs
, &(cap
->arglen
)))
138 XDR_SETPOS (xdrs
, position
);
143 libc_hidden_def(xdr_rmtcall_args
)
146 * XDR remote call results
147 * written for XDR_DECODE direction only
150 xdr_rmtcallres (XDR
*xdrs
, struct rmtcallres
*crp
)
154 port_ptr
= (caddr_t
) crp
->port_ptr
;
155 if (xdr_reference (xdrs
, &port_ptr
, sizeof (u_long
), (xdrproc_t
) xdr_u_long
)
156 && xdr_u_long (xdrs
, &crp
->resultslen
))
158 crp
->port_ptr
= (u_long
*) port_ptr
;
159 return (*(crp
->xdr_results
)) (xdrs
, crp
->results_ptr
);
163 libc_hidden_def(xdr_rmtcallres
)
167 * The following is kludged-up support for simple rpc broadcasts.
168 * Someday a large, complicated system will replace these trivial
169 * routines which only support udp/ip .
174 getbroadcastnets (struct in_addr
*addrs
, int sock
, char *buf
)
175 /* int sock: any valid socket will do */
176 /* char *buf: why allocate more when we can use existing... */
179 struct ifreq ifreq
, *ifr
;
180 struct sockaddr_in
*sin
;
183 ifc
.ifc_len
= UDPMSGSIZE
;
185 if (ioctl (sock
, SIOCGIFCONF
, (char *) &ifc
) < 0)
187 perror (_("broadcast: ioctl (get interface configuration)"));
191 for (i
= 0, n
= ifc
.ifc_len
/ sizeof (struct ifreq
); n
> 0; n
--, ifr
++)
194 if (ioctl (sock
, SIOCGIFFLAGS
, (char *) &ifreq
) < 0)
196 perror (_("broadcast: ioctl (get interface flags)"));
199 if ((ifreq
.ifr_flags
& IFF_BROADCAST
) &&
200 (ifreq
.ifr_flags
& IFF_UP
) &&
201 ifr
->ifr_addr
.sa_family
== AF_INET
)
203 sin
= (struct sockaddr_in
*) &ifr
->ifr_addr
;
204 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
205 if (ioctl (sock
, SIOCGIFBRDADDR
, (char *) &ifreq
) < 0)
207 addrs
[i
++] = inet_makeaddr (inet_netof
208 /* Changed to pass struct instead of s_addr member
210 (sin
->sin_addr
), INADDR_ANY
);
214 addrs
[i
++] = ((struct sockaddr_in
*)
215 &ifreq
.ifr_addr
)->sin_addr
;
218 addrs
[i
++] = inet_makeaddr (inet_netof
219 (sin
->sin_addr
.s_addr
), INADDR_ANY
);
229 u_long prog
, /* program number */
230 u_long vers
, /* version number */
231 u_long proc
, /* procedure number */
232 xdrproc_t xargs
, /* xdr routine for args */
233 caddr_t argsp
, /* pointer to args */
234 xdrproc_t xresults
, /* xdr routine for results */
235 caddr_t resultsp
, /* pointer to results */
236 resultproc_t eachresult
/* call with each result obtained */)
238 enum clnt_stat stat
= RPC_FAILED
;
239 AUTH
*unix_auth
= authunix_create_default ();
241 XDR
*xdrs
= &xdr_stream
;
243 int outlen
, inlen
, nets
;
253 struct in_addr addrs
[20];
254 struct sockaddr_in baddr
, raddr
; /* broadcast and response addresses */
255 struct rmtcallargs a
;
258 char outbuf
[MAX_BROADCAST_SIZE
], inbuf
[UDPMSGSIZE
];
261 * initialization: create a socket, a broadcast address, and
262 * preserialize the arguments into a send buffer.
264 if ((sock
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0)
266 perror (_("Cannot create socket for broadcast rpc"));
271 if (setsockopt (sock
, SOL_SOCKET
, SO_BROADCAST
, &on
, sizeof (on
)) < 0)
273 perror (_("Cannot set socket option SO_BROADCAST"));
277 #endif /* def SO_BROADCAST */
280 nets
= getbroadcastnets (addrs
, sock
, inbuf
);
281 memset ((char *) &baddr
, 0, sizeof (baddr
));
282 baddr
.sin_family
= AF_INET
;
283 baddr
.sin_port
= htons (PMAPPORT
);
284 baddr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
285 /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */
286 msg
.rm_xid
= xid
= _create_xid ();
288 msg
.rm_direction
= CALL
;
289 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
290 msg
.rm_call
.cb_prog
= PMAPPROG
;
291 msg
.rm_call
.cb_vers
= PMAPVERS
;
292 msg
.rm_call
.cb_proc
= PMAPPROC_CALLIT
;
293 msg
.rm_call
.cb_cred
= unix_auth
->ah_cred
;
294 msg
.rm_call
.cb_verf
= unix_auth
->ah_verf
;
301 r
.xdr_results
= xresults
;
302 r
.results_ptr
= resultsp
;
303 xdrmem_create (xdrs
, outbuf
, MAX_BROADCAST_SIZE
, XDR_ENCODE
);
304 if ((!xdr_callmsg (xdrs
, &msg
)) || (!xdr_rmtcall_args (xdrs
, &a
)))
306 stat
= RPC_CANTENCODEARGS
;
309 outlen
= (int) xdr_getpos (xdrs
);
312 * Basic loop: broadcast a packet and wait a while for response(s).
313 * The response timeout grows larger per iteration.
315 for (t
.tv_sec
= 4; t
.tv_sec
<= 14; t
.tv_sec
+= 2)
317 for (i
= 0; i
< nets
; i
++)
319 baddr
.sin_addr
= addrs
[i
];
320 if (sendto (sock
, outbuf
, outlen
, 0,
321 (struct sockaddr
*) &baddr
,
322 sizeof (struct sockaddr
)) != outlen
)
324 perror (_("Cannot send broadcast packet"));
329 if (eachresult
== NULL
)
335 msg
.acpted_rply
.ar_verf
= _null_auth
;
336 msg
.acpted_rply
.ar_results
.where
= (caddr_t
) & r
;
337 msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
) xdr_rmtcallres
;
338 milliseconds
= t
.tv_sec
* 1000 + t
.tv_usec
/ 1000;
339 switch (poll(&fd
, 1, milliseconds
))
342 case 0: /* timed out */
346 case -1: /* some kind of error */
349 perror (_("Broadcast poll problem"));
353 } /* end of poll results switch */
355 fromlen
= sizeof (struct sockaddr
);
356 inlen
= recvfrom (sock
, inbuf
, UDPMSGSIZE
, 0,
357 (struct sockaddr
*) &raddr
, &fromlen
);
362 perror (_("Cannot receive reply to broadcast"));
366 if ((size_t) inlen
< sizeof (u_long
))
369 * see if reply transaction id matches sent id.
370 * If so, decode the results.
372 xdrmem_create (xdrs
, inbuf
, (u_int
) inlen
, XDR_DECODE
);
373 if (xdr_replymsg (xdrs
, &msg
))
375 if (((u_int32_t
) msg
.rm_xid
== (u_int32_t
) xid
) &&
376 (msg
.rm_reply
.rp_stat
== MSG_ACCEPTED
) &&
377 (msg
.acpted_rply
.ar_stat
== SUCCESS
))
379 raddr
.sin_port
= htons ((u_short
) port
);
380 done
= (*eachresult
) (resultsp
, &raddr
);
382 /* otherwise, we just ignore the errors ... */
387 /* some kind of deserialization problem ... */
388 if ((u_int32_t
) msg
.rm_xid
== (u_int32_t
) xid
)
389 fprintf (stderr
, "Broadcast deserialization problem");
390 /* otherwise, just random garbage */
393 xdrs
->x_op
= XDR_FREE
;
394 msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
)xdr_void
;
395 (void) xdr_replymsg (xdrs
, &msg
);
396 (void) (*xresults
) (xdrs
, resultsp
);
410 AUTH_DESTROY (unix_auth
);