Use apidsl to generate LAN_discovery.h.

This commit is contained in:
iphydf 2018-01-15 00:48:35 +00:00
parent b3ec05543a
commit 0b13936ce8
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
12 changed files with 141 additions and 46 deletions

View File

@ -232,6 +232,7 @@ endif()
# LAYER 3: Distributed Hash Table # LAYER 3: Distributed Hash Table
# ------------------------------- # -------------------------------
apidsl(toxcore/LAN_discovery.api.h)
apidsl(toxcore/ping.api.h) apidsl(toxcore/ping.api.h)
apidsl(toxcore/ping_array.api.h) apidsl(toxcore/ping_array.api.h)
add_submodule(toxcore toxdht add_submodule(toxcore toxdht

View File

@ -181,7 +181,7 @@ int main(int argc, char *argv[])
int is_waiting_for_dht_connection = 1; int is_waiting_for_dht_connection = 1;
uint64_t last_LANdiscovery = 0; uint64_t last_LANdiscovery = 0;
LANdiscovery_init(dht); lan_discovery_init(dht);
while (1) { while (1) {
if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { if (is_waiting_for_dht_connection && DHT_isconnected(dht)) {
@ -192,7 +192,7 @@ int main(int argc, char *argv[])
do_DHT(dht); do_DHT(dht);
if (is_timeout(last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) { if (is_timeout(last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) {
send_LANdiscovery(net_htons(PORT), dht); lan_discovery_send(net_htons(PORT), dht);
last_LANdiscovery = unix_time(); last_LANdiscovery = unix_time();
} }

View File

@ -316,7 +316,7 @@ int main(int argc, char *argv[])
int waiting_for_dht_connection = 1; int waiting_for_dht_connection = 1;
if (enable_lan_discovery) { if (enable_lan_discovery) {
LANdiscovery_init(dht); lan_discovery_init(dht);
log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n"); log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n");
} }
@ -324,7 +324,7 @@ int main(int argc, char *argv[])
do_DHT(dht); do_DHT(dht);
if (enable_lan_discovery && is_timeout(last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) { if (enable_lan_discovery && is_timeout(last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) {
send_LANdiscovery(net_htons_port, dht); lan_discovery_send(net_htons_port, dht);
last_LANdiscovery = unix_time(); last_LANdiscovery = unix_time();
} }

View File

@ -565,7 +565,7 @@ static void update_client(Logger *log, int index, Client_data *client, IP_Port i
net_ntohs(ip_port.port)); net_ntohs(ip_port.port));
} }
if (LAN_ip(assoc->ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { if (ip_is_lan(assoc->ip_port.ip) != 0 && ip_is_lan(ip_port.ip) == 0) {
return; return;
} }
@ -702,11 +702,11 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_
} }
/* don't send LAN ips to non LAN peers */ /* don't send LAN ips to non LAN peers */
if (LAN_ip(ipptp->ip_port.ip) == 0 && !is_LAN) { if (ip_is_lan(ipptp->ip_port.ip) == 0 && !is_LAN) {
continue; continue;
} }
if (LAN_ip(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK if (ip_is_lan(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK
&& !id_equal(public_key, client->public_key)) { && !id_equal(public_key, client->public_key)) {
continue; continue;
} }
@ -1223,7 +1223,7 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
size_t Node_format_size = sizeof(Node_format); size_t Node_format_size = sizeof(Node_format);
Node_format nodes_list[MAX_SENT_NODES]; Node_format nodes_list[MAX_SENT_NODES];
uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, LAN_ip(ip_port.ip) == 0, 1); uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, ip_is_lan(ip_port.ip) == 0, 1);
VLA(uint8_t, plain, 1 + Node_format_size * MAX_SENT_NODES + length); VLA(uint8_t, plain, 1 + Node_format_size * MAX_SENT_NODES + length);
@ -2840,11 +2840,11 @@ int DHT_non_lan_connected(const DHT *dht)
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
const Client_data *client = &dht->close_clientlist[i]; const Client_data *client = &dht->close_clientlist[i];
if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc4.ip_port.ip) == -1) { if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && ip_is_lan(client->assoc4.ip_port.ip) == -1) {
return 1; return 1;
} }
if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc6.ip_port.ip) == -1) { if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && ip_is_lan(client->assoc6.ip_port.ip) == -1) {
return 1; return 1;
} }
} }

View File

@ -0,0 +1,73 @@
%{
/*
* LAN discovery implementation.
*/
/*
* Copyright © 2016-2017 The TokTok team.
* Copyright © 2013 Tox project.
*
* This file is part of Tox, the free peer to peer instant messenger.
*
* 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/>.
*/
#ifndef LAN_DISCOVERY_H
#define LAN_DISCOVERY_H
#include "DHT.h"
%}
class dHT { struct this; }
class iP { struct this; }
namespace lan_discovery {
/**
* Interval in seconds between LAN discovery packet sending.
*/
const INTERVAL = 10;
/**
* Send a LAN discovery pcaket to the broadcast address with port port.
*/
static int32_t send(uint16_t port, dHT::this *dht);
/**
* Sets up packet handlers.
*/
static void init(dHT::this *dht);
/**
* Clear packet handlers.
*/
static void kill(dHT::this *dht);
}
/**
* Is IP a local ip or not.
*/
static bool ip_is_local(iP::this ip);
/**
* checks if a given IP isn't routable
*
* return 0 if ip is a LAN ip.
* return -1 if it is not.
*/
static int32_t ip_is_lan(iP::this ip);
%{
#endif
%}

View File

@ -256,7 +256,7 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast)
} }
/* Is IP a local ip or not. */ /* Is IP a local ip or not. */
bool Local_ip(IP ip) bool ip_is_local(IP ip)
{ {
if (ip.family == TOX_AF_INET) { if (ip.family == TOX_AF_INET) {
IP4 ip4 = ip.ip4; IP4 ip4 = ip.ip4;
@ -271,7 +271,7 @@ bool Local_ip(IP ip)
IP ip4; IP ip4;
ip4.family = TOX_AF_INET; ip4.family = TOX_AF_INET;
ip4.ip4.uint32 = ip.ip6.uint32[3]; ip4.ip4.uint32 = ip.ip6.uint32[3];
return Local_ip(ip4); return ip_is_local(ip4);
} }
/* localhost in IPv6 (::1) */ /* localhost in IPv6 (::1) */
@ -286,9 +286,9 @@ bool Local_ip(IP ip)
/* return 0 if ip is a LAN ip. /* return 0 if ip is a LAN ip.
* return -1 if it is not. * return -1 if it is not.
*/ */
int LAN_ip(IP ip) int ip_is_lan(IP ip)
{ {
if (Local_ip(ip)) { if (ip_is_local(ip)) {
return 0; return 0;
} }
@ -335,7 +335,7 @@ int LAN_ip(IP ip)
IP ip4; IP ip4;
ip4.family = TOX_AF_INET; ip4.family = TOX_AF_INET;
ip4.ip4.uint32 = ip.ip6.uint32[3]; ip4.ip4.uint32 = ip.ip6.uint32[3];
return LAN_ip(ip4); return ip_is_lan(ip4);
} }
} }
@ -346,7 +346,7 @@ static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *pack
{ {
DHT *dht = (DHT *)object; DHT *dht = (DHT *)object;
if (LAN_ip(source.ip) == -1) { if (ip_is_lan(source.ip) == -1) {
return 1; return 1;
} }
@ -363,7 +363,7 @@ static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *pack
} }
int send_LANdiscovery(uint16_t port, DHT *dht) int lan_discovery_send(uint16_t port, DHT *dht)
{ {
uint8_t data[CRYPTO_PUBLIC_KEY_SIZE + 1]; uint8_t data[CRYPTO_PUBLIC_KEY_SIZE + 1];
data[0] = NET_PACKET_LAN_DISCOVERY; data[0] = NET_PACKET_LAN_DISCOVERY;
@ -399,12 +399,12 @@ int send_LANdiscovery(uint16_t port, DHT *dht)
} }
void LANdiscovery_init(DHT *dht) void lan_discovery_init(DHT *dht)
{ {
networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht);
} }
void LANdiscovery_kill(DHT *dht) void lan_discovery_kill(DHT *dht)
{ {
networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, NULL, NULL); networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, NULL, NULL);
} }

View File

@ -26,27 +26,49 @@
#include "DHT.h" #include "DHT.h"
/* Interval in seconds between LAN discovery packet sending. */ #ifndef DHT_DEFINED
#define LAN_DISCOVERY_INTERVAL 10 #define DHT_DEFINED
typedef struct DHT DHT;
#endif /* DHT_DEFINED */
/* Send a LAN discovery pcaket to the broadcast address with port port. */ #ifndef IP_DEFINED
int send_LANdiscovery(uint16_t port, DHT *dht); #define IP_DEFINED
typedef struct IP IP;
#endif /* IP_DEFINED */
/* Sets up packet handlers. */ /**
void LANdiscovery_init(DHT *dht); * Interval in seconds between LAN discovery packet sending.
*/
#define LAN_DISCOVERY_INTERVAL 10
/* Clear packet handlers. */ uint32_t lan_discovery_interval(void);
void LANdiscovery_kill(DHT *dht);
/* Is IP a local ip or not. */ /**
bool Local_ip(IP ip); * Send a LAN discovery pcaket to the broadcast address with port port.
*/
int32_t lan_discovery_send(uint16_t port, DHT *dht);
/* checks if a given IP isn't routable /**
* Sets up packet handlers.
*/
void lan_discovery_init(DHT *dht);
/**
* Clear packet handlers.
*/
void lan_discovery_kill(DHT *dht);
/**
* Is IP a local ip or not.
*/
bool ip_is_local(IP ip);
/**
* checks if a given IP isn't routable
* *
* return 0 if ip is a LAN ip. * return 0 if ip is a LAN ip.
* return -1 if it is not. * return -1 if it is not.
*/ */
int LAN_ip(IP ip); int32_t ip_is_lan(IP ip);
#endif #endif

View File

@ -229,7 +229,7 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_
} }
/* Local ip and same pk means that they are hosting a TCP relay. */ /* Local ip and same pk means that they are hosting a TCP relay. */
if (Local_ip(ip_port.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) { if (ip_is_local(ip_port.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) {
if (friend_con->dht_ip_port.ip.family != 0) { if (friend_con->dht_ip_port.ip.family != 0) {
ip_port.ip = friend_con->dht_ip_port.ip; ip_port.ip = friend_con->dht_ip_port.ip;
} else { } else {
@ -892,7 +892,7 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis
new_connection_handler(temp->net_crypto, &handle_new_connections, temp); new_connection_handler(temp->net_crypto, &handle_new_connections, temp);
if (temp->local_discovery_enabled) { if (temp->local_discovery_enabled) {
LANdiscovery_init(temp->dht); lan_discovery_init(temp->dht);
} }
return temp; return temp;
@ -907,11 +907,11 @@ static void LANdiscovery(Friend_Connections *fr_c)
last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last; last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last;
// Always send to default port // Always send to default port
send_LANdiscovery(net_htons(TOX_PORT_DEFAULT), fr_c->dht); lan_discovery_send(net_htons(TOX_PORT_DEFAULT), fr_c->dht);
// And check some extra ports // And check some extra ports
for (uint16_t port = first; port < last; port++) { for (uint16_t port = first; port < last; port++) {
send_LANdiscovery(net_htons(port), fr_c->dht); lan_discovery_send(net_htons(port), fr_c->dht);
} }
// Don't include default port in port range // Don't include default port in port range
@ -987,7 +987,7 @@ void kill_friend_connections(Friend_Connections *fr_c)
} }
if (fr_c->local_discovery_enabled) { if (fr_c->local_discovery_enabled) {
LANdiscovery_kill(fr_c->dht); lan_discovery_kill(fr_c->dht);
} }
free(fr_c); free(fr_c);

View File

@ -432,7 +432,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por
} }
if (ip_port.ip.family == TOX_AF_INET) { if (ip_port.ip.family == TOX_AF_INET) {
if (!ipport_equal(&ip_port, &conn->ip_portv4) && LAN_ip(conn->ip_portv4.ip) != 0) { if (!ipport_equal(&ip_port, &conn->ip_portv4) && ip_is_lan(conn->ip_portv4.ip) != 0) {
if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) { if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) {
return -1; return -1;
} }
@ -482,7 +482,7 @@ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id)
v6 = 1; v6 = 1;
} }
if (v4 && LAN_ip(conn->ip_portv4.ip) == 0) { if (v4 && ip_is_lan(conn->ip_portv4.ip) == 0) {
return conn->ip_portv4; return conn->ip_portv4;
} }

View File

@ -140,20 +140,19 @@ typedef union {
uint16_t uint16[8]; uint16_t uint16[8];
uint32_t uint32[4]; uint32_t uint32[4];
uint64_t uint64[2]; uint64_t uint64[2];
} } IP6;
IP6;
IP6 get_ip6_loopback(void); IP6 get_ip6_loopback(void);
extern const IP6 IP6_BROADCAST; extern const IP6 IP6_BROADCAST;
#define IP_DEFINED
typedef struct { typedef struct {
uint8_t family; uint8_t family;
GNU_EXTENSION union { GNU_EXTENSION union {
IP4 ip4; IP4 ip4;
IP6 ip6; IP6 ip6;
}; };
} } IP;
IP;
#define IP_PORT_DEFINED #define IP_PORT_DEFINED
typedef struct IP_Port { typedef struct IP_Port {

View File

@ -405,7 +405,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t *
/*Respond with a announce response packet*/ /*Respond with a announce response packet*/
Node_format nodes_list[MAX_SENT_NODES]; Node_format nodes_list[MAX_SENT_NODES];
unsigned int num_nodes = get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, 0, unsigned int num_nodes = get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, 0,
LAN_ip(source.ip) == 0, 1); ip_is_lan(source.ip) == 0, 1);
uint8_t nonce[CRYPTO_NONCE_SIZE]; uint8_t nonce[CRYPTO_NONCE_SIZE];
random_nonce(nonce); random_nonce(nonce);

View File

@ -793,12 +793,12 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for
} }
unsigned int i, j; unsigned int i, j;
int lan_ips_accepted = (LAN_ip(source.ip) == 0); int lan_ips_accepted = (ip_is_lan(source.ip) == 0);
for (i = 0; i < num_nodes; ++i) { for (i = 0; i < num_nodes; ++i) {
if (!lan_ips_accepted) { if (!lan_ips_accepted) {
if (LAN_ip(nodes[i].ip_port.ip) == 0) { if (ip_is_lan(nodes[i].ip_port.ip) == 0) {
continue; continue;
} }
} }