123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #include "StdAfx.h"
- #include "DNSMap.h"
- int CDNSMap::m_nErrCodeBase = 0; //错误码起始值
- CDNSMap::CDNSMap(void)
- {
- }
- CDNSMap::~CDNSMap(void)
- {
- }
- /******************************************************************************
- 设置错误码起始值
- ******************************************************************************/
- int CDNSMap::SetErrCodeBase(int nErrCodeBase)
- {
- //是否已经设置了起始值
- if (m_nErrCodeBase > 0)
- {
- return nErrCodeBase;
- }
- //设置本类的起始值
- m_nErrCodeBase = nErrCodeBase;
- nErrCodeBase += 1000;
- //返回结束值
- return nErrCodeBase;
- }
- /******************************************************************************
- 获取域名的IP地址
- ******************************************************************************/
- int CDNSMap::GetIPStr(string & strIP, string strDomain)
- {
- try
- {
- return _GetIPStr(strIP, strDomain);
- }
- catch (...)
- {
- return m_nErrCodeBase + DNSMap::GET_IP_STR_EXCEPTION;
- }
- }
- /******************************************************************************
- 获取域名的IP数据
- ******************************************************************************/
- int CDNSMap::GetIPData(ULONG & ulIPData, string strDomain)
- {
- try
- {
- return _GetIPData(ulIPData, strDomain);
- }
- catch (...)
- {
- return m_nErrCodeBase + DNSMap::GET_IP_DATA_EXCEPTION;
- }
- }
- /******************************************************************************
- 清除DNS缓存
- ******************************************************************************/
- int CDNSMap::ClearDNS(void)
- {
- try
- {
- return _ClearDNS();
- }
- catch (...)
- {
- return m_nErrCodeBase + DNSMap::CLEAR_DNS_EXCEPTION;
- }
- }
- /******************************************************************************
- 获取域名的IP地址
- ******************************************************************************/
- int CDNSMap::_GetIPStr(string & strIP, string strDomain)
- {
- //获取IP数据
- if (strDomain == "")
- {
- return m_nErrCodeBase + DNSMap::GET_IP_STR_DOMAIN_NULL;
- }
- ULONG ulIPData = 0;
- int nGetIPData = _GetIPData(ulIPData, strDomain);
- if (nGetIPData != 0)
- {
- return nGetIPData;
- }
- if (ulIPData == 0)
- {
- return m_nErrCodeBase + DNSMap::GET_IP_STR_DATA_ZERO;
- }
- //转换成IP字符串
- in_addr addr;
- addr.s_addr = ulIPData;
- char * pchIP = inet_ntoa(addr);
- if (pchIP == NULL)
- {
- return m_nErrCodeBase + DNSMap::GET_IP_STR_TO_STR_FAIL;
- }
- strIP = pchIP;
- if (strIP == "")
- {
- return m_nErrCodeBase + DNSMap::GET_IP_STR_IP_NULL;
- }
- return 0;
- }
- /******************************************************************************
- 获取域名的IP数据
- ******************************************************************************/
- int CDNSMap::_GetIPData(ULONG & ulIPData, string strDomain)
- {
- //先看DNS缓存表中有没有
- if (strDomain == "")
- {
- return m_nErrCodeBase + DNSMap::GET_IP_DATA_DOMAIN_NULL;
- }
- if (!m_lockDNS.Lock())
- {
- return m_nErrCodeBase + DNSMap::GET_IP_DATA_DNS_LOCK_FAIL;
- }
- ulIPData = m_mapDNS[strDomain];
- if (!m_lockDNS.Unlock())
- {
- return m_nErrCodeBase + DNSMap::GET_IP_DATA_DNS_UNLOCK_FAIL;
- }
- //DNS缓存中没有
- if (ulIPData == 0)
- {
- //解析域名
- int nParseDomain = _ParseDomain(ulIPData, strDomain);
- if (nParseDomain != 0)
- {
- return nParseDomain;
- }
- if (ulIPData == 0)
- {
- return m_nErrCodeBase + DNSMap::GET_IP_DATA_DATA_ZERO;
- }
- }
- return 0;
- }
- /******************************************************************************
- 清除DNS缓存
- ******************************************************************************/
- int CDNSMap::_ClearDNS(void)
- {
- if (!m_lockDNS.Lock())
- {
- return m_nErrCodeBase + DNSMap::CLEAR_DNS_DNS_LOCK_FAIL;
- }
- m_mapDNS.clear();
- if (!m_lockDNS.Unlock())
- {
- return m_nErrCodeBase + DNSMap::CLEAR_DNS_DNS_UNLOCK_FAIL;
- }
- return 0;
- }
- /******************************************************************************
- 解析域名
- ******************************************************************************/
- int CDNSMap::_ParseDomain(ULONG & ulIPData, string strDomain)
- {
- if (strDomain == "")
- {
- return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_DOMAIN_NULL;
- }
- //获取IP信息
- hostent * pHostent = gethostbyname(strDomain.c_str());
- if (pHostent == NULL
- || pHostent->h_addr_list[0] == NULL)
- {
- return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_GET_FAIL;
- }
- //保存到DNS缓存表中
- ulIPData = *(ULONG *)pHostent->h_addr_list[0];
- if (ulIPData == 0)
- {
- return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_GET_ZERO;
- }
- if (!m_lockDNS.Lock())
- {
- return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_DNS_LOCK_FAIL;
- }
- m_mapDNS[strDomain] = ulIPData;
- if (!m_lockDNS.Unlock())
- {
- return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_DNS_UNLOCK_FAIL;
- }
- return 0;
- }
|