DNSMap.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "StdAfx.h"
  2. #include "DNSMap.h"
  3. int CDNSMap::m_nErrCodeBase = 0; //错误码起始值
  4. CDNSMap::CDNSMap(void)
  5. {
  6. }
  7. CDNSMap::~CDNSMap(void)
  8. {
  9. }
  10. /******************************************************************************
  11. 设置错误码起始值
  12. ******************************************************************************/
  13. int CDNSMap::SetErrCodeBase(int nErrCodeBase)
  14. {
  15. //是否已经设置了起始值
  16. if (m_nErrCodeBase > 0)
  17. {
  18. return nErrCodeBase;
  19. }
  20. //设置本类的起始值
  21. m_nErrCodeBase = nErrCodeBase;
  22. nErrCodeBase += 1000;
  23. //返回结束值
  24. return nErrCodeBase;
  25. }
  26. /******************************************************************************
  27. 获取域名的IP地址
  28. ******************************************************************************/
  29. int CDNSMap::GetIPStr(string & strIP, string strDomain)
  30. {
  31. try
  32. {
  33. return _GetIPStr(strIP, strDomain);
  34. }
  35. catch (...)
  36. {
  37. return m_nErrCodeBase + DNSMap::GET_IP_STR_EXCEPTION;
  38. }
  39. }
  40. /******************************************************************************
  41. 获取域名的IP数据
  42. ******************************************************************************/
  43. int CDNSMap::GetIPData(ULONG & ulIPData, string strDomain)
  44. {
  45. try
  46. {
  47. return _GetIPData(ulIPData, strDomain);
  48. }
  49. catch (...)
  50. {
  51. return m_nErrCodeBase + DNSMap::GET_IP_DATA_EXCEPTION;
  52. }
  53. }
  54. /******************************************************************************
  55. 清除DNS缓存
  56. ******************************************************************************/
  57. int CDNSMap::ClearDNS(void)
  58. {
  59. try
  60. {
  61. return _ClearDNS();
  62. }
  63. catch (...)
  64. {
  65. return m_nErrCodeBase + DNSMap::CLEAR_DNS_EXCEPTION;
  66. }
  67. }
  68. /******************************************************************************
  69. 获取域名的IP地址
  70. ******************************************************************************/
  71. int CDNSMap::_GetIPStr(string & strIP, string strDomain)
  72. {
  73. //获取IP数据
  74. if (strDomain == "")
  75. {
  76. return m_nErrCodeBase + DNSMap::GET_IP_STR_DOMAIN_NULL;
  77. }
  78. ULONG ulIPData = 0;
  79. int nGetIPData = _GetIPData(ulIPData, strDomain);
  80. if (nGetIPData != 0)
  81. {
  82. return nGetIPData;
  83. }
  84. if (ulIPData == 0)
  85. {
  86. return m_nErrCodeBase + DNSMap::GET_IP_STR_DATA_ZERO;
  87. }
  88. //转换成IP字符串
  89. in_addr addr;
  90. addr.s_addr = ulIPData;
  91. char * pchIP = inet_ntoa(addr);
  92. if (pchIP == NULL)
  93. {
  94. return m_nErrCodeBase + DNSMap::GET_IP_STR_TO_STR_FAIL;
  95. }
  96. strIP = pchIP;
  97. if (strIP == "")
  98. {
  99. return m_nErrCodeBase + DNSMap::GET_IP_STR_IP_NULL;
  100. }
  101. return 0;
  102. }
  103. /******************************************************************************
  104. 获取域名的IP数据
  105. ******************************************************************************/
  106. int CDNSMap::_GetIPData(ULONG & ulIPData, string strDomain)
  107. {
  108. //先看DNS缓存表中有没有
  109. if (strDomain == "")
  110. {
  111. return m_nErrCodeBase + DNSMap::GET_IP_DATA_DOMAIN_NULL;
  112. }
  113. if (!m_lockDNS.Lock())
  114. {
  115. return m_nErrCodeBase + DNSMap::GET_IP_DATA_DNS_LOCK_FAIL;
  116. }
  117. ulIPData = m_mapDNS[strDomain];
  118. if (!m_lockDNS.Unlock())
  119. {
  120. return m_nErrCodeBase + DNSMap::GET_IP_DATA_DNS_UNLOCK_FAIL;
  121. }
  122. //DNS缓存中没有
  123. if (ulIPData == 0)
  124. {
  125. //解析域名
  126. int nParseDomain = _ParseDomain(ulIPData, strDomain);
  127. if (nParseDomain != 0)
  128. {
  129. return nParseDomain;
  130. }
  131. if (ulIPData == 0)
  132. {
  133. return m_nErrCodeBase + DNSMap::GET_IP_DATA_DATA_ZERO;
  134. }
  135. }
  136. return 0;
  137. }
  138. /******************************************************************************
  139. 清除DNS缓存
  140. ******************************************************************************/
  141. int CDNSMap::_ClearDNS(void)
  142. {
  143. if (!m_lockDNS.Lock())
  144. {
  145. return m_nErrCodeBase + DNSMap::CLEAR_DNS_DNS_LOCK_FAIL;
  146. }
  147. m_mapDNS.clear();
  148. if (!m_lockDNS.Unlock())
  149. {
  150. return m_nErrCodeBase + DNSMap::CLEAR_DNS_DNS_UNLOCK_FAIL;
  151. }
  152. return 0;
  153. }
  154. /******************************************************************************
  155. 解析域名
  156. ******************************************************************************/
  157. int CDNSMap::_ParseDomain(ULONG & ulIPData, string strDomain)
  158. {
  159. if (strDomain == "")
  160. {
  161. return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_DOMAIN_NULL;
  162. }
  163. //获取IP信息
  164. hostent * pHostent = gethostbyname(strDomain.c_str());
  165. if (pHostent == NULL
  166. || pHostent->h_addr_list[0] == NULL)
  167. {
  168. return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_GET_FAIL;
  169. }
  170. //保存到DNS缓存表中
  171. ulIPData = *(ULONG *)pHostent->h_addr_list[0];
  172. if (ulIPData == 0)
  173. {
  174. return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_GET_ZERO;
  175. }
  176. if (!m_lockDNS.Lock())
  177. {
  178. return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_DNS_LOCK_FAIL;
  179. }
  180. m_mapDNS[strDomain] = ulIPData;
  181. if (!m_lockDNS.Unlock())
  182. {
  183. return m_nErrCodeBase + DNSMap::PARSE_DOMAIN_DNS_UNLOCK_FAIL;
  184. }
  185. return 0;
  186. }