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

📄 gethnamaddr.c

📁 一个C源代码分析器
💻 C
📖 第 1 页 / 共 2 页
字号:
struct hostent *gethostbyname(name)	const char *name;{	querybuf buf;	register const char *cp;	int n;	extern struct hostent *_gethtbyname();	/*	 * disallow names consisting only of digits/dots, unless	 * they end in a dot.	 */	if (isdigit(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_aton(name, &host_addr)) {					h_errno = HOST_NOT_FOUND;					return (NULL);				}				host.h_name = (char *)name;				host.h_aliases = host_aliases;				host_aliases[0] = NULL;				host.h_addrtype = AF_INET;				host.h_length = INT32SZ;				h_addr_ptrs[0] = (char *)&host_addr;				h_addr_ptrs[1] = NULL;#if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */				host.h_addr_list = h_addr_ptrs;#else				host.h_addr = h_addr_ptrs[0];#endif				return (&host);			}			if (!isdigit(*cp) && *cp != '.') 				break;		}	if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) {#ifdef DEBUG		if (_res.options & RES_DEBUG)			printf("res_search failed\n");#endif		if (errno == ECONNREFUSED)			return (_gethtbyname(name));		else			return (NULL);	}	return (getanswer(&buf, n, name, C_IN, T_A));}struct hostent *gethostbyaddr(addr, len, type)	const char *addr;	int len, type;{	int n;	querybuf buf;	register struct hostent *hp;	char qbuf[MAXDNAME+1];#ifdef SUNSECURITY	register struct hostent *rhp;	char **haddr;	u_long old_options;#endif /*SUNSECURITY*/	extern struct hostent *_gethtbyaddr();		if (type != AF_INET)		return (NULL);	(void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",		((unsigned)addr[3] & 0xff),		((unsigned)addr[2] & 0xff),		((unsigned)addr[1] & 0xff),		((unsigned)addr[0] & 0xff));	n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);	if (n < 0) {#ifdef DEBUG		if (_res.options & RES_DEBUG)			printf("res_query failed\n");#endif		if (errno == ECONNREFUSED)			return (_gethtbyaddr(addr, len, type));		return (NULL);	}	if (!(hp = getanswer(&buf, n, qbuf, C_IN, T_PTR)))		return (NULL);#ifdef SUNSECURITY	/*	 * turn off search as the name should be absolute,	 * 'localhost' should be matched by defnames	 */	old_options = _res.options;	_res.options &= ~RES_DNSRCH;	_res.options |= RES_DEFNAMES;	if (!(rhp = gethostbyname(hp->h_name))) {		syslog(LOG_NOTICE|LOG_AUTH,		       "gethostbyaddr: No A record for %s (verifying [%s])",		       hp->h_name, inet_ntoa(*((struct in_addr *)addr)));		_res.options = old_options;		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]",		       hp->h_name, inet_ntoa(*((struct in_addr *)addr)));		h_errno = HOST_NOT_FOUND;		return (NULL);	}#endif /*SUNSECURITY*/	hp->h_addrtype = type;	hp->h_length = len;	h_addr_ptrs[0] = (char *)&host_addr;	h_addr_ptrs[1] = NULL;	host_addr = *(struct in_addr *)addr;#if BSD < 43 && !defined(h_addr)	/* new-style hostent structure */	hp->h_addr = h_addr_ptrs[0];#endif	return (hp);}void_sethtent(f)	int f;{	if (!hostf)		hostf = fopen(_PATH_HOSTS, "r" );	else		rewind(hostf);	stayopen = f;}void_endhtent(){	if (hostf && !stayopen) {		(void) fclose(hostf);		hostf = NULL;	}}struct hostent *_gethtent(){	char *p;	register char *cp, **q;	if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" )))		return (NULL);again:	if (!(p = fgets(hostbuf, sizeof hostbuf, hostf)))		return (NULL);	if (*p == '#')		goto again;	if (!(cp = strpbrk(p, "#\n")))		goto again;	*cp = '\0';	if (!(cp = strpbrk(p, " \t")))		goto again;	*cp++ = '\0';	/* THIS STUFF IS INTERNET SPECIFIC */	if (!inet_aton(p, &host_addr))		goto again;	h_addr_ptrs[0] = (char *)&host_addr;	h_addr_ptrs[1] = NULL;#if BSD >= 43 || defined(h_addr)	/* new-style hostent structure */	host.h_addr_list = h_addr_ptrs;#else	host.h_addr = h_addr_ptrs[0];#endif	host.h_length = INT32SZ;	host.h_addrtype = AF_INET;	while (*cp == ' ' || *cp == '\t')		cp++;	host.h_name = cp;	q = host.h_aliases = host_aliases;	if (cp = strpbrk(cp, " \t"))		*cp++ = '\0';	while (cp && *cp) {		if (*cp == ' ' || *cp == '\t') {			cp++;			continue;		}		if (q < &host_aliases[MAXALIASES - 1])			*q++ = cp;		if (cp = strpbrk(cp, " \t"))			*cp++ = '\0';	}	*q = NULL;	return (&host);}struct hostent *_gethtbyname(name)	char *name;{	register struct hostent *p;	register char **cp;		_sethtent(0);	while (p = _gethtent()) {		if (strcasecmp(p->h_name, name) == 0)			break;		for (cp = p->h_aliases; *cp != 0; cp++)			if (strcasecmp(*cp, name) == 0)				goto found;	}found:	_endhtent();	return (p);}struct hostent *_gethtbyaddr(addr, len, type)	const char *addr;	int len, type;{	register struct hostent *p;	_sethtent(0);	while (p = _gethtent())		if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len))			break;	_endhtent();	return (p);}#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 ; 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++;	}}#endif#if defined(BSD43_BSD43_NFS) || defined(sun)/* some libc's out there are bound internally to these names (UMIPS) */voidht_sethostent(stayopen)	int stayopen;{	_sethtent(stayopen);}voidht_endhostent(){	_endhtent();}struct hostent *ht_gethostbyname(name)	char *name;{	return (_gethtbyname(name));}struct hostent *ht_gethostbyaddr(addr, len, type)	const char *addr;	int len, type;{	return (_gethtbyaddr(addr, len, type));}struct hostent *gethostent(){	return (_gethtent());}voiddns_service(){	return;}#undef dn_skipnamedn_skipname(comp_dn, eom)	const u_char *comp_dn, *eom;{	return (__dn_skipname(comp_dn, eom));}#endif /*old-style libc with yp junk in it*/#ifdef ultrix/* more icky libc packaging in ultrix */intlocal_hostname_length(hostname)	const char *hostname;{	int len_host, len_domain;	if (!*_res.defdname)		res_init();	len_host = strlen(hostname);	len_domain = strlen(_res.defdname);	if (len_host > len_domain &&	    !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&	    hostname[len_host - len_domain - 1] == '.')		return (len_host - len_domain - 1);	return (0);}#endif

⌨️ 快捷键说明

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