123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)moduleEvents=P2p_events.P2p_welcometypeconnect_handler=|Connect_handler:('msg,'meta,'meta_conn)P2p_connect_handler.t->connect_handlertypet={socket:Lwt_unix.file_descr;canceler:Lwt_canceler.t;connect_handler:connect_handler;mutableworker:unitLwt.t;}letrecworker_loopst=letopenLwt_syntaxinlet(Connect_handlerconnect_handler)=st.connect_handlerinlet*()=Lwt.pause()inlet*r=protect~canceler:st.canceler(fun()->let*r=P2p_fd.acceptst.socketinResult.fold~ok:(fun(fd,addr)->letpoint=matchaddrwith|Lwt_unix.ADDR_UNIX_->assertfalse|Lwt_unix.ADDR_INET(addr,port)->(Ipaddr_unix.V6.of_inet_addr_exnaddr,port)inP2p_connect_handler.acceptconnect_handlerfdpoint;Lwt.return_ok())~error:(function(* These are temporary system errors, giving some time for
the system to recover *)|`System_errorex->let*()=Events.(emitincoming_error)(TzTrace.make(error_of_exnex),"system")inlet*()=Lwt_unix.sleep5.inLwt.return_ok()(* These errors are specific to attempted incoming connection,
ignoring them. *)|`Socket_errorex->let*()=Events.(emitincoming_error)(TzTrace.make(error_of_exnex),"socket")inLwt.return(Ok())|`Unexpected_errorex->Lwt.return_error(TzTrace.make(error_of_exnex)))r)inmatchrwith|Ok()->worker_loopst|Error(Canceled::_)->Lwt.return_unit|Errorerr->Events.(emitunexpected_error)errletcreate?reuse_port?addr~backlogconnect_handlerport=letopenLwt_result_syntaxinLwt.catch(fun()->let*socket=P2p_fd.create_listening_socket?reuse_port~backlog?addrportinletcanceler=Lwt_canceler.create()inletst={socket;canceler;connect_handler=Connect_handlerconnect_handler;worker=Lwt.return_unit;}inLwt_canceler.on_cancelcanceler(fun()->let*!()=st.workerinlet*!r=Lwt_utils_unix.safe_closesocketinResult.iter_error_sEvents.(emitunexpected_error_closing_socket)r);returnst)(funexn->leterror=error_of_exnexninlet*!()=Events.(emitincoming_connection_error)errorintzfailerror)letactivatest=st.worker<-Lwt_utils.worker"welcome"~on_event:Internal_event.Lwt_worker_logger.on_event~run:(fun()->worker_loopst)~cancel:(fun()->Error_monad.cancel_with_exceptionsst.canceler)letshutdownst=Error_monad.cancel_with_exceptionsst.canceler