123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185(*****************************************************************************)(* *)(* 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_welcometypelistening_socket_open_failure={reason:Unix.error;address:P2p_addr.t;port:int;}typeerror+=Failed_to_open_listening_socketoflistening_socket_open_failurelet()=register_error_kind`Permanent~id:"p2p.welcome.failed_to_open_listening_socket"~title:"Failed to open listening socket"~description:"The p2p listening socket could not be opened."~pp:(funppf(reason,address,port)->lettipsppf()=matchreasonwith|Unix.EADDRINUSE->Format.fprintfppf"Another tezos node is probably running on this address.@;\
Please choose another P2P port using --net-addr."|_->Format.fprintfppf""inFormat.fprintfppf"@[<v 2>An error occured while initializing P2P server on this \
address: %a:%d.@;\
Reason: %s.@;\
%a@]"P2p_addr.ppaddressport(Unix.error_messagereason)tips())Data_encoding.(obj3(req"reason"Unix_error.encoding)(req"address"P2p_addr.encoding)(req"port"uint16))(function|Failed_to_open_listening_socket{reason;address;port}->Some(reason,address,port)|_->None)(fun(reason,address,port)->Failed_to_open_listening_socket{reason;address;port})typeconnect_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_listening_socket~backlog?(addr=Ipaddr.V6.unspecified)port=letopenLwt_result_syntaxinLwt.catch(fun()->letmain_socket=Lwt_unix.(socketPF_INET6SOCK_STREAM0)inLwt_unix.(setsockoptmain_socketSO_REUSEADDRtrue);let*!()=Lwt_unix.bindmain_socketUnix.(ADDR_INET(Ipaddr_unix.V6.to_inet_addraddr,port))inLwt_unix.listenmain_socketbacklog;returnmain_socket)(function|Unix.Unix_error(err,_,_)->tzfail(Failed_to_open_listening_socket{reason=err;address=addr;port})|exn->Lwt.failexn)letcreate?addr~backlogconnect_handlerport=letopenLwt_result_syntaxinLwt.catch(fun()->let*socket=create_listening_socket~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