本例主要提供使用WIN32 API实现的DNS域名解析的工具。
直接上代码:
.h
#pragma once
#include <vector>
#include "Ws2tcpip.h"
#include "Wspiapi.h"
#include "mmsystem.h"
#include <windows.h>
class CDNS
{
public:
CDNS(void);
~CDNS(void);
int GetDnsRandom(char * cDomainName,char * cIpAddress,size_t IpSize);
int GetDnsFirstIp(char * cDomainName,char * cIpAddress,size_t IpSize);
int GetDnsIPList(char * cDomainName,std::vector<DWORD>& ipList);
};
.cpp
//#include "StdAfx.h"
#include "CDNS.h"
CDNS::CDNS(void)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
}
CDNS::~CDNS(void)
{
WSACleanup();
}
// This is an example of an exported function.
int CDNS::GetDnsRandom(char * cDomainName,char * cIpAddress,size_t IpSize)
{
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_protocol=IPPROTO_IP ;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_CANONNAME ;
if (getaddrinfo(cDomainName, NULL, &hints, &res)==0)
{
int totalresult=1;
addrinfo *addritem=res;
while(addritem->ai_next!=0)
{
totalresult++;
addritem=addritem->ai_next;
}
srand( (unsigned)timeGetTime( ) );
int selectedaddr=(int)(((double)rand()) / (RAND_MAX + 1) * (totalresult - 0));
addritem=res;
for (int i=0;i<selectedaddr;i++)
{
addritem=addritem->ai_next;
}
memset(cIpAddress,0,IpSize);
if(getnameinfo(addritem->ai_addr,addritem-> ai_addrlen,cIpAddress,IpSize,NULL,0,NI_NUMERICHOST)==0)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
return 1;
}
int CDNS::GetDnsIPList(char * cDomainName,std::vector<DWORD>& ipList)
{
int iRet = 0;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_protocol=IPPROTO_IP ;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_CANONNAME ;
char szIPAddress[32] = {0};
DWORD dwIPAddress;
ipList.clear();
if (getaddrinfo(cDomainName, NULL, &hints, &res)==0)
{
int totalresult=1;
addrinfo *addritem=res;
while(addritem->ai_next!=0)
{
memset(&szIPAddress,0,sizeof(szIPAddress));
if(getnameinfo(addritem->ai_addr,addritem-> ai_addrlen,szIPAddress,sizeof(szIPAddress),NULL,0,NI_NUMERICHOST)==0)
{
dwIPAddress = inet_addr(szIPAddress);
ipList.push_back(dwIPAddress);
iRet = 1;
}
totalresult++;
addritem=addritem->ai_next;
}
memset(&szIPAddress,0,sizeof(szIPAddress));
if(getnameinfo(addritem->ai_addr,addritem-> ai_addrlen,szIPAddress,sizeof(szIPAddress),NULL,0,NI_NUMERICHOST)==0)
{
dwIPAddress = inet_addr(szIPAddress);
ipList.push_back(dwIPAddress);
iRet = 1;
}
}
return iRet;
}
int CDNS::GetDnsFirstIp(char * cDomainName,char * cIpAddress,size_t IpSize)
{
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_protocol=IPPROTO_IP ;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_CANONNAME ;
if (getaddrinfo(cDomainName, NULL, &hints, &res)==0)
{
int totalresult=1;
addrinfo *addritem=res;
while(addritem->ai_next!=0)
{
totalresult++;
addritem=addritem->ai_next;
}
// srand( (unsigned)timeGetTime( ) );
// int selectedaddr=(int)(((double)rand()) / (RAND_MAX + 1) * (totalresult - 0));
//
// addritem=res;
// for (int i=0;i<selectedaddr;i++)
// {
//
// addritem=addritem->ai_next;
//
// }
memset(cIpAddress,0,IpSize);
if(getnameinfo(addritem->ai_addr,addritem-> ai_addrlen,cIpAddress,IpSize,NULL,0,NI_NUMERICHOST)==0)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
return 1;
}