Is it possible to get the remote IP address in ocaml cohttp or conduit?

149 Views Asked by At

Is it possible to get the remote IP address at the conduit or cohttp level? I've tried digging though the source code, but it seems to be buried under several layers of abstraction. I'm writing rest services that I plan to deploy as unikernels and logging the IP address of the requester is a security requirement.

I'm using cohttp and would want to be able to do this in both unix (for testing) and xen (for deployment). I could put in conditional code for the two environments if necessary.

1

There are 1 best solutions below

1
Thomas Leonard On

I modified the example in cohttp's readme to:

open Sexplib
[...]

  let callback (flow, _connection_id) req body =
    Conduit_lwt_unix.sexp_of_flow flow |> Sexp.to_string |> print_endline;

When I connected from localhost, it printed:

(TCP((fd <opaque>)(ip(V4 127.0.0.1))(port 62168)))

(figuring this out isn't too obvious; I just replaced the _conn in the example with () and followed the type errors...)