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.

147 lines
2.7 KiB

  1. #include "network.h"
  2. #include "diagnostics.h"
  3. #include "util.h"
  4. BOOL
  5. CDiagnostics::IsInvalidIPAddress(
  6. IN LPCSTR pszHostName
  7. )
  8. /*++
  9. Routine Description
  10. Checks to see if an IP Host is a in valid IP address
  11. 0.0.0.0 is not valid
  12. 255.255.255.255 is not valid
  13. "" is not valid
  14. Arguments
  15. pszHostName Host Address
  16. Return Value
  17. TRUE Is invalid IP address
  18. FALSE Valid IP address
  19. --*/
  20. {
  21. BYTE bIP[4];
  22. int iRetVal;
  23. LONG lAddr;
  24. if( NULL == pszHostName || strcmp(pszHostName,"") == 0 || strcmp(pszHostName,"255.255.255.255") ==0)
  25. {
  26. // Invalid IP Host
  27. //
  28. return TRUE;
  29. }
  30. lAddr = inet_addr(pszHostName);
  31. if( INADDR_NONE != lAddr )
  32. {
  33. // Formatted like an IP address X.X.X.X
  34. //
  35. if( lAddr == 0 )
  36. {
  37. // Invalid IP address 0.0.0.0
  38. //
  39. return TRUE;
  40. }
  41. }
  42. return FALSE;
  43. }
  44. BOOL
  45. CDiagnostics::IsInvalidIPAddress(
  46. IN LPCWSTR pszHostName
  47. )
  48. {
  49. CHAR szIPAddress[MAX_PATH+1];
  50. szIPAddress[MAX_PATH] = L'\0';
  51. if( lstrlen(pszHostName) > 255 )
  52. {
  53. // A host name can only be 255 chars long
  54. return TRUE;
  55. }
  56. wcstombs(szIPAddress,pszHostName,MAX_PATH);
  57. return IsInvalidIPAddress(szIPAddress);
  58. }
  59. BOOL
  60. CDiagnostics::Connect(
  61. IN LPCTSTR pszwHostName,
  62. IN DWORD dwPort
  63. )
  64. /*++
  65. Routine Description
  66. Establish a TCP connect
  67. Arguments
  68. pszwHostName Host to ping
  69. dwPort Port to connect to
  70. Return Value
  71. TRUE Successfully connected
  72. FALSE Failed to establish connection
  73. --*/
  74. {
  75. SOCKET s;
  76. SOCKADDR_IN sAddr;
  77. CHAR szAscii[MAX_PATH + 1];
  78. hostent * pHostent;
  79. // Create the socket
  80. //
  81. s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC);
  82. if (INVALID_SOCKET == s)
  83. {
  84. return FALSE;
  85. }
  86. // Bind this socket to the server's socket address
  87. //
  88. memset(&sAddr, 0, sizeof (sAddr));
  89. sAddr.sin_family = AF_INET;
  90. sAddr.sin_port = htons((u_short)dwPort);
  91. wcstombs(szAscii,(WCHAR *)pszwHostName,MAX_PATH);
  92. pHostent = gethostbyname(szAscii);
  93. if( !pHostent )
  94. {
  95. return FALSE;
  96. }
  97. // Set the destination info
  98. //
  99. ULONG ulAddr;
  100. memcpy(&ulAddr,pHostent->h_addr,pHostent->h_length);
  101. sAddr.sin_addr.s_addr = ulAddr;
  102. // Attempt to connect
  103. //
  104. if (connect(s, (SOCKADDR*)&sAddr, sizeof(SOCKADDR_IN)) == 0)
  105. {
  106. // Connection succeded
  107. //
  108. closesocket(s);
  109. return TRUE;
  110. }
  111. else
  112. {
  113. // Connection failed
  114. //
  115. closesocket(s);
  116. return FALSE;
  117. }
  118. }