Merge from vendor branch CVS:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / irp_ho.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
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 */
21
22 /* Imports. */
23
24 #include "port_before.h"
25
26 #include <syslog.h>
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/socket.h>
30
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
34
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>
44
45 #include <irs.h>
46 #include <irp.h>
47 #include <isc/irpmarshall.h>
48 #include <isc/memcluster.h>
49
50 #include "irs_p.h"
51 #include "dns_p.h"
52 #include "irp_p.h"
53
54 #include "port_after.h"
55
56 /* Definitions. */
57
58 #define MAXALIASES      35
59 #define MAXADDRS        35
60 #define Max(a,b)        ((a) > (b) ? (a) : (b))
61
62
63 struct pvt {
64         struct irp_p           *girpdata;
65         int                     warned;
66         struct hostent          host;
67 };
68
69 /* Forward. */
70
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);
80
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);
84
85 /* Public. */
86
87
88
89 /*
90  * struct irs_ho * irs_irp_ho(struct irs_acc *this)
91  *
92  * Notes:
93  *
94  *      Initializes the irp_ho module.
95  *
96  */
97
98 struct irs_ho *
99 irs_irp_ho(struct irs_acc *this) {
100         struct irs_ho *ho;
101         struct pvt *pvt;
102
103         if (!(ho = memget(sizeof *ho))) {
104                 errno = ENOMEM;
105                 return (NULL);
106         }
107         memset(ho, 0x0, sizeof *ho);
108
109         if (!(pvt = memget(sizeof *pvt))) {
110                 memput(ho, sizeof *ho);
111                 errno = ENOMEM;
112                 return (NULL);
113         }
114         memset(pvt, 0, sizeof *pvt);
115         pvt->girpdata = this->private;
116
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;
126
127         return (ho);
128 }
129
130 /* Methods. */
131
132
133
134 /*
135  * void ho_close(struct irs_ho *this)
136  *
137  * Notes:
138  *
139  *      Closes down the module.
140  *
141  */
142
143 static void
144 ho_close(struct irs_ho *this) {
145         struct pvt *pvt = (struct pvt *)this->private;
146
147         ho_minimize(this);
148
149         free_host(&pvt->host);
150
151         memput(pvt, sizeof *pvt);
152         memput(this, sizeof *this);
153 }
154
155
156
157 /*
158  * struct hostent * ho_byname(struct irs_ho *this, const char *name)
159  *
160  */
161
162 static struct hostent *
163 ho_byname(struct irs_ho *this, const char *name) {
164         return (ho_byname2(this, name, AF_INET));
165 }
166
167
168
169
170
171 /*
172  * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af)
173  *
174  */
175
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];
184
185         if (ho->h_name != NULL &&
186             strcmp(name, ho->h_name) == 0 &&
187             af == ho->h_addrtype) {
188                 return (ho);
189         }
190
191         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
192                 return (NULL);
193         }
194
195         if (irs_irp_send_command(pvt->girpdata, "gethostbyname2 %s %s",
196                                  name, ADDR_T_STR(af)) != 0)
197                 return (NULL);
198
199         if (irs_irp_get_full_response(pvt->girpdata, &code,
200                                       text, sizeof text,
201                                       &body, &bodylen) != 0) {
202                 return (NULL);
203         }
204
205         if (code == IRPD_GETHOST_OK) {
206                 free_host(ho);
207                 if (irp_unmarshall_ho(ho, body) != 0) {
208                         ho = NULL;
209                 }
210         } else {
211                 ho = NULL;
212         }
213
214         if (body != NULL) {
215                 memput(body, bodylen);
216         }
217
218         return (ho);
219 }
220
221
222
223 /*
224  * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
225  *                         int len, int af)
226  *
227  */
228
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];
239
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);
246                 }
247         }
248
249         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
250                 return (NULL);
251         }
252
253         if (inet_ntop(af, addr, paddr, sizeof paddr) == NULL) {
254                 return (NULL);
255         }
256
257         if (irs_irp_send_command(pvt->girpdata, "gethostbyaddr %s %s",
258                                  paddr, ADDR_T_STR(af)) != 0) {
259                 return (NULL);
260         }
261
262         if (irs_irp_get_full_response(pvt->girpdata, &code,
263                                       text, sizeof text,
264                                       &body, &bodylen) != 0) {
265                 return (NULL);
266         }
267
268         if (code == IRPD_GETHOST_OK) {
269                 free_host(ho);
270                 if (irp_unmarshall_ho(ho, body) != 0) {
271                         ho = NULL;
272                 }
273         } else {
274                 ho = NULL;
275         }
276
277         if (body != NULL) {
278                 memput(body, bodylen);
279         }
280
281         return (ho);
282 }
283
284
285
286
287
288 /*
289  * struct hostent * ho_next(struct irs_ho *this)
290  *
291  * Notes:
292  *
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.
297  *
298  */
299
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];
308
309         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
310                 return (NULL);
311         }
312
313         if (irs_irp_send_command(pvt->girpdata, "gethostent") != 0) {
314                 return (NULL);
315         }
316
317         if (irs_irp_get_full_response(pvt->girpdata, &code,
318                                       text, sizeof text,
319                                       &body, &bodylen) != 0) {
320                 return (NULL);
321         }
322
323         if (code == IRPD_GETHOST_OK) {
324                 free_host(ho);
325                 if (irp_unmarshall_ho(ho, body) != 0) {
326                         ho = NULL;
327                 }
328         } else {
329                 ho = NULL;
330         }
331
332         if (body != NULL) {
333                 memput(body, bodylen);
334         }
335
336         return (ho);
337 }
338
339
340
341
342
343 /*
344  * void ho_rewind(struct irs_ho *this)
345  *
346  */
347
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;
353
354         if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
355                 return;
356         }
357
358         if (irs_irp_send_command(pvt->girpdata, "sethostent") != 0) {
359                 return;
360         }
361
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);
366                 }
367         }
368
369         return;
370 }
371
372
373
374
375 /*
376  * void ho_minimize(struct irs_ho *this)
377  *
378  */
379
380 static void
381 ho_minimize(struct irs_ho *this) {
382         struct pvt *pvt = (struct pvt *)this->private;
383
384         free_host(&pvt->host);
385
386         irs_irp_disconnect(pvt->girpdata);
387 }
388
389
390
391
392 /*
393  * void free_host(struct hostent *ho)
394  *
395  */
396
397 static void
398 free_host(struct hostent *ho) {
399         char **p;
400
401         if (ho == NULL) {
402                 return;
403         }
404
405         if (ho->h_name != NULL)
406                 free(ho->h_name);
407
408         if (ho->h_aliases != NULL) {
409                 for (p = ho->h_aliases ; *p != NULL ; p++)
410                         free(*p);
411                 free(ho->h_aliases);
412         }
413
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);
418         }
419 }
420
421 /* dummy */
422 static struct addrinfo *
423 ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
424 {
425         UNUSED(this);
426         UNUSED(name);
427         UNUSED(pai);
428         return(NULL);
429 }