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
210
211
212
open Lwt.Infix
type t =
Plain
| SSL of Ssl.socket
type socket = Lwt_unix.file_descr * t
type uninitialized_socket = Lwt_unix.file_descr * Ssl.socket
let ssl_socket (_fd, kind) =
match kind with
| Plain -> None
| SSL socket -> Some socket
let ssl_socket_of_uninitialized_socket (_fd, socket) = socket
let is_ssl s =
match snd s with
Plain -> false
| SSL _ -> true
let wrap_call f () =
try
f ()
with
(Ssl.Connection_error err | Ssl.Accept_error err |
Ssl.Read_error err | Ssl.Write_error err) as e ->
(match err with
Ssl.Error_want_read ->
raise Lwt_unix.Retry_read
| Ssl.Error_want_write ->
raise Lwt_unix.Retry_write
| _ ->
raise e) [@ocaml.warning "-4"]
let repeat_call fd f =
Lwt_unix.blocking fd >>= fun _blocking ->
try
Lwt.return (wrap_call f ())
with
Lwt_unix.Retry_read ->
Lwt_unix.register_action Lwt_unix.Read fd (wrap_call f)
| Lwt_unix.Retry_write ->
Lwt_unix.register_action Lwt_unix.Write fd (wrap_call f)
| e ->
Lwt.fail e
let plain fd = (fd, Plain)
let embed_socket fd context = (fd, SSL(Ssl.embed_socket (Lwt_unix.unix_file_descr fd) context))
let embed_uninitialized_socket fd context = (fd, Ssl.embed_socket (Lwt_unix.unix_file_descr fd) context)
let ssl_accept fd ctx =
let socket = Ssl.embed_socket (Lwt_unix.unix_file_descr fd) ctx in
Lwt.bind
(repeat_call fd (fun () -> Ssl.accept socket)) (fun () ->
Lwt.return (fd, SSL socket))
let ssl_connect fd ctx =
let socket = Ssl.embed_socket (Lwt_unix.unix_file_descr fd) ctx in
Lwt.bind
(repeat_call fd (fun () -> Ssl.connect socket)) (fun () ->
Lwt.return (fd, SSL socket))
let ssl_accept_handshake (fd, socket) =
Lwt.bind
(repeat_call fd (fun () -> Ssl.accept socket)) (fun () ->
Lwt.return (fd, SSL socket))
let ssl_perform_handshake (fd, socket) =
Lwt.bind
(repeat_call fd (fun () -> Ssl.connect socket)) (fun () ->
Lwt.return (fd, SSL socket))
let read (fd, s) buf pos len =
match s with
| Plain ->
Lwt_unix.read fd buf pos len
| SSL s ->
if len = 0 then
Lwt.return 0
else
repeat_call fd
(fun () ->
try
Ssl.read s buf pos len
with Ssl.Read_error Ssl.Error_zero_return ->
0)
let read_bytes (fd, s) buf pos len =
match s with
| Plain ->
Lwt_bytes.read fd buf pos len
| SSL s ->
if len = 0 then
Lwt.return 0
else
repeat_call fd
(fun () ->
try
Ssl.read_into_bigarray s buf pos len
with Ssl.Read_error Ssl.Error_zero_return ->
0)
let write (fd, s) buf pos len =
match s with
| Plain ->
Lwt_unix.write fd buf pos len
| SSL s ->
if len = 0 then
Lwt.return 0
else
repeat_call fd
(fun () ->
Ssl.write s buf pos len)
let write_bytes (fd, s) buf pos len =
match s with
| Plain ->
Lwt_bytes.write fd buf pos len
| SSL s ->
if len = 0 then
Lwt.return 0
else
repeat_call fd
(fun () -> Ssl.write_bigarray s buf pos len)
let wait_read (fd, s) =
match s with
Plain -> Lwt_unix.wait_read fd
| SSL _ -> Lwt.pause ()
let wait_write (fd, s) =
match s with
Plain -> Lwt_unix.wait_write fd
| SSL _ -> Lwt.pause ()
let ssl_shutdown (fd, s) =
match s with
Plain -> Lwt.return_unit
| SSL s -> repeat_call fd (fun () -> Ssl.shutdown s)
let shutdown (fd, _) cmd = Lwt_unix.shutdown fd cmd
let close_notify = function
| (_, Plain) as s ->
shutdown s Unix.SHUTDOWN_SEND;
Lwt.return_true
| (fd, SSL s) ->
repeat_call fd (fun () -> Ssl.close_notify s)
let close (fd, _) = Lwt_unix.close fd
let abort (fd, _) = Lwt_unix.abort fd
let shutdown_and_close s =
ssl_shutdown s >>= fun () ->
Lwt.wrap2 shutdown s Unix.SHUTDOWN_ALL >>= fun () ->
close s
let out_channel_of_descr ?buffer s =
Lwt_io.make
?buffer
~mode:Lwt_io.output
~close:(fun () -> shutdown_and_close s)
(fun buf pos len -> write_bytes s buf pos len)
let in_channel_of_descr ?buffer s =
Lwt_io.make
?buffer
~mode:Lwt_io.input
~close:(fun () -> shutdown_and_close s)
(fun buf pos len -> read_bytes s buf pos len)
let get_fd (fd, _socket) = fd
let get_unix_fd (fd,socket) =
match socket with
| Plain -> Lwt_unix.unix_file_descr fd
| SSL socket -> (Ssl.file_descr_of_socket socket)
let getsockname s =
Unix.getsockname (get_unix_fd s)
let getpeername s =
Unix.getpeername (get_unix_fd s)