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 /core/DHT.c | |
parent | 388c0f38f7d2096efd1049b59851b00c448f940f (diff) |
Added distance function and changed some stuff.
Also fixed typo in Readme.
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 71 |
1 files changed, 58 insertions, 13 deletions
@@ -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 | ||