Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/zeroconf/_services/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ipaddress
import random
import socket
from functools import lru_cache
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Union, cast

from .._dns import (
Expand Down Expand Up @@ -79,6 +80,9 @@ def instance_name_from_service_info(info: "ServiceInfo") -> str:
return info.name[: -len(service_name) - 1]


_cached_ip_addresses = lru_cache(maxsize=256)(ipaddress.ip_address)


class ServiceInfo(RecordUpdateListener):
"""Service information.

Expand Down Expand Up @@ -196,7 +200,7 @@ def addresses(self, value: List[bytes]) -> None:

for address in value:
try:
addr = ipaddress.ip_address(address)
addr = _cached_ip_addresses(address)
except ValueError:
raise TypeError(
"Addresses must either be IPv4 or IPv6 strings, bytes, or integers;"
Expand Down Expand Up @@ -245,7 +249,7 @@ def parsed_scoped_addresses(self, version: IPVersion = IPVersion.All) -> List[st
return self.parsed_addresses(version)

def is_link_local(addr_str: str) -> Any:
addr = ipaddress.ip_address(addr_str)
addr = _cached_ip_addresses(addr_str)
return addr.version == 6 and addr.is_link_local

ll_addrs = list(filter(is_link_local, self.parsed_addresses(version)))
Expand Down Expand Up @@ -346,7 +350,7 @@ def _process_record_threadsafe(self, record: DNSRecord, now: float) -> None:
if record.key != self.server_key:
return
try:
ip_addr = ipaddress.ip_address(record.address)
ip_addr = _cached_ip_addresses(record.address)
except ValueError as ex:
log.warning("Encountered invalid address while processing %s: %s", record, ex)
return
Expand Down