fix buffer overflows in DNS resolution
[mldonkey.git] / tools / udpdump2txt.ml
blobdc0f9ae395a73b9246ef8ede79eee7a29aaaa595
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 _ =
40 let header = ref "" in
41 let buf = Buffer.create 1000 in
42 try
43 let left = ref 0 in
44 let rec iter pos line len =
45 if pos+1 < len then
46 try
47 let v = digit_hexa line.[pos] in
48 let vv = digit_hexa line.[pos+1] in
49 let v = v * 16 + vv in
50 if !left = 0 then
51 Buffer.add_char buf (char_of_int v)
52 else
53 decr left;
54 iter (pos+2) line len
55 with _ -> iter (pos+1) line len
57 while true do
58 let line = input_line stdin in
59 let len = String.length line in
60 if len > 2 && line.[0] = '0' && line.[1] = 'x' then
61 let pos = String.index line '\t' in
62 let end_pos = String.rindex line '\t' in
63 let len = end_pos - pos - 2 in
65 if !left > 100000 then begin
66 lprintf "DISCARD [%s]" line;
67 lprint_newline ();
68 end; *)
69 let line = String.sub line (pos+2) len in
70 iter 0 line len;
71 else begin
72 dump !header (Buffer.contents buf);
73 left := 28;
74 Buffer.clear buf;
75 header := line
76 end
77 done
78 with e ->
79 lprintf "Exception %s" (Printexc2.to_string e);lprint_newline ();
80 dump !header (Buffer.contents buf)