1 /* $NetBSD: yplib_host.c,v 1.7 2004/10/30 16:01:48 dsl Exp $ */
4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: yplib_host.c,v 1.7 2004/10/30 16:01:48 dsl Exp $");
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
54 #include <rpcsvc/yp_prot.h>
55 #include <rpcsvc/ypclnt.h>
57 #include "yplib_host.h"
59 struct timeval _yplib_host_timeout
= { 10, 0 };
62 yp_bind_host(char *server
, u_int program
, u_int version
, u_short port
,
65 struct sockaddr_in rsrv_sin
;
68 static CLIENT
*client
;
70 memset(&rsrv_sin
, 0, sizeof rsrv_sin
);
71 rsrv_sin
.sin_len
= sizeof rsrv_sin
;
72 rsrv_sin
.sin_family
= AF_INET
;
73 rsrv_sock
= RPC_ANYSOCK
;
75 rsrv_sin
.sin_port
= htons(port
);
78 if (isdigit((unsigned char)*server
)) {
79 if (inet_aton(server
,&rsrv_sin
.sin_addr
) == 0) {
80 errx(1, "invalid IP address `%s'", server
);
83 h
= gethostbyname(server
);
85 errx(1, "unknown host `%s'", server
);
87 memcpy(&rsrv_sin
.sin_addr
.s_addr
, h
->h_addr_list
[0],
92 client
= clnttcp_create(&rsrv_sin
, program
, version
,
95 client
= clntudp_create(&rsrv_sin
, program
, version
,
96 _yplib_host_timeout
, &rsrv_sock
);
99 errx(1, "%s: no contact with host `%s'",
100 usetcp
? "clnttcp_create" : "clntudp_create", server
);
106 yp_bind_local(u_int program
, u_int version
)
108 struct sockaddr_in rsrv_sin
;
110 static CLIENT
*client
;
112 memset(&rsrv_sin
, 0, sizeof rsrv_sin
);
113 rsrv_sin
.sin_len
= sizeof rsrv_sin
;
114 rsrv_sin
.sin_family
= AF_INET
;
115 rsrv_sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
116 rsrv_sock
= RPC_ANYSOCK
;
118 client
= clntudp_create(&rsrv_sin
, program
, version
,
119 _yplib_host_timeout
, &rsrv_sock
);
121 errx(1, "clntudp_create: no contact with localhost");
127 yp_match_host(CLIENT
*client
, char *indomain
, char *inmap
, const char *inkey
,
128 int inkeylen
, char **outval
, int *outvallen
)
130 struct ypresp_val yprv
;
131 struct ypreq_key yprk
;
137 yprk
.domain
= indomain
;
139 yprk
.keydat
.dptr
= __UNCONST(inkey
);
140 yprk
.keydat
.dsize
= inkeylen
;
142 memset(&yprv
, 0, sizeof yprv
);
144 r
= clnt_call(client
, YPPROC_MATCH
, xdr_ypreq_key
, &yprk
,
145 xdr_ypresp_val
, &yprv
, _yplib_host_timeout
);
147 clnt_perror(client
, "yp_match_host: clnt_call");
149 if(!(r
=ypprot_err(yprv
.status
)) ) {
150 *outvallen
= yprv
.valdat
.dsize
;
151 *outval
= (char *)malloc(*outvallen
+1);
152 memcpy(*outval
, yprv
.valdat
.dptr
, *outvallen
);
153 (*outval
)[*outvallen
] = '\0';
155 xdr_free(xdr_ypresp_val
, (char *)&yprv
);
160 yp_first_host(CLIENT
*client
, char *indomain
, char *inmap
, char **outkey
,
161 int *outkeylen
, char **outval
, int *outvallen
)
163 struct ypresp_key_val yprkv
;
164 struct ypreq_nokey yprnk
;
167 *outkey
= *outval
= NULL
;
168 *outkeylen
= *outvallen
= 0;
170 yprnk
.domain
= indomain
;
172 memset(&yprkv
, 0, sizeof yprkv
);
174 r
= clnt_call(client
, YPPROC_FIRST
, xdr_ypreq_nokey
, &yprnk
,
175 xdr_ypresp_key_val
, &yprkv
, _yplib_host_timeout
);
176 if (r
!= RPC_SUCCESS
)
177 clnt_perror(client
, "yp_first_host: clnt_call");
179 if(!(r
=ypprot_err(yprkv
.status
)) ) {
180 *outkeylen
= yprkv
.keydat
.dsize
;
181 *outkey
= (char *)malloc(*outkeylen
+1);
182 memcpy(*outkey
, yprkv
.keydat
.dptr
, *outkeylen
);
183 (*outkey
)[*outkeylen
] = '\0';
184 *outvallen
= yprkv
.valdat
.dsize
;
185 *outval
= (char *)malloc(*outvallen
+1);
186 memcpy(*outval
, yprkv
.valdat
.dptr
, *outvallen
);
187 (*outval
)[*outvallen
] = '\0';
189 xdr_free(xdr_ypresp_key_val
, (char *)&yprkv
);
194 yp_next_host(CLIENT
*client
, char *indomain
, char *inmap
, char *inkey
,
195 int inkeylen
, char **outkey
, int *outkeylen
, char **outval
,
198 struct ypresp_key_val yprkv
;
199 struct ypreq_key yprk
;
202 *outkey
= *outval
= NULL
;
203 *outkeylen
= *outvallen
= 0;
205 yprk
.domain
= indomain
;
207 yprk
.keydat
.dptr
= inkey
;
208 yprk
.keydat
.dsize
= inkeylen
;
209 memset(&yprkv
, 0, sizeof yprkv
);
211 r
= clnt_call(client
, YPPROC_NEXT
, xdr_ypreq_key
, &yprk
,
212 xdr_ypresp_key_val
, &yprkv
, _yplib_host_timeout
);
213 if (r
!= RPC_SUCCESS
)
214 clnt_perror(client
, "yp_next_host: clnt_call");
216 if(!(r
=ypprot_err(yprkv
.status
)) ) {
217 *outkeylen
= yprkv
.keydat
.dsize
;
218 *outkey
= (char *)malloc(*outkeylen
+1);
219 memcpy(*outkey
, yprkv
.keydat
.dptr
, *outkeylen
);
220 (*outkey
)[*outkeylen
] = '\0';
221 *outvallen
= yprkv
.valdat
.dsize
;
222 *outval
= (char *)malloc(*outvallen
+1);
223 memcpy(*outval
, yprkv
.valdat
.dptr
, *outvallen
);
224 (*outval
)[*outvallen
] = '\0';
226 xdr_free(xdr_ypresp_key_val
, (char *)&yprkv
);
231 yp_all_host(CLIENT
*client
, const char *indomain
, const char *inmap
,
232 struct ypall_callback
*incallback
)
234 struct ypreq_nokey yprnk
;
237 yprnk
.domain
= indomain
;
240 status
= clnt_call(client
, YPPROC_ALL
, xdr_ypreq_nokey
, &yprnk
,
241 xdr_ypall
, (char *)incallback
, _yplib_host_timeout
);
243 if (status
!= RPC_SUCCESS
)
250 yp_order_host(CLIENT
*client
, char *indomain
, char *inmap
, int *outorder
)
252 struct ypresp_order ypro
;
253 struct ypreq_nokey yprnk
;
256 yprnk
.domain
= indomain
;
259 memset(&ypro
, 0, sizeof ypro
);
261 r
= clnt_call(client
, YPPROC_ORDER
, xdr_ypreq_nokey
, &yprnk
,
262 xdr_ypresp_order
, &ypro
, _yplib_host_timeout
);
263 if (r
!= RPC_SUCCESS
)
264 clnt_perror(client
, "yp_order_host: clnt_call");
266 *outorder
= ypro
.ordernum
;
267 xdr_free(xdr_ypresp_order
, (char *)&ypro
);
268 return ypprot_err(ypro
.status
);
272 yp_master_host(CLIENT
*client
, char *indomain
, char *inmap
, char **outname
)
274 struct ypresp_master yprm
;
275 struct ypreq_nokey yprnk
;
278 yprnk
.domain
= indomain
;
281 memset(&yprm
, 0, sizeof yprm
);
283 r
= clnt_call(client
, YPPROC_MASTER
, xdr_ypreq_nokey
, &yprnk
,
284 xdr_ypresp_master
, &yprm
, _yplib_host_timeout
);
285 if (r
!= RPC_SUCCESS
)
286 clnt_perror(client
, "yp_master: clnt_call");
288 if (!(r
= ypprot_err(yprm
.status
))) {
289 *outname
= (char *)strdup(yprm
.master
);
291 xdr_free(xdr_ypresp_master
, (char *)&yprm
);
296 yp_maplist_host(CLIENT
*client
, char *indomain
, struct ypmaplist
**outmaplist
)
298 struct ypresp_maplist ypml
;
301 memset(&ypml
, 0, sizeof ypml
);
303 r
= clnt_call(client
, YPPROC_MAPLIST
, xdr_ypdomain_wrap_string
,
304 indomain
, xdr_ypresp_maplist
, &ypml
, _yplib_host_timeout
);
305 if (r
!= RPC_SUCCESS
)
306 clnt_perror(client
, "yp_maplist: clnt_call");
308 *outmaplist
= ypml
.list
;
309 /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/
310 return ypprot_err(ypml
.status
);