17c478bd9Sstevel@tonic-gate /* 2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 37c478bd9Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium. 47c478bd9Sstevel@tonic-gate * 57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 87c478bd9Sstevel@tonic-gate * 9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 167c478bd9Sstevel@tonic-gate */ 177c478bd9Sstevel@tonic-gate 187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: dns_pr.c,v 1.5 2005/04/27 04:56:22 sra Exp $"; 207c478bd9Sstevel@tonic-gate #endif 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate /* Imports */ 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate #include "port_before.h" 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 277c478bd9Sstevel@tonic-gate #include <netinet/in.h> 287c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 297c478bd9Sstevel@tonic-gate #include <resolv.h> 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <stdio.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate #include <netdb.h> 347c478bd9Sstevel@tonic-gate #include <ctype.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include <isc/memcluster.h> 397c478bd9Sstevel@tonic-gate #include <irs.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include "port_after.h" 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include "irs_p.h" 447c478bd9Sstevel@tonic-gate #include "hesiod.h" 457c478bd9Sstevel@tonic-gate #include "dns_p.h" 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* Types. */ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate struct pvt { 507c478bd9Sstevel@tonic-gate struct dns_p * dns; 517c478bd9Sstevel@tonic-gate struct protoent proto; 527c478bd9Sstevel@tonic-gate char * prbuf; 537c478bd9Sstevel@tonic-gate }; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate /* Forward. */ 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate static void pr_close(struct irs_pr *); 587c478bd9Sstevel@tonic-gate static struct protoent * pr_byname(struct irs_pr *, const char *); 597c478bd9Sstevel@tonic-gate static struct protoent * pr_bynumber(struct irs_pr *, int); 607c478bd9Sstevel@tonic-gate static struct protoent * pr_next(struct irs_pr *); 617c478bd9Sstevel@tonic-gate static void pr_rewind(struct irs_pr *); 627c478bd9Sstevel@tonic-gate static void pr_minimize(struct irs_pr *); 637c478bd9Sstevel@tonic-gate static struct __res_state * pr_res_get(struct irs_pr *); 647c478bd9Sstevel@tonic-gate static void pr_res_set(struct irs_pr *, 657c478bd9Sstevel@tonic-gate struct __res_state *, 667c478bd9Sstevel@tonic-gate void (*)(void *)); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static struct protoent * parse_hes_list(struct irs_pr *, char **); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate /* Public. */ 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate struct irs_pr * 737c478bd9Sstevel@tonic-gate irs_dns_pr(struct irs_acc *this) { 747c478bd9Sstevel@tonic-gate struct dns_p *dns = (struct dns_p *)this->private; 757c478bd9Sstevel@tonic-gate struct pvt *pvt; 767c478bd9Sstevel@tonic-gate struct irs_pr *pr; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate if (!dns->hes_ctx) { 797c478bd9Sstevel@tonic-gate errno = ENODEV; 807c478bd9Sstevel@tonic-gate return (NULL); 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 837c478bd9Sstevel@tonic-gate errno = ENOMEM; 847c478bd9Sstevel@tonic-gate return (NULL); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 877c478bd9Sstevel@tonic-gate if (!(pr = memget(sizeof *pr))) { 887c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt); 897c478bd9Sstevel@tonic-gate errno = ENOMEM; 907c478bd9Sstevel@tonic-gate return (NULL); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate memset(pr, 0x5e, sizeof *pr); 937c478bd9Sstevel@tonic-gate pvt->dns = dns; 947c478bd9Sstevel@tonic-gate pr->private = pvt; 957c478bd9Sstevel@tonic-gate pr->byname = pr_byname; 967c478bd9Sstevel@tonic-gate pr->bynumber = pr_bynumber; 977c478bd9Sstevel@tonic-gate pr->next = pr_next; 987c478bd9Sstevel@tonic-gate pr->rewind = pr_rewind; 997c478bd9Sstevel@tonic-gate pr->close = pr_close; 1007c478bd9Sstevel@tonic-gate pr->minimize = pr_minimize; 1017c478bd9Sstevel@tonic-gate pr->res_get = pr_res_get; 1027c478bd9Sstevel@tonic-gate pr->res_set = pr_res_set; 1037c478bd9Sstevel@tonic-gate return (pr); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* Methods. */ 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate static void 1097c478bd9Sstevel@tonic-gate pr_close(struct irs_pr *this) { 1107c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate if (pvt->proto.p_aliases) 1137c478bd9Sstevel@tonic-gate free(pvt->proto.p_aliases); 1147c478bd9Sstevel@tonic-gate if (pvt->prbuf) 1157c478bd9Sstevel@tonic-gate free(pvt->prbuf); 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt); 1187c478bd9Sstevel@tonic-gate memput(this, sizeof *this); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate static struct protoent * 1227c478bd9Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) { 1237c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1247c478bd9Sstevel@tonic-gate struct dns_p *dns = pvt->dns; 1257c478bd9Sstevel@tonic-gate struct protoent *proto; 1267c478bd9Sstevel@tonic-gate char **hes_list; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "protocol"))) 1297c478bd9Sstevel@tonic-gate return (NULL); 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate proto = parse_hes_list(this, hes_list); 1327c478bd9Sstevel@tonic-gate hesiod_free_list(dns->hes_ctx, hes_list); 1337c478bd9Sstevel@tonic-gate return (proto); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate static struct protoent * 1377c478bd9Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int num) { 1387c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1397c478bd9Sstevel@tonic-gate struct dns_p *dns = pvt->dns; 1407c478bd9Sstevel@tonic-gate struct protoent *proto; 1417c478bd9Sstevel@tonic-gate char numstr[16]; 1427c478bd9Sstevel@tonic-gate char **hes_list; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate sprintf(numstr, "%d", num); 1457c478bd9Sstevel@tonic-gate if (!(hes_list = hesiod_resolve(dns->hes_ctx, numstr, "protonum"))) 1467c478bd9Sstevel@tonic-gate return (NULL); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate proto = parse_hes_list(this, hes_list); 1497c478bd9Sstevel@tonic-gate hesiod_free_list(dns->hes_ctx, hes_list); 1507c478bd9Sstevel@tonic-gate return (proto); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate static struct protoent * 1547c478bd9Sstevel@tonic-gate pr_next(struct irs_pr *this) { 1557c478bd9Sstevel@tonic-gate UNUSED(this); 1567c478bd9Sstevel@tonic-gate errno = ENODEV; 1577c478bd9Sstevel@tonic-gate return (NULL); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate static void 1617c478bd9Sstevel@tonic-gate pr_rewind(struct irs_pr *this) { 1627c478bd9Sstevel@tonic-gate UNUSED(this); 1637c478bd9Sstevel@tonic-gate /* NOOP */ 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate static void 1677c478bd9Sstevel@tonic-gate pr_minimize(struct irs_pr *this) { 1687c478bd9Sstevel@tonic-gate UNUSED(this); 1697c478bd9Sstevel@tonic-gate /* NOOP */ 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate static struct __res_state * 1737c478bd9Sstevel@tonic-gate pr_res_get(struct irs_pr *this) { 1747c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1757c478bd9Sstevel@tonic-gate struct dns_p *dns = pvt->dns; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate return (__hesiod_res_get(dns->hes_ctx)); 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate static void 1817c478bd9Sstevel@tonic-gate pr_res_set(struct irs_pr *this, struct __res_state * res, 1827c478bd9Sstevel@tonic-gate void (*free_res)(void *)) { 1837c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1847c478bd9Sstevel@tonic-gate struct dns_p *dns = pvt->dns; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate __hesiod_res_set(dns->hes_ctx, res, free_res); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* Private. */ 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate static struct protoent * 1927c478bd9Sstevel@tonic-gate parse_hes_list(struct irs_pr *this, char **hes_list) { 1937c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1947c478bd9Sstevel@tonic-gate char *p, *cp, **cpp, **new; 1957c478bd9Sstevel@tonic-gate int num = 0; 1967c478bd9Sstevel@tonic-gate int max = 0; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate for (cpp = hes_list; *cpp; cpp++) { 1997c478bd9Sstevel@tonic-gate cp = *cpp; 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate /* Strip away comments, if any. */ 2027c478bd9Sstevel@tonic-gate if ((p = strchr(cp, '#'))) 2037c478bd9Sstevel@tonic-gate *p = 0; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* Skip blank lines. */ 2067c478bd9Sstevel@tonic-gate p = cp; 2077c478bd9Sstevel@tonic-gate while (*p && !isspace((unsigned char)*p)) 2087c478bd9Sstevel@tonic-gate p++; 2097c478bd9Sstevel@tonic-gate if (!*p) 2107c478bd9Sstevel@tonic-gate continue; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate /* OK, we've got a live one. Let's parse it for real. */ 2137c478bd9Sstevel@tonic-gate if (pvt->prbuf) 2147c478bd9Sstevel@tonic-gate free(pvt->prbuf); 2157c478bd9Sstevel@tonic-gate pvt->prbuf = strdup(cp); 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate p = pvt->prbuf; 2187c478bd9Sstevel@tonic-gate pvt->proto.p_name = p; 2197c478bd9Sstevel@tonic-gate while (*p && !isspace((unsigned char)*p)) 2207c478bd9Sstevel@tonic-gate p++; 2217c478bd9Sstevel@tonic-gate if (!*p) 2227c478bd9Sstevel@tonic-gate continue; 2237c478bd9Sstevel@tonic-gate *p++ = '\0'; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate pvt->proto.p_proto = atoi(p); 2267c478bd9Sstevel@tonic-gate while (*p && !isspace((unsigned char)*p)) 2277c478bd9Sstevel@tonic-gate p++; 2287c478bd9Sstevel@tonic-gate if (*p) 2297c478bd9Sstevel@tonic-gate *p++ = '\0'; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate while (*p) { 2327c478bd9Sstevel@tonic-gate if ((num + 1) >= max || !pvt->proto.p_aliases) { 2337c478bd9Sstevel@tonic-gate max += 10; 2347c478bd9Sstevel@tonic-gate new = realloc(pvt->proto.p_aliases, 2357c478bd9Sstevel@tonic-gate max * sizeof(char *)); 2367c478bd9Sstevel@tonic-gate if (!new) { 2377c478bd9Sstevel@tonic-gate errno = ENOMEM; 2387c478bd9Sstevel@tonic-gate goto cleanup; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate pvt->proto.p_aliases = new; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate pvt->proto.p_aliases[num++] = p; 2437c478bd9Sstevel@tonic-gate while (*p && !isspace((unsigned char)*p)) 2447c478bd9Sstevel@tonic-gate p++; 2457c478bd9Sstevel@tonic-gate if (*p) 2467c478bd9Sstevel@tonic-gate *p++ = '\0'; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate if (!pvt->proto.p_aliases) 2497c478bd9Sstevel@tonic-gate pvt->proto.p_aliases = malloc(sizeof(char *)); 2507c478bd9Sstevel@tonic-gate if (!pvt->proto.p_aliases) 2517c478bd9Sstevel@tonic-gate goto cleanup; 2527c478bd9Sstevel@tonic-gate pvt->proto.p_aliases[num] = NULL; 2537c478bd9Sstevel@tonic-gate return (&pvt->proto); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate cleanup: 2577c478bd9Sstevel@tonic-gate if (pvt->proto.p_aliases) { 2587c478bd9Sstevel@tonic-gate free(pvt->proto.p_aliases); 2597c478bd9Sstevel@tonic-gate pvt->proto.p_aliases = NULL; 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate if (pvt->prbuf) { 2627c478bd9Sstevel@tonic-gate free(pvt->prbuf); 2637c478bd9Sstevel@tonic-gate pvt->prbuf = NULL; 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate return (NULL); 2667c478bd9Sstevel@tonic-gate } 267*9525b14bSRao Shoaib 268*9525b14bSRao Shoaib /*! \file */ 269