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

📄 getinfo.c

📁 一个可用的dns服务器源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * ++Copyright++ 1985, 1989 * - * Copyright (c) 1985, 1989 *    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-- */#ifndef lintstatic char sccsid[] = "@(#)getinfo.c	5.26 (Berkeley) 3/21/91";static char rcsid[] = "$Id: getinfo.c,v 8.6 1996/12/02 09:17:24 vixie Exp $";#endif /* not lint *//* ****************************************************************************** * *  getinfo.c -- * *	Routines to create requests to name servers *	and interpret the answers. * *	Adapted from 4.3BSD BIND gethostnamadr.c * ****************************************************************************** */#ifndef WINNT#include <sys/param.h>#include <sys/socket.h>#include <netinet/in.h>#endif #include <nameser.h>#include <inet.h>#include <resolv.h>#include <stdio.h>#include <ctype.h>#include "res.h"#include "portability.h"extern char *_res_resultcodes[];extern char *res_skip();static char *addr_list[MAXADDRS + 1];static char *host_aliases[MAXALIASES];static int   host_aliases_len[MAXALIASES];static u_char  hostbuf[BUFSIZ+1];typedef struct {    char *name;    char *domain[MAXDOMAINS];    int   numDomains;    char *address[MAXADDRS];    int   numAddresses;} ServerTable;ServerTable server[MAXSERVERS];typedef union {    HEADER qb1;    u_char qb2[PACKETSZ*2];} querybuf;typedef union {    int32_t al;    char ac;} align;#define GetShort(cp)	_getshort(cp); cp += INT16SZ;/* * Forward references */int GetHostDomain(struct in_addr *nsAddrPtr, int queryClass, int queryType, char *name,               char *domain, HostInfo *hostPtr, Boolean isServer);static int GetAnswer(struct in_addr	*nsAddrPtr, int queryType, char *msg, int msglen, Boolean iquery,                      register HostInfo	*hostPtr, Boolean isServer);/* ****************************************************************************** * *  GetAnswer -- * *	Interprets an answer packet and retrieves the following *	information: * *  Results: *      SUCCESS         the info was retrieved. *      NO_INFO         the packet did not contain an answer. *	NONAUTH		non-authoritative information was found. *      ERROR           the answer was malformed. *      Other errors    returned in the packet header. * ****************************************************************************** */static intGetAnswer(nsAddrPtr, queryType, msg, msglen, iquery, hostPtr, isServer)    struct in_addr	*nsAddrPtr;    char		*msg;    int			queryType;    int			msglen;    Boolean		iquery;    register HostInfo	*hostPtr;    Boolean		isServer;{    register HEADER	*headerPtr;    register u_char	*cp;    querybuf		answer;    char		**aliasPtr;    u_char		*eom, *bp;    char		**addrPtr;    char		*namePtr;    char		*dnamePtr;    int			type, class;    int			qdcount, ancount, arcount, nscount, buflen;    int			origClass;    int			numAliases = 0;    int			numAddresses = 0;    int			n, i, j;    int			len;    int			dlen;    int			status;    int			numServers;    Boolean		haveAnswer;    Boolean		printedAnswers = FALSE;#ifndef GUI filePtr = stdout;#else filePtr = outputfile;#endif    /*     *  If the hostPtr was used before, free up the calloc'd areas.     */    FreeHostInfoPtr(hostPtr);    status = SendRequest(nsAddrPtr, msg, msglen, (char *) &answer,			    sizeof(answer), &n);    if (status != SUCCESS) {	    if (_res.options & RES_DEBUG2)		    printf("SendRequest failed\n");	    return (status);    }    eom = (u_char *) &answer + n;    headerPtr = (HEADER *) &answer;    if (headerPtr->rcode != NOERROR) {	return (headerPtr->rcode);    }    qdcount = ntohs( (u_short) headerPtr->qdcount);    ancount = ntohs( (u_short) headerPtr->ancount);    arcount = ntohs( (u_short) headerPtr->arcount);    nscount = ntohs( (u_short) headerPtr->nscount);    /*     * If there are no answer, n.s. or additional records     * then return with an error.     */    if (ancount == 0 && nscount == 0 && arcount == 0) {	return (NO_INFO);    }    bp	   = hostbuf;    buflen = sizeof(hostbuf);    cp	   = (u_char *) &answer + HFIXEDSZ;    /* Skip over question section. */    while (qdcount-- > 0) {	cp += dn_skipname(cp, eom) + QFIXEDSZ;    }    aliasPtr	= host_aliases;    addrPtr	= addr_list;    haveAnswer	= FALSE;    /*     * Scan through the answer resource records.     * Answers for address query types are saved.     * Other query type answers are just printed.     */    if (ancount != 0) {	if (!isServer && !headerPtr->aa) {#ifndef GUI	    printf("Non-authoritative answer:\n");#else	    fprintf(outputfile,"Non-authoritative answer:\n");#endif	}	if (queryType != T_A && !(iquery && queryType == T_PTR)) {	    while (--ancount >= 0 && cp < eom) {		if ((cp = (u_char *)Print_rr(cp,		    (char *)&answer, eom, filePtr)) == NULL) {		    return(ERROR);		}	    }	    printedAnswers = TRUE;	} else {	    while (--ancount >= 0 && cp < eom) {		n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);		if (n < 0) {		    return(ERROR);		}		cp   += n;		type  = GetShort(cp);		class = GetShort(cp);		cp   += INT32SZ;	/* skip TTL */		dlen  = GetShort(cp);		if (type == T_CNAME) {		    /*		     * Found an alias.		     */		    cp += dlen;		    if (aliasPtr >= &host_aliases[MAXALIASES-1]) {			continue;		    }		    *aliasPtr++ = (char *)bp;		    n = strlen((char *)bp) + 1;		    host_aliases_len[numAliases] = n;		    numAliases++;		    bp += n;		    buflen -= n;		    continue;		} else if (type == T_PTR) {		    /*		     *  Found a "pointer" to the real name.		     */		    n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);		    if (n < 0) {			cp += n;			continue;		    }		    cp += n;		    len = strlen((char *)bp) + 1;		    hostPtr->name = Calloc(1, len);		    bcopy(bp, hostPtr->name, len);		    haveAnswer = TRUE;		    break;		} else if (type != T_A) {		    cp += dlen;		    continue;		}		if (haveAnswer) {		    /*		     * If we've already got 1 address, we aren't interested		     * in addresses with a different length or class.		     */		    if (dlen != hostPtr->addrLen) {			cp += dlen;			continue;		    }		    if (class != origClass) {			cp += dlen;			continue;		    }		} else {		    /*		     * First address: record its length and class so we		     * only save additonal ones with the same attributes.		     */		    hostPtr->addrLen = dlen;		    origClass = class;		    hostPtr->addrType = (class == C_IN) ? AF_INET : AF_UNSPEC;		    len = strlen((char *)bp) + 1;		    hostPtr->name = Calloc(1, len);		    bcopy(bp, hostPtr->name, len);		}		bp += (((u_int32_t)bp) % sizeof(align));		if (bp + dlen >= &hostbuf[sizeof(hostbuf)]) {		    if (_res.options & RES_DEBUG) {			printf("Size (%d) too big\n", dlen);		    }		    break;		}		if (numAddresses >= MAXADDRS) {#ifndef GUI			fprintf(stderr,"MAXADDRS exceeded: skipping address\n");#else			fprintf(outputfile,"MAXADDRS exceeded: skipping address\n");#endif			cp += dlen;			continue;		}		bcopy(cp, *addrPtr++ = (char *)bp, dlen);		bp +=dlen;		cp += dlen;		numAddresses++;		haveAnswer = TRUE;	    }	}    }    if ((queryType == T_A || queryType == T_PTR) && haveAnswer) {	/*	 *  Go through the alias and address lists and return them	 *  in the hostPtr variable.	 */	if (numAliases > 0) {	    hostPtr->aliases =		(char **) Calloc(1 + numAliases, sizeof(char *));	    for (i = 0; i < numAliases; i++) {		hostPtr->aliases[i] = Calloc(1, host_aliases_len[i]);		bcopy(host_aliases[i],		      hostPtr->aliases[i],		      host_aliases_len[i]);	    }	    hostPtr->aliases[i] = NULL;	}	if (numAddresses > 0) {	    hostPtr->addrList =		(char **)Calloc(1+numAddresses, sizeof(char *));	    for (i = 0; i < numAddresses; i++) {		hostPtr->addrList[i] = Calloc(1, hostPtr->addrLen);		bcopy(addr_list[i], hostPtr->addrList[i], hostPtr->addrLen);	    }	    hostPtr->addrList[i] = NULL;	}#ifdef verbose	if (headerPtr->aa || nscount == 0) {	    hostPtr->servers = NULL;	    return (SUCCESS);	}#else	hostPtr->servers = NULL;	return (SUCCESS);#endif    }    /*     * At this point, for the T_A query type, only empty answers remain.     * For other query types, additional information might be found     * in the additional resource records part.     */    if (!headerPtr->aa && (queryType != T_A) && (nscount > 0 || arcount > 0)) {	if (printedAnswers) {	    putchar('\n');	}#ifndef GUI	printf("Authoritative answers can be found from:\n");#else	fprintf(outputfile,"Authoritative answers can be found from:\n");#endif    }    cp = (u_char *)res_skip((char *) &answer, 2, eom);    numServers = 0;    if (queryType != T_A) {	/*	 * If we don't need to save the record, just print it.	 */	while (--nscount >= 0 && cp < eom) {	    if ((cp = (u_char *)Print_rr(cp,		(char *) &answer, eom, filePtr)) == NULL) {		return(ERROR);	    }	}    } else {	while (--nscount >= 0 && cp < eom) {	    /*	     *  Go through the NS records and retrieve the names of hosts	     *  that serve the requested domain.	     */	    n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);	    if (n < 0) {		return(ERROR);	    }	    cp += n;	    len = strlen((char *)bp) + 1;	    dnamePtr = Calloc(1, len);   /* domain name */	    bcopy(bp, dnamePtr, len);	    type  = GetShort(cp);	    class = GetShort(cp);	    cp   += INT32SZ;	/* skip TTL */	    dlen  = GetShort(cp);	    if (type != T_NS) {		cp += dlen;	    } else {

⌨️ 快捷键说明

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