xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/dns_sv.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: dns_sv.c,v 1.5 2005/04/27 04:56:23 sra Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Imports */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <netinet/in.h>
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <netdb.h>
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <netinet/in.h>
380Sstevel@tonic-gate #include <arpa/nameser.h>
390Sstevel@tonic-gate #include <resolv.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <isc/memcluster.h>
420Sstevel@tonic-gate #include <irs.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include "port_after.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include "irs_p.h"
470Sstevel@tonic-gate #include "hesiod.h"
480Sstevel@tonic-gate #include "dns_p.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* Definitions */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate struct pvt {
530Sstevel@tonic-gate 	struct dns_p *		dns;
540Sstevel@tonic-gate 	struct servent		serv;
550Sstevel@tonic-gate 	char *			svbuf;
560Sstevel@tonic-gate 	struct __res_state *	res;
570Sstevel@tonic-gate 	void			(*free_res)(void *);
580Sstevel@tonic-gate };
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* Forward. */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static void 			sv_close(struct irs_sv *);
630Sstevel@tonic-gate static struct servent *		sv_byname(struct irs_sv *,
640Sstevel@tonic-gate 					  const char *, const char *);
650Sstevel@tonic-gate static struct servent *		sv_byport(struct irs_sv *, int, const char *);
660Sstevel@tonic-gate static struct servent *		sv_next(struct irs_sv *);
670Sstevel@tonic-gate static void			sv_rewind(struct irs_sv *);
680Sstevel@tonic-gate static void			sv_minimize(struct irs_sv *);
690Sstevel@tonic-gate #ifdef SV_RES_SETGET
700Sstevel@tonic-gate static struct __res_state *	sv_res_get(struct irs_sv *);
710Sstevel@tonic-gate static void			sv_res_set(struct irs_sv *,
720Sstevel@tonic-gate 					   struct __res_state *,
730Sstevel@tonic-gate 					   void (*)(void *));
740Sstevel@tonic-gate #endif
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static struct servent *		parse_hes_list(struct irs_sv *,
770Sstevel@tonic-gate 					       char **, const char *);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* Public */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate struct irs_sv *
irs_dns_sv(struct irs_acc * this)820Sstevel@tonic-gate irs_dns_sv(struct irs_acc *this) {
830Sstevel@tonic-gate 	struct dns_p *dns = (struct dns_p *)this->private;
840Sstevel@tonic-gate 	struct irs_sv *sv;
850Sstevel@tonic-gate 	struct pvt *pvt;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if (!dns || !dns->hes_ctx) {
880Sstevel@tonic-gate 		errno = ENODEV;
890Sstevel@tonic-gate 		return (NULL);
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
920Sstevel@tonic-gate 		errno = ENOMEM;
930Sstevel@tonic-gate 		return (NULL);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
960Sstevel@tonic-gate 	pvt->dns = dns;
970Sstevel@tonic-gate 	if (!(sv = memget(sizeof *sv))) {
980Sstevel@tonic-gate 		memput(pvt, sizeof *pvt);
990Sstevel@tonic-gate 		errno = ENOMEM;
1000Sstevel@tonic-gate 		return (NULL);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	memset(sv, 0x5e, sizeof *sv);
1030Sstevel@tonic-gate 	sv->private = pvt;
1040Sstevel@tonic-gate 	sv->byname = sv_byname;
1050Sstevel@tonic-gate 	sv->byport = sv_byport;
1060Sstevel@tonic-gate 	sv->next = sv_next;
1070Sstevel@tonic-gate 	sv->rewind = sv_rewind;
1080Sstevel@tonic-gate 	sv->close = sv_close;
1090Sstevel@tonic-gate 	sv->minimize = sv_minimize;
1100Sstevel@tonic-gate #ifdef SV_RES_SETGET
1110Sstevel@tonic-gate 	sv->res_get = sv_res_get;
1120Sstevel@tonic-gate 	sv->res_set = sv_res_set;
1130Sstevel@tonic-gate #else
114*11038SRao.Shoaib@Sun.COM 	sv->res_get = NULL; /*%< sv_res_get; */
115*11038SRao.Shoaib@Sun.COM 	sv->res_set = NULL; /*%< sv_res_set; */
1160Sstevel@tonic-gate #endif
1170Sstevel@tonic-gate 	return (sv);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /* Methods */
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static void
sv_close(struct irs_sv * this)1230Sstevel@tonic-gate sv_close(struct irs_sv *this) {
1240Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (pvt->serv.s_aliases)
1270Sstevel@tonic-gate 		free(pvt->serv.s_aliases);
1280Sstevel@tonic-gate 	if (pvt->svbuf)
1290Sstevel@tonic-gate 		free(pvt->svbuf);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	if (pvt->res && pvt->free_res)
1320Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
1330Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1340Sstevel@tonic-gate 	memput(this, sizeof *this);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static struct servent *
sv_byname(struct irs_sv * this,const char * name,const char * proto)1380Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) {
1390Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1400Sstevel@tonic-gate 	struct dns_p *dns = pvt->dns;
1410Sstevel@tonic-gate 	struct servent *s;
1420Sstevel@tonic-gate 	char **hes_list;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "service")))
1450Sstevel@tonic-gate 		return (NULL);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	s = parse_hes_list(this, hes_list, proto);
1480Sstevel@tonic-gate 	hesiod_free_list(dns->hes_ctx, hes_list);
1490Sstevel@tonic-gate 	return (s);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate static struct servent *
sv_byport(struct irs_sv * this,int port,const char * proto)1530Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) {
1540Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1550Sstevel@tonic-gate 	struct dns_p *dns = pvt->dns;
1560Sstevel@tonic-gate 	struct servent *s;
1570Sstevel@tonic-gate 	char portstr[16];
1580Sstevel@tonic-gate 	char **hes_list;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	sprintf(portstr, "%d", ntohs(port));
1610Sstevel@tonic-gate 	if (!(hes_list = hesiod_resolve(dns->hes_ctx, portstr, "port")))
1620Sstevel@tonic-gate 		return (NULL);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	s = parse_hes_list(this, hes_list, proto);
1650Sstevel@tonic-gate 	hesiod_free_list(dns->hes_ctx, hes_list);
1660Sstevel@tonic-gate 	return (s);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static struct servent *
sv_next(struct irs_sv * this)1700Sstevel@tonic-gate sv_next(struct irs_sv *this) {
1710Sstevel@tonic-gate 	UNUSED(this);
1720Sstevel@tonic-gate 	errno = ENODEV;
1730Sstevel@tonic-gate 	return (NULL);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static void
sv_rewind(struct irs_sv * this)1770Sstevel@tonic-gate sv_rewind(struct irs_sv *this) {
1780Sstevel@tonic-gate 	UNUSED(this);
1790Sstevel@tonic-gate 	/* NOOP */
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /* Private */
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static struct servent *
parse_hes_list(struct irs_sv * this,char ** hes_list,const char * proto)1850Sstevel@tonic-gate parse_hes_list(struct irs_sv *this, char **hes_list, const char *proto) {
1860Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1870Sstevel@tonic-gate 	char *p, *cp, **cpp, **new;
1880Sstevel@tonic-gate 	int proto_len;
1890Sstevel@tonic-gate 	int num = 0;
1900Sstevel@tonic-gate 	int max = 0;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	for (cpp = hes_list; *cpp; cpp++) {
1930Sstevel@tonic-gate 		cp = *cpp;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		/* Strip away comments, if any. */
1960Sstevel@tonic-gate 		if ((p = strchr(cp, '#')))
1970Sstevel@tonic-gate 			*p = 0;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		/* Check to make sure the protocol matches. */
2000Sstevel@tonic-gate 		p = cp;
2010Sstevel@tonic-gate 		while (*p && !isspace((unsigned char)*p))
2020Sstevel@tonic-gate 			p++;
2030Sstevel@tonic-gate 		if (!*p)
2040Sstevel@tonic-gate 			continue;
2050Sstevel@tonic-gate 		if (proto) {
2060Sstevel@tonic-gate 		     proto_len = strlen(proto);
2070Sstevel@tonic-gate 		     if (strncasecmp(++p, proto, proto_len) != 0)
2080Sstevel@tonic-gate 			  continue;
2090Sstevel@tonic-gate 		     if (p[proto_len] && !isspace(p[proto_len]&0xff))
2100Sstevel@tonic-gate 			  continue;
2110Sstevel@tonic-gate 		}
2120Sstevel@tonic-gate 		/* OK, we've got a live one.  Let's parse it for real. */
2130Sstevel@tonic-gate 		if (pvt->svbuf)
2140Sstevel@tonic-gate 			free(pvt->svbuf);
2150Sstevel@tonic-gate 		pvt->svbuf = strdup(cp);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		p = pvt->svbuf;
2180Sstevel@tonic-gate 		pvt->serv.s_name = p;
2190Sstevel@tonic-gate 		while (*p && !isspace(*p&0xff))
2200Sstevel@tonic-gate 			p++;
2210Sstevel@tonic-gate 		if (!*p)
2220Sstevel@tonic-gate 			continue;
2230Sstevel@tonic-gate 		*p++ = '\0';
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		pvt->serv.s_proto = p;
2260Sstevel@tonic-gate 		while (*p && !isspace(*p&0xff))
2270Sstevel@tonic-gate 			p++;
2280Sstevel@tonic-gate 		if (!*p)
2290Sstevel@tonic-gate 			continue;
2300Sstevel@tonic-gate 		*p++ = '\0';
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		pvt->serv.s_port = htons((u_short) atoi(p));
2330Sstevel@tonic-gate 		while (*p && !isspace(*p&0xff))
2340Sstevel@tonic-gate 			p++;
2350Sstevel@tonic-gate 		if (*p)
2360Sstevel@tonic-gate 			*p++ = '\0';
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		while (*p) {
2390Sstevel@tonic-gate 			if ((num + 1) >= max || !pvt->serv.s_aliases) {
2400Sstevel@tonic-gate 				max += 10;
2410Sstevel@tonic-gate 				new = realloc(pvt->serv.s_aliases,
2420Sstevel@tonic-gate 					      max * sizeof(char *));
2430Sstevel@tonic-gate 				if (!new) {
2440Sstevel@tonic-gate 					errno = ENOMEM;
2450Sstevel@tonic-gate 					goto cleanup;
2460Sstevel@tonic-gate 				}
2470Sstevel@tonic-gate 				pvt->serv.s_aliases = new;
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 			pvt->serv.s_aliases[num++] = p;
2500Sstevel@tonic-gate 			while (*p && !isspace(*p&0xff))
2510Sstevel@tonic-gate 				p++;
2520Sstevel@tonic-gate 			if (*p)
2530Sstevel@tonic-gate 				*p++ = '\0';
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 		if (!pvt->serv.s_aliases)
2560Sstevel@tonic-gate 			pvt->serv.s_aliases = malloc(sizeof(char *));
2570Sstevel@tonic-gate 		if (!pvt->serv.s_aliases)
2580Sstevel@tonic-gate 			goto cleanup;
2590Sstevel@tonic-gate 		pvt->serv.s_aliases[num] = NULL;
2600Sstevel@tonic-gate 		return (&pvt->serv);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate  cleanup:
2640Sstevel@tonic-gate 	if (pvt->serv.s_aliases) {
2650Sstevel@tonic-gate 		free(pvt->serv.s_aliases);
2660Sstevel@tonic-gate 		pvt->serv.s_aliases = NULL;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 	if (pvt->svbuf) {
2690Sstevel@tonic-gate 		free(pvt->svbuf);
2700Sstevel@tonic-gate 		pvt->svbuf = NULL;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	return (NULL);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate static void
sv_minimize(struct irs_sv * this)2760Sstevel@tonic-gate sv_minimize(struct irs_sv *this) {
2770Sstevel@tonic-gate 	UNUSED(this);
2780Sstevel@tonic-gate 	/* NOOP */
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate #ifdef SV_RES_SETGET
2820Sstevel@tonic-gate static struct __res_state *
sv_res_get(struct irs_sv * this)2830Sstevel@tonic-gate sv_res_get(struct irs_sv *this) {
2840Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2850Sstevel@tonic-gate 	struct dns_p *dns = pvt->dns;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	return (__hesiod_res_get(dns->hes_ctx));
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate static void
sv_res_set(struct irs_sv * this,struct __res_state * res,void (* free_res)(void *))2910Sstevel@tonic-gate sv_res_set(struct irs_sv *this, struct __res_state * res,
2920Sstevel@tonic-gate 	   void (*free_res)(void *)) {
2930Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2940Sstevel@tonic-gate 	struct dns_p *dns = pvt->dns;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	__hesiod_res_set(dns->hes_ctx, res, free_res);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate #endif
299*11038SRao.Shoaib@Sun.COM 
300*11038SRao.Shoaib@Sun.COM /*! \file */
301