1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (c) 1997-2000 by Sun Microsystems, Inc. 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* 7*0Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 10*0Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 11*0Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 12*0Sstevel@tonic-gate * 13*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 14*0Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 15*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 16*0Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 17*0Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 18*0Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 19*0Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 20*0Sstevel@tonic-gate * SOFTWARE. 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER) 26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: gen_nw.c,v 1.13 1999/10/13 16:39:29 vixie Exp $"; 27*0Sstevel@tonic-gate #endif 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* Imports */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include "port_before.h" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <sys/types.h> 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include <netinet/in.h> 36*0Sstevel@tonic-gate #include <arpa/nameser.h> 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate #include <errno.h> 39*0Sstevel@tonic-gate #include <resolv.h> 40*0Sstevel@tonic-gate #include <stdlib.h> 41*0Sstevel@tonic-gate #include <string.h> 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include <isc/memcluster.h> 44*0Sstevel@tonic-gate #include <irs.h> 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate #include "port_after.h" 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #include "irs_p.h" 49*0Sstevel@tonic-gate #include "gen_p.h" 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /* Types */ 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate struct pvt { 54*0Sstevel@tonic-gate struct irs_rule * rules; 55*0Sstevel@tonic-gate struct irs_rule * rule; 56*0Sstevel@tonic-gate struct __res_state * res; 57*0Sstevel@tonic-gate void (*free_res)(void *); 58*0Sstevel@tonic-gate }; 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate /* Forward */ 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate static void nw_close(struct irs_nw*); 63*0Sstevel@tonic-gate static struct nwent * nw_next(struct irs_nw *); 64*0Sstevel@tonic-gate static struct nwent * nw_byname(struct irs_nw *, const char *, int); 65*0Sstevel@tonic-gate static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); 66*0Sstevel@tonic-gate static void nw_rewind(struct irs_nw *); 67*0Sstevel@tonic-gate static void nw_minimize(struct irs_nw *); 68*0Sstevel@tonic-gate static struct __res_state * nw_res_get(struct irs_nw *this); 69*0Sstevel@tonic-gate static void nw_res_set(struct irs_nw *this, 70*0Sstevel@tonic-gate struct __res_state *res, 71*0Sstevel@tonic-gate void (*free_res)(void *)); 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate static int init(struct irs_nw *this); 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate /* Public */ 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate struct irs_nw * 78*0Sstevel@tonic-gate irs_gen_nw(struct irs_acc *this) { 79*0Sstevel@tonic-gate struct gen_p *accpvt = (struct gen_p *)this->private; 80*0Sstevel@tonic-gate struct irs_nw *nw; 81*0Sstevel@tonic-gate struct pvt *pvt; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 84*0Sstevel@tonic-gate errno = ENOMEM; 85*0Sstevel@tonic-gate return (NULL); 86*0Sstevel@tonic-gate } 87*0Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 88*0Sstevel@tonic-gate if (!(nw = memget(sizeof *nw))) { 89*0Sstevel@tonic-gate memput(pvt, sizeof *pvt); 90*0Sstevel@tonic-gate errno = ENOMEM; 91*0Sstevel@tonic-gate return (NULL); 92*0Sstevel@tonic-gate } 93*0Sstevel@tonic-gate memset(nw, 0x5e, sizeof *nw); 94*0Sstevel@tonic-gate pvt->rules = accpvt->map_rules[irs_nw]; 95*0Sstevel@tonic-gate pvt->rule = pvt->rules; 96*0Sstevel@tonic-gate nw->private = pvt; 97*0Sstevel@tonic-gate nw->close = nw_close; 98*0Sstevel@tonic-gate nw->next = nw_next; 99*0Sstevel@tonic-gate nw->byname = nw_byname; 100*0Sstevel@tonic-gate nw->byaddr = nw_byaddr; 101*0Sstevel@tonic-gate nw->rewind = nw_rewind; 102*0Sstevel@tonic-gate nw->minimize = nw_minimize; 103*0Sstevel@tonic-gate nw->res_get = nw_res_get; 104*0Sstevel@tonic-gate nw->res_set = nw_res_set; 105*0Sstevel@tonic-gate return (nw); 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* Methods */ 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate static void 111*0Sstevel@tonic-gate nw_close(struct irs_nw *this) { 112*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate nw_minimize(this); 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate if (pvt->res && pvt->free_res) 117*0Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate memput(pvt, sizeof *pvt); 120*0Sstevel@tonic-gate memput(this, sizeof *this); 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate static struct nwent * 124*0Sstevel@tonic-gate nw_next(struct irs_nw *this) { 125*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 126*0Sstevel@tonic-gate struct nwent *rval; 127*0Sstevel@tonic-gate struct irs_nw *nw; 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate if (init(this) == -1) 130*0Sstevel@tonic-gate return(NULL); 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate while (pvt->rule) { 133*0Sstevel@tonic-gate nw = pvt->rule->inst->nw; 134*0Sstevel@tonic-gate rval = (*nw->next)(nw); 135*0Sstevel@tonic-gate if (rval) 136*0Sstevel@tonic-gate return (rval); 137*0Sstevel@tonic-gate if (!(pvt->rules->flags & IRS_CONTINUE)) 138*0Sstevel@tonic-gate break; 139*0Sstevel@tonic-gate pvt->rule = pvt->rule->next; 140*0Sstevel@tonic-gate if (pvt->rule) { 141*0Sstevel@tonic-gate nw = pvt->rule->inst->nw; 142*0Sstevel@tonic-gate (*nw->rewind)(nw); 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate return (NULL); 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate static struct nwent * 149*0Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int type) { 150*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 151*0Sstevel@tonic-gate struct irs_rule *rule; 152*0Sstevel@tonic-gate struct nwent *rval; 153*0Sstevel@tonic-gate struct irs_nw *nw; 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate if (init(this) == -1) 156*0Sstevel@tonic-gate return(NULL); 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 159*0Sstevel@tonic-gate nw = rule->inst->nw; 160*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 161*0Sstevel@tonic-gate rval = (*nw->byname)(nw, name, type); 162*0Sstevel@tonic-gate if (rval != NULL) 163*0Sstevel@tonic-gate return (rval); 164*0Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN && 165*0Sstevel@tonic-gate !(rule->flags & IRS_CONTINUE)) 166*0Sstevel@tonic-gate break; 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate return (NULL); 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate static struct nwent * 172*0Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int length, int type) { 173*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 174*0Sstevel@tonic-gate struct irs_rule *rule; 175*0Sstevel@tonic-gate struct nwent *rval; 176*0Sstevel@tonic-gate struct irs_nw *nw; 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate if (init(this) == -1) 179*0Sstevel@tonic-gate return(NULL); 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate for (rule = pvt->rules; rule; rule = rule->next) { 182*0Sstevel@tonic-gate nw = rule->inst->nw; 183*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 184*0Sstevel@tonic-gate rval = (*nw->byaddr)(nw, net, length, type); 185*0Sstevel@tonic-gate if (rval != NULL) 186*0Sstevel@tonic-gate return (rval); 187*0Sstevel@tonic-gate if (pvt->res->res_h_errno != TRY_AGAIN && 188*0Sstevel@tonic-gate !(rule->flags & IRS_CONTINUE)) 189*0Sstevel@tonic-gate break; 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate return (NULL); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate static void 195*0Sstevel@tonic-gate nw_rewind(struct irs_nw *this) { 196*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 197*0Sstevel@tonic-gate struct irs_nw *nw; 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate pvt->rule = pvt->rules; 200*0Sstevel@tonic-gate if (pvt->rule) { 201*0Sstevel@tonic-gate nw = pvt->rule->inst->nw; 202*0Sstevel@tonic-gate (*nw->rewind)(nw); 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate static void 207*0Sstevel@tonic-gate nw_minimize(struct irs_nw *this) { 208*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 209*0Sstevel@tonic-gate struct irs_rule *rule; 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate if (pvt->res) 212*0Sstevel@tonic-gate res_nclose(pvt->res); 213*0Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 214*0Sstevel@tonic-gate struct irs_nw *nw = rule->inst->nw; 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate (*nw->minimize)(nw); 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate static struct __res_state * 221*0Sstevel@tonic-gate nw_res_get(struct irs_nw *this) { 222*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate if (!pvt->res) { 225*0Sstevel@tonic-gate struct __res_state *res; 226*0Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res); 227*0Sstevel@tonic-gate if (!res) { 228*0Sstevel@tonic-gate errno = ENOMEM; 229*0Sstevel@tonic-gate return (NULL); 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate memset(res, 0, sizeof *res); 232*0Sstevel@tonic-gate nw_res_set(this, res, free); 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate return (pvt->res); 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate static void 239*0Sstevel@tonic-gate nw_res_set(struct irs_nw *this, struct __res_state *res, 240*0Sstevel@tonic-gate void (*free_res)(void *)) { 241*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 242*0Sstevel@tonic-gate struct irs_rule *rule; 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate if (pvt->res && pvt->free_res) { 245*0Sstevel@tonic-gate res_nclose(pvt->res); 246*0Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 247*0Sstevel@tonic-gate } 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate pvt->res = res; 250*0Sstevel@tonic-gate pvt->free_res = free_res; 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gate for (rule = pvt->rules; rule != NULL; rule = rule->next) { 253*0Sstevel@tonic-gate struct irs_nw *nw = rule->inst->nw; 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate (*nw->res_set)(nw, pvt->res, NULL); 256*0Sstevel@tonic-gate } 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate static int 260*0Sstevel@tonic-gate init(struct irs_nw *this) { 261*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate if (!pvt->res && !nw_res_get(this)) 264*0Sstevel@tonic-gate return (-1); 265*0Sstevel@tonic-gate if (((pvt->res->options & RES_INIT) == 0) && 266*0Sstevel@tonic-gate res_ninit(pvt->res) == -1) 267*0Sstevel@tonic-gate return (-1); 268*0Sstevel@tonic-gate return (0); 269*0Sstevel@tonic-gate } 270