diff options
Diffstat (limited to 'toxcore/friend_connection.c')
-rw-r--r-- | toxcore/friend_connection.c | 61 |
1 files changed, 57 insertions, 4 deletions
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 | ||