The network module reads and changes the network configuration of the gateway: Ethernet, WAN, Wi-Fi and USB-LAN interfaces, IP addresses, DNS, static routes, port forwarding and the DHCP server. It is the API behind System configuration > Network (see Network for the screen).
Changing the network configuration can cut the gateway off the network, including your own API connection, and may leave it unreachable until someone fixes it on site. Read section 4 before your first write, and never guess an IP address, netmask or gateway.
The network stack runs in its own process: it is a separate peer called network, not a module of the supervision peer. Load its single module, api:
peer('network').require('api', function(err, network){
if(err) return console.error(err)
network.getConfig(function(err, config){
console.log(config.ethernet.address, config.network.dns1)
})
})
The connection itself (login, API key, browser or Node.js) is described in Getting started.
Over HTTP, the same methods are reached at /peers/network/api/<method> (see REST: peers dispatcher). The account's role applies as for any other module: give integration accounts only the methods they need.
IP addresses, netmask, gateway, DNS and routes are in
network.getConfig, not insystem.getConfigof the system module. Enabling the modem or the VPN is done in the system module (setModemEnable,setTincEnable).
| Method | Returns |
|---|---|
getConfig(callback) |
The current configuration (see section 3). The usual starting point. |
readConfig(callback) |
The same configuration, re-read from disk. Answers NOT_READY while the network process is starting. |
getInterfaces(callback) |
Live state of the interfaces: { ethernet, wireless, wan, usblan }. |
getEthernet(callback) / getWireless(callback) / getWan(callback) / getUsblan(callback) |
Live state of one interface. |
getHostname(callback) |
The operating system host name (string). |
getDNSState(callback) |
Result of the last DNS test: { code, state, resolve, addresses }. state is 1 when the test name (resolve) could be resolved, 0 otherwise; code is 0 or the resolver error code. |
network.getDNSState(function(err, dns){
if(!dns.state) console.warn('DNS resolution fails:', dns.code)
})
Some properties of the module are plain values, not functions: config, hasWan, hasWlan, isBridgable, devices, device, pings, waitingConfirmation, DNS_STATE. Read them on the loaded module object; calling them fails.
getDefaultBridge is a synchronous function: it returns its result directly and never calls a callback, so an asynchronous peers call to it never gets an answer. Use getConfig and getInterfaces instead.
Listen on the module object, for example network.on('config updated', fn).
| Event | Argument | When |
|---|---|---|
config updated |
new configuration | After every successful write. |
ethernet, wireless, wan, usblan, bridge |
interface state | When the live state of an interface changes. |
dnsState |
DNS state | When the result of the DNS test changes. |
pings |
ping results | Periodic connectivity checks. |
waitingConfirmation |
deadline in ms, or 0 / false |
An automatic restore is armed, confirmed or cancelled (section 4). |
restarting network, restarting gsm |
— | The network or modem loop is about to restart after a write. |
getConfig returns one object per section. The keys below are the ones the gateway reads; values are strings or numbers.
| Section | Keys |
|---|---|
network |
hostname, mode (interfaces or bridged), dnsMode (defined or auto), dns1, dns2, route, routerMode, proxy_enable, proxy |
ethernet, wan |
dhcp (1 = DHCP client, 0 = fixed address), address, netmask, gateway, dhcp_server and dhcp_server_start, dhcp_server_end, dhcp_server_netmask, dhcp_server_gateway, dhcp_server_gateway_local, dhcp_server_dns1, dhcp_server_dns2, dhcp_server_hosts; dhcp_ipv4_fallback with _address, _netmask, _timeout, _retry |
ethernet only |
secondary (0/1), secondary_address, secondary_netmask |
wireless |
mode (off, repeater = Wi-Fi client, hotspot), essid, psk, security, dhcp, address, netmask, gateway, bridged, hotspot_essid, hotspot_psk, hotspot_security, hotspot_channel (1 to 12), dhcp_server* |
gsm |
mode (off, internet, no internet), gateway (the APN), username, password, pin, puk, cop, roaming |
usbs |
USB network adapters, keyed by MAC address |
routes |
Static routes and port forwarding (section 3.2) |
network.vpn and network.TINC_CHISEL mirror the VPN settings: writing them has no effect. Use setTincEnable of the system module.
Never send
null, an array or an object as a value. To clear a value, send an empty string"".
dhcp ruleAn interface uses address, netmask and gateway only when its dhcp is 0. If dhcp is still 1, the address you write is stored, but the interface keeps the address given by the DHCP server of your network. To set a fixed address, send dhcp: 0 together with the address. When an interface is a DHCP client, its own DHCP server (dhcp_server) is switched off.
routes is an object keyed by destination; each value is { forward, active }. A rule is applied only with active: 1.
| Kind | Key | forward |
|---|---|---|
| Port forwarding | external port, as a numeric string ("8080") |
"ip:port" of the target device (TCP and UDP) |
| Static route | "ip/mask", for example "10.8.0.0/16" (a bare IP means /32) |
IP address of the next router |
Never forward the gateway's own web ports (80 and 443 by default, or the HTTP / HTTPS ports set in System configuration). The rule catches that port on every interface, VPN included, and the web interface and the API become unreachable. The web interface refuses such a rule; the API does not check it.
Each interface (ethernet, wireless, wan) can hold fixed leases in dhcp_server_hosts, keyed by the MAC address in lowercase: { "00:11:22:33:44:55": { address: "192.168.1.50", active: 1 } }. They are served only when the interface has dhcp_server: 1.
Every write can arm an automatic restore: the gateway applies the new configuration, then goes back to the previous one after a delay unless you confirm. This is the "restore" option of the confirmation dialog in the web interface, and the recommended way to change a remote gateway.
restoreConfigDelay to the object you write. A value below 1000 is read as minutes, a larger value as milliseconds. The web interface proposes 5 minutes and accepts 5 to 60.confirmConfig(callback) to keep the change.waitingConfirmation holds the restore deadline (timestamp in ms) while a restore is pending, and the waitingConfirmation event announces each change.restoreConfigDelay: 'cancel' with a write applies it without restore and cancels a pending one.NO_CHANGE_DETECTED) does not leave a restore armed.Do not reboot the gateway or restart its services while a restore is pending: the previous configuration is brought back when the network process starts again.
network.extendConfig({
restoreConfigDelay : 5, // minutes
ethernet : { dhcp: 0, address: '192.168.10.20', netmask: '255.255.255.0', gateway: '192.168.10.1' }
}, function(err){
if(err) return console.error('refused:', err)
// reconnect to 192.168.10.20, check, then:
// network.confirmConfig(function(){ console.log('kept') })
})
| Method | Effect |
|---|---|
extendConfig(partialConfig, callback) |
Merges the object into the current configuration, then writes it. Send only the keys you change. Recommended. A merge cannot remove a key. |
writeConfig(fullConfig, callback) |
Replaces the whole configuration. Start from the complete object returned by getConfig; the sections network, ethernet, wireless, wan and gsm are required. It is the only way to remove a route or a DHCP lease. |
scanWireless(iface, callback) |
Scans Wi-Fi networks (iface optional, default Wi-Fi interface). Returns an array of networks with ssid, selected (the configured network) and connected. |
restart() |
Restarts the network control loop and re-applies all interfaces. No callback, no answer. Can cut connectivity. |
The callback of a write receives (err, config, changes, restoreDelay).
What happens after a write:
WRONG_HOSTNAME, WRONG_ETHERNET_IP, WRONG_ETHERNET_MASK, WRONG_ETHERNET_GATEWAY_NETMASK, WRONG_ETHERNET_DHCP_RANGE, ETHERNET_ADDRESS_IN_DHCP_RANGE, BAD_HOTSPOT_CHANNEL. NO_CHANGE_DETECTED means the configuration already has these values.ip, mask, ssid…) is stored and ignored. Use the names of section 3.Add a port forwarding rule (8080 to 192.168.1.50:80):
network.extendConfig({
restoreConfigDelay : 5,
routes : { '8080': { forward: '192.168.1.50:80', active: 1 } }
}, callback)
Remove it (full write):
network.getConfig(function(err, config){
var full = JSON.parse(JSON.stringify(config))
delete full.routes['8080']
full.restoreConfigDelay = 5
network.writeConfig(full, callback)
})
Change the DNS servers:
network.extendConfig({ restoreConfigDelay: 5, network: { dnsMode: 'defined', dns1: '1.1.1.1', dns2: '9.9.9.9' } }, callback)
| Symptom | Cause and fix |
|---|---|
NOT_READY |
The network process is starting. Retry after a few seconds. |
| The new fixed address is not used | dhcp is still 1 on that interface. Send dhcp: 0 with the address (section 3.1). |
A route is still there after extendConfig |
A merge cannot delete. Use writeConfig with the full object (section 5.1). |
| A change has no effect | Misspelt key name. Compare with section 3. |
| The gateway went back to its old configuration | An automatic restore was armed and not confirmed. Write again and call confirmConfig. |
getDefaultBridge never answers |
It is synchronous (section 2.1). |