Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ipv6 hosts #279

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions unix/capnp_rpc_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,10 @@ let handle_connection ?tags ~secret_key vat client =
)

let addr_of_host host =
match Unix.gethostbyname host with
| exception Not_found ->
Capnp_rpc.Debug.failf "Unknown host %S" host
| addr ->
if Array.length addr.Unix.h_addr_list = 0 then
Capnp_rpc.Debug.failf "No addresses found for host name %S" host
else
addr.Unix.h_addr_list.(0)
match Unix.getaddrinfo host "" [Unix.AI_SOCKTYPE Unix.SOCK_STREAM] with
| {ai_addr = ADDR_INET(addr, _) ; _} :: _ -> addr
| [] -> Capnp_rpc.Debug.failf "No addresses found for host name %S" host
| _ -> Capnp_rpc.Debug.failf "Unknown host %S" host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit odd to say "unknown host" where there are multiple matches.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this branch was only taken for Unix sockets.
I updated the match to make it explicit.
However, the message is maybe not so relevant anymore. I kept it as it was before, but maybe it deserves to be reworded a bit


let serve ?switch ?tags ?restore config =
let {Vat_config.backlog; secret_key = _; serve_tls; listen_address; public_address} = config in
Expand All @@ -197,11 +193,13 @@ let serve ?switch ?tags ?restore config =
Unix.bind socket (Unix.ADDR_UNIX path);
socket
| `TCP (host, port) ->
let socket = Unix.(socket PF_INET SOCK_STREAM 0) in
let addr = addr_of_host host in
let socket_domain = if Unix.is_inet6_addr addr then Unix.PF_INET6 else PF_INET in
let socket = Unix.(socket socket_domain SOCK_STREAM 0) in
Unix.setsockopt socket Unix.SO_REUSEADDR true;
Unix.setsockopt socket Unix.SO_KEEPALIVE true;
Keepalive.try_set_idle socket 60;
Unix.bind socket (Unix.ADDR_INET (addr_of_host host, port));
Unix.bind socket (Unix.ADDR_INET (addr, port));
socket
in
Unix.listen socket backlog;
Expand Down
20 changes: 9 additions & 11 deletions unix/network.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Location = struct
let tcp ~host ~port = `TCP (host, port)

let parse_tcp s =
match String.cut ~sep:":" s with
match String.cut ~rev:true ~sep:":" s with
| None -> Error (`Msg "Missing :PORT in listen address")
| Some (host, port) ->
match String.to_int port with
Expand Down Expand Up @@ -59,14 +59,10 @@ let error fmt =
let parse_third_party_cap_id _ = `Two_party_only

let addr_of_host host =
match Unix.gethostbyname host with
| exception Not_found ->
Capnp_rpc.Debug.failf "Unknown host %S" host
| addr ->
if Array.length addr.Unix.h_addr_list = 0 then
Capnp_rpc.Debug.failf "No addresses found for host name %S" host
else
addr.Unix.h_addr_list.(0)
match Unix.getaddrinfo host "" [Unix.AI_SOCKTYPE Unix.SOCK_STREAM] with
| {ai_addr = ADDR_INET(addr, _) ; _} :: _ -> addr
| [] -> Capnp_rpc.Debug.failf "No addresses found for host name %S" host
| _ -> Capnp_rpc.Debug.failf "Unknown host %S" host

let connect_socket = function
| `Unix path ->
Expand All @@ -77,12 +73,14 @@ let connect_socket = function
(fun ex -> Lwt_unix.close socket >>= fun () -> Lwt.fail ex)
| `TCP (host, port) ->
Log.info (fun f -> f "Connecting to %s:%d..." host port);
let socket = Lwt_unix.(socket PF_INET SOCK_STREAM 0) in
let addr = addr_of_host host in
let socket_domain = if Unix.is_inet6_addr addr then Unix.PF_INET6 else PF_INET in
let socket = Lwt_unix.(socket socket_domain SOCK_STREAM 0) in
Lwt.catch
(fun () ->
Lwt_unix.setsockopt socket Unix.SO_KEEPALIVE true;
Keepalive.try_set_idle (Lwt_unix.unix_file_descr socket) 60;
Lwt_unix.connect socket (Unix.ADDR_INET (addr_of_host host, port)) >|= fun () ->
Lwt_unix.connect socket (Unix.ADDR_INET (addr, port)) >|= fun () ->
socket
)
(fun ex -> Lwt_unix.close socket >>= fun () -> Lwt.fail ex)
Expand Down
Loading