Skip to content

Commit

Permalink
Use apidsl to generate LAN_discovery.h.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 16, 2018
1 parent b3ec055 commit 0b13936
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 46 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,15 @@ 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");
}

while (1) {
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();
}

Expand Down
12 changes: 6 additions & 6 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
}
Expand Down
73 changes: 73 additions & 0 deletions toxcore/LAN_discovery.api.h
Original file line number Diff line number Diff line change
@@ -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
%}
18 changes: 9 additions & 9 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) */
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
48 changes: 35 additions & 13 deletions toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions toxcore/friend_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 3 additions & 4 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion toxcore/onion_announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading

0 comments on commit 0b13936

Please sign in to comment.