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.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid
[] = "$Id: irp_ho.c,v 1.3 2005/04/27 04:56:28 sra Exp $";
20 #endif /* LIBC_SCCS and not lint */
24 #include "port_before.h"
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
47 #include <isc/irpmarshall.h>
48 #include <isc/memcluster.h>
54 #include "port_after.h"
60 #define Max(a,b) ((a) > (b) ? (a) : (b))
64 struct irp_p
*girpdata
;
71 static void ho_close(struct irs_ho
*this);
72 static struct hostent
* ho_byname(struct irs_ho
*this, const char *name
);
73 static struct hostent
* ho_byname2(struct irs_ho
*this, const char *name
,
75 static struct hostent
* ho_byaddr(struct irs_ho
*this, const void *addr
,
77 static struct hostent
* ho_next(struct irs_ho
*this);
78 static void ho_rewind(struct irs_ho
*this);
79 static void ho_minimize(struct irs_ho
*this);
81 static void free_host(struct hostent
*ho
);
82 static struct addrinfo
* ho_addrinfo(struct irs_ho
*this, const char *name
,
83 const struct addrinfo
*pai
);
88 * struct irs_ho * irs_irp_ho(struct irs_acc *this)
92 * Initializes the irp_ho module.
97 irs_irp_ho(struct irs_acc
*this) {
101 if (!(ho
= memget(sizeof *ho
))) {
105 memset(ho
, 0x0, sizeof *ho
);
107 if (!(pvt
= memget(sizeof *pvt
))) {
108 memput(ho
, sizeof *ho
);
112 memset(pvt
, 0, sizeof *pvt
);
113 pvt
->girpdata
= this->private;
116 ho
->close
= ho_close
;
117 ho
->byname
= ho_byname
;
118 ho
->byname2
= ho_byname2
;
119 ho
->byaddr
= ho_byaddr
;
121 ho
->rewind
= ho_rewind
;
122 ho
->minimize
= ho_minimize
;
123 ho
->addrinfo
= ho_addrinfo
;
131 * Closes down the module.
136 ho_close(struct irs_ho
*this) {
137 struct pvt
*pvt
= (struct pvt
*)this->private;
141 free_host(&pvt
->host
);
143 memput(pvt
, sizeof *pvt
);
144 memput(this, sizeof *this);
150 * struct hostent * ho_byname(struct irs_ho *this, const char *name)
154 static struct hostent
*
155 ho_byname(struct irs_ho
*this, const char *name
) {
156 return (ho_byname2(this, name
, AF_INET
));
164 * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af)
168 static struct hostent
*
169 ho_byname2(struct irs_ho
*this, const char *name
, int af
) {
170 struct pvt
*pvt
= (struct pvt
*)this->private;
171 struct hostent
*ho
= &pvt
->host
;
177 if (ho
->h_name
!= NULL
&&
178 strcmp(name
, ho
->h_name
) == 0 &&
179 af
== ho
->h_addrtype
) {
183 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
187 if (irs_irp_send_command(pvt
->girpdata
, "gethostbyname2 %s %s",
188 name
, ADDR_T_STR(af
)) != 0)
191 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
193 &body
, &bodylen
) != 0) {
197 if (code
== IRPD_GETHOST_OK
) {
199 if (irp_unmarshall_ho(ho
, body
) != 0) {
207 memput(body
, bodylen
);
216 * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
221 static struct hostent
*
222 ho_byaddr(struct irs_ho
*this, const void *addr
, int len
, int af
) {
223 struct pvt
*pvt
= (struct pvt
*)this->private;
224 struct hostent
*ho
= &pvt
->host
;
229 char paddr
[MAXPADDRSIZE
];
232 if (ho
->h_name
!= NULL
&&
233 af
== ho
->h_addrtype
&&
234 len
== ho
->h_length
) {
235 for (p
= ho
->h_addr_list
; *p
!= NULL
; p
++) {
236 if (memcmp(*p
, addr
, len
) == 0)
241 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
245 if (inet_ntop(af
, addr
, paddr
, sizeof paddr
) == NULL
) {
249 if (irs_irp_send_command(pvt
->girpdata
, "gethostbyaddr %s %s",
250 paddr
, ADDR_T_STR(af
)) != 0) {
254 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
256 &body
, &bodylen
) != 0) {
260 if (code
== IRPD_GETHOST_OK
) {
262 if (irp_unmarshall_ho(ho
, body
) != 0) {
270 memput(body
, bodylen
);
277 * The implementation for gethostent(3). The first time it's
278 * called all the data is pulled from the remote(i.e. what
279 * the maximum number of gethostent(3) calls would return)
280 * and that data is cached.
284 static struct hostent
*
285 ho_next(struct irs_ho
*this) {
286 struct pvt
*pvt
= (struct pvt
*)this->private;
287 struct hostent
*ho
= &pvt
->host
;
293 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
297 if (irs_irp_send_command(pvt
->girpdata
, "gethostent") != 0) {
301 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
303 &body
, &bodylen
) != 0) {
307 if (code
== IRPD_GETHOST_OK
) {
309 if (irp_unmarshall_ho(ho
, body
) != 0) {
317 memput(body
, bodylen
);
324 * void ho_rewind(struct irs_ho *this)
329 ho_rewind(struct irs_ho
*this) {
330 struct pvt
*pvt
= (struct pvt
*)this->private;
334 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
338 if (irs_irp_send_command(pvt
->girpdata
, "sethostent") != 0) {
342 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
343 if (code
!= IRPD_GETHOST_SETOK
) {
344 if (irp_log_errors
) {
345 syslog(LOG_WARNING
, "sethostent failed: %s", text
);
353 * void ho_minimize(struct irs_ho *this)
358 ho_minimize(struct irs_ho
*this) {
359 struct pvt
*pvt
= (struct pvt
*)this->private;
361 free_host(&pvt
->host
);
363 irs_irp_disconnect(pvt
->girpdata
);
367 * void free_host(struct hostent *ho)
372 free_host(struct hostent
*ho
) {
381 if (ho
->h_aliases
!= NULL
) {
382 for (p
= ho
->h_aliases
; *p
!= NULL
; p
++)
387 if (ho
->h_addr_list
!= NULL
) {
388 for (p
= ho
->h_addr_list
; *p
!= NULL
; p
++)
390 free(ho
->h_addr_list
);
395 static struct addrinfo
*
396 ho_addrinfo(struct irs_ho
*this, const char *name
, const struct addrinfo
*pai
)