diff options
author | irungentoo <irungentoo@gmail.com> | 2013-06-24 10:42:55 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-06-24 10:42:55 -0400 |
commit | 73a742a2ba10a7807251deea2f8f21d38a6e8646 (patch) | |
tree | b38880677f7502422c95f2097aef98ced223b41d | |
parent | 388c0f38f7d2096efd1049b59851b00c448f940f (diff) |
Added distance function and changed some stuff.
Also fixed typo in Readme.
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | core/DHT.c | 71 | ||||
-rw-r--r-- | core/DHT.h | 16 |
3 files changed, 69 insertions, 20 deletions
@@ -122,6 +122,6 @@ Proposal of a free as in freedom skype replacement: | |||
122 | 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] | 122 | 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] |
123 | Valid replies: a send_nodes packet | 123 | Valid replies: a send_nodes packet |
124 | 124 | ||
125 | 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] | 125 | 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] |
126 | ex: 03[Node][Node][Node] | 126 | ex: 03[Node][Node][Node] |
127 | 127 | ||
@@ -9,6 +9,30 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length) | |||
9 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 9 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); |
10 | } | 10 | } |
11 | 11 | ||
12 | //Compares client_id1 and client_id2 with client_id | ||
13 | //return 0 if both are same distance | ||
14 | //return 1 if client_id1 is closer. | ||
15 | //return 2 if client_id2 is closer. | ||
16 | int id_distance(char * client_id, char * client_id1, char * client_id2) | ||
17 | { | ||
18 | int i; | ||
19 | for(i = 0; i < CLIENT_ID_SIZE; i++) | ||
20 | { | ||
21 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) | ||
22 | { | ||
23 | return 1; | ||
24 | } | ||
25 | else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) | ||
26 | { | ||
27 | return 2; | ||
28 | } | ||
29 | |||
30 | } | ||
31 | |||
32 | return 0; | ||
33 | } | ||
34 | |||
35 | |||
12 | //Attempt to add client with ip_port and client_id to the friends client list and close_clientlist | 36 | //Attempt to add client with ip_port and client_id to the friends client list and close_clientlist |
13 | int addto_lists(IP_Port ip_port, char * client_id) | 37 | int addto_lists(IP_Port ip_port, char * client_id) |
14 | { | 38 | { |
@@ -22,10 +46,10 @@ int addto_lists(IP_Port ip_port, char * client_id) | |||
22 | //Currently incomplete: missing the ping_id part | 46 | //Currently incomplete: missing the ping_id part |
23 | int pingreq(IP_Port ip_port) | 47 | int pingreq(IP_Port ip_port) |
24 | { | 48 | { |
25 | char data[37]; | 49 | char data[5 + CLIENT_ID_SIZE]; |
26 | data[0] = 0; | 50 | data[0] = 0; |
27 | 51 | ||
28 | memcpy(data + 5, self_client_id, 32); | 52 | memcpy(data + 5, self_client_id, CLIENT_ID_SIZE); |
29 | 53 | ||
30 | sendpacket(ip_port, data, sizeof(data)); | 54 | sendpacket(ip_port, data, sizeof(data)); |
31 | 55 | ||
@@ -35,11 +59,11 @@ int pingreq(IP_Port ip_port) | |||
35 | //Currently incomplete: missing the ping_id part | 59 | //Currently incomplete: missing the ping_id part |
36 | int pingres(IP_Port ip_port, uint32_t ping_id) | 60 | int pingres(IP_Port ip_port, uint32_t ping_id) |
37 | { | 61 | { |
38 | char data[37]; | 62 | char data[5 + CLIENT_ID_SIZE]; |
39 | data[0] = 1; | 63 | data[0] = 1; |
40 | 64 | ||
41 | memcpy(data + 1, &ping_id, 4); | 65 | memcpy(data + 1, &ping_id, 4); |
42 | memcpy(data + 5, self_client_id, 32); | 66 | memcpy(data + 5, self_client_id, CLIENT_ID_SIZE); |
43 | 67 | ||
44 | sendpacket(ip_port, data, sizeof(data)); | 68 | sendpacket(ip_port, data, sizeof(data)); |
45 | 69 | ||
@@ -49,11 +73,11 @@ int pingres(IP_Port ip_port, uint32_t ping_id) | |||
49 | //Currently incomplete: missing the ping_id part | 73 | //Currently incomplete: missing the ping_id part |
50 | int getnodes(IP_Port ip_port, char * client_id) | 74 | int getnodes(IP_Port ip_port, char * client_id) |
51 | { | 75 | { |
52 | char data[69]; | 76 | char data[5 + CLIENT_ID_SIZE*2]; |
53 | data[0] = 2; | 77 | data[0] = 2; |
54 | 78 | ||
55 | memcpy(data + 5, self_client_id, 32); | 79 | memcpy(data + 5, self_client_id, CLIENT_ID_SIZE); |
56 | memcpy(data + 37, client_id, 32); | 80 | memcpy(data + 5 + CLIENT_ID_SIZE, client_id, CLIENT_ID_SIZE); |
57 | 81 | ||
58 | sendpacket(ip_port, data, sizeof(data)); | 82 | sendpacket(ip_port, data, sizeof(data)); |
59 | } | 83 | } |
@@ -62,11 +86,11 @@ int getnodes(IP_Port ip_port, char * client_id) | |||
62 | //Currently incomplete: missing the ping_id part | 86 | //Currently incomplete: missing the ping_id part |
63 | int sendnodes(IP_Port ip_port, char * client_id) | 87 | int sendnodes(IP_Port ip_port, char * client_id) |
64 | { | 88 | { |
65 | char data[325]; | 89 | char data[5 + (CLIENT_ID_SIZE + 6)*8]; |
66 | data[0] = 3; | 90 | data[0] = 3; |
67 | 91 | ||
68 | memcpy(data + 5, self_client_id, 32); | 92 | memcpy(data + 5, self_client_id, CLIENT_ID_SIZE); |
69 | memcpy(data + 37, client_id, 32); | 93 | memcpy(data + 5 + CLIENT_ID_SIZE, client_id, CLIENT_ID_SIZE); |
70 | 94 | ||
71 | sendpacket(ip_port, data, sizeof(data)); | 95 | sendpacket(ip_port, data, sizeof(data)); |
72 | } | 96 | } |
@@ -79,28 +103,50 @@ int sendnodes(IP_Port ip_port, char * client_id) | |||
79 | 103 | ||
80 | int handle_pingreq(char * packet, uint32_t length, IP_Port source) | 104 | int handle_pingreq(char * packet, uint32_t length, IP_Port source) |
81 | { | 105 | { |
106 | if(length != 5 + CLIENT_ID_SIZE) | ||
107 | { | ||
108 | return 1; | ||
109 | } | ||
110 | |||
82 | uint32_t ping_id; | 111 | uint32_t ping_id; |
83 | 112 | ||
84 | memcpy(&ping_id, packet + 1, 4); | 113 | memcpy(&ping_id, packet + 1, 4); |
85 | |||
86 | pingres(source, ping_id); | 114 | pingres(source, ping_id); |
87 | 115 | ||
116 | addto_lists(source, packet + 5); | ||
117 | |||
118 | return 0; | ||
88 | } | 119 | } |
89 | 120 | ||
90 | int handle_pingres(char * packet, uint32_t length, IP_Port source) | 121 | int handle_pingres(char * packet, uint32_t length, IP_Port source) |
91 | { | 122 | { |
123 | if(length != (5 + CLIENT_ID_SIZE)) | ||
124 | { | ||
125 | return 1; | ||
126 | } | ||
92 | 127 | ||
93 | 128 | ||
94 | } | 129 | } |
95 | 130 | ||
96 | int handle_getnodes(char * packet, uint32_t length, IP_Port source) | 131 | int handle_getnodes(char * packet, uint32_t length, IP_Port source) |
97 | { | 132 | { |
133 | if(length != (5 + CLIENT_ID_SIZE*2)) | ||
134 | { | ||
135 | return 1; | ||
136 | } | ||
137 | |||
98 | 138 | ||
139 | addto_lists(source, packet + 5); | ||
99 | 140 | ||
141 | return 0; | ||
100 | } | 142 | } |
101 | 143 | ||
102 | int handle_sendnodes(char * packet, uint32_t length, IP_Port source) | 144 | int handle_sendnodes(char * packet, uint32_t length, IP_Port source) |
103 | { | 145 | { |
146 | if(length > 325 || (length - 5) % (CLIENT_ID_SIZE + 6) != 0) | ||
147 | { | ||
148 | return 1; | ||
149 | } | ||
104 | 150 | ||
105 | 151 | ||
106 | } | 152 | } |
@@ -148,14 +194,12 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) | |||
148 | switch (packet[0]) { | 194 | switch (packet[0]) { |
149 | case 0: | 195 | case 0: |
150 | handle_pingreq(packet, length, source); | 196 | handle_pingreq(packet, length, source); |
151 | //TODO: try to add requesting node to client_list if packet is valid | ||
152 | break; | 197 | break; |
153 | case 1: | 198 | case 1: |
154 | handle_pingres(packet, length, source); | 199 | handle_pingres(packet, length, source); |
155 | break; | 200 | break; |
156 | case 2: | 201 | case 2: |
157 | handle_getnodes(packet, length, source); | 202 | handle_getnodes(packet, length, source); |
158 | //TODO: try to add requesting node to client_list if packet is valid | ||
159 | break; | 203 | break; |
160 | case 3: | 204 | case 3: |
161 | handle_sendnodes(packet, length, source); | 205 | handle_sendnodes(packet, length, source); |
@@ -171,6 +215,7 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) | |||
171 | 215 | ||
172 | 216 | ||
173 | 217 | ||
218 | |||
174 | void doDHT() | 219 | void doDHT() |
175 | { | 220 | { |
176 | 221 | ||
@@ -18,6 +18,10 @@ | |||
18 | 18 | ||
19 | #endif | 19 | #endif |
20 | 20 | ||
21 | //Current time, unix format | ||
22 | #define unix_time() ((uint32_t)time(NULL)) | ||
23 | |||
24 | #define CLIENT_ID_SIZE 32 | ||
21 | 25 | ||
22 | typedef union | 26 | typedef union |
23 | { | 27 | { |
@@ -36,7 +40,7 @@ typedef struct | |||
36 | 40 | ||
37 | typedef struct | 41 | typedef struct |
38 | { | 42 | { |
39 | char client_id[32]; | 43 | char client_id[CLIENT_ID_SIZE]; |
40 | IP_Port ip_port; | 44 | IP_Port ip_port; |
41 | uint32_t timestamp; | 45 | uint32_t timestamp; |
42 | 46 | ||
@@ -45,7 +49,7 @@ typedef struct | |||
45 | 49 | ||
46 | typedef struct | 50 | typedef struct |
47 | { | 51 | { |
48 | char client_id[32]; | 52 | char client_id[CLIENT_ID_SIZE]; |
49 | Client_data client_list[8]; | 53 | Client_data client_list[8]; |
50 | 54 | ||
51 | }Friend; | 55 | }Friend; |
@@ -73,18 +77,18 @@ typedef struct | |||
73 | 77 | ||
74 | 78 | ||
75 | //Add a new friend to the friends list | 79 | //Add a new friend to the friends list |
76 | //client_id must be 32 bytes long. | 80 | //client_id must be CLIENT_ID_SIZE bytes long. |
77 | void addfriend(char * client_id); | 81 | void addfriend(char * client_id); |
78 | 82 | ||
79 | //Delete a friend from the friends list | 83 | //Delete a friend from the friends list |
80 | //client_id must be 32 bytes long. | 84 | //client_id must be CLIENT_ID_SIZE bytes long. |
81 | //returns 0 if success | 85 | //returns 0 if success |
82 | //returns 1 if failure (client_id not in friends list) | 86 | //returns 1 if failure (client_id not in friends list) |
83 | char delfriend(char * client_id); | 87 | char delfriend(char * client_id); |
84 | 88 | ||
85 | 89 | ||
86 | //Get ip of friend | 90 | //Get ip of friend |
87 | //client_id must be 32 bytes long. | 91 | //client_id must be CLIENT_ID_SIZE bytes long. |
88 | //ip must be 4 bytes long. | 92 | //ip must be 4 bytes long. |
89 | //port must be 2 bytes long. | 93 | //port must be 2 bytes long. |
90 | //returns ip if success | 94 | //returns ip if success |
@@ -110,7 +114,7 @@ void bootstrap(IP_Port ip_port); | |||
110 | //Global variables | 114 | //Global variables |
111 | 115 | ||
112 | //Our client id | 116 | //Our client id |
113 | char self_client_id[32]; | 117 | char self_client_id[CLIENT_ID_SIZE]; |
114 | 118 | ||
115 | //Our UDP socket. | 119 | //Our UDP socket. |
116 | //We only use one so it's much easier to have it as a global variable | 120 | //We only use one so it's much easier to have it as a global variable |