diff options
author | Coren[m] <Break@Ocean> | 2013-09-21 01:27:53 +0200 |
---|---|---|
committer | Coren[m] <Break@Ocean> | 2013-09-21 01:27:53 +0200 |
commit | 89005f17018fcf829efee3cd243c84ef330becfc (patch) | |
tree | 7f4f584e810a5de0c637c066e13a5778a5dc02ad | |
parent | 13bd6aab187d481e51f45f74f64f5b92c7acf935 (diff) | |
parent | 20b6900fb181f71eca4577a5f1e1f5f3ecd6a28b (diff) |
Merge branch 'master' of https://github.com/irungentoo/ProjectTox-Core.git into ipv6.yield50%toipv4
-rw-r--r-- | auto_tests/messenger_test.c | 8 | ||||
-rw-r--r-- | auto_tests/network_test.c | 308 | ||||
-rw-r--r-- | testing/nTox.c | 52 | ||||
-rw-r--r-- | toxcore/DHT.c | 2 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.c | 9 | ||||
-rw-r--r-- | toxcore/Messenger.c | 28 | ||||
-rw-r--r-- | toxcore/Messenger.h | 14 | ||||
-rw-r--r-- | toxcore/group_chats.c | 22 | ||||
-rw-r--r-- | toxcore/group_chats.h | 13 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 4 | ||||
-rw-r--r-- | toxcore/network.c | 18 | ||||
-rw-r--r-- | toxcore/tox.c | 16 | ||||
-rw-r--r-- | toxcore/tox.h | 12 | ||||
-rw-r--r-- | toxcore/util.c | 52 | ||||
-rw-r--r-- | toxcore/util.h | 2 |
15 files changed, 340 insertions, 220 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) | |||
239 | memset(buffer, 0xCD, extra); | 239 | memset(buffer, 0xCD, extra); |
240 | memset(buffer + extra + size, 0xCD, extra); | 240 | memset(buffer + extra + size, 0xCD, extra); |
241 | DHT_save(m->dht, buffer + extra); | 241 | DHT_save(m->dht, buffer + extra); |
242 | for(i = 0; i < extra; i++) { | 242 | |
243 | for (i = 0; i < extra; i++) { | ||
243 | ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from DHT_save() @%u", i); | 244 | ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from DHT_save() @%u", i); |
244 | ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from DHT_save() @%u", i); | 245 | ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from DHT_save() @%u", i); |
245 | } | 246 | } |
246 | 247 | ||
247 | int res = DHT_load_new(m->dht, buffer + extra, size); | 248 | int res = DHT_load_new(m->dht, buffer + extra, size); |
249 | |||
248 | if (res == -1) | 250 | if (res == -1) |
249 | ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1"); | 251 | ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1"); |
250 | else { | 252 | else { |
@@ -279,12 +281,14 @@ START_TEST(test_messenger_state_saveloadsave) | |||
279 | memset(buffer, 0xCD, extra); | 281 | memset(buffer, 0xCD, extra); |
280 | memset(buffer + extra + size, 0xCD, extra); | 282 | memset(buffer + extra + size, 0xCD, extra); |
281 | Messenger_save(m, buffer + extra); | 283 | Messenger_save(m, buffer + extra); |
282 | for(i = 0; i < extra; i++) { | 284 | |
285 | for (i = 0; i < extra; i++) { | ||
283 | ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from Messenger_save() @%u", i); | 286 | ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from Messenger_save() @%u", i); |
284 | ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from Messenger_save() @%u", i); | 287 | ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from Messenger_save() @%u", i); |
285 | } | 288 | } |
286 | 289 | ||
287 | int res = Messenger_load(m, buffer + extra, size); | 290 | int res = Messenger_load(m, buffer + extra, size); |
291 | |||
288 | if (res == -1) | 292 | if (res == -1) |
289 | ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1"); | 293 | ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1"); |
290 | else { | 294 | 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 @@ | |||
1 | #ifdef HAVE_CONFIG_H | 1 | #ifdef HAVE_CONFIG_H |
2 | #include "config.h" | 2 | #include "config.h" |
3 | #endif | 3 | #endif |
4 | 4 | ||
5 | #include <sys/types.h> | 5 | #include <sys/types.h> |
6 | #include <stdint.h> | 6 | #include <stdint.h> |
7 | #include <string.h> | 7 | #include <string.h> |
8 | #include <check.h> | 8 | #include <check.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
10 | #include <time.h> | 10 | #include <time.h> |
11 | 11 | ||
12 | #include "../toxcore/network.h" | 12 | #include "../toxcore/network.h" |
13 | 13 | ||
14 | START_TEST(test_addr_resolv_localhost) | 14 | START_TEST(test_addr_resolv_localhost) |
15 | { | 15 | { |
16 | #ifdef __CYGWIN__ | 16 | #ifdef __CYGWIN__ |
17 | /* force initialization of network stack | 17 | /* force initialization of network stack |
18 | * normally this should happen automatically | 18 | * normally this should happen automatically |
19 | * cygwin doesn't do it for every network related function though | 19 | * cygwin doesn't do it for every network related function though |
20 | * e.g. not for getaddrinfo... */ | 20 | * e.g. not for getaddrinfo... */ |
21 | socket(0, 0, 0); | 21 | socket(0, 0, 0); |
22 | errno = 0; | 22 | errno = 0; |
23 | #endif | 23 | #endif |
24 | 24 | ||
25 | const char localhost[] = "localhost"; | 25 | const char localhost[] = "localhost"; |
26 | 26 | ||
27 | IP ip; | 27 | IP ip; |
28 | ip_init(&ip, 0); | 28 | ip_init(&ip, 0); |
29 | 29 | ||
30 | int res = addr_resolve(localhost, &ip, NULL); | 30 | int res = addr_resolve(localhost, &ip, NULL); |
31 | 31 | ||
32 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); | 32 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); |
33 | 33 | ||
34 | if (res > 0) { | 34 | if (res > 0) { |
35 | ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); | 35 | ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); |
36 | ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr)); | 36 | ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr)); |
37 | } | 37 | } |
38 | 38 | ||
39 | ip_init(&ip, 1); | 39 | ip_init(&ip, 1); |
40 | res = addr_resolve(localhost, &ip, NULL); | 40 | res = addr_resolve(localhost, &ip, NULL); |
41 | 41 | ||
42 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); | 42 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); |
43 | 43 | ||
44 | if (res > 0) { | 44 | if (res > 0) { |
45 | ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); | 45 | ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); |
46 | ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); | 46 | ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); |
47 | } | 47 | } |
48 | 48 | ||
49 | ip_init(&ip, 1); | 49 | ip_init(&ip, 1); |
50 | ip.family = AF_UNSPEC; | 50 | ip.family = AF_UNSPEC; |
51 | IP extra; | 51 | IP extra; |
52 | ip_reset(&extra); | 52 | ip_reset(&extra); |
53 | res = addr_resolve(localhost, &ip, &extra); | 53 | res = addr_resolve(localhost, &ip, &extra); |
54 | 54 | ||
55 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); | 55 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); |
56 | 56 | ||
57 | if (res > 0) { | 57 | if (res > 0) { |
58 | ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); | 58 | ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); |
59 | ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); | 59 | ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); |
60 | 60 | ||
61 | ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family); | 61 | ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family); |
62 | ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); | 62 | ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); |
63 | } | 63 | } |
64 | } | 64 | } |
65 | END_TEST | 65 | END_TEST |
66 | 66 | ||
67 | START_TEST(test_ip_equal) | 67 | START_TEST(test_ip_equal) |
68 | { | 68 | { |
69 | int res; | 69 | int res; |
70 | IP ip1, ip2; | 70 | IP ip1, ip2; |
71 | ip_reset(&ip1); | 71 | ip_reset(&ip1); |
72 | ip_reset(&ip2); | 72 | ip_reset(&ip2); |
73 | 73 | ||
74 | res = ip_equal(NULL, NULL); | 74 | res = ip_equal(NULL, NULL); |
75 | ck_assert_msg(res == 0, "ip_equal(NULL, NULL): expected result 0, got %u.", res); | 75 | ck_assert_msg(res == 0, "ip_equal(NULL, NULL): expected result 0, got %u.", res); |
76 | 76 | ||
77 | res = ip_equal(&ip1, NULL); | 77 | res = ip_equal(&ip1, NULL); |
78 | ck_assert_msg(res == 0, "ip_equal(PTR, NULL): expected result 0, got %u.", res); | 78 | ck_assert_msg(res == 0, "ip_equal(PTR, NULL): expected result 0, got %u.", res); |
79 | 79 | ||
80 | res = ip_equal(NULL, &ip1); | 80 | res = ip_equal(NULL, &ip1); |
81 | ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); | 81 | ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); |
82 | 82 | ||
83 | ip1.family = AF_INET; | 83 | ip1.family = AF_INET; |
84 | ip1.ip4.uint32 = htonl(0x7F000001); | 84 | ip1.ip4.uint32 = htonl(0x7F000001); |
85 | 85 | ||
86 | res = ip_equal(&ip1, &ip2); | 86 | res = ip_equal(&ip1, &ip2); |
87 | ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): expected result 0, got %u.", res); | 87 | ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): expected result 0, got %u.", res); |
88 | 88 | ||
89 | ip2.family = AF_INET; | 89 | ip2.family = AF_INET; |
90 | ip2.ip4.uint32 = htonl(0x7F000001); | 90 | ip2.ip4.uint32 = htonl(0x7F000001); |
91 | 91 | ||
92 | res = ip_equal(&ip1, &ip2); | 92 | res = ip_equal(&ip1, &ip2); |
93 | ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.1} ): expected result != 0, got 0."); | 93 | ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.1} ): expected result != 0, got 0."); |
94 | 94 | ||
95 | ip2.ip4.uint32 = htonl(0x7F000002); | 95 | ip2.ip4.uint32 = htonl(0x7F000002); |
96 | 96 | ||
97 | res = ip_equal(&ip1, &ip2); | 97 | res = ip_equal(&ip1, &ip2); |
98 | 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); | 98 | 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); |
99 | 99 | ||
100 | ip2.family = AF_INET6; | 100 | ip2.family = AF_INET6; |
101 | ip2.ip6.uint32[0] = 0; | 101 | ip2.ip6.uint32[0] = 0; |
102 | ip2.ip6.uint32[1] = 0; | 102 | ip2.ip6.uint32[1] = 0; |
103 | ip2.ip6.uint32[2] = htonl(0xFFFF); | 103 | ip2.ip6.uint32[2] = htonl(0xFFFF); |
104 | ip2.ip6.uint32[3] = htonl(0x7F000001); | 104 | ip2.ip6.uint32[3] = htonl(0x7F000001); |
105 | 105 | ||
106 | ck_assert_msg(IN6_IS_ADDR_V4MAPPED(&ip2.ip6) != 0, "IN6_IS_ADDR_V4MAPPED(::ffff:127.0.0.1): expected != 0, got 0."); | 106 | ck_assert_msg(IN6_IS_ADDR_V4MAPPED(&ip2.ip6) != 0, "IN6_IS_ADDR_V4MAPPED(::ffff:127.0.0.1): expected != 0, got 0."); |
107 | 107 | ||
108 | res = ip_equal(&ip1, &ip2); | 108 | res = ip_equal(&ip1, &ip2); |
109 | 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."); | 109 | 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."); |
110 | 110 | ||
111 | memcpy(&ip2.ip6, &in6addr_loopback, sizeof(IP6)); | 111 | memcpy(&ip2.ip6, &in6addr_loopback, sizeof(IP6)); |
112 | res = ip_equal(&ip1, &ip2); | 112 | res = ip_equal(&ip1, &ip2); |
113 | ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::1} ): expected result 0, got %u.", res); | 113 | ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::1} ): expected result 0, got %u.", res); |
114 | 114 | ||
115 | memcpy(&ip1, &ip2, sizeof(IP)); | 115 | memcpy(&ip1, &ip2, sizeof(IP)); |
116 | res = ip_equal(&ip1, &ip2); | 116 | res = ip_equal(&ip1, &ip2); |
117 | ck_assert_msg(res != 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::1} ): expected result != 0, got 0."); | 117 | ck_assert_msg(res != 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::1} ): expected result != 0, got 0."); |
118 | 118 | ||
119 | ip2.ip6.uint8[15]++; | 119 | ip2.ip6.uint8[15]++; |
120 | res = ip_equal(&ip1, &ip2); | 120 | res = ip_equal(&ip1, &ip2); |
121 | ck_assert_msg(res == 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::2} ): expected result 0, got %res.", res); | 121 | ck_assert_msg(res == 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::2} ): expected result 0, got %res.", res); |
122 | } | 122 | } |
123 | END_TEST | 123 | END_TEST |
124 | 124 | ||
125 | #define DEFTESTCASE(NAME) \ | 125 | #define DEFTESTCASE(NAME) \ |
126 | TCase *NAME = tcase_create(#NAME); \ | 126 | TCase *NAME = tcase_create(#NAME); \ |
127 | tcase_add_test(NAME, test_##NAME); \ | 127 | tcase_add_test(NAME, test_##NAME); \ |
128 | suite_add_tcase(s, NAME); | 128 | suite_add_tcase(s, NAME); |
129 | 129 | ||
130 | Suite *network_suite(void) | 130 | Suite *network_suite(void) |
131 | { | 131 | { |
132 | Suite *s = suite_create("Network"); | 132 | Suite *s = suite_create("Network"); |
133 | 133 | ||
134 | DEFTESTCASE(addr_resolv_localhost); | 134 | DEFTESTCASE(addr_resolv_localhost); |
135 | DEFTESTCASE(ip_equal); | 135 | DEFTESTCASE(ip_equal); |
136 | 136 | ||
137 | return s; | 137 | return s; |
138 | } | 138 | } |
139 | 139 | ||
140 | int main(int argc, char *argv[]) | 140 | int main(int argc, char *argv[]) |
141 | { | 141 | { |
142 | srand((unsigned int) time(NULL)); | 142 | srand((unsigned int) time(NULL)); |
143 | 143 | ||
144 | Suite *network = network_suite(); | 144 | Suite *network = network_suite(); |
145 | SRunner *test_runner = srunner_create(network); | 145 | SRunner *test_runner = srunner_create(network); |
146 | int number_failed = 0; | 146 | int number_failed = 0; |
147 | 147 | ||
148 | srunner_run_all(test_runner, CK_NORMAL); | 148 | srunner_run_all(test_runner, CK_NORMAL); |
149 | number_failed = srunner_ntests_failed(test_runner); | 149 | number_failed = srunner_ntests_failed(test_runner); |
150 | 150 | ||
151 | srunner_free(test_runner); | 151 | srunner_free(test_runner); |
152 | 152 | ||
153 | return number_failed; | 153 | return number_failed; |
154 | } | 154 | } |
diff --git a/testing/nTox.c b/testing/nTox.c index e87b4b83..33c8ab85 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -137,6 +137,7 @@ void get_id(Tox *m, char *data) | |||
137 | tox_getaddress(m, address); | 137 | tox_getaddress(m, address); |
138 | 138 | ||
139 | uint32_t i = 0; | 139 | uint32_t i = 0; |
140 | |||
140 | for (; i < TOX_FRIEND_ADDRESS_SIZE; i++) { | 141 | for (; i < TOX_FRIEND_ADDRESS_SIZE; i++) { |
141 | sprintf(data + 2 * i + offset, "%02X ", address[i]); | 142 | sprintf(data + 2 * i + offset, "%02X ", address[i]); |
142 | } | 143 | } |
@@ -257,9 +258,9 @@ void line_eval(Tox *m, char *line) | |||
257 | if (num >= 0) { | 258 | if (num >= 0) { |
258 | sprintf(numstring, "[i] Added friend as %d.", num); | 259 | sprintf(numstring, "[i] Added friend as %d.", num); |
259 | save_data(m); | 260 | save_data(m); |
260 | } | 261 | } else |
261 | else | ||
262 | sprintf(numstring, "[i] Unknown error %i.", num); | 262 | sprintf(numstring, "[i] Unknown error %i.", num); |
263 | |||
263 | break; | 264 | break; |
264 | } | 265 | } |
265 | 266 | ||
@@ -268,35 +269,19 @@ void line_eval(Tox *m, char *line) | |||
268 | } else if (inpt_command == 'd') { | 269 | } else if (inpt_command == 'd') { |
269 | tox_do(m); | 270 | tox_do(m); |
270 | } else if (inpt_command == 'm') { //message command: /m friendnumber messsage | 271 | } else if (inpt_command == 'm') { //message command: /m friendnumber messsage |
271 | size_t len = strlen(line); | 272 | char *posi[1]; |
272 | 273 | int num = strtoul(line + prompt_offset, posi, 0); | |
273 | if (len < 3) | ||
274 | return; | ||
275 | |||
276 | char numstring[len - 3]; | ||
277 | char message[len - 3]; | ||
278 | uint32_t i; | ||
279 | 274 | ||
280 | for (i = 0; i < len; i++) { | 275 | if (**posi != 0) { |
281 | if (line[i + 3] != ' ') { | 276 | if (tox_sendmessage(m, num, (uint8_t *) *posi + 1, strlen(*posi + 1) + 1) < 1) { |
282 | numstring[i] = line[i + 3]; | 277 | char sss[256]; |
278 | sprintf(sss, "[i] could not send message to friend num %u", num); | ||
279 | new_lines(sss); | ||
283 | } else { | 280 | } else { |
284 | uint32_t j; | 281 | new_lines(format_message(m, *posi + 1, -1)); |
285 | |||
286 | for (j = (i + 1); j < (len + 1); j++) | ||
287 | message[j - i - 1] = line[j + 3]; | ||
288 | |||
289 | break; | ||
290 | } | 282 | } |
291 | } | 283 | } else |
292 | 284 | new_lines("Error, bad input."); | |
293 | int num = atoi(numstring); | ||
294 | |||
295 | if (tox_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) < 1) { | ||
296 | new_lines("[i] could not send message"); | ||
297 | } else { | ||
298 | new_lines(format_message(m, message, -1)); | ||
299 | } | ||
300 | } else if (inpt_command == 'n') { | 285 | } else if (inpt_command == 'n') { |
301 | uint8_t name[TOX_MAX_NAME_LENGTH]; | 286 | uint8_t name[TOX_MAX_NAME_LENGTH]; |
302 | size_t i, len = strlen(line); | 287 | size_t i, len = strlen(line); |
@@ -508,12 +493,14 @@ static int load_data(Tox *m) | |||
508 | { | 493 | { |
509 | FILE *data_file = fopen(data_file_name, "r"); | 494 | FILE *data_file = fopen(data_file_name, "r"); |
510 | size_t size = 0; | 495 | size_t size = 0; |
496 | |||
511 | if (data_file) { | 497 | if (data_file) { |
512 | fseek(data_file, 0, SEEK_END); | 498 | fseek(data_file, 0, SEEK_END); |
513 | size = ftell(data_file); | 499 | size = ftell(data_file); |
514 | rewind(data_file); | 500 | rewind(data_file); |
515 | 501 | ||
516 | uint8_t data[size]; | 502 | uint8_t data[size]; |
503 | |||
517 | if (fread(data, sizeof(uint8_t), size, data_file) != size) { | 504 | if (fread(data, sizeof(uint8_t), size, data_file) != size) { |
518 | fputs("[!] could not read data file!\n", stderr); | 505 | fputs("[!] could not read data file!\n", stderr); |
519 | fclose(data_file); | 506 | fclose(data_file); |
@@ -537,6 +524,7 @@ static int load_data(Tox *m) | |||
537 | static int save_data(Tox *m) | 524 | static int save_data(Tox *m) |
538 | { | 525 | { |
539 | FILE *data_file = fopen(data_file_name, "w"); | 526 | FILE *data_file = fopen(data_file_name, "w"); |
527 | |||
540 | if (!data_file) { | 528 | if (!data_file) { |
541 | perror("[!] load_key"); | 529 | perror("[!] load_key"); |
542 | return 0; | 530 | return 0; |
@@ -563,6 +551,7 @@ static int save_data(Tox *m) | |||
563 | static int load_data_or_init(Tox *m, char *path) | 551 | static int load_data_or_init(Tox *m, char *path) |
564 | { | 552 | { |
565 | data_file_name = path; | 553 | data_file_name = path; |
554 | |||
566 | if (load_data(m)) | 555 | if (load_data(m)) |
567 | return 1; | 556 | return 1; |
568 | 557 | ||
@@ -588,10 +577,12 @@ void print_invite(Tox *m, int friendnumber, uint8_t *group_public_key, void *use | |||
588 | new_lines(msg); | 577 | new_lines(msg); |
589 | } | 578 | } |
590 | 579 | ||
591 | void print_groupmessage(Tox *m, int groupnumber, uint8_t *message, uint16_t length, void *userdata) | 580 | void print_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, uint16_t length, void *userdata) |
592 | { | 581 | { |
593 | char msg[256 + length]; | 582 | char msg[256 + length]; |
594 | sprintf(msg, "[g] %u: %s", groupnumber, message); | 583 | uint8_t name[TOX_MAX_NAME_LENGTH]; |
584 | tox_group_peername(m, groupnumber, peernumber, name); | ||
585 | sprintf(msg, "[g] %u: <%s>: %s", groupnumber, name, message); | ||
595 | new_lines(msg); | 586 | new_lines(msg); |
596 | } | 587 | } |
597 | 588 | ||
@@ -669,6 +660,7 @@ int main(int argc, char *argv[]) | |||
669 | new_lines("[i] change username with /n"); | 660 | new_lines("[i] change username with /n"); |
670 | uint8_t name[TOX_MAX_NAME_LENGTH]; | 661 | uint8_t name[TOX_MAX_NAME_LENGTH]; |
671 | uint16_t namelen = tox_getselfname(m, name, sizeof(name)); | 662 | uint16_t namelen = tox_getselfname(m, name, sizeof(name)); |
663 | |||
672 | if (namelen > 0) { | 664 | if (namelen > 0) { |
673 | char whoami[128 + TOX_MAX_NAME_LENGTH]; | 665 | char whoami[128 + TOX_MAX_NAME_LENGTH]; |
674 | snprintf(whoami, sizeof(whoami), "[i] your current username is: %s", name); | 666 | snprintf(whoami, sizeof(whoami), "[i] your current username is: %s", name); |
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index cecfb2ce..cabd96c2 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -1658,6 +1658,7 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
1658 | len = num * sizeof(Client_data); | 1658 | len = num * sizeof(Client_data); |
1659 | type = DHT_STATE_TYPE_CLIENTS; | 1659 | type = DHT_STATE_TYPE_CLIENTS; |
1660 | data = z_state_save_subheader(data, len, type); | 1660 | data = z_state_save_subheader(data, len, type); |
1661 | |||
1661 | if (num) { | 1662 | if (num) { |
1662 | Client_data *clients = (Client_data *)data; | 1663 | Client_data *clients = (Client_data *)data; |
1663 | 1664 | ||
@@ -1665,6 +1666,7 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
1665 | if (dht->close_clientlist[i].timestamp != 0) | 1666 | if (dht->close_clientlist[i].timestamp != 0) |
1666 | memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); | 1667 | memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); |
1667 | } | 1668 | } |
1669 | |||
1668 | data += len; | 1670 | data += len; |
1669 | } | 1671 | } |
1670 | 1672 | ||
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index 46b0bed1..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) | |||
60 | * TODO: make this better | 60 | * TODO: make this better |
61 | */ | 61 | */ |
62 | 62 | ||
63 | static uint8_t randtable_initget(Lossless_UDP *ludp, uint32_t index, uint8_t value) | 63 | static uint32_t randtable_initget(Lossless_UDP *ludp, uint32_t index, uint8_t value) |
64 | { | 64 | { |
65 | if (ludp->randtable[index][value] == 0) | 65 | if (ludp->randtable[index][value] == 0) |
66 | ludp->randtable[index][value] = random_int(); | 66 | ludp->randtable[index][value] = random_int(); |
@@ -170,6 +170,7 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port) | |||
170 | memset(connection, 0, sizeof(Connection)); | 170 | memset(connection, 0, sizeof(Connection)); |
171 | 171 | ||
172 | uint32_t handshake_id1 = handshake_id(ludp, ip_port); | 172 | uint32_t handshake_id1 = handshake_id(ludp, ip_port); |
173 | uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT; | ||
173 | 174 | ||
174 | *connection = (Connection) { | 175 | *connection = (Connection) { |
175 | .ip_port = ip_port, | 176 | .ip_port = ip_port, |
@@ -186,7 +187,7 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port) | |||
186 | .killat = ~0, | 187 | .killat = ~0, |
187 | .send_counter = 0, | 188 | .send_counter = 0, |
188 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ | 189 | /* add randomness to timeout to prevent connections getting stuck in a loop. */ |
189 | .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT | 190 | .timeout = timeout |
190 | }; | 191 | }; |
191 | 192 | ||
192 | return connection_id; | 193 | return connection_id; |
@@ -673,7 +674,7 @@ static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, | |||
673 | /* Packet valid. */ | 674 | /* Packet valid. */ |
674 | if (comp_1 <= BUFFER_PACKET_NUM && | 675 | if (comp_1 <= BUFFER_PACKET_NUM && |
675 | comp_2 <= BUFFER_PACKET_NUM && | 676 | comp_2 <= BUFFER_PACKET_NUM && |
676 | comp_counter < 10 && comp_counter != 0) { | 677 | comp_counter == 1) { |
677 | connection->orecv_packetnum = recv_packetnum; | 678 | connection->orecv_packetnum = recv_packetnum; |
678 | connection->osent_packetnum = sent_packetnum; | 679 | connection->osent_packetnum = sent_packetnum; |
679 | connection->successful_sent = recv_packetnum; | 680 | connection->successful_sent = recv_packetnum; |
@@ -684,7 +685,7 @@ static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, | |||
684 | 685 | ||
685 | for (i = 0; i < number; ++i) { | 686 | for (i = 0; i < number; ++i) { |
686 | temp = ntohl(req_packets[i]); | 687 | temp = ntohl(req_packets[i]); |
687 | memcpy(connection->req_packets + i, &temp, 4 * number); | 688 | memcpy(connection->req_packets + i, &temp, sizeof(uint32_t)); |
688 | } | 689 | } |
689 | 690 | ||
690 | connection->num_req_paquets = number; | 691 | connection->num_req_paquets = number; |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index a27a44dc..dbbfa58c 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 | |||
686 | 686 | ||
687 | /* Set the callback for group messages. | 687 | /* Set the callback for group messages. |
688 | * | 688 | * |
689 | * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata) | 689 | * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) |
690 | */ | 690 | */ |
691 | void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), | 691 | void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), |
692 | void *userdata) | 692 | void *userdata) |
693 | { | 693 | { |
694 | m->group_message = function; | 694 | m->group_message = function; |
@@ -705,7 +705,7 @@ static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *m | |||
705 | } | 705 | } |
706 | 706 | ||
707 | if (m->group_message) | 707 | if (m->group_message) |
708 | (*m->group_message)(m, i, message, length, m->group_invite_userdata); | 708 | (*m->group_message)(m, i, peer_number, message, length, m->group_invite_userdata); |
709 | } | 709 | } |
710 | 710 | ||
711 | /* Creates a new groupchat and puts it in the chats array. | 711 | /* Creates a new groupchat and puts it in the chats array. |
@@ -773,6 +773,8 @@ int del_groupchat(Messenger *m, int groupnumber) | |||
773 | break; | 773 | break; |
774 | } | 774 | } |
775 | 775 | ||
776 | m->numchats = i; | ||
777 | |||
776 | if (i == 0) { | 778 | if (i == 0) { |
777 | free(m->chats); | 779 | free(m->chats); |
778 | m->chats = NULL; | 780 | m->chats = NULL; |
@@ -786,6 +788,25 @@ int del_groupchat(Messenger *m, int groupnumber) | |||
786 | return 0; | 788 | return 0; |
787 | } | 789 | } |
788 | 790 | ||
791 | /* Copy the name of peernumber who is in groupnumber to name. | ||
792 | * name must be at least MAX_NICK_BYTES long. | ||
793 | * | ||
794 | * return length of name if success | ||
795 | * return -1 if failure | ||
796 | */ | ||
797 | int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *name) | ||
798 | { | ||
799 | if ((unsigned int)groupnumber >= m->numchats) | ||
800 | return -1; | ||
801 | |||
802 | if (m->chats == NULL) | ||
803 | return -1; | ||
804 | |||
805 | if (m->chats[groupnumber] == NULL) | ||
806 | return -1; | ||
807 | |||
808 | return group_peername(m->chats[groupnumber], peernumber, name); | ||
809 | } | ||
789 | /* return 1 if that friend was invited to the group | 810 | /* return 1 if that friend was invited to the group |
790 | * return 0 if the friend was not or error. | 811 | * return 0 if the friend was not or error. |
791 | */ | 812 | */ |
@@ -1591,6 +1612,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le | |||
1591 | break; | 1612 | break; |
1592 | 1613 | ||
1593 | #ifdef DEBUG | 1614 | #ifdef DEBUG |
1615 | |||
1594 | default: | 1616 | default: |
1595 | fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", | 1617 | fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", |
1596 | length, type); | 1618 | length, type); |
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index bfcc69df..78580dc0 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -159,7 +159,7 @@ typedef struct Messenger { | |||
159 | void *friend_connectionstatuschange_userdata; | 159 | void *friend_connectionstatuschange_userdata; |
160 | void (*group_invite)(struct Messenger *m, int, uint8_t *, void *); | 160 | void (*group_invite)(struct Messenger *m, int, uint8_t *, void *); |
161 | void *group_invite_userdata; | 161 | void *group_invite_userdata; |
162 | void (*group_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *); | 162 | void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); |
163 | void *group_message_userdata; | 163 | void *group_message_userdata; |
164 | 164 | ||
165 | } Messenger; | 165 | } Messenger; |
@@ -382,9 +382,9 @@ void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, u | |||
382 | 382 | ||
383 | /* Set the callback for group messages. | 383 | /* Set the callback for group messages. |
384 | * | 384 | * |
385 | * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata) | 385 | * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) |
386 | */ | 386 | */ |
387 | void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), | 387 | void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), |
388 | void *userdata); | 388 | void *userdata); |
389 | 389 | ||
390 | /* Creates a new groupchat and puts it in the chats array. | 390 | /* Creates a new groupchat and puts it in the chats array. |
@@ -401,6 +401,14 @@ int add_groupchat(Messenger *m); | |||
401 | */ | 401 | */ |
402 | int del_groupchat(Messenger *m, int groupnumber); | 402 | int del_groupchat(Messenger *m, int groupnumber); |
403 | 403 | ||
404 | /* Copy the name of peernumber who is in groupnumber to name. | ||
405 | * name must be at least MAX_NICK_BYTES long. | ||
406 | * | ||
407 | * return length of name if success | ||
408 | * return -1 if failure | ||
409 | */ | ||
410 | int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *name); | ||
411 | |||
404 | /* invite friendnumber to groupnumber | 412 | /* invite friendnumber to groupnumber |
405 | * return 0 on success | 413 | * return 0 on success |
406 | * return -1 on failure | 414 | * return -1 on failure |
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index f37c6a9c..dc8e158b 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c | |||
@@ -278,6 +278,28 @@ static int delpeer(Group_Chat *chat, uint8_t *client_id) | |||
278 | 278 | ||
279 | return -1; | 279 | return -1; |
280 | } | 280 | } |
281 | |||
282 | /* Copy the name of peernum to name. | ||
283 | * name must be at least MAX_NICK_BYTES long. | ||
284 | * | ||
285 | * return length of name if success | ||
286 | * return -1 if failure | ||
287 | */ | ||
288 | int group_peername(Group_Chat *chat, int peernum, uint8_t *name) | ||
289 | { | ||
290 | if ((uint32_t)peernum >= chat->numpeers) | ||
291 | return -1; | ||
292 | |||
293 | if (chat->group[peernum].nick_len == 0) { | ||
294 | memcpy(name, "NSA Agent", 10); /* Kindly remind the user that someone with no name might be a NSA agent.*/ | ||
295 | return 10; | ||
296 | } | ||
297 | |||
298 | memcpy(name, chat->group[peernum].nick, chat->group[peernum].nick_len); | ||
299 | return chat->group[peernum].nick_len; | ||
300 | } | ||
301 | |||
302 | |||
281 | /* min time between pings sent to one peer in seconds */ | 303 | /* min time between pings sent to one peer in seconds */ |
282 | #define PING_TIMEOUT 5 | 304 | #define PING_TIMEOUT 5 |
283 | static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum) | 305 | 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..e45f2af2 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h | |||
@@ -31,6 +31,8 @@ | |||
31 | extern "C" { | 31 | extern "C" { |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #define MAX_NICK_BYTES 128 | ||
35 | |||
34 | typedef struct { | 36 | typedef struct { |
35 | uint8_t client_id[crypto_box_PUBLICKEYBYTES]; | 37 | uint8_t client_id[crypto_box_PUBLICKEYBYTES]; |
36 | uint64_t pingid; | 38 | uint64_t pingid; |
@@ -39,6 +41,9 @@ typedef struct { | |||
39 | uint64_t last_recv; | 41 | uint64_t last_recv; |
40 | uint64_t last_recv_msgping; | 42 | uint64_t last_recv_msgping; |
41 | uint32_t last_message_number; | 43 | uint32_t last_message_number; |
44 | |||
45 | uint8_t nick[MAX_NICK_BYTES]; | ||
46 | uint16_t nick_len; | ||
42 | } Group_Peer; | 47 | } Group_Peer; |
43 | 48 | ||
44 | typedef struct { | 49 | typedef struct { |
@@ -65,6 +70,14 @@ typedef struct Group_Chat { | |||
65 | 70 | ||
66 | } Group_Chat; | 71 | } Group_Chat; |
67 | 72 | ||
73 | /* Copy the name of peernum to name. | ||
74 | * name must be at least MAX_NICK_BYTES long. | ||
75 | * | ||
76 | * return length of name if success | ||
77 | * return -1 if failure | ||
78 | */ | ||
79 | int group_peername(Group_Chat *chat, int peernum, uint8_t *name); | ||
80 | |||
68 | /* | 81 | /* |
69 | * Set callback function for chat messages. | 82 | * Set callback function for chat messages. |
70 | * | 83 | * |
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) | |||
456 | } | 456 | } |
457 | 457 | ||
458 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 | 458 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 |
459 | || c->crypto_connections == NULL) | 459 | || c->crypto_connections == NULL) |
460 | return -1; | 460 | return -1; |
461 | 461 | ||
462 | memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); | 462 | 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, | |||
580 | * } | 580 | * } |
581 | */ | 581 | */ |
582 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 | 582 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 |
583 | || c->crypto_connections == NULL) | 583 | || c->crypto_connections == NULL) |
584 | return -1; | 584 | return -1; |
585 | 585 | ||
586 | memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); | 586 | memset(&(c->crypto_connections[c->crypto_connections_length]), 0, sizeof(Crypto_Connection)); |
diff --git a/toxcore/network.c b/toxcore/network.c index a44ef4c4..49e1726c 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -325,18 +325,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
325 | #endif | 325 | #endif |
326 | 326 | ||
327 | /* Functions to increase the size of the send and receive UDP buffers. | 327 | /* Functions to increase the size of the send and receive UDP buffers. |
328 | * NOTE: Uncomment if necessary. | ||
329 | */ | 328 | */ |
330 | /* | ||
331 | int n = 1024 * 1024 * 2; | 329 | int n = 1024 * 1024 * 2; |
332 | if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1) | 330 | setsockopt(temp->sock, SOL_SOCKET, SO_RCVBUF, (char *)&n, sizeof(n)); |
333 | { | 331 | setsockopt(temp->sock, SOL_SOCKET, SO_SNDBUF, (char *)&n, sizeof(n)); |
334 | return -1; | ||
335 | } | ||
336 | |||
337 | if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1) | ||
338 | return -1; | ||
339 | */ | ||
340 | 332 | ||
341 | /* Enable broadcast on socket */ | 333 | /* Enable broadcast on socket */ |
342 | int broadcast = 1; | 334 | int broadcast = 1; |
@@ -352,10 +344,6 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
352 | fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); | 344 | fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); |
353 | #endif | 345 | #endif |
354 | 346 | ||
355 | #ifdef LOGGING | ||
356 | loginit(ntohs(port)); | ||
357 | #endif | ||
358 | |||
359 | /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ | 347 | /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ |
360 | uint16_t *portptr = NULL; | 348 | uint16_t *portptr = NULL; |
361 | struct sockaddr_storage addr; | 349 | struct sockaddr_storage addr; |
@@ -461,6 +449,8 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
461 | if (!res) { | 449 | if (!res) { |
462 | temp->port = *portptr; | 450 | temp->port = *portptr; |
463 | #ifdef LOGGING | 451 | #ifdef LOGGING |
452 | loginit(temp->port); | ||
453 | |||
464 | sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); | 454 | sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); |
465 | loglog(logbuffer); | 455 | loglog(logbuffer); |
466 | #endif | 456 | #endif |
diff --git a/toxcore/tox.c b/toxcore/tox.c index 80d64626..4fba360b 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, | |||
379 | } | 379 | } |
380 | /* Set the callback for group messages. | 380 | /* Set the callback for group messages. |
381 | * | 381 | * |
382 | * Function(Tox *tox, int groupnumber, uint8_t * message, uint16_t length, void *userdata) | 382 | * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) |
383 | */ | 383 | */ |
384 | void tox_callback_group_message(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), | 384 | void tox_callback_group_message(void *tox, void (*function)(Messenger *tox, int, int, uint8_t *, uint16_t, void *), |
385 | void *userdata) | 385 | void *userdata) |
386 | { | 386 | { |
387 | Messenger *m = tox; | 387 | Messenger *m = tox; |
@@ -407,6 +407,18 @@ int tox_del_groupchat(void *tox, int groupnumber) | |||
407 | Messenger *m = tox; | 407 | Messenger *m = tox; |
408 | return del_groupchat(m, groupnumber); | 408 | return del_groupchat(m, groupnumber); |
409 | } | 409 | } |
410 | |||
411 | /* Copy the name of peernumber who is in groupnumber to name. | ||
412 | * name must be at least MAX_NICK_BYTES long. | ||
413 | * | ||
414 | * return length of name if success | ||
415 | * return -1 if failure | ||
416 | */ | ||
417 | int tox_group_peername(void *tox, int groupnumber, int peernumber, uint8_t *name) | ||
418 | { | ||
419 | Messenger *m = tox; | ||
420 | return m_group_peername(m, groupnumber, peernumber, name); | ||
421 | } | ||
410 | /* invite friendnumber to groupnumber | 422 | /* invite friendnumber to groupnumber |
411 | * return 0 on success | 423 | * return 0 on success |
412 | * return -1 on failure | 424 | * return -1 on failure |
diff --git a/toxcore/tox.h b/toxcore/tox.h index 89242f1f..b39008fe 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 | |||
357 | 357 | ||
358 | /* Set the callback for group messages. | 358 | /* Set the callback for group messages. |
359 | * | 359 | * |
360 | * Function(Tox *tox, int groupnumber, uint8_t * message, uint16_t length, void *userdata) | 360 | * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) |
361 | */ | 361 | */ |
362 | void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), | 362 | void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t *, uint16_t, void *), |
363 | void *userdata); | 363 | void *userdata); |
364 | 364 | ||
365 | /* Creates a new groupchat and puts it in the chats array. | 365 | /* Creates a new groupchat and puts it in the chats array. |
@@ -376,6 +376,14 @@ int tox_add_groupchat(Tox *tox); | |||
376 | */ | 376 | */ |
377 | int tox_del_groupchat(Tox *tox, int groupnumber); | 377 | int tox_del_groupchat(Tox *tox, int groupnumber); |
378 | 378 | ||
379 | /* Copy the name of peernumber who is in groupnumber to name. | ||
380 | * name must be at least TOX_MAX_NAME_LENGTH long. | ||
381 | * | ||
382 | * return length of name if success | ||
383 | * return -1 if failure | ||
384 | */ | ||
385 | int tox_group_peername(Tox *tox, int groupnumber, int peernumber, uint8_t *name); | ||
386 | |||
379 | /* invite friendnumber to groupnumber | 387 | /* invite friendnumber to groupnumber |
380 | * return 0 on success | 388 | * return 0 on success |
381 | * return -1 on failure | 389 | * return -1 on failure |
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, | |||
94 | 94 | ||
95 | #ifdef LOGGING | 95 | #ifdef LOGGING |
96 | time_t starttime = 0; | 96 | time_t starttime = 0; |
97 | size_t logbufferprelen = 0; | ||
98 | char *logbufferpredata = NULL; | ||
99 | char *logbufferprehead = NULL; | ||
97 | char logbuffer[512]; | 100 | char logbuffer[512]; |
98 | static FILE *logfile = NULL; | 101 | static FILE *logfile = NULL; |
99 | void loginit(uint16_t port) | 102 | void loginit(uint16_t port) |
@@ -101,9 +104,23 @@ void loginit(uint16_t port) | |||
101 | if (logfile) | 104 | if (logfile) |
102 | fclose(logfile); | 105 | fclose(logfile); |
103 | 106 | ||
104 | sprintf(logbuffer, "%u-%u.log", ntohs(port), (uint32_t)now()); | 107 | if (!starttime) |
108 | starttime = now(); | ||
109 | |||
110 | struct tm *tm = localtime(&starttime); | ||
111 | if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm)) | ||
112 | sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32); | ||
113 | else | ||
114 | sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime); | ||
105 | logfile = fopen(logbuffer, "w"); | 115 | logfile = fopen(logbuffer, "w"); |
106 | starttime = now(); | 116 | if (logbufferpredata) { |
117 | if (logfile) | ||
118 | fprintf(logfile, logbufferpredata); | ||
119 | |||
120 | free(logbufferpredata); | ||
121 | logbufferpredata = NULL; | ||
122 | } | ||
123 | |||
107 | }; | 124 | }; |
108 | void loglog(char *text) | 125 | void loglog(char *text) |
109 | { | 126 | { |
@@ -111,8 +128,37 @@ void loglog(char *text) | |||
111 | fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); | 128 | fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); |
112 | fprintf(logfile, text); | 129 | fprintf(logfile, text); |
113 | fflush(logfile); | 130 | fflush(logfile); |
131 | |||
132 | return; | ||
114 | } | 133 | } |
115 | }; | 134 | |
135 | /* log messages before file was opened: store */ | ||
136 | |||
137 | size_t len = strlen(text); | ||
138 | if (!starttime) { | ||
139 | starttime = now(); | ||
140 | logbufferprelen = 1024 + len - (len % 1024); | ||
141 | logbufferpredata = malloc(logbufferprelen); | ||
142 | logbufferprehead = logbufferpredata; | ||
143 | } | ||
144 | |||
145 | /* loginit() called meanwhile? (but failed to open) */ | ||
146 | if (!logbufferpredata) | ||
147 | return; | ||
148 | |||
149 | if (len + logbufferprehead - logbufferpredata + 16U < logbufferprelen) { | ||
150 | size_t logpos = logbufferprehead - logbufferpredata; | ||
151 | size_t lennew = logbufferprelen * 1.4; | ||
152 | logbufferpredata = realloc(logbufferpredata, lennew); | ||
153 | logbufferprehead = logbufferpredata + logpos; | ||
154 | logbufferprelen = lennew; | ||
155 | } | ||
156 | |||
157 | size_t written; | ||
158 | sprintf(logbufferprehead, "%4u %s%n", (uint32_t)(now() - starttime), text, &written); | ||
159 | logbufferprehead += written; | ||
160 | } | ||
161 | |||
116 | void logexit() | 162 | void logexit() |
117 | { | 163 | { |
118 | if (logfile) { | 164 | if (logfile) { |
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); | |||
18 | 18 | ||
19 | typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len, uint16_t type); | 19 | typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len, uint16_t type); |
20 | int load_state(load_state_callback_func load_state_callback, void *outer, | 20 | int load_state(load_state_callback_func load_state_callback, void *outer, |
21 | uint8_t *data, uint32_t length, uint16_t cookie_inner); | 21 | uint8_t *data, uint32_t length, uint16_t cookie_inner); |
22 | 22 | ||
23 | #undef LOGGING | 23 | #undef LOGGING |
24 | /* #define LOGGING */ | 24 | /* #define LOGGING */ |