mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Code cleanups.
This commit is contained in:
parent
b515eac0a3
commit
a67b4f8c6d
|
@ -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);
|
||||
|
@ -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);
|
||||
|
|
|
@ -30,12 +30,6 @@
|
|||
|
||||
#include "net_crypto.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;
|
||||
|
@ -174,7 +168,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];
|
||||
|
@ -220,7 +214,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];
|
||||
|
@ -420,7 +414,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 (memcmp(public_key, c->crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
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);
|
||||
memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,12 @@
|
|||
#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */
|
||||
#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. */
|
||||
uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* Nonce of received packets. */
|
||||
|
|
|
@ -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__
|
||||
|
|
|
@ -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…
Reference in New Issue
Block a user