Skip to content

Commit

Permalink
Merge pull request ocaml-multicore#261 from talex5/ipv6
Browse files Browse the repository at this point in the history
eio-linux: fix IPv6 support
  • Loading branch information
haesbaert authored Jul 25, 2022
2 parents 7d758fc + 68dcf4b commit 338d74b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib_eio_linux/eio_linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1019,11 +1019,18 @@ let listening_socket fd = object
flow, client_addr
end

let socket_domain_of = function
| `Unix _ -> Unix.PF_UNIX
| `Tcp (host, _) ->
Eio.Net.Ipaddr.fold host
~v4:(fun _ -> Unix.PF_INET)
~v6:(fun _ -> Unix.PF_INET6)

let net = object
inherit Eio.Net.t

method listen ~reuse_addr ~reuse_port ~backlog ~sw listen_addr =
let socket_domain, socket_type, addr =
let socket_type, addr =
match listen_addr with
| `Unix path ->
if reuse_addr then (
Expand All @@ -1032,12 +1039,12 @@ let net = object
| _ -> ()
| exception Unix.Unix_error (Unix.ENOENT, _, _) -> ()
);
Unix.PF_UNIX, Unix.SOCK_STREAM, Unix.ADDR_UNIX path
Unix.SOCK_STREAM, Unix.ADDR_UNIX path
| `Tcp (host, port) ->
let host = Eio_unix.Ipaddr.to_unix host in
Unix.PF_INET, Unix.SOCK_STREAM, Unix.ADDR_INET (host, port)
Unix.SOCK_STREAM, Unix.ADDR_INET (host, port)
in
let sock_unix = Unix.socket socket_domain socket_type 0 in
let sock_unix = Unix.socket (socket_domain_of listen_addr) socket_type 0 in
(* For Unix domain sockets, remove the path when done (except for abstract sockets). *)
begin match listen_addr with
| `Unix path ->
Expand All @@ -1054,15 +1061,15 @@ let net = object
Unix.listen sock_unix backlog;
listening_socket sock

method connect ~sw addr =
let socket_domain, socket_type, addr =
match addr with
| `Unix path -> Unix.PF_UNIX, Unix.SOCK_STREAM, Unix.ADDR_UNIX path
method connect ~sw connect_addr =
let socket_type, addr =
match connect_addr with
| `Unix path -> Unix.SOCK_STREAM, Unix.ADDR_UNIX path
| `Tcp (host, port) ->
let host = Eio_unix.Ipaddr.to_unix host in
Unix.PF_INET, Unix.SOCK_STREAM, Unix.ADDR_INET (host, port)
Unix.SOCK_STREAM, Unix.ADDR_INET (host, port)
in
let sock_unix = Unix.socket socket_domain socket_type 0 in
let sock_unix = Unix.socket (socket_domain_of connect_addr) socket_type 0 in
let sock = FD.of_unix ~sw ~seekable:false ~close_unix:true sock_unix in
Low_level.connect sock addr;
(flow sock :> <Eio.Flow.two_way; Eio.Flow.close>)
Expand Down
13 changes: 13 additions & 0 deletions tests/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let run (fn : net:Eio.Net.t -> Switch.t -> unit) =
Switch.run (fn ~net)
let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, 8081)
let addr6 = `Tcp (Eio.Net.Ipaddr.V6.loopback, 8081)
let read_all flow =
let b = Buffer.create 100 in
Expand Down Expand Up @@ -103,6 +104,18 @@ Handling one connection on an abstract Unix domain socket (this only works on Li
Exception: Graceful_shutdown.
```

Handling one connection using IPv6:

```ocaml
# run (test_address addr6);;
+Connecting to server...
+Server accepted connection from client
+Server received: "Hello from client"
+Client received: "Bye"
+Client finished - cancelling server
Exception: Graceful_shutdown.
```

Cancelling the read:

```ocaml
Expand Down

0 comments on commit 338d74b

Please sign in to comment.