diff options
-rw-r--r-- | toxcore/Messenger.c | 6 | ||||
-rw-r--r-- | toxcore/friend_connection.c | 61 | ||||
-rw-r--r-- | toxcore/friend_connection.h | 13 |
3 files changed, 72 insertions, 8 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index fefc3b17..bd44115d 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2185,10 +2185,10 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) | |||
2185 | if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1) | 2185 | if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1) |
2186 | break; | 2186 | break; |
2187 | 2187 | ||
2188 | int i; | 2188 | int j; |
2189 | 2189 | ||
2190 | for (i = 0; i < n; i++) { | 2190 | for (j = 0; j < n; j++) { |
2191 | add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].public_key); | 2191 | friend_add_tcp_relay(m->fr_c, m->friendlist[i].friendcon_id, nodes[j].ip_port, nodes[j].public_key); |
2192 | } | 2192 | } |
2193 | 2193 | ||
2194 | break; | 2194 | break; |
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 074021da..d38ad73a 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c | |||
@@ -146,6 +146,58 @@ int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk) | |||
146 | return -1; | 146 | return -1; |
147 | } | 147 | } |
148 | 148 | ||
149 | /* Add a TCP relay associated to the friend. | ||
150 | * | ||
151 | * return -1 on failure. | ||
152 | * return 0 on success. | ||
153 | */ | ||
154 | int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key) | ||
155 | { | ||
156 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | ||
157 | |||
158 | if (!friend_con) | ||
159 | return -1; | ||
160 | |||
161 | unsigned int i; | ||
162 | |||
163 | uint16_t index = friend_con->tcp_relay_counter; | ||
164 | |||
165 | for (i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) { | ||
166 | if (friend_con->tcp_relays[i].ip_port.ip.family != 0 | ||
167 | && memcmp(friend_con->tcp_relays[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) { | ||
168 | memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format)); | ||
169 | } | ||
170 | } | ||
171 | |||
172 | friend_con->tcp_relays[index % FRIEND_MAX_STORED_TCP_RELAYS].ip_port = ip_port; | ||
173 | memcpy(friend_con->tcp_relays[index % FRIEND_MAX_STORED_TCP_RELAYS].public_key, public_key, crypto_box_PUBLICKEYBYTES); | ||
174 | ++friend_con->tcp_relay_counter; | ||
175 | |||
176 | return add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, public_key); | ||
177 | } | ||
178 | |||
179 | /* Connect to number saved relays for friend. */ | ||
180 | static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_id, unsigned int number) | ||
181 | { | ||
182 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | ||
183 | |||
184 | if (!friend_con) | ||
185 | return; | ||
186 | |||
187 | unsigned int i; | ||
188 | |||
189 | for (i = 0; (i < FRIEND_MAX_STORED_TCP_RELAYS) && (number != 0); ++i) { | ||
190 | uint16_t index = (friend_con->tcp_relay_counter - (i + 1)) % FRIEND_MAX_STORED_TCP_RELAYS; | ||
191 | |||
192 | if (friend_con->tcp_relays[index].ip_port.ip.family) { | ||
193 | if (add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->tcp_relays[index].ip_port, | ||
194 | friend_con->tcp_relays[index].public_key) == 0) { | ||
195 | --number; | ||
196 | } | ||
197 | } | ||
198 | } | ||
199 | } | ||
200 | |||
149 | /* callback for recv TCP relay nodes. */ | 201 | /* callback for recv TCP relay nodes. */ |
150 | static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) | 202 | static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) |
151 | { | 203 | { |
@@ -156,7 +208,7 @@ static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_por | |||
156 | return -1; | 208 | return -1; |
157 | 209 | ||
158 | if (friend_con->crypt_connection_id != -1) { | 210 | if (friend_con->crypt_connection_id != -1) { |
159 | return add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, public_key); | 211 | return friend_add_tcp_relay(fr_c, number, ip_port, public_key); |
160 | } else { | 212 | } else { |
161 | return add_tcp_relay(fr_c->net_crypto, ip_port, public_key); | 213 | return add_tcp_relay(fr_c->net_crypto, ip_port, public_key); |
162 | } | 214 | } |
@@ -190,7 +242,7 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub | |||
190 | if (!friend_con) | 242 | if (!friend_con) |
191 | return; | 243 | return; |
192 | 244 | ||
193 | friend_con->dht_ping_lastrecv = unix_time(); | 245 | friend_con->dht_pk_lastrecv = unix_time(); |
194 | 246 | ||
195 | if (memcmp(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 247 | if (memcmp(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
196 | return; | 248 | return; |
@@ -234,7 +286,7 @@ static int handle_status(void *object, int number, uint8_t status) | |||
234 | } else { /* Went offline. */ | 286 | } else { /* Went offline. */ |
235 | if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) { | 287 | if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) { |
236 | call_cb = 1; | 288 | call_cb = 1; |
237 | friend_con->dht_ping_lastrecv = unix_time(); | 289 | friend_con->dht_pk_lastrecv = unix_time(); |
238 | onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); | 290 | onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); |
239 | } | 291 | } |
240 | 292 | ||
@@ -647,7 +699,7 @@ void do_friend_connections(Friend_Connections *fr_c) | |||
647 | 699 | ||
648 | if (friend_con) { | 700 | if (friend_con) { |
649 | if (friend_con->status == FRIENDCONN_STATUS_CONNECTING) { | 701 | if (friend_con->status == FRIENDCONN_STATUS_CONNECTING) { |
650 | if (friend_con->dht_ping_lastrecv + FRIEND_DHT_TIMEOUT < temp_time) { | 702 | if (friend_con->dht_pk_lastrecv + FRIEND_DHT_TIMEOUT < temp_time) { |
651 | if (friend_con->dht_lock) { | 703 | if (friend_con->dht_lock) { |
652 | DHT_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock); | 704 | DHT_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock); |
653 | friend_con->dht_lock = 0; | 705 | friend_con->dht_lock = 0; |
@@ -662,6 +714,7 @@ void do_friend_connections(Friend_Connections *fr_c) | |||
662 | if (friend_new_connection(fr_c, i) == 0) { | 714 | if (friend_new_connection(fr_c, i) == 0) { |
663 | set_connection_dht_public_key(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_temp_pk); | 715 | set_connection_dht_public_key(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_temp_pk); |
664 | set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port); | 716 | set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port); |
717 | connect_to_saved_tcp_relays(fr_c, i, (MAX_FRIEND_TCP_CONNECTIONS / 2)); /* Only fill it half up. */ | ||
665 | } | 718 | } |
666 | } | 719 | } |
667 | 720 | ||
diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index b814eb0c..641d2872 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h | |||
@@ -47,6 +47,7 @@ | |||
47 | /* Time before friend is removed from the DHT after last hearing about him. */ | 47 | /* Time before friend is removed from the DHT after last hearing about him. */ |
48 | #define FRIEND_DHT_TIMEOUT BAD_NODE_TIMEOUT | 48 | #define FRIEND_DHT_TIMEOUT BAD_NODE_TIMEOUT |
49 | 49 | ||
50 | #define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4) | ||
50 | 51 | ||
51 | enum { | 52 | enum { |
52 | FRIENDCONN_STATUS_NONE, | 53 | FRIENDCONN_STATUS_NONE, |
@@ -61,7 +62,7 @@ typedef struct { | |||
61 | uint8_t dht_temp_pk[crypto_box_PUBLICKEYBYTES]; | 62 | uint8_t dht_temp_pk[crypto_box_PUBLICKEYBYTES]; |
62 | uint16_t dht_lock; | 63 | uint16_t dht_lock; |
63 | IP_Port dht_ip_port; | 64 | IP_Port dht_ip_port; |
64 | uint64_t dht_ping_lastrecv, dht_ip_port_lastrecv; | 65 | uint64_t dht_pk_lastrecv, dht_ip_port_lastrecv; |
65 | 66 | ||
66 | int onion_friendnum; | 67 | int onion_friendnum; |
67 | int crypt_connection_id; | 68 | int crypt_connection_id; |
@@ -83,6 +84,9 @@ typedef struct { | |||
83 | } callbacks[MAX_FRIEND_CONNECTION_CALLBACKS]; | 84 | } callbacks[MAX_FRIEND_CONNECTION_CALLBACKS]; |
84 | 85 | ||
85 | uint16_t lock_count; | 86 | uint16_t lock_count; |
87 | |||
88 | Node_format tcp_relays[FRIEND_MAX_STORED_TCP_RELAYS]; | ||
89 | uint16_t tcp_relay_counter; | ||
86 | } Friend_Conn; | 90 | } Friend_Conn; |
87 | 91 | ||
88 | 92 | ||
@@ -127,6 +131,13 @@ int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Con | |||
127 | */ | 131 | */ |
128 | void set_dht_temp_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_temp_pk); | 132 | void set_dht_temp_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_temp_pk); |
129 | 133 | ||
134 | /* Add a TCP relay associated to the friend. | ||
135 | * | ||
136 | * return -1 on failure. | ||
137 | * return 0 on success. | ||
138 | */ | ||
139 | int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key); | ||
140 | |||
130 | /* Set the callbacks for the friend connection. | 141 | /* Set the callbacks for the friend connection. |
131 | * index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we want the callback to set in the array. | 142 | * index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we want the callback to set in the array. |
132 | * | 143 | * |