Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
5.5 KiB

  1. /*++
  2. Copyright (c) 1994-1998 Microsoft Corporation
  3. Module Name :
  4. dnsnamed.cpp
  5. Abstract:
  6. DNS name resolution dialog
  7. Author:
  8. Ronald Meijer (ronaldm)
  9. Project:
  10. Internet Services Manager
  11. Revision History:
  12. --*/
  13. #include "stdafx.h"
  14. #include <winsock2.h>
  15. #include "comprop.h"
  16. #include "dnsnamed.h"
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21. CDnsNameDlg::CDnsNameDlg(
  22. IN CIPAddressCtl * pIpControl OPTIONAL,
  23. IN CWnd * pParent OPTIONAL
  24. )
  25. /*++
  26. Routine Description:
  27. Construct with optional associated IP address control
  28. Arguments:
  29. CWndIpAddress * pIpControl : Associated IP control
  30. CWnd * pParent : Pointer to parent window
  31. Return Value:
  32. N/A
  33. --*/
  34. : m_pIpControl(pIpControl),
  35. m_dwIPValue(0L),
  36. CDialog(CDnsNameDlg::IDD, pParent)
  37. {
  38. #if 0 // Keep class wizard happy
  39. //{{AFX_DATA_INIT(CDnsNameDlg)
  40. //}}AFX_DATA_INIT
  41. #endif // 0
  42. if (m_pIpControl)
  43. {
  44. m_pIpControl->GetAddress(&m_dwIPValue);
  45. }
  46. }
  47. CDnsNameDlg::CDnsNameDlg(
  48. IN DWORD dwIPValue,
  49. IN CWnd * pParent OPTIONAL
  50. )
  51. /*++
  52. Routine Description:
  53. Construct with associated IP value
  54. Arguments:
  55. DWORD dwIPValue : IP Value
  56. CWnd * pParent : Pointer to parent window
  57. Return Value:
  58. N/A
  59. --*/
  60. : m_pIpControl(NULL),
  61. m_dwIPValue(dwIPValue),
  62. CDialog(CDnsNameDlg::IDD, pParent)
  63. {
  64. }
  65. void
  66. CDnsNameDlg::DoDataExchange(
  67. IN CDataExchange * pDX
  68. )
  69. /*++
  70. Routine Description:
  71. Initialise/Store control data
  72. Arguments:
  73. CDataExchange * pDX - DDX/DDV control structure
  74. Return Value:
  75. None
  76. --*/
  77. {
  78. CDialog::DoDataExchange(pDX);
  79. //{{AFX_DATA_MAP(CDnsNameDlg)
  80. DDX_Control(pDX, IDC_EDIT_DNS_NAME, m_edit_DNSName);
  81. DDX_Control(pDX, IDOK, m_button_OK);
  82. //}}AFX_DATA_MAP
  83. }
  84. //
  85. // Message Map
  86. //
  87. BEGIN_MESSAGE_MAP(CDnsNameDlg, CDialog)
  88. //{{AFX_MSG_MAP(CDnsNameDlg)
  89. ON_EN_CHANGE(IDC_EDIT_DNS_NAME, OnChangeEditDnsName)
  90. //}}AFX_MSG_MAP
  91. END_MESSAGE_MAP()
  92. DWORD
  93. CDnsNameDlg::FillIpControlFromName()
  94. /*++
  95. Routine Description:
  96. Do a DNS lookup from the hostname in the edit control, and place
  97. the ip value in the ip control if we have one.
  98. Arguments:
  99. None
  100. Return Value:
  101. Error return code
  102. --*/
  103. {
  104. CString str;
  105. DWORD err = 0;
  106. HOSTENT * pHostent = NULL;
  107. m_edit_DNSName.GetWindowText(str);
  108. BeginWaitCursor();
  109. #ifdef _UNICODE
  110. CHAR * pAnsi = AllocAnsiString(str);
  111. if (pAnsi == NULL)
  112. {
  113. return ERROR_NOT_ENOUGH_MEMORY;
  114. }
  115. pHostent = ::gethostbyname(pAnsi);
  116. #else
  117. pHostent = ::gethostbyname((LPCTSTR)str);
  118. #endif // _UNICODE;
  119. if (pHostent != NULL)
  120. {
  121. //
  122. // Got a valid lookup. Convert the value to host order,
  123. // optionally set the value in the associated ip control
  124. //
  125. m_dwIPValue = ::ntohl(*((u_long *)pHostent->h_addr_list[0]));
  126. if (m_pIpControl)
  127. {
  128. m_pIpControl->SetAddress(m_dwIPValue);
  129. }
  130. }
  131. else
  132. {
  133. err = ::WSAGetLastError();
  134. }
  135. EndWaitCursor();
  136. #ifdef _UNICODE
  137. FreeMem(pAnsi);
  138. #endif // _UNICODE
  139. return err;
  140. }
  141. DWORD
  142. CDnsNameDlg::FillNameFromIpValue()
  143. /*++
  144. Routine Description:
  145. Given the ip value, fill, do a reverse lookup, and fill the name in
  146. the edit control.
  147. Arguments:
  148. None
  149. Return Value:
  150. Error return code
  151. --*/
  152. {
  153. DWORD err = ERROR_SUCCESS;
  154. if (m_dwIPValue == 0L)
  155. {
  156. //
  157. // Don't bother filling this
  158. // one in -- not an error, though
  159. //
  160. return err;
  161. }
  162. //
  163. // Call the Winsock API to get host name and alias information.
  164. //
  165. u_long ulAddrInNetOrder = ::htonl((u_long)m_dwIPValue);
  166. BeginWaitCursor();
  167. HOSTENT * pHostInfo = ::gethostbyaddr(
  168. (CHAR *)&ulAddrInNetOrder,
  169. sizeof ulAddrInNetOrder,
  170. PF_INET
  171. );
  172. EndWaitCursor();
  173. if (pHostInfo == NULL)
  174. {
  175. return ::WSAGetLastError();
  176. }
  177. try
  178. {
  179. CString str(pHostInfo->h_name);
  180. m_edit_DNSName.SetWindowText(str);
  181. }
  182. catch(CException * e)
  183. {
  184. err = ::GetLastError();
  185. e->Delete();
  186. }
  187. return err;
  188. }
  189. //
  190. // Message Handlers
  191. //
  192. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  193. void
  194. CDnsNameDlg::OnOK()
  195. /*++
  196. Routine Description:
  197. Attempt to resolve the the IP address in response to the OK button
  198. being pressed. Don't dismiss the dialog if the name is not
  199. resolvable.
  200. Arguments:
  201. None
  202. Return Value:
  203. None
  204. --*/
  205. {
  206. CError err(FillIpControlFromName());
  207. if (err.MessageBoxOnFailure())
  208. {
  209. //
  210. // Failed, don't dismiss the dialog
  211. //
  212. return;
  213. }
  214. //
  215. // Dismiss the dialog
  216. //
  217. CDialog::OnOK();
  218. }
  219. void
  220. CDnsNameDlg::OnChangeEditDnsName()
  221. /*++
  222. Routine Description:
  223. Enable/disable the ok button depending on the contents of the edit control.
  224. Arguments:
  225. None
  226. Return Value:
  227. None
  228. --*/
  229. {
  230. m_button_OK.EnableWindow(m_edit_DNSName.GetWindowTextLength() > 0);
  231. }
  232. BOOL
  233. CDnsNameDlg::OnInitDialog()
  234. /*++
  235. Routine Description:
  236. WM_INITDIALOG handler. Initialize the dialog.
  237. Arguments:
  238. None.
  239. Return Value:
  240. TRUE if focus is to be set automatically, FALSE if the focus
  241. is already set.
  242. --*/
  243. {
  244. CDialog::OnInitDialog();
  245. //
  246. // If an address has been pre-set do a reverse lookup
  247. //
  248. if (m_dwIPValue)
  249. {
  250. CError err(FillNameFromIpValue());
  251. err.MessageBoxOnFailure();
  252. }
  253. OnChangeEditDnsName();
  254. return TRUE;
  255. }