实现使用C++/WIN32进行DNS域名解析工具

本例主要提供使用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;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值