xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/gen_ng.c (revision 11038:74b12212b8a2)
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_ng.c,v 1.3 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 #include <resolv.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <errno.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 	char *			curgroup;
500Sstevel@tonic-gate };
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /* Forward */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static void		ng_close(struct irs_ng *);
550Sstevel@tonic-gate static int		ng_next(struct irs_ng *, const char **,
560Sstevel@tonic-gate 				const char **, const char **);
570Sstevel@tonic-gate static int 		ng_test(struct irs_ng *, const char *,
580Sstevel@tonic-gate 				const char *, const char *,
590Sstevel@tonic-gate 				const char *);
600Sstevel@tonic-gate static void 		ng_rewind(struct irs_ng *, const char *);
610Sstevel@tonic-gate static void		ng_minimize(struct irs_ng *);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* Public */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate struct irs_ng *
irs_gen_ng(struct irs_acc * this)660Sstevel@tonic-gate irs_gen_ng(struct irs_acc *this) {
670Sstevel@tonic-gate 	struct gen_p *accpvt = (struct gen_p *)this->private;
680Sstevel@tonic-gate 	struct irs_ng *ng;
690Sstevel@tonic-gate 	struct pvt *pvt;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if (!(ng = memget(sizeof *ng))) {
720Sstevel@tonic-gate 		errno = ENOMEM;
730Sstevel@tonic-gate 		return (NULL);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	memset(ng, 0x5e, sizeof *ng);
760Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
770Sstevel@tonic-gate 		memput(ng, sizeof *ng);
780Sstevel@tonic-gate 		errno = ENOMEM;
790Sstevel@tonic-gate 		return (NULL);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
820Sstevel@tonic-gate 	pvt->rules = accpvt->map_rules[irs_ng];
830Sstevel@tonic-gate 	pvt->rule = pvt->rules;
840Sstevel@tonic-gate 	ng->private = pvt;
850Sstevel@tonic-gate 	ng->close = ng_close;
860Sstevel@tonic-gate 	ng->next = ng_next;
870Sstevel@tonic-gate 	ng->test = ng_test;
880Sstevel@tonic-gate 	ng->rewind = ng_rewind;
890Sstevel@tonic-gate 	ng->minimize = ng_minimize;
900Sstevel@tonic-gate 	return (ng);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /* Methods */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static void
ng_close(struct irs_ng * this)960Sstevel@tonic-gate ng_close(struct irs_ng *this) {
970Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	ng_minimize(this);
1000Sstevel@tonic-gate 	if (pvt->curgroup)
1010Sstevel@tonic-gate 		free(pvt->curgroup);
1020Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1030Sstevel@tonic-gate 	memput(this, sizeof *this);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static int
ng_next(struct irs_ng * this,const char ** host,const char ** user,const char ** domain)1070Sstevel@tonic-gate ng_next(struct irs_ng *this, const char **host, const char **user,
1080Sstevel@tonic-gate 	const char **domain)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1110Sstevel@tonic-gate 	struct irs_ng *ng;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	while (pvt->rule) {
1140Sstevel@tonic-gate 		ng = pvt->rule->inst->ng;
1150Sstevel@tonic-gate 		if ((*ng->next)(ng, host, user, domain) == 1)
1160Sstevel@tonic-gate 			return (1);
1170Sstevel@tonic-gate 		if (!(pvt->rule->flags & IRS_CONTINUE))
1180Sstevel@tonic-gate 			break;
1190Sstevel@tonic-gate 		pvt->rule = pvt->rule->next;
1200Sstevel@tonic-gate 		if (pvt->rule) {
1210Sstevel@tonic-gate 			ng = pvt->rule->inst->ng;
1220Sstevel@tonic-gate 			(*ng->rewind)(ng, pvt->curgroup);
1230Sstevel@tonic-gate 		}
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 	return (0);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static int
ng_test(struct irs_ng * this,const char * name,const char * user,const char * host,const char * domain)1290Sstevel@tonic-gate ng_test(struct irs_ng *this, const char *name,
1300Sstevel@tonic-gate 	const char *user, const char *host, const char *domain)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1330Sstevel@tonic-gate 	struct irs_rule *rule;
1340Sstevel@tonic-gate 	struct irs_ng *ng;
1350Sstevel@tonic-gate 	int rval;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	rval = 0;
1380Sstevel@tonic-gate 	for (rule = pvt->rules; rule; rule = rule->next) {
1390Sstevel@tonic-gate 		ng = rule->inst->ng;
1400Sstevel@tonic-gate 		rval = (*ng->test)(ng, name, user, host, domain);
1410Sstevel@tonic-gate 		if (rval || !(rule->flags & IRS_CONTINUE))
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	return (rval);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static void
ng_rewind(struct irs_ng * this,const char * group)1480Sstevel@tonic-gate ng_rewind(struct irs_ng *this, const char *group) {
1490Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1500Sstevel@tonic-gate 	struct irs_ng *ng;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	pvt->rule = pvt->rules;
1530Sstevel@tonic-gate 	if (pvt->rule) {
1540Sstevel@tonic-gate 		if (pvt->curgroup)
1550Sstevel@tonic-gate 			free(pvt->curgroup);
1560Sstevel@tonic-gate 		pvt->curgroup = strdup(group);
1570Sstevel@tonic-gate 		ng = pvt->rule->inst->ng;
1580Sstevel@tonic-gate 		(*ng->rewind)(ng, pvt->curgroup);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate static void
ng_minimize(struct irs_ng * this)1630Sstevel@tonic-gate ng_minimize(struct irs_ng *this) {
1640Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1650Sstevel@tonic-gate 	struct irs_rule *rule;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
1680Sstevel@tonic-gate 		struct irs_ng *ng = rule->inst->ng;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 		(*ng->minimize)(ng);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate }
173*11038SRao.Shoaib@Sun.COM 
174*11038SRao.Shoaib@Sun.COM /*! \file */
175