pmap_rmt.c revision 1.24 1 1.24 mycroft /* $NetBSD: pmap_rmt.c,v 1.24 2000/01/22 22:19:18 mycroft 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.24 mycroft __RCSID("$NetBSD: pmap_rmt.c,v 1.24 2000/01/22 22:19:18 mycroft 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.22 lukem #include <assert.h>
62 1.14 lukem #include <err.h>
63 1.14 lukem #include <errno.h>
64 1.14 lukem #include <stdio.h>
65 1.14 lukem #include <string.h>
66 1.14 lukem #include <unistd.h>
67 1.13 lukem
68 1.13 lukem #include <rpc/rpc.h>
69 1.13 lukem #include <rpc/pmap_prot.h>
70 1.13 lukem #include <rpc/pmap_clnt.h>
71 1.13 lukem #include <rpc/pmap_rmt.h>
72 1.7 mrg
73 1.10 jtc #ifdef __weak_alias
74 1.24 mycroft __weak_alias(clnt_broadcast,_clnt_broadcast)
75 1.24 mycroft __weak_alias(pmap_rmtcall,_pmap_rmtcall)
76 1.24 mycroft __weak_alias(xdr_rmtcall_args,_xdr_rmtcall_args)
77 1.24 mycroft __weak_alias(xdr_rmtcallres,_xdr_rmtcallres)
78 1.10 jtc #endif
79 1.10 jtc
80 1.1 cgd #define MAX_BROADCAST_SIZE 1400
81 1.1 cgd
82 1.9 christos static int getbroadcastnets __P((struct in_addr *, int, char *));
83 1.9 christos
84 1.16 mycroft static const struct timeval timeout = { 3, 0 };
85 1.1 cgd
86 1.1 cgd /*
87 1.1 cgd * pmapper remote-call-service interface.
88 1.1 cgd * This routine is used to call the pmapper remote call service
89 1.1 cgd * which will look up a service program in the port maps, and then
90 1.1 cgd * remotely call that routine with the given parameters. This allows
91 1.1 cgd * programs to do a lookup and call in one step.
92 1.1 cgd */
93 1.1 cgd enum clnt_stat
94 1.18 lukem pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout,
95 1.18 lukem port_ptr)
96 1.1 cgd struct sockaddr_in *addr;
97 1.13 lukem u_long prog, vers, proc;
98 1.1 cgd xdrproc_t xdrargs, xdrres;
99 1.1 cgd caddr_t argsp, resp;
100 1.1 cgd struct timeval tout;
101 1.13 lukem u_long *port_ptr;
102 1.1 cgd {
103 1.17 christos int sock = -1;
104 1.14 lukem CLIENT *client;
105 1.1 cgd struct rmtcallargs a;
106 1.1 cgd struct rmtcallres r;
107 1.1 cgd enum clnt_stat stat;
108 1.1 cgd
109 1.22 lukem _DIAGASSERT(addr != NULL);
110 1.22 lukem _DIAGASSERT(port_ptr != NULL);
111 1.22 lukem
112 1.1 cgd addr->sin_port = htons(PMAPPORT);
113 1.17 christos client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &sock);
114 1.1 cgd if (client != (CLIENT *)NULL) {
115 1.1 cgd a.prog = prog;
116 1.1 cgd a.vers = vers;
117 1.1 cgd a.proc = proc;
118 1.1 cgd a.args_ptr = argsp;
119 1.1 cgd a.xdr_args = xdrargs;
120 1.1 cgd r.port_ptr = port_ptr;
121 1.1 cgd r.results_ptr = resp;
122 1.1 cgd r.xdr_results = xdrres;
123 1.19 christos stat = CLNT_CALL(client, PMAPPROC_CALLIT,
124 1.19 christos (xdrproc_t)xdr_rmtcall_args, &a, (xdrproc_t)xdr_rmtcallres,
125 1.19 christos &r, tout);
126 1.1 cgd CLNT_DESTROY(client);
127 1.1 cgd } else {
128 1.1 cgd stat = RPC_FAILED;
129 1.1 cgd }
130 1.1 cgd addr->sin_port = 0;
131 1.1 cgd return (stat);
132 1.1 cgd }
133 1.1 cgd
134 1.1 cgd
135 1.1 cgd /*
136 1.1 cgd * XDR remote call arguments
137 1.1 cgd * written for XDR_ENCODE direction only
138 1.1 cgd */
139 1.1 cgd bool_t
140 1.1 cgd xdr_rmtcall_args(xdrs, cap)
141 1.14 lukem XDR *xdrs;
142 1.14 lukem struct rmtcallargs *cap;
143 1.1 cgd {
144 1.13 lukem u_int lenposition, argposition, position;
145 1.1 cgd
146 1.22 lukem _DIAGASSERT(xdrs != NULL);
147 1.22 lukem _DIAGASSERT(cap != NULL);
148 1.22 lukem
149 1.13 lukem if (xdr_u_long(xdrs, &(cap->prog)) &&
150 1.13 lukem xdr_u_long(xdrs, &(cap->vers)) &&
151 1.13 lukem xdr_u_long(xdrs, &(cap->proc))) {
152 1.1 cgd lenposition = XDR_GETPOS(xdrs);
153 1.13 lukem if (! xdr_u_long(xdrs, &(cap->arglen)))
154 1.1 cgd return (FALSE);
155 1.1 cgd argposition = XDR_GETPOS(xdrs);
156 1.1 cgd if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
157 1.1 cgd return (FALSE);
158 1.1 cgd position = XDR_GETPOS(xdrs);
159 1.13 lukem cap->arglen = (u_long)position - (u_long)argposition;
160 1.1 cgd XDR_SETPOS(xdrs, lenposition);
161 1.13 lukem if (! xdr_u_long(xdrs, &(cap->arglen)))
162 1.1 cgd return (FALSE);
163 1.1 cgd XDR_SETPOS(xdrs, position);
164 1.1 cgd return (TRUE);
165 1.1 cgd }
166 1.1 cgd return (FALSE);
167 1.1 cgd }
168 1.1 cgd
169 1.1 cgd /*
170 1.1 cgd * XDR remote call results
171 1.1 cgd * written for XDR_DECODE direction only
172 1.1 cgd */
173 1.1 cgd bool_t
174 1.1 cgd xdr_rmtcallres(xdrs, crp)
175 1.14 lukem XDR *xdrs;
176 1.14 lukem struct rmtcallres *crp;
177 1.1 cgd {
178 1.1 cgd caddr_t port_ptr;
179 1.1 cgd
180 1.22 lukem _DIAGASSERT(xdrs != NULL);
181 1.22 lukem _DIAGASSERT(crp != NULL);
182 1.22 lukem
183 1.17 christos port_ptr = (caddr_t)(void *)crp->port_ptr;
184 1.13 lukem if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
185 1.19 christos (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
186 1.17 christos crp->port_ptr = (u_long *)(void *)port_ptr;
187 1.1 cgd return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
188 1.1 cgd }
189 1.1 cgd return (FALSE);
190 1.1 cgd }
191 1.1 cgd
192 1.1 cgd
193 1.1 cgd /*
194 1.1 cgd * The following is kludged-up support for simple rpc broadcasts.
195 1.1 cgd * Someday a large, complicated system will replace these trivial
196 1.1 cgd * routines which only support udp/ip .
197 1.1 cgd */
198 1.1 cgd
199 1.1 cgd static int
200 1.1 cgd getbroadcastnets(addrs, sock, buf)
201 1.1 cgd struct in_addr *addrs;
202 1.1 cgd int sock; /* any valid socket will do */
203 1.1 cgd char *buf; /* why allocxate more when we can use existing... */
204 1.1 cgd {
205 1.1 cgd struct ifconf ifc;
206 1.1 cgd struct ifreq ifreq, *ifr;
207 1.1 cgd struct sockaddr_in *sin;
208 1.1 cgd char *cp, *cplim;
209 1.9 christos int i = 0;
210 1.22 lukem
211 1.22 lukem _DIAGASSERT(addrs != NULL);
212 1.22 lukem _DIAGASSERT(buf != NULL);
213 1.1 cgd
214 1.1 cgd ifc.ifc_len = UDPMSGSIZE;
215 1.1 cgd ifc.ifc_buf = buf;
216 1.17 christos if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
217 1.18 lukem warn("getbroadcastnets: ioctl (get interface configuration)");
218 1.1 cgd return (0);
219 1.1 cgd }
220 1.1 cgd #define max(a, b) (a > b ? a : b)
221 1.1 cgd #define size(p) max((p).sa_len, sizeof(p))
222 1.1 cgd cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
223 1.1 cgd for (cp = buf; cp < cplim;
224 1.1 cgd cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
225 1.17 christos ifr = (struct ifreq *)(void *)cp;
226 1.1 cgd if (ifr->ifr_addr.sa_family != AF_INET)
227 1.1 cgd continue;
228 1.1 cgd ifreq = *ifr;
229 1.17 christos if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
230 1.18 lukem warn("getbroadcastnets: ioctl (get interface flags)");
231 1.1 cgd continue;
232 1.1 cgd }
233 1.1 cgd if ((ifreq.ifr_flags & IFF_BROADCAST) &&
234 1.1 cgd (ifreq.ifr_flags & IFF_UP)) {
235 1.17 christos sin = (struct sockaddr_in *)(void *)&ifr->ifr_addr;
236 1.1 cgd #ifdef SIOCGIFBRDADDR /* 4.3BSD */
237 1.17 christos if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
238 1.1 cgd addrs[i++] =
239 1.1 cgd inet_makeaddr(inet_netof(sin->sin_addr),
240 1.17 christos (unsigned long)INADDR_ANY);
241 1.1 cgd } else {
242 1.17 christos addrs[i++] = ((struct sockaddr_in *)(void *)
243 1.1 cgd &ifreq.ifr_addr)->sin_addr;
244 1.1 cgd }
245 1.1 cgd #else /* 4.2 BSD */
246 1.1 cgd addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr),
247 1.1 cgd INADDR_ANY);
248 1.1 cgd #endif
249 1.1 cgd }
250 1.1 cgd }
251 1.1 cgd return (i);
252 1.1 cgd }
253 1.1 cgd
254 1.9 christos typedef bool_t (*resultproc_t) __P((caddr_t, struct sockaddr_in *));
255 1.1 cgd
256 1.1 cgd enum clnt_stat
257 1.1 cgd clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
258 1.13 lukem u_long prog; /* program number */
259 1.13 lukem u_long vers; /* version number */
260 1.13 lukem u_long proc; /* procedure number */
261 1.1 cgd xdrproc_t xargs; /* xdr routine for args */
262 1.1 cgd caddr_t argsp; /* pointer to args */
263 1.1 cgd xdrproc_t xresults; /* xdr routine for results */
264 1.1 cgd caddr_t resultsp; /* pointer to results */
265 1.1 cgd resultproc_t eachresult; /* call with each result obtained */
266 1.1 cgd {
267 1.1 cgd enum clnt_stat stat;
268 1.1 cgd AUTH *unix_auth = authunix_create_default();
269 1.1 cgd XDR xdr_stream;
270 1.14 lukem XDR *xdrs = &xdr_stream;
271 1.21 christos int inlen, nets;
272 1.21 christos socklen_t fromlen;
273 1.17 christos size_t outlen;
274 1.14 lukem int sock;
275 1.1 cgd int on = 1;
276 1.7 mrg struct pollfd fd;
277 1.14 lukem int i;
278 1.1 cgd bool_t done = FALSE;
279 1.17 christos u_int32_t xid;
280 1.13 lukem u_long port;
281 1.1 cgd struct in_addr addrs[20];
282 1.1 cgd struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
283 1.1 cgd struct rmtcallargs a;
284 1.1 cgd struct rmtcallres r;
285 1.1 cgd struct rpc_msg msg;
286 1.1 cgd struct timeval t;
287 1.7 mrg int milliseconds;
288 1.1 cgd char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
289 1.1 cgd
290 1.1 cgd /*
291 1.1 cgd * initialization: create a socket, a broadcast address, and
292 1.1 cgd * preserialize the arguments into a send buffer.
293 1.1 cgd */
294 1.1 cgd if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
295 1.18 lukem warn("Cannot create socket for broadcast rpc");
296 1.1 cgd stat = RPC_CANTSEND;
297 1.1 cgd goto done_broad;
298 1.1 cgd }
299 1.1 cgd #ifdef SO_BROADCAST
300 1.1 cgd if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
301 1.18 lukem warn("Cannot set socket option SO_BROADCAST");
302 1.1 cgd stat = RPC_CANTSEND;
303 1.1 cgd goto done_broad;
304 1.1 cgd }
305 1.1 cgd #endif /* def SO_BROADCAST */
306 1.7 mrg fd.fd = sock;
307 1.8 mycroft fd.events = POLLIN;
308 1.1 cgd nets = getbroadcastnets(addrs, sock, inbuf);
309 1.5 jtc memset(&baddr, 0, sizeof (baddr));
310 1.6 mycroft baddr.sin_len = sizeof(struct sockaddr_in);
311 1.1 cgd baddr.sin_family = AF_INET;
312 1.1 cgd baddr.sin_port = htons(PMAPPORT);
313 1.1 cgd baddr.sin_addr.s_addr = htonl(INADDR_ANY);
314 1.1 cgd (void)gettimeofday(&t, (struct timezone *)0);
315 1.17 christos msg.rm_xid = xid = (u_int32_t)(getpid() ^ t.tv_sec ^ t.tv_usec);
316 1.1 cgd t.tv_usec = 0;
317 1.1 cgd msg.rm_direction = CALL;
318 1.1 cgd msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
319 1.1 cgd msg.rm_call.cb_prog = PMAPPROG;
320 1.1 cgd msg.rm_call.cb_vers = PMAPVERS;
321 1.1 cgd msg.rm_call.cb_proc = PMAPPROC_CALLIT;
322 1.1 cgd msg.rm_call.cb_cred = unix_auth->ah_cred;
323 1.1 cgd msg.rm_call.cb_verf = unix_auth->ah_verf;
324 1.1 cgd a.prog = prog;
325 1.1 cgd a.vers = vers;
326 1.1 cgd a.proc = proc;
327 1.1 cgd a.xdr_args = xargs;
328 1.1 cgd a.args_ptr = argsp;
329 1.1 cgd r.port_ptr = &port;
330 1.1 cgd r.xdr_results = xresults;
331 1.1 cgd r.results_ptr = resultsp;
332 1.1 cgd xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
333 1.1 cgd if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
334 1.1 cgd stat = RPC_CANTENCODEARGS;
335 1.1 cgd goto done_broad;
336 1.1 cgd }
337 1.17 christos outlen = xdr_getpos(xdrs);
338 1.1 cgd xdr_destroy(xdrs);
339 1.1 cgd /*
340 1.1 cgd * Basic loop: broadcast a packet and wait a while for response(s).
341 1.1 cgd * The response timeout grows larger per iteration.
342 1.1 cgd */
343 1.1 cgd for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
344 1.1 cgd for (i = 0; i < nets; i++) {
345 1.1 cgd baddr.sin_addr = addrs[i];
346 1.1 cgd if (sendto(sock, outbuf, outlen, 0,
347 1.17 christos (struct sockaddr *)(void *)&baddr,
348 1.1 cgd sizeof (struct sockaddr)) != outlen) {
349 1.18 lukem warn("Cannot send broadcast packet");
350 1.1 cgd stat = RPC_CANTSEND;
351 1.1 cgd goto done_broad;
352 1.1 cgd }
353 1.1 cgd }
354 1.1 cgd if (eachresult == NULL) {
355 1.1 cgd stat = RPC_SUCCESS;
356 1.1 cgd goto done_broad;
357 1.1 cgd }
358 1.1 cgd recv_again:
359 1.17 christos milliseconds = (int)(t.tv_sec * 1000 + t.tv_usec / 1000);
360 1.1 cgd msg.acpted_rply.ar_verf = _null_auth;
361 1.17 christos msg.acpted_rply.ar_results.where = (caddr_t)(void *)&r;
362 1.19 christos msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
363 1.7 mrg switch (poll(&fd, 1, milliseconds)) {
364 1.1 cgd
365 1.1 cgd case 0: /* timed out */
366 1.1 cgd stat = RPC_TIMEDOUT;
367 1.1 cgd continue;
368 1.1 cgd
369 1.1 cgd case -1: /* some kind of error */
370 1.1 cgd if (errno == EINTR)
371 1.1 cgd goto recv_again;
372 1.18 lukem warn("Broadcast poll problem");
373 1.1 cgd stat = RPC_CANTRECV;
374 1.1 cgd goto done_broad;
375 1.1 cgd
376 1.7 mrg } /* end of poll results switch */
377 1.1 cgd try_again:
378 1.1 cgd fromlen = sizeof(struct sockaddr);
379 1.1 cgd inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0,
380 1.17 christos (struct sockaddr *)(void *)&raddr, &fromlen);
381 1.1 cgd if (inlen < 0) {
382 1.1 cgd if (errno == EINTR)
383 1.1 cgd goto try_again;
384 1.18 lukem warn("Cannot receive reply to broadcast");
385 1.1 cgd stat = RPC_CANTRECV;
386 1.1 cgd goto done_broad;
387 1.1 cgd }
388 1.3 cgd if (inlen < sizeof(u_int32_t))
389 1.1 cgd goto recv_again;
390 1.1 cgd /*
391 1.1 cgd * see if reply transaction id matches sent id.
392 1.1 cgd * If so, decode the results.
393 1.1 cgd */
394 1.13 lukem xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE);
395 1.1 cgd if (xdr_replymsg(xdrs, &msg)) {
396 1.1 cgd if ((msg.rm_xid == xid) &&
397 1.1 cgd (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
398 1.1 cgd (msg.acpted_rply.ar_stat == SUCCESS)) {
399 1.13 lukem raddr.sin_port = htons((u_short)port);
400 1.1 cgd done = (*eachresult)(resultsp, &raddr);
401 1.1 cgd }
402 1.1 cgd /* otherwise, we just ignore the errors ... */
403 1.1 cgd } else {
404 1.1 cgd #ifdef notdef
405 1.1 cgd /* some kind of deserialization problem ... */
406 1.1 cgd if (msg.rm_xid == xid)
407 1.18 lukem fprintf(stderr,
408 1.18 lukem "Broadcast deserialization problem");
409 1.1 cgd /* otherwise, just random garbage */
410 1.1 cgd #endif
411 1.1 cgd }
412 1.1 cgd xdrs->x_op = XDR_FREE;
413 1.19 christos msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
414 1.1 cgd (void)xdr_replymsg(xdrs, &msg);
415 1.1 cgd (void)(*xresults)(xdrs, resultsp);
416 1.1 cgd xdr_destroy(xdrs);
417 1.1 cgd if (done) {
418 1.1 cgd stat = RPC_SUCCESS;
419 1.1 cgd goto done_broad;
420 1.1 cgd } else {
421 1.1 cgd goto recv_again;
422 1.1 cgd }
423 1.1 cgd }
424 1.15 tv stat = RPC_TIMEDOUT;
425 1.1 cgd done_broad:
426 1.18 lukem if (sock != -1)
427 1.18 lukem (void)close(sock);
428 1.1 cgd AUTH_DESTROY(unix_auth);
429 1.1 cgd return (stat);
430 1.1 cgd }
431