Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / irp_ho.c
blob37949b2a0a225cb9178010e3c94ecdb3be3a6eca
1 /*
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.1.2.1 2004/03/09 09:17:31 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 /* Imports. */
24 #include "port_before.h"
26 #include <syslog.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>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <netdb.h>
39 #include <resolv.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <syslog.h>
45 #include <irs.h>
46 #include <irp.h>
47 #include <isc/irpmarshall.h>
48 #include <isc/memcluster.h>
50 #include "irs_p.h"
51 #include "dns_p.h"
52 #include "irp_p.h"
54 #include "port_after.h"
56 /* Definitions. */
58 #define MAXALIASES 35
59 #define MAXADDRS 35
60 #define Max(a,b) ((a) > (b) ? (a) : (b))
63 struct pvt {
64 struct irp_p *girpdata;
65 int warned;
66 struct hostent host;
69 /* Forward. */
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,
74 int af);
75 static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
76 int len, int af);
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);
85 /* Public. */
90 * struct irs_ho * irs_irp_ho(struct irs_acc *this)
92 * Notes:
94 * Initializes the irp_ho module.
98 struct irs_ho *
99 irs_irp_ho(struct irs_acc *this) {
100 struct irs_ho *ho;
101 struct pvt *pvt;
103 if (!(ho = memget(sizeof *ho))) {
104 errno = ENOMEM;
105 return (NULL);
107 memset(ho, 0x0, sizeof *ho);
109 if (!(pvt = memget(sizeof *pvt))) {
110 memput(ho, sizeof *ho);
111 errno = ENOMEM;
112 return (NULL);
114 memset(pvt, 0, sizeof *pvt);
115 pvt->girpdata = this->private;
117 ho->private = pvt;
118 ho->close = ho_close;
119 ho->byname = ho_byname;
120 ho->byname2 = ho_byname2;
121 ho->byaddr = ho_byaddr;
122 ho->next = ho_next;
123 ho->rewind = ho_rewind;
124 ho->minimize = ho_minimize;
125 ho->addrinfo = ho_addrinfo;
127 return (ho);
130 /* Methods. */
135 * void ho_close(struct irs_ho *this)
137 * Notes:
139 * Closes down the module.
143 static void
144 ho_close(struct irs_ho *this) {
145 struct pvt *pvt = (struct pvt *)this->private;
147 ho_minimize(this);
149 free_host(&pvt->host);
151 memput(pvt, sizeof *pvt);
152 memput(this, sizeof *this);
158 * struct hostent * ho_byname(struct irs_ho *this, const char *name)
162 static struct hostent *
163 ho_byname(struct irs_ho *this, const char *name) {
164 return (ho_byname2(this, name, AF_INET));
172 * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af)
176 static struct hostent *
177 ho_byname2(struct irs_ho *this, const char *name, int af) {
178 struct pvt *pvt = (struct pvt *)this->private;
179 struct hostent *ho = &pvt->host;
180 char *body = NULL;
181 size_t bodylen;
182 int code;
183 char text[256];
185 if (ho->h_name != NULL &&
186 strcmp(name, ho->h_name) == 0 &&
187 af == ho->h_addrtype) {
188 return (ho);
191 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
192 return (NULL);
195 if (irs_irp_send_command(pvt->girpdata, "gethostbyname2 %s %s",
196 name, ADDR_T_STR(af)) != 0)
197 return (NULL);
199 if (irs_irp_get_full_response(pvt->girpdata, &code,
200 text, sizeof text,
201 &body, &bodylen) != 0) {
202 return (NULL);
205 if (code == IRPD_GETHOST_OK) {
206 free_host(ho);
207 if (irp_unmarshall_ho(ho, body) != 0) {
208 ho = NULL;
210 } else {
211 ho = NULL;
214 if (body != NULL) {
215 memput(body, bodylen);
218 return (ho);
224 * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
225 * int len, int af)
229 static struct hostent *
230 ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
231 struct pvt *pvt = (struct pvt *)this->private;
232 struct hostent *ho = &pvt->host;
233 char *body = NULL;
234 size_t bodylen;
235 int code;
236 char **p;
237 char paddr[MAXPADDRSIZE];
238 char text[256];
240 if (ho->h_name != NULL &&
241 af == ho->h_addrtype &&
242 len == ho->h_length) {
243 for (p = ho->h_addr_list ; *p != NULL ; p++) {
244 if (memcmp(*p, addr, len) == 0)
245 return (ho);
249 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
250 return (NULL);
253 if (inet_ntop(af, addr, paddr, sizeof paddr) == NULL) {
254 return (NULL);
257 if (irs_irp_send_command(pvt->girpdata, "gethostbyaddr %s %s",
258 paddr, ADDR_T_STR(af)) != 0) {
259 return (NULL);
262 if (irs_irp_get_full_response(pvt->girpdata, &code,
263 text, sizeof text,
264 &body, &bodylen) != 0) {
265 return (NULL);
268 if (code == IRPD_GETHOST_OK) {
269 free_host(ho);
270 if (irp_unmarshall_ho(ho, body) != 0) {
271 ho = NULL;
273 } else {
274 ho = NULL;
277 if (body != NULL) {
278 memput(body, bodylen);
281 return (ho);
289 * struct hostent * ho_next(struct irs_ho *this)
291 * Notes:
293 * The implementation for gethostent(3). The first time it's
294 * called all the data is pulled from the remote(i.e. what
295 * the maximum number of gethostent(3) calls would return)
296 * and that data is cached.
300 static struct hostent *
301 ho_next(struct irs_ho *this) {
302 struct pvt *pvt = (struct pvt *)this->private;
303 struct hostent *ho = &pvt->host;
304 char *body;
305 size_t bodylen;
306 int code;
307 char text[256];
309 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
310 return (NULL);
313 if (irs_irp_send_command(pvt->girpdata, "gethostent") != 0) {
314 return (NULL);
317 if (irs_irp_get_full_response(pvt->girpdata, &code,
318 text, sizeof text,
319 &body, &bodylen) != 0) {
320 return (NULL);
323 if (code == IRPD_GETHOST_OK) {
324 free_host(ho);
325 if (irp_unmarshall_ho(ho, body) != 0) {
326 ho = NULL;
328 } else {
329 ho = NULL;
332 if (body != NULL) {
333 memput(body, bodylen);
336 return (ho);
344 * void ho_rewind(struct irs_ho *this)
348 static void
349 ho_rewind(struct irs_ho *this) {
350 struct pvt *pvt = (struct pvt *)this->private;
351 char text[256];
352 int code;
354 if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
355 return;
358 if (irs_irp_send_command(pvt->girpdata, "sethostent") != 0) {
359 return;
362 code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
363 if (code != IRPD_GETHOST_SETOK) {
364 if (irp_log_errors) {
365 syslog(LOG_WARNING, "sethostent failed: %s", text);
369 return;
376 * void ho_minimize(struct irs_ho *this)
380 static void
381 ho_minimize(struct irs_ho *this) {
382 struct pvt *pvt = (struct pvt *)this->private;
384 free_host(&pvt->host);
386 irs_irp_disconnect(pvt->girpdata);
393 * void free_host(struct hostent *ho)
397 static void
398 free_host(struct hostent *ho) {
399 char **p;
401 if (ho == NULL) {
402 return;
405 if (ho->h_name != NULL)
406 free(ho->h_name);
408 if (ho->h_aliases != NULL) {
409 for (p = ho->h_aliases ; *p != NULL ; p++)
410 free(*p);
411 free(ho->h_aliases);
414 if (ho->h_addr_list != NULL) {
415 for (p = ho->h_addr_list ; *p != NULL ; p++)
416 free(*p);
417 free(ho->h_addr_list);
421 /* dummy */
422 static struct addrinfo *
423 ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
425 UNUSED(this);
426 UNUSED(name);
427 UNUSED(pai);
428 return(NULL);