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.

257 lines
5.5 KiB

  1. // Copyright (c) Microsoft. All rights reserved.
  2. //
  3. // This is unpublished source code of Microsoft.
  4. // The copyright notice above does not evidence any
  5. // actual or intended publication of such source code.
  6. // OneLiner : Implementation of MIPAddressAdmin
  7. // DevUnit : wlbstest
  8. // Author : Murtaza Hakim
  9. // include files
  10. #include "MIPAddress.h"
  11. #include "WTokens.h"
  12. #include "wlbsutil.h"
  13. // checkIfValid
  14. //
  15. bool
  16. MIPAddress::checkIfValid( const _bstr_t& ipAddrToCheck )
  17. {
  18. // The validity rules are as follows
  19. //
  20. // The first byte (FB) has to be : 0 < FB < 224 && FB != 127
  21. // Note that 127 is loopback address.
  22. // hostid portion of an address cannot be zero.
  23. //
  24. // class A range is 1 - 126. hostid portion is last 3 bytes.
  25. // class B range is 128 - 191 hostid portion is last 2 bytes
  26. // class C range is 192 - 223 hostid portion is last byte.
  27. // split up the ipAddrToCheck into its 4 bytes.
  28. //
  29. WTokens tokens;
  30. tokens.init( wstring( ipAddrToCheck ) , L".");
  31. vector<wstring> byteTokens = tokens.tokenize();
  32. if( byteTokens.size() != 4 )
  33. {
  34. return false;
  35. }
  36. int firstByte = _wtoi( byteTokens[0].c_str() );
  37. int secondByte = _wtoi( byteTokens[1].c_str() );
  38. int thirdByte = _wtoi( byteTokens[2].c_str() );
  39. int fourthByte = _wtoi( byteTokens[3].c_str() );
  40. // check firstByte
  41. if ( ( firstByte > 0 )
  42. &&
  43. ( firstByte < 224 )
  44. &&
  45. ( firstByte != 127 )
  46. )
  47. {
  48. // check that host id portion is not zero.
  49. IPClass ipClass;
  50. getIPClass( ipAddrToCheck, ipClass );
  51. switch( ipClass )
  52. {
  53. case classA :
  54. // last three bytes should not be zero.
  55. if( ( _wtoi( byteTokens[1].c_str() ) == 0 )
  56. &&
  57. ( _wtoi( byteTokens[2].c_str() )== 0 )
  58. &&
  59. ( _wtoi( byteTokens[3].c_str() )== 0 )
  60. )
  61. {
  62. return false;
  63. }
  64. break;
  65. case classB :
  66. // last two bytes should not be zero.
  67. if( ( _wtoi( byteTokens[2].c_str() )== 0 )
  68. &&
  69. ( _wtoi( byteTokens[3].c_str() )== 0 )
  70. )
  71. {
  72. return false;
  73. }
  74. break;
  75. case classC :
  76. // last byte should not be zero.
  77. if( _wtoi( byteTokens[3].c_str() )
  78. == 0 )
  79. {
  80. return false;
  81. }
  82. break;
  83. default :
  84. // this should not have happened.
  85. return false;
  86. break;
  87. }
  88. return true;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94. }
  95. // getDefaultSubnetMask
  96. //
  97. bool
  98. MIPAddress::getDefaultSubnetMask( const _bstr_t& ipAddr,
  99. _bstr_t& subnetMask )
  100. {
  101. // first ensure that the ip is valid.
  102. //
  103. bool isValid = checkIfValid( ipAddr );
  104. if( isValid == false )
  105. {
  106. return false;
  107. }
  108. // get the class to which this ip belongs.
  109. // as this determines the subnet.
  110. IPClass ipClass;
  111. getIPClass( ipAddr,
  112. ipClass );
  113. switch( ipClass )
  114. {
  115. case classA :
  116. subnetMask = L"255.0.0.0";
  117. break;
  118. case classB :
  119. subnetMask = L"255.255.0.0";
  120. break;
  121. case classC :
  122. subnetMask = L"255.255.255.0";
  123. break;
  124. default :
  125. // this should not have happened.
  126. return false;
  127. break;
  128. }
  129. return true;
  130. }
  131. // getIPClass
  132. //
  133. bool
  134. MIPAddress::getIPClass( const _bstr_t& ipAddr,
  135. IPClass& ipClass )
  136. {
  137. // get the first byte of the ipAddr
  138. WTokens tokens;
  139. tokens.init( wstring( ipAddr ) , L".");
  140. vector<wstring> byteTokens = tokens.tokenize();
  141. if( byteTokens.size() == 0 )
  142. {
  143. return false;
  144. }
  145. int firstByte = _wtoi( byteTokens[0].c_str() );
  146. if( ( firstByte >= 1 )
  147. &&
  148. ( firstByte <= 126 )
  149. )
  150. {
  151. // classA
  152. ipClass = classA;
  153. return true;
  154. }
  155. else if( (firstByte >= 128 )
  156. &&
  157. (firstByte <= 191 )
  158. )
  159. {
  160. // classB
  161. ipClass = classB;
  162. return true;
  163. }
  164. else if( (firstByte >= 192 )
  165. &&
  166. (firstByte <= 223 )
  167. )
  168. {
  169. // classC
  170. ipClass = classC;
  171. return true;
  172. }
  173. else if( (firstByte >= 224 )
  174. &&
  175. (firstByte <= 239 )
  176. )
  177. {
  178. // classD
  179. ipClass = classD;
  180. return true;
  181. }
  182. else if( (firstByte >= 240 )
  183. &&
  184. (firstByte <= 247 )
  185. )
  186. {
  187. // classE
  188. ipClass = classE;
  189. return true;
  190. }
  191. else
  192. {
  193. // invalid net portiion.
  194. return false;
  195. }
  196. }
  197. bool
  198. MIPAddress::isValidIPAddressSubnetMaskPair( const _bstr_t& ipAddress,
  199. const _bstr_t& subnetMask )
  200. {
  201. if( IsValidIPAddressSubnetMaskPair( ipAddress, subnetMask ) == TRUE )
  202. {
  203. return true;
  204. }
  205. else
  206. {
  207. return false;
  208. }
  209. }
  210. bool
  211. MIPAddress::isContiguousSubnetMask( const _bstr_t& subnetMask )
  212. {
  213. if( IsContiguousSubnetMask( subnetMask ) == TRUE )
  214. {
  215. return true;
  216. }
  217. else
  218. {
  219. return false;
  220. }
  221. }