1
mirror of https://github.com/home-assistant/supervisor synced 2024-10-02 06:19:58 +02:00

Fix byte order for network DNS settings (#2315)

This commit is contained in:
Pascal Vizeli 2020-11-30 15:22:02 +01:00 committed by GitHub
parent d9c4dae739
commit e09a839148
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 6 deletions

View File

@ -1,7 +1,9 @@
"""Payload generators for DBUS communication."""
from __future__ import annotations
from ipaddress import IPv4Address, IPv6Address
from pathlib import Path
import socket
from typing import TYPE_CHECKING, Optional
from uuid import uuid4
@ -22,7 +24,20 @@ def interface_update_payload(
interface: Interface, name: Optional[str] = None, uuid: Optional[str] = None
) -> str:
"""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
if not uuid:

View File

@ -21,7 +21,7 @@
'method': <'disabled'>
{% else %}
'method': <'manual'>,
'dns': <[uint32 {{ interface.ipv4.nameservers | map("int") | join(",") }}]>,
'dns': <[uint32 {{ interface.ipv4.nameservers | map("ipv4_to_int") | join(",") }}]>,
'address-data': <[
{% for address in interface.ipv4.address %}
{
@ -44,7 +44,7 @@
'method': <'disabled'>
{% else %}
'method': <'manual'>,
'dns': <[uint32 {{ interface.ipv6.nameservers | map("int") | join(",") }}]>,
'dns': <[{{ interface.ipv6.nameservers | map("ipv6_to_byte") | join(",") }}]>,
'address-data': <[
{% for address in interface.ipv6.address if not address.with_prefixlen.startswith("fe80::") %}
{

View File

@ -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"
)
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 (
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"]["dns"] == [
50543257694033307102031451402929176676,
50543257694033307102031451402929202176,
[100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 0, 71, 6, 38],
[0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 0, 71, 6, 38],
]
assert (
DBus.parse_gvariant(data)["connection"]["uuid"] == inet.settings.connection.uuid