윈도우 소켓 프로그래밍을 공부하고 있습니다.

신농상@Google의 이미지

도메인 이름에서 IP값을 뽑아내고 다시 도메인 이름으로 바꾸는 연습하는 중입니다.
gethostbyname()을 이용해서 IP를 뽑아내는 것은 잘 되는데 gethostbyaddr()를 이용해서 다시 도메인 이름으로 바꿀 때는 이상한 주소로 나오네요.
나온 주소로 가보니 제대로된 페이지도 아니구요.

아래는 제가 작성한 코드입니다.

#pragma comment(lib, "ws2_32")
//#include <WinSock2.h>			// WS2tcpip.h에 포함되어 있으므로 따로 추가 안해줘도 된다.
#include <WS2tcpip.h>
#include <iostream>
 
// 유니코드 문자 집합
 
using namespace std;
 
void err_display(LPCWSTR msg)
{
	LPVOID lpMsgBuf;
 
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
		nullptr, WSAGetLastError(),
		LANG_USER_DEFAULT,
		(LPWSTR)&lpMsgBuf
		, 0, nullptr
	);
 
	MessageBox(nullptr, (LPCWSTR)lpMsgBuf, msg, MB_ICONERROR);	// 메세지 박스로 보고싶으면 사용
	//cout << "[" << msg << "] " << (LPCWSTR)lpMsgBuf << endl;	// 콘솔에 에러코드가 출력된다.
									// 말 그대로 에러코드가 출력되니 가급적 메세지 박스를 사용하자.
 
	LocalFree(lpMsgBuf);
}
 
// IPv4 주소 -> 도메인 이름
bool GetIPAddr(const char * name, IN_ADDR * addr)
{
	HOSTENT * ptr = gethostbyname(name);	// 
 
	if (nullptr == ptr)						// 도메인을 찾지 못하면
	{
		err_display(L"gethostbyname");
		return false;
	}
	if (AF_INET != ptr->h_addrtype)			// 찾은 도메인이 IPv4가 아니라면
		return false;
 
	memcpy(addr, ptr->h_addr, ptr->h_length);	// 포인터 값 끼리의 복사는 memcpy로 해야 한다.
	return true;
}
 
// 도메인 이름 -> IPv4
bool GetDomainName(IN_ADDR addr, char * name, int namelen)
{
	HOSTENT * ptr = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);  // 여기서 문제가 발생하는 듯
 
	if (nullptr == ptr)
	{
		err_display(L"gethostbyaddr");
		return false;
	}
	if (AF_INET != ptr->h_addrtype)
		return false;
 
	strncpy(name, ptr->h_name, namelen);
	return true;
}
 
int main()
{
	WSADATA wsa;
 
	if (0 != WSAStartup(MAKEWORD(2, 2), &wsa))
		return 1;
 
	MessageBox(NULL, L"윈속 초기화", L"알림", MB_OK);
 
	const char * TestName = "www.naver.com";
 
	cout << "도메인 이름 변환 전 : " << TestName << endl;
 
	IN_ADDR addr;
	if (GetIPAddr(TestName, &addr))
	{
		cout << "IP 주소 변환 후 : " << inet_ntoa(addr) << endl;
	}
 
	char name[256];
	if (GetDomainName(addr, name, sizeof(name)))
	{
		cout << "도메인 이름 다시 변환 후 : " << name << endl;
	}
 
	WSACleanup();
 
	return 0;
}

C++로 작성했고, 문자 집합은 유니코드로 했습니다.
비주얼 스튜디오 2017 커뮤니티 버전에 윈도우 10입니다.

익명 사용자의 이미지

대개의 경우 gethostbyaddr을 사용했을때 www.naver.com과 같이 나오지 않습니다.
자세한건 DNS, reverse dns lookup 등을 찾아보세요.

결과값이 제대로인지 확인해 보고 싶으신 거라면, 반환된 도메인 네임에 GetIPAddr 함수를 다시 적용시켜서 같은 주소가 나오는지 확인해 보세요.
또는 cvsmtppost001.nm.naver.com 이 주소로 테스트 해보세요.
저 주소는 아마 naver의 메일 서버 주소인듯 한데, 메일 서버들은 대개 reverse dns lookup을 제대로 지원하도록 설정되어 있습니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.