Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions src/zeroconf/_cache.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ cdef class DNSCache:

cpdef async_entries_with_server(self, str name)

@cython.locals(
cached_entry=DNSRecord,
)
cpdef get_by_details(self, str name, object type_, object class_)

@cython.locals(
records=cython.dict,
entry=DNSRecord,
)
cpdef get_all_by_details(self, str name, object type_, object class_)

@cython.locals(
store=cython.dict,
)
Expand All @@ -60,5 +71,3 @@ cdef class DNSCache:
created_float=cython.float,
)
cpdef async_mark_unique_records_older_than_1s_to_expire(self, cython.set unique_types, object answers, float now)

cdef _dns_record_matches(DNSRecord record, object key, object type_, object class_)
24 changes: 12 additions & 12 deletions src/zeroconf/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def async_all_by_details(self, name: _str, type_: _int, class_: _int) -> List[DN
if records is None:
return matches
for record in records:
if _dns_record_matches(record, key, type_, class_):
if type_ == record.type and class_ == record.class_:
matches.append(record)
return matches

Expand Down Expand Up @@ -181,7 +181,7 @@ def get(self, entry: DNSEntry) -> Optional[DNSRecord]:
return cached_entry
return None

def get_by_details(self, name: str, type_: int, class_: int) -> Optional[DNSRecord]:
def get_by_details(self, name: str, type_: _int, class_: _int) -> Optional[DNSRecord]:
"""Gets the first matching entry by details. Returns None if no entries match.

Calling this function is not recommended as it will only
Expand All @@ -194,17 +194,21 @@ def get_by_details(self, name: str, type_: int, class_: int) -> Optional[DNSReco
Use get_all_by_details instead.
"""
key = name.lower()
for cached_entry in reversed(list(self.cache.get(key, []))):
if _dns_record_matches(cached_entry, key, type_, class_):
records = self.cache.get(key)
if records is None:
return None
for cached_entry in reversed(list(records)):
Comment thread
bdraco marked this conversation as resolved.
if type_ == cached_entry.type and class_ == cached_entry.class_:
return cached_entry
return None

def get_all_by_details(self, name: str, type_: int, class_: int) -> List[DNSRecord]:
def get_all_by_details(self, name: str, type_: _int, class_: _int) -> List[DNSRecord]:
"""Gets all matching entries by details."""
key = name.lower()
return [
entry for entry in list(self.cache.get(key, [])) if _dns_record_matches(entry, key, type_, class_)
]
records = self.cache.get(key)
if records is None:
return []
return [entry for entry in list(records) if type_ == entry.type and class_ == entry.class_]

def entries_with_server(self, server: str) -> List[DNSRecord]:
"""Returns a list of entries whose server matches the name."""
Expand Down Expand Up @@ -243,7 +247,3 @@ def async_mark_unique_records_older_than_1s_to_expire(
if (now - created_float > _ONE_SECOND) and record not in answers_rrset:
# Expire in 1s
record.set_created_ttl(now, 1)


def _dns_record_matches(record: _DNSRecord, key: _str, type_: _int, class_: _int) -> bool:
Comment thread
bdraco marked this conversation as resolved.
return key == record.key and type_ == record.type and class_ == record.class_