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