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.

307 lines
7.0 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. transport.cxx
  5. Abstract:
  6. transport
  7. Author:
  8. Larry Zhu (LZhu) January 1, 2002 Created
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "precomp.hxx"
  14. #pragma hdrstop
  15. #include "sockcomm.h"
  16. #include "transport.hxx"
  17. ULONG g_MessageNumTlsIndex = TLS_OUT_OF_INDEXES;
  18. ULONG g_MsgHeaderLen = kMsgHeaderLen;
  19. HRESULT
  20. ServerInit(
  21. IN USHORT Port,
  22. IN OPTIONAL PCSTR pszDescription,
  23. OUT SOCKET* pSocketListen
  24. )
  25. {
  26. THResult hRetval = S_OK;
  27. SOCKADDR_IN sin = {0};
  28. SOCKET sockListen = INVALID_SOCKET;
  29. int nRes = SOCKET_ERROR;
  30. *pSocketListen = INVALID_SOCKET;
  31. //
  32. // create listening socket
  33. //
  34. sockListen = socket(PF_INET, SOCK_STREAM, 0);
  35. hRetval DBGCHK = (INVALID_SOCKET != sockListen) ? S_OK : HResultFromWin32(WSAGetLastError());
  36. //
  37. // bind to local port
  38. //
  39. if (SUCCEEDED(hRetval))
  40. {
  41. sin.sin_family = AF_INET;
  42. sin.sin_addr.s_addr = 0;
  43. sin.sin_port = htons(Port);
  44. nRes = bind(sockListen, (PSOCKADDR) &sin, sizeof(sin));
  45. DBGCFG1(hRetval, HRESULT_FROM_WIN32(WSAEADDRINUSE));
  46. hRetval DBGCHK = (SOCKET_ERROR != nRes) ? S_OK : HResultFromWin32(WSAGetLastError());
  47. if (FAILED(hRetval) && (WSAEADDRINUSE == HRESULT_CODE(hRetval)))
  48. {
  49. DebugPrintf(SSPI_ERROR, "ServerInit port %d(%#x) in use, failed to bind\n", Port, Port);
  50. }
  51. else if (FAILED(hRetval))
  52. {
  53. DebugPrintf(SSPI_ERROR, "ServerInit binding to port %d failed with %#x\n", Port, HRESULT_CODE(hRetval));
  54. }
  55. }
  56. //
  57. // listen for client
  58. //
  59. if (SUCCEEDED(hRetval))
  60. {
  61. DebugPrintf(SSPI_LOG, "%s%slistening on port %d(%#x)\n",
  62. pszDescription ? pszDescription : "", pszDescription ? " " : "", Port, Port);
  63. nRes = listen(sockListen, 1);
  64. hRetval DBGCHK = (SOCKET_ERROR != nRes) ? S_OK : HResultFromWin32(WSAGetLastError());
  65. }
  66. if (SUCCEEDED(hRetval))
  67. {
  68. *pSocketListen = sockListen;
  69. sockListen = INVALID_SOCKET;
  70. }
  71. THResult hr;
  72. if (sockListen != INVALID_SOCKET)
  73. {
  74. hr DBGCHK = closesocket(sockListen) == ERROR_SUCCESS ? S_OK : HResultFromWin32(WSAGetLastError());
  75. }
  76. return hRetval;
  77. }
  78. HRESULT
  79. ClientConnect(
  80. IN OPTIONAL PCSTR pszServer,
  81. IN USHORT Port,
  82. OUT SOCKET* pSocketConnected
  83. )
  84. {
  85. THResult hRetval = S_OK;
  86. SOCKET sockServer = INVALID_SOCKET;
  87. ULONG ulAddress = INADDR_NONE;
  88. struct hostent *pHost = NULL;
  89. SOCKADDR_IN sin = {0};
  90. CHAR szServer[DNS_MAX_NAME_LENGTH + 1] = {0};
  91. //
  92. // lookup the address for the server name
  93. //
  94. if (!pszServer)
  95. {
  96. hRetval DBGCHK = gethostname(szServer, sizeof(szServer) - 1) == ERROR_SUCCESS ? S_OK : HResultFromWin32(WSAGetLastError());
  97. if (SUCCEEDED(hRetval))
  98. {
  99. pszServer = szServer;
  100. }
  101. }
  102. if (SUCCEEDED(hRetval))
  103. {
  104. ulAddress = inet_addr(pszServer);
  105. if (INADDR_NONE == ulAddress)
  106. {
  107. pHost = gethostbyname(pszServer);
  108. hRetval DBGCHK = pHost ? S_OK : HResultFromWin32(WSAGetLastError());
  109. if (SUCCEEDED(hRetval))
  110. {
  111. RtlCopyMemory((CHAR *)&ulAddress, pHost->h_addr, pHost->h_length);
  112. }
  113. }
  114. }
  115. //
  116. // create the socket
  117. //
  118. if (SUCCEEDED(hRetval))
  119. {
  120. sockServer = socket(PF_INET, SOCK_STREAM, 0);
  121. hRetval DBGCHK = (INVALID_SOCKET == sockServer) ? S_OK : HResultFromWin32(WSAGetLastError());
  122. }
  123. if (SUCCEEDED(hRetval))
  124. {
  125. sin.sin_family = AF_INET;
  126. sin.sin_addr.s_addr = ulAddress;
  127. sin.sin_port = htons(Port);
  128. //
  129. // connect to remote endpoint
  130. //
  131. hRetval DBGCHK = connect(sockServer, (PSOCKADDR) &sin, sizeof(sin)) == ERROR_SUCCESS ? S_OK : HResultFromWin32(WSAGetLastError());
  132. }
  133. if (SUCCEEDED(hRetval))
  134. {
  135. *pSocketConnected = sockServer;
  136. sockServer = INVALID_SOCKET;
  137. }
  138. THResult hr;
  139. if (INVALID_SOCKET != sockServer)
  140. {
  141. hr DBGCHK = closesocket(sockServer) == ERROR_SUCCESS ? S_OK : HResultFromWin32(WSAGetLastError());
  142. }
  143. return hRetval;
  144. }
  145. HRESULT
  146. WriteMessage(
  147. IN SOCKET s,
  148. IN ULONG cbBuf,
  149. IN VOID* pBuf
  150. )
  151. {
  152. THResult hRetval = S_OK;
  153. ULONG cbWritten = 0;
  154. ULONG cbRead = 0;
  155. ULONG* pMessageNum = NULL;
  156. hRetval DBGCHK = GetPerThreadpMessageNum(g_MessageNumTlsIndex, &pMessageNum);
  157. if (SUCCEEDED(hRetval))
  158. {
  159. hRetval DBGCHK = SendMsg(s, cbBuf, pBuf) ? S_OK : GetLastErrorAsHResult();
  160. }
  161. if (SUCCEEDED(hRetval))
  162. {
  163. hRetval DBGCHK = ReceiveMsg(s, sizeof(cbWritten), &cbWritten, &cbRead);
  164. }
  165. if (SUCCEEDED(hRetval))
  166. {
  167. hRetval DBGCHK = (cbWritten == cbBuf) && (cbRead == sizeof(cbWritten)) ? S_OK : E_FAIL;
  168. }
  169. if (SUCCEEDED(hRetval))
  170. {
  171. CHAR szBanner[MAX_PATH] = {0};
  172. _snprintf(szBanner, sizeof(szBanner), "*******Message #%#x sent %#x bytes:*********", (*pMessageNum)++, cbWritten);
  173. DebugPrintHex(SSPI_MSG, szBanner, min(cbBuf, g_MsgHeaderLen), pBuf);
  174. }
  175. else
  176. {
  177. DebugPrintf(SSPI_ERROR, "cbWritten %#x, cbBuf %#x, cbRead %#x\n", cbWritten, cbBuf, cbRead);
  178. }
  179. return hRetval;
  180. }
  181. HRESULT
  182. GetPerThreadpMessageNum(
  183. IN ULONG Index,
  184. OUT ULONG** ppMessageNum
  185. )
  186. {
  187. THResult hRetval = E_FAIL;
  188. ULONG* pMsgNum = NULL;
  189. *ppMessageNum = NULL;
  190. hRetval DBGCHK = (TLS_OUT_OF_INDEXES != Index) ? S_OK : E_INVALIDARG;
  191. if (SUCCEEDED(hRetval))
  192. {
  193. pMsgNum = (ULONG*) TlsGetValue(g_MessageNumTlsIndex);
  194. }
  195. hRetval DBGCHK = pMsgNum ? S_OK : GetLastErrorAsHResult(); // last error can be NO_ERROR
  196. if (SUCCEEDED(hRetval) && !pMsgNum)
  197. {
  198. hRetval DBGCHK = E_POINTER;
  199. }
  200. if (SUCCEEDED(hRetval))
  201. {
  202. *ppMessageNum = pMsgNum;
  203. }
  204. return hRetval;
  205. }
  206. HRESULT
  207. ReadMessage(
  208. IN SOCKET s,
  209. IN ULONG cbBuf,
  210. IN OUT VOID* pBuf,
  211. OUT ULONG* pcbRead
  212. )
  213. {
  214. THResult hRetval = S_OK;
  215. ULONG* pMessageNum = NULL;
  216. hRetval DBGCHK = GetPerThreadpMessageNum(g_MessageNumTlsIndex, &pMessageNum);
  217. if (SUCCEEDED(hRetval))
  218. {
  219. hRetval DBGCHK = ReceiveMsg(s, cbBuf, pBuf, pcbRead) ? S_OK : GetLastErrorAsHResult();
  220. }
  221. if (SUCCEEDED(hRetval))
  222. {
  223. hRetval DBGCHK = SendMsg(s, sizeof(*pcbRead), pcbRead);
  224. }
  225. if (SUCCEEDED(hRetval))
  226. {
  227. CHAR szBanner[MAX_PATH] = {0};
  228. _snprintf(szBanner, sizeof(szBanner), "*********Message #%#x received %#x bytes:**********", (*pMessageNum)++, *pcbRead);
  229. DebugPrintHex(SSPI_MSG, szBanner, min(*pcbRead, g_MsgHeaderLen), pBuf);
  230. }
  231. return hRetval;
  232. }