In the Core library there is the Linux_ext.get_ipv4_address_for_interface
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 fork
to read the content of ifconfig
or ip addr show
?
Asked
Active
Viewed 361 times
1

Anthony Scemama
- 1,563
- 12
- 19
-
1(I think the quick answer is no. The base OCaml language doesn't have this kind of low-level network API built in.) – Jeffrey Scofield Oct 17 '16 at 17:35
1 Answers
2
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.
-
Sorry, instead of `Pervasive`, I meant all the modules of the standard library : no external library. I edit my question. Thanks for your answer – Anthony Scemama Oct 17 '16 at 18:04