package tezos-base
Tezos: meta-package and pervasive type definitions for Tezos
Install
Dune Dependency
Authors
Maintainers
Sources
tezos-17.3.tar.gz
sha256=7062cd57addd452852598a2214ade393130efa087b99068d53713bdf912b3680
sha512=08e4091144a03ce3c107fb91a66501bd8b65ca3278917c455a2eaac6df3e108ade63f6ab8340a4bb152d60f404326e464d0ec95d26cafe8e82f870465d24a5fc
doc/src/tezos-base.unix/socket.ml.html
Source file socket.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2020-2021 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. *) (* *) (*****************************************************************************) open Error_monad type addr = | Unix of string | Tcp of string * string * Unix.getaddrinfo_option list let handle_literal_ipv6 host = (* To strip '[' and ']' when a literal IPv6 is provided *) match Ipaddr.of_string host with | Error (`Msg _) -> host | Ok ipaddr -> Ipaddr.to_string ipaddr let connect ?(timeout = !Lwt_utils_unix.default_net_timeout) = let open Lwt_result_syntax in function | Unix path -> let addr = Lwt_unix.ADDR_UNIX path in let sock = Lwt_unix.socket PF_UNIX SOCK_STREAM 0 in Lwt_unix.set_close_on_exec sock ; let*! () = Lwt_unix.connect sock addr in return sock | Tcp (host, service, opts) -> ( let host = handle_literal_ipv6 host in let*! addrs = Lwt_unix.getaddrinfo host service opts in match addrs with | [] -> failwith "could not resolve host '%s'" host | addrs -> let rec try_connect acc = function | [] -> Lwt.return (Error (let err = error_of_fmt "could not connect to '%s'" host in match acc with | None -> TzTrace.make err | Some tr -> TzTrace.cons err tr)) | {Unix.ai_family; ai_socktype; ai_protocol; ai_addr; _} :: addrs -> ( let sock = Lwt_unix.socket ai_family ai_socktype ai_protocol in Lwt_unix.set_close_on_exec sock ; let*! r = protect ~on_error:(fun e -> let*! () = Lwt_unix.close sock in Lwt.return_error e) (fun () -> Lwt_unix.with_timeout (Ptime.Span.to_float_s timeout) (fun () -> let*! () = Lwt_unix.connect sock ai_addr in return sock)) in match r with | Ok sock -> return sock | Error (e : error trace) -> let acc = match acc with | None -> Some e | Some tr -> Some (TzTrace.conp e tr) in try_connect acc addrs) in try_connect None addrs) let with_connection ?timeout addr f = let open Lwt_result_syntax in let* conn = connect ?timeout addr in protect (fun () -> let* a = f conn in let* () = Lwt_utils_unix.safe_close conn in return a) ~on_error:(fun e -> let* () = Lwt_utils_unix.safe_close conn in Lwt.return (Error e)) let bind ?(backlog = 10) = let open Lwt_syntax in function | Unix path -> let addr = Lwt_unix.ADDR_UNIX path in let sock = Lwt_unix.socket PF_UNIX SOCK_STREAM 0 in Lwt_unix.set_close_on_exec sock ; let* () = Lwt_unix.bind sock addr in Lwt_unix.listen sock backlog ; return_ok [sock] | Tcp (host, service, opts) -> ( let* addrs = Lwt_unix.getaddrinfo (handle_literal_ipv6 host) service (AI_PASSIVE :: opts) in match addrs with | [] -> failwith "could not resolve host '%s'" host | addrs -> let do_bind {Unix.ai_family; ai_socktype; ai_protocol; ai_addr; _} = let sock = Lwt_unix.socket ai_family ai_socktype ai_protocol in Lwt_unix.set_close_on_exec sock ; Lwt_unix.setsockopt sock SO_REUSEADDR true ; let* () = Lwt_unix.bind sock ai_addr in Lwt_unix.listen sock backlog ; return_ok sock in Tezos_error_monad.TzLwtreslib.List.map_es do_bind addrs) (* To get the encoding/decoding errors into scope. *) open Data_encoding_wrapper (* length information is encoded as an [int16] which has a size of [2] bytes *) let size_of_length_of_message_payload = 2 (* some messages may be too long for their length to be encoded *) let maximum_length_of_message_payload = (* or [0b1111_1111_1111_1111] *) 1 lsl (size_of_length_of_message_payload * 8) let send fd encoding message = let open Lwt_result_syntax in let length_of_message_payload = Data_encoding.Binary.length encoding message in assert (length_of_message_payload >= 0) ; let* () = fail_unless (length_of_message_payload < maximum_length_of_message_payload) Unexpected_size_of_encoded_value in let total_length_of_message = size_of_length_of_message_payload + length_of_message_payload in let message_serialisation_buffer = Bytes.create total_length_of_message in let serialisation_state = Data_encoding.Binary.make_writer_state message_serialisation_buffer ~offset:size_of_length_of_message_payload ~allowed_bytes:length_of_message_payload in (* By construction, the length of the serialisation buffer is the state's offset + the state's allowed_length. As a result, we are within the range of valid parameter for [make_writer_state]. *) assert (Option.is_some serialisation_state) ; let serialisation_state = Stdlib.Option.get serialisation_state in match Data_encoding.Binary.write encoding message serialisation_state with | Error we -> tzfail (Encoding_error we) | Ok last -> let* () = fail_unless (last = total_length_of_message) Unexpected_size_of_encoded_value in (* we set the beginning of the buf with the length of what is next *) Tezos_stdlib.TzEndian.set_int16 message_serialisation_buffer 0 length_of_message_payload ; protect (fun () -> Lwt_result.ok @@ Lwt_utils_unix.write_bytes fd message_serialisation_buffer) let recv ?timeout fd encoding = let open Lwt_result_syntax in let header_buf = Bytes.create size_of_length_of_message_payload in let* () = protect (fun () -> Lwt_result.ok @@ Lwt_utils_unix.read_bytes_with_timeout ?timeout ~len:size_of_length_of_message_payload fd header_buf) in let len = Tezos_stdlib.TzEndian.get_uint16 header_buf 0 in let buf = Bytes.create len in let* () = protect (fun () -> Lwt_result.ok @@ Lwt_utils_unix.read_bytes_with_timeout ?timeout ~len fd buf) in let buf = Bytes.unsafe_to_string buf in match Data_encoding.Binary.read encoding buf 0 len with | Error re -> tzfail (Decoding_error re) | Ok (read_len, message) -> if read_len <> len then tzfail (Decoding_error Extra_bytes) else return message
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>