24 |
open AnyEndian |
open AnyEndian |
25 |
open BasicSocket |
open BasicSocket |
26 |
open LittleEndian |
open LittleEndian |
27 |
|
|
28 |
open CommonOptions |
open CommonOptions |
29 |
open CommonTypes |
open CommonTypes |
30 |
open CommonGlobals |
open CommonGlobals |
31 |
|
|
32 |
open DonkeyMftp |
open DonkeyMftp |
33 |
open DonkeyOvernet |
open DonkeyOvernet |
34 |
open DonkeyOptions |
open DonkeyOptions |
35 |
|
|
36 |
module P = struct |
module P = struct |
37 |
|
|
38 |
let names_of_tag = |
let names_of_tag = |
39 |
[ |
[ |
40 |
|
"\248", Field_UNKNOWN "buddyhash"; (* 0xF8 *) |
41 |
|
"\249", Field_UNKNOWN "clientlowid"; (* 0xF9 *) |
42 |
"\250", Field_UNKNOWN "serverport"; (* 0xFA *) |
"\250", Field_UNKNOWN "serverport"; (* 0xFA *) |
43 |
"\251", Field_UNKNOWN "serverip"; (* 0xFB *) |
"\251", Field_UNKNOWN "serverip"; (* 0xFB *) |
44 |
"\252", Field_UNKNOWN "sourceuport"; (* 0xFC *) |
"\252", Field_UNKNOWN "sourceuport"; (* 0xFC *) |
51 |
16 8-bits integers... welcome back to the non-portability problems... *) |
16 8-bits integers... welcome back to the non-portability problems... *) |
52 |
let get_md4 s pos = |
let get_md4 s pos = |
53 |
let ss = String.create 16 in |
let ss = String.create 16 in |
54 |
|
|
55 |
ss.[0] <- s.[pos+3]; |
ss.[0] <- s.[pos+3]; |
56 |
ss.[1] <- s.[pos+2]; |
ss.[1] <- s.[pos+2]; |
57 |
ss.[2] <- s.[pos+1]; |
ss.[2] <- s.[pos+1]; |
58 |
ss.[3] <- s.[pos+0]; |
ss.[3] <- s.[pos+0]; |
59 |
|
|
60 |
ss.[4] <- s.[pos+7]; |
ss.[4] <- s.[pos+7]; |
61 |
ss.[5] <- s.[pos+6]; |
ss.[5] <- s.[pos+6]; |
62 |
ss.[6] <- s.[pos+5]; |
ss.[6] <- s.[pos+5]; |
63 |
ss.[7] <- s.[pos+4]; |
ss.[7] <- s.[pos+4]; |
64 |
|
|
65 |
ss.[8] <- s.[pos+11]; |
ss.[8] <- s.[pos+11]; |
66 |
ss.[9] <- s.[pos+10]; |
ss.[9] <- s.[pos+10]; |
67 |
ss.[10] <- s.[pos+9]; |
ss.[10] <- s.[pos+9]; |
68 |
ss.[11] <- s.[pos+8]; |
ss.[11] <- s.[pos+8]; |
69 |
|
|
70 |
ss.[12] <- s.[pos+15]; |
ss.[12] <- s.[pos+15]; |
71 |
ss.[13] <- s.[pos+14]; |
ss.[13] <- s.[pos+14]; |
72 |
ss.[14] <- s.[pos+13]; |
ss.[14] <- s.[pos+13]; |
73 |
ss.[15] <- s.[pos+12]; |
ss.[15] <- s.[pos+12]; |
74 |
|
|
75 |
Md4.direct_of_string ss |
Md4.direct_of_string ss |
76 |
|
|
77 |
let buf_md4 buf s = |
let buf_md4 buf s = |
78 |
let s = Md4.direct_to_string s in |
let s = Md4.direct_to_string s in |
79 |
|
|
80 |
let ss = String.create 16 in |
let ss = String.create 16 in |
81 |
let pos = 0 in |
let pos = 0 in |
82 |
|
|
83 |
ss.[0] <- s.[pos+3]; |
ss.[0] <- s.[pos+3]; |
84 |
ss.[1] <- s.[pos+2]; |
ss.[1] <- s.[pos+2]; |
85 |
ss.[2] <- s.[pos+1]; |
ss.[2] <- s.[pos+1]; |
86 |
ss.[3] <- s.[pos+0]; |
ss.[3] <- s.[pos+0]; |
87 |
|
|
88 |
ss.[4] <- s.[pos+7]; |
ss.[4] <- s.[pos+7]; |
89 |
ss.[5] <- s.[pos+6]; |
ss.[5] <- s.[pos+6]; |
90 |
ss.[6] <- s.[pos+5]; |
ss.[6] <- s.[pos+5]; |
91 |
ss.[7] <- s.[pos+4]; |
ss.[7] <- s.[pos+4]; |
92 |
|
|
93 |
ss.[8] <- s.[pos+11]; |
ss.[8] <- s.[pos+11]; |
94 |
ss.[9] <- s.[pos+10]; |
ss.[9] <- s.[pos+10]; |
95 |
ss.[10] <- s.[pos+9]; |
ss.[10] <- s.[pos+9]; |
96 |
ss.[11] <- s.[pos+8]; |
ss.[11] <- s.[pos+8]; |
97 |
|
|
98 |
ss.[12] <- s.[pos+15]; |
ss.[12] <- s.[pos+15]; |
99 |
ss.[13] <- s.[pos+14]; |
ss.[13] <- s.[pos+14]; |
100 |
ss.[14] <- s.[pos+13]; |
ss.[14] <- s.[pos+13]; |
101 |
ss.[15] <- s.[pos+12]; |
ss.[15] <- s.[pos+12]; |
102 |
|
|
103 |
Buffer.add_string buf ss |
Buffer.add_string buf ss |
104 |
|
|
105 |
|
|
110 |
let c3 = int_of_char s.[pos+2] in |
let c3 = int_of_char s.[pos+2] in |
111 |
let c4 = int_of_char s.[pos+3] in |
let c4 = int_of_char s.[pos+3] in |
112 |
Ip.of_ints (c4, c3, c2, c1) |
Ip.of_ints (c4, c3, c2, c1) |
113 |
|
|
|
|
|
114 |
let buf_ip buf ip = |
let buf_ip buf ip = |
115 |
let (ip3,ip2,ip1,ip0) = Ip.to_ints ip in |
let (ip3,ip2,ip1,ip0) = Ip.to_ints ip in |
116 |
buf_int8 buf ip0; |
buf_int8 buf ip0; |
117 |
buf_int8 buf ip1; |
buf_int8 buf ip1; |
118 |
buf_int8 buf ip2; |
buf_int8 buf ip2; |
119 |
buf_int8 buf ip3 |
buf_int8 buf ip3 |
120 |
|
|
121 |
let buf_peer buf p = |
let buf_peer buf p = |
122 |
buf_md4 buf p.peer_md4; |
buf_md4 buf p.peer_md4; |
123 |
buf_ip buf p.peer_ip; |
buf_ip buf p.peer_ip; |
124 |
buf_int16 buf p.peer_port; |
buf_int16 buf p.peer_port; |
125 |
buf_int16 buf p.peer_tcpport; |
buf_int16 buf p.peer_tcpport; |
126 |
buf_int8 buf p.peer_kind |
buf_int8 buf p.peer_kind |
127 |
|
|
128 |
let write buf t = |
let write buf t = |
129 |
match t with |
match t with |
130 |
| OvernetConnect p -> |
| OvernetConnect p -> |
131 |
buf_int8 buf 0x00; |
buf_int8 buf 0x00; |
132 |
buf_peer buf p |
buf_peer buf p |
133 |
|
|
134 |
| OvernetConnectReply peers -> |
| OvernetConnectReply peers -> |
135 |
buf_int8 buf 0x08; |
buf_int8 buf 0x08; |
136 |
buf_int16 buf (List.length peers); |
buf_int16 buf (List.length peers); |
137 |
List.iter (buf_peer buf) peers |
List.iter (buf_peer buf) peers |
138 |
|
|
139 |
| OvernetPublicize p -> |
| OvernetPublicize p -> |
140 |
buf_int8 buf 0x10; |
buf_int8 buf 0x10; |
141 |
buf_peer buf p |
buf_peer buf p |
142 |
|
|
143 |
| OvernetPublicized (Some p) -> |
| OvernetPublicized (Some p) -> |
144 |
buf_int8 buf 0x18; |
buf_int8 buf 0x18; |
145 |
buf_peer buf p |
buf_peer buf p |
146 |
|
|
147 |
| OvernetSearch (nresults, target, Some uid) -> |
| OvernetSearch (nresults, target, Some uid) -> |
148 |
buf_int8 buf 0x20; |
buf_int8 buf 0x20; |
149 |
buf_int8 buf (nresults land 0x1f); |
buf_int8 buf (nresults land 0x1f); |
150 |
buf_md4 buf target; |
buf_md4 buf target; |
151 |
buf_md4 buf uid |
buf_md4 buf uid |
152 |
|
|
153 |
| OvernetSearchReply (md4, peers) -> |
| OvernetSearchReply (md4, peers) -> |
154 |
buf_int8 buf 0x28; |
buf_int8 buf 0x28; |
155 |
buf_md4 buf md4; |
buf_md4 buf md4; |
156 |
buf_int8 buf (List.length peers); |
buf_int8 buf (List.length peers); |
157 |
List.iter (buf_peer buf) peers |
List.iter (buf_peer buf) peers |
158 |
|
|
159 |
| OvernetGetSearchResults (target, kind, min, max) -> |
| OvernetGetSearchResults (target, kind, min, max) -> |
160 |
buf_int8 buf 0x30; |
buf_int8 buf 0x30; |
161 |
buf_md4 buf target; |
buf_md4 buf target; |
162 |
begin |
begin |
163 |
match kind with |
match kind with |
164 |
| Search_for_kind _ |
| Search_for_kind _ |
165 |
| Search_for_file -> |
| Search_for_file -> |
166 |
buf_int8 buf 1 |
buf_int8 buf 1 |
167 |
| Search_for_keyword None -> |
| Search_for_keyword None -> |
168 |
buf_int8 buf 0 |
buf_int8 buf 0 |
169 |
| Search_for_keyword (Some e) -> |
| Search_for_keyword (Some e) -> |
170 |
buf_int8 buf 1 |
buf_int8 buf 1 |
|
|
|
171 |
end |
end |
172 |
|
|
173 |
| OvernetSearchFilesResults (target, results) -> |
| OvernetSearchFilesResults (target, results) -> |
174 |
buf_int8 buf 0x38; |
buf_int8 buf 0x38; |
175 |
buf_md4 buf target; |
buf_md4 buf target; |
180 |
List.iter (fun tag -> |
List.iter (fun tag -> |
181 |
buf_tag buf tag names_of_tag |
buf_tag buf tag names_of_tag |
182 |
) tags |
) tags |
183 |
) results |
) results |
184 |
|
|
185 |
| OvernetPublishFiles (target, results) -> |
| OvernetPublishFiles (target, results) -> |
186 |
buf_int8 buf 0x40; |
buf_int8 buf 0x40; |
187 |
buf_md4 buf target; |
buf_md4 buf target; |
192 |
List.iter (fun tag -> |
List.iter (fun tag -> |
193 |
buf_tag buf tag names_of_tag |
buf_tag buf tag names_of_tag |
194 |
) tags |
) tags |
195 |
) results |
) results |
196 |
|
|
197 |
| OvernetPublishSources _ |
| OvernetPublishSources _ |
198 |
| OvernetSearchSourcesResults _ |
| OvernetSearchSourcesResults _ |
199 |
| OvernetUnknown21 _ |
| OvernetUnknown21 _ |
201 |
| OvernetFirewallConnectionNACK _ |
| OvernetFirewallConnectionNACK _ |
202 |
| OvernetFirewallConnectionACK _ |
| OvernetFirewallConnectionACK _ |
203 |
| OvernetFirewallConnection (_, _) |
| OvernetFirewallConnection (_, _) |
204 |
| OvernetGetMyIPResult _ |
| OvernetGetMyIPResult _ |
205 |
| OvernetGetMyIP _ |
| OvernetGetMyIP _ |
206 |
| OvernetNoResult _ |
| OvernetNoResult _ |
207 |
| OvernetPublished _ |
| OvernetPublished _ |
208 |
| OvernetSearch (_,_, None) |
| OvernetSearch (_,_, None) |
209 |
| OvernetPublicized None |
| OvernetPublicized None |
210 |
| OvernetGetMyIPDone -> raise MessageNotImplemented |
| OvernetGetMyIPDone -> raise MessageNotImplemented |
211 |
|
|
212 |
| OvernetUnknown (opcode, s) -> |
| OvernetUnknown (opcode, s) -> |
213 |
buf_int8 buf opcode; |
buf_int8 buf opcode; |
214 |
Buffer.add_string buf s |
Buffer.add_string buf s |
215 |
|
|
216 |
let get_peer s pos = |
let get_peer s pos = |
217 |
|
|
218 |
let md4 = get_md4 s pos in |
let md4 = get_md4 s pos in |
219 |
let ip = get_ip s (pos+16) in |
let ip = get_ip s (pos+16) in |
220 |
let udp_port = get_int16 s (pos + 20) in |
let udp_port = get_int16 s (pos + 20) in |
229 |
peer_last_recv = 0; |
peer_last_recv = 0; |
230 |
peer_last_send = 0; |
peer_last_send = 0; |
231 |
}, pos + 25 |
}, pos + 25 |
232 |
|
|
233 |
let get_peers_from_results ip port answers = |
let get_peers_from_results ip port answers = |
234 |
List.map (fun (r_md4, r_tags) -> |
List.map (fun (r_md4, r_tags) -> |
235 |
let peer_ip = ref ip in |
let peer_ip = ref ip in |
236 |
let peer_udpport = ref port in |
let peer_udpport = ref port in |
252 |
for_int_tag tag (fun kind -> |
for_int_tag tag (fun kind -> |
253 |
peer_kind := kind) |
peer_kind := kind) |
254 |
| _ -> |
| _ -> |
255 |
if !verbose_hidden_errors then |
if !verbose_unknown_messages then |
256 |
lprintf_nl "Unused source tag [%s]" |
lprintf_nl "[Kademlia] Unused source tag [%s]" |
257 |
(escaped_string_of_field tag) |
(escaped_string_of_field tag) |
258 |
) r_tags; |
) r_tags; |
259 |
{ |
{ |
266 |
peer_kind = !peer_kind; |
peer_kind = !peer_kind; |
267 |
} |
} |
268 |
) answers |
) answers |
269 |
|
|
270 |
let parse ip port opcode s = |
let parse ip port opcode s = |
271 |
match opcode with |
match opcode with |
272 |
0x00 -> |
0x00 -> |
273 |
let p, pos = get_peer s 0 in |
let p, pos = get_peer s 0 in |
274 |
OvernetConnect p |
OvernetConnect p |
275 |
|
|
276 |
| 0x08 -> |
| 0x08 -> |
277 |
let n = get_int16 s 0 in |
let n = get_int16 s 0 in |
278 |
let peers, pos = get_list16 get_peer s 0 in |
let peers, pos = get_list16 get_peer s 0 in |
279 |
OvernetConnectReply peers |
OvernetConnectReply peers |
280 |
|
|
281 |
| 0x10 -> |
| 0x10 -> |
282 |
let p, pos = get_peer s 0 in |
let p, pos = get_peer s 0 in |
283 |
OvernetPublicize p |
OvernetPublicize p |
284 |
|
|
285 |
| 0x18 -> |
| 0x18 -> |
286 |
let p, pos = get_peer s 0 in |
let p, pos = get_peer s 0 in |
287 |
OvernetPublicized (Some p) |
OvernetPublicized (Some p) |
288 |
|
|
289 |
| 0x20 -> |
| 0x20 -> |
290 |
let nresults = (get_uint8 s 0) land 0x1f in |
let nresults = (get_uint8 s 0) land 0x1f in |
291 |
let target = get_md4 s 1 in |
let target = get_md4 s 1 in |
292 |
let uid = get_md4 s 17 in |
let uid = get_md4 s 17 in |
293 |
OvernetSearch (nresults, target, Some uid) |
OvernetSearch (nresults, target, Some uid) |
294 |
|
|
295 |
| 0x28 -> |
| 0x28 -> |
296 |
let target = get_md4 s 0 in |
let target = get_md4 s 0 in |
297 |
let peers, pos = get_list8 get_peer s 16 in |
let peers, pos = get_list8 get_peer s 16 in |
298 |
OvernetSearchReply (target, peers) |
OvernetSearchReply (target, peers) |
299 |
|
|
300 |
| 0x30 -> |
| 0x30 -> |
301 |
let target = get_md4 s 0 in |
let target = get_md4 s 0 in |
302 |
let kind = get_uint8 s 16 in |
let kind = get_uint8 s 16 in |
303 |
let kind = |
let kind = |
304 |
if String.length s = 17 then |
if String.length s = 17 then |
305 |
if kind = 1 then Search_for_file else |
if kind = 1 then Search_for_file else |
306 |
Search_for_keyword None |
Search_for_keyword None |
308 |
Search_for_kind kind |
Search_for_kind kind |
309 |
in |
in |
310 |
OvernetGetSearchResults (target, kind, 0, 100) |
OvernetGetSearchResults (target, kind, 0, 100) |
311 |
|
|
312 |
| 0x38 -> |
| 0x38 -> |
313 |
let target = get_md4 s 0 in |
let target = get_md4 s 0 in |
314 |
let answers, pos = get_list16 (fun s pos -> |
let answers, pos = get_list16 (fun s pos -> |
327 |
if !sources then |
if !sources then |
328 |
let peers = get_peers_from_results Ip.null 0 answers in |
let peers = get_peers_from_results Ip.null 0 answers in |
329 |
OvernetSearchSourcesResults (target, peers) |
OvernetSearchSourcesResults (target, peers) |
330 |
|
|
331 |
else |
else |
332 |
OvernetSearchFilesResults (target, answers) |
OvernetSearchFilesResults (target, answers) |
333 |
| [] -> |
| [] -> |
334 |
OvernetSearchFilesResults (target, answers) |
OvernetSearchFilesResults (target, answers) |
335 |
end |
end |
336 |
|
|
337 |
| 0x40 -> |
| 0x40 -> |
338 |
let target = get_md4 s 0 in |
let target = get_md4 s 0 in |
339 |
let answers, pos = get_list16 (fun s pos -> |
let answers, pos = get_list16 (fun s pos -> |
342 |
s (pos + 16) in |
s (pos + 16) in |
343 |
(uid, tags), pos |
(uid, tags), pos |
344 |
) s 16 in |
) s 16 in |
345 |
|
|
346 |
begin |
begin |
347 |
match answers with |
match answers with |
348 |
(_, first_tags) :: _ -> |
(_, first_tags) :: _ -> |
353 |
if !sources then |
if !sources then |
354 |
let peers = get_peers_from_results ip port answers in |
let peers = get_peers_from_results ip port answers in |
355 |
OvernetPublishSources (target, peers) |
OvernetPublishSources (target, peers) |
356 |
|
|
357 |
else |
else |
358 |
OvernetPublishFiles (target, answers) |
OvernetPublishFiles (target, answers) |
359 |
| [] -> |
| [] -> |
360 |
OvernetPublishFiles (target, answers) |
OvernetPublishFiles (target, answers) |
361 |
end |
end |
362 |
|
|
|
|
|
363 |
| 0x48 -> |
| 0x48 -> |
364 |
let target = get_md4 s 0 in |
let target = get_md4 s 0 in |
365 |
OvernetPublished target |
OvernetPublished target |
366 |
|
|
367 |
(* |
(* |
368 |
#define KADEMLIA_PUBLISH_REQ 0x40 // <HASH (key) [16]> <CNT1 [2]> (<HASH (target) [16]> <CNT2 [2]> <META>*(CNT2))*(CNT1) |
#define KADEMLIA_SRC_NOTES_RES 0x3A // <HASH (key) [16]> <CNT1 [2]> (<HASH (answer) [16]> <CNT2 [2]> <META>*(CNT2))*(CNT1) |
369 |
#define KADEMLIA_PUBLISH_RES 0x48 // <HASH (key) [16]> |
#define KADEMLIA_PUB_NOTES_REQ 0x42 // <HASH (key) [16]> <HASH (target) [16]> <CNT2 [2]> <META>*(CNT2))*(CNT1) |
370 |
|
#define KADEMLIA_PUB_NOTES_RES 0x4A // <HASH (key) [16]> |
371 |
#define KADEMLIA_FIREWALLED_REQ 0x50 // <TCPPORT (sender) [2]> |
#define KADEMLIA_FIREWALLED_REQ 0x50 // <TCPPORT (sender) [2]> |
372 |
#define KADEMLIA_BUDDY_REQ 0x51 // <TCPPORT (sender) [2]> |
#define KADEMLIA_FINDBUDDY_REQ 0x51 // <TCPPORT (sender) [2]> |
373 |
#define KADEMLIA_BUDDY_CON 0x52 // |
#define KADEMLIA_CALLBACK_REQ 0x52 // <TCPPORT (sender) [2]> |
374 |
#define KADEMLIA_BUDDY_ACK 0x57 // <TCPPORT (sender) [2]> |
#define KADEMLIA_FIREWALLED_RES 0x58 // <IP (sender) [4]> |
375 |
#define KADEMLIA_FIREWALLED_RES 0x58 // <IP (sender) [4]> |
#define KADEMLIA_FIREWALLED_ACK 0x59 // (null) |
376 |
#define KADEMLIA_FIREWALLED_ACK 0x59 // (null) |
#define KADEMLIA_FINDBUDDY_RES 0x5A // <TCPPORT (sender) [2]> |
377 |
*) |
*) |
378 |
|
|
379 |
| _ -> |
| _ -> |
380 |
OvernetUnknown (opcode, String.sub s 1 (String.length s - 1)) |
OvernetUnknown (opcode, String.sub s 1 (String.length s - 1)) |
381 |
|
|
382 |
let udp_buf = Buffer.create 2000 |
let udp_buf = Buffer.create 2000 |
383 |
|
|
384 |
let kademlia_header_code = char_of_int 0xE4 |
let kademlia_header_code = char_of_int 0xE4 |
385 |
let kademlia_packed_header_code = char_of_int 0xE5 |
let kademlia_packed_header_code = char_of_int 0xE5 |
386 |
let kademlia_header = String.make 1 kademlia_header_code |
let kademlia_header = String.make 1 kademlia_header_code |
387 |
let kademlia_packed_header = String.make 1 kademlia_packed_header_code |
let kademlia_packed_header = String.make 1 kademlia_packed_header_code |
388 |
|
|
389 |
let parse_message ip port pbuf = |
let parse_message ip port pbuf = |
390 |
let len = String.length pbuf in |
let len = String.length pbuf in |
391 |
if len < 2 || |
if len < 2 || |
392 |
(let magic = pbuf.[0] in |
(let magic = pbuf.[0] in |
393 |
magic <> kademlia_header_code && |
magic <> kademlia_header_code && |
394 |
magic <> kademlia_packed_header_code) then |
magic <> kademlia_packed_header_code) then |
398 |
dump pbuf; |
dump pbuf; |
399 |
end; |
end; |
400 |
raise Not_found |
raise Not_found |
401 |
|
|
402 |
end |
end |
403 |
else |
else |
404 |
let magic = pbuf.[0] in |
let magic = pbuf.[0] in |
405 |
let opcode = int_of_char pbuf.[1] in |
let opcode = int_of_char pbuf.[1] in |
406 |
let msg = String.sub pbuf 2 (len-2) in |
let msg = String.sub pbuf 2 (len-2) in |
412 |
else msg |
else msg |
413 |
in |
in |
414 |
let t = parse ip port opcode msg in |
let t = parse ip port opcode msg in |
415 |
t |
t |
416 |
|
|
417 |
let udp_send sock ip port ping msg = |
let udp_send sock ip port ping msg = |
418 |
try |
try |
419 |
Buffer.clear udp_buf; |
Buffer.clear udp_buf; |
420 |
write udp_buf msg; |
write udp_buf msg; |
421 |
let s = Buffer.contents udp_buf in |
let s = Buffer.contents udp_buf in |
422 |
|
|
423 |
let s = |
let s = |
424 |
if String.length s > 200 then |
if String.length s > 200 then |
425 |
let opcode = String.sub s 0 1 in |
let opcode = String.sub s 0 1 in |
426 |
let args = String.sub s 1 (String.length s - 1) in |
let args = String.sub s 1 (String.length s - 1) in |
427 |
kademlia_packed_header ^ opcode ^ (Autoconf.zlib__compress_string args) |
kademlia_packed_header ^ opcode ^ (Autoconf.zlib__compress_string args) |
428 |
else |
else |
429 |
kademlia_header ^ s |
kademlia_header ^ s |
430 |
in |
in |
431 |
|
|
432 |
if !verbose_overnet then |
if !verbose_overnet then |
433 |
begin |
begin |
434 |
lprintf_nl "Sending UDP to %s:%d type %s (opcode 0x%02X len %d)" |
lprintf_nl "[Kademlia] Sending UDP to %s:%d type %s (opcode 0x%02X len %d)" |
435 |
(Ip.to_string ip) port (message_to_string msg) (get_uint8 s 1) |
(Ip.to_string ip) port (message_to_string msg) (get_uint8 s 1) |
436 |
(String.length s); |
(String.length s); |
437 |
(*dump s; lprint_newline ();*) |
(*dump s; lprint_newline ();*) |
442 |
if t <> msg then begin |
if t <> msg then begin |
443 |
lprintf "********** SENT MESSAGE DIFFERS FROM EXPECTED ******\n"; |
lprintf "********** SENT MESSAGE DIFFERS FROM EXPECTED ******\n"; |
444 |
end; |
end; |
445 |
*) |
*) |
446 |
|
|
447 |
UdpSocket.write sock ping s ip port |
UdpSocket.write sock ping s ip port |
448 |
with |
with |
449 |
| MessageNotImplemented -> () |
| MessageNotImplemented -> () |
450 |
| e -> lprintf_nl "Exception %s in udp_send" (Printexc2.to_string e) |
| e -> lprintf_nl "[Kademlia] Exception %s in udp_send" (Printexc2.to_string e) |
451 |
|
|
452 |
let udp_handler f sock event = |
let udp_handler f sock event = |
453 |
match event with |
match event with |
454 |
UdpSocket.READ_DONE -> |
UdpSocket.READ_DONE -> |
455 |
UdpSocket.read_packets sock (fun p -> |
UdpSocket.read_packets sock (fun p -> |
456 |
try |
try |
457 |
let pbuf = p.UdpSocket.udp_content in |
let pbuf = p.UdpSocket.udp_content in |
458 |
|
|
459 |
let (ip, port) = |
let (ip, port) = |
460 |
match p.UdpSocket.udp_addr with |
match p.UdpSocket.udp_addr with |
461 |
Unix.ADDR_INET (inet, port) -> |
Unix.ADDR_INET (inet, port) -> |
465 |
let t = parse_message ip port pbuf in |
let t = parse_message ip port pbuf in |
466 |
f t p |
f t p |
467 |
with e -> |
with e -> |
468 |
if !verbose_hidden_errors then |
if !verbose_unknown_messages then |
469 |
begin |
begin |
470 |
lprintf_nl "Error %s in udp_handler, dump of packet:" |
lprintf_nl "[Kademlia] Error %s in udp_handler, dump of packet:" |
471 |
(Printexc2.to_string e); |
(Printexc2.to_string e); |
472 |
dump p.UdpSocket.udp_content; |
dump p.UdpSocket.udp_content; |
473 |
lprint_newline () |
lprint_newline () |
474 |
end |
end |
475 |
); |
); |
476 |
| _ -> () |
| _ -> () |
477 |
|
|
478 |
let redirector_section = "DKKA" |
let redirector_section = "DKKA" |
479 |
let options_section_name = "Kademlia" |
let options_section_name = "Kademlia" |
480 |
|
|
481 |
let enable_overnet = enable_kademlia |
let enable_overnet = enable_kademlia |
482 |
let source_brand = false |
let source_brand = false |
483 |
|
|
484 |
let overnet_section = file_section donkey_ini |
let overnet_section = file_section donkey_ini |
485 |
[ options_section_name ] |
[ options_section_name ] |
486 |
"Kademlia options" |
"Kademlia options" |
487 |
|
|
488 |
let overnet_port = |
let overnet_port = |
489 |
define_option overnet_section [options_section_name; "port"] |
define_option overnet_section [options_section_name; "port"] |
490 |
"port for Kademlia" |
"port for Kademlia" |
491 |
int_option (2000 + Random.int 20000) |
int_option (2000 + Random.int 20000) |
492 |
|
|
493 |
let overnet_tcpport = donkey_port |
let overnet_tcpport = donkey_port |
494 |
let command_prefix = "kad_" |
let command_prefix = "kad_" |
495 |
|
|
496 |
let web_info = "kad" |
let web_info = "kad" |
497 |
end |
end |
498 |
|
|
499 |
module Kademlia = Make(P) |
module Kademlia = Make(P) |
500 |
|
|