Skip to content

Commit

Permalink
[change] Wireless: avoid unnecessary network config #289
Browse files Browse the repository at this point in the history
Avoid generating unnecessary config in /etc/config/network
when defining wireless interfaces.

Closes #289
  • Loading branch information
nemesifier authored Jul 9, 2024
1 parent 176b51b commit d0e9815
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 747 deletions.
34 changes: 21 additions & 13 deletions netjsonconfig/backends/openwrt/converters/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ def __set_dsa_interface(self, interface):
)

def to_intermediate_loop(self, block, result, index=None):
result.setdefault('network', [])
uci_name = self._get_uci_name(block.get('network') or block['name'])
address_list = self.__intermediate_addresses(block)
interface = self.__intermediate_interface(block, uci_name)
self.__set_dsa_interface(interface)
if self.dsa_interface:
uci_device = self.__intermediate_device(interface)
if uci_device:
result.setdefault('network', [])
result['network'].append(self.sorted_dict(uci_device))
# create one or more "config interface" UCI blocks
i = 1
Expand Down Expand Up @@ -95,6 +95,9 @@ def __intermediate_addresses(self, interface):
if interface.get('type') == 'wireguard':
return self.__intermediate_wireguard_addresses(interface)
address_list = self.get_copy(interface, 'addresses')
# ignore wireless interfaces without addresses
if not address_list and interface['type'] == 'wireless':
return []
# do not ignore interfaces if they do not contain any address
if not address_list:
return [{'proto': 'none'}]
Expand All @@ -117,21 +120,24 @@ def __intermediate_addresses(self, interface):
static[address_key].append('{address}/{mask}'.format(**address))
static.update(self.__intermediate_address(address))
if static:
# do not use CIDR notation when using a single ipv4
# see https://github.com/openwisp/netjsonconfig/issues/54
if len(static.get('ipaddr', [])) == 1:
network = ip_interface(static['ipaddr'][0])
static['ipaddr'] = str(network.ip)
static['netmask'] = str(network.netmask)
# do not use lists when using a single ipv6 address
# (avoids to change output of existing configuration)
if len(static.get('ip6addr', [])) == 1:
static['ip6addr'] = static['ip6addr'][0]
result.append(static)
result.append(self.__intermediate_static_address(static))
if dhcp:
result += dhcp
return result

def __intermediate_static_address(self, uci):
# do not use CIDR notation when using a single ipv4
# see https://github.com/openwisp/netjsonconfig/issues/54
if len(uci.get('ipaddr', [])) == 1:
network = ip_interface(uci['ipaddr'][0])
uci['ipaddr'] = str(network.ip)
uci['netmask'] = str(network.netmask)
# do not use lists when using a single ipv6 address
# (avoids to change output of existing configuration)
if len(uci.get('ip6addr', [])) == 1:
uci['ip6addr'] = uci['ip6addr'][0]
return uci

def __intermediate_wireguard_addresses(self, interface):
addresses = interface.pop('addresses')
address_list = []
Expand Down Expand Up @@ -210,8 +216,10 @@ def __intermediate_device(self, interface):
data structure compatible with new syntax
introduced in OpenWrt 21.02.
"""

device = {}
# ignore wireless interfaces
if interface['type'] == 'wireless':
return device
# Add L2 options (needed for > OpenWrt 21.02)
self._add_l2_options(device, interface)
device.update(
Expand Down
5 changes: 3 additions & 2 deletions netjsonconfig/backends/openwrt/converters/wireless.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def to_intermediate_loop(self, block, result, index=None):
def __intermediate_wireless(self, interface):
if 'wireless' not in interface:
return
interface.pop('network', None)
wireless = interface['wireless']
# inherit "disabled" attribute from interface if present
wireless['disabled'] = interface.get('disabled')
Expand Down Expand Up @@ -72,8 +73,8 @@ def __intermediate_wireless(self, interface):
try:
bridges = self._bridged_wifi[interface['name']]
except KeyError:
# default to the value of "network" or inteface name
network = [interface.get('network', interface['name'])]
# don't bridge to anything unless explicitly specified
network = []
else:
network = bridges
wireless['network'] = network
Expand Down
7 changes: 1 addition & 6 deletions tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ def test_generate(self):
network = tar.getmember('etc/config/network')
contents = tar.extractfile(network).read().decode()
expected = self._tabs(
"""config device 'device_wlan0'
option name 'wlan0'
config interface 'wlan0'
option device 'wlan0'
"""config interface 'wlan0'
option ipaddr '192.168.1.1'
option netmask '255.255.255.0'
option proto 'static'
Expand All @@ -185,7 +181,6 @@ def test_generate(self):
option hidden '1'
option ifname 'wlan0'
option mode 'ap'
option network 'wlan0'
option ssid 'MyWifiAP'
"""
)
Expand Down
Loading

0 comments on commit d0e9815

Please sign in to comment.