Use apidsl to generate ping.h.

The ping.api.h file looks rather ugly, but it works. This is an exercise
in finding the complete set of use cases needed from apidsl for toxcore.
We'll try to make things work as much as possible, and then make apidsl
better and make the .api.h files pretty.
This commit is contained in:
iphydf 2018-01-14 01:06:49 +00:00
parent 1eea3f0ab6
commit d79b15c52d
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
7 changed files with 115 additions and 33 deletions

View File

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

View File

@ -1285,7 +1285,7 @@ static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet,
sendnodes_ipv6(dht, source, packet + 1, plain, plain + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint64_t), shared_key);
add_to_ping(dht->ping, packet + 1, source);
ping_add(dht->ping, packet + 1, source);
return 0;
}
@ -2069,7 +2069,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
IP_Port pinging;
ip_copy(&pinging.ip, &ip);
pinging.port = net_htons(first_port);
send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
ping_send_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
} else {
for (i = 0; i < MAX_PUNCHING_PORTS; ++i) {
/* TODO(irungentoo): Improve port guessing algorithm. */
@ -2081,7 +2081,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
IP_Port pinging;
ip_copy(&pinging.ip, &ip);
pinging.port = net_htons(port);
send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
ping_send_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
}
dht->friends_list[friend_num].nat.punching_index += i;
@ -2095,7 +2095,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
for (i = 0; i < MAX_PUNCHING_PORTS; ++i) {
uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2;
pinging.port = net_htons(port + it);
send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
ping_send_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
}
dht->friends_list[friend_num].nat.punching_index2 += i - (MAX_PUNCHING_PORTS / 2);
@ -2562,7 +2562,7 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled)
dht->hole_punching_enabled = holepunching_enabled;
dht->ping = new_ping(dht);
dht->ping = ping_new(dht);
if (dht->ping == NULL) {
kill_DHT(dht);
@ -2610,7 +2610,7 @@ void do_DHT(DHT *dht)
do_Close(dht);
do_DHT_friends(dht);
do_NAT(dht);
do_to_ping(dht->ping);
ping_iterate(dht->ping);
#if DHT_HARDENING
do_hardening(dht);
#endif
@ -2624,7 +2624,7 @@ void kill_DHT(DHT *dht)
cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL);
ping_array_kill(dht->dht_ping_array);
ping_array_kill(dht->dht_harden_ping_array);
kill_ping(dht->ping);
ping_kill(dht->ping);
free(dht->friends_list);
free(dht->loaded_nodes_list);
free(dht);

View File

@ -225,7 +225,8 @@ typedef struct {
void *object;
} Cryptopacket_Handles;
typedef struct {
#define DHT_DEFINED
typedef struct DHT {
Logger *log;
Networking_Core *net;
@ -251,7 +252,7 @@ typedef struct {
Shared_Keys shared_keys_recv;
Shared_Keys shared_keys_sent;
struct PING *ping;
struct Ping *ping;
Ping_Array *dht_ping_array;
Ping_Array *dht_harden_ping_array;
uint64_t last_run;

View File

@ -155,11 +155,11 @@ typedef struct {
}
IP;
typedef struct {
#define IP_PORT_DEFINED
typedef struct IP_Port {
IP ip;
uint16_t port;
}
IP_Port;
} IP_Port;
/* Convert values between host and network byte order.
*/

65
toxcore/ping.api.h Normal file
View File

@ -0,0 +1,65 @@
%{
/*
* Buffered pinging using cyclic arrays.
*/
/*
* Copyright © 2016-2017 The TokTok team.
* Copyright © 2013 Tox project.
* Copyright © 2013 plutooo
*
* This file is part of Tox, the free peer to peer instant messenger.
* This file is donated to the Tox Project.
*
* 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 PING_H
#define PING_H
#include "DHT.h"
#include "network.h"
#include <stdint.h>
%}
class iP_Port { struct this; }
class dHT { struct this; }
class ping {
struct this;
static this new(dHT::this *dht);
void kill();
/** Add nodes to the to_ping list.
* All nodes in this list are pinged every TIME_TOPING seconds
* and are then removed from the list.
* If the list is full the nodes farthest from our public_key are replaced.
* 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.
*/
int32_t add(const uint8_t *public_key, iP_Port::this ip_port);
void iterate();
int32_t send_request(iP_Port::this ipp, const uint8_t *public_key);
}
%{
#endif /* PING_H */
%}

View File

@ -45,7 +45,7 @@
#define TIME_TO_PING 2
struct PING {
struct Ping {
DHT *dht;
Ping_Array *ping_array;
@ -58,7 +58,7 @@ struct PING {
#define DHT_PING_SIZE (1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + PING_PLAIN_SIZE + CRYPTO_MAC_SIZE)
#define PING_DATA_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port))
int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key)
{
uint8_t pk[DHT_PING_SIZE];
int rc;
@ -103,7 +103,7 @@ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
return sendpacket(ping->dht->net, ipp, pk, sizeof(pk));
}
static int send_ping_response(PING *ping, IP_Port ipp, const uint8_t *public_key, uint64_t ping_id,
static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key, uint64_t ping_id,
uint8_t *shared_encryption_key)
{
uint8_t pk[DHT_PING_SIZE];
@ -143,7 +143,7 @@ static int handle_ping_request(void *object, IP_Port source, const uint8_t *pack
return 1;
}
PING *ping = dht->ping;
Ping *ping = dht->ping;
if (id_equal(packet + 1, ping->dht->self_public_key)) {
return 1;
@ -171,8 +171,8 @@ static int handle_ping_request(void *object, IP_Port source, const uint8_t *pack
uint64_t ping_id;
memcpy(&ping_id, ping_plain + 1, sizeof(ping_id));
// Send response
send_ping_response(ping, source, packet + 1, ping_id, shared_key);
add_to_ping(ping, packet + 1, source);
ping_send_response(ping, source, packet + 1, ping_id, shared_key);
ping_add(ping, packet + 1, source);
return 0;
}
@ -186,7 +186,7 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac
return 1;
}
PING *ping = dht->ping;
Ping *ping = dht->ping;
if (id_equal(packet + 1, ping->dht->self_public_key)) {
return 1;
@ -274,7 +274,7 @@ static int in_list(const Client_data *list, uint16_t length, const uint8_t *publ
* return 0 if node was added.
* return -1 if node was not added.
*/
int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
int32_t ping_add(Ping *ping, const uint8_t *public_key, IP_Port ip_port)
{
if (!ip_isset(&ip_port.ip)) {
return -1;
@ -291,7 +291,7 @@ int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
IP_Port temp;
if (DHT_getfriendip(ping->dht, public_key, &temp) == 0) {
send_ping_request(ping, ip_port, public_key);
ping_send_request(ping, ip_port, public_key);
return -1;
}
@ -320,7 +320,7 @@ int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
/* 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.
*/
void do_to_ping(PING *ping)
void ping_iterate(Ping *ping)
{
if (!is_timeout(ping->last_to_ping, TIME_TO_PING)) {
return;
@ -341,7 +341,7 @@ void do_to_ping(PING *ping)
continue;
}
send_ping_request(ping, ping->to_ping[i].ip_port, ping->to_ping[i].public_key);
ping_send_request(ping, ping->to_ping[i].ip_port, ping->to_ping[i].public_key);
ip_reset(&ping->to_ping[i].ip_port.ip);
}
@ -351,9 +351,9 @@ void do_to_ping(PING *ping)
}
PING *new_ping(DHT *dht)
Ping *ping_new(DHT *dht)
{
PING *ping = (PING *)calloc(1, sizeof(PING));
Ping *ping = (Ping *)calloc(1, sizeof(Ping));
if (ping == NULL) {
return NULL;
@ -373,7 +373,7 @@ PING *new_ping(DHT *dht)
return ping;
}
void kill_ping(PING *ping)
void ping_kill(Ping *ping)
{
networking_registerhandler(ping->dht->net, NET_PACKET_PING_REQUEST, NULL, NULL);
networking_registerhandler(ping->dht->net, NET_PACKET_PING_RESPONSE, NULL, NULL);

View File

@ -31,9 +31,26 @@
#include <stdint.h>
typedef struct PING PING;
#ifndef IP_PORT_DEFINED
#define IP_PORT_DEFINED
typedef struct IP_Port IP_Port;
#endif /* IP_PORT_DEFINED */
/* Add nodes to the to_ping list.
#ifndef DHT_DEFINED
#define DHT_DEFINED
typedef struct DHT DHT;
#endif /* DHT_DEFINED */
#ifndef PING_DEFINED
#define PING_DEFINED
typedef struct Ping Ping;
#endif /* PING_DEFINED */
Ping *ping_new(DHT *dht);
void ping_kill(Ping *ping);
/** Add nodes to the to_ping list.
* All nodes in this list are pinged every TIME_TOPING seconds
* and are then removed from the list.
* If the list is full the nodes farthest from our public_key are replaced.
@ -43,12 +60,10 @@ typedef struct PING PING;
* return 0 if node was added.
* return -1 if node was not added.
*/
int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port);
void do_to_ping(PING *ping);
int32_t ping_add(Ping *ping, const uint8_t *public_key, struct IP_Port ip_port);
PING *new_ping(DHT *dht);
void kill_ping(PING *ping);
void ping_iterate(Ping *ping);
int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key);
int32_t ping_send_request(Ping *ping, struct IP_Port ipp, const uint8_t *public_key);
#endif /* PING_H */