Home | History | Annotate | Line # | Download | only in irs
gen_pr.c revision 1.1.1.1.4.2
      1  1.1.1.1.4.2  riz /*	$NetBSD: gen_pr.c,v 1.1.1.1.4.2 2011/01/06 21:42:16 riz Exp $	*/
      2  1.1.1.1.4.2  riz 
      3  1.1.1.1.4.2  riz /*
      4  1.1.1.1.4.2  riz  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1.1.1.4.2  riz  * Copyright (c) 1996,1999 by Internet Software Consortium.
      6  1.1.1.1.4.2  riz  *
      7  1.1.1.1.4.2  riz  * Permission to use, copy, modify, and distribute this software for any
      8  1.1.1.1.4.2  riz  * purpose with or without fee is hereby granted, provided that the above
      9  1.1.1.1.4.2  riz  * copyright notice and this permission notice appear in all copies.
     10  1.1.1.1.4.2  riz  *
     11  1.1.1.1.4.2  riz  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  1.1.1.1.4.2  riz  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1.1.1.4.2  riz  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  1.1.1.1.4.2  riz  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1.1.1.4.2  riz  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1.1.1.4.2  riz  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  1.1.1.1.4.2  riz  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1.1.1.4.2  riz  */
     19  1.1.1.1.4.2  riz 
     20  1.1.1.1.4.2  riz #if !defined(LINT) && !defined(CODECENTER)
     21  1.1.1.1.4.2  riz static const char rcsid[] = "Id: gen_pr.c,v 1.3 2005/04/27 04:56:24 sra Exp";
     22  1.1.1.1.4.2  riz #endif
     23  1.1.1.1.4.2  riz 
     24  1.1.1.1.4.2  riz /* Imports */
     25  1.1.1.1.4.2  riz 
     26  1.1.1.1.4.2  riz #include "port_before.h"
     27  1.1.1.1.4.2  riz 
     28  1.1.1.1.4.2  riz #include <sys/types.h>
     29  1.1.1.1.4.2  riz #include <netinet/in.h>
     30  1.1.1.1.4.2  riz #include <arpa/nameser.h>
     31  1.1.1.1.4.2  riz 
     32  1.1.1.1.4.2  riz #include <errno.h>
     33  1.1.1.1.4.2  riz #include <resolv.h>
     34  1.1.1.1.4.2  riz #include <stdlib.h>
     35  1.1.1.1.4.2  riz #include <string.h>
     36  1.1.1.1.4.2  riz 
     37  1.1.1.1.4.2  riz #include <isc/memcluster.h>
     38  1.1.1.1.4.2  riz #include <irs.h>
     39  1.1.1.1.4.2  riz 
     40  1.1.1.1.4.2  riz #include "port_after.h"
     41  1.1.1.1.4.2  riz 
     42  1.1.1.1.4.2  riz #include "irs_p.h"
     43  1.1.1.1.4.2  riz #include "gen_p.h"
     44  1.1.1.1.4.2  riz 
     45  1.1.1.1.4.2  riz /* Types */
     46  1.1.1.1.4.2  riz 
     47  1.1.1.1.4.2  riz struct pvt {
     48  1.1.1.1.4.2  riz 	struct irs_rule *	rules;
     49  1.1.1.1.4.2  riz 	struct irs_rule *	rule;
     50  1.1.1.1.4.2  riz 	struct __res_state *	res;
     51  1.1.1.1.4.2  riz 	void			(*free_res)(void *);
     52  1.1.1.1.4.2  riz };
     53  1.1.1.1.4.2  riz 
     54  1.1.1.1.4.2  riz /* Forward */
     55  1.1.1.1.4.2  riz 
     56  1.1.1.1.4.2  riz static void			pr_close(struct irs_pr*);
     57  1.1.1.1.4.2  riz static struct protoent *	pr_next(struct irs_pr *);
     58  1.1.1.1.4.2  riz static struct protoent *	pr_byname(struct irs_pr *, const char *);
     59  1.1.1.1.4.2  riz static struct protoent * 	pr_bynumber(struct irs_pr *, int);
     60  1.1.1.1.4.2  riz static void 			pr_rewind(struct irs_pr *);
     61  1.1.1.1.4.2  riz static void			pr_minimize(struct irs_pr *);
     62  1.1.1.1.4.2  riz static struct __res_state *	pr_res_get(struct irs_pr *);
     63  1.1.1.1.4.2  riz static void			pr_res_set(struct irs_pr *,
     64  1.1.1.1.4.2  riz 					   struct __res_state *,
     65  1.1.1.1.4.2  riz 					   void (*)(void *));
     66  1.1.1.1.4.2  riz 
     67  1.1.1.1.4.2  riz /* Public */
     68  1.1.1.1.4.2  riz 
     69  1.1.1.1.4.2  riz struct irs_pr *
     70  1.1.1.1.4.2  riz irs_gen_pr(struct irs_acc *this) {
     71  1.1.1.1.4.2  riz 	struct gen_p *accpvt = (struct gen_p *)this->private;
     72  1.1.1.1.4.2  riz 	struct irs_pr *pr;
     73  1.1.1.1.4.2  riz 	struct pvt *pvt;
     74  1.1.1.1.4.2  riz 
     75  1.1.1.1.4.2  riz 	if (!(pr = memget(sizeof *pr))) {
     76  1.1.1.1.4.2  riz 		errno = ENOMEM;
     77  1.1.1.1.4.2  riz 		return (NULL);
     78  1.1.1.1.4.2  riz 	}
     79  1.1.1.1.4.2  riz 	memset(pr, 0x5e, sizeof *pr);
     80  1.1.1.1.4.2  riz 	if (!(pvt = memget(sizeof *pvt))) {
     81  1.1.1.1.4.2  riz 		memput(pr, sizeof *pr);
     82  1.1.1.1.4.2  riz 		errno = ENOMEM;
     83  1.1.1.1.4.2  riz 		return (NULL);
     84  1.1.1.1.4.2  riz 	}
     85  1.1.1.1.4.2  riz 	memset(pvt, 0, sizeof *pvt);
     86  1.1.1.1.4.2  riz 	pvt->rules = accpvt->map_rules[irs_pr];
     87  1.1.1.1.4.2  riz 	pvt->rule = pvt->rules;
     88  1.1.1.1.4.2  riz 	pr->private = pvt;
     89  1.1.1.1.4.2  riz 	pr->close = pr_close;
     90  1.1.1.1.4.2  riz 	pr->next = pr_next;
     91  1.1.1.1.4.2  riz 	pr->byname = pr_byname;
     92  1.1.1.1.4.2  riz 	pr->bynumber = pr_bynumber;
     93  1.1.1.1.4.2  riz 	pr->rewind = pr_rewind;
     94  1.1.1.1.4.2  riz 	pr->minimize = pr_minimize;
     95  1.1.1.1.4.2  riz 	pr->res_get = pr_res_get;
     96  1.1.1.1.4.2  riz 	pr->res_set = pr_res_set;
     97  1.1.1.1.4.2  riz 	return (pr);
     98  1.1.1.1.4.2  riz }
     99  1.1.1.1.4.2  riz 
    100  1.1.1.1.4.2  riz /* Methods */
    101  1.1.1.1.4.2  riz 
    102  1.1.1.1.4.2  riz static void
    103  1.1.1.1.4.2  riz pr_close(struct irs_pr *this) {
    104  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    105  1.1.1.1.4.2  riz 
    106  1.1.1.1.4.2  riz 	memput(pvt, sizeof *pvt);
    107  1.1.1.1.4.2  riz 	memput(this, sizeof *this);
    108  1.1.1.1.4.2  riz }
    109  1.1.1.1.4.2  riz 
    110  1.1.1.1.4.2  riz static struct protoent *
    111  1.1.1.1.4.2  riz pr_next(struct irs_pr *this) {
    112  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    113  1.1.1.1.4.2  riz 	struct protoent *rval;
    114  1.1.1.1.4.2  riz 	struct irs_pr *pr;
    115  1.1.1.1.4.2  riz 
    116  1.1.1.1.4.2  riz 	while (pvt->rule) {
    117  1.1.1.1.4.2  riz 		pr = pvt->rule->inst->pr;
    118  1.1.1.1.4.2  riz 		rval = (*pr->next)(pr);
    119  1.1.1.1.4.2  riz 		if (rval)
    120  1.1.1.1.4.2  riz 			return (rval);
    121  1.1.1.1.4.2  riz 		if (!(pvt->rules->flags & IRS_CONTINUE))
    122  1.1.1.1.4.2  riz 			break;
    123  1.1.1.1.4.2  riz 		pvt->rule = pvt->rule->next;
    124  1.1.1.1.4.2  riz 		if (pvt->rule) {
    125  1.1.1.1.4.2  riz 			pr = pvt->rule->inst->pr;
    126  1.1.1.1.4.2  riz 			(*pr->rewind)(pr);
    127  1.1.1.1.4.2  riz 		}
    128  1.1.1.1.4.2  riz 	}
    129  1.1.1.1.4.2  riz 	return (NULL);
    130  1.1.1.1.4.2  riz }
    131  1.1.1.1.4.2  riz 
    132  1.1.1.1.4.2  riz static struct protoent *
    133  1.1.1.1.4.2  riz pr_byname(struct irs_pr *this, const char *name) {
    134  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    135  1.1.1.1.4.2  riz 	struct irs_rule *rule;
    136  1.1.1.1.4.2  riz 	struct protoent *rval;
    137  1.1.1.1.4.2  riz 	struct irs_pr *pr;
    138  1.1.1.1.4.2  riz 
    139  1.1.1.1.4.2  riz 	rval = NULL;
    140  1.1.1.1.4.2  riz 	for (rule = pvt->rules; rule; rule = rule->next) {
    141  1.1.1.1.4.2  riz 		pr = rule->inst->pr;
    142  1.1.1.1.4.2  riz 		rval = (*pr->byname)(pr, name);
    143  1.1.1.1.4.2  riz 		if (rval || !(rule->flags & IRS_CONTINUE))
    144  1.1.1.1.4.2  riz 			break;
    145  1.1.1.1.4.2  riz 	}
    146  1.1.1.1.4.2  riz 	return (rval);
    147  1.1.1.1.4.2  riz }
    148  1.1.1.1.4.2  riz 
    149  1.1.1.1.4.2  riz static struct protoent *
    150  1.1.1.1.4.2  riz pr_bynumber(struct irs_pr *this, int proto) {
    151  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    152  1.1.1.1.4.2  riz 	struct irs_rule *rule;
    153  1.1.1.1.4.2  riz 	struct protoent *rval;
    154  1.1.1.1.4.2  riz 	struct irs_pr *pr;
    155  1.1.1.1.4.2  riz 
    156  1.1.1.1.4.2  riz 	rval = NULL;
    157  1.1.1.1.4.2  riz 	for (rule = pvt->rules; rule; rule = rule->next) {
    158  1.1.1.1.4.2  riz 		pr = rule->inst->pr;
    159  1.1.1.1.4.2  riz 		rval = (*pr->bynumber)(pr, proto);
    160  1.1.1.1.4.2  riz 		if (rval || !(rule->flags & IRS_CONTINUE))
    161  1.1.1.1.4.2  riz 			break;
    162  1.1.1.1.4.2  riz 	}
    163  1.1.1.1.4.2  riz 	return (rval);
    164  1.1.1.1.4.2  riz }
    165  1.1.1.1.4.2  riz 
    166  1.1.1.1.4.2  riz static void
    167  1.1.1.1.4.2  riz pr_rewind(struct irs_pr *this) {
    168  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    169  1.1.1.1.4.2  riz 	struct irs_pr *pr;
    170  1.1.1.1.4.2  riz 
    171  1.1.1.1.4.2  riz 	pvt->rule = pvt->rules;
    172  1.1.1.1.4.2  riz 	if (pvt->rule) {
    173  1.1.1.1.4.2  riz 		pr = pvt->rule->inst->pr;
    174  1.1.1.1.4.2  riz 		(*pr->rewind)(pr);
    175  1.1.1.1.4.2  riz 	}
    176  1.1.1.1.4.2  riz }
    177  1.1.1.1.4.2  riz 
    178  1.1.1.1.4.2  riz static void
    179  1.1.1.1.4.2  riz pr_minimize(struct irs_pr *this) {
    180  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    181  1.1.1.1.4.2  riz 	struct irs_rule *rule;
    182  1.1.1.1.4.2  riz 
    183  1.1.1.1.4.2  riz 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
    184  1.1.1.1.4.2  riz 		struct irs_pr *pr = rule->inst->pr;
    185  1.1.1.1.4.2  riz 
    186  1.1.1.1.4.2  riz 		(*pr->minimize)(pr);
    187  1.1.1.1.4.2  riz 	}
    188  1.1.1.1.4.2  riz }
    189  1.1.1.1.4.2  riz 
    190  1.1.1.1.4.2  riz static struct __res_state *
    191  1.1.1.1.4.2  riz pr_res_get(struct irs_pr *this) {
    192  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    193  1.1.1.1.4.2  riz 
    194  1.1.1.1.4.2  riz 	if (!pvt->res) {
    195  1.1.1.1.4.2  riz 		struct __res_state *res;
    196  1.1.1.1.4.2  riz 		res = (struct __res_state *)malloc(sizeof *res);
    197  1.1.1.1.4.2  riz 		if (!res) {
    198  1.1.1.1.4.2  riz 			errno = ENOMEM;
    199  1.1.1.1.4.2  riz 			return (NULL);
    200  1.1.1.1.4.2  riz 		}
    201  1.1.1.1.4.2  riz 		memset(res, 0, sizeof *res);
    202  1.1.1.1.4.2  riz 		pr_res_set(this, res, free);
    203  1.1.1.1.4.2  riz 	}
    204  1.1.1.1.4.2  riz 
    205  1.1.1.1.4.2  riz 	return (pvt->res);
    206  1.1.1.1.4.2  riz }
    207  1.1.1.1.4.2  riz 
    208  1.1.1.1.4.2  riz static void
    209  1.1.1.1.4.2  riz pr_res_set(struct irs_pr *this, struct __res_state *res,
    210  1.1.1.1.4.2  riz 		void (*free_res)(void *)) {
    211  1.1.1.1.4.2  riz 	struct pvt *pvt = (struct pvt *)this->private;
    212  1.1.1.1.4.2  riz 	struct irs_rule *rule;
    213  1.1.1.1.4.2  riz 
    214  1.1.1.1.4.2  riz 	if (pvt->res && pvt->free_res) {
    215  1.1.1.1.4.2  riz 		res_nclose(pvt->res);
    216  1.1.1.1.4.2  riz 		(*pvt->free_res)(pvt->res);
    217  1.1.1.1.4.2  riz 	}
    218  1.1.1.1.4.2  riz 
    219  1.1.1.1.4.2  riz 	pvt->res = res;
    220  1.1.1.1.4.2  riz 	pvt->free_res = free_res;
    221  1.1.1.1.4.2  riz 
    222  1.1.1.1.4.2  riz 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
    223  1.1.1.1.4.2  riz 		struct irs_pr *pr = rule->inst->pr;
    224  1.1.1.1.4.2  riz 
    225  1.1.1.1.4.2  riz 		if (pr->res_set)
    226  1.1.1.1.4.2  riz 			(*pr->res_set)(pr, pvt->res, NULL);
    227  1.1.1.1.4.2  riz 	}
    228  1.1.1.1.4.2  riz }
    229  1.1.1.1.4.2  riz 
    230  1.1.1.1.4.2  riz /*! \file */
    231