Commit | Line | Data |
---|---|---|
15637ed4 RG |
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. | |
8 | * | |
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. | |
12 | * | |
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. | |
16 | * | |
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. | |
20 | * | |
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. | |
24 | * | |
25 | * Sun Microsystems, Inc. | |
26 | * 2550 Garcia Avenue | |
27 | * Mountain View, California 94043 | |
28 | */ | |
78ed81a3 | 29 | |
30 | #if defined(LIBC_SCCS) && !defined(lint) | |
31 | /*static char *sccsid = "from: @(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro";*/ | |
32 | /*static char *sccsid = "from: @(#)pmap_rmt.c 2.2 88/08/01 4.0 RPCSRC";*/ | |
33 | static char *rcsid = "$Id: pmap_rmt.c,v 1.3 1993/08/26 00:53:32 jtc Exp $"; | |
15637ed4 RG |
34 | #endif |
35 | ||
36 | /* | |
37 | * pmap_rmt.c | |
38 | * Client interface to pmap rpc service. | |
39 | * remote call and broadcast service | |
40 | * | |
41 | * Copyright (C) 1984, Sun Microsystems, Inc. | |
42 | */ | |
43 | ||
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 <errno.h> | |
51 | #include <net/if.h> | |
52 | #include <sys/ioctl.h> | |
53 | #include <arpa/inet.h> | |
54 | #define MAX_BROADCAST_SIZE 1400 | |
55 | ||
56 | extern int errno; | |
57 | static struct timeval timeout = { 3, 0 }; | |
58 | ||
59 | ||
60 | /* | |
61 | * pmapper remote-call-service interface. | |
62 | * This routine is used to call the pmapper remote call service | |
63 | * which will look up a service program in the port maps, and then | |
64 | * remotely call that routine with the given parameters. This allows | |
65 | * programs to do a lookup and call in one step. | |
66 | */ | |
67 | enum clnt_stat | |
68 | pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr) | |
69 | struct sockaddr_in *addr; | |
70 | u_long prog, vers, proc; | |
71 | xdrproc_t xdrargs, xdrres; | |
72 | caddr_t argsp, resp; | |
73 | struct timeval tout; | |
74 | u_long *port_ptr; | |
75 | { | |
76 | int socket = -1; | |
77 | register CLIENT *client; | |
78 | struct rmtcallargs a; | |
79 | struct rmtcallres r; | |
80 | enum clnt_stat stat; | |
81 | ||
82 | addr->sin_port = htons(PMAPPORT); | |
83 | client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket); | |
84 | if (client != (CLIENT *)NULL) { | |
85 | a.prog = prog; | |
86 | a.vers = vers; | |
87 | a.proc = proc; | |
88 | a.args_ptr = argsp; | |
89 | a.xdr_args = xdrargs; | |
90 | r.port_ptr = port_ptr; | |
91 | r.results_ptr = resp; | |
92 | r.xdr_results = xdrres; | |
93 | stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a, | |
94 | xdr_rmtcallres, &r, tout); | |
95 | CLNT_DESTROY(client); | |
96 | } else { | |
97 | stat = RPC_FAILED; | |
98 | } | |
99 | (void)close(socket); | |
100 | addr->sin_port = 0; | |
101 | return (stat); | |
102 | } | |
103 | ||
104 | ||
105 | /* | |
106 | * XDR remote call arguments | |
107 | * written for XDR_ENCODE direction only | |
108 | */ | |
109 | bool_t | |
110 | xdr_rmtcall_args(xdrs, cap) | |
111 | register XDR *xdrs; | |
112 | register struct rmtcallargs *cap; | |
113 | { | |
114 | u_int lenposition, argposition, position; | |
115 | ||
116 | if (xdr_u_long(xdrs, &(cap->prog)) && | |
117 | xdr_u_long(xdrs, &(cap->vers)) && | |
118 | xdr_u_long(xdrs, &(cap->proc))) { | |
119 | lenposition = XDR_GETPOS(xdrs); | |
120 | if (! xdr_u_long(xdrs, &(cap->arglen))) | |
121 | return (FALSE); | |
122 | argposition = XDR_GETPOS(xdrs); | |
123 | if (! (*(cap->xdr_args))(xdrs, cap->args_ptr)) | |
124 | return (FALSE); | |
125 | position = XDR_GETPOS(xdrs); | |
126 | cap->arglen = (u_long)position - (u_long)argposition; | |
127 | XDR_SETPOS(xdrs, lenposition); | |
128 | if (! xdr_u_long(xdrs, &(cap->arglen))) | |
129 | return (FALSE); | |
130 | XDR_SETPOS(xdrs, position); | |
131 | return (TRUE); | |
132 | } | |
133 | return (FALSE); | |
134 | } | |
135 | ||
136 | /* | |
137 | * XDR remote call results | |
138 | * written for XDR_DECODE direction only | |
139 | */ | |
140 | bool_t | |
141 | xdr_rmtcallres(xdrs, crp) | |
142 | register XDR *xdrs; | |
143 | register struct rmtcallres *crp; | |
144 | { | |
145 | caddr_t port_ptr; | |
146 | ||
147 | port_ptr = (caddr_t)crp->port_ptr; | |
148 | if (xdr_reference(xdrs, &port_ptr, sizeof (u_long), | |
149 | xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) { | |
150 | crp->port_ptr = (u_long *)port_ptr; | |
151 | return ((*(crp->xdr_results))(xdrs, crp->results_ptr)); | |
152 | } | |
153 | return (FALSE); | |
154 | } | |
155 | ||
156 | ||
157 | /* | |
158 | * The following is kludged-up support for simple rpc broadcasts. | |
159 | * Someday a large, complicated system will replace these trivial | |
160 | * routines which only support udp/ip . | |
161 | */ | |
162 | ||
163 | static int | |
164 | getbroadcastnets(addrs, sock, buf) | |
165 | struct in_addr *addrs; | |
166 | int sock; /* any valid socket will do */ | |
167 | char *buf; /* why allocxate more when we can use existing... */ | |
168 | { | |
169 | struct ifconf ifc; | |
170 | struct ifreq ifreq, *ifr; | |
171 | struct sockaddr_in *sin; | |
172 | char *cp, *cplim; | |
173 | int n, i = 0; | |
174 | ||
175 | ifc.ifc_len = UDPMSGSIZE; | |
176 | ifc.ifc_buf = buf; | |
177 | if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) { | |
178 | perror("broadcast: ioctl (get interface configuration)"); | |
179 | return (0); | |
180 | } | |
181 | #define max(a, b) (a > b ? a : b) | |
182 | #define size(p) max((p).sa_len, sizeof(p)) | |
183 | cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ | |
184 | for (cp = buf; cp < cplim; | |
185 | cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) { | |
186 | ifr = (struct ifreq *)cp; | |
187 | if (ifr->ifr_addr.sa_family != AF_INET) | |
188 | continue; | |
189 | ifreq = *ifr; | |
190 | if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) { | |
191 | perror("broadcast: ioctl (get interface flags)"); | |
192 | continue; | |
193 | } | |
194 | if ((ifreq.ifr_flags & IFF_BROADCAST) && | |
195 | (ifreq.ifr_flags & IFF_UP)) { | |
196 | sin = (struct sockaddr_in *)&ifr->ifr_addr; | |
197 | #ifdef SIOCGIFBRDADDR /* 4.3BSD */ | |
198 | if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { | |
199 | addrs[i++] = | |
200 | inet_makeaddr(inet_netof(sin->sin_addr), | |
201 | INADDR_ANY); | |
202 | } else { | |
203 | addrs[i++] = ((struct sockaddr_in*) | |
204 | &ifreq.ifr_addr)->sin_addr; | |
205 | } | |
206 | #else /* 4.2 BSD */ | |
207 | addrs[i++] = inet_makeaddr(inet_netof(sin->sin_addr), | |
208 | INADDR_ANY); | |
209 | #endif | |
210 | } | |
211 | } | |
212 | return (i); | |
213 | } | |
214 | ||
215 | typedef bool_t (*resultproc_t)(); | |
216 | ||
217 | enum clnt_stat | |
218 | clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) | |
219 | u_long prog; /* program number */ | |
220 | u_long vers; /* version number */ | |
221 | u_long proc; /* procedure number */ | |
222 | xdrproc_t xargs; /* xdr routine for args */ | |
223 | caddr_t argsp; /* pointer to args */ | |
224 | xdrproc_t xresults; /* xdr routine for results */ | |
225 | caddr_t resultsp; /* pointer to results */ | |
226 | resultproc_t eachresult; /* call with each result obtained */ | |
227 | { | |
228 | enum clnt_stat stat; | |
229 | AUTH *unix_auth = authunix_create_default(); | |
230 | XDR xdr_stream; | |
231 | register XDR *xdrs = &xdr_stream; | |
232 | int outlen, inlen, fromlen, nets; | |
233 | register int sock; | |
234 | int on = 1; | |
235 | #ifdef FD_SETSIZE | |
236 | fd_set mask; | |
237 | fd_set readfds; | |
238 | #else | |
239 | int readfds; | |
240 | register int mask; | |
241 | #endif /* def FD_SETSIZE */ | |
242 | register int i; | |
243 | bool_t done = FALSE; | |
244 | register u_long xid; | |
245 | u_long port; | |
246 | struct in_addr addrs[20]; | |
247 | struct sockaddr_in baddr, raddr; /* broadcast and response addresses */ | |
248 | struct rmtcallargs a; | |
249 | struct rmtcallres r; | |
250 | struct rpc_msg msg; | |
251 | struct timeval t; | |
252 | char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE]; | |
253 | ||
254 | /* | |
255 | * initialization: create a socket, a broadcast address, and | |
256 | * preserialize the arguments into a send buffer. | |
257 | */ | |
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; | |
262 | } | |
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; | |
268 | } | |
269 | #endif /* def SO_BROADCAST */ | |
270 | #ifdef FD_SETSIZE | |
271 | FD_ZERO(&mask); | |
272 | FD_SET(sock, &mask); | |
273 | #else | |
274 | mask = (1 << sock); | |
275 | #endif /* def FD_SETSIZE */ | |
276 | nets = getbroadcastnets(addrs, sock, inbuf); | |
277 | bzero((char *)&baddr, sizeof (baddr)); | |
278 | baddr.sin_family = AF_INET; | |
279 | baddr.sin_port = htons(PMAPPORT); | |
280 | baddr.sin_addr.s_addr = htonl(INADDR_ANY); | |
281 | /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */ | |
282 | (void)gettimeofday(&t, (struct timezone *)0); | |
283 | msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec; | |
284 | t.tv_usec = 0; | |
285 | msg.rm_direction = CALL; | |
286 | msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; | |
287 | msg.rm_call.cb_prog = PMAPPROG; | |
288 | msg.rm_call.cb_vers = PMAPVERS; | |
289 | msg.rm_call.cb_proc = PMAPPROC_CALLIT; | |
290 | msg.rm_call.cb_cred = unix_auth->ah_cred; | |
291 | msg.rm_call.cb_verf = unix_auth->ah_verf; | |
292 | a.prog = prog; | |
293 | a.vers = vers; | |
294 | a.proc = proc; | |
295 | a.xdr_args = xargs; | |
296 | a.args_ptr = argsp; | |
297 | r.port_ptr = &port; | |
298 | r.xdr_results = xresults; | |
299 | r.results_ptr = resultsp; | |
300 | xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE); | |
301 | if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) { | |
302 | stat = RPC_CANTENCODEARGS; | |
303 | goto done_broad; | |
304 | } | |
305 | outlen = (int)xdr_getpos(xdrs); | |
306 | xdr_destroy(xdrs); | |
307 | /* | |
308 | * Basic loop: broadcast a packet and wait a while for response(s). | |
309 | * The response timeout grows larger per iteration. | |
310 | */ | |
311 | for (t.tv_sec = 4; t.tv_sec <= 14; t.tv_sec += 2) { | |
312 | for (i = 0; i < nets; i++) { | |
313 | baddr.sin_addr = addrs[i]; | |
314 | if (sendto(sock, outbuf, outlen, 0, | |
315 | (struct sockaddr *)&baddr, | |
316 | sizeof (struct sockaddr)) != outlen) { | |
317 | perror("Cannot send broadcast packet"); | |
318 | stat = RPC_CANTSEND; | |
319 | goto done_broad; | |
320 | } | |
321 | } | |
322 | if (eachresult == NULL) { | |
323 | stat = RPC_SUCCESS; | |
324 | goto done_broad; | |
325 | } | |
326 | recv_again: | |
327 | msg.acpted_rply.ar_verf = _null_auth; | |
328 | msg.acpted_rply.ar_results.where = (caddr_t)&r; | |
329 | msg.acpted_rply.ar_results.proc = xdr_rmtcallres; | |
330 | readfds = mask; | |
331 | switch (select(_rpc_dtablesize(), &readfds, (int *)NULL, | |
332 | (int *)NULL, &t)) { | |
333 | ||
334 | case 0: /* timed out */ | |
335 | stat = RPC_TIMEDOUT; | |
336 | continue; | |
337 | ||
338 | case -1: /* some kind of error */ | |
339 | if (errno == EINTR) | |
340 | goto recv_again; | |
341 | perror("Broadcast select problem"); | |
342 | stat = RPC_CANTRECV; | |
343 | goto done_broad; | |
344 | ||
345 | } /* end of select results switch */ | |
346 | try_again: | |
347 | fromlen = sizeof(struct sockaddr); | |
348 | inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0, | |
349 | (struct sockaddr *)&raddr, &fromlen); | |
350 | if (inlen < 0) { | |
351 | if (errno == EINTR) | |
352 | goto try_again; | |
353 | perror("Cannot receive reply to broadcast"); | |
354 | stat = RPC_CANTRECV; | |
355 | goto done_broad; | |
356 | } | |
357 | if (inlen < sizeof(u_long)) | |
358 | goto recv_again; | |
359 | /* | |
360 | * see if reply transaction id matches sent id. | |
361 | * If so, decode the results. | |
362 | */ | |
363 | xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE); | |
364 | if (xdr_replymsg(xdrs, &msg)) { | |
365 | if ((msg.rm_xid == xid) && | |
366 | (msg.rm_reply.rp_stat == MSG_ACCEPTED) && | |
367 | (msg.acpted_rply.ar_stat == SUCCESS)) { | |
368 | raddr.sin_port = htons((u_short)port); | |
369 | done = (*eachresult)(resultsp, &raddr); | |
370 | } | |
371 | /* otherwise, we just ignore the errors ... */ | |
372 | } else { | |
373 | #ifdef notdef | |
374 | /* some kind of deserialization problem ... */ | |
375 | if (msg.rm_xid == xid) | |
376 | fprintf(stderr, "Broadcast deserialization problem"); | |
377 | /* otherwise, just random garbage */ | |
378 | #endif | |
379 | } | |
380 | xdrs->x_op = XDR_FREE; | |
381 | msg.acpted_rply.ar_results.proc = xdr_void; | |
382 | (void)xdr_replymsg(xdrs, &msg); | |
383 | (void)(*xresults)(xdrs, resultsp); | |
384 | xdr_destroy(xdrs); | |
385 | if (done) { | |
386 | stat = RPC_SUCCESS; | |
387 | goto done_broad; | |
388 | } else { | |
389 | goto recv_again; | |
390 | } | |
391 | } | |
392 | done_broad: | |
393 | (void)close(sock); | |
394 | AUTH_DESTROY(unix_auth); | |
395 | return (stat); | |
396 | } | |
397 |