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

📄 gethostbydns.c

📁 RTEMS (Real-Time Executive for Multiprocessor Systems) is a free open source real-time operating sys
💻 C
📖 第 1 页 / 共 2 页
字号:
				bp += nn;				buflen -= nn;			}			bp += sizeof(align) - ((u_long)bp % sizeof(align));			if (bp + n >= &hostbuf[sizeof hostbuf]) {				dprintf("size (%d) too big\n", n);				had_error++;				continue;			}			if (hap >= &h_addr_ptrs[MAXADDRS-1]) {				if (!toobig++)					dprintf("Too many addresses (%d)\n",						MAXADDRS);				cp += n;				continue;			}			bcopy(cp, *hap++ = bp, n);			bp += n;			buflen -= n;			cp += n;			if (cp != erdata) {				h_errno = NO_RECOVERY;				return (NULL);			}			break;		default:			dprintf("Impossible condition (type=%d)\n", type);			h_errno = NO_RECOVERY;			return (NULL);			/* BIND has abort() here, too risky on bad data */		}		if (!had_error)			haveanswer++;	}	if (haveanswer) {		*ap = NULL;		*hap = NULL;# if defined(RESOLVSORT)		/*		 * Note: we sort even if host can take only one address		 * in its return structures - should give it the "best"		 * address in that case, not some random one		 */		if (_res.nsort && haveanswer > 1 && qtype == T_A)			addrsort(h_addr_ptrs, haveanswer);# endif /*RESOLVSORT*/		if (!host.h_name) {			n = strlen(qname) + 1;	/* for the \0 */			if (n > buflen || n >= MAXHOSTNAMELEN)				goto no_recovery;			strcpy(bp, qname);			host.h_name = bp;			bp += n;			buflen -= n;		}		if (_res.options & RES_USE_INET6)			_map_v4v6_hostent(&host, &bp, &buflen);		h_errno = NETDB_SUCCESS;		return (&host);	} no_recovery:	h_errno = NO_RECOVERY;	return (NULL);}struct hostent *__dns_getanswer(answer, anslen, qname, qtype)	const char *answer;	int anslen;	const char *qname;	int qtype;{	switch(qtype) {	case T_AAAA:		host.h_addrtype = AF_INET6;		host.h_length = IN6ADDRSZ;		break;	case T_A:	default:		host.h_addrtype = AF_INET;		host.h_length = INADDRSZ;		break;	}	return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));}struct hostent *_gethostbydnsname(name, af)	const char *name;	int af;{	querybuf buf;	register const char *cp;	char *bp;	int n, size, type, len;	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {		h_errno = NETDB_INTERNAL;		return (NULL);	}	switch (af) {	case AF_INET:		size = INADDRSZ;		type = T_A;		break;	case AF_INET6:		size = IN6ADDRSZ;		type = T_AAAA;		break;	default:		h_errno = NETDB_INTERNAL;		errno = EAFNOSUPPORT;		return (NULL);	}	host.h_addrtype = af;	host.h_length = size;	/*	 * if there aren't any dots, it could be a user-level alias.	 * this is also done in res_query() since we are not the only	 * function that looks up host names.	 */	if (!strchr(name, '.') && (cp = __hostalias(name)))		name = cp;	/*	 * disallow names consisting only of digits/dots, unless	 * they end in a dot.	 */	if (isdigit((int)name[0]))		for (cp = name;; ++cp) {			if (!*cp) {				if (*--cp == '.')					break;				/*				 * All-numeric, no dot at the end.				 * Fake up a hostent as if we'd actually				 * done a lookup.				 */				if (inet_pton(af, name, host_addr) <= 0) {					h_errno = HOST_NOT_FOUND;					return (NULL);				}				strncpy(hostbuf, name, MAXDNAME);				hostbuf[MAXDNAME] = '\0';				bp = hostbuf + MAXDNAME;				len = sizeof hostbuf - MAXDNAME;				host.h_name = hostbuf;				host.h_aliases = host_aliases;				host_aliases[0] = NULL;				h_addr_ptrs[0] = (char *)host_addr;				h_addr_ptrs[1] = NULL;				host.h_addr_list = h_addr_ptrs;				if (_res.options & RES_USE_INET6)					_map_v4v6_hostent(&host, &bp, &len);				h_errno = NETDB_SUCCESS;				return (&host);			}			if (!isdigit((int)*cp) && *cp != '.') 				break;		}	if ((isxdigit((int)name[0]) && strchr(name, ':') != NULL) ||	    name[0] == ':')		for (cp = name;; ++cp) {			if (!*cp) {				if (*--cp == '.')					break;				/*				 * All-IPv6-legal, no dot at the end.				 * Fake up a hostent as if we'd actually				 * done a lookup.				 */				if (inet_pton(af, name, host_addr) <= 0) {					h_errno = HOST_NOT_FOUND;					return (NULL);				}				strncpy(hostbuf, name, MAXDNAME);				hostbuf[MAXDNAME] = '\0';				bp = hostbuf + MAXDNAME;				len = sizeof hostbuf - MAXDNAME;				host.h_name = hostbuf;				host.h_aliases = host_aliases;				host_aliases[0] = NULL;				h_addr_ptrs[0] = (char *)host_addr;				h_addr_ptrs[1] = NULL;				host.h_addr_list = h_addr_ptrs;				h_errno = NETDB_SUCCESS;				return (&host);			}			if (!isxdigit((int)*cp) && *cp != ':' && *cp != '.') 				break;		}	if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {		dprintf("res_search failed (%d)\n", n);		return (NULL);	}	return (gethostanswer(&buf, n, name, type));}struct hostent *_gethostbydnsaddr(addr, len, af)	const char *addr;	/* XXX should have been def'd as u_char! */	int len, af;{	const u_char *uaddr = (const u_char *)addr;	static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };	static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };	int n, size;	querybuf buf;	register struct hostent *hp;	char qbuf[MAXDNAME+1], *qp;#ifdef SUNSECURITY	register struct hostent *rhp;	char **haddr;	u_long old_options;	char hname2[MAXDNAME+1];#endif /*SUNSECURITY*/		if ((_res.options & RES_INIT) == 0 && res_init() == -1) {		h_errno = NETDB_INTERNAL;		return (NULL);	}	if (af == AF_INET6 && len == IN6ADDRSZ &&	    (!bcmp(uaddr, mapped, sizeof mapped) ||	     !bcmp(uaddr, tunnelled, sizeof tunnelled))) {		/* Unmap. */		addr += sizeof mapped;		uaddr += sizeof mapped;		af = AF_INET;		len = INADDRSZ;	}	switch (af) {	case AF_INET:		size = INADDRSZ;		break;	case AF_INET6:		size = IN6ADDRSZ;		break;	default:		errno = EAFNOSUPPORT;		h_errno = NETDB_INTERNAL;		return (NULL);	}	if (size != len) {		errno = EINVAL;		h_errno = NETDB_INTERNAL;		return (NULL);	}	switch (af) {	case AF_INET:		(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",			       (uaddr[3] & 0xff),			       (uaddr[2] & 0xff),			       (uaddr[1] & 0xff),			       (uaddr[0] & 0xff));		break;	case AF_INET6:		qp = qbuf;		for (n = IN6ADDRSZ - 1; n >= 0; n--) {			qp += SPRINTF((qp, "%x.%x.",				       uaddr[n] & 0xf,				       (uaddr[n] >> 4) & 0xf));		}		strcpy(qp, "ip6.int");		break;	default:		abort();	}	n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);	if (n < 0) {		dprintf("res_query failed (%d)\n", n);		return (NULL);	}	if (!(hp = gethostanswer(&buf, n, qbuf, T_PTR)))		return (NULL);	/* h_errno was set by gethostanswer() */#ifdef SUNSECURITY	if (af == AF_INET) {	    /*	     * turn off search as the name should be absolute,	     * 'localhost' should be matched by defnames	     */	    strncpy(hname2, hp->h_name, MAXDNAME);	    hname2[MAXDNAME] = '\0';	    old_options = _res.options;	    _res.options &= ~RES_DNSRCH;	    _res.options |= RES_DEFNAMES;	    if (!(rhp = gethostbyname(hname2))) {		syslog(LOG_NOTICE|LOG_AUTH,		       "gethostbyaddr: No A record for %s (verifying [%s])",		       hname2, inet_ntoa(*((struct in_addr *)addr)));		_res.options = old_options;		h_errno = HOST_NOT_FOUND;		return (NULL);	    }	    _res.options = old_options;	    for (haddr = rhp->h_addr_list; *haddr; haddr++)		if (!memcmp(*haddr, addr, INADDRSZ))			break;	    if (!*haddr) {		syslog(LOG_NOTICE|LOG_AUTH,		       "gethostbyaddr: A record of %s != PTR record [%s]",		       hname2, inet_ntoa(*((struct in_addr *)addr)));		h_errno = HOST_NOT_FOUND;		return (NULL);	    }	}#endif /*SUNSECURITY*/	hp->h_addrtype = af;	hp->h_length = len;	bcopy(addr, host_addr, len);	h_addr_ptrs[0] = (char *)host_addr;	h_addr_ptrs[1] = NULL;	if (af == AF_INET && (_res.options & RES_USE_INET6)) {		_map_v4v6_address((char*)host_addr, (char*)host_addr);		hp->h_addrtype = AF_INET6;		hp->h_length = IN6ADDRSZ;	}	h_errno = NETDB_SUCCESS;	return (hp);}#ifdef RESOLVSORTstatic voidaddrsort(ap, num)	char **ap;	int num;{	int i, j;	char **p;	short aval[MAXADDRS];	int needsort = 0;	p = ap;	for (i = 0; i < num; i++, p++) {	    for (j = 0 ; (unsigned)j < _res.nsort; j++)		if (_res.sort_list[j].addr.s_addr == 		    (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))			break;	    aval[i] = j;	    if (needsort == 0 && i > 0 && j < aval[i-1])		needsort = i;	}	if (!needsort)	    return;	while (needsort < num) {	    for (j = needsort - 1; j >= 0; j--) {		if (aval[j] > aval[j+1]) {		    char *hp;		    i = aval[j];		    aval[j] = aval[j+1];		    aval[j+1] = i;		    hp = ap[j];		    ap[j] = ap[j+1];		    ap[j+1] = hp;		} else		    break;	    }	    needsort++;	}}#endifvoid_sethostdnsent(stayopen)	int stayopen;{	if ((_res.options & RES_INIT) == 0 && res_init() == -1)		return;	if (stayopen)		_res.options |= RES_STAYOPEN | RES_USEVC;}void_endhostdnsent(){	_res.options &= ~(RES_STAYOPEN | RES_USEVC);	res_close();}

⌨️ 快捷键说明

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