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

Interoperation with mirage/ocaml-ipaddr #462

Closed
RyanGibb opened this issue Mar 16, 2023 · 9 comments
Closed

Interoperation with mirage/ocaml-ipaddr #462

RyanGibb opened this issue Mar 16, 2023 · 9 comments

Comments

@RyanGibb
Copy link
Contributor

RyanGibb commented Mar 16, 2023

A number of libraries use https://github.com/mirage/ocaml-ipaddr. Using EIO with these libraries requires translating an Eio.Net.Ipaddr to an Ipaddr [0]:

let convert_eio_addr_to_ipaddr (ip : Eio.Net.Ipaddr.t) ->
    let src = (ip :> string) in
    Eio.Net.Ipaddr.fold
      ~v4:(fun _v4 -> Ipaddr.V4 (Result.get_ok @@ Ipaddr.V4.of_octets src))
      ~v6:(fun _v6 -> Ipaddr.V6 (Result.get_ok @@ Ipaddr.V6.of_octets src))
      ip

I imagine adding mirage/ocaml-ipaddr as a dependency of eio is not desirable (and vice versa) but perhaps this is worth documenting?

Also, with mirage/ocaml-ipaddr#115 changing the internal representation of Ipaddr.V6.t to a Bytes (Ipaddr.V4.t is a int32) I imagine there is a more efficient and/or elegant way to do this.

[0] https://github.com/RyanGibb/aeon/blob/4adbb7be231c3cc6a1c22cc3f3dae71e89f9e06a/src/server.ml#L4-L15 (thanks to @patricoferris for this)

@RyanGibb
Copy link
Contributor Author

And similar with the reverse:

let convert_ipaddr_to_eio (addr : Ipaddr.t) =
  (match addr with
  | Ipaddr.V4 v4 -> Ipaddr.V4.to_octets v4
  | Ipaddr.V6 v6 -> Ipaddr.V6.to_octets v6
  ) |> Eio.Net.Ipaddr.of_raw

@talex5
Copy link
Collaborator

talex5 commented Mar 17, 2023

If Ipaddr adds to_raw/from_raw then the conversion would be very simple. The "raw" format is what Unix uses (internally, though it doesn't expose this unfortunately).

@RyanGibb
Copy link
Contributor Author

RyanGibb commented Mar 20, 2023

Do you mean something like?

In ocaml-ipaddr's ipaddr.ml:

let of_octets_exn ?(off = 0) bs =
  match String.length bs - off with
    | 4 -> V4.of_octets_exn off bs
    | 16 -> V6.of_octets_exn off bs
    | _ -> raise (Parse_error ("octets must be of length 4 or 16", bs))

let of_octets ?off bs = try_with_result (of_octets_exn ?off) bs

let to_octets i = function
    | V4 p -> V4.Prefix.to_octets p
    | V6 p -> V6.Prefix.to_octets p

Which would then be used with:

let convert_eio_addr_to_ipaddr (ip : Eio.Net.Ipaddr.t) ->
  Ipaddr.of_octets (ip :> string)

let convert_ipaddr_to_eio (addr : Ipaddr.t) =
  Ipaddr.to_octets addr |> Eio.Net.Ipaddr.of_raw

@talex5
Copy link
Collaborator

talex5 commented Mar 20, 2023

Yes, exactly.

@RyanGibb
Copy link
Contributor Author

Great! I've created a PR here: mirage/ocaml-ipaddr#117

@avsm
Copy link
Contributor

avsm commented Mar 31, 2023

Relevant Ipaddr release now heading into opam-repository; ocaml/opam-repository#23602

@RyanGibb
Copy link
Contributor Author

Thanks @avsm! Do you think this is worth documenting @talex5, or shall I close the issue?

@talex5
Copy link
Collaborator

talex5 commented Apr 6, 2023

Do you think this is worth documenting @talex5, or shall I close the issue?

Up to you. Mentioning Ipaddr (and Eio_unix.Ipaddr) in Eio.Net.Ipaddr seems reasonable to me.

@RyanGibb
Copy link
Contributor Author

I've created a PR documenting this here: #492

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants