欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++如何實(shí)現(xiàn)DNS域名解析

 更新時(shí)間:2015年07月30日 17:23:15   作者:GoAgent  
這片文章介紹了C++如何實(shí)現(xiàn)DNS域名解析,還有對(duì)相關(guān)技術(shù)的介紹,代碼很詳細(xì),需要的朋友可以參考下

一、概述

現(xiàn)在來(lái)搞定DNS域名解析,其實(shí)這是前面一篇文章C++實(shí)現(xiàn)Ping里面的遺留問題,要干的活是ping的過(guò)程中畫紅線的部分:

cmd下域名解析的命令是nslookup,比如“nslookup www.baidu.com”的結(jié)果如下:

其中,Address返回的就是www.baidu.com對(duì)應(yīng)的IP地址,這個(gè)可能有多個(gè)

Alias指別名,也就是說(shuō)www.baidu.com是www.a.shifen.com的別名,而www.a.shifen.com則是www.baidu.com的規(guī)范名(Canonical Name,CName),具體參考RFC1035 3.2.2 & wikipedia

二、實(shí)現(xiàn)結(jié)果預(yù)覽

先看一下最終搞成了什么樣子

輸入:域名字符串

輸出:IP列表、CName列表、DNS查詢所用時(shí)間

三、相關(guān)技術(shù)

3.1、UDP or TCP ? (RFC1035 4.2)

UDP:DNS查詢和回復(fù)采用低開銷高性能的UDP,端口號(hào)為53。

TCP:輔助DNS服務(wù)器從主DNS服務(wù)器拉取最新數(shù)據(jù)時(shí),采用可靠的TCP傳輸,端口號(hào)也為53。

我們這里做DNS查詢采用UDP,53端口。

3.2、DNS查詢/回復(fù)包頭部解析 (RFC1035 4.1.1)

重點(diǎn)介紹一下我們關(guān)心的部分:

ID(16bits):標(biāo)識(shí)符,一般填入本進(jìn)程的標(biāo)識(shí)符

QR(1bits):標(biāo)志位,查詢包為0,回復(fù)包為1

Opcode(4bits):查詢的種類,標(biāo)準(zhǔn)查詢?yōu)?

QDCOUNT(16bits):DNS查詢/回復(fù)包數(shù)據(jù)部分Question字段的個(gè)數(shù)

ANCOUNT(16bits):DNS查詢/回復(fù)包數(shù)據(jù)部分Answer字段的個(gè)數(shù)

3.2、DNS查詢/回復(fù)包數(shù)據(jù)部分解析 (RFC1035 4.1.2 & 4.1.3)

查詢/回復(fù)包的數(shù)據(jù)部分依次為QDCOUNT個(gè)Question字段、ANCOUNT個(gè)Answer字段....

對(duì)于任意字段,其格式如下:

Name(不定長(zhǎng)):域名,這部分的格式比較復(fù)雜,后面單獨(dú)說(shuō)。

TYPE(16bits):查詢類型/回復(fù)包RDATA類型,比如TYPE=1表示主機(jī)IP地址、TYPE=5表示CNAME,詳見RFC1035 3.2.2

CLASS(16bits):類,一般情況下CLASS=1表示Internet,詳見RFC1035 3.2.4

TTL(32bits,僅回復(fù)包):生存時(shí)間

RDLENGTH(16bits,僅回復(fù)包):RDATA部分的字節(jié)數(shù)

RDATA(不定長(zhǎng),僅回復(fù)包):資源數(shù)據(jù),具體格式取決于TYPE和CLASS,比如TYPE=1、CLASS=1時(shí),RDATA為四個(gè)字節(jié)的IP地址

3.3、Name解析&消息壓縮

3.3.1、一般格式 (RFC1035 4.1.2)

標(biāo)簽內(nèi)容長(zhǎng)度(1個(gè)字節(jié)) + 標(biāo)簽內(nèi)容,以標(biāo)簽內(nèi)容長(zhǎng)度0作為Name的結(jié)束符,例如:

3.3.2、消息壓縮格式 (RFC1035 4.1.4)

如果標(biāo)簽內(nèi)容長(zhǎng)度的二進(jìn)制前兩位是11,則表示消息壓縮。

此時(shí),標(biāo)簽內(nèi)容長(zhǎng)度1個(gè)字節(jié)+后面的1個(gè)字節(jié)一共16位,后14位表示相對(duì)DNS包起始地址的偏移(Byte),例如:

上述例子中,DNS包起始地址為0x0000,c0 13的二進(jìn)制為11000000 00010003,即跳轉(zhuǎn)偏移為0x13個(gè)字節(jié),對(duì)應(yīng)的數(shù)據(jù)為03 63 6f 6d 00。

RFC1035中規(guī)定,支持的消息壓縮規(guī)則為:

①以內(nèi)容長(zhǎng)度0結(jié)尾的標(biāo)簽序列

②偏移指針

③標(biāo)簽序列+偏移指針

也就是說(shuō),Name的消息壓縮要求偏移指針必須在Name的尾部,且不支持同一級(jí)存在多個(gè)偏移指針(偏移指針序列),

但Name的消息壓縮支持嵌套的偏移指針,即指針指向的偏移位置仍然是以偏移指針結(jié)尾的數(shù)據(jù)

四、代碼實(shí)現(xiàn)

#pragma once

//這里需要導(dǎo)入庫(kù) Ws2_32.lib,在不同的IDE下可能不太一樣
//#pragma comment(lib, "Ws2_32.lib")

#include <windows.h>
#include <string>
#include <vector>

#define MAX_DOMAINNAME_LEN 255
#define DNS_PORT  53
#define DNS_TYPE_SIZE 2
#define DNS_CLASS_SIZE 2
#define DNS_TTL_SIZE 4
#define DNS_DATALEN_SIZE 2
#define DNS_TYPE_A  0x0001 //1 a host address
#define DNS_TYPE_CNAME 0x0005 //5 the canonical name for an alias
#define DNS_PACKET_MAX_SIZE (sizeof(DNSHeader) + MAX_DOMAINNAME_LEN + DNS_TYPE_SIZE + DNS_CLASS_SIZE)

struct DNSHeader
{
 USHORT usTransID; //標(biāo)識(shí)符
 USHORT usFlags; //各種標(biāo)志位
 USHORT usQuestionCount; //Question字段個(gè)數(shù) 
 USHORT usAnswerCount; //Answer字段個(gè)數(shù)
 USHORT usAuthorityCount; //Authority字段個(gè)數(shù)
 USHORT usAdditionalCount; //Additional字段個(gè)數(shù)
};

class CDNSLookup
{
public:
 CDNSLookup();
 ~CDNSLookup();

 BOOL DNSLookup(ULONG ulDNSServerIP, char *szDomainName, std::vector<ULONG> *pveculIPList = NULL, std::vector<std::string> *pvecstrCNameList = NULL, ULONG ulTimeout = 1000, ULONG *pulTimeSpent = NULL);
 BOOL DNSLookup(ULONG ulDNSServerIP, char *szDomainName, std::vector<std::string> *pvecstrIPList = NULL, std::vector<std::string> *pvecstrCNameList = NULL, ULONG ulTimeout = 1000, ULONG *pulTimeSpent = NULL);

private:
 BOOL Init();
 BOOL UnInit();
 BOOL DNSLookupCore(ULONG ulDNSServerIP, char *szDomainName, std::vector<ULONG> *pveculIPList, std::vector<std::string> *pvecstrCNameList, ULONG ulTimeout, ULONG *pulTimeSpent);
 BOOL SendDNSRequest(sockaddr_in sockAddrDNSServer, char *szDomainName);
 BOOL RecvDNSResponse(sockaddr_in sockAddrDNSServer, ULONG ulTimeout, std::vector<ULONG> *pveculIPList, std::vector<std::string> *pvecstrCNameList, ULONG *pulTimeSpent);
 BOOL EncodeDotStr(char *szDotStr, char *szEncodedStr, USHORT nEncodedStrSize);
 BOOL DecodeDotStr(char *szEncodedStr, USHORT *pusEncodedStrLen, char *szDotStr, USHORT nDotStrSize, char *szPacketStartPos = NULL);
 ULONG GetTickCountCalibrate();

private:
 BOOL m_bIsInitOK;
 SOCKET m_sock;
 WSAEVENT m_event;
 USHORT m_usCurrentProcID;
 char *m_szDNSPacket;
};
 [DNSLookup.h]

 

#include "DNSLookup.h"
#include <stdio.h>
#include <string.h>

CDNSLookup::CDNSLookup() : 
 m_bIsInitOK(FALSE), 
 m_sock(INVALID_SOCKET),
 m_szDNSPacket(NULL)
{
 m_bIsInitOK = Init();
}

CDNSLookup::~CDNSLookup()
{
 UnInit();
}

BOOL CDNSLookup::DNSLookup(ULONG ulDNSServerIP, char *szDomainName, std::vector<ULONG> *pveculIPList, std::vector<std::string> *pvecstrCNameList, ULONG ulTimeout, ULONG *pulTimeSpent)
{
 return DNSLookupCore(ulDNSServerIP, szDomainName, pveculIPList, pvecstrCNameList, ulTimeout, pulTimeSpent);
}

BOOL CDNSLookup::DNSLookup(ULONG ulDNSServerIP, char *szDomainName, std::vector<std::string> *pvecstrIPList, std::vector<std::string> *pvecstrCNameList, ULONG ulTimeout, ULONG *pulTimeSpent)
{
 std::vector<ULONG> *pveculIPList = NULL;
 if (pvecstrIPList != NULL)
 {
 std::vector<ULONG> veculIPList;
 pveculIPList = &veculIPList;
 }

 BOOL bRet = DNSLookupCore(ulDNSServerIP, szDomainName, pveculIPList, pvecstrCNameList, ulTimeout, pulTimeSpent);

 if (bRet)
 {
 pvecstrIPList->clear();
 char szIP[16] = {'\0'};
 for (std::vector<ULONG>::iterator iter = pveculIPList->begin(); iter != pveculIPList->end(); ++iter)
 {
  BYTE *pbyIPSegment = (BYTE*)(&(*iter));
  //sprintf_s(szIP, 16, "%d.%d.%d.%d", pbyIPSegment[0], pbyIPSegment[1], pbyIPSegment[2], pbyIPSegment[3]);
  sprintf(szIP, "%d.%d.%d.%d", pbyIPSegment[0], pbyIPSegment[1], pbyIPSegment[2], pbyIPSegment[3]);
  pvecstrIPList->push_back(szIP);
 }
 }

 return bRet;
}


BOOL CDNSLookup::Init()
{
 WSADATA wsaData;
 if (WSAStartup(MAKEWORD(2, 2), &wsaData) == SOCKET_ERROR)
 {
 return FALSE;
 }
 
 if ((m_sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
 {
 return FALSE;
 }

 m_event = WSACreateEvent();
 WSAEventSelect(m_sock, m_event, FD_READ);

 m_szDNSPacket = new (std::nothrow) char[DNS_PACKET_MAX_SIZE];
 if (m_szDNSPacket == NULL)
 {
 return FALSE;
 }

 m_usCurrentProcID = (USHORT)GetCurrentProcessId();

 return TRUE;
}

BOOL CDNSLookup::UnInit()
{
 WSACleanup();

 if (m_szDNSPacket != NULL)
 {
 delete [] m_szDNSPacket;
 }

 return TRUE;
}

BOOL CDNSLookup::DNSLookupCore(ULONG ulDNSServerIP, char *szDomainName, std::vector<ULONG> *pveculIPList, std::vector<std::string> *pvecstrCNameList, ULONG ulTimeout, ULONG *pulTimeSpent)
{
 if (m_bIsInitOK == FALSE || szDomainName == NULL)
 {
 return FALSE;
 }

 //配置SOCKET
 sockaddr_in sockAddrDNSServer; 
 sockAddrDNSServer.sin_family = AF_INET; 
 sockAddrDNSServer.sin_addr.s_addr = ulDNSServerIP;
 sockAddrDNSServer.sin_port = htons( DNS_PORT );

 //DNS查詢與解析
 if (!SendDNSRequest(sockAddrDNSServer, szDomainName)
 || !RecvDNSResponse(sockAddrDNSServer, ulTimeout, pveculIPList, pvecstrCNameList, pulTimeSpent))
 {
 return FALSE;
 }

 return TRUE;
}

BOOL CDNSLookup::SendDNSRequest(sockaddr_in sockAddrDNSServer, char *szDomainName)
{
 char *pWriteDNSPacket = m_szDNSPacket;
 memset(pWriteDNSPacket, 0, DNS_PACKET_MAX_SIZE); 

 //填充DNS查詢報(bào)文頭部
 DNSHeader *pDNSHeader = (DNSHeader*)pWriteDNSPacket;
 pDNSHeader->usTransID = m_usCurrentProcID;
 pDNSHeader->usFlags = htons(0x0100);
 pDNSHeader->usQuestionCount = htons(0x0001);
 pDNSHeader->usAnswerCount = 0x0000;
 pDNSHeader->usAuthorityCount = 0x0000;
 pDNSHeader->usAdditionalCount = 0x0000;

 //設(shè)置DNS查詢報(bào)文內(nèi)容
 USHORT usQType = htons(0x0001);
 USHORT usQClass = htons(0x0001);
 USHORT nDomainNameLen = strlen(szDomainName);
 char *szEncodedDomainName = (char *)malloc(nDomainNameLen + 2);
 if (szEncodedDomainName == NULL)
 {
 return FALSE;
 }
 if (!EncodeDotStr(szDomainName, szEncodedDomainName, nDomainNameLen + 2))
 {
 return FALSE;
 }

 //填充DNS查詢報(bào)文內(nèi)容
 USHORT nEncodedDomainNameLen = strlen(szEncodedDomainName) + 1;
 memcpy(pWriteDNSPacket += sizeof(DNSHeader), szEncodedDomainName, nEncodedDomainNameLen);
 memcpy(pWriteDNSPacket += nEncodedDomainNameLen, (char*)(&usQType), DNS_TYPE_SIZE);
 memcpy(pWriteDNSPacket += DNS_TYPE_SIZE, (char*)(&usQClass), DNS_CLASS_SIZE);
 free (szEncodedDomainName);

 //發(fā)送DNS查詢報(bào)文
 USHORT nDNSPacketSize = sizeof(DNSHeader) + nEncodedDomainNameLen + DNS_TYPE_SIZE + DNS_CLASS_SIZE;
 if (sendto(m_sock, m_szDNSPacket, nDNSPacketSize, 0, (sockaddr*)&sockAddrDNSServer, sizeof(sockAddrDNSServer)) == SOCKET_ERROR)
 {
 return FALSE;
 }

 return TRUE;
}

BOOL CDNSLookup::RecvDNSResponse(sockaddr_in sockAddrDNSServer, ULONG ulTimeout, std::vector<ULONG> *pveculIPList, std::vector<std::string> *pvecstrCNameList, ULONG *pulTimeSpent)
{
 ULONG ulSendTimestamp = GetTickCountCalibrate();

 if (pveculIPList != NULL)
 {
 pveculIPList->clear();
 }
 if (pvecstrCNameList != NULL)
 {
 pvecstrCNameList->clear();
 }

 char recvbuf[1024] = {'\0'};
 char szDotName[128] = {'\0'};
 USHORT nEncodedNameLen = 0;

 while (TRUE)
 {
 if (WSAWaitForMultipleEvents(1, &m_event, FALSE, 100, FALSE) != WSA_WAIT_TIMEOUT)
 {
  WSANETWORKEVENTS netEvent;
  WSAEnumNetworkEvents(m_sock, m_event, &netEvent);

  if (netEvent.lNetworkEvents & FD_READ)
  {
  ULONG ulRecvTimestamp = GetTickCountCalibrate();
  int nSockaddrDestSize = sizeof(sockAddrDNSServer);

  //接收響應(yīng)報(bào)文
  if (recvfrom(m_sock, recvbuf, 1024, 0, (struct sockaddr*)&sockAddrDNSServer, &nSockaddrDestSize) != SOCKET_ERROR)
  {
   DNSHeader *pDNSHeader = (DNSHeader*)recvbuf;
   USHORT usQuestionCount = 0;
   USHORT usAnswerCount = 0;

   if (pDNSHeader->usTransID == m_usCurrentProcID
   && (ntohs(pDNSHeader->usFlags) & 0xfb7f) == 0x8100 //RFC1035 4.1.1(Header section format)
   && (usQuestionCount = ntohs(pDNSHeader->usQuestionCount)) >= 0
   && (usAnswerCount = ntohs(pDNSHeader->usAnswerCount)) > 0)
   {
   char *pDNSData = recvbuf + sizeof(DNSHeader);

   //解析Question字段
   for (int q = 0; q != usQuestionCount; ++q)
   {
    if (!DecodeDotStr(pDNSData, &nEncodedNameLen, szDotName, sizeof(szDotName)))
    {
    return FALSE;
    }
    pDNSData += (nEncodedNameLen + DNS_TYPE_SIZE + DNS_CLASS_SIZE);
   }

   //解析Answer字段
   for (int a = 0; a != usAnswerCount; ++a)
   {
    if (!DecodeDotStr(pDNSData, &nEncodedNameLen, szDotName, sizeof(szDotName), recvbuf))
    {
    return FALSE;
    }
    pDNSData += nEncodedNameLen;

    USHORT usAnswerType = ntohs(*(USHORT*)(pDNSData));
    USHORT usAnswerClass = ntohs(*(USHORT*)(pDNSData + DNS_TYPE_SIZE));
    ULONG usAnswerTTL = ntohl(*(ULONG*)(pDNSData + DNS_TYPE_SIZE + DNS_CLASS_SIZE));
    USHORT usAnswerDataLen = ntohs(*(USHORT*)(pDNSData + DNS_TYPE_SIZE + DNS_CLASS_SIZE + DNS_TTL_SIZE));
    pDNSData += (DNS_TYPE_SIZE + DNS_CLASS_SIZE + DNS_TTL_SIZE + DNS_DATALEN_SIZE);

    if (usAnswerType == DNS_TYPE_A && pveculIPList != NULL)
    {
    ULONG ulIP = *(ULONG*)(pDNSData);
    pveculIPList->push_back(ulIP);
    }
    else if (usAnswerType == DNS_TYPE_CNAME && pvecstrCNameList != NULL)
    {
    if (!DecodeDotStr(pDNSData, &nEncodedNameLen, szDotName, sizeof(szDotName), recvbuf))
    {
     return FALSE;
    }
    pvecstrCNameList->push_back(szDotName);
    }

    pDNSData += (usAnswerDataLen);
   }

   //計(jì)算DNS查詢所用時(shí)間
   if (pulTimeSpent != NULL)
   {
    *pulTimeSpent = ulRecvTimestamp - ulSendTimestamp;
   }

   break;
   }
  }
  }
 }

 //超時(shí)退出
 if (GetTickCountCalibrate() - ulSendTimestamp > ulTimeout)
 {
  *pulTimeSpent = ulTimeout + 1;
  return FALSE;
 }
 }

 return TRUE;
}

/*
 * convert "www.baidu.com" to "\x03www\x05baidu\x03com"
 * 0x0000 03 77 77 77 05 62 61 69 64 75 03 63 6f 6d 00 ff
 */
BOOL CDNSLookup::EncodeDotStr(char *szDotStr, char *szEncodedStr, USHORT nEncodedStrSize)
{
 USHORT nDotStrLen = strlen(szDotStr);

 if (szDotStr == NULL || szEncodedStr == NULL || nEncodedStrSize < nDotStrLen + 2)
 {
 return FALSE;
 }

 char *szDotStrCopy = new char[nDotStrLen + 1];
 //strcpy_s(szDotStrCopy, nDotStrLen + 1, szDotStr);
 strcpy(szDotStrCopy, szDotStr);

 char *pNextToken = NULL;
 //char *pLabel = strtok_s(szDotStrCopy, ".", &pNextToken);
 char *pLabel = strtok(szDotStrCopy, ".");
 USHORT nLabelLen = 0;
 USHORT nEncodedStrLen = 0;
 while (pLabel != NULL)
 {
 if ((nLabelLen = strlen(pLabel)) != 0)
 {
  //sprintf_s(szEncodedStr + nEncodedStrLen, nEncodedStrSize - nEncodedStrLen, "%c%s", nLabelLen, pLabel);
  sprintf(szEncodedStr + nEncodedStrLen, "%c%s", nLabelLen, pLabel);
  nEncodedStrLen += (nLabelLen + 1);
 }
 //pLabel = strtok_s(NULL, ".", &pNextToken);
 pLabel = strtok(NULL, ".");
 }

 delete [] szDotStrCopy;
 
 return TRUE;
}

/*
 * convert "\x03www\x05baidu\x03com\x00" to "www.baidu.com"
 * 0x0000 03 77 77 77 05 62 61 69 64 75 03 63 6f 6d 00 ff
 * convert "\x03www\x05baidu\xc0\x13" to "www.baidu.com"
 * 0x0000 03 77 77 77 05 62 61 69 64 75 c0 13 ff ff ff ff
 * 0x0010 ff ff ff 03 63 6f 6d 00 ff ff ff ff ff ff ff ff
 */
BOOL CDNSLookup::DecodeDotStr(char *szEncodedStr, USHORT *pusEncodedStrLen, char *szDotStr, USHORT nDotStrSize, char *szPacketStartPos)
{
 if (szEncodedStr == NULL || pusEncodedStrLen == NULL || szDotStr == NULL)
 {
 return FALSE;
 }

 char *pDecodePos = szEncodedStr;
 USHORT usPlainStrLen = 0;
 BYTE nLabelDataLen = 0; 
 *pusEncodedStrLen = 0;

 while ((nLabelDataLen = *pDecodePos) != 0x00)
 {
 if ((nLabelDataLen & 0xc0) == 0) //普通格式,LabelDataLen + Label
 {
  if (usPlainStrLen + nLabelDataLen + 1 > nDotStrSize)
  {
  return FALSE;
  }
  memcpy(szDotStr + usPlainStrLen, pDecodePos + 1, nLabelDataLen);
  memcpy(szDotStr + usPlainStrLen + nLabelDataLen, ".", 1);
  pDecodePos += (nLabelDataLen + 1);
  usPlainStrLen += (nLabelDataLen + 1);
  *pusEncodedStrLen += (nLabelDataLen + 1);
 }
 else //消息壓縮格式,11000000 00000000,兩個(gè)字節(jié),前2位為跳轉(zhuǎn)標(biāo)志,后14位為跳轉(zhuǎn)的偏移
 {
  if (szPacketStartPos == NULL)
  {
  return FALSE;
  }
  USHORT usJumpPos = ntohs(*(USHORT*)(pDecodePos)) & 0x3fff;
  USHORT nEncodeStrLen = 0;
  if (!DecodeDotStr(szPacketStartPos + usJumpPos, &nEncodeStrLen, szDotStr + usPlainStrLen, nDotStrSize - usPlainStrLen, szPacketStartPos))
  {
  return FALSE;
  }
  else
  {
  *pusEncodedStrLen += 2;
  return TRUE;
  }
 }
 }

 szDotStr[usPlainStrLen - 1] = '\0';
 *pusEncodedStrLen += 1;

 return TRUE;
}

ULONG CDNSLookup::GetTickCountCalibrate()
{
 static ULONG s_ulFirstCallTick = 0;
 static LONGLONG s_ullFirstCallTickMS = 0;

 SYSTEMTIME systemtime;
 FILETIME filetime;
 GetLocalTime(&systemtime); 
 SystemTimeToFileTime(&systemtime, &filetime);
 LARGE_INTEGER liCurrentTime;
 liCurrentTime.HighPart = filetime.dwHighDateTime;
 liCurrentTime.LowPart = filetime.dwLowDateTime;
 LONGLONG llCurrentTimeMS = liCurrentTime.QuadPart / 10000;

 if (s_ulFirstCallTick == 0)
 {
 s_ulFirstCallTick = GetTickCount();
 }
 if (s_ullFirstCallTickMS == 0)
 {
 s_ullFirstCallTickMS = llCurrentTimeMS;
 }

 return s_ulFirstCallTick + (ULONG)(llCurrentTimeMS - s_ullFirstCallTickMS);
}
 [DNSLookup.cpp]

 

#include <stdio.h>
#include <windows.h>
#include "DNSLookup.h"

int main(void)
{
 char szDomainName[] = "www.baidu.com";
 std::vector<ULONG> veculIPList;
 std::vector<std::string> vecstrIPList;
 std::vector<std::string> vecCNameList;
 ULONG ulTimeSpent = 0;
 CDNSLookup dnslookup;
 BOOL bRet = dnslookup.DNSLookup(inet_addr("114.114.114.114"), szDomainName, &vecstrIPList, &vecCNameList, 1000, &ulTimeSpent);

 printf("DNSLookup result (%s):\n", szDomainName);
 if (!bRet)
 {
 printf("timeout!\n");
 return -1;
 }

 for (int i = 0; i != veculIPList.size(); ++i)
 {
 printf("IP%d(ULONG) = %u\n", i + 1, veculIPList[i]);
 }
 for (int i = 0; i != vecstrIPList.size(); ++i)
 {
 printf("IP%d(string) = %s\n", i + 1, vecstrIPList[i].c_str());
 }
 for (int i = 0; i != vecCNameList.size(); ++i)
 {
 printf("CName%d = %s\n", i + 1, vecCNameList[i].c_str());
 }
 printf("time spent = %ums\n", ulTimeSpent);
 
 return 0;
}

以上就是C++實(shí)現(xiàn)DNS域名解析的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論