⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ns_init.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
#if !defined(lint) && !defined(SABER)static char sccsid[] = "@(#)ns_init.c	4.38 (Berkeley) 3/21/91";static char rcsid[] = "$Id:";#endif /* not lint *//* * ++Copyright++ 1986, 1990 * - * Copyright (c) 1986, 1990 *    The Regents of the University of California.  All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: * 	This product includes software developed by the University of * 	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. *  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * Portions Copyright (c) 1993 by Digital Equipment Corporation. *  * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies, and that * the name of Digital Equipment Corporation not be used in advertising or * publicity pertaining to distribution of the document or software without * specific, written prior permission. *  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS * SOFTWARE. * - * --Copyright-- */#include <sys/param.h>#include <sys/socket.h>#include <sys/stat.h>#include <netinet/in.h>#include <arpa/nameser.h>#include <arpa/inet.h>#include <syslog.h>#include <signal.h>#include <resolv.h>#include <stdio.h>#include <errno.h>#include <ctype.h>#include "named.h"#undef nsaddrstatic void		zoneinit __P((struct zoneinfo *)),			get_forwarders __P((FILE *)),			boot_read __P((char *)),#ifdef DEBUG			content_zone __P((int)),#endif			free_forwarders __P((void));static struct zoneinfo	*find_zone __P((char *, int, int));/* * Read boot file for configuration info. */voidns_init(bootfile)	char *bootfile;{	register struct zoneinfo *zp;	static int loads = 0;			/* number of times loaded */	dprintf(1, (ddt, "\nns_init(%s)\n", bootfile));	gettime(&tt);        if (loads == 0) {		if ((zones =			(struct zoneinfo *)calloc(64, sizeof(struct zoneinfo)))					    == NULL) {		    syslog(LOG_ERR,			"Not enough memory to allocate initial zones array");		    exit(1);		}		nzones = 1;		/* zone zero is cache data */		/* allocate cache hash table, formerly the root hash table. */		hashtab = savehash((struct hashbuf *)NULL);		/* allocate root-hints/file-cache hash table */		fcachetab = savehash((struct hashbuf *)NULL);		/* init zone data */		zones[0].z_type = Z_CACHE;        } else {		/* Mark previous zones as not yet found in boot file. */		for (zp = &zones[1]; zp < &zones[nzones]; zp++)			zp->z_state &= ~Z_FOUND;#ifdef LOCALDOM		if (localdomain) {			free(localdomain);			localdomain = NULL;		}#endif		free_forwarders();		free_netlist(enettab);#ifdef XFRNETS		free_netlist(&xfrnets);#endif#ifdef BOGUSNS		free_netlist(&boglist);#endif		forward_only = 0;	}	dprintf(3, (ddt, "\n content of zones before loading \n"));#ifdef DEBUG        if (debug >= 3) {        	content_zone(nzones - 1);        }#endif	boot_read(bootfile);        /* erase all old zones that were not found */        for (zp = &zones[1]; zp < &zones[nzones]; zp++) {            if (zp->z_type && (zp->z_state & Z_FOUND) == 0) {		remove_zone(hashtab, zp - zones);		bzero((char *) zp, sizeof(*zp));		dprintf(2, (ddt, "\n zone #%d was removed\n", zp - zones));	    }        }	dprintf(2, (ddt,"\n content of zones after loading\n"));#ifdef DEBUG	if (debug >= 2) {        	content_zone(nzones-1);        }#endif	/*	 * Schedule calls to ns_maint().	 */	if (needmaint == 0)		sched_maint();	dprintf(1, (ddt, "exit ns_init()%s\n",		    needmaint ? ", need maintenance immediately" : ""));	loads++;}/* * Read the actual boot file. * Set up to recurse. */static voidboot_read(bootfile)	char *bootfile;{	register struct zoneinfo *zp;	char buf[BUFSIZ], obuf[BUFSIZ], *source;	FILE *fp;	int type;	int class;#ifdef GEN_AXFR	char *class_p;#endif	struct stat f_time;	static int tmpnum = 0;		/* unique number for tmp zone files */#ifdef ALLOW_UPDATES	char *cp, *flag;#endif	int slineno;			/* Saved global line number. */	if ((fp = fopen(bootfile, "r")) == NULL) {		syslog(LOG_ERR, "%s: %m", bootfile);		exit(1);	}	slineno = lineno;	lineno = 0;	while (!feof(fp) && !ferror(fp)) {		if (!getword(buf, sizeof(buf), fp))			continue;		/* read named.boot keyword and process args */		if (strcasecmp(buf, "directory") == 0) {			(void) getword(buf, sizeof(buf), fp);			if (chdir(buf) < 0) {				syslog(LOG_CRIT, "directory %s: %m\n",					buf);				exit(1);			}			continue;		} else if (strcasecmp(buf, "sortlist") == 0) {			get_netlist(fp, enettab, ALLOW_NETS, buf);			continue;		} else if (strcasecmp(buf, "forwarders") == 0) {			get_forwarders(fp);			continue;		} else if (strcasecmp(buf, "slave") == 0) {			forward_only++;			endline(fp);			continue;#ifdef BOGUSNS		} else if (strcasecmp(buf, "bogusns") == 0) {			get_netlist(fp, &boglist, ALLOW_HOSTS, buf);			continue;#endif#ifdef XFRNETS		} else if ((strcasecmp(buf, "tcplist") == 0) ||			   (strcasecmp(buf, "xfrnets") == 0)) {			get_netlist(fp, &xfrnets, ALLOW_NETS, buf);			continue;#endif#ifdef LOCALDOM		} else if (strcasecmp(buf, "domain") == 0) {			if (getword(buf, sizeof(buf), fp))				localdomain = savestr(buf);			endline(fp);			continue;#endif		} else if (strcasecmp(buf, "include") == 0) {			if (getword(buf, sizeof(buf), fp))				boot_read(buf);			endline(fp);			continue;		} else if (strcasecmp(buf, "cache") == 0) {			type = Z_CACHE;			class = C_IN;#ifdef GEN_AXFR			if (class_p = strchr(buf, '/'))				class = get_class(class_p+1);#endif		} else if (strncasecmp(buf, "primary", 7) == 0) {			type = Z_PRIMARY;			class = C_IN;#ifdef GEN_AXFR			if (class_p = strchr(buf, '/'))				class = get_class(class_p+1);#endif		} else if (strncasecmp(buf, "secondary", 9) == 0) {			type = Z_SECONDARY;			class = C_IN;#ifdef GEN_AXFR			if (class_p = strchr(buf, '/'))				class = get_class(class_p+1);#endif#ifdef STUBS		} else if (strncasecmp(buf, "stub", 4) == 0) {			type = Z_STUB;			class = C_IN;#ifdef GEN_AXFR			if (class_p = strchr(buf, '/'))				class = get_class(class_p+1);#endif#endif		} else {			syslog(LOG_ERR, "%s: line %d: unknown field '%s'\n",				bootfile, lineno+1, buf);			endline(fp);			continue;		}		/*		 * read zone origin		 */		if (!getword(obuf, sizeof(obuf), fp)) {			syslog(LOG_ERR, "%s: line %d: missing origin\n",			    bootfile, lineno);			continue;		}		dprintf(1, (ddt, "zone origin %s", obuf));		if (obuf[0] == '.' && obuf[1] == '\0')			obuf[0] = '\0';		/*		 * read source file or host address		 */		if (!getword(buf, sizeof(buf), fp)) {			syslog(LOG_ERR, "%s: line %d: missing %s\n",				bootfile, lineno, #ifdef STUBS			   (type == Z_SECONDARY || type == Z_STUB)#else			   (type == Z_SECONDARY)#endif			       ?"host address"			       :"source file");			continue;		}		/* check for previous instance of this zone (reload) */		if (!(zp = find_zone(obuf, type, class))) {			if (type == Z_CACHE) {				zp = &zones[0];				zp->z_origin = "";				goto gotzone;			}			for (zp = &zones[1]; zp < &zones[nzones]; zp++)				if (zp->z_type == Z_NIL)					goto gotzone;			/*			 * this code assumes that nzones never decreases			 */			if (nzones % 64 == 0) {			    dprintf(1, (ddt,					"Reallocating zones structure\n"));			    /*			     * Realloc() not used since it might damage zones			     * if an error occurs			     */			    zp = (struct zoneinfo *)				malloc((64 + nzones)				       * sizeof(struct zoneinfo));			    if (zp == (struct zoneinfo *)0) {				    syslog(LOG_ERR,					   "no memory for more zones");				    dprintf(1, (ddt,						"Out of memory for new zones\n"						)					    );				    endline(fp);				    continue;			    }			    bcopy((char *)zones, (char *)zp,				  nzones * sizeof(struct zoneinfo));			    bzero((char *)&zp[nzones],				  64 * sizeof(struct zoneinfo));			    free(zones);			    zones = zp;			}			zp = &zones[nzones++];	gotzone:			zp->z_origin = savestr(obuf);			zp->z_type = type;			zp->z_class = class;		}		zp->z_addrcnt = 0;		switch (type) {		case Z_CACHE:			source = savestr(buf);			dprintf(1, (ddt, ", source = %s\n", source));			zp->z_refresh = 0;	/* by default, no dumping */			if (getword(buf, sizeof(buf), fp)) {#ifdef notyet				zp->z_refresh = atoi(buf);				if (zp->z_refresh <= 0) {					syslog(LOG_ERR,				"%s: line %d: bad refresh time '%s', ignored\n",						bootfile, lineno, buf);					zp->z_refresh = 0;				} else if (cache_file == NULL)					cache_file = source;#else				syslog(LOG_WARNING,				"%s: line %d: cache refresh ignored\n",					bootfile, lineno);#endif				endline(fp);			}			/*			 * If we've loaded this file, and the file has			 * not been modified and contains no $include,			 * then there's no need to reload.			 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -