mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Passed everything through astyle.
This commit is contained in:
parent
c5af8f44a9
commit
88ff81d9de
|
@ -10,8 +10,8 @@
|
|||
void rand_bytes(uint8_t *b, size_t blen)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < blen; i++)
|
||||
{
|
||||
|
||||
for (i = 0; i < blen; i++) {
|
||||
b[i] = rand();
|
||||
}
|
||||
}
|
||||
|
@ -86,7 +86,8 @@ START_TEST(test_known)
|
|||
unsigned char m[131];
|
||||
int clen, mlen;
|
||||
|
||||
ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char), "cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext");
|
||||
ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char),
|
||||
"cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext");
|
||||
ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
|
||||
ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");
|
||||
|
||||
|
@ -111,7 +112,8 @@ START_TEST(test_fast_known)
|
|||
|
||||
encrypt_precompute(bobpk, alicesk, k);
|
||||
|
||||
ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char), "cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext");
|
||||
ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char),
|
||||
"cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext");
|
||||
ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
|
||||
ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");
|
||||
|
||||
|
@ -156,8 +158,7 @@ START_TEST(test_endtoend)
|
|||
int testno;
|
||||
|
||||
// Test 100 random messages and keypairs
|
||||
for (testno = 0; testno < 100; testno++)
|
||||
{
|
||||
for (testno = 0; testno < 100; testno++) {
|
||||
//Generate random message (random length from 100 to 500)
|
||||
mlen = (rand() % 400) + 100;
|
||||
rand_bytes(m, mlen);
|
||||
|
@ -181,7 +182,8 @@ START_TEST(test_endtoend)
|
|||
|
||||
ck_assert_msg(c1len == c2len && c1len == c3len && c1len == c4len, "cyphertext lengths differ");
|
||||
ck_assert_msg(c1len == mlen + ENCRYPTION_PADDING, "wrong cyphertext length");
|
||||
ck_assert_msg(memcmp(c1, c2, c1len) == 0 && memcmp(c1, c3, c1len) == 0 && memcmp(c1, c4, c1len) == 0, "crypertexts differ");
|
||||
ck_assert_msg(memcmp(c1, c2, c1len) == 0 && memcmp(c1, c3, c1len) == 0
|
||||
&& memcmp(c1, c4, c1len) == 0, "crypertexts differ");
|
||||
|
||||
//Decrypt all four ways
|
||||
m1len = decrypt_data(pk2, sk1, n, c1, c1len, m1);
|
||||
|
@ -191,7 +193,8 @@ START_TEST(test_endtoend)
|
|||
|
||||
ck_assert_msg(m1len == m2len && m1len == m3len && m1len == m4len, "decrypted text lengths differ");
|
||||
ck_assert_msg(m1len == mlen, "wrong decrypted text length");
|
||||
ck_assert_msg(memcmp(m1, m2, mlen) == 0 && memcmp(m1, m3, mlen) == 0 && memcmp(m1, m4, mlen) == 0, "decrypted texts differ");
|
||||
ck_assert_msg(memcmp(m1, m2, mlen) == 0 && memcmp(m1, m3, mlen) == 0
|
||||
&& memcmp(m1, m4, mlen) == 0, "decrypted texts differ");
|
||||
ck_assert_msg(memcmp(m1, m, mlen) == 0, "wrong decrypted text");
|
||||
}
|
||||
}
|
||||
|
|
5
auto_tests/friends_test.c
Executable file → Normal file
5
auto_tests/friends_test.c
Executable file → Normal file
|
@ -91,8 +91,10 @@ int parent_friend_request(void)
|
|||
/* wait on the status change */
|
||||
for (i = 0; i < WAIT_COUNT; i++) {
|
||||
do_tox();
|
||||
|
||||
if (request_flags & FIRST_FLAG)
|
||||
break;
|
||||
|
||||
fputs(".", stdout);
|
||||
fflush(stdout);
|
||||
c_sleep(WAIT_TIME);
|
||||
|
@ -130,8 +132,10 @@ int parent_wait_for_message(void)
|
|||
|
||||
for (i = 0; i < WAIT_COUNT; i++) {
|
||||
do_tox();
|
||||
|
||||
if (request_flags & SECOND_FLAG)
|
||||
break;
|
||||
|
||||
fputs(".", stdout);
|
||||
fflush(stdout);
|
||||
c_sleep(WAIT_TIME);
|
||||
|
@ -165,6 +169,7 @@ int main(int argc, char *argv[])
|
|||
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
|
||||
fputs("friends_test: Starting test...\n", stdout);
|
||||
|
||||
if ((child_pid = fork()) == 0) {
|
||||
/* child */
|
||||
int i = 0;
|
||||
|
|
|
@ -42,8 +42,10 @@ unsigned char * hex_string_to_bin(char hex_string[])
|
|||
unsigned char *val = calloc(1, len);
|
||||
char *pos = hex_string;
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < len; ++i, pos += 2)
|
||||
sscanf(pos, "%2hhx", &val[i]);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
@ -157,6 +159,7 @@ START_TEST(test_setname)
|
|||
if (setname(m, (uint8_t *)good_name, bad_length) != -1)
|
||||
ck_abort_msg("setname() did NOT error on %d as a length argument!\n",
|
||||
bad_length);
|
||||
|
||||
if (setname(m, (uint8_t *)good_name, good_length) != 0)
|
||||
ck_abort_msg("setname() did NOT return 0 on good arguments!\n");
|
||||
}
|
||||
|
|
57
core/DHT.c
57
core/DHT.c
|
@ -133,9 +133,11 @@ static int id_closest(uint8_t * id, uint8_t * id1, uint8_t * id2)
|
|||
|
||||
if (distance1 < distance2)
|
||||
return 1;
|
||||
|
||||
if (distance1 > distance2)
|
||||
return 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -180,6 +182,7 @@ static int client_in_list(Client_data * list, uint32_t length, uint8_t * client_
|
|||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -246,6 +249,7 @@ static int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
|
|||
closest = id_closest( client_id,
|
||||
nodes_list[j].client_id,
|
||||
close_clientlist[i].client_id );
|
||||
|
||||
if (closest == 2) {
|
||||
memcpy( nodes_list[j].client_id,
|
||||
close_clientlist[i].client_id,
|
||||
|
@ -284,6 +288,7 @@ static int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
|
|||
closest = id_closest( client_id,
|
||||
nodes_list[k].client_id,
|
||||
friends_list[i].client_list[j].client_id );
|
||||
|
||||
if (closest == 2) {
|
||||
memcpy( nodes_list[k].client_id,
|
||||
friends_list[i].client_list[j].client_id,
|
||||
|
@ -296,6 +301,7 @@ static int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return num_nodes;
|
||||
}
|
||||
|
||||
|
@ -310,6 +316,7 @@ static int replace_bad( Client_data * list,
|
|||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = unix_time();
|
||||
|
||||
for (i = 0; i < length; ++i) {
|
||||
/* if node is bad */
|
||||
if (is_timeout(temp_time, list[i].timestamp, BAD_NODE_TIMEOUT)) {
|
||||
|
@ -331,9 +338,12 @@ static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_i
|
|||
{
|
||||
if (length == 0)
|
||||
return;
|
||||
|
||||
uint32_t i, count;
|
||||
|
||||
while (1) {
|
||||
count = 0;
|
||||
|
||||
for (i = 0; i < (length - 1); ++i) {
|
||||
if (id_closest(comp_client_id, list[i].client_id, list[i + 1].client_id) == 1) {
|
||||
Client_data temp = list[i + 1];
|
||||
|
@ -342,6 +352,7 @@ static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_i
|
|||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
return;
|
||||
}
|
||||
|
@ -635,6 +646,7 @@ static int handle_sendnodes(IP_Port source, uint8_t * packet, uint32_t length)
|
|||
return 1;
|
||||
|
||||
memcpy(&ping_id, plain, sizeof(ping_id));
|
||||
|
||||
if (!is_gettingnodes(source, ping_id))
|
||||
return 1;
|
||||
|
||||
|
@ -644,6 +656,7 @@ static int handle_sendnodes(IP_Port source, uint8_t * packet, uint32_t length)
|
|||
addto_lists(source, packet + 1);
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < num_nodes; ++i) {
|
||||
send_ping_request(nodes_list[i].ip_port, (clientid_t *) &nodes_list[i].client_id);
|
||||
returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
|
||||
|
@ -659,8 +672,10 @@ int DHT_addfriend(uint8_t * client_id)
|
|||
{
|
||||
if (friend_number(client_id) != -1) /*Is friend already in DHT?*/
|
||||
return 1;
|
||||
|
||||
Friend *temp;
|
||||
temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
|
||||
|
||||
if (temp == NULL)
|
||||
return 1;
|
||||
|
||||
|
@ -677,6 +692,7 @@ int DHT_delfriend(uint8_t * client_id)
|
|||
{
|
||||
uint32_t i;
|
||||
Friend *temp;
|
||||
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
/* Equal */
|
||||
if (id_equal(friends_list[i].client_id, client_id)) {
|
||||
|
@ -689,6 +705,7 @@ int DHT_delfriend(uint8_t * client_id)
|
|||
}
|
||||
|
||||
temp = realloc(friends_list, sizeof(Friend) * (num_friends));
|
||||
|
||||
if (temp == NULL)
|
||||
return 1;
|
||||
|
||||
|
@ -711,12 +728,15 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
|
|||
/* Equal */
|
||||
if (id_equal(friends_list[i].client_id, client_id)) {
|
||||
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
||||
if (id_equal(friends_list[i].client_list[j].client_id, client_id) && !is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT))
|
||||
if (id_equal(friends_list[i].client_list[j].client_id, client_id)
|
||||
&& !is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT))
|
||||
return friends_list[i].client_list[j].ip_port;
|
||||
}
|
||||
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
empty.ip.i = 1;
|
||||
return empty;
|
||||
}
|
||||
|
@ -733,6 +753,7 @@ static void doDHTFriends(void)
|
|||
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
uint32_t num_nodes = 0;
|
||||
|
||||
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
||||
/* if node is not dead. */
|
||||
if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) {
|
||||
|
@ -741,6 +762,7 @@ static void doDHTFriends(void)
|
|||
(clientid_t *) &friends_list[i].client_list[j].client_id );
|
||||
friends_list[i].client_list[j].last_pinged = temp_time;
|
||||
}
|
||||
|
||||
/* if node is good. */
|
||||
if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT)) {
|
||||
index[num_nodes] = j;
|
||||
|
@ -748,6 +770,7 @@ static void doDHTFriends(void)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) {
|
||||
rand_node = rand() % num_nodes;
|
||||
getnodes( friends_list[i].client_list[index[rand_node]].ip_port,
|
||||
|
@ -779,6 +802,7 @@ static void doClose(void)
|
|||
(clientid_t *) &close_clientlist[i].client_id );
|
||||
close_clientlist[i].last_pinged = temp_time;
|
||||
}
|
||||
|
||||
/* if node is good. */
|
||||
if (!is_timeout(temp_time, close_clientlist[i].timestamp, BAD_NODE_TIMEOUT)) {
|
||||
index[num_nodes] = i;
|
||||
|
@ -847,6 +871,7 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
|
|||
++num_ips;
|
||||
}
|
||||
}
|
||||
|
||||
return num_ips;
|
||||
}
|
||||
|
||||
|
@ -856,6 +881,7 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
|
|||
int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
int num = friend_number(friend_id);
|
||||
|
||||
if (num == -1)
|
||||
return 0;
|
||||
|
||||
|
@ -874,6 +900,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
|
|||
++sent;
|
||||
}
|
||||
}
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
|
@ -883,6 +910,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
|
|||
static int routeone_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length)
|
||||
{
|
||||
int num = friend_number(friend_id);
|
||||
|
||||
if (num == -1)
|
||||
return 0;
|
||||
|
||||
|
@ -903,10 +931,13 @@ static int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t len
|
|||
++n;
|
||||
}
|
||||
}
|
||||
|
||||
if (n < 1)
|
||||
return 0;
|
||||
|
||||
if (sendpacket(ip_list[rand() % n], packet, length) == length)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -919,11 +950,13 @@ static int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t len
|
|||
int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
/* Equal */
|
||||
if (id_equal(friends_list[i].client_id, friend_id))
|
||||
return friend_iplist(ip_portlist, i);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -952,6 +985,7 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
|
|||
|
||||
if (num == 0)
|
||||
return -1;
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
@ -962,6 +996,7 @@ static int handle_NATping(IP_Port source, uint8_t * source_pubkey, uint8_t * pac
|
|||
memcpy(&ping_id, packet + 1, sizeof(uint64_t));
|
||||
|
||||
int friendnumber = friend_number(source_pubkey);
|
||||
|
||||
if (friendnumber == -1)
|
||||
return 1;
|
||||
|
||||
|
@ -979,6 +1014,7 @@ static int handle_NATping(IP_Port source, uint8_t * source_pubkey, uint8_t * pac
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -990,6 +1026,7 @@ static int handle_NATping(IP_Port source, uint8_t * source_pubkey, uint8_t * pac
|
|||
static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num)
|
||||
{
|
||||
IP zero = {{0}};
|
||||
|
||||
if (len > MAX_FRIEND_CLIENTS)
|
||||
return zero;
|
||||
|
||||
|
@ -1001,9 +1038,11 @@ static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num)
|
|||
if (ip_portlist[i].ip.i == ip_portlist[j].ip.i)
|
||||
++numbers[i];
|
||||
}
|
||||
|
||||
if (numbers[i] >= min_num)
|
||||
return ip_portlist[i].ip;
|
||||
}
|
||||
|
||||
return zero;
|
||||
}
|
||||
|
||||
|
@ -1023,6 +1062,7 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_
|
|||
++num;
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
@ -1040,6 +1080,7 @@ static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t
|
|||
IP_Port pinging = {ip, htons(port)};
|
||||
send_ping_request(pinging, (clientid_t *) &friends_list[friend_num].client_id);
|
||||
}
|
||||
|
||||
friends_list[friend_num].punching_index = i;
|
||||
}
|
||||
|
||||
|
@ -1060,11 +1101,13 @@ static void doNAT(void)
|
|||
send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/
|
||||
friends_list[i].NATping_timestamp = temp_time;
|
||||
}
|
||||
|
||||
if (friends_list[i].hole_punching == 1 &&
|
||||
friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time &&
|
||||
friends_list[i].recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) {
|
||||
|
||||
IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2);
|
||||
|
||||
if (ip.i == 0)
|
||||
continue;
|
||||
|
||||
|
@ -1094,7 +1137,9 @@ int add_toping(uint8_t *client_id, IP_Port ip_port)
|
|||
{
|
||||
if (ip_port.ip.i == 0)
|
||||
return -1;
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_TOPING; ++i) {
|
||||
if (toping[i].ip_port.ip.i == 0) {
|
||||
memcpy(toping[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
|
@ -1103,6 +1148,7 @@ int add_toping(uint8_t *client_id, IP_Port ip_port)
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_TOPING; ++i) {
|
||||
if (id_closest(self_public_key, toping[i].client_id, client_id) == 2) {
|
||||
memcpy(toping[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
|
@ -1111,6 +1157,7 @@ int add_toping(uint8_t *client_id, IP_Port ip_port)
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -1119,13 +1166,17 @@ int add_toping(uint8_t *client_id, IP_Port ip_port)
|
|||
static void do_toping()
|
||||
{
|
||||
uint64_t temp_time = unix_time();
|
||||
|
||||
if (!is_timeout(temp_time, last_toping, TIME_TOPING))
|
||||
return;
|
||||
|
||||
last_toping = temp_time;
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_TOPING; ++i) {
|
||||
if (toping[i].ip_port.ip.i == 0)
|
||||
return;
|
||||
|
||||
send_ping_request(toping[i].ip_port, (clientid_t *) toping[i].client_id);
|
||||
toping[i].ip_port.ip.i = 0;
|
||||
}
|
||||
|
@ -1192,11 +1243,13 @@ int DHT_load(uint8_t * data, uint32_t size)
|
|||
|
||||
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
||||
client = &tempfriends_list[i].client_list[j];
|
||||
|
||||
if (client->timestamp != 0)
|
||||
getnodes(client->ip_port, client->client_id, tempfriends_list[i].client_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Client_data *tempclose_clientlist = (Client_data *)data;
|
||||
|
||||
for (i = 0; i < LCLIENT_LIST; ++i) {
|
||||
|
@ -1204,6 +1257,7 @@ int DHT_load(uint8_t * data, uint32_t size)
|
|||
DHT_bootstrap( tempclose_clientlist[i].ip_port,
|
||||
tempclose_clientlist[i].client_id );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1219,5 +1273,6 @@ int DHT_isconnected(void)
|
|||
if (!is_timeout(temp_time, close_clientlist[i].timestamp, BAD_NODE_TIMEOUT))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -51,6 +51,7 @@ static uint32_t get_broadcast(void)
|
|||
ifconf.ifc_buf = (char *)i_faces;
|
||||
ifconf.ifc_len = sizeof(i_faces);
|
||||
count = ifconf.ifc_len / sizeof(struct ifreq);
|
||||
|
||||
if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) {
|
||||
perror("get_broadcast: ioctl() error");
|
||||
return 0;
|
||||
|
@ -69,7 +70,9 @@ static uint32_t get_broadcast(void)
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(sock);
|
||||
|
||||
if (sock_holder == NULL) {
|
||||
perror("[!] no broadcast device found");
|
||||
return 0;
|
||||
|
@ -85,9 +88,11 @@ static IP broadcast_ip(void)
|
|||
IP ip;
|
||||
#ifdef __linux
|
||||
ip.i = get_broadcast();
|
||||
|
||||
if (ip.i == 0)
|
||||
/* error errored, but try anyway? */
|
||||
ip.i = ~0;
|
||||
|
||||
#else
|
||||
ip.i = ~0;
|
||||
#endif
|
||||
|
@ -100,14 +105,19 @@ static int LAN_ip(IP ip)
|
|||
{
|
||||
if (ip.c[0] == 127)/* Loopback */
|
||||
return 0;
|
||||
|
||||
if (ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */
|
||||
return 0;
|
||||
|
||||
if (ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */
|
||||
return 0;
|
||||
|
||||
if (ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */
|
||||
return 0;
|
||||
|
||||
if (ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -115,8 +125,10 @@ static int handle_LANdiscovery(IP_Port source, uint8_t *packet, uint32_t length)
|
|||
{
|
||||
if (LAN_ip(source.ip) == -1)
|
||||
return 1;
|
||||
|
||||
if (length != crypto_box_PUBLICKEYBYTES + 1)
|
||||
return 1;
|
||||
|
||||
DHT_bootstrap(source, packet + 1);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -130,6 +130,7 @@ static uint32_t connections_number; /* Number of connections in connections arra
|
|||
int getconnection_id(IP_Port ip_port)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].ip_port.ip.i == ip_port.ip.i &&
|
||||
connections[i].ip_port.port == ip_port.port &&
|
||||
|
@ -152,11 +153,14 @@ static uint32_t randtable[6][256];
|
|||
static uint32_t handshake_id(IP_Port source)
|
||||
{
|
||||
uint32_t id = 0, i;
|
||||
|
||||
for (i = 0; i < 6; ++i) {
|
||||
if (randtable[i][((uint8_t *)&source)[i]] == 0)
|
||||
randtable[i][((uint8_t *)&source)[i]] = random_int();
|
||||
|
||||
id ^= randtable[i][((uint8_t *)&source)[i]];
|
||||
}
|
||||
|
||||
if (id == 0) /* id can't be zero */
|
||||
id = 1;
|
||||
|
||||
|
@ -183,6 +187,7 @@ static void change_handshake(IP_Port source)
|
|||
int new_connection(IP_Port ip_port)
|
||||
{
|
||||
int connect = getconnection_id(ip_port);
|
||||
|
||||
if (connect != -1)
|
||||
return connect;
|
||||
|
||||
|
@ -199,6 +204,7 @@ int new_connection(IP_Port ip_port)
|
|||
}
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 0) {
|
||||
memset(&connections[i], 0, sizeof(Connection));
|
||||
|
@ -226,6 +232,7 @@ int new_connection(IP_Port ip_port)
|
|||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -252,6 +259,7 @@ static int new_inconnection(IP_Port ip_port)
|
|||
}
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 0) {
|
||||
memset(&connections[i], 0, sizeof(Connection));
|
||||
|
@ -277,6 +285,7 @@ static int new_inconnection(IP_Port ip_port)
|
|||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -287,6 +296,7 @@ static int new_inconnection(IP_Port ip_port)
|
|||
int incoming_connection(void)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].inbound == 2) {
|
||||
connections[i].inbound = 1;
|
||||
|
@ -301,6 +311,7 @@ int incoming_connection(void)
|
|||
static void free_connections(void)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = connections_length; i != 0; --i)
|
||||
if (connections[i - 1].status != 0)
|
||||
break;
|
||||
|
@ -310,6 +321,7 @@ static void free_connections(void)
|
|||
|
||||
Connection *temp;
|
||||
temp = realloc(connections, sizeof(Connection) * i);
|
||||
|
||||
if (temp == NULL && i != 0)
|
||||
return;
|
||||
|
||||
|
@ -332,6 +344,7 @@ int kill_connection(int connection_id)
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -348,6 +361,7 @@ int kill_connection_in(int connection_id, uint32_t seconds)
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -363,6 +377,7 @@ int is_connected(int connection_id)
|
|||
{
|
||||
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
||||
return connections[connection_id].status;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -371,6 +386,7 @@ IP_Port connection_ip(int connection_id)
|
|||
{
|
||||
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
||||
return connections[connection_id].ip_port;
|
||||
|
||||
IP_Port zero = {{{0}}, 0};
|
||||
return zero;
|
||||
}
|
||||
|
@ -380,6 +396,7 @@ uint32_t sendqueue(int connection_id)
|
|||
{
|
||||
if (connection_id < 0 || connection_id >= MAX_CONNECTIONS)
|
||||
return 0;
|
||||
|
||||
return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent;
|
||||
}
|
||||
|
||||
|
@ -388,6 +405,7 @@ uint32_t recvqueue(int connection_id)
|
|||
{
|
||||
if (connection_id < 0 || connection_id >= MAX_CONNECTIONS)
|
||||
return 0;
|
||||
|
||||
return connections[connection_id].recv_packetnum - connections[connection_id].successful_read;
|
||||
}
|
||||
|
||||
|
@ -416,6 +434,7 @@ int read_packet(int connection_id, uint8_t * data)
|
|||
connections[connection_id].recvbuffer[index].size = 0;
|
||||
return size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -530,6 +549,7 @@ static int send_DATA(uint32_t connection_id)
|
|||
{
|
||||
int ret;
|
||||
uint32_t buffer[BUFFER_PACKET_NUM];
|
||||
|
||||
if (connections[connection_id].num_req_paquets > 0) {
|
||||
ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]);
|
||||
connections[connection_id].num_req_paquets--;
|
||||
|
@ -537,11 +557,13 @@ static int send_DATA(uint32_t connection_id)
|
|||
memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) {
|
||||
ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum);
|
||||
connections[connection_id].sent_packetnum++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -573,6 +595,7 @@ static int handle_handshake(IP_Port source, uint8_t * packet, uint32_t length)
|
|||
send_handshake(source, handshake_id(source), handshake_id1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (is_connected(connection) != 1)
|
||||
return 1;
|
||||
|
||||
|
@ -595,9 +618,11 @@ static int SYNC_valid(uint32_t length)
|
|||
{
|
||||
if (length < 4 + 4 + 2)
|
||||
return 0;
|
||||
|
||||
if (length > (BUFFER_PACKET_NUM * 4 + 4 + 4 + 2) ||
|
||||
((length - 4 - 4 - 2) % 4) != 0)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -606,6 +631,7 @@ static int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_p
|
|||
{
|
||||
if (handshake_id(source) == recv_packetnum) {
|
||||
int x = new_inconnection(source);
|
||||
|
||||
if (x != -1) {
|
||||
connections[x].orecv_packetnum = recv_packetnum;
|
||||
connections[x].sent_packetnum = recv_packetnum;
|
||||
|
@ -618,6 +644,7 @@ static int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_p
|
|||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -632,10 +659,12 @@ static int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packet
|
|||
send_SYNC(connection_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
/* case 3 in handle_SYNC: */
|
||||
static int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets,
|
||||
static int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum,
|
||||
uint32_t *req_packets,
|
||||
uint16_t number)
|
||||
{
|
||||
uint8_t comp_counter = (counter - connections[connection_id].recv_counter );
|
||||
|
@ -666,6 +695,7 @@ static int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packet
|
|||
connections[connection_id].num_req_paquets = number;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -701,6 +731,7 @@ static int handle_SYNC(IP_Port source, uint8_t *packet, uint32_t length)
|
|||
if (connections[connection].status == 3)
|
||||
return handle_SYNC3(connection, counter, recv_packetnum,
|
||||
sent_packetnum, req_packets, number);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -785,6 +816,7 @@ static void doNew(void)
|
|||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 1)
|
||||
if ((connections[i].last_sent + (1000000UL / connections[i].SYNC_rate)) <= temp_time) {
|
||||
|
@ -809,6 +841,7 @@ static void doSYNC(void)
|
|||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 2 || connections[i].status == 3)
|
||||
if ((connections[i].last_SYNC + (1000000UL / connections[i].SYNC_rate)) <= temp_time) {
|
||||
|
@ -823,11 +856,13 @@ static void doData(void)
|
|||
uint32_t i;
|
||||
uint64_t j;
|
||||
uint64_t temp_time = current_time();
|
||||
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i)
|
||||
if (connections[i].status == 3 && sendqueue(i) != 0)
|
||||
if ((connections[i].last_sent + (1000000UL / connections[i].data_rate)) <= temp_time) {
|
||||
for (j = connections[i].last_sent; j < temp_time; j += (1000000UL / connections[i].data_rate))
|
||||
send_DATA(i);
|
||||
|
||||
connections[i].last_sent = temp_time;
|
||||
}
|
||||
}
|
||||
|
@ -843,9 +878,11 @@ static void adjustRates(void)
|
|||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
|
||||
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
||||
if (connections[i].status == 1 || connections[i].status == 2)
|
||||
connections[i].SYNC_rate = MAX_SYNC_RATE;
|
||||
|
||||
if (connections[i].status == 3) {
|
||||
if (sendqueue(i) != 0) {
|
||||
connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
|
||||
|
|
118
core/Messenger.c
118
core/Messenger.c
|
@ -35,10 +35,13 @@ static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_i
|
|||
|
||||
/* set the size of the friend list to numfriends
|
||||
return -1 if realloc fails */
|
||||
int realloc_friendlist(Messenger *m, uint32_t num) {
|
||||
int realloc_friendlist(Messenger *m, uint32_t num)
|
||||
{
|
||||
Friend *newfriendlist = realloc(m->friendlist, num * sizeof(Friend));
|
||||
|
||||
if (newfriendlist == NULL)
|
||||
return -1;
|
||||
|
||||
memset(&newfriendlist[num - 1], 0, sizeof(Friend));
|
||||
m->friendlist = newfriendlist;
|
||||
return 0;
|
||||
|
@ -85,8 +88,10 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len)
|
|||
uint8_t checksum[2] = {0};
|
||||
uint16_t check;
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < len; ++i)
|
||||
checksum[i % 2] ^= address[i];
|
||||
|
||||
memcpy(&check, checksum, sizeof(check));
|
||||
return check;
|
||||
}
|
||||
|
@ -128,22 +133,30 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
|
|||
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
|
||||
+ crypto_box_ZEROBYTES))
|
||||
return FAERR_TOOLONG;
|
||||
|
||||
uint8_t client_id[crypto_box_PUBLICKEYBYTES];
|
||||
memcpy(client_id, address, crypto_box_PUBLICKEYBYTES);
|
||||
uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
||||
memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check));
|
||||
|
||||
if (check != checksum)
|
||||
return FAERR_BADCHECKSUM;
|
||||
|
||||
if (length < 1)
|
||||
return FAERR_NOMESSAGE;
|
||||
|
||||
if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
return FAERR_OWNKEY;
|
||||
|
||||
int friend_id = getfriend_id(m, client_id);
|
||||
|
||||
if (friend_id != -1) {
|
||||
uint32_t nospam;
|
||||
memcpy(&nospam, address + crypto_box_PUBLICKEYBYTES, sizeof(nospam));
|
||||
|
||||
if (m->friendlist[friend_id].friendrequest_nospam == nospam)
|
||||
return FAERR_ALREADYSENT;
|
||||
|
||||
m->friendlist[friend_id].friendrequest_nospam = nospam;
|
||||
return FAERR_SETNEWNOSPAM;
|
||||
}
|
||||
|
@ -153,6 +166,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
|
|||
return FAERR_NOMEM;
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i <= m->numfriends; ++i) {
|
||||
if (m->friendlist[i].status == NOFRIEND) {
|
||||
DHT_addfriend(client_id);
|
||||
|
@ -173,6 +187,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
|
|||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return FAERR_UNKNOWN;
|
||||
}
|
||||
|
||||
|
@ -186,6 +201,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t * client_id)
|
|||
return FAERR_NOMEM;
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i <= m->numfriends; ++i) {
|
||||
if (m->friendlist[i].status == NOFRIEND) {
|
||||
DHT_addfriend(client_id);
|
||||
|
@ -202,6 +218,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t * client_id)
|
|||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -223,6 +240,7 @@ int m_delfriend(Messenger *m, int friendnumber)
|
|||
if (m->friendlist[i - 1].status != NOFRIEND)
|
||||
break;
|
||||
}
|
||||
|
||||
m->numfriends = i;
|
||||
|
||||
if (realloc_friendlist(m, m->numfriends + 1) != 0)
|
||||
|
@ -240,6 +258,7 @@ int m_friendstatus(Messenger *m, int friendnumber)
|
|||
{
|
||||
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
||||
return NOFRIEND;
|
||||
|
||||
return m->friendlist[friendnumber].status;
|
||||
}
|
||||
|
||||
|
@ -250,9 +269,12 @@ uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_
|
|||
{
|
||||
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
||||
return 0;
|
||||
|
||||
uint32_t msgid = ++m->friendlist[friendnumber].message_id;
|
||||
|
||||
if (msgid == 0)
|
||||
msgid = 1; /* otherwise, false error */
|
||||
|
||||
if (m_sendmessage_withid(m, friendnumber, msgid, message, length)) {
|
||||
return msgid;
|
||||
}
|
||||
|
@ -264,6 +286,7 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui
|
|||
{
|
||||
if (length >= (MAX_DATA_SIZE - sizeof(theid)))
|
||||
return 0;
|
||||
|
||||
uint8_t temp[MAX_DATA_SIZE];
|
||||
theid = htonl(theid);
|
||||
memcpy(temp, &theid, sizeof(theid));
|
||||
|
@ -285,6 +308,7 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t * name, uint16_t l
|
|||
{
|
||||
if (length > MAX_NAME_LENGTH || length == 0)
|
||||
return 0;
|
||||
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length);
|
||||
}
|
||||
|
||||
|
@ -295,6 +319,7 @@ static int setfriendname(Messenger *m, int friendnumber, uint8_t * name)
|
|||
{
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
|
||||
memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
@ -309,11 +334,14 @@ int setname(Messenger *m, uint8_t * name, uint16_t length)
|
|||
{
|
||||
if (length > MAX_NAME_LENGTH || length == 0)
|
||||
return -1;
|
||||
|
||||
memcpy(m->name, name, length);
|
||||
m->name_length = length;
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < m->numfriends; ++i)
|
||||
m->friendlist[i].name_sent = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -344,6 +372,7 @@ int getname(Messenger *m, int friendnumber, uint8_t * name)
|
|||
{
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
|
||||
memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
@ -352,12 +381,15 @@ int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length)
|
|||
{
|
||||
if (length > MAX_STATUSMESSAGE_LENGTH)
|
||||
return -1;
|
||||
|
||||
memcpy(m->statusmessage, status, length);
|
||||
m->statusmessage_length = length;
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < m->numfriends; ++i)
|
||||
m->friendlist[i].statusmessage_sent = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -366,10 +398,13 @@ int m_set_userstatus(Messenger *m, USERSTATUS status)
|
|||
if (status >= USERSTATUS_INVALID) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
m->userstatus = status;
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < m->numfriends; ++i)
|
||||
m->friendlist[i].userstatus_sent = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -379,6 +414,7 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber)
|
|||
{
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
|
||||
return m->friendlist[friendnumber].statusmessage_length;
|
||||
}
|
||||
|
||||
|
@ -388,6 +424,7 @@ int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t * buf, uint32_t
|
|||
{
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
|
||||
memset(buf, 0, maxlen);
|
||||
memcpy(buf, m->friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
||||
return 0;
|
||||
|
@ -404,10 +441,13 @@ USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
|
|||
{
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return USERSTATUS_INVALID;
|
||||
|
||||
USERSTATUS status = m->friendlist[friendnumber].userstatus;
|
||||
|
||||
if (status >= USERSTATUS_INVALID) {
|
||||
status = USERSTATUS_NONE;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
@ -431,6 +471,7 @@ static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t * st
|
|||
{
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
|
||||
uint8_t *newstatus = calloc(length, 1);
|
||||
memcpy(newstatus, status, length);
|
||||
free(m->friendlist[friendnumber].statusmessage);
|
||||
|
@ -449,8 +490,10 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
|
|||
{
|
||||
if (yesno != 0 || yesno != 1)
|
||||
return;
|
||||
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return;
|
||||
|
||||
m->friendlist[friendnumber].receives_read_receipts = yesno;
|
||||
}
|
||||
|
||||
|
@ -462,7 +505,8 @@ void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t
|
|||
}
|
||||
|
||||
/* set the function that will be executed when a message from a friend is received. */
|
||||
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
|
||||
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata)
|
||||
{
|
||||
m->friend_message = function;
|
||||
m->friend_message_userdata = userdata;
|
||||
|
@ -474,13 +518,15 @@ void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t
|
|||
m->friend_action_userdata = userdata;
|
||||
}
|
||||
|
||||
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
|
||||
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata)
|
||||
{
|
||||
m->friend_namechange = function;
|
||||
m->friend_namechange_userdata = userdata;
|
||||
}
|
||||
|
||||
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
|
||||
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata)
|
||||
{
|
||||
m->friend_statusmessagechange = function;
|
||||
m->friend_statuschange_userdata = userdata;
|
||||
|
@ -508,10 +554,13 @@ static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_
|
|||
{
|
||||
if (!m->friend_connectionstatuschange)
|
||||
return;
|
||||
|
||||
if (status == NOFRIEND)
|
||||
return;
|
||||
|
||||
const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE;
|
||||
const uint8_t is_connected = status == FRIEND_ONLINE;
|
||||
|
||||
if (is_connected != was_connected)
|
||||
m->friend_connectionstatuschange(m, friendnumber, is_connected, m->friend_connectionstatuschange_userdata);
|
||||
}
|
||||
|
@ -526,8 +575,10 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
|
|||
{
|
||||
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
||||
return 0;
|
||||
|
||||
if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
||||
return 0;
|
||||
|
||||
uint8_t packet[length + 1];
|
||||
packet[0] = packet_id;
|
||||
memcpy(packet + 1, data, length);
|
||||
|
@ -552,6 +603,7 @@ int LANdiscovery(timer* t, void* arg)
|
|||
Messenger *initMessenger(void)
|
||||
{
|
||||
Messenger *m = calloc(1, sizeof(Messenger));
|
||||
|
||||
if ( ! m )
|
||||
return 0;
|
||||
|
||||
|
@ -576,7 +628,8 @@ Messenger * initMessenger(void)
|
|||
}
|
||||
|
||||
/* run this before closing shop */
|
||||
void cleanupMessenger(Messenger *m){
|
||||
void cleanupMessenger(Messenger *m)
|
||||
{
|
||||
/* FIXME TODO ideally cleanupMessenger will mirror initMessenger
|
||||
* this requires the other modules to expose cleanup functions
|
||||
*/
|
||||
|
@ -591,115 +644,153 @@ void doFriends(Messenger *m)
|
|||
uint32_t i;
|
||||
int len;
|
||||
uint8_t temp[MAX_DATA_SIZE];
|
||||
|
||||
for (i = 0; i < m->numfriends; ++i) {
|
||||
if (m->friendlist[i].status == FRIEND_ADDED) {
|
||||
int fr = send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, m->friendlist[i].info, m->friendlist[i].info_size);
|
||||
int fr = send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, m->friendlist[i].info,
|
||||
m->friendlist[i].info_size);
|
||||
|
||||
if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */
|
||||
set_friend_status(m, i, FRIEND_REQUESTED);
|
||||
else if (fr > 0)
|
||||
set_friend_status(m, i, FRIEND_REQUESTED);
|
||||
}
|
||||
if (m->friendlist[i].status == FRIEND_REQUESTED || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
|
||||
|
||||
if (m->friendlist[i].status == FRIEND_REQUESTED
|
||||
|| m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
|
||||
if (m->friendlist[i].status == FRIEND_REQUESTED) {
|
||||
if (m->friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/
|
||||
send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, m->friendlist[i].info, m->friendlist[i].info_size);
|
||||
send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, m->friendlist[i].info,
|
||||
m->friendlist[i].info_size);
|
||||
m->friendlist[i].friend_request_id = unix_time();
|
||||
}
|
||||
}
|
||||
|
||||
IP_Port friendip = DHT_getfriendip(m->friendlist[i].client_id);
|
||||
|
||||
switch (is_cryptoconnected(m->friendlist[i].crypt_connection_id)) {
|
||||
case 0:
|
||||
if (friendip.ip.i > 1)
|
||||
m->friendlist[i].crypt_connection_id = crypto_connect(m->friendlist[i].client_id, friendip);
|
||||
|
||||
break;
|
||||
|
||||
case 3: /* Connection is established */
|
||||
set_friend_status(m, i, FRIEND_ONLINE);
|
||||
m->friendlist[i].name_sent = 0;
|
||||
m->friendlist[i].userstatus_sent = 0;
|
||||
m->friendlist[i].statusmessage_sent = 0;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
crypto_kill(m->friendlist[i].crypt_connection_id);
|
||||
m->friendlist[i].crypt_connection_id = -1;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */
|
||||
if (m->friendlist[i].name_sent == 0) {
|
||||
if (m_sendname(m, i, m->name, m->name_length))
|
||||
m->friendlist[i].name_sent = 1;
|
||||
}
|
||||
|
||||
if (m->friendlist[i].statusmessage_sent == 0) {
|
||||
if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length))
|
||||
m->friendlist[i].statusmessage_sent = 1;
|
||||
}
|
||||
|
||||
if (m->friendlist[i].userstatus_sent == 0) {
|
||||
if (send_userstatus(m, i, m->userstatus))
|
||||
m->friendlist[i].userstatus_sent = 1;
|
||||
}
|
||||
|
||||
len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp);
|
||||
uint8_t packet_id = temp[0];
|
||||
uint8_t *data = temp + 1;
|
||||
int data_length = len - 1;
|
||||
|
||||
if (len > 0) {
|
||||
switch (packet_id) {
|
||||
case PACKET_ID_NICKNAME: {
|
||||
if (data_length >= MAX_NAME_LENGTH || data_length == 0)
|
||||
break;
|
||||
|
||||
if (m->friend_namechange)
|
||||
m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata);
|
||||
|
||||
memcpy(m->friendlist[i].name, data, data_length);
|
||||
m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */
|
||||
break;
|
||||
}
|
||||
|
||||
case PACKET_ID_STATUSMESSAGE: {
|
||||
if (data_length == 0)
|
||||
break;
|
||||
|
||||
uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1);
|
||||
memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
|
||||
|
||||
if (m->friend_statusmessagechange)
|
||||
m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH),
|
||||
m->friend_statuschange_userdata);
|
||||
|
||||
set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
|
||||
free(status);
|
||||
break;
|
||||
}
|
||||
|
||||
case PACKET_ID_USERSTATUS: {
|
||||
if (data_length != 1)
|
||||
break;
|
||||
|
||||
USERSTATUS status = data[0];
|
||||
|
||||
if (m->friend_userstatuschange)
|
||||
m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata);
|
||||
|
||||
set_friend_userstatus(m, i, status);
|
||||
break;
|
||||
}
|
||||
|
||||
case PACKET_ID_MESSAGE: {
|
||||
uint8_t *message_id = data;
|
||||
uint8_t message_id_length = 4;
|
||||
uint8_t *message = data + message_id_length;
|
||||
uint16_t message_length = data_length - message_id_length;
|
||||
|
||||
if (m->friendlist[i].receives_read_receipts) {
|
||||
write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
|
||||
}
|
||||
|
||||
if (m->friend_message)
|
||||
(*m->friend_message)(m, i, message, message_length, m->friend_message_userdata);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case PACKET_ID_ACTION: {
|
||||
if (m->friend_action)
|
||||
(*m->friend_action)(m, i, data, data_length, m->friend_action_userdata);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case PACKET_ID_RECEIPT: {
|
||||
uint32_t msgid;
|
||||
|
||||
if (data_length < sizeof(msgid))
|
||||
break;
|
||||
|
||||
memcpy(&msgid, data, sizeof(msgid));
|
||||
msgid = ntohl(msgid);
|
||||
|
||||
if (m->read_receipt)
|
||||
(*m->read_receipt)(m, i, msgid, m->read_receipt_userdata);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -709,6 +800,7 @@ void doFriends(Messenger *m)
|
|||
m->friendlist[i].crypt_connection_id = -1;
|
||||
set_friend_status(m, i, FRIEND_CONFIRMED);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -721,8 +813,10 @@ void doInbound(Messenger *m)
|
|||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
||||
int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
|
||||
|
||||
if (inconnection != -1) {
|
||||
int friend_id = getfriend_id(m, public_key);
|
||||
|
||||
if (friend_id != -1) {
|
||||
crypto_kill(m->friendlist[friend_id].crypt_connection_id);
|
||||
m->friendlist[friend_id].crypt_connection_id =
|
||||
|
@ -779,8 +873,10 @@ int Messenger_load(Messenger *m, uint8_t * data, uint32_t length)
|
|||
{
|
||||
if (length == ~0)
|
||||
return -1;
|
||||
|
||||
if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3)
|
||||
return -1;
|
||||
|
||||
length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3;
|
||||
load_keys(data);
|
||||
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
||||
|
@ -794,12 +890,16 @@ int Messenger_load(Messenger *m, uint8_t * data, uint32_t length)
|
|||
|
||||
if (length < size)
|
||||
return -1;
|
||||
|
||||
length -= size;
|
||||
|
||||
if (DHT_load(data, size) == -1)
|
||||
return -1;
|
||||
|
||||
data += size;
|
||||
memcpy(&size, data, sizeof(size));
|
||||
data += sizeof(size);
|
||||
|
||||
if (length != size || length % sizeof(Friend) != 0)
|
||||
return -1;
|
||||
|
||||
|
@ -809,6 +909,7 @@ int Messenger_load(Messenger *m, uint8_t * data, uint32_t length)
|
|||
uint16_t num = size / sizeof(Friend);
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < num; ++i) {
|
||||
if (temp[i].status >= 3) {
|
||||
int fnum = m_addfriend_norequest(m, temp[i].client_id);
|
||||
|
@ -824,6 +925,7 @@ int Messenger_load(Messenger *m, uint8_t * data, uint32_t length)
|
|||
m_addfriend(m, address, temp[i].info, temp[i].info_size);
|
||||
}
|
||||
}
|
||||
|
||||
free(temp);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -76,7 +76,8 @@ typedef enum {
|
|||
USERSTATUS_AWAY,
|
||||
USERSTATUS_BUSY,
|
||||
USERSTATUS_INVALID
|
||||
} USERSTATUS;
|
||||
}
|
||||
USERSTATUS;
|
||||
|
||||
typedef struct {
|
||||
uint8_t client_id[CLIENT_ID_SIZE];
|
||||
|
@ -259,7 +260,8 @@ void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t
|
|||
|
||||
/* set the function that will be executed when a message from a friend is received.
|
||||
function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
|
||||
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
|
||||
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata);
|
||||
|
||||
/* set the function that will be executed when an action from a friend is received.
|
||||
function format is: function(int friendnumber, uint8_t * action, uint32_t length) */
|
||||
|
@ -268,12 +270,14 @@ void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t
|
|||
/* set the callback for name changes
|
||||
function(int friendnumber, uint8_t *newname, uint16_t length)
|
||||
you are not responsible for freeing newname */
|
||||
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
|
||||
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata);
|
||||
|
||||
/* set the callback for status message changes
|
||||
function(int friendnumber, uint8_t *newstatus, uint16_t length)
|
||||
you are not responsible for freeing newstatus */
|
||||
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
|
||||
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata);
|
||||
|
||||
/* set the callback for status type changes
|
||||
function(int friendnumber, USERSTATUS kind) */
|
||||
|
|
|
@ -40,7 +40,8 @@ int send_friendrequest(uint8_t * public_key, uint32_t nospam_num, uint8_t * data
|
|||
memcpy(temp, &nospam_num, sizeof(nospam_num));
|
||||
memcpy(temp + sizeof(nospam_num), data, length);
|
||||
uint8_t packet[MAX_DATA_SIZE];
|
||||
int len = create_request(packet, public_key, temp, length + sizeof(nospam_num), 32); /* 32 is friend request packet id */
|
||||
int len = create_request(packet, public_key, temp, length + sizeof(nospam_num),
|
||||
32); /* 32 is friend request packet id */
|
||||
|
||||
if (len == -1)
|
||||
return -1;
|
||||
|
@ -53,6 +54,7 @@ int send_friendrequest(uint8_t * public_key, uint32_t nospam_num, uint8_t * data
|
|||
if (ip_port.ip.i != 0) {
|
||||
if (sendpacket(ip_port, packet, len) != -1)
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -127,10 +129,13 @@ static int friendreq_handlepacket(IP_Port source, uint8_t * source_pubkey, uint8
|
|||
{
|
||||
if (handle_friendrequest_isset == 0)
|
||||
return 1;
|
||||
|
||||
if (length <= sizeof(nospam))
|
||||
return 1;
|
||||
|
||||
if (request_received(source_pubkey))
|
||||
return 1;
|
||||
|
||||
if (memcmp(packet, &nospam, sizeof(nospam)) != 0)
|
||||
return 1;
|
||||
|
||||
|
|
|
@ -65,9 +65,11 @@ uint8_t crypto_iszero(uint8_t *mem, uint32_t length)
|
|||
{
|
||||
uint8_t check = 0;
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < length; ++i) {
|
||||
check |= mem[i];
|
||||
}
|
||||
|
||||
return check; // We return zero if mem is made out of zeroes.
|
||||
}
|
||||
|
||||
|
@ -148,8 +150,10 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
|
|||
static void increment_nonce(uint8_t *nonce)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < crypto_box_NONCEBYTES; ++i) {
|
||||
++nonce[i];
|
||||
|
||||
if (nonce[i] != 0)
|
||||
break;
|
||||
}
|
||||
|
@ -159,6 +163,7 @@ static void increment_nonce(uint8_t *nonce)
|
|||
void random_nonce(uint8_t *nonce)
|
||||
{
|
||||
uint32_t i, temp;
|
||||
|
||||
for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) {
|
||||
temp = random_int();
|
||||
memcpy(nonce + 4 * i, &temp, 4);
|
||||
|
@ -172,21 +177,28 @@ int read_cryptpacket(int crypt_connection_id, uint8_t *data)
|
|||
{
|
||||
if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
return 0;
|
||||
|
||||
if (crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED)
|
||||
return 0;
|
||||
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data);
|
||||
|
||||
if (length == 0)
|
||||
return 0;
|
||||
|
||||
if (temp_data[0] != 3)
|
||||
return -1;
|
||||
|
||||
int len = decrypt_data_fast(crypto_connections[crypt_connection_id].shared_key,
|
||||
crypto_connections[crypt_connection_id].recv_nonce,
|
||||
temp_data + 1, length - 1, data);
|
||||
|
||||
if (len != -1) {
|
||||
increment_nonce(crypto_connections[crypt_connection_id].recv_nonce);
|
||||
return len;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -196,19 +208,26 @@ int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length)
|
|||
{
|
||||
if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
return 0;
|
||||
|
||||
if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
|
||||
return 0;
|
||||
|
||||
if (crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED)
|
||||
return 0;
|
||||
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
int len = encrypt_data_fast(crypto_connections[crypt_connection_id].shared_key,
|
||||
crypto_connections[crypt_connection_id].sent_nonce,
|
||||
data, length, temp_data + 1);
|
||||
|
||||
if (len == -1)
|
||||
return 0;
|
||||
|
||||
temp_data[0] = 3;
|
||||
|
||||
if (write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0)
|
||||
return 0;
|
||||
|
||||
increment_nonce(crypto_connections[crypt_connection_id].sent_nonce);
|
||||
return 1;
|
||||
}
|
||||
|
@ -223,6 +242,7 @@ int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t
|
|||
{
|
||||
if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING)
|
||||
return -1;
|
||||
|
||||
uint8_t nonce[crypto_box_NONCEBYTES];
|
||||
uint8_t temp[MAX_DATA_SIZE];
|
||||
memcpy(temp + 1, data, length);
|
||||
|
@ -230,8 +250,10 @@ int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t
|
|||
random_nonce(nonce);
|
||||
int len = encrypt_data(public_key, self_secret_key, nonce, temp, length + 1,
|
||||
1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
|
||||
|
||||
if (len == -1)
|
||||
return -1;
|
||||
|
||||
packet[0] = 32;
|
||||
memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES);
|
||||
memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES);
|
||||
|
@ -254,10 +276,13 @@ static int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *request_i
|
|||
uint8_t nonce[crypto_box_NONCEBYTES];
|
||||
uint8_t temp[MAX_DATA_SIZE];
|
||||
memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
|
||||
int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
|
||||
int len1 = decrypt_data(public_key, self_secret_key, nonce,
|
||||
packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
|
||||
length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
|
||||
|
||||
if (len1 == -1 || len1 == 0)
|
||||
return -1;
|
||||
|
||||
request_id[0] = temp[0];
|
||||
--len1;
|
||||
memcpy(data, temp + 1, len1);
|
||||
|
@ -279,14 +304,18 @@ static int cryptopacket_handle(IP_Port source, uint8_t * packet, uint32_t length
|
|||
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
|
||||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
|
||||
return 1;
|
||||
|
||||
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us.
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t data[MAX_DATA_SIZE];
|
||||
uint8_t number;
|
||||
int len = handle_request(public_key, data, &number, packet, length);
|
||||
|
||||
if (len == -1 || len == 0)
|
||||
return 1;
|
||||
|
||||
if (!cryptopackethandlers[number]) return 1;
|
||||
|
||||
cryptopackethandlers[number](source, public_key, data, len);
|
||||
|
||||
} else { /* if request is not for us, try routing it. */
|
||||
|
@ -294,6 +323,7 @@ static int cryptopacket_handle(IP_Port source, uint8_t * packet, uint32_t length
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -312,8 +342,10 @@ static int send_cryptohandshake(int connection_id, uint8_t *public_key, uint8_t
|
|||
|
||||
int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
|
||||
1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data);
|
||||
|
||||
if (len == -1)
|
||||
return 0;
|
||||
|
||||
temp_data[0] = 2;
|
||||
memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
|
||||
memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
|
||||
|
@ -327,12 +359,15 @@ static int handle_cryptohandshake(uint8_t *public_key, uint8_t *secret_nonce,
|
|||
uint8_t *session_key, uint8_t *data, uint16_t length)
|
||||
{
|
||||
int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
|
||||
|
||||
if (length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
|
||||
+ crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (data[0] != 2)
|
||||
return 0;
|
||||
|
||||
uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES];
|
||||
|
||||
memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES);
|
||||
|
@ -355,11 +390,13 @@ static int handle_cryptohandshake(uint8_t *public_key, uint8_t *secret_nonce,
|
|||
static int getcryptconnection_id(uint8_t *public_key)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status != CONN_NO_CONNECTION)
|
||||
if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -370,16 +407,21 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port)
|
|||
{
|
||||
uint32_t i;
|
||||
int id = getcryptconnection_id(public_key);
|
||||
|
||||
if (id != -1) {
|
||||
IP_Port c_ip = connection_ip(crypto_connections[id].number);
|
||||
|
||||
if (c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port)
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status == CONN_NO_CONNECTION) {
|
||||
int id = new_connection(ip_port);
|
||||
|
||||
if (id == -1)
|
||||
return -1;
|
||||
|
||||
crypto_connections[i].number = id;
|
||||
crypto_connections[i].status = CONN_HANDSHAKE_SENT;
|
||||
random_nonce(crypto_connections[i].recv_nonce);
|
||||
|
@ -391,9 +433,11 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port)
|
|||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1; /* this should never happen. */
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -407,6 +451,7 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port)
|
|||
int crypto_inbound(uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_INCOMING; ++i) {
|
||||
if (incoming_connections[i] != -1) {
|
||||
if (is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) {
|
||||
|
@ -414,9 +459,11 @@ int crypto_inbound(uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_
|
|||
incoming_connections[i] = -1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (id_packet(incoming_connections[i]) == 2) {
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
uint16_t len = read_packet(incoming_connections[i], temp_data);
|
||||
|
||||
if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
|
||||
int connection_id = incoming_connections[i];
|
||||
incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */
|
||||
|
@ -425,6 +472,7 @@ int crypto_inbound(uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -435,6 +483,7 @@ int crypto_kill(int crypt_connection_id)
|
|||
{
|
||||
if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
|
||||
return 1;
|
||||
|
||||
if (crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) {
|
||||
crypto_connections[crypt_connection_id].status = CONN_NO_CONNECTION;
|
||||
kill_connection(crypto_connections[crypt_connection_id].number);
|
||||
|
@ -442,6 +491,7 @@ int crypto_kill(int crypt_connection_id)
|
|||
crypto_connections[crypt_connection_id].number = ~0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -451,8 +501,10 @@ int crypto_kill(int crypt_connection_id)
|
|||
int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
if (connection_id == -1)
|
||||
return -1;
|
||||
|
||||
/*
|
||||
if(getcryptconnection_id(public_key) != -1)
|
||||
{
|
||||
|
@ -482,9 +534,11 @@ int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secre
|
|||
crypto_connections[i].status = CONN_NOT_CONFIRMED; /* set it to its proper value right after. */
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1; /* this should never happen. */
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -495,6 +549,7 @@ int is_cryptoconnected(int crypt_connection_id)
|
|||
{
|
||||
if (crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
|
||||
return crypto_connections[crypt_connection_id].status;
|
||||
|
||||
return CONN_NO_CONNECTION;
|
||||
}
|
||||
|
||||
|
@ -528,12 +583,14 @@ void load_keys(uint8_t *keys)
|
|||
static int new_incoming(int id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_INCOMING; ++i) {
|
||||
if (incoming_connections[i] == -1) {
|
||||
incoming_connections[i] = id;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -542,8 +599,10 @@ static int new_incoming(int id)
|
|||
static void handle_incomings(void)
|
||||
{
|
||||
int income;
|
||||
|
||||
while (1) {
|
||||
income = incoming_connection();
|
||||
|
||||
if (income == -1 || new_incoming(income) )
|
||||
break;
|
||||
}
|
||||
|
@ -553,6 +612,7 @@ static void handle_incomings(void)
|
|||
static void receive_crypto(void)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status == CONN_HANDSHAKE_SENT) {
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
|
@ -560,11 +620,14 @@ static void receive_crypto(void)
|
|||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint16_t len;
|
||||
|
||||
if (id_packet(crypto_connections[i].number) == 1)
|
||||
/* if the packet is a friend request drop it (because we are already friends) */
|
||||
len = read_packet(crypto_connections[i].number, temp_data);
|
||||
|
||||
if (id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */
|
||||
len = read_packet(crypto_connections[i].number, temp_data);
|
||||
|
||||
if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) {
|
||||
if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) {
|
||||
memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES);
|
||||
|
@ -583,6 +646,7 @@ static void receive_crypto(void)
|
|||
crypto_kill(crypto_connections[i].number);
|
||||
|
||||
}
|
||||
|
||||
if (crypto_connections[i].status == CONN_NOT_CONFIRMED) {
|
||||
if (id_packet(crypto_connections[i].number) == 3) {
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
|
@ -592,6 +656,7 @@ static void receive_crypto(void)
|
|||
crypto_connections[i].sessionsecret_key,
|
||||
crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data);
|
||||
uint32_t zero = 0;
|
||||
|
||||
if (len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) {
|
||||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
encrypt_precompute(crypto_connections[i].peersessionpublic_key,
|
||||
|
@ -619,6 +684,7 @@ void initNetCrypto(void)
|
|||
memset(incoming_connections, -1 , sizeof(incoming_connections));
|
||||
networking_registerhandler(32, &cryptopacket_handle);
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
crypto_connections[i].number = ~0;
|
||||
}
|
||||
|
@ -626,6 +692,7 @@ void initNetCrypto(void)
|
|||
static void killTimedout(void)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status != CONN_NO_CONNECTION && is_connected(crypto_connections[i].number) == 4)
|
||||
crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
|
|
|
@ -80,6 +80,7 @@ static int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
|||
uint32_t addrlen = sizeof(addr);
|
||||
#endif
|
||||
(*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||
|
||||
if (*(int32_t *)length <= 0)
|
||||
return -1; /* nothing received or empty packet */
|
||||
|
||||
|
@ -101,10 +102,11 @@ void networking_poll()
|
|||
uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||
uint32_t length;
|
||||
|
||||
while (receivepacket(&ip_port, data, &length) != -1)
|
||||
{
|
||||
while (receivepacket(&ip_port, data, &length) != -1) {
|
||||
if (length < 1) continue;
|
||||
|
||||
if (!packethandlers[data[0]]) continue;
|
||||
|
||||
packethandlers[data[0]](ip_port, data, length);
|
||||
}
|
||||
}
|
||||
|
@ -119,8 +121,10 @@ int init_networking(IP ip, uint16_t port)
|
|||
{
|
||||
#ifdef WIN32
|
||||
WSADATA wsaData;
|
||||
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR)
|
||||
return -1;
|
||||
|
||||
#else
|
||||
srandom((uint32_t)current_time());
|
||||
#endif
|
||||
|
@ -131,11 +135,15 @@ int init_networking(IP ip, uint16_t port)
|
|||
|
||||
/* Check for socket error */
|
||||
#ifdef WIN32
|
||||
|
||||
if (sock == INVALID_SOCKET) /* MSDN recommends this */
|
||||
return -1;
|
||||
|
||||
#else
|
||||
|
||||
if (sock < 0)
|
||||
return -1;
|
||||
|
||||
#endif
|
||||
|
||||
/* Functions to increase the size of the send and receive UDP buffers
|
||||
|
|
19
core/timer.c
19
core/timer.c
|
@ -46,8 +46,7 @@ enum timer_state {
|
|||
STATE_CALLBACK
|
||||
};
|
||||
|
||||
struct timer
|
||||
{
|
||||
struct timer {
|
||||
enum timer_state state;
|
||||
timer *_prev;
|
||||
timer *_next;
|
||||
|
@ -68,13 +67,16 @@ inline static void timer_dequeue(timer* t, timer** queue)
|
|||
} else {
|
||||
*queue = t->_next;
|
||||
}
|
||||
|
||||
if (t->_next) t->_next->_prev = t->_prev;
|
||||
|
||||
t->state = STATE_INACTIVE;
|
||||
}
|
||||
|
||||
static void timer_enqueue(timer *t, timer **queue, timer *prev)
|
||||
{
|
||||
t->state = STATE_ACTIVE;
|
||||
|
||||
while (true) {
|
||||
if (!*queue) {
|
||||
t->_next = 0;
|
||||
|
@ -109,12 +111,14 @@ static timer* timer_pool; /* timer_pool is SINGLY LINKED!! */
|
|||
timer *new_timer(void)
|
||||
{
|
||||
timer *ret;
|
||||
|
||||
if (timer_pool) {
|
||||
ret = timer_pool;
|
||||
timer_pool = timer_pool->_next;
|
||||
} else {
|
||||
ret = calloc(1, sizeof(struct timer));
|
||||
}
|
||||
|
||||
ret->state = STATE_INACTIVE;
|
||||
return ret;
|
||||
}
|
||||
|
@ -153,13 +157,16 @@ static void timer_delay_us(timer* t, int us)
|
|||
void timer_start(timer *t, int sec)
|
||||
{
|
||||
uint64_t newdeadline = current_time() + sec * US_PER_SECOND;
|
||||
|
||||
if (timer_is_active(t)) {
|
||||
if (t->deadline < newdeadline) {
|
||||
timer_delay_us(t, newdeadline - t->deadline);
|
||||
return;
|
||||
}
|
||||
|
||||
timer_dequeue(t, &timer_main_queue);
|
||||
}
|
||||
|
||||
t->deadline = newdeadline;
|
||||
timer_enqueue(t, &timer_main_queue, 0);
|
||||
}
|
||||
|
@ -177,6 +184,7 @@ int timer_stop(timer* t)
|
|||
int timer_delay(timer *t, int additonalsec)
|
||||
{
|
||||
if (!timer_is_active(t)) return -1;
|
||||
|
||||
timer_delay_us(t, additonalsec * US_PER_SECOND);
|
||||
return 0;
|
||||
}
|
||||
|
@ -184,6 +192,7 @@ int timer_delay(timer* t, int additonalsec)
|
|||
static uint64_t timer_diff(timer *t, uint64_t time)
|
||||
{
|
||||
if (t->deadline <= time) return 0;
|
||||
|
||||
return time - t->deadline;
|
||||
}
|
||||
|
||||
|
@ -193,6 +202,7 @@ static uint64_t timer_diff(timer* t, uint64_t time)
|
|||
int timer_time_remaining(timer *t)
|
||||
{
|
||||
if (!timer_is_active(t)) return -1;
|
||||
|
||||
return timer_diff(t, current_time()) / US_PER_SECOND;
|
||||
}
|
||||
|
||||
|
@ -228,6 +238,7 @@ void timer_poll(void)
|
|||
/* Handle millisecond timers */
|
||||
while (timer_us_queue) {
|
||||
if (timer_diff(timer_us_queue, time) != 0) break;
|
||||
|
||||
timer *t = timer_us_queue;
|
||||
timer_dequeue(t, &timer_us_queue);
|
||||
t->cb(0, t->userdata);
|
||||
|
@ -240,14 +251,17 @@ void timer_poll(void)
|
|||
|
||||
while (timer_main_queue) {
|
||||
if (timer_diff(timer_main_queue, time) != 0) break;
|
||||
|
||||
timer *t = timer_main_queue;
|
||||
t->state = STATE_CALLBACK;
|
||||
int rv = t->cb(t, t->userdata);
|
||||
|
||||
if (rv != 0) {
|
||||
timer_dequeue(t, &timer_main_queue);
|
||||
delete_timer(t);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (t->state != STATE_ACTIVE) {
|
||||
timer_dequeue(t, &timer_main_queue);
|
||||
}
|
||||
|
@ -268,6 +282,7 @@ void timer_debug_print()
|
|||
{
|
||||
timer *t = timer_main_queue;
|
||||
printf("Queue:\n");
|
||||
|
||||
while (t) {
|
||||
printf("%" PRIu64 " (%" PRIu64 ") : %s\n", t->deadline, t->deadline / US_PER_SECOND, (char *)t->userdata);
|
||||
t = t->_next;
|
||||
|
|
|
@ -50,13 +50,16 @@ void manage_keys()
|
|||
uint8_t keys[KEYS_SIZE];
|
||||
|
||||
FILE *keys_file = fopen("key", "r");
|
||||
|
||||
if (keys_file != NULL) {
|
||||
//if file was opened successfully -- load keys
|
||||
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
|
||||
|
||||
if (read_size != KEYS_SIZE) {
|
||||
printf("Error while reading the key file\nExiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
load_keys(keys);
|
||||
printf("Keys loaded successfully\n");
|
||||
} else {
|
||||
|
@ -64,10 +67,12 @@ void manage_keys()
|
|||
new_keys();
|
||||
save_keys(keys);
|
||||
keys_file = fopen("key", "w");
|
||||
|
||||
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
|
||||
printf("Error while writing the key file.\nExiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Keys saved successfully\n");
|
||||
}
|
||||
|
||||
|
@ -83,10 +88,10 @@ int main(int argc, char *argv[])
|
|||
FILE *file;
|
||||
file = fopen("PUBLIC_ID.txt", "w");
|
||||
|
||||
for(i = 0; i < 32; i++)
|
||||
{
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (self_public_key[i] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", self_public_key[i]);
|
||||
fprintf(file, "%hhX", self_public_key[i]);
|
||||
}
|
||||
|
@ -117,19 +122,20 @@ int main(int argc, char *argv[])
|
|||
friendreq_init();
|
||||
|
||||
int is_waiting_for_dht_connection = 1;
|
||||
while(1)
|
||||
{
|
||||
if (is_waiting_for_dht_connection && DHT_isconnected())
|
||||
{
|
||||
|
||||
while (1) {
|
||||
if (is_waiting_for_dht_connection && DHT_isconnected()) {
|
||||
printf("Connected to other bootstrap server successfully.\n");
|
||||
is_waiting_for_dht_connection = 0;
|
||||
}
|
||||
|
||||
doDHT();
|
||||
|
||||
networking_poll();
|
||||
|
||||
c_sleep(1);
|
||||
}
|
||||
|
||||
shutdown_networking();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -55,19 +55,20 @@ struct server_conf_s {
|
|||
struct server_info_s info[32];
|
||||
};
|
||||
|
||||
int b16_to_key(char b16_string[], uint8_t *bs_pubkey) {
|
||||
int b16_to_key(char b16_string[], uint8_t *bs_pubkey)
|
||||
{
|
||||
|
||||
int i;
|
||||
unsigned int num1 = 0, num2 = 0;
|
||||
|
||||
for(i = 0; i < 32; ++i)
|
||||
{
|
||||
for (i = 0; i < 32; ++i) {
|
||||
sscanf(&b16_string[i * 2], "%1X", &num1);
|
||||
sscanf(&b16_string[i * 2 + 1], "%1X", &num2);
|
||||
num1 = num1 << 4;
|
||||
bs_pubkey[i] = bs_pubkey[i] | num1;
|
||||
bs_pubkey[i] = bs_pubkey[i] | num2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -92,11 +93,13 @@ int connect_to_servers(struct server_info_s *info)
|
|||
/* Check if we're connected to the DHT */
|
||||
for (c = 0; c != 100; ++c) {
|
||||
usleep(10000);
|
||||
|
||||
if (DHT_isconnected()) {
|
||||
//puts("Connected");
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (DHT_isconnected() == 0 && c == 99) {
|
||||
//puts("Not connected");
|
||||
return -1;
|
||||
|
@ -123,12 +126,14 @@ void manage_keys(char *keys_file)
|
|||
if (stat(keys_file, &existence) >= 0) {
|
||||
keysf = fopen(keys_file, "r");
|
||||
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keysf);
|
||||
|
||||
if (read_size != KEYS_SIZE) {
|
||||
printf("Error while reading the key file\nExiting.\n");
|
||||
exit(1);
|
||||
} else {
|
||||
printf("Keys loaded successfully\n");
|
||||
}
|
||||
|
||||
load_keys(keys);
|
||||
|
||||
} else {
|
||||
|
@ -137,6 +142,7 @@ void manage_keys(char *keys_file)
|
|||
new_keys();
|
||||
save_keys(keys);
|
||||
keysf = fopen(keys_file, "w");
|
||||
|
||||
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keysf) != KEYS_SIZE) {
|
||||
printf("Error while writing the key file.\nExiting.\n");
|
||||
exit(1);
|
||||
|
@ -183,8 +189,7 @@ struct server_conf_s configure_server(char *cfg_file)
|
|||
config_init(&cfg);
|
||||
|
||||
/* Read the file. If there is an error, report it and exit. */
|
||||
if(! config_read_file(&cfg, cfg_file))
|
||||
{
|
||||
if (! config_read_file(&cfg, cfg_file)) {
|
||||
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
|
||||
config_error_line(&cfg), config_error_text(&cfg));
|
||||
config_destroy(&cfg);
|
||||
|
@ -217,12 +222,14 @@ struct server_conf_s configure_server(char *cfg_file)
|
|||
|
||||
/* Get all the servers in the list */
|
||||
server_list = config_lookup(&cfg, "bootstrap_servers");
|
||||
|
||||
if (server_list != NULL) {
|
||||
int count = config_setting_length(server_list);
|
||||
int i;
|
||||
|
||||
char tmp_ip[30]; /* IP */
|
||||
char tmp_pk[64]; /* bs_pk */
|
||||
|
||||
for (i = 0; i < count; ++i) {
|
||||
config_setting_t *server = config_setting_get_elem(server_list, i);
|
||||
/* Get a pointer on the key aray */
|
||||
|
@ -274,7 +281,8 @@ struct server_conf_s configure_server(char *cfg_file)
|
|||
return server_conf;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
pid_t pid, sid; /* Process- and Session-ID */
|
||||
struct server_conf_s server_conf;
|
||||
|
@ -311,14 +319,15 @@ int main(int argc, char *argv[]) {
|
|||
/* Public key */
|
||||
int i;
|
||||
printf("\nPublic Key: ");
|
||||
for(i = 0; i < 32; ++i)
|
||||
{
|
||||
|
||||
for (i = 0; i < 32; ++i) {
|
||||
uint8_t ln, hn;
|
||||
ln = 0x0F & self_public_key[i];
|
||||
hn = 0xF0 & self_public_key[i];
|
||||
hn = hn >> 4;
|
||||
printf("%X%X", hn, ln);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
/* initialize networking
|
||||
|
@ -355,6 +364,7 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
/* Fork off from the parent process */
|
||||
pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
printf("Forking failed.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
|
@ -378,6 +388,7 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
/* Create a new SID for the child process */
|
||||
sid = setsid();
|
||||
|
||||
if (sid < 0) {
|
||||
printf("SID creation failure.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
|
@ -396,8 +407,7 @@ int main(int argc, char *argv[]) {
|
|||
/* Main loop */
|
||||
friendreq_init();
|
||||
|
||||
while(1)
|
||||
{
|
||||
while (1) {
|
||||
doDHT();
|
||||
|
||||
networking_poll();
|
||||
|
|
|
@ -58,7 +58,8 @@
|
|||
|
||||
void printip(IP_Port ip_port)
|
||||
{
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port));
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u\n", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3],
|
||||
ntohs(ip_port.port));
|
||||
}
|
||||
|
||||
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
||||
|
@ -69,14 +70,18 @@ int main(int argc, char *argv[])
|
|||
printf("usage %s ip port filename(of file to send)\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
new_keys();
|
||||
printf("OUR ID: ");
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (self_public_key[i] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", self_public_key[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
memcpy(self_client_id, self_public_key, 32);
|
||||
|
@ -122,9 +127,18 @@ int main(int argc, char *argv[])
|
|||
uint8_t buffer2[128];
|
||||
int read2 = 0;
|
||||
FILE *file1 = fopen(argv[3], "rb");
|
||||
if ( file1==NULL ){printf("Error opening file.\n");return 1;}
|
||||
|
||||
if ( file1 == NULL ) {
|
||||
printf("Error opening file.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE *file2 = fopen("received.txt", "wb");
|
||||
if ( file2==NULL ){return 1;}
|
||||
|
||||
if ( file2 == NULL ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
read1 = fread(buffer1, 1, 128, file1);
|
||||
|
||||
while (1) {
|
||||
|
@ -138,7 +152,9 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
friend_ip = DHT_getfriendip(friend_id);
|
||||
|
||||
if (friend_ip.ip.i != 0) {
|
||||
if (connection == -1 && friendrequest == -1) {
|
||||
printf("Sending friend request to peer:");
|
||||
|
@ -147,23 +163,27 @@ int main(int argc, char *argv[])
|
|||
/* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */
|
||||
/* connection = new_connection(friend_ip); */
|
||||
}
|
||||
|
||||
if (check_friendrequest(friendrequest) == 1) {
|
||||
printf("Started connecting to friend:");
|
||||
connection = crypto_connect(friend_id, friend_ip);
|
||||
}
|
||||
}
|
||||
|
||||
if (inconnection == -1) {
|
||||
uint8_t secret_nonce[crypto_box_NONCEBYTES];
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
||||
inconnection = crypto_inbound(public_key, secret_nonce, session_key);
|
||||
inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key);
|
||||
|
||||
/* inconnection = incoming_connection(); */
|
||||
if (inconnection != -1) {
|
||||
printf("Someone connected to us:\n");
|
||||
/* printip(connection_ip(inconnection)); */
|
||||
}
|
||||
}
|
||||
|
||||
if (handle_friendrequest(acceptedfriend_public_key, request_data) > 1) {
|
||||
printf("RECEIVED FRIEND REQUEST: %s\n", request_data);
|
||||
}
|
||||
|
@ -175,12 +195,16 @@ int main(int argc, char *argv[])
|
|||
printf("Wrote data1.\n");
|
||||
read1 = fread(buffer1, 1, 128, file1);
|
||||
}
|
||||
|
||||
read2 = read_cryptpacket(inconnection, buffer2);
|
||||
|
||||
if (read2 != 0) {
|
||||
printf("Received data1.\n");
|
||||
|
||||
if (!fwrite(buffer2, read2, 1, file2)) {
|
||||
printf("file write error1\n");
|
||||
}
|
||||
|
||||
if (read2 < 128) {
|
||||
printf("Closed file1 %u\n", read2);
|
||||
fclose(file2);
|
||||
|
@ -191,6 +215,7 @@ int main(int argc, char *argv[])
|
|||
crypto_kill(inconnection);
|
||||
}
|
||||
}
|
||||
|
||||
/* if we are connected to a friend send him data from the file.
|
||||
* also put what he sends us in a file. */
|
||||
if (is_cryptoconnected(connection) >= 3) {
|
||||
|
@ -198,12 +223,16 @@ int main(int argc, char *argv[])
|
|||
printf("Wrote data2.\n");
|
||||
read1 = fread(buffer1, 1, 128, file1);
|
||||
}
|
||||
|
||||
read2 = read_cryptpacket(0, buffer2);
|
||||
|
||||
if (read2 != 0) {
|
||||
printf("Received data2.\n");
|
||||
|
||||
if (!fwrite(buffer2, read2, 1, file2)) {
|
||||
printf("file write error2\n");
|
||||
}
|
||||
|
||||
if (read2 < 128) {
|
||||
printf("Closed file2 %u\n", read2);
|
||||
fclose(file2);
|
||||
|
@ -214,6 +243,7 @@ int main(int argc, char *argv[])
|
|||
crypto_kill(connection);
|
||||
}
|
||||
}
|
||||
|
||||
doDHT();
|
||||
doLossless_UDP();
|
||||
doNetCrypto();
|
||||
|
|
|
@ -56,7 +56,8 @@
|
|||
|
||||
void printip(IP_Port ip_port)
|
||||
{
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port));
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u\n", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3],
|
||||
ntohs(ip_port.port));
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
@ -67,6 +68,7 @@ int main(int argc, char *argv[])
|
|||
printf("usage %s ip port client_id(of friend to find ip_port of) filename(of file to send) client_id(ours)\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
DHT_addfriend((uint8_t *)argv[3]);
|
||||
IP_Port friend_ip;
|
||||
int connection = -1;
|
||||
|
@ -96,11 +98,14 @@ int main(int argc, char *argv[])
|
|||
uint8_t buffer2[128];
|
||||
int read2 = 0;
|
||||
FILE *file1 = fopen(argv[4], "rb");
|
||||
|
||||
if (file1 == NULL) {
|
||||
printf("Error opening file.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE *file2 = fopen("received.txt", "wb");
|
||||
|
||||
if (file2 == NULL)
|
||||
return 1;
|
||||
|
||||
|
@ -115,7 +120,9 @@ int main(int argc, char *argv[])
|
|||
printf("Received handled packet with length: %u\n", length);
|
||||
}
|
||||
}
|
||||
|
||||
friend_ip = DHT_getfriendip((uint8_t *)argv[3]);
|
||||
|
||||
if (friend_ip.ip.i != 0) {
|
||||
if (connection == -1) {
|
||||
printf("Started connecting to friend:");
|
||||
|
@ -123,13 +130,16 @@ int main(int argc, char *argv[])
|
|||
connection = new_connection(friend_ip);
|
||||
}
|
||||
}
|
||||
|
||||
if (inconnection == -1) {
|
||||
inconnection = incoming_connection();
|
||||
|
||||
if (inconnection != -1) {
|
||||
printf("Someone connected to us:");
|
||||
printip(connection_ip(inconnection));
|
||||
}
|
||||
}
|
||||
|
||||
/* if someone connected to us write what he sends to a file */
|
||||
/* also send him our file. */
|
||||
if (inconnection != -1) {
|
||||
|
@ -137,16 +147,21 @@ int main(int argc, char *argv[])
|
|||
printf("Wrote data.\n");
|
||||
read1 = fread(buffer1, 1, 128, file1);
|
||||
}
|
||||
|
||||
read2 = read_packet(inconnection, buffer2);
|
||||
|
||||
if (read2 != 0) {
|
||||
printf("Received data.\n");
|
||||
|
||||
if (!fwrite(buffer2, read2, 1, file2))
|
||||
printf("file write error\n");
|
||||
|
||||
if (read2 < 128) {
|
||||
fclose(file2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* if we are connected to a friend send him data from the file.
|
||||
* also put what he sends us in a file. */
|
||||
if (is_connected(connection) == 3) {
|
||||
|
@ -154,15 +169,20 @@ int main(int argc, char *argv[])
|
|||
printf("Wrote data.\n");
|
||||
read1 = fread(buffer1, 1, 128, file1);
|
||||
}
|
||||
|
||||
read2 = read_packet(0, buffer2);
|
||||
|
||||
if (read2 != 0) {
|
||||
printf("Received data.\n");
|
||||
|
||||
if (!fwrite(buffer2, read2, 1, file2))
|
||||
printf("file write error\n");
|
||||
|
||||
if (read2 < 128)
|
||||
fclose(file2);
|
||||
}
|
||||
}
|
||||
|
||||
doDHT();
|
||||
doLossless_UDP();
|
||||
/* print_clientlist();
|
||||
|
|
|
@ -53,11 +53,14 @@ void print_clientlist()
|
|||
uint32_t i, j;
|
||||
IP_Port p_ip;
|
||||
printf("___________________CLOSE________________________________\n");
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
printf("ClientID: ");
|
||||
|
||||
for (j = 0; j < 32; j++) {
|
||||
printf("%02hhX", close_clientlist[i].client_id[j]);
|
||||
}
|
||||
|
||||
p_ip = close_clientlist[i].ip_port;
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port));
|
||||
printf("\nTimestamp: %llu", (long long unsigned int) close_clientlist[i].timestamp);
|
||||
|
@ -73,12 +76,15 @@ void print_friendlist()
|
|||
uint32_t i, j, k;
|
||||
IP_Port p_ip;
|
||||
printf("_________________FRIENDS__________________________________\n");
|
||||
|
||||
for (k = 0; k < num_friends; k++) {
|
||||
printf("FRIEND %u\n", k);
|
||||
printf("ID: ");
|
||||
|
||||
for (j = 0; j < 32; j++) {
|
||||
printf("%c", friends_list[k].client_id[j]);
|
||||
}
|
||||
|
||||
p_ip = DHT_getfriendip(friends_list[k].client_id);
|
||||
printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port));
|
||||
|
||||
|
@ -86,11 +92,14 @@ void print_friendlist()
|
|||
|
||||
for (i = 0; i < 4; i++) {
|
||||
printf("ClientID: ");
|
||||
|
||||
for (j = 0; j < 32; j++) {
|
||||
if (friends_list[k].client_list[i].client_id[j] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", friends_list[k].client_list[i].client_id[j]);
|
||||
}
|
||||
|
||||
p_ip = friends_list[k].client_list[i].ip_port;
|
||||
printf("\nIP: %u.%u.%u.%u:%u", p_ip.ip.c[0], p_ip.ip.c[1], p_ip.ip.c[2], p_ip.ip.c[3], ntohs(p_ip.port));
|
||||
printf("\nTimestamp: %llu", (long long unsigned int) friends_list[k].client_list[i].timestamp);
|
||||
|
@ -107,11 +116,14 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port)
|
|||
uint32_t i;
|
||||
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
|
||||
printf("--------------------BEGIN-----------------------------\n");
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
if (data[i] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", data[i]);
|
||||
}
|
||||
|
||||
printf("\n--------------------END-----------------------------\n\n\n");
|
||||
}
|
||||
|
||||
|
@ -123,17 +135,21 @@ int main(int argc, char *argv[])
|
|||
printf("usage %s ip port public_key\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
new_keys();
|
||||
printf("OUR ID: ");
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (self_public_key[i] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", self_public_key[i]);
|
||||
}
|
||||
|
||||
char temp_id[128];
|
||||
printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
|
||||
|
||||
if (scanf("%s", temp_id) != 1)
|
||||
exit(0);
|
||||
|
||||
|
|
|
@ -49,17 +49,21 @@ void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
|
|||
uint32_t i;
|
||||
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
|
||||
printf("--------------------BEGIN-----------------------------\n");
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
if (data[i] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", data[i]);
|
||||
}
|
||||
|
||||
printf("\n--------------------END-----------------------------\n\n\n");
|
||||
}
|
||||
|
||||
void printip(IP_Port ip_port)
|
||||
{
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3], ntohs(ip_port.port));
|
||||
printf("\nIP: %u.%u.%u.%u Port: %u", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3],
|
||||
ntohs(ip_port.port));
|
||||
}
|
||||
/*
|
||||
void printpackets(Data test)
|
||||
|
@ -153,6 +157,7 @@ int main(int argc, char *argv[])
|
|||
int read;
|
||||
|
||||
FILE *file = fopen(argv[3], "rb");
|
||||
|
||||
if (file == NULL)
|
||||
return 1;
|
||||
|
||||
|
@ -169,19 +174,24 @@ int main(int argc, char *argv[])
|
|||
printip(serverip);
|
||||
int connection = new_connection(serverip);
|
||||
uint64_t timer = current_time();
|
||||
|
||||
while (1) {
|
||||
/* printconnection(connection); */
|
||||
Lossless_UDP();
|
||||
|
||||
if (is_connected(connection) == 3) {
|
||||
printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_connected(connection) == 0) {
|
||||
printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
return 1;
|
||||
}
|
||||
|
||||
c_sleep(1);
|
||||
}
|
||||
|
||||
timer = current_time();
|
||||
|
||||
LosslessUDP_init();
|
||||
|
@ -192,6 +202,7 @@ int main(int argc, char *argv[])
|
|||
while (1) {
|
||||
/* printconnection(connection); */
|
||||
Lossless_UDP();
|
||||
|
||||
if (is_connected(connection) == 3) {
|
||||
|
||||
if (write_packet(connection, buffer, read)) {
|
||||
|
@ -199,6 +210,7 @@ int main(int argc, char *argv[])
|
|||
read = fread(buffer, 1, 512, file);
|
||||
|
||||
}
|
||||
|
||||
/* printf("%u\n", sendqueue(connection)); */
|
||||
if (sendqueue(connection) == 0) {
|
||||
if (read == 0) {
|
||||
|
@ -206,11 +218,11 @@ int main(int argc, char *argv[])
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* c_sleep(1); */
|
||||
}
|
||||
|
||||
|
|
|
@ -50,11 +50,14 @@ void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
|
|||
uint32_t i;
|
||||
printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
|
||||
printf("--------------------BEGIN-----------------------------\n");
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
if (data[i] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", data[i]);
|
||||
}
|
||||
|
||||
printf("\n--------------------END-----------------------------\n\n\n");
|
||||
}
|
||||
|
||||
|
@ -149,6 +152,7 @@ int main(int argc, char *argv[])
|
|||
int read;
|
||||
|
||||
FILE *file = fopen(argv[1], "wb");
|
||||
|
||||
if (file == NULL)
|
||||
return 1;
|
||||
|
||||
|
@ -168,13 +172,16 @@ int main(int argc, char *argv[])
|
|||
while (1) {
|
||||
Lossless_UDP();
|
||||
connection = incoming_connection();
|
||||
|
||||
if (connection != -1) {
|
||||
if (is_connected(connection) == 2) {
|
||||
printf("Received the connection.\n");
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
c_sleep(1);
|
||||
}
|
||||
|
||||
|
@ -183,20 +190,24 @@ int main(int argc, char *argv[])
|
|||
while (1) {
|
||||
//printconnection(0);
|
||||
Lossless_UDP();
|
||||
|
||||
if (is_connected(connection) >= 2) {
|
||||
kill_connection_in(connection, 3000000);
|
||||
read = read_packet(connection, buffer);
|
||||
|
||||
if (read != 0) {
|
||||
// printf("Received data.\n");
|
||||
if (!fwrite(buffer, read, 1, file))
|
||||
printf("file write error\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (is_connected(connection) == 4) {
|
||||
printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
|
||||
fclose(file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
c_sleep(1);
|
||||
}
|
||||
|
||||
|
|
|
@ -60,18 +60,20 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length, void*
|
|||
printf("Friend request received from: \n");
|
||||
printf("ClientID: ");
|
||||
uint32_t j;
|
||||
for(j = 0; j < 32; j++)
|
||||
{
|
||||
|
||||
for (j = 0; j < 32; j++) {
|
||||
if (public_key[j] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", public_key[j]);
|
||||
}
|
||||
|
||||
printf("\nOf length: %u with data: %s \n", length, data);
|
||||
|
||||
if(length != sizeof("Install Gentoo"))
|
||||
{
|
||||
if (length != sizeof("Install Gentoo")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 )
|
||||
//if the request contained the message of peace the person is obviously a friend so we add him.
|
||||
{
|
||||
|
@ -94,6 +96,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
m = initMessenger();
|
||||
|
||||
if ( !m ) {
|
||||
fputs("Failed to allocate messenger datastructure\n", stderr);
|
||||
exit(0);
|
||||
|
@ -106,7 +109,11 @@ int main(int argc, char *argv[])
|
|||
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
||||
} else {
|
||||
FILE *file = fopen(argv[1], "rb");
|
||||
if ( file==NULL ){return 1;}
|
||||
|
||||
if ( file == NULL ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int read;
|
||||
uint8_t buffer[128000];
|
||||
read = fread(buffer, 1, 128000, file);
|
||||
|
@ -114,6 +121,7 @@ int main(int argc, char *argv[])
|
|||
fclose(file);
|
||||
|
||||
}
|
||||
|
||||
m_callback_friendrequest(m, print_request, NULL);
|
||||
m_callback_friendmessage(m, print_message, NULL);
|
||||
|
||||
|
@ -121,9 +129,11 @@ int main(int argc, char *argv[])
|
|||
uint32_t i;
|
||||
uint8_t address[FRIEND_ADDRESS_SIZE];
|
||||
getaddress(m, address);
|
||||
|
||||
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
|
||||
if (address[i] < 16)
|
||||
printf("0");
|
||||
|
||||
printf("%hhX", address[i]);
|
||||
}
|
||||
|
||||
|
@ -131,9 +141,11 @@ int main(int argc, char *argv[])
|
|||
|
||||
char temp_id[128];
|
||||
printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n");
|
||||
|
||||
if (scanf("%s", temp_id) != 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
|
||||
perror("Initialization");
|
||||
|
@ -147,13 +159,22 @@ int main(int argc, char *argv[])
|
|||
doMessenger(m);
|
||||
c_sleep(30);
|
||||
FILE *file = fopen("Save.bak", "wb");
|
||||
if ( file==NULL ){return 1;}
|
||||
|
||||
if ( file == NULL ) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t *buffer = malloc(Messenger_size(m));
|
||||
Messenger_save(m, buffer);
|
||||
size_t write_result = fwrite(buffer, 1, Messenger_size(m), file);
|
||||
if (write_result < Messenger_size(m)) {return 1;}
|
||||
|
||||
if (write_result < Messenger_size(m)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
cleanupMessenger(m);
|
||||
}
|
||||
|
|
|
@ -35,8 +35,8 @@ double get_time()
|
|||
void rand_bytes(uint8_t *b, size_t blen)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < blen; i++)
|
||||
{
|
||||
|
||||
for (i = 0; i < blen; i++) {
|
||||
b[i] = rand();
|
||||
}
|
||||
}
|
||||
|
@ -78,41 +78,45 @@ int main(int argc, char* argv[])
|
|||
|
||||
printf("starting slow...\n");
|
||||
starttime = get_time();
|
||||
for (trialno = 0; trialno < numtrials; trialno++)
|
||||
{
|
||||
|
||||
for (trialno = 0; trialno < numtrials; trialno++) {
|
||||
encrypt_data(pk1, sk2, n, m, sizeof(m), c);
|
||||
decrypt_data(pk2, sk1, n, c, sizeof(c), m);
|
||||
}
|
||||
|
||||
endtime = get_time();
|
||||
slow_time = endtime - starttime;
|
||||
|
||||
printf("starting fast...\n");
|
||||
starttime = get_time();
|
||||
for (trialno = 0; trialno < numtrials; trialno++)
|
||||
{
|
||||
|
||||
for (trialno = 0; trialno < numtrials; trialno++) {
|
||||
encrypt_data_fast(k1, n, m, sizeof(m), c);
|
||||
decrypt_data_fast(k2, n, c, sizeof(c), m);
|
||||
}
|
||||
|
||||
endtime = get_time();
|
||||
fast_time = endtime - starttime;
|
||||
|
||||
printf("starting keygen...\n");
|
||||
starttime = get_time();
|
||||
for (trialno = 0; trialno < numtrials; trialno++)
|
||||
{
|
||||
|
||||
for (trialno = 0; trialno < numtrials; trialno++) {
|
||||
crypto_box_keypair(pk1, sk1);
|
||||
crypto_box_keypair(pk2, sk2);
|
||||
}
|
||||
|
||||
endtime = get_time();
|
||||
keygen_time = endtime - starttime;
|
||||
|
||||
printf("starting precompute...\n");
|
||||
starttime = get_time();
|
||||
for (trialno = 0; trialno < numtrials; trialno++)
|
||||
{
|
||||
|
||||
for (trialno = 0; trialno < numtrials; trialno++) {
|
||||
encrypt_precompute(pk1, sk2, k);
|
||||
encrypt_precompute(pk2, sk1, k);
|
||||
}
|
||||
|
||||
endtime = get_time();
|
||||
precompute_time = endtime - starttime;
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ unsigned char * hex_string_to_bin(char hex_string[])
|
|||
unsigned char *val = malloc(len);
|
||||
char *pos = hex_string;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; ++i, pos += 2)
|
||||
sscanf(pos, "%2hhx", &val[i]);
|
||||
|
||||
|
|
|
@ -146,6 +146,7 @@ static inline void _tox_array_push(struct tox_array *arr, void *new)
|
|||
{
|
||||
if (arr->length + 1 >= arr->size)
|
||||
tox_array_shrink_to_fit(arr, arr->size);
|
||||
|
||||
arr->data[arr->length++] = new;
|
||||
}
|
||||
#define tox_array_push(arr, new) _tox_array_push(arr, (void*)new)
|
||||
|
@ -154,6 +155,7 @@ static inline void* tox_array_pop(struct tox_array *arr)
|
|||
{
|
||||
if (arr->length - 1 < arr->size / 4)
|
||||
tox_array_shrink_to_fit(arr, arr->length * 2);
|
||||
|
||||
return arr->data[arr->length--];
|
||||
}
|
||||
|
||||
|
|
|
@ -57,8 +57,8 @@ void get_id(Messenger *m, char *data)
|
|||
int i = 0;
|
||||
uint8_t address[FRIEND_ADDRESS_SIZE];
|
||||
getaddress(m, address);
|
||||
for(; i < FRIEND_ADDRESS_SIZE; i++)
|
||||
{
|
||||
|
||||
for (; i < FRIEND_ADDRESS_SIZE; i++) {
|
||||
sprintf(data + 2 * i + offset, "%02X ", address[i]);
|
||||
}
|
||||
}
|
||||
|
@ -66,6 +66,7 @@ void get_id(Messenger *m, char *data)
|
|||
void new_lines(char *line)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (i = HISTORY - 1; i > 0; i--)
|
||||
strncpy(lines[i], lines[i - 1], STRING_LENGTH - 1);
|
||||
|
||||
|
@ -79,6 +80,7 @@ void print_friendlist(Messenger *m)
|
|||
char name[MAX_NAME_LENGTH];
|
||||
int i = 0;
|
||||
new_lines("[i] Friend List:");
|
||||
|
||||
while (getname(m, i, (uint8_t *)name) != -1) {
|
||||
/* account for the longest name and the longest "base" string */
|
||||
char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")];
|
||||
|
@ -88,6 +90,7 @@ void print_friendlist(Messenger *m)
|
|||
} else {
|
||||
sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t *)name, i);
|
||||
}
|
||||
|
||||
i++;
|
||||
new_lines(fstring);
|
||||
}
|
||||
|
@ -99,11 +102,13 @@ void print_friendlist(Messenger *m)
|
|||
char *format_message(Messenger *m, char *message, int friendnum)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
|
||||
if (friendnum != -1) {
|
||||
getname(m, friendnum, (uint8_t *)name);
|
||||
} else {
|
||||
getself_name(m, (uint8_t *)name, sizeof(name));
|
||||
}
|
||||
|
||||
char *msg = malloc(100 + strlen(message) + strlen(name) + 1);
|
||||
|
||||
time_t rawtime;
|
||||
|
@ -113,12 +118,14 @@ char *format_message(Messenger *m, char *message, int friendnum)
|
|||
char *time = asctime(timeinfo);
|
||||
size_t len = strlen(time);
|
||||
time[len - 1] = '\0';
|
||||
|
||||
if (friendnum != -1) {
|
||||
sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message);
|
||||
} else {
|
||||
// This message came from ourselves
|
||||
sprintf(msg, "%s <%s> %s", time, name, message);
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
@ -130,9 +137,11 @@ void line_eval(Messenger *m, char *line)
|
|||
int prompt_offset = 3;
|
||||
strcat(prompt, line);
|
||||
new_lines(prompt);
|
||||
|
||||
if (inpt_command == 'f') { // add friend command: /f ID
|
||||
int i;
|
||||
char temp_id[128];
|
||||
|
||||
for (i = 0; i < 128; i++)
|
||||
temp_id[i] = line[i + prompt_offset];
|
||||
|
||||
|
@ -140,96 +149,111 @@ void line_eval(Messenger *m, char *line)
|
|||
int num = m_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
free(bin_string);
|
||||
char numstring[100];
|
||||
|
||||
switch (num) {
|
||||
case FAERR_TOOLONG:
|
||||
sprintf(numstring, "[i] Message is too long.");
|
||||
break;
|
||||
|
||||
case FAERR_NOMESSAGE:
|
||||
sprintf(numstring, "[i] Please add a message to your request.");
|
||||
break;
|
||||
|
||||
case FAERR_OWNKEY:
|
||||
sprintf(numstring, "[i] That appears to be your own ID.");
|
||||
break;
|
||||
|
||||
case FAERR_ALREADYSENT:
|
||||
sprintf(numstring, "[i] Friend request already sent.");
|
||||
break;
|
||||
|
||||
case FAERR_UNKNOWN:
|
||||
sprintf(numstring, "[i] Undefined error when adding friend.");
|
||||
break;
|
||||
|
||||
default:
|
||||
sprintf(numstring, "[i] Added friend as %d.", num);
|
||||
break;
|
||||
}
|
||||
|
||||
new_lines(numstring);
|
||||
do_refresh();
|
||||
}
|
||||
else if (inpt_command == 'd') {
|
||||
} else if (inpt_command == 'd') {
|
||||
doMessenger(m);
|
||||
}
|
||||
else if (inpt_command == 'm') { //message command: /m friendnumber messsage
|
||||
} 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];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (line[i + 3] != ' ') {
|
||||
numstring[i] = line[i + 3];
|
||||
} else {
|
||||
int j;
|
||||
|
||||
for (j = (i + 1); j < (len + 1); j++)
|
||||
message[j - i - 1] = line[j + 3];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int num = atoi(numstring);
|
||||
|
||||
if (m_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 if (inpt_command == 'n') {
|
||||
} else if (inpt_command == 'n') {
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
int i = 0;
|
||||
size_t len = strlen(line);
|
||||
|
||||
for (i = 3; i < len; i++) {
|
||||
if (line[i] == 0 || line[i] == '\n') break;
|
||||
|
||||
name[i - 3] = line[i];
|
||||
}
|
||||
|
||||
name[i - 3] = 0;
|
||||
setname(m, name, i - 2);
|
||||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed nick to %s", (char *)name);
|
||||
new_lines(numstring);
|
||||
}
|
||||
else if (inpt_command == 'l') {
|
||||
} else if (inpt_command == 'l') {
|
||||
print_friendlist(m);
|
||||
}
|
||||
else if (inpt_command == 's') {
|
||||
} else if (inpt_command == 's') {
|
||||
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
|
||||
int i = 0;
|
||||
size_t len = strlen(line);
|
||||
|
||||
for (i = 3; i < len; i++) {
|
||||
if (line[i] == 0 || line[i] == '\n') break;
|
||||
|
||||
status[i - 3] = line[i];
|
||||
}
|
||||
|
||||
status[i - 3] = 0;
|
||||
m_set_statusmessage(m, status, strlen((char *)status) + 1);
|
||||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed status to %s", (char *)status);
|
||||
new_lines(numstring);
|
||||
}
|
||||
else if (inpt_command == 'a') {
|
||||
} else if (inpt_command == 'a') {
|
||||
uint8_t numf = atoi(line + 3);
|
||||
char numchar[100];
|
||||
|
||||
if (numf >= num_requests || pending_requests[numf].accepted) {
|
||||
sprintf(numchar, "[i] you either didn't receive that request or you already accepted it");
|
||||
new_lines(numchar);
|
||||
} else {
|
||||
int num = m_addfriend_norequest(m, pending_requests[numf].id);
|
||||
|
||||
if (num != -1) {
|
||||
pending_requests[numf].accepted = 1;
|
||||
sprintf(numchar, "[i] friend request %u accepted", numf);
|
||||
|
@ -241,12 +265,11 @@ void line_eval(Messenger *m, char *line)
|
|||
new_lines(numchar);
|
||||
}
|
||||
}
|
||||
|
||||
do_refresh();
|
||||
}
|
||||
else if (inpt_command == 'h') { //help
|
||||
} else if (inpt_command == 'h') { //help
|
||||
new_lines(help);
|
||||
}
|
||||
else if (inpt_command == 'i') { //info
|
||||
} else if (inpt_command == 'i') { //info
|
||||
char idstring[200];
|
||||
get_id(m, idstring);
|
||||
new_lines(idstring);
|
||||
|
@ -269,10 +292,12 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
|
|||
strcpy(output, input);
|
||||
size_t len = strlen(output);
|
||||
int i = 0;
|
||||
|
||||
for (i = line_width; i < len; i = i + line_width) {
|
||||
while (output[i] != ' ' && i != 0) {
|
||||
i--;
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
output[i] = '\n';
|
||||
}
|
||||
|
@ -284,20 +309,24 @@ int count_lines(char *string)
|
|||
size_t len = strlen(string);
|
||||
int count = 1;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (string[i] == '\n')
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
char *appender(char *str, const char c)
|
||||
{
|
||||
size_t len = strlen(str);
|
||||
|
||||
if (len < STRING_LENGTH) {
|
||||
str[len + 1] = str[len];
|
||||
str[len] = c;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
@ -307,16 +336,19 @@ void do_refresh()
|
|||
char wrap_output[STRING_LENGTH];
|
||||
int L;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < HISTORY; i++) {
|
||||
wrap(wrap_output, lines[i], x);
|
||||
L = count_lines(wrap_output);
|
||||
count = count + L;
|
||||
|
||||
if (count < y) {
|
||||
move(y - 1 - count, 0);
|
||||
printw(wrap_output);
|
||||
clrtoeol();
|
||||
}
|
||||
}
|
||||
|
||||
move(y - 1, 0);
|
||||
clrtoeol();
|
||||
printw(">> ");
|
||||
|
@ -346,6 +378,7 @@ void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t le
|
|||
void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
|
||||
if (getname(m, friendnumber, (uint8_t *)name) != -1) {
|
||||
char msg[100 + length];
|
||||
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
|
||||
|
@ -356,6 +389,7 @@ void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t
|
|||
void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
|
||||
if (getname(m, friendnumber, (uint8_t *)name) != -1) {
|
||||
char msg[100 + length + strlen(name) + 1];
|
||||
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
|
||||
|
@ -375,10 +409,12 @@ void load_key(Messenger *m, char *path)
|
|||
rewind(data_file);
|
||||
|
||||
uint8_t data[size];
|
||||
|
||||
if (fread(data, sizeof(uint8_t), size, data_file) != size) {
|
||||
fputs("[!] could not read data file! exiting...\n", stderr);
|
||||
goto FILE_ERROR;
|
||||
}
|
||||
|
||||
Messenger_load(m, data, size);
|
||||
|
||||
} else {
|
||||
|
@ -401,11 +437,14 @@ void load_key(Messenger *m, char *path)
|
|||
|
||||
if (fclose(data_file) < 0)
|
||||
perror("[!] fclose failed");
|
||||
|
||||
return;
|
||||
|
||||
FILE_ERROR:
|
||||
|
||||
if (fclose(data_file) < 0)
|
||||
perror("[!] fclose failed");
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
@ -449,6 +488,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
m = initMessenger();
|
||||
|
||||
if ( !m ) {
|
||||
fputs("Failed to allocate Messenger datastructure", stderr);
|
||||
exit(0);
|
||||
|
@ -474,6 +514,7 @@ int main(int argc, char *argv[])
|
|||
IP_Port bootstrap_ip_port;
|
||||
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
||||
int resolved_address = resolve_addr(argv[1]);
|
||||
|
||||
if (resolved_address != 0)
|
||||
bootstrap_ip_port.ip.i = resolved_address;
|
||||
else
|
||||
|
@ -483,6 +524,7 @@ int main(int argc, char *argv[])
|
|||
DHT_bootstrap(bootstrap_ip_port, binary_string);
|
||||
free(binary_string);
|
||||
nodelay(stdscr, TRUE);
|
||||
|
||||
while (true) {
|
||||
if (on == 0 && DHT_isconnected()) {
|
||||
new_lines("[i] connected to DHT\n[i] define username with /n");
|
||||
|
@ -494,10 +536,12 @@ int main(int argc, char *argv[])
|
|||
do_refresh();
|
||||
|
||||
c = getch();
|
||||
|
||||
if (c == ERR || c == 27)
|
||||
continue;
|
||||
|
||||
getmaxyx(stdscr, y, x);
|
||||
|
||||
if (c == '\n') {
|
||||
line_eval(m, line);
|
||||
strcpy(line, "");
|
||||
|
@ -507,6 +551,7 @@ int main(int argc, char *argv[])
|
|||
strcpy(line, appender(line, (char) c));
|
||||
}
|
||||
}
|
||||
|
||||
cleanupMessenger(m);
|
||||
endwin();
|
||||
return 0;
|
||||
|
|
|
@ -101,11 +101,13 @@ void load_key()
|
|||
{
|
||||
FILE *data_file = NULL;
|
||||
data_file = fopen("data", "r");
|
||||
|
||||
if (data_file) {
|
||||
fseek(data_file, 0, SEEK_END);
|
||||
int size = ftell(data_file);
|
||||
fseek(data_file, 0, SEEK_SET);
|
||||
uint8_t data[size];
|
||||
|
||||
if (fread(data, sizeof(uint8_t), size, data_file) != size) {
|
||||
printf("\n[i] Could not read the data file. Exiting.");
|
||||
exit(1);
|
||||
|
@ -124,6 +126,7 @@ void load_key()
|
|||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(data_file);
|
||||
}
|
||||
|
||||
|
@ -142,8 +145,7 @@ void add_friend()
|
|||
sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num);
|
||||
printf(numstring);
|
||||
++maxnumfriends;
|
||||
}
|
||||
else if (num == -1)
|
||||
} else if (num == -1)
|
||||
printf("\n[i] Message is too long.\n\n");
|
||||
|
||||
else if (num == -2)
|
||||
|
@ -170,6 +172,7 @@ void list_friends()
|
|||
for (i = 0; i <= maxnumfriends; i++) {
|
||||
char name[MAX_NAME_LENGTH];
|
||||
getname(messenger, i, (uint8_t *)name);
|
||||
|
||||
if (m_friendstatus(messenger, i) > 0 && m_friendstatus(messenger, i) < 4)
|
||||
printf("[%d] %s\n", i, (uint8_t *)name);
|
||||
}
|
||||
|
@ -295,11 +298,13 @@ void accept_friend_request()
|
|||
friend_request_received = 0;
|
||||
uint8_t numf = atoi(line + 3);
|
||||
char numchar[100];
|
||||
|
||||
if (numf >= num_requests || pending_requests[numf].accepted) {
|
||||
sprintf(numchar, "\n[i] you either didn't receive that request or you already accepted it");
|
||||
printf(numchar);
|
||||
} else {
|
||||
int num = m_addfriend_norequest(messenger, pending_requests[numf].id);
|
||||
|
||||
if (num != -1) {
|
||||
pending_requests[numf].accepted = 1;
|
||||
sprintf(numchar, "\n[i] Added friendnumber: %d\n\n", num);
|
||||
|
@ -375,11 +380,14 @@ int main(int argc, char *argv[])
|
|||
printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
messenger = initMessenger();
|
||||
|
||||
if (messenger == 0) {
|
||||
printf("initMessenger failed");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (argc > 4) {
|
||||
if (strncmp(argv[4], "nokey", 6) < 0) {
|
||||
}
|
||||
|
@ -392,11 +400,14 @@ int main(int argc, char *argv[])
|
|||
|
||||
FILE *name_file = NULL;
|
||||
name_file = fopen("namefile.txt", "r");
|
||||
|
||||
if (name_file) {
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
|
||||
while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) {
|
||||
sscanf(line, "%s", (char *)name);
|
||||
}
|
||||
|
||||
setname(messenger, name, strlen((char *)name) + 1);
|
||||
nameloaded = 1;
|
||||
printf("%s\n", name);
|
||||
|
@ -405,11 +416,14 @@ int main(int argc, char *argv[])
|
|||
|
||||
FILE *status_file = NULL;
|
||||
status_file = fopen("statusfile.txt", "r");
|
||||
|
||||
if (status_file) {
|
||||
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
|
||||
|
||||
while (fgets(line, MAX_STATUSMESSAGE_LENGTH, status_file) != NULL) {
|
||||
sscanf(line, "%s", (char *)status);
|
||||
}
|
||||
|
||||
m_set_statusmessage(messenger, status, strlen((char *)status) + 1);
|
||||
statusloaded = 1;
|
||||
printf("%s\n", status);
|
||||
|
@ -423,16 +437,19 @@ int main(int argc, char *argv[])
|
|||
char idstring1[PUB_KEY_BYTES][5];
|
||||
char idstring2[PUB_KEY_BYTES][5];
|
||||
int i;
|
||||
for(i = 0; i < PUB_KEY_BYTES; i++)
|
||||
{
|
||||
|
||||
for (i = 0; i < PUB_KEY_BYTES; i++) {
|
||||
if (self_public_key[i] < (PUB_KEY_BYTES / 2))
|
||||
strcpy(idstring1[i], "0");
|
||||
else
|
||||
strcpy(idstring1[i], "");
|
||||
|
||||
sprintf(idstring2[i], "%hhX", self_public_key[i]);
|
||||
}
|
||||
|
||||
strcpy(users_id, "[i] your ID: ");
|
||||
int j;
|
||||
|
||||
for (j = 0; j < PUB_KEY_BYTES; j++) {
|
||||
strcat(users_id, idstring1[j]);
|
||||
strcat(users_id, idstring2[j]);
|
||||
|
@ -443,6 +460,7 @@ int main(int argc, char *argv[])
|
|||
IP_Port bootstrap_ip_port;
|
||||
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
||||
int resolved_address = resolve_addr(argv[1]);
|
||||
|
||||
if (resolved_address != 0)
|
||||
bootstrap_ip_port.ip.i = resolved_address;
|
||||
else
|
||||
|
@ -472,13 +490,16 @@ int main(int argc, char *argv[])
|
|||
printf("\n---------------------------------\n\n");
|
||||
on = 0;
|
||||
}
|
||||
|
||||
if (on == 0 && DHT_isconnected()) {
|
||||
printf("\n[i] Connected to DHT");
|
||||
printf("\n---------------------------------\n\n");
|
||||
on = 1;
|
||||
}
|
||||
|
||||
doMessenger(messenger);
|
||||
Sleep(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,12 +16,14 @@ void mssleep(int ms)
|
|||
#endif
|
||||
}
|
||||
|
||||
int callback(timer* t, void* arg){
|
||||
int callback(timer *t, void *arg)
|
||||
{
|
||||
printf("%s\n", (char *)arg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int repeating(timer* t, void *arg) {
|
||||
int repeating(timer *t, void *arg)
|
||||
{
|
||||
printf("%s\n", (char *)arg);
|
||||
timer_start(t, 3);
|
||||
return 0;
|
||||
|
|
|
@ -92,6 +92,7 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
|
|||
{
|
||||
ChatContext *ctx = (ChatContext *) self->x;
|
||||
struct tm *timeinfo = get_time();
|
||||
|
||||
if (ctx->friendnum != num)
|
||||
return;
|
||||
|
||||
|
@ -112,6 +113,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1
|
|||
{
|
||||
ChatContext *ctx = (ChatContext *) self->x;
|
||||
struct tm *timeinfo = get_time();
|
||||
|
||||
if (ctx->friendnum != num)
|
||||
return;
|
||||
|
||||
|
@ -161,6 +163,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
||||
if (ctx->pos > 0) {
|
||||
ctx->line[--ctx->pos] = '\0';
|
||||
|
||||
if (x == 0)
|
||||
mvwdelch(self->window, y - 1, x2 - 1);
|
||||
else
|
||||
|
@ -173,6 +176,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
wclear(ctx->linewin);
|
||||
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
||||
wclrtobot(self->window);
|
||||
|
||||
if (ctx->line[0] == '/')
|
||||
execute(self, ctx, m, ctx->line);
|
||||
else {
|
||||
|
@ -189,6 +193,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
wprintw(ctx->history, "%s: ", selfname);
|
||||
wattroff(ctx->history, COLOR_PAIR(1));
|
||||
wprintw(ctx->history, "%s\n", ctx->line);
|
||||
|
||||
if (m_sendmessage(m, ctx->friendnum, (uint8_t *) ctx->line, strlen(ctx->line) + 1) == 0) {
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
wprintw(ctx->history, " * Failed to send message.\n");
|
||||
|
@ -196,6 +201,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx->line[0] = '\0';
|
||||
ctx->pos = 0;
|
||||
}
|
||||
|
@ -223,10 +229,12 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
|
|||
else if (!strncmp(cmd, "/me ", strlen("/me "))) {
|
||||
struct tm *timeinfo = get_time();
|
||||
char *action = strchr(cmd, ' ');
|
||||
|
||||
if (action == NULL) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
action++;
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(2));
|
||||
|
@ -241,6 +249,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
|
|||
wattron(ctx->history, COLOR_PAIR(5));
|
||||
wprintw(ctx->history, msg);
|
||||
wattroff(ctx->history, COLOR_PAIR(5));
|
||||
|
||||
if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) {
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
wprintw(ctx->history, " * Failed to send action\n");
|
||||
|
@ -252,12 +261,15 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
|
|||
char *status = strchr(cmd, ' ');
|
||||
char *msg;
|
||||
char *status_text;
|
||||
|
||||
if (status == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
status++;
|
||||
USERSTATUS status_kind;
|
||||
|
||||
if (!strncmp(status, "online", strlen("online"))) {
|
||||
status_kind = USERSTATUS_NONE;
|
||||
status_text = "ONLINE";
|
||||
|
@ -279,11 +291,11 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
|
|||
}
|
||||
|
||||
msg = strchr(status, ' ');
|
||||
|
||||
if (msg == NULL) {
|
||||
m_set_userstatus(m, status_kind);
|
||||
wprintw(ctx->history, "Status set to: %s\n", status_text);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
msg++;
|
||||
m_set_userstatus(m, status_kind);
|
||||
m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
|
||||
|
@ -294,6 +306,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
|
|||
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
|
||||
char *nick;
|
||||
nick = strchr(cmd, ' ');
|
||||
|
||||
if (nick == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
|
@ -309,11 +322,13 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
|
|||
int i;
|
||||
uint8_t address[FRIEND_ADDRESS_SIZE];
|
||||
getaddress(m, address);
|
||||
|
||||
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
|
||||
char xx[3];
|
||||
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
||||
strcat(id, xx);
|
||||
}
|
||||
|
||||
wprintw(ctx->history, "%s\n", id);
|
||||
}
|
||||
|
||||
|
|
|
@ -50,6 +50,7 @@ char *get_user_config_dir(void)
|
|||
BOOL ok;
|
||||
|
||||
ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE);
|
||||
|
||||
if (!ok) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -72,13 +73,16 @@ char *get_user_config_dir(void)
|
|||
int rc;
|
||||
|
||||
rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
|
||||
|
||||
if (rc == 0) {
|
||||
home = pwd.pw_dir;
|
||||
} else {
|
||||
home = getenv("HOME");
|
||||
|
||||
if (home == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* env variables can be tainted */
|
||||
snprintf(buf, sizeof(buf), "%s", home);
|
||||
home = buf;
|
||||
|
@ -87,6 +91,7 @@ char *get_user_config_dir(void)
|
|||
# if defined(__APPLE__)
|
||||
len = strlen(home) + strlen("/Library/Application Support") + 1;
|
||||
user_config_dir = malloc(len);
|
||||
|
||||
if (user_config_dir == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -95,6 +100,7 @@ char *get_user_config_dir(void)
|
|||
# else /* __APPLE__ */
|
||||
len = strlen(home) + strlen("/.config") + 1;
|
||||
user_config_dir = malloc(len);
|
||||
|
||||
if (user_config_dir == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -123,6 +129,7 @@ int create_user_config_dir(char *path)
|
|||
|
||||
mkdir_err = _mkdir(fullpath);
|
||||
struct __stat64 buf;
|
||||
|
||||
if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) {
|
||||
free(fullpath);
|
||||
return -1;
|
||||
|
|
|
@ -18,6 +18,7 @@ static void dhtstatus_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
case 'k':
|
||||
if (--num_selected < 0)
|
||||
num_selected = CLIENT_ID_SIZE - 1;
|
||||
|
||||
break;
|
||||
|
||||
case KEY_DOWN:
|
||||
|
@ -42,12 +43,18 @@ static void dhtstatus_onDraw(ToxWindow *self)
|
|||
uint64_t now = unix_time();
|
||||
uint32_t i, j;
|
||||
ipbuf ipbuf;
|
||||
wprintw(self->window,"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n", now);
|
||||
wprintw(self->window,
|
||||
"\n%llu ______________________ CLOSE LIST ________________________ ___ IP ADDR ___ _PRT_ LST PNG ____ SELF ____ _PRT_ LST\n\n",
|
||||
now);
|
||||
|
||||
for (i = 0; i < 32; i++) { /*Number of nodes in closelist*/
|
||||
Client_data *client = close_clientlist + i;
|
||||
|
||||
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
|
||||
|
||||
wprintw(self->window, "[%02i] ", i);
|
||||
uint16_t port = ntohs(client->ip_port.port);
|
||||
|
||||
if (port) {
|
||||
for (j = 0; j < CLIENT_ID_SIZE; j++)
|
||||
wprintw(self->window, "%02hhx", client->client_id[j]);
|
||||
|
@ -58,12 +65,15 @@ static void dhtstatus_onDraw(ToxWindow *self)
|
|||
wprintw(self->window, " %3llu ", now - client->last_pinged);
|
||||
|
||||
port = ntohs(client->ret_ip_port.port);
|
||||
|
||||
if (port) {
|
||||
printip(ipbuf, client->ret_ip_port.ip);
|
||||
wprintw(self->window, " %15s %5u %3llu", ipbuf, port, now - close_clientlist[i].ret_timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
wprintw(self->window, "\n");
|
||||
|
||||
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
|
||||
}
|
||||
|
||||
|
|
|
@ -31,11 +31,14 @@ void fix_name(uint8_t *name)
|
|||
/* Remove all non alphanumeric characters */
|
||||
uint8_t *p = name;
|
||||
uint8_t *q = name;
|
||||
|
||||
while (*p != 0) {
|
||||
if (isprint(*p))
|
||||
*q++ = *p;
|
||||
|
||||
p++;
|
||||
}
|
||||
|
||||
*q = 0;
|
||||
}
|
||||
|
||||
|
@ -47,6 +50,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str,
|
|||
if (friends[num].chatwin == -1) {
|
||||
friends[num].chatwin = num;
|
||||
int i;
|
||||
|
||||
/* Find first open slot to hold chat window */
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == -1) {
|
||||
|
@ -96,15 +100,14 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
if (key == KEY_UP) {
|
||||
if (--num_selected < 0)
|
||||
num_selected = num_friends - 1;
|
||||
}
|
||||
else if (key == KEY_DOWN) {
|
||||
} else if (key == KEY_DOWN) {
|
||||
if (num_friends != 0)
|
||||
num_selected = (num_selected + 1) % num_friends;
|
||||
}
|
||||
else if (key == '\n') {
|
||||
} else if (key == '\n') {
|
||||
/* Jump to chat window if already open */
|
||||
if (friends[num_selected].chatwin != -1) {
|
||||
int i;
|
||||
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == num_selected) {
|
||||
set_active_window(i);
|
||||
|
@ -113,6 +116,7 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
}
|
||||
} else {
|
||||
int i;
|
||||
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == -1) {
|
||||
WINDOW_STATUS[i] = num_selected;
|
||||
|
@ -129,10 +133,10 @@ static void friendlist_onDraw(ToxWindow *self)
|
|||
{
|
||||
curs_set(0);
|
||||
werase(self->window);
|
||||
|
||||
if (num_friends == 0) {
|
||||
wprintw(self->window, "Empty. Add some friends! :-)\n");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
|
||||
wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
|
||||
wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
|
||||
|
@ -140,9 +144,12 @@ static void friendlist_onDraw(ToxWindow *self)
|
|||
|
||||
wprintw(self->window, "\n");
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num_friends; ++i) {
|
||||
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
|
||||
|
||||
wprintw(self->window, " [#%d] ", friends[i].num);
|
||||
|
||||
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
|
||||
|
||||
attron(A_BOLD);
|
||||
|
@ -151,6 +158,7 @@ static void friendlist_onDraw(ToxWindow *self)
|
|||
|
||||
wprintw(self->window, "(%s)\n", friends[i].status);
|
||||
}
|
||||
|
||||
wrefresh(self->window);
|
||||
}
|
||||
|
||||
|
@ -164,7 +172,8 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m)
|
|||
|
||||
}
|
||||
|
||||
ToxWindow new_friendlist(char * ws) {
|
||||
ToxWindow new_friendlist(char *ws)
|
||||
{
|
||||
WINDOW_STATUS = ws;
|
||||
ToxWindow ret;
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
|
|
|
@ -53,6 +53,7 @@ static void init_term()
|
|||
init_pair(4, COLOR_BLUE, COLOR_BLACK);
|
||||
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
|
@ -88,34 +89,41 @@ int init_connection(void)
|
|||
return 0;
|
||||
|
||||
FILE *fp = fopen("../../../other/DHTservers", "r");
|
||||
|
||||
if (!fp)
|
||||
return 1;
|
||||
|
||||
char servers[MAXSERVERS][MAXLINE];
|
||||
char line[MAXLINE];
|
||||
int linecnt = 0;
|
||||
|
||||
while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
|
||||
if (strlen(line) > MINLINE)
|
||||
strcpy(servers[linecnt++], line);
|
||||
}
|
||||
|
||||
if (linecnt < 1) {
|
||||
fclose(fp);
|
||||
return 2;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
char *server = servers[rand() % linecnt];
|
||||
char *ip = strtok(server, " ");
|
||||
char *port = strtok(NULL, " ");
|
||||
char *key = strtok(NULL, " ");
|
||||
|
||||
if (!ip || !port || !key)
|
||||
return 3;
|
||||
|
||||
IP_Port dht;
|
||||
dht.port = htons(atoi(port));
|
||||
uint32_t resolved_address = resolve_addr(ip);
|
||||
|
||||
if (resolved_address == 0)
|
||||
return 0;
|
||||
|
||||
dht.ip.i = resolved_address;
|
||||
unsigned char *binary_string = hex_string_to_bin(key);
|
||||
DHT_bootstrap(dht, binary_string);
|
||||
|
@ -128,22 +136,23 @@ static void do_tox(Messenger *m, ToxWindow * prompt)
|
|||
static int conn_try = 0;
|
||||
static int conn_err = 0;
|
||||
static bool dht_on = false;
|
||||
|
||||
if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) {
|
||||
if (!conn_err) {
|
||||
conn_err = init_connection();
|
||||
wprintw(prompt->window, "\nEstablishing connection...\n");
|
||||
|
||||
if (conn_err)
|
||||
wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err);
|
||||
}
|
||||
}
|
||||
else if (!dht_on && DHT_isconnected()) {
|
||||
} else if (!dht_on && DHT_isconnected()) {
|
||||
dht_on = true;
|
||||
wprintw(prompt->window, "\nDHT connected.\n");
|
||||
}
|
||||
else if (dht_on && !DHT_isconnected()) {
|
||||
} else if (dht_on && !DHT_isconnected()) {
|
||||
dht_on = false;
|
||||
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
|
||||
}
|
||||
|
||||
doMessenger(m);
|
||||
}
|
||||
|
||||
|
@ -160,18 +169,22 @@ int store_data(Messenger *m, char *path)
|
|||
{
|
||||
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
|
||||
return 0;
|
||||
|
||||
FILE *fd;
|
||||
size_t len;
|
||||
uint8_t *buf;
|
||||
|
||||
len = Messenger_size(m);
|
||||
buf = malloc(len);
|
||||
|
||||
if (buf == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Messenger_save(m, buf);
|
||||
|
||||
fd = fopen(path, "w");
|
||||
|
||||
if (fd == NULL) {
|
||||
free(buf);
|
||||
return 2;
|
||||
|
@ -192,6 +205,7 @@ static void load_data(Messenger *m, char *path)
|
|||
{
|
||||
if (f_loadfromfile == 0) /*If file loading/saving is disabled*/
|
||||
return;
|
||||
|
||||
FILE *fd;
|
||||
size_t len;
|
||||
uint8_t *buf;
|
||||
|
@ -202,12 +216,14 @@ static void load_data(Messenger *m, char *path)
|
|||
fseek(fd, 0, SEEK_SET);
|
||||
|
||||
buf = malloc(len);
|
||||
|
||||
if (buf == NULL) {
|
||||
fprintf(stderr, "malloc() failed.\n");
|
||||
fclose(fd);
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (fread(buf, len, 1, fd) != 1) {
|
||||
fprintf(stderr, "fread() failed.\n");
|
||||
free(buf);
|
||||
|
@ -215,9 +231,11 @@ static void load_data(Messenger *m, char *path)
|
|||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Messenger_load(m, buf, len);
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < m->numfriends; i++) {
|
||||
on_friendadded(m, i);
|
||||
}
|
||||
|
@ -226,6 +244,7 @@ static void load_data(Messenger *m, char *path)
|
|||
fclose(fd);
|
||||
} else {
|
||||
int st;
|
||||
|
||||
if ((st = store_data(m, path)) != 0) {
|
||||
fprintf(stderr, "Store messenger failed with return code: %d\n", st);
|
||||
endwin();
|
||||
|
@ -242,6 +261,7 @@ int main(int argc, char *argv[])
|
|||
f_loadfromfile = 1;
|
||||
int f_flag = 0;
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < argc; ++i) {
|
||||
if (argv[i] == NULL)
|
||||
break;
|
||||
|
@ -259,6 +279,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
if (DATA_FILE == NULL ) {
|
||||
config_err = create_user_config_dir(user_config_dir);
|
||||
|
||||
if (config_err) {
|
||||
DATA_FILE = strdup("data");
|
||||
} else {
|
||||
|
@ -268,6 +289,7 @@ int main(int argc, char *argv[])
|
|||
strcat(DATA_FILE, "data");
|
||||
}
|
||||
}
|
||||
|
||||
free(user_config_dir);
|
||||
|
||||
init_term();
|
||||
|
@ -291,6 +313,7 @@ int main(int argc, char *argv[])
|
|||
"defaulting to 'data' for a keyfile...\n");
|
||||
attroff(COLOR_PAIR(3) | A_BOLD);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
/* Update tox */
|
||||
do_tox(m, prompt);
|
||||
|
|
|
@ -70,20 +70,24 @@ unsigned char *hex_string_to_bin(char hex_string[])
|
|||
unsigned char *val = malloc(len);
|
||||
char *pos = hex_string;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; ++i, pos += 2)
|
||||
sscanf(pos, "%2hhx", &val[i]);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
void cmd_accept(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
int num = atoi(args[1]);
|
||||
|
||||
if (num >= num_requests) {
|
||||
wprintw(self->window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
num = m_addfriend_norequest(m, pending_requests[num]);
|
||||
|
||||
if (num == -1)
|
||||
wprintw(self->window, "Failed to add friend.\n");
|
||||
else {
|
||||
|
@ -104,6 +108,7 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
|
|||
wprintw(self->window, "Invalid command: add expected at least one argument.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!msg)
|
||||
msg = "";
|
||||
|
||||
|
@ -111,15 +116,19 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
|
|||
wprintw(self->window, "Invalid ID length.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
|
||||
xx[0] = id[2 * i];
|
||||
xx[1] = id[2 * i + 1];
|
||||
xx[2] = '\0';
|
||||
|
||||
if (sscanf(xx, "%02x", &x) != 1) {
|
||||
wprintw(self->window, "Invalid ID.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
id_bin[i] = x;
|
||||
}
|
||||
|
||||
|
@ -128,28 +137,36 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
|
|||
}
|
||||
|
||||
int num = m_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1);
|
||||
|
||||
switch (num) {
|
||||
case FAERR_TOOLONG:
|
||||
wprintw(self->window, "Message is too long.\n");
|
||||
break;
|
||||
|
||||
case FAERR_NOMESSAGE:
|
||||
wprintw(self->window, "Please add a message to your request.\n");
|
||||
break;
|
||||
|
||||
case FAERR_OWNKEY:
|
||||
wprintw(self->window, "That appears to be your own ID.\n");
|
||||
break;
|
||||
|
||||
case FAERR_ALREADYSENT:
|
||||
wprintw(self->window, "Friend request already sent.\n");
|
||||
break;
|
||||
|
||||
case FAERR_UNKNOWN:
|
||||
wprintw(self->window, "Undefined error when adding friend.\n");
|
||||
break;
|
||||
|
||||
case FAERR_BADCHECKSUM:
|
||||
wprintw(self->window, "Bad checksum in address.\n");
|
||||
break;
|
||||
|
||||
case FAERR_SETNEWNOSPAM:
|
||||
wprintw(self->window, "Nospam was different.\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
wprintw(self->window, "Friend added as %d.\n", num);
|
||||
on_friendadded_cb(m, num);
|
||||
|
@ -176,6 +193,7 @@ void cmd_connect(ToxWindow *self, Messenger *m, char **args)
|
|||
|
||||
dht.port = htons(atoi(port));
|
||||
uint32_t resolved_address = resolve_addr(ip);
|
||||
|
||||
if (resolved_address == 0) {
|
||||
return;
|
||||
}
|
||||
|
@ -221,6 +239,7 @@ void cmd_msg(ToxWindow *self, Messenger *m, char **args)
|
|||
{
|
||||
char *id = args[1];
|
||||
char *msg = args[2];
|
||||
|
||||
if (m_sendmessage(m, atoi(id), (uint8_t *) msg, strlen(msg) + 1) == 0)
|
||||
wprintw(self->window, "Error occurred while sending message.\n");
|
||||
else
|
||||
|
@ -233,11 +252,13 @@ void cmd_myid(ToxWindow *self, Messenger *m, char **args)
|
|||
size_t i;
|
||||
uint8_t address[FRIEND_ADDRESS_SIZE];
|
||||
getaddress(m, address);
|
||||
|
||||
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
|
||||
char xx[3];
|
||||
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
|
||||
strcat(id, xx);
|
||||
}
|
||||
|
||||
wprintw(self->window, "%s\n", id);
|
||||
}
|
||||
|
||||
|
@ -254,30 +275,27 @@ void cmd_status(ToxWindow *self, Messenger *m, char **args)
|
|||
char *status_text;
|
||||
|
||||
USERSTATUS status_kind;
|
||||
|
||||
if (!strncmp(status, "online", strlen("online"))) {
|
||||
status_kind = USERSTATUS_NONE;
|
||||
status_text = "ONLINE";
|
||||
}
|
||||
else if (!strncmp(status, "away", strlen("away"))) {
|
||||
} else if (!strncmp(status, "away", strlen("away"))) {
|
||||
status_kind = USERSTATUS_AWAY;
|
||||
status_text = "AWAY";
|
||||
}
|
||||
else if (!strncmp(status, "busy", strlen("busy"))) {
|
||||
} else if (!strncmp(status, "busy", strlen("busy"))) {
|
||||
status_kind = USERSTATUS_BUSY;
|
||||
status_text = "BUSY";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
wprintw(self->window, "Invalid status.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *msg = args[2];
|
||||
|
||||
if (msg == NULL) {
|
||||
m_set_userstatus(m, status_kind);
|
||||
wprintw(self->window, "Status set to: %s\n", status_text);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
m_set_userstatus(m, status_kind);
|
||||
m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
|
||||
wprintw(self->window, "Status set to: %s, %s\n", status_text, msg);
|
||||
|
@ -296,6 +314,7 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
|
|||
int newlines = 0;
|
||||
char cmd[MAX_STR_SIZE] = {0};
|
||||
int i;
|
||||
|
||||
for (i = 0; i < strlen(prompt_buf); ++i) {
|
||||
if (u_cmd[i] == '\n')
|
||||
++newlines;
|
||||
|
@ -304,21 +323,27 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
|
|||
}
|
||||
|
||||
int leading_spc = 0;
|
||||
|
||||
for (i = 0; i < MAX_STR_SIZE && isspace(cmd[i]); ++i)
|
||||
leading_spc++;
|
||||
|
||||
memmove(cmd, cmd + leading_spc, MAX_STR_SIZE - leading_spc);
|
||||
|
||||
int cmd_end = strlen(cmd);
|
||||
|
||||
while (cmd_end > 0 && cmd_end--)
|
||||
if (!isspace(cmd[cmd_end]))
|
||||
break;
|
||||
|
||||
cmd[cmd_end + 1] = '\0';
|
||||
|
||||
/* insert \0 at argument boundaries */
|
||||
int numargs = 0;
|
||||
|
||||
for (i = 0; i < MAX_STR_SIZE; i++) {
|
||||
if (cmd[i] == '\"')
|
||||
while (cmd[++i] != '\"'); /* skip over strings */
|
||||
|
||||
if (cmd[i] == ' ') {
|
||||
cmd[i] = '\0';
|
||||
numargs++;
|
||||
|
@ -334,6 +359,7 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
|
|||
/* read arguments into array */
|
||||
char *cmdargs[5];
|
||||
int pos = 0;
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
cmdargs[i] = cmd + pos;
|
||||
pos += strlen(cmdargs[i]) + 1;
|
||||
|
@ -348,6 +374,7 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
|
|||
if (!strcmp(cmdargs[0], commands[i].name)) {
|
||||
/* check for missing arguments */
|
||||
int j;
|
||||
|
||||
for (j = 0; j <= commands[i].numargs; j++) {
|
||||
if (strlen(cmdargs[j]) == 0) {
|
||||
wprintw(self->window, "Invalid command: %s expected %d arguments, got %d.\n",
|
||||
|
@ -355,11 +382,13 @@ static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
|
|||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* check for excess arguments */
|
||||
if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) {
|
||||
wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* pass arguments to command function */
|
||||
(commands[i].func)(self, m, cmdargs);
|
||||
return;
|
||||
|
@ -378,15 +407,14 @@ static void prompt_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
wprintw(self->window, "\nToo Long.\n");
|
||||
prompt_buf_pos = 0;
|
||||
prompt_buf[0] = 0;
|
||||
}
|
||||
else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
|
||||
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
|
||||
&& (prompt_buf_pos % (COLS - 3) == 0)) {
|
||||
prompt_buf[prompt_buf_pos++] = '\n';
|
||||
}
|
||||
else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
|
||||
} else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
|
||||
&& ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
|
||||
prompt_buf[prompt_buf_pos++] = '\n';
|
||||
}
|
||||
|
||||
prompt_buf[prompt_buf_pos++] = key;
|
||||
prompt_buf[prompt_buf_pos] = 0;
|
||||
}
|
||||
|
@ -414,6 +442,7 @@ static void prompt_onDraw(ToxWindow *self)
|
|||
getyx(self->window, y, x);
|
||||
(void) x;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < (strlen(prompt_buf)); ++i) {
|
||||
if ((prompt_buf[i] == '\n') && (y != 0))
|
||||
--y;
|
||||
|
|
|
@ -23,6 +23,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userd
|
|||
wprintw(prompt->window, "\nFriend request from:\n");
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < KEY_SIZE_BYTES; ++i) {
|
||||
wprintw(prompt->window, "%02x", public_key[i] & 0xff);
|
||||
}
|
||||
|
@ -39,6 +40,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userd
|
|||
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onMessage != NULL)
|
||||
windows[i].onMessage(&windows[i], m, friendnumber, string, length);
|
||||
|
@ -48,6 +50,7 @@ void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length
|
|||
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onAction != NULL)
|
||||
windows[i].onAction(&windows[i], m, friendnumber, string, length);
|
||||
|
@ -58,6 +61,7 @@ void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t len
|
|||
{
|
||||
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onNickChange != NULL)
|
||||
windows[i].onNickChange(&windows[i], friendnumber, string, length);
|
||||
|
@ -68,6 +72,7 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l
|
|||
{
|
||||
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onStatusChange != NULL)
|
||||
windows[i].onStatusChange(&windows[i], friendnumber, string, length);
|
||||
|
@ -77,6 +82,7 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l
|
|||
void on_friendadded(Messenger *m, int friendnumber)
|
||||
{
|
||||
friendlist_onFriendAdded(m, friendnumber);
|
||||
|
||||
if (store_data(m, DATA_FILE) != 0) {
|
||||
wprintw(prompt->window, "\nCould not store Messenger data\n");
|
||||
}
|
||||
|
@ -92,6 +98,7 @@ int add_window(Messenger *m, ToxWindow w, int n)
|
|||
return -1;
|
||||
|
||||
w.window = newwin(LINES - 2, COLS, 0, 0);
|
||||
|
||||
if (w.window == NULL)
|
||||
return -1;
|
||||
|
||||
|
@ -108,6 +115,7 @@ void del_window(ToxWindow *w, int f_num)
|
|||
active_window = 0; // Go to prompt screen
|
||||
delwin(w->window);
|
||||
int i;
|
||||
|
||||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == f_num) {
|
||||
WINDOW_STATUS[i] = -1;
|
||||
|
@ -115,6 +123,7 @@ void del_window(ToxWindow *w, int f_num)
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
clear();
|
||||
refresh();
|
||||
}
|
||||
|
@ -124,14 +133,18 @@ void set_active_window(int ch)
|
|||
{
|
||||
int f_inf = 0;
|
||||
int max = MAX_WINDOW_SLOTS - 1;
|
||||
|
||||
if (ch == '\t') {
|
||||
int i = (active_window + 1) % max;
|
||||
|
||||
while (true) {
|
||||
if (WINDOW_STATUS[i] != -1) {
|
||||
active_window = i;
|
||||
return;
|
||||
}
|
||||
|
||||
i = (i + 1) % max;
|
||||
|
||||
if (f_inf++ > max) { // infinite loop check
|
||||
endwin();
|
||||
exit(2);
|
||||
|
@ -139,13 +152,17 @@ void set_active_window(int ch)
|
|||
}
|
||||
} else {
|
||||
int i = active_window - 1;
|
||||
|
||||
if (i < 0) i = max;
|
||||
|
||||
while (true) {
|
||||
if (WINDOW_STATUS[i] != -1) {
|
||||
active_window = i;
|
||||
return;
|
||||
}
|
||||
|
||||
if (--i < 0) i = max;
|
||||
|
||||
if (f_inf++ > max) {
|
||||
endwin();
|
||||
exit(2);
|
||||
|
@ -158,10 +175,12 @@ void init_window_status()
|
|||
{
|
||||
/* Default window values decrement from -2 */
|
||||
int i;
|
||||
|
||||
for (i = 0; i < N_DEFAULT_WINS; ++i)
|
||||
WINDOW_STATUS[i] = -(i + 2);
|
||||
|
||||
int j;
|
||||
|
||||
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
|
||||
WINDOW_STATUS[j] = -1;
|
||||
}
|
||||
|
@ -172,6 +191,7 @@ ToxWindow *init_windows()
|
|||
int n_prompt = 0;
|
||||
int n_friendslist = 1;
|
||||
int n_dhtstatus = 2;
|
||||
|
||||
if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1
|
||||
|| add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1
|
||||
|| add_window(m, new_dhtstatus(), n_dhtstatus) == -1) {
|
||||
|
@ -179,6 +199,7 @@ ToxWindow *init_windows()
|
|||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
active_window = n_prompt;
|
||||
prompt = &windows[n_prompt];
|
||||
return prompt;
|
||||
|
@ -200,16 +221,19 @@ static void draw_bar()
|
|||
attroff(COLOR_PAIR(4) | A_BOLD);
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
|
||||
if (WINDOW_STATUS[i] != -1) {
|
||||
if (i == active_window)
|
||||
attron(A_BOLD);
|
||||
|
||||
odd = (odd + 1) % blinkrate;
|
||||
|
||||
if (windows[i].blink && (odd < (blinkrate / 2)))
|
||||
attron(COLOR_PAIR(3));
|
||||
|
||||
printw(" %s", windows[i].title);
|
||||
|
||||
if (windows[i].blink && (odd < (blinkrate / 2)))
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
|
@ -218,6 +242,7 @@ static void draw_bar()
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
|
@ -238,6 +263,7 @@ void draw_active_window(Messenger *m)
|
|||
|
||||
/* Handle input */
|
||||
int ch = getch();
|
||||
|
||||
if (ch == '\t' || ch == KEY_BTAB)
|
||||
set_active_window(ch);
|
||||
else if (ch != ERR)
|
||||
|
|
Loading…
Reference in New Issue
Block a user