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.

235 lines
3.9 KiB

  1. /*++
  2. Copyright (C) 2001 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. NCsockets.cxx
  6. Abstract:
  7. Contains implementatio of classes and functions which expose
  8. sockets related funcitonaliuty.
  9. Author:
  10. Felix Maxa (AMaxa) 16 May 2001
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. #include <winsock2.h>
  15. #include <Ws2tcpip.h>
  16. #include "ncsockets.hxx"
  17. /*++
  18. Name:
  19. IsIPAddress
  20. Description:
  21. Checks if a string represents an IP address
  22. Arguments:
  23. pszName - name to check
  24. Return Value:
  25. S_OK - pszName represents an IP address
  26. S_FALSE - pszName is a valid name
  27. other HRESULT - an error occurred while trying to check the name
  28. --*/
  29. HRESULT
  30. IsIPAddress(
  31. IN LPCWSTR pszName
  32. )
  33. {
  34. TStatusH hRetval;
  35. hRetval DBGNOCHK = E_INVALIDARG;
  36. if (pszName && *pszName)
  37. {
  38. LPSTR pszAnsi = NULL;
  39. hRetval DBGCHK = UnicodeToAnsiWithAlloc(pszName, &pszAnsi);
  40. if (SUCCEEDED(hRetval))
  41. {
  42. TWinsockStart WsaStart;
  43. hRetval DBGCHK = WsaStart.Valid();
  44. if (SUCCEEDED(hRetval))
  45. {
  46. DWORD Error;
  47. ADDRINFO *pAddrInfo;
  48. ADDRINFO Hint = {0};
  49. Hint.ai_flags = AI_NUMERICHOST;
  50. Hint.ai_family = PF_INET;
  51. Error = getaddrinfo(pszAnsi, NULL, &Hint, &pAddrInfo);
  52. if (Error == ERROR_SUCCESS)
  53. {
  54. //
  55. // It is an IP address
  56. //
  57. hRetval DBGCHK = S_OK;
  58. freeaddrinfo(pAddrInfo);
  59. }
  60. else if (Error == EAI_NONAME)
  61. {
  62. //
  63. // It is not an IP address
  64. //
  65. hRetval DBGCHK = S_FALSE;
  66. }
  67. else
  68. {
  69. //
  70. // Host does not exist
  71. //
  72. hRetval DBGCHK = GetWSAErrorAsHResult();
  73. }
  74. }
  75. delete [] pszAnsi;
  76. }
  77. }
  78. return hRetval;
  79. }
  80. /*++
  81. Name:
  82. GetWSAErrorAsHResult
  83. Description:
  84. Retreives last WSA error as HRESULT
  85. Arguments:
  86. None
  87. Return Value:
  88. HRESULT
  89. --*/
  90. HRESULT
  91. GetWSAErrorAsHResult(
  92. VOID
  93. )
  94. {
  95. DWORD WSAError = WSAGetLastError();
  96. return HRESULT_FROM_WIN32(WSAError);
  97. }
  98. /*++
  99. Name:
  100. TWinsockStart::TWinsockStart
  101. Description:
  102. Initializes winsock for the current thread. Use Valid() member function
  103. to verify if the CTOR terminated successfully.
  104. Arguments:
  105. MajorVersion - major version of winsock to initialize
  106. MinorVersion - minor version of winsock to initialize
  107. Return Value:
  108. None
  109. --*/
  110. TWinsockStart::
  111. TWinsockStart(
  112. IN DWORD MajorVersion,
  113. IN DWORD MinorVersion
  114. )
  115. {
  116. WORD wVersion = MAKEWORD(MajorVersion, MinorVersion);
  117. DWORD Result = WSAStartup(wVersion, &m_WSAData);
  118. m_hr = HRESULT_FROM_WIN32(Result);
  119. }
  120. /*++
  121. Name:
  122. TWinsockStart::~TWinsockStart
  123. Description:
  124. The destructor cleans up the winsock structures initialized
  125. by WSAStartup in the constructor
  126. Arguments:
  127. None
  128. Return Value:
  129. None
  130. --*/
  131. TWinsockStart::
  132. ~TWinsockStart(
  133. VOID
  134. )
  135. {
  136. if (m_hr == S_OK)
  137. {
  138. WSACleanup();
  139. }
  140. }
  141. /*++
  142. Name:
  143. TWinsockStart::Valid
  144. Description:
  145. Checks if the object is valid.
  146. Arguments:
  147. None
  148. Return Value:
  149. S_OK - object is valid
  150. other HRESULT, the object is not valid
  151. --*/
  152. HRESULT
  153. TWinsockStart::
  154. Valid(
  155. VOID
  156. ) CONST
  157. {
  158. return m_hr;
  159. }