Skip to content

Commit 6d4acc2

Browse files
joaomariolagoWilliangalvani
authored andcommitted
core:services:cable_guy: Fix DHCP marker removing
* Make sure that when `_execute_route` is called it keeps the DHCP requisition IP marker `0.0.0.0` `Mode.Client` alive
1 parent 7cd22c3 commit 6d4acc2

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

core/services/cable_guy/api/manager.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,17 @@ def remove_ip(self, interface_name: str, ip_address: str) -> None:
383383

384384
self._update_interface_settings(interface_name, saved_interface)
385385

386-
def get_interface_by_name(self, name: str) -> NetworkInterface:
387-
for interface in self.get_ethernet_interfaces():
386+
def get_interface_by_name(self, name: str, include_dhcp_markers: bool = False) -> NetworkInterface:
387+
"""Get interface by name.
388+
389+
Args:
390+
name (str): Interface name
391+
include_dhcp_markers (bool, optional): Include DHCP markers. Defaults to False
392+
393+
Returns:
394+
NetworkInterface: Interface object
395+
"""
396+
for interface in self.get_ethernet_interfaces(include_dhcp_markers):
388397
if interface.name == name:
389398
return interface
390399
raise ValueError(f"No interface with name '{name}' is present.")
@@ -393,11 +402,13 @@ def get_saved_interface_by_name(self, name: str) -> Optional[NetworkInterface]:
393402
return next((i for i in self._settings.content if i.name == name), None)
394403

395404
# pylint: disable=too-many-locals
396-
def get_interfaces(self, filter_wifi: bool = False) -> List[NetworkInterface]:
405+
def get_interfaces(self, filter_wifi: bool = False, include_dhcp_markers: bool = False) -> List[NetworkInterface]:
397406
"""Get interfaces information
398407
399408
Args:
400409
filter_wifi (boolean, optional): Enable wifi interface filtering
410+
include_dhcp_markers (boolean, optional): DHCP marker is the IP 0.0.0.0 AddressMode.Client, used to
411+
inform cable guy that a dynamic IP should be acquired if available.
401412
402413
Returns:
403414
List of NetworkInterface instances available
@@ -431,6 +442,20 @@ def get_interfaces(self, filter_wifi: bool = False) -> List[NetworkInterface]:
431442
else:
432443
mode = AddressMode.Unmanaged if is_static_ip and valid_ip else AddressMode.Client
433444
valid_addresses.append(InterfaceAddress(ip=ip, mode=mode))
445+
# Check if there is a 0.0.0.0 AddressMode.Client in current interface on self._settings.content and if not in valid_addresses add it
446+
interface_settings = self.get_saved_interface_by_name(interface)
447+
if (
448+
include_dhcp_markers
449+
and interface_settings
450+
and any(
451+
str(address.ip) == "0.0.0.0" and address.mode == AddressMode.Client
452+
for address in interface_settings.addresses
453+
)
454+
and not any(
455+
str(address.ip) == "0.0.0.0" and address.mode == AddressMode.Client for address in valid_addresses
456+
)
457+
):
458+
valid_addresses.append(InterfaceAddress(ip="0.0.0.0", mode=AddressMode.Client))
434459
info = self.get_interface_info(interface)
435460
saved_interface = self.get_saved_interface_by_name(interface)
436461
# Get priority from saved interface or from current interface metrics, defaulting to None if neither exists
@@ -453,13 +478,17 @@ def get_interfaces(self, filter_wifi: bool = False) -> List[NetworkInterface]:
453478

454479
return result
455480

456-
def get_ethernet_interfaces(self) -> List[NetworkInterface]:
481+
def get_ethernet_interfaces(self, include_dhcp_markers: bool = False) -> List[NetworkInterface]:
457482
"""Get ethernet interfaces information
458483
484+
Args:
485+
include_dhcp_markers (boolean, optional): DHCP marker is the IP 0.0.0.0 AddressMode.Client, used to
486+
inform cable guy that a dynamic IP should be acquired if available.
487+
459488
Returns:
460489
List of NetworkInterface instances available
461490
"""
462-
return self.get_interfaces(filter_wifi=True)
491+
return self.get_interfaces(filter_wifi=True, include_dhcp_markers=include_dhcp_markers)
463492

464493
def get_interface_ndb(self, interface_name: str) -> Any:
465494
"""Get interface NDB information for interface
@@ -634,7 +663,7 @@ def _execute_route(self, action: str, interface_name: str, route: Route) -> None
634663
raise
635664

636665
# Update settings
637-
current_interface = self.get_interface_by_name(interface_name)
666+
current_interface = self.get_interface_by_name(interface_name, include_dhcp_markers=True)
638667
for current_route in current_interface.routes:
639668
if current_route.destination_parsed == route.destination_parsed and current_route.gateway == route.gateway:
640669
current_route.managed = route.managed

0 commit comments

Comments
 (0)