main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "main.h"
  2. //#include "wac_network_connection_factory.h"
  3. #include "ServiceBuild.h"
  4. #include <openssl/ssl.h>
  5. #include "bfc/platform/export.h"
  6. #include "netinc.h"
  7. #include "api__wac_network.h"
  8. #include "wac_network_http_receiver_factory.h"
  9. #include "wac_network_connection_factory.h"
  10. #include "wac_network_ssl_connection_factory.h"
  11. #include "wac_network_dns_factory.h"
  12. #include "wac_network_web_server_factory.h"
  13. #include "util.h"
  14. #include "api/syscb/api_syscb.h"
  15. #include "wac_network_ssl_connection.h"
  16. #include <openssl/ssl.h>
  17. #include "bfc/platform/export.h"
  18. #include "../nu/nonewthrow.c"
  19. JNL_HTTPGetFactory HTTPGetService;
  20. JNL_ConnectionFactory connectionService;
  21. JNL_AsyncDNSFactory dnsService;
  22. JNL_WebServFactory webserverService;
  23. api_service *WASABI_API_SVC = Q_NULLPTR;
  24. api_application *WASABI_API_APP = Q_NULLPTR;
  25. api_config *AGAVE_API_CONFIG = Q_NULLPTR;
  26. api_threadpool *WASABI_API_THREADPOOL = Q_NULLPTR;
  27. api_syscb *WASABI_API_SYSCB = Q_NULLPTR;
  28. static wa::Components::WAC_Network_AsyncDNS *global_dns = NULL;
  29. wa::Components::WAC_Network_AsyncDNS *GetGlobalDNS()
  30. {
  31. if ( !global_dns )
  32. {
  33. if ( JNL::open_socketlib() )
  34. return NULL;
  35. global_dns = new wa::Components::WAC_Network_AsyncDNS;
  36. }
  37. return global_dns;
  38. }
  39. void DestroyGlobalDNS()
  40. {
  41. if ( global_dns )
  42. {
  43. delete global_dns;
  44. global_dns = NULL;
  45. JNL::close_socketlib();
  46. }
  47. }
  48. #ifdef USE_SSL
  49. #include <wincrypt.h>
  50. #include <openssl/rand.h>
  51. static HCRYPTPROV GetKeySet()
  52. {
  53. HCRYPTPROV hCryptProv;
  54. LPCWSTR UserName = L"WinampKeyContainer"; // name of the key container
  55. if ( CryptAcquireContext(
  56. &hCryptProv, // handle to the CSP
  57. UserName, // container name
  58. NULL, // use the default provider
  59. PROV_RSA_FULL, // provider type
  60. 0 ) ) // flag values
  61. {
  62. return hCryptProv;
  63. }
  64. else if ( CryptAcquireContext(
  65. &hCryptProv,
  66. UserName,
  67. NULL,
  68. PROV_RSA_FULL,
  69. CRYPT_NEWKEYSET ) )
  70. {
  71. return hCryptProv;
  72. }
  73. else
  74. return 0;
  75. }
  76. JNL_SSL_ConnectionFactory sslConnectionService;
  77. void InitSSL()
  78. {
  79. SSL_load_error_strings();
  80. SSL_library_init();
  81. HCRYPTPROV hCryptProv = GetKeySet();
  82. if ( hCryptProv )
  83. {
  84. BYTE pbData[ 8 * sizeof( unsigned long ) ] = { 0 };
  85. if ( CryptGenRandom( hCryptProv, 8 * sizeof( unsigned long ), pbData ) )
  86. {
  87. RAND_seed( pbData, 16 );
  88. }
  89. CryptReleaseContext( hCryptProv, 0 );
  90. }
  91. sslContext = SSL_CTX_new( SSLv23_client_method() );
  92. SSL_CTX_set_verify( sslContext, SSL_VERIFY_NONE, NULL );
  93. // SSL_CTX_set_session_cache_mode(sslContext, SSL_SESS_CACHE_OFF);
  94. }
  95. void QuitSSL()
  96. {
  97. SSL_CTX_free( sslContext );
  98. }
  99. #endif // !USE_SSL
  100. wa::Components::WAC_Network _wac_network;
  101. void wa::Components::WAC_Network::RegisterServices( api_service *p_service )
  102. {
  103. WASABI_API_SVC = p_service;
  104. ServiceBuild( WASABI_API_SYSCB, syscbApiServiceGuid );
  105. ServiceBuild( WASABI_API_APP, applicationApiServiceGuid );
  106. ServiceBuild( AGAVE_API_CONFIG, AgaveConfigGUID );
  107. ServiceBuild( WASABI_API_THREADPOOL, ThreadPoolGUID );
  108. WASABI_API_SVC->service_register( &HTTPGetService );
  109. WASABI_API_SVC->service_register( &connectionService );
  110. #ifdef USE_SSL
  111. WASABI_API_SVC->service_register( &sslConnectionService );
  112. #endif
  113. WASABI_API_SVC->service_register( &dnsService );
  114. WASABI_API_SVC->service_register( &webserverService );
  115. //dlMgrFactory.Register( &dlMgr );
  116. }
  117. void wa::Components::WAC_Network::DeregisterServices( api_service *p_service )
  118. {
  119. Q_UNUSED( p_service )
  120. ServiceRelease( WASABI_API_SYSCB, syscbApiServiceGuid );
  121. ServiceRelease( WASABI_API_APP, applicationApiServiceGuid );
  122. ServiceRelease( AGAVE_API_CONFIG, AgaveConfigGUID );
  123. //dlMgr.Kill();
  124. //dlMgrFactory.Deregister();
  125. DestroyGlobalDNS();
  126. p_service->service_deregister( &HTTPGetService );
  127. p_service->service_deregister( &connectionService );
  128. #ifdef USE_SSL
  129. p_service->service_deregister( &sslConnectionService );
  130. #endif
  131. p_service->service_deregister( &dnsService );
  132. #ifdef USE_SSL
  133. QuitSSL();
  134. #endif
  135. ServiceRelease( WASABI_API_THREADPOOL, ThreadPoolGUID );
  136. }
  137. extern "C" WAC_NETWORK_EXPORT ifc_wa5component * GetWinamp5SystemComponent()
  138. {
  139. return &_wac_network;
  140. }
  141. #ifdef CBCLASS
  142. #undef CBCLASS
  143. #endif
  144. #define CBCLASS wa::Components::WAC_Network
  145. START_DISPATCH;
  146. VCB( API_WA5COMPONENT_REGISTERSERVICES, RegisterServices )
  147. CB( API_WA5COMPONENT_REGISTERSERVICES_SAFE_MODE, RegisterServicesSafeModeOk )
  148. VCB( API_WA5COMPONENT_DEREEGISTERSERVICES, DeregisterServices )
  149. END_DISPATCH;
  150. #undef CBCLASS