mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge remote-tracking branch 'upstream/master' into cleanup_unix_time_id_eq_cpy_is_timeout
Conflicts: toxcore/net_crypto.c
This commit is contained in:
commit
065495cd7c
27
.gitignore
vendored
27
.gitignore
vendored
@ -3,31 +3,28 @@
|
||||
.DS_Store?
|
||||
._*
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
.Trash*
|
||||
Icon?
|
||||
ethumbs.db
|
||||
Thumbs.db
|
||||
*.tmp
|
||||
|
||||
//nacl build
|
||||
nacl/build/
|
||||
build/
|
||||
!build/Makefile.am
|
||||
sodium
|
||||
|
||||
# Make
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
Makefile
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
tags
|
||||
Makefile.in
|
||||
|
||||
# Testing
|
||||
testing/data
|
||||
*~
|
||||
|
||||
# Vim
|
||||
*.swp
|
||||
|
||||
# Ctags
|
||||
tags
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.lo
|
||||
@ -37,14 +34,13 @@ tags
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.swp
|
||||
*.la
|
||||
|
||||
# Misc (?)
|
||||
m4/*
|
||||
!m4/pkg.m4
|
||||
configure
|
||||
configure_aux
|
||||
Makefile.in
|
||||
!m4/pkg.m4
|
||||
aclocal.m4
|
||||
config.h*
|
||||
config.log
|
||||
@ -53,13 +49,12 @@ stamp-h1
|
||||
autom4te.cache
|
||||
libtoxcore.pc
|
||||
libtool
|
||||
|
||||
.deps
|
||||
.libs
|
||||
.dirstamp
|
||||
|
||||
#netbeans
|
||||
# Netbeans
|
||||
nbproject
|
||||
|
||||
#astyle
|
||||
# astyle
|
||||
*.orig
|
||||
|
@ -12,9 +12,7 @@ With the rise of governmental monitoring programs, Tox aims to be an easy to use
|
||||
**Website translations**: [here](https://github.com/Tox/tox.im)<br/>
|
||||
**Qt GUI**: [see nurupo's repository](https://github.com/nurupo/ProjectTox-Qt-GUI)
|
||||
|
||||
**How to build Tox on Linux**: [YouTube video](http://www.youtube.com/watch?v=M4WXE4VKmyg)<br />
|
||||
**How to use Tox on Windows**: [YouTube video](http://www.youtube.com/watch?v=qg_j_sDb6WQ)<br />
|
||||
**For Mac OSX read** [INSTALL.md](INSTALL.md)
|
||||
**How to build Tox** [INSTALL.md](INSTALL.md)
|
||||
|
||||
### Objectives:
|
||||
|
||||
|
@ -276,18 +276,18 @@ START_TEST(test_messenger_state_saveloadsave)
|
||||
* c) a second save() is of equal size
|
||||
* d) the second save() is of equal content */
|
||||
size_t i, extra = 64;
|
||||
size_t size = Messenger_size(m);
|
||||
size_t size = messenger_size(m);
|
||||
uint8_t buffer[size + 2 * extra];
|
||||
memset(buffer, 0xCD, extra);
|
||||
memset(buffer + extra + size, 0xCD, extra);
|
||||
Messenger_save(m, buffer + extra);
|
||||
messenger_save(m, buffer + extra);
|
||||
|
||||
for (i = 0; i < extra; i++) {
|
||||
ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from Messenger_save() @%u", i);
|
||||
ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from Messenger_save() @%u", i);
|
||||
ck_assert_msg(buffer[i] == 0xCD, "Buffer underwritten from messenger_save() @%u", i);
|
||||
ck_assert_msg(buffer[extra + size + i] == 0xCD, "Buffer overwritten from messenger_save() @%u", i);
|
||||
}
|
||||
|
||||
int res = Messenger_load(m, buffer + extra, size);
|
||||
int res = messenger_load(m, buffer + extra, size);
|
||||
|
||||
if (res == -1)
|
||||
ck_assert_msg(res == 0, "Failed to load back stored buffer: res == -1");
|
||||
@ -300,11 +300,11 @@ START_TEST(test_messenger_state_saveloadsave)
|
||||
ck_assert_msg(res == 0, msg);
|
||||
}
|
||||
|
||||
size_t size2 = Messenger_size(m);
|
||||
size_t size2 = messenger_size(m);
|
||||
ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2);
|
||||
|
||||
uint8_t buffer2[size2];
|
||||
Messenger_save(m, buffer2);
|
||||
messenger_save(m, buffer2);
|
||||
|
||||
ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "Messenger state changed by store/load/store cycle");
|
||||
}
|
||||
@ -350,7 +350,7 @@ int main(int argc, char *argv[])
|
||||
bad_id = hex_string_to_bin(bad_id_str);
|
||||
|
||||
/* IPv6 status from global define */
|
||||
m = initMessenger(TOX_ENABLE_IPV6_DEFAULT);
|
||||
m = new_messenger(TOX_ENABLE_IPV6_DEFAULT);
|
||||
|
||||
/* setup a default friend and friendnum */
|
||||
if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0)
|
||||
@ -372,7 +372,7 @@ int main(int argc, char *argv[])
|
||||
free(good_id_b);
|
||||
free(bad_id);
|
||||
|
||||
cleanupMessenger(m);
|
||||
kill_messenger(m);
|
||||
|
||||
return number_failed;
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ int main(int argc, char *argv[])
|
||||
exit(0);
|
||||
}
|
||||
|
||||
m = initMessenger(ipv6enabled);
|
||||
m = new_messenger(ipv6enabled);
|
||||
|
||||
if ( !m ) {
|
||||
fputs("Failed to allocate messenger datastructure\n", stderr);
|
||||
@ -139,7 +139,7 @@ int main(int argc, char *argv[])
|
||||
int read;
|
||||
uint8_t buffer[128000];
|
||||
read = fread(buffer, 1, 128000, file);
|
||||
printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read));
|
||||
printf("Messenger loaded: %i\n", messenger_load(m, buffer, read));
|
||||
fclose(file);
|
||||
|
||||
}
|
||||
@ -178,7 +178,7 @@ int main(int argc, char *argv[])
|
||||
printf("%s\n", name);
|
||||
|
||||
m_sendmessage(m, num, (uint8_t *)"Test", 5);
|
||||
doMessenger(m);
|
||||
do_messenger(m);
|
||||
c_sleep(30);
|
||||
FILE *file = fopen("Save.bak", "wb");
|
||||
|
||||
@ -186,11 +186,11 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t *buffer = malloc(Messenger_size(m));
|
||||
Messenger_save(m, buffer);
|
||||
size_t write_result = fwrite(buffer, 1, Messenger_size(m), file);
|
||||
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)) {
|
||||
if (write_result < messenger_size(m)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -198,5 +198,5 @@ int main(int argc, char *argv[])
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
cleanupMessenger(m);
|
||||
kill_messenger(m);
|
||||
}
|
||||
|
@ -198,8 +198,8 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||
memset(connection, 0, sizeof(Connection));
|
||||
|
||||
uint32_t handshake_id1 = handshake_id(ludp, ip_port);
|
||||
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
||||
uint8_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
|
||||
/* Add randomness to timeout to prevent connections getting stuck in a loop. */
|
||||
uint8_t timeout = CONNECTION_TIMEOUT + rand() % CONNECTION_TIMEOUT;
|
||||
|
||||
*connection = (Connection) {
|
||||
.ip_port = ip_port,
|
||||
@ -260,7 +260,7 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
|
||||
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
|
||||
memset(connection, 0, sizeof(Connection));
|
||||
/* Add randomness to timeout to prevent connections getting stuck in a loop. */
|
||||
uint8_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
|
||||
uint8_t timeout = CONNECTION_TIMEOUT + rand() % CONNECTION_TIMEOUT;
|
||||
|
||||
*connection = (Connection) {
|
||||
.ip_port = ip_port,
|
||||
@ -403,7 +403,7 @@ int connection_confirmed(Lossless_UDP *ludp, int connection_id)
|
||||
}
|
||||
|
||||
/* Confirm an incoming connection.
|
||||
* Also disables the auto kill timeout on incomming connections.
|
||||
* Also disable the auto kill timeout on incomming connections.
|
||||
*
|
||||
* return 0 on success
|
||||
* return -1 on failure.
|
||||
@ -777,7 +777,7 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3
|
||||
/* if handshake_id2 is what we sent previously as handshake_id1 */
|
||||
if (handshake_id2 == connection->handshake_id1) {
|
||||
connection->status = LUDP_NOT_CONFIRMED;
|
||||
/* NOTE: is this necessary?
|
||||
/* NOTE: Is this necessary?
|
||||
connection->handshake_id2 = handshake_id1; */
|
||||
connection->orecv_packetnum = handshake_id2;
|
||||
connection->osent_packetnum = handshake_id1;
|
||||
@ -847,7 +847,7 @@ static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter,
|
||||
/*
|
||||
* Automatically adjusts send rates of data packets for optimal transmission.
|
||||
*
|
||||
* TODO: Impove this.
|
||||
* TODO: Improve this.
|
||||
*/
|
||||
static void adjust_datasendspeed(Connection *connection, uint32_t req_packets)
|
||||
{
|
||||
@ -1144,7 +1144,7 @@ static void adjust_rates(Lossless_UDP *ludp)
|
||||
}
|
||||
}
|
||||
|
||||
/* Call this function a couple times per second It's the main loop. */
|
||||
/* Call this function a couple times per second. It is the main loop. */
|
||||
void do_lossless_udp(Lossless_UDP *ludp)
|
||||
{
|
||||
do_new(ludp);
|
||||
|
@ -38,10 +38,10 @@
|
||||
/* Maximum number of data packets in the buffer. */
|
||||
#define MAX_REQUESTED_PACKETS 256
|
||||
|
||||
/* Timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT. */
|
||||
#define CONNEXION_TIMEOUT 5
|
||||
/* Timeout per connection is randomly set between CONNECTION_TIMEOUT and 2*CONNECTION_TIMEOUT. */
|
||||
#define CONNECTION_TIMEOUT 5
|
||||
|
||||
/* Initial amount of sync/hanshake packets to send per second. */
|
||||
/* Initial amount of sync/handshake packets to send per second. */
|
||||
#define SYNC_RATE 2
|
||||
|
||||
/* Initial send rate of data. */
|
||||
@ -124,7 +124,7 @@ typedef struct {
|
||||
uint8_t send_counter;
|
||||
uint8_t timeout; /* connection timeout in seconds. */
|
||||
|
||||
/* is the connection confirmed or not 1 if yes, 0 if no */
|
||||
/* Is the connection confirmed or not? 1 if yes, 0 if no */
|
||||
uint8_t confirmed;
|
||||
} Connection;
|
||||
|
||||
@ -249,7 +249,7 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id);
|
||||
*/
|
||||
int is_connected(Lossless_UDP *ludp, int connection_id);
|
||||
|
||||
/* Call this function a couple times per second It's the main loop. */
|
||||
/* Call this function a couple times per second. It is the main loop. */
|
||||
void do_lossless_udp(Lossless_UDP *ludp);
|
||||
|
||||
/* This function sets up LosslessUDP packet handling. */
|
||||
|
@ -714,7 +714,7 @@ IP_Port get_friend_ipport(Messenger *m, int friendnumber)
|
||||
|
||||
int crypt_id = m->friendlist[friendnumber].crypt_connection_id;
|
||||
|
||||
if (is_cryptoconnected(m->net_crypto, crypt_id) != 3)
|
||||
if (is_cryptoconnected(m->net_crypto, crypt_id) != CRYPTO_CONN_ESTABLISHED)
|
||||
return zero;
|
||||
|
||||
return connection_ip(m->net_crypto->lossless_udp, m->net_crypto->crypto_connections[crypt_id].number);
|
||||
@ -1357,7 +1357,7 @@ static void LANdiscovery(Messenger *m)
|
||||
}
|
||||
|
||||
/* Run this at startup. */
|
||||
Messenger *initMessenger(uint8_t ipv6enabled)
|
||||
Messenger *new_messenger(uint8_t ipv6enabled)
|
||||
{
|
||||
Messenger *m = calloc(1, sizeof(Messenger));
|
||||
|
||||
@ -1402,7 +1402,7 @@ Messenger *initMessenger(uint8_t ipv6enabled)
|
||||
}
|
||||
|
||||
/* Run this before closing shop. */
|
||||
void cleanupMessenger(Messenger *m)
|
||||
void kill_messenger(Messenger *m)
|
||||
{
|
||||
/* FIXME TODO: ideally cleanupMessenger will mirror initMessenger.
|
||||
* This requires the other modules to expose cleanup functions.
|
||||
@ -1415,7 +1415,7 @@ void cleanupMessenger(Messenger *m)
|
||||
}
|
||||
|
||||
/* TODO: Make this function not suck. */
|
||||
void doFriends(Messenger *m)
|
||||
void do_friends(Messenger *m)
|
||||
{
|
||||
/* TODO: Add incoming connections and some other stuff. */
|
||||
uint32_t i;
|
||||
@ -1454,13 +1454,13 @@ void doFriends(Messenger *m)
|
||||
int friendok = DHT_getfriendip(m->dht, m->friendlist[i].client_id, &friendip);
|
||||
|
||||
switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) {
|
||||
case 0:
|
||||
case CRYPTO_CONN_NO_CONNECTION:
|
||||
if (friendok == 1)
|
||||
m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip);
|
||||
|
||||
break;
|
||||
|
||||
case 3: /* Connection is established. */
|
||||
case CRYPTO_CONN_ESTABLISHED: /* Connection is established. */
|
||||
set_friend_status(m, i, FRIEND_ONLINE);
|
||||
m->friendlist[i].name_sent = 0;
|
||||
m->friendlist[i].userstatus_sent = 0;
|
||||
@ -1468,7 +1468,7 @@ void doFriends(Messenger *m)
|
||||
m->friendlist[i].ping_lastrecv = temp_time;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
case CRYPTO_CONN_TIMED_OUT:
|
||||
crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
|
||||
m->friendlist[i].crypt_connection_id = -1;
|
||||
break;
|
||||
@ -1706,7 +1706,7 @@ void doFriends(Messenger *m)
|
||||
}
|
||||
} else {
|
||||
if (is_cryptoconnected(m->net_crypto,
|
||||
m->friendlist[i].crypt_connection_id) == 4) { /* If the connection timed out, kill it. */
|
||||
m->friendlist[i].crypt_connection_id) == CRYPTO_CONN_TIMED_OUT) { /* If the connection timed out, kill it. */
|
||||
crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
|
||||
m->friendlist[i].crypt_connection_id = -1;
|
||||
set_friend_status(m, i, FRIEND_CONFIRMED);
|
||||
@ -1725,7 +1725,7 @@ void doFriends(Messenger *m)
|
||||
}
|
||||
}
|
||||
|
||||
void doInbound(Messenger *m)
|
||||
void do_inbound(Messenger *m)
|
||||
{
|
||||
uint8_t secret_nonce[crypto_box_NONCEBYTES];
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
@ -1767,7 +1767,7 @@ static char *ID2String(uint8_t *client_id)
|
||||
#endif
|
||||
|
||||
/* The main loop that needs to be run at least 20 times per second. */
|
||||
void doMessenger(Messenger *m)
|
||||
void do_messenger(Messenger *m)
|
||||
{
|
||||
unix_time_update();
|
||||
|
||||
@ -1775,8 +1775,8 @@ void doMessenger(Messenger *m)
|
||||
|
||||
do_DHT(m->dht);
|
||||
do_net_crypto(m->net_crypto);
|
||||
doFriends(m);
|
||||
doInbound(m);
|
||||
do_friends(m);
|
||||
do_inbound(m);
|
||||
do_allgroupchats(m);
|
||||
LANdiscovery(m);
|
||||
|
||||
@ -1875,17 +1875,17 @@ void doMessenger(Messenger *m)
|
||||
/*
|
||||
* functions to avoid excessive polling
|
||||
*/
|
||||
int waitprepareMessenger(Messenger *m, uint8_t *data, uint16_t *lenptr)
|
||||
int wait_prepare_messenger(Messenger *m, uint8_t *data, uint16_t *lenptr)
|
||||
{
|
||||
return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data, lenptr);
|
||||
}
|
||||
|
||||
int waitexecuteMessenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds)
|
||||
int wait_execute_messenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds)
|
||||
{
|
||||
return networking_wait_execute(data, len, milliseconds);
|
||||
};
|
||||
|
||||
void waitcleanupMessenger(Messenger *m, uint8_t *data, uint16_t len)
|
||||
void wait_cleanup_messenger(Messenger *m, uint8_t *data, uint16_t len)
|
||||
{
|
||||
networking_wait_cleanup(m->net, data, len);
|
||||
}
|
||||
@ -2039,7 +2039,7 @@ static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length)
|
||||
#define MESSENGER_STATE_TYPE_NAME 4
|
||||
|
||||
/* return size of the messenger data (for saving) */
|
||||
uint32_t Messenger_size(Messenger *m)
|
||||
uint32_t messenger_size(Messenger *m)
|
||||
{
|
||||
uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
|
||||
return size32 * 2 // global cookie
|
||||
@ -2060,7 +2060,7 @@ static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t typ
|
||||
}
|
||||
|
||||
/* Save the messenger in data of size Messenger_size(). */
|
||||
void Messenger_save(Messenger *m, uint8_t *data)
|
||||
void messenger_save(Messenger *m, uint8_t *data)
|
||||
{
|
||||
uint32_t len;
|
||||
uint16_t type;
|
||||
@ -2163,7 +2163,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le
|
||||
}
|
||||
|
||||
/* Load the messenger from data of size length. */
|
||||
int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
|
||||
int messenger_load(Messenger *m, uint8_t *data, uint32_t length)
|
||||
{
|
||||
uint32_t cookie_len = 2 * sizeof(uint32_t);
|
||||
|
||||
|
@ -580,33 +580,33 @@ int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length)
|
||||
* return allocated instance of Messenger on success.
|
||||
* return 0 if there are problems.
|
||||
*/
|
||||
Messenger *initMessenger(uint8_t ipv6enabled);
|
||||
Messenger *new_messenger(uint8_t ipv6enabled);
|
||||
|
||||
/* Run this before closing shop
|
||||
* Free all datastructures.
|
||||
*/
|
||||
void cleanupMessenger(Messenger *M);
|
||||
void kill_messenger(Messenger *M);
|
||||
|
||||
/* The main loop that needs to be run at least 20 times per second. */
|
||||
void doMessenger(Messenger *m);
|
||||
void do_messenger(Messenger *m);
|
||||
|
||||
/*
|
||||
* functions to avoid excessive polling
|
||||
*/
|
||||
int waitprepareMessenger(Messenger *m, uint8_t *data, uint16_t *lenptr);
|
||||
int waitexecuteMessenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds);
|
||||
void waitcleanupMessenger(Messenger *m, uint8_t *data, uint16_t len);
|
||||
int wait_prepare_messenger(Messenger *m, uint8_t *data, uint16_t *lenptr);
|
||||
int wait_execute_messenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds);
|
||||
void wait_cleanup_messenger(Messenger *m, uint8_t *data, uint16_t len);
|
||||
|
||||
/* SAVING AND LOADING FUNCTIONS: */
|
||||
|
||||
/* return size of the messenger data (for saving). */
|
||||
uint32_t Messenger_size(Messenger *m);
|
||||
uint32_t messenger_size(Messenger *m);
|
||||
|
||||
/* Save the messenger in data (must be allocated memory of size Messenger_size()) */
|
||||
void Messenger_save(Messenger *m, uint8_t *data);
|
||||
void messenger_save(Messenger *m, uint8_t *data);
|
||||
|
||||
/* Load the messenger from data of size length. */
|
||||
int Messenger_load(Messenger *m, uint8_t *data, uint32_t length);
|
||||
int messenger_load(Messenger *m, uint8_t *data, uint32_t length);
|
||||
|
||||
/* Return the number of friends in the instance m.
|
||||
* You should use this to determine how much memory to allocate
|
||||
|
@ -31,12 +31,6 @@
|
||||
#include "net_crypto.h"
|
||||
#include "util.h"
|
||||
|
||||
#define CONN_NO_CONNECTION 0
|
||||
#define CONN_HANDSHAKE_SENT 1
|
||||
#define CONN_NOT_CONFIRMED 2
|
||||
#define CONN_ESTABLISHED 3
|
||||
#define CONN_TIMED_OUT 4
|
||||
|
||||
static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id)
|
||||
{
|
||||
return (uint32_t)crypt_connection_id >= c->crypto_connections_length;
|
||||
@ -153,7 +147,7 @@ void random_nonce(uint8_t *nonce)
|
||||
static uint8_t base_nonce[crypto_box_NONCEBYTES];
|
||||
static uint8_t nonce_set = 0;
|
||||
|
||||
/*Gives a nonce guaranteed to be different from previous ones.*/
|
||||
/* Gives a nonce guaranteed to be different from previous ones.*/
|
||||
void new_nonce(uint8_t *nonce)
|
||||
{
|
||||
if (nonce_set == 0) {
|
||||
@ -175,7 +169,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data)
|
||||
if (crypt_connection_id_not_valid(c, crypt_connection_id))
|
||||
return 0;
|
||||
|
||||
if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED)
|
||||
if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_ESTABLISHED)
|
||||
return 0;
|
||||
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
@ -221,7 +215,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
|
||||
if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1)
|
||||
return 0;
|
||||
|
||||
if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED)
|
||||
if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_ESTABLISHED)
|
||||
return 0;
|
||||
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
@ -241,7 +235,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Ceate a request to peer.
|
||||
/* Create a request to peer.
|
||||
* send_public_key and send_secret_key are the pub/secret keys of the sender.
|
||||
* recv_public_key is public key of reciever.
|
||||
* packet must be an array of MAX_DATA_SIZE big.
|
||||
@ -421,7 +415,7 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key)
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < c->crypto_connections_length; ++i)
|
||||
if (c->crypto_connections[i].status != CONN_NO_CONNECTION)
|
||||
if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION)
|
||||
if (id_equal(public_key, c->crypto_connections[i].public_key))
|
||||
return i;
|
||||
|
||||
@ -474,14 +468,14 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port)
|
||||
c->crypto_connections[c->crypto_connections_length].number = ~0;
|
||||
|
||||
for (i = 0; i <= c->crypto_connections_length; ++i) {
|
||||
if (c->crypto_connections[i].status == CONN_NO_CONNECTION) {
|
||||
if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) {
|
||||
int id_new = new_connection(c->lossless_udp, ip_port);
|
||||
|
||||
if (id_new == -1)
|
||||
return -1;
|
||||
|
||||
c->crypto_connections[i].number = id_new;
|
||||
c->crypto_connections[i].status = CONN_HANDSHAKE_SENT;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_HANDSHAKE_SENT;
|
||||
random_nonce(c->crypto_connections[i].recv_nonce);
|
||||
id_copy(c->crypto_connections[i].public_key, public_key);
|
||||
crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key);
|
||||
@ -550,15 +544,15 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
|
||||
if (crypt_connection_id_not_valid(c, crypt_connection_id))
|
||||
return 1;
|
||||
|
||||
if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) {
|
||||
c->crypto_connections[crypt_connection_id].status = CONN_NO_CONNECTION;
|
||||
if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_NO_CONNECTION) {
|
||||
c->crypto_connections[crypt_connection_id].status = CRYPTO_CONN_NO_CONNECTION;
|
||||
kill_connection(c->lossless_udp, c->crypto_connections[crypt_connection_id].number);
|
||||
memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection));
|
||||
c->crypto_connections[crypt_connection_id].number = ~0;
|
||||
uint32_t i;
|
||||
|
||||
for (i = c->crypto_connections_length; i != 0; --i) {
|
||||
if (c->crypto_connections[i - 1].status != CONN_NO_CONNECTION)
|
||||
if (c->crypto_connections[i - 1].status != CRYPTO_CONN_NO_CONNECTION)
|
||||
break;
|
||||
}
|
||||
|
||||
@ -598,9 +592,9 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
|
||||
c->crypto_connections[c->crypto_connections_length].number = ~0;
|
||||
|
||||
for (i = 0; i <= c->crypto_connections_length; ++i) {
|
||||
if (c->crypto_connections[i].status == CONN_NO_CONNECTION) {
|
||||
if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) {
|
||||
c->crypto_connections[i].number = connection_id;
|
||||
c->crypto_connections[i].status = CONN_NOT_CONFIRMED;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED;
|
||||
c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT;
|
||||
random_nonce(c->crypto_connections[i].recv_nonce);
|
||||
memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES);
|
||||
@ -621,9 +615,9 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
|
||||
c->crypto_connections[i].sessionsecret_key,
|
||||
c->crypto_connections[i].shared_key);
|
||||
c->crypto_connections[i].status =
|
||||
CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */
|
||||
CRYPTO_CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */
|
||||
write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero));
|
||||
c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */
|
||||
return i;
|
||||
}
|
||||
|
||||
@ -645,7 +639,7 @@ int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id)
|
||||
if ((unsigned int)crypt_connection_id < c->crypto_connections_length)
|
||||
return c->crypto_connections[crypt_connection_id].status;
|
||||
|
||||
return CONN_NO_CONNECTION;
|
||||
return CRYPTO_CONN_NO_CONNECTION;
|
||||
}
|
||||
|
||||
void new_keys(Net_Crypto *c)
|
||||
@ -678,10 +672,10 @@ static void receive_crypto(Net_Crypto *c)
|
||||
uint64_t temp_time = unix_time();
|
||||
|
||||
for (i = 0; i < c->crypto_connections_length; ++i) {
|
||||
if (c->crypto_connections[i].status == CONN_NO_CONNECTION)
|
||||
if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION)
|
||||
continue;
|
||||
|
||||
if (c->crypto_connections[i].status == CONN_HANDSHAKE_SENT) {
|
||||
if (c->crypto_connections[i].status == CRYPTO_CONN_HANDSHAKE_SENT) {
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
uint8_t secret_nonce[crypto_box_NONCEBYTES];
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
@ -701,25 +695,25 @@ static void receive_crypto(Net_Crypto *c)
|
||||
c->crypto_connections[i].sessionsecret_key,
|
||||
c->crypto_connections[i].shared_key);
|
||||
c->crypto_connections[i].status =
|
||||
CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */
|
||||
CRYPTO_CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */
|
||||
write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero));
|
||||
c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */
|
||||
} else {
|
||||
/* This should not happen, timeout the connection if it does. */
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT;
|
||||
}
|
||||
} else {
|
||||
/* This should not happen, timeout the connection if it does. */
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT;
|
||||
}
|
||||
} else if (id_packet(c->lossless_udp,
|
||||
c->crypto_connections[i].number) != -1) {
|
||||
/* This should not happen, timeout the connection if it does. */
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
if (c->crypto_connections[i].status == CONN_NOT_CONFIRMED) {
|
||||
if (c->crypto_connections[i].status == CRYPTO_CONN_NOT_CONFIRMED) {
|
||||
if (id_packet(c->lossless_udp, c->crypto_connections[i].number) == 3) {
|
||||
uint8_t temp_data[MAX_DATA_SIZE];
|
||||
uint8_t data[MAX_DATA_SIZE];
|
||||
@ -734,22 +728,22 @@ static void receive_crypto(Net_Crypto *c)
|
||||
encrypt_precompute(c->crypto_connections[i].peersessionpublic_key,
|
||||
c->crypto_connections[i].sessionsecret_key,
|
||||
c->crypto_connections[i].shared_key);
|
||||
c->crypto_connections[i].status = CONN_ESTABLISHED;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_ESTABLISHED;
|
||||
c->crypto_connections[i].timeout = ~0;
|
||||
/* Connection is accepted. */
|
||||
confirm_connection(c->lossless_udp, c->crypto_connections[i].number);
|
||||
} else {
|
||||
/* This should not happen, timeout the connection if it does. */
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT;
|
||||
}
|
||||
} else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) {
|
||||
/* This should not happen, timeout the connection if it does. */
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
if (temp_time > c->crypto_connections[i].timeout) {
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -788,9 +782,9 @@ static void kill_timedout(Net_Crypto *c)
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < c->crypto_connections_length; ++i) {
|
||||
if (c->crypto_connections[i].status != CONN_NO_CONNECTION
|
||||
if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION
|
||||
&& is_connected(c->lossless_udp, c->crypto_connections[i].number) == LUDP_TIMED_OUT)
|
||||
c->crypto_connections[i].status = CONN_TIMED_OUT;
|
||||
c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,13 @@
|
||||
|
||||
#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */
|
||||
#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */
|
||||
#define CRYPTO_HANDSHAKE_TIMEOUT (CONNEXION_TIMEOUT * 2)
|
||||
#define CRYPTO_HANDSHAKE_TIMEOUT (CONNECTION_TIMEOUT * 2)
|
||||
|
||||
#define CRYPTO_CONN_NO_CONNECTION 0
|
||||
#define CRYPTO_CONN_HANDSHAKE_SENT 1
|
||||
#define CRYPTO_CONN_NOT_CONFIRMED 2
|
||||
#define CRYPTO_CONN_ESTABLISHED 3
|
||||
#define CRYPTO_CONN_TIMED_OUT 4
|
||||
|
||||
typedef struct {
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The real public key of the peer. */
|
||||
@ -38,7 +44,7 @@ typedef struct {
|
||||
uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* Our private key for this session. */
|
||||
uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */
|
||||
uint8_t shared_key[crypto_box_BEFORENMBYTES]; /* The precomputed shared key from encrypt_precompute. */
|
||||
uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
||||
uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connection is not confirmed yet
|
||||
* (we have received a handshake but no empty data packet), 3 if the connection is established.
|
||||
* 4 if the connection is timed out.
|
||||
*/
|
||||
|
@ -3,6 +3,24 @@
|
||||
*
|
||||
* This file is donated to the Tox Project.
|
||||
* Copyright 2013 plutooo
|
||||
*
|
||||
* Copyright (C) 2013 Tox project All Rights Reserved.
|
||||
*
|
||||
* This file is part of Tox.
|
||||
*
|
||||
* Tox is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tox is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
|
@ -3,6 +3,23 @@
|
||||
*
|
||||
* This file is donated to the Tox Project.
|
||||
* Copyright 2013 plutooo
|
||||
*
|
||||
* Copyright (C) 2013 Tox project All Rights Reserved.
|
||||
*
|
||||
* This file is part of Tox.
|
||||
*
|
||||
* Tox is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tox is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef __PING_H__
|
||||
#define __PING_H__
|
||||
|
@ -609,7 +609,7 @@ int tox_isconnected(Tox *tox)
|
||||
*/
|
||||
Tox *tox_new(uint8_t ipv6enabled)
|
||||
{
|
||||
return initMessenger(ipv6enabled);
|
||||
return new_messenger(ipv6enabled);
|
||||
}
|
||||
|
||||
/* Run this before closing shop.
|
||||
@ -618,14 +618,14 @@ Tox *tox_new(uint8_t ipv6enabled)
|
||||
void tox_kill(Tox *tox)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
cleanupMessenger(m);
|
||||
kill_messenger(m);
|
||||
}
|
||||
|
||||
/* The main loop that needs to be run at least 20 times per second. */
|
||||
void tox_do(Tox *tox)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
doMessenger(m);
|
||||
do_messenger(m);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -634,19 +634,19 @@ void tox_do(Tox *tox)
|
||||
int tox_wait_prepare(Tox *tox, uint8_t *data, uint16_t *lenptr)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
return waitprepareMessenger(m, data, lenptr);
|
||||
return wait_prepare_messenger(m, data, lenptr);
|
||||
}
|
||||
|
||||
int tox_wait_execute(Tox *tox, uint8_t *data, uint16_t len, uint16_t milliseconds)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
return waitexecuteMessenger(m, data, len, milliseconds);
|
||||
return wait_execute_messenger(m, data, len, milliseconds);
|
||||
}
|
||||
|
||||
void tox_wait_cleanup(Tox *tox, uint8_t *data, uint16_t len)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
waitcleanupMessenger(m, data, len);
|
||||
wait_cleanup_messenger(m, data, len);
|
||||
}
|
||||
|
||||
/* SAVING AND LOADING FUNCTIONS: */
|
||||
@ -655,20 +655,20 @@ void tox_wait_cleanup(Tox *tox, uint8_t *data, uint16_t len)
|
||||
uint32_t tox_size(Tox *tox)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
return Messenger_size(m);
|
||||
return messenger_size(m);
|
||||
}
|
||||
|
||||
/* Save the messenger in data (must be allocated memory of size Messenger_size()). */
|
||||
void tox_save(Tox *tox, uint8_t *data)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
Messenger_save(m, data);
|
||||
messenger_save(m, data);
|
||||
}
|
||||
|
||||
/* Load the messenger from data of size length. */
|
||||
int tox_load(Tox *tox, uint8_t *data, uint32_t length)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
return Messenger_load(m, data, length);
|
||||
return messenger_load(m, data, length);
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,23 @@
|
||||
*
|
||||
* This file is donated to the Tox Project.
|
||||
* Copyright 2013 plutooo
|
||||
*
|
||||
* Copyright (C) 2013 Tox project All Rights Reserved.
|
||||
*
|
||||
* This file is part of Tox.
|
||||
*
|
||||
* Tox is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tox is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
|
@ -3,6 +3,23 @@
|
||||
*
|
||||
* This file is donated to the Tox Project.
|
||||
* Copyright 2013 plutooo
|
||||
*
|
||||
* Copyright (C) 2013 Tox project All Rights Reserved.
|
||||
*
|
||||
* This file is part of Tox.
|
||||
*
|
||||
* Tox is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tox is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_H__
|
||||
|
Loading…
x
Reference in New Issue
Block a user