File tree Expand file tree Collapse file tree 9 files changed +36
-21
lines changed Expand file tree Collapse file tree 9 files changed +36
-21
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ module Impl = struct
36
36
Switch. on_release sw (fun () -> Eio.Resource. close socket);
37
37
socket
38
38
39
- let connect t ~sw addr =
39
+ let connect t ~sw ? bind : _ addr =
40
40
traceln " %s: connect to %a" t.label Eio.Net.Sockaddr. pp addr;
41
41
let socket = Handler. run t.on_connect in
42
42
Switch. on_release sw (fun () -> Eio.Flow. close socket);
Original file line number Diff line number Diff line change @@ -235,7 +235,7 @@ module Pi = struct
235
235
type tag
236
236
237
237
val listen : t -> reuse_addr :bool -> reuse_port :bool -> backlog :int -> sw :Switch .t -> Sockaddr .stream -> tag listening_socket_ty r
238
- val connect : t -> sw :Switch .t -> Sockaddr .stream -> tag stream_socket_ty r
238
+ val connect : t -> sw :Switch .t -> ? bind : Sockaddr .stream -> Sockaddr .stream -> tag stream_socket_ty r
239
239
val datagram_socket :
240
240
t
241
241
-> reuse_addr :bool
@@ -295,10 +295,10 @@ let listen (type tag) ?(reuse_addr=false) ?(reuse_port=false) ~backlog ~sw (t:[>
295
295
let module X = (val (Resource. get ops Pi. Network )) in
296
296
X. listen t ~reuse_addr ~reuse_port ~backlog ~sw
297
297
298
- let connect (type tag ) ~sw (t :[> tag ty] r ) addr =
298
+ let connect (type tag ) ~sw ? bind (t :[> tag ty] r ) addr =
299
299
let (Resource. T (t, ops)) = t in
300
300
let module X = (val (Resource. get ops Pi. Network )) in
301
- try X. connect t ~sw addr
301
+ try X. connect t ~sw ?bind addr
302
302
with Exn. Io _ as ex ->
303
303
let bt = Printexc. get_raw_backtrace () in
304
304
Exn. reraise_with_context ex bt " connecting to %a" Sockaddr. pp addr
Original file line number Diff line number Diff line change @@ -129,10 +129,12 @@ type 'a t = 'a r
129
129
130
130
(* * {2 Out-bound Connections} *)
131
131
132
- val connect : sw :Switch .t -> [> 'tag ty ] t -> Sockaddr .stream -> 'tag stream_socket_ty r
132
+ val connect : sw :Switch .t -> ? bind : Sockaddr .stream -> [> 'tag ty ] t -> Sockaddr .stream -> 'tag stream_socket_ty r
133
133
(* * [connect ~sw t addr] is a new socket connected to remote address [addr].
134
134
135
- The new socket will be closed when [sw] finishes, unless closed manually first. *)
135
+ The new socket will be closed when [sw] finishes, unless closed manually first.
136
+
137
+ @param bind Set the outbound client address. *)
136
138
137
139
val with_tcp_connect :
138
140
?timeout : Time.Timeout .t ->
@@ -346,7 +348,7 @@ module Pi : sig
346
348
t -> reuse_addr :bool -> reuse_port :bool -> backlog :int -> sw :Switch .t ->
347
349
Sockaddr .stream -> tag listening_socket_ty r
348
350
349
- val connect : t -> sw :Switch .t -> Sockaddr .stream -> tag stream_socket_ty r
351
+ val connect : t -> sw :Switch .t -> ? bind : Sockaddr .stream -> Sockaddr .stream -> tag stream_socket_ty r
350
352
351
353
val datagram_socket :
352
354
t
Original file line number Diff line number Diff line change @@ -256,11 +256,12 @@ let socket_domain_of = function
256
256
~v4: (fun _ -> Unix. PF_INET )
257
257
~v6: (fun _ -> Unix. PF_INET6 )
258
258
259
- let connect ~sw connect_addr =
259
+ let connect ~sw ? bind connect_addr =
260
260
let addr = Eio_unix.Net. sockaddr_to_unix connect_addr in
261
261
let sock_unix = Unix. socket ~cloexec: true (socket_domain_of connect_addr) Unix. SOCK_STREAM 0 in
262
262
let sock = Fd. of_unix ~sw ~seekable: false ~close_unix: true sock_unix in
263
- Low_level. connect sock addr;
263
+ let bind = Option. map Eio_unix.Net. sockaddr_to_unix bind in
264
+ Low_level. connect sock ?bind addr;
264
265
(flow sock :> _ Eio_unix.Net.stream_socket )
265
266
266
267
module Impl = struct
@@ -296,7 +297,8 @@ module Impl = struct
296
297
Unix. listen sock_unix backlog;
297
298
(listening_socket sock :> _ Eio.Net.listening_socket_ty r )
298
299
299
- let connect () ~sw addr = (connect ~sw addr :> [`Generic | `Unix ] Eio.Net. stream_socket_ty r)
300
+ let connect () ~sw ?bind addr =
301
+ (connect ~sw ?bind addr :> [`Generic | `Unix ] Eio.Net. stream_socket_ty r)
300
302
301
303
let datagram_socket () ~reuse_addr ~reuse_port ~sw saddr =
302
304
if reuse_addr then (
Original file line number Diff line number Diff line change @@ -221,8 +221,15 @@ let splice src ~dst ~len =
221
221
else if res = 0 then raise End_of_file
222
222
else raise @@ Err. wrap (Uring. error_of_errno res) " splice" " "
223
223
224
- let connect fd addr =
224
+ let try_bind fd = function
225
+ | None -> ()
226
+ | Some bind_addr ->
227
+ try Unix. bind fd bind_addr
228
+ with Unix. Unix_error (code , name , arg ) -> raise @@ Err. wrap_fs code name arg
229
+
230
+ let connect fd ?bind addr =
225
231
Fd. use_exn " connect" fd @@ fun fd ->
232
+ try_bind fd bind;
226
233
let res = Sched. enter " connect" (enqueue_connect fd addr) in
227
234
if res < 0 then (
228
235
let ex =
Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ val splice : fd -> dst:fd -> len:int -> int
111
111
@raise End_of_file [src] is at the end of the file.
112
112
@raise Unix.Unix_error(EINVAL, "splice", _) if splice is not supported for these FDs. *)
113
113
114
- val connect : fd -> Unix .sockaddr -> unit
114
+ val connect : fd -> ? bind : Unix .sockaddr -> Unix .sockaddr -> unit
115
115
(* * [connect fd addr] attempts to connect socket [fd] to [addr]. *)
116
116
117
117
val await_readable : fd -> unit
Original file line number Diff line number Diff line change @@ -67,15 +67,18 @@ let socket ~sw socket_domain socket_type protocol =
67
67
Unix. set_nonblock sock_unix;
68
68
Fd. of_unix ~sw ~blocking: false ~close_unix: true sock_unix
69
69
70
- let connect fd addr =
70
+ let connect fd ? bind addr =
71
71
try
72
- Fd. use_exn " connect" fd (fun fd -> Unix. connect fd addr)
72
+ Fd. use_exn " connect" fd @@ fun fd ->
73
+ Option. iter (Unix. bind fd) bind;
74
+ Unix. connect fd addr
73
75
with
74
76
| Unix. Unix_error ((EINTR | EAGAIN | EWOULDBLOCK | EINPROGRESS ), _ , _ ) ->
75
77
await_writable " connect" fd;
76
- match Fd. use_exn " connect" fd Unix. getsockopt_error with
78
+ ( match Fd. use_exn " connect" fd Unix. getsockopt_error with
77
79
| None -> ()
78
- | Some code -> raise (Err. wrap code " connect-in-progress" " " )
80
+ | Some code -> raise (Err. wrap code " connect-in-progress" " " ))
81
+ | Unix. Unix_error (code , name , arg ) -> raise (Err. wrap code name arg)
79
82
80
83
let accept ~sw sock =
81
84
Fd. use_exn " accept" sock @@ fun sock ->
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ val read : fd -> bytes -> int -> int -> int
28
28
val write : fd -> bytes -> int -> int -> int
29
29
30
30
val socket : sw :Switch .t -> Unix .socket_domain -> Unix .socket_type -> int -> fd
31
- val connect : fd -> Unix .sockaddr -> unit
31
+ val connect : fd -> ? bind : Unix .sockaddr -> Unix .sockaddr -> unit
32
32
val accept : sw :Switch .t -> fd -> fd * Unix .sockaddr
33
33
34
34
val shutdown : fd -> Unix .shutdown_command -> unit
Original file line number Diff line number Diff line change @@ -138,7 +138,7 @@ let listen ~reuse_addr ~reuse_port ~backlog ~sw (listen_addr : Eio.Net.Sockaddr.
138
138
);
139
139
(listening_socket ~hook sock :> _ Eio.Net. listening_socket_ty r)
140
140
141
- let connect ~sw connect_addr =
141
+ let connect ~sw ? bind connect_addr =
142
142
let socket_type, addr =
143
143
match connect_addr with
144
144
| `Unix path -> Unix. SOCK_STREAM , Unix. ADDR_UNIX path
@@ -147,8 +147,9 @@ let connect ~sw connect_addr =
147
147
Unix. SOCK_STREAM , Unix. ADDR_INET (host, port)
148
148
in
149
149
let sock = Low_level. socket ~sw (socket_domain_of connect_addr) socket_type 0 in
150
+ let bind = Option. map Eio_unix.Net. sockaddr_to_unix bind in
150
151
try
151
- Low_level. connect sock addr;
152
+ Low_level. connect sock ?bind addr;
152
153
(Flow. of_fd sock :> _ Eio_unix.Net.stream_socket )
153
154
with Unix. Unix_error (code , name , arg ) -> raise (Err. wrap code name arg)
154
155
@@ -174,8 +175,8 @@ module Impl = struct
174
175
175
176
let listen () = listen
176
177
177
- let connect () ~sw addr =
178
- let socket = connect ~sw addr in
178
+ let connect () ~sw ? bind addr =
179
+ let socket = connect ~sw ?bind addr in
179
180
(socket :> [`Generic | `Unix] Eio.Net.stream_socket_ty r )
180
181
181
182
let datagram_socket () ~reuse_addr ~reuse_port ~sw saddr =
You can’t perform that action at this time.
0 commit comments