Home | History | Annotate | Line # | Download | only in irs
irp_ho.c revision 1.1
      1  1.1  christos /*	$NetBSD: irp_ho.c,v 1.1 2009/04/12 15:33:43 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
      6  1.1  christos  *
      7  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9  1.1  christos  * copyright notice and this permission notice appear in all copies.
     10  1.1  christos  *
     11  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1  christos  */
     19  1.1  christos 
     20  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     21  1.1  christos static const char rcsid[] = "Id: irp_ho.c,v 1.3 2005/04/27 04:56:28 sra Exp";
     22  1.1  christos #endif /* LIBC_SCCS and not lint */
     23  1.1  christos 
     24  1.1  christos /* Imports. */
     25  1.1  christos 
     26  1.1  christos #include "port_before.h"
     27  1.1  christos 
     28  1.1  christos #include <syslog.h>
     29  1.1  christos #include <sys/types.h>
     30  1.1  christos #include <sys/param.h>
     31  1.1  christos #include <sys/socket.h>
     32  1.1  christos 
     33  1.1  christos #include <netinet/in.h>
     34  1.1  christos #include <arpa/inet.h>
     35  1.1  christos #include <arpa/nameser.h>
     36  1.1  christos 
     37  1.1  christos #include <ctype.h>
     38  1.1  christos #include <errno.h>
     39  1.1  christos #include <fcntl.h>
     40  1.1  christos #include <netdb.h>
     41  1.1  christos #include <resolv.h>
     42  1.1  christos #include <stdio.h>
     43  1.1  christos #include <stdlib.h>
     44  1.1  christos #include <string.h>
     45  1.1  christos #include <syslog.h>
     46  1.1  christos 
     47  1.1  christos #include <irs.h>
     48  1.1  christos #include <irp.h>
     49  1.1  christos #include <isc/irpmarshall.h>
     50  1.1  christos #include <isc/memcluster.h>
     51  1.1  christos 
     52  1.1  christos #include "irs_p.h"
     53  1.1  christos #include "dns_p.h"
     54  1.1  christos #include "irp_p.h"
     55  1.1  christos 
     56  1.1  christos #include "port_after.h"
     57  1.1  christos 
     58  1.1  christos /* Definitions. */
     59  1.1  christos 
     60  1.1  christos #define	MAXALIASES	35
     61  1.1  christos #define	MAXADDRS	35
     62  1.1  christos #define	Max(a,b)	((a) > (b) ? (a) : (b))
     63  1.1  christos 
     64  1.1  christos 
     65  1.1  christos struct pvt {
     66  1.1  christos 	struct irp_p	       *girpdata;
     67  1.1  christos 	int			warned;
     68  1.1  christos 	struct hostent		host;
     69  1.1  christos };
     70  1.1  christos 
     71  1.1  christos /* Forward. */
     72  1.1  christos 
     73  1.1  christos static void		ho_close(struct irs_ho *this);
     74  1.1  christos static struct hostent *	ho_byname(struct irs_ho *this, const char *name);
     75  1.1  christos static struct hostent *	ho_byname2(struct irs_ho *this, const char *name,
     76  1.1  christos 				   int af);
     77  1.1  christos static struct hostent *	ho_byaddr(struct irs_ho *this, const void *addr,
     78  1.1  christos 				  int len, int af);
     79  1.1  christos static struct hostent *	ho_next(struct irs_ho *this);
     80  1.1  christos static void		ho_rewind(struct irs_ho *this);
     81  1.1  christos static void		ho_minimize(struct irs_ho *this);
     82  1.1  christos 
     83  1.1  christos static void		free_host(struct hostent *ho);
     84  1.1  christos static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
     85  1.1  christos 				     const struct addrinfo *pai);
     86  1.1  christos 
     87  1.1  christos /* Public. */
     88  1.1  christos 
     89  1.1  christos /*%
     90  1.1  christos  * struct irs_ho * irs_irp_ho(struct irs_acc *this)
     91  1.1  christos  *
     92  1.1  christos  * Notes:
     93  1.1  christos  *
     94  1.1  christos  *	Initializes the irp_ho module.
     95  1.1  christos  *
     96  1.1  christos  */
     97  1.1  christos 
     98  1.1  christos struct irs_ho *
     99  1.1  christos irs_irp_ho(struct irs_acc *this) {
    100  1.1  christos 	struct irs_ho *ho;
    101  1.1  christos 	struct pvt *pvt;
    102  1.1  christos 
    103  1.1  christos 	if (!(ho = memget(sizeof *ho))) {
    104  1.1  christos 		errno = ENOMEM;
    105  1.1  christos 		return (NULL);
    106  1.1  christos 	}
    107  1.1  christos 	memset(ho, 0x0, sizeof *ho);
    108  1.1  christos 
    109  1.1  christos 	if (!(pvt = memget(sizeof *pvt))) {
    110  1.1  christos 		memput(ho, sizeof *ho);
    111  1.1  christos 		errno = ENOMEM;
    112  1.1  christos 		return (NULL);
    113  1.1  christos 	}
    114  1.1  christos 	memset(pvt, 0, sizeof *pvt);
    115  1.1  christos 	pvt->girpdata = this->private;
    116  1.1  christos 
    117  1.1  christos 	ho->private = pvt;
    118  1.1  christos 	ho->close = ho_close;
    119  1.1  christos 	ho->byname = ho_byname;
    120  1.1  christos 	ho->byname2 = ho_byname2;
    121  1.1  christos 	ho->byaddr = ho_byaddr;
    122  1.1  christos 	ho->next = ho_next;
    123  1.1  christos 	ho->rewind = ho_rewind;
    124  1.1  christos 	ho->minimize = ho_minimize;
    125  1.1  christos 	ho->addrinfo = ho_addrinfo;
    126  1.1  christos 
    127  1.1  christos 	return (ho);
    128  1.1  christos }
    129  1.1  christos 
    130  1.1  christos /* Methods. */
    131  1.1  christos 
    132  1.1  christos /*%
    133  1.1  christos  *	Closes down the module.
    134  1.1  christos  *
    135  1.1  christos  */
    136  1.1  christos 
    137  1.1  christos static void
    138  1.1  christos ho_close(struct irs_ho *this) {
    139  1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    140  1.1  christos 
    141  1.1  christos 	ho_minimize(this);
    142  1.1  christos 
    143  1.1  christos 	free_host(&pvt->host);
    144  1.1  christos 
    145  1.1  christos 	memput(pvt, sizeof *pvt);
    146  1.1  christos 	memput(this, sizeof *this);
    147  1.1  christos }
    148  1.1  christos 
    149  1.1  christos 
    150  1.1  christos 
    151  1.1  christos /*
    152  1.1  christos  * struct hostent * ho_byname(struct irs_ho *this, const char *name)
    153  1.1  christos  *
    154  1.1  christos  */
    155  1.1  christos 
    156  1.1  christos static struct hostent *
    157  1.1  christos ho_byname(struct irs_ho *this, const char *name) {
    158  1.1  christos 	return (ho_byname2(this, name, AF_INET));
    159  1.1  christos }
    160  1.1  christos 
    161  1.1  christos 
    162  1.1  christos 
    163  1.1  christos 
    164  1.1  christos 
    165  1.1  christos /*
    166  1.1  christos  * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af)
    167  1.1  christos  *
    168  1.1  christos  */
    169  1.1  christos 
    170  1.1  christos static struct hostent *
    171  1.1  christos ho_byname2(struct irs_ho *this, const char *name, int af) {
    172  1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    173  1.1  christos 	struct hostent *ho = &pvt->host;
    174  1.1  christos 	char *body = NULL;
    175  1.1  christos 	size_t bodylen;
    176  1.1  christos 	int code;
    177  1.1  christos 	char text[256];
    178  1.1  christos 
    179  1.1  christos 	if (ho->h_name != NULL &&
    180  1.1  christos 	    strcmp(name, ho->h_name) == 0 &&
    181  1.1  christos 	    af == ho->h_addrtype) {
    182  1.1  christos 		return (ho);
    183  1.1  christos 	}
    184  1.1  christos 
    185  1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    186  1.1  christos 		return (NULL);
    187  1.1  christos 	}
    188  1.1  christos 
    189  1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "gethostbyname2 %s %s",
    190  1.1  christos 				 name, ADDR_T_STR(af)) != 0)
    191  1.1  christos 		return (NULL);
    192  1.1  christos 
    193  1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    194  1.1  christos 				      text, sizeof text,
    195  1.1  christos 				      &body, &bodylen) != 0) {
    196  1.1  christos 		return (NULL);
    197  1.1  christos 	}
    198  1.1  christos 
    199  1.1  christos 	if (code == IRPD_GETHOST_OK) {
    200  1.1  christos 		free_host(ho);
    201  1.1  christos 		if (irp_unmarshall_ho(ho, body) != 0) {
    202  1.1  christos 			ho = NULL;
    203  1.1  christos 		}
    204  1.1  christos 	} else {
    205  1.1  christos 		ho = NULL;
    206  1.1  christos 	}
    207  1.1  christos 
    208  1.1  christos 	if (body != NULL) {
    209  1.1  christos 		memput(body, bodylen);
    210  1.1  christos 	}
    211  1.1  christos 
    212  1.1  christos 	return (ho);
    213  1.1  christos }
    214  1.1  christos 
    215  1.1  christos 
    216  1.1  christos 
    217  1.1  christos /*
    218  1.1  christos  * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr,
    219  1.1  christos  *			   int len, int af)
    220  1.1  christos  *
    221  1.1  christos  */
    222  1.1  christos 
    223  1.1  christos static struct hostent *
    224  1.1  christos ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
    225  1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    226  1.1  christos 	struct hostent *ho = &pvt->host;
    227  1.1  christos 	char *body = NULL;
    228  1.1  christos 	size_t bodylen;
    229  1.1  christos 	int code;
    230  1.1  christos 	char **p;
    231  1.1  christos 	char paddr[MAXPADDRSIZE];
    232  1.1  christos 	char text[256];
    233  1.1  christos 
    234  1.1  christos 	if (ho->h_name != NULL &&
    235  1.1  christos 	    af == ho->h_addrtype &&
    236  1.1  christos 	    len == ho->h_length) {
    237  1.1  christos 		for (p = ho->h_addr_list ; *p != NULL ; p++) {
    238  1.1  christos 			if (memcmp(*p, addr, len) == 0)
    239  1.1  christos 				return (ho);
    240  1.1  christos 		}
    241  1.1  christos 	}
    242  1.1  christos 
    243  1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    244  1.1  christos 		return (NULL);
    245  1.1  christos 	}
    246  1.1  christos 
    247  1.1  christos 	if (inet_ntop(af, addr, paddr, sizeof paddr) == NULL) {
    248  1.1  christos 		return (NULL);
    249  1.1  christos 	}
    250  1.1  christos 
    251  1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "gethostbyaddr %s %s",
    252  1.1  christos 				 paddr, ADDR_T_STR(af)) != 0) {
    253  1.1  christos 		return (NULL);
    254  1.1  christos 	}
    255  1.1  christos 
    256  1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    257  1.1  christos 				      text, sizeof text,
    258  1.1  christos 				      &body, &bodylen) != 0) {
    259  1.1  christos 		return (NULL);
    260  1.1  christos 	}
    261  1.1  christos 
    262  1.1  christos 	if (code == IRPD_GETHOST_OK) {
    263  1.1  christos 		free_host(ho);
    264  1.1  christos 		if (irp_unmarshall_ho(ho, body) != 0) {
    265  1.1  christos 			ho = NULL;
    266  1.1  christos 		}
    267  1.1  christos 	} else {
    268  1.1  christos 		ho = NULL;
    269  1.1  christos 	}
    270  1.1  christos 
    271  1.1  christos 	if (body != NULL) {
    272  1.1  christos 		memput(body, bodylen);
    273  1.1  christos 	}
    274  1.1  christos 
    275  1.1  christos 	return (ho);
    276  1.1  christos }
    277  1.1  christos 
    278  1.1  christos /*%
    279  1.1  christos  *	The implementation for gethostent(3). The first time it's
    280  1.1  christos  *	called all the data is pulled from the remote(i.e. what
    281  1.1  christos  *	the maximum number of gethostent(3) calls would return)
    282  1.1  christos  *	and that data is cached.
    283  1.1  christos  *
    284  1.1  christos  */
    285  1.1  christos 
    286  1.1  christos static struct hostent *
    287  1.1  christos ho_next(struct irs_ho *this) {
    288  1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    289  1.1  christos 	struct hostent *ho = &pvt->host;
    290  1.1  christos 	char *body;
    291  1.1  christos 	size_t bodylen;
    292  1.1  christos 	int code;
    293  1.1  christos 	char text[256];
    294  1.1  christos 
    295  1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    296  1.1  christos 		return (NULL);
    297  1.1  christos 	}
    298  1.1  christos 
    299  1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "gethostent") != 0) {
    300  1.1  christos 		return (NULL);
    301  1.1  christos 	}
    302  1.1  christos 
    303  1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    304  1.1  christos 				      text, sizeof text,
    305  1.1  christos 				      &body, &bodylen) != 0) {
    306  1.1  christos 		return (NULL);
    307  1.1  christos 	}
    308  1.1  christos 
    309  1.1  christos 	if (code == IRPD_GETHOST_OK) {
    310  1.1  christos 		free_host(ho);
    311  1.1  christos 		if (irp_unmarshall_ho(ho, body) != 0) {
    312  1.1  christos 			ho = NULL;
    313  1.1  christos 		}
    314  1.1  christos 	} else {
    315  1.1  christos 		ho = NULL;
    316  1.1  christos 	}
    317  1.1  christos 
    318  1.1  christos 	if (body != NULL) {
    319  1.1  christos 		memput(body, bodylen);
    320  1.1  christos 	}
    321  1.1  christos 
    322  1.1  christos 	return (ho);
    323  1.1  christos }
    324  1.1  christos 
    325  1.1  christos /*%
    326  1.1  christos  * void ho_rewind(struct irs_ho *this)
    327  1.1  christos  *
    328  1.1  christos  */
    329  1.1  christos 
    330  1.1  christos static void
    331  1.1  christos ho_rewind(struct irs_ho *this) {
    332  1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    333  1.1  christos 	char text[256];
    334  1.1  christos 	int code;
    335  1.1  christos 
    336  1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    337  1.1  christos 		return;
    338  1.1  christos 	}
    339  1.1  christos 
    340  1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "sethostent") != 0) {
    341  1.1  christos 		return;
    342  1.1  christos 	}
    343  1.1  christos 
    344  1.1  christos 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
    345  1.1  christos 	if (code != IRPD_GETHOST_SETOK) {
    346  1.1  christos 		if (irp_log_errors) {
    347  1.1  christos 			syslog(LOG_WARNING, "sethostent failed: %s", text);
    348  1.1  christos 		}
    349  1.1  christos 	}
    350  1.1  christos 
    351  1.1  christos 	return;
    352  1.1  christos }
    353  1.1  christos 
    354  1.1  christos /*%
    355  1.1  christos  * void ho_minimize(struct irs_ho *this)
    356  1.1  christos  *
    357  1.1  christos  */
    358  1.1  christos 
    359  1.1  christos static void
    360  1.1  christos ho_minimize(struct irs_ho *this) {
    361  1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    362  1.1  christos 
    363  1.1  christos 	free_host(&pvt->host);
    364  1.1  christos 
    365  1.1  christos 	irs_irp_disconnect(pvt->girpdata);
    366  1.1  christos }
    367  1.1  christos 
    368  1.1  christos /*%
    369  1.1  christos  * void free_host(struct hostent *ho)
    370  1.1  christos  *
    371  1.1  christos  */
    372  1.1  christos 
    373  1.1  christos static void
    374  1.1  christos free_host(struct hostent *ho) {
    375  1.1  christos 	char **p;
    376  1.1  christos 
    377  1.1  christos 	if (ho == NULL) {
    378  1.1  christos 		return;
    379  1.1  christos 	}
    380  1.1  christos 
    381  1.1  christos 	if (ho->h_name != NULL)
    382  1.1  christos 		free(ho->h_name);
    383  1.1  christos 
    384  1.1  christos 	if (ho->h_aliases != NULL) {
    385  1.1  christos 		for (p = ho->h_aliases ; *p != NULL ; p++)
    386  1.1  christos 			free(*p);
    387  1.1  christos 		free(ho->h_aliases);
    388  1.1  christos 	}
    389  1.1  christos 
    390  1.1  christos 	if (ho->h_addr_list != NULL) {
    391  1.1  christos 		for (p = ho->h_addr_list ; *p != NULL ; p++)
    392  1.1  christos 			free(*p);
    393  1.1  christos 		free(ho->h_addr_list);
    394  1.1  christos 	}
    395  1.1  christos }
    396  1.1  christos 
    397  1.1  christos /* dummy */
    398  1.1  christos static struct addrinfo *
    399  1.1  christos ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
    400  1.1  christos {
    401  1.1  christos 	UNUSED(this);
    402  1.1  christos 	UNUSED(name);
    403  1.1  christos 	UNUSED(pai);
    404  1.1  christos 	return(NULL);
    405  1.1  christos }
    406  1.1  christos 
    407  1.1  christos /*! \file */
    408