Skip to content

Commit

Permalink
Move things to net and implement libuv
Browse files Browse the repository at this point in the history
  • Loading branch information
haesbaert committed Dec 1, 2022
1 parent a26ddac commit b419150
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 36 deletions.
17 changes: 17 additions & 0 deletions lib_eio/net.ml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ let datagram_socket ?(reuse_addr=false) ?(reuse_port=false) ~sw (t:#t) addr =
let addr = (addr :> [Sockaddr.datagram | `UdpV4 | `UdpV6]) in
t#datagram_socket ~reuse_addr ~reuse_port ~sw addr

(* keep in sync with C stubs *)
type getaddrinfo_error =
| EAI_ADDRFAMILY
| EAI_AGAIN
| EAI_BADFLAGS
| EAI_BADHINTS
| EAI_FAIL
| EAI_FAMILY
| EAI_MEMORY
| EAI_NODATA
| EAI_NONAME
| EAI_SERVICE
| EAI_SOCKTYPE
| EAI_SYSTEM

exception Getaddrinfo_error of getaddrinfo_error

let getaddrinfo ?(service="") (t:#t) hostname = t#getaddrinfo ~service hostname

let getaddrinfo_stream ?service t hostname =
Expand Down
17 changes: 17 additions & 0 deletions lib_eio/net.mli
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,23 @@ val recv : #datagram_socket -> Cstruct.t -> Sockaddr.datagram * int

(** {2 DNS queries} *)

(* keep in sync with C stubs *)
type getaddrinfo_error =
| EAI_ADDRFAMILY
| EAI_AGAIN
| EAI_BADFLAGS
| EAI_BADHINTS
| EAI_FAIL
| EAI_FAMILY
| EAI_MEMORY
| EAI_NODATA
| EAI_NONAME
| EAI_SERVICE
| EAI_SOCKTYPE
| EAI_SYSTEM

exception Getaddrinfo_error of getaddrinfo_error

val getaddrinfo: ?service:string -> #t -> string -> Sockaddr.t list
(** [getaddrinfo ?service t node] returns a list of IP addresses for [node]. [node] is either a domain name or
an IP address.
Expand Down
21 changes: 4 additions & 17 deletions lib_eio_linux/eio_linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -858,23 +858,8 @@ module Low_level = struct

external eio_getdents : Unix.file_descr -> string list = "caml_eio_getdents"

(* keep in sync with eio_stubs.c *)
type gai_error =
| EAI_ADDRFAMILY
| EAI_AGAIN
| EAI_BADFLAGS
| EAI_BADHINTS
| EAI_FAIL
| EAI_FAMILY
| EAI_MEMORY
| EAI_NODATA
| EAI_NONAME
| EAI_SERVICE
| EAI_SOCKTYPE
| EAI_SYSTEM

external eio_getaddrinfo : string -> string -> Unix.getaddrinfo_option list ->
(Unix.addr_info list, gai_error) result
(Unix.addr_info list, Eio.Net.getaddrinfo_error) result
= "caml_eio_getaddrinfo"

let getrandom { Cstruct.buffer; off; len } =
Expand Down Expand Up @@ -973,7 +958,9 @@ module Low_level = struct
| _ -> None
in
Eio_unix.run_in_systhread @@ fun () ->
Unix.getaddrinfo node service []
(match (eio_getaddrinfo node service []) with
| Ok l -> l
| Error e -> raise (Eio.Net.Getaddrinfo_error e))
|> List.filter_map to_eio_sockaddr_t
end

Expand Down
17 changes: 2 additions & 15 deletions lib_eio_linux/eio_linux.mli
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,8 @@ module Low_level : sig
(** [getaddrinfo host] returns a list of IP addresses for [host]. [host] is either a domain name or
an ipaddress. *)

type gai_error =
| EAI_ADDRFAMILY
| EAI_AGAIN
| EAI_BADFLAGS
| EAI_BADHINTS
| EAI_FAIL
| EAI_FAMILY
| EAI_MEMORY
| EAI_NODATA
| EAI_NONAME
| EAI_SERVICE
| EAI_SOCKTYPE
| EAI_SYSTEM

val eio_getaddrinfo : string -> string -> Unix.getaddrinfo_option list -> (Unix.addr_info list, gai_error) result
val eio_getaddrinfo : string -> string -> Unix.getaddrinfo_option list ->
(Unix.addr_info list, Eio.Net.getaddrinfo_error) result


end
19 changes: 15 additions & 4 deletions lib_eio_luv/eio_luv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,25 @@ module Low_level = struct
| _ -> None
in
let request = Luv.DNS.Addr_info.Request.make () in
let r e = raise (Eio.Net.Getaddrinfo_error e) in
match (await_with_cancel ~request
(fun loop -> Luv.DNS.getaddrinfo ~loop ~request ~service ~node ()))
with
| Ok nl -> List.filter_map to_eio_sockaddr_t nl
| Error `EAI_ADDRFAMILY | Error `EAI_AGAIN | Error `EAI_BADFLAGS | Error `EAI_BADHINTS
| Error `EAI_CANCELED | Error `EAI_FAIL | Error `EAI_FAMILY | Error `EAI_MEMORY
| Error `EAI_NODATA | Error `EAI_NONAME| Error `EAI_OVERFLOW | Error `EAI_PROTOCOL
| Error `EAI_SERVICE | Error `EAI_SOCKTYPE -> []
| Error `EAI_ADDRFAMILY -> r EAI_ADDRFAMILY
| Error `EAI_AGAIN -> r EAI_AGAIN
| Error `EAI_BADFLAGS -> r EAI_BADFLAGS
| Error `EAI_BADHINTS -> r EAI_BADHINTS
| Error `EAI_CANCELED -> r EAI_FAIL (* note *)
| Error `EAI_FAIL -> r EAI_FAIL
| Error `EAI_FAMILY -> r EAI_FAMILY
| Error `EAI_MEMORY -> r EAI_MEMORY
| Error `EAI_NODATA -> r EAI_NODATA
| Error `EAI_NONAME -> r EAI_NONAME
| Error `EAI_OVERFLOW -> r EAI_FAIL (* note *)
| Error `EAI_PROTOCOL -> r EAI_FAIL (* note *)
| Error `EAI_SERVICE -> r EAI_SERVICE
| Error `EAI_SOCKTYPE -> r EAI_SOCKTYPE
| Error e -> raise (Luv_error e)
end

Expand Down

0 comments on commit b419150

Please sign in to comment.