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 Error_monad
type addr =
| Unix of string
| Tcp of string * string * Unix.getaddrinfo_option list
let handle_literal_ipv6 host =
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)
open Data_encoding_wrapper
let size_of_length_of_message_payload = 2
let maximum_length_of_message_payload =
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
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
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 = 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