]>
git.saurik.com Git - apple/network_cmds.git/blob - ypxfr.tproj/yplib_host.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.0 (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
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
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: yplib_host.c,v 1.7 1997/06/23 01:11:12 deraadt Exp $ */
27 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Theo de Raadt.
41 * 4. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
45 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
48 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 static char *rcsid
= "$OpenBSD: yplib_host.c,v 1.7 1997/06/23 01:11:12 deraadt Exp $";
61 #include <sys/param.h>
62 #include <sys/types.h>
63 #include <sys/socket.h>
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
72 #include <sys/socket.h>
77 #include <rpcsvc/yp.h>
78 #include <rpcsvc/ypclnt.h>
80 extern bool_t
xdr_domainname(), xdr_ypbind_resp();
81 extern bool_t
xdr_ypreq_key(), xdr_ypresp_val();
82 extern bool_t
xdr_ypreq_nokey(), xdr_ypresp_key_val();
83 extern bool_t
xdr_ypresp_all(), xdr_ypresp_all_seq();
84 extern bool_t
xdr_ypresp_master();
86 extern int (*ypresp_allfn
)();
87 extern void *ypresp_data
;
89 int _yplib_host_timeout
= 10;
92 yp_bind_host(server
,program
,version
,port
,usetcp
)
94 u_long program
,version
;
98 struct sockaddr_in rsrv_sin
;
102 static CLIENT
*client
;
104 memset(&rsrv_sin
, 0, sizeof rsrv_sin
);
105 rsrv_sin
.sin_len
= sizeof rsrv_sin
;
106 rsrv_sin
.sin_family
= AF_INET
;
107 rsrv_sock
= RPC_ANYSOCK
;
109 rsrv_sin
.sin_port
= htons(port
);
112 if ((*server
>= '0') && (*server
<= '9')) {
113 if(inet_aton(server
,&rsrv_sin
.sin_addr
) == 0) {
114 fprintf(stderr
, "inet_aton: invalid address %s.\n",
119 h
= gethostbyname(server
);
121 fprintf(stderr
, "gethostbyname: unknown host %s.\n",
125 rsrv_sin
.sin_addr
.s_addr
= *(u_int32_t
*)h
->h_addr
;
132 client
= clnttcp_create(&rsrv_sin
, program
, version
,
135 client
= clntudp_create(&rsrv_sin
, program
, version
, tv
,
139 if (client
== NULL
) {
140 fprintf(stderr
, "clntudp_create: no contact with host %s.\n",
150 yp_bind_local(program
,version
)
151 u_long program
,version
;
153 struct sockaddr_in rsrv_sin
;
156 static CLIENT
*client
;
158 memset(&rsrv_sin
, 0, sizeof rsrv_sin
);
159 rsrv_sin
.sin_len
= sizeof rsrv_sin
;
160 rsrv_sin
.sin_family
= AF_INET
;
161 rsrv_sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
162 rsrv_sock
= RPC_ANYSOCK
;
167 client
= clntudp_create(&rsrv_sin
, program
, version
, tv
, &rsrv_sock
);
168 if (client
== NULL
) {
169 fprintf(stderr
,"clntudp_create: no contact with localhost.\n");
178 yp_match_host(client
, indomain
, inmap
, inkey
, inkeylen
, outval
, outvallen
)
187 struct ypresp_val yprv
;
189 struct ypreq_key yprk
;
195 tv
.tv_sec
= _yplib_host_timeout
;
198 yprk
.domain
= indomain
;
200 yprk
.key
.keydat_val
= (char *)inkey
;
201 yprk
.key
.keydat_len
= inkeylen
;
203 memset(&yprv
, 0, sizeof yprv
);
205 r
= clnt_call(client
, YPPROC_MATCH
,
206 xdr_ypreq_key
, &yprk
, xdr_ypresp_val
, &yprv
, tv
);
207 if(r
!= RPC_SUCCESS
) {
208 clnt_perror(client
, "yp_match_host: clnt_call");
210 if( !(r
=ypprot_err(yprv
.stat
)) ) {
211 *outvallen
= yprv
.val
.valdat_len
;
212 *outval
= (char *)malloc(*outvallen
+1);
213 memcpy(*outval
, yprv
.val
.valdat_val
, *outvallen
);
214 (*outval
)[*outvallen
] = '\0';
216 xdr_free(xdr_ypresp_val
, (char *)&yprv
);
221 yp_first_host(client
, indomain
, inmap
, outkey
, outkeylen
, outval
, outvallen
)
230 struct ypresp_key_val yprkv
;
231 struct ypreq_nokey yprnk
;
235 *outkey
= *outval
= NULL
;
236 *outkeylen
= *outvallen
= 0;
238 tv
.tv_sec
= _yplib_host_timeout
;
241 yprnk
.domain
= indomain
;
243 memset(&yprkv
, 0, sizeof yprkv
);
245 r
= clnt_call(client
, YPPROC_FIRST
,
246 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_key_val
, &yprkv
, tv
);
247 if(r
!= RPC_SUCCESS
) {
248 clnt_perror(client
, "yp_first_host: clnt_call");
250 if( !(r
=ypprot_err(yprkv
.stat
)) ) {
251 *outkeylen
= yprkv
.key
.keydat_len
;
252 *outkey
= (char *)malloc(*outkeylen
+1);
253 memcpy(*outkey
, yprkv
.key
.keydat_val
, *outkeylen
);
254 (*outkey
)[*outkeylen
] = '\0';
255 *outvallen
= yprkv
.val
.valdat_len
;
256 *outval
= (char *)malloc(*outvallen
+1);
257 memcpy(*outval
, yprkv
.val
.valdat_val
, *outvallen
);
258 (*outval
)[*outvallen
] = '\0';
260 xdr_free(xdr_ypresp_key_val
, (char *)&yprkv
);
265 yp_next_host(client
, indomain
, inmap
, inkey
, inkeylen
, outkey
, outkeylen
, outval
, outvallen
)
276 struct ypresp_key_val yprkv
;
277 struct ypreq_key yprk
;
281 *outkey
= *outval
= NULL
;
282 *outkeylen
= *outvallen
= 0;
284 tv
.tv_sec
= _yplib_host_timeout
;
287 yprk
.domain
= indomain
;
289 yprk
.key
.keydat_val
= inkey
;
290 yprk
.key
.keydat_len
= inkeylen
;
291 memset(&yprkv
, 0, sizeof yprkv
);
293 r
= clnt_call(client
, YPPROC_NEXT
,
294 xdr_ypreq_key
, &yprk
, xdr_ypresp_key_val
, &yprkv
, tv
);
295 if(r
!= RPC_SUCCESS
) {
296 clnt_perror(client
, "yp_next_host: clnt_call");
298 if( !(r
=ypprot_err(yprkv
.stat
)) ) {
299 *outkeylen
= yprkv
.key
.keydat_len
;
300 *outkey
= (char *)malloc(*outkeylen
+1);
301 memcpy(*outkey
, yprkv
.key
.keydat_val
, *outkeylen
);
302 (*outkey
)[*outkeylen
] = '\0';
303 *outvallen
= yprkv
.val
.valdat_len
;
304 *outval
= (char *)malloc(*outvallen
+1);
305 memcpy(*outval
, yprkv
.val
.valdat_val
, *outvallen
);
306 (*outval
)[*outvallen
] = '\0';
308 xdr_free(xdr_ypresp_key_val
, (char *)&yprkv
);
313 yp_all_host(client
, indomain
, inmap
, incallback
)
317 struct ypall_callback
*incallback
;
319 struct ypreq_nokey yprnk
;
323 tv
.tv_sec
= _yplib_host_timeout
;
326 yprnk
.domain
= indomain
;
328 ypresp_allfn
= incallback
->foreach
;
329 ypresp_data
= (void *)incallback
->data
;
331 (void) clnt_call(client
, YPPROC_ALL
,
332 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_all_seq
, &status
, tv
);
333 xdr_free(xdr_ypresp_all_seq
, (char *)&status
); /* not really needed... */
335 if(status
!= YP_FALSE
)
336 return ypprot_err(status
);
341 yp_order_host(client
, indomain
, inmap
, outorder
)
347 struct ypresp_order ypro
;
348 struct ypreq_nokey yprnk
;
352 tv
.tv_sec
= _yplib_host_timeout
;
355 yprnk
.domain
= indomain
;
358 memset(&ypro
, 0, sizeof ypro
);
360 r
= clnt_call(client
, YPPROC_ORDER
,
361 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_order
, &ypro
, tv
);
362 if(r
!= RPC_SUCCESS
) {
363 clnt_perror(client
, "yp_order_host: clnt_call");
366 *outorder
= ypro
.ordernum
;
367 xdr_free(xdr_ypresp_order
, (char *)&ypro
);
368 return ypprot_err(ypro
.stat
);
372 yp_master_host(client
, indomain
, inmap
, outname
)
378 struct ypresp_master yprm
;
379 struct ypreq_nokey yprnk
;
383 tv
.tv_sec
= _yplib_host_timeout
;
386 yprnk
.domain
= indomain
;
389 memset(&yprm
, 0, sizeof yprm
);
391 r
= clnt_call(client
, YPPROC_MASTER
,
392 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_master
, &yprm
, tv
);
393 if(r
!= RPC_SUCCESS
) {
394 clnt_perror(client
, "yp_master: clnt_call");
396 if( !(r
=ypprot_err(yprm
.stat
)) ) {
397 *outname
= (char *)strdup(yprm
.peer
);
399 xdr_free(xdr_ypresp_master
, (char *)&yprm
);
404 yp_maplist_host(client
, indomain
, outmaplist
)
407 struct ypmaplist
**outmaplist
;
409 struct ypresp_maplist ypml
;
413 tv
.tv_sec
= _yplib_host_timeout
;
416 memset(&ypml
, 0, sizeof ypml
);
418 r
= clnt_call(client
, YPPROC_MAPLIST
,
419 xdr_domainname
, &indomain
, xdr_ypresp_maplist
, &ypml
, tv
);
420 if (r
!= RPC_SUCCESS
) {
421 clnt_perror(client
, "yp_maplist: clnt_call");
423 *outmaplist
= ypml
.maps
;
424 /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
425 return ypprot_err(ypml
.stat
);