Skip to content

Commit

Permalink
feat(addrinfo): include protocols 6 and 17 only
Browse files Browse the repository at this point in the history
  • Loading branch information
bikallem committed Aug 15, 2022
1 parent f774dd8 commit e510251
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
18 changes: 9 additions & 9 deletions lib_eio_linux/eio_linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -893,22 +893,22 @@ module Low_level = struct
in
Eio_unix.run_in_systhread (fun () -> read_all [] fd)

(* https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml *)
let getaddrinfo ~service node =
let to_eio_sockaddr_t {Unix.ai_family; ai_addr; ai_socktype; _ } =
let to_eio_sockaddr_t {Unix.ai_family; ai_addr; ai_socktype; ai_protocol; _ } =
match ai_family, ai_socktype, ai_addr with
| (Unix.PF_INET | PF_INET6),
(Unix.SOCK_STREAM | SOCK_DGRAM as socktype),
(Unix.SOCK_STREAM | SOCK_DGRAM),
Unix.ADDR_INET (inet_addr,port) -> (
match socktype with
| Unix.SOCK_STREAM -> Some (`Tcp (Eio_unix.Ipaddr.of_unix inet_addr, port))
| Unix.SOCK_DGRAM -> Some (`Udp (Eio_unix.Ipaddr.of_unix inet_addr, port))
match ai_protocol with
| 6 -> Some (`Tcp (Eio_unix.Ipaddr.of_unix inet_addr, port))
| 17 -> Some (`Udp (Eio_unix.Ipaddr.of_unix inet_addr, port))
| _ -> None)
| _ -> None
in
Eio_unix.run_in_systhread (fun () ->
Unix.getaddrinfo node service []
|> List.filter_map to_eio_sockaddr_t
)
Eio_unix.run_in_systhread @@ fun () ->
Unix.getaddrinfo node service []
|> List.filter_map to_eio_sockaddr_t
end

external eio_eventfd : int -> Unix.file_descr = "caml_eio_eventfd"
Expand Down
16 changes: 9 additions & 7 deletions lib_eio_luv/eio_luv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,20 @@ module Low_level = struct
if Fiber_context.clear_cancel_fn k.fiber then enqueue_thread st k ()
) |> or_raise

(* https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml *)
let getaddrinfo ~service node =
let ( let* ) o f = Option.bind o f in
let ( let+ ) o f = Option.map f o in
let to_eio_sockaddr_t {Luv.DNS.Addr_info.family; addr; socktype;_ } =
let to_eio_sockaddr_t {Luv.DNS.Addr_info.family; addr; socktype; protocol; _ } =
match family, socktype with
| (`INET | `INET6), (`STREAM | `DGRAM as socktype') -> (
| (`INET | `INET6),
(`STREAM | `DGRAM) -> (
let* host = Luv.Sockaddr.to_string addr in
let+ port = Luv.Sockaddr.port addr in
let* port = Luv.Sockaddr.port addr in
let ipaddr = Unix.inet_addr_of_string host |> Eio_unix.Ipaddr.of_unix in
match socktype' with
| `STREAM -> `Tcp (ipaddr, port)
| `DGRAM -> `Udp (ipaddr, port))
match protocol with
| 6 -> Some (`Tcp (ipaddr, port))
| 9 -> Some (`Udp (ipaddr, port))
| _ -> None)
| _ -> None
in
let request = Luv.DNS.Addr_info.Request.make () in
Expand Down
18 changes: 14 additions & 4 deletions tests/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,18 +495,28 @@ EPIPE:
[`Tcp ("\127\000\000\001", 0); `Udp ("\127\000\000\001", 0)]
```

<!-- $MDX non-deterministic=output -->
```ocaml
# Eio_main.run @@ fun env ->
Eio.Net.getaddrinfo ~service:"http" env#net "127.0.0.1";;
- : Eio.Net.Sockaddr.t list =
[`Tcp ("\127\000\000\001", 80); `Udp ("\127\000\000\001", 80);
`Tcp ("\127\000\000\001", 80)]
[`Tcp ("\127\000\000\001", 80); `Udp ("\127\000\000\001", 80)]
```

<!-- $MDX non-deterministic=output -->
```ocaml
# Eio_main.run @@ fun env ->
Eio.Net.getaddrinfo ~service:"ftp" env#net "127.0.0.1";;
- : Eio.Net.Sockaddr.t list =
[`Tcp ("\127\000\000\001", 21); `Udp ("\127\000\000\001", 21);
`Tcp ("\127\000\000\001", 21)]
[`Tcp ("\127\000\000\001", 21); `Udp ("\127\000\000\001", 21)]
```

<!-- $MDX non-deterministic=output -->
```ocaml
# Eio_main.run @@ fun env ->
Eio.Net.getaddrinfo ~service:"https" env#net "google.com";;
- : Eio.Net.Sockaddr.t list =
[`Tcp ("Ø:ÔÎ", 443); `Udp ("Ø:ÔÎ", 443);
`Tcp ("*\000\020P@\t\b \000\000\000\000\000\000 \014", 443);
`Udp ("*\000\020P@\t\b \000\000\000\000\000\000 \014", 443)]
```

0 comments on commit e510251

Please sign in to comment.