]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ypbind/yp_ping.c
This commit was generated by cvs2svn to compensate for changes in r90019,
[FreeBSD/FreeBSD.git] / usr.sbin / ypbind / yp_ping.c
1 /*
2  * Copyright (c) 1996, 1997
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
35  * unrestricted use provided that this legend is included on all tape
36  * media and as a part of the software program in whole or part.  Users
37  * may copy or modify Sun RPC without charge, but are not authorized
38  * to license or distribute it to anyone else except as part of a product or
39  * program developed by the user.
40  *
41  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
42  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
44  *
45  * Sun RPC is provided with no support and without any obligation on the
46  * part of Sun Microsystems, Inc. to assist in its use, correction,
47  * modification or enhancement.
48  *
49  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
50  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
51  * OR ANY PART THEREOF.
52  *
53  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
54  * or profits or other special, indirect and consequential damages, even if
55  * Sun has been advised of the possibility of such damages.
56  *
57  * Sun Microsystems, Inc.
58  * 2550 Garcia Avenue
59  * Mountain View, California  94043
60  */
61
62 #ifndef lint
63 #if 0
64 static char *sccsid = "@(#)from: clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
65 static char *sccsid = "@(#)from: clnt_udp.c     2.2 88/08/01 4.0 RPCSRC";
66 #endif
67 static const char rcsid[] =
68   "$FreeBSD$";
69 #endif
70
71 /*
72  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
73  *
74  * Copyright (C) 1984, Sun Microsystems, Inc.
75  */
76
77 #include <errno.h>
78 #include <netdb.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include <pthread.h>
84 #include <rpc/rpc.h>
85 #include <rpc/pmap_clnt.h>
86 #include <rpc/pmap_prot.h>
87 #include <rpcsvc/yp.h>
88 #include <sys/types.h>
89 #include <sys/poll.h>
90 #include <sys/socket.h>
91 #include <sys/signal.h>
92 #include <sys/ioctl.h>
93 #include <net/if.h>
94
95 #include "yp_ping.h"
96
97 struct cu_data {
98         int                     cu_fd;          /* connections fd */
99         bool_t                  cu_closeit;     /* opened by library */
100         struct sockaddr_storage cu_raddr;       /* remote address */
101         int                     cu_rlen;
102         struct timeval          cu_wait;        /* retransmit interval */
103         struct timeval          cu_total;       /* total time for the call */
104         struct rpc_err          cu_error;
105         XDR                     cu_outxdrs;
106         u_int                   cu_xdrpos;
107         u_int                   cu_sendsz;      /* send size */
108         char                    *cu_outbuf;
109         u_int                   cu_recvsz;      /* recv size */
110         struct pollfd           pfdp;
111         int                     cu_async;
112         char                    cu_inbuf[1];
113 };
114
115 /*
116  * pmap_getport.c
117  * Client interface to pmap rpc service.
118  *
119  * Copyright (C) 1984, Sun Microsystems, Inc.
120  */
121
122
123 static struct timeval timeout = { 1, 0 };
124 static struct timeval tottimeout = { 1, 0 };
125
126 /*
127  * Find the mapped port for program,version.
128  * Calls the pmap service remotely to do the lookup.
129  * Returns 0 if no map exists.
130  */
131 static u_short
132 __pmap_getport(address, program, version, protocol)
133         struct sockaddr_in *address;
134         u_long program;
135         u_long version;
136         u_int protocol;
137 {
138         u_short port = 0;
139         int sock = -1;
140         register CLIENT *client;
141         struct pmap parms;
142
143         address->sin_port = htons(PMAPPORT);
144
145         client = clntudp_bufcreate(address, PMAPPROG,
146             PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
147         if (client != (CLIENT *)NULL) {
148                 parms.pm_prog = program;
149                 parms.pm_vers = version;
150                 parms.pm_prot = protocol;
151                 parms.pm_port = 0;  /* not needed or used */
152                 if (CLNT_CALL(client, PMAPPROC_GETPORT, xdr_pmap, &parms,
153                     xdr_u_short, &port, tottimeout) != RPC_SUCCESS){
154                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
155                         clnt_geterr(client, &rpc_createerr.cf_error);
156                 } else if (port == 0) {
157                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
158                 }
159                 CLNT_DESTROY(client);
160         }
161         if (sock != -1)
162                 (void)close(sock);
163         address->sin_port = 0;
164         return (port);
165 }
166
167 /*
168  * Transmit to YPPROC_DOMAIN_NONACK, return immediately.
169  */
170 static bool_t *
171 ypproc_domain_nonack_2_send(domainname *argp, CLIENT *clnt)
172 {
173         static bool_t clnt_res;
174         struct timeval TIMEOUT = { 0, 0 };
175
176         memset((char *)&clnt_res, 0, sizeof (clnt_res));
177         if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
178                 (xdrproc_t) xdr_domainname, (caddr_t) argp,
179                 (xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
180                 TIMEOUT) != RPC_SUCCESS) {
181                 return (NULL);
182         }
183         return (&clnt_res);
184 }
185
186 /*
187  * Receive response from YPPROC_DOMAIN_NONACK asynchronously.
188  */
189 static bool_t *
190 ypproc_domain_nonack_2_recv(domainname *argp, CLIENT *clnt)
191 {
192         static bool_t clnt_res;
193         struct timeval TIMEOUT = { 0, 0 };
194
195         memset((char *)&clnt_res, 0, sizeof (clnt_res));
196         if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
197                 (xdrproc_t) NULL, (caddr_t) argp,
198                 (xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
199                 TIMEOUT) != RPC_SUCCESS) {
200                 return (NULL);
201         }
202         return (&clnt_res);
203 }
204
205 /*
206  * "We have the machine that goes 'ping!'" -- Monty Python
207  *
208  * This function blasts packets at the YPPROC_DOMAIN_NONACK procedures
209  * of the NIS servers listed in restricted_addrs structure.
210  * Whoever replies the fastest becomes our chosen server.
211  *
212  * Note: THIS IS NOT A BROADCAST OPERATION! We could use clnt_broadcast()
213  * for this, but that has the following problems:
214  * - We only get the address of the machine that replied in the
215  *   'eachresult' callback, and on multi-homed machines this can
216  *   lead to confusion.
217  * - clnt_broadcast() only transmits to local networks, whereas with
218  *   NIS+ you can have a perfectly good server located anywhere on or
219  *   off the local network.
220  * - clnt_broadcast() blocks for an arbitrary amount of time which the
221  *   caller can't control -- we want to avoid that.
222  *
223  * Also note that this has nothing to do with the NIS_PING procedure used
224  * for replica updates.
225  */
226
227 struct ping_req {
228         struct sockaddr_in      sin;
229         unsigned long           xid;
230 };
231
232 int __yp_ping(restricted_addrs, cnt, dom, port)
233         struct in_addr          *restricted_addrs;
234         int                     cnt;
235         char                    *dom;
236         short                   *port;
237 {
238         struct timeval          tv = { 5 , 0 };
239         struct ping_req         **reqs;
240         unsigned long           i;
241         int                     async;
242         struct sockaddr_in      sin, *any = NULL;
243         int                     winner = -1;
244         time_t                  xid_seed, xid_lookup;
245         int                     sock, dontblock = 1;
246         CLIENT                  *clnt;
247         char                    *foo = dom;
248         struct cu_data          *cu;
249         int                     validsrvs = 0;
250
251         /* Set up handles. */
252         reqs = calloc(1, sizeof(struct ping_req *) * cnt);
253         xid_seed = time(NULL) ^ getpid();
254
255         for (i = 0; i < cnt; i++) {
256                 bzero((char *)&sin, sizeof(sin));
257                 sin.sin_family = AF_INET;
258                 bcopy((char *)&restricted_addrs[i],
259                         (char *)&sin.sin_addr, sizeof(struct in_addr));
260                 sin.sin_port = htons(__pmap_getport(&sin, YPPROG,
261                                         YPVERS, IPPROTO_UDP));
262                 if (sin.sin_port == 0)
263                         continue;
264                 reqs[i] = calloc(1, sizeof(struct ping_req));
265                 bcopy((char *)&sin, (char *)&reqs[i]->sin, sizeof(sin));
266                 any = &reqs[i]->sin;
267                 reqs[i]->xid = xid_seed;
268                 xid_seed++;
269                 validsrvs++;
270         }
271
272         /* Make sure at least one server was assigned */
273         if (!validsrvs) {
274                 free(reqs);
275                 return(-1);
276         }
277
278         /* Create RPC handle */
279         sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
280         clnt = clntudp_create(any, YPPROG, YPVERS, tv, &sock);
281         if (clnt == NULL) {
282                 close(sock);
283                 for (i = 0; i < cnt; i++)
284                         if (reqs[i] != NULL)
285                                 free(reqs[i]);
286                 free(reqs);
287                 return(-1);
288         }
289         clnt->cl_auth = authunix_create_default();
290         cu = (struct cu_data *)clnt->cl_private;
291         tv.tv_sec = 0;
292
293         clnt_control(clnt, CLSET_TIMEOUT, (char *)&tv);
294         async = TRUE;
295         clnt_control(clnt, CLSET_ASYNC, (char *)&async);
296         ioctl(sock, FIONBIO, &dontblock);
297
298         /* Transmit */
299         for (i = 0; i < cnt; i++) {
300                 if (reqs[i] != NULL) {
301                         clnt_control(clnt, CLSET_XID, (char *)&reqs[i]->xid);
302                         bcopy((char *)&reqs[i]->sin, (char *)&cu->cu_raddr,
303                                 sizeof(struct sockaddr_in));
304                         ypproc_domain_nonack_2_send(&foo, clnt);
305                 }
306         }
307
308         /* Receive reply */
309         ypproc_domain_nonack_2_recv(&foo, clnt);
310
311         /* Got a winner -- look him up. */
312         clnt_control(clnt, CLGET_XID, (char *)&xid_lookup);
313         for (i = 0; i < cnt; i++) {
314                 if (reqs[i] != NULL && reqs[i]->xid == xid_lookup) {
315                         winner = i;
316                         *port = reqs[i]->sin.sin_port;
317                 }
318         }
319
320         /* Shut everything down */
321         auth_destroy(clnt->cl_auth);
322         clnt_destroy(clnt);
323         close(sock);
324
325         for (i = 0; i < cnt; i++)
326                 if (reqs[i] != NULL)
327                         free(reqs[i]);
328         free(reqs);
329
330         return(winner);
331 }