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(LINT) && !defined(CODECENTER) 19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: gen_nw.c,v 1.4 2005/04/27 04:56:23 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 287c478bd9Sstevel@tonic-gate #include <netinet/in.h> 297c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <errno.h> 327c478bd9Sstevel@tonic-gate #include <resolv.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <string.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <isc/memcluster.h> 377c478bd9Sstevel@tonic-gate #include <irs.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #include "port_after.h" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include "irs_p.h" 427c478bd9Sstevel@tonic-gate #include "gen_p.h" 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* Types */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate struct pvt { 477c478bd9Sstevel@tonic-gate struct irs_rule * rules; 487c478bd9Sstevel@tonic-gate struct irs_rule * rule; 497c478bd9Sstevel@tonic-gate struct __res_state * res; 507c478bd9Sstevel@tonic-gate void (*free_res)(void *); 517c478bd9Sstevel@tonic-gate }; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* Forward */ 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate static void nw_close(struct irs_nw*); 567c478bd9Sstevel@tonic-gate static struct nwent * nw_next(struct irs_nw *); 577c478bd9Sstevel@tonic-gate static struct nwent * nw_byname(struct irs_nw *, const char *, int); 587c478bd9Sstevel@tonic-gate static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); 597c478bd9Sstevel@tonic-gate static void nw_rewind(struct irs_nw *); 607c478bd9Sstevel@tonic-gate static void nw_minimize(struct irs_nw *); 617c478bd9Sstevel@tonic-gate static struct __res_state * nw_res_get(struct irs_nw *this); 627c478bd9Sstevel@tonic-gate static void nw_res_set(struct irs_nw *this, 637c478bd9Sstevel@tonic-gate struct __res_state *res, 647c478bd9Sstevel@tonic-gate void (*free_res)(void *)); 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate static int init(struct irs_nw *this); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /* Public */ 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate struct irs_nw * 717c478bd9Sstevel@tonic-gate irs_gen_nw(struct irs_acc *this) { 727c478bd9Sstevel@tonic-gate struct gen_p *accpvt = (struct gen_p *)this->private; 737c478bd9Sstevel@tonic-gate struct irs_nw *nw; 747c478bd9Sstevel@tonic-gate struct pvt *pvt; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 777c478bd9Sstevel@tonic-gate errno = ENOMEM; 787c478bd9Sstevel@tonic-gate return (NULL); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 817c478bd9Sstevel@tonic-gate if (!(nw = memget(sizeof *nw))) { 827c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt); 837c478bd9Sstevel@tonic-gate errno = ENOMEM; 847c478bd9Sstevel@tonic-gate return (NULL); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate memset(nw, 0x5e, sizeof *nw); 877c478bd9Sstevel@tonic-gate pvt->rules = accpvt->map_rules[irs_nw]; 887c478bd9Sstevel@tonic-gate pvt->rule = pvt->rules; 897c478bd9Sstevel@tonic-gate nw->private = pvt; 907c478bd9Sstevel@tonic-gate nw->close = nw_close; 917c478bd9Sstevel@tonic-gate nw->next = nw_next; 927c478bd9Sstevel@tonic-gate nw->byname = nw_byname; 937c478bd9Sstevel@tonic-gate nw->byaddr = nw_byaddr; 947c478bd9Sstevel@tonic-gate nw->rewind = nw_rewind; 957c478bd9Sstevel@tonic-gate nw->minimize = nw_minimize; 967c478bd9Sstevel@tonic-gate nw->res_get = nw_res_get; 977c478bd9Sstevel@tonic-gate nw->res_set = nw_res_set; 987c478bd9Sstevel@tonic-gate return (nw); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* Methods */ 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate static void 1047c478bd9Sstevel@tonic-gate nw_close(struct irs_nw *this) { 1057c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate nw_minimize(this); 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res) 1107c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt); 1137c478bd9Sstevel@tonic-gate memput(this, sizeof *this); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate static struct nwent * 1177c478bd9Sstevel@tonic-gate nw_next(struct irs_nw *this) { 1187c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1197c478bd9Sstevel@tonic-gate struct nwent *rval; 1207c478bd9Sstevel@tonic-gate struct irs_nw *nw; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate if (init(this) == -1) 1237c478bd9Sstevel@tonic-gate return(NULL); 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate while (pvt->rule) { 1267c478bd9Sstevel@tonic-gate nw = pvt->rule->inst->nw; 1277c478bd9Sstevel@tonic-gate rval = (*nw->next)(nw); 1287c478bd9Sstevel@tonic-gate if (rval) 1297c478bd9Sstevel@tonic-gate return (rval); 1307c478bd9Sstevel@tonic-gate if (!(pvt->rules->flags & IRS_CONTINUE)) 1317c478bd9Sstevel@tonic-gate break; 1327c478bd9Sstevel@tonic-gate pvt->rule = pvt->rule->next; 1337c478bd9Sstevel@tonic-gate if (pvt->rule) { 1347c478bd9Sstevel@tonic-gate nw = pvt->rule->inst->nw; 1357c478bd9Sstevel@tonic-gate (*nw->rewind)(nw); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate return (NULL); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate static struct nwent * 1427c478bd9Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int type) { 1437c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1447c478bd9Sstevel@tonic-gate struct irs_rule *rule; 1457c478bd9Sstevel@tonic-gate struct nwent *rval; 1467c478bd9Sstevel@tonic-gate struct irs_nw *nw; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate if (init(this) == -1) 1497c478bd9Sstevel@tonic-gate return(NULL); 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 1527c478bd9Sstevel@tonic-gate nw = rule->inst->nw; 1537c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 1547c478bd9Sstevel@tonic-gate rval = (*nw->byname)(nw, name, type); 1557c478bd9Sstevel@tonic-gate if (rval != NULL) 1567c478bd9Sstevel@tonic-gate return (rval); 1577c478bd9Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN && 1587c478bd9Sstevel@tonic-gate !(rule->flags & IRS_CONTINUE)) 1597c478bd9Sstevel@tonic-gate break; 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate return (NULL); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate static struct nwent * 1657c478bd9Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int length, int type) { 1667c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1677c478bd9Sstevel@tonic-gate struct irs_rule *rule; 1687c478bd9Sstevel@tonic-gate struct nwent *rval; 1697c478bd9Sstevel@tonic-gate struct irs_nw *nw; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if (init(this) == -1) 1727c478bd9Sstevel@tonic-gate return(NULL); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 1757c478bd9Sstevel@tonic-gate nw = rule->inst->nw; 1767c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 1777c478bd9Sstevel@tonic-gate rval = (*nw->byaddr)(nw, net, length, type); 1787c478bd9Sstevel@tonic-gate if (rval != NULL) 1797c478bd9Sstevel@tonic-gate return (rval); 1807c478bd9Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN && 1817c478bd9Sstevel@tonic-gate !(rule->flags & IRS_CONTINUE)) 1827c478bd9Sstevel@tonic-gate break; 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate return (NULL); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate static void 1887c478bd9Sstevel@tonic-gate nw_rewind(struct irs_nw *this) { 1897c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1907c478bd9Sstevel@tonic-gate struct irs_nw *nw; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate pvt->rule = pvt->rules; 1937c478bd9Sstevel@tonic-gate if (pvt->rule) { 1947c478bd9Sstevel@tonic-gate nw = pvt->rule->inst->nw; 1957c478bd9Sstevel@tonic-gate (*nw->rewind)(nw); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate static void 2007c478bd9Sstevel@tonic-gate nw_minimize(struct irs_nw *this) { 2017c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2027c478bd9Sstevel@tonic-gate struct irs_rule *rule; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate if (pvt->res) 2057c478bd9Sstevel@tonic-gate res_nclose(pvt->res); 2067c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 2077c478bd9Sstevel@tonic-gate struct irs_nw *nw = rule->inst->nw; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate (*nw->minimize)(nw); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate static struct __res_state * 2147c478bd9Sstevel@tonic-gate nw_res_get(struct irs_nw *this) { 2157c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if (!pvt->res) { 2187c478bd9Sstevel@tonic-gate struct __res_state *res; 2197c478bd9Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res); 2207c478bd9Sstevel@tonic-gate if (!res) { 2217c478bd9Sstevel@tonic-gate errno = ENOMEM; 2227c478bd9Sstevel@tonic-gate return (NULL); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate memset(res, 0, sizeof *res); 2257c478bd9Sstevel@tonic-gate nw_res_set(this, res, free); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate return (pvt->res); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate static void 2327c478bd9Sstevel@tonic-gate nw_res_set(struct irs_nw *this, struct __res_state *res, 2337c478bd9Sstevel@tonic-gate void (*free_res)(void *)) { 2347c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2357c478bd9Sstevel@tonic-gate struct irs_rule *rule; 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res) { 2387c478bd9Sstevel@tonic-gate res_nclose(pvt->res); 2397c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate pvt->res = res; 2437c478bd9Sstevel@tonic-gate pvt->free_res = free_res; 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 2467c478bd9Sstevel@tonic-gate struct irs_nw *nw = rule->inst->nw; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate (*nw->res_set)(nw, pvt->res, NULL); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate static int 2537c478bd9Sstevel@tonic-gate init(struct irs_nw *this) { 2547c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (!pvt->res && !nw_res_get(this)) 2577c478bd9Sstevel@tonic-gate return (-1); 258*9525b14bSRao Shoaib if (((pvt->res->options & RES_INIT) == 0U) && 2597c478bd9Sstevel@tonic-gate res_ninit(pvt->res) == -1) 2607c478bd9Sstevel@tonic-gate return (-1); 2617c478bd9Sstevel@tonic-gate return (0); 2627c478bd9Sstevel@tonic-gate } 263*9525b14bSRao Shoaib 264*9525b14bSRao Shoaib /*! \file */ 265