2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "port_before.h"
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <arpa/nameser.h>
43 #include <isc/irpmarshall.h>
44 #include <isc/memcluster.h>
50 #include "port_after.h"
56 #define Max(a,b) ((a) > (b) ? (a) : (b))
60 struct irp_p
*girpdata
;
67 static void ho_close(struct irs_ho
*this);
68 static struct hostent
* ho_byname(struct irs_ho
*this, const char *name
);
69 static struct hostent
* ho_byname2(struct irs_ho
*this, const char *name
,
71 static struct hostent
* ho_byaddr(struct irs_ho
*this, const void *addr
,
73 static struct hostent
* ho_next(struct irs_ho
*this);
74 static void ho_rewind(struct irs_ho
*this);
75 static void ho_minimize(struct irs_ho
*this);
77 static void free_host(struct hostent
*ho
);
78 static struct addrinfo
* ho_addrinfo(struct irs_ho
*this, const char *name
,
79 const struct addrinfo
*pai
);
84 * struct irs_ho * irs_irp_ho(struct irs_acc *this)
88 * Initializes the irp_ho module.
93 irs_irp_ho(struct irs_acc
*this) {
97 if (!(ho
= memget(sizeof *ho
))) {
101 memset(ho
, 0x0, sizeof *ho
);
103 if (!(pvt
= memget(sizeof *pvt
))) {
104 memput(ho
, sizeof *ho
);
108 memset(pvt
, 0, sizeof *pvt
);
109 pvt
->girpdata
= this->private;
112 ho
->close
= ho_close
;
113 ho
->byname
= ho_byname
;
114 ho
->byname2
= ho_byname2
;
115 ho
->byaddr
= ho_byaddr
;
117 ho
->rewind
= ho_rewind
;
118 ho
->minimize
= ho_minimize
;
119 ho
->addrinfo
= ho_addrinfo
;
127 * Closes down the module.
132 ho_close(struct irs_ho
*this) {
133 struct pvt
*pvt
= (struct pvt
*)this->private;
137 free_host(&pvt
->host
);
139 memput(pvt
, sizeof *pvt
);
140 memput(this, sizeof *this);
146 * struct hostent * ho_byname(struct irs_ho *this, const char *name)
150 static struct hostent
*
151 ho_byname(struct irs_ho
*this, const char *name
) {
152 return (ho_byname2(this, name
, AF_INET
));
160 * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af)
164 static struct hostent
*
165 ho_byname2(struct irs_ho
*this, const char *name
, int af
) {
166 struct pvt
*pvt
= (struct pvt
*)this->private;
167 struct hostent
*ho
= &pvt
->host
;
173 if (ho
->h_name
!= NULL
&&
174 strcmp(name
, ho
->h_name
) == 0 &&
175 af
== ho
->h_addrtype
) {
179 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
183 if (irs_irp_send_command(pvt
->girpdata
, "gethostbyname2 %s %s",
184 name
, ADDR_T_STR(af
)) != 0)
187 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
189 &body
, &bodylen
) != 0) {
193 if (code
== IRPD_GETHOST_OK
) {
195 if (irp_unmarshall_ho(ho
, body
) != 0) {
203 memput(body
, bodylen
);
212 * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
217 static struct hostent
*
218 ho_byaddr(struct irs_ho
*this, const void *addr
, int len
, int af
) {
219 struct pvt
*pvt
= (struct pvt
*)this->private;
220 struct hostent
*ho
= &pvt
->host
;
225 char paddr
[MAXPADDRSIZE
];
228 if (ho
->h_name
!= NULL
&&
229 af
== ho
->h_addrtype
&&
230 len
== ho
->h_length
) {
231 for (p
= ho
->h_addr_list
; *p
!= NULL
; p
++) {
232 if (memcmp(*p
, addr
, len
) == 0)
237 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
241 if (inet_ntop(af
, addr
, paddr
, sizeof paddr
) == NULL
) {
245 if (irs_irp_send_command(pvt
->girpdata
, "gethostbyaddr %s %s",
246 paddr
, ADDR_T_STR(af
)) != 0) {
250 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
252 &body
, &bodylen
) != 0) {
256 if (code
== IRPD_GETHOST_OK
) {
258 if (irp_unmarshall_ho(ho
, body
) != 0) {
266 memput(body
, bodylen
);
273 * The implementation for gethostent(3). The first time it's
274 * called all the data is pulled from the remote(i.e. what
275 * the maximum number of gethostent(3) calls would return)
276 * and that data is cached.
280 static struct hostent
*
281 ho_next(struct irs_ho
*this) {
282 struct pvt
*pvt
= (struct pvt
*)this->private;
283 struct hostent
*ho
= &pvt
->host
;
289 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
293 if (irs_irp_send_command(pvt
->girpdata
, "gethostent") != 0) {
297 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
299 &body
, &bodylen
) != 0) {
303 if (code
== IRPD_GETHOST_OK
) {
305 if (irp_unmarshall_ho(ho
, body
) != 0) {
313 memput(body
, bodylen
);
320 * void ho_rewind(struct irs_ho *this)
325 ho_rewind(struct irs_ho
*this) {
326 struct pvt
*pvt
= (struct pvt
*)this->private;
330 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
334 if (irs_irp_send_command(pvt
->girpdata
, "sethostent") != 0) {
338 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
339 if (code
!= IRPD_GETHOST_SETOK
) {
340 if (irp_log_errors
) {
341 syslog(LOG_WARNING
, "sethostent failed: %s", text
);
349 * void ho_minimize(struct irs_ho *this)
354 ho_minimize(struct irs_ho
*this) {
355 struct pvt
*pvt
= (struct pvt
*)this->private;
357 free_host(&pvt
->host
);
359 irs_irp_disconnect(pvt
->girpdata
);
363 * void free_host(struct hostent *ho)
368 free_host(struct hostent
*ho
) {
377 if (ho
->h_aliases
!= NULL
) {
378 for (p
= ho
->h_aliases
; *p
!= NULL
; p
++)
383 if (ho
->h_addr_list
!= NULL
) {
384 for (p
= ho
->h_addr_list
; *p
!= NULL
; p
++)
386 free(ho
->h_addr_list
);
391 static struct addrinfo
*
392 ho_addrinfo(struct irs_ho
*this, const char *name
, const struct addrinfo
*pai
)