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.

130 lines
2.6 KiB

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