|
| 1 | +""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine |
| 2 | + Copyright 2003 Paul Scott-Murphy, 2014 William McBrine |
| 3 | +
|
| 4 | + This module provides a framework for the use of DNS Service Discovery |
| 5 | + using IP multicast. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 20 | + USA |
| 21 | +""" |
| 22 | + |
| 23 | +from typing import Dict, Set, Tuple |
| 24 | + |
| 25 | +from ._dns import DNSQuestion, DNSRecord |
| 26 | +from .const import _DUPLICATE_QUESTION_INTERVAL |
| 27 | + |
| 28 | +# The QuestionHistory is used to implement Duplicate Question Suppression |
| 29 | +# https://datatracker.ietf.org/doc/html/rfc6762#section-7.3 |
| 30 | + |
| 31 | + |
| 32 | +class QuestionHistory: |
| 33 | + def __init__(self) -> None: |
| 34 | + self._history: Dict[DNSQuestion, Tuple[float, Set[DNSRecord]]] = {} |
| 35 | + |
| 36 | + def add_question_at_time(self, question: DNSQuestion, now: float, known_answers: Set[DNSRecord]) -> None: |
| 37 | + """Remember a question with known answers.""" |
| 38 | + self._history[question] = (now, known_answers) |
| 39 | + |
| 40 | + def suppresses(self, question: DNSQuestion, now: float, known_answers: Set[DNSRecord]) -> bool: |
| 41 | + """Check to see if a question should be suppressed. |
| 42 | +
|
| 43 | + https://datatracker.ietf.org/doc/html/rfc6762#section-7.3 |
| 44 | + When multiple queriers on the network are querying |
| 45 | + for the same resource records, there is no need for them to all be |
| 46 | + repeatedly asking the same question. |
| 47 | + """ |
| 48 | + previous_question = self._history.get(question) |
| 49 | + # There was not previous question in the history |
| 50 | + if not previous_question: |
| 51 | + return False |
| 52 | + than, previous_known_answers = previous_question |
| 53 | + # The last question was older than 999ms |
| 54 | + if now - than > _DUPLICATE_QUESTION_INTERVAL: |
| 55 | + return False |
| 56 | + # The last question has more known answers than |
| 57 | + # we knew so we have to ask |
| 58 | + if previous_known_answers - known_answers: |
| 59 | + return False |
| 60 | + return True |
| 61 | + |
| 62 | + def async_expire(self, now: float) -> None: |
| 63 | + """Expire the history of old questions.""" |
| 64 | + removes = [ |
| 65 | + question |
| 66 | + for question, now_known_answers in self._history.items() |
| 67 | + if now - now_known_answers[0] > _DUPLICATE_QUESTION_INTERVAL |
| 68 | + ] |
| 69 | + for question in removes: |
| 70 | + del self._history[question] |
0 commit comments