mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge branch 'master' of https://github.com/irungentoo/ProjectTox-Core.git into ipv6.yield50%toipv4
This commit is contained in:
commit
89005f1701
|
@ -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 {
|
||||
|
|
|
@ -1,154 +1,154 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <check.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#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 <sys/types.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <check.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -268,35 +269,19 @@ 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);
|
||||
char *posi[1];
|
||||
int num = strtoul(line + prompt_offset, posi, 0);
|
||||
|
||||
if (len < 3)
|
||||
return;
|
||||
|
||||
char numstring[len - 3];
|
||||
char message[len - 3];
|
||||
uint32_t i;
|
||||
|
||||
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));
|
||||
}
|
||||
} else
|
||||
new_lines("Error, bad input.");
|
||||
} else if (inpt_command == 'n') {
|
||||
uint8_t name[TOX_MAX_NAME_LENGTH];
|
||||
size_t i, len = strlen(line);
|
||||
|
@ -508,12 +493,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 +524,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 +551,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;
|
||||
|
||||
|
@ -588,10 +577,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);
|
||||
}
|
||||
|
||||
|
@ -669,6 +660,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);
|
||||
|
|
|
@ -1658,6 +1658,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;
|
||||
|
||||
|
@ -1665,6 +1666,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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -673,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;
|
||||
|
@ -684,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;
|
||||
|
|
|
@ -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.
|
||||
|
@ -773,6 +773,8 @@ int del_groupchat(Messenger *m, int groupnumber)
|
|||
break;
|
||||
}
|
||||
|
||||
m->numchats = i;
|
||||
|
||||
if (i == 0) {
|
||||
free(m->chats);
|
||||
m->chats = NULL;
|
||||
|
@ -786,6 +788,25 @@ 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.
|
||||
*/
|
||||
|
@ -1591,6 +1612,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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -278,6 +278,28 @@ 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)
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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;
|
||||
|
@ -352,10 +344,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 +449,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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in New Issue
Block a user