summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_network.cpp
blob: 1090b75e0cd2793ea5a8cf854c0ee396e1dcca9b (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "stdafx.h"

bool CToxProto::IsOnline()
{
	return toxThread && toxThread->IsConnected() && m_iStatus >= ID_STATUS_ONLINE;
}

void CToxProto::BootstrapNode(const char *address, int port, const char *hexKey)
{
	if (hexKey == NULL || toxThread == NULL)
		return;

	ToxBinAddress binKey(hexKey, TOX_PUBLIC_KEY_SIZE * 2);
	TOX_ERR_BOOTSTRAP error;
	if (!tox_bootstrap(toxThread->Tox(), address, port, binKey, &error))
		logger->Log(__FUNCTION__ ": failed to bootstrap node %s:%d \"%s\" (%d)", address, port, hexKey, error);
	if (!tox_add_tcp_relay(toxThread->Tox(), address, port, binKey, &error))
		logger->Log(__FUNCTION__ ": failed to add tcp relay %s:%d \"%s\" (%d)", address, port, hexKey, error);
}

void CToxProto::BootstrapNodesFromDb(bool isIPv6)
{
	char module[MAX_PATH];
	mir_snprintf(module, "%s_Nodes", m_szModuleName);
	int nodeCount = db_get_w(NULL, module, TOX_SETTINGS_NODE_COUNT, 0);
	if (nodeCount > 0)
	{
		char setting[MAX_PATH];
		for (int i = 0; i < nodeCount; i++)
		{
			mir_snprintf(setting, TOX_SETTINGS_NODE_IPV4, i);
			ptrA address(db_get_sa(NULL, module, setting));
			mir_snprintf(setting, TOX_SETTINGS_NODE_PORT, i);
			int port = db_get_w(NULL, module, setting, 33445);
			mir_snprintf(setting, TOX_SETTINGS_NODE_PKEY, i);
			ptrA pubKey(db_get_sa(NULL, module, setting));
			BootstrapNode(address, port, pubKey);
			if (isIPv6)
			{
				mir_snprintf(setting, TOX_SETTINGS_NODE_IPV6, i);
				address = db_get_sa(NULL, module, setting);
				BootstrapNode(address, port, pubKey);
			}
		}
	}
}

void CToxProto::BootstrapNodesFromJson(bool isIPv6)
{
	char *json = NULL;

	ptrT path(mir_tstrdup((TCHAR*)VARST(_T(TOX_JSON_PATH))));

	if (!IsFileExists(path))
		UpdateNodes();

	if (IsFileExists(path))
	{
		FILE *hFile = _tfopen(path, L"r");
		if (hFile != NULL)
		{
			_fseeki64(hFile, 0, SEEK_END);
			size_t size = _ftelli64(hFile);
			json = (char*)mir_calloc(size);
			rewind(hFile);
			fread(json, sizeof(char), size, hFile);
			fclose(hFile);
		}
	}

	if (json)
	{
		JSONNode root = JSONNode::parse(json);
		if (!root.empty())
		{
			JSONNode nodes = root.at("nodes").as_array();
			for (size_t i = 0; i < nodes.size(); i++)
			{
				JSONNode node = nodes[i];

				JSONNode address = node.at("ipv4");
				int port = node.at("port").as_int();
				JSONNode pubKey = node.at("public_key");
				BootstrapNode(address.as_string().c_str(), port, pubKey.as_string().c_str());
				if (isIPv6)
				{
					address = node.at("ipv6");
					BootstrapNode(address.as_string().c_str(), port, pubKey.as_string().c_str());
				}
			}
		}
	}
}

void CToxProto::BootstrapNodes()
{
	logger->Log(__FUNCTION__": bootstraping DHT");
	bool isIPv6 = getBool("EnableIPv6", 0);
	BootstrapNodesFromDb(isIPv6);
	BootstrapNodesFromJson(isIPv6);
}

void CToxProto::UpdateNodes()
{
	ptrT path(mir_tstrdup((TCHAR*)VARST(_T(TOX_JSON_PATH))));

	if (!IsFileExists(path))
	{
		HANDLE hProfile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
		if (hProfile == NULL)
		{
			logger->Log(__FUNCTION__": failed to create tox.json");
			return;
		}
		CloseHandle(hProfile);
	}

	HttpRequest request(REQUEST_GET, "https://nodes.tox.chat/json");
	NLHR_PTR response(request.Send(hNetlib));

	if (response->resultCode == 200 && response->pData)
	{
		FILE *hFile = _tfopen(path, _T("w"));
		if (hFile)
		{
			fwrite(response->pData, sizeof(char), response->dataLength, hFile);
			fclose(hFile);
		}
	}
}

void CToxProto::TryConnect()
{
	if (toxThread != NULL)
	{
		if (tox_self_get_connection_status(toxThread->Tox()) != TOX_CONNECTION_NONE)
		{
			toxThread->Connect();
			logger->Log(__FUNCTION__": successfuly connected to DHT");

			ForkThread(&CToxProto::LoadFriendList, NULL);

			m_iStatus = m_iDesiredStatus;
			ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_CONNECTING, m_iStatus);
			tox_self_set_status(toxThread->Tox(), MirandaToToxStatus(m_iStatus));
			logger->Log(__FUNCTION__": changing status from %i to %i", ID_STATUS_CONNECTING, m_iDesiredStatus);
		}
		else if (m_iStatus++ > TOX_MAX_CONNECT_RETRIES)
		{
			SetStatus(ID_STATUS_OFFLINE);
			ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGINERR_NONETWORK);
			logger->Log(__FUNCTION__": failed to connect to DHT");
		}
	}
}

void CToxProto::CheckConnection(int &retriesCount)
{
	if (!toxThread || !toxThread->IsConnected())
	{
		TryConnect();
	}
	else if (tox_self_get_connection_status(toxThread->Tox()) != TOX_CONNECTION_NONE)
	{
		if (retriesCount < TOX_MAX_DISCONNECT_RETRIES)
		{
			logger->Log(__FUNCTION__": restored connection with DHT");
			retriesCount = TOX_MAX_DISCONNECT_RETRIES;
		}
	}
	else
	{
		if (retriesCount == TOX_MAX_DISCONNECT_RETRIES)
		{
			retriesCount--;
			logger->Log(__FUNCTION__": lost connection with DHT");
		}
		else if (retriesCount % 50 == 0)
		{
			retriesCount--;
			BootstrapNodes();
		}
		else if (!(--retriesCount))
		{
			toxThread->Disconnect();
			logger->Log(__FUNCTION__": disconnected from DHT");
			SetStatus(ID_STATUS_OFFLINE);
		}
	}
}

void CToxProto::PollingThread(void*)
{
	Tox_Options *options = GetToxOptions();
	if (!options)
	{
		SetStatus(ID_STATUS_OFFLINE);
		ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL);
		logger->Log(__FUNCTION__": leaving");
		return;
	}

	TOX_ERR_NEW error;
	CToxThread toxThread(options, &error);
	if (error != TOX_ERR_NEW_OK)
	{
		logger->Log(__FUNCTION__": failed to initialize tox core (%d)", error);
		ShowNotification(ToxErrorToString(error), TranslateT("Unable to initialize Tox core"), MB_ICONERROR);
		tox_options_free(options);
	}
	tox_options_free(options);

	this->toxThread = &toxThread;

	logger->Log(__FUNCTION__": entering");

	if (!InitToxCore(&toxThread))
	{
		UninitToxCore(&toxThread);
		SetStatus(ID_STATUS_OFFLINE);
		ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, NULL, LOGINERR_WRONGPASSWORD);
		logger->Log(__FUNCTION__": leaving");
		return;
	}

	int retriesCount = TOX_MAX_DISCONNECT_RETRIES;
	BootstrapNodes();

	while (!toxThread.IsTerminated())
	{
		CheckConnection(retriesCount);
		toxThread.Iterate();
	}

	this->toxThread = NULL;

	toxThread.Disconnect();
	UninitToxCore(&toxThread);

	logger->Log(__FUNCTION__": leaving");
}