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

📄 getinfo.c

📁 一个可用的dns服务器源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
		Boolean	found;		n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);		if (n < 0) {		    return(ERROR);		}		cp += n;		len = strlen((char *)bp) + 1;		namePtr = Calloc(1, len); /* server host name */		bcopy(bp, namePtr, len);		/*		 * Store the information keyed by the server host name.		 */		found = FALSE;		for (j = 0; j < numServers; j++) {		    if (strcmp(namePtr, server[j].name) == 0) {			found = TRUE;			free(namePtr);			break;		    }		}		if (found) {		    server[j].numDomains++;		    if (server[j].numDomains <= MAXDOMAINS) {			server[j].domain[server[j].numDomains-1] = dnamePtr;		    }		} else {		    if (numServers >= MAXSERVERS) {			break;		    }		    server[numServers].name = namePtr;		    server[numServers].domain[0] = dnamePtr;		    server[numServers].numDomains = 1;		    server[numServers].numAddresses = 0;		    numServers++;		}	    }	}    }    /*     * Additional resource records contain addresses of servers.     */    cp = (u_char *)res_skip((char *) &answer, 3, eom);    if (queryType != T_A) {	/*	 * If we don't need to save the record, just print it.	 */	while (--arcount >= 0 && cp < eom) {	    if ((cp = (u_char *)Print_rr(cp,		(char *) &answer, eom, filePtr)) == NULL) {		return(ERROR);	    }	}    } else {	while (--arcount >= 0 && cp < eom) {	    n = dn_expand(answer.qb2, eom, cp, (char *)bp, buflen);	    if (n < 0) {		break;	    }	    cp   += n;	    type  = GetShort(cp);	    class = GetShort(cp);	    cp   += INT32SZ;	/* skip TTL */	    dlen  = GetShort(cp);	    if (type != T_A)  {		cp += dlen;		continue;	    } else {		for (j = 0; j < numServers; j++) {		    if (strcmp((char *)bp, server[j].name) == 0) {			server[j].numAddresses++;			if (server[j].numAddresses <= MAXADDRS) {			    server[j].address[server[j].numAddresses-1] = 				    				Calloc(1,dlen);			    bcopy(cp,			      server[j].address[server[j].numAddresses-1],dlen);			    break;			}		    }		}		cp += dlen;	    }	}    }    /*     * If we are returning name server info, transfer it to the hostPtr.     */    if (numServers > 0) {	hostPtr->servers = (ServerInfo **)				Calloc(numServers+1, sizeof(ServerInfo *));	for (i = 0; i < numServers; i++) {	    hostPtr->servers[i] = (ServerInfo *) Calloc(1, sizeof(ServerInfo));	    hostPtr->servers[i]->name = server[i].name;	    hostPtr->servers[i]->domains = (char **)				Calloc(server[i].numDomains+1,sizeof(char *));	    for (j = 0; j < server[i].numDomains; j++) {		hostPtr->servers[i]->domains[j] = server[i].domain[j];	    }	    hostPtr->servers[i]->domains[j] = NULL;	    hostPtr->servers[i]->addrList = (char **)				Calloc(server[i].numAddresses+1,sizeof(char *));	    for (j = 0; j < server[i].numAddresses; j++) {		hostPtr->servers[i]->addrList[j] = server[i].address[j];	    }	    hostPtr->servers[i]->addrList[j] = NULL;	}	hostPtr->servers[i] = NULL;    }    switch (queryType) {	case T_A:		return NONAUTH;	case T_PTR:		if (iquery)			return NO_INFO;		/* fall through */	default:		return SUCCESS;    }}/**********************************************************************************  GetHostInfo --**	Retrieves host name, address and alias information*	for a domain.**	Algorithm from res_search().**  Results:*	ERROR		- res_mkquery failed.*	+ return values from GetAnswer()*********************************************************************************/intGetHostInfoByName(nsAddrPtr, queryClass, queryType, name, hostPtr, isServer)    struct in_addr	*nsAddrPtr;    int			queryClass;    int			queryType;    char		*name;    HostInfo		*hostPtr;    Boolean		isServer;{    int			n;    register int	result;    register char	**domain;    const char		*cp;    Boolean		got_nodata = FALSE;    struct in_addr	ina;    Boolean		tried_as_is = FALSE;    /* Catch explicit addresses */    if ((queryType == T_A) && IsAddr(name, &ina)) {	hostPtr->name = Calloc(strlen(name)+3, 1);	(void)sprintf(hostPtr->name,"[%s]",name);	hostPtr->aliases = NULL;	hostPtr->servers = NULL;	hostPtr->addrType = AF_INET;	hostPtr->addrLen = INADDRSZ;	hostPtr->addrList = (char **)Calloc(2, sizeof(char *));	hostPtr->addrList[0] = Calloc(INT32SZ, sizeof(char));	bcopy((char *)&ina, hostPtr->addrList[0], INADDRSZ);	hostPtr->addrList[1] = NULL;	return(SUCCESS);    }    result = NXDOMAIN;    for (cp = name, n = 0; *cp; cp++)	    if (*cp == '.')		    n++;    if (n == 0 && (cp = hostalias(name))) {#ifndef GUI	    printf("Aliased to \"%s\"\n\n", cp);#else	    fprintf(outputfile,"Aliased to \"%s\"\n\n", cp);#endif	    return (GetHostDomain(nsAddrPtr, queryClass, queryType,		    (char *)cp, (char *)NULL, hostPtr, isServer));    }    /*     * If there are dots in the name already, let's just give it a try     * 'as is'.  The threshold can be set with the "ndots" option.     */    if (n >= (int)_res.ndots) {	    result = GetHostDomain(nsAddrPtr, queryClass, queryType,				   name, (char *)NULL, hostPtr, isServer);            if (result == SUCCESS)	        return (result);	    if (result == NO_INFO)		got_nodata++;            tried_as_is++;    }    /*     * We do at least one level of search if     *	- there is no dot and RES_DEFNAME is set, or     *	- there is at least one dot, there is no trailing dot,     *	  and RES_DNSRCH is set.     */    if ((n == 0 && _res.options & RES_DEFNAMES) ||       (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))	 for (domain = _res.dnsrch; *domain; domain++) {	    result = GetHostDomain(nsAddrPtr, queryClass, queryType,		    name, *domain, hostPtr, isServer);	    /*	     * If no server present, give up.	     * If name isn't found in this domain,	     * keep trying higher domains in the search list	     * (if that's enabled).	     * On a NO_INFO error, keep trying, otherwise	     * a wildcard entry of another type could keep us	     * from finding this entry higher in the domain.	     * If we get some other error (negative answer or	     * server failure), then stop searching up,	     * but try the input name below in case it's fully-qualified.	     */	    if (result == SUCCESS || result == NO_RESPONSE)		    return result;	    if (result == NO_INFO)		    got_nodata++;	    if ((result != NXDOMAIN && result != NO_INFO) ||		(_res.options & RES_DNSRCH) == 0)		    break;    }    /* if we have not already tried the name "as is", do that now.     * note that we do this regardless of how many dots were in the     * name or whether it ends with a dot.     */    if (!tried_as_is &&	(result = GetHostDomain(nsAddrPtr, queryClass, queryType,				name, (char *)NULL, hostPtr, isServer)	 ) == SUCCESS)	    return (result);    if (got_nodata)	result = NO_INFO;    return (result);}/* * Perform a query on the concatenation of name and domain, * removing a trailing dot from name if domain is NULL. */intGetHostDomain(nsAddrPtr, queryClass, queryType, name, domain, hostPtr, isServer)    struct in_addr	*nsAddrPtr;    int			queryClass;    int			queryType;    char		*name, *domain;    HostInfo		*hostPtr;    Boolean		isServer;{    querybuf buf;    char nbuf[2*MAXDNAME+2];    char *longname = nbuf;    int n;    if (domain == NULL) {	    /*	     * Check for trailing '.';	     * copy without '.' if present.	     */	    n = strlen(name) - 1;	    if (name[n] == '.' && n < sizeof(nbuf) - 1) {		    bcopy(name, nbuf, n);		    nbuf[n] = '\0';	    } else		    longname = name;    } else {	    (void)sprintf(nbuf, "%.*s.%.*s",		    MAXDNAME, name, MAXDNAME, domain);	    longname = nbuf;    }    n = res_mkquery(QUERY, longname, queryClass, queryType,		    NULL, 0, 0, buf.qb2, sizeof(buf));    if (n < 0) {	if (_res.options & RES_DEBUG) {	    printf("Res_mkquery failed\n");	}	return (ERROR);    }    n = GetAnswer(nsAddrPtr, queryType, (char *)&buf, n, 0, hostPtr, isServer);    /*     * GetAnswer didn't find a name, so set it to the specified one.     */    if (n == NONAUTH) {	if (hostPtr->name == NULL) {	    int len = strlen(longname) + 1;	    hostPtr->name = Calloc(len, sizeof(char));	    bcopy(longname, hostPtr->name, len);	}    }    return(n);}/**********************************************************************************  GetHostInfoByAddr --**	Performs a PTR lookup in in-addr.arpa to find the host name*	that corresponds to the given address.**  Results:*	ERROR		- res_mkquery failed.*	+ return values from GetAnswer()*********************************************************************************/intGetHostInfoByAddr(nsAddrPtr, address, hostPtr)    struct in_addr	*nsAddrPtr;    struct in_addr	*address;    HostInfo		*hostPtr;{    int		n;    querybuf	buf;    char	qbuf[MAXDNAME];    char	*p = (char *) &address->s_addr;    (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",	    ((unsigned)p[3] & 0xff),	    ((unsigned)p[2] & 0xff),	    ((unsigned)p[1] & 0xff),	    ((unsigned)p[0] & 0xff));    n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, NULL, 0, NULL,		    buf.qb2, sizeof buf);    if (n < 0) {	if (_res.options & RES_DEBUG) {	    printf("res_mkquery() failed\n");	}	return (ERROR);    }    n = GetAnswer(nsAddrPtr, T_PTR, (char *) &buf, n, 1, hostPtr, 1);    if (n == SUCCESS) {	hostPtr->addrType = AF_INET;	hostPtr->addrLen = 4;	hostPtr->addrList = (char **)Calloc(2, sizeof(char *));	hostPtr->addrList[0] = Calloc(INT32SZ, sizeof(char));	bcopy((char *)p, hostPtr->addrList[0], INADDRSZ);	hostPtr->addrList[1] = NULL;    }    return n;}/**********************************************************************************  FreeHostInfoPtr --**	Deallocates all the calloc'd areas for a HostInfo variable.*********************************************************************************/voidFreeHostInfoPtr(hostPtr)    register HostInfo *hostPtr;{    int i, j;    if (hostPtr->name != NULL) {	free(hostPtr->name);	hostPtr->name = NULL;    }    if (hostPtr->aliases != NULL) {	i = 0;	while (hostPtr->aliases[i] != NULL) {	    free(hostPtr->aliases[i]);	    i++;	}	free((char *)hostPtr->aliases);	hostPtr->aliases = NULL;    }    if (hostPtr->addrList != NULL) {	i = 0;	while (hostPtr->addrList[i] != NULL) {	    free(hostPtr->addrList[i]);	    i++;	}	free((char *)hostPtr->addrList);	hostPtr->addrList = NULL;    }    if (hostPtr->servers != NULL) {	i = 0;	while (hostPtr->servers[i] != NULL) {	    if (hostPtr->servers[i]->name != NULL) {		free(hostPtr->servers[i]->name);	    }	    if (hostPtr->servers[i]->domains != NULL) {		j = 0;		while (hostPtr->servers[i]->domains[j] != NULL) {		    free(hostPtr->servers[i]->domains[j]);		    j++;		}		free((char *)hostPtr->servers[i]->domains);	    }	    if (hostPtr->servers[i]->addrList != NULL) {		j = 0;		while (hostPtr->servers[i]->addrList[j] != NULL) {		    free(hostPtr->servers[i]->addrList[j]);		    j++;		}		free((char *)hostPtr->servers[i]->addrList);	    }	    free((char *)hostPtr->servers[i]);	    i++;	}	free((char *)hostPtr->servers);	hostPtr->servers = NULL;    }}

⌨️ 快捷键说明

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