|
| 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 | +import logging |
| 24 | +from typing import TYPE_CHECKING, Optional, Tuple, Union |
| 25 | + |
| 26 | +from ._logger import QuietLogger, log |
| 27 | +from ._protocol.outgoing import DNSOutgoing |
| 28 | +from ._transport import _WrappedTransport |
| 29 | +from ._utils.net import ( |
| 30 | + can_send_to, |
| 31 | +) |
| 32 | +from .const import ( |
| 33 | + _MAX_MSG_ABSOLUTE, |
| 34 | + _MDNS_ADDR, |
| 35 | + _MDNS_ADDR6, |
| 36 | + _MDNS_PORT, |
| 37 | +) |
| 38 | + |
| 39 | +if TYPE_CHECKING: |
| 40 | + from ._core import Zeroconf |
| 41 | + |
| 42 | + |
| 43 | +def async_send_with_transport( |
| 44 | + log_debug: bool, |
| 45 | + transport: _WrappedTransport, |
| 46 | + packet: bytes, |
| 47 | + packet_num: int, |
| 48 | + out: DNSOutgoing, |
| 49 | + addr: Optional[str], |
| 50 | + port: int, |
| 51 | + v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = (), |
| 52 | +) -> None: |
| 53 | + ipv6_socket = transport.is_ipv6 |
| 54 | + if addr is None: |
| 55 | + real_addr = _MDNS_ADDR6 if ipv6_socket else _MDNS_ADDR |
| 56 | + else: |
| 57 | + real_addr = addr |
| 58 | + if not can_send_to(ipv6_socket, real_addr): |
| 59 | + return |
| 60 | + if log_debug: |
| 61 | + log.debug( |
| 62 | + "Sending to (%s, %d) via [socket %s (%s)] (%d bytes #%d) %r as %r...", |
| 63 | + real_addr, |
| 64 | + port or _MDNS_PORT, |
| 65 | + transport.fileno, |
| 66 | + transport.sock_name, |
| 67 | + len(packet), |
| 68 | + packet_num + 1, |
| 69 | + out, |
| 70 | + packet, |
| 71 | + ) |
| 72 | + # Get flowinfo and scopeid for the IPV6 socket to create a complete IPv6 |
| 73 | + # address tuple: https://docs.python.org/3.6/library/socket.html#socket-families |
| 74 | + if ipv6_socket and not v6_flow_scope: |
| 75 | + _, _, sock_flowinfo, sock_scopeid = transport.sock_name |
| 76 | + v6_flow_scope = (sock_flowinfo, sock_scopeid) |
| 77 | + transport.transport.sendto(packet, (real_addr, port or _MDNS_PORT, *v6_flow_scope)) |
| 78 | + |
| 79 | + |
| 80 | +class _ZeroconfSender: |
| 81 | + """Send implementation for Zeroconf.""" |
| 82 | + |
| 83 | + __slots__ = ("zc", "loop", "done", "engine") |
| 84 | + |
| 85 | + def __init__(self, zc: "Zeroconf") -> None: |
| 86 | + """Initialize the ZeroconfSender.""" |
| 87 | + self.zc = zc |
| 88 | + self.loop = zc.loop |
| 89 | + self.done = zc.done |
| 90 | + self.engine = zc.engine |
| 91 | + |
| 92 | + def send( |
| 93 | + self, |
| 94 | + out: DNSOutgoing, |
| 95 | + addr: Optional[str] = None, |
| 96 | + port: int = _MDNS_PORT, |
| 97 | + v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = (), |
| 98 | + transport: Optional[_WrappedTransport] = None, |
| 99 | + ) -> None: |
| 100 | + """Sends an outgoing packet threadsafe.""" |
| 101 | + assert self.loop is not None |
| 102 | + self.loop.call_soon_threadsafe(self.async_send, out, addr, port, v6_flow_scope, transport) |
| 103 | + |
| 104 | + def async_send( |
| 105 | + self, |
| 106 | + out: DNSOutgoing, |
| 107 | + addr: Optional[str] = None, |
| 108 | + port: int = _MDNS_PORT, |
| 109 | + v6_flow_scope: Union[Tuple[()], Tuple[int, int]] = (), |
| 110 | + transport: Optional[_WrappedTransport] = None, |
| 111 | + ) -> None: |
| 112 | + """Sends an outgoing packet.""" |
| 113 | + if self.done: |
| 114 | + return |
| 115 | + |
| 116 | + # If no transport is specified, we send to all the ones |
| 117 | + # with the same address family |
| 118 | + transports = [transport] if transport else self.engine.senders |
| 119 | + log_debug = log.isEnabledFor(logging.DEBUG) |
| 120 | + |
| 121 | + for packet_num, packet in enumerate(out.packets()): |
| 122 | + if len(packet) > _MAX_MSG_ABSOLUTE: |
| 123 | + QuietLogger.log_warning_once( |
| 124 | + "Dropping %r over-sized packet (%d bytes) %r", |
| 125 | + out, |
| 126 | + len(packet), |
| 127 | + packet, |
| 128 | + ) |
| 129 | + return |
| 130 | + for send_transport in transports: |
| 131 | + async_send_with_transport( |
| 132 | + log_debug, |
| 133 | + send_transport, |
| 134 | + packet, |
| 135 | + packet_num, |
| 136 | + out, |
| 137 | + addr, |
| 138 | + port, |
| 139 | + v6_flow_scope, |
| 140 | + ) |
0 commit comments