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(LINT) && !defined(CODECENTER)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: gen_nw.c,v 1.4 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
280Sstevel@tonic-gate #include <netinet/in.h>
290Sstevel@tonic-gate #include <arpa/nameser.h>
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <resolv.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <isc/memcluster.h>
370Sstevel@tonic-gate #include <irs.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate #include "port_after.h"
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include "irs_p.h"
420Sstevel@tonic-gate #include "gen_p.h"
430Sstevel@tonic-gate
440Sstevel@tonic-gate /* Types */
450Sstevel@tonic-gate
460Sstevel@tonic-gate struct pvt {
470Sstevel@tonic-gate struct irs_rule * rules;
480Sstevel@tonic-gate struct irs_rule * rule;
490Sstevel@tonic-gate struct __res_state * res;
500Sstevel@tonic-gate void (*free_res)(void *);
510Sstevel@tonic-gate };
520Sstevel@tonic-gate
530Sstevel@tonic-gate /* Forward */
540Sstevel@tonic-gate
550Sstevel@tonic-gate static void nw_close(struct irs_nw*);
560Sstevel@tonic-gate static struct nwent * nw_next(struct irs_nw *);
570Sstevel@tonic-gate static struct nwent * nw_byname(struct irs_nw *, const char *, int);
580Sstevel@tonic-gate static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int);
590Sstevel@tonic-gate static void nw_rewind(struct irs_nw *);
600Sstevel@tonic-gate static void nw_minimize(struct irs_nw *);
610Sstevel@tonic-gate static struct __res_state * nw_res_get(struct irs_nw *this);
620Sstevel@tonic-gate static void nw_res_set(struct irs_nw *this,
630Sstevel@tonic-gate struct __res_state *res,
640Sstevel@tonic-gate void (*free_res)(void *));
650Sstevel@tonic-gate
660Sstevel@tonic-gate static int init(struct irs_nw *this);
670Sstevel@tonic-gate
680Sstevel@tonic-gate /* Public */
690Sstevel@tonic-gate
700Sstevel@tonic-gate struct irs_nw *
irs_gen_nw(struct irs_acc * this)710Sstevel@tonic-gate irs_gen_nw(struct irs_acc *this) {
720Sstevel@tonic-gate struct gen_p *accpvt = (struct gen_p *)this->private;
730Sstevel@tonic-gate struct irs_nw *nw;
740Sstevel@tonic-gate struct pvt *pvt;
750Sstevel@tonic-gate
760Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
770Sstevel@tonic-gate errno = ENOMEM;
780Sstevel@tonic-gate return (NULL);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
810Sstevel@tonic-gate if (!(nw = memget(sizeof *nw))) {
820Sstevel@tonic-gate memput(pvt, sizeof *pvt);
830Sstevel@tonic-gate errno = ENOMEM;
840Sstevel@tonic-gate return (NULL);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate memset(nw, 0x5e, sizeof *nw);
870Sstevel@tonic-gate pvt->rules = accpvt->map_rules[irs_nw];
880Sstevel@tonic-gate pvt->rule = pvt->rules;
890Sstevel@tonic-gate nw->private = pvt;
900Sstevel@tonic-gate nw->close = nw_close;
910Sstevel@tonic-gate nw->next = nw_next;
920Sstevel@tonic-gate nw->byname = nw_byname;
930Sstevel@tonic-gate nw->byaddr = nw_byaddr;
940Sstevel@tonic-gate nw->rewind = nw_rewind;
950Sstevel@tonic-gate nw->minimize = nw_minimize;
960Sstevel@tonic-gate nw->res_get = nw_res_get;
970Sstevel@tonic-gate nw->res_set = nw_res_set;
980Sstevel@tonic-gate return (nw);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /* Methods */
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate static void
nw_close(struct irs_nw * this)1040Sstevel@tonic-gate nw_close(struct irs_nw *this) {
1050Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate nw_minimize(this);
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if (pvt->res && pvt->free_res)
1100Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1130Sstevel@tonic-gate memput(this, sizeof *this);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate static struct nwent *
nw_next(struct irs_nw * this)1170Sstevel@tonic-gate nw_next(struct irs_nw *this) {
1180Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1190Sstevel@tonic-gate struct nwent *rval;
1200Sstevel@tonic-gate struct irs_nw *nw;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate if (init(this) == -1)
1230Sstevel@tonic-gate return(NULL);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate while (pvt->rule) {
1260Sstevel@tonic-gate nw = pvt->rule->inst->nw;
1270Sstevel@tonic-gate rval = (*nw->next)(nw);
1280Sstevel@tonic-gate if (rval)
1290Sstevel@tonic-gate return (rval);
1300Sstevel@tonic-gate if (!(pvt->rules->flags & IRS_CONTINUE))
1310Sstevel@tonic-gate break;
1320Sstevel@tonic-gate pvt->rule = pvt->rule->next;
1330Sstevel@tonic-gate if (pvt->rule) {
1340Sstevel@tonic-gate nw = pvt->rule->inst->nw;
1350Sstevel@tonic-gate (*nw->rewind)(nw);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate return (NULL);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate static struct nwent *
nw_byname(struct irs_nw * this,const char * name,int type)1420Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int type) {
1430Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1440Sstevel@tonic-gate struct irs_rule *rule;
1450Sstevel@tonic-gate struct nwent *rval;
1460Sstevel@tonic-gate struct irs_nw *nw;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate if (init(this) == -1)
1490Sstevel@tonic-gate return(NULL);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) {
1520Sstevel@tonic-gate nw = rule->inst->nw;
1530Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
1540Sstevel@tonic-gate rval = (*nw->byname)(nw, name, type);
1550Sstevel@tonic-gate if (rval != NULL)
1560Sstevel@tonic-gate return (rval);
1570Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN &&
1580Sstevel@tonic-gate !(rule->flags & IRS_CONTINUE))
1590Sstevel@tonic-gate break;
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate return (NULL);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate static struct nwent *
nw_byaddr(struct irs_nw * this,void * net,int length,int type)1650Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
1660Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1670Sstevel@tonic-gate struct irs_rule *rule;
1680Sstevel@tonic-gate struct nwent *rval;
1690Sstevel@tonic-gate struct irs_nw *nw;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (init(this) == -1)
1720Sstevel@tonic-gate return(NULL);
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) {
1750Sstevel@tonic-gate nw = rule->inst->nw;
1760Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
1770Sstevel@tonic-gate rval = (*nw->byaddr)(nw, net, length, type);
1780Sstevel@tonic-gate if (rval != NULL)
1790Sstevel@tonic-gate return (rval);
1800Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN &&
1810Sstevel@tonic-gate !(rule->flags & IRS_CONTINUE))
1820Sstevel@tonic-gate break;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate return (NULL);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate static void
nw_rewind(struct irs_nw * this)1880Sstevel@tonic-gate nw_rewind(struct irs_nw *this) {
1890Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1900Sstevel@tonic-gate struct irs_nw *nw;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate pvt->rule = pvt->rules;
1930Sstevel@tonic-gate if (pvt->rule) {
1940Sstevel@tonic-gate nw = pvt->rule->inst->nw;
1950Sstevel@tonic-gate (*nw->rewind)(nw);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate static void
nw_minimize(struct irs_nw * this)2000Sstevel@tonic-gate nw_minimize(struct irs_nw *this) {
2010Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2020Sstevel@tonic-gate struct irs_rule *rule;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate if (pvt->res)
2050Sstevel@tonic-gate res_nclose(pvt->res);
2060Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) {
2070Sstevel@tonic-gate struct irs_nw *nw = rule->inst->nw;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate (*nw->minimize)(nw);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate static struct __res_state *
nw_res_get(struct irs_nw * this)2140Sstevel@tonic-gate nw_res_get(struct irs_nw *this) {
2150Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate if (!pvt->res) {
2180Sstevel@tonic-gate struct __res_state *res;
2190Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res);
2200Sstevel@tonic-gate if (!res) {
2210Sstevel@tonic-gate errno = ENOMEM;
2220Sstevel@tonic-gate return (NULL);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate memset(res, 0, sizeof *res);
2250Sstevel@tonic-gate nw_res_set(this, res, free);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate return (pvt->res);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate static void
nw_res_set(struct irs_nw * this,struct __res_state * res,void (* free_res)(void *))2320Sstevel@tonic-gate nw_res_set(struct irs_nw *this, struct __res_state *res,
2330Sstevel@tonic-gate void (*free_res)(void *)) {
2340Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2350Sstevel@tonic-gate struct irs_rule *rule;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate if (pvt->res && pvt->free_res) {
2380Sstevel@tonic-gate res_nclose(pvt->res);
2390Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate pvt->res = res;
2430Sstevel@tonic-gate pvt->free_res = free_res;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) {
2460Sstevel@tonic-gate struct irs_nw *nw = rule->inst->nw;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate (*nw->res_set)(nw, pvt->res, NULL);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate static int
init(struct irs_nw * this)2530Sstevel@tonic-gate init(struct irs_nw *this) {
2540Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate if (!pvt->res && !nw_res_get(this))
2570Sstevel@tonic-gate return (-1);
258*11038SRao.Shoaib@Sun.COM if (((pvt->res->options & RES_INIT) == 0U) &&
2590Sstevel@tonic-gate res_ninit(pvt->res) == -1)
2600Sstevel@tonic-gate return (-1);
2610Sstevel@tonic-gate return (0);
2620Sstevel@tonic-gate }
263*11038SRao.Shoaib@Sun.COM
264*11038SRao.Shoaib@Sun.COM /*! \file */
265