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.

374 lines
9.3 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. AddRes.cpp
  7. Dialog to add a reservation
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include "scope.h"
  12. #include "addres.h"
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define RADIO_CLIENT_TYPE_BOTH 0
  19. #define RADIO_CLIENT_TYPE_DHCP 1
  20. #define RADIO_CLIENT_TYPE_BOOTP 2
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CAddReservation dialog
  23. CAddReservation::CAddReservation(ITFSNode * pScopeNode,
  24. LARGE_INTEGER liVersion,
  25. CWnd* pParent /*=NULL*/)
  26. : CBaseDialog(CAddReservation::IDD, pParent)
  27. {
  28. //{{AFX_DATA_INIT(CAddReservation)
  29. m_nClientType = -1;
  30. //}}AFX_DATA_INIT
  31. m_spScopeNode.Set(pScopeNode);
  32. m_pScopeObject = GETHANDLER(CDhcpScope, pScopeNode);
  33. m_bChange = FALSE; // We are creating new clients, not changing
  34. m_liVersion = liVersion;
  35. // the default client type is BOTH
  36. m_nClientType = RADIO_CLIENT_TYPE_BOTH;
  37. }
  38. void CAddReservation::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CBaseDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAddReservation)
  42. DDX_Control(pDX, IDC_STATIC_CLIENT_TYPE, m_staticClientType);
  43. DDX_Control(pDX, IDC_EDIT_CLIENT_UID, m_editClientUID);
  44. DDX_Control(pDX, IDC_EDIT_CLIENT_NAME, m_editClientName);
  45. DDX_Control(pDX, IDC_EDIT_CLIENT_COMMENT, m_editClientComment);
  46. DDX_Radio(pDX, IDC_RADIO_TYPE_BOTH, m_nClientType);
  47. //}}AFX_DATA_MAP
  48. DDX_Control(pDX, IDC_IPADDR_RESERVATION_IP, m_ipaAddress);
  49. }
  50. BEGIN_MESSAGE_MAP(CAddReservation, CBaseDialog)
  51. //{{AFX_MSG_MAP(CAddReservation)
  52. //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CAddReservation message handlers
  56. BOOL CAddReservation::OnInitDialog()
  57. {
  58. CBaseDialog::OnInitDialog();
  59. m_editClientName.LimitText( STRING_LENGTH_MAX ) ;
  60. m_editClientComment.LimitText( STRING_LENGTH_MAX ) ;
  61. FillInSubnetId();
  62. if (m_liVersion.QuadPart < DHCP_SP2_VERSION)
  63. {
  64. m_staticClientType.ShowWindow(SW_HIDE);
  65. GetDlgItem(IDC_RADIO_TYPE_DHCP)->ShowWindow(SW_HIDE);
  66. GetDlgItem(IDC_RADIO_TYPE_BOOTP)->ShowWindow(SW_HIDE);
  67. GetDlgItem(IDC_RADIO_TYPE_BOTH)->ShowWindow(SW_HIDE);
  68. }
  69. return TRUE; // return TRUE unless you set the focus to a control
  70. // EXCEPTION: OCX Property Pages should return FALSE
  71. }
  72. void CAddReservation::OnOK()
  73. {
  74. DWORD err = 0;
  75. CDhcpClient dhcpClient;
  76. UpdateData();
  77. if ( m_bChange )
  78. {
  79. err = UpdateClient(&dhcpClient);
  80. }
  81. else
  82. {
  83. err = CreateClient(&dhcpClient);
  84. }
  85. if ( err == ERROR_SUCCESS )
  86. {
  87. //
  88. // the dialog only gets dismissed if we're editing an
  89. // existing client (because we may want to add more than
  90. // one client)
  91. //
  92. if (m_bChange)
  93. {
  94. CBaseDialog::OnOK();
  95. }
  96. else
  97. {
  98. //
  99. // Get ready for the next client to be added.
  100. //
  101. m_editClientUID.SetWindowText(_T(""));
  102. m_editClientName.SetWindowText(_T(""));
  103. m_editClientComment.SetWindowText(_T(""));
  104. FillInSubnetId();
  105. //
  106. // And continue on...
  107. //
  108. }
  109. }
  110. else
  111. {
  112. // don't put up another error box for this case,
  113. // we already asked the user
  114. if (err != IDS_UID_MAY_BE_WRONG)
  115. {
  116. ::DhcpMessageBox(err);
  117. }
  118. return;
  119. }
  120. //CBaseDialog::OnOK();
  121. }
  122. //
  123. // For new clients, fill in what we can on the ip address control (i.e.
  124. // the subnet id portion
  125. //
  126. void
  127. CAddReservation::FillInSubnetId()
  128. {
  129. DWORD dwIp = m_pScopeObject->GetAddress() & m_pScopeObject->QuerySubnetMask();
  130. m_ipaAddress.ClearAddress();
  131. int i = 0;
  132. while (i < sizeof(dwIp))
  133. {
  134. if (!dwIp)
  135. {
  136. break;
  137. }
  138. m_ipaAddress.SetField(i, TRUE, HIBYTE(HIWORD(dwIp)));
  139. dwIp <<= 8;
  140. ++i;
  141. }
  142. if (i < sizeof(dwIp))
  143. {
  144. m_ipaAddress.SetFocusField(i);
  145. }
  146. }
  147. //
  148. // Construct the client structure from the dialog's edit controls.
  149. //
  150. LONG
  151. CAddReservation::BuildClient
  152. (
  153. CDhcpClient * pClient
  154. )
  155. {
  156. DWORD err = 0;
  157. CString str;
  158. DATE_TIME dt;
  159. DHCP_IP_ADDRESS dhipa;
  160. CByteArray cabUid;
  161. int i;
  162. BOOL fValidUID = TRUE;
  163. CATCH_MEM_EXCEPTION
  164. {
  165. do
  166. {
  167. dt.dwLowDateTime = DHCP_DATE_TIME_ZERO_LOW;
  168. dt.dwHighDateTime = DHCP_DATE_TIME_ZERO_HIGH;
  169. pClient->SetExpiryDateTime( dt );
  170. m_ipaAddress.GetAddress( &dhipa );
  171. if ( dhipa == 0 )
  172. {
  173. err = IDS_ERR_INVALID_CLIENT_IPADDR ;
  174. m_ipaAddress.SetFocusField(-1);
  175. break ;
  176. }
  177. m_editClientUID.GetWindowText(str);
  178. if (str.IsEmpty())
  179. {
  180. err = IDS_ERR_INVALID_UID ;
  181. m_editClientUID.SetSel(0,-1);
  182. m_editClientUID.SetFocus();
  183. break ;
  184. }
  185. //
  186. // Since the rest of the Windows UI displays MAC addresses as
  187. // 00-00-00-00-00-00, we must strip out the dashes before
  188. // processing the mac address
  189. //
  190. int nLength = str.GetLength();
  191. LPTSTR pstrSource = str.GetBuffer(nLength);
  192. LPTSTR pstrDest = pstrSource;
  193. LPTSTR pstrEnd = pstrSource + nLength;
  194. while (pstrSource < pstrEnd)
  195. {
  196. if (*pstrSource != '-')
  197. {
  198. *pstrDest = *pstrSource;
  199. pstrDest = _tcsinc(pstrDest);
  200. }
  201. pstrSource = _tcsinc(pstrSource);
  202. }
  203. *pstrDest = '\0';
  204. str.ReleaseBuffer();
  205. //
  206. // Client UIDs should be 48 bits (6 bytes or 12 hex characters)
  207. //
  208. if (str.GetLength() != 6 * 2)
  209. fValidUID = FALSE;
  210. for (i = 0; i < str.GetLength(); i++)
  211. {
  212. if (!wcschr(rgchHex, str[i]))
  213. fValidUID = FALSE;
  214. }
  215. if (!::UtilCvtHexString(str, cabUid) && fValidUID)
  216. {
  217. err = IDS_ERR_INVALID_UID ;
  218. m_editClientUID.SetSel(0,-1);
  219. m_editClientUID.SetFocus();
  220. break ;
  221. }
  222. // UIDs must be <= 255 bytes
  223. if (cabUid.GetSize() > 255)
  224. {
  225. err = IDS_UID_TOO_LONG;
  226. break;
  227. }
  228. if (!fValidUID)
  229. {
  230. if (IDYES != AfxMessageBox(IDS_UID_MAY_BE_WRONG, MB_ICONQUESTION | MB_YESNO))
  231. {
  232. m_editClientUID.SetSel(0,-1);
  233. m_editClientUID.SetFocus();
  234. err = IDS_UID_MAY_BE_WRONG;
  235. break;
  236. }
  237. }
  238. pClient->SetHardwareAddress( cabUid ) ;
  239. m_editClientName.GetWindowText( str ) ;
  240. if ( str.GetLength() == 0 )
  241. {
  242. err = IDS_ERR_INVALID_CLIENT_NAME ;
  243. m_editClientName.SetFocus();
  244. break ;
  245. }
  246. //
  247. // Convert client name to oem
  248. //
  249. pClient->SetName( str ) ;
  250. m_editClientComment.GetWindowText( str ) ;
  251. pClient->SetComment( str ) ;
  252. //
  253. // Can't change IP address in change mode
  254. //
  255. ASSERT ( !m_bChange || dhipa == pClient->QueryIpAddress() ) ;
  256. pClient->SetIpAddress( dhipa ) ;
  257. //
  258. // Set the client type
  259. //
  260. if (m_liVersion.QuadPart >= DHCP_SP2_VERSION)
  261. {
  262. switch (m_nClientType)
  263. {
  264. case RADIO_CLIENT_TYPE_DHCP:
  265. pClient->SetClientType(CLIENT_TYPE_DHCP);
  266. break;
  267. case RADIO_CLIENT_TYPE_BOOTP:
  268. pClient->SetClientType(CLIENT_TYPE_BOOTP);
  269. break;
  270. case RADIO_CLIENT_TYPE_BOTH:
  271. pClient->SetClientType(CLIENT_TYPE_BOTH);
  272. break;
  273. default:
  274. Assert(FALSE); // should never get here
  275. break;
  276. }
  277. }
  278. }
  279. while ( FALSE ) ;
  280. }
  281. END_MEM_EXCEPTION( err ) ;
  282. return err ;
  283. }
  284. //
  285. // Creates a new reservation for this scope
  286. //
  287. LONG
  288. CAddReservation::CreateClient
  289. (
  290. CDhcpClient * pClient
  291. )
  292. {
  293. LONG err = BuildClient(pClient);
  294. if ( err == 0 )
  295. {
  296. BEGIN_WAIT_CURSOR;
  297. err = m_pScopeObject->CreateReservation(pClient);
  298. END_WAIT_CURSOR;
  299. }
  300. return err ;
  301. }
  302. LONG
  303. CAddReservation::UpdateClient
  304. (
  305. CDhcpClient * pClient
  306. )
  307. {
  308. LONG err = BuildClient(pClient) ;
  309. if ( err == 0 )
  310. {
  311. err = m_pScopeObject->SetClientInfo(pClient);
  312. }
  313. return err ;
  314. }