pmap_rmt.c revision 1.25 1 /* $NetBSD: pmap_rmt.c,v 1.25 2000/02/18 08:26:01 itojun Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: pmap_rmt.c,v 1.25 2000/02/18 08:26:01 itojun Exp $");
39 #endif
40 #endif
41
42 /*
43 * pmap_rmt.c
44 * Client interface to pmap rpc service.
45 * remote call and broadcast service
46 *
47 * Copyright (C) 1984, Sun Microsystems, Inc.
48 */
49
50 #include "namespace.h"
51
52 #include <sys/types.h>
53 #include <sys/ioctl.h>
54 #include <sys/poll.h>
55 #include <sys/socket.h>
56
57 #include <net/if.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
60
61 #include <assert.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <unistd.h>
67
68 #include <rpc/rpc.h>
69 #include <rpc/pmap_prot.h>
70 #include <rpc/pmap_clnt.h>
71 #include <rpc/pmap_rmt.h>
72
73 #ifdef __weak_alias
74 __weak_alias(clnt_broadcast,_clnt_broadcast)
75 __weak_alias(pmap_rmtcall,_pmap_rmtcall)
76 __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args)
77 __weak_alias(xdr_rmtcallres,_xdr_rmtcallres)
78 #endif
79
80 #define MAX_BROADCAST_SIZE 1400
81
82 static int getbroadcastnets __P((struct in_addr *, int, char *));
83
84 static const struct timeval timeout = { 3, 0 };
85
86 /*
87 * pmapper remote-call-service interface.
88 * This routine is used to call the pmapper remote call service
89 * which will look up a service program in the port maps, and then
90 * remotely call that routine with the given parameters. This allows
91 * programs to do a lookup and call in one step.
92 */
93 enum clnt_stat
94 pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
95 port_ptr)
96 struct sockaddr_in *addr;
97 u_long prog, vers, proc;
98 xdrproc_t xdrargs, xdrres;
99 caddr_t argsp, resp;
100 struct timeval tout;
101 u_long *port_ptr;
102 {
103 int sock = -1;
104 CLIENT *client;
105 struct rmtcallargs a;
106 struct rmtcallres r;
107 enum clnt_stat stat;
108
109 _DIAGASSERT(addr != NULL);
110 _DIAGASSERT(port_ptr != NULL);
111
112 addr->sin_port = htons(PMAPPORT);
113 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
114 if (client != (CLIENT *)NULL) {
115 a.prog = prog;
116 a.vers = vers;
117 a.proc = proc;
118 a.args_ptr = argsp;
119 a.xdr_args = xdrargs;
120 r.port_ptr = port_ptr;
121 r.results_ptr = resp;
122 r.xdr_results = xdrres;
123 stat = CLNT_CALL(client, PMAPPROC_CALLIT,
124 (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
125 &r, tout);
126 CLNT_DESTROY(client);
127 } else {
128 stat = RPC_FAILED;
129 }
130 addr->sin_port = 0;
131 return (stat);
132 }
133
134
135 /*
136 * XDR remote call arguments
137 * written for XDR_ENCODE direction only
138 */
139 bool_t
140 xdr_rmtcall_args(xdrs, cap)
141 XDR *xdrs;
142 struct rmtcallargs *cap;
143 {
144 u_int lenposition, argposition, position;
145
146 _DIAGASSERT(xdrs != NULL);
147 _DIAGASSERT(cap != NULL);
148
149 if (xdr_u_long(xdrs, &(cap->prog)) &&
150 xdr_u_long(xdrs, &(cap->vers)) &&
151 xdr_u_long(xdrs, &(cap->proc))) {
152 lenposition = XDR_GETPOS(xdrs);
153 if (! xdr_u_long(xdrs, &(cap->arglen)))
154 return (FALSE);
155 argposition = XDR_GETPOS(xdrs);
156 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
157 return (FALSE);
158 position = XDR_GETPOS(xdrs);
159 cap->arglen = (u_long)position - (u_long)argposition;
160 XDR_SETPOS(xdrs, lenposition);
161 if (! xdr_u_long(xdrs, &(cap->arglen)))
162 return (FALSE);
163 XDR_SETPOS(xdrs, position);
164 return (TRUE);
165 }
166 return (FALSE);
167 }
168
169 /*
170 * XDR remote call results
171 * written for XDR_DECODE direction only
172 */
173 bool_t
174 xdr_rmtcallres(xdrs, crp)
175 XDR *xdrs;
176 struct rmtcallres *crp;
177 {
178 caddr_t port_ptr;
179
180 _DIAGASSERT(xdrs != NULL);
181 _DIAGASSERT(crp != NULL);
182
183 port_ptr = (caddr_t)(void *)crp->port_ptr;
184 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
185 (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
186 crp->port_ptr = (u_long *)(void *)port_ptr;
187 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
188 }
189 return (FALSE);
190 }
191
192
193 /*
194 * The following is kludged-up support for simple rpc broadcasts.
195 * Someday a large, complicated system will replace these trivial
196 * routines which only support udp/ip .
197 */
198
199 static int
200 getbroadcastnets(addrs, sock, buf)
201 struct in_addr *addrs;
202 int sock; /* any valid socket will do */
203 char *buf; /* why allocxate more when we can use existing... */
204 {
205 struct ifconf ifc;
206 struct ifreq ifreq, *ifr;
207 struct sockaddr_in *sin;
208 char *cp, *cplim;
209 int i = 0;
210 size_t siz;
211 char ifrbuf[8192];
212
213 _DIAGASSERT(addrs != NULL);
214 _DIAGASSERT(buf != NULL);
215
216 ifc.ifc_len = UDPMSGSIZE;
217 ifc.ifc_buf = buf;
218 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
219 warn("getbroadcastnets: ioctl (get interface configuration)");
220 return (0);
221 }
222 #define max(a, b) (a > b ? a : b)
223 #define size(p) max((p).sa_len, sizeof(p))
224 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
225 for (cp = buf; cp < cplim; cp += siz) {
226 ifr = (struct ifreq *)(void *)cp;
227 memcpy(ifrbuf, ifr, sizeof(*ifr));
228 siz = ((struct ifreq *)ifrbuf)->ifr_addr.sa_len;
229 if (siz < sizeof(ifr->ifr_addr))
230 siz = sizeof(ifr->ifr_addr);
231 siz += sizeof (ifr->ifr_name);
232 if (siz > sizeof(ifrbuf)) {
233 /* ifr too big */
234 break;
235 }
236 memcpy(ifrbuf, ifr, siz);
237 ifr = (struct ifreq *)ifrbuf;
238
239 if (ifr->ifr_addr.sa_family != AF_INET)
240 continue;
241 ifreq = *ifr;
242 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
243 warn("getbroadcastnets: ioctl (get interface flags)");
244 continue;
245 }
246 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
247 (ifreq.ifr_flags & IFF_UP)) {
248 sin = (struct sockaddr_in *)(void *)&ifr->ifr_addr;
249 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
250 ifreq = *ifr;
251 if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
252 addrs[i++] =
253 inet_makeaddr(inet_netof(sin->sin_addr),
254 (unsigned long)INADDR_ANY);
255 } else {
256 addrs[i++] = ((struct sockaddr_in *)(void *)
257 &ifreq.ifr_addr)->sin_addr;
258 }
259 #else /* 4.2 BSD */
260 addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr),
261 INADDR_ANY);
262 #endif
263 }
264 }
265 return (i);
266 }
267
268 typedef bool_t (*resultproc_t) __P((caddr_t, struct sockaddr_in *));
269
270 enum clnt_stat
271 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
272 u_long prog; /* program number */
273 u_long vers; /* version number */
274 u_long proc; /* procedure number */
275 xdrproc_t xargs; /* xdr routine for args */
276 caddr_t argsp; /* pointer to args */
277 xdrproc_t xresults; /* xdr routine for results */
278 caddr_t resultsp; /* pointer to results */
279 resultproc_t eachresult; /* call with each result obtained */
280 {
281 enum clnt_stat stat;
282 AUTH *unix_auth = authunix_create_default();
283 XDR xdr_stream;
284 XDR *xdrs = &xdr_stream;
285 int inlen, nets;
286 socklen_t fromlen;
287 size_t outlen;
288 int sock;
289 int on = 1;
290 struct pollfd fd;
291 int i;
292 bool_t done = FALSE;
293 u_int32_t xid;
294 u_long port;
295 struct in_addr addrs[20];
296 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
297 struct rmtcallargs a;
298 struct rmtcallres r;
299 struct rpc_msg msg;
300 struct timeval t;
301 int milliseconds;
302 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
303
304 /*
305 * initialization: create a socket, a broadcast address, and
306 * preserialize the arguments into a send buffer.
307 */
308 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
309 warn("Cannot create socket for broadcast rpc");
310 stat = RPC_CANTSEND;
311 goto done_broad;
312 }
313 #ifdef SO_BROADCAST
314 if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
315 warn("Cannot set socket option SO_BROADCAST");
316 stat = RPC_CANTSEND;
317 goto done_broad;
318 }
319 #endif /* def SO_BROADCAST */
320 fd.fd = sock;
321 fd.events = POLLIN;
322 nets = getbroadcastnets(addrs, sock, inbuf);
323 memset(&baddr, 0, sizeof (baddr));
324 baddr.sin_len = sizeof(struct sockaddr_in);
325 baddr.sin_family = AF_INET;
326 baddr.sin_port = htons(PMAPPORT);
327 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
328 (void)gettimeofday(&t, (struct timezone *)0);
329 msg.rm_xid = xid = (u_int32_t)(getpid() ^ t.tv_sec ^ t.tv_usec);
330 t.tv_usec = 0;
331 msg.rm_direction = CALL;
332 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
333 msg.rm_call.cb_prog = PMAPPROG;
334 msg.rm_call.cb_vers = PMAPVERS;
335 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
336 msg.rm_call.cb_cred = unix_auth->ah_cred;
337 msg.rm_call.cb_verf = unix_auth->ah_verf;
338 a.prog = prog;
339 a.vers = vers;
340 a.proc = proc;
341 a.xdr_args = xargs;
342 a.args_ptr = argsp;
343 r.port_ptr = &port;
344 r.xdr_results = xresults;
345 r.results_ptr = resultsp;
346 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
347 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
348 stat = RPC_CANTENCODEARGS;
349 goto done_broad;
350 }
351 outlen = xdr_getpos(xdrs);
352 xdr_destroy(xdrs);
353 /*
354 * Basic loop: broadcast a packet and wait a while for response(s).
355 * The response timeout grows larger per iteration.
356 */
357 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
358 for (i = 0; i < nets; i++) {
359 baddr.sin_addr = addrs[i];
360 if (sendto(sock, outbuf, outlen, 0,
361 (struct sockaddr *)(void *)&baddr,
362 sizeof (struct sockaddr)) != outlen) {
363 warn("Cannot send broadcast packet");
364 stat = RPC_CANTSEND;
365 goto done_broad;
366 }
367 }
368 if (eachresult == NULL) {
369 stat = RPC_SUCCESS;
370 goto done_broad;
371 }
372 recv_again:
373 milliseconds = (int)(t.tv_sec * 1000 + t.tv_usec / 1000);
374 msg.acpted_rply.ar_verf = _null_auth;
375 msg.acpted_rply.ar_results.where = (caddr_t)(void *)&r;
376 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
377 switch (poll(&fd, 1, milliseconds)) {
378
379 case 0: /* timed out */
380 stat = RPC_TIMEDOUT;
381 continue;
382
383 case -1: /* some kind of error */
384 if (errno == EINTR)
385 goto recv_again;
386 warn("Broadcast poll problem");
387 stat = RPC_CANTRECV;
388 goto done_broad;
389
390 } /* end of poll results switch */
391 try_again:
392 fromlen = sizeof(struct sockaddr);
393 inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
394 (struct sockaddr *)(void *)&raddr, &fromlen);
395 if (inlen < 0) {
396 if (errno == EINTR)
397 goto try_again;
398 warn("Cannot receive reply to broadcast");
399 stat = RPC_CANTRECV;
400 goto done_broad;
401 }
402 if (inlen < sizeof(u_int32_t))
403 goto recv_again;
404 /*
405 * see if reply transaction id matches sent id.
406 * If so, decode the results.
407 */
408 xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
409 if (xdr_replymsg(xdrs, &msg)) {
410 if ((msg.rm_xid == xid) &&
411 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
412 (msg.acpted_rply.ar_stat == SUCCESS)) {
413 raddr.sin_port = htons((u_short)port);
414 done = (*eachresult)(resultsp, &raddr);
415 }
416 /* otherwise, we just ignore the errors ... */
417 } else {
418 #ifdef notdef
419 /* some kind of deserialization problem ... */
420 if (msg.rm_xid == xid)
421 fprintf(stderr,
422 "Broadcast deserialization problem");
423 /* otherwise, just random garbage */
424 #endif
425 }
426 xdrs->x_op = XDR_FREE;
427 msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
428 (void)xdr_replymsg(xdrs, &msg);
429 (void)(*xresults)(xdrs, resultsp);
430 xdr_destroy(xdrs);
431 if (done) {
432 stat = RPC_SUCCESS;
433 goto done_broad;
434 } else {
435 goto recv_again;
436 }
437 }
438 stat = RPC_TIMEDOUT;
439 done_broad:
440 if (sock != -1)
441 (void)close(sock);
442 AUTH_DESTROY(unix_auth);
443 return (stat);
444 }
445