Move the send tcp relay packet from Messenger to friend connection.

This commit is contained in:
irungentoo 2015-04-21 20:12:24 -04:00
parent e1a98987ff
commit 3bd4f5902c
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
4 changed files with 70 additions and 67 deletions

View File

@ -749,37 +749,6 @@ static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t
return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), 0);
}
static int send_relays(const Messenger *m, int32_t friendnumber)
{
if (friend_not_valid(m, friendnumber))
return 0;
Node_format nodes[MAX_SHARED_RELAYS];
uint8_t data[1024];
int n, length;
n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS);
unsigned int i;
for (i = 0; i < n; ++i) {
/* Associated the relays being sent with this connection.
On receiving the peer will do the same which will establish the connection. */
friend_add_tcp_relay(m->fr_c, m->friendlist[friendnumber].friendcon_id, nodes[i].ip_port, nodes[i].public_key);
}
length = pack_nodes(data, sizeof(data), nodes, n);
int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length, 0);
if (ret == 1)
m->friendlist[friendnumber].share_relays_lastsent = unix_time();
return ret;
}
static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
{
if (friend_not_valid(m, friendnumber))
@ -1922,7 +1891,6 @@ static int handle_status(void *object, int i, uint8_t status)
m->friendlist[i].userstatus_sent = 0;
m->friendlist[i].statusmessage_sent = 0;
m->friendlist[i].user_istyping_sent = 0;
m->friendlist[i].share_relays_lastsent = 0;
} else { /* Went offline. */
if (m->friendlist[i].status == FRIEND_ONLINE) {
set_friend_status(m, i, FRIEND_CONFIRMED);
@ -2187,22 +2155,6 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
break;
}
case PACKET_ID_SHARE_RELAYS: {
Node_format nodes[MAX_SHARED_RELAYS];
int n;
if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1)
break;
int j;
for (j = 0; j < n; j++) {
friend_add_tcp_relay(m->fr_c, m->friendlist[i].friendcon_id, nodes[j].ip_port, nodes[j].public_key);
}
break;
}
default: {
handle_custom_lossless_packet(object, i, temp, len);
break;
@ -2260,10 +2212,6 @@ void do_friends(Messenger *m)
m->friendlist[i].user_istyping_sent = 1;
}
if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) {
send_relays(m, i);
}
check_friend_tcp_udp(m, i);
do_receipts(m, i);
do_reqchunk_filecb(m, i);

View File

@ -42,8 +42,7 @@ enum {
MESSAGE_ACTION
};
/* NOTE: Packet ids below 20 must never be used. */
#define PACKET_ID_SHARE_RELAYS 23
/* NOTE: Packet ids below 24 must never be used. */
#define PACKET_ID_ONLINE 24
#define PACKET_ID_OFFLINE 25
#define PACKET_ID_NICKNAME 48
@ -62,9 +61,6 @@ enum {
#define PACKET_ID_MESSAGE_GROUPCHAT 99
#define PACKET_ID_LOSSY_GROUPCHAT 199
/* Max number of tcp relays sent to friends */
#define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS)
/* All packets starting with a byte in this range can be used for anything. */
#define PACKET_ID_LOSSLESS_RANGE_START 160
#define PACKET_ID_LOSSLESS_RANGE_SIZE 32
@ -110,9 +106,6 @@ enum {
/* Default start timeout in seconds between friend requests. */
#define FRIENDREQUEST_TIMEOUT 5;
/* Interval between the sending of tcp relay information */
#define FRIEND_SHARE_RELAYS_INTERVAL (5 * 60)
enum {
CONNECTION_NONE,
CONNECTION_TCP,
@ -199,7 +192,6 @@ typedef struct {
uint32_t message_id; // a semi-unique id used in read receipts.
uint32_t friendrequest_nospam; // The nospam number used in the friend request.
uint64_t last_seen_time;
uint64_t share_relays_lastsent;
uint8_t last_connection_udp_tcp;
struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES];
unsigned int num_sending_files;

View File

@ -198,6 +198,43 @@ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_
}
}
static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
{
Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
if (!friend_con)
return 0;
Node_format nodes[MAX_SHARED_RELAYS];
uint8_t data[1024];
int n, length;
n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS);
unsigned int i;
for (i = 0; i < n; ++i) {
/* Associated the relays being sent with this connection.
On receiving the peer will do the same which will establish the connection. */
friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key);
}
length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n);
if (length <= 0)
return 0;
data[0] = PACKET_ID_SHARE_RELAYS;
++length;
if (write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, data, length, 0) != -1) {
friend_con->share_relays_lastsent = unix_time();
return 1;
}
return 0;
}
/* callback for recv TCP relay nodes. */
static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key)
{
@ -293,6 +330,7 @@ static int handle_status(void *object, int number, uint8_t status)
call_cb = 1;
friend_con->status = FRIENDCONN_STATUS_CONNECTED;
friend_con->ping_lastrecv = unix_time();
friend_con->share_relays_lastsent = 0;
onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status);
} else { /* Went offline. */
if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) {
@ -326,18 +364,30 @@ static int handle_packet(void *object, int number, uint8_t *data, uint16_t lengt
Friend_Connections *fr_c = object;
Friend_Conn *friend_con = get_conn(fr_c, number);
if (!friend_con)
return -1;
if (data[0] == PACKET_ID_FRIEND_REQUESTS) {
if (fr_c->fr_request_callback)
fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length);
return 0;
}
if (!friend_con)
return -1;
if (data[0] == PACKET_ID_ALIVE) {
} else if (data[0] == PACKET_ID_ALIVE) {
friend_con->ping_lastrecv = unix_time();
return 0;
} else if (data[0] == PACKET_ID_SHARE_RELAYS) {
Node_format nodes[MAX_SHARED_RELAYS];
int n;
if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data + 1, length - 1, 1)) == -1)
return -1;
int j;
for (j = 0; j < n; j++) {
friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key);
}
return 0;
}
@ -745,6 +795,10 @@ void do_friend_connections(Friend_Connections *fr_c)
send_ping(fr_c, i);
}
if (friend_con->share_relays_lastsent + SHARE_RELAYS_INTERVAL < temp_time) {
send_relays(fr_c, i);
}
if (friend_con->ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
/* If we stopped receiving ping packets, kill it. */
crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id);

View File

@ -36,6 +36,7 @@
#define GROUPCHAT_CALLBACK_INDEX 1
#define PACKET_ID_ALIVE 16
#define PACKET_ID_SHARE_RELAYS 17
#define PACKET_ID_FRIEND_REQUESTS 18
/* Interval between the sending of ping packets. */
@ -49,6 +50,13 @@
#define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4)
/* Max number of tcp relays sent to friends */
#define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS)
/* Interval between the sending of tcp relay information */
#define SHARE_RELAYS_INTERVAL (5 * 60)
enum {
FRIENDCONN_STATUS_NONE,
FRIENDCONN_STATUS_CONNECTING,
@ -68,6 +76,7 @@ typedef struct {
int crypt_connection_id;
uint64_t ping_lastrecv, ping_lastsent;
uint64_t share_relays_lastsent;
struct {
int (*status_callback)(void *object, int id, uint8_t status);