Anthony Scemama Anthony Scemama - 1 year ago 77
Linux Question

Get IP Address for a specific interface in OCaml

In the Core library there is the

function which returns the IP address assigned to a specific network interface. Is there a simple way to do the same using only the standard library and without doing a
to read the content of
ip addr show

ivg ivg
Answer Source

The OCaml abstract machine doesn't include a notion of a network interface, so it is not possible to implement such behavior with the portable part of the OCaml standard library, without relying on OS-specific interfaces.

In fact, it is not even possible in C. As the interface is kernel specific, so in order to write such function, you will need to write a separate implementation for each Unix system, you're planning to target. For example, in C you will need to use ioctl interface, and use Linux kernel specific operations.

Good news is that you can still do it using OCaml. Although OCaml doesn't provide ioctl function, you can still call it via either C Types foreign function interface or, directly, by writing a stub function.