struct hostent에서 h_name과 h_aliases의 출력 형태에 대한 질문

cwseo7의 이미지

안녕하세요.

------------------------------------
1) 질문 1

다름이 아니라 윈도우에서 gethostbyname과 gethostbyaddr를 이용하여 프로그램을 작성하였습니다.
그런데, 문제는 일반적인 서버의 도메인 네임과 ip 주소를 이용할 경우,
도메인 네임으로 gethostbyname을 사용할때는 h_aliases의 내용이 나오지 않는데,
ip 주소로 gethostbyaddr를 이용하면 h_aliases 내용을 출력할 수 있었습니다.
이 반대의 경우도 생깁니다.
왜 이런 현상이 일어나는지요?
------------------------------------
2) 질문 2

질문 1)에서와 같이 실행하고 나서 결과를 보면 h_name이 서로 다른 경우가 많은데,
이것은 어떻게 그러한지요?
보통 h_name은 공식적인 도메인 아닌가요?
------------------------------------

프로그램은 일반적인 VC++6을 사용하였고, 아래에 프로그램을 올립니다.
여기서 도메인 네임과 ip주소는 같은 곳이며,
프로그램에서 도메인 네임(여기서는 www.yahoo.co.kr)을 gethostbyname에 입력하고,
그 결과로 얻은 h_addr_list[0] 주소를 다시 gethostbyaddr에 입력하였습니다.

알려주시면 감사하겠습니다.

------------------------

#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
 
void ViewInfo(HOSTENT  *ptr);
void ViewInfo(HOSTENT  *ptr)
{
	printf("  # h_name         | %s \n", ptr->h_name);
	int n1=0;
	while(ptr->h_aliases[n1] != NULL) {
		printf("  # h_aliases[%d]   | %s \n", n1, (ptr->h_aliases[n1++]) );
	}
		printf("  # h_addtype      | %d \n", ptr->h_addrtype);
		printf("  # h_length       | %d \n", ptr->h_length);
 
	int n2=0;
	while(ptr->h_addr_list[n2] != NULL) {
		IN_ADDR addr1;
		memcpy(&addr1, ptr->h_addr_list[n2], ptr->h_length);
		printf("  # h_addr_list[%d] | %s \n", n2++, inet_ntoa(addr1) );
	} 
}
 
void err_display(char *msg)
{
	LPVOID lpMsgBuf;
	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER|
		FORMAT_MESSAGE_FROM_SYSTEM,
		NULL, WSAGetLastError(),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR)&lpMsgBuf, 0, NULL);
	printf("[%s] %s", msg, (LPCTSTR)lpMsgBuf);
	LocalFree(lpMsgBuf);
}
 
BOOL GetIPAddr(char *name, IN_ADDR *addr)
{
	HOSTENT *ptr = gethostbyname(name);
	if(ptr == NULL){
		err_display("gethostbyname()");
		return FALSE;
	}
	ViewInfo(ptr); 
 
	memcpy(addr, ptr->h_addr, ptr->h_length);
	return TRUE;
}
 
BOOL GetDomainName(IN_ADDR addr, char *name)
{
	HOSTENT *ptr = gethostbyaddr((char *)&addr,	sizeof(addr), AF_INET);
	if(ptr == NULL){ 
		err_display("gethostbyaddr()");
		return FALSE;
	}
	ViewInfo(ptr); 
 
	strcpy(name, ptr->h_name);
	return TRUE;
}
 
int main(int argc, char* argv[])
{
	WSADATA wsa;
	if(WSAStartup(MAKEWORD(2, 2), &wsa) != 0) return -1;
 
	IN_ADDR addr;
	if(GetIPAddr("www.yahoo.co.kr", &addr)){ 
		printf("IP 주소 = %s\n", inet_ntoa(addr));
 
		char name[256];
		if(GetDomainName(addr, name)){
			printf("도메인 이름 = %s\n", name);
		}
	}
 
	WSACleanup();
	return 0;
}

*****************************
다음은 결과입니다.
*****************************

  # h_name         | rc.fy.b.yahoo.com 
  # h_aliases[0]   | <a href="http://www.yahoo.co.kr" rel="nofollow">www.yahoo.co.kr</a> 
  # h_aliases[1]   | rc.yahoo.com 
  # h_addtype      | 2 
  # h_length       | 4 
  # h_addr_list[0] | 68.180.206.184 
IP 주소 = 68.180.206.184
  # h_name         | w2.rc.vip.sp1.yahoo.com 
  # h_addtype      | 2 
  # h_length       | 4 
  # h_addr_list[0] | 68.180.206.184 
도메인 이름 = w2.rc.vip.sp1.yahoo.com

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.