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