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.9 KiB

  1. /*****************************************************************************
  2. *
  3. * $Workfile: HostName.cpp $
  4. *
  5. * Copyright (C) 1997 Hewlett-Packard Company.
  6. * Copyright (c) 1997 Microsoft Corporation.
  7. * All rights reserved.
  8. *
  9. * 11311 Chinden Blvd.
  10. * Boise, Idaho 83714
  11. *
  12. *****************************************************************************/
  13. #include "precomp.h" // pre-compiled header
  14. #include "HostName.h"
  15. //
  16. // FUNCTION: CHostName constructor
  17. //
  18. // PURPOSE:
  19. //
  20. CHostName::CHostName()
  21. {
  22. }
  23. //
  24. // FUNCTION: CHostName constructor
  25. //
  26. // PURPOSE:
  27. //
  28. CHostName::CHostName(LPTSTR psztHostName)
  29. {
  30. SetAddress(psztHostName);
  31. }
  32. //
  33. // FUNCTION: CHostName destructor
  34. //
  35. // PURPOSE:
  36. //
  37. CHostName::~CHostName()
  38. {
  39. }
  40. //
  41. // FUNCTION: IsValid
  42. //
  43. // PURPOSE: Used for validation while the user is typing.
  44. // It is less strict then IsValid() (The no argument version)
  45. //
  46. BOOL CHostName::IsValid(TCHAR *psztStringOriginal,
  47. TCHAR *psztReturnVal,
  48. DWORD cRtnVal)
  49. {
  50. TCHAR *pctPtr = NULL;
  51. TCHAR psztString[MAX_FULLY_QUALIFIED_HOSTNAME_LENGTH] = NULLSTR;
  52. BOOL bIsValid = FALSE;
  53. lstrcpyn(psztString, psztStringOriginal, MAX_FULLY_QUALIFIED_HOSTNAME_LENGTH);
  54. // Check the total length of the string.
  55. bIsValid = (_tcslen(psztString) <= MAX_FULLY_QUALIFIED_HOSTNAME_LENGTH);
  56. if(bIsValid)
  57. {
  58. // Find the first dot and check the length of the part before the first dot.
  59. TCHAR psztSubString[MAX_FULLY_QUALIFIED_HOSTNAME_LENGTH] = NULLSTR;
  60. lstrcpyn(psztSubString, psztString, MAX_FULLY_QUALIFIED_HOSTNAME_LENGTH);
  61. TCHAR *pDotIndex = NULL;
  62. pDotIndex = _tcschr(psztSubString, '.');
  63. if(pDotIndex != NULL) {
  64. *pDotIndex = TCHAR('\0');
  65. }
  66. bIsValid = (_tcslen(psztSubString) <= MAX_HOSTNAME_LEN);
  67. }
  68. // Check for invalid characters.
  69. for (pctPtr = psztString; bIsValid && *pctPtr; pctPtr++)
  70. {
  71. switch (*pctPtr)
  72. {
  73. case (TCHAR)' ':
  74. case (TCHAR)'"':
  75. case (TCHAR)'&':
  76. case (TCHAR)'*':
  77. case (TCHAR)'(':
  78. case (TCHAR)')':
  79. case (TCHAR)'+':
  80. case (TCHAR)',':
  81. case (TCHAR)'/':
  82. case (TCHAR)':':
  83. case (TCHAR)';':
  84. case (TCHAR)'<':
  85. case (TCHAR)'=':
  86. case (TCHAR)'>':
  87. case (TCHAR)'?':
  88. case (TCHAR)'[':
  89. case (TCHAR)'\\':
  90. case (TCHAR)']':
  91. case (TCHAR)'|':
  92. case (TCHAR)'~':
  93. case (TCHAR)'@':
  94. case (TCHAR)'#':
  95. case (TCHAR)'$':
  96. case (TCHAR)'%':
  97. case (TCHAR)'^':
  98. case (TCHAR)'!':
  99. bIsValid = FALSE;
  100. break;
  101. default:
  102. if ( ( *pctPtr < ((TCHAR)'!') ) || ( *pctPtr > (TCHAR)'~' ) ) {
  103. bIsValid = FALSE;
  104. }
  105. break;
  106. }
  107. }
  108. if (!bIsValid) {
  109. if(psztReturnVal != NULL) {
  110. StringCchCopy (psztReturnVal, cRtnVal, m_psztStorageString);
  111. }
  112. } else {
  113. lstrcpyn(m_psztStorageString, psztString, MAX_FULLY_QUALIFIED_HOSTNAME_LENGTH);
  114. // _ASSERTE( _tcsicmp(psztString, psztStringOriginal) == 0 );
  115. }
  116. return(bIsValid);
  117. }
  118. //
  119. // FUNCTION: IsValid
  120. //
  121. // PURPOSE: Strict validation of a host name.
  122. //
  123. BOOL CHostName::IsValid()
  124. {
  125. if(!IsValid(m_psztAddress)) {
  126. return(FALSE);
  127. }
  128. // We know that it is mostly valid now do the final more exact test.
  129. // check to be sure the first character is an alphanumeric character:
  130. if(! ((m_psztAddress[0] >= TCHAR('0') && m_psztAddress[0] <= TCHAR('9')) ||
  131. (m_psztAddress[0] >= TCHAR('A') && m_psztAddress[0] <= TCHAR('Z')) ||
  132. (m_psztAddress[0] >= TCHAR('a') && m_psztAddress[0] <= TCHAR('z'))) ) {
  133. return(FALSE);
  134. }
  135. // check to be sure the name is longer then 1 character.
  136. int length = _tcslen(m_psztAddress);
  137. if(length <= 1) {
  138. return(FALSE);
  139. }
  140. // check to be sure the last character is not a minus sign or period.
  141. if( m_psztAddress[length - 1] == TCHAR('-') ||
  142. m_psztAddress[length - 1] == TCHAR('.')) {
  143. return(FALSE);
  144. }
  145. return(TRUE);
  146. } // IsValid
  147. //
  148. // FUNCTION: SetAddress
  149. //
  150. // PURPOSE: Sets the host name.
  151. //
  152. void CHostName::SetAddress(TCHAR *AddressString)
  153. {
  154. IsValid(AddressString);
  155. lstrcpyn(m_psztAddress, AddressString, MAX_FULLY_QUALIFIED_HOSTNAME_LENGTH);
  156. } // SetAddress
  157. //
  158. // FUNCTION: ToString
  159. //
  160. // PURPOSE: Returns the address in the given buffer.
  161. //
  162. void CHostName::ToString(TCHAR *Buffer, int size)
  163. {
  164. lstrcpyn(Buffer, m_psztAddress, size);
  165. } // ToString