mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Added distance function and changed some stuff.
Also fixed typo in Readme.
This commit is contained in:
parent
388c0f38f7
commit
73a742a2ba
|
@ -122,6 +122,6 @@ Proposal of a free as in freedom skype replacement:
|
||||||
Packet contents: [byte with value: 02][random 4 byte (ping_id)][char array (client node_id), length=32 bytes][char array: requested_node_id (node_id of which we want the ip), length=32 bytes]
|
Packet contents: [byte with value: 02][random 4 byte (ping_id)][char array (client node_id), length=32 bytes][char array: requested_node_id (node_id of which we want the ip), length=32 bytes]
|
||||||
Valid replies: a send_nodes packet
|
Valid replies: a send_nodes packet
|
||||||
|
|
||||||
Send_nodes (response): [byte with value: 03][random 4 byte (ping_id)][Nodes in node format, length=40 * (number of nodes (maximum of 8 nodes)) bytes]
|
Send_nodes (response): [byte with value: 03][random 4 byte (ping_id)][Nodes in node format, length=38 * (number of nodes (maximum of 8 nodes)) bytes]
|
||||||
ex: 03[Node][Node][Node]
|
ex: 03[Node][Node][Node]
|
||||||
|
|
||||||
|
|
71
core/DHT.c
71
core/DHT.c
|
@ -9,6 +9,30 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length)
|
||||||
return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
|
return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Compares client_id1 and client_id2 with client_id
|
||||||
|
//return 0 if both are same distance
|
||||||
|
//return 1 if client_id1 is closer.
|
||||||
|
//return 2 if client_id2 is closer.
|
||||||
|
int id_distance(char * client_id, char * client_id1, char * client_id2)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for(i = 0; i < CLIENT_ID_SIZE; i++)
|
||||||
|
{
|
||||||
|
if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i]))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i]))
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist
|
//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist
|
||||||
int addto_lists(IP_Port ip_port, char * client_id)
|
int addto_lists(IP_Port ip_port, char * client_id)
|
||||||
{
|
{
|
||||||
|
@ -22,10 +46,10 @@ int addto_lists(IP_Port ip_port, char * client_id)
|
||||||
//Currently incomplete: missing the ping_id part
|
//Currently incomplete: missing the ping_id part
|
||||||
int pingreq(IP_Port ip_port)
|
int pingreq(IP_Port ip_port)
|
||||||
{
|
{
|
||||||
char data[37];
|
char data[5 + CLIENT_ID_SIZE];
|
||||||
data[0] = 0;
|
data[0] = 0;
|
||||||
|
|
||||||
memcpy(data + 5, self_client_id, 32);
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
||||||
|
|
||||||
sendpacket(ip_port, data, sizeof(data));
|
sendpacket(ip_port, data, sizeof(data));
|
||||||
|
|
||||||
|
@ -35,11 +59,11 @@ int pingreq(IP_Port ip_port)
|
||||||
//Currently incomplete: missing the ping_id part
|
//Currently incomplete: missing the ping_id part
|
||||||
int pingres(IP_Port ip_port, uint32_t ping_id)
|
int pingres(IP_Port ip_port, uint32_t ping_id)
|
||||||
{
|
{
|
||||||
char data[37];
|
char data[5 + CLIENT_ID_SIZE];
|
||||||
data[0] = 1;
|
data[0] = 1;
|
||||||
|
|
||||||
memcpy(data + 1, &ping_id, 4);
|
memcpy(data + 1, &ping_id, 4);
|
||||||
memcpy(data + 5, self_client_id, 32);
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
||||||
|
|
||||||
sendpacket(ip_port, data, sizeof(data));
|
sendpacket(ip_port, data, sizeof(data));
|
||||||
|
|
||||||
|
@ -49,11 +73,11 @@ int pingres(IP_Port ip_port, uint32_t ping_id)
|
||||||
//Currently incomplete: missing the ping_id part
|
//Currently incomplete: missing the ping_id part
|
||||||
int getnodes(IP_Port ip_port, char * client_id)
|
int getnodes(IP_Port ip_port, char * client_id)
|
||||||
{
|
{
|
||||||
char data[69];
|
char data[5 + CLIENT_ID_SIZE*2];
|
||||||
data[0] = 2;
|
data[0] = 2;
|
||||||
|
|
||||||
memcpy(data + 5, self_client_id, 32);
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
||||||
memcpy(data + 37, client_id, 32);
|
memcpy(data + 5 + CLIENT_ID_SIZE, client_id, CLIENT_ID_SIZE);
|
||||||
|
|
||||||
sendpacket(ip_port, data, sizeof(data));
|
sendpacket(ip_port, data, sizeof(data));
|
||||||
}
|
}
|
||||||
|
@ -62,11 +86,11 @@ int getnodes(IP_Port ip_port, char * client_id)
|
||||||
//Currently incomplete: missing the ping_id part
|
//Currently incomplete: missing the ping_id part
|
||||||
int sendnodes(IP_Port ip_port, char * client_id)
|
int sendnodes(IP_Port ip_port, char * client_id)
|
||||||
{
|
{
|
||||||
char data[325];
|
char data[5 + (CLIENT_ID_SIZE + 6)*8];
|
||||||
data[0] = 3;
|
data[0] = 3;
|
||||||
|
|
||||||
memcpy(data + 5, self_client_id, 32);
|
memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
|
||||||
memcpy(data + 37, client_id, 32);
|
memcpy(data + 5 + CLIENT_ID_SIZE, client_id, CLIENT_ID_SIZE);
|
||||||
|
|
||||||
sendpacket(ip_port, data, sizeof(data));
|
sendpacket(ip_port, data, sizeof(data));
|
||||||
}
|
}
|
||||||
|
@ -79,28 +103,50 @@ int sendnodes(IP_Port ip_port, char * client_id)
|
||||||
|
|
||||||
int handle_pingreq(char * packet, uint32_t length, IP_Port source)
|
int handle_pingreq(char * packet, uint32_t length, IP_Port source)
|
||||||
{
|
{
|
||||||
|
if(length != 5 + CLIENT_ID_SIZE)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t ping_id;
|
uint32_t ping_id;
|
||||||
|
|
||||||
memcpy(&ping_id, packet + 1, 4);
|
memcpy(&ping_id, packet + 1, 4);
|
||||||
|
|
||||||
pingres(source, ping_id);
|
pingres(source, ping_id);
|
||||||
|
|
||||||
|
addto_lists(source, packet + 5);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_pingres(char * packet, uint32_t length, IP_Port source)
|
int handle_pingres(char * packet, uint32_t length, IP_Port source)
|
||||||
{
|
{
|
||||||
|
if(length != (5 + CLIENT_ID_SIZE))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_getnodes(char * packet, uint32_t length, IP_Port source)
|
int handle_getnodes(char * packet, uint32_t length, IP_Port source)
|
||||||
{
|
{
|
||||||
|
if(length != (5 + CLIENT_ID_SIZE*2))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
addto_lists(source, packet + 5);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_sendnodes(char * packet, uint32_t length, IP_Port source)
|
int handle_sendnodes(char * packet, uint32_t length, IP_Port source)
|
||||||
{
|
{
|
||||||
|
if(length > 325 || (length - 5) % (CLIENT_ID_SIZE + 6) != 0)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -148,14 +194,12 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source)
|
||||||
switch (packet[0]) {
|
switch (packet[0]) {
|
||||||
case 0:
|
case 0:
|
||||||
handle_pingreq(packet, length, source);
|
handle_pingreq(packet, length, source);
|
||||||
//TODO: try to add requesting node to client_list if packet is valid
|
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
handle_pingres(packet, length, source);
|
handle_pingres(packet, length, source);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
handle_getnodes(packet, length, source);
|
handle_getnodes(packet, length, source);
|
||||||
//TODO: try to add requesting node to client_list if packet is valid
|
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
handle_sendnodes(packet, length, source);
|
handle_sendnodes(packet, length, source);
|
||||||
|
@ -171,6 +215,7 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void doDHT()
|
void doDHT()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
16
core/DHT.h
16
core/DHT.h
|
@ -18,6 +18,10 @@
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
//Current time, unix format
|
||||||
|
#define unix_time() ((uint32_t)time(NULL))
|
||||||
|
|
||||||
|
#define CLIENT_ID_SIZE 32
|
||||||
|
|
||||||
typedef union
|
typedef union
|
||||||
{
|
{
|
||||||
|
@ -36,7 +40,7 @@ typedef struct
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char client_id[32];
|
char client_id[CLIENT_ID_SIZE];
|
||||||
IP_Port ip_port;
|
IP_Port ip_port;
|
||||||
uint32_t timestamp;
|
uint32_t timestamp;
|
||||||
|
|
||||||
|
@ -45,7 +49,7 @@ typedef struct
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char client_id[32];
|
char client_id[CLIENT_ID_SIZE];
|
||||||
Client_data client_list[8];
|
Client_data client_list[8];
|
||||||
|
|
||||||
}Friend;
|
}Friend;
|
||||||
|
@ -73,18 +77,18 @@ typedef struct
|
||||||
|
|
||||||
|
|
||||||
//Add a new friend to the friends list
|
//Add a new friend to the friends list
|
||||||
//client_id must be 32 bytes long.
|
//client_id must be CLIENT_ID_SIZE bytes long.
|
||||||
void addfriend(char * client_id);
|
void addfriend(char * client_id);
|
||||||
|
|
||||||
//Delete a friend from the friends list
|
//Delete a friend from the friends list
|
||||||
//client_id must be 32 bytes long.
|
//client_id must be CLIENT_ID_SIZE bytes long.
|
||||||
//returns 0 if success
|
//returns 0 if success
|
||||||
//returns 1 if failure (client_id not in friends list)
|
//returns 1 if failure (client_id not in friends list)
|
||||||
char delfriend(char * client_id);
|
char delfriend(char * client_id);
|
||||||
|
|
||||||
|
|
||||||
//Get ip of friend
|
//Get ip of friend
|
||||||
//client_id must be 32 bytes long.
|
//client_id must be CLIENT_ID_SIZE bytes long.
|
||||||
//ip must be 4 bytes long.
|
//ip must be 4 bytes long.
|
||||||
//port must be 2 bytes long.
|
//port must be 2 bytes long.
|
||||||
//returns ip if success
|
//returns ip if success
|
||||||
|
@ -110,7 +114,7 @@ void bootstrap(IP_Port ip_port);
|
||||||
//Global variables
|
//Global variables
|
||||||
|
|
||||||
//Our client id
|
//Our client id
|
||||||
char self_client_id[32];
|
char self_client_id[CLIENT_ID_SIZE];
|
||||||
|
|
||||||
//Our UDP socket.
|
//Our UDP socket.
|
||||||
//We only use one so it's much easier to have it as a global variable
|
//We only use one so it's much easier to have it as a global variable
|
||||||
|
|
Loading…
Reference in New Issue
Block a user