patch #7310
[mldonkey.git] / src / networks / donkey / donkeyOvernetImport.ml
blob7607ede5c19c21f11903b524fade68578dd595bd
1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
2 (* Copyright 2005 beedauchon *)
3 (*
4 This file is part of mldonkey.
6 mldonkey is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 mldonkey is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with mldonkey; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 open AnyEndian
21 open Printf2
22 open Md4
23 open LittleEndian
25 open CommonTypes
26 open CommonGlobals
28 open DonkeyTypes
29 open DonkeyMftp
31 let dump_file filename =
32 Unix2.tryopen_read_bin filename (fun ic ->
33 let s = String.create 20 in
34 try
35 lprintf "file: %s\n" filename;
36 let pos = ref 0 in
37 while true do
38 let n = input ic s 0 20 in
39 lprintf "pos = %d\n" !pos;
40 if n = 0 then raise Exit;
41 dump (String.sub s 0 n);
42 pos := !pos + n;
43 done
44 with End_of_file -> ())
46 module Peer = struct
48 type peer = {
49 md4 : Md4.t;
50 ip : Ip.t;
51 port : int;
52 kind : int;
55 type t = peer list
57 let get_md4 s pos =
58 get_md4 s pos
60 let get_port s pos =
61 get_int16 s pos
63 let get_kind s pos =
64 get_uint8 s pos
66 let rec read_peers s pos left =
67 if pos + 22 >= String.length s then List.rev left else
68 match
69 try
70 let md4 = get_md4 s pos in
71 let ip = get_ip s (pos+16) in
72 let port = get_port s (pos+20) in
73 let kind = get_kind s (pos+22) in
74 (* debug
75 lprintf "{ pos = %d " pos;
76 lprintf "md4 = %s " (Md4.to_string md4);
77 lprintf "ip = %s " (Ip.to_string ip);
78 lprintf "port = %d " port;
79 lprintf "type = %d}\n" kind;
81 let pos = pos + 23 in
82 Some ({
83 md4 = md4;
84 ip = ip;
85 port = port;
86 kind = kind;
87 }, pos)
88 with e ->
90 let len = String.length s - pos in
91 lprintf "Error while reading peer %s (left %d)\n"
92 (Printexc2.to_string e) len;
93 dump (String.sub s pos len);
95 None
96 with
97 None -> List.rev left
98 | Some (peer, pos) ->
99 read_peers s pos (peer :: left)
101 let read s =
102 read_peers s 4 []
103 (* unused
104 let write buf t =
105 buf_int8 buf 14;
106 buf_int buf (List.length t);
107 List.iter (fun s ->
108 buf_ip buf s.ip;
109 buf_port buf s.port
112 let print t =
113 lprintf "contact.dat: %d peers found\n" (List.length t);
114 List.iter (fun s ->
115 lprintf " peer %s:%d\n" (Ip.to_string s.ip) s.port;
116 ) t;