merge DHT.c/h and network.c changes from new groupchats fork

This commit is contained in:
jfreegman
2021-12-14 13:54:52 -05:00
parent f45807a0f2
commit 570dcdb807
4 changed files with 85 additions and 4 deletions
+56 -3
View File
@@ -1002,6 +1002,7 @@ static void update_client_with_reset(const Mono_Time *mono_time, Client_data *cl
ip_reset(&ipptp_write->ret_ip_port.ip);
ipptp_write->ret_ip_port.port = 0;
ipptp_write->ret_timestamp = 0;
ipptp_write->ret_ip_self = false;
/* zero out other address */
memset(ipptp_clear, 0, sizeof(*ipptp_clear));
@@ -1244,7 +1245,7 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
}
static bool update_client_data(const Mono_Time *mono_time, Client_data *array, size_t size, IP_Port ip_port,
const uint8_t *pk)
const uint8_t *pk, bool node_is_self)
{
const uint64_t temp_time = mono_time_get(mono_time);
const uint32_t index = index_of_client_pk(array, size, pk);
@@ -1266,6 +1267,8 @@ static bool update_client_data(const Mono_Time *mono_time, Client_data *array, s
assoc->ret_ip_port = ip_port;
assoc->ret_timestamp = temp_time;
assoc->ret_ip_self = node_is_self;
return true;
}
@@ -1281,7 +1284,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_ke
}
if (id_equal(public_key, dht->self_public_key)) {
update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, ip_port, nodepublic_key);
update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, ip_port, nodepublic_key, true);
return;
}
@@ -1289,7 +1292,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_ke
if (id_equal(public_key, dht->friends_list[i].public_key)) {
Client_data *const client_list = dht->friends_list[i].client_list;
if (update_client_data(dht->mono_time, client_list, MAX_FRIEND_CLIENTS, ip_port, nodepublic_key)) {
if (update_client_data(dht->mono_time, client_list, MAX_FRIEND_CLIENTS, ip_port, nodepublic_key, false)) {
return;
}
}
@@ -3031,3 +3034,53 @@ bool dht_non_lan_connected(const DHT *dht)
return false;
}
/* Copies our own ip_port structure to `dest`. WAN addresses take priority over LAN addresses.
*
* This function will zero the `dest` buffer before use.
*
* Return 0 if our ip port can't be found (this usually means we're not connected to the DHT).
* Return 1 if IP is a WAN address.
* Return 2 if IP is a LAN address.
*/
unsigned int ipport_self_copy(const DHT *dht, IP_Port *dest)
{
ipport_reset(dest);
bool is_lan = false;
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
const Client_data *client = dht_get_close_client(dht, i);
const IP_Port *ip_port4 = &client->assoc4.ret_ip_port;
if (client->assoc4.ret_ip_self && ipport_isset(ip_port4)) {
ipport_copy(dest, ip_port4);
is_lan = ip_is_lan(dest->ip);
if (!is_lan) {
break;
}
}
const IP_Port *ip_port6 = &client->assoc6.ret_ip_port;
if (client->assoc6.ret_ip_self && ipport_isset(ip_port6)) {
ipport_copy(dest, ip_port6);
is_lan = ip_is_lan(dest->ip);
if (!is_lan) {
break;
}
}
}
if (!ipport_isset(dest)) {
return 0;
}
if (is_lan) {
return 2;
}
return 1;
}
+17 -1
View File
@@ -108,9 +108,11 @@ typedef struct IPPTsPng {
uint64_t last_pinged;
Hardening hardening;
/* Returned by this node. Either our friend or us. */
/* Returned by this node */
IP_Port ret_ip_port;
uint64_t ret_timestamp;
/* true if this ip_port is ours */
bool ret_ip_self;
} IPPTsPng;
typedef struct Client_data {
@@ -152,6 +154,8 @@ const Client_data *dht_friend_client(const DHT_Friend *dht_friend, size_t index)
int packed_node_size(Family ip_family);
/* Packs an IP_Port structure into data of max size length.
*
* Packed_length is the offset of data currently packed.
*
* Returns size of packed IP_Port data on success
* Return -1 on failure.
@@ -159,6 +163,8 @@ int packed_node_size(Family ip_family);
int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port);
/* Unpack IP_Port structure from data of max size length into ip_port.
*
* len_processed is the offset of data currently unpacked.
*
* Return size of unpacked ip_port on success.
* Return -1 on failure.
@@ -409,6 +415,16 @@ bool dht_non_lan_connected(const DHT *dht);
uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key);
/* Copies our own ip_port structure to `dest`. WAN addresses take priority over LAN addresses.
*
* This function will zero the `dest` buffer before use.
*
* Return 0 if our ip port can't be found (this usually means we're not connected to the DHT).
* Return 1 if IP is a WAN address.
* Return 2 if IP is a LAN address.
*/
unsigned int ipport_self_copy(const DHT *dht, IP_Port *dest);
#ifdef __cplusplus
} // extern "C"
#endif
+10
View File
@@ -1071,6 +1071,16 @@ void ip_reset(IP *ip)
memset(ip, 0, sizeof(IP));
}
/* nulls out ip_port */
void ipport_reset(IP_Port *ipport)
{
if (!ipport) {
return;
}
memset(ipport, 0, sizeof(IP_Port));
}
/* nulls out ip, sets family according to flag */
void ip_init(IP *ip, bool ipv6enabled)
{
+2
View File
@@ -266,6 +266,8 @@ bool ipport_equal(const IP_Port *a, const IP_Port *b);
/* nulls out ip */
void ip_reset(IP *ip);
/* nulls out ip_port */
void ipport_reset(IP_Port *ipport);
/* nulls out ip, sets family according to flag */
void ip_init(IP *ip, bool ipv6enabled);
/* checks if ip is valid */