aboutsummaryrefslogtreecommitdiff
path: root/Src/Components/wac_network/main.cpp
blob: ee5168055143ae9b918589d1e825c9f2c46741f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "main.h"

//#include "wac_network_connection_factory.h"
#include "ServiceBuild.h"

#include <openssl/ssl.h>

#include "bfc/platform/export.h"



#include "netinc.h"
#include "api__wac_network.h"

#include "wac_network_http_receiver_factory.h"
#include "wac_network_connection_factory.h"
#include "wac_network_ssl_connection_factory.h"
#include "wac_network_dns_factory.h"
#include "wac_network_web_server_factory.h"
#include "util.h"

#include "api/syscb/api_syscb.h"
#include "wac_network_ssl_connection.h"

#include <openssl/ssl.h>

#include "bfc/platform/export.h"
#include "../nu/nonewthrow.c"


JNL_HTTPGetFactory    HTTPGetService;
JNL_ConnectionFactory connectionService;
JNL_AsyncDNSFactory   dnsService;
JNL_WebServFactory    webserverService;




api_service     *WASABI_API_SVC        = Q_NULLPTR;
api_application *WASABI_API_APP        = Q_NULLPTR;
api_config      *AGAVE_API_CONFIG      = Q_NULLPTR;
api_threadpool  *WASABI_API_THREADPOOL = Q_NULLPTR;
api_syscb       *WASABI_API_SYSCB      = Q_NULLPTR;


static wa::Components::WAC_Network_AsyncDNS *global_dns = NULL;

wa::Components::WAC_Network_AsyncDNS *GetGlobalDNS()
{
	if ( !global_dns )
	{
		if ( JNL::open_socketlib() )
			return NULL;

		global_dns = new wa::Components::WAC_Network_AsyncDNS;
	}

	return global_dns;
}

void DestroyGlobalDNS()
{
	if ( global_dns )
	{
		delete global_dns;
		global_dns = NULL;

		JNL::close_socketlib();
	}
}


#ifdef USE_SSL
#include <wincrypt.h>
#include <openssl/rand.h>

static HCRYPTPROV GetKeySet()
{
	HCRYPTPROV   hCryptProv;
	LPCWSTR UserName = L"WinampKeyContainer";  // name of the key container

	if ( CryptAcquireContext(
		&hCryptProv,               // handle to the CSP
		UserName,                  // container name
		NULL,                      // use the default provider
		PROV_RSA_FULL,             // provider type
		0 ) )                        // flag values
	{
		return hCryptProv;
	}
	else if ( CryptAcquireContext(
		&hCryptProv,
		UserName,
		NULL,
		PROV_RSA_FULL,
		CRYPT_NEWKEYSET ) )
	{
		return hCryptProv;
	}
	else
		return 0;

}

JNL_SSL_ConnectionFactory sslConnectionService;

void InitSSL()
{
	SSL_load_error_strings();
	SSL_library_init();

	HCRYPTPROV hCryptProv = GetKeySet();
	if ( hCryptProv )
	{
		BYTE pbData[ 8 * sizeof( unsigned long ) ] = { 0 };
		if ( CryptGenRandom( hCryptProv, 8 * sizeof( unsigned long ), pbData ) )
		{
			RAND_seed( pbData, 16 );
		}

		CryptReleaseContext( hCryptProv, 0 );
	}

	sslContext = SSL_CTX_new( SSLv23_client_method() );

	SSL_CTX_set_verify( sslContext, SSL_VERIFY_NONE, NULL );
	//	SSL_CTX_set_session_cache_mode(sslContext, SSL_SESS_CACHE_OFF);
}

void QuitSSL()
{
	SSL_CTX_free( sslContext );
}

#endif  // !USE_SSL



wa::Components::WAC_Network _wac_network;


void wa::Components::WAC_Network::RegisterServices( api_service *p_service )
{
	WASABI_API_SVC = p_service;

	ServiceBuild( WASABI_API_SYSCB,      syscbApiServiceGuid );
	ServiceBuild( WASABI_API_APP,        applicationApiServiceGuid );
	ServiceBuild( AGAVE_API_CONFIG,      AgaveConfigGUID );
	ServiceBuild( WASABI_API_THREADPOOL, ThreadPoolGUID );


	WASABI_API_SVC->service_register( &HTTPGetService );
	WASABI_API_SVC->service_register( &connectionService );

#ifdef USE_SSL
	WASABI_API_SVC->service_register( &sslConnectionService );
#endif

	WASABI_API_SVC->service_register( &dnsService );
	WASABI_API_SVC->service_register( &webserverService );
	//dlMgrFactory.Register( &dlMgr );
}

void wa::Components::WAC_Network::DeregisterServices( api_service *p_service )
{
	Q_UNUSED( p_service )

	ServiceRelease( WASABI_API_SYSCB,      syscbApiServiceGuid );

	ServiceRelease( WASABI_API_APP,        applicationApiServiceGuid );
	ServiceRelease( AGAVE_API_CONFIG,      AgaveConfigGUID );


	//dlMgr.Kill();
	//dlMgrFactory.Deregister();
	DestroyGlobalDNS();
	p_service->service_deregister( &HTTPGetService );
	p_service->service_deregister( &connectionService );

#ifdef USE_SSL
	p_service->service_deregister( &sslConnectionService );
#endif

	p_service->service_deregister( &dnsService );

#ifdef USE_SSL
	QuitSSL();
#endif

	ServiceRelease( WASABI_API_THREADPOOL, ThreadPoolGUID );
}


extern "C" WAC_NETWORK_EXPORT ifc_wa5component * GetWinamp5SystemComponent()
{
	return &_wac_network;
}


#ifdef CBCLASS
#undef CBCLASS
#endif

#define CBCLASS wa::Components::WAC_Network
START_DISPATCH;
VCB( API_WA5COMPONENT_REGISTERSERVICES,           RegisterServices )
CB(  API_WA5COMPONENT_REGISTERSERVICES_SAFE_MODE, RegisterServicesSafeModeOk )
VCB( API_WA5COMPONENT_DEREEGISTERSERVICES,        DeregisterServices )
END_DISPATCH;
#undef CBCLASS