开源的socket服务端客户端,支持C# C++

HPSocket4C-SSL.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 4.1.3
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #include "stdafx.h"
  25. #include "HPSocket4C-SSL.h"
  26. #include "SocketObject4C.h"
  27. #include "TcpServer.h"
  28. #include "TcpClient.h"
  29. #include "TcpAgent.h"
  30. #include "TcpPullServer.h"
  31. #include "TcpPullClient.h"
  32. #include "TcpPullAgent.h"
  33. #include "TcpPackServer.h"
  34. #include "TcpPackClient.h"
  35. #include "TcpPackAgent.h"
  36. #include "HttpServer.h"
  37. #include "HttpAgent.h"
  38. #include "HttpClient.h"
  39. #if !defined(_WIN64) && !defined(HPSOCKET_STATIC_LIB)
  40. #pragma comment(linker, "/EXPORT:Create_HP_SSLAgent=_Create_HP_SSLAgent@4")
  41. #pragma comment(linker, "/EXPORT:Create_HP_SSLClient=_Create_HP_SSLClient@4")
  42. #pragma comment(linker, "/EXPORT:Create_HP_SSLPullAgent=_Create_HP_SSLPullAgent@4")
  43. #pragma comment(linker, "/EXPORT:Create_HP_SSLPullClient=_Create_HP_SSLPullClient@4")
  44. #pragma comment(linker, "/EXPORT:Create_HP_SSLPullServer=_Create_HP_SSLPullServer@4")
  45. #pragma comment(linker, "/EXPORT:Create_HP_SSLPackAgent=_Create_HP_SSLPackAgent@4")
  46. #pragma comment(linker, "/EXPORT:Create_HP_SSLPackClient=_Create_HP_SSLPackClient@4")
  47. #pragma comment(linker, "/EXPORT:Create_HP_SSLPackServer=_Create_HP_SSLPackServer@4")
  48. #pragma comment(linker, "/EXPORT:Create_HP_SSLServer=_Create_HP_SSLServer@4")
  49. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLAgent=_Destroy_HP_SSLAgent@4")
  50. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLClient=_Destroy_HP_SSLClient@4")
  51. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLPullAgent=_Destroy_HP_SSLPullAgent@4")
  52. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLPullClient=_Destroy_HP_SSLPullClient@4")
  53. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLPullServer=_Destroy_HP_SSLPullServer@4")
  54. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLPackAgent=_Destroy_HP_SSLPackAgent@4")
  55. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLPackClient=_Destroy_HP_SSLPackClient@4")
  56. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLPackServer=_Destroy_HP_SSLPackServer@4")
  57. #pragma comment(linker, "/EXPORT:Destroy_HP_SSLServer=_Destroy_HP_SSLServer@4")
  58. #pragma comment(linker, "/EXPORT:Create_HP_HttpsAgent=_Create_HP_HttpsAgent@4")
  59. #pragma comment(linker, "/EXPORT:Create_HP_HttpsClient=_Create_HP_HttpsClient@4")
  60. #pragma comment(linker, "/EXPORT:Create_HP_HttpsServer=_Create_HP_HttpsServer@4")
  61. #pragma comment(linker, "/EXPORT:Create_HP_HttpsSyncClient=_Create_HP_HttpsSyncClient@0")
  62. #pragma comment(linker, "/EXPORT:Destroy_HP_HttpsAgent=_Destroy_HP_HttpsAgent@4")
  63. #pragma comment(linker, "/EXPORT:Destroy_HP_HttpsClient=_Destroy_HP_HttpsClient@4")
  64. #pragma comment(linker, "/EXPORT:Destroy_HP_HttpsServer=_Destroy_HP_HttpsServer@4")
  65. #pragma comment(linker, "/EXPORT:Destroy_HP_HttpsSyncClient=_Destroy_HP_HttpsSyncClient@4")
  66. #pragma comment(linker, "/EXPORT:HP_SSL_Initialize=_HP_SSL_Initialize@28")
  67. #pragma comment(linker, "/EXPORT:HP_SSL_AddServerContext=_HP_SSL_AddServerContext@20")
  68. #pragma comment(linker, "/EXPORT:HP_SSL_Cleanup=_HP_SSL_Cleanup@0")
  69. #pragma comment(linker, "/EXPORT:HP_SSL_RemoveThreadLocalState=_HP_SSL_RemoveThreadLocalState@4")
  70. #pragma comment(linker, "/EXPORT:HP_SSL_IsValid=_HP_SSL_IsValid@0")
  71. #endif
  72. /*****************************************************************************************************************************************************/
  73. /******************************************************************** SSL Exports ********************************************************************/
  74. /*****************************************************************************************************************************************************/
  75. typedef C_HP_ObjectT<CSSLServer, ITcpServerListener> C_HP_SSLServer;
  76. typedef C_HP_ObjectT<CSSLPullServer, ITcpServerListener, sizeof(IPullSocket)> C_HP_SSLPullServer;
  77. typedef C_HP_ObjectT<CSSLPackServer, ITcpServerListener, sizeof(IPackSocket)> C_HP_SSLPackServer;
  78. typedef C_HP_ObjectT<CSSLAgent, ITcpAgentListener> C_HP_SSLAgent;
  79. typedef C_HP_ObjectT<CSSLPullAgent, ITcpAgentListener, sizeof(IPullSocket)> C_HP_SSLPullAgent;
  80. typedef C_HP_ObjectT<CSSLPackAgent, ITcpAgentListener, sizeof(IPackSocket)> C_HP_SSLPackAgent;
  81. typedef C_HP_ObjectT<CSSLClient, ITcpClientListener> C_HP_SSLClient;
  82. typedef C_HP_ObjectT<CSSLPullClient, ITcpClientListener, sizeof(IPullClient)> C_HP_SSLPullClient;
  83. typedef C_HP_ObjectT<CSSLPackClient, ITcpClientListener, sizeof(IPackClient)> C_HP_SSLPackClient;
  84. /********************************************************/
  85. /************** HPSocket4C-SSL 对象创建函数 **************/
  86. HPSOCKET_API HP_SSLServer __stdcall Create_HP_SSLServer(HP_TcpServerListener pListener)
  87. {
  88. return (HP_SSLServer)(new C_HP_SSLServer((ITcpServerListener*)pListener));
  89. }
  90. HPSOCKET_API HP_SSLAgent __stdcall Create_HP_SSLAgent(HP_TcpAgentListener pListener)
  91. {
  92. return (HP_SSLAgent)(new C_HP_SSLAgent((ITcpAgentListener*)pListener));
  93. }
  94. HPSOCKET_API HP_SSLClient __stdcall Create_HP_SSLClient(HP_TcpClientListener pListener)
  95. {
  96. return (HP_SSLClient)(new C_HP_SSLClient((ITcpClientListener*)pListener));
  97. }
  98. HPSOCKET_API HP_SSLPullServer __stdcall Create_HP_SSLPullServer(HP_TcpPullServerListener pListener)
  99. {
  100. return (HP_SSLPullServer)(new C_HP_SSLPullServer((ITcpServerListener*)pListener));
  101. }
  102. HPSOCKET_API HP_SSLPullAgent __stdcall Create_HP_SSLPullAgent(HP_TcpPullAgentListener pListener)
  103. {
  104. return (HP_SSLPullAgent)(new C_HP_SSLPullAgent((ITcpAgentListener*)pListener));
  105. }
  106. HPSOCKET_API HP_SSLPullClient __stdcall Create_HP_SSLPullClient(HP_TcpPullClientListener pListener)
  107. {
  108. return (HP_SSLPullClient)(new C_HP_SSLPullClient((ITcpClientListener*)pListener));
  109. }
  110. HPSOCKET_API HP_SSLPackServer __stdcall Create_HP_SSLPackServer(HP_TcpServerListener pListener)
  111. {
  112. return (HP_SSLPackServer)(new C_HP_SSLPackServer((ITcpServerListener*)pListener));
  113. }
  114. HPSOCKET_API HP_SSLPackAgent __stdcall Create_HP_SSLPackAgent(HP_TcpAgentListener pListener)
  115. {
  116. return (HP_SSLPackAgent)(new C_HP_SSLPackAgent((ITcpAgentListener*)pListener));
  117. }
  118. HPSOCKET_API HP_SSLPackClient __stdcall Create_HP_SSLPackClient(HP_TcpClientListener pListener)
  119. {
  120. return (HP_SSLPackClient)(new C_HP_SSLPackClient((ITcpClientListener*)pListener));
  121. }
  122. HPSOCKET_API void __stdcall Destroy_HP_SSLServer(HP_SSLServer pServer)
  123. {
  124. delete (C_HP_SSLServer*)pServer;
  125. }
  126. HPSOCKET_API void __stdcall Destroy_HP_SSLAgent(HP_SSLAgent pAgent)
  127. {
  128. delete (C_HP_SSLAgent*)pAgent;
  129. }
  130. HPSOCKET_API void __stdcall Destroy_HP_SSLClient(HP_SSLClient pClient)
  131. {
  132. delete (C_HP_SSLClient*)pClient;
  133. }
  134. HPSOCKET_API void __stdcall Destroy_HP_SSLPullServer(HP_SSLPullServer pServer)
  135. {
  136. delete (C_HP_SSLPullServer*)pServer;
  137. }
  138. HPSOCKET_API void __stdcall Destroy_HP_SSLPullAgent(HP_SSLPullAgent pAgent)
  139. {
  140. delete (C_HP_SSLPullAgent*)pAgent;
  141. }
  142. HPSOCKET_API void __stdcall Destroy_HP_SSLPullClient(HP_SSLPullClient pClient)
  143. {
  144. delete (C_HP_SSLPullClient*)pClient;
  145. }
  146. HPSOCKET_API void __stdcall Destroy_HP_SSLPackServer(HP_SSLPackServer pServer)
  147. {
  148. delete (C_HP_SSLPackServer*)pServer;
  149. }
  150. HPSOCKET_API void __stdcall Destroy_HP_SSLPackAgent(HP_SSLPackAgent pAgent)
  151. {
  152. delete (C_HP_SSLPackAgent*)pAgent;
  153. }
  154. HPSOCKET_API void __stdcall Destroy_HP_SSLPackClient(HP_SSLPackClient pClient)
  155. {
  156. delete (C_HP_SSLPackClient*)pClient;
  157. }
  158. /*****************************************************************************************************************************************************/
  159. /******************************************************************** HTTPS Exports ******************************************************************/
  160. /*****************************************************************************************************************************************************/
  161. typedef C_HP_ObjectT<CHttpsServer, IHttpServerListener, sizeof(IComplexHttpResponder)> C_HP_HttpsServer;
  162. typedef C_HP_ObjectT<CHttpsAgent, IHttpAgentListener, sizeof(IComplexHttpRequester)> C_HP_HttpsAgent;
  163. typedef C_HP_ObjectT<CHttpsClient, IHttpClientListener, sizeof(IHttpRequester)> C_HP_HttpsClient;
  164. typedef C_HP_ObjectT<CHttpsSyncClient, void, sizeof(IHttpSyncRequester)> C_HP_HttpsSyncClient;
  165. /****************************************************/
  166. /**************** HTTPS 对象创建函数 *****************/
  167. HPSOCKET_API HP_HttpsServer __stdcall Create_HP_HttpsServer(HP_HttpServerListener pListener)
  168. {
  169. return (HP_HttpsServer)(new C_HP_HttpsServer((IHttpServerListener*)pListener));
  170. }
  171. HPSOCKET_API HP_HttpsAgent __stdcall Create_HP_HttpsAgent(HP_HttpAgentListener pListener)
  172. {
  173. return (HP_HttpsAgent)(new C_HP_HttpsAgent((IHttpAgentListener*)pListener));
  174. }
  175. HPSOCKET_API HP_HttpsClient __stdcall Create_HP_HttpsClient(HP_HttpClientListener pListener)
  176. {
  177. return (HP_HttpsClient)(new C_HP_HttpsClient((IHttpClientListener*)pListener));
  178. }
  179. HPSOCKET_API HP_HttpsSyncClient __stdcall Create_HP_HttpsSyncClient()
  180. {
  181. return (HP_HttpsSyncClient)(new C_HP_HttpsSyncClient());
  182. }
  183. HPSOCKET_API void __stdcall Destroy_HP_HttpsServer(HP_HttpsServer pServer)
  184. {
  185. delete (C_HP_HttpsServer*)pServer;
  186. }
  187. HPSOCKET_API void __stdcall Destroy_HP_HttpsAgent(HP_HttpsAgent pAgent)
  188. {
  189. delete (C_HP_HttpsAgent*)pAgent;
  190. }
  191. HPSOCKET_API void __stdcall Destroy_HP_HttpsClient(HP_HttpsClient pClient)
  192. {
  193. delete (C_HP_HttpsClient*)pClient;
  194. }
  195. HPSOCKET_API void __stdcall Destroy_HP_HttpsSyncClient(HP_HttpsSyncClient pClient)
  196. {
  197. delete (C_HP_HttpsSyncClient*)pClient;
  198. }
  199. /*****************************************************************************************************************************************************/
  200. /*************************************************************** Global Function Exports *************************************************************/
  201. /*****************************************************************************************************************************************************/
  202. HPSOCKET_API BOOL __stdcall HP_SSL_Initialize(En_HP_SSLSessionMode enSessionMode, int iVerifyMode, LPCTSTR lpszPemCertFile, LPCTSTR lpszPemKeyFile, LPCTSTR lpszKeyPasswod, LPCTSTR lpszCAPemCertFileOrPath, HP_Fn_SNI_ServerNameCallback fnServerNameCallback)
  203. {
  204. return g_SSL.Initialize(enSessionMode, iVerifyMode, lpszPemCertFile, lpszPemKeyFile, lpszKeyPasswod, lpszCAPemCertFileOrPath, fnServerNameCallback);
  205. }
  206. HPSOCKET_API int __stdcall HP_SSL_AddServerContext(int iVerifyMode, LPCTSTR lpszPemCertFile, LPCTSTR lpszPemKeyFile, LPCTSTR lpszKeyPasswod, LPCTSTR lpszCAPemCertFileOrPath)
  207. {
  208. return g_SSL.AddServerContext(iVerifyMode, lpszPemCertFile, lpszPemKeyFile, lpszKeyPasswod, lpszCAPemCertFileOrPath);
  209. }
  210. HPSOCKET_API void __stdcall HP_SSL_Cleanup()
  211. {
  212. g_SSL.Cleanup();
  213. }
  214. HPSOCKET_API void __stdcall HP_SSL_RemoveThreadLocalState(DWORD dwThreadID)
  215. {
  216. g_SSL.RemoveThreadLocalState(dwThreadID);
  217. }
  218. HPSOCKET_API BOOL __stdcall HP_SSL_IsValid()
  219. {
  220. return g_SSL.IsValid();
  221. }