mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-19 07:06:30 +00:00
Fix byte order for network DNS settings (#2315)
This commit is contained in:
parent
d9c4dae739
commit
e09a839148
@ -1,7 +1,9 @@
|
|||||||
"""Payload generators for DBUS communication."""
|
"""Payload generators for DBUS communication."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from ipaddress import IPv4Address, IPv6Address
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import socket
|
||||||
from typing import TYPE_CHECKING, Optional
|
from typing import TYPE_CHECKING, Optional
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
@ -22,7 +24,20 @@ def interface_update_payload(
|
|||||||
interface: Interface, name: Optional[str] = None, uuid: Optional[str] = None
|
interface: Interface, name: Optional[str] = None, uuid: Optional[str] = None
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Generate a payload for network interface update."""
|
"""Generate a payload for network interface update."""
|
||||||
template = jinja2.Template(INTERFACE_UPDATE_TEMPLATE.read_text())
|
env = jinja2.Environment()
|
||||||
|
|
||||||
|
def ipv4_to_int(ip_address: IPv4Address) -> int:
|
||||||
|
"""Convert an ipv4 to an int."""
|
||||||
|
return socket.htonl(int(ip_address))
|
||||||
|
|
||||||
|
def ipv6_to_byte(ip_address: IPv6Address) -> str:
|
||||||
|
"""Convert an ipv6 to an byte array."""
|
||||||
|
return f'[byte {", ".join("0x{:02x}".format(val) for val in reversed(ip_address.packed))}]'
|
||||||
|
|
||||||
|
# Init template
|
||||||
|
env.filters["ipv4_to_int"] = ipv4_to_int
|
||||||
|
env.filters["ipv6_to_byte"] = ipv6_to_byte
|
||||||
|
template: jinja2.Template = env.from_string(INTERFACE_UPDATE_TEMPLATE.read_text())
|
||||||
|
|
||||||
# Generate UUID
|
# Generate UUID
|
||||||
if not uuid:
|
if not uuid:
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
'method': <'disabled'>
|
'method': <'disabled'>
|
||||||
{% else %}
|
{% else %}
|
||||||
'method': <'manual'>,
|
'method': <'manual'>,
|
||||||
'dns': <[uint32 {{ interface.ipv4.nameservers | map("int") | join(",") }}]>,
|
'dns': <[uint32 {{ interface.ipv4.nameservers | map("ipv4_to_int") | join(",") }}]>,
|
||||||
'address-data': <[
|
'address-data': <[
|
||||||
{% for address in interface.ipv4.address %}
|
{% for address in interface.ipv4.address %}
|
||||||
{
|
{
|
||||||
@ -44,7 +44,7 @@
|
|||||||
'method': <'disabled'>
|
'method': <'disabled'>
|
||||||
{% else %}
|
{% else %}
|
||||||
'method': <'manual'>,
|
'method': <'manual'>,
|
||||||
'dns': <[uint32 {{ interface.ipv6.nameservers | map("int") | join(",") }}]>,
|
'dns': <[{{ interface.ipv6.nameservers | map("ipv6_to_byte") | join(",") }}]>,
|
||||||
'address-data': <[
|
'address-data': <[
|
||||||
{% for address in interface.ipv6.address if not address.with_prefixlen.startswith("fe80::") %}
|
{% for address in interface.ipv6.address if not address.with_prefixlen.startswith("fe80::") %}
|
||||||
{
|
{
|
||||||
|
@ -49,7 +49,7 @@ async def test_interface_update_payload_ethernet_ipv4(coresys):
|
|||||||
DBus.parse_gvariant(data)["ipv4"]["address-data"][0]["address"] == "192.168.1.1"
|
DBus.parse_gvariant(data)["ipv4"]["address-data"][0]["address"] == "192.168.1.1"
|
||||||
)
|
)
|
||||||
assert DBus.parse_gvariant(data)["ipv4"]["address-data"][0]["prefix"] == 24
|
assert DBus.parse_gvariant(data)["ipv4"]["address-data"][0]["prefix"] == 24
|
||||||
assert DBus.parse_gvariant(data)["ipv4"]["dns"] == [16843009, 16777473]
|
assert DBus.parse_gvariant(data)["ipv4"]["dns"] == [16843009, 16842753]
|
||||||
assert (
|
assert (
|
||||||
DBus.parse_gvariant(data)["connection"]["uuid"] == inet.settings.connection.uuid
|
DBus.parse_gvariant(data)["connection"]["uuid"] == inet.settings.connection.uuid
|
||||||
)
|
)
|
||||||
@ -129,8 +129,8 @@ async def test_interface_update_payload_ethernet_ipv6(coresys):
|
|||||||
)
|
)
|
||||||
assert DBus.parse_gvariant(data)["ipv6"]["address-data"][0]["prefix"] == 64
|
assert DBus.parse_gvariant(data)["ipv6"]["address-data"][0]["prefix"] == 64
|
||||||
assert DBus.parse_gvariant(data)["ipv6"]["dns"] == [
|
assert DBus.parse_gvariant(data)["ipv6"]["dns"] == [
|
||||||
50543257694033307102031451402929176676,
|
[100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 0, 71, 6, 38],
|
||||||
50543257694033307102031451402929202176,
|
[0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 0, 71, 6, 38],
|
||||||
]
|
]
|
||||||
assert (
|
assert (
|
||||||
DBus.parse_gvariant(data)["connection"]["uuid"] == inet.settings.connection.uuid
|
DBus.parse_gvariant(data)["connection"]["uuid"] == inet.settings.connection.uuid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user