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.

362 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 1994-1998 Microsoft Corporation
  3. Module Name :
  4. ipa.h
  5. Abstract:
  6. IP Address value
  7. Author:
  8. Ronald Meijer (ronaldm)
  9. Project:
  10. Internet Services Manager
  11. Revision History:
  12. --*/
  13. #ifndef _IPA_H
  14. #define _IPA_H
  15. //
  16. // IP Address Conversion Macros
  17. //
  18. #ifdef MAKEIPADDRESS
  19. #undef MAKEIPADDRESS
  20. #endif // MAKEIPADDRESS
  21. #define MAKEIPADDRESS(b1,b2,b3,b4) (((DWORD)(b1)<<24) +\
  22. ((DWORD)(b2)<<16) +\
  23. ((DWORD)(b3)<< 8) +\
  24. ((DWORD)(b4)))
  25. #ifndef GETIP_FIRST
  26. #define GETIP_FIRST(x) ((x>>24) & 0xff)
  27. #define GETIP_SECOND(x) ((x>>16) & 0xff)
  28. #define GETIP_THIRD(x) ((x>> 8) & 0xff)
  29. #define GETIP_FOURTH(x) ((x) & 0xff)
  30. #endif // GETIP_FIRST
  31. //
  32. // Some predefined IP values
  33. //
  34. #define NULL_IP_ADDRESS (DWORD)(0x00000000)
  35. #define NULL_IP_MASK (DWORD)(0xFFFFFFFF)
  36. #define BAD_IP_ADDRESS (DWORD)(0xFFFFFFFF)
  37. class COMDLL CIPAddress : public CObjectPlus
  38. /*++
  39. Class Description:
  40. IP Address classes. Winsock is required to be initialized for this
  41. to work.
  42. Public Interface:
  43. CIPAddress : Various constructors
  44. operator = : Assignment operator
  45. operator == : Comparison operators
  46. operator const DWORD : Cast operator
  47. operator LPCTSTR : Cast operator
  48. operator CString : Cast operator
  49. CompareItem : Comparison function
  50. QueryIPAddress : Get the ip address value
  51. QueryNetworkOrderIPAddress : Get the ip address value (network order)
  52. QueryHostOrderIPAddress : Get the ip address value (host order)
  53. StringToLong : Convert ip address string to 32 bit number
  54. LongToString : Convert 32 bit value to ip address string
  55. --*/
  56. {
  57. //
  58. // Helper Functions
  59. //
  60. public:
  61. static DWORD StringToLong(
  62. IN LPCTSTR lpstr,
  63. IN int nLength
  64. );
  65. static DWORD StringToLong(
  66. IN const CString & str
  67. );
  68. static LPCTSTR LongToString(
  69. IN const DWORD dwIPAddress,
  70. OUT CString & str
  71. );
  72. static LPTSTR LongToString(
  73. IN const DWORD dwIPAddress,
  74. OUT LPTSTR lpStr,
  75. IN int cbSize
  76. );
  77. static LPBYTE DWORDtoLPBYTE(
  78. IN DWORD dw,
  79. OUT LPBYTE lpBytes
  80. );
  81. public:
  82. //
  83. // Constructors
  84. //
  85. CIPAddress();
  86. //
  87. // Construct from DWORD
  88. //
  89. CIPAddress(
  90. IN DWORD dwIPValue,
  91. IN BOOL fNetworkByteOrder = FALSE
  92. );
  93. //
  94. // Construct from byte stream
  95. //
  96. CIPAddress(
  97. IN LPBYTE lpBytes,
  98. IN BOOL fNetworkByteOrder = FALSE
  99. );
  100. //
  101. // Construct from octets
  102. //
  103. CIPAddress(
  104. IN BYTE b1,
  105. IN BYTE b2,
  106. IN BYTE b3,
  107. IN BYTE b4
  108. );
  109. //
  110. // Copy constructor
  111. //
  112. CIPAddress(
  113. IN const CIPAddress & ia
  114. );
  115. //
  116. // Construct from string
  117. //
  118. CIPAddress(
  119. IN LPCTSTR lpstr,
  120. IN int nLength
  121. );
  122. //
  123. // Construct from CString
  124. //
  125. CIPAddress(
  126. IN const CString & str
  127. );
  128. //
  129. // Access Functions
  130. //
  131. public:
  132. int CompareItem(
  133. IN const CIPAddress & ia
  134. ) const;
  135. //
  136. // Query IP address value as a dword
  137. //
  138. DWORD QueryIPAddress(
  139. IN BOOL fNetworkByteOrder = FALSE
  140. ) const;
  141. //
  142. // Get the ip address value as a byte stream
  143. //
  144. LPBYTE QueryIPAddress(
  145. OUT LPBYTE lpBytes,
  146. IN BOOL fNetworkByteOrder = FALSE
  147. ) const;
  148. //
  149. // Get the ip address as a CString
  150. //
  151. LPCTSTR QueryIPAddress(
  152. OUT CString & strAddress
  153. ) const;
  154. //
  155. // Get ip address in network byte order DWORD
  156. //
  157. DWORD QueryNetworkOrderIPAddress() const;
  158. //
  159. // Get the ip address in host byte order DWORD
  160. //
  161. DWORD QueryHostOrderIPAddress() const;
  162. //
  163. // Assignment operators
  164. //
  165. const CIPAddress & operator =(
  166. IN const DWORD dwIPAddress
  167. );
  168. const CIPAddress & operator =(
  169. IN LPCTSTR lpstr
  170. );
  171. const CIPAddress & operator =(
  172. IN const CString & str
  173. );
  174. const CIPAddress & operator =(
  175. IN const CIPAddress & ia
  176. );
  177. //
  178. // Comparison operators
  179. //
  180. BOOL operator ==(
  181. IN const CIPAddress & ia
  182. ) const;
  183. BOOL operator ==(
  184. IN DWORD dwIPAddress
  185. ) const;
  186. BOOL operator !=(
  187. IN const CIPAddress & ia
  188. ) const;
  189. BOOL operator !=(
  190. IN DWORD dwIPAddress
  191. ) const;
  192. //
  193. // Conversion operators
  194. //
  195. operator const DWORD() const { return m_dwIPAddress; }
  196. operator LPCTSTR() const;
  197. operator CString() const;
  198. //
  199. // Value Verification Helpers
  200. //
  201. void SetZeroValue();
  202. BOOL IsZeroValue() const;
  203. BOOL IsBadValue() const;
  204. private:
  205. DWORD m_dwIPAddress;
  206. };
  207. //
  208. // Inline Expansion
  209. //
  210. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  211. inline /* static */ DWORD CIPAddress::StringToLong(
  212. IN const CString & str
  213. )
  214. {
  215. return CIPAddress::StringToLong(str, str.GetLength());
  216. }
  217. inline LPCTSTR CIPAddress::QueryIPAddress(
  218. OUT CString & strAddress
  219. ) const
  220. {
  221. return LongToString(m_dwIPAddress, strAddress);
  222. }
  223. inline DWORD CIPAddress::QueryNetworkOrderIPAddress() const
  224. {
  225. return QueryIPAddress(TRUE);
  226. }
  227. inline DWORD CIPAddress::QueryHostOrderIPAddress() const
  228. {
  229. return QueryIPAddress(FALSE);
  230. }
  231. inline BOOL CIPAddress::operator ==(
  232. IN const CIPAddress & ia
  233. ) const
  234. {
  235. return CompareItem(ia) == 0;
  236. }
  237. inline BOOL CIPAddress::operator ==(
  238. IN DWORD dwIPAddress
  239. ) const
  240. {
  241. return m_dwIPAddress == dwIPAddress;
  242. }
  243. inline BOOL CIPAddress::operator !=(
  244. IN const CIPAddress & ia
  245. ) const
  246. {
  247. return CompareItem(ia) != 0;
  248. }
  249. inline BOOL CIPAddress::operator !=(
  250. IN DWORD dwIPAddress
  251. ) const
  252. {
  253. return m_dwIPAddress != dwIPAddress;
  254. }
  255. inline void CIPAddress::SetZeroValue()
  256. {
  257. m_dwIPAddress = NULL_IP_ADDRESS;
  258. }
  259. inline BOOL CIPAddress::IsZeroValue() const
  260. {
  261. return m_dwIPAddress == NULL_IP_ADDRESS;
  262. }
  263. inline BOOL CIPAddress::IsBadValue() const
  264. {
  265. return m_dwIPAddress == BAD_IP_ADDRESS;
  266. }
  267. //
  268. // Helper function to build a list of known IP addresses,
  269. // and add them to a combo box
  270. //
  271. DWORD COMDLL PopulateComboWithKnownIpAddresses(
  272. IN LPCTSTR lpszServer,
  273. IN CComboBox & combo,
  274. IN CIPAddress & iaIpAddress,
  275. OUT CObListPlus & oblIpAddresses,
  276. OUT int & nIpAddressSel
  277. );
  278. //
  279. // Helper function to get an ip address from a combo/edit/list
  280. // control
  281. //
  282. BOOL COMDLL FetchIpAddressFromCombo(
  283. IN CComboBox & combo,
  284. IN CObListPlus & oblIpAddresses,
  285. OUT CIPAddress & ia
  286. );
  287. #endif // _IPA_H