首页 > C/C++ > 使用c-ares 获取域名IP地址
2014
05-04

使用c-ares 获取域名IP地址

本地解析域名 DNS IP,在这里使用c-ares 官方 http://c-ares.haxx.se/

源码如下:
文件 GetDomainIp.h

#include <WTypes.h>
#include <strsafe.h>

int GetDomainIp(char *pdomain, char* ipaddress);

 

文件 GetDomainIp.cpp

#define CARES_STATICLIB
#include "include/config-win32.h"
#include "include/ares.h"
#include "include/ares_dns.h"
#include "include/ares_getopt.h"
#include "include/ares_ipv6.h"
#include "include/ares_nowarn.h"
#include "GetDomainIp.h"

#pragma comment(lib,"ws2_32.lib")

#if _DEBUG
#pragma comment(lib,"lib\\libcaresd.lib")
#else
#pragma comment(lib,"lib\\libcares.lib")
#endif

char ipaddr[46];

static void callback(void *arg, int status, int timeouts, struct hostent *host);

int GetDomainIp(char *pdomain, char* ipaddress)
{
	ares_channel channel;
	int status, nfds, addr_family = AF_INET;
	fd_set read_fds, write_fds;
	struct timeval *tvp, tv;
	struct in_addr addr4;
	struct ares_in6_addr addr6;

	WORD wVersionRequested = MAKEWORD(2,2);
	WSADATA wsaData;
	WSAStartup(wVersionRequested, &wsaData);

	status = ares_library_init(ARES_LIB_INIT_ALL);
	if (status != ARES_SUCCESS)
	{
		//fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
		return 1;
	}

	status = ares_init(&channel);
	if (status != ARES_SUCCESS)
	{
		//fprintf(stderr, "ares_init: %s\n", ares_strerror(status));
		return 1;
	}

	/* Initiate the queries, one per command-line argument. */
	if (ares_inet_pton(AF_INET, pdomain, &addr4) == 1)
	{
		ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback,
			pdomain);
	}
	else if (ares_inet_pton(AF_INET6, pdomain, &addr6) == 1)
	{
		ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback,
			pdomain);
	}
	else
	{
		ares_gethostbyname(channel, pdomain, addr_family, callback, pdomain);
	}

	/* Wait for all queries to complete. */
	for (;;)
	{
		FD_ZERO(&read_fds);
		FD_ZERO(&write_fds);
		nfds = ares_fds(channel, &read_fds, &write_fds);
		if (nfds == 0)
			break;
		tvp = ares_timeout(channel, NULL, &tv);
		select(nfds, &read_fds, &write_fds, NULL, tvp);
		ares_process(channel, &read_fds, &write_fds);
	}

	if (ipaddr != NULL)
	{
		strcpy(ipaddress,ipaddr);
	}
	ares_destroy(channel);
	ares_library_cleanup();
	WSACleanup();

	return 0;
}

static void callback(void *arg, int status, int timeouts, struct hostent *host)
{
	char **p;

	(void)timeouts;

	if (status != ARES_SUCCESS)
	{
		//fprintf(stderr, "%s: %s\n", (char *) arg, ares_strerror(status));
		return;
	}

	for (p = host->h_addr_list; *p; p++)
	{
		char addr_buf[46] = "??";

		ares_inet_ntop(host->h_addrtype, *p, addr_buf, sizeof(addr_buf));
		//复制到全局变量里去
		strcpy(ipaddr,addr_buf);
		//printf("%-32s\t%s", host->h_name, addr_buf);
#if 0
		if (host->h_aliases[0])
		{
			int i;

			printf (", Aliases: ");
			for (i = 0; host->h_aliases[i]; i++)
				printf("%s ", host->h_aliases[i]);
		}
#endif
		//puts("");
	}
}

int main()
{
	char *ipaddr = new char[46];
	char *domain = "www.mydoop.com";
	GetDomainIp(domain,ipaddr);
	printf("This %s Domain ipaddress : %s" ,domain, ipaddr);
}

 

运行结果:

E:\GetDomainIp\Release>GetDomainIp.exe
This www.mydoop.com Domain ipaddress : 103.242.110.41

 

源码下载:
http://pan.baidu.com/s/1jGJXlFw

最后编辑:
作者:dnybz
这个作者貌似有点懒,什么都没有留下。