]>
git.saurik.com Git - apple/network_cmds.git/blob - yppush.tproj/yplib_host.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $OpenBSD: yplib_host.c,v 1.7 1997/06/23 01:11:12 deraadt Exp $ */
28 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by Theo de Raadt.
42 * 4. The name of the author may not be used to endorse or promote products
43 * derived from this software without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
46 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
49 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 static char *rcsid
= "$OpenBSD: yplib_host.c,v 1.7 1997/06/23 01:11:12 deraadt Exp $";
62 #include <sys/param.h>
63 #include <sys/types.h>
64 #include <sys/socket.h>
71 #include <netinet/in.h>
72 #include <arpa/inet.h>
73 #include <sys/socket.h>
78 #include <rpcsvc/yp.h>
79 #include <rpcsvc/ypclnt.h>
81 extern bool_t
xdr_domainname(), xdr_ypbind_resp();
82 extern bool_t
xdr_ypreq_key(), xdr_ypresp_val();
83 extern bool_t
xdr_ypreq_nokey(), xdr_ypresp_key_val();
84 extern bool_t
xdr_ypresp_all(), xdr_ypresp_all_seq();
85 extern bool_t
xdr_ypresp_master();
87 extern int (*ypresp_allfn
)();
88 extern void *ypresp_data
;
90 int _yplib_host_timeout
= 10;
93 yp_bind_host(server
,program
,version
,port
,usetcp
)
95 u_long program
,version
;
99 struct sockaddr_in rsrv_sin
;
103 static CLIENT
*client
;
105 memset(&rsrv_sin
, 0, sizeof rsrv_sin
);
106 rsrv_sin
.sin_len
= sizeof rsrv_sin
;
107 rsrv_sin
.sin_family
= AF_INET
;
108 rsrv_sock
= RPC_ANYSOCK
;
110 rsrv_sin
.sin_port
= htons(port
);
113 if ((*server
>= '0') && (*server
<= '9')) {
114 if(inet_aton(server
,&rsrv_sin
.sin_addr
) == 0) {
115 fprintf(stderr
, "inet_aton: invalid address %s.\n",
120 h
= gethostbyname(server
);
122 fprintf(stderr
, "gethostbyname: unknown host %s.\n",
126 rsrv_sin
.sin_addr
.s_addr
= *(u_int32_t
*)h
->h_addr
;
133 client
= clnttcp_create(&rsrv_sin
, program
, version
,
136 client
= clntudp_create(&rsrv_sin
, program
, version
, tv
,
140 if (client
== NULL
) {
141 fprintf(stderr
, "clntudp_create: no contact with host %s.\n",
151 yp_bind_local(program
,version
)
152 u_long program
,version
;
154 struct sockaddr_in rsrv_sin
;
157 static CLIENT
*client
;
159 memset(&rsrv_sin
, 0, sizeof rsrv_sin
);
160 rsrv_sin
.sin_len
= sizeof rsrv_sin
;
161 rsrv_sin
.sin_family
= AF_INET
;
162 rsrv_sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
163 rsrv_sock
= RPC_ANYSOCK
;
168 client
= clntudp_create(&rsrv_sin
, program
, version
, tv
, &rsrv_sock
);
169 if (client
== NULL
) {
170 fprintf(stderr
,"clntudp_create: no contact with localhost.\n");
179 yp_match_host(client
, indomain
, inmap
, inkey
, inkeylen
, outval
, outvallen
)
188 struct ypresp_val yprv
;
190 struct ypreq_key yprk
;
196 tv
.tv_sec
= _yplib_host_timeout
;
199 yprk
.domain
= indomain
;
201 yprk
.key
.keydat_val
= (char *)inkey
;
202 yprk
.key
.keydat_len
= inkeylen
;
204 memset(&yprv
, 0, sizeof yprv
);
206 r
= clnt_call(client
, YPPROC_MATCH
,
207 xdr_ypreq_key
, &yprk
, xdr_ypresp_val
, &yprv
, tv
);
208 if(r
!= RPC_SUCCESS
) {
209 clnt_perror(client
, "yp_match_host: clnt_call");
211 if( !(r
=ypprot_err(yprv
.stat
)) ) {
212 *outvallen
= yprv
.val
.valdat_len
;
213 *outval
= (char *)malloc(*outvallen
+1);
214 memcpy(*outval
, yprv
.val
.valdat_val
, *outvallen
);
215 (*outval
)[*outvallen
] = '\0';
217 xdr_free(xdr_ypresp_val
, (char *)&yprv
);
222 yp_first_host(client
, indomain
, inmap
, outkey
, outkeylen
, outval
, outvallen
)
231 struct ypresp_key_val yprkv
;
232 struct ypreq_nokey yprnk
;
236 *outkey
= *outval
= NULL
;
237 *outkeylen
= *outvallen
= 0;
239 tv
.tv_sec
= _yplib_host_timeout
;
242 yprnk
.domain
= indomain
;
244 memset(&yprkv
, 0, sizeof yprkv
);
246 r
= clnt_call(client
, YPPROC_FIRST
,
247 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_key_val
, &yprkv
, tv
);
248 if(r
!= RPC_SUCCESS
) {
249 clnt_perror(client
, "yp_first_host: clnt_call");
251 if( !(r
=ypprot_err(yprkv
.stat
)) ) {
252 *outkeylen
= yprkv
.key
.keydat_len
;
253 *outkey
= (char *)malloc(*outkeylen
+1);
254 memcpy(*outkey
, yprkv
.key
.keydat_val
, *outkeylen
);
255 (*outkey
)[*outkeylen
] = '\0';
256 *outvallen
= yprkv
.val
.valdat_len
;
257 *outval
= (char *)malloc(*outvallen
+1);
258 memcpy(*outval
, yprkv
.val
.valdat_val
, *outvallen
);
259 (*outval
)[*outvallen
] = '\0';
261 xdr_free(xdr_ypresp_key_val
, (char *)&yprkv
);
266 yp_next_host(client
, indomain
, inmap
, inkey
, inkeylen
, outkey
, outkeylen
, outval
, outvallen
)
277 struct ypresp_key_val yprkv
;
278 struct ypreq_key yprk
;
282 *outkey
= *outval
= NULL
;
283 *outkeylen
= *outvallen
= 0;
285 tv
.tv_sec
= _yplib_host_timeout
;
288 yprk
.domain
= indomain
;
290 yprk
.key
.keydat_val
= inkey
;
291 yprk
.key
.keydat_len
= inkeylen
;
292 memset(&yprkv
, 0, sizeof yprkv
);
294 r
= clnt_call(client
, YPPROC_NEXT
,
295 xdr_ypreq_key
, &yprk
, xdr_ypresp_key_val
, &yprkv
, tv
);
296 if(r
!= RPC_SUCCESS
) {
297 clnt_perror(client
, "yp_next_host: clnt_call");
299 if( !(r
=ypprot_err(yprkv
.stat
)) ) {
300 *outkeylen
= yprkv
.key
.keydat_len
;
301 *outkey
= (char *)malloc(*outkeylen
+1);
302 memcpy(*outkey
, yprkv
.key
.keydat_val
, *outkeylen
);
303 (*outkey
)[*outkeylen
] = '\0';
304 *outvallen
= yprkv
.val
.valdat_len
;
305 *outval
= (char *)malloc(*outvallen
+1);
306 memcpy(*outval
, yprkv
.val
.valdat_val
, *outvallen
);
307 (*outval
)[*outvallen
] = '\0';
309 xdr_free(xdr_ypresp_key_val
, (char *)&yprkv
);
314 yp_all_host(client
, indomain
, inmap
, incallback
)
318 struct ypall_callback
*incallback
;
320 struct ypreq_nokey yprnk
;
324 tv
.tv_sec
= _yplib_host_timeout
;
327 yprnk
.domain
= indomain
;
329 ypresp_allfn
= incallback
->foreach
;
330 ypresp_data
= (void *)incallback
->data
;
332 (void) clnt_call(client
, YPPROC_ALL
,
333 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_all_seq
, &status
, tv
);
334 xdr_free(xdr_ypresp_all_seq
, (char *)&status
); /* not really needed... */
336 if(status
!= YP_FALSE
)
337 return ypprot_err(status
);
342 yp_order_host(client
, indomain
, inmap
, outorder
)
348 struct ypresp_order ypro
;
349 struct ypreq_nokey yprnk
;
353 tv
.tv_sec
= _yplib_host_timeout
;
356 yprnk
.domain
= indomain
;
359 memset(&ypro
, 0, sizeof ypro
);
361 r
= clnt_call(client
, YPPROC_ORDER
,
362 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_order
, &ypro
, tv
);
363 if(r
!= RPC_SUCCESS
) {
364 clnt_perror(client
, "yp_order_host: clnt_call");
367 *outorder
= ypro
.ordernum
;
368 xdr_free(xdr_ypresp_order
, (char *)&ypro
);
369 return ypprot_err(ypro
.stat
);
373 yp_master_host(client
, indomain
, inmap
, outname
)
379 struct ypresp_master yprm
;
380 struct ypreq_nokey yprnk
;
384 tv
.tv_sec
= _yplib_host_timeout
;
387 yprnk
.domain
= indomain
;
390 memset(&yprm
, 0, sizeof yprm
);
392 r
= clnt_call(client
, YPPROC_MASTER
,
393 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_master
, &yprm
, tv
);
394 if(r
!= RPC_SUCCESS
) {
395 clnt_perror(client
, "yp_master: clnt_call");
397 if( !(r
=ypprot_err(yprm
.stat
)) ) {
398 *outname
= (char *)strdup(yprm
.peer
);
400 xdr_free(xdr_ypresp_master
, (char *)&yprm
);
405 yp_maplist_host(client
, indomain
, outmaplist
)
408 struct ypmaplist
**outmaplist
;
410 struct ypresp_maplist ypml
;
414 tv
.tv_sec
= _yplib_host_timeout
;
417 memset(&ypml
, 0, sizeof ypml
);
419 r
= clnt_call(client
, YPPROC_MAPLIST
,
420 xdr_domainname
, &indomain
, xdr_ypresp_maplist
, &ypml
, tv
);
421 if (r
!= RPC_SUCCESS
) {
422 clnt_perror(client
, "yp_maplist: clnt_call");
424 *outmaplist
= ypml
.maps
;
425 /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
426 return ypprot_err(ypml
.stat
);