Skip to content

Commit

Permalink
cli: use AF_INET(6) instead of hardcoded values
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloegea committed Jun 12, 2024
1 parent 0f7d3d0 commit 762b73e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
10 changes: 5 additions & 5 deletions netplan_cli/cli/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import json
import logging
import re
import socket
from socket import inet_ntop, AF_INET, AF_INET6
import subprocess
import sys
from io import StringIO
Expand Down Expand Up @@ -107,7 +107,7 @@ def __init__(self, ip: dict, nd_data: JSON = [], nm_data: JSON = [],
if int(itr[0]) == int(self.idx):
ipfamily = itr[1]
dns = itr[2]
self.dns_addresses.append(socket.inet_ntop(ipfamily, b''.join([v.to_bytes(1, 'big') for v in dns])))
self.dns_addresses.append(inet_ntop(ipfamily, b''.join([v.to_bytes(1, 'big') for v in dns])))
self.dns_search: list = None
if resolved_data[1]:
self.dns_search = []
Expand Down Expand Up @@ -153,7 +153,7 @@ def __init__(self, ip: dict, nd_data: JSON = [], nm_data: JSON = [],
for route in self.routes:
if (route.get('protocol') == 'ra'
and route.get('to') != 'default'
and route.get('family') == 10):
and route.get('family') == AF_INET6.value):
ra_networks.add(ipaddress.ip_interface(route['to']).network)

self.addresses = []
Expand Down Expand Up @@ -519,10 +519,10 @@ def query_routes(cls) -> tuple:
# IPv4: 2, IPv6: 10
if data4:
for route in data4:
route.update({'family': socket.AF_INET.value})
route.update({'family': AF_INET.value})
if data6:
for route in data6:
route.update({'family': socket.AF_INET6.value})
route.update({'family': AF_INET6.value})
return (data4, data6)

@classmethod
Expand Down
18 changes: 10 additions & 8 deletions netplan_cli/cli/state_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from collections import defaultdict
import ipaddress
import json
from socket import AF_INET, AF_INET6
from typing import AbstractSet

from netplan.netdef import NetplanRoute
Expand Down Expand Up @@ -508,10 +509,10 @@ def _normalize_gateways(self, config: dict) -> AbstractSet[NetplanRoute]:
if gateway4 := config.get('gateway4'):
default_routes = [route for route in routes
if route.get('to') == 'default'
and route.get('family') == 2
and route.get('family') == AF_INET.value
and route.get('protocol') == 'static']

route = NetplanRoute(to='default', via=gateway4, family=2, protocol='static')
route = NetplanRoute(to='default', via=gateway4, family=AF_INET.value, protocol='static')

if len(default_routes) == 1:
if metric := default_routes[0].get('metric'):
Expand All @@ -524,13 +525,13 @@ def _normalize_gateways(self, config: dict) -> AbstractSet[NetplanRoute]:
if gateway6 := config.get('gateway6'):
default_routes = [route for route in routes
if route.get('to') == 'default'
and route.get('family') == 10
and route.get('family') == AF_INET6.value
and route.get('protocol') == 'static']

# Compress the address so it will match the system representation
gateway6 = self._compress_ipv6_address(gateway6)

route = NetplanRoute(to='default', via=gateway6, family=10, protocol='static')
route = NetplanRoute(to='default', via=gateway6, family=AF_INET6.value, protocol='static')

if len(default_routes) == 1:
if metric := default_routes[0].get('metric'):
Expand Down Expand Up @@ -576,9 +577,9 @@ def _filter_system_routes(self, system_routes: AbstractSet[NetplanRoute], system
if route.to != 'default':
route_to = ipaddress.ip_interface(route.to)
if route_to.is_link_local:
if route.family == 10 and 'ipv6' in link_local:
if route.family == AF_INET6.value and 'ipv6' in link_local:
continue
if route.family == 2 and 'ipv4' in link_local:
if route.family == AF_INET.value and 'ipv4' in link_local:
continue

# Filter out host scoped routes
Expand All @@ -587,11 +588,12 @@ def _filter_system_routes(self, system_routes: AbstractSet[NetplanRoute], system
continue

# Filter out the default IPv6 multicast route
if route.family == 10 and route.type == 'multicast' and route.to == 'ff00::/8':
if route.family == AF_INET6.value and route.type == 'multicast' and route.to == 'ff00::/8':
continue

# Filter IPv6 local routes
if route.family == 10 and route.protocol != 'ra' and (route.to in local_networks or route.to in addresses):
if (route.family == AF_INET6.value and route.protocol != 'ra'
and (route.to in local_networks or route.to in addresses)):
continue

routes.add(route)
Expand Down

0 comments on commit 762b73e

Please sign in to comment.