2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (c) 1996,1999 by Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A#if !defined(LINT) && !defined(CODECENTER)
2N/Astatic const char rcsid[] = "$Id: gen_pr.c,v 1.3 2005/04/27 04:56:24 sra Exp $";
2N/A#endif
2N/A
2N/A/* Imports */
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#include <sys/types.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/nameser.h>
2N/A
2N/A#include <errno.h>
2N/A#include <resolv.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/A#include <isc/memcluster.h>
2N/A#include <irs.h>
2N/A
2N/A#include "port_after.h"
2N/A
2N/A#include "irs_p.h"
2N/A#include "gen_p.h"
2N/A
2N/A/* Types */
2N/A
2N/Astruct pvt {
2N/A struct irs_rule * rules;
2N/A struct irs_rule * rule;
2N/A struct __res_state * res;
2N/A void (*free_res)(void *);
2N/A};
2N/A
2N/A/* Forward */
2N/A
2N/Astatic void pr_close(struct irs_pr*);
2N/Astatic struct protoent * pr_next(struct irs_pr *);
2N/Astatic struct protoent * pr_byname(struct irs_pr *, const char *);
2N/Astatic struct protoent * pr_bynumber(struct irs_pr *, int);
2N/Astatic void pr_rewind(struct irs_pr *);
2N/Astatic void pr_minimize(struct irs_pr *);
2N/Astatic struct __res_state * pr_res_get(struct irs_pr *);
2N/Astatic void pr_res_set(struct irs_pr *,
2N/A struct __res_state *,
2N/A void (*)(void *));
2N/A
2N/A/* Public */
2N/A
2N/Astruct irs_pr *
2N/Airs_gen_pr(struct irs_acc *this) {
2N/A struct gen_p *accpvt = (struct gen_p *)this->private;
2N/A struct irs_pr *pr;
2N/A struct pvt *pvt;
2N/A
2N/A if (!(pr = memget(sizeof *pr))) {
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A memset(pr, 0x5e, sizeof *pr);
2N/A if (!(pvt = memget(sizeof *pvt))) {
2N/A memput(pr, sizeof *pr);
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A memset(pvt, 0, sizeof *pvt);
2N/A pvt->rules = accpvt->map_rules[irs_pr];
2N/A pvt->rule = pvt->rules;
2N/A pr->private = pvt;
2N/A pr->close = pr_close;
2N/A pr->next = pr_next;
2N/A pr->byname = pr_byname;
2N/A pr->bynumber = pr_bynumber;
2N/A pr->rewind = pr_rewind;
2N/A pr->minimize = pr_minimize;
2N/A pr->res_get = pr_res_get;
2N/A pr->res_set = pr_res_set;
2N/A return (pr);
2N/A}
2N/A
2N/A/* Methods */
2N/A
2N/Astatic void
2N/Apr_close(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A memput(pvt, sizeof *pvt);
2N/A memput(this, sizeof *this);
2N/A}
2N/A
2N/Astatic struct protoent *
2N/Apr_next(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct protoent *rval;
2N/A struct irs_pr *pr;
2N/A
2N/A while (pvt->rule) {
2N/A pr = pvt->rule->inst->pr;
2N/A rval = (*pr->next)(pr);
2N/A if (rval)
2N/A return (rval);
2N/A if (!(pvt->rules->flags & IRS_CONTINUE))
2N/A break;
2N/A pvt->rule = pvt->rule->next;
2N/A if (pvt->rule) {
2N/A pr = pvt->rule->inst->pr;
2N/A (*pr->rewind)(pr);
2N/A }
2N/A }
2N/A return (NULL);
2N/A}
2N/A
2N/Astatic struct protoent *
2N/Apr_byname(struct irs_pr *this, const char *name) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct irs_rule *rule;
2N/A struct protoent *rval;
2N/A struct irs_pr *pr;
2N/A
2N/A rval = NULL;
2N/A for (rule = pvt->rules; rule; rule = rule->next) {
2N/A pr = rule->inst->pr;
2N/A rval = (*pr->byname)(pr, name);
2N/A if (rval || !(rule->flags & IRS_CONTINUE))
2N/A break;
2N/A }
2N/A return (rval);
2N/A}
2N/A
2N/Astatic struct protoent *
2N/Apr_bynumber(struct irs_pr *this, int proto) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct irs_rule *rule;
2N/A struct protoent *rval;
2N/A struct irs_pr *pr;
2N/A
2N/A rval = NULL;
2N/A for (rule = pvt->rules; rule; rule = rule->next) {
2N/A pr = rule->inst->pr;
2N/A rval = (*pr->bynumber)(pr, proto);
2N/A if (rval || !(rule->flags & IRS_CONTINUE))
2N/A break;
2N/A }
2N/A return (rval);
2N/A}
2N/A
2N/Astatic void
2N/Apr_rewind(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct irs_pr *pr;
2N/A
2N/A pvt->rule = pvt->rules;
2N/A if (pvt->rule) {
2N/A pr = pvt->rule->inst->pr;
2N/A (*pr->rewind)(pr);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Apr_minimize(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct irs_rule *rule;
2N/A
2N/A for (rule = pvt->rules; rule != NULL; rule = rule->next) {
2N/A struct irs_pr *pr = rule->inst->pr;
2N/A
2N/A (*pr->minimize)(pr);
2N/A }
2N/A}
2N/A
2N/Astatic struct __res_state *
2N/Apr_res_get(struct irs_pr *this) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A
2N/A if (!pvt->res) {
2N/A struct __res_state *res;
2N/A res = (struct __res_state *)malloc(sizeof *res);
2N/A if (!res) {
2N/A errno = ENOMEM;
2N/A return (NULL);
2N/A }
2N/A memset(res, 0, sizeof *res);
2N/A pr_res_set(this, res, free);
2N/A }
2N/A
2N/A return (pvt->res);
2N/A}
2N/A
2N/Astatic void
2N/Apr_res_set(struct irs_pr *this, struct __res_state *res,
2N/A void (*free_res)(void *)) {
2N/A struct pvt *pvt = (struct pvt *)this->private;
2N/A struct irs_rule *rule;
2N/A
2N/A if (pvt->res && pvt->free_res) {
2N/A res_nclose(pvt->res);
2N/A (*pvt->free_res)(pvt->res);
2N/A }
2N/A
2N/A pvt->res = res;
2N/A pvt->free_res = free_res;
2N/A
2N/A for (rule = pvt->rules; rule != NULL; rule = rule->next) {
2N/A struct irs_pr *pr = rule->inst->pr;
2N/A
2N/A if (pr->res_set)
2N/A (*pr->res_set)(pr, pvt->res, NULL);
2N/A }
2N/A}
2N/A
2N/A/*! \file */