2017-01-14 16:14:07 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2017-01-14 16:14:07 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
(* TODO Test cancelation of a (pending) connection *)
|
|
|
|
|
|
|
|
(* TODO do not recompute list_known_points at each requests... but
|
|
|
|
only once in a while, e.g. every minutes or when a point
|
2017-02-24 06:50:33 +04:00
|
|
|
or the associated peer_id is blacklisted. *)
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
(* TODO allow to track "requested peer_ids" when we reconnect to a point. *)
|
2017-01-14 16:14:07 +04:00
|
|
|
|
|
|
|
include Logging.Make (struct let name = "p2p.connection-pool" end)
|
|
|
|
|
|
|
|
type 'msg encoding = Encoding : {
|
|
|
|
tag: int ;
|
|
|
|
encoding: 'a Data_encoding.t ;
|
|
|
|
wrap: 'a -> 'msg ;
|
|
|
|
unwrap: 'msg -> 'a option ;
|
|
|
|
max_length: int option ;
|
|
|
|
} -> 'msg encoding
|
|
|
|
|
|
|
|
module Message = struct
|
|
|
|
|
|
|
|
type 'msg t =
|
|
|
|
| Bootstrap
|
2018-01-24 15:48:25 +04:00
|
|
|
| Advertise of P2p_point.Id.t list
|
|
|
|
| Swap_request of P2p_point.Id.t * P2p_peer.Id.t
|
|
|
|
| Swap_ack of P2p_point.Id.t * P2p_peer.Id.t
|
2017-01-14 16:14:07 +04:00
|
|
|
| Message of 'msg
|
|
|
|
| Disconnect
|
|
|
|
|
|
|
|
let encoding msg_encoding =
|
|
|
|
let open Data_encoding in
|
2017-04-18 20:32:31 +04:00
|
|
|
dynamic_size @@
|
2017-01-14 16:14:07 +04:00
|
|
|
union ~tag_size:`Uint16
|
2018-03-28 02:04:51 +04:00
|
|
|
([ case (Tag 0x01) ~name:"Disconnect"
|
|
|
|
(obj1 (req "kind" (constant "Disconnect")))
|
2017-01-14 16:14:07 +04:00
|
|
|
(function Disconnect -> Some () | _ -> None)
|
|
|
|
(fun () -> Disconnect);
|
2018-03-28 02:04:51 +04:00
|
|
|
case (Tag 0x02) ~name:"Bootstrap"
|
|
|
|
(obj1 (req "kind" (constant "Bootstrap")))
|
2017-01-14 16:14:07 +04:00
|
|
|
(function Bootstrap -> Some () | _ -> None)
|
|
|
|
(fun () -> Bootstrap);
|
2018-03-28 02:04:51 +04:00
|
|
|
case (Tag 0x03) ~name:"Advertise"
|
|
|
|
(obj2
|
|
|
|
(req "id" (Variable.list P2p_point.Id.encoding))
|
|
|
|
(req "kind" (constant "Advertise")))
|
|
|
|
(function Advertise points -> Some (points, ()) | _ -> None)
|
|
|
|
(fun (points, ()) -> Advertise points);
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 0x04) ~name:"Swap_request"
|
2018-03-28 02:04:51 +04:00
|
|
|
(obj3
|
|
|
|
(req "point" P2p_point.Id.encoding)
|
|
|
|
(req "peer_id" P2p_peer.Id.encoding)
|
|
|
|
(req "kind" (constant "Swap_request")))
|
2017-03-14 13:51:44 +04:00
|
|
|
(function
|
2018-03-28 02:04:51 +04:00
|
|
|
| Swap_request (point, peer_id) -> Some (point, peer_id, ())
|
2017-03-14 13:51:44 +04:00
|
|
|
| _ -> None)
|
2018-03-28 02:04:51 +04:00
|
|
|
(fun (point, peer_id, ()) -> Swap_request (point, peer_id)) ;
|
2018-03-27 17:15:03 +04:00
|
|
|
case (Tag 0x05)
|
|
|
|
~name:"Swap_ack"
|
2018-03-28 02:04:51 +04:00
|
|
|
(obj3
|
|
|
|
(req "point" P2p_point.Id.encoding)
|
|
|
|
(req "peer_id" P2p_peer.Id.encoding)
|
|
|
|
(req "kind" (constant "Swap_ack")))
|
2017-03-14 13:51:44 +04:00
|
|
|
(function
|
2018-03-28 02:04:51 +04:00
|
|
|
| Swap_ack (point, peer_id) -> Some (point, peer_id, ())
|
2017-03-14 13:51:44 +04:00
|
|
|
| _ -> None)
|
2018-03-28 02:04:51 +04:00
|
|
|
(fun (point, peer_id, ()) -> Swap_ack (point, peer_id)) ;
|
2017-01-14 16:14:07 +04:00
|
|
|
] @
|
|
|
|
ListLabels.map msg_encoding
|
|
|
|
~f:(function Encoding { tag ; encoding ; wrap ; unwrap } ->
|
2018-03-28 02:04:51 +04:00
|
|
|
Data_encoding.case (Tag tag) encoding
|
2017-01-14 16:14:07 +04:00
|
|
|
(function Message msg -> unwrap msg | _ -> None)
|
|
|
|
(fun msg -> Message (wrap msg))))
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
module Answerer = struct
|
|
|
|
|
|
|
|
type 'msg callback = {
|
2018-01-24 15:48:25 +04:00
|
|
|
bootstrap: unit -> P2p_point.Id.t list Lwt.t ;
|
|
|
|
advertise: P2p_point.Id.t list -> unit Lwt.t ;
|
2017-01-23 20:19:54 +04:00
|
|
|
message: int -> 'msg -> unit Lwt.t ;
|
2018-01-24 15:48:25 +04:00
|
|
|
swap_request: P2p_point.Id.t -> P2p_peer.Id.t -> unit Lwt.t ;
|
|
|
|
swap_ack: P2p_point.Id.t -> P2p_peer.Id.t -> unit Lwt.t ;
|
2017-01-14 16:14:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type 'msg t = {
|
2017-11-27 09:13:12 +04:00
|
|
|
canceler: Lwt_canceler.t ;
|
2018-01-24 15:48:25 +04:00
|
|
|
conn: 'msg Message.t P2p_socket.t ;
|
2017-01-14 16:14:07 +04:00
|
|
|
callback: 'msg callback ;
|
|
|
|
mutable worker: unit Lwt.t ;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rec worker_loop st =
|
|
|
|
Lwt_unix.yield () >>= fun () ->
|
2018-02-08 13:51:01 +04:00
|
|
|
protect ~canceler:st.canceler begin fun () ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.read st.conn
|
2017-01-14 16:14:07 +04:00
|
|
|
end >>= function
|
2017-01-23 20:19:54 +04:00
|
|
|
| Ok (_, Bootstrap) -> begin
|
2017-01-14 16:14:07 +04:00
|
|
|
st.callback.bootstrap () >>= function
|
|
|
|
| [] ->
|
|
|
|
worker_loop st
|
|
|
|
| points ->
|
2018-01-24 15:48:25 +04:00
|
|
|
match P2p_socket.write_now st.conn (Advertise points) with
|
2017-01-14 16:14:07 +04:00
|
|
|
| Ok _sent ->
|
|
|
|
(* if not sent then ?? TODO count dropped message ?? *)
|
|
|
|
worker_loop st
|
|
|
|
| Error _ ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel st.canceler >>= fun () ->
|
2017-01-14 16:14:07 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
end
|
2017-01-23 20:19:54 +04:00
|
|
|
| Ok (_, Advertise points) ->
|
2017-01-14 16:14:07 +04:00
|
|
|
st.callback.advertise points >>= fun () ->
|
|
|
|
worker_loop st
|
2017-03-14 13:51:44 +04:00
|
|
|
| Ok (_, Swap_request (point, peer)) ->
|
|
|
|
st.callback.swap_request point peer >>= fun () ->
|
|
|
|
worker_loop st
|
|
|
|
| Ok (_, Swap_ack (point, peer)) ->
|
|
|
|
st.callback.swap_ack point peer >>= fun () ->
|
|
|
|
worker_loop st
|
2017-01-23 20:19:54 +04:00
|
|
|
| Ok (size, Message msg) ->
|
|
|
|
st.callback.message size msg >>= fun () ->
|
2017-01-14 16:14:07 +04:00
|
|
|
worker_loop st
|
2018-02-27 19:33:50 +04:00
|
|
|
| Ok (_, Disconnect) | Error [P2p_errors.Connection_closed] ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel st.canceler >>= fun () ->
|
2017-04-10 02:40:47 +04:00
|
|
|
Lwt.return_unit
|
2018-02-27 19:33:50 +04:00
|
|
|
| Error [P2p_errors.Decoding_error] ->
|
2017-04-10 02:40:47 +04:00
|
|
|
(* TODO: Penalize peer... *)
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel st.canceler >>= fun () ->
|
2017-01-14 16:14:07 +04:00
|
|
|
Lwt.return_unit
|
2018-02-08 13:51:01 +04:00
|
|
|
| Error [ Canceled ] ->
|
2017-01-14 16:14:07 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
| Error err ->
|
|
|
|
lwt_log_error "@[Answerer unexpected error:@ %a@]"
|
|
|
|
Error_monad.pp_print_error err >>= fun () ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel st.canceler >>= fun () ->
|
2017-01-14 16:14:07 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
|
|
|
|
let run conn canceler callback =
|
|
|
|
let st = {
|
|
|
|
canceler ; conn ; callback ;
|
|
|
|
worker = Lwt.return_unit ;
|
|
|
|
} in
|
|
|
|
st.worker <-
|
|
|
|
Lwt_utils.worker "answerer"
|
2017-11-13 17:29:28 +04:00
|
|
|
~run:(fun () -> worker_loop st)
|
2017-11-27 09:13:12 +04:00
|
|
|
~cancel:(fun () -> Lwt_canceler.cancel canceler) ;
|
2017-01-14 16:14:07 +04:00
|
|
|
st
|
|
|
|
|
|
|
|
let shutdown st =
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel st.canceler >>= fun () ->
|
2017-01-14 16:14:07 +04:00
|
|
|
st.worker
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
type config = {
|
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
identity : P2p_identity.t ;
|
2017-01-14 16:14:07 +04:00
|
|
|
proof_of_work_target : Crypto_box.target ;
|
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
trusted_points : P2p_point.Id.t list ;
|
2017-01-14 16:14:07 +04:00
|
|
|
peers_file : string ;
|
|
|
|
closed_network : bool ;
|
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
listening_port : P2p_addr.port option ;
|
2017-01-14 16:14:07 +04:00
|
|
|
min_connections : int ;
|
|
|
|
max_connections : int ;
|
|
|
|
max_incoming_connections : int ;
|
2018-02-20 21:28:05 +04:00
|
|
|
connection_timeout : float ;
|
|
|
|
authentication_timeout : float ;
|
2018-02-22 18:35:50 +04:00
|
|
|
greylist_timeout : float ;
|
2017-01-14 16:14:07 +04:00
|
|
|
|
|
|
|
incoming_app_message_queue_size : int option ;
|
|
|
|
incoming_message_queue_size : int option ;
|
|
|
|
outgoing_message_queue_size : int option ;
|
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
known_peer_ids_history_size : int ;
|
2017-01-24 17:36:42 +04:00
|
|
|
known_points_history_size : int ;
|
|
|
|
max_known_points : (int * int) option ; (* max, gc target *)
|
2017-02-24 06:50:33 +04:00
|
|
|
max_known_peer_ids : (int * int) option ; (* max, gc target *)
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
swap_linger : float ;
|
2017-04-18 20:32:31 +04:00
|
|
|
|
|
|
|
binary_chunks_size : int option ;
|
2017-01-14 16:14:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type 'meta meta_config = {
|
|
|
|
encoding : 'meta Data_encoding.t;
|
|
|
|
initial : 'meta;
|
2017-01-24 17:36:42 +04:00
|
|
|
score : 'meta -> float;
|
2017-01-14 16:14:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type 'msg message_config = {
|
|
|
|
encoding : 'msg encoding list ;
|
2018-01-24 15:48:25 +04:00
|
|
|
versions : P2p_version.t list;
|
2017-01-14 16:14:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ('msg, 'meta) t = {
|
|
|
|
config : config ;
|
|
|
|
meta_config : 'meta meta_config ;
|
|
|
|
message_config : 'msg message_config ;
|
2018-01-24 15:48:25 +04:00
|
|
|
my_id_points : unit P2p_point.Table.t ;
|
2017-03-14 13:51:44 +04:00
|
|
|
known_peer_ids :
|
2018-02-08 13:51:00 +04:00
|
|
|
(('msg, 'meta) connection, 'meta) P2p_peer_state.Info.t P2p_peer.Table.t ;
|
2017-03-14 13:51:44 +04:00
|
|
|
connected_peer_ids :
|
2018-02-08 13:51:00 +04:00
|
|
|
(('msg, 'meta) connection, 'meta) P2p_peer_state.Info.t P2p_peer.Table.t ;
|
|
|
|
known_points : ('msg, 'meta) connection P2p_point_state.Info.t P2p_point.Table.t ;
|
|
|
|
connected_points : ('msg, 'meta) connection P2p_point_state.Info.t P2p_point.Table.t ;
|
2018-01-24 15:48:25 +04:00
|
|
|
incoming : Lwt_canceler.t P2p_point.Table.t ;
|
2017-01-14 16:14:07 +04:00
|
|
|
io_sched : P2p_io_scheduler.t ;
|
|
|
|
encoding : 'msg Message.t Data_encoding.t ;
|
|
|
|
events : events ;
|
2018-01-24 15:48:25 +04:00
|
|
|
watcher : P2p_connection.Pool_event.t Lwt_watcher.input ;
|
2018-02-22 18:35:50 +04:00
|
|
|
acl : P2p_acl.t;
|
2017-02-24 20:17:53 +04:00
|
|
|
mutable new_connection_hook :
|
2018-01-24 15:48:25 +04:00
|
|
|
(P2p_peer.Id.t -> ('msg, 'meta) connection -> unit) list ;
|
2017-03-14 13:51:44 +04:00
|
|
|
mutable latest_accepted_swap : Time.t ;
|
|
|
|
mutable latest_succesfull_swap : Time.t ;
|
2017-01-14 16:14:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
and events = {
|
|
|
|
too_few_connections : unit Lwt_condition.t ;
|
|
|
|
too_many_connections : unit Lwt_condition.t ;
|
2017-02-17 22:12:06 +04:00
|
|
|
new_peer : unit Lwt_condition.t ;
|
2017-02-13 17:52:16 +04:00
|
|
|
new_connection : unit Lwt_condition.t ;
|
2017-01-14 16:14:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
and ('msg, 'meta) connection = {
|
2017-11-27 09:13:12 +04:00
|
|
|
canceler : Lwt_canceler.t ;
|
2017-01-23 20:19:54 +04:00
|
|
|
messages : (int * 'msg) Lwt_pipe.t ;
|
2018-01-24 15:48:25 +04:00
|
|
|
conn : 'msg Message.t P2p_socket.t ;
|
2018-02-08 13:51:00 +04:00
|
|
|
peer_info : (('msg, 'meta) connection, 'meta) P2p_peer_state.Info.t ;
|
|
|
|
point_info : ('msg, 'meta) connection P2p_point_state.Info.t option ;
|
2017-03-14 13:51:44 +04:00
|
|
|
answerer : 'msg Answerer.t Lazy.t ;
|
2018-01-24 15:48:25 +04:00
|
|
|
mutable last_sent_swap_request : (Time.t * P2p_peer.Id.t) option ;
|
2017-01-14 16:14:07 +04:00
|
|
|
mutable wait_close : bool ;
|
|
|
|
}
|
|
|
|
|
|
|
|
type ('msg, 'meta) pool = ('msg, 'meta) t
|
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
module Pool_event = struct
|
2017-02-17 22:12:06 +04:00
|
|
|
let wait_too_few_connections pool =
|
|
|
|
Lwt_condition.wait pool.events.too_few_connections
|
|
|
|
let wait_too_many_connections pool =
|
|
|
|
Lwt_condition.wait pool.events.too_many_connections
|
|
|
|
let wait_new_peer pool =
|
|
|
|
Lwt_condition.wait pool.events.new_peer
|
|
|
|
let wait_new_connection pool =
|
|
|
|
Lwt_condition.wait pool.events.new_connection
|
|
|
|
end
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
let watch { watcher } = Lwt_watcher.create_stream watcher
|
|
|
|
let log { watcher } event = Lwt_watcher.notify watcher event
|
2017-02-17 22:12:06 +04:00
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
module Gc_point_set = List.Bounded(struct
|
|
|
|
type t = Time.t * P2p_point.Id.t
|
2017-01-24 17:36:42 +04:00
|
|
|
let compare (x, _) (y, _) = - (Time.compare x y)
|
|
|
|
end)
|
|
|
|
|
2017-02-17 22:12:06 +04:00
|
|
|
let gc_points ({ config = { max_known_points } ; known_points } as pool) =
|
2017-01-24 17:36:42 +04:00
|
|
|
match max_known_points with
|
|
|
|
| None -> ()
|
|
|
|
| Some (_, target) ->
|
|
|
|
let now = Time.now () in (* TODO: maybe time of discovery? *)
|
2018-01-24 15:48:25 +04:00
|
|
|
let table = Gc_point_set.create target in
|
|
|
|
P2p_point.Table.iter (fun p point_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
if P2p_point_state.is_disconnected point_info then
|
2017-01-24 17:36:42 +04:00
|
|
|
let time =
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_point_state.Info.last_miss point_info with
|
2017-01-24 17:36:42 +04:00
|
|
|
| None -> now
|
|
|
|
| Some t -> t in
|
2018-01-24 15:48:25 +04:00
|
|
|
Gc_point_set.insert (time, p) table
|
2017-01-24 17:36:42 +04:00
|
|
|
) known_points ;
|
2018-01-24 15:48:25 +04:00
|
|
|
let to_remove = Gc_point_set.get table in
|
2017-01-24 17:36:42 +04:00
|
|
|
ListLabels.iter to_remove ~f:begin fun (_, p) ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.remove known_points p
|
2017-02-17 22:12:06 +04:00
|
|
|
end ;
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool Gc_points
|
2017-01-24 17:36:42 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
let register_point pool ?trusted _source_peer_id (addr, port as point) =
|
2018-03-11 18:02:59 +04:00
|
|
|
match P2p_point.Table.find_opt pool.known_points point with
|
|
|
|
| None ->
|
2018-02-08 13:51:00 +04:00
|
|
|
let point_info = P2p_point_state.Info.create ?trusted addr port in
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.iter pool.config.max_known_points ~f:begin fun (max, _) ->
|
2018-01-24 15:48:25 +04:00
|
|
|
if P2p_point.Table.length pool.known_points >= max then gc_points pool
|
2017-01-24 17:36:42 +04:00
|
|
|
end ;
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.add pool.known_points point point_info ;
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (New_point point) ;
|
|
|
|
point_info
|
2018-03-11 18:02:59 +04:00
|
|
|
| Some point_info -> point_info
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let may_register_my_id_point pool = function
|
2018-02-27 19:33:50 +04:00
|
|
|
| [P2p_errors.Myself (addr, Some port)] ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.add pool.my_id_points (addr, port) () ;
|
|
|
|
P2p_point.Table.remove pool.known_points (addr, port)
|
2017-03-14 13:51:44 +04:00
|
|
|
| _ -> ()
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2017-01-24 17:36:42 +04:00
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
(* Bounded table used to garbage collect peer_id infos when needed. The
|
|
|
|
strategy used is to remove the info of the peer_id with the lowest
|
2017-01-24 17:36:42 +04:00
|
|
|
score first. In case of equality, the info of the most recent added
|
2017-02-24 06:50:33 +04:00
|
|
|
peer_id is removed. The rationale behind this choice is that in the
|
2017-01-24 17:36:42 +04:00
|
|
|
case of a flood attack, the newly added infos will probably belong
|
2017-02-24 06:50:33 +04:00
|
|
|
to peer_ids with the same (low) score and removing the most recent ones
|
|
|
|
ensure that older (and probably legit) peer_id infos are kept. *)
|
2018-01-24 15:48:25 +04:00
|
|
|
module Gc_peer_set = List.Bounded(struct
|
|
|
|
type t = float * Time.t * P2p_peer.Id.t
|
2017-01-24 17:36:42 +04:00
|
|
|
let compare (s, t, _) (s', t', _) =
|
|
|
|
let score_cmp = Pervasives.compare s s' in
|
|
|
|
if score_cmp = 0 then Time.compare t t' else - score_cmp
|
|
|
|
end)
|
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
let gc_peer_ids ({ meta_config = { score } ;
|
2017-11-13 19:34:00 +04:00
|
|
|
config = { max_known_peer_ids } ;
|
|
|
|
known_peer_ids ; } as pool) =
|
2017-02-24 06:50:33 +04:00
|
|
|
match max_known_peer_ids with
|
2017-01-24 17:36:42 +04:00
|
|
|
| None -> ()
|
|
|
|
| Some (_, target) ->
|
2018-01-24 15:48:25 +04:00
|
|
|
let table = Gc_peer_set.create target in
|
|
|
|
P2p_peer.Table.iter (fun peer_id peer_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
let created = P2p_peer_state.Info.created peer_info in
|
|
|
|
let score = score @@ P2p_peer_state.Info.metadata peer_info in
|
2018-01-24 15:48:25 +04:00
|
|
|
Gc_peer_set.insert (score, created, peer_id) table
|
2017-02-24 06:50:33 +04:00
|
|
|
) known_peer_ids ;
|
2018-01-24 15:48:25 +04:00
|
|
|
let to_remove = Gc_peer_set.get table in
|
2017-02-24 06:50:33 +04:00
|
|
|
ListLabels.iter to_remove ~f:begin fun (_, _, peer_id) ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.remove known_peer_ids peer_id
|
2017-02-17 22:12:06 +04:00
|
|
|
end ;
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool Gc_peer_ids
|
2017-01-24 17:36:42 +04:00
|
|
|
|
2017-02-24 06:50:33 +04:00
|
|
|
let register_peer pool peer_id =
|
2018-01-24 15:48:25 +04:00
|
|
|
match P2p_peer.Table.find pool.known_peer_ids peer_id with
|
2017-01-14 16:14:07 +04:00
|
|
|
| exception Not_found ->
|
2017-02-17 22:12:06 +04:00
|
|
|
Lwt_condition.broadcast pool.events.new_peer () ;
|
2018-02-08 13:51:00 +04:00
|
|
|
let peer = P2p_peer_state.Info.create peer_id ~metadata:pool.meta_config.initial in
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.iter pool.config.max_known_peer_ids ~f:begin fun (max, _) ->
|
2018-01-24 15:48:25 +04:00
|
|
|
if P2p_peer.Table.length pool.known_peer_ids >= max then gc_peer_ids pool
|
2017-01-24 17:36:42 +04:00
|
|
|
end ;
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.add pool.known_peer_ids peer_id peer ;
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (New_peer peer_id) ;
|
2017-01-14 16:14:07 +04:00
|
|
|
peer
|
|
|
|
| peer -> peer
|
|
|
|
|
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
(***************************************************************************)
|
|
|
|
|
|
|
|
let read { messages ; conn } =
|
|
|
|
Lwt.catch
|
|
|
|
(fun () -> Lwt_pipe.pop messages >>= fun (s, msg) ->
|
|
|
|
lwt_debug "%d bytes message popped from queue %a\027[0m"
|
2018-01-24 15:48:25 +04:00
|
|
|
s P2p_connection.Info.pp (P2p_socket.info conn) >>= fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
return msg)
|
2018-02-27 19:33:50 +04:00
|
|
|
(fun _ (* Closed *) -> fail P2p_errors.Connection_closed)
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let is_readable { messages } =
|
|
|
|
Lwt.catch
|
|
|
|
(fun () -> Lwt_pipe.values_available messages >>= return)
|
2018-02-27 19:33:50 +04:00
|
|
|
(fun _ (* Closed *) -> fail P2p_errors.Connection_closed)
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let write { conn } msg =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.write conn (Message msg)
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let write_sync { conn } msg =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.write_sync conn (Message msg)
|
2017-03-14 13:51:44 +04:00
|
|
|
|
2017-04-09 21:05:56 +04:00
|
|
|
let raw_write_sync { conn } buf =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.raw_write_sync conn buf
|
2017-04-09 21:05:56 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
let write_now { conn } msg =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.write_now conn (Message msg)
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let write_all pool msg =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.iter
|
2017-03-14 13:51:44 +04:00
|
|
|
(fun _peer_id peer_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_peer_state.get peer_info with
|
2017-03-14 13:51:44 +04:00
|
|
|
| Running { data = conn } ->
|
|
|
|
ignore (write_now conn msg : bool tzresult )
|
|
|
|
| _ -> ())
|
|
|
|
pool.connected_peer_ids
|
|
|
|
|
|
|
|
let broadcast_bootstrap_msg pool =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.iter
|
2017-03-14 13:51:44 +04:00
|
|
|
(fun _peer_id peer_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_peer_state.get peer_info with
|
2017-03-14 13:51:44 +04:00
|
|
|
| Running { data = { conn } } ->
|
2018-01-24 15:48:25 +04:00
|
|
|
ignore (P2p_socket.write_now conn Bootstrap : bool tzresult )
|
2017-03-14 13:51:44 +04:00
|
|
|
| _ -> ())
|
|
|
|
pool.connected_peer_ids
|
|
|
|
|
|
|
|
|
|
|
|
(***************************************************************************)
|
|
|
|
|
2018-02-22 18:35:50 +04:00
|
|
|
(* this function duplicates bit of code from the modules below to avoid
|
|
|
|
creating mutually recurvive modules *)
|
|
|
|
let get_addr pool peer_id =
|
|
|
|
let info peer_id =
|
|
|
|
try Some (P2p_peer.Table.find pool.known_peer_ids peer_id)
|
|
|
|
with Not_found -> None
|
|
|
|
in
|
|
|
|
let find_by_peer_id peer_id =
|
|
|
|
Option.apply
|
|
|
|
(info peer_id)
|
|
|
|
~f:(fun p ->
|
|
|
|
match P2p_peer_state.get p with
|
|
|
|
| Running { data } -> Some data
|
|
|
|
| _ -> None)
|
|
|
|
in
|
|
|
|
match find_by_peer_id peer_id with
|
|
|
|
|None -> None
|
|
|
|
|Some ci ->
|
|
|
|
let info = P2p_socket.info ci.conn in
|
|
|
|
Some(info.id_point)
|
|
|
|
|
|
|
|
module Points = struct
|
|
|
|
|
|
|
|
type ('msg, 'meta) info = ('msg, 'meta) connection P2p_point_state.Info.t
|
|
|
|
|
|
|
|
let info { known_points } point =
|
2018-03-11 18:02:59 +04:00
|
|
|
P2p_point.Table.find_opt known_points point
|
2018-02-22 18:35:50 +04:00
|
|
|
|
|
|
|
let get_trusted pool point =
|
2018-03-11 18:02:59 +04:00
|
|
|
Option.unopt_map ~default:false ~f:P2p_point_state.Info.trusted
|
|
|
|
(P2p_point.Table.find_opt pool.known_points point)
|
2018-02-22 18:35:50 +04:00
|
|
|
|
|
|
|
let set_trusted pool point =
|
2018-03-11 18:02:59 +04:00
|
|
|
P2p_point_state.Info.set_trusted
|
|
|
|
(register_point pool pool.config.identity.peer_id point)
|
2018-02-22 18:35:50 +04:00
|
|
|
|
|
|
|
let unset_trusted pool point =
|
2018-03-11 18:02:59 +04:00
|
|
|
Option.iter ~f:P2p_point_state.Info.unset_trusted
|
|
|
|
(P2p_point.Table.find_opt pool.known_points point)
|
2018-02-22 18:35:50 +04:00
|
|
|
|
|
|
|
let fold_known pool ~init ~f =
|
|
|
|
P2p_point.Table.fold f pool.known_points init
|
|
|
|
|
|
|
|
let fold_connected pool ~init ~f =
|
|
|
|
P2p_point.Table.fold f pool.connected_points init
|
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let banned pool (addr, _port) =
|
|
|
|
P2p_acl.banned_addr pool.acl addr
|
2018-02-22 18:35:50 +04:00
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let ban pool (addr, _port) =
|
|
|
|
P2p_acl.IPBlacklist.add pool.acl addr
|
2018-02-22 18:35:50 +04:00
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let trust pool (addr, _port) =
|
|
|
|
P2p_acl.IPBlacklist.remove pool.acl addr
|
2018-02-22 18:35:50 +04:00
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let forget pool ((addr, _port) as point) =
|
2018-02-22 18:35:50 +04:00
|
|
|
unset_trusted pool point; (* remove from whitelist *)
|
2018-03-11 18:02:59 +04:00
|
|
|
P2p_acl.IPBlacklist.remove pool.acl addr
|
2018-02-22 18:35:50 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
module Peers = struct
|
2017-03-14 13:51:44 +04:00
|
|
|
|
2018-02-08 13:51:00 +04:00
|
|
|
type ('msg, 'meta) info = (('msg, 'meta) connection, 'meta) P2p_peer_state.Info.t
|
2017-03-14 13:51:44 +04:00
|
|
|
|
2018-02-22 18:35:50 +04:00
|
|
|
let info { known_peer_ids } peer_id =
|
|
|
|
try Some (P2p_peer.Table.find known_peer_ids peer_id)
|
2017-03-14 13:51:44 +04:00
|
|
|
with Not_found -> None
|
|
|
|
|
|
|
|
let get_metadata pool peer_id =
|
2018-02-08 13:51:00 +04:00
|
|
|
try P2p_peer_state.Info.metadata (P2p_peer.Table.find pool.known_peer_ids peer_id)
|
2017-03-14 13:51:44 +04:00
|
|
|
with Not_found -> pool.meta_config.initial
|
|
|
|
|
|
|
|
let get_score pool peer_id =
|
|
|
|
pool.meta_config.score (get_metadata pool peer_id)
|
|
|
|
|
|
|
|
let set_metadata pool peer_id data =
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_peer_state.Info.set_metadata (register_peer pool peer_id) data
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let get_trusted pool peer_id =
|
2018-02-08 13:51:00 +04:00
|
|
|
try P2p_peer_state.Info.trusted (P2p_peer.Table.find pool.known_peer_ids peer_id)
|
2017-03-14 13:51:44 +04:00
|
|
|
with Not_found -> false
|
|
|
|
|
|
|
|
let set_trusted pool peer_id =
|
2018-02-08 13:51:00 +04:00
|
|
|
try P2p_peer_state.Info.set_trusted (register_peer pool peer_id)
|
2017-03-14 13:51:44 +04:00
|
|
|
with Not_found -> ()
|
|
|
|
|
|
|
|
let unset_trusted pool peer_id =
|
2018-02-08 13:51:00 +04:00
|
|
|
try P2p_peer_state.Info.unset_trusted (P2p_peer.Table.find pool.known_peer_ids peer_id)
|
2017-03-14 13:51:44 +04:00
|
|
|
with Not_found -> ()
|
|
|
|
|
|
|
|
let fold_known pool ~init ~f =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.fold f pool.known_peer_ids init
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let fold_connected pool ~init ~f =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.fold f pool.connected_peer_ids init
|
2017-03-14 13:51:44 +04:00
|
|
|
|
2018-02-22 18:35:50 +04:00
|
|
|
let forget pool peer =
|
2018-03-11 18:02:59 +04:00
|
|
|
Option.iter (get_addr pool peer) ~f:begin fun (addr, _port) ->
|
|
|
|
unset_trusted pool peer; (* remove from whitelist *)
|
|
|
|
P2p_acl.PeerBlacklist.remove pool.acl peer;
|
|
|
|
P2p_acl.IPBlacklist.remove pool.acl addr
|
|
|
|
end
|
2018-02-22 18:35:50 +04:00
|
|
|
|
|
|
|
let ban pool peer =
|
2018-03-11 18:02:59 +04:00
|
|
|
Option.iter (get_addr pool peer) ~f:begin fun point ->
|
|
|
|
Points.ban pool point ;
|
|
|
|
P2p_acl.PeerBlacklist.add pool.acl peer
|
|
|
|
end
|
2018-02-22 18:35:50 +04:00
|
|
|
|
|
|
|
let trust pool peer =
|
2018-03-11 18:02:59 +04:00
|
|
|
Option.iter (get_addr pool peer) ~f:(Points.trust pool)
|
2018-02-22 18:35:50 +04:00
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let banned pool peer =
|
|
|
|
P2p_acl.banned_peer pool.acl peer
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module Connection = struct
|
|
|
|
|
|
|
|
let fold pool ~init ~f =
|
2018-01-24 15:48:25 +04:00
|
|
|
Peers.fold_connected pool ~init ~f:begin fun peer_id peer_info acc ->
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_peer_state.get peer_info with
|
2017-03-14 13:51:44 +04:00
|
|
|
| Running { data } -> f peer_id data acc
|
|
|
|
| _ -> acc
|
|
|
|
end
|
|
|
|
|
|
|
|
let list pool =
|
|
|
|
fold pool ~init:[] ~f:(fun peer_id c acc -> (peer_id, c) :: acc)
|
|
|
|
|
|
|
|
let random ?different_than pool =
|
|
|
|
let candidates =
|
|
|
|
fold pool ~init:[] ~f:begin fun _peer conn acc ->
|
|
|
|
match different_than with
|
|
|
|
| Some excluded_conn
|
2018-01-24 15:48:25 +04:00
|
|
|
when P2p_socket.equal conn.conn excluded_conn.conn -> acc
|
2017-03-14 13:51:44 +04:00
|
|
|
| Some _ | None -> conn :: acc
|
|
|
|
end in
|
|
|
|
match candidates with
|
|
|
|
| [] -> None
|
|
|
|
| _ :: _ ->
|
|
|
|
Some (List.nth candidates (Random.int @@ List.length candidates))
|
|
|
|
|
|
|
|
let random_lowid ?different_than pool =
|
|
|
|
let candidates =
|
|
|
|
fold pool ~init:[] ~f:begin fun _peer conn acc ->
|
|
|
|
match different_than with
|
|
|
|
| Some excluded_conn
|
2018-01-24 15:48:25 +04:00
|
|
|
when P2p_socket.equal conn.conn excluded_conn.conn -> acc
|
2017-03-14 13:51:44 +04:00
|
|
|
| Some _ | None ->
|
2018-01-24 15:48:25 +04:00
|
|
|
let ci = P2p_socket.info conn.conn in
|
2017-03-14 13:51:44 +04:00
|
|
|
match ci.id_point with
|
|
|
|
| _, None -> acc
|
|
|
|
| addr, Some port -> ((addr, port), ci.peer_id, conn) :: acc
|
|
|
|
end in
|
|
|
|
match candidates with
|
|
|
|
| [] -> None
|
|
|
|
| _ :: _ ->
|
|
|
|
Some (List.nth candidates (Random.int @@ List.length candidates))
|
|
|
|
|
|
|
|
let stat { conn } =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.stat conn
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let score { meta_config = { score }} meta = score meta
|
|
|
|
|
|
|
|
let info { conn } =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.info conn
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let find_by_peer_id pool peer_id =
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.apply
|
2018-01-24 15:48:25 +04:00
|
|
|
(Peers.info pool peer_id)
|
2017-03-14 13:51:44 +04:00
|
|
|
~f:(fun p ->
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_peer_state.get p with
|
2017-03-14 13:51:44 +04:00
|
|
|
| Running { data } -> Some data
|
|
|
|
| _ -> None)
|
|
|
|
|
|
|
|
let find_by_point pool point =
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.apply
|
2017-03-14 13:51:44 +04:00
|
|
|
(Points.info pool point)
|
|
|
|
~f:(fun p ->
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_point_state.get p with
|
2017-03-14 13:51:44 +04:00
|
|
|
| Running { data } -> Some data
|
|
|
|
| _ -> None)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let greylist_addr pool addr =
|
2018-02-22 18:35:50 +04:00
|
|
|
P2p_acl.IPGreylist.add pool.acl addr
|
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let greylist_peer pool peer =
|
|
|
|
Option.iter (get_addr pool peer) ~f:begin fun (addr, _port) ->
|
|
|
|
P2p_acl.IPGreylist.add pool.acl addr ;
|
|
|
|
P2p_acl.PeerGreylist.add pool.acl peer
|
|
|
|
end
|
2018-02-22 18:35:50 +04:00
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let acl_clear pool =
|
|
|
|
P2p_acl.clear pool.acl
|
2018-02-22 18:35:50 +04:00
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let gc_greylist ~delay pool =
|
|
|
|
P2p_acl.IPGreylist.gc ~delay pool.acl
|
2018-02-22 18:35:50 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
let pool_stat { io_sched } =
|
|
|
|
P2p_io_scheduler.global_stat io_sched
|
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let config { config } = config
|
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
(***************************************************************************)
|
|
|
|
|
|
|
|
let fail_unless_disconnected_point point_info =
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_point_state.get point_info with
|
2017-03-14 13:51:44 +04:00
|
|
|
| Disconnected -> return ()
|
2018-02-27 19:33:50 +04:00
|
|
|
| Requested _ | Accepted _ -> fail P2p_errors.Pending_connection
|
|
|
|
| Running _ -> fail P2p_errors.Connected
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
let fail_unless_disconnected_peer_id peer_info =
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_peer_state.get peer_info with
|
2017-03-14 13:51:44 +04:00
|
|
|
| Disconnected -> return ()
|
2018-02-27 19:33:50 +04:00
|
|
|
| Accepted _ -> fail P2p_errors.Pending_connection
|
|
|
|
| Running _ -> fail P2p_errors.Connected
|
2017-01-14 16:14:07 +04:00
|
|
|
|
|
|
|
let compare_known_point_info p1 p2 =
|
|
|
|
(* The most-recently disconnected peers are greater. *)
|
|
|
|
(* Then come long-standing connected peers. *)
|
2018-02-08 13:51:00 +04:00
|
|
|
let disconnected1 = P2p_point_state.is_disconnected p1
|
|
|
|
and disconnected2 = P2p_point_state.is_disconnected p2 in
|
2017-01-14 16:14:07 +04:00
|
|
|
let compare_last_seen p1 p2 =
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_point_state.Info.last_seen p1, P2p_point_state.Info.last_seen p2 with
|
2017-01-14 16:14:07 +04:00
|
|
|
| None, None -> Random.int 2 * 2 - 1 (* HACK... *)
|
|
|
|
| Some _, None -> 1
|
|
|
|
| None, Some _ -> -1
|
|
|
|
| Some (_, time1), Some (_, time2) ->
|
|
|
|
match compare time1 time2 with
|
|
|
|
| 0 -> Random.int 2 * 2 - 1 (* HACK... *)
|
|
|
|
| x -> x in
|
|
|
|
match disconnected1, disconnected2 with
|
|
|
|
| false, false -> compare_last_seen p1 p2
|
|
|
|
| false, true -> -1
|
|
|
|
| true, false -> 1
|
|
|
|
| true, true -> compare_last_seen p2 p1
|
|
|
|
|
2018-02-20 21:28:05 +04:00
|
|
|
let rec connect ?timeout pool point =
|
2018-03-11 18:02:59 +04:00
|
|
|
fail_when (Points.banned pool point)
|
2018-02-22 18:35:50 +04:00
|
|
|
(P2p_errors.Point_banned point) >>=? fun () ->
|
2018-02-20 21:28:05 +04:00
|
|
|
let timeout =
|
|
|
|
Option.unopt ~default:pool.config.connection_timeout timeout in
|
2017-03-14 13:51:44 +04:00
|
|
|
fail_unless
|
|
|
|
(active_connections pool <= pool.config.max_connections)
|
2018-02-27 19:33:50 +04:00
|
|
|
P2p_errors.Too_many_connections >>=? fun () ->
|
2017-11-27 09:13:12 +04:00
|
|
|
let canceler = Lwt_canceler.create () in
|
2018-02-08 13:51:01 +04:00
|
|
|
with_timeout ~canceler (Lwt_unix.sleep timeout) begin fun canceler ->
|
2017-03-14 13:51:44 +04:00
|
|
|
let point_info =
|
|
|
|
register_point pool pool.config.identity.peer_id point in
|
2018-02-08 13:51:00 +04:00
|
|
|
let addr, port as point = P2p_point_state.Info.point point_info in
|
2017-03-14 13:51:44 +04:00
|
|
|
fail_unless
|
2018-02-08 13:51:00 +04:00
|
|
|
(not pool.config.closed_network || P2p_point_state.Info.trusted point_info)
|
2018-02-27 19:33:50 +04:00
|
|
|
P2p_errors.Closed_network >>=? fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
fail_unless_disconnected_point point_info >>=? fun () ->
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_point_state.set_requested point_info canceler ;
|
2017-03-14 13:51:44 +04:00
|
|
|
let fd = Lwt_unix.socket PF_INET6 SOCK_STREAM 0 in
|
|
|
|
let uaddr =
|
|
|
|
Lwt_unix.ADDR_INET (Ipaddr_unix.V6.to_inet_addr addr, port) in
|
2018-01-24 15:48:25 +04:00
|
|
|
lwt_debug "connect: %a" P2p_point.Id.pp point >>= fun () ->
|
2018-02-08 13:51:01 +04:00
|
|
|
protect ~canceler begin fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Outgoing_connection point) ;
|
|
|
|
Lwt_unix.connect fd uaddr >>= fun () ->
|
|
|
|
return ()
|
|
|
|
end ~on_error: begin fun err ->
|
2018-01-24 15:48:25 +04:00
|
|
|
lwt_debug "connect: %a -> disconnect" P2p_point.Id.pp point >>= fun () ->
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_point_state.set_disconnected point_info ;
|
2018-02-08 13:51:01 +04:00
|
|
|
Lwt_utils_unix.safe_close fd >>= fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
match err with
|
|
|
|
| [Exn (Unix.Unix_error (Unix.ECONNREFUSED, _, _))] ->
|
2018-02-27 19:33:50 +04:00
|
|
|
fail P2p_errors.Connection_refused
|
2017-03-14 13:51:44 +04:00
|
|
|
| err -> Lwt.return (Error err)
|
|
|
|
end >>=? fun () ->
|
2018-01-24 15:48:25 +04:00
|
|
|
lwt_debug "connect: %a -> authenticate" P2p_point.Id.pp point >>= fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
authenticate pool ~point_info canceler fd point
|
|
|
|
end
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
and authenticate pool ?point_info canceler fd point =
|
|
|
|
let incoming = point_info = None in
|
2017-01-14 16:14:07 +04:00
|
|
|
lwt_debug "authenticate: %a%s"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
2017-01-14 16:14:07 +04:00
|
|
|
(if incoming then " incoming" else "") >>= fun () ->
|
2018-02-08 13:51:01 +04:00
|
|
|
protect ~canceler begin fun () ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.authenticate
|
2017-01-14 16:14:07 +04:00
|
|
|
~proof_of_work_target:pool.config.proof_of_work_target
|
|
|
|
~incoming (P2p_io_scheduler.register pool.io_sched fd) point
|
|
|
|
?listening_port:pool.config.listening_port
|
|
|
|
pool.config.identity pool.message_config.versions
|
|
|
|
end ~on_error: begin fun err ->
|
|
|
|
(* TODO do something when the error is Not_enough_proof_of_work ?? *)
|
2017-11-28 18:07:34 +04:00
|
|
|
begin match err with
|
2018-02-08 13:51:01 +04:00
|
|
|
| [ Canceled ] ->
|
2017-11-28 18:07:34 +04:00
|
|
|
(* Currently only on time out *)
|
|
|
|
lwt_debug "authenticate: %a%s -> canceled"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
2017-11-28 18:07:34 +04:00
|
|
|
(if incoming then " incoming" else "")
|
|
|
|
| err ->
|
|
|
|
(* Authentication incorrect! *)
|
|
|
|
lwt_debug "@[authenticate: %a%s -> failed@ %a@]"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
2017-11-28 18:07:34 +04:00
|
|
|
(if incoming then " incoming" else "")
|
|
|
|
pp_print_error err
|
|
|
|
end >>= fun () ->
|
2017-01-14 16:14:07 +04:00
|
|
|
may_register_my_id_point pool err ;
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Authentication_failed point) ;
|
2017-01-14 16:14:07 +04:00
|
|
|
if incoming then
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.remove pool.incoming point
|
2017-01-14 16:14:07 +04:00
|
|
|
else
|
2018-02-08 13:51:00 +04:00
|
|
|
Option.iter ~f:P2p_point_state.set_disconnected point_info ;
|
2017-01-14 16:14:07 +04:00
|
|
|
Lwt.return (Error err)
|
|
|
|
end >>=? fun (info, auth_fd) ->
|
2017-02-17 22:12:06 +04:00
|
|
|
(* Authentication correct! *)
|
2017-01-14 16:14:07 +04:00
|
|
|
lwt_debug "authenticate: %a -> auth %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
|
|
|
P2p_connection.Info.pp info >>= fun () ->
|
2018-03-11 18:02:59 +04:00
|
|
|
fail_when (Peers.banned pool info.peer_id)
|
2018-02-22 18:35:50 +04:00
|
|
|
(P2p_errors.Peer_banned info.peer_id) >>=? fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
let remote_point_info =
|
2017-01-14 16:14:07 +04:00
|
|
|
match info.id_point with
|
|
|
|
| addr, Some port
|
2018-01-24 15:48:25 +04:00
|
|
|
when not (P2p_point.Table.mem pool.my_id_points (addr, port)) ->
|
2017-03-14 13:51:44 +04:00
|
|
|
Some (register_point pool info.peer_id (addr, port))
|
2017-01-14 16:14:07 +04:00
|
|
|
| _ -> None in
|
2017-03-14 13:51:44 +04:00
|
|
|
let connection_point_info =
|
|
|
|
match point_info, remote_point_info with
|
2017-01-14 16:14:07 +04:00
|
|
|
| None, None -> None
|
2017-03-14 13:51:44 +04:00
|
|
|
| Some _ as point_info, _ | _, (Some _ as point_info) -> point_info in
|
|
|
|
let peer_info = register_peer pool info.peer_id in
|
2017-02-13 17:52:16 +04:00
|
|
|
let acceptable_versions =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_version.common info.versions pool.message_config.versions
|
2017-02-13 17:52:16 +04:00
|
|
|
in
|
2017-01-14 16:14:07 +04:00
|
|
|
let acceptable_point =
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.unopt_map connection_point_info
|
2017-01-14 16:14:07 +04:00
|
|
|
~default:(not pool.config.closed_network)
|
2017-03-14 13:51:44 +04:00
|
|
|
~f:begin fun connection_point_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_point_state.get connection_point_info with
|
2017-11-13 19:34:00 +04:00
|
|
|
| Requested _ -> not incoming
|
|
|
|
| Disconnected ->
|
|
|
|
not pool.config.closed_network
|
2018-02-08 13:51:00 +04:00
|
|
|
|| P2p_point_state.Info.trusted connection_point_info
|
2017-11-13 19:34:00 +04:00
|
|
|
| Accepted _ | Running _ -> false
|
|
|
|
end
|
2017-01-14 16:14:07 +04:00
|
|
|
in
|
2017-02-24 06:50:33 +04:00
|
|
|
let acceptable_peer_id =
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_peer_state.get peer_info with
|
2017-01-14 16:14:07 +04:00
|
|
|
| Accepted _ ->
|
|
|
|
(* TODO: in some circumstances cancel and accept... *)
|
|
|
|
false
|
|
|
|
| Running _ -> false
|
|
|
|
| Disconnected -> true
|
|
|
|
in
|
2017-02-17 22:12:06 +04:00
|
|
|
if incoming then
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.remove pool.incoming point ;
|
2017-02-13 17:52:16 +04:00
|
|
|
match acceptable_versions with
|
2017-02-24 06:50:33 +04:00
|
|
|
| Some version when acceptable_peer_id && acceptable_point -> begin
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Accepting_request (point, info.id_point, info.peer_id)) ;
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.iter connection_point_info
|
2017-03-14 13:51:44 +04:00
|
|
|
~f:(fun point_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_point_state.set_accepted point_info info.peer_id canceler) ;
|
|
|
|
P2p_peer_state.set_accepted peer_info info.id_point canceler ;
|
2017-02-13 17:52:16 +04:00
|
|
|
lwt_debug "authenticate: %a -> accept %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
|
|
|
P2p_connection.Info.pp info >>= fun () ->
|
2018-02-08 13:51:01 +04:00
|
|
|
protect ~canceler begin fun () ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.accept
|
2017-02-13 17:52:16 +04:00
|
|
|
?incoming_message_queue_size:pool.config.incoming_message_queue_size
|
|
|
|
?outgoing_message_queue_size:pool.config.outgoing_message_queue_size
|
2017-04-18 20:32:31 +04:00
|
|
|
?binary_chunks_size:pool.config.binary_chunks_size
|
2017-02-13 17:52:16 +04:00
|
|
|
auth_fd pool.encoding >>= fun conn ->
|
|
|
|
lwt_debug "authenticate: %a -> Connected %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
|
|
|
P2p_connection.Info.pp info >>= fun () ->
|
2017-02-13 17:52:16 +04:00
|
|
|
Lwt.return conn
|
|
|
|
end ~on_error: begin fun err ->
|
2017-02-17 22:12:06 +04:00
|
|
|
if incoming then
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool
|
|
|
|
(Request_rejected (point, Some (info.id_point, info.peer_id))) ;
|
2017-02-13 17:52:16 +04:00
|
|
|
lwt_debug "authenticate: %a -> rejected %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
|
|
|
P2p_connection.Info.pp info >>= fun () ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.iter connection_point_info
|
2018-02-08 13:51:00 +04:00
|
|
|
~f:P2p_point_state.set_disconnected ;
|
|
|
|
P2p_peer_state.set_disconnected peer_info ;
|
2017-02-13 17:52:16 +04:00
|
|
|
Lwt.return (Error err)
|
|
|
|
end >>=? fun conn ->
|
|
|
|
let id_point =
|
2018-02-08 13:51:00 +04:00
|
|
|
match info.id_point, Option.map ~f:P2p_point_state.Info.point point_info with
|
2017-02-13 17:52:16 +04:00
|
|
|
| (addr, _), Some (_, port) -> addr, Some port
|
|
|
|
| id_point, None -> id_point in
|
2017-03-14 13:51:44 +04:00
|
|
|
return
|
|
|
|
(create_connection
|
|
|
|
pool conn
|
|
|
|
id_point connection_point_info peer_info version)
|
2017-02-13 17:52:16 +04:00
|
|
|
end
|
|
|
|
| _ -> begin
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Rejecting_request (point, info.id_point, info.peer_id)) ;
|
2017-02-24 06:50:33 +04:00
|
|
|
lwt_debug "authenticate: %a -> kick %a point: %B peer_id: %B"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp point
|
|
|
|
P2p_connection.Info.pp info
|
2017-02-24 06:50:33 +04:00
|
|
|
acceptable_point acceptable_peer_id >>= fun () ->
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.kick auth_fd >>= fun () ->
|
2017-02-13 17:52:16 +04:00
|
|
|
if not incoming then begin
|
2018-02-08 13:51:00 +04:00
|
|
|
Option.iter ~f:P2p_point_state.set_disconnected point_info ;
|
|
|
|
(* FIXME P2p_peer_state.set_disconnected ~requested:true peer_info ; *)
|
2017-02-13 17:52:16 +04:00
|
|
|
end ;
|
2018-02-27 19:33:50 +04:00
|
|
|
fail (P2p_errors.Rejected info.peer_id)
|
2017-02-13 17:52:16 +04:00
|
|
|
end
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
and create_connection pool p2p_conn id_point point_info peer_info _version =
|
2018-02-08 13:51:00 +04:00
|
|
|
let peer_id = P2p_peer_state.Info.peer_id peer_info in
|
2017-11-27 09:13:12 +04:00
|
|
|
let canceler = Lwt_canceler.create () in
|
2017-03-14 13:51:44 +04:00
|
|
|
let size =
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.map pool.config.incoming_app_message_queue_size
|
2017-11-11 06:34:12 +04:00
|
|
|
~f:(fun qs -> qs, fun (size, _) ->
|
|
|
|
(Sys.word_size / 8) * 11 + size + Lwt_pipe.push_overhead) in
|
2017-03-14 13:51:44 +04:00
|
|
|
let messages = Lwt_pipe.create ?size () in
|
|
|
|
let rec callback =
|
|
|
|
{ Answerer.message =
|
|
|
|
(fun size msg -> Lwt_pipe.push messages (size, msg)) ;
|
|
|
|
advertise =
|
|
|
|
(fun points -> register_new_points pool conn points ) ;
|
|
|
|
bootstrap =
|
|
|
|
(fun () -> list_known_points pool conn () ) ;
|
|
|
|
swap_request =
|
|
|
|
(fun point peer_id -> swap_request pool conn point peer_id ) ;
|
|
|
|
swap_ack =
|
|
|
|
(fun point peer_id -> swap_ack pool conn point peer_id ) ;
|
|
|
|
}
|
|
|
|
and answerer = lazy (Answerer.run p2p_conn canceler callback)
|
|
|
|
and conn =
|
|
|
|
{ conn = p2p_conn ; point_info ; peer_info ;
|
|
|
|
messages ; canceler ; answerer ; wait_close = false ;
|
|
|
|
last_sent_swap_request = None } in
|
|
|
|
ignore (Lazy.force answerer) ;
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.iter point_info ~f:begin fun point_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
let point = P2p_point_state.Info.point point_info in
|
|
|
|
P2p_point_state.set_running point_info peer_id conn ;
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.add pool.connected_points point point_info ;
|
2017-03-14 13:51:44 +04:00
|
|
|
end ;
|
|
|
|
log pool (Connection_established (id_point, peer_id)) ;
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_peer_state.set_running peer_info id_point conn ;
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.add pool.connected_peer_ids peer_id peer_info ;
|
2017-03-14 13:51:44 +04:00
|
|
|
Lwt_condition.broadcast pool.events.new_connection () ;
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.on_cancel canceler begin fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
lwt_debug "Disconnect: %a (%a)"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Id.pp peer_id P2p_connection.Id.pp id_point >>= fun () ->
|
2018-02-08 13:51:00 +04:00
|
|
|
Option.iter ~f:P2p_point_state.set_disconnected point_info ;
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Disconnection peer_id) ;
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_peer_state.set_disconnected peer_info ;
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.iter point_info ~f:begin fun point_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_point.Table.remove pool.connected_points (P2p_point_state.Info.point point_info) ;
|
2017-03-14 13:51:44 +04:00
|
|
|
end ;
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.remove pool.connected_peer_ids peer_id ;
|
2017-03-14 13:51:44 +04:00
|
|
|
if pool.config.max_connections <= active_connections pool then begin
|
|
|
|
Lwt_condition.broadcast pool.events.too_many_connections () ;
|
|
|
|
log pool Too_many_connections ;
|
|
|
|
end ;
|
2017-04-10 02:40:47 +04:00
|
|
|
Lwt_pipe.close messages ;
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_socket.close ~wait:conn.wait_close conn.conn
|
2017-03-14 13:51:44 +04:00
|
|
|
end ;
|
|
|
|
List.iter (fun f -> f peer_id conn) pool.new_connection_hook ;
|
|
|
|
if active_connections pool < pool.config.min_connections then begin
|
|
|
|
Lwt_condition.broadcast pool.events.too_few_connections () ;
|
|
|
|
log pool Too_few_connections ;
|
|
|
|
end ;
|
|
|
|
conn
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
and disconnect ?(wait = false) conn =
|
|
|
|
conn.wait_close <- wait ;
|
|
|
|
Answerer.shutdown (Lazy.force conn.answerer)
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
and register_new_points pool conn =
|
2018-02-08 13:51:00 +04:00
|
|
|
let source_peer_id = P2p_peer_state.Info.peer_id conn.peer_info in
|
2017-03-14 13:51:44 +04:00
|
|
|
fun points ->
|
|
|
|
List.iter (register_new_point pool source_peer_id) points ;
|
|
|
|
Lwt.return_unit
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
and register_new_point pool _source_peer_id point =
|
2018-01-24 15:48:25 +04:00
|
|
|
if not (P2p_point.Table.mem pool.my_id_points point) then
|
2017-03-14 13:51:44 +04:00
|
|
|
ignore (register_point pool _source_peer_id point)
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
and list_known_points pool _conn () =
|
|
|
|
let knowns =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.fold
|
2017-03-14 13:51:44 +04:00
|
|
|
(fun _ point_info acc -> point_info :: acc)
|
|
|
|
pool.known_points [] in
|
|
|
|
let best_knowns =
|
2017-11-27 09:13:12 +04:00
|
|
|
List.take_n ~compare:compare_known_point_info 50 knowns in
|
2018-02-08 13:51:00 +04:00
|
|
|
Lwt.return (List.map P2p_point_state.Info.point best_knowns)
|
2017-01-14 16:14:07 +04:00
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
and active_connections pool = P2p_peer.Table.length pool.connected_peer_ids
|
2017-03-14 13:51:44 +04:00
|
|
|
|
|
|
|
and swap_request pool conn new_point _new_peer_id =
|
2018-02-08 13:51:00 +04:00
|
|
|
let source_peer_id = P2p_peer_state.Info.peer_id conn.peer_info in
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Swap_request_received { source = source_peer_id }) ;
|
|
|
|
lwt_log_info
|
2018-01-24 15:48:25 +04:00
|
|
|
"Swap request received from %a" P2p_peer.Id.pp source_peer_id >>= fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
(* Ignore if already connected to peer or already swapped less
|
|
|
|
than <swap_linger> seconds ago. *)
|
|
|
|
let now = Time.now () in
|
|
|
|
let span_since_last_swap =
|
|
|
|
Int64.to_int @@
|
|
|
|
Time.diff now
|
|
|
|
(Time.max pool.latest_succesfull_swap pool.latest_accepted_swap) in
|
|
|
|
let new_point_info = register_point pool source_peer_id new_point in
|
|
|
|
if span_since_last_swap < int_of_float pool.config.swap_linger
|
2018-02-08 13:51:00 +04:00
|
|
|
|| not (P2p_point_state.is_disconnected new_point_info) then begin
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Swap_request_ignored { source = source_peer_id }) ;
|
2018-01-24 15:48:25 +04:00
|
|
|
lwt_log_info "Ignoring swap request from %a" P2p_peer.Id.pp source_peer_id
|
2017-03-14 13:51:44 +04:00
|
|
|
end else begin
|
|
|
|
match Connection.random_lowid pool with
|
|
|
|
| None ->
|
|
|
|
lwt_log_info
|
2018-01-24 15:48:25 +04:00
|
|
|
"No swap candidate for %a" P2p_peer.Id.pp source_peer_id
|
2017-03-14 13:51:44 +04:00
|
|
|
| Some (proposed_point, proposed_peer_id, _proposed_conn) ->
|
2018-01-24 15:48:25 +04:00
|
|
|
match P2p_socket.write_now
|
2017-03-14 13:51:44 +04:00
|
|
|
conn.conn (Swap_ack (proposed_point, proposed_peer_id)) with
|
|
|
|
| Ok true ->
|
|
|
|
log pool (Swap_ack_sent { source = source_peer_id }) ;
|
|
|
|
swap pool conn proposed_peer_id new_point >>= fun () ->
|
|
|
|
Lwt.return_unit
|
|
|
|
| Ok false ->
|
|
|
|
log pool (Swap_request_received { source = source_peer_id }) ;
|
|
|
|
Lwt.return_unit
|
|
|
|
| Error _ ->
|
|
|
|
log pool (Swap_request_received { source = source_peer_id }) ;
|
|
|
|
Lwt.return_unit
|
2017-01-14 16:14:07 +04:00
|
|
|
end
|
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
and swap_ack pool conn new_point _new_peer_id =
|
2018-02-08 13:51:00 +04:00
|
|
|
let source_peer_id = P2p_peer_state.Info.peer_id conn.peer_info in
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Swap_ack_received { source = source_peer_id }) ;
|
|
|
|
lwt_log_info
|
2018-01-24 15:48:25 +04:00
|
|
|
"Swap ack received from %a" P2p_peer.Id.pp source_peer_id >>= fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
match conn.last_sent_swap_request with
|
|
|
|
| None -> Lwt.return_unit (* ignore *)
|
|
|
|
| Some (_time, proposed_peer_id) ->
|
|
|
|
match Connection.find_by_peer_id pool proposed_peer_id with
|
|
|
|
| None ->
|
|
|
|
swap pool conn proposed_peer_id new_point >>= fun () ->
|
|
|
|
Lwt.return_unit
|
|
|
|
| Some _ ->
|
|
|
|
Lwt.return_unit
|
|
|
|
|
|
|
|
and swap pool conn current_peer_id new_point =
|
2018-02-08 13:51:00 +04:00
|
|
|
let source_peer_id = P2p_peer_state.Info.peer_id conn.peer_info in
|
2017-03-14 13:51:44 +04:00
|
|
|
pool.latest_accepted_swap <- Time.now () ;
|
2018-02-20 21:28:05 +04:00
|
|
|
connect pool new_point >>= function
|
2017-03-14 13:51:44 +04:00
|
|
|
| Ok _new_conn -> begin
|
|
|
|
pool.latest_succesfull_swap <- Time.now () ;
|
|
|
|
log pool (Swap_success { source = source_peer_id }) ;
|
2018-01-24 15:48:25 +04:00
|
|
|
lwt_log_info "Swap to %a succeeded" P2p_point.Id.pp new_point >>= fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
match Connection.find_by_peer_id pool current_peer_id with
|
|
|
|
| None -> Lwt.return_unit
|
|
|
|
| Some conn ->
|
|
|
|
disconnect conn >>= fun () ->
|
|
|
|
Lwt.return_unit
|
|
|
|
end
|
|
|
|
| Error err -> begin
|
|
|
|
pool.latest_accepted_swap <- pool.latest_succesfull_swap ;
|
|
|
|
log pool (Swap_failure { source = source_peer_id }) ;
|
2017-11-08 17:49:54 +04:00
|
|
|
match err with
|
2018-02-08 13:51:01 +04:00
|
|
|
| [ Timeout ] ->
|
2017-11-08 17:49:54 +04:00
|
|
|
lwt_debug "Swap to %a was interupted: %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp new_point pp_print_error err
|
2017-11-08 17:49:54 +04:00
|
|
|
| _ ->
|
|
|
|
lwt_log_error "Swap to %a failed: %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Id.pp new_point pp_print_error err
|
2017-03-14 13:51:44 +04:00
|
|
|
end
|
|
|
|
|
2017-01-14 16:14:07 +04:00
|
|
|
let accept pool fd point =
|
2017-03-14 13:51:44 +04:00
|
|
|
log pool (Incoming_connection point) ;
|
2018-01-24 15:48:25 +04:00
|
|
|
if pool.config.max_incoming_connections <= P2p_point.Table.length pool.incoming
|
2018-02-22 18:35:50 +04:00
|
|
|
|| pool.config.max_connections <= active_connections pool
|
|
|
|
(* silently ignore banned points *)
|
2018-03-11 18:02:59 +04:00
|
|
|
|| (P2p_acl.banned_addr pool.acl (fst point)) then
|
2018-02-08 13:51:01 +04:00
|
|
|
Lwt.async (fun () -> Lwt_utils_unix.safe_close fd)
|
2017-01-14 16:14:07 +04:00
|
|
|
else
|
2017-11-27 09:13:12 +04:00
|
|
|
let canceler = Lwt_canceler.create () in
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.add pool.incoming point canceler ;
|
2017-01-14 16:14:07 +04:00
|
|
|
Lwt.async begin fun () ->
|
2018-02-08 13:51:01 +04:00
|
|
|
with_timeout
|
2018-02-20 21:28:05 +04:00
|
|
|
~canceler (Lwt_unix.sleep pool.config.authentication_timeout)
|
2017-01-14 16:14:07 +04:00
|
|
|
(fun canceler -> authenticate pool canceler fd point)
|
|
|
|
end
|
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
let send_swap_request pool =
|
|
|
|
match Connection.random pool with
|
|
|
|
| None -> ()
|
|
|
|
| Some recipient ->
|
|
|
|
let recipient_peer_id = (Connection.info recipient).peer_id in
|
|
|
|
match Connection.random_lowid ~different_than:recipient pool with
|
|
|
|
| None -> ()
|
|
|
|
| Some (proposed_point, proposed_peer_id, _proposed_conn) ->
|
|
|
|
log pool (Swap_request_sent { source = recipient_peer_id }) ;
|
|
|
|
recipient.last_sent_swap_request <-
|
|
|
|
Some (Time.now (), proposed_peer_id) ;
|
2018-01-24 15:48:25 +04:00
|
|
|
ignore (P2p_socket.write_now recipient.conn
|
2017-03-14 13:51:44 +04:00
|
|
|
(Swap_request (proposed_point, proposed_peer_id)))
|
2017-01-14 16:14:07 +04:00
|
|
|
|
|
|
|
(***************************************************************************)
|
|
|
|
|
|
|
|
let create config meta_config message_config io_sched =
|
|
|
|
let events = {
|
|
|
|
too_few_connections = Lwt_condition.create () ;
|
|
|
|
too_many_connections = Lwt_condition.create () ;
|
2017-02-17 22:12:06 +04:00
|
|
|
new_peer = Lwt_condition.create () ;
|
2017-02-13 17:52:16 +04:00
|
|
|
new_connection = Lwt_condition.create () ;
|
2017-01-14 16:14:07 +04:00
|
|
|
} in
|
|
|
|
let pool = {
|
|
|
|
config ; meta_config ; message_config ;
|
2018-01-24 15:48:25 +04:00
|
|
|
my_id_points = P2p_point.Table.create 7 ;
|
|
|
|
known_peer_ids = P2p_peer.Table.create 53 ;
|
|
|
|
connected_peer_ids = P2p_peer.Table.create 53 ;
|
|
|
|
known_points = P2p_point.Table.create 53 ;
|
|
|
|
connected_points = P2p_point.Table.create 53 ;
|
|
|
|
incoming = P2p_point.Table.create 53 ;
|
2017-01-14 16:14:07 +04:00
|
|
|
io_sched ;
|
|
|
|
encoding = Message.encoding message_config.encoding ;
|
|
|
|
events ;
|
2017-11-27 09:13:12 +04:00
|
|
|
watcher = Lwt_watcher.create_input () ;
|
2018-02-22 18:35:50 +04:00
|
|
|
acl = P2p_acl.create 1023;
|
2017-02-24 20:17:53 +04:00
|
|
|
new_connection_hook = [] ;
|
2017-03-14 13:51:44 +04:00
|
|
|
latest_accepted_swap = Time.epoch ;
|
|
|
|
latest_succesfull_swap = Time.epoch ;
|
2017-01-14 16:14:07 +04:00
|
|
|
} in
|
|
|
|
List.iter (Points.set_trusted pool) config.trusted_points ;
|
2018-02-08 13:51:00 +04:00
|
|
|
P2p_peer_state.Info.File.load config.peers_file meta_config.encoding >>= function
|
2017-02-24 06:50:33 +04:00
|
|
|
| Ok peer_ids ->
|
2017-01-23 14:09:45 +04:00
|
|
|
List.iter
|
2017-03-14 13:51:44 +04:00
|
|
|
(fun peer_info ->
|
2018-02-08 13:51:00 +04:00
|
|
|
let peer_id = P2p_peer_state.Info.peer_id peer_info in
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.add pool.known_peer_ids peer_id peer_info)
|
2017-02-24 06:50:33 +04:00
|
|
|
peer_ids ;
|
2017-01-23 14:09:45 +04:00
|
|
|
Lwt.return pool
|
|
|
|
| Error err ->
|
|
|
|
log_error "@[Failed to parsed peers file:@ %a@]"
|
|
|
|
pp_print_error err ;
|
|
|
|
Lwt.return pool
|
2017-01-14 16:14:07 +04:00
|
|
|
|
|
|
|
let destroy pool =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.fold (fun _point point_info acc ->
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_point_state.get point_info with
|
2017-01-14 16:14:07 +04:00
|
|
|
| Requested { cancel } | Accepted { cancel } ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel cancel >>= fun () -> acc
|
2017-01-14 16:14:07 +04:00
|
|
|
| Running { data = conn } ->
|
|
|
|
disconnect conn >>= fun () -> acc
|
|
|
|
| Disconnected -> acc)
|
|
|
|
pool.known_points @@
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Table.fold (fun _peer_id peer_info acc ->
|
2018-02-08 13:51:00 +04:00
|
|
|
match P2p_peer_state.get peer_info with
|
2017-01-14 16:14:07 +04:00
|
|
|
| Accepted { cancel } ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel cancel >>= fun () -> acc
|
2017-01-14 16:14:07 +04:00
|
|
|
| Running { data = conn } ->
|
|
|
|
disconnect conn >>= fun () -> acc
|
|
|
|
| Disconnected -> acc)
|
2017-02-24 06:50:33 +04:00
|
|
|
pool.known_peer_ids @@
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_point.Table.fold (fun _point canceler acc ->
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel canceler >>= fun () -> acc)
|
2017-01-14 16:14:07 +04:00
|
|
|
pool.incoming Lwt.return_unit
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let on_new_connection pool f =
|
|
|
|
pool.new_connection_hook <- f :: pool.new_connection_hook
|