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.

131 lines
2.5 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corp., 1995 **/
  4. /**********************************************************************/
  5. /*
  6. FILE HISTORY:
  7. */
  8. #define OEMRESOURCE
  9. #include "stdafx.h"
  10. #include <stdlib.h>
  11. #include <memory.h>
  12. #include <ctype.h>
  13. #include <winsock.h>
  14. #include "comprop.h"
  15. #include "objplus.h"
  16. #include "ipaddres.h"
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21. #define new DEBUG_NEW
  22. // CAVEAT: The functions herein require the winsock lib.
  23. // Constructor
  24. CIpAddress::CIpAddress (const CString & str)
  25. {
  26. CHAR szString [ MAX_PATH ] = {0};
  27. if (IsValidIp(str))
  28. {
  29. #ifdef UNICODE
  30. ::WideCharToMultiByte(CP_ACP, 0, str, -1, szString, sizeof(szString), NULL, NULL);
  31. #else
  32. strcpy (szString, str, str.GetLength());
  33. #endif
  34. ULONG ul = ::inet_addr( szString );
  35. m_fInitOk = (ul != INADDR_NONE);
  36. // Convert the string to network byte order, then to host byte order.
  37. if (m_fInitOk)
  38. {
  39. m_lIpAddress = (LONG)::ntohl(ul) ;
  40. }
  41. }
  42. else
  43. {
  44. m_fInitOk = FALSE;
  45. m_lIpAddress = 0;
  46. }
  47. }
  48. // Assignment operator
  49. const CIpAddress & CIpAddress::operator =(const LONG l)
  50. {
  51. m_lIpAddress = l;
  52. m_fInitOk = TRUE;
  53. return (*this);
  54. }
  55. // Assignment operator
  56. const CIpAddress & CIpAddress::operator =(const CString & str)
  57. {
  58. CHAR szString [ MAX_PATH ] = {0};
  59. if (IsValidIp(str))
  60. {
  61. #ifdef UNICODE
  62. ::WideCharToMultiByte(CP_ACP, 0, str, -1, szString, sizeof(szString), NULL, NULL);
  63. #else
  64. strcpy (szString, str, str.GetLength());
  65. #endif
  66. ULONG ul = ::inet_addr( szString );
  67. m_fInitOk = (ul != INADDR_NONE);
  68. // Convert the string to network byte order, then to host byte order.
  69. if (m_fInitOk)
  70. {
  71. m_lIpAddress = (LONG)::ntohl(ul) ;
  72. }
  73. }
  74. else
  75. {
  76. m_fInitOk = FALSE;
  77. m_lIpAddress = 0;
  78. }
  79. return(*this);
  80. }
  81. BOOL
  82. CIpAddress::IsValidIp(const CString & str)
  83. {
  84. BOOL fValid = TRUE;
  85. for (int i = 0; i < str.GetLength(); i++)
  86. {
  87. if (str[i] != '.' &&
  88. !iswdigit(str[i]))
  89. {
  90. fValid = FALSE;
  91. break;
  92. }
  93. }
  94. return fValid;
  95. }
  96. // Conversion operator
  97. CIpAddress::operator const CString&() const
  98. {
  99. struct in_addr ipaddr ;
  100. static CString strAddr;
  101. // Convert the unsigned long to network byte order
  102. ipaddr.s_addr = ::htonl( (u_long) m_lIpAddress ) ;
  103. // Convert the IP address value to a string
  104. strAddr = inet_ntoa( ipaddr ) ;
  105. return(strAddr);
  106. }