From 0b13936ce8498735e245531c222c035f41fd5e0f Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 15 Jan 2018 00:48:35 +0000 Subject: [PATCH] Use apidsl to generate LAN_discovery.h. --- CMakeLists.txt | 1 + other/DHT_bootstrap.c | 4 +- other/bootstrap_daemon/src/tox-bootstrapd.c | 4 +- toxcore/DHT.c | 12 ++-- toxcore/LAN_discovery.api.h | 73 +++++++++++++++++++++ toxcore/LAN_discovery.c | 18 ++--- toxcore/LAN_discovery.h | 48 ++++++++++---- toxcore/friend_connection.c | 10 +-- toxcore/net_crypto.c | 4 +- toxcore/network.h | 7 +- toxcore/onion_announce.c | 2 +- toxcore/onion_client.c | 4 +- 12 files changed, 141 insertions(+), 46 deletions(-) create mode 100644 toxcore/LAN_discovery.api.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cca2f79e..2dd63713 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -232,6 +232,7 @@ endif() # LAYER 3: Distributed Hash Table # ------------------------------- +apidsl(toxcore/LAN_discovery.api.h) apidsl(toxcore/ping.api.h) apidsl(toxcore/ping_array.api.h) add_submodule(toxcore toxdht diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 0e95805d..5eae46b9 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -181,7 +181,7 @@ int main(int argc, char *argv[]) int is_waiting_for_dht_connection = 1; uint64_t last_LANdiscovery = 0; - LANdiscovery_init(dht); + lan_discovery_init(dht); while (1) { if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { @@ -192,7 +192,7 @@ int main(int argc, char *argv[]) do_DHT(dht); 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(); } diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index c3af4a10..c6780fa3 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -316,7 +316,7 @@ int main(int argc, char *argv[]) int waiting_for_dht_connection = 1; if (enable_lan_discovery) { - LANdiscovery_init(dht); + lan_discovery_init(dht); log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n"); } @@ -324,7 +324,7 @@ int main(int argc, char *argv[]) do_DHT(dht); 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(); } diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 41c24ac0..a4251518 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -565,7 +565,7 @@ static void update_client(Logger *log, int index, Client_data *client, IP_Port i 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; } @@ -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 */ - if (LAN_ip(ipptp->ip_port.ip) == 0 && !is_LAN) { + if (ip_is_lan(ipptp->ip_port.ip) == 0 && !is_LAN) { 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)) { 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); 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); @@ -2840,11 +2840,11 @@ int DHT_non_lan_connected(const DHT *dht) for (uint32_t i = 0; i < LCLIENT_LIST; ++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; } - 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; } } diff --git a/toxcore/LAN_discovery.api.h b/toxcore/LAN_discovery.api.h new file mode 100644 index 00000000..9e6c6481 --- /dev/null +++ b/toxcore/LAN_discovery.api.h @@ -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 . + */ +#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 +%} diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index eeba96bd..b5cf2c52 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -256,7 +256,7 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) } /* Is IP a local ip or not. */ -bool Local_ip(IP ip) +bool ip_is_local(IP ip) { if (ip.family == TOX_AF_INET) { IP4 ip4 = ip.ip4; @@ -271,7 +271,7 @@ bool Local_ip(IP ip) IP ip4; ip4.family = TOX_AF_INET; ip4.ip4.uint32 = ip.ip6.uint32[3]; - return Local_ip(ip4); + return ip_is_local(ip4); } /* localhost in IPv6 (::1) */ @@ -286,9 +286,9 @@ bool Local_ip(IP ip) /* return 0 if ip is a LAN ip. * 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; } @@ -335,7 +335,7 @@ int LAN_ip(IP ip) IP ip4; ip4.family = TOX_AF_INET; 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; - if (LAN_ip(source.ip) == -1) { + if (ip_is_lan(source.ip) == -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]; 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); } -void LANdiscovery_kill(DHT *dht) +void lan_discovery_kill(DHT *dht) { networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, NULL, NULL); } diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 753de524..6d9d17b6 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -26,27 +26,49 @@ #include "DHT.h" -/* Interval in seconds between LAN discovery packet sending. */ -#define LAN_DISCOVERY_INTERVAL 10 +#ifndef DHT_DEFINED +#define DHT_DEFINED +typedef struct DHT DHT; +#endif /* DHT_DEFINED */ -/* Send a LAN discovery pcaket to the broadcast address with port port. */ -int send_LANdiscovery(uint16_t port, DHT *dht); +#ifndef IP_DEFINED +#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. */ -void LANdiscovery_kill(DHT *dht); +uint32_t lan_discovery_interval(void); -/* 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 -1 if it is not. */ -int LAN_ip(IP ip); - +int32_t ip_is_lan(IP ip); #endif diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 41fef426..2ebc0d9b 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -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. */ - 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) { ip_port.ip = friend_con->dht_ip_port.ip; } 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); if (temp->local_discovery_enabled) { - LANdiscovery_init(temp->dht); + lan_discovery_init(temp->dht); } return temp; @@ -907,11 +907,11 @@ static void LANdiscovery(Friend_Connections *fr_c) last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last; // 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 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 @@ -987,7 +987,7 @@ void kill_friend_connections(Friend_Connections *fr_c) } if (fr_c->local_discovery_enabled) { - LANdiscovery_kill(fr_c->dht); + lan_discovery_kill(fr_c->dht); } free(fr_c); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index b3cf2cbf..3150392e 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -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 (!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)) { return -1; } @@ -482,7 +482,7 @@ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) 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; } diff --git a/toxcore/network.h b/toxcore/network.h index c8671f57..d3b80218 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -140,20 +140,19 @@ typedef union { uint16_t uint16[8]; uint32_t uint32[4]; uint64_t uint64[2]; -} -IP6; +} IP6; IP6 get_ip6_loopback(void); extern const IP6 IP6_BROADCAST; +#define IP_DEFINED typedef struct { uint8_t family; GNU_EXTENSION union { IP4 ip4; IP6 ip6; }; -} -IP; +} IP; #define IP_PORT_DEFINED typedef struct IP_Port { diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index d60c09c9..ab96a546 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -405,7 +405,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t * /*Respond with a announce response packet*/ 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, - LAN_ip(source.ip) == 0, 1); + ip_is_lan(source.ip) == 0, 1); uint8_t nonce[CRYPTO_NONCE_SIZE]; random_nonce(nonce); diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 8a4dd40d..50373330 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -793,12 +793,12 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for } 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) { if (!lan_ips_accepted) { - if (LAN_ip(nodes[i].ip_port.ip) == 0) { + if (ip_is_lan(nodes[i].ip_port.ip) == 0) { continue; } }