From da6afd3553ac4e189071efbffb27cc0c9c951dff Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 19 Feb 2015 20:12:41 +0000 Subject: Tox: - twiked connection algorithm - added ability to load nodes from ini automatically git-svn-id: http://svn.miranda-ng.org/main/trunk@12202 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/common.h | 14 +++-- protocols/Tox/src/tox_network.cpp | 124 +++++++++++++++++++++++++------------- protocols/Tox/src/tox_proto.h | 2 + 3 files changed, 92 insertions(+), 48 deletions(-) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/common.h b/protocols/Tox/src/common.h index 13aadf38aa..1d75169087 100644 --- a/protocols/Tox/src/common.h +++ b/protocols/Tox/src/common.h @@ -46,18 +46,22 @@ extern HINSTANCE g_hInstance; #define TOX_ERROR -1 +#define TOX_MAX_CONNECT_RETRIES 200 #define TOX_MAX_DISCONNECT_RETRIES 100 +#define TOX_INI_PATH "%miranda_path%\\Plugins\\tox.ini" + #define TOX_SETTINGS_ID "ToxID" #define TOX_SETTINGS_DNS "DnsID" #define TOX_SETTINGS_GROUP "DefaultGroup" #define TOX_SETTINGS_AVATAR_HASH "AvatarHash" -#define TOX_SETTINGS_NODE_IPV4 "Node_%d_IPv4" -#define TOX_SETTINGS_NODE_IPV6 "Node_%d_IPv6" -#define TOX_SETTINGS_NODE_PORT "Node_%d_Port" -#define TOX_SETTINGS_NODE_PKEY "Node_%d_PubKey" -#define TOX_SETTINGS_NODE_COUNT "NodeCount" +#define TOX_SETTINGS_NODE_PREFIX "Node_" +#define TOX_SETTINGS_NODE_IPV4 TOX_SETTINGS_NODE_PREFIX"%d_IPv4" +#define TOX_SETTINGS_NODE_IPV6 TOX_SETTINGS_NODE_PREFIX"%d_IPv6" +#define TOX_SETTINGS_NODE_PORT TOX_SETTINGS_NODE_PREFIX"%d_Port" +#define TOX_SETTINGS_NODE_PKEY TOX_SETTINGS_NODE_PREFIX"%d_PubKey" +#define TOX_SETTINGS_NODE_COUNT TOX_SETTINGS_NODE_PREFIX"Count" #define TOX_DB_EVENT_TYPE_ACTION 10001 diff --git a/protocols/Tox/src/tox_network.cpp b/protocols/Tox/src/tox_network.cpp index 588a41a8fc..dcc7f39153 100644 --- a/protocols/Tox/src/tox_network.cpp +++ b/protocols/Tox/src/tox_network.cpp @@ -5,23 +5,11 @@ bool CToxProto::IsOnline() return isConnected && m_iStatus > ID_STATUS_OFFLINE; } -void CToxProto::BootstrapNodes() +int CToxProto::BootstrapNodesFromDb(bool isIPv6) { - debugLogA("CToxProto::BootstrapDht: bootstraping DHT"); - - bool isIPv6 = !getBool("DisableIPv6", 0); + int nodesLoaded = 0; int nodeCount = db_get_w(NULL, MODULE, TOX_SETTINGS_NODE_COUNT, 0); - if (!nodeCount) - { - tox_bootstrap_from_address( - tox, "192.254.75.102", 33445, - ToxBinAddress("951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F")); - if (isIPv6) - tox_bootstrap_from_address( - tox, "2607:5600:284::2", 33445, - ToxBinAddress("951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F")); - } - else + if (nodeCount > 0) { char setting[MAX_PATH]; for (int i = 0; i < nodeCount; i++) @@ -32,15 +20,71 @@ void CToxProto::BootstrapNodes() int port = db_get_w(NULL, MODULE, setting, 33445); mir_snprintf(setting, SIZEOF(setting), TOX_SETTINGS_NODE_PKEY, i); ptrA pubKey(db_get_sa(NULL, MODULE, setting)); - tox_bootstrap_from_address(tox, address, port, ToxBinAddress(pubKey)); + nodesLoaded += tox_bootstrap_from_address(tox, address, port, ToxBinAddress(pubKey)); if (isIPv6) { mir_snprintf(setting, SIZEOF(setting), TOX_SETTINGS_NODE_IPV6, i); address = db_get_sa(NULL, MODULE, setting); - tox_bootstrap_from_address(tox, address, port, ToxBinAddress(pubKey)); + nodesLoaded += tox_bootstrap_from_address(tox, address, port, ToxBinAddress(pubKey)); } } } + return nodesLoaded; +} + +int CToxProto::BootstrapNodesFromIni(bool isIPv6) +{ + int nodesLoaded = 0; + if (IsFileExists((TCHAR*)VARST(_T(TOX_INI_PATH)))) + { + char fileName[MAX_PATH]; + mir_strcpy(fileName, VARS(TOX_INI_PATH)); + + char *section, sections[MAX_PATH], value[MAX_PATH]; + GetPrivateProfileSectionNamesA(sections, SIZEOF(sections), fileName); + section = sections; + while (*section != NULL) + { + if (strstr(section, TOX_SETTINGS_NODE_PREFIX) == section) + { + GetPrivateProfileStringA(section, "IPv4", NULL, value, SIZEOF(value), fileName); + ptrA address(mir_strdup(value)); + int port = GetPrivateProfileIntA(section, "Port", 33445, fileName); + GetPrivateProfileStringA(section, "PubKey", NULL, value, SIZEOF(value), fileName); + ToxBinAddress pubKey(value); + nodesLoaded += tox_bootstrap_from_address(tox, address, port, pubKey); + if (isIPv6) + { + GetPrivateProfileStringA(section, "IPv6", NULL, value, SIZEOF(value), fileName); + address = mir_strdup(value); + nodesLoaded += tox_bootstrap_from_address(tox, address, port, pubKey); + } + } + section += strlen(section) + 1; + } + } + return nodesLoaded; +} + +void CToxProto::BootstrapNodes() +{ + debugLogA("CToxProto::BootstrapDht: bootstraping DHT"); + bool isIPv6 = !getBool("DisableIPv6", 0); + int nodesLoaded = + BootstrapNodesFromDb(isIPv6) + + BootstrapNodesFromIni(isIPv6); + if (!nodesLoaded) + { + tox_bootstrap_from_address( + tox, "192.254.75.102", 33445, + ToxBinAddress("951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F")); + if (isIPv6) + { + tox_bootstrap_from_address( + tox, "2607:5600:284::2", 33445, + ToxBinAddress("951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F")); + } + } } void CToxProto::TryConnect() @@ -57,14 +101,11 @@ void CToxProto::TryConnect() tox_set_user_status(tox, MirandaToToxStatus(m_iStatus)); debugLogA("CToxProto::PollingThread: changing status from %i to %i", ID_STATUS_CONNECTING, m_iDesiredStatus); } - else + else if (m_iStatus++ > TOX_MAX_CONNECT_RETRIES) { - if (m_iStatus++ > MAX_CONNECT_RETRIES) - { - SetStatus(ID_STATUS_OFFLINE); - ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, (HANDLE)NULL, LOGINERR_NONETWORK); - debugLogA("CToxProto::PollingThread: failed to connect to DHT"); - } + SetStatus(ID_STATUS_OFFLINE); + ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, (HANDLE)NULL, LOGINERR_NONETWORK); + debugLogA("CToxProto::PollingThread: failed to connect to DHT"); } } @@ -72,32 +113,29 @@ void CToxProto::CheckConnection(int &retriesCount) { if (!isConnected) { - debugLogA("CToxProto::CheckConnection: lost connection with DHT"); TryConnect(); } + else if (tox_isconnected(tox)) + { + if (retriesCount < TOX_MAX_DISCONNECT_RETRIES) + { + debugLogA("CToxProto::CheckConnection: restored connection with DHT"); + retriesCount = TOX_MAX_DISCONNECT_RETRIES; + } + } else { - if (tox_isconnected(tox)) + if (retriesCount == TOX_MAX_DISCONNECT_RETRIES - 10) { - if (retriesCount < TOX_MAX_DISCONNECT_RETRIES) - { - debugLogA("CToxProto::CheckConnection: restored connection with DHT"); - retriesCount = TOX_MAX_DISCONNECT_RETRIES; - } + debugLogA("CToxProto::CheckConnection: lost connection with DHT"); + retriesCount--; + BootstrapNodes(); } - else + else if (!(--retriesCount)) { - if (retriesCount == TOX_MAX_DISCONNECT_RETRIES) - { - retriesCount --; - BootstrapNodes(); - } - else if (!(--retriesCount)) - { - isConnected = false; - debugLogA("CToxProto::CheckConnection: disconnected from DHT"); - SetStatus(ID_STATUS_OFFLINE); - } + isConnected = false; + debugLogA("CToxProto::CheckConnection: disconnected from DHT"); + SetStatus(ID_STATUS_OFFLINE); } } } diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 2dfec2ba12..047969eca2 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -91,6 +91,8 @@ private: // tox network bool IsOnline(); + int BootstrapNodesFromDb(bool isIPv6); + int BootstrapNodesFromIni(bool isIPv6); void BootstrapNodes(); void TryConnect(); void CheckConnection(int &retriesCount); -- cgit v1.2.3