Home | History | Annotate | Line # | Download | only in irs
dns_sv.c revision 1.1.1.1.6.2
      1  1.1.1.1.6.2  riz /*	$NetBSD: dns_sv.c,v 1.1.1.1.6.2 2011/01/09 20:42:53 riz Exp $	*/
      2  1.1.1.1.6.2  riz 
      3  1.1.1.1.6.2  riz /*
      4  1.1.1.1.6.2  riz  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1.1.1.6.2  riz  * Copyright (c) 1996,1999 by Internet Software Consortium.
      6  1.1.1.1.6.2  riz  *
      7  1.1.1.1.6.2  riz  * Permission to use, copy, modify, and distribute this software for any
      8  1.1.1.1.6.2  riz  * purpose with or without fee is hereby granted, provided that the above
      9  1.1.1.1.6.2  riz  * copyright notice and this permission notice appear in all copies.
     10  1.1.1.1.6.2  riz  *
     11  1.1.1.1.6.2  riz  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  1.1.1.1.6.2  riz  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1.1.1.6.2  riz  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  1.1.1.1.6.2  riz  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1.1.1.6.2  riz  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1.1.1.6.2  riz  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  1.1.1.1.6.2  riz  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1.1.1.6.2  riz  */
     19  1.1.1.1.6.2  riz 
     20  1.1.1.1.6.2  riz #if defined(LIBC_SCCS) && !defined(lint)
     21  1.1.1.1.6.2  riz static const char rcsid[] = "Id: dns_sv.c,v 1.5 2005/04/27 04:56:23 sra Exp";
     22  1.1.1.1.6.2  riz #endif
     23  1.1.1.1.6.2  riz 
     24  1.1.1.1.6.2  riz /* Imports */
     25  1.1.1.1.6.2  riz 
     26  1.1.1.1.6.2  riz #include "port_before.h"
     27  1.1.1.1.6.2  riz 
     28  1.1.1.1.6.2  riz #include <sys/types.h>
     29  1.1.1.1.6.2  riz #include <netinet/in.h>
     30  1.1.1.1.6.2  riz 
     31  1.1.1.1.6.2  riz #include <stdio.h>
     32  1.1.1.1.6.2  riz #include <string.h>
     33  1.1.1.1.6.2  riz #include <netdb.h>
     34  1.1.1.1.6.2  riz #include <ctype.h>
     35  1.1.1.1.6.2  riz #include <stdlib.h>
     36  1.1.1.1.6.2  riz #include <errno.h>
     37  1.1.1.1.6.2  riz 
     38  1.1.1.1.6.2  riz #include <sys/types.h>
     39  1.1.1.1.6.2  riz #include <netinet/in.h>
     40  1.1.1.1.6.2  riz #include <arpa/nameser.h>
     41  1.1.1.1.6.2  riz #include <resolv.h>
     42  1.1.1.1.6.2  riz 
     43  1.1.1.1.6.2  riz #include <isc/memcluster.h>
     44  1.1.1.1.6.2  riz #include <irs.h>
     45  1.1.1.1.6.2  riz 
     46  1.1.1.1.6.2  riz #include "port_after.h"
     47  1.1.1.1.6.2  riz 
     48  1.1.1.1.6.2  riz #include "irs_p.h"
     49  1.1.1.1.6.2  riz #include "hesiod.h"
     50  1.1.1.1.6.2  riz #include "dns_p.h"
     51  1.1.1.1.6.2  riz 
     52  1.1.1.1.6.2  riz /* Definitions */
     53  1.1.1.1.6.2  riz 
     54  1.1.1.1.6.2  riz struct pvt {
     55  1.1.1.1.6.2  riz 	struct dns_p *		dns;
     56  1.1.1.1.6.2  riz 	struct servent		serv;
     57  1.1.1.1.6.2  riz 	char *			svbuf;
     58  1.1.1.1.6.2  riz 	struct __res_state *	res;
     59  1.1.1.1.6.2  riz 	void			(*free_res)(void *);
     60  1.1.1.1.6.2  riz };
     61  1.1.1.1.6.2  riz 
     62  1.1.1.1.6.2  riz /* Forward. */
     63  1.1.1.1.6.2  riz 
     64  1.1.1.1.6.2  riz static void 			sv_close(struct irs_sv *);
     65  1.1.1.1.6.2  riz static struct servent *		sv_byname(struct irs_sv *,
     66  1.1.1.1.6.2  riz 					  const char *, const char *);
     67  1.1.1.1.6.2  riz static struct servent *		sv_byport(struct irs_sv *, int, const char *);
     68  1.1.1.1.6.2  riz static struct servent *		sv_next(struct irs_sv *);
     69  1.1.1.1.6.2  riz static void			sv_rewind(struct irs_sv *);
     70  1.1.1.1.6.2  riz static void			sv_minimize(struct irs_sv *);
     71  1.1.1.1.6.2  riz #ifdef SV_RES_SETGET
     72  1.1.1.1.6.2  riz static struct __res_state *	sv_res_get(struct irs_sv *);
     73  1.1.1.1.6.2  riz static void			sv_res_set(struct irs_sv *,
     74  1.1.1.1.6.2  riz 					   struct __res_state *,
     75  1.1.1.1.6.2  riz 					   void (*)(void *));
     76  1.1.1.1.6.2  riz #endif
     77  1.1.1.1.6.2  riz 
     78  1.1.1.1.6.2  riz static struct servent *		parse_hes_list(struct irs_sv *,
     79  1.1.1.1.6.2  riz 					       char **, const char *);
     80  1.1.1.1.6.2  riz 
     81  1.1.1.1.6.2  riz /* Public */
     82  1.1.1.1.6.2  riz 
     83  1.1.1.1.6.2  riz struct irs_sv *
     84  1.1.1.1.6.2  riz irs_dns_sv(struct irs_acc *this) {
     85  1.1.1.1.6.2  riz 	struct dns_p *dns = (struct dns_p *)this->private;
     86  1.1.1.1.6.2  riz 	struct irs_sv *sv;
     87  1.1.1.1.6.2  riz 	struct pvt *pvt;
     88  1.1.1.1.6.2  riz 
     89  1.1.1.1.6.2  riz 	if (!dns || !dns->hes_ctx) {
     90  1.1.1.1.6.2  riz 		errno = ENODEV;
     91  1.1.1.1.6.2  riz 		return (NULL);
     92  1.1.1.1.6.2  riz 	}
     93  1.1.1.1.6.2  riz 	if (!(pvt = memget(sizeof *pvt))) {
     94  1.1.1.1.6.2  riz 		errno = ENOMEM;
     95  1.1.1.1.6.2  riz 		return (NULL);
     96  1.1.1.1.6.2  riz 	}
     97  1.1.1.1.6.2  riz 	memset(pvt, 0, sizeof *pvt);
     98  1.1.1.1.6.2  riz 	pvt->dns = dns;
     99  1.1.1.1.6.2  riz 	if (!(sv = memget(sizeof *sv))) {
    100  1.1.1.1.6.2  riz 		memput(pvt, sizeof *pvt);
    101  1.1.1.1.6.2  riz 		errno = ENOMEM;
    102  1.1.1.1.6.2  riz 		return (NULL);
    103  1.1.1.1.6.2  riz 	}
    104  1.1.1.1.6.2  riz 	memset(sv, 0x5e, sizeof *sv);
    105  1.1.1.1.6.2  riz 	sv->private = pvt;
    106  1.1.1.1.6.2  riz 	sv->byname = sv_byname;
    107  1.1.1.1.6.2  riz 	sv->byport = sv_byport;
    108  1.1.1.1.6.2  riz 	sv->next = sv_next;
    109  1.1.1.1.6.2  riz 	sv->rewind = sv_rewind;
    110  1.1.1.1.6.2  riz 	sv->close = sv_close;
    111  1.1.1.1.6.2  riz 	sv->minimize = sv_minimize;
    112  1.1.1.1.6.2  riz #ifdef SV_RES_SETGET
    113  1.1.1.1.6.2  riz 	sv->res_get = sv_res_get;
    114  1.1.1.1.6.2  riz 	sv->res_set = sv_res_set;
    115  1.1.1.1.6.2  riz #else
    116  1.1.1.1.6.2  riz 	sv->res_get = NULL; /*%< sv_res_get; */
    117  1.1.1.1.6.2  riz 	sv->res_set = NULL; /*%< sv_res_set; */
    118  1.1.1.1.6.2  riz #endif
    119  1.1.1.1.6.2  riz 	return (sv);
    120  1.1.1.1.6.2  riz }
    121  1.1.1.1.6.2  riz 
    122  1.1.1.1.6.2  riz /* Methods */
    123  1.1.1.1.6.2  riz 
    124  1.1.1.1.6.2  riz static void
    125  1.1.1.1.6.2  riz sv_close(struct irs_sv *this) {
    126  1.1.1.1.6.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    127  1.1.1.1.6.2  riz 
    128  1.1.1.1.6.2  riz 	if (pvt->serv.s_aliases)
    129  1.1.1.1.6.2  riz 		free(pvt->serv.s_aliases);
    130  1.1.1.1.6.2  riz 	if (pvt->svbuf)
    131  1.1.1.1.6.2  riz 		free(pvt->svbuf);
    132  1.1.1.1.6.2  riz 
    133  1.1.1.1.6.2  riz 	if (pvt->res && pvt->free_res)
    134  1.1.1.1.6.2  riz 		(*pvt->free_res)(pvt->res);
    135  1.1.1.1.6.2  riz 	memput(pvt, sizeof *pvt);
    136  1.1.1.1.6.2  riz 	memput(this, sizeof *this);
    137  1.1.1.1.6.2  riz }
    138  1.1.1.1.6.2  riz 
    139  1.1.1.1.6.2  riz static struct servent *
    140  1.1.1.1.6.2  riz sv_byname(struct irs_sv *this, const char *name, const char *proto) {
    141  1.1.1.1.6.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    142  1.1.1.1.6.2  riz 	struct dns_p *dns = pvt->dns;
    143  1.1.1.1.6.2  riz 	struct servent *s;
    144  1.1.1.1.6.2  riz 	char **hes_list;
    145  1.1.1.1.6.2  riz 
    146  1.1.1.1.6.2  riz 	if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "service")))
    147  1.1.1.1.6.2  riz 		return (NULL);
    148  1.1.1.1.6.2  riz 
    149  1.1.1.1.6.2  riz 	s = parse_hes_list(this, hes_list, proto);
    150  1.1.1.1.6.2  riz 	hesiod_free_list(dns->hes_ctx, hes_list);
    151  1.1.1.1.6.2  riz 	return (s);
    152  1.1.1.1.6.2  riz }
    153  1.1.1.1.6.2  riz 
    154  1.1.1.1.6.2  riz static struct servent *
    155  1.1.1.1.6.2  riz sv_byport(struct irs_sv *this, int port, const char *proto) {
    156  1.1.1.1.6.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    157  1.1.1.1.6.2  riz 	struct dns_p *dns = pvt->dns;
    158  1.1.1.1.6.2  riz 	struct servent *s;
    159  1.1.1.1.6.2  riz 	char portstr[16];
    160  1.1.1.1.6.2  riz 	char **hes_list;
    161  1.1.1.1.6.2  riz 
    162  1.1.1.1.6.2  riz 	sprintf(portstr, "%d", ntohs(port));
    163  1.1.1.1.6.2  riz 	if (!(hes_list = hesiod_resolve(dns->hes_ctx, portstr, "port")))
    164  1.1.1.1.6.2  riz 		return (NULL);
    165  1.1.1.1.6.2  riz 
    166  1.1.1.1.6.2  riz 	s = parse_hes_list(this, hes_list, proto);
    167  1.1.1.1.6.2  riz 	hesiod_free_list(dns->hes_ctx, hes_list);
    168  1.1.1.1.6.2  riz 	return (s);
    169  1.1.1.1.6.2  riz }
    170  1.1.1.1.6.2  riz 
    171  1.1.1.1.6.2  riz static struct servent *
    172  1.1.1.1.6.2  riz sv_next(struct irs_sv *this) {
    173  1.1.1.1.6.2  riz 	UNUSED(this);
    174  1.1.1.1.6.2  riz 	errno = ENODEV;
    175  1.1.1.1.6.2  riz 	return (NULL);
    176  1.1.1.1.6.2  riz }
    177  1.1.1.1.6.2  riz 
    178  1.1.1.1.6.2  riz static void
    179  1.1.1.1.6.2  riz sv_rewind(struct irs_sv *this) {
    180  1.1.1.1.6.2  riz 	UNUSED(this);
    181  1.1.1.1.6.2  riz 	/* NOOP */
    182  1.1.1.1.6.2  riz }
    183  1.1.1.1.6.2  riz 
    184  1.1.1.1.6.2  riz /* Private */
    185  1.1.1.1.6.2  riz 
    186  1.1.1.1.6.2  riz static struct servent *
    187  1.1.1.1.6.2  riz parse_hes_list(struct irs_sv *this, char **hes_list, const char *proto) {
    188  1.1.1.1.6.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    189  1.1.1.1.6.2  riz 	char *p, *cp, **cpp, **new;
    190  1.1.1.1.6.2  riz 	int proto_len;
    191  1.1.1.1.6.2  riz 	int num = 0;
    192  1.1.1.1.6.2  riz 	int max = 0;
    193  1.1.1.1.6.2  riz 
    194  1.1.1.1.6.2  riz 	for (cpp = hes_list; *cpp; cpp++) {
    195  1.1.1.1.6.2  riz 		cp = *cpp;
    196  1.1.1.1.6.2  riz 
    197  1.1.1.1.6.2  riz 		/* Strip away comments, if any. */
    198  1.1.1.1.6.2  riz 		if ((p = strchr(cp, '#')))
    199  1.1.1.1.6.2  riz 			*p = 0;
    200  1.1.1.1.6.2  riz 
    201  1.1.1.1.6.2  riz 		/* Check to make sure the protocol matches. */
    202  1.1.1.1.6.2  riz 		p = cp;
    203  1.1.1.1.6.2  riz 		while (*p && !isspace((unsigned char)*p))
    204  1.1.1.1.6.2  riz 			p++;
    205  1.1.1.1.6.2  riz 		if (!*p)
    206  1.1.1.1.6.2  riz 			continue;
    207  1.1.1.1.6.2  riz 		if (proto) {
    208  1.1.1.1.6.2  riz 		     proto_len = strlen(proto);
    209  1.1.1.1.6.2  riz 		     if (strncasecmp(++p, proto, proto_len) != 0)
    210  1.1.1.1.6.2  riz 			  continue;
    211  1.1.1.1.6.2  riz 		     if (p[proto_len] && !isspace(p[proto_len]&0xff))
    212  1.1.1.1.6.2  riz 			  continue;
    213  1.1.1.1.6.2  riz 		}
    214  1.1.1.1.6.2  riz 		/* OK, we've got a live one.  Let's parse it for real. */
    215  1.1.1.1.6.2  riz 		if (pvt->svbuf)
    216  1.1.1.1.6.2  riz 			free(pvt->svbuf);
    217  1.1.1.1.6.2  riz 		pvt->svbuf = strdup(cp);
    218  1.1.1.1.6.2  riz 
    219  1.1.1.1.6.2  riz 		p = pvt->svbuf;
    220  1.1.1.1.6.2  riz 		pvt->serv.s_name = p;
    221  1.1.1.1.6.2  riz 		while (*p && !isspace(*p&0xff))
    222  1.1.1.1.6.2  riz 			p++;
    223  1.1.1.1.6.2  riz 		if (!*p)
    224  1.1.1.1.6.2  riz 			continue;
    225  1.1.1.1.6.2  riz 		*p++ = '\0';
    226  1.1.1.1.6.2  riz 
    227  1.1.1.1.6.2  riz 		pvt->serv.s_proto = p;
    228  1.1.1.1.6.2  riz 		while (*p && !isspace(*p&0xff))
    229  1.1.1.1.6.2  riz 			p++;
    230  1.1.1.1.6.2  riz 		if (!*p)
    231  1.1.1.1.6.2  riz 			continue;
    232  1.1.1.1.6.2  riz 		*p++ = '\0';
    233  1.1.1.1.6.2  riz 
    234  1.1.1.1.6.2  riz 		pvt->serv.s_port = htons((u_short) atoi(p));
    235  1.1.1.1.6.2  riz 		while (*p && !isspace(*p&0xff))
    236  1.1.1.1.6.2  riz 			p++;
    237  1.1.1.1.6.2  riz 		if (*p)
    238  1.1.1.1.6.2  riz 			*p++ = '\0';
    239  1.1.1.1.6.2  riz 
    240  1.1.1.1.6.2  riz 		while (*p) {
    241  1.1.1.1.6.2  riz 			if ((num + 1) >= max || !pvt->serv.s_aliases) {
    242  1.1.1.1.6.2  riz 				max += 10;
    243  1.1.1.1.6.2  riz 				new = realloc(pvt->serv.s_aliases,
    244  1.1.1.1.6.2  riz 					      max * sizeof(char *));
    245  1.1.1.1.6.2  riz 				if (!new) {
    246  1.1.1.1.6.2  riz 					errno = ENOMEM;
    247  1.1.1.1.6.2  riz 					goto cleanup;
    248  1.1.1.1.6.2  riz 				}
    249  1.1.1.1.6.2  riz 				pvt->serv.s_aliases = new;
    250  1.1.1.1.6.2  riz 			}
    251  1.1.1.1.6.2  riz 			pvt->serv.s_aliases[num++] = p;
    252  1.1.1.1.6.2  riz 			while (*p && !isspace(*p&0xff))
    253  1.1.1.1.6.2  riz 				p++;
    254  1.1.1.1.6.2  riz 			if (*p)
    255  1.1.1.1.6.2  riz 				*p++ = '\0';
    256  1.1.1.1.6.2  riz 		}
    257  1.1.1.1.6.2  riz 		if (!pvt->serv.s_aliases)
    258  1.1.1.1.6.2  riz 			pvt->serv.s_aliases = malloc(sizeof(char *));
    259  1.1.1.1.6.2  riz 		if (!pvt->serv.s_aliases)
    260  1.1.1.1.6.2  riz 			goto cleanup;
    261  1.1.1.1.6.2  riz 		pvt->serv.s_aliases[num] = NULL;
    262  1.1.1.1.6.2  riz 		return (&pvt->serv);
    263  1.1.1.1.6.2  riz 	}
    264  1.1.1.1.6.2  riz 
    265  1.1.1.1.6.2  riz  cleanup:
    266  1.1.1.1.6.2  riz 	if (pvt->serv.s_aliases) {
    267  1.1.1.1.6.2  riz 		free(pvt->serv.s_aliases);
    268  1.1.1.1.6.2  riz 		pvt->serv.s_aliases = NULL;
    269  1.1.1.1.6.2  riz 	}
    270  1.1.1.1.6.2  riz 	if (pvt->svbuf) {
    271  1.1.1.1.6.2  riz 		free(pvt->svbuf);
    272  1.1.1.1.6.2  riz 		pvt->svbuf = NULL;
    273  1.1.1.1.6.2  riz 	}
    274  1.1.1.1.6.2  riz 	return (NULL);
    275  1.1.1.1.6.2  riz }
    276  1.1.1.1.6.2  riz 
    277  1.1.1.1.6.2  riz static void
    278  1.1.1.1.6.2  riz sv_minimize(struct irs_sv *this) {
    279  1.1.1.1.6.2  riz 	UNUSED(this);
    280  1.1.1.1.6.2  riz 	/* NOOP */
    281  1.1.1.1.6.2  riz }
    282  1.1.1.1.6.2  riz 
    283  1.1.1.1.6.2  riz #ifdef SV_RES_SETGET
    284  1.1.1.1.6.2  riz static struct __res_state *
    285  1.1.1.1.6.2  riz sv_res_get(struct irs_sv *this) {
    286  1.1.1.1.6.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    287  1.1.1.1.6.2  riz 	struct dns_p *dns = pvt->dns;
    288  1.1.1.1.6.2  riz 
    289  1.1.1.1.6.2  riz 	return (__hesiod_res_get(dns->hes_ctx));
    290  1.1.1.1.6.2  riz }
    291  1.1.1.1.6.2  riz 
    292  1.1.1.1.6.2  riz static void
    293  1.1.1.1.6.2  riz sv_res_set(struct irs_sv *this, struct __res_state * res,
    294  1.1.1.1.6.2  riz 	   void (*free_res)(void *)) {
    295  1.1.1.1.6.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    296  1.1.1.1.6.2  riz 	struct dns_p *dns = pvt->dns;
    297  1.1.1.1.6.2  riz 
    298  1.1.1.1.6.2  riz 	__hesiod_res_set(dns->hes_ctx, res, free_res);
    299  1.1.1.1.6.2  riz }
    300  1.1.1.1.6.2  riz #endif
    301  1.1.1.1.6.2  riz 
    302  1.1.1.1.6.2  riz /*! \file */
    303