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