Skip to content

Commit 87b6a32

Browse files
authored
Seperate zeroconf._protocol into an incoming and outgoing modules (#988)
1 parent f4665fc commit 87b6a32

11 files changed

Lines changed: 377 additions & 317 deletions

File tree

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
author='Paul Scott-Murphy, William McBrine, Jakub Stasiak',
2424
url='https://github.com/jstasiak/python-zeroconf',
2525
package_data={"zeroconf": ["py.typed"]},
26-
packages=["zeroconf", "zeroconf._services", "zeroconf._utils"],
26+
packages=["zeroconf", "zeroconf._protocol", "zeroconf._services", "zeroconf._utils"],
2727
platforms=['unix', 'linux', 'osx'],
2828
license='LGPL',
2929
zip_safe=False,

tests/test_core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
from unittest.mock import patch
1919

2020
import zeroconf as r
21-
from zeroconf import _core, _protocol, const, Zeroconf, current_time_millis
21+
from zeroconf import _core, const, Zeroconf, current_time_millis
2222
from zeroconf.asyncio import AsyncZeroconf
23+
from zeroconf._protocol import outgoing
2324

2425
from . import has_working_ipv6, _clear_cache, _inject_response, _wait_for_start
2526

@@ -670,8 +671,8 @@ def test_guard_against_oversized_packets():
670671
)
671672

672673
# We are patching to generate an oversized packet
673-
with patch.object(_protocol, "_MAX_MSG_ABSOLUTE", 100000), patch.object(
674-
_protocol, "_MAX_MSG_TYPICAL", 100000
674+
with patch.object(outgoing, "_MAX_MSG_ABSOLUTE", 100000), patch.object(
675+
outgoing, "_MAX_MSG_TYPICAL", 100000
675676
):
676677
over_sized_packet = generated.packets()[0]
677678
assert len(over_sized_packet) > const._MAX_MSG_ABSOLUTE

zeroconf/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
NonUniqueNameException,
4747
ServiceNameAlreadyRegistered,
4848
)
49-
from ._protocol import DNSIncoming, DNSOutgoing # noqa # import needed for backwards compat
49+
from ._protocol.incoming import DNSIncoming # noqa # import needed for backwards compat
50+
from ._protocol.outgoing import DNSOutgoing # noqa # import needed for backwards compat
5051
from ._services import ( # noqa # import needed for backwards compat
5152
Signal,
5253
SignalRegistrationInterface,

zeroconf/_core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
)
4242
from ._history import QuestionHistory
4343
from ._logger import QuietLogger, log
44-
from ._protocol import DNSIncoming, DNSOutgoing
44+
from ._protocol.incoming import DNSIncoming
45+
from ._protocol.outgoing import DNSOutgoing
4546
from ._services import ServiceListener
4647
from ._services.browser import ServiceBrowser
4748
from ._services.info import ServiceInfo, instance_name_from_service_info

zeroconf/_dns.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848

4949

5050
if TYPE_CHECKING:
51-
from ._protocol import DNSIncoming, DNSOutgoing
51+
from ._protocol.incoming import DNSIncoming
52+
from ._protocol.outgoing import DNSOutgoing
5253

5354

5455
@enum.unique

zeroconf/_handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
from ._dns import DNSAddress, DNSNsec, DNSPointer, DNSQuestion, DNSRRSet, DNSRecord
3030
from ._history import QuestionHistory
3131
from ._logger import log
32-
from ._protocol import DNSIncoming, DNSOutgoing
32+
from ._protocol.incoming import DNSIncoming
33+
from ._protocol.outgoing import DNSOutgoing
3334
from ._services.info import ServiceInfo
3435
from ._services.registry import ServiceRegistry
3536
from ._updates import RecordUpdate, RecordUpdateListener

zeroconf/_protocol/__init__.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 ..const import (
24+
_FLAGS_QR_MASK,
25+
_FLAGS_QR_QUERY,
26+
_FLAGS_QR_RESPONSE,
27+
_FLAGS_TC,
28+
)
29+
30+
31+
class DNSMessage:
32+
"""A base class for DNS messages."""
33+
34+
__slots__ = ('flags',)
35+
36+
def __init__(self, flags: int) -> None:
37+
"""Construct a DNS message."""
38+
self.flags = flags
39+
40+
def is_query(self) -> bool:
41+
"""Returns true if this is a query."""
42+
return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY
43+
44+
def is_response(self) -> bool:
45+
"""Returns true if this is a response."""
46+
return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE
47+
48+
@property
49+
def truncated(self) -> bool:
50+
"""Returns true if this is a truncated."""
51+
return (self.flags & _FLAGS_TC) == _FLAGS_TC

0 commit comments

Comments
 (0)