Linx.Netlink.Rtnl.Address (Linx v0.1.0)

Copy Markdown View Source

rtnetlink interface addresses — the RTM_*ADDR messages.

list/1 and list/2 read addresses; add/4 and delete/4 assign and remove them. IPv4 and IPv6 are both supported — the address family is detected from the IP value.

Address fields (:address, :local) on a decoded %Address{} are Linx.IP structs, not raw bytes; they accept either an Linx.IP or a string at the verb's input.

Example

{:ok, sock} = Rtnl.open()

:ok = Address.add(sock, "eth0", "10.0.0.5", 24)
:ok = Address.add(sock, "eth0", "fd00::5", 64)

{:ok, addrs} = Address.list(sock, "eth0")
# => [#Linx.Netlink.Rtnl.Address<10.0.0.5/24 ifindex=2>,
#     #Linx.Netlink.Rtnl.Address<fd00::5/64 ifindex=2>]

:ok = Address.delete(sock, "eth0", "10.0.0.5", 24)

The wire format — struct ifaddrmsg and the IFA_* attributes (include/uapi/linux/if_addr.h) — is declared with the Linx.Netlink.Codec DSL.

Summary

Functions

Adds address ip with prefix length prefix to link link_name.

Decodes a netlink message body into a t/0.

Removes address ip/prefix from link link_name.

Encodes a t/0 into its netlink message body.

Lists every address in the socket's network namespace.

Lists the addresses on the link named link_name.

Types

t()

@type t() :: %Linx.Netlink.Rtnl.Address{
  address: term(),
  family: term(),
  flags: term(),
  index: term(),
  local: term(),
  prefixlen: term(),
  scope: term()
}

Functions

add(socket, link_name, ip, prefix)

@spec add(
  Linx.Netlink.Socket.t(),
  binary(),
  binary() | Linx.IP.t(),
  non_neg_integer()
) ::
  :ok | {:error, term()}

Adds address ip with prefix length prefix to link link_name.

ip may be a string ("10.0.0.5", "fc00::1") or a Linx.IP. The address family is taken from the IP.

decode(body)

@spec decode(binary()) :: t()

Decodes a netlink message body into a t/0.

delete(socket, link_name, ip, prefix)

@spec delete(
  Linx.Netlink.Socket.t(),
  binary(),
  binary() | Linx.IP.t(),
  non_neg_integer()
) ::
  :ok | {:error, term()}

Removes address ip/prefix from link link_name.

encode(message)

@spec encode(t()) :: binary()

Encodes a t/0 into its netlink message body.

list(socket)

@spec list(Linx.Netlink.Socket.t()) :: {:ok, [t()]} | {:error, term()}

Lists every address in the socket's network namespace.

list(socket, link_name)

@spec list(Linx.Netlink.Socket.t(), binary()) :: {:ok, [t()]} | {:error, term()}

Lists the addresses on the link named link_name.