patch #7161
[mldonkey.git] / tools / overnetdump.ml
blob32a31e6cc42faab172ee39b2755b5aca5f726447
1 let i_a = int_of_char 'a'
2 let i_A = int_of_char 'A'
3 let i_f = int_of_char 'f'
4 let i_F = int_of_char 'F'
5 let i_0 = int_of_char '0'
6 let i_9 = int_of_char '9'
8 let digit_hexa c =
9 let i = int_of_char c in
10 if i >= i_a && i <= i_f then i - i_a + 10 else
11 if i >= i_A && i <= i_F then i - i_A + 10 else
12 if i >= i_0 && i <= i_9 then i - i_0 else
13 failwith "Bad hexa char"
15 let dump header s =
16 let len = String.length s in
17 if len > 0 then begin
18 lprintf "%s" header; lprint_newline ();
19 lprintf "MESSAGE SIZE: %d" len; lprint_newline ();
20 lprintf "ascii: [";
21 for i = 0 to len - 1 do
22 let c = s.[i] in
23 let n = int_of_char c in
24 if n > 31 && n < 127 then
25 lprintf " %c" c
26 else
27 lprintf "(%d)" n
28 done;
29 lprintf "]\n";
30 lprintf "dec: [";
31 for i = 0 to len - 1 do
32 let c = s.[i] in
33 let n = int_of_char c in
34 lprintf "(%d)" n
35 done;
36 lprintf "]\n\n"
37 end
39 let dump header s =
40 let len = String.length s in
41 if len >= 2 && int_of_char s.[0] = 227 then
42 let opcode = int_of_char s.[1] in
43 begin
44 match opcode with
45 | 10 -> lprintf "OK: CONNECT MESSAGE"; lprint_newline ();
46 | 11 -> lprintf "OK: CONNECT REPLY"; lprint_newline ();
47 | 14 -> lprintf "OK: SEARCH MESSAGE"; lprint_newline ();
48 | 15 -> lprintf "OK: SEARCH REPLY"; lprint_newline ();
49 | 16 -> lprintf "OK: SEARCH GET REPLIES"; lprint_newline ();
50 | 17 -> lprintf "OK: ONE REPLY"; lprint_newline ();
51 | _ ->
52 lprintf "UNKNOWN: opcode %d" opcode; lprint_newline ();
53 end;
54 dump header s
56 let _ =
57 let header = ref "" in
58 let buf = Buffer.create 1000 in
59 try
60 let left = ref 0 in
61 let rec iter pos line len =
62 if pos+1 < len then
63 try
64 let v = digit_hexa line.[pos] in
65 let vv = digit_hexa line.[pos+1] in
66 let v = v * 16 + vv in
67 if !left = 0 then
68 Buffer.add_char buf (char_of_int v)
69 else
70 decr left;
71 iter (pos+2) line len
72 with _ -> iter (pos+1) line len
74 while true do
75 let line = input_line stdin in
76 let len = String.length line in
77 if len > 2 && line.[0] = '0' && line.[1] = 'x' then
78 let pos = String.index line '\t' in
79 let end_pos = String.rindex line '\t' in
80 let len = end_pos - pos - 2 in
82 if !left > 100000 then begin
83 lprintf "DISCARD [%s]" line;
84 lprint_newline ();
85 end; *)
86 let line = String.sub line (pos+2) len in
87 iter 0 line len;
88 else begin
89 dump !header (Buffer.contents buf);
90 left := 28;
91 Buffer.clear buf;
92 header := line
93 end
94 done
95 with e ->
96 lprintf "Exception %s" (Printexc2.to_string e);lprint_newline ();
97 dump !header (Buffer.contents buf)