From 681ba8556e859f38a0d7c910e9d40a9a917b3280 Mon Sep 17 00:00:00 2001 From: Kirill Volinsky Date: Sat, 9 Aug 2014 16:01:49 +0000 Subject: in debug you can see that it connects to network git-svn-id: http://svn.miranda-ng.org/main/trunk@10137 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/common.h | 1 + protocols/Tox/src/tox_proto.cpp | 68 ++++++++++++++++++++++++++++++++++++++--- protocols/Tox/src/tox_proto.h | 2 +- protocols/Tox/src/tox_utils.cpp | 29 ++++++------------ 4 files changed, 76 insertions(+), 24 deletions(-) (limited to 'protocols') diff --git a/protocols/Tox/src/common.h b/protocols/Tox/src/common.h index c8bb724f73..00d98984d8 100644 --- a/protocols/Tox/src/common.h +++ b/protocols/Tox/src/common.h @@ -3,6 +3,7 @@ #include #include +#include #include diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 8961ed679b..e2cc4f0407 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -1,9 +1,44 @@ #include "common.h" +#define FRADDR_TOSTR_CHUNK_LEN 8 + +static void fraddr_to_str(uint8_t *id_bin, char *id_str) +{ + uint32_t i, delta = 0, pos_extra, sum_extra = 0; + + for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) { + sprintf(&id_str[2 * i + delta], "%02hhX", id_bin[i]); + + if ((i + 1) == TOX_CLIENT_ID_SIZE) + pos_extra = 2 * (i + 1) + delta; + + if (i >= TOX_CLIENT_ID_SIZE) + sum_extra |= id_bin[i]; + + if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) { + id_str[2 * (i + 1) + delta] = ' '; + delta++; + } + } + + id_str[2 * i + delta] = 0; + + if (!sum_extra) + id_str[pos_extra] = 0; +} + +void get_id(Tox *m, char *data) +{ + int offset = strlen(data); + uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; + tox_get_address(m, address); + fraddr_to_str(address, data + offset); +} + CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : PROTO(protoName, userName) { - tox = tox_new(0); + tox = tox_new(1); tox_callback_friend_request(tox, OnFriendRequest, this); tox_callback_friend_message(tox, OnFriendMessage, this); @@ -13,6 +48,9 @@ CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : tox_callback_user_status(tox, OnUserStatusChanged, this); tox_callback_connection_status(tox, OnConnectionStatusChanged, this); + char idstring[200] = { 0 }; + get_id(tox, idstring); + CreateProtoService(PS_CREATEACCMGRUI, &CToxProto::CreateAccMgrUI); } @@ -119,14 +157,36 @@ int __cdecl CToxProto::SetStatus(int iNewStatus) char *name = "my_nickname"; int res = tox_set_name(tox, (uint8_t*)name, strlen(name)); - uint8_t *pub_key = HexToBinString(BOOTSTRAP_KEY); - res = tox_bootstrap_from_address(tox, BOOTSTRAP_ADDRESS, 0, htons(BOOTSTRAP_PORT), pub_key); - mir_free(pub_key); + char *pub_key = HexToBinString(BOOTSTRAP_KEY); + res = tox_bootstrap_from_address(tox, BOOTSTRAP_ADDRESS, 1, htons(BOOTSTRAP_PORT), (uint8_t *)pub_key); + //mir_free(pub_key); if (!res) { SetStatus(ID_STATUS_OFFLINE); } + time_t timestamp0 = time(NULL); + int on = 0; + + while (1) { + if (on == 0) { + if (tox_isconnected(tox)) { + //new_lines("[i] connected to DHT"); + on = 1; + mir_free(pub_key); + } + else { + time_t timestamp1 = time(NULL); + + if (timestamp0 + 10 < timestamp1) { + timestamp0 = timestamp1; + tox_bootstrap_from_address(tox, BOOTSTRAP_ADDRESS, 1, htons(BOOTSTRAP_PORT), (uint8_t *)pub_key); + } + } + } + tox_do(tox); + } + res = tox_isconnected(tox); if (!res) { diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 5319744a2f..92cdf667e2 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -102,7 +102,7 @@ private: void __cdecl SearchByUidAsync(void* arg); // utils - uint8_t *HexToBinString(char *hex_string); + char *HexToBinString(const char *hex_string); char *BinToHexString(uint8_t *bin_string); // dialogs diff --git a/protocols/Tox/src/tox_utils.cpp b/protocols/Tox/src/tox_utils.cpp index 3888da24bd..38cb154ce5 100644 --- a/protocols/Tox/src/tox_utils.cpp +++ b/protocols/Tox/src/tox_utils.cpp @@ -1,25 +1,16 @@ #include "common.h" -uint8_t *CToxProto::HexToBinString(char *hex_string) +char *CToxProto::HexToBinString(const char *hex_string) { - // byte is represented by exactly 2 hex digits, so lenth of binary string - // is half of that of the hex one. only hex string with even length valid. - // the more proper implementation would be to check if strlen(hex_string) - // is odd and return error code if it is. we assume strlen is even. - // if it's not then the last byte just won't be written in 'ret'. - int length = strlen(hex_string) / 2; - uint8_t *ret = (uint8_t*)mir_alloc(length); - char *pos = hex_string; - - for (int i = 0; i < length; i++, pos += 2) - { - //sscanf(pos, "%2hhx", &ret[i]); - uint8_t byteval; - sscanf(pos, "%2hhx", &byteval); - ret[i] = byteval; - } - - return ret; + size_t len = strlen(hex_string); + char *val = (char*)mir_alloc(len); + + size_t i; + + for (i = 0; i < len; ++i, hex_string += 2) + sscanf(hex_string, "%2hhx", &val[i]); + + return val; } char *CToxProto::BinToHexString(uint8_t *bin_string) -- cgit v1.2.3