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_pr.c,v 1.5 2005/04/27 04:56:22 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 #include <arpa/nameser.h>
290Sstevel@tonic-gate #include <resolv.h>
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <netdb.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <isc/memcluster.h>
390Sstevel@tonic-gate #include <irs.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include "port_after.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate #include "irs_p.h"
440Sstevel@tonic-gate #include "hesiod.h"
450Sstevel@tonic-gate #include "dns_p.h"
460Sstevel@tonic-gate
470Sstevel@tonic-gate /* Types. */
480Sstevel@tonic-gate
490Sstevel@tonic-gate struct pvt {
500Sstevel@tonic-gate struct dns_p * dns;
510Sstevel@tonic-gate struct protoent proto;
520Sstevel@tonic-gate char * prbuf;
530Sstevel@tonic-gate };
540Sstevel@tonic-gate
550Sstevel@tonic-gate /* Forward. */
560Sstevel@tonic-gate
570Sstevel@tonic-gate static void pr_close(struct irs_pr *);
580Sstevel@tonic-gate static struct protoent * pr_byname(struct irs_pr *, const char *);
590Sstevel@tonic-gate static struct protoent * pr_bynumber(struct irs_pr *, int);
600Sstevel@tonic-gate static struct protoent * pr_next(struct irs_pr *);
610Sstevel@tonic-gate static void pr_rewind(struct irs_pr *);
620Sstevel@tonic-gate static void pr_minimize(struct irs_pr *);
630Sstevel@tonic-gate static struct __res_state * pr_res_get(struct irs_pr *);
640Sstevel@tonic-gate static void pr_res_set(struct irs_pr *,
650Sstevel@tonic-gate struct __res_state *,
660Sstevel@tonic-gate void (*)(void *));
670Sstevel@tonic-gate
680Sstevel@tonic-gate static struct protoent * parse_hes_list(struct irs_pr *, char **);
690Sstevel@tonic-gate
700Sstevel@tonic-gate /* Public. */
710Sstevel@tonic-gate
720Sstevel@tonic-gate struct irs_pr *
irs_dns_pr(struct irs_acc * this)730Sstevel@tonic-gate irs_dns_pr(struct irs_acc *this) {
740Sstevel@tonic-gate struct dns_p *dns = (struct dns_p *)this->private;
750Sstevel@tonic-gate struct pvt *pvt;
760Sstevel@tonic-gate struct irs_pr *pr;
770Sstevel@tonic-gate
780Sstevel@tonic-gate if (!dns->hes_ctx) {
790Sstevel@tonic-gate errno = ENODEV;
800Sstevel@tonic-gate return (NULL);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
830Sstevel@tonic-gate errno = ENOMEM;
840Sstevel@tonic-gate return (NULL);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
870Sstevel@tonic-gate if (!(pr = memget(sizeof *pr))) {
880Sstevel@tonic-gate memput(pvt, sizeof *pvt);
890Sstevel@tonic-gate errno = ENOMEM;
900Sstevel@tonic-gate return (NULL);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate memset(pr, 0x5e, sizeof *pr);
930Sstevel@tonic-gate pvt->dns = dns;
940Sstevel@tonic-gate pr->private = pvt;
950Sstevel@tonic-gate pr->byname = pr_byname;
960Sstevel@tonic-gate pr->bynumber = pr_bynumber;
970Sstevel@tonic-gate pr->next = pr_next;
980Sstevel@tonic-gate pr->rewind = pr_rewind;
990Sstevel@tonic-gate pr->close = pr_close;
1000Sstevel@tonic-gate pr->minimize = pr_minimize;
1010Sstevel@tonic-gate pr->res_get = pr_res_get;
1020Sstevel@tonic-gate pr->res_set = pr_res_set;
1030Sstevel@tonic-gate return (pr);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate /* Methods. */
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate static void
pr_close(struct irs_pr * this)1090Sstevel@tonic-gate pr_close(struct irs_pr *this) {
1100Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate if (pvt->proto.p_aliases)
1130Sstevel@tonic-gate free(pvt->proto.p_aliases);
1140Sstevel@tonic-gate if (pvt->prbuf)
1150Sstevel@tonic-gate free(pvt->prbuf);
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1180Sstevel@tonic-gate memput(this, sizeof *this);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate static struct protoent *
pr_byname(struct irs_pr * this,const char * name)1220Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) {
1230Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1240Sstevel@tonic-gate struct dns_p *dns = pvt->dns;
1250Sstevel@tonic-gate struct protoent *proto;
1260Sstevel@tonic-gate char **hes_list;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "protocol")))
1290Sstevel@tonic-gate return (NULL);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate proto = parse_hes_list(this, hes_list);
1320Sstevel@tonic-gate hesiod_free_list(dns->hes_ctx, hes_list);
1330Sstevel@tonic-gate return (proto);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate static struct protoent *
pr_bynumber(struct irs_pr * this,int num)1370Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int num) {
1380Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1390Sstevel@tonic-gate struct dns_p *dns = pvt->dns;
1400Sstevel@tonic-gate struct protoent *proto;
1410Sstevel@tonic-gate char numstr[16];
1420Sstevel@tonic-gate char **hes_list;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate sprintf(numstr, "%d", num);
1450Sstevel@tonic-gate if (!(hes_list = hesiod_resolve(dns->hes_ctx, numstr, "protonum")))
1460Sstevel@tonic-gate return (NULL);
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate proto = parse_hes_list(this, hes_list);
1490Sstevel@tonic-gate hesiod_free_list(dns->hes_ctx, hes_list);
1500Sstevel@tonic-gate return (proto);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate static struct protoent *
pr_next(struct irs_pr * this)1540Sstevel@tonic-gate pr_next(struct irs_pr *this) {
1550Sstevel@tonic-gate UNUSED(this);
1560Sstevel@tonic-gate errno = ENODEV;
1570Sstevel@tonic-gate return (NULL);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate static void
pr_rewind(struct irs_pr * this)1610Sstevel@tonic-gate pr_rewind(struct irs_pr *this) {
1620Sstevel@tonic-gate UNUSED(this);
1630Sstevel@tonic-gate /* NOOP */
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate static void
pr_minimize(struct irs_pr * this)1670Sstevel@tonic-gate pr_minimize(struct irs_pr *this) {
1680Sstevel@tonic-gate UNUSED(this);
1690Sstevel@tonic-gate /* NOOP */
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate static struct __res_state *
pr_res_get(struct irs_pr * this)1730Sstevel@tonic-gate pr_res_get(struct irs_pr *this) {
1740Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1750Sstevel@tonic-gate struct dns_p *dns = pvt->dns;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate return (__hesiod_res_get(dns->hes_ctx));
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate static void
pr_res_set(struct irs_pr * this,struct __res_state * res,void (* free_res)(void *))1810Sstevel@tonic-gate pr_res_set(struct irs_pr *this, struct __res_state * res,
1820Sstevel@tonic-gate void (*free_res)(void *)) {
1830Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1840Sstevel@tonic-gate struct dns_p *dns = pvt->dns;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate __hesiod_res_set(dns->hes_ctx, res, free_res);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate /* Private. */
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate static struct protoent *
parse_hes_list(struct irs_pr * this,char ** hes_list)1920Sstevel@tonic-gate parse_hes_list(struct irs_pr *this, char **hes_list) {
1930Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1940Sstevel@tonic-gate char *p, *cp, **cpp, **new;
1950Sstevel@tonic-gate int num = 0;
1960Sstevel@tonic-gate int max = 0;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate for (cpp = hes_list; *cpp; cpp++) {
1990Sstevel@tonic-gate cp = *cpp;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /* Strip away comments, if any. */
2020Sstevel@tonic-gate if ((p = strchr(cp, '#')))
2030Sstevel@tonic-gate *p = 0;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /* Skip blank lines. */
2060Sstevel@tonic-gate p = cp;
2070Sstevel@tonic-gate while (*p && !isspace((unsigned char)*p))
2080Sstevel@tonic-gate p++;
2090Sstevel@tonic-gate if (!*p)
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->prbuf)
2140Sstevel@tonic-gate free(pvt->prbuf);
2150Sstevel@tonic-gate pvt->prbuf = strdup(cp);
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate p = pvt->prbuf;
2180Sstevel@tonic-gate pvt->proto.p_name = p;
2190Sstevel@tonic-gate while (*p && !isspace((unsigned char)*p))
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->proto.p_proto = atoi(p);
2260Sstevel@tonic-gate while (*p && !isspace((unsigned char)*p))
2270Sstevel@tonic-gate p++;
2280Sstevel@tonic-gate if (*p)
2290Sstevel@tonic-gate *p++ = '\0';
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate while (*p) {
2320Sstevel@tonic-gate if ((num + 1) >= max || !pvt->proto.p_aliases) {
2330Sstevel@tonic-gate max += 10;
2340Sstevel@tonic-gate new = realloc(pvt->proto.p_aliases,
2350Sstevel@tonic-gate max * sizeof(char *));
2360Sstevel@tonic-gate if (!new) {
2370Sstevel@tonic-gate errno = ENOMEM;
2380Sstevel@tonic-gate goto cleanup;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate pvt->proto.p_aliases = new;
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate pvt->proto.p_aliases[num++] = p;
2430Sstevel@tonic-gate while (*p && !isspace((unsigned char)*p))
2440Sstevel@tonic-gate p++;
2450Sstevel@tonic-gate if (*p)
2460Sstevel@tonic-gate *p++ = '\0';
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate if (!pvt->proto.p_aliases)
2490Sstevel@tonic-gate pvt->proto.p_aliases = malloc(sizeof(char *));
2500Sstevel@tonic-gate if (!pvt->proto.p_aliases)
2510Sstevel@tonic-gate goto cleanup;
2520Sstevel@tonic-gate pvt->proto.p_aliases[num] = NULL;
2530Sstevel@tonic-gate return (&pvt->proto);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate cleanup:
2570Sstevel@tonic-gate if (pvt->proto.p_aliases) {
2580Sstevel@tonic-gate free(pvt->proto.p_aliases);
2590Sstevel@tonic-gate pvt->proto.p_aliases = NULL;
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate if (pvt->prbuf) {
2620Sstevel@tonic-gate free(pvt->prbuf);
2630Sstevel@tonic-gate pvt->prbuf = NULL;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate return (NULL);
2660Sstevel@tonic-gate }
267*11038SRao.Shoaib@Sun.COM
268*11038SRao.Shoaib@Sun.COM /*! \file */
269