2013-08-06 03:51:58 +08:00
|
|
|
/*
|
|
|
|
* ping.c -- Buffered pinging using cyclic arrays.
|
|
|
|
*
|
|
|
|
* This file is donated to the Tox Project.
|
|
|
|
* Copyright 2013 plutooo
|
2013-10-25 01:34:04 +08:00
|
|
|
*
|
2013-10-24 02:32:09 +08:00
|
|
|
* 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/>.
|
2013-10-25 01:34:04 +08:00
|
|
|
*
|
2013-08-06 03:51:58 +08:00
|
|
|
*/
|
|
|
|
|
2013-09-05 18:02:26 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-11-15 02:05:36 +08:00
|
|
|
#include "ping.h"
|
|
|
|
|
2016-09-11 22:47:51 +08:00
|
|
|
#include "DHT.h"
|
2013-11-15 02:05:36 +08:00
|
|
|
#include "network.h"
|
2014-05-13 02:07:03 +08:00
|
|
|
#include "ping_array.h"
|
2016-09-01 07:33:20 +08:00
|
|
|
#include "util.h"
|
2013-08-06 03:51:58 +08:00
|
|
|
|
2016-09-11 22:47:51 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-01-26 10:09:26 +08:00
|
|
|
#define PING_NUM_MAX 512
|
2013-08-06 03:51:58 +08:00
|
|
|
|
2014-03-17 01:57:21 +08:00
|
|
|
/* Maximum newly announced nodes to ping per TIME_TO_PING seconds. */
|
2016-01-05 09:14:57 +08:00
|
|
|
#define MAX_TO_PING 32
|
2014-03-17 01:57:21 +08:00
|
|
|
|
|
|
|
/* Ping newly announced nodes to ping per TIME_TO_PING seconds*/
|
2016-01-05 09:14:57 +08:00
|
|
|
#define TIME_TO_PING 2
|
2013-08-06 03:51:58 +08:00
|
|
|
|
2013-10-25 06:59:00 +08:00
|
|
|
|
2013-12-02 04:06:20 +08:00
|
|
|
struct PING {
|
2014-01-18 02:35:40 +08:00
|
|
|
DHT *dht;
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2014-05-13 02:07:03 +08:00
|
|
|
Ping_Array ping_array;
|
2014-03-17 01:57:21 +08:00
|
|
|
Node_format to_ping[MAX_TO_PING];
|
|
|
|
uint64_t last_to_ping;
|
2013-12-02 04:06:20 +08:00
|
|
|
};
|
2013-08-06 03:51:58 +08:00
|
|
|
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2014-05-20 00:56:36 +08:00
|
|
|
#define PING_PLAIN_SIZE (1 + sizeof(uint64_t))
|
2015-08-08 08:53:53 +08:00
|
|
|
#define DHT_PING_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + PING_PLAIN_SIZE + crypto_box_MACBYTES)
|
|
|
|
#define PING_DATA_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(IP_Port))
|
2013-08-30 07:06:09 +08:00
|
|
|
|
2015-07-30 10:39:56 +08:00
|
|
|
int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
|
2013-08-06 06:04:38 +08:00
|
|
|
{
|
2013-08-30 07:06:09 +08:00
|
|
|
uint8_t pk[DHT_PING_SIZE];
|
2013-08-06 06:04:38 +08:00
|
|
|
int rc;
|
|
|
|
uint64_t ping_id;
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (id_equal(public_key, ping->dht->self_public_key)) {
|
2013-08-06 06:04:38 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2014-03-03 08:46:47 +08:00
|
|
|
uint8_t shared_key[crypto_box_BEFORENMBYTES];
|
|
|
|
|
|
|
|
// generate key to encrypt ping_id with recipient privkey
|
2015-07-30 10:39:56 +08:00
|
|
|
DHT_get_shared_key_sent(ping->dht, shared_key, public_key);
|
2013-08-30 05:17:51 +08:00
|
|
|
// Generate random ping_id.
|
2014-05-13 02:07:03 +08:00
|
|
|
uint8_t data[PING_DATA_SIZE];
|
2015-07-30 10:39:56 +08:00
|
|
|
id_copy(data, public_key);
|
2015-08-08 08:53:53 +08:00
|
|
|
memcpy(data + crypto_box_PUBLICKEYBYTES, &ipp, sizeof(IP_Port));
|
2014-05-13 02:07:03 +08:00
|
|
|
ping_id = ping_array_add(&ping->ping_array, data, sizeof(data));
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ping_id == 0) {
|
2014-05-13 02:07:03 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2014-05-20 00:56:36 +08:00
|
|
|
uint8_t ping_plain[PING_PLAIN_SIZE];
|
|
|
|
ping_plain[0] = NET_PACKET_PING_REQUEST;
|
|
|
|
memcpy(ping_plain + 1, &ping_id, sizeof(ping_id));
|
|
|
|
|
2013-08-30 07:06:09 +08:00
|
|
|
pk[0] = NET_PACKET_PING_REQUEST;
|
2014-01-18 02:35:40 +08:00
|
|
|
id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey
|
2016-11-06 23:14:02 +08:00
|
|
|
random_nonce(pk + 1 + crypto_box_PUBLICKEYBYTES); // Generate new nonce
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2014-03-03 08:46:47 +08:00
|
|
|
|
2014-04-22 04:51:36 +08:00
|
|
|
rc = encrypt_data_symmetric(shared_key,
|
2015-08-08 08:53:53 +08:00
|
|
|
pk + 1 + crypto_box_PUBLICKEYBYTES,
|
2014-05-20 00:56:36 +08:00
|
|
|
ping_plain, sizeof(ping_plain),
|
2015-08-08 08:53:53 +08:00
|
|
|
pk + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES) {
|
2013-08-06 06:04:38 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
return sendpacket(ping->dht->net, ipp, pk, sizeof(pk));
|
2013-08-06 06:04:38 +08:00
|
|
|
}
|
|
|
|
|
2015-07-30 10:39:56 +08:00
|
|
|
static int send_ping_response(PING *ping, IP_Port ipp, const uint8_t *public_key, uint64_t ping_id,
|
2014-03-02 10:18:53 +08:00
|
|
|
uint8_t *shared_encryption_key)
|
2013-08-06 06:04:38 +08:00
|
|
|
{
|
2013-08-30 07:06:09 +08:00
|
|
|
uint8_t pk[DHT_PING_SIZE];
|
2013-08-06 06:04:38 +08:00
|
|
|
int rc;
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (id_equal(public_key, ping->dht->self_public_key)) {
|
2013-08-06 06:04:38 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2014-05-20 00:56:36 +08:00
|
|
|
uint8_t ping_plain[PING_PLAIN_SIZE];
|
|
|
|
ping_plain[0] = NET_PACKET_PING_RESPONSE;
|
|
|
|
memcpy(ping_plain + 1, &ping_id, sizeof(ping_id));
|
|
|
|
|
2013-08-30 07:06:09 +08:00
|
|
|
pk[0] = NET_PACKET_PING_RESPONSE;
|
2014-01-18 02:35:40 +08:00
|
|
|
id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey
|
2016-11-06 23:14:02 +08:00
|
|
|
random_nonce(pk + 1 + crypto_box_PUBLICKEYBYTES); // Generate new nonce
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2013-08-30 07:06:09 +08:00
|
|
|
// Encrypt ping_id using recipient privkey
|
2014-04-22 04:51:36 +08:00
|
|
|
rc = encrypt_data_symmetric(shared_encryption_key,
|
2015-08-08 08:53:53 +08:00
|
|
|
pk + 1 + crypto_box_PUBLICKEYBYTES,
|
2014-05-20 00:56:36 +08:00
|
|
|
ping_plain, sizeof(ping_plain),
|
2016-09-13 04:37:58 +08:00
|
|
|
pk + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES) {
|
2013-08-06 06:04:38 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-06 06:04:38 +08:00
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
return sendpacket(ping->dht->net, ipp, pk, sizeof(pk));
|
2013-08-06 03:51:58 +08:00
|
|
|
}
|
2013-08-07 04:09:14 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
static int handle_ping_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
|
2013-08-07 04:09:14 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
DHT *dht = (DHT *)object;
|
2013-08-07 04:09:14 +08:00
|
|
|
int rc;
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (length != DHT_PING_SIZE) {
|
2013-08-30 07:06:09 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-30 07:06:09 +08:00
|
|
|
|
2013-09-21 21:39:15 +08:00
|
|
|
PING *ping = dht->ping;
|
2013-09-25 22:07:07 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (id_equal(packet + 1, ping->dht->self_public_key)) {
|
2013-08-07 04:09:14 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-07 04:09:14 +08:00
|
|
|
|
2014-03-02 10:18:53 +08:00
|
|
|
uint8_t shared_key[crypto_box_BEFORENMBYTES];
|
|
|
|
|
2014-05-20 00:56:36 +08:00
|
|
|
uint8_t ping_plain[PING_PLAIN_SIZE];
|
2013-08-30 07:06:09 +08:00
|
|
|
// Decrypt ping_id
|
2014-03-06 05:54:17 +08:00
|
|
|
DHT_get_shared_key_recv(dht, shared_key, packet + 1);
|
2014-04-22 04:51:36 +08:00
|
|
|
rc = decrypt_data_symmetric(shared_key,
|
2015-08-08 08:53:53 +08:00
|
|
|
packet + 1 + crypto_box_PUBLICKEYBYTES,
|
|
|
|
packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
|
2014-05-20 00:56:36 +08:00
|
|
|
PING_PLAIN_SIZE + crypto_box_MACBYTES,
|
2016-09-13 04:37:58 +08:00
|
|
|
ping_plain);
|
2014-05-20 00:56:36 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (rc != sizeof(ping_plain)) {
|
2014-05-20 00:56:36 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-07 04:09:14 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ping_plain[0] != NET_PACKET_PING_REQUEST) {
|
2013-08-07 04:09:14 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-07 04:09:14 +08:00
|
|
|
|
2014-05-20 00:56:36 +08:00
|
|
|
uint64_t ping_id;
|
|
|
|
memcpy(&ping_id, ping_plain + 1, sizeof(ping_id));
|
2013-08-30 07:06:09 +08:00
|
|
|
// Send response
|
2014-03-02 10:18:53 +08:00
|
|
|
send_ping_response(ping, source, packet + 1, ping_id, shared_key);
|
2014-03-17 01:57:21 +08:00
|
|
|
add_to_ping(ping, packet + 1, source);
|
2013-08-07 04:09:14 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
static int handle_ping_response(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
|
2013-08-07 04:09:14 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
DHT *dht = (DHT *)object;
|
2013-08-07 04:09:14 +08:00
|
|
|
int rc;
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (length != DHT_PING_SIZE) {
|
2013-08-30 07:06:09 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-30 07:06:09 +08:00
|
|
|
|
2013-09-21 21:39:15 +08:00
|
|
|
PING *ping = dht->ping;
|
2013-09-25 22:07:07 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (id_equal(packet + 1, ping->dht->self_public_key)) {
|
2013-08-07 04:09:14 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-07 04:09:14 +08:00
|
|
|
|
2014-05-13 02:07:03 +08:00
|
|
|
uint8_t shared_key[crypto_box_BEFORENMBYTES];
|
2014-03-03 08:46:47 +08:00
|
|
|
|
2014-05-13 02:07:03 +08:00
|
|
|
// generate key to encrypt ping_id with recipient privkey
|
|
|
|
DHT_get_shared_key_sent(ping->dht, shared_key, packet + 1);
|
2014-03-03 08:46:47 +08:00
|
|
|
|
2014-05-20 00:56:36 +08:00
|
|
|
uint8_t ping_plain[PING_PLAIN_SIZE];
|
2013-08-30 07:06:09 +08:00
|
|
|
// Decrypt ping_id
|
2014-05-13 02:07:03 +08:00
|
|
|
rc = decrypt_data_symmetric(shared_key,
|
2015-08-08 08:53:53 +08:00
|
|
|
packet + 1 + crypto_box_PUBLICKEYBYTES,
|
|
|
|
packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
|
2014-05-20 00:56:36 +08:00
|
|
|
PING_PLAIN_SIZE + crypto_box_MACBYTES,
|
|
|
|
ping_plain);
|
2013-08-07 04:09:14 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (rc != sizeof(ping_plain)) {
|
2013-08-07 04:09:14 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-07 04:09:14 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ping_plain[0] != NET_PACKET_PING_RESPONSE) {
|
2014-05-20 00:56:36 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-20 00:56:36 +08:00
|
|
|
|
|
|
|
uint64_t ping_id;
|
|
|
|
memcpy(&ping_id, ping_plain + 1, sizeof(ping_id));
|
2014-05-13 02:07:03 +08:00
|
|
|
uint8_t data[PING_DATA_SIZE];
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ping_array_check(data, sizeof(data), &ping->ping_array, ping_id) != sizeof(data)) {
|
2013-08-07 04:09:14 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-08-07 04:09:14 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!id_equal(packet + 1, data)) {
|
2014-05-13 02:07:03 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-13 02:07:03 +08:00
|
|
|
|
|
|
|
IP_Port ipp;
|
2015-08-08 08:53:53 +08:00
|
|
|
memcpy(&ipp, data + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port));
|
2013-11-15 02:05:36 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!ipport_equal(&ipp, &source)) {
|
2014-05-13 02:07:03 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-13 02:07:03 +08:00
|
|
|
|
|
|
|
addto_lists(dht, source, packet + 1);
|
2013-08-07 04:09:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2015-07-30 10:39:56 +08:00
|
|
|
/* Check if public_key with ip_port is in the list.
|
2014-05-14 00:14:09 +08:00
|
|
|
*
|
|
|
|
* return 1 if it is.
|
|
|
|
* return 0 if it isn't.
|
|
|
|
*/
|
2015-07-30 10:39:56 +08:00
|
|
|
static int in_list(const Client_data *list, uint16_t length, const uint8_t *public_key, IP_Port ip_port)
|
2014-05-14 00:14:09 +08:00
|
|
|
{
|
2015-12-09 08:09:38 +08:00
|
|
|
unsigned int i;
|
2014-05-14 00:14:09 +08:00
|
|
|
|
|
|
|
for (i = 0; i < length; ++i) {
|
2015-07-30 10:39:56 +08:00
|
|
|
if (id_equal(list[i].public_key, public_key)) {
|
2014-06-11 02:54:48 +08:00
|
|
|
const IPPTsPng *ipptp;
|
2014-05-14 00:14:09 +08:00
|
|
|
|
|
|
|
if (ip_port.ip.family == AF_INET) {
|
|
|
|
ipptp = &list[i].assoc4;
|
|
|
|
} else {
|
|
|
|
ipptp = &list[i].assoc6;
|
|
|
|
}
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!is_timeout(ipptp->timestamp, BAD_NODE_TIMEOUT) && ipport_equal(&ipptp->ip_port, &ip_port)) {
|
2014-05-14 00:14:09 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-14 00:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2014-03-17 01:57:21 +08:00
|
|
|
/* Add nodes to the to_ping list.
|
|
|
|
* All nodes in this list are pinged every TIME_TO_PING seconds
|
2013-09-21 21:39:15 +08:00
|
|
|
* and are then removed from the list.
|
2015-07-30 10:39:56 +08:00
|
|
|
* If the list is full the nodes farthest from our public_key are replaced.
|
2013-09-21 21:39:15 +08:00
|
|
|
* The purpose of this list is to enable quick integration of new nodes into the
|
|
|
|
* network while preventing amplification attacks.
|
|
|
|
*
|
|
|
|
* return 0 if node was added.
|
|
|
|
* return -1 if node was not added.
|
|
|
|
*/
|
2015-07-30 10:39:56 +08:00
|
|
|
int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
|
2013-09-21 21:39:15 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!ip_isset(&ip_port.ip)) {
|
2013-09-21 21:39:15 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!node_addable_to_close_list(ping->dht, public_key, ip_port)) {
|
2014-05-14 00:14:09 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-05-14 00:14:09 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (in_list(ping->dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) {
|
2016-01-05 09:14:57 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2015-12-09 04:43:03 +08:00
|
|
|
IP_Port temp;
|
|
|
|
|
|
|
|
if (DHT_getfriendip(ping->dht, public_key, &temp) == 0) {
|
2015-12-09 07:37:02 +08:00
|
|
|
send_ping_request(ping, ip_port, public_key);
|
2015-12-09 04:43:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-09 08:09:38 +08:00
|
|
|
unsigned int i;
|
|
|
|
|
2014-03-17 01:57:21 +08:00
|
|
|
for (i = 0; i < MAX_TO_PING; ++i) {
|
|
|
|
if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
|
2015-08-08 08:53:53 +08:00
|
|
|
memcpy(ping->to_ping[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
|
2014-03-17 01:57:21 +08:00
|
|
|
ipport_copy(&ping->to_ping[i].ip_port, &ip_port);
|
2013-09-21 21:39:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-17 01:53:51 +08:00
|
|
|
|
2016-01-25 00:16:40 +08:00
|
|
|
if (public_key_cmp(ping->to_ping[i].public_key, public_key) == 0) {
|
2014-04-17 01:53:51 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
}
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (add_to_list(ping->to_ping, MAX_TO_PING, public_key, ip_port, ping->dht->self_public_key)) {
|
2015-12-17 00:28:30 +08:00
|
|
|
return 0;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-17 01:57:21 +08:00
|
|
|
/* Ping all the valid nodes in the to_ping list every TIME_TO_PING seconds.
|
|
|
|
* This function must be run at least once every TIME_TO_PING seconds.
|
2013-09-21 21:39:15 +08:00
|
|
|
*/
|
2014-03-17 01:57:21 +08:00
|
|
|
void do_to_ping(PING *ping)
|
2013-09-21 21:39:15 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!is_timeout(ping->last_to_ping, TIME_TO_PING)) {
|
2013-09-21 21:39:15 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!ip_isset(&ping->to_ping[0].ip_port.ip)) {
|
2014-09-01 07:12:47 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-09-01 07:12:47 +08:00
|
|
|
|
2015-12-09 08:09:38 +08:00
|
|
|
unsigned int i;
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2014-03-17 01:57:21 +08:00
|
|
|
for (i = 0; i < MAX_TO_PING; ++i) {
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
|
2015-05-08 08:58:39 +08:00
|
|
|
break;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!node_addable_to_close_list(ping->dht, ping->to_ping[i].public_key, ping->to_ping[i].ip_port)) {
|
2016-01-05 09:14:57 +08:00
|
|
|
continue;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2016-01-05 09:14:57 +08:00
|
|
|
|
2015-01-30 08:38:44 +08:00
|
|
|
send_ping_request(ping, ping->to_ping[i].ip_port, ping->to_ping[i].public_key);
|
2014-03-17 01:57:21 +08:00
|
|
|
ip_reset(&ping->to_ping[i].ip_port.ip);
|
2013-09-21 21:39:15 +08:00
|
|
|
}
|
2015-05-08 08:58:39 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (i != 0) {
|
2015-05-08 08:58:39 +08:00
|
|
|
ping->last_to_ping = unix_time();
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
PING *new_ping(DHT *dht)
|
2013-09-21 21:39:15 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
PING *ping = (PING *)calloc(1, sizeof(PING));
|
2013-09-25 22:07:07 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ping == NULL) {
|
2013-09-21 21:39:15 +08:00
|
|
|
return NULL;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-21 21:39:15 +08:00
|
|
|
|
2014-05-13 02:07:03 +08:00
|
|
|
if (ping_array_init(&ping->ping_array, PING_NUM_MAX, PING_TIMEOUT) != 0) {
|
|
|
|
free(ping);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
ping->dht = dht;
|
|
|
|
networking_registerhandler(ping->dht->net, NET_PACKET_PING_REQUEST, &handle_ping_request, dht);
|
|
|
|
networking_registerhandler(ping->dht->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, dht);
|
2013-09-21 21:39:15 +08:00
|
|
|
|
|
|
|
return ping;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kill_ping(PING *ping)
|
|
|
|
{
|
2014-01-18 02:35:40 +08:00
|
|
|
networking_registerhandler(ping->dht->net, NET_PACKET_PING_REQUEST, NULL, NULL);
|
|
|
|
networking_registerhandler(ping->dht->net, NET_PACKET_PING_RESPONSE, NULL, NULL);
|
2014-05-13 02:07:03 +08:00
|
|
|
ping_array_free_all(&ping->ping_array);
|
2013-09-21 21:39:15 +08:00
|
|
|
|
|
|
|
free(ping);
|
|
|
|
}
|