Anthony Scemama
Anthony Scemama

Reputation: 1593

Get IP Address for a specific interface in OCaml

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 ?

Upvotes: 1

Views: 416

Answers (1)

ivg
ivg

Reputation: 35280

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.

Upvotes: 2

Related Questions