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.

215 lines
5.6 KiB

  1. /*****************************************************************************
  2. *
  3. * $Workfile: IPAddr.cpp $
  4. *
  5. * Copyright (C) 1997 Hewlett-Packard Company.
  6. * All rights reserved.
  7. *
  8. * 11311 Chinden Blvd.
  9. * Boise, Idaho 83714
  10. *
  11. *****************************************************************************/
  12. #include "precomp.h" // pre-compiled header
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // CIPAddress::CIPAddress()
  15. // Initializes the IP address
  16. CIPAddress::CIPAddress()
  17. {
  18. m_psztStorageStringComponent[0] = '\0';
  19. m_psztStorageString[0] = '\0';
  20. } // ::CIPAddress()
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // CIPAddress::CIPAddress(someString)
  23. // Initializes the IP address given a string
  24. CIPAddress::CIPAddress(LPTSTR in psztIPAddr)
  25. {
  26. int num0 = 0;
  27. int num1 = 0;
  28. int num2 = 0;
  29. int num3 = 0;
  30. if (_stscanf(psztIPAddr, _TEXT("%d.%d.%d.%d"), &num0, &num1, &num2, &num3) == 4)
  31. {
  32. m_bAddress[0] = (BYTE)num0;
  33. m_bAddress[1] = (BYTE)num1;
  34. m_bAddress[2] = (BYTE)num2;
  35. m_bAddress[3] = (BYTE)num3;
  36. }
  37. else
  38. {
  39. ZeroMemory (m_bAddress, sizeof m_bAddress);
  40. }
  41. } // ::CIPAddress()
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // CIPAddress::~CIPAddress()
  44. //
  45. CIPAddress::~CIPAddress()
  46. {
  47. } // ::~CIPAddress()
  48. ///////////////////////////////////////////////////////////////////////////////
  49. // IsValid -- validate an ip address
  50. //
  51. BOOL CIPAddress::IsValid(TCHAR *psztStringAddress,
  52. TCHAR *psztReturnVal,
  53. DWORD CRtnValSize)
  54. {
  55. BOOL bIsValid = TRUE;
  56. CHAR szHostName[MAX_NETWORKNAME_LEN];
  57. UNICODE_TO_MBCS(szHostName, MAX_NETWORKNAME_LEN, psztStringAddress, -1);
  58. if ( inet_addr(szHostName) == INADDR_NONE ) {
  59. bIsValid = FALSE;
  60. }
  61. else
  62. {
  63. int num0 = 0;
  64. int num1 = 0;
  65. int num2 = 0;
  66. int num3 = 0;
  67. //
  68. // Scan for correct dotted notation
  69. //
  70. if( _stscanf(psztStringAddress, _TEXT("%d.%d.%d.%d"),
  71. &num0,
  72. &num1,
  73. &num2,
  74. &num3) != 4 )
  75. {
  76. bIsValid = FALSE;
  77. }
  78. if( num0 == 0 )
  79. {
  80. bIsValid = FALSE;
  81. }
  82. }
  83. // Finish
  84. if (!bIsValid)
  85. {
  86. if(psztReturnVal != NULL)
  87. {
  88. lstrcpyn(psztReturnVal,
  89. m_psztStorageString,
  90. CRtnValSize);
  91. }
  92. }
  93. else
  94. {
  95. lstrcpyn(m_psztStorageString,
  96. psztStringAddress,
  97. STORAGE_STRING_LEN);
  98. }
  99. return(bIsValid);
  100. } // IsValid
  101. ///////////////////////////////////////////////////////////////////////////////
  102. // IsValid -- validate an ip number entered in an edit control.
  103. //
  104. BOOL CIPAddress::IsValid(BYTE Address[4])
  105. {
  106. for(int i=0; i<4; i++)
  107. {
  108. if ((Address[i] > 255) || (Address[i] < 0))
  109. {
  110. return FALSE;
  111. }
  112. }
  113. // if we got through all that stuff:
  114. return TRUE;
  115. } // IsValid
  116. ///////////////////////////////////////////////////////////////////////////////
  117. // SetAddress -- set the value of this IPAddress object given 4 strings
  118. //
  119. void CIPAddress::SetAddress(TCHAR *psztAddr1,
  120. TCHAR *psztAddr2,
  121. TCHAR *psztAddr3,
  122. TCHAR *psztAddr4)
  123. {
  124. m_bAddress[0] = (BYTE) _ttoi( psztAddr1 );
  125. m_bAddress[1] = (BYTE) _ttoi( psztAddr2 );
  126. m_bAddress[2] = (BYTE) _ttoi( psztAddr3 );
  127. m_bAddress[3] = (BYTE) _ttoi( psztAddr4 );
  128. } // SetAddress
  129. ///////////////////////////////////////////////////////////////////////////////
  130. // SetAddress -- Set the address given a string
  131. //
  132. void CIPAddress::SetAddress(TCHAR *psztAddress)
  133. {
  134. int num0 = 0;
  135. int num1 = 0;
  136. int num2 = 0;
  137. int num3 = 0;
  138. if(IsValid(psztAddress) &&
  139. _stscanf(psztAddress, _TEXT("%d.%d.%d.%d"), &num0, &num1, &num2, &num3) == 4
  140. )
  141. {
  142. m_bAddress[0] = (BYTE)num0;
  143. m_bAddress[1] = (BYTE)num1;
  144. m_bAddress[2] = (BYTE)num2;
  145. m_bAddress[3] = (BYTE)num3;
  146. }
  147. else
  148. {
  149. ZeroMemory (m_bAddress, sizeof m_bAddress);
  150. }
  151. } // SetAddress
  152. ///////////////////////////////////////////////////////////////////////////////
  153. // ToString -- fill the given buffer with a string representing the IP address.
  154. //
  155. void CIPAddress::ToString(TCHAR *psztBuffer,
  156. int iSize)
  157. {
  158. TCHAR strAddr[MAX_IPADDR_STR_LEN] = NULLSTR;
  159. StringCchPrintf (strAddr, COUNTOF (strAddr), _TEXT("%d.%d.%d.%d"), m_bAddress[0], m_bAddress[1], m_bAddress[2], m_bAddress[3]);
  160. lstrcpyn(psztBuffer, strAddr, iSize);
  161. } // ToString
  162. ///////////////////////////////////////////////////////////////////////////////
  163. // ToComponentStrings -- fill the given buffers with 4 strings representing the IP address.
  164. //
  165. void CIPAddress::ToComponentStrings(TCHAR *str1,
  166. TCHAR *str2,
  167. TCHAR *str3,
  168. TCHAR *str4,
  169. size_t cchStr)
  170. {
  171. StringCchPrintf (str1, cchStr, TEXT("%d"), m_bAddress[0]);
  172. StringCchPrintf (str2, cchStr, TEXT("%d"), m_bAddress[1]);
  173. StringCchPrintf (str3, cchStr, TEXT("%d"), m_bAddress[2]);
  174. StringCchPrintf (str4, cchStr, TEXT("%d"), m_bAddress[3]);
  175. } // ToComponentStrings