Skip to content

Commit

Permalink
Better.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Jan 18, 2025
1 parent a96f25b commit 9c7cfb8
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions src/core/io/srt_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,39 +72,24 @@ let string_of_address = function
let getaddrinfo ~(log : Log.t) ~prefer_address address port =
let open Ctypes in
let open Posix_socket in
match getaddrinfo ~port:(`Int port) address with
let hints = allocate_n Addrinfo.t ~count:1 in
hints |-> Addrinfo.ai_flags <-@ ni_numerichost;
(hints |-> Addrinfo.ai_family
<-@
match prefer_address with
| `System_default -> af_unspec
| `Ipv4 -> af_inet
| `Ipv6 -> af_inet6);
hints |-> Addrinfo.ai_socktype <-@ sock_stream;
match getaddrinfo ~hints ~port:(`Int port) address with
| ptr when is_null !@ptr ->
Runtime_error.raise ~pos:[]
~message:
(Printf.sprintf "getaddrinfo could not resolve address: %s:%i"
address port)
"srt"
| ptr ->
let first_address = !@ptr in
let sockaddr =
match prefer_address with
| `System_default -> first_address
| `Ipv4 ->
let rec f ptr cur =
let sockaddr = !@ptr in
if is_null sockaddr then cur
else (
match !@(sockaddr |-> Sockaddr.sa_family) with
| id when id = af_inet -> sockaddr
| _ -> f (ptr +@ 1) cur)
in
f ptr first_address
| `Ipv6 ->
let rec f ptr cur =
let sockaddr = !@ptr in
if is_null sockaddr then cur
else (
match !@(sockaddr |-> Sockaddr.sa_family) with
| id when id = af_inet6 -> sockaddr
| _ -> f (ptr +@ 1) cur)
in
f ptr first_address
in
let sockaddr = !@ptr in
if log#active 5 then
log#f 5 "Address %s:%n resolved to: %s" address port
(string_of_address (to_unix_sockaddr sockaddr));
Expand Down

0 comments on commit 9c7cfb8

Please sign in to comment.