Home | History | Annotate | Line # | Download | only in irs
dns_sv.c revision 1.1.1.1.14.1
      1  1.1.1.1.14.1      yamt /*	$NetBSD: dns_sv.c,v 1.1.1.1.14.1 2012/10/30 18:55:26 yamt 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  * Copyright (c) 1996,1999 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.1.1.14.1      yamt static const char rcsid[] = "Id: dns_sv.c,v 1.5 2005/04/27 04:56:23 sra Exp ";
     22           1.1  christos #endif
     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 <sys/types.h>
     29           1.1  christos #include <netinet/in.h>
     30           1.1  christos 
     31           1.1  christos #include <stdio.h>
     32           1.1  christos #include <string.h>
     33           1.1  christos #include <netdb.h>
     34           1.1  christos #include <ctype.h>
     35           1.1  christos #include <stdlib.h>
     36           1.1  christos #include <errno.h>
     37           1.1  christos 
     38           1.1  christos #include <sys/types.h>
     39           1.1  christos #include <netinet/in.h>
     40           1.1  christos #include <arpa/nameser.h>
     41           1.1  christos #include <resolv.h>
     42           1.1  christos 
     43           1.1  christos #include <isc/memcluster.h>
     44           1.1  christos #include <irs.h>
     45           1.1  christos 
     46           1.1  christos #include "port_after.h"
     47           1.1  christos 
     48           1.1  christos #include "irs_p.h"
     49           1.1  christos #include "hesiod.h"
     50           1.1  christos #include "dns_p.h"
     51           1.1  christos 
     52           1.1  christos /* Definitions */
     53           1.1  christos 
     54           1.1  christos struct pvt {
     55           1.1  christos 	struct dns_p *		dns;
     56           1.1  christos 	struct servent		serv;
     57           1.1  christos 	char *			svbuf;
     58           1.1  christos 	struct __res_state *	res;
     59           1.1  christos 	void			(*free_res)(void *);
     60           1.1  christos };
     61           1.1  christos 
     62           1.1  christos /* Forward. */
     63           1.1  christos 
     64           1.1  christos static void 			sv_close(struct irs_sv *);
     65           1.1  christos static struct servent *		sv_byname(struct irs_sv *,
     66           1.1  christos 					  const char *, const char *);
     67           1.1  christos static struct servent *		sv_byport(struct irs_sv *, int, const char *);
     68           1.1  christos static struct servent *		sv_next(struct irs_sv *);
     69           1.1  christos static void			sv_rewind(struct irs_sv *);
     70           1.1  christos static void			sv_minimize(struct irs_sv *);
     71           1.1  christos #ifdef SV_RES_SETGET
     72           1.1  christos static struct __res_state *	sv_res_get(struct irs_sv *);
     73           1.1  christos static void			sv_res_set(struct irs_sv *,
     74           1.1  christos 					   struct __res_state *,
     75           1.1  christos 					   void (*)(void *));
     76           1.1  christos #endif
     77           1.1  christos 
     78           1.1  christos static struct servent *		parse_hes_list(struct irs_sv *,
     79           1.1  christos 					       char **, const char *);
     80           1.1  christos 
     81           1.1  christos /* Public */
     82           1.1  christos 
     83           1.1  christos struct irs_sv *
     84           1.1  christos irs_dns_sv(struct irs_acc *this) {
     85           1.1  christos 	struct dns_p *dns = (struct dns_p *)this->private;
     86           1.1  christos 	struct irs_sv *sv;
     87           1.1  christos 	struct pvt *pvt;
     88           1.1  christos 
     89           1.1  christos 	if (!dns || !dns->hes_ctx) {
     90           1.1  christos 		errno = ENODEV;
     91           1.1  christos 		return (NULL);
     92           1.1  christos 	}
     93           1.1  christos 	if (!(pvt = memget(sizeof *pvt))) {
     94           1.1  christos 		errno = ENOMEM;
     95           1.1  christos 		return (NULL);
     96           1.1  christos 	}
     97           1.1  christos 	memset(pvt, 0, sizeof *pvt);
     98           1.1  christos 	pvt->dns = dns;
     99           1.1  christos 	if (!(sv = memget(sizeof *sv))) {
    100           1.1  christos 		memput(pvt, sizeof *pvt);
    101           1.1  christos 		errno = ENOMEM;
    102           1.1  christos 		return (NULL);
    103           1.1  christos 	}
    104           1.1  christos 	memset(sv, 0x5e, sizeof *sv);
    105           1.1  christos 	sv->private = pvt;
    106           1.1  christos 	sv->byname = sv_byname;
    107           1.1  christos 	sv->byport = sv_byport;
    108           1.1  christos 	sv->next = sv_next;
    109           1.1  christos 	sv->rewind = sv_rewind;
    110           1.1  christos 	sv->close = sv_close;
    111           1.1  christos 	sv->minimize = sv_minimize;
    112           1.1  christos #ifdef SV_RES_SETGET
    113           1.1  christos 	sv->res_get = sv_res_get;
    114           1.1  christos 	sv->res_set = sv_res_set;
    115           1.1  christos #else
    116           1.1  christos 	sv->res_get = NULL; /*%< sv_res_get; */
    117           1.1  christos 	sv->res_set = NULL; /*%< sv_res_set; */
    118           1.1  christos #endif
    119           1.1  christos 	return (sv);
    120           1.1  christos }
    121           1.1  christos 
    122           1.1  christos /* Methods */
    123           1.1  christos 
    124           1.1  christos static void
    125           1.1  christos sv_close(struct irs_sv *this) {
    126           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    127           1.1  christos 
    128           1.1  christos 	if (pvt->serv.s_aliases)
    129           1.1  christos 		free(pvt->serv.s_aliases);
    130           1.1  christos 	if (pvt->svbuf)
    131           1.1  christos 		free(pvt->svbuf);
    132           1.1  christos 
    133           1.1  christos 	if (pvt->res && pvt->free_res)
    134           1.1  christos 		(*pvt->free_res)(pvt->res);
    135           1.1  christos 	memput(pvt, sizeof *pvt);
    136           1.1  christos 	memput(this, sizeof *this);
    137           1.1  christos }
    138           1.1  christos 
    139           1.1  christos static struct servent *
    140           1.1  christos sv_byname(struct irs_sv *this, const char *name, const char *proto) {
    141           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    142           1.1  christos 	struct dns_p *dns = pvt->dns;
    143           1.1  christos 	struct servent *s;
    144           1.1  christos 	char **hes_list;
    145           1.1  christos 
    146           1.1  christos 	if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "service")))
    147           1.1  christos 		return (NULL);
    148           1.1  christos 
    149           1.1  christos 	s = parse_hes_list(this, hes_list, proto);
    150           1.1  christos 	hesiod_free_list(dns->hes_ctx, hes_list);
    151           1.1  christos 	return (s);
    152           1.1  christos }
    153           1.1  christos 
    154           1.1  christos static struct servent *
    155           1.1  christos sv_byport(struct irs_sv *this, int port, const char *proto) {
    156           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    157           1.1  christos 	struct dns_p *dns = pvt->dns;
    158           1.1  christos 	struct servent *s;
    159           1.1  christos 	char portstr[16];
    160           1.1  christos 	char **hes_list;
    161           1.1  christos 
    162           1.1  christos 	sprintf(portstr, "%d", ntohs(port));
    163           1.1  christos 	if (!(hes_list = hesiod_resolve(dns->hes_ctx, portstr, "port")))
    164           1.1  christos 		return (NULL);
    165           1.1  christos 
    166           1.1  christos 	s = parse_hes_list(this, hes_list, proto);
    167           1.1  christos 	hesiod_free_list(dns->hes_ctx, hes_list);
    168           1.1  christos 	return (s);
    169           1.1  christos }
    170           1.1  christos 
    171           1.1  christos static struct servent *
    172           1.1  christos sv_next(struct irs_sv *this) {
    173           1.1  christos 	UNUSED(this);
    174           1.1  christos 	errno = ENODEV;
    175           1.1  christos 	return (NULL);
    176           1.1  christos }
    177           1.1  christos 
    178           1.1  christos static void
    179           1.1  christos sv_rewind(struct irs_sv *this) {
    180           1.1  christos 	UNUSED(this);
    181           1.1  christos 	/* NOOP */
    182           1.1  christos }
    183           1.1  christos 
    184           1.1  christos /* Private */
    185           1.1  christos 
    186           1.1  christos static struct servent *
    187           1.1  christos parse_hes_list(struct irs_sv *this, char **hes_list, const char *proto) {
    188           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    189           1.1  christos 	char *p, *cp, **cpp, **new;
    190           1.1  christos 	int proto_len;
    191           1.1  christos 	int num = 0;
    192           1.1  christos 	int max = 0;
    193           1.1  christos 
    194           1.1  christos 	for (cpp = hes_list; *cpp; cpp++) {
    195           1.1  christos 		cp = *cpp;
    196           1.1  christos 
    197           1.1  christos 		/* Strip away comments, if any. */
    198           1.1  christos 		if ((p = strchr(cp, '#')))
    199           1.1  christos 			*p = 0;
    200           1.1  christos 
    201           1.1  christos 		/* Check to make sure the protocol matches. */
    202           1.1  christos 		p = cp;
    203           1.1  christos 		while (*p && !isspace((unsigned char)*p))
    204           1.1  christos 			p++;
    205           1.1  christos 		if (!*p)
    206           1.1  christos 			continue;
    207           1.1  christos 		if (proto) {
    208           1.1  christos 		     proto_len = strlen(proto);
    209           1.1  christos 		     if (strncasecmp(++p, proto, proto_len) != 0)
    210           1.1  christos 			  continue;
    211           1.1  christos 		     if (p[proto_len] && !isspace(p[proto_len]&0xff))
    212           1.1  christos 			  continue;
    213           1.1  christos 		}
    214           1.1  christos 		/* OK, we've got a live one.  Let's parse it for real. */
    215           1.1  christos 		if (pvt->svbuf)
    216           1.1  christos 			free(pvt->svbuf);
    217           1.1  christos 		pvt->svbuf = strdup(cp);
    218           1.1  christos 
    219           1.1  christos 		p = pvt->svbuf;
    220           1.1  christos 		pvt->serv.s_name = p;
    221           1.1  christos 		while (*p && !isspace(*p&0xff))
    222           1.1  christos 			p++;
    223           1.1  christos 		if (!*p)
    224           1.1  christos 			continue;
    225           1.1  christos 		*p++ = '\0';
    226           1.1  christos 
    227           1.1  christos 		pvt->serv.s_proto = p;
    228           1.1  christos 		while (*p && !isspace(*p&0xff))
    229           1.1  christos 			p++;
    230           1.1  christos 		if (!*p)
    231           1.1  christos 			continue;
    232           1.1  christos 		*p++ = '\0';
    233           1.1  christos 
    234           1.1  christos 		pvt->serv.s_port = htons((u_short) atoi(p));
    235           1.1  christos 		while (*p && !isspace(*p&0xff))
    236           1.1  christos 			p++;
    237           1.1  christos 		if (*p)
    238           1.1  christos 			*p++ = '\0';
    239           1.1  christos 
    240           1.1  christos 		while (*p) {
    241           1.1  christos 			if ((num + 1) >= max || !pvt->serv.s_aliases) {
    242           1.1  christos 				max += 10;
    243           1.1  christos 				new = realloc(pvt->serv.s_aliases,
    244           1.1  christos 					      max * sizeof(char *));
    245           1.1  christos 				if (!new) {
    246           1.1  christos 					errno = ENOMEM;
    247           1.1  christos 					goto cleanup;
    248           1.1  christos 				}
    249           1.1  christos 				pvt->serv.s_aliases = new;
    250           1.1  christos 			}
    251           1.1  christos 			pvt->serv.s_aliases[num++] = p;
    252           1.1  christos 			while (*p && !isspace(*p&0xff))
    253           1.1  christos 				p++;
    254           1.1  christos 			if (*p)
    255           1.1  christos 				*p++ = '\0';
    256           1.1  christos 		}
    257           1.1  christos 		if (!pvt->serv.s_aliases)
    258           1.1  christos 			pvt->serv.s_aliases = malloc(sizeof(char *));
    259           1.1  christos 		if (!pvt->serv.s_aliases)
    260           1.1  christos 			goto cleanup;
    261           1.1  christos 		pvt->serv.s_aliases[num] = NULL;
    262           1.1  christos 		return (&pvt->serv);
    263           1.1  christos 	}
    264           1.1  christos 
    265           1.1  christos  cleanup:
    266           1.1  christos 	if (pvt->serv.s_aliases) {
    267           1.1  christos 		free(pvt->serv.s_aliases);
    268           1.1  christos 		pvt->serv.s_aliases = NULL;
    269           1.1  christos 	}
    270           1.1  christos 	if (pvt->svbuf) {
    271           1.1  christos 		free(pvt->svbuf);
    272           1.1  christos 		pvt->svbuf = NULL;
    273           1.1  christos 	}
    274           1.1  christos 	return (NULL);
    275           1.1  christos }
    276           1.1  christos 
    277           1.1  christos static void
    278           1.1  christos sv_minimize(struct irs_sv *this) {
    279           1.1  christos 	UNUSED(this);
    280           1.1  christos 	/* NOOP */
    281           1.1  christos }
    282           1.1  christos 
    283           1.1  christos #ifdef SV_RES_SETGET
    284           1.1  christos static struct __res_state *
    285           1.1  christos sv_res_get(struct irs_sv *this) {
    286           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    287           1.1  christos 	struct dns_p *dns = pvt->dns;
    288           1.1  christos 
    289           1.1  christos 	return (__hesiod_res_get(dns->hes_ctx));
    290           1.1  christos }
    291           1.1  christos 
    292           1.1  christos static void
    293           1.1  christos sv_res_set(struct irs_sv *this, struct __res_state * res,
    294           1.1  christos 	   void (*free_res)(void *)) {
    295           1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    296           1.1  christos 	struct dns_p *dns = pvt->dns;
    297           1.1  christos 
    298           1.1  christos 	__hesiod_res_set(dns->hes_ctx, res, free_res);
    299           1.1  christos }
    300           1.1  christos #endif
    301           1.1  christos 
    302           1.1  christos /*! \file */
    303