1/* $NetBSD: gen_ng.c,v 1.1.1.1 2009/04/12 15:33:37 christos Exp $ */ 2 3/* 4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (c) 1996,1999 by Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20#if !defined(LINT) && !defined(CODECENTER) 21static const char rcsid[] = "Id: gen_ng.c,v 1.3 2005/04/27 04:56:23 sra Exp"; 22#endif 23 24/* Imports */ 25 26#include "port_before.h" 27 28#include <sys/types.h> 29 30#include <netinet/in.h> 31#include <arpa/nameser.h> 32#include <resolv.h> 33 34#include <errno.h> 35#include <stdlib.h> 36#include <string.h> 37 38#include <isc/memcluster.h> 39#include <irs.h> 40 41#include "port_after.h" 42 43#include "irs_p.h" 44#include "gen_p.h" 45 46/* Types */ 47 48struct pvt { 49 struct irs_rule * rules; 50 struct irs_rule * rule; 51 char * curgroup; 52}; 53 54/* Forward */ 55 56static void ng_close(struct irs_ng *); 57static int ng_next(struct irs_ng *, const char **, 58 const char **, const char **); 59static int ng_test(struct irs_ng *, const char *, 60 const char *, const char *, 61 const char *); 62static void ng_rewind(struct irs_ng *, const char *); 63static void ng_minimize(struct irs_ng *); 64 65/* Public */ 66 67struct irs_ng * 68irs_gen_ng(struct irs_acc *this) { 69 struct gen_p *accpvt = (struct gen_p *)this->private; 70 struct irs_ng *ng; 71 struct pvt *pvt; 72 73 if (!(ng = memget(sizeof *ng))) { 74 errno = ENOMEM; 75 return (NULL); 76 } 77 memset(ng, 0x5e, sizeof *ng); 78 if (!(pvt = memget(sizeof *pvt))) { 79 memput(ng, sizeof *ng); 80 errno = ENOMEM; 81 return (NULL); 82 } 83 memset(pvt, 0, sizeof *pvt); 84 pvt->rules = accpvt->map_rules[irs_ng]; 85 pvt->rule = pvt->rules; 86 ng->private = pvt; 87 ng->close = ng_close; 88 ng->next = ng_next; 89 ng->test = ng_test; 90 ng->rewind = ng_rewind; 91 ng->minimize = ng_minimize; 92 return (ng); 93} 94 95/* Methods */ 96 97static void 98ng_close(struct irs_ng *this) { 99 struct pvt *pvt = (struct pvt *)this->private; 100 101 ng_minimize(this); 102 if (pvt->curgroup) 103 free(pvt->curgroup); 104 memput(pvt, sizeof *pvt); 105 memput(this, sizeof *this); 106} 107 108static int 109ng_next(struct irs_ng *this, const char **host, const char **user, 110 const char **domain) 111{ 112 struct pvt *pvt = (struct pvt *)this->private; 113 struct irs_ng *ng; 114 115 while (pvt->rule) { 116 ng = pvt->rule->inst->ng; 117 if ((*ng->next)(ng, host, user, domain) == 1) 118 return (1); 119 if (!(pvt->rule->flags & IRS_CONTINUE)) 120 break; 121 pvt->rule = pvt->rule->next; 122 if (pvt->rule) { 123 ng = pvt->rule->inst->ng; 124 (*ng->rewind)(ng, pvt->curgroup); 125 } 126 } 127 return (0); 128} 129 130static int 131ng_test(struct irs_ng *this, const char *name, 132 const char *user, const char *host, const char *domain) 133{ 134 struct pvt *pvt = (struct pvt *)this->private; 135 struct irs_rule *rule; 136 struct irs_ng *ng; 137 int rval; 138 139 rval = 0; 140 for (rule = pvt->rules; rule; rule = rule->next) { 141 ng = rule->inst->ng; 142 rval = (*ng->test)(ng, name, user, host, domain); 143 if (rval || !(rule->flags & IRS_CONTINUE)) 144 break; 145 } 146 return (rval); 147} 148 149static void 150ng_rewind(struct irs_ng *this, const char *group) { 151 struct pvt *pvt = (struct pvt *)this->private; 152 struct irs_ng *ng; 153 154 pvt->rule = pvt->rules; 155 if (pvt->rule) { 156 if (pvt->curgroup) 157 free(pvt->curgroup); 158 pvt->curgroup = strdup(group); 159 ng = pvt->rule->inst->ng; 160 (*ng->rewind)(ng, pvt->curgroup); 161 } 162} 163 164static void 165ng_minimize(struct irs_ng *this) { 166 struct pvt *pvt = (struct pvt *)this->private; 167 struct irs_rule *rule; 168 169 for (rule = pvt->rules; rule != NULL; rule = rule->next) { 170 struct irs_ng *ng = rule->inst->ng; 171 172 (*ng->minimize)(ng); 173 } 174} 175 176/*! \file */ 177