nerves_network_interface v0.3.1 Nerves.NetworkInterface

This module exposes a simplified view of Linux network configuration to applications.

Overview

This module should be added to a supervision tree or started via the start_link/0 call. Once running, the module provides functions to list network interfaces, modify their state (up or down), get statistics and set IP networking parameters. Network events, such as when an Ethernet cable is connected, are reported via a GenEvent.

Privilege

The functions that return information don’t require that the Nerves.NetworkInterface’s associated port process has privileged access to the system. If you need to change any parameters or bring up or down an interface, you should ensure that the port process is running as a privileged user.

Summary

Functions

Return the GenEvent pid that is being used for sending events

Bring the specified interface down

Bring the specified interface up

Return the list of network interfaces on this machine

Return the IP configuration for the specified interface as a map. See setup/3 for options

Set IP settings for the specified interface. The following options are available

Start and link a Nerves.NetworkInterface process. A GenEvent will be spawned for managing link layer events. Call Event_manager/1 to get the GenEvent pid

Return link-level status on the specified interface

Functions

event_manager()

Return the GenEvent pid that is being used for sending events.

ifdown(ifname)

Bring the specified interface down.

Returns :ok on success or {:error, reason} if an error occurs.

ifup(ifname)

Bring the specified interface up.

Returns :ok on success or {:error, reason} if an error occurs.

interfaces()

Return the list of network interfaces on this machine.

settings(ifname)

Return the IP configuration for the specified interface as a map. See setup/3 for options.

Returns {:ok, config} on success or {:error, reason} if an error occurs.

setup(ifname, options)

Set IP settings for the specified interface. The following options are available:

  • :ipv4_address - the IPv4 address of the interface
  • :ipv4_broadcast - the IPv4 broadcast address for the interface
  • :ipv4_subnet_mask - the IPv4 subnet mask
  • :ipv4_gateway - the default gateway

Options can be specified either as a keyword list or as a map.

Returns :ok on success or {:error, reason} if an error occurs.

start(type, args)

Start and link a Nerves.NetworkInterface process. A GenEvent will be spawned for managing link layer events. Call Event_manager/1 to get the GenEvent pid.

status(ifname)

Return link-level status on the specified interface.

For example, Nerves.NetworkInterface.status pid, "eth0" could return:

{:ok,
 %{ifname: "eth0", index: 2, is_broadcast: true, is_lower_up: true,
   is_multicast: true, is_running: true, is_up: true,
   mac_address: <<224, 219, 85, 231, 139, 93>>,
   mac_broadcast: <<255, 255, 255, 255, 255, 255>>, mtu: 1500, operstate: :up,
   stats: %{collisions: 0, multicast: 427, rx_bytes: 358417207, rx_dropped: 0,
     rx_errors: 0, rx_packets: 301021, tx_bytes: 22813761, tx_dropped: 0,
     tx_errors: 0, tx_packets: 212480}, type: :ethernet}}

If the interface doesn’t exist, {:error, :enodev} is returned.