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.

396 lines
7.4 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 DWORD StringToLong(
  69. IN const CComBSTR & bstr
  70. );
  71. static LPCTSTR LongToString(
  72. IN const DWORD dwIPAddress,
  73. OUT CString & str
  74. );
  75. static LPCTSTR LongToString(
  76. IN const DWORD dwIPAddress,
  77. OUT CComBSTR & bstr
  78. );
  79. static LPTSTR LongToString(
  80. IN const DWORD dwIPAddress,
  81. OUT LPTSTR lpStr,
  82. IN int cbSize
  83. );
  84. static LPBYTE DWORDtoLPBYTE(
  85. IN DWORD dw,
  86. OUT LPBYTE lpBytes
  87. );
  88. public:
  89. //
  90. // Constructors
  91. //
  92. CIPAddress();
  93. //
  94. // Construct from DWORD
  95. //
  96. CIPAddress(
  97. IN DWORD dwIPValue,
  98. IN BOOL fNetworkByteOrder = FALSE
  99. );
  100. //
  101. // Construct from byte stream
  102. //
  103. CIPAddress(
  104. IN LPBYTE lpBytes,
  105. IN BOOL fNetworkByteOrder = FALSE
  106. );
  107. //
  108. // Construct from octets
  109. //
  110. CIPAddress(
  111. IN BYTE b1,
  112. IN BYTE b2,
  113. IN BYTE b3,
  114. IN BYTE b4
  115. );
  116. //
  117. // Copy constructor
  118. //
  119. CIPAddress(
  120. IN const CIPAddress & ia
  121. );
  122. //
  123. // Construct from string
  124. //
  125. CIPAddress(
  126. IN LPCTSTR lpstr,
  127. IN int nLength
  128. );
  129. //
  130. // Construct from CString
  131. //
  132. CIPAddress(
  133. IN const CString & str
  134. );
  135. //
  136. // Access Functions
  137. //
  138. public:
  139. int CompareItem(
  140. IN const CIPAddress & ia
  141. ) const;
  142. //
  143. // Query IP address value as a dword
  144. //
  145. DWORD QueryIPAddress(
  146. IN BOOL fNetworkByteOrder = FALSE
  147. ) const;
  148. //
  149. // Get the ip address value as a byte stream
  150. //
  151. LPBYTE QueryIPAddress(
  152. OUT LPBYTE lpBytes,
  153. IN BOOL fNetworkByteOrder = FALSE
  154. ) const;
  155. //
  156. // Get the ip address as a CString
  157. //
  158. LPCTSTR QueryIPAddress(
  159. OUT CString & strAddress
  160. ) const;
  161. //
  162. // Get the ip address as a CComBSTR
  163. //
  164. LPCTSTR QueryIPAddress(
  165. OUT CComBSTR & bstrAddress
  166. ) const;
  167. //
  168. // Get ip address in network byte order DWORD
  169. //
  170. DWORD QueryNetworkOrderIPAddress() const;
  171. //
  172. // Get the ip address in host byte order DWORD
  173. //
  174. DWORD QueryHostOrderIPAddress() const;
  175. //
  176. // Assignment operators
  177. //
  178. const CIPAddress & operator =(
  179. IN const DWORD dwIPAddress
  180. );
  181. const CIPAddress & operator =(
  182. IN LPCTSTR lpstr
  183. );
  184. const CIPAddress & operator =(
  185. IN const CString & str
  186. );
  187. const CIPAddress & operator =(
  188. IN const CIPAddress & ia
  189. );
  190. //
  191. // Comparison operators
  192. //
  193. BOOL operator ==(
  194. IN const CIPAddress & ia
  195. ) const;
  196. BOOL operator ==(
  197. IN DWORD dwIPAddress
  198. ) const;
  199. BOOL operator !=(
  200. IN const CIPAddress & ia
  201. ) const;
  202. BOOL operator !=(
  203. IN DWORD dwIPAddress
  204. ) const;
  205. //
  206. // Conversion operators
  207. //
  208. operator const DWORD() const { return m_dwIPAddress; }
  209. operator LPCTSTR() const;
  210. operator CString() const;
  211. //
  212. // Value Verification Helpers
  213. //
  214. void SetZeroValue();
  215. BOOL IsZeroValue() const;
  216. BOOL IsBadValue() const;
  217. private:
  218. DWORD m_dwIPAddress;
  219. };
  220. //
  221. // Inline Expansion
  222. //
  223. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  224. inline /* static */ DWORD CIPAddress::StringToLong(
  225. IN const CString & str
  226. )
  227. {
  228. return CIPAddress::StringToLong(str, str.GetLength());
  229. }
  230. inline /* static */ DWORD CIPAddress::StringToLong(
  231. IN const CComBSTR & bstr
  232. )
  233. {
  234. return CIPAddress::StringToLong(bstr, bstr.Length());
  235. }
  236. inline LPCTSTR CIPAddress::QueryIPAddress(
  237. OUT CString & strAddress
  238. ) const
  239. {
  240. return LongToString(m_dwIPAddress, strAddress);
  241. }
  242. inline LPCTSTR CIPAddress::QueryIPAddress(
  243. OUT CComBSTR & bstrAddress
  244. ) const
  245. {
  246. return LongToString(m_dwIPAddress, bstrAddress);
  247. }
  248. inline DWORD CIPAddress::QueryNetworkOrderIPAddress() const
  249. {
  250. return QueryIPAddress(TRUE);
  251. }
  252. inline DWORD CIPAddress::QueryHostOrderIPAddress() const
  253. {
  254. return QueryIPAddress(FALSE);
  255. }
  256. inline BOOL CIPAddress::operator ==(
  257. IN const CIPAddress & ia
  258. ) const
  259. {
  260. return CompareItem(ia) == 0;
  261. }
  262. inline BOOL CIPAddress::operator ==(
  263. IN DWORD dwIPAddress
  264. ) const
  265. {
  266. return m_dwIPAddress == dwIPAddress;
  267. }
  268. inline BOOL CIPAddress::operator !=(
  269. IN const CIPAddress & ia
  270. ) const
  271. {
  272. return CompareItem(ia) != 0;
  273. }
  274. inline BOOL CIPAddress::operator !=(
  275. IN DWORD dwIPAddress
  276. ) const
  277. {
  278. return m_dwIPAddress != dwIPAddress;
  279. }
  280. inline void CIPAddress::SetZeroValue()
  281. {
  282. m_dwIPAddress = NULL_IP_ADDRESS;
  283. }
  284. inline BOOL CIPAddress::IsZeroValue() const
  285. {
  286. return m_dwIPAddress == NULL_IP_ADDRESS;
  287. }
  288. inline BOOL CIPAddress::IsBadValue() const
  289. {
  290. return m_dwIPAddress == BAD_IP_ADDRESS;
  291. }
  292. //
  293. // Helper function to build a list of known IP addresses,
  294. // and add them to a combo box
  295. //
  296. DWORD
  297. COMDLL
  298. PopulateComboWithKnownIpAddresses(
  299. IN LPCTSTR lpszServer,
  300. IN CComboBox & combo,
  301. IN CIPAddress & iaIpAddress,
  302. OUT CObListPlus & oblIpAddresses,
  303. OUT int & nIpAddressSel
  304. );
  305. //
  306. // Helper function to get an ip address from a combo/edit/list
  307. // control
  308. //
  309. BOOL
  310. COMDLL
  311. FetchIpAddressFromCombo(
  312. IN CComboBox & combo,
  313. IN CObListPlus & oblIpAddresses,
  314. OUT CIPAddress & ia
  315. );
  316. #endif // _IPA_H