From dd12d464de48ca478540457cee4207c38ce69982 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 16 Sep 2013 20:59:53 -0400 Subject: Functions to get name of peer in group chat added. Group message callback modified. --- testing/nTox.c | 6 ++++-- toxcore/Messenger.c | 24 +++++++++++++++++++++--- toxcore/Messenger.h | 14 +++++++++++--- toxcore/group_chats.c | 20 ++++++++++++++++++++ toxcore/group_chats.h | 13 +++++++++++++ toxcore/tox.c | 16 ++++++++++++++-- toxcore/tox.h | 12 ++++++++++-- 7 files changed, 93 insertions(+), 12 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index e87b4b83..cf79b3c4 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -588,10 +588,12 @@ void print_invite(Tox *m, int friendnumber, uint8_t *group_public_key, void *use new_lines(msg); } -void print_groupmessage(Tox *m, int groupnumber, uint8_t *message, uint16_t length, void *userdata) +void print_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, uint16_t length, void *userdata) { char msg[256 + length]; - sprintf(msg, "[g] %u: %s", groupnumber, message); + uint8_t name[TOX_MAX_NAME_LENGTH]; + tox_group_peername(m, groupnumber, peernumber, name); + sprintf(msg, "[g] %u: <%s>: %s", groupnumber, name, message); new_lines(msg); } diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index a27a44dc..0aacc703 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -686,9 +686,9 @@ void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, u /* Set the callback for group messages. * - * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata) + * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) */ -void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), void *userdata) { m->group_message = function; @@ -705,7 +705,7 @@ static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *m } if (m->group_message) - (*m->group_message)(m, i, message, length, m->group_invite_userdata); + (*m->group_message)(m, i, peer_number, message, length, m->group_invite_userdata); } /* Creates a new groupchat and puts it in the chats array. @@ -786,6 +786,24 @@ int del_groupchat(Messenger *m, int groupnumber) return 0; } +/* Copy the name of peernumber who is in groupnumber to name. + * name must be at least MAX_NICK_BYTES long. + * + * return length of name if success + * return -1 if failure + */ +int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *name) +{ + if ((unsigned int)groupnumber >= m->numchats) + return -1; + + if (m->chats == NULL) + return -1; + + if (m->chats[groupnumber] == NULL) + return -1; + return group_peername(m->chats[groupnumber], peernumber, name); +} /* return 1 if that friend was invited to the group * return 0 if the friend was not or error. */ diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index bfcc69df..2aabd90d 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -159,7 +159,7 @@ typedef struct Messenger { void *friend_connectionstatuschange_userdata; void (*group_invite)(struct Messenger *m, int, uint8_t *, void *); void *group_invite_userdata; - void (*group_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *); + void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); void *group_message_userdata; } Messenger; @@ -382,9 +382,9 @@ void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, u /* Set the callback for group messages. * - * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata) + * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) */ -void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), +void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), void *userdata); /* Creates a new groupchat and puts it in the chats array. @@ -401,6 +401,14 @@ int add_groupchat(Messenger *m); */ int del_groupchat(Messenger *m, int groupnumber); +/* Copy the name of peernumber who is in groupnumber to name. + * name must be at least MAX_NICK_BYTES long. + * + * return length of name if success + * return -1 if failure + */ +int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *name); + /* invite friendnumber to groupnumber * return 0 on success * return -1 on failure diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index f37c6a9c..23018cab 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -278,6 +278,26 @@ static int delpeer(Group_Chat *chat, uint8_t *client_id) return -1; } + +/* Copy the name of peernum to name. + * name must be at least MAX_NICK_BYTES long. + * + * return length of name if success + * return -1 if failure + */ +int group_peername(Group_Chat *chat, int peernum, uint8_t *name) +{ + if ((uint32_t)peernum >= chat->numpeers) + return -1; + if (chat->group[peernum].nick_len == 0) { + memcpy(name, "NSA Agent", 10); /* Kindly remind the user that someone with no name might be a NSA agent.*/ + return 10; + } + memcpy(name, chat->group[peernum].nick, chat->group[peernum].nick_len); + return chat->group[peernum].nick_len; +} + + /* min time between pings sent to one peer in seconds */ #define PING_TIMEOUT 5 static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index 78a5488c..7089458c 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -31,6 +31,8 @@ extern "C" { #endif +#define MAX_NICK_BYTES 128 + typedef struct { uint8_t client_id[crypto_box_PUBLICKEYBYTES]; uint64_t pingid; @@ -39,6 +41,9 @@ typedef struct { uint64_t last_recv; uint64_t last_recv_msgping; uint32_t last_message_number; + + uint8_t nick[MAX_NICK_BYTES]; + uint16_t nick_len; } Group_Peer; typedef struct { @@ -65,6 +70,14 @@ typedef struct Group_Chat { } Group_Chat; +/* Copy the name of peernum to name. + * name must be at least MAX_NICK_BYTES long. + * + * return length of name if success + * return -1 if failure + */ +int group_peername(Group_Chat *chat, int peernum, uint8_t *name); + /* * Set callback function for chat messages. * diff --git a/toxcore/tox.c b/toxcore/tox.c index 80d64626..78074db1 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -379,9 +379,9 @@ void tox_callback_group_invite(void *tox, void (*function)(Messenger *tox, int, } /* Set the callback for group messages. * - * Function(Tox *tox, int groupnumber, uint8_t * message, uint16_t length, void *userdata) + * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) */ -void tox_callback_group_message(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_group_message(void *tox, void (*function)(Messenger *tox, int, int, uint8_t *, uint16_t, void *), void *userdata) { Messenger *m = tox; @@ -407,6 +407,18 @@ int tox_del_groupchat(void *tox, int groupnumber) Messenger *m = tox; return del_groupchat(m, groupnumber); } + +/* Copy the name of peernumber who is in groupnumber to name. + * name must be at least MAX_NICK_BYTES long. + * + * return length of name if success + * return -1 if failure + */ +int tox_group_peername(void *tox, int groupnumber, int peernumber, uint8_t *name) +{ + Messenger *m = tox; + return m_group_peername(m, groupnumber, peernumber, name); +} /* invite friendnumber to groupnumber * return 0 on success * return -1 on failure diff --git a/toxcore/tox.h b/toxcore/tox.h index 89242f1f..cc7fc5ec 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -357,9 +357,9 @@ void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int, uint8_t /* Set the callback for group messages. * - * Function(Tox *tox, int groupnumber, uint8_t * message, uint16_t length, void *userdata) + * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) */ -void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t *, uint16_t, void *), void *userdata); /* Creates a new groupchat and puts it in the chats array. @@ -376,6 +376,14 @@ int tox_add_groupchat(Tox *tox); */ int tox_del_groupchat(Tox *tox, int groupnumber); +/* Copy the name of peernumber who is in groupnumber to name. + * name must be at least TOX_MAX_NAME_LENGTH long. + * + * return length of name if success + * return -1 if failure + */ +int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name); + /* invite friendnumber to groupnumber * return 0 on success * return -1 on failure -- cgit v1.2.3 From d4e135d763b02559f94677b0a612cd53fcd26eda Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 17 Sep 2013 06:52:02 -0400 Subject: numchats needs to be decremented --- toxcore/Messenger.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index a27a44dc..d9e085fb 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -773,6 +773,8 @@ int del_groupchat(Messenger *m, int groupnumber) break; } + m->numchats = i; + if (i == 0) { free(m->chats); m->chats = NULL; -- cgit v1.2.3 From 5a83c1296cee5b0bfaae564896f56d78ae265fbc Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 17 Sep 2013 16:28:39 -0400 Subject: astyled everything. --- auto_tests/messenger_test.c | 8 +- auto_tests/network_test.c | 308 ++++++++++++++++++++++---------------------- testing/nTox.c | 10 +- toxcore/DHT.c | 2 + toxcore/Messenger.c | 4 +- toxcore/Messenger.h | 2 +- toxcore/group_chats.c | 4 +- toxcore/group_chats.h | 4 +- toxcore/net_crypto.c | 4 +- toxcore/tox.c | 2 +- toxcore/tox.h | 2 +- toxcore/util.h | 2 +- 12 files changed, 184 insertions(+), 168 deletions(-) diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index dc1625a3..f749f6cb 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -239,12 +239,14 @@ START_TEST(test_dht_state_saveloadsave) memset(buffer, 0xCD, extra); memset(buffer + extra + size, 0xCD, extra); DHT_save(m->dht, buffer + extra); - for(i = 0; i < extra; i++) { + + for (i = 0; i < extra; i++) { ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from DHT_save() @%u", i); ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from DHT_save() @%u", i); } int res = DHT_load_new(m->dht, buffer + extra, size); + if (res == -1) ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1"); else { @@ -279,12 +281,14 @@ START_TEST(test_messenger_state_saveloadsave) memset(buffer, 0xCD, extra); memset(buffer + extra + size, 0xCD, extra); Messenger_save(m, buffer + extra); - for(i = 0; i < extra; i++) { + + for (i = 0; i < extra; i++) { ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from Messenger_save() @%u", i); ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from Messenger_save() @%u", i); } int res = Messenger_load(m, buffer + extra, size); + if (res == -1) ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1"); else { diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 593b1f40..8d2a12d9 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -1,154 +1,154 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include -#include -#include -#include -#include - -#include "../toxcore/network.h" - -START_TEST(test_addr_resolv_localhost) -{ -#ifdef __CYGWIN__ - /* force initialization of network stack - * normally this should happen automatically - * cygwin doesn't do it for every network related function though - * e.g. not for getaddrinfo... */ - socket(0, 0, 0); - errno = 0; -#endif - - const char localhost[] = "localhost"; - - IP ip; - ip_init(&ip, 0); - - int res = addr_resolve(localhost, &ip, NULL); - - ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - - if (res > 0) { - ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); - ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr)); - } - - ip_init(&ip, 1); - res = addr_resolve(localhost, &ip, NULL); - - ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - - if (res > 0) { - ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); - ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); - } - - ip_init(&ip, 1); - ip.family = AF_UNSPEC; - IP extra; - ip_reset(&extra); - res = addr_resolve(localhost, &ip, &extra); - - ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - - if (res > 0) { - ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); - ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); - - ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family); - ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); - } -} -END_TEST - -START_TEST(test_ip_equal) -{ - int res; - IP ip1, ip2; - ip_reset(&ip1); - ip_reset(&ip2); - - res = ip_equal(NULL, NULL); - ck_assert_msg(res == 0, "ip_equal(NULL, NULL): expected result 0, got %u.", res); - - res = ip_equal(&ip1, NULL); - ck_assert_msg(res == 0, "ip_equal(PTR, NULL): expected result 0, got %u.", res); - - res = ip_equal(NULL, &ip1); - ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); - - ip1.family = AF_INET; - ip1.ip4.uint32 = htonl(0x7F000001); - - res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): expected result 0, got %u.", res); - - ip2.family = AF_INET; - ip2.ip4.uint32 = htonl(0x7F000001); - - res = ip_equal(&ip1, &ip2); - ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.1} ): expected result != 0, got 0."); - - ip2.ip4.uint32 = htonl(0x7F000002); - - res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.2} ): expected result 0, got %u.", res); - - ip2.family = AF_INET6; - ip2.ip6.uint32[0] = 0; - ip2.ip6.uint32[1] = 0; - ip2.ip6.uint32[2] = htonl(0xFFFF); - ip2.ip6.uint32[3] = htonl(0x7F000001); - - ck_assert_msg(IN6_IS_ADDR_V4MAPPED(&ip2.ip6) != 0, "IN6_IS_ADDR_V4MAPPED(::ffff:127.0.0.1): expected != 0, got 0."); - - res = ip_equal(&ip1, &ip2); - ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::ffff:127.0.0.1} ): expected result != 0, got 0."); - - memcpy(&ip2.ip6, &in6addr_loopback, sizeof(IP6)); - res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::1} ): expected result 0, got %u.", res); - - memcpy(&ip1, &ip2, sizeof(IP)); - res = ip_equal(&ip1, &ip2); - ck_assert_msg(res != 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::1} ): expected result != 0, got 0."); - - ip2.ip6.uint8[15]++; - res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::2} ): expected result 0, got %res.", res); -} -END_TEST - -#define DEFTESTCASE(NAME) \ - TCase *NAME = tcase_create(#NAME); \ - tcase_add_test(NAME, test_##NAME); \ - suite_add_tcase(s, NAME); - -Suite *network_suite(void) -{ - Suite *s = suite_create("Network"); - - DEFTESTCASE(addr_resolv_localhost); - DEFTESTCASE(ip_equal); - - return s; -} - -int main(int argc, char *argv[]) -{ - srand((unsigned int) time(NULL)); - - Suite *network = network_suite(); - SRunner *test_runner = srunner_create(network); - int number_failed = 0; - - srunner_run_all(test_runner, CK_NORMAL); - number_failed = srunner_ntests_failed(test_runner); - - srunner_free(test_runner); - - return number_failed; -} +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include + +#include "../toxcore/network.h" + +START_TEST(test_addr_resolv_localhost) +{ +#ifdef __CYGWIN__ + /* force initialization of network stack + * normally this should happen automatically + * cygwin doesn't do it for every network related function though + * e.g. not for getaddrinfo... */ + socket(0, 0, 0); + errno = 0; +#endif + + const char localhost[] = "localhost"; + + IP ip; + ip_init(&ip, 0); + + int res = addr_resolve(localhost, &ip, NULL); + + ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); + + if (res > 0) { + ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); + ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr)); + } + + ip_init(&ip, 1); + res = addr_resolve(localhost, &ip, NULL); + + ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); + + if (res > 0) { + ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); + ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); + } + + ip_init(&ip, 1); + ip.family = AF_UNSPEC; + IP extra; + ip_reset(&extra); + res = addr_resolve(localhost, &ip, &extra); + + ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); + + if (res > 0) { + ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); + ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); + + ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family); + ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); + } +} +END_TEST + +START_TEST(test_ip_equal) +{ + int res; + IP ip1, ip2; + ip_reset(&ip1); + ip_reset(&ip2); + + res = ip_equal(NULL, NULL); + ck_assert_msg(res == 0, "ip_equal(NULL, NULL): expected result 0, got %u.", res); + + res = ip_equal(&ip1, NULL); + ck_assert_msg(res == 0, "ip_equal(PTR, NULL): expected result 0, got %u.", res); + + res = ip_equal(NULL, &ip1); + ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); + + ip1.family = AF_INET; + ip1.ip4.uint32 = htonl(0x7F000001); + + res = ip_equal(&ip1, &ip2); + ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): expected result 0, got %u.", res); + + ip2.family = AF_INET; + ip2.ip4.uint32 = htonl(0x7F000001); + + res = ip_equal(&ip1, &ip2); + ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.1} ): expected result != 0, got 0."); + + ip2.ip4.uint32 = htonl(0x7F000002); + + res = ip_equal(&ip1, &ip2); + ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.2} ): expected result 0, got %u.", res); + + ip2.family = AF_INET6; + ip2.ip6.uint32[0] = 0; + ip2.ip6.uint32[1] = 0; + ip2.ip6.uint32[2] = htonl(0xFFFF); + ip2.ip6.uint32[3] = htonl(0x7F000001); + + ck_assert_msg(IN6_IS_ADDR_V4MAPPED(&ip2.ip6) != 0, "IN6_IS_ADDR_V4MAPPED(::ffff:127.0.0.1): expected != 0, got 0."); + + res = ip_equal(&ip1, &ip2); + ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::ffff:127.0.0.1} ): expected result != 0, got 0."); + + memcpy(&ip2.ip6, &in6addr_loopback, sizeof(IP6)); + res = ip_equal(&ip1, &ip2); + ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::1} ): expected result 0, got %u.", res); + + memcpy(&ip1, &ip2, sizeof(IP)); + res = ip_equal(&ip1, &ip2); + ck_assert_msg(res != 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::1} ): expected result != 0, got 0."); + + ip2.ip6.uint8[15]++; + res = ip_equal(&ip1, &ip2); + ck_assert_msg(res == 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::2} ): expected result 0, got %res.", res); +} +END_TEST + +#define DEFTESTCASE(NAME) \ + TCase *NAME = tcase_create(#NAME); \ + tcase_add_test(NAME, test_##NAME); \ + suite_add_tcase(s, NAME); + +Suite *network_suite(void) +{ + Suite *s = suite_create("Network"); + + DEFTESTCASE(addr_resolv_localhost); + DEFTESTCASE(ip_equal); + + return s; +} + +int main(int argc, char *argv[]) +{ + srand((unsigned int) time(NULL)); + + Suite *network = network_suite(); + SRunner *test_runner = srunner_create(network); + int number_failed = 0; + + srunner_run_all(test_runner, CK_NORMAL); + number_failed = srunner_ntests_failed(test_runner); + + srunner_free(test_runner); + + return number_failed; +} diff --git a/testing/nTox.c b/testing/nTox.c index cf79b3c4..2e394528 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -137,6 +137,7 @@ void get_id(Tox *m, char *data) tox_getaddress(m, address); uint32_t i = 0; + for (; i < TOX_FRIEND_ADDRESS_SIZE; i++) { sprintf(data + 2 * i + offset, "%02X ", address[i]); } @@ -257,9 +258,9 @@ void line_eval(Tox *m, char *line) if (num >= 0) { sprintf(numstring, "[i] Added friend as %d.", num); save_data(m); - } - else + } else sprintf(numstring, "[i] Unknown error %i.", num); + break; } @@ -508,12 +509,14 @@ static int load_data(Tox *m) { FILE *data_file = fopen(data_file_name, "r"); size_t size = 0; + if (data_file) { fseek(data_file, 0, SEEK_END); size = ftell(data_file); rewind(data_file); uint8_t data[size]; + if (fread(data, sizeof(uint8_t), size, data_file) != size) { fputs("[!] could not read data file!\n", stderr); fclose(data_file); @@ -537,6 +540,7 @@ static int load_data(Tox *m) static int save_data(Tox *m) { FILE *data_file = fopen(data_file_name, "w"); + if (!data_file) { perror("[!] load_key"); return 0; @@ -563,6 +567,7 @@ static int save_data(Tox *m) static int load_data_or_init(Tox *m, char *path) { data_file_name = path; + if (load_data(m)) return 1; @@ -671,6 +676,7 @@ int main(int argc, char *argv[]) new_lines("[i] change username with /n"); uint8_t name[TOX_MAX_NAME_LENGTH]; uint16_t namelen = tox_getselfname(m, name, sizeof(name)); + if (namelen > 0) { char whoami[128 + TOX_MAX_NAME_LENGTH]; snprintf(whoami, sizeof(whoami), "[i] your current username is: %s", name); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index f38ce3a5..84c00c70 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -1579,6 +1579,7 @@ void DHT_save(DHT *dht, uint8_t *data) len = num * sizeof(Client_data); type = DHT_STATE_TYPE_CLIENTS; data = z_state_save_subheader(data, len, type); + if (num) { Client_data *clients = (Client_data *)data; @@ -1586,6 +1587,7 @@ void DHT_save(DHT *dht, uint8_t *data) if (dht->close_clientlist[i].timestamp != 0) memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); } + data += len; } diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 0aacc703..d6bee5a1 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -788,7 +788,7 @@ int del_groupchat(Messenger *m, int groupnumber) /* Copy the name of peernumber who is in groupnumber to name. * name must be at least MAX_NICK_BYTES long. - * + * * return length of name if success * return -1 if failure */ @@ -802,6 +802,7 @@ int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *nam if (m->chats[groupnumber] == NULL) return -1; + return group_peername(m->chats[groupnumber], peernumber, name); } /* return 1 if that friend was invited to the group @@ -1609,6 +1610,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le break; #ifdef DEBUG + default: fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", length, type); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 2aabd90d..78580dc0 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -403,7 +403,7 @@ int del_groupchat(Messenger *m, int groupnumber); /* Copy the name of peernumber who is in groupnumber to name. * name must be at least MAX_NICK_BYTES long. - * + * * return length of name if success * return -1 if failure */ diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 23018cab..dc8e158b 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -281,7 +281,7 @@ static int delpeer(Group_Chat *chat, uint8_t *client_id) /* Copy the name of peernum to name. * name must be at least MAX_NICK_BYTES long. - * + * * return length of name if success * return -1 if failure */ @@ -289,10 +289,12 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name) { if ((uint32_t)peernum >= chat->numpeers) return -1; + if (chat->group[peernum].nick_len == 0) { memcpy(name, "NSA Agent", 10); /* Kindly remind the user that someone with no name might be a NSA agent.*/ return 10; } + memcpy(name, chat->group[peernum].nick, chat->group[peernum].nick_len); return chat->group[peernum].nick_len; } diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index 7089458c..e45f2af2 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -41,7 +41,7 @@ typedef struct { uint64_t last_recv; uint64_t last_recv_msgping; uint32_t last_message_number; - + uint8_t nick[MAX_NICK_BYTES]; uint16_t nick_len; } Group_Peer; @@ -72,7 +72,7 @@ typedef struct Group_Chat { /* Copy the name of peernum to name. * name must be at least MAX_NICK_BYTES long. - * + * * return length of name if success * return -1 if failure */ diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1de32cb0..a2e42557 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -456,7 +456,7 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) } if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 - || c->crypto_connections == NULL) + || c->crypto_connections == NULL) return -1; memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); @@ -580,7 +580,7 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, * } */ if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 - || c->crypto_connections == NULL) + || c->crypto_connections == NULL) return -1; memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); diff --git a/toxcore/tox.c b/toxcore/tox.c index 78074db1..4fba360b 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -410,7 +410,7 @@ int tox_del_groupchat(void *tox, int groupnumber) /* Copy the name of peernumber who is in groupnumber to name. * name must be at least MAX_NICK_BYTES long. - * + * * return length of name if success * return -1 if failure */ diff --git a/toxcore/tox.h b/toxcore/tox.h index cc7fc5ec..b39008fe 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -378,7 +378,7 @@ int tox_del_groupchat(Tox *tox, int groupnumber); /* Copy the name of peernumber who is in groupnumber to name. * name must be at least TOX_MAX_NAME_LENGTH long. - * + * * return length of name if success * return -1 if failure */ diff --git a/toxcore/util.h b/toxcore/util.h index 6937f7d4..9e4ac79a 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -18,7 +18,7 @@ void id_cpy(uint8_t *dest, uint8_t *src); typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len, uint16_t type); int load_state(load_state_callback_func load_state_callback, void *outer, - uint8_t *data, uint32_t length, uint16_t cookie_inner); + uint8_t *data, uint32_t length, uint16_t cookie_inner); #undef LOGGING /* #define LOGGING */ -- cgit v1.2.3 From 88e80dc88f87897f8cf4c4839b1225e899310067 Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Wed, 18 Sep 2013 16:25:55 +0200 Subject: Allow loginit() to be delayed, store loglog() data in intermediate buffer and flush it out when loginit() is called util.c: - handle loglog() before loginit() by storing the lines into an expanding buffer - when loginit() is called, write out and kill the buffer network.c: - push loginit() to the point where we know the actually used port --- toxcore/network.c | 6 ++---- toxcore/util.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/toxcore/network.c b/toxcore/network.c index a44ef4c4..e04d2608 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -352,10 +352,6 @@ Networking_Core *new_networking(IP ip, uint16_t port) fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); #endif -#ifdef LOGGING - loginit(ntohs(port)); -#endif - /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ uint16_t *portptr = NULL; struct sockaddr_storage addr; @@ -461,6 +457,8 @@ Networking_Core *new_networking(IP ip, uint16_t port) if (!res) { temp->port = *portptr; #ifdef LOGGING + loginit(temp->port); + sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); loglog(logbuffer); #endif diff --git a/toxcore/util.c b/toxcore/util.c index b3263d05..7b2735a0 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -94,6 +94,9 @@ int load_state(load_state_callback_func load_state_callback, void *outer, #ifdef LOGGING time_t starttime = 0; +size_t logbufferprelen = 0; +char *logbufferpredata = NULL; +char *logbufferprehead = NULL; char logbuffer[512]; static FILE *logfile = NULL; void loginit(uint16_t port) @@ -101,9 +104,23 @@ void loginit(uint16_t port) if (logfile) fclose(logfile); - sprintf(logbuffer, "%u-%u.log", ntohs(port), (uint32_t)now()); + if (!starttime) + starttime = now(); + + struct tm *tm = localtime(&starttime); + if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm)) + sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32); + else + sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime); logfile = fopen(logbuffer, "w"); - starttime = now(); + if (logbufferpredata) { + if (logfile) + fprintf(logfile, logbufferpredata); + + free(logbufferpredata); + logbufferpredata = NULL; + } + }; void loglog(char *text) { @@ -111,8 +128,37 @@ void loglog(char *text) fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); fprintf(logfile, text); fflush(logfile); + + return; } -}; + + /* log messages before file was opened: store */ + + size_t len = strlen(text); + if (!starttime) { + starttime = now(); + logbufferprelen = 1024 + len - (len % 1024); + logbufferpredata = malloc(logbufferprelen); + logbufferprehead = logbufferpredata; + } + + /* loginit() called meanwhile? (but failed to open) */ + if (!logbufferpredata) + return; + + if (len + logbufferprehead - logbufferpredata + 16U < logbufferprelen) { + size_t logpos = logbufferprehead - logbufferpredata; + size_t lennew = logbufferprelen * 1.4; + logbufferpredata = realloc(logbufferpredata, lennew); + logbufferprehead = logbufferpredata + logpos; + logbufferprelen = lennew; + } + + size_t written; + sprintf(logbufferprehead, "%4u %s%n", (uint32_t)(now() - starttime), text, &written); + logbufferprehead += written; +} + void logexit() { if (logfile) { -- cgit v1.2.3 From 1a9ee5c95f1864957a25e32c9198f637f7db2032 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 18 Sep 2013 11:11:10 -0400 Subject: Fixed bad code in nTox. --- testing/nTox.c | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 2e394528..a1eea94c 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -269,34 +269,18 @@ void line_eval(Tox *m, char *line) } else if (inpt_command == 'd') { tox_do(m); } else if (inpt_command == 'm') { //message command: /m friendnumber messsage - size_t len = strlen(line); - - if (len < 3) - return; - - char numstring[len - 3]; - char message[len - 3]; - uint32_t i; + char *posi[1]; + int num = strtoul(line + prompt_offset, posi, 0); - for (i = 0; i < len; i++) { - if (line[i + 3] != ' ') { - numstring[i] = line[i + 3]; + if (**posi != 0) { + if (tox_sendmessage(m, num, (uint8_t *) *posi + 1, strlen(*posi + 1) + 1) < 1) { + char sss[256]; + sprintf(sss, "[i] could not send message to friend num %u", num); + new_lines(sss); } else { - uint32_t j; - - for (j = (i + 1); j < (len + 1); j++) - message[j - i - 1] = line[j + 3]; - - break; + new_lines(format_message(m, *posi + 1, -1)); } - } - - int num = atoi(numstring); - - if (tox_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) < 1) { - new_lines("[i] could not send message"); - } else { - new_lines(format_message(m, message, -1)); + new_lines("Error, bad input."); } } else if (inpt_command == 'n') { uint8_t name[TOX_MAX_NAME_LENGTH]; -- cgit v1.2.3 From d2b9b059ba6915ef5318d902d0aef33e2384db89 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 18 Sep 2013 11:19:32 -0400 Subject: Forgot an else. --- testing/nTox.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/nTox.c b/testing/nTox.c index a1eea94c..33c8ab85 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -280,8 +280,8 @@ void line_eval(Tox *m, char *line) } else { new_lines(format_message(m, *posi + 1, -1)); } + } else new_lines("Error, bad input."); - } } else if (inpt_command == 'n') { uint8_t name[TOX_MAX_NAME_LENGTH]; size_t i, len = strlen(line); -- cgit v1.2.3 From 993554bdf6e45f084f52d0c6f3b6200dc550e11c Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 19 Sep 2013 09:46:55 -0400 Subject: Fixed bug in Lossless UDP. --- toxcore/Lossless_UDP.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index 46b0bed1..0ee9b890 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c @@ -170,6 +170,7 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port) memset(connection, 0, sizeof(Connection)); uint32_t handshake_id1 = handshake_id(ludp, ip_port); + uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; *connection = (Connection) { .ip_port = ip_port, @@ -186,7 +187,7 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port) .killat = ~0, .send_counter = 0, /* add randomness to timeout to prevent connections getting stuck in a loop. */ - .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT + .timeout = timeout }; return connection_id; -- cgit v1.2.3 From 7e5da03637aa9fd909179a1ff0a240e21a26506c Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 19 Sep 2013 10:18:43 -0400 Subject: Fixed bug in Lossless UDP. --- toxcore/Lossless_UDP.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index 0ee9b890..94e94122 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c @@ -685,7 +685,7 @@ static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, for (i = 0; i < number; ++i) { temp = ntohl(req_packets[i]); - memcpy(connection->req_packets + i, &temp, 4 * number); + memcpy(connection->req_packets + i, &temp, sizeof(uint32_t)); } connection->num_req_paquets = number; -- cgit v1.2.3 From 524af7ef07c8b3d49bedf55415e1752d1b7855b0 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 20 Sep 2013 08:25:01 -0400 Subject: Increased the size of the UDP buffers. --- toxcore/network.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/toxcore/network.c b/toxcore/network.c index e04d2608..49e1726c 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -325,18 +325,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) #endif /* Functions to increase the size of the send and receive UDP buffers. - * NOTE: Uncomment if necessary. */ - /* int n = 1024 * 1024 * 2; - if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1) - { - return -1; - } - - if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1) - return -1; - */ + setsockopt(temp->sock, SOL_SOCKET, SO_RCVBUF, (char *)&n, sizeof(n)); + setsockopt(temp->sock, SOL_SOCKET, SO_SNDBUF, (char *)&n, sizeof(n)); /* Enable broadcast on socket */ int broadcast = 1; -- cgit v1.2.3 From 53397f607717828575ebccd3fdc1b8736ce87ea3 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 20 Sep 2013 10:02:16 -0400 Subject: Small fix in Lossless UDP. --- toxcore/Lossless_UDP.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index 94e94122..e976370f 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c @@ -674,7 +674,7 @@ static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, /* Packet valid. */ if (comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && - comp_counter < 10 && comp_counter != 0) { + comp_counter == 1) { connection->orecv_packetnum = recv_packetnum; connection->osent_packetnum = sent_packetnum; connection->successful_sent = recv_packetnum; -- cgit v1.2.3 From 20b6900fb181f71eca4577a5f1e1f5f3ecd6a28b Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 20 Sep 2013 11:33:53 -0400 Subject: Fixed bug. --- toxcore/Lossless_UDP.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index e976370f..83a5b338 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c @@ -60,7 +60,7 @@ int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) * TODO: make this better */ -static uint8_t randtable_initget(Lossless_UDP *ludp, uint32_t index, uint8_t value) +static uint32_t randtable_initget(Lossless_UDP *ludp, uint32_t index, uint8_t value) { if (ludp->randtable[index][value] == 0) ludp->randtable[index][value] = random_int(); -- cgit v1.2.3