File: overnetdump.ml

package info (click to toggle)
mldonkey 2.8.1-2etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 16,940 kB
  • ctags: 26,220
  • sloc: ml: 138,666; sh: 15,368; cpp: 12,076; ansic: 8,243; asm: 3,858; xml: 3,367; perl: 1,831; makefile: 259; python: 258
file content (98 lines) | stat: -rw-r--r-- 2,933 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
let i_a = int_of_char 'a'  
let i_A = int_of_char 'A'  
let i_f = int_of_char 'f'  
let i_F = int_of_char 'F'  
let i_0 = int_of_char '0'
let i_9 = int_of_char '9'

let digit_hexa c =
  let i = int_of_char c in
  if i >= i_a && i <= i_f then i - i_a + 10 else
  if i >= i_A && i <= i_F then i - i_A + 10 else
  if i >= i_0 && i <= i_9 then i - i_0 else
    failwith "Bad hexa char"
    
let dump header s =
  let len = String.length s in
  if len > 0 then begin
      lprintf "%s" header; lprint_newline ();
      lprintf "MESSAGE SIZE: %d" len; lprint_newline ();
      lprintf "ascii: [";
      for i = 0 to len - 1 do
        let c = s.[i] in
        let n = int_of_char c in
        if n > 31 && n < 127 then
          lprintf " %c" c
        else
          lprintf "(%d)" n
      done;
      lprintf "]\n";
      lprintf "dec: [";
      for i = 0 to len - 1 do
        let c = s.[i] in
        let n = int_of_char c in
        lprintf "(%d)" n            
      done;
      lprintf "]\n\n"
    end

let dump header s =
  let len = String.length s in
  if len >= 2 && int_of_char s.[0] = 227 then
    let opcode = int_of_char s.[1] in
    begin
      match opcode with
      | 10 -> lprintf "OK: CONNECT MESSAGE"; lprint_newline ();
      | 11 -> lprintf "OK: CONNECT REPLY"; lprint_newline ();
      | 14 -> lprintf "OK: SEARCH MESSAGE"; lprint_newline ();
      | 15 -> lprintf "OK: SEARCH REPLY"; lprint_newline ();
      | 16 -> lprintf "OK: SEARCH GET REPLIES"; lprint_newline ();
      | 17 -> lprintf "OK: ONE REPLY"; lprint_newline ();
      | _ ->
          lprintf "UNKNOWN: opcode %d" opcode; lprint_newline ();
    end;
    dump header s

let _ =
  let header = ref "" in
  let buf = Buffer.create 1000 in
  try
    let left = ref 0 in
    let rec iter pos line len =
      if pos+1 < len then
        try
          let v = digit_hexa line.[pos] in
          let vv = digit_hexa line.[pos+1] in
          let v = v * 16 + vv in
          if !left = 0 then
            Buffer.add_char buf (char_of_int v)
          else
            decr left;
          iter (pos+2) line len
        with _ -> iter (pos+1) line len
    in
    while true do
      let line = input_line stdin in
      let len = String.length line in
      if len > 2 && line.[0] = '0' && line.[1] = 'x' then
        let pos = String.index line '\t' in
        let end_pos = String.rindex line '\t' in
        let len = end_pos - pos - 2 in
        (*
        if !left > 100000 then begin
            lprintf "DISCARD [%s]" line;
            lprint_newline ();
          end; *)
        let line = String.sub line (pos+2) len in
        iter 0 line len;
      else begin
          dump !header (Buffer.contents buf);
          left := 28;
          Buffer.clear buf;        
          header := line
        end
    done
  with e -> 
      lprintf "Exception %s" (Printexc2.to_string e);lprint_newline ();
      dump !header (Buffer.contents buf)