Leaked source code of windows server 2003
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.

194 lines
4.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // SYNOPSIS
  6. //
  7. // Defines the class Resolver and its subclasses.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include <proxypch.h>
  11. #include <resolver.h>
  12. #include <winsock2.h>
  13. #include <svcguid.h>
  14. #include <iasapi.h>
  15. Resolver::Resolver(UINT dialog, PCWSTR dnsName, CWnd* pParent)
  16. : CHelpDialog(dialog, pParent),
  17. name(dnsName),
  18. choice(name)
  19. {
  20. WSADATA wsaData;
  21. WSAStartup(MAKEWORD(2, 0), &wsaData);
  22. }
  23. Resolver::~Resolver()
  24. {
  25. WSACleanup();
  26. }
  27. BOOL Resolver::IsAddress(PCWSTR sz) const throw ()
  28. {
  29. return FALSE;
  30. }
  31. BOOL Resolver::OnInitDialog()
  32. {
  33. /////////
  34. // Subclass the list control.
  35. /////////
  36. if (!results.SubclassWindow(::GetDlgItem(m_hWnd, IDC_LIST_IPADDRS)))
  37. {
  38. AfxThrowNotSupportedException();
  39. }
  40. /////////
  41. // Set the column header.
  42. /////////
  43. RECT rect;
  44. results.GetClientRect(&rect);
  45. LONG width = rect.right - rect.left;
  46. ResourceString addrsCol(IDS_RESOLVER_COLUMN_ADDRS);
  47. results.InsertColumn(0, addrsCol, LVCFMT_LEFT, width);
  48. results.SetExtendedStyle(results.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
  49. return CHelpDialog::OnInitDialog();
  50. }
  51. void Resolver::DoDataExchange(CDataExchange* pDX)
  52. {
  53. CHelpDialog::DoDataExchange(pDX);
  54. DDX_Text(pDX, IDC_EDIT_NAME, name);
  55. if (pDX->m_bSaveAndValidate)
  56. {
  57. int item = results.GetNextItem(-1, LVNI_SELECTED);
  58. choice = (item >= 0) ? results.GetItemText(item, 0) : name;
  59. }
  60. }
  61. void Resolver::OnResolve()
  62. {
  63. // Remove the existing result set.
  64. results.DeleteAllItems();
  65. // Get the name to resolve.
  66. UpdateData();
  67. if (IsAddress(name))
  68. {
  69. // It's already an address, so no need to resolve.
  70. results.InsertItem(0, name);
  71. }
  72. else
  73. {
  74. // Change the cursor to busy signal since this will block.
  75. BeginWaitCursor();
  76. // Resolve the hostname.
  77. PHOSTENT he = IASGetHostByName(name);
  78. // The blocking part is over, so restore the cursor.
  79. EndWaitCursor();
  80. if (he)
  81. {
  82. // Add the IP addresses to the combo box.
  83. for (ULONG i = 0; he->h_addr_list[i] && i < 8; ++i)
  84. {
  85. PBYTE p = (PBYTE)he->h_addr_list[i];
  86. WCHAR szAddr[16];
  87. wsprintfW(szAddr, L"%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
  88. results.InsertItem(i, szAddr);
  89. }
  90. // Free the results.
  91. LocalFree(he);
  92. }
  93. else
  94. {
  95. OnResolveError();
  96. }
  97. }
  98. // If we have at least one result ...
  99. if (results.GetItemCount() > 0)
  100. {
  101. // Make the OK button the default ...
  102. setButtonStyle(IDOK, BS_DEFPUSHBUTTON, true);
  103. setButtonStyle(IDC_BUTTON_RESOLVE, BS_DEFPUSHBUTTON, false);
  104. // ... and give it the focus.
  105. setFocusControl(IDOK);
  106. }
  107. }
  108. BEGIN_MESSAGE_MAP(Resolver, CHelpDialog)
  109. ON_BN_CLICKED(IDC_BUTTON_RESOLVE, OnResolve)
  110. END_MESSAGE_MAP()
  111. void Resolver::setButtonStyle(int controlId, long flags, bool set)
  112. {
  113. // Get the button handle.
  114. HWND button = ::GetDlgItem(m_hWnd, controlId);
  115. // Retrieve the current style.
  116. long style = ::GetWindowLong(button, GWL_STYLE);
  117. // Update the flags.
  118. if (set)
  119. {
  120. style |= flags;
  121. }
  122. else
  123. {
  124. style &= ~flags;
  125. }
  126. // Set the new style.
  127. ::SendMessage(button, BM_SETSTYLE, LOWORD(style), MAKELPARAM(1,0));
  128. }
  129. void Resolver::setFocusControl(int controlId)
  130. {
  131. ::SetFocus(::GetDlgItem(m_hWnd, controlId));
  132. }
  133. ServerResolver::ServerResolver(PCWSTR dnsName, CWnd* pParent)
  134. : Resolver(IDD_RESOLVE_SERVER_ADDRESS, dnsName, pParent)
  135. {
  136. }
  137. void ServerResolver::OnResolveError()
  138. {
  139. ResourceString text(IDS_SERVER_E_NO_RESOLVE);
  140. ResourceString caption(IDS_SERVER_E_CAPTION);
  141. MessageBox(text, caption, MB_ICONWARNING);
  142. }
  143. ClientResolver::ClientResolver(PCWSTR dnsName, CWnd* pParent)
  144. : Resolver(IDD_RESOLVE_CLIENT_ADDRESS, dnsName, pParent)
  145. {
  146. }
  147. void ClientResolver::OnResolveError()
  148. {
  149. ResourceString text(IDS_CLIENT_E_NO_RESOLVE);
  150. ResourceString caption(IDS_CLIENT_E_CAPTION);
  151. MessageBox(text, caption, MB_ICONWARNING);
  152. }
  153. BOOL ClientResolver::IsAddress(PCWSTR sz) const throw ()
  154. {
  155. ULONG width;
  156. return (sz != 0) && (IASStringToSubNetW(sz, &width) != INADDR_NONE);
  157. }