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