Sync Citrus iconv support with NetBSD.
[dragonfly.git] / lib / libc / rpc / pmap_rmt.c
blobe8ae0b2b08a224fbc22e34569d69e886dd1bd38f
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
29 * @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro
30 * @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/rpc/pmap_rmt.c,v 1.16.2.1 2002/06/30 23:34:58 iedowse Exp $
32 * $DragonFly: src/lib/libc/rpc/pmap_rmt.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
36 * pmap_rmt.c
37 * Client interface to pmap rpc service.
38 * remote call and broadcast service
40 * Copyright (C) 1984, Sun Microsystems, Inc.
43 #include "namespace.h"
44 #include <rpc/rpc.h>
45 #include <rpc/pmap_prot.h>
46 #include <rpc/pmap_clnt.h>
47 #include <rpc/pmap_rmt.h>
48 #include <sys/socket.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <errno.h>
53 #include <string.h>
54 #include <net/if.h>
55 #include <sys/ioctl.h>
56 #include <arpa/inet.h>
57 #include "un-namespace.h"
59 #define MAX_BROADCAST_SIZE 1400
61 static struct timeval timeout = { 3, 0 };
64 * pmapper remote-call-service interface.
65 * This routine is used to call the pmapper remote call service
66 * which will look up a service program in the port maps, and then
67 * remotely call that routine with the given parameters. This allows
68 * programs to do a lookup and call in one step.
70 enum clnt_stat
71 pmap_rmtcall(struct sockaddr_in *addr, u_long prog, u_long vers, u_long proc,
72 xdrproc_t xdrargs, caddr_t argsp, xdrproc_t xdrres, caddr_t resp,
73 struct timeval tout, u_long *port_ptr)
75 int socket = -1;
76 CLIENT *client;
77 struct rmtcallargs a;
78 struct rmtcallres r;
79 enum clnt_stat stat;
81 addr->sin_port = htons(PMAPPORT);
82 client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket);
83 if (client != (CLIENT *)NULL) {
84 a.prog = prog;
85 a.vers = vers;
86 a.proc = proc;
87 a.args_ptr = argsp;
88 a.xdr_args = xdrargs;
89 r.port_ptr = port_ptr;
90 r.results_ptr = resp;
91 r.xdr_results = xdrres;
92 stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a,
93 xdr_rmtcallres, &r, tout);
94 CLNT_DESTROY(client);
95 } else {
96 stat = RPC_FAILED;
98 if (socket != -1)
99 _close(socket);
100 addr->sin_port = 0;
101 return (stat);
106 * XDR remote call arguments
107 * written for XDR_ENCODE direction only
109 bool_t
110 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
112 u_int lenposition, argposition, position;
114 if (xdr_u_long(xdrs, &(cap->prog)) &&
115 xdr_u_long(xdrs, &(cap->vers)) &&
116 xdr_u_long(xdrs, &(cap->proc))) {
117 lenposition = XDR_GETPOS(xdrs);
118 if (! xdr_u_long(xdrs, &(cap->arglen)))
119 return (FALSE);
120 argposition = XDR_GETPOS(xdrs);
121 if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
122 return (FALSE);
123 position = XDR_GETPOS(xdrs);
124 cap->arglen = (u_long)position - (u_long)argposition;
125 XDR_SETPOS(xdrs, lenposition);
126 if (! xdr_u_long(xdrs, &(cap->arglen)))
127 return (FALSE);
128 XDR_SETPOS(xdrs, position);
129 return (TRUE);
131 return (FALSE);
135 * XDR remote call results
136 * written for XDR_DECODE direction only
138 bool_t
139 xdr_rmtcallres(XDR *xdrs, struct rmtcallres *crp)
141 caddr_t port_ptr;
143 port_ptr = (caddr_t)crp->port_ptr;
144 if (xdr_reference(xdrs, &port_ptr, sizeof (u_long),
145 xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
146 crp->port_ptr = (u_long *)port_ptr;
147 return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
149 return (FALSE);
154 * The following is kludged-up support for simple rpc broadcasts.
155 * Someday a large, complicated system will replace these trivial
156 * routines which only support udp/ip .
159 static int
160 getbroadcastnets(struct in_addr *addrs,
161 int sock, /* any valid socket will do */
162 char *buf) /* why allocxate more when we can use existing... */
164 struct ifconf ifc;
165 struct ifreq ifreq, *ifr;
166 struct sockaddr_in *sin;
167 struct in_addr addr;
168 char *cp, *cplim;
169 int n, i = 0;
171 ifc.ifc_len = UDPMSGSIZE;
172 ifc.ifc_buf = buf;
173 if (_ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
174 perror("broadcast: ioctl (get interface configuration)");
175 return (0);
177 #define max(a, b) (a > b ? a : b)
178 #define size(p) max((p).sa_len, sizeof(p))
179 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
180 for (cp = buf; cp < cplim;
181 cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
182 ifr = (struct ifreq *)cp;
183 if (ifr->ifr_addr.sa_family != AF_INET)
184 continue;
185 memcpy(&ifreq, ifr, sizeof(ifreq));
186 if (_ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
187 perror("broadcast: ioctl (get interface flags)");
188 continue;
190 if ((ifreq.ifr_flags & IFF_BROADCAST) &&
191 (ifreq.ifr_flags & IFF_UP)) {
192 sin = (struct sockaddr_in *)&ifr->ifr_addr;
193 #ifdef SIOCGIFBRDADDR /* 4.3BSD */
194 if (_ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
195 addr =
196 inet_makeaddr(inet_netof(sin->sin_addr),
197 INADDR_ANY);
198 } else {
199 addr = ((struct sockaddr_in*)
200 &ifreq.ifr_addr)->sin_addr;
202 #else /* 4.2 BSD */
203 addr = inet_makeaddr(inet_netof(sin->sin_addr),
204 INADDR_ANY);
205 #endif
206 for (n=i-1; n>=0; n--) {
207 if (addr.s_addr == addrs[n].s_addr)
208 break;
210 if (n<0) {
211 addrs[i++] = addr;
215 return (i);
218 typedef bool_t (*resultproc_t)();
220 enum clnt_stat
221 clnt_broadcast(u_long prog, /* program number */
222 u_long vers, /* version number */
223 u_long proc, /* procedure number */
224 xdrproc_t xargs, /* xdr routine for args */
225 caddr_t argsp, /* pointer to args */
226 xdrproc_t xresults, /* xdr routine for results */
227 caddr_t resultsp, /* pointer to results */
228 resultproc_t eachresult) /* call with each result obtained */
230 enum clnt_stat stat;
231 AUTH *unix_auth = authunix_create_default();
232 XDR xdr_stream;
233 XDR *xdrs = &xdr_stream;
234 int outlen, inlen, fromlen, nets;
235 int sock;
236 int on = 1;
237 fd_set *fds, readfds;
238 int i;
239 bool_t done = FALSE;
240 u_long xid;
241 u_long port;
242 struct in_addr addrs[20];
243 struct sockaddr_in baddr, raddr; /* broadcast and response addresses */
244 struct rmtcallargs a;
245 struct rmtcallres r;
246 struct rpc_msg msg;
247 struct timeval t, tv;
248 char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE];
249 static u_int32_t disrupt;
251 if (disrupt == 0)
252 disrupt = (u_int32_t)(long)resultsp;
255 * initialization: create a socket, a broadcast address, and
256 * preserialize the arguments into a send buffer.
258 if ((sock = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
259 perror("Cannot create socket for broadcast rpc");
260 stat = RPC_CANTSEND;
261 goto done_broad;
263 #ifdef SO_BROADCAST
264 if (_setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
265 perror("Cannot set socket option SO_BROADCAST");
266 stat = RPC_CANTSEND;
267 goto done_broad;
269 #endif /* def SO_BROADCAST */
270 if (sock + 1 > FD_SETSIZE) {
271 int bytes = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
272 fds = (fd_set *)malloc(bytes);
273 if (fds == NULL) {
274 stat = RPC_CANTSEND;
275 goto done_broad;
277 memset(fds, 0, bytes);
278 } else {
279 fds = &readfds;
280 FD_ZERO(fds);
283 nets = getbroadcastnets(addrs, sock, inbuf);
284 memset(&baddr, 0, sizeof (baddr));
285 baddr.sin_len = sizeof(struct sockaddr_in);
286 baddr.sin_family = AF_INET;
287 baddr.sin_port = htons(PMAPPORT);
288 baddr.sin_addr.s_addr = htonl(INADDR_ANY);
289 gettimeofday(&t, (struct timezone *)0);
290 msg.rm_xid = xid = (++disrupt) ^ getpid() ^ t.tv_sec ^ t.tv_usec;
291 t.tv_usec = 0;
292 msg.rm_direction = CALL;
293 msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
294 msg.rm_call.cb_prog = PMAPPROG;
295 msg.rm_call.cb_vers = PMAPVERS;
296 msg.rm_call.cb_proc = PMAPPROC_CALLIT;
297 msg.rm_call.cb_cred = unix_auth->ah_cred;
298 msg.rm_call.cb_verf = unix_auth->ah_verf;
299 a.prog = prog;
300 a.vers = vers;
301 a.proc = proc;
302 a.xdr_args = xargs;
303 a.args_ptr = argsp;
304 r.port_ptr = &port;
305 r.xdr_results = xresults;
306 r.results_ptr = resultsp;
307 xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE);
308 if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) {
309 stat = RPC_CANTENCODEARGS;
310 goto done_broad;
312 outlen = (int)xdr_getpos(xdrs);
313 xdr_destroy(xdrs);
315 * Basic loop: broadcast a packet and wait a while for response(s).
316 * The response timeout grows larger per iteration.
318 * XXX This will loop about 5 times the stop. If there are
319 * lots of signals being received by the process it will quit
320 * send them all in one quick burst, not paying attention to
321 * the intended function of sending them slowly over half a
322 * minute or so
324 for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) {
325 int success = 0;
326 for (i = 0; i < nets; i++) {
327 baddr.sin_addr = addrs[i];
328 if (_sendto(sock, outbuf, outlen, 0,
329 (struct sockaddr *)&baddr,
330 sizeof (struct sockaddr)) == outlen) {
331 success++;
334 if (!success) {
335 perror("Cannot send broadcast packet");
336 stat = RPC_CANTSEND;
337 goto done_broad;
339 if (eachresult == NULL) {
340 stat = RPC_SUCCESS;
341 goto done_broad;
343 recv_again:
344 msg.acpted_rply.ar_verf = _null_auth;
345 msg.acpted_rply.ar_results.where = (caddr_t)&r;
346 msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
347 /* XXX we know the other bits are still clear */
348 FD_SET(sock, fds);
349 tv = t; /* for _select() that copies back */
350 switch (_select(sock + 1, fds, NULL, NULL, &tv)) {
352 case 0: /* timed out */
353 stat = RPC_TIMEDOUT;
354 continue;
356 case -1: /* some kind of error */
357 if (errno == EINTR)
358 goto recv_again;
359 perror("Broadcast select problem");
360 stat = RPC_CANTRECV;
361 goto done_broad;
363 } /* end of select results switch */
364 try_again:
365 fromlen = sizeof(struct sockaddr);
366 inlen = _recvfrom(sock, inbuf, UDPMSGSIZE, 0,
367 (struct sockaddr *)&raddr, &fromlen);
368 if (inlen < 0) {
369 if (errno == EINTR)
370 goto try_again;
371 perror("Cannot receive reply to broadcast");
372 stat = RPC_CANTRECV;
373 goto done_broad;
375 if (inlen < sizeof(u_int32_t))
376 goto recv_again;
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)) {
383 if ((msg.rm_xid == xid) &&
384 (msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
385 (msg.acpted_rply.ar_stat == SUCCESS)) {
386 raddr.sin_port = htons((u_short)port);
387 done = (*eachresult)(resultsp, &raddr);
389 /* otherwise, we just ignore the errors ... */
391 xdrs->x_op = XDR_FREE;
392 msg.acpted_rply.ar_results.proc = xdr_void;
393 xdr_replymsg(xdrs, &msg);
394 (*xresults)(xdrs, resultsp);
395 xdr_destroy(xdrs);
396 if (done) {
397 stat = RPC_SUCCESS;
398 goto done_broad;
399 } else {
400 goto recv_again;
403 done_broad:
404 if (fds != &readfds)
405 free(fds);
406 if (sock >= 0)
407 _close(sock);
408 AUTH_DESTROY(unix_auth);
409 return (stat);