12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2023 Nomadic Labs, <contact@nomadic-labs.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. *)(* *)(*****************************************************************************)typeerror+=Failed_to_parse_addressof(string*string)let()=(* Parsing of an address failed with an explanation *)register_error_kind`Permanent~id:"p2p_resolve.parsing_address_failed"~title:"Parsing of an address failed"~description:"Failed to parse the address given."~pp:(funppf(addr,explanation)->Format.fprintfppf"Failed to parse address '%s': %s@."addrexplanation)Data_encoding.(obj2(req"addr"string)(req"explanation"string))(functionFailed_to_parse_addresss->Somes|_->None)(funs->Failed_to_parse_addresss)letpeer_id_unused=letopenInternal_event.Simpleindeclare_1~section:["config"]~level:Warning~name:"config_peer_id_unused"~msg:"While parsing {string} a peer id was provided but will not be used."("string",Data_encoding.string)letno_points_found=letopenInternal_event.Simpleindeclare_1~section:["config"]~level:Warning~name:"config_no_points_found"~msg:"The DNS lookup of {string} returns 0 point."("string",Data_encoding.string)letresolve_addr_with_peer_id~default_addr~default_port?(passive=false)peer:(P2p_point.Id.t*P2p_peer.Id.toption)listtzresultLwt.t=letopenLwt_result_syntaxinmatchP2p_point.Id.parse_addr_port_idpeerwith|Errorerr->tzfail(Failed_to_parse_address(peer,P2p_point.Id.string_of_parsing_errorerr))|Ok{addr;port;peer_id}->letservice_port=match(port,default_port)with|Someport,_->port|None,default_port->default_portinletservice=string_of_intservice_portinletnode=ifaddr=""||addr="_"thendefault_addrelseaddrinlet*!l=Lwt_utils_unix.getaddrinfo~passive~node~serviceinlet*!()=ifList.is_emptylthenInternal_event.Simple.(emitno_points_foundpeer)elseLwt.return_unitinreturn(List.map(funpoint->(point,peer_id))l)letresolve_addr~default_addr~default_port?passivepeer:P2p_point.Id.tlisttzresultLwt.t=letopenLwt_result_syntaxinlet*l=resolve_addr_with_peer_id~default_addr~default_port?passivepeerinletpoints=List.mapfstlinlet*!()=ifList.exists(function_,Some_->true|_->false)lthenInternal_event.Simple.(emitpeer_id_unused)peerelseLwt.return_unitinreturnpoints