4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: irp_ho.c,v 1.3 2005/04/27 04:56:28 sra Exp";
22 #endif /* LIBC_SCCS and not lint */
26 #include "port_before.h"
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <arpa/nameser.h>
49 #include <isc/irpmarshall.h>
50 #include <isc/memcluster.h>
56 #include "port_after.h"
62 #define Max(a,b) ((a) > (b) ? (a) : (b))
66 struct irp_p
*girpdata
;
73 static void ho_close(struct irs_ho
*this);
74 static struct hostent
* ho_byname(struct irs_ho
*this, const char *name
);
75 static struct hostent
* ho_byname2(struct irs_ho
*this, const char *name
,
77 static struct hostent
* ho_byaddr(struct irs_ho
*this, const void *addr
,
79 static struct hostent
* ho_next(struct irs_ho
*this);
80 static void ho_rewind(struct irs_ho
*this);
81 static void ho_minimize(struct irs_ho
*this);
83 static void free_host(struct hostent
*ho
);
84 static struct addrinfo
* ho_addrinfo(struct irs_ho
*this, const char *name
,
85 const struct addrinfo
*pai
);
90 * struct irs_ho * irs_irp_ho(struct irs_acc *this)
94 * Initializes the irp_ho module.
99 irs_irp_ho(struct irs_acc
*this) {
103 if (!(ho
= memget(sizeof *ho
))) {
107 memset(ho
, 0x0, sizeof *ho
);
109 if (!(pvt
= memget(sizeof *pvt
))) {
110 memput(ho
, sizeof *ho
);
114 memset(pvt
, 0, sizeof *pvt
);
115 pvt
->girpdata
= this->private;
118 ho
->close
= ho_close
;
119 ho
->byname
= ho_byname
;
120 ho
->byname2
= ho_byname2
;
121 ho
->byaddr
= ho_byaddr
;
123 ho
->rewind
= ho_rewind
;
124 ho
->minimize
= ho_minimize
;
125 ho
->addrinfo
= ho_addrinfo
;
133 * Closes down the module.
138 ho_close(struct irs_ho
*this) {
139 struct pvt
*pvt
= (struct pvt
*)this->private;
143 free_host(&pvt
->host
);
145 memput(pvt
, sizeof *pvt
);
146 memput(this, sizeof *this);
152 * struct hostent * ho_byname(struct irs_ho *this, const char *name)
156 static struct hostent
*
157 ho_byname(struct irs_ho
*this, const char *name
) {
158 return (ho_byname2(this, name
, AF_INET
));
166 * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af)
170 static struct hostent
*
171 ho_byname2(struct irs_ho
*this, const char *name
, int af
) {
172 struct pvt
*pvt
= (struct pvt
*)this->private;
173 struct hostent
*ho
= &pvt
->host
;
179 if (ho
->h_name
!= NULL
&&
180 strcmp(name
, ho
->h_name
) == 0 &&
181 af
== ho
->h_addrtype
) {
185 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
189 if (irs_irp_send_command(pvt
->girpdata
, "gethostbyname2 %s %s",
190 name
, ADDR_T_STR(af
)) != 0)
193 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
195 &body
, &bodylen
) != 0) {
199 if (code
== IRPD_GETHOST_OK
) {
201 if (irp_unmarshall_ho(ho
, body
) != 0) {
209 memput(body
, bodylen
);
218 * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
223 static struct hostent
*
224 ho_byaddr(struct irs_ho
*this, const void *addr
, int len
, int af
) {
225 struct pvt
*pvt
= (struct pvt
*)this->private;
226 struct hostent
*ho
= &pvt
->host
;
231 char paddr
[MAXPADDRSIZE
];
234 if (ho
->h_name
!= NULL
&&
235 af
== ho
->h_addrtype
&&
236 len
== ho
->h_length
) {
237 for (p
= ho
->h_addr_list
; *p
!= NULL
; p
++) {
238 if (memcmp(*p
, addr
, len
) == 0)
243 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
247 if (inet_ntop(af
, addr
, paddr
, sizeof paddr
) == NULL
) {
251 if (irs_irp_send_command(pvt
->girpdata
, "gethostbyaddr %s %s",
252 paddr
, ADDR_T_STR(af
)) != 0) {
256 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
258 &body
, &bodylen
) != 0) {
262 if (code
== IRPD_GETHOST_OK
) {
264 if (irp_unmarshall_ho(ho
, body
) != 0) {
272 memput(body
, bodylen
);
279 * The implementation for gethostent(3). The first time it's
280 * called all the data is pulled from the remote(i.e. what
281 * the maximum number of gethostent(3) calls would return)
282 * and that data is cached.
286 static struct hostent
*
287 ho_next(struct irs_ho
*this) {
288 struct pvt
*pvt
= (struct pvt
*)this->private;
289 struct hostent
*ho
= &pvt
->host
;
295 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
299 if (irs_irp_send_command(pvt
->girpdata
, "gethostent") != 0) {
303 if (irs_irp_get_full_response(pvt
->girpdata
, &code
,
305 &body
, &bodylen
) != 0) {
309 if (code
== IRPD_GETHOST_OK
) {
311 if (irp_unmarshall_ho(ho
, body
) != 0) {
319 memput(body
, bodylen
);
326 * void ho_rewind(struct irs_ho *this)
331 ho_rewind(struct irs_ho
*this) {
332 struct pvt
*pvt
= (struct pvt
*)this->private;
336 if (irs_irp_connection_setup(pvt
->girpdata
, &pvt
->warned
) != 0) {
340 if (irs_irp_send_command(pvt
->girpdata
, "sethostent") != 0) {
344 code
= irs_irp_read_response(pvt
->girpdata
, text
, sizeof text
);
345 if (code
!= IRPD_GETHOST_SETOK
) {
346 if (irp_log_errors
) {
347 syslog(LOG_WARNING
, "sethostent failed: %s", text
);
355 * void ho_minimize(struct irs_ho *this)
360 ho_minimize(struct irs_ho
*this) {
361 struct pvt
*pvt
= (struct pvt
*)this->private;
363 free_host(&pvt
->host
);
365 irs_irp_disconnect(pvt
->girpdata
);
369 * void free_host(struct hostent *ho)
374 free_host(struct hostent
*ho
) {
381 if (ho
->h_name
!= NULL
)
384 if (ho
->h_aliases
!= NULL
) {
385 for (p
= ho
->h_aliases
; *p
!= NULL
; p
++)
390 if (ho
->h_addr_list
!= NULL
) {
391 for (p
= ho
->h_addr_list
; *p
!= NULL
; p
++)
393 free(ho
->h_addr_list
);
398 static struct addrinfo
*
399 ho_addrinfo(struct irs_ho
*this, const char *name
, const struct addrinfo
*pai
)