diff --git a/.claude/skills/pr-workflow/SKILL.md b/.claude/skills/pr-workflow/SKILL.md new file mode 100644 index 000000000..72bbef708 --- /dev/null +++ b/.claude/skills/pr-workflow/SKILL.md @@ -0,0 +1,130 @@ +--- +name: pr-workflow +description: Create pull requests for python-zeroconf/python-zeroconf. Use when creating PRs, submitting changes, or preparing contributions. +allowed-tools: Read, Bash, Glob, Grep +--- + +# python-zeroconf PR Workflow + +When creating a pull request for `python-zeroconf/python-zeroconf`, +follow these steps. Repo-wide conventions live in +[CLAUDE.md](../../../CLAUDE.md); this skill summarises the parts +that matter at PR-creation time. + +## 1. Create branch from origin/master + +The default branch is `master`, not `main`. `origin` already +points at `python-zeroconf/python-zeroconf` — there is no fork in +this workflow. Always re-fetch first so the branch is based on +the latest `master`: + +```bash +git fetch origin +git checkout -b origin/master +``` + +If you accidentally branch from `main`, `gh pr create` will fail +because the base branch does not exist. + +## 2. There is no PR template + +`python-zeroconf` does not ship a `.github/PULL_REQUEST_TEMPLATE.md` +— PR bodies are free-form. Aim for a body that looks roughly like: + +``` +## Summary +<1–3 sentence prose description of what changed and why> + +## Details + + +## Test plan +- [ ] +- [ ] +``` + +Cite the relevant RFC section (RFC 6762 / RFC 6763) for any +behaviour change that affects packet contents or timing — +reviewers shouldn't have to reverse-engineer why a constant moved +or a probe interval changed. + +## 3. PR title conventions + +PRs are squash-merged, so the PR title becomes the commit on +`master`. Only the PR title is linted (by the `pr-title` CI job +running `amannn/action-semantic-pull-request`); per-commit +messages on the PR branch are not checked. + +- **Conventional Commits prefix is required on the PR title.** + Pick from: `feat`, `fix`, `perf`, `refactor`, `docs`, `test`, + `build`, `ci`, `chore`, `style`, `revert`. The + `feat`/`fix`/`perf` prefixes show up in the release-notes; + `chore*` and `ci*` are excluded by semantic-release + (`exclude_commit_patterns` in `pyproject.toml`), so use those + for housekeeping. +- **Imperative-mood subject.** "fix: handle empty answer", not + "fix: handled empty answer". +- **Lowercase first character after the prefix** (enforced by + `subjectPattern: ^(?![A-Z]).+$`). +- **No `Co-Authored-By` trailers from automated agents.** +- **One logical change per PR.** Let pre-commit run (ruff + lint + format, mypy, flake8, codespell, cython-lint, + pyupgrade). If a hook auto-fixes something, re-stage and + re-commit. + +## 4. Cython / `.pxd` discipline + +If the PR touches any module listed in `TO_CYTHONIZE` +(`build_ext.py`): + +- Update the sibling `.pxd` in the same commit if you changed a + `cdef class` layout or a `cpdef`/`cdef` signature. +- Do not hand-edit the in-tree `.c` files; the build regenerates + them, and they're excluded from sdist (`exclude = ["**/*.c"]` + in `pyproject.toml`). +- Verify the extension still builds locally: + `REQUIRE_CYTHON=1 poetry install` (re-installs in-place, + failing loudly if Cython rejects anything). +- Verify it still works without the extension: + `SKIP_CYTHON=1 poetry install && poetry run pytest tests/`. + +## 5. Push and create the PR + +```bash +git push -u origin +gh pr create --repo python-zeroconf/python-zeroconf --base master \ + --title "" \ + --body-file /tmp/pr-body.md +``` + +Always pass the body via `--body-file`, never `--body "..."` with +shell-escaping — Markdown backticks, asterisks, and angle +brackets must pass through verbatim. + +The PR title is what gets enforced — it becomes the squash-merge +commit subject on `master`, so it has to parse as a Conventional +Commit on its own. Per-commit messages on the branch are not +linted. + +## 6. After the PR is open + +CI runs three jobs: + +- `lint` — `pre-commit/action`. If pre-commit passed locally + this passes too. +- `pr-title` — `amannn/action-semantic-pull-request`. Validates + the PR title against Conventional Commits. If it fails, fix + the title in the GitHub UI or with `gh pr edit --title "..."`; + the workflow re-runs on the edit, no push needed. +- `test` — the full pytest matrix across CPython 3.10–3.14, + 3.14t (free-threaded), and PyPy 3.10, on Linux + macOS + + Windows. The free-threaded entry is the canary for unguarded + shared-state bugs; failures there are often genuine even when + the GIL-enabled rows pass. + +CodSpeed also runs on PRs (`CodSpeedHQ/action`) and posts a +benchmark delta as a check. A regression there is signal — if +the PR is a perf change, the comment is the evidence; if not, a +red CodSpeed check usually means the hot path picked up an extra +Python-level branch and wants a second look. diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 000000000..13add00a3 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,5 @@ +[report] +exclude_lines = + pragma: no cover + if TYPE_CHECKING: + if sys.version_info diff --git a/.flake8 b/.flake8 new file mode 100644 index 000000000..bb5e15d37 --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +exclude = docs +max-line-length = 180 +extend-ignore = E203 diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 000000000..ba2becff2 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,21 @@ +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file + +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "monthly" + commit-message: + prefix: "chore(ci): " + groups: + github-actions: + patterns: + - "*" + - package-ecosystem: "pip" # See documentation for possible values + directory: "/" # Location of package manifests + schedule: + interval: "weekly" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..2ac9d2125 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,365 @@ +name: CI + +on: + push: + branches: + - master + - release-0.x + pull_request: + +concurrency: + group: ${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +env: + POETRY_VIRTUALENVS_IN_PROJECT: "true" + UV_PYTHON_PREFERENCE: only-managed # avoid ancient system Python + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4 + - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v5 + with: + python-version: "3.12" + - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 + + # Make sure the PR title follows the conventional commits convention: + # https://www.conventionalcommits.org + # PRs are squash-merged, so the PR title becomes the commit on master and + # drives python-semantic-release's version bump. + pr-title: + name: Lint PR Title + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + permissions: + pull-requests: read + steps: + - uses: amannn/action-semantic-pull-request@48f256284bd46cdaab1048c3721360e808335d50 # v6.1.1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + subjectPattern: ^(?![A-Z]).+$ + subjectPatternError: | + The subject "{subject}" found in the pull request title "{title}" + didn't match the configured pattern. Please ensure that the subject + starts with a lowercase character. + + test: + strategy: + fail-fast: false + matrix: + python-version: + - "3.10" + - "3.11" + - "3.12" + - "3.13" + - "3.14" + - "3.14t" + - "pypy-3.10" + os: + - ubuntu-latest + - macos-latest + - windows-latest + extension: + - "skip_cython" + - "use_cython" + exclude: + - os: macos-latest + extension: use_cython + - os: windows-latest + extension: use_cython + - os: windows-latest + python-version: "pypy-3.10" + - os: macos-latest + python-version: "pypy-3.10" + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4 + - name: Set up uv + uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0 + with: + enable-cache: true + - name: Install poetry + run: uv tool install poetry + - name: Set up Python + id: setup-python + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v5 + with: + python-version: ${{ matrix.python-version }} + cache: "poetry" + allow-prereleases: true + - name: Cache poetry venv + id: cache-venv + uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 + with: + path: | + .venv + src/zeroconf/**/*.so + key: venv-v1-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-${{ matrix.extension }}-${{ hashFiles('poetry.lock', 'pyproject.toml', 'build_ext.py', 'src/zeroconf/**/*.py', 'src/zeroconf/**/*.pxd') }} + - name: Install Dependencies no cython + if: ${{ matrix.extension == 'skip_cython' && steps.cache-venv.outputs.cache-hit != 'true' }} + env: + SKIP_CYTHON: 1 + run: poetry install --only=main,dev + - name: Install Dependencies with cython + if: ${{ matrix.extension != 'skip_cython' && steps.cache-venv.outputs.cache-hit != 'true' }} + env: + REQUIRE_CYTHON: 1 + run: poetry install --only=main,dev + - name: Test with Pytest + run: poetry run pytest --durations=20 --timeout=60 -v --cov=zeroconf --cov-branch --cov-report xml --cov-report html --cov-report term-missing tests + - name: Upload coverage to Codecov + uses: codecov/codecov-action@57e3a136b779b570ffcdbf80b3bdc90e7fab3de2 # v5 + with: + token: ${{ secrets.CODECOV_TOKEN }} + + benchmark: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4 + - name: Setup Python 3.13 + id: setup-python + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v5 + with: + python-version: 3.13 + - name: Set up uv + uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0 + with: + enable-cache: true + - name: Install poetry + run: uv tool install poetry + - name: Cache poetry venv + id: cache-venv + uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 + with: + path: | + .venv + src/zeroconf/**/*.so + key: venv-v1-${{ runner.os }}-benchmark-py${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock', 'pyproject.toml', 'build_ext.py', 'src/zeroconf/**/*.py', 'src/zeroconf/**/*.pxd') }} + - name: Install Dependencies + if: steps.cache-venv.outputs.cache-hit != 'true' + run: | + REQUIRE_CYTHON=1 poetry install --only=main,dev + shell: bash + - name: Run benchmarks + uses: CodSpeedHQ/action@3194d9a39c4d46684cb44bf7207fc56626aad8fd # v3 + with: + token: ${{ secrets.CODSPEED_TOKEN }} + run: poetry run pytest --no-cov -vvvvv --codspeed tests/benchmarks + mode: instrumentation + + release: + needs: + - test + - lint + if: ${{ github.repository_owner }} == "python-zeroconf" + + runs-on: ubuntu-latest + environment: release + concurrency: + group: release-${{ github.head_ref || github.ref }} + cancel-in-progress: false + permissions: + id-token: write + contents: write + outputs: + released: ${{ steps.release.outputs.released }} + newest_release_tag: ${{ steps.release.outputs.tag }} + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4 + with: + fetch-depth: 0 + ref: ${{ github.ref }} + + - name: Create local branch name + run: git switch -C ${{ github.head_ref || github.ref_name }} + + # Do a dry run of PSR + - name: Test release + uses: python-semantic-release/python-semantic-release@350c48fcb3ffcdfd2e0a235206bc2ecea6b69df0 # v10.5.3 + if: github.ref_name != 'master' + with: + no_operation_mode: true + + # On main branch: actual PSR + upload to PyPI & GitHub + - name: Release + uses: python-semantic-release/python-semantic-release@350c48fcb3ffcdfd2e0a235206bc2ecea6b69df0 # v10.5.3 + id: release + if: github.ref_name == 'master' + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + + - name: Publish package distributions to PyPI + uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # release/v1 + if: steps.release.outputs.released == 'true' + + - name: Publish package distributions to GitHub Releases + uses: python-semantic-release/publish-action@310a9983a0ae878b29f3aac778d7c77c1db27378 # v10.5.3 + if: steps.release.outputs.released == 'true' + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + + build_wheels: + needs: [release] + if: needs.release.outputs.released == 'true' + + name: Wheels for ${{ matrix.os }} (${{ matrix.musl == 'musllinux' && 'musllinux' || 'manylinux' }}) ${{ matrix.qemu }} ${{ matrix.pyver }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: + [ + ubuntu-24.04-arm, + ubuntu-latest, + windows-latest, + macos-latest, + ] + qemu: [""] + musl: [""] + pyver: [""] + include: + - os: ubuntu-latest + musl: "musllinux" + - os: ubuntu-24.04-arm + musl: "musllinux" + # qemu is slow, make a single + # runner per Python version + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "musllinux" + pyver: cp310 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "musllinux" + pyver: cp311 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "musllinux" + pyver: cp312 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "musllinux" + pyver: cp313 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "musllinux" + pyver: cp314 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "musllinux" + pyver: cp314t + # qemu is slow, make a single + # runner per Python version + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "" + pyver: cp310 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "" + pyver: cp311 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "" + pyver: cp312 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "" + pyver: cp313 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "" + pyver: cp314 + - os: ubuntu-24.04-arm + qemu: armv7l + musl: "" + pyver: cp314t + # qemu is slow, make a single runner per Python version + - {os: ubuntu-latest, qemu: riscv64, musl: "musllinux", pyver: cp310} + - {os: ubuntu-latest, qemu: riscv64, musl: "musllinux", pyver: cp311} + - {os: ubuntu-latest, qemu: riscv64, musl: "musllinux", pyver: cp312} + - {os: ubuntu-latest, qemu: riscv64, musl: "musllinux", pyver: cp313} + - {os: ubuntu-latest, qemu: riscv64, musl: "musllinux", pyver: cp314} + - {os: ubuntu-latest, qemu: riscv64, musl: "musllinux", pyver: cp314t} + - {os: ubuntu-latest, qemu: riscv64, musl: "", pyver: cp310} + - {os: ubuntu-latest, qemu: riscv64, musl: "", pyver: cp311} + - {os: ubuntu-latest, qemu: riscv64, musl: "", pyver: cp312} + - {os: ubuntu-latest, qemu: riscv64, musl: "", pyver: cp313} + - {os: ubuntu-latest, qemu: riscv64, musl: "", pyver: cp314} + - {os: ubuntu-latest, qemu: riscv64, musl: "", pyver: cp314t} + steps: + - name: Checkout + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4 + with: + fetch-depth: 0 + ref: "master" + # Used to host cibuildwheel + - name: Set up Python + uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v5 + with: + python-version: "3.12" + - name: Set up QEMU + if: ${{ matrix.qemu }} + uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0 + with: + platforms: all + # This should be temporary + # xref https://github.com/docker/setup-qemu-action/issues/188 + # xref https://github.com/tonistiigi/binfmt/issues/215 + image: tonistiigi/binfmt:qemu-v8.1.5 + id: qemu + - name: Prepare emulation + if: ${{ matrix.qemu }} + run: | + if [[ -n "${{ matrix.qemu }}" ]]; then + # Build emulated architectures only if QEMU is set, + # use default "auto" otherwise + echo "CIBW_ARCHS_LINUX=${{ matrix.qemu }}" >> $GITHUB_ENV + fi + - name: Limit to a specific Python version on slow QEMU + if: ${{ matrix.pyver }} + run: | + if [[ -n "${{ matrix.pyver }}" ]]; then + echo "CIBW_BUILD=${{ matrix.pyver }}*" >> $GITHUB_ENV + fi + + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4 + with: + ref: ${{ needs.release.outputs.newest_release_tag }} + fetch-depth: 0 + + - name: Build wheels ${{ matrix.musl }} (${{ matrix.qemu }}) + uses: pypa/cibuildwheel@8d2b08b68458a16aeb24b64e68a09ab1c8e82084 # v3.4.1 + # to supply options, put them in 'env', like: + env: + CIBW_SKIP: cp38-* cp39-* pp38-* pp39-* ${{ matrix.musl == 'musllinux' && '*manylinux*' || '*musllinux*' }} + CIBW_BEFORE_ALL_LINUX: apt install -y gcc || yum install -y gcc || apk add gcc + CIBW_ARCHS_MACOS: arm64 + REQUIRE_CYTHON: 1 + + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v4 + with: + path: ./wheelhouse/*.whl + name: wheels-${{ matrix.os }}-${{ matrix.musl }}-${{ matrix.qemu }}-${{ matrix.pyver }} + + upload_pypi: + needs: [build_wheels] + runs-on: ubuntu-latest + environment: release + permissions: + id-token: write # IMPORTANT: this permission is mandatory for trusted publishing + + steps: + - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v4 + with: + # unpacks default artifact into dist/ + # if `name: artifact` is omitted, the action will create extra parent dir + path: dist + pattern: wheels-* + merge-multiple: true + + - uses: + pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0 diff --git a/.gitignore b/.gitignore index 8b23c0e1e..4dde1f97b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ build/ *.pyc *.pyo +.coverage +coverage.xml +htmlcov/ Thumbs.db .DS_Store .project @@ -10,3 +13,8 @@ Thumbs.db .vslick .cache .mypy_cache/ +docs/_build/ +.vscode +/dist/ +/zeroconf.egg-info/ +/src/**/*.c diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..1e6df8db7 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,60 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +exclude: "CHANGELOG.md" +default_stages: [pre-commit] + +ci: + autofix_commit_msg: "chore(pre-commit.ci): auto fixes" + autoupdate_commit_msg: "chore(pre-commit.ci): pre-commit autoupdate" + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v6.0.0 + hooks: + - id: check-builtin-literals + - id: check-case-conflict + - id: check-docstring-first + - id: check-json + - id: check-shebang-scripts-are-executable + - id: check-toml + - id: check-xml + - id: check-yaml + - id: debug-statements + - id: detect-private-key + - id: end-of-file-fixer + - id: trailing-whitespace + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v4.0.0-alpha.8 + hooks: + - id: prettier + args: ["--tab-width", "2"] + files: ".(css|html|js|json|md|toml|yaml)$" + - repo: https://github.com/asottile/pyupgrade + rev: v3.21.2 + hooks: + - id: pyupgrade + args: [--py310-plus] + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.15.13 + hooks: + - id: ruff + args: [--fix, --exit-non-zero-on-fix] + - id: ruff-format + - repo: https://github.com/codespell-project/codespell + rev: v2.4.2 + hooks: + - id: codespell + - repo: https://github.com/PyCQA/flake8 + rev: 7.3.0 + hooks: + - id: flake8 + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v2.1.0 + hooks: + - id: mypy + additional_dependencies: [ifaddr] + - repo: https://github.com/MarcoGorelli/cython-lint + rev: v0.19.0 + hooks: + - id: cython-lint + - id: double-quote-cython-strings diff --git a/.readthedocs.yaml b/.readthedocs.yaml new file mode 100644 index 000000000..aee2616a1 --- /dev/null +++ b/.readthedocs.yaml @@ -0,0 +1,16 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details +version: 2 + +build: + os: ubuntu-24.04 + tools: + python: "3.12" + jobs: + post_install: + # https://docs.readthedocs.com/platform/stable/build-customization.html#install-dependencies-with-poetry + - pip install poetry + - SKIP_CYTHON=1 VIRTUAL_ENV=$READTHEDOCS_VIRTUALENV_PATH poetry install --with docs + +sphinx: + configuration: docs/conf.py diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 90625484f..000000000 --- a/.travis.yml +++ /dev/null @@ -1,21 +0,0 @@ -language: python -python: - - "3.4" - - "3.5" - - "3.6" - - "pypy3.5-5.10.1" -matrix: - fast_finish: true - include: - - { python: "3.7", dist: xenial, sudo: true } -install: - - pip install -r requirements-dev.txt - # mypy can't be installed on pypy - - if [[ "${TRAVIS_PYTHON_VERSION}" != "pypy"* ]] ; then pip install mypy ; fi -script: - - make test_coverage - - flake8 --version - - make flake8 - - if [[ "${TRAVIS_PYTHON_VERSION}" != "pypy"* ]] ; then make mypy ; fi -after_success: - - coveralls diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..d303d06a3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2311 @@ +# CHANGELOG + + + +## v0.149.16 (2026-05-21) + +### Bug Fixes + +- Re-release for GHSA-qc2x-6f54-m6h9 + ([#1770](https://github.com/python-zeroconf/python-zeroconf/pull/1770), + [`fad8646`](https://github.com/python-zeroconf/python-zeroconf/commit/fad86461630237d1f0c04e3745fef65e4cc3055c)) + + +## v0.149.15 (2026-05-21) + +### Bug Fixes + +- Preserve scope_id when scoped AAAA arrives alongside unscoped + ([#1764](https://github.com/python-zeroconf/python-zeroconf/pull/1764), + [`e2352ea`](https://github.com/python-zeroconf/python-zeroconf/commit/e2352ea84437d6fca81dfbdc41116feaaf45fefc)) + + +## v0.149.14 (2026-05-20) + +### Bug Fixes + +- Skip NSEC records in ServiceBrowser to suppress spurious updates + ([#1762](https://github.com/python-zeroconf/python-zeroconf/pull/1762), + [`137a5d6`](https://github.com/python-zeroconf/python-zeroconf/commit/137a5d6c29389ffcd8abfba0925d8cbe58cb2c1b)) + +### Testing + +- Add blockbuster to detect blocking calls in asyncio tests + ([#1761](https://github.com/python-zeroconf/python-zeroconf/pull/1761), + [`90a5a39`](https://github.com/python-zeroconf/python-zeroconf/commit/90a5a39df7400926bc5498a86b9fe4c46eaffd44)) + +- Scale aggregation timings 10x to speed up timing-dependent tests + ([#1759](https://github.com/python-zeroconf/python-zeroconf/pull/1759), + [`3e5ac4f`](https://github.com/python-zeroconf/python-zeroconf/commit/3e5ac4fbc5cd8d8b5ffed5cc8994782e7157cfad)) + +- Shave loopback timing overhead from remaining slow tests + ([#1760](https://github.com/python-zeroconf/python-zeroconf/pull/1760), + [`343dc7a`](https://github.com/python-zeroconf/python-zeroconf/commit/343dc7a305b47a574f58265081f172ed54f461bb)) + +- Widen QM follow-up window in info_asking_default test + ([#1765](https://github.com/python-zeroconf/python-zeroconf/pull/1765), + [`4ffba87`](https://github.com/python-zeroconf/python-zeroconf/commit/4ffba87177fcc11a0fd8ccb7d93c6996a1269a26)) + + +## v0.149.13 (2026-05-20) + +### Bug Fixes + +- Bound record payload reads against rdlength overrun + ([#1756](https://github.com/python-zeroconf/python-zeroconf/pull/1756), + [`5444495`](https://github.com/python-zeroconf/python-zeroconf/commit/544449596e645fcaad3834fa0cb614a54f847a82)) + +### Documentation + +- Clarify LGPL-2.1-or-later license in README + ([#1763](https://github.com/python-zeroconf/python-zeroconf/pull/1763), + [`28bb01f`](https://github.com/python-zeroconf/python-zeroconf/commit/28bb01f23951f7883d8c3af66b6d537c34c516c7)) + +### Refactoring + +- Extract loopback Zeroconf fixtures and mock_incoming_msg helper + ([#1758](https://github.com/python-zeroconf/python-zeroconf/pull/1758), + [`cb0af4a`](https://github.com/python-zeroconf/python-zeroconf/commit/cb0af4a8f4cdaad3721a2851d9fa17709d39ae62)) + + +## v0.149.12 (2026-05-20) + +### Bug Fixes + +- Bound QuestionHistory per-entry known-answer payload + ([#1755](https://github.com/python-zeroconf/python-zeroconf/pull/1755), + [`4ff6540`](https://github.com/python-zeroconf/python-zeroconf/commit/4ff65407bdc097f73a8b1f98659572e24d5c0df1)) + +- Bound TC-deferred queues against spoofed-source flood OOM + ([#1751](https://github.com/python-zeroconf/python-zeroconf/pull/1751), + [`b22c8ff`](https://github.com/python-zeroconf/python-zeroconf/commit/b22c8ff19c66c68907d220a4823c0950f4fa93f7)) + + +## v0.149.11 (2026-05-20) + +### Bug Fixes + +- Bound duplicate-packet dedup against alternating-payload floods + ([#1750](https://github.com/python-zeroconf/python-zeroconf/pull/1750), + [`8c9d6ce`](https://github.com/python-zeroconf/python-zeroconf/commit/8c9d6ce0ccdb8854d14606c93f3790482363e1b9)) + + +## v0.149.10 (2026-05-20) + +### Bug Fixes + +- Accept uppercase .local. trailer in service_type_name + ([#1747](https://github.com/python-zeroconf/python-zeroconf/pull/1747), + [`37edde2`](https://github.com/python-zeroconf/python-zeroconf/commit/37edde2f5d9688e9d6c6573ee41a7cd25a54111e)) + +- Bound TC-deferral assembly window to first-arrival + max delay + ([#1732](https://github.com/python-zeroconf/python-zeroconf/pull/1732), + [`a096238`](https://github.com/python-zeroconf/python-zeroconf/commit/a0962385a5079d6204fac7744fee9a9d67233eec)) + +### Testing + +- Add codspeed benchmarks for listener duplicate-packet dedup + ([#1744](https://github.com/python-zeroconf/python-zeroconf/pull/1744), + [`068c3f6`](https://github.com/python-zeroconf/python-zeroconf/commit/068c3f68aeaaaf085d5fa197f7bff304ab80f847)) + + +## v0.149.9 (2026-05-20) + +### Bug Fixes + +- Bound QuestionHistory size to prevent LAN-driven OOM + ([#1733](https://github.com/python-zeroconf/python-zeroconf/pull/1733), + [`0e5e637`](https://github.com/python-zeroconf/python-zeroconf/commit/0e5e637172ab7991e8e1f13be7e4e5d228ce8b8b)) + + +## v0.149.8 (2026-05-19) + +### Bug Fixes + +- Bound NSEC bitmap length against record end + ([#1731](https://github.com/python-zeroconf/python-zeroconf/pull/1731), + [`1d83550`](https://github.com/python-zeroconf/python-zeroconf/commit/1d83550c58ed0ea69b611a907cd4bdfcb2eef535)) + +### Testing + +- Give IPv6-only loopback find() its own timeout + ([#1721](https://github.com/python-zeroconf/python-zeroconf/pull/1721), + [`fcd1ffb`](https://github.com/python-zeroconf/python-zeroconf/commit/fcd1ffb4af9b3b26bc0ecae30641251a4c9a4ba6)) + +- Widen LOOPBACK_FIND_TIMEOUT under PyPy + ([#1742](https://github.com/python-zeroconf/python-zeroconf/pull/1742), + [`6924606`](https://github.com/python-zeroconf/python-zeroconf/commit/69246065085fa25f9baf20abbf0eaddcb4a4d88c)) + +- Widen safety margin in test_response_aggregation_timings_multiple + ([#1737](https://github.com/python-zeroconf/python-zeroconf/pull/1737), + [`228af17`](https://github.com/python-zeroconf/python-zeroconf/commit/228af178657a70dd2e76420a8187345de6ede46f)) + + +## v0.149.7 (2026-05-18) + +### Bug Fixes + +- Bound DNSCache record count to prevent unbounded LAN-driven growth + ([#1718](https://github.com/python-zeroconf/python-zeroconf/pull/1718), + [`0ad3f37`](https://github.com/python-zeroconf/python-zeroconf/commit/0ad3f37b5b852b8f614d322283d148efb2cef6e4)) + +### Testing + +- Shave ServiceBrowser first-query delay on loopback + ([#1720](https://github.com/python-zeroconf/python-zeroconf/pull/1720), + [`0ff3c6b`](https://github.com/python-zeroconf/python-zeroconf/commit/0ff3c6b9dd40e01263ce88803139c3ba68349682)) + + +## v0.149.6 (2026-05-18) + +### Bug Fixes + +- Bound _seen_logs and stop retaining exc_info + ([#1717](https://github.com/python-zeroconf/python-zeroconf/pull/1717), + [`95561e2`](https://github.com/python-zeroconf/python-zeroconf/commit/95561e28b24922358f1991e38e3a86d70d72dcec)) + + +## v0.149.5 (2026-05-18) + +### Bug Fixes + +- Bound DNS compression-pointer chain depth in DNSIncoming + ([#1719](https://github.com/python-zeroconf/python-zeroconf/pull/1719), + [`f9e2359`](https://github.com/python-zeroconf/python-zeroconf/commit/f9e23592137f30fdf7ef710dba065da31c79b1cf)) + +### Testing + +- Cap helper get_service_info timeout in suppression test (~3.0s → ~1.85s) + ([#1708](https://github.com/python-zeroconf/python-zeroconf/pull/1708), + [`ee3c7d7`](https://github.com/python-zeroconf/python-zeroconf/commit/ee3c7d74ff45327a3a6d520b86a691e21e2bc219)) + +- Drop ZeroconfServiceTypes.find() timeouts from 500ms to 200ms on loopback + ([#1710](https://github.com/python-zeroconf/python-zeroconf/pull/1710), + [`64d143d`](https://github.com/python-zeroconf/python-zeroconf/commit/64d143d2ee7874ee1d9cef0dd2799c008b4aa791)) + +- Eliminate test_get_info_single race by injecting from the send mock + ([#1716](https://github.com/python-zeroconf/python-zeroconf/pull/1716), + [`963d3d7`](https://github.com/python-zeroconf/python-zeroconf/commit/963d3d70e1cde056967eba0d8747ddcd247ae707)) + +- Fix race in test_register_and_lookup_type_by_uppercase_name + ([#1712](https://github.com/python-zeroconf/python-zeroconf/pull/1712), + [`91aa21d`](https://github.com/python-zeroconf/python-zeroconf/commit/91aa21d52a0873f5fc12d43675b1b521dfe20519)) + +- Speed up service-info request tests with quick_request_timing fixture + ([#1709](https://github.com/python-zeroconf/python-zeroconf/pull/1709), + [`4bae30a`](https://github.com/python-zeroconf/python-zeroconf/commit/4bae30a2ed0910ee7c4f1d0f92f2c400a7b10f31)) + + +## v0.149.4 (2026-05-17) + +### Bug Fixes + +- **core**: Release sockets when close runs before engine setup completes + ([#1706](https://github.com/python-zeroconf/python-zeroconf/pull/1706), + [`0deb56b`](https://github.com/python-zeroconf/python-zeroconf/commit/0deb56b78fe6cd701a43ce34dccd4b69d6dd6d36)) + +### Testing + +- Drop pending multicast responses before TOCTOU assertion + ([#1701](https://github.com/python-zeroconf/python-zeroconf/pull/1701), + [`d2058d9`](https://github.com/python-zeroconf/python-zeroconf/commit/d2058d95882c70fb2eb786d373a630314e656c15)) + +- Speed up slow loopback tests (closes #1697) + ([#1699](https://github.com/python-zeroconf/python-zeroconf/pull/1699), + [`dd341a3`](https://github.com/python-zeroconf/python-zeroconf/commit/dd341a378e904cbf9b9a09e5c2ac8ff7c9944cd0)) + +- Speed up slow loopback tests (closes #1700) + ([#1703](https://github.com/python-zeroconf/python-zeroconf/pull/1703), + [`d03ea36`](https://github.com/python-zeroconf/python-zeroconf/commit/d03ea364dea5866e0301ff11f6b78714ecf991e8)) + +- Speed up test_async_wait_unblocks_on_update + ([#1702](https://github.com/python-zeroconf/python-zeroconf/pull/1702), + [`653c385`](https://github.com/python-zeroconf/python-zeroconf/commit/653c38559c468672cf907d808d432dec0fb06968)) + +- Widen scheduling buffer in flaky get_info suppression test + ([#1698](https://github.com/python-zeroconf/python-zeroconf/pull/1698), + [`9b4db62`](https://github.com/python-zeroconf/python-zeroconf/commit/9b4db625e121c2d590ed8fe2f4d187d5e3bb9a73)) + + +## v0.149.3 (2026-05-17) + +### Bug Fixes + +- **ci**: Drop x86_64 mac wheels and clean up obsolete CIBW_SKIP entries + ([#1694](https://github.com/python-zeroconf/python-zeroconf/pull/1694), + [`104c5d6`](https://github.com/python-zeroconf/python-zeroconf/commit/104c5d6674896612aa83a89fd17b90de5f38a508)) + + +## v0.149.2 (2026-05-17) + +### Bug Fixes + +- **ci**: Drop retired macos-13 runner and skip cp39/pp39 in wheel matrix + ([#1693](https://github.com/python-zeroconf/python-zeroconf/pull/1693), + [`745198b`](https://github.com/python-zeroconf/python-zeroconf/commit/745198b1128213915c1c89829feb950046e3de91)) + + +## v0.149.1 (2026-05-16) + +### Bug Fixes + +- **ci**: Drop cp39 from cibuildwheel matrix + ([#1691](https://github.com/python-zeroconf/python-zeroconf/pull/1691), + [`591288b`](https://github.com/python-zeroconf/python-zeroconf/commit/591288ba77a872ce6ccfe040f9c73da89e180f8d)) + + +## v0.149.0 (2026-05-16) + +### Features + +- Drop Python 3.9 support ([#1688](https://github.com/python-zeroconf/python-zeroconf/pull/1688), + [`327b93d`](https://github.com/python-zeroconf/python-zeroconf/commit/327b93dc602707e25d53788f0fb14e142f4558b3)) + +### Performance Improvements + +- **build**: Parallelize cython extension compilation + ([#1689](https://github.com/python-zeroconf/python-zeroconf/pull/1689), + [`1ea6b94`](https://github.com/python-zeroconf/python-zeroconf/commit/1ea6b940ecbfd7e7654ad022cf7f4f888cf1daa5)) + + +## v0.147.4 (2026-05-16) + +### Bug Fixes + +- **core**: Close owned event loop on Zeroconf.close() to stop FD leak + ([#1685](https://github.com/python-zeroconf/python-zeroconf/pull/1685), + [`2f78370`](https://github.com/python-zeroconf/python-zeroconf/commit/2f78370c75d1082afe3191b7447aebfff1206657)) + +### Build System + +- Adjust actions checkout ref parameter on release + ([#1669](https://github.com/python-zeroconf/python-zeroconf/pull/1669), + [`bc8ec8d`](https://github.com/python-zeroconf/python-zeroconf/commit/bc8ec8d59d875522f75901644d423d30d803a030)) + +### Documentation + +- Add CLAUDE.md orientation file and pr-workflow skill + ([#1672](https://github.com/python-zeroconf/python-zeroconf/pull/1672), + [`8f8b4d6`](https://github.com/python-zeroconf/python-zeroconf/commit/8f8b4d6526729906337f4562c7b391745bb878af)) + +- Add Cython gotchas section to CLAUDE.md + ([#1679](https://github.com/python-zeroconf/python-zeroconf/pull/1679), + [`5cfb09d`](https://github.com/python-zeroconf/python-zeroconf/commit/5cfb09d89395cb507d436c45fc38edb9e44b94c8)) + +- Add SECURITY.md with private vulnerability reporting policy + ([#1675](https://github.com/python-zeroconf/python-zeroconf/pull/1675), + [`13f9048`](https://github.com/python-zeroconf/python-zeroconf/commit/13f9048f0f9786ce18e89daef04073847735a006)) + +### Testing + +- Add quick_timing fixture and apply to register-heavy tests + ([#1678](https://github.com/python-zeroconf/python-zeroconf/pull/1678), + [`d5e1f01`](https://github.com/python-zeroconf/python-zeroconf/commit/d5e1f01bb336ea19d982ce7d99f191723d3f18af)) + +- Fix flaky test_run_coro_with_timeout + ([#1683](https://github.com/python-zeroconf/python-zeroconf/pull/1683), + [`277f80d`](https://github.com/python-zeroconf/python-zeroconf/commit/277f80da2c0fea5b256f981bd3f425906f6b7be6)) + +- Pass timeout=0 explicitly in test_event_loop_blocked + ([#1676](https://github.com/python-zeroconf/python-zeroconf/pull/1676), + [`1b31ed5`](https://github.com/python-zeroconf/python-zeroconf/commit/1b31ed5fed9db1608255799701cd6f32b494952f)) + +- Pass timeout=200 to ServiceInfo-request timeout tests + ([#1677](https://github.com/python-zeroconf/python-zeroconf/pull/1677), + [`01ef6ff`](https://github.com/python-zeroconf/python-zeroconf/commit/01ef6ffd9ff442b3cfb37d2793e0ca6ad5148832)) + + +## v0.148.0 (2025-10-05) + +### Features + +- Trigger semantic releases for 0.x branch + ([#1626](https://github.com/python-zeroconf/python-zeroconf/pull/1626), + [`812a2b3`](https://github.com/python-zeroconf/python-zeroconf/commit/812a2b3ff4370593a7a0c3ad67389c76c434aa9b)) + + +## v0.147.3 (2025-10-04) + +### Bug Fixes + +- Update poetry to v2 ([#1623](https://github.com/python-zeroconf/python-zeroconf/pull/1623), + [`2c3c296`](https://github.com/python-zeroconf/python-zeroconf/commit/2c3c29655bd365213a7e0a4360b8dd860d833470)) + + +## v0.147.2 (2025-09-05) + +### Bug Fixes + +- Missing wheel builds for Windows + ([#1613](https://github.com/python-zeroconf/python-zeroconf/pull/1613), + [`f8e2381`](https://github.com/python-zeroconf/python-zeroconf/commit/f8e2381a500c78dcefeba3772822d5d3ec5f6060)) + + +## v0.147.1 (2025-09-05) + +### Bug Fixes + +- Increase check time and add random wait to avoid service collisions + ([#1611](https://github.com/python-zeroconf/python-zeroconf/pull/1611), + [`8c382ee`](https://github.com/python-zeroconf/python-zeroconf/commit/8c382eedc6da80031d9a7a42f299f95f115b7e47)) + +Co-authored-by: J. Nick Koston + + +## v0.147.0 (2025-05-03) + +### Features + +- Add cython 3.1 support ([#1580](https://github.com/python-zeroconf/python-zeroconf/pull/1580), + [`1d9c94a`](https://github.com/python-zeroconf/python-zeroconf/commit/1d9c94a82d8da16b8f5355131e6167b69293da6c)) + +- Cython 3.1 support ([#1578](https://github.com/python-zeroconf/python-zeroconf/pull/1578), + [`daaf8d6`](https://github.com/python-zeroconf/python-zeroconf/commit/daaf8d6981c778fe4ba0a63371d9368cf217891a)) + +- Cython 3.11 support ([#1579](https://github.com/python-zeroconf/python-zeroconf/pull/1579), + [`1569383`](https://github.com/python-zeroconf/python-zeroconf/commit/1569383c6cf8ce8977427cfdaf5c7104ce52ab08)) + + +## v0.146.5 (2025-04-14) + +### Bug Fixes + +- Address non-working socket configuration + ([#1563](https://github.com/python-zeroconf/python-zeroconf/pull/1563), + [`cc0f835`](https://github.com/python-zeroconf/python-zeroconf/commit/cc0f8350c30c82409b1a9bfecb19ff9b3368d6a7)) + +Co-authored-by: J. Nick Koston + + +## v0.146.4 (2025-04-14) + +### Bug Fixes + +- Avoid loading adapter list twice + ([#1564](https://github.com/python-zeroconf/python-zeroconf/pull/1564), + [`8359488`](https://github.com/python-zeroconf/python-zeroconf/commit/83594887521507cf77bfc0a397becabaaab287c2)) + + +## v0.146.3 (2025-04-02) + +### Bug Fixes + +- Correctly override question type flag for requests + ([#1558](https://github.com/python-zeroconf/python-zeroconf/pull/1558), + [`bd643a2`](https://github.com/python-zeroconf/python-zeroconf/commit/bd643a227bc4d6a949d558850ad1431bc2940d74)) + +* fix: correctly override question type flag for requests + +Currently even when setting the explicit question type flag, the implementation ignores it for + subsequent queries. This commit ensures that all queries respect the explicit question type flag. + +* chore(tests): add test for explicit question type flag + +Add unit test to validate that the explicit question type flag is set correctly in outgoing + requests. + + +## v0.146.2 (2025-04-01) + +### Bug Fixes + +- Create listener socket with specific IP version + ([#1557](https://github.com/python-zeroconf/python-zeroconf/pull/1557), + [`b757ddf`](https://github.com/python-zeroconf/python-zeroconf/commit/b757ddf98d7d04c366281a4281a449c5c2cb897d)) + +* fix: create listener socket with specific IP version + +Create listener sockets when using unicast with specific IP version as well, just like in + `new_respond_socket()`. + +* chore(tests): add unit test for socket creation with unicast addressing + + +## v0.146.1 (2025-03-05) + +### Bug Fixes + +- Use trusted publishing for uploading wheels + ([#1541](https://github.com/python-zeroconf/python-zeroconf/pull/1541), + [`fa65cc8`](https://github.com/python-zeroconf/python-zeroconf/commit/fa65cc8791a6f4c53bc29088cb60b83f420b1ae6)) + + +## v0.146.0 (2025-03-05) + +### Features + +- Reduce size of wheels ([#1540](https://github.com/python-zeroconf/python-zeroconf/pull/1540), + [`dea233c`](https://github.com/python-zeroconf/python-zeroconf/commit/dea233c1e0e80584263090727ce07648755964af)) + +feat: reduce size of binaries + + +## v0.145.1 (2025-02-18) + +### Bug Fixes + +- Hold a strong reference to the AsyncEngine setup task + ([#1533](https://github.com/python-zeroconf/python-zeroconf/pull/1533), + [`d4e6f25`](https://github.com/python-zeroconf/python-zeroconf/commit/d4e6f25754c15417b8bd9839dc8636b2cff717c8)) + + +## v0.145.0 (2025-02-15) + +### Features + +- **docs**: Enable link to source code + ([#1529](https://github.com/python-zeroconf/python-zeroconf/pull/1529), + [`1c7f354`](https://github.com/python-zeroconf/python-zeroconf/commit/1c7f3548b6cbddf73dbb9d69cd8987c8ad32c705)) + + +## v0.144.3 (2025-02-14) + +### Bug Fixes + +- Non unique name during wheel upload + ([#1527](https://github.com/python-zeroconf/python-zeroconf/pull/1527), + [`43136fa`](https://github.com/python-zeroconf/python-zeroconf/commit/43136fa418d4d7826415e1d0f7761b198347ced7)) + + +## v0.144.2 (2025-02-14) + +### Bug Fixes + +- Add a helpful hint for when EADDRINUSE happens during startup + ([#1526](https://github.com/python-zeroconf/python-zeroconf/pull/1526), + [`48dbb71`](https://github.com/python-zeroconf/python-zeroconf/commit/48dbb7190a4f5126e39dbcdb87e34380d4562cd0)) + + +## v0.144.1 (2025-02-12) + +### Bug Fixes + +- Wheel builds failing after adding armv7l builds + ([#1518](https://github.com/python-zeroconf/python-zeroconf/pull/1518), + [`e7adac9`](https://github.com/python-zeroconf/python-zeroconf/commit/e7adac9c59fc4d0c4822c6097a4daee3d68eb4de)) + + +## v0.144.0 (2025-02-12) + +### Features + +- Add armv7l wheel builds ([#1517](https://github.com/python-zeroconf/python-zeroconf/pull/1517), + [`39887b8`](https://github.com/python-zeroconf/python-zeroconf/commit/39887b80328d616e8e6f6ca9d08aecc06f7b0711)) + + +## v0.143.1 (2025-02-12) + +### Bug Fixes + +- Make no buffer space available when adding multicast memberships forgiving + ([#1516](https://github.com/python-zeroconf/python-zeroconf/pull/1516), + [`f377d5c`](https://github.com/python-zeroconf/python-zeroconf/commit/f377d5cd08d724282c8487785163b466f3971344)) + + +## v0.143.0 (2025-01-31) + +### Features + +- Eliminate async_timeout dep on python less than 3.11 + ([#1500](https://github.com/python-zeroconf/python-zeroconf/pull/1500), + [`44457be`](https://github.com/python-zeroconf/python-zeroconf/commit/44457be4571add2f851192db3b37a96d9d27b00e)) + + +## v0.142.0 (2025-01-30) + +### Features + +- Add simple address resolvers and examples + ([#1499](https://github.com/python-zeroconf/python-zeroconf/pull/1499), + [`ae3c352`](https://github.com/python-zeroconf/python-zeroconf/commit/ae3c3523e5f2896989d0b932d53ef1e24ef4aee8)) + + +## v0.141.0 (2025-01-22) + +### Features + +- Speed up adding and expiring records in the DNSCache + ([#1490](https://github.com/python-zeroconf/python-zeroconf/pull/1490), + [`628b136`](https://github.com/python-zeroconf/python-zeroconf/commit/628b13670d04327dd8d4908842f31b476598c7e8)) + + +## v0.140.1 (2025-01-17) + +### Bug Fixes + +- Wheel builds for aarch64 ([#1485](https://github.com/python-zeroconf/python-zeroconf/pull/1485), + [`9d228e2`](https://github.com/python-zeroconf/python-zeroconf/commit/9d228e28eead1561deda696e8837d59896cbc98d)) + + +## v0.140.0 (2025-01-17) + +### Bug Fixes + +- **docs**: Remove repetition of words + ([#1479](https://github.com/python-zeroconf/python-zeroconf/pull/1479), + [`dde26c6`](https://github.com/python-zeroconf/python-zeroconf/commit/dde26c655a49811c11071b0531e408a188687009)) + +Co-authored-by: J. Nick Koston + +### Features + +- Migrate to native types ([#1472](https://github.com/python-zeroconf/python-zeroconf/pull/1472), + [`22a0fb4`](https://github.com/python-zeroconf/python-zeroconf/commit/22a0fb487db27bc2c6448a9167742f3040e910ba)) + +Co-authored-by: J. Nick Koston + +Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> + +- Small performance improvement to writing outgoing packets + ([#1482](https://github.com/python-zeroconf/python-zeroconf/pull/1482), + [`d9be715`](https://github.com/python-zeroconf/python-zeroconf/commit/d9be7155a0ef1ac521e5bbedd3884ddeb9f0b99d)) + + +## v0.139.0 (2025-01-09) + +### Features + +- Implement heapq for tracking cache expire times + ([#1465](https://github.com/python-zeroconf/python-zeroconf/pull/1465), + [`09db184`](https://github.com/python-zeroconf/python-zeroconf/commit/09db1848957b34415f364b7338e4adce99b57abc)) + + +## v0.138.1 (2025-01-08) + +### Bug Fixes + +- Ensure cache does not return stale created and ttl values + ([#1469](https://github.com/python-zeroconf/python-zeroconf/pull/1469), + [`e05055c`](https://github.com/python-zeroconf/python-zeroconf/commit/e05055c584ca46080990437b2b385a187bc48458)) + + +## v0.138.0 (2025-01-08) + +### Features + +- Improve performance of processing incoming records + ([#1467](https://github.com/python-zeroconf/python-zeroconf/pull/1467), + [`ebbb2af`](https://github.com/python-zeroconf/python-zeroconf/commit/ebbb2afccabd3841a3cb0a39824b49773cc6258a)) + +Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> + + +## v0.137.2 (2025-01-06) + +### Bug Fixes + +- Split wheel builds to avoid timeout + ([#1461](https://github.com/python-zeroconf/python-zeroconf/pull/1461), + [`be05f0d`](https://github.com/python-zeroconf/python-zeroconf/commit/be05f0dc4f6b2431606031a7bb24585728d15f01)) + + +## v0.137.1 (2025-01-06) + +### Bug Fixes + +- Move wheel builds to macos-13 + ([#1459](https://github.com/python-zeroconf/python-zeroconf/pull/1459), + [`4ff48a0`](https://github.com/python-zeroconf/python-zeroconf/commit/4ff48a01bc76c82e5710aafaf6cf6e79c069cd85)) + + +## v0.137.0 (2025-01-06) + +### Features + +- Speed up parsing incoming records + ([#1458](https://github.com/python-zeroconf/python-zeroconf/pull/1458), + [`783c1b3`](https://github.com/python-zeroconf/python-zeroconf/commit/783c1b37d1372c90dfce658c66d03aa753afbf49)) + + +## v0.136.2 (2024-11-21) + +### Bug Fixes + +- Retrigger release from failed github workflow + ([#1443](https://github.com/python-zeroconf/python-zeroconf/pull/1443), + [`2ea705d`](https://github.com/python-zeroconf/python-zeroconf/commit/2ea705d850c1cb096c87372d5ec855f684603d01)) + + +## v0.136.1 (2024-11-21) + +### Bug Fixes + +- **ci**: Run release workflow only on main repository + ([#1441](https://github.com/python-zeroconf/python-zeroconf/pull/1441), + [`f637c75`](https://github.com/python-zeroconf/python-zeroconf/commit/f637c75f638ba20c193e58ff63c073a4003430b9)) + +- **docs**: Update python to 3.8 + ([#1430](https://github.com/python-zeroconf/python-zeroconf/pull/1430), + [`483d067`](https://github.com/python-zeroconf/python-zeroconf/commit/483d0673d4ae3eec37840452723fc1839a6cc95c)) + + +## v0.136.0 (2024-10-26) + +### Bug Fixes + +- Add ignore for .c file for wheels + ([#1424](https://github.com/python-zeroconf/python-zeroconf/pull/1424), + [`6535963`](https://github.com/python-zeroconf/python-zeroconf/commit/6535963b5b789ce445e77bb728a5b7ee4263e582)) + +- Correct typos ([#1422](https://github.com/python-zeroconf/python-zeroconf/pull/1422), + [`3991b42`](https://github.com/python-zeroconf/python-zeroconf/commit/3991b4256b8de5b37db7a6144e5112f711b2efef)) + +- Update python-semantic-release to fix release process + ([#1426](https://github.com/python-zeroconf/python-zeroconf/pull/1426), + [`2f20155`](https://github.com/python-zeroconf/python-zeroconf/commit/2f201558d0ab089cdfebb18d2d7bb5785b2cce16)) + +### Features + +- Use SPDX license identifier + ([#1425](https://github.com/python-zeroconf/python-zeroconf/pull/1425), + [`1596145`](https://github.com/python-zeroconf/python-zeroconf/commit/1596145452721e0de4e2a724b055e8e290792d3e)) + + +## v0.135.0 (2024-09-24) + +### Features + +- Improve performance of DNSCache backend + ([#1415](https://github.com/python-zeroconf/python-zeroconf/pull/1415), + [`1df2e69`](https://github.com/python-zeroconf/python-zeroconf/commit/1df2e691ff11c9592e1cdad5599fb6601eb1aa3f)) + + +## v0.134.0 (2024-09-08) + +### Bug Fixes + +- Improve helpfulness of ServiceInfo.request assertions + ([#1408](https://github.com/python-zeroconf/python-zeroconf/pull/1408), + [`9262626`](https://github.com/python-zeroconf/python-zeroconf/commit/9262626895d354ed7376aa567043b793c37a985e)) + +### Features + +- Improve performance when IP addresses change frequently + ([#1407](https://github.com/python-zeroconf/python-zeroconf/pull/1407), + [`111c91a`](https://github.com/python-zeroconf/python-zeroconf/commit/111c91ab395a7520e477eb0e75d5924fba3c64c7)) + + +## v0.133.0 (2024-08-27) + +### Features + +- Add classifier for python 3.13 + ([#1393](https://github.com/python-zeroconf/python-zeroconf/pull/1393), + [`7fb2bb2`](https://github.com/python-zeroconf/python-zeroconf/commit/7fb2bb21421c70db0eb288fa7e73d955f58b0f5d)) + +- Enable building of arm64 macOS builds + ([#1384](https://github.com/python-zeroconf/python-zeroconf/pull/1384), + [`0df2ce0`](https://github.com/python-zeroconf/python-zeroconf/commit/0df2ce0e6f7313831da6a63d477019982d5df55c)) + +Co-authored-by: Alex Ciobanu + +Co-authored-by: J. Nick Koston + +- Improve performance of ip address caching + ([#1392](https://github.com/python-zeroconf/python-zeroconf/pull/1392), + [`f7c7708`](https://github.com/python-zeroconf/python-zeroconf/commit/f7c77081b2f8c70b1ed6a9b9751a86cf91f9aae2)) + +- Python 3.13 support ([#1390](https://github.com/python-zeroconf/python-zeroconf/pull/1390), + [`98cfa83`](https://github.com/python-zeroconf/python-zeroconf/commit/98cfa83710e43880698353821bae61108b08cb2f)) + + +## v0.132.2 (2024-04-13) + +### Bug Fixes + +- Bump cibuildwheel to fix wheel builds + ([#1371](https://github.com/python-zeroconf/python-zeroconf/pull/1371), + [`83e4ce3`](https://github.com/python-zeroconf/python-zeroconf/commit/83e4ce3e31ddd4ae9aec2f8c9d84d7a93f8be210)) + +- Update references to minimum-supported python version of 3.8 + ([#1369](https://github.com/python-zeroconf/python-zeroconf/pull/1369), + [`599524a`](https://github.com/python-zeroconf/python-zeroconf/commit/599524a5ce1e4c1731519dd89377c2a852e59935)) + + +## v0.132.1 (2024-04-12) + +### Bug Fixes + +- Set change during iteration when dispatching listeners + ([#1370](https://github.com/python-zeroconf/python-zeroconf/pull/1370), + [`e9f8aa5`](https://github.com/python-zeroconf/python-zeroconf/commit/e9f8aa5741ae2d490c33a562b459f0af1014dbb0)) + + +## v0.132.0 (2024-04-01) + +### Bug Fixes + +- Avoid including scope_id in IPv6Address object if its zero + ([#1367](https://github.com/python-zeroconf/python-zeroconf/pull/1367), + [`edc4a55`](https://github.com/python-zeroconf/python-zeroconf/commit/edc4a556819956c238a11332052000dcbcb07e3d)) + +### Features + +- Drop python 3.7 support ([#1359](https://github.com/python-zeroconf/python-zeroconf/pull/1359), + [`4877829`](https://github.com/python-zeroconf/python-zeroconf/commit/4877829e6442de5426db152d11827b1ba85dbf59)) + +- Make async_get_service_info available on the Zeroconf object + ([#1366](https://github.com/python-zeroconf/python-zeroconf/pull/1366), + [`c4c2dee`](https://github.com/python-zeroconf/python-zeroconf/commit/c4c2deeb05279ddbb0eba1330c7ae58795fea001)) + + +## v0.131.0 (2023-12-19) + +### Features + +- Small speed up to constructing outgoing packets + ([#1354](https://github.com/python-zeroconf/python-zeroconf/pull/1354), + [`517d7d0`](https://github.com/python-zeroconf/python-zeroconf/commit/517d7d00ca7738c770077738125aec0e4824c000)) + +- Speed up processing incoming packets + ([#1352](https://github.com/python-zeroconf/python-zeroconf/pull/1352), + [`6c15325`](https://github.com/python-zeroconf/python-zeroconf/commit/6c153258a995cf9459a6f23267b7e379b5e2550f)) + +- Speed up the query handler ([#1350](https://github.com/python-zeroconf/python-zeroconf/pull/1350), + [`9eac0a1`](https://github.com/python-zeroconf/python-zeroconf/commit/9eac0a122f28a7a4fa76cbfdda21d9a3571d7abb)) + + +## v0.130.0 (2023-12-16) + +### Bug Fixes + +- Ensure IPv6 scoped address construction uses the string cache + ([#1336](https://github.com/python-zeroconf/python-zeroconf/pull/1336), + [`f78a196`](https://github.com/python-zeroconf/python-zeroconf/commit/f78a196db632c4fe017a34f1af8a58903c15a575)) + +- Ensure question history suppresses duplicates + ([#1338](https://github.com/python-zeroconf/python-zeroconf/pull/1338), + [`6f23656`](https://github.com/python-zeroconf/python-zeroconf/commit/6f23656576daa04e3de44e100f3ddd60ee4c560d)) + +- Microsecond precision loss in the query handler + ([#1339](https://github.com/python-zeroconf/python-zeroconf/pull/1339), + [`6560fad`](https://github.com/python-zeroconf/python-zeroconf/commit/6560fad584e0d392962c9a9248759f17c416620e)) + +- Scheduling race with the QueryScheduler + ([#1347](https://github.com/python-zeroconf/python-zeroconf/pull/1347), + [`cf40470`](https://github.com/python-zeroconf/python-zeroconf/commit/cf40470b89f918d3c24d7889d3536f3ffa44846c)) + +### Features + +- Make ServiceInfo aware of question history + ([#1348](https://github.com/python-zeroconf/python-zeroconf/pull/1348), + [`b9aae1d`](https://github.com/python-zeroconf/python-zeroconf/commit/b9aae1de07bf1491e873bc314f8a1d7996127ad3)) + +- Significantly improve efficiency of the ServiceBrowser scheduler + ([#1335](https://github.com/python-zeroconf/python-zeroconf/pull/1335), + [`c65d869`](https://github.com/python-zeroconf/python-zeroconf/commit/c65d869aec731b803484871e9d242a984f9f5848)) + +- Small performance improvement constructing outgoing questions + ([#1340](https://github.com/python-zeroconf/python-zeroconf/pull/1340), + [`157185f`](https://github.com/python-zeroconf/python-zeroconf/commit/157185f28bf1e83e6811e2a5cd1fa9b38966f780)) + +- Small performance improvement for converting time + ([#1342](https://github.com/python-zeroconf/python-zeroconf/pull/1342), + [`73d3ab9`](https://github.com/python-zeroconf/python-zeroconf/commit/73d3ab90dd3b59caab771235dd6dbedf05bfe0b3)) + +- Small performance improvement for ServiceInfo asking questions + ([#1341](https://github.com/python-zeroconf/python-zeroconf/pull/1341), + [`810a309`](https://github.com/python-zeroconf/python-zeroconf/commit/810a3093c5a9411ee97740b468bd706bdf4a95de)) + +- Small speed up to processing incoming records + ([#1345](https://github.com/python-zeroconf/python-zeroconf/pull/1345), + [`7de655b`](https://github.com/python-zeroconf/python-zeroconf/commit/7de655b6f05012f20a3671e0bcdd44a1913d7b52)) + +- Small speed up to ServiceInfo construction + ([#1346](https://github.com/python-zeroconf/python-zeroconf/pull/1346), + [`b329d99`](https://github.com/python-zeroconf/python-zeroconf/commit/b329d99917bb731b4c70bf20c7c010eeb85ad9fd)) + + +## v0.129.0 (2023-12-13) + +### Features + +- Add decoded_properties method to ServiceInfo + ([#1332](https://github.com/python-zeroconf/python-zeroconf/pull/1332), + [`9b595a1`](https://github.com/python-zeroconf/python-zeroconf/commit/9b595a1dcacf109c699953219d70fe36296c7318)) + +- Cache is_unspecified for zeroconf ip address objects + ([#1331](https://github.com/python-zeroconf/python-zeroconf/pull/1331), + [`a1c84dc`](https://github.com/python-zeroconf/python-zeroconf/commit/a1c84dc6adeebd155faec1a647c0f70d70de2945)) + +- Ensure ServiceInfo.properties always returns bytes + ([#1333](https://github.com/python-zeroconf/python-zeroconf/pull/1333), + [`d29553a`](https://github.com/python-zeroconf/python-zeroconf/commit/d29553ab7de6b7af70769ddb804fe2aaf492f320)) + + +## v0.128.5 (2023-12-13) + +### Bug Fixes + +- Performance regression with ServiceInfo IPv6Addresses + ([#1330](https://github.com/python-zeroconf/python-zeroconf/pull/1330), + [`e2f9f81`](https://github.com/python-zeroconf/python-zeroconf/commit/e2f9f81dbc54c3dd527eeb3298897d63f99d33f4)) + + +## v0.128.4 (2023-12-10) + +### Bug Fixes + +- Re-expose ServiceInfo._set_properties for backwards compat + ([#1327](https://github.com/python-zeroconf/python-zeroconf/pull/1327), + [`39c4005`](https://github.com/python-zeroconf/python-zeroconf/commit/39c40051d7a63bdc63a3e2dfa20bd944fee4e761)) + + +## v0.128.3 (2023-12-10) + +### Bug Fixes + +- Correct nsec record writing + ([#1326](https://github.com/python-zeroconf/python-zeroconf/pull/1326), + [`cd7a16a`](https://github.com/python-zeroconf/python-zeroconf/commit/cd7a16a32c37b2f7a2e90d3c749525a5393bad57)) + + +## v0.128.2 (2023-12-10) + +### Bug Fixes + +- Match cython version for dev deps to build deps + ([#1325](https://github.com/python-zeroconf/python-zeroconf/pull/1325), + [`a0dac46`](https://github.com/python-zeroconf/python-zeroconf/commit/a0dac46c01202b3d5a0823ac1928fc1d75332522)) + +- Timestamps missing double precision + ([#1324](https://github.com/python-zeroconf/python-zeroconf/pull/1324), + [`ecea4e4`](https://github.com/python-zeroconf/python-zeroconf/commit/ecea4e4217892ca8cf763074ac3e5d1b898acd21)) + + +## v0.128.1 (2023-12-10) + +### Bug Fixes + +- Correct handling of IPv6 addresses with scope_id in ServiceInfo + ([#1322](https://github.com/python-zeroconf/python-zeroconf/pull/1322), + [`1682991`](https://github.com/python-zeroconf/python-zeroconf/commit/1682991b985b1f7b2bf0cff1a7eb7793070e7cb1)) + + +## v0.128.0 (2023-12-02) + +### Features + +- Speed up unpacking TXT record data in ServiceInfo + ([#1318](https://github.com/python-zeroconf/python-zeroconf/pull/1318), + [`a200842`](https://github.com/python-zeroconf/python-zeroconf/commit/a20084281e66bdb9c37183a5eb992435f5b866ac)) + + +## v0.127.0 (2023-11-15) + +### Features + +- Small speed up to processing incoming dns records + ([#1315](https://github.com/python-zeroconf/python-zeroconf/pull/1315), + [`bfe4c24`](https://github.com/python-zeroconf/python-zeroconf/commit/bfe4c24881a7259713425df5ab00ffe487518841)) + +- Small speed up to writing outgoing packets + ([#1316](https://github.com/python-zeroconf/python-zeroconf/pull/1316), + [`cd28476`](https://github.com/python-zeroconf/python-zeroconf/commit/cd28476f6b0a6c2c733273fb24ddaac6c7bbdf65)) + +- Speed up incoming packet reader + ([#1314](https://github.com/python-zeroconf/python-zeroconf/pull/1314), + [`0d60b61`](https://github.com/python-zeroconf/python-zeroconf/commit/0d60b61538a5d4b6f44b2369333b6e916a0a55b4)) + + +## v0.126.0 (2023-11-13) + +### Features + +- Speed up outgoing packet writer + ([#1313](https://github.com/python-zeroconf/python-zeroconf/pull/1313), + [`55cf4cc`](https://github.com/python-zeroconf/python-zeroconf/commit/55cf4ccdff886a136db4e2133d3e6cdd001a8bd6)) + +- Speed up writing name compression for outgoing packets + ([#1312](https://github.com/python-zeroconf/python-zeroconf/pull/1312), + [`9caeabb`](https://github.com/python-zeroconf/python-zeroconf/commit/9caeabb6d4659a25ea1251c1ee7bb824e05f3d8b)) + + +## v0.125.0 (2023-11-12) + +### Features + +- Speed up service browser queries when browsing many types + ([#1311](https://github.com/python-zeroconf/python-zeroconf/pull/1311), + [`d192d33`](https://github.com/python-zeroconf/python-zeroconf/commit/d192d33b1f05aa95a89965e86210aec086673a17)) + + +## v0.124.0 (2023-11-12) + +### Features + +- Avoid decoding known answers if we have no answers to give + ([#1308](https://github.com/python-zeroconf/python-zeroconf/pull/1308), + [`605dc9c`](https://github.com/python-zeroconf/python-zeroconf/commit/605dc9ccd843a535802031f051b3d93310186ad1)) + +- Small speed up to process incoming packets + ([#1309](https://github.com/python-zeroconf/python-zeroconf/pull/1309), + [`56ef908`](https://github.com/python-zeroconf/python-zeroconf/commit/56ef90865189c01d2207abcc5e2efe3a7a022fa1)) + + +## v0.123.0 (2023-11-12) + +### Features + +- Speed up instances only used to lookup answers + ([#1307](https://github.com/python-zeroconf/python-zeroconf/pull/1307), + [`0701b8a`](https://github.com/python-zeroconf/python-zeroconf/commit/0701b8ab6009891cbaddaa1d17116d31fd1b2f78)) + + +## v0.122.3 (2023-11-09) + +### Bug Fixes + +- Do not build musllinux aarch64 wheels to reduce release time + ([#1306](https://github.com/python-zeroconf/python-zeroconf/pull/1306), + [`79aafb0`](https://github.com/python-zeroconf/python-zeroconf/commit/79aafb0acf7ca6b17976be7ede748008deada27b)) + + +## v0.122.2 (2023-11-09) + +### Bug Fixes + +- Do not build aarch64 wheels for PyPy + ([#1305](https://github.com/python-zeroconf/python-zeroconf/pull/1305), + [`7e884db`](https://github.com/python-zeroconf/python-zeroconf/commit/7e884db4d958459e64257aba860dba2450db0687)) + + +## v0.122.1 (2023-11-09) + +### Bug Fixes + +- Skip wheel builds for eol python and older python with aarch64 + ([#1304](https://github.com/python-zeroconf/python-zeroconf/pull/1304), + [`6c8f5a5`](https://github.com/python-zeroconf/python-zeroconf/commit/6c8f5a5dec2072aa6a8f889c5d8a4623ab392234)) + + +## v0.122.0 (2023-11-08) + +### Features + +- Build aarch64 wheels ([#1302](https://github.com/python-zeroconf/python-zeroconf/pull/1302), + [`4fe58e2`](https://github.com/python-zeroconf/python-zeroconf/commit/4fe58e2edc6da64a8ece0e2b16ec9ebfc5b3cd83)) + + +## v0.121.0 (2023-11-08) + +### Features + +- Speed up record updates ([#1301](https://github.com/python-zeroconf/python-zeroconf/pull/1301), + [`d2af6a0`](https://github.com/python-zeroconf/python-zeroconf/commit/d2af6a0978f5abe4f8bb70d3e29d9836d0fd77c4)) + + +## v0.120.0 (2023-11-05) + +### Features + +- Speed up decoding labels from incoming data + ([#1291](https://github.com/python-zeroconf/python-zeroconf/pull/1291), + [`c37ead4`](https://github.com/python-zeroconf/python-zeroconf/commit/c37ead4d7000607e81706a97b4cdffd80cf8cf99)) + +- Speed up incoming packet processing with a memory view + ([#1290](https://github.com/python-zeroconf/python-zeroconf/pull/1290), + [`f1f0a25`](https://github.com/python-zeroconf/python-zeroconf/commit/f1f0a2504afd4d29bc6b7cf715cd3cb81b9049f7)) + +- Speed up ServiceBrowsers with a pxd for the signal interface + ([#1289](https://github.com/python-zeroconf/python-zeroconf/pull/1289), + [`8a17f20`](https://github.com/python-zeroconf/python-zeroconf/commit/8a17f2053a89db4beca9e8c1de4640faf27726b4)) + + +## v0.119.0 (2023-10-18) + +### Features + +- Update cibuildwheel to build wheels on latest cython final release + ([#1285](https://github.com/python-zeroconf/python-zeroconf/pull/1285), + [`e8c9083`](https://github.com/python-zeroconf/python-zeroconf/commit/e8c9083bb118764a85b12fac9055152a2f62a212)) + + +## v0.118.1 (2023-10-18) + +### Bug Fixes + +- Reduce size of wheels by excluding generated .c files + ([#1284](https://github.com/python-zeroconf/python-zeroconf/pull/1284), + [`b6afa4b`](https://github.com/python-zeroconf/python-zeroconf/commit/b6afa4b2775a1fdb090145eccdc5711c98e7147a)) + + +## v0.118.0 (2023-10-14) + +### Features + +- Small improvements to ServiceBrowser performance + ([#1283](https://github.com/python-zeroconf/python-zeroconf/pull/1283), + [`0fc031b`](https://github.com/python-zeroconf/python-zeroconf/commit/0fc031b1e7bf1766d5a1d39d70d300b86e36715e)) + + +## v0.117.0 (2023-10-14) + +### Features + +- Small cleanups to incoming data handlers + ([#1282](https://github.com/python-zeroconf/python-zeroconf/pull/1282), + [`4f4bd9f`](https://github.com/python-zeroconf/python-zeroconf/commit/4f4bd9ff7c1e575046e5ea213d9b8c91ac7a24a9)) + + +## v0.116.0 (2023-10-13) + +### Features + +- Reduce type checking overhead at run time + ([#1281](https://github.com/python-zeroconf/python-zeroconf/pull/1281), + [`8f30099`](https://github.com/python-zeroconf/python-zeroconf/commit/8f300996e5bd4316b2237f0502791dd0d6a855fe)) + + +## v0.115.2 (2023-10-05) + +### Bug Fixes + +- Ensure ServiceInfo cache is cleared when adding to the registry + ([#1279](https://github.com/python-zeroconf/python-zeroconf/pull/1279), + [`2060eb2`](https://github.com/python-zeroconf/python-zeroconf/commit/2060eb2cc43489c34bea08924c3f40b875d5a498)) + +* There were production use cases that mutated the service info and re-registered it that need to be + accounted for + + +## v0.115.1 (2023-10-01) + +### Bug Fixes + +- Add missing python definition for addresses_by_version + ([#1278](https://github.com/python-zeroconf/python-zeroconf/pull/1278), + [`52ee02b`](https://github.com/python-zeroconf/python-zeroconf/commit/52ee02b16860e344c402124f4b2e2869536ec839)) + + +## v0.115.0 (2023-09-26) + +### Features + +- Speed up outgoing multicast queue + ([#1277](https://github.com/python-zeroconf/python-zeroconf/pull/1277), + [`a13fd49`](https://github.com/python-zeroconf/python-zeroconf/commit/a13fd49d77474fd5858de809e48cbab1ccf89173)) + + +## v0.114.0 (2023-09-25) + +### Features + +- Speed up responding to queries + ([#1275](https://github.com/python-zeroconf/python-zeroconf/pull/1275), + [`3c6b18c`](https://github.com/python-zeroconf/python-zeroconf/commit/3c6b18cdf4c94773ad6f4497df98feb337939ee9)) + + +## v0.113.0 (2023-09-24) + +### Features + +- Improve performance of loading records from cache in ServiceInfo + ([#1274](https://github.com/python-zeroconf/python-zeroconf/pull/1274), + [`6257d49`](https://github.com/python-zeroconf/python-zeroconf/commit/6257d49952e02107f800f4ad4894716508edfcda)) + + +## v0.112.0 (2023-09-14) + +### Features + +- Improve AsyncServiceBrowser performance + ([#1273](https://github.com/python-zeroconf/python-zeroconf/pull/1273), + [`0c88ecf`](https://github.com/python-zeroconf/python-zeroconf/commit/0c88ecf5ef6b9b256f991e7a630048de640999a6)) + + +## v0.111.0 (2023-09-14) + +### Features + +- Speed up question and answer internals + ([#1272](https://github.com/python-zeroconf/python-zeroconf/pull/1272), + [`d24722b`](https://github.com/python-zeroconf/python-zeroconf/commit/d24722bfa4201d48ab482d35b0ef004f070ada80)) + + +## v0.110.0 (2023-09-14) + +### Features + +- Small speed ups to ServiceBrowser + ([#1271](https://github.com/python-zeroconf/python-zeroconf/pull/1271), + [`22c433d`](https://github.com/python-zeroconf/python-zeroconf/commit/22c433ddaea3049ac49933325ba938fd87a529c0)) + + +## v0.109.0 (2023-09-14) + +### Features + +- Speed up ServiceBrowsers with a cython pxd + ([#1270](https://github.com/python-zeroconf/python-zeroconf/pull/1270), + [`4837876`](https://github.com/python-zeroconf/python-zeroconf/commit/48378769c3887b5746ca00de30067a4c0851765c)) + + +## v0.108.0 (2023-09-11) + +### Features + +- Improve performance of constructing outgoing queries + ([#1267](https://github.com/python-zeroconf/python-zeroconf/pull/1267), + [`00c439a`](https://github.com/python-zeroconf/python-zeroconf/commit/00c439a6400b7850ef9fdd75bc8d82d4e64b1da0)) + + +## v0.107.0 (2023-09-11) + +### Features + +- Speed up responding to queries + ([#1266](https://github.com/python-zeroconf/python-zeroconf/pull/1266), + [`24a0a00`](https://github.com/python-zeroconf/python-zeroconf/commit/24a0a00b3e457979e279a2eeadc8fad2ab09e125)) + + +## v0.106.0 (2023-09-11) + +### Features + +- Speed up answering questions + ([#1265](https://github.com/python-zeroconf/python-zeroconf/pull/1265), + [`37bfaf2`](https://github.com/python-zeroconf/python-zeroconf/commit/37bfaf2f630358e8c68652f3b3120931a6f94910)) + + +## v0.105.0 (2023-09-10) + +### Features + +- Speed up ServiceInfo with a cython pxd + ([#1264](https://github.com/python-zeroconf/python-zeroconf/pull/1264), + [`7ca690a`](https://github.com/python-zeroconf/python-zeroconf/commit/7ca690ac3fa75e7474d3412944bbd5056cb313dd)) + + +## v0.104.0 (2023-09-10) + +### Features + +- Speed up generating answers + ([#1262](https://github.com/python-zeroconf/python-zeroconf/pull/1262), + [`50a8f06`](https://github.com/python-zeroconf/python-zeroconf/commit/50a8f066b6ab90bc9e3300f81cf9332550b720df)) + + +## v0.103.0 (2023-09-09) + +### Features + +- Avoid calling get_running_loop when resolving ServiceInfo + ([#1261](https://github.com/python-zeroconf/python-zeroconf/pull/1261), + [`33a2714`](https://github.com/python-zeroconf/python-zeroconf/commit/33a2714cadff96edf016b869cc63b0661d16ef2c)) + + +## v0.102.0 (2023-09-07) + +### Features + +- Significantly speed up writing outgoing dns records + ([#1260](https://github.com/python-zeroconf/python-zeroconf/pull/1260), + [`bf2f366`](https://github.com/python-zeroconf/python-zeroconf/commit/bf2f3660a1f341e50ab0ae586dfbacbc5ddcc077)) + + +## v0.101.0 (2023-09-07) + +### Features + +- Speed up writing outgoing dns records + ([#1259](https://github.com/python-zeroconf/python-zeroconf/pull/1259), + [`248655f`](https://github.com/python-zeroconf/python-zeroconf/commit/248655f0276223b089373c70ec13a0385dfaa4d6)) + + +## v0.100.0 (2023-09-07) + +### Features + +- Small speed up to writing outgoing dns records + ([#1258](https://github.com/python-zeroconf/python-zeroconf/pull/1258), + [`1ed6bd2`](https://github.com/python-zeroconf/python-zeroconf/commit/1ed6bd2ec4db0612b71384f923ffff1efd3ce878)) + + +## v0.99.0 (2023-09-06) + +### Features + +- Reduce IP Address parsing overhead in ServiceInfo + ([#1257](https://github.com/python-zeroconf/python-zeroconf/pull/1257), + [`83d0b7f`](https://github.com/python-zeroconf/python-zeroconf/commit/83d0b7fda2eb09c9c6e18b85f329d1ddc701e3fb)) + + +## v0.98.0 (2023-09-06) + +### Features + +- Speed up decoding incoming packets + ([#1256](https://github.com/python-zeroconf/python-zeroconf/pull/1256), + [`ac081cf`](https://github.com/python-zeroconf/python-zeroconf/commit/ac081cf00addde1ceea2c076f73905fdb293de3a)) + + +## v0.97.0 (2023-09-03) + +### Features + +- Speed up answering queries ([#1255](https://github.com/python-zeroconf/python-zeroconf/pull/1255), + [`2d3aed3`](https://github.com/python-zeroconf/python-zeroconf/commit/2d3aed36e24c73013fcf4acc90803fc1737d0917)) + + +## v0.96.0 (2023-09-03) + +### Features + +- Optimize DNSCache.get_by_details + ([#1254](https://github.com/python-zeroconf/python-zeroconf/pull/1254), + [`ce59787`](https://github.com/python-zeroconf/python-zeroconf/commit/ce59787a170781ffdaa22425018d288b395ac081)) + +* feat: optimize DNSCache.get_by_details + +This is one of the most called functions since ServiceInfo.load_from_cache calls it + +* fix: make get_all_by_details thread-safe + +* fix: remove unneeded key checks + + +## v0.95.0 (2023-09-03) + +### Features + +- Speed up adding and removing RecordUpdateListeners + ([#1253](https://github.com/python-zeroconf/python-zeroconf/pull/1253), + [`22e4a29`](https://github.com/python-zeroconf/python-zeroconf/commit/22e4a296d440b3038c0ff5ed6fc8878304ec4937)) + + +## v0.94.0 (2023-09-03) + +### Features + +- Optimize cache implementation + ([#1252](https://github.com/python-zeroconf/python-zeroconf/pull/1252), + [`8d3ec79`](https://github.com/python-zeroconf/python-zeroconf/commit/8d3ec792277aaf7ef790318b5b35ab00839ca3b3)) + + +## v0.93.1 (2023-09-03) + +### Bug Fixes + +- No change re-release due to unrecoverable failed CI run + ([#1251](https://github.com/python-zeroconf/python-zeroconf/pull/1251), + [`730921b`](https://github.com/python-zeroconf/python-zeroconf/commit/730921b155dfb9c62251c8c643b1302e807aff3b)) + + +## v0.93.0 (2023-09-02) + +### Features + +- Reduce overhead to answer questions + ([#1250](https://github.com/python-zeroconf/python-zeroconf/pull/1250), + [`7cb8da0`](https://github.com/python-zeroconf/python-zeroconf/commit/7cb8da0c6c5c944588009fe36012c1197c422668)) + + +## v0.92.0 (2023-09-02) + +### Features + +- Cache construction of records used to answer queries from the service registry + ([#1243](https://github.com/python-zeroconf/python-zeroconf/pull/1243), + [`0890f62`](https://github.com/python-zeroconf/python-zeroconf/commit/0890f628dbbd577fb77d3e6f2e267052b2b2b515)) + + +## v0.91.1 (2023-09-02) + +### Bug Fixes + +- Remove useless calls in ServiceInfo + ([#1248](https://github.com/python-zeroconf/python-zeroconf/pull/1248), + [`4e40fae`](https://github.com/python-zeroconf/python-zeroconf/commit/4e40fae20bf50b4608e28fad4a360c4ed48ac86b)) + + +## v0.91.0 (2023-09-02) + +### Features + +- Reduce overhead to process incoming updates by avoiding the handle_response shim + ([#1247](https://github.com/python-zeroconf/python-zeroconf/pull/1247), + [`5e31f0a`](https://github.com/python-zeroconf/python-zeroconf/commit/5e31f0afe4c341fbdbbbe50348a829ea553cbda0)) + + +## v0.90.0 (2023-09-02) + +### Features + +- Avoid python float conversion in listener hot path + ([#1245](https://github.com/python-zeroconf/python-zeroconf/pull/1245), + [`816ad4d`](https://github.com/python-zeroconf/python-zeroconf/commit/816ad4dceb3859bad4bb136bdb1d1ee2daa0bf5a)) + +### Refactoring + +- Reduce duplicate code in engine.py + ([#1246](https://github.com/python-zeroconf/python-zeroconf/pull/1246), + [`36ae505`](https://github.com/python-zeroconf/python-zeroconf/commit/36ae505dc9f95b59fdfb632960845a45ba8575b8)) + + +## v0.89.0 (2023-09-02) + +### Features + +- Reduce overhead to process incoming questions + ([#1244](https://github.com/python-zeroconf/python-zeroconf/pull/1244), + [`18b65d1`](https://github.com/python-zeroconf/python-zeroconf/commit/18b65d1c75622869b0c29258215d3db3ae520d6c)) + + +## v0.88.0 (2023-08-29) + +### Features + +- Speed up RecordManager with additional cython defs + ([#1242](https://github.com/python-zeroconf/python-zeroconf/pull/1242), + [`5a76fc5`](https://github.com/python-zeroconf/python-zeroconf/commit/5a76fc5ff74f2941ffbf7570e45390f35e0b7e01)) + + +## v0.87.0 (2023-08-29) + +### Features + +- Improve performance by adding cython pxd for RecordManager + ([#1241](https://github.com/python-zeroconf/python-zeroconf/pull/1241), + [`a7dad3d`](https://github.com/python-zeroconf/python-zeroconf/commit/a7dad3d9743586f352e21eea1e129c6875f9a713)) + + +## v0.86.0 (2023-08-28) + +### Features + +- Build wheels for cpython 3.12 + ([#1239](https://github.com/python-zeroconf/python-zeroconf/pull/1239), + [`58bc154`](https://github.com/python-zeroconf/python-zeroconf/commit/58bc154f55b06b4ddfc4a141592488abe76f062a)) + +- Use server_key when processing DNSService records + ([#1238](https://github.com/python-zeroconf/python-zeroconf/pull/1238), + [`cc8feb1`](https://github.com/python-zeroconf/python-zeroconf/commit/cc8feb110fefc3fb714fd482a52f16e2b620e8c4)) + + +## v0.85.0 (2023-08-27) + +### Features + +- Simplify code to unpack properties + ([#1237](https://github.com/python-zeroconf/python-zeroconf/pull/1237), + [`68d9998`](https://github.com/python-zeroconf/python-zeroconf/commit/68d99985a0e9d2c72ff670b2e2af92271a6fe934)) + + +## v0.84.0 (2023-08-27) + +### Features + +- Context managers in ServiceBrowser and AsyncServiceBrowser + ([#1233](https://github.com/python-zeroconf/python-zeroconf/pull/1233), + [`bd8d846`](https://github.com/python-zeroconf/python-zeroconf/commit/bd8d8467dec2a39a0b525043ea1051259100fded)) + +Co-authored-by: J. Nick Koston + + +## v0.83.1 (2023-08-27) + +### Bug Fixes + +- Rebuild wheels with cython 3.0.2 + ([#1236](https://github.com/python-zeroconf/python-zeroconf/pull/1236), + [`dd637fb`](https://github.com/python-zeroconf/python-zeroconf/commit/dd637fb2e5a87ba283750e69d116e124bef54e7c)) + + +## v0.83.0 (2023-08-26) + +### Features + +- Speed up question and answer history with a cython pxd + ([#1234](https://github.com/python-zeroconf/python-zeroconf/pull/1234), + [`703ecb2`](https://github.com/python-zeroconf/python-zeroconf/commit/703ecb2901b2150fb72fac3deed61d7302561298)) + + +## v0.82.1 (2023-08-22) + +### Bug Fixes + +- Build failures with older cython 0.29 series + ([#1232](https://github.com/python-zeroconf/python-zeroconf/pull/1232), + [`30c3ad9`](https://github.com/python-zeroconf/python-zeroconf/commit/30c3ad9d1bc6b589e1ca6675fea21907ebcd1ced)) + + +## v0.82.0 (2023-08-22) + +### Features + +- Optimize processing of records in RecordUpdateListener subclasses + ([#1231](https://github.com/python-zeroconf/python-zeroconf/pull/1231), + [`3e89294`](https://github.com/python-zeroconf/python-zeroconf/commit/3e89294ea0ecee1122e1c1ffdc78925add8ca40e)) + + +## v0.81.0 (2023-08-22) + +### Features + +- Optimizing sending answers to questions + ([#1227](https://github.com/python-zeroconf/python-zeroconf/pull/1227), + [`cd7b56b`](https://github.com/python-zeroconf/python-zeroconf/commit/cd7b56b2aa0c8ee429da430e9a36abd515512011)) + +- Speed up the service registry with a cython pxd + ([#1226](https://github.com/python-zeroconf/python-zeroconf/pull/1226), + [`47d3c7a`](https://github.com/python-zeroconf/python-zeroconf/commit/47d3c7ad4bc5f2247631c3ad5e6b6156d45a0a4e)) + + +## v0.80.0 (2023-08-15) + +### Features + +- Optimize unpacking properties in ServiceInfo + ([#1225](https://github.com/python-zeroconf/python-zeroconf/pull/1225), + [`1492e41`](https://github.com/python-zeroconf/python-zeroconf/commit/1492e41b3d5cba5598cc9dd6bd2bc7d238f13555)) + + +## v0.79.0 (2023-08-14) + +### Features + +- Refactor notify implementation to reduce overhead of adding and removing listeners + ([#1224](https://github.com/python-zeroconf/python-zeroconf/pull/1224), + [`ceb92cf`](https://github.com/python-zeroconf/python-zeroconf/commit/ceb92cfe42d885dbb38cee7aaeebf685d97627a9)) + + +## v0.78.0 (2023-08-14) + +### Features + +- Add cython pxd file for _listener.py to improve incoming message processing performance + ([#1221](https://github.com/python-zeroconf/python-zeroconf/pull/1221), + [`f459856`](https://github.com/python-zeroconf/python-zeroconf/commit/f459856a0a61b8afa8a541926d7e15d51f8e4aea)) + + +## v0.77.0 (2023-08-14) + +### Features + +- Cythonize _listener.py to improve incoming message processing performance + ([#1220](https://github.com/python-zeroconf/python-zeroconf/pull/1220), + [`9efde8c`](https://github.com/python-zeroconf/python-zeroconf/commit/9efde8c8c1ed14c5d3c162f185b49212fcfcb5c9)) + + +## v0.76.0 (2023-08-14) + +### Features + +- Improve performance responding to queries + ([#1217](https://github.com/python-zeroconf/python-zeroconf/pull/1217), + [`69b33be`](https://github.com/python-zeroconf/python-zeroconf/commit/69b33be3b2f9d4a27ef5154cae94afca048efffa)) + + +## v0.75.0 (2023-08-13) + +### Features + +- Expose flag to disable strict name checking in service registration + ([#1215](https://github.com/python-zeroconf/python-zeroconf/pull/1215), + [`5df8a57`](https://github.com/python-zeroconf/python-zeroconf/commit/5df8a57a14d59687a3c22ea8ee063e265031e278)) + +- Speed up processing incoming records + ([#1216](https://github.com/python-zeroconf/python-zeroconf/pull/1216), + [`aff625d`](https://github.com/python-zeroconf/python-zeroconf/commit/aff625dc6a5e816dad519644c4adac4f96980c04)) + + +## v0.74.0 (2023-08-04) + +### Bug Fixes + +- Remove typing on reset_ttl for cython compat + ([#1213](https://github.com/python-zeroconf/python-zeroconf/pull/1213), + [`0094e26`](https://github.com/python-zeroconf/python-zeroconf/commit/0094e2684344c6b7edd7948924f093f1b4c19901)) + +### Features + +- Speed up unpacking text records in ServiceInfo + ([#1212](https://github.com/python-zeroconf/python-zeroconf/pull/1212), + [`99a6f98`](https://github.com/python-zeroconf/python-zeroconf/commit/99a6f98e44a1287ba537eabb852b1b69923402f0)) + + +## v0.73.0 (2023-08-03) + +### Features + +- Add a cache to service_type_name + ([#1211](https://github.com/python-zeroconf/python-zeroconf/pull/1211), + [`53a694f`](https://github.com/python-zeroconf/python-zeroconf/commit/53a694f60e675ae0560e727be6b721b401c2b68f)) + + +## v0.72.3 (2023-08-03) + +### Bug Fixes + +- Revert adding typing to DNSRecord.suppressed_by + ([#1210](https://github.com/python-zeroconf/python-zeroconf/pull/1210), + [`3dba5ae`](https://github.com/python-zeroconf/python-zeroconf/commit/3dba5ae0c0e9473b7b20fd6fc79fa1a3b298dc5a)) + + +## v0.72.2 (2023-08-03) + +### Bug Fixes + +- Revert DNSIncoming cimport in _dns.pxd + ([#1209](https://github.com/python-zeroconf/python-zeroconf/pull/1209), + [`5f14b6d`](https://github.com/python-zeroconf/python-zeroconf/commit/5f14b6dc687b3a0716d0ca7f61ccf1e93dfe5fa1)) + + +## v0.72.1 (2023-08-03) + +### Bug Fixes + +- Race with InvalidStateError when async_request times out + ([#1208](https://github.com/python-zeroconf/python-zeroconf/pull/1208), + [`2233b6b`](https://github.com/python-zeroconf/python-zeroconf/commit/2233b6bc4ceeee5524d2ee88ecae8234173feb5f)) + + +## v0.72.0 (2023-08-02) + +### Features + +- Speed up processing incoming records + ([#1206](https://github.com/python-zeroconf/python-zeroconf/pull/1206), + [`126849c`](https://github.com/python-zeroconf/python-zeroconf/commit/126849c92be8cec9253fba9faa591029d992fcc3)) + + +## v0.71.5 (2023-08-02) + +### Bug Fixes + +- Improve performance of ServiceInfo.async_request + ([#1205](https://github.com/python-zeroconf/python-zeroconf/pull/1205), + [`8019a73`](https://github.com/python-zeroconf/python-zeroconf/commit/8019a73c952f2fc4c88d849aab970fafedb316d8)) + + +## v0.71.4 (2023-07-24) + +### Bug Fixes + +- Cleanup naming from previous refactoring in ServiceInfo + ([#1202](https://github.com/python-zeroconf/python-zeroconf/pull/1202), + [`b272d75`](https://github.com/python-zeroconf/python-zeroconf/commit/b272d75abd982f3be1f4b20f683cac38011cc6f4)) + + +## v0.71.3 (2023-07-23) + +### Bug Fixes + +- Pin python-semantic-release to fix release process + ([#1200](https://github.com/python-zeroconf/python-zeroconf/pull/1200), + [`c145a23`](https://github.com/python-zeroconf/python-zeroconf/commit/c145a238d768aa17c3aebe120c20a46bfbec6b99)) + + +## v0.71.2 (2023-07-23) + +### Bug Fixes + +- No change re-release to fix wheel builds + ([#1199](https://github.com/python-zeroconf/python-zeroconf/pull/1199), + [`8c3a4c8`](https://github.com/python-zeroconf/python-zeroconf/commit/8c3a4c80c221bea7401c12e1c6a525e75b7ffea2)) + + +## v0.71.1 (2023-07-23) + +### Bug Fixes + +- Add missing if TYPE_CHECKING guard to generate_service_query + ([#1198](https://github.com/python-zeroconf/python-zeroconf/pull/1198), + [`ac53adf`](https://github.com/python-zeroconf/python-zeroconf/commit/ac53adf7e71db14c1a0f9adbfd1d74033df36898)) + + +## v0.71.0 (2023-07-08) + +### Features + +- Improve incoming data processing performance + ([#1194](https://github.com/python-zeroconf/python-zeroconf/pull/1194), + [`a56c776`](https://github.com/python-zeroconf/python-zeroconf/commit/a56c776008ef86f99db78f5997e45a57551be725)) + + +## v0.70.0 (2023-07-02) + +### Features + +- Add support for sending to a specific `addr` and `port` with `ServiceInfo.async_request` and + `ServiceInfo.request` ([#1192](https://github.com/python-zeroconf/python-zeroconf/pull/1192), + [`405f547`](https://github.com/python-zeroconf/python-zeroconf/commit/405f54762d3f61e97de9c1787e837e953de31412)) + + +## v0.69.0 (2023-06-18) + +### Features + +- Cython3 support ([#1190](https://github.com/python-zeroconf/python-zeroconf/pull/1190), + [`8ae8ba1`](https://github.com/python-zeroconf/python-zeroconf/commit/8ae8ba1af324b0c8c2da3bd12c264a5c0f3dcc3d)) + +- Reorder incoming data handler to reduce overhead + ([#1189](https://github.com/python-zeroconf/python-zeroconf/pull/1189), + [`32756ff`](https://github.com/python-zeroconf/python-zeroconf/commit/32756ff113f675b7a9cf16d3c0ab840ba733e5e4)) + + +## v0.68.1 (2023-06-18) + +### Bug Fixes + +- Reduce debug logging overhead by adding missing checks to datagram_received + ([#1188](https://github.com/python-zeroconf/python-zeroconf/pull/1188), + [`ac5c50a`](https://github.com/python-zeroconf/python-zeroconf/commit/ac5c50afc70aaa33fcd20bf02222ff4f0c596fa3)) + + +## v0.68.0 (2023-06-17) + +### Features + +- Reduce overhead to handle queries and responses + ([#1184](https://github.com/python-zeroconf/python-zeroconf/pull/1184), + [`81126b7`](https://github.com/python-zeroconf/python-zeroconf/commit/81126b7600f94848ef8c58b70bac0c6ab993c6ae)) + +- adds slots to handler classes + +- avoid any expression overhead and inline instead + + +## v0.67.0 (2023-06-17) + +### Features + +- Speed up answering incoming questions + ([#1186](https://github.com/python-zeroconf/python-zeroconf/pull/1186), + [`8f37665`](https://github.com/python-zeroconf/python-zeroconf/commit/8f376658d2a3bef0353646e6fddfda15626b73a9)) + + +## v0.66.0 (2023-06-13) + +### Features + +- Optimize construction of outgoing dns records + ([#1182](https://github.com/python-zeroconf/python-zeroconf/pull/1182), + [`fc0341f`](https://github.com/python-zeroconf/python-zeroconf/commit/fc0341f281cdb71428c0f1cf90c12d34cbb4acae)) + + +## v0.65.0 (2023-06-13) + +### Features + +- Reduce overhead to enumerate ip addresses in ServiceInfo + ([#1181](https://github.com/python-zeroconf/python-zeroconf/pull/1181), + [`6a85cbf`](https://github.com/python-zeroconf/python-zeroconf/commit/6a85cbf2b872cb0abd184c2dd728d9ae3eb8115c)) + + +## v0.64.1 (2023-06-05) + +### Bug Fixes + +- Small internal typing cleanups + ([#1180](https://github.com/python-zeroconf/python-zeroconf/pull/1180), + [`f03e511`](https://github.com/python-zeroconf/python-zeroconf/commit/f03e511f7aae72c5ccd4f7514d89e168847bd7a2)) + + +## v0.64.0 (2023-06-05) + +### Bug Fixes + +- Always answer QU questions when the exact same packet is received from different sources in + sequence ([#1178](https://github.com/python-zeroconf/python-zeroconf/pull/1178), + [`74d7ba1`](https://github.com/python-zeroconf/python-zeroconf/commit/74d7ba1aeeae56be087ee8142ee6ca1219744baa)) + +If the exact same packet with a QU question is asked from two different sources in a 1s window we + end up ignoring the second one as a duplicate. We should still respond in this case because the + client wants a unicast response and the question may not be answered by the previous packet since + the response may not be multicast. + +fix: include NSEC records in initial broadcast when registering a new service + +This also revealed that we do not send NSEC records in the initial broadcast. This needed to be + fixed in this PR as well for everything to work as expected since all the tests would fail with 2 + updates otherwise. + +### Features + +- Speed up processing incoming records + ([#1179](https://github.com/python-zeroconf/python-zeroconf/pull/1179), + [`d919316`](https://github.com/python-zeroconf/python-zeroconf/commit/d9193160b05beeca3755e19fd377ba13fe37b071)) + + +## v0.63.0 (2023-05-25) + +### Features + +- Improve dns cache performance + ([#1172](https://github.com/python-zeroconf/python-zeroconf/pull/1172), + [`bb496a1`](https://github.com/python-zeroconf/python-zeroconf/commit/bb496a1dd5fa3562c0412cb064d14639a542592e)) + +- Small speed up to fetch dns addresses from ServiceInfo + ([#1176](https://github.com/python-zeroconf/python-zeroconf/pull/1176), + [`4deaa6e`](https://github.com/python-zeroconf/python-zeroconf/commit/4deaa6ed7c9161db55bf16ec068ab7260bbd4976)) + +- Speed up the service registry + ([#1174](https://github.com/python-zeroconf/python-zeroconf/pull/1174), + [`360ceb2`](https://github.com/python-zeroconf/python-zeroconf/commit/360ceb2548c4c4974ff798aac43a6fff9803ea0e)) + + +## v0.62.0 (2023-05-04) + +### Features + +- Improve performance of ServiceBrowser outgoing query scheduler + ([#1170](https://github.com/python-zeroconf/python-zeroconf/pull/1170), + [`963d022`](https://github.com/python-zeroconf/python-zeroconf/commit/963d022ef82b615540fa7521d164a98a6c6f5209)) + + +## v0.61.0 (2023-05-03) + +### Features + +- Speed up parsing NSEC records + ([#1169](https://github.com/python-zeroconf/python-zeroconf/pull/1169), + [`06fa94d`](https://github.com/python-zeroconf/python-zeroconf/commit/06fa94d87b4f0451cb475a921ce1d8e9562e0f26)) + + +## v0.60.0 (2023-05-01) + +### Features + +- Speed up processing incoming data + ([#1167](https://github.com/python-zeroconf/python-zeroconf/pull/1167), + [`fbaaf7b`](https://github.com/python-zeroconf/python-zeroconf/commit/fbaaf7bb6ff985bdabb85feb6cba144f12d4f1d6)) + + +## v0.59.0 (2023-05-01) + +### Features + +- Speed up decoding dns questions when processing incoming data + ([#1168](https://github.com/python-zeroconf/python-zeroconf/pull/1168), + [`f927190`](https://github.com/python-zeroconf/python-zeroconf/commit/f927190cb24f70fd7c825c6e12151fcc0daf3973)) + + +## v0.58.2 (2023-04-26) + +### Bug Fixes + +- Re-release to rebuild failed wheels + ([#1165](https://github.com/python-zeroconf/python-zeroconf/pull/1165), + [`4986271`](https://github.com/python-zeroconf/python-zeroconf/commit/498627166a4976f1d9d8cd1f3654b0d50272d266)) + + +## v0.58.1 (2023-04-26) + +### Bug Fixes + +- Reduce cast calls in service browser + ([#1164](https://github.com/python-zeroconf/python-zeroconf/pull/1164), + [`c0d65ae`](https://github.com/python-zeroconf/python-zeroconf/commit/c0d65aeae7037a18ed1149336f5e7bdb8b2dd8cf)) + + +## v0.58.0 (2023-04-23) + +### Features + +- Speed up incoming parser ([#1163](https://github.com/python-zeroconf/python-zeroconf/pull/1163), + [`4626399`](https://github.com/python-zeroconf/python-zeroconf/commit/46263999c0c7ea5176885f1eadd2c8498834b70e)) + + +## v0.57.0 (2023-04-23) + +### Features + +- Speed up incoming data parser + ([#1161](https://github.com/python-zeroconf/python-zeroconf/pull/1161), + [`cb4c3b2`](https://github.com/python-zeroconf/python-zeroconf/commit/cb4c3b2b80ca3b88b8de6e87062a45e03e8805a6)) + + +## v0.56.0 (2023-04-07) + +### Features + +- Reduce denial of service protection overhead + ([#1157](https://github.com/python-zeroconf/python-zeroconf/pull/1157), + [`2c2f26a`](https://github.com/python-zeroconf/python-zeroconf/commit/2c2f26a87d0aac81a77205b06bc9ba499caa2321)) + + +## v0.55.0 (2023-04-07) + +### Features + +- Improve performance of processing incoming records + ([#1155](https://github.com/python-zeroconf/python-zeroconf/pull/1155), + [`b65e279`](https://github.com/python-zeroconf/python-zeroconf/commit/b65e2792751c44e0fafe9ad3a55dadc5d8ee9d46)) + + +## v0.54.0 (2023-04-03) + +### Features + +- Avoid waking async_request when record updates are not relevant + ([#1153](https://github.com/python-zeroconf/python-zeroconf/pull/1153), + [`a3f970c`](https://github.com/python-zeroconf/python-zeroconf/commit/a3f970c7f66067cf2c302c49ed6ad8286f19b679)) + + +## v0.53.1 (2023-04-03) + +### Bug Fixes + +- Addresses incorrect after server name change + ([#1154](https://github.com/python-zeroconf/python-zeroconf/pull/1154), + [`41ea06a`](https://github.com/python-zeroconf/python-zeroconf/commit/41ea06a0192c0d186e678009285759eb37d880d5)) + + +## v0.53.0 (2023-04-02) + +### Bug Fixes + +- Make parsed_scoped_addresses return addresses in the same order as all other methods + ([#1150](https://github.com/python-zeroconf/python-zeroconf/pull/1150), + [`9b6adcf`](https://github.com/python-zeroconf/python-zeroconf/commit/9b6adcf5c04a469632ee866c32f5898c5cbf810a)) + +### Features + +- Improve ServiceBrowser performance by removing OrderedDict + ([#1148](https://github.com/python-zeroconf/python-zeroconf/pull/1148), + [`9a16be5`](https://github.com/python-zeroconf/python-zeroconf/commit/9a16be56a9f69a5d0f7cde13dc1337b6d93c1433)) + + +## v0.52.0 (2023-04-02) + +### Features + +- Add ip_addresses_by_version to ServiceInfo + ([#1145](https://github.com/python-zeroconf/python-zeroconf/pull/1145), + [`524494e`](https://github.com/python-zeroconf/python-zeroconf/commit/524494edd49bd049726b19ae8ac8f6eea69a3943)) + +- Include tests and docs in sdist archives + ([#1142](https://github.com/python-zeroconf/python-zeroconf/pull/1142), + [`da10a3b`](https://github.com/python-zeroconf/python-zeroconf/commit/da10a3b2827cee0719d3bb9152ae897f061c6e2e)) + +feat: Include tests and docs in sdist archives + +Include documentation and test files in source distributions, in order to make them more useful for + packagers (Linux distributions, Conda). Testing is an important part of packaging process, and at + least Gentoo users have requested offline documentation for Python packages. Furthermore, the + COPYING file was missing from sdist, even though it was referenced in README. + +- Small cleanups to cache cleanup interval + ([#1146](https://github.com/python-zeroconf/python-zeroconf/pull/1146), + [`b434b60`](https://github.com/python-zeroconf/python-zeroconf/commit/b434b60f14ebe8f114b7b19bb4f54081c8ae0173)) + +- Speed up matching types in the ServiceBrowser + ([#1144](https://github.com/python-zeroconf/python-zeroconf/pull/1144), + [`68871c3`](https://github.com/python-zeroconf/python-zeroconf/commit/68871c3b5569e41740a66b7d3d7fa5cc41514ea5)) + +- Speed up processing records in the ServiceBrowser + ([#1143](https://github.com/python-zeroconf/python-zeroconf/pull/1143), + [`6a327d0`](https://github.com/python-zeroconf/python-zeroconf/commit/6a327d00ffb81de55b7c5b599893c789996680c1)) + + +## v0.51.0 (2023-04-01) + +### Features + +- Improve performance of constructing ServiceInfo + ([#1141](https://github.com/python-zeroconf/python-zeroconf/pull/1141), + [`36d5b45`](https://github.com/python-zeroconf/python-zeroconf/commit/36d5b45a4ece1dca902e9c3c79b5a63b8d9ae41f)) + + +## v0.50.0 (2023-04-01) + +### Features + +- Small speed up to handler dispatch + ([#1140](https://github.com/python-zeroconf/python-zeroconf/pull/1140), + [`5bd1b6e`](https://github.com/python-zeroconf/python-zeroconf/commit/5bd1b6e7b4dd796069461c737ded956305096307)) + + +## v0.49.0 (2023-04-01) + +### Features + +- Speed up processing incoming records + ([#1139](https://github.com/python-zeroconf/python-zeroconf/pull/1139), + [`7246a34`](https://github.com/python-zeroconf/python-zeroconf/commit/7246a344b6c0543871b40715c95c9435db4c7f81)) + + +## v0.48.0 (2023-04-01) + +### Features + +- Reduce overhead to send responses + ([#1135](https://github.com/python-zeroconf/python-zeroconf/pull/1135), + [`c4077dd`](https://github.com/python-zeroconf/python-zeroconf/commit/c4077dde6dfde9e2598eb63daa03c36063a3e7b0)) + + +## v0.47.4 (2023-03-20) + +### Bug Fixes + +- Correct duplicate record entries in windows wheels by updating poetry-core + ([#1134](https://github.com/python-zeroconf/python-zeroconf/pull/1134), + [`a43055d`](https://github.com/python-zeroconf/python-zeroconf/commit/a43055d3fa258cd762c3e9394b01f8bdcb24f97e)) + + +## v0.47.3 (2023-02-14) + +### Bug Fixes + +- Hold a strong reference to the query sender start task + ([#1128](https://github.com/python-zeroconf/python-zeroconf/pull/1128), + [`808c3b2`](https://github.com/python-zeroconf/python-zeroconf/commit/808c3b2194a7f499a469a9893102d328ccee83db)) + + +## v0.47.2 (2023-02-14) + +### Bug Fixes + +- Missing c extensions with newer poetry + ([#1129](https://github.com/python-zeroconf/python-zeroconf/pull/1129), + [`44d7fc6`](https://github.com/python-zeroconf/python-zeroconf/commit/44d7fc6483485102f60c91d591d0d697872f8865)) + + +## v0.47.1 (2022-12-24) + +### Bug Fixes + +- The equality checks for DNSPointer and DNSService should be case insensitive + ([#1122](https://github.com/python-zeroconf/python-zeroconf/pull/1122), + [`48ae77f`](https://github.com/python-zeroconf/python-zeroconf/commit/48ae77f026a96e2ca475b0ff80cb6d22207ce52f)) + + +## v0.47.0 (2022-12-22) + +### Features + +- Optimize equality checks for DNS records + ([#1120](https://github.com/python-zeroconf/python-zeroconf/pull/1120), + [`3a25ff7`](https://github.com/python-zeroconf/python-zeroconf/commit/3a25ff74bea83cd7d50888ce1ebfd7650d704bfa)) + + +## v0.46.0 (2022-12-21) + +### Features + +- Optimize the dns cache ([#1119](https://github.com/python-zeroconf/python-zeroconf/pull/1119), + [`e80fcef`](https://github.com/python-zeroconf/python-zeroconf/commit/e80fcef967024f8e846e44b464a82a25f5550edf)) + + +## v0.45.0 (2022-12-20) + +### Features + +- Optimize construction of outgoing packets + ([#1118](https://github.com/python-zeroconf/python-zeroconf/pull/1118), + [`81e186d`](https://github.com/python-zeroconf/python-zeroconf/commit/81e186d365c018381f9b486a4dbe4e2e4b8bacbf)) + + +## v0.44.0 (2022-12-18) + +### Features + +- Optimize dns objects by adding pxd files + ([#1113](https://github.com/python-zeroconf/python-zeroconf/pull/1113), + [`919d4d8`](https://github.com/python-zeroconf/python-zeroconf/commit/919d4d875747b4fa68e25bccd5aae7f304d8a36d)) + + +## v0.43.0 (2022-12-18) + +### Features + +- Optimize incoming parser by reducing call stack + ([#1116](https://github.com/python-zeroconf/python-zeroconf/pull/1116), + [`11f3f0e`](https://github.com/python-zeroconf/python-zeroconf/commit/11f3f0e699e00c1ee3d6d8ab5e30f62525510589)) + + +## v0.42.0 (2022-12-18) + +### Features + +- Optimize incoming parser by using unpack_from + ([#1115](https://github.com/python-zeroconf/python-zeroconf/pull/1115), + [`a7d50ba`](https://github.com/python-zeroconf/python-zeroconf/commit/a7d50baab362eadd2d292df08a39de6836b41ea7)) + + +## v0.41.0 (2022-12-18) + +### Features + +- Optimize incoming parser by adding pxd files + ([#1111](https://github.com/python-zeroconf/python-zeroconf/pull/1111), + [`26efeb0`](https://github.com/python-zeroconf/python-zeroconf/commit/26efeb09783050266242542228f34eb4dd83e30c)) + + +## v0.40.1 (2022-12-18) + +### Bug Fixes + +- Fix project name in pyproject.toml + ([#1112](https://github.com/python-zeroconf/python-zeroconf/pull/1112), + [`a330f62`](https://github.com/python-zeroconf/python-zeroconf/commit/a330f62040475257c4a983044e1675aeb95e030a)) + + +## v0.40.0 (2022-12-17) + +### Features + +- Drop async_timeout requirement for python 3.11+ + ([#1107](https://github.com/python-zeroconf/python-zeroconf/pull/1107), + [`1f4224e`](https://github.com/python-zeroconf/python-zeroconf/commit/1f4224ef122299235013cb81b501f8ff9a30dea1)) + + +## v0.39.5 (2022-12-17) + + +## v0.39.4 (2022-10-31) + + +## v0.39.3 (2022-10-26) + + +## v0.39.2 (2022-10-20) + + +## v0.39.1 (2022-09-05) + + +## v0.39.0 (2022-08-05) + + +## v0.38.7 (2022-06-14) + + +## v0.38.6 (2022-05-06) + + +## v0.38.5 (2022-05-01) + + +## v0.38.4 (2022-02-28) + + +## v0.38.3 (2022-01-31) + + +## v0.38.2 (2022-01-31) + + +## v0.38.1 (2021-12-23) + + +## v0.38.0 (2021-12-23) + + +## v0.37.0 (2021-11-18) + + +## v0.36.13 (2021-11-13) + + +## v0.36.12 (2021-11-05) + + +## v0.36.11 (2021-10-30) + + +## v0.36.10 (2021-10-30) + + +## v0.36.9 (2021-10-22) + + +## v0.36.8 (2021-10-10) + + +## v0.36.7 (2021-09-22) + + +## v0.36.6 (2021-09-19) + + +## v0.36.5 (2021-09-18) + + +## v0.36.4 (2021-09-16) + + +## v0.36.3 (2021-09-14) + + +## v0.36.2 (2021-08-30) + + +## v0.36.1 (2021-08-29) + + +## v0.36.0 (2021-08-16) + + +## v0.35.1 (2021-08-15) + + +## v0.35.0 (2021-08-13) + + +## v0.34.3 (2021-08-09) + + +## v0.34.2 (2021-08-09) + + +## v0.34.1 (2021-08-08) + + +## v0.34.0 (2021-08-08) + + +## v0.33.4 (2021-08-06) + + +## v0.33.3 (2021-08-05) + + +## v0.33.2 (2021-07-28) + + +## v0.33.1 (2021-07-18) + + +## v0.33.0 (2021-07-18) + + +## v0.32.1 (2021-07-05) + + +## v0.32.0 (2021-06-30) + + +## v0.29.0 (2021-03-25) + + +## v0.28.8 (2021-01-04) + + +## v0.28.7 (2020-12-13) + + +## v0.28.6 (2020-10-13) + + +## v0.28.5 (2020-09-11) + + +## v0.28.4 (2020-09-06) + + +## v0.28.3 (2020-08-31) + + +## v0.28.2 (2020-08-27) + + +## v0.28.1 (2020-08-17) + + +## v0.28.0 (2020-07-07) + + +## v0.27.1 (2020-06-05) + + +## v0.27.0 (2020-05-27) + + +## v0.26.3 (2020-05-26) + + +## v0.26.1 (2020-05-06) + + +## v0.26.0 (2020-04-26) + + +## v0.25.1 (2020-04-14) + + +## v0.25.0 (2020-04-03) + + +## v0.24.5 (2020-03-08) + + +## v0.24.4 (2019-12-30) + + +## v0.24.3 (2019-12-23) + + +## v0.24.2 (2019-12-17) + + +## v0.24.1 (2019-12-16) + + +## v0.24.0 (2019-11-19) + + +## v0.23.0 (2019-06-04) + + +## v0.22.0 (2019-04-27) + + +## v0.21.3 (2018-09-21) + + +## v0.21.2 (2018-09-20) + + +## v0.21.1 (2018-09-17) + + +## v0.21.0 (2018-09-16) + + +## v0.20.0 (2018-02-21) + + +## v0.19.1 (2017-06-13) + + +## v0.19.0 (2017-03-21) + + +## v0.18.0 (2017-02-03) + + +## v0.17.7 (2017-02-01) + + +## v0.17.6 (2016-07-08) + +### Testing + +- Added test for DNS-SD subtype discovery + ([`914241b`](https://github.com/python-zeroconf/python-zeroconf/commit/914241b92c3097669e1e8c1a380f6c2f23a14cf8)) + + +## v0.17.5 (2016-03-14) + + +## v0.17.4 (2015-09-22) + + +## v0.17.3 (2015-08-19) + + +## v0.17.2 (2015-07-12) + + +## v0.17.1 (2015-04-10) + + +## v0.17.0 (2015-04-10) + + +## v0.15.1 (2014-07-10) + +- Initial Release diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 000000000..e64b2207d --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,287 @@ +# Notes for LLM contributors + +A short orientation file for an LLM working in this repo. Skim +before making changes; keep edits consistent with what's described +here. Read [README.rst](README.rst) for the user-facing intro. + +## What this project is + +`python-zeroconf` is a pure-Python implementation of multicast DNS +service discovery (mDNS / DNS-SD, RFC 6762 + RFC 6763). It is the +mDNS engine behind Home Assistant and a long list of other Python +projects that need to announce or discover services on the local +network. Public API is exported from the top-level `zeroconf` +package; an async API lives at `zeroconf.asyncio`. + +There is no external protocol owner — the on-the-wire format is +the mDNS / DNS-SD RFCs. Behaviour changes that affect packet +contents or timing should cite the relevant RFC section. + +Hot paths (`_cache`, `_dns`, `_history`, `_listener`, +`_record_update`, `_updates`, `_protocol/{incoming,outgoing}`, +`_handlers/*`, `_services/*`, `_utils/{ipaddress,time}`) are +Cythonized at build time for throughput. They keep working as +pure Python — `SKIP_CYTHON=1` disables the extension build — but +production wheels ship compiled and CodSpeed benchmarks track that +path. The authoritative list of cythonized modules lives in +`build_ext.py` (`TO_CYTHONIZE`). + +## Code style + +- **Docstrings: terse, default to single-line.** A docstring is + the function's _contract_, not its narrative. Almost every + docstring should be one line — `"""Summary."""` — describing + what the function does and what the caller can pass. Multi-line + is the exception, only justified when there is non-obvious + caller-visible behaviour the type signature and parameter names + don't already convey. + + **What does NOT belong in docstrings or comments:** + - Rationale / motivation / "why we used to do X" — that's the + PR description and the commit message. Git already remembers. + - Cross-references to issue numbers ("closes #N", "follow-up + to #M") — the PR body carries those. + - Restatement of the function body in prose. If the next line + of the docstring is just describing what the next line of + code does, delete the docstring line. + - Test docstrings retelling the production-side story. A test + docstring should name what the test pins, in one sentence — + not re-explain the bug, the fix, or the surrounding flow. + +- **Comments**: same bar. Default to writing no comments. Add + one only when the _why_ is non-obvious: a hidden constraint, a + subtle invariant, a workaround for a specific bug, behaviour + that would surprise a reader. RFC citations are useful when the + reason for a timing constant or framing decision is "the spec + says so" — leave those in. If removing the comment wouldn't + confuse a future reader, don't write it. + + **Don't remove existing comments** unless the code they + describe is gone — the original author left them for a reason. + +- **Don't pad commits, docstrings, or comments with cross- + references** to old codepaths or issue numbers unless there's + a clear reason a future reader needs that link. + +- **Method order**: public API at the top, private helpers + (`_underscore_prefixed`) at the bottom. Modules whose names + start with `_` (`_cache`, `_dns`, `_handlers/`, etc.) are + internal; the supported surface is what `zeroconf/__init__.py` + and `zeroconf/asyncio.py` re-export. + +- **Line length**: 110 (ruff `line-length = 110`). + `requires-python = ">=3.10"`, `target-version = "py310"` for + ruff; pyupgrade runs `--py310-plus`. + +- **Imports**: ruff/isort sorted, `profile = "black"`, + `known_first_party = ["zeroconf", "tests"]`. Prefer + `from __future__ import annotations` so modern type syntax + works on 3.10. + +- **Generated `.c` files are not lint-targets.** `*.c` files + next to each cythonized module are Cython output — never hand- + edit them. They are excluded from sdist (`exclude = ["**/*.c"]` + in `pyproject.toml`) and regenerated by the build. + +## Commit / PR conventions + +- **Conventional Commits PR title, lowercase subject.** PRs are + squash-merged, so the **PR title** becomes the commit on + `master` and is the only string that has to parse as a + Conventional Commit. The repo enforces this via the `pr-title` + CI job in `ci.yml` using `amannn/action-semantic-pull-request`. + Accepted types: `feat`, `fix`, `chore`, `ci`, `docs`, + `refactor`, `test`, `perf`, `build`, etc. The subject (text + after `type(scope):`) must start lowercase (enforced by + `subjectPattern: ^(?![A-Z]).+$`). Per-commit messages on the + PR branch are **not** linted; they get collapsed at squash- + merge. `semantic-release` excludes `chore*` and `ci*` from the + changelog, so use those prefixes for housekeeping and reserve + `feat`/`fix`/`perf` for user-visible changes. +- **No `Co-Authored-By` trailers from automated agents.** Project + preference. +- Imperative-mood subject after the type prefix ("fix: handle + empty answer", not "fix: handled empty answer"). +- There is no `.github/PULL_REQUEST_TEMPLATE.md` in this repo — + the PR body is free-form. The `pr-workflow` skill (under + `.claude/skills/pr-workflow/`) walks through the conventions + that do apply: conventional-commit subject, RFC citations for + protocol-affecting changes, a test-plan section. +- Pre-commit runs ruff (lint + format), mypy, flake8, codespell, + cython-lint, and pyupgrade. Run pre-commit locally before + pushing; the CI `lint` job is just `pre-commit/action`, so a + green local pre-commit run = a green CI lint job. + +## Running tests + +```bash +poetry run pytest --durations=20 --timeout=60 -v tests +``` + +…or `make test`, which runs the same command. Test discovery +defaults from `pyproject.toml` already pass `--cov=zeroconf` +and `pythonpath = ["src"]`. `pytest-asyncio` is used in the +default per-test mode (no auto mode); async tests are marked +explicitly with `@pytest.mark.asyncio`. + +CodSpeed benchmarks live under `tests/benchmarks/` and run in CI +through `CodSpeedHQ/action`. Ad-hoc microbenchmarks for manual +profiling live under `bench/` — those don't run in CI. + +The CI matrix includes CPython 3.10 – 3.14, the free-threaded +3.14t build, and PyPy 3.10. Don't add anything that breaks +on the free-threaded build (no module-level mutable globals +mutated from multiple threads without locks; no +`PyDict_Next`-style escape hatches in Cython). + +## Build conventions + +- **Cython is optional but expected in wheels.** `build_ext.py` + cythonizes every module listed in `TO_CYTHONIZE`. The build is + driven by `poetry-core` (`generate-setup-file = true`, + `script = "build_ext.py"`); `BuildExt.build_extensions` + swallows build failures so source installs fall back to pure + Python. `SKIP_CYTHON=1` skips the Cython step entirely; + `REQUIRE_CYTHON=1` re-raises so a missing extension fails the + build loudly (CI wheel builds use this). +- **Modules that get Cythonized ship a sibling `.pxd`** for type + declarations. When changing the signature of a Cythonized + function — or adding a new attribute to a `cdef class` — update + the `.pxd` in the same commit, or the extension will pick up a + stale declaration and the in-tree `.c` will be regenerated with + the wrong layout. +- Adding a new module to `TO_CYTHONIZE` is a deliberate decision: + the module must be hot enough to matter, must not rely on + Python-only constructs that Cython refuses (the existing + `PERF401`, `PYI032`, `PYI041` ruff ignores exist because Cython + rejects closures and PEP 604 unions in `cpdef`), and must stay + free-threading-safe. +- `compiler_directives = {"language_level": "3"}`. The build + pipeline does not currently set `freethreading_compatible`, + but the test matrix exercises 3.14t, so any new Cython module + needs to keep working there. + +## Cython gotchas + +Non-obvious traps in the `.py` + `.pxd` setup that work fine in +pure-Python mode but break or silently misbehave in the shipped +Cython wheels. Distilled from the patterns that already exist in +this repo's `.pxd` files and from incidents in sibling Cython- +accelerated projects. + +- **`cdef`-typed module constants are not Python-importable.** + Declaring `cdef unsigned int _ANSWER_STRATEGY_POINTER` in + `query_handler.pxd` makes Cython treat + `_ANSWER_STRATEGY_POINTER = 1` in `query_handler.py` as a C int + assignment; the Python module dict never gets the binding. + `from zeroconf._handlers.query_handler import +_ANSWER_STRATEGY_POINTER` succeeds in pure-Python but raises + `ImportError` under Cython. If you need the value visible from + Python (e.g. a test wants to assert on it), define both names — + a public `ANSWER_STRATEGY_POINTER = 1` Python binding plus a + `cdef`-typed `_ANSWER_STRATEGY_POINTER = ANSWER_STRATEGY_POINTER` + alias for hot-path comparisons. + +- **Match the existing `unsigned int` convention for length, TTL, + type/class, and offset fields.** `_protocol/incoming.pxd`, + `_cache.pxd`, and `_handlers/*.pxd` already declare these as + `unsigned int` end-to-end. Introducing a `cdef int` return that + carries a value originally decoded into `unsigned int` flips + sign for any value with bit 31 set — TTL is a 32-bit DNS field + (RFC 1035 §3.2.1, interpreted as unsigned), so a large TTL + passed back through a `cdef int` boundary becomes negative and + trips `< 0` sentinel branches. Stay with `unsigned int` across + the whole call chain; if you need a real sentinel, return an + explicit value (`UINT_MAX`, a dedicated constant) and check for + it by equality. + +- **Module-level Python int constants force `PyLong_AsLong` on + every hot-path comparison.** `if record.type == _TYPE_PTR` + compiles to a Python attribute lookup + `PyLong_AsLong` per + call when `_TYPE_PTR` is just a `.py`-level binding. The repo + already follows the right pattern — `_cache.pxd` / + `record_manager.pxd` declare `cdef unsigned int _TYPE_PTR`, + `_DNS_PTR_MIN_TTL`, `_MIN_SCHEDULED_RECORD_EXPIRATION`, etc. + When adding a new size / TTL / type constant from `const.py` + to a `cdef` hot path in `_protocol/`, `_cache`, `_handlers/`, + or `_listener`, add the `cdef`-typed alias to the corresponding + `.pxd` at the same time. + +- **Sign-compare warnings in generated C are real.** `gcc`/ + `clang` warns when comparing `unsigned int` with `int` because + the signed value is implicitly converted to unsigned for the + compare — a negative value becomes a huge positive. Match the + signedness of compared operands in the `.pxd` (e.g. if the + local is `unsigned int`, declare the constant as + `cdef unsigned int`; if the local is `int`, declare it + `cdef int`). The warning predicts the unsigned -> signed + overflow class of bug. + +- **CodSpeed regressions only show up in the Cython build.** + Pure-Python (`SKIP_CYTHON=1`) tests can pass while production + wire-format hot paths regress. Trust the CodSpeed check on PRs + that touch any file in `TO_CYTHONIZE`; rebuild in place with + `REQUIRE_CYTHON=1 poetry install --only=main,dev` before + pushing if perf-sensitive code changed. + +## Reporting security issues + +Suspected security vulnerabilities go through GitHub's [private +vulnerability reporting][gh-report], not public issues or pull +requests. The policy is spelled out in [SECURITY.md](SECURITY.md). +If a user describes what sounds like a vulnerability in chat, +point them at that route instead of opening a public issue, PR, +or commit that names the bug class and the affected code path. + +[gh-report]: https://github.com/python-zeroconf/python-zeroconf/security/advisories/new + +## Useful entry points + +| Path | What | +| -------------------------------- | ------------------------------------------------------------------------------ | +| `src/zeroconf/__init__.py` | Public package — re-exports `Zeroconf`, `ServiceBrowser`, `ServiceInfo`, etc. | +| `src/zeroconf/asyncio.py` | Async API: `AsyncZeroconf`, `AsyncServiceBrowser`, `AsyncZeroconfServiceTypes` | +| `src/zeroconf/_core.py` | `Zeroconf` core — socket setup, send/recv loop, registration/probing | +| `src/zeroconf/_engine.py` | Asyncio engine driving the listener | +| `src/zeroconf/_listener.py` | Cython-accelerated packet listener | +| `src/zeroconf/_cache.py` | DNS record cache (Cythonized) | +| `src/zeroconf/_dns.py` | DNS record / question classes (Cythonized) | +| `src/zeroconf/_history.py` | Outgoing-question history for known-answer suppression | +| `src/zeroconf/_record_update.py` | Record-update dataclass passed to listeners | +| `src/zeroconf/_protocol/` | `DNSIncoming` / `DNSOutgoing` wire codec (Cythonized) | +| `src/zeroconf/_handlers/` | Query / answer / multicast queueing (Cythonized) | +| `src/zeroconf/_services/` | `ServiceBrowser`, `ServiceInfo`, `ServiceRegistry`, types | +| `src/zeroconf/_updates.py` | `RecordUpdateListener` base class (Cythonized) | +| `src/zeroconf/_utils/` | `ipaddress`, `time`, `net`, `name`, `asyncio` helpers | +| `src/zeroconf/const.py` | Timeouts, intervals, multicast group constants | +| `src/zeroconf/_exceptions.py` | Public exception hierarchy | +| `tests/` | Pytest suite | +| `tests/benchmarks/` | CodSpeed benchmarks | +| `bench/` | Manual microbenchmarks (not run in CI) | +| `build_ext.py` | `TO_CYTHONIZE` list + `poetry-core` build hook | + +## Things not to do + +- **Don't hand-edit the generated `.c` files** next to Cythonized + modules. They are build output; modify the `.py` (and `.pxd`) + and let Cython regenerate. +- **Don't change a Cythonized module's `cdef class` layout or a + `cpdef`/`cdef` signature without updating its `.pxd`** — the + extension build will silently pick up a stale declaration and + the resulting wheel will crash at import time. +- **Don't add `Co-Authored-By` trailers from automated agents + to commits** in this repo. +- **Don't introduce a PR title that violates Conventional + Commits.** The `pr-title` job will fail the PR. +- **Don't tighten timings or constants in `const.py` without an + RFC citation in the commit message.** mDNS interop with + Avahi / Bonjour / Windows hinges on those numbers. +- **Don't bypass `BuildExt`'s exception swallowing in + `build_ext.py` without thought.** Pure-Python fallback is a + feature for source installs on platforms without a compiler + (and for the PyPy matrix entries, which never load the C + extensions). +- **Don't break the free-threaded test matrix entry (`3.14t`).** + CPython 3.14t exercises this code without the GIL; module- + level mutable state and unguarded cross-thread Cython attribute + access will surface as flakiness there before anywhere else. diff --git a/COPYING b/COPYING index 2cba2ac74..3e57b08d5 100644 --- a/COPYING +++ b/COPYING @@ -55,7 +55,7 @@ modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. - + Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a @@ -111,7 +111,7 @@ modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. - + GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION @@ -146,7 +146,7 @@ such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. - + 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an @@ -158,7 +158,7 @@ Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. - + 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 @@ -216,7 +216,7 @@ instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. - + Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. @@ -267,7 +267,7 @@ Library will still fall under Section 6.) distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. - + 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work @@ -329,7 +329,7 @@ restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. - + 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined @@ -370,7 +370,7 @@ subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. - + 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or @@ -422,7 +422,7 @@ conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. - + 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is diff --git a/MANIFEST.in b/MANIFEST.in index 9491f804d..f8eef3376 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ include README.rst include COPYING +global-exclude *.c diff --git a/Makefile b/Makefile index e1aa7fffb..95b371566 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,5 @@ -.PHONY: all virtualenv -MAX_LINE_LENGTH=110 +# version: 1.3 -virtualenv: ./env/requirements.built - -env: - virtualenv env - -./env/requirements.built: env requirements-dev.txt - ./env/bin/pip install -r requirements-dev.txt - cp requirements-dev.txt ./env/requirements.built - -flake8: - flake8 --max-line-length=$(MAX_LINE_LENGTH) examples *.py - -mypy: - mypy examples/*.py test_zeroconf.py zeroconf.py test: - nosetests -v - -test_coverage: - nosetests -v --with-coverage --cover-package=zeroconf - -autopep8: - autopep8 --max-line-length=$(MAX_LINE_LENGTH) -i examples *.py + poetry run pytest --durations=20 --timeout=60 -v tests diff --git a/README.rst b/README.rst index 36b521962..08d48822f 100644 --- a/README.rst +++ b/README.rst @@ -1,16 +1,25 @@ python-zeroconf =============== -.. image:: https://travis-ci.org/jstasiak/python-zeroconf.svg?branch=master - :target: https://travis-ci.org/jstasiak/python-zeroconf - +.. image:: https://github.com/python-zeroconf/python-zeroconf/workflows/CI/badge.svg + :target: https://github.com/python-zeroconf/python-zeroconf?query=workflow%3ACI+branch%3Amaster + .. image:: https://img.shields.io/pypi/v/zeroconf.svg :target: https://pypi.python.org/pypi/zeroconf -.. image:: https://img.shields.io/coveralls/jstasiak/python-zeroconf.svg - :target: https://coveralls.io/r/jstasiak/python-zeroconf +.. image:: https://codecov.io/gh/python-zeroconf/python-zeroconf/branch/master/graph/badge.svg + :target: https://codecov.io/gh/python-zeroconf/python-zeroconf + +.. image:: https://img.shields.io/endpoint?url=https://codspeed.io/badge.json + :target: https://codspeed.io/python-zeroconf/python-zeroconf + :alt: Codspeed.io status for python-zeroconf + +.. image:: https://readthedocs.org/projects/python-zeroconf/badge/?version=latest + :target: https://python-zeroconf.readthedocs.io/en/latest/?badge=latest + :alt: Documentation Status + +`Documentation `_. - This is fork of pyzeroconf, Multicast DNS Service Discovery for Python, originally by Paul Scott-Murphy (https://github.com/paulsm/pyzeroconf), modified by William McBrine (https://github.com/wmcbrine/pyzeroconf). @@ -21,7 +30,7 @@ The original William McBrine's fork note:: (and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. Before this, I was tracking the changes for zeroconf.py in three separate repos. I figured I should have an authoritative source. - + Although I make changes based on my experience with TiVos, I expect that they're generally applicable. This version also includes patches found on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere @@ -36,37 +45,51 @@ Compared to some other Zeroconf/Bonjour/Avahi Python packages, python-zeroconf: * isn't tied to Bonjour or Avahi * doesn't use D-Bus -* doesn't force you to use particular event loop or Twisted +* doesn't force you to use particular event loop or Twisted (asyncio is used under the hood but not required) * is pip-installable * has PyPI distribution +* has an optional cython extension for performance (pure python is supported as well) Python compatibility -------------------- -* CPython 3.4+ -* PyPy3 5.8+ +* CPython 3.10+ +* PyPy 3.10+ Versioning ---------- -This project's versions follow the following pattern: MAJOR.MINOR.PATCH. - -* MAJOR version has been 0 so far -* MINOR version is incremented on backward incompatible changes -* PATCH version is incremented on backward compatible changes +This project uses semantic versioning. Status ------ -There are some people using this package. I don't actively use it and as such -any help I can offer with regard to any issues is very limited. +This project is actively maintained. + +Traffic Reduction +----------------- +Before version 0.32, most traffic reduction techniques described in https://datatracker.ietf.org/doc/html/rfc6762#section-7 +where not implemented which could lead to excessive network traffic. It is highly recommended that version 0.32 or later +is used if this is a concern. + +IPv6 support +------------ + +IPv6 support is relatively new and currently limited, specifically: + +* `InterfaceChoice.All` is an alias for `InterfaceChoice.Default` on non-POSIX + systems. +* Dual-stack IPv6 sockets are used, which may not be supported everywhere (some + BSD variants do not have them). +* Listening on localhost (`::1`) does not work. Help with understanding why is + appreciated. How to get python-zeroconf? =========================== -* PyPI page https://pypi.python.org/pypi/zeroconf -* GitHub project https://github.com/jstasiak/python-zeroconf +* PyPI page https://pypi.org/project/zeroconf/ +* GitHub project https://github.com/python-zeroconf/python-zeroconf The easiest way to install python-zeroconf is using pip:: @@ -81,19 +104,22 @@ Here's an example of browsing for a service: .. code-block:: python - from zeroconf import ServiceBrowser, Zeroconf - - - class MyListener: - - def remove_service(self, zeroconf, type, name): - print("Service %s removed" % (name,)) - - def add_service(self, zeroconf, type, name): - info = zeroconf.get_service_info(type, name) - print("Service %s added, service info: %s" % (name, info)) - - + from zeroconf import ServiceBrowser, ServiceListener, Zeroconf + + + class MyListener(ServiceListener): + + def update_service(self, zc: Zeroconf, type_: str, name: str) -> None: + print(f"Service {name} updated") + + def remove_service(self, zc: Zeroconf, type_: str, name: str) -> None: + print(f"Service {name} removed") + + def add_service(self, zc: Zeroconf, type_: str, name: str) -> None: + info = zc.get_service_info(type_, name) + print(f"Service {name} added, service info: {info}") + + zeroconf = Zeroconf() listener = MyListener() browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) @@ -120,257 +146,15 @@ See examples directory for more. Changelog ========= -0.21.3 ------- - -* This time really allowed incoming service names to contain underscores (patch released - as part of 0.21.0 was defective) - -0.21.2 ------- - -* Fixed import-time typing-related TypeError when older typing version is used - -0.21.1 ------- - -* Fixed installation on Python 3.4 (we use typing now but there was no explicit dependency on it) - -0.21.0 ------- - -* Added an error message when importing the package using unsupported Python version -* Fixed TTL handling for published service -* Implemented unicast support -* Fixed WSL (Windows Subsystem for Linux) compatibility -* Fixed occasional UnboundLocalError issue -* Fixed UTF-8 multibyte name compression -* Switched from netifaces to ifaddr (pure Python) -* Allowed incoming service names to contain underscores - -0.20.0 ------- - -* Dropped support for Python 2 (this includes PyPy) and 3.3 -* Fixed some class' equality operators -* ServiceBrowser entries are being refreshed when 'stale' now -* Cache returns new records first now instead of last - -0.19.1 ------- - -* Allowed installation with netifaces >= 0.10.6 (a bug that was concerning us - got fixed) - -0.19.0 ------- - -* Technically backwards incompatible - restricted netifaces dependency version to - work around a bug, see https://github.com/jstasiak/python-zeroconf/issues/84 for - details - -0.18.0 ------- - -* Dropped Python 2.6 support -* Improved error handling inside code executed when Zeroconf object is being closed - -0.17.7 ------- - -* Better Handling of DNS Incoming Packets parsing exceptions -* Many exceptions will now log a warning the first time they are seen -* Catch and log sendto() errors -* Fix/Implement duplicate name change -* Fix overly strict name validation introduced in 0.17.6 -* Greatly improve handling of oversized packets including: - - - Implement name compression per RFC1035 - - Limit size of generated packets to 9000 bytes as per RFC6762 - - Better handle over sized incoming packets - -* Increased test coverage to 95% - -0.17.6 ------- - -* Many improvements to address race conditions and exceptions during ZC() - startup and shutdown, thanks to: morpav, veawor, justingiorgi, herczy, - stephenrauch -* Added more test coverage: strahlex, stephenrauch -* Stephen Rauch contributed: - - - Speed up browser startup - - Add ZeroconfServiceTypes() query class to discover all advertised service types - - Add full validation for service names, types and subtypes - - Fix for subtype browsing - - Fix DNSHInfo support - -0.17.5 ------- - -* Fixed OpenBSD compatibility, thanks to Alessio Sergi -* Fixed race condition on ServiceBrowser startup, thanks to gbiddison -* Fixed installation on some Python 3 systems, thanks to Per Sandström -* Fixed "size change during iteration" bug on Python 3, thanks to gbiddison - -0.17.4 ------- - -* Fixed support for Linux kernel versions < 3.9 (thanks to Giovanni Harting - and Luckydonald, GitHub pull request #26) - -0.17.3 ------- - -* Fixed DNSText repr on Python 3 (it'd crash when the text was longer than - 10 bytes), thanks to Paulus Schoutsen for the patch, GitHub pull request #24 - -0.17.2 ------- - -* Fixed installation on Python 3.4.3+ (was failing because of enum34 dependency - which fails to install on 3.4.3+, changed to depend on enum-compat instead; - thanks to Michael Brennan for the original patch, GitHub pull request #22) - -0.17.1 ------- - -* Fixed EADDRNOTAVAIL when attempting to use dummy network interfaces on Windows, - thanks to daid - -0.17.0 ------- - -* Added some Python dependencies so it's not zero-dependencies anymore -* Improved exception handling (it'll be quieter now) -* Messages are listened to and sent using all available network interfaces - by default (configurable); thanks to Marcus Müller -* Started using logging more freely -* Fixed a bug with binary strings as property values being converted to False - (https://github.com/jstasiak/python-zeroconf/pull/10); thanks to Dr. Seuss -* Added new ``ServiceBrowser`` event handler interface (see the examples) -* PyPy3 now officially supported -* Fixed ServiceInfo repr on Python 3, thanks to Yordan Miladinov - -0.16.0 ------- - -* Set up Python logging and started using it -* Cleaned up code style (includes migrating from camel case to snake case) - -0.15.1 ------- - -* Fixed handling closed socket (GitHub #4) - -0.15 ----- - -* Forked by Jakub Stasiak -* Made Python 3 compatible -* Added setup script, made installable by pip and uploaded to PyPI -* Set up Travis build -* Reformatted the code and moved files around -* Stopped catching BaseException in several places, that could hide errors -* Marked threads as daemonic, they won't keep application alive now - -0.14 ----- - -* Fix for SOL_IP undefined on some systems - thanks Mike Erdely. -* Cleaned up examples. -* Lowercased module name. - -0.13 ----- - -* Various minor changes; see git for details. -* No longer compatible with Python 2.2. Only tested with 2.5-2.7. -* Fork by William McBrine. - -0.12 ----- - -* allow selection of binding interface -* typo fix - Thanks A. M. Kuchlingi -* removed all use of word 'Rendezvous' - this is an API change - -0.11 ----- - -* correction to comments for addListener method -* support for new record types seen from OS X - - IPv6 address - - hostinfo - -* ignore unknown DNS record types -* fixes to name decoding -* works alongside other processes using port 5353 (e.g. on Mac OS X) -* tested against Mac OS X 10.3.2's mDNSResponder -* corrections to removal of list entries for service browser - -0.10 ----- - -* Jonathon Paisley contributed these corrections: - - - always multicast replies, even when query is unicast - - correct a pointer encoding problem - - can now write records in any order - - traceback shown on failure - - better TXT record parsing - - server is now separate from name - - can cancel a service browser - -* modified some unit tests to accommodate these changes - -0.09 ----- - -* remove all records on service unregistration -* fix DOS security problem with readName - -0.08 ----- - -* changed licensing to LGPL - -0.07 ----- - -* faster shutdown on engine -* pointer encoding of outgoing names -* ServiceBrowser now works -* new unit tests - -0.06 ----- -* small improvements with unit tests -* added defined exception types -* new style objects -* fixed hostname/interface problem -* fixed socket timeout problem -* fixed add_service_listener() typo bug -* using select() for socket reads -* tested on Debian unstable with Python 2.2.2 - -0.05 ----- - -* ensure case insensitivty on domain names -* support for unicast DNS queries - -0.04 ----- - -* added some unit tests -* added __ne__ adjuncts where required -* ensure names end in '.local.' -* timeout on receiving socket for clean shutdown - +`Changelog `_ License ======= -LGPL, see COPYING file for details. +GNU Lesser General Public License v2.1 or later (LGPL-2.1-or-later). + +The full text of LGPL 2.1 is included in the `COPYING `_ file. +You may, at your option, use this library under the terms of any later +version of the LGPL published by the Free Software Foundation. The +canonical SPDX identifier for this project is ``LGPL-2.1-or-later``, as +declared in ``pyproject.toml``. diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 000000000..5dee00d68 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,52 @@ +# Security Policy + +## Reporting a vulnerability + +Please report security vulnerabilities privately through GitHub's +[private vulnerability reporting][gh-report] for this repository. +That route sends the report directly to the maintainers and lets +us coordinate a fix, a CVE, and a release before public +disclosure. + +**Do not** open a regular GitHub issue, a pull request, or post +to a public channel (mailing list, chat room, Stack Overflow, +etc.) for a suspected vulnerability. If you are unsure whether +something is a vulnerability, use the private report — we would +rather see a false alarm than a public one. + +We aim to acknowledge new reports within a few business days. + +[gh-report]: https://github.com/python-zeroconf/python-zeroconf/security/advisories/new + +## Supported versions + +Security fixes are released against the latest `0.x` line on +PyPI. Older releases are not maintained — please upgrade to the +current release before reporting, and confirm the issue still +reproduces there. + +## Scope + +`python-zeroconf` is an mDNS / DNS-SD library. By design it +parses untrusted multicast traffic from the local network +(RFC 6762, RFC 6763). In-scope issues include: + +- Memory-safety, parsing, or denial-of-service issues triggered + by crafted mDNS / DNS-SD packets reaching `DNSIncoming`, the + record cache, the service registry, or listener callbacks. +- Logic bugs that cause the library to answer queries it should + not, leak information across interfaces, or hijack a service + name from another responder in a way the RFCs don't sanction. +- Issues in the build / packaging pipeline (`build_ext.py`, + wheel contents, signed-release flow) that could lead to a + compromised wheel on PyPI. + +Out of scope: + +- Risks inherent to running an mDNS responder on an untrusted + network — mDNS is unauthenticated by design (RFC 6762 §21). + Reports of the form "a malicious LAN peer can send packets" + are expected behaviour unless they cross one of the lines + above. +- Misconfiguration of a downstream application that uses the + library. diff --git a/bench/create_destory.py b/bench/create_destory.py new file mode 100644 index 000000000..6fde9ebe3 --- /dev/null +++ b/bench/create_destory.py @@ -0,0 +1,24 @@ +"""Benchmark for AsyncZeroconf.""" + +import asyncio +import time + +from zeroconf.asyncio import AsyncZeroconf + +iterations = 10000 + + +async def _create_destroy(count: int) -> None: + for _ in range(count): + async with AsyncZeroconf() as zc: + await zc.zeroconf.async_wait_for_start() + + +async def _run() -> None: + start = time.perf_counter() + await _create_destroy(iterations) + duration = time.perf_counter() - start + print(f"Creating and destroying {iterations} Zeroconf instances took {duration} seconds") + + +asyncio.run(_run()) diff --git a/bench/incoming.py b/bench/incoming.py new file mode 100644 index 000000000..eb35f8a92 --- /dev/null +++ b/bench/incoming.py @@ -0,0 +1,186 @@ +"""Benchmark for DNSIncoming.""" + +import socket +import timeit + +from zeroconf import ( + DNSAddress, + DNSIncoming, + DNSNsec, + DNSOutgoing, + DNSService, + DNSText, + const, +) + + +def generate_packets() -> list[bytes]: + out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) + address = socket.inet_pton(socket.AF_INET, "192.168.208.5") + + additionals = [ + { + "name": "HASS Bridge ZJWH FF5137._hap._tcp.local.", + "address": address, + "port": 51832, + "text": b"\x13md=HASS Bridge" + b" ZJWH\x06pv=1.0\x14id=01:6B:30:FF:51:37\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=L0m/aQ==", + }, + { + "name": "HASS Bridge 3K9A C2582A._hap._tcp.local.", + "address": address, + "port": 51834, + "text": b"\x13md=HASS Bridge" + b" 3K9A\x06pv=1.0\x14id=E2:AA:5B:C2:58:2A\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b2CnzQ==", + }, + { + "name": "Master Bed TV CEDB27._hap._tcp.local.", + "address": address, + "port": 51830, + "text": b"\x10md=Master Bed" + b" TV\x06pv=1.0\x14id=9E:B7:44:CE:DB:27\x05c#=18\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=CVj1kw==", + }, + { + "name": "Living Room TV 921B77._hap._tcp.local.", + "address": address, + "port": 51833, + "text": b"\x11md=Living Room" + b" TV\x06pv=1.0\x14id=11:61:E7:92:1B:77\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=qU77SQ==", + }, + { + "name": "HASS Bridge ZC8X FF413D._hap._tcp.local.", + "address": address, + "port": 51829, + "text": b"\x13md=HASS Bridge" + b" ZC8X\x06pv=1.0\x14id=96:14:45:FF:41:3D\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b0QZlg==", + }, + { + "name": "HASS Bridge WLTF 4BE61F._hap._tcp.local.", + "address": address, + "port": 51837, + "text": b"\x13md=HASS Bridge" + b" WLTF\x06pv=1.0\x14id=E0:E7:98:4B:E6:1F\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=ahAISA==", + }, + { + "name": "FrontdoorCamera 8941D1._hap._tcp.local.", + "address": address, + "port": 54898, + "text": b"\x12md=FrontdoorCamera\x06pv=1.0\x14id=9F:B7:DC:89:41:D1\x04c#=2\x04" + b"s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=0+MXmA==", + }, + { + "name": "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + "address": address, + "port": 51836, + "text": b"\x13md=HASS Bridge" + b" W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=6fLM5A==", + }, + { + "name": "HASS Bridge Y9OO EFF0A7._hap._tcp.local.", + "address": address, + "port": 51838, + "text": b"\x13md=HASS Bridge" + b" Y9OO\x06pv=1.0\x14id=D3:FE:98:EF:F0:A7\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=u3bdfw==", + }, + { + "name": "Snooze Room TV 6B89B0._hap._tcp.local.", + "address": address, + "port": 51835, + "text": b"\x11md=Snooze Room" + b" TV\x06pv=1.0\x14id=5F:D5:70:6B:89:B0\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=xNTqsg==", + }, + { + "name": "AlexanderHomeAssistant 74651D._hap._tcp.local.", + "address": address, + "port": 54811, + "text": b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05" + b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==", + }, + { + "name": "HASS Bridge OS95 39C053._hap._tcp.local.", + "address": address, + "port": 51831, + "text": b"\x13md=HASS Bridge" + b" OS95\x06pv=1.0\x14id=7E:8C:E6:39:C0:53\x05c#=12\x04s#=1\x04ff=0\x04ci=2" + b"\x04sf=0\x0bsh=Xfe5LQ==", + }, + ] + + out.add_answer_at_time( + DNSText( + "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1" + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + + for record in additionals: + out.add_additional_answer( + DNSService( + record["name"], # type: ignore + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + record["port"], # type: ignore + record["name"], # type: ignore + ) + ) + out.add_additional_answer( + DNSText( + record["name"], # type: ignore + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + record["text"], # type: ignore + ) + ) + out.add_additional_answer( + DNSAddress( + record["name"], # type: ignore + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + record["address"], # type: ignore + ) + ) + out.add_additional_answer( + DNSNsec( + record["name"], # type: ignore + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + record["name"], # type: ignore + [const._TYPE_TXT, const._TYPE_SRV], + ) + ) + + return out.packets() + + +packets = generate_packets() + + +def parse_incoming_message() -> None: + for packet in packets: + DNSIncoming(packet).answers # noqa: B018 + break + + +count = 100000 +time = timeit.Timer(parse_incoming_message).timeit(count) +print(f"Parsing {count} incoming messages took {time} seconds") diff --git a/bench/outgoing.py b/bench/outgoing.py new file mode 100644 index 000000000..8c8097cbf --- /dev/null +++ b/bench/outgoing.py @@ -0,0 +1,170 @@ +"""Benchmark for DNSOutgoing.""" + +import socket +import timeit + +from zeroconf import DNSAddress, DNSOutgoing, DNSService, DNSText, const +from zeroconf._protocol.outgoing import State + + +def generate_packets() -> DNSOutgoing: + out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) + address = socket.inet_pton(socket.AF_INET, "192.168.208.5") + + additionals = [ + { + "name": "HASS Bridge ZJWH FF5137._hap._tcp.local.", + "address": address, + "port": 51832, + "text": b"\x13md=HASS Bridge" + b" ZJWH\x06pv=1.0\x14id=01:6B:30:FF:51:37\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=L0m/aQ==", + }, + { + "name": "HASS Bridge 3K9A C2582A._hap._tcp.local.", + "address": address, + "port": 51834, + "text": b"\x13md=HASS Bridge" + b" 3K9A\x06pv=1.0\x14id=E2:AA:5B:C2:58:2A\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b2CnzQ==", + }, + { + "name": "Master Bed TV CEDB27._hap._tcp.local.", + "address": address, + "port": 51830, + "text": b"\x10md=Master Bed" + b" TV\x06pv=1.0\x14id=9E:B7:44:CE:DB:27\x05c#=18\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=CVj1kw==", + }, + { + "name": "Living Room TV 921B77._hap._tcp.local.", + "address": address, + "port": 51833, + "text": b"\x11md=Living Room" + b" TV\x06pv=1.0\x14id=11:61:E7:92:1B:77\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=qU77SQ==", + }, + { + "name": "HASS Bridge ZC8X FF413D._hap._tcp.local.", + "address": address, + "port": 51829, + "text": b"\x13md=HASS Bridge" + b" ZC8X\x06pv=1.0\x14id=96:14:45:FF:41:3D\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b0QZlg==", + }, + { + "name": "HASS Bridge WLTF 4BE61F._hap._tcp.local.", + "address": address, + "port": 51837, + "text": b"\x13md=HASS Bridge" + b" WLTF\x06pv=1.0\x14id=E0:E7:98:4B:E6:1F\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=ahAISA==", + }, + { + "name": "FrontdoorCamera 8941D1._hap._tcp.local.", + "address": address, + "port": 54898, + "text": b"\x12md=FrontdoorCamera\x06pv=1.0\x14id=9F:B7:DC:89:41:D1\x04c#=2\x04" + b"s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=0+MXmA==", + }, + { + "name": "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + "address": address, + "port": 51836, + "text": b"\x13md=HASS Bridge" + b" W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=6fLM5A==", + }, + { + "name": "HASS Bridge Y9OO EFF0A7._hap._tcp.local.", + "address": address, + "port": 51838, + "text": b"\x13md=HASS Bridge" + b" Y9OO\x06pv=1.0\x14id=D3:FE:98:EF:F0:A7\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=u3bdfw==", + }, + { + "name": "Snooze Room TV 6B89B0._hap._tcp.local.", + "address": address, + "port": 51835, + "text": b"\x11md=Snooze Room" + b" TV\x06pv=1.0\x14id=5F:D5:70:6B:89:B0\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=xNTqsg==", + }, + { + "name": "AlexanderHomeAssistant 74651D._hap._tcp.local.", + "address": address, + "port": 54811, + "text": b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05" + b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==", + }, + { + "name": "HASS Bridge OS95 39C053._hap._tcp.local.", + "address": address, + "port": 51831, + "text": b"\x13md=HASS Bridge" + b" OS95\x06pv=1.0\x14id=7E:8C:E6:39:C0:53\x05c#=12\x04s#=1\x04ff=0\x04ci=2" + b"\x04sf=0\x0bsh=Xfe5LQ==", + }, + ] + + out.add_answer_at_time( + DNSText( + "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1" + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + + for record in additionals: + out.add_additional_answer( + DNSService( + record["name"], # type: ignore + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + record["port"], # type: ignore + record["name"], # type: ignore + ) + ) + out.add_additional_answer( + DNSText( + record["name"], # type: ignore + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + record["text"], # type: ignore + ) + ) + out.add_additional_answer( + DNSAddress( + record["name"], # type: ignore + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + record["address"], # type: ignore + ) + ) + + return out + + +out = generate_packets() + + +def make_outgoing_message() -> None: + out.packets() + out.state = State.init.value + out.finished = False + out._reset_for_next_packet() + + +count = 100000 +time = timeit.Timer(make_outgoing_message).timeit(count) +print(f"Construction {count} outgoing messages took {time} seconds") diff --git a/bench/txt_properties.py b/bench/txt_properties.py new file mode 100644 index 000000000..f9adeccf1 --- /dev/null +++ b/bench/txt_properties.py @@ -0,0 +1,22 @@ +import timeit + +from zeroconf import ServiceInfo + +info = ServiceInfo( + "_test._tcp.local.", + "test._test._tcp.local.", + properties=( + b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05" + b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==" + ), +) + + +def process_properties() -> None: + info._properties = None + info.properties # noqa: B018 + + +count = 100000 +time = timeit.Timer(process_properties).timeit(count) +print(f"Processing {count} properties took {time} seconds") diff --git a/build_ext.py b/build_ext.py new file mode 100644 index 000000000..f77d3e2e6 --- /dev/null +++ b/build_ext.py @@ -0,0 +1,75 @@ +"""Build optional cython modules.""" + +import logging +import os +from distutils.command.build_ext import build_ext +from typing import Any + +try: + from setuptools import Extension +except ImportError: + from distutils.core import Extension + +_LOGGER = logging.getLogger(__name__) + +TO_CYTHONIZE = [ + "src/zeroconf/_dns.py", + "src/zeroconf/_cache.py", + "src/zeroconf/_history.py", + "src/zeroconf/_record_update.py", + "src/zeroconf/_listener.py", + "src/zeroconf/_protocol/incoming.py", + "src/zeroconf/_protocol/outgoing.py", + "src/zeroconf/_handlers/answers.py", + "src/zeroconf/_handlers/record_manager.py", + "src/zeroconf/_handlers/multicast_outgoing_queue.py", + "src/zeroconf/_handlers/query_handler.py", + "src/zeroconf/_services/__init__.py", + "src/zeroconf/_services/browser.py", + "src/zeroconf/_services/info.py", + "src/zeroconf/_services/registry.py", + "src/zeroconf/_updates.py", + "src/zeroconf/_utils/ipaddress.py", + "src/zeroconf/_utils/time.py", +] + +EXTENSIONS = [ + Extension( + ext.removeprefix("src/").removesuffix(".py").replace("/", "."), + [ext], + language="c", + extra_compile_args=["-O3", "-g0"], + ) + for ext in TO_CYTHONIZE +] + + +class BuildExt(build_ext): + def build_extensions(self) -> None: + if self.parallel is None: # type: ignore[has-type] + self.parallel = os.cpu_count() or 1 + try: + super().build_extensions() + except Exception: + _LOGGER.info("Failed to build cython extensions") + + +def build(setup_kwargs: Any) -> None: + if os.environ.get("SKIP_CYTHON"): + return + try: + from Cython.Build import cythonize # noqa: PLC0415 + + setup_kwargs.update( + { + "ext_modules": cythonize( + EXTENSIONS, + compiler_directives={"language_level": "3"}, # Python 3 + ), + "cmdclass": {"build_ext": BuildExt}, + } + ) + setup_kwargs["exclude_package_data"] = {pkg: ["*.c"] for pkg in setup_kwargs["packages"]} + except Exception: + if os.environ.get("REQUIRE_CYTHON"): + raise diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 000000000..d4bb2cbb9 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/_ext/zeroconfautodocfix.py b/docs/_ext/zeroconfautodocfix.py new file mode 100644 index 000000000..8163a9c6c --- /dev/null +++ b/docs/_ext/zeroconfautodocfix.py @@ -0,0 +1,19 @@ +""" +Must be included after 'sphinx.ext.autodoc'. Fixes unwanted 'alias of' behavior. +""" + +# pylint: disable=import-error +from sphinx.application import Sphinx + + +def skip_member(app, what, name, obj, skip: bool, options) -> bool: # type: ignore[no-untyped-def] + return ( + skip + or getattr(obj, "__doc__", None) is None + or getattr(obj, "__private__", False) is True + or getattr(getattr(obj, "__func__", None), "__private__", False) is True + ) + + +def setup(app: Sphinx) -> None: + app.connect("autodoc-skip-member", skip_member) diff --git a/docs/api.rst b/docs/api.rst new file mode 100644 index 000000000..1704db5af --- /dev/null +++ b/docs/api.rst @@ -0,0 +1,12 @@ +python-zeroconf API reference +============================= + +.. automodule:: zeroconf + :members: + :undoc-members: + :show-inheritance: + +.. automodule:: zeroconf.asyncio + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 000000000..11a0f2d43 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,76 @@ +# Configuration file for the Sphinx documentation builder. +# +# For the full list of built-in configuration values, see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html +import sys +from collections.abc import Sequence +from pathlib import Path + +# If your extensions are in another directory, add it here. If the directory +# is relative to the documentation root, use Path.absolute to make it absolute. +sys.path.append(str(Path(__file__).parent / "_ext")) +sys.path.insert(0, str(Path(__file__).parent.parent)) + +# -- Project information ----------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information + +project = "python-zeroconf" +project_copyright = "python-zeroconf authors" +author = "python-zeroconf authors" + +try: + import zeroconf + + # The short X.Y version. + version = zeroconf.__version__ + # The full version, including alpha/beta/rc tags. + release = version +except ImportError: + version = "" + release = "" + +# -- General configuration --------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration + +extensions = [ + "sphinx.ext.todo", # Allow todo comments. + "sphinx.ext.viewcode", # Link to source code. + "sphinx.ext.autodoc", + "zeroconfautodocfix", # Must be after "sphinx.ext.autodoc" + "sphinx.ext.intersphinx", + "sphinx.ext.coverage", # Enable the overage report. + "sphinx.ext.duration", # Show build duration at the end. + "sphinx_rtd_theme", # Required for theme. +] + +templates_path = ["_templates"] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] + +# -- Options for HTML output ------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output + +html_theme = "sphinx_rtd_theme" +html_static_path = ["_static"] + +# Custom sidebar templates, maps document names to template names. +html_sidebars: dict[str, Sequence[str]] = { + "index": ("sidebar.html", "sourcelink.html", "searchbox.html"), + "**": ("localtoc.html", "relations.html", "sourcelink.html", "searchbox.html"), +} + +# -- Options for RTD theme --------------------------------------------------- +# https://sphinx-rtd-theme.readthedocs.io/en/stable/configuring.html + +# html_theme_options = {} + +# -- Options for HTML help output -------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-help-output + +htmlhelp_basename = "zeroconfdoc" + +# -- Options for intersphinx extension --------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html#configuration + +intersphinx_mapping = { + "python": ("https://docs.python.org/3", None), +} diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 000000000..7899fad9e --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,29 @@ +Welcome to python-zeroconf documentation! +========================================= + +.. image:: https://github.com/jstasiak/python-zeroconf/workflows/CI/badge.svg + :target: https://github.com/jstasiak/python-zeroconf?query=workflow%3ACI+branch%3Amaster + +.. image:: https://img.shields.io/pypi/v/zeroconf.svg + :target: https://pypi.python.org/pypi/zeroconf + +.. image:: https://codecov.io/gh/jstasiak/python-zeroconf/branch/master/graph/badge.svg + :target: https://codecov.io/gh/jstasiak/python-zeroconf + +GitHub (code repository, issues): https://github.com/jstasiak/python-zeroconf + +PyPI (installable, stable distributions): https://pypi.org/project/zeroconf. You can install python-zeroconf using pip:: + + pip install zeroconf + +python-zeroconf works with CPython 3.8+ and PyPy 3 implementing Python 3.8+. + +Contents +-------- + +.. toctree:: + :maxdepth: 1 + + api + +See `the project's README `_ for more information. diff --git a/examples/async_apple_scanner.py b/examples/async_apple_scanner.py new file mode 100755 index 000000000..00744b5c5 --- /dev/null +++ b/examples/async_apple_scanner.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python + +"""Scan for apple devices.""" + +from __future__ import annotations + +import argparse +import asyncio +import logging +from typing import Any, cast + +from zeroconf import DNSQuestionType, IPVersion, ServiceStateChange, Zeroconf +from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconf + +HOMESHARING_SERVICE: str = "_appletv-v2._tcp.local." +DEVICE_SERVICE: str = "_touch-able._tcp.local." +MEDIAREMOTE_SERVICE: str = "_mediaremotetv._tcp.local." +AIRPLAY_SERVICE: str = "_airplay._tcp.local." +COMPANION_SERVICE: str = "_companion-link._tcp.local." +RAOP_SERVICE: str = "_raop._tcp.local." +AIRPORT_ADMIN_SERVICE: str = "_airport._tcp.local." +DEVICE_INFO_SERVICE: str = "_device-info._tcp.local." + +ALL_SERVICES = [ + HOMESHARING_SERVICE, + DEVICE_SERVICE, + MEDIAREMOTE_SERVICE, + AIRPLAY_SERVICE, + COMPANION_SERVICE, + RAOP_SERVICE, + AIRPORT_ADMIN_SERVICE, + DEVICE_INFO_SERVICE, +] + +log = logging.getLogger(__name__) + +_PENDING_TASKS: set[asyncio.Task] = set() + + +def async_on_service_state_change( + zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange +) -> None: + print(f"Service {name} of type {service_type} state changed: {state_change}") + if state_change is not ServiceStateChange.Added: + return + base_name = name[: -len(service_type) - 1] + device_name = f"{base_name}.{DEVICE_INFO_SERVICE}" + task = asyncio.ensure_future(_async_show_service_info(zeroconf, service_type, name)) + _PENDING_TASKS.add(task) + task.add_done_callback(_PENDING_TASKS.discard) + # Also probe for device info + task = asyncio.ensure_future(_async_show_service_info(zeroconf, DEVICE_INFO_SERVICE, device_name)) + _PENDING_TASKS.add(task) + task.add_done_callback(_PENDING_TASKS.discard) + + +async def _async_show_service_info(zeroconf: Zeroconf, service_type: str, name: str) -> None: + info = AsyncServiceInfo(service_type, name) + await info.async_request(zeroconf, 3000, question_type=DNSQuestionType.QU) + print(f"Info from zeroconf.get_service_info: {info!r}") + if info: + addresses = [f"{addr}:{cast(int, info.port)}" for addr in info.parsed_addresses()] + print(f" Name: {name}") + print(f" Addresses: {', '.join(addresses)}") + print(f" Weight: {info.weight}, priority: {info.priority}") + print(f" Server: {info.server}") + if info.properties: + print(" Properties are:") + for key, value in info.properties.items(): + print(f" {key!r}: {value!r}") + else: + print(" No properties") + else: + print(" No info") + print("\n") + + +class AsyncAppleScanner: + def __init__(self, args: Any) -> None: + self.args = args + self.aiobrowser: AsyncServiceBrowser | None = None + self.aiozc: AsyncZeroconf | None = None + + async def async_run(self) -> None: + self.aiozc = AsyncZeroconf(ip_version=ip_version) + await self.aiozc.zeroconf.async_wait_for_start() + print(f"\nBrowsing {ALL_SERVICES} service(s), press Ctrl-C to exit...\n") + kwargs = { + "handlers": [async_on_service_state_change], + "question_type": DNSQuestionType.QU, + } + if self.args.target: + kwargs["addr"] = self.args.target + self.aiobrowser = AsyncServiceBrowser( + self.aiozc.zeroconf, + ALL_SERVICES, + **kwargs, # type: ignore[arg-type] + ) + await asyncio.Event().wait() + + async def async_close(self) -> None: + assert self.aiozc is not None + assert self.aiobrowser is not None + await self.aiobrowser.async_cancel() + await self.aiozc.async_close() + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + + parser = argparse.ArgumentParser() + parser.add_argument("--debug", action="store_true") + version_group = parser.add_mutually_exclusive_group() + version_group.add_argument("--target", help="Unicast target") + version_group.add_argument("--v6", action="store_true") + version_group.add_argument("--v6-only", action="store_true") + args = parser.parse_args() + + if args.debug: + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + if args.v6: + ip_version = IPVersion.All + elif args.v6_only: + ip_version = IPVersion.V6Only + else: + ip_version = IPVersion.V4Only + + loop = asyncio.get_event_loop() + runner = AsyncAppleScanner(args) + try: + loop.run_until_complete(runner.async_run()) + except KeyboardInterrupt: + loop.run_until_complete(runner.async_close()) diff --git a/examples/async_browser.py b/examples/async_browser.py new file mode 100755 index 000000000..58193f705 --- /dev/null +++ b/examples/async_browser.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python + +"""Example of browsing for a service. + +The default is HTTP and HAP; use --find to search for all available services in the network +""" + +from __future__ import annotations + +import argparse +import asyncio +import logging +from typing import Any, cast + +from zeroconf import IPVersion, ServiceStateChange, Zeroconf +from zeroconf.asyncio import ( + AsyncServiceBrowser, + AsyncServiceInfo, + AsyncZeroconf, + AsyncZeroconfServiceTypes, +) + +_PENDING_TASKS: set[asyncio.Task] = set() + + +def async_on_service_state_change( + zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange +) -> None: + print(f"Service {name} of type {service_type} state changed: {state_change}") + if state_change is not ServiceStateChange.Added: + return + task = asyncio.ensure_future(async_display_service_info(zeroconf, service_type, name)) + _PENDING_TASKS.add(task) + task.add_done_callback(_PENDING_TASKS.discard) + + +async def async_display_service_info(zeroconf: Zeroconf, service_type: str, name: str) -> None: + info = AsyncServiceInfo(service_type, name) + await info.async_request(zeroconf, 3000) + print(f"Info from zeroconf.get_service_info: {info!r}") + if info: + addresses = [f"{addr}:{cast(int, info.port)}" for addr in info.parsed_scoped_addresses()] + print(f" Name: {name}") + print(f" Addresses: {', '.join(addresses)}") + print(f" Weight: {info.weight}, priority: {info.priority}") + print(f" Server: {info.server}") + if info.properties: + print(" Properties are:") + for key, value in info.properties.items(): + print(f" {key!r}: {value!r}") + else: + print(" No properties") + else: + print(" No info") + print("\n") + + +class AsyncRunner: + def __init__(self, args: Any) -> None: + self.args = args + self.aiobrowser: AsyncServiceBrowser | None = None + self.aiozc: AsyncZeroconf | None = None + + async def async_run(self) -> None: + self.aiozc = AsyncZeroconf(ip_version=ip_version) + + services = ["_http._tcp.local.", "_hap._tcp.local."] + if self.args.find: + services = list( + await AsyncZeroconfServiceTypes.async_find(aiozc=self.aiozc, ip_version=ip_version) + ) + + print(f"\nBrowsing {services} service(s), press Ctrl-C to exit...\n") + self.aiobrowser = AsyncServiceBrowser( + self.aiozc.zeroconf, services, handlers=[async_on_service_state_change] + ) + await asyncio.Event().wait() + + async def async_close(self) -> None: + assert self.aiozc is not None + assert self.aiobrowser is not None + await self.aiobrowser.async_cancel() + await self.aiozc.async_close() + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + + parser = argparse.ArgumentParser() + parser.add_argument("--debug", action="store_true") + parser.add_argument("--find", action="store_true", help="Browse all available services") + version_group = parser.add_mutually_exclusive_group() + version_group.add_argument("--v6", action="store_true") + version_group.add_argument("--v6-only", action="store_true") + args = parser.parse_args() + + if args.debug: + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + if args.v6: + ip_version = IPVersion.All + elif args.v6_only: + ip_version = IPVersion.V6Only + else: + ip_version = IPVersion.V4Only + + loop = asyncio.get_event_loop() + runner = AsyncRunner(args) + try: + loop.run_until_complete(runner.async_run()) + except KeyboardInterrupt: + loop.run_until_complete(runner.async_close()) diff --git a/examples/async_registration.py b/examples/async_registration.py new file mode 100755 index 000000000..5c774cadb --- /dev/null +++ b/examples/async_registration.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +"""Example of announcing 250 services (in this case, a fake HTTP server).""" + +from __future__ import annotations + +import argparse +import asyncio +import logging +import socket + +from zeroconf import IPVersion +from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf + + +class AsyncRunner: + def __init__(self, ip_version: IPVersion) -> None: + self.ip_version = ip_version + self.aiozc: AsyncZeroconf | None = None + + async def register_services(self, infos: list[AsyncServiceInfo]) -> None: + self.aiozc = AsyncZeroconf(ip_version=self.ip_version) + tasks = [self.aiozc.async_register_service(info) for info in infos] + background_tasks = await asyncio.gather(*tasks) + await asyncio.gather(*background_tasks) + print("Finished registration, press Ctrl-C to exit...") + await asyncio.Event().wait() + + async def unregister_services(self, infos: list[AsyncServiceInfo]) -> None: + assert self.aiozc is not None + tasks = [self.aiozc.async_unregister_service(info) for info in infos] + background_tasks = await asyncio.gather(*tasks) + await asyncio.gather(*background_tasks) + await self.aiozc.async_close() + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + + parser = argparse.ArgumentParser() + parser.add_argument("--debug", action="store_true") + version_group = parser.add_mutually_exclusive_group() + version_group.add_argument("--v6", action="store_true") + version_group.add_argument("--v6-only", action="store_true") + args = parser.parse_args() + + if args.debug: + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + if args.v6: + ip_version = IPVersion.All + elif args.v6_only: + ip_version = IPVersion.V6Only + else: + ip_version = IPVersion.V4Only + + infos = [] + for i in range(250): + infos.append( + AsyncServiceInfo( + "_http._tcp.local.", + f"Paul's Test Web Site {i}._http._tcp.local.", + addresses=[socket.inet_aton("127.0.0.1")], + port=80, + properties={"path": "/~paulsm/"}, + server=f"zcdemohost-{i}.local.", + ) + ) + + print("Registration of 250 services...") + loop = asyncio.get_event_loop() + runner = AsyncRunner(ip_version) + try: + loop.run_until_complete(runner.register_services(infos)) + except KeyboardInterrupt: + loop.run_until_complete(runner.unregister_services(infos)) diff --git a/examples/async_service_info_request.py b/examples/async_service_info_request.py new file mode 100755 index 000000000..ca75fc522 --- /dev/null +++ b/examples/async_service_info_request.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python + +"""Example of perodic dump of homekit services. + +This example is useful when a user wants an ondemand +list of HomeKit devices on the network. + +""" + +from __future__ import annotations + +import argparse +import asyncio +import logging +from typing import Any, cast + +from zeroconf import IPVersion, ServiceBrowser, ServiceStateChange, Zeroconf +from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf + +HAP_TYPE = "_hap._tcp.local." + + +async def async_watch_services(aiozc: AsyncZeroconf) -> None: + zeroconf = aiozc.zeroconf + while True: + await asyncio.sleep(5) + infos: list[AsyncServiceInfo] = [] + for name in zeroconf.cache.names(): + if not name.endswith(HAP_TYPE): + continue + infos.append(AsyncServiceInfo(HAP_TYPE, name)) + tasks = [info.async_request(aiozc.zeroconf, 3000) for info in infos] + await asyncio.gather(*tasks) + for info in infos: + print(f"Info for {info.name}") + if info: + addresses = [f"{addr}:{cast(int, info.port)}" for addr in info.parsed_addresses()] + print(f" Addresses: {', '.join(addresses)}") + print(f" Weight: {info.weight}, priority: {info.priority}") + print(f" Server: {info.server}") + if info.properties: + print(" Properties are:") + for key, value in info.properties.items(): + print(f" {key!r}: {value!r}") + else: + print(" No properties") + else: + print(" No info") + print("\n") + + +class AsyncRunner: + def __init__(self, args: Any) -> None: + self.args = args + self.threaded_browser: ServiceBrowser | None = None + self.aiozc: AsyncZeroconf | None = None + + async def async_run(self) -> None: + self.aiozc = AsyncZeroconf(ip_version=ip_version) + assert self.aiozc is not None + + def on_service_state_change( + zeroconf: Zeroconf, + service_type: str, + state_change: ServiceStateChange, + name: str, + ) -> None: + """Dummy handler.""" + + self.threaded_browser = ServiceBrowser( + self.aiozc.zeroconf, [HAP_TYPE], handlers=[on_service_state_change] + ) + await async_watch_services(self.aiozc) + + async def async_close(self) -> None: + assert self.aiozc is not None + assert self.threaded_browser is not None + self.threaded_browser.cancel() + await self.aiozc.async_close() + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + + parser = argparse.ArgumentParser() + parser.add_argument("--debug", action="store_true") + version_group = parser.add_mutually_exclusive_group() + version_group.add_argument("--v6", action="store_true") + version_group.add_argument("--v6-only", action="store_true") + args = parser.parse_args() + + if args.debug: + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + if args.v6: + ip_version = IPVersion.All + elif args.v6_only: + ip_version = IPVersion.V6Only + else: + ip_version = IPVersion.V4Only + + print(f"Services with {HAP_TYPE} will be shown every 5s, press Ctrl-C to exit...") + loop = asyncio.get_event_loop() + runner = AsyncRunner(args) + try: + loop.run_until_complete(runner.async_run()) + except KeyboardInterrupt: + loop.run_until_complete(runner.async_close()) diff --git a/examples/browser.py b/examples/browser.py index 17a778ac9..92adc9491 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -1,47 +1,84 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python -""" Example of browsing for a service (in this case, HTTP) """ +"""Example of browsing for a service. +The default is HTTP and HAP; use --find to search for all available services in the network +""" + +from __future__ import annotations + +import argparse import logging -import socket -import sys from time import sleep from typing import cast -from zeroconf import ServiceBrowser, ServiceStateChange, Zeroconf +from zeroconf import ( + IPVersion, + ServiceBrowser, + ServiceStateChange, + Zeroconf, + ZeroconfServiceTypes, +) def on_service_state_change( - zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange, + zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange ) -> None: - print("Service %s of type %s state changed: %s" % (name, service_type, state_change)) + print(f"Service {name} of type {service_type} state changed: {state_change}") if state_change is ServiceStateChange.Added: info = zeroconf.get_service_info(service_type, name) + print(f"Info from zeroconf.get_service_info: {info!r}") + if info: - print(" Address: %s:%d" % (socket.inet_ntoa(cast(bytes, info.address)), cast(int, info.port))) - print(" Weight: %d, priority: %d" % (info.weight, info.priority)) - print(" Server: %s" % (info.server,)) + addresses = [f"{addr}:{cast(int, info.port)}" for addr in info.parsed_scoped_addresses()] + print(f" Addresses: {', '.join(addresses)}") + print(f" Weight: {info.weight}, priority: {info.priority}") + print(f" Server: {info.server}") if info.properties: print(" Properties are:") for key, value in info.properties.items(): - print(" %s: %s" % (key, value)) + print(f" {key!r}: {value!r}") else: print(" No properties") else: print(" No info") - print('\n') + print("\n") -if __name__ == '__main__': +if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) - if len(sys.argv) > 1: - assert sys.argv[1:] == ['--debug'] - logging.getLogger('zeroconf').setLevel(logging.DEBUG) - zeroconf = Zeroconf() - print("\nBrowsing services, press Ctrl-C to exit...\n") - browser = ServiceBrowser(zeroconf, "_http._tcp.local.", handlers=[on_service_state_change]) + parser = argparse.ArgumentParser() + parser.add_argument("--debug", action="store_true") + parser.add_argument("--find", action="store_true", help="Browse all available services") + version_group = parser.add_mutually_exclusive_group() + version_group.add_argument("--v6-only", action="store_true") + version_group.add_argument("--v4-only", action="store_true") + args = parser.parse_args() + + if args.debug: + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + if args.v6_only: + ip_version = IPVersion.V6Only + elif args.v4_only: + ip_version = IPVersion.V4Only + else: + ip_version = IPVersion.All + + zeroconf = Zeroconf(ip_version=ip_version) + + services = [ + "_http._tcp.local.", + "_hap._tcp.local.", + "_esphomelib._tcp.local.", + "_airplay._tcp.local.", + ] + if args.find: + services = list(ZeroconfServiceTypes.find(zc=zeroconf)) + + print(f"\nBrowsing {len(services)} service(s), press Ctrl-C to exit...\n") + browser = ServiceBrowser(zeroconf, services, handlers=[on_service_state_change]) try: while True: diff --git a/examples/registration.py b/examples/registration.py index ad707c220..1ba19b16a 100755 --- a/examples/registration.py +++ b/examples/registration.py @@ -1,28 +1,47 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python -""" Example of announcing a service (in this case, a fake HTTP server) """ +"""Example of announcing a service (in this case, a fake HTTP server)""" +from __future__ import annotations + +import argparse import logging import socket -import sys from time import sleep -from zeroconf import ServiceInfo, Zeroconf +from zeroconf import IPVersion, ServiceInfo, Zeroconf -if __name__ == '__main__': +if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) - if len(sys.argv) > 1: - assert sys.argv[1:] == ['--debug'] - logging.getLogger('zeroconf').setLevel(logging.DEBUG) - desc = {'path': '/~paulsm/'} + parser = argparse.ArgumentParser() + parser.add_argument("--debug", action="store_true") + version_group = parser.add_mutually_exclusive_group() + version_group.add_argument("--v6", action="store_true") + version_group.add_argument("--v6-only", action="store_true") + args = parser.parse_args() + + if args.debug: + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + if args.v6: + ip_version = IPVersion.All + elif args.v6_only: + ip_version = IPVersion.V6Only + else: + ip_version = IPVersion.V4Only + + desc = {"path": "/~paulsm/"} - info = ServiceInfo("_http._tcp.local.", - "Paul's Test Web Site._http._tcp.local.", - socket.inet_aton("127.0.0.1"), 80, 0, 0, - desc, "ash-2.local.") + info = ServiceInfo( + "_http._tcp.local.", + "Paul's Test Web Site._http._tcp.local.", + addresses=[socket.inet_aton("127.0.0.1")], + port=80, + properties=desc, + server="ash-2.local.", + ) - zeroconf = Zeroconf() + zeroconf = Zeroconf(ip_version=ip_version) print("Registration of a service, press Ctrl-C to exit...") zeroconf.register_service(info) try: diff --git a/examples/resolve_address.py b/examples/resolve_address.py new file mode 100755 index 000000000..88ce825b7 --- /dev/null +++ b/examples/resolve_address.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +"""Example of resolving a name to an IP address.""" + +from __future__ import annotations + +import asyncio +import logging +import sys + +from zeroconf import AddressResolver, IPVersion +from zeroconf.asyncio import AsyncZeroconf + + +async def resolve_name(name: str) -> None: + aiozc = AsyncZeroconf() + await aiozc.zeroconf.async_wait_for_start() + resolver = AddressResolver(name) + if await resolver.async_request(aiozc.zeroconf, 3000): + print(f"{name} IP addresses:", resolver.ip_addresses_by_version(IPVersion.All)) + else: + print(f"Name {name} not resolved") + await aiozc.async_close() + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + argv = sys.argv.copy() + if "--debug" in argv: + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + argv.remove("--debug") + + if len(argv) < 2 or not argv[1]: + raise ValueError("Usage: resolve_address.py [--debug] ") + + name = argv[1] + if not name.endswith("."): + name += "." + + asyncio.run(resolve_name(name)) diff --git a/examples/resolver.py b/examples/resolver.py index 6a550fcb2..a52050f41 100755 --- a/examples/resolver.py +++ b/examples/resolver.py @@ -1,24 +1,26 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python -""" Example of resolving a service with a known name """ +"""Example of resolving a service with a known name""" + +from __future__ import annotations import logging import sys from zeroconf import Zeroconf -TYPE = '_test._tcp.local.' -NAME = 'My Service Name' +TYPE = "_test._tcp.local." +NAME = "My Service Name" -if __name__ == '__main__': +if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) if len(sys.argv) > 1: - assert sys.argv[1:] == ['--debug'] - logging.getLogger('zeroconf').setLevel(logging.DEBUG) + assert sys.argv[1:] == ["--debug"] + logging.getLogger("zeroconf").setLevel(logging.DEBUG) zeroconf = Zeroconf() try: - print(zeroconf.get_service_info(TYPE, NAME + '.' + TYPE)) + print(zeroconf.get_service_info(TYPE, NAME + "." + TYPE)) finally: zeroconf.close() diff --git a/examples/self_test.py b/examples/self_test.py index c5b56ecb2..3d1fa050c 100755 --- a/examples/self_test.py +++ b/examples/self_test.py @@ -1,4 +1,5 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python +from __future__ import annotations import logging import socket @@ -6,32 +7,41 @@ from zeroconf import ServiceInfo, Zeroconf, __version__ -if __name__ == '__main__': +if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) if len(sys.argv) > 1: - assert sys.argv[1:] == ['--debug'] - logging.getLogger('zeroconf').setLevel(logging.DEBUG) + assert sys.argv[1:] == ["--debug"] + logging.getLogger("zeroconf").setLevel(logging.DEBUG) # Test a few module features, including service registration, service # query (for Zoe), and service unregistration. - print("Multicast DNS Service Discovery for Python, version %s" % (__version__,)) + print(f"Multicast DNS Service Discovery for Python, version {__version__}") r = Zeroconf() print("1. Testing registration of a service...") - desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'} - info = ServiceInfo("_http._tcp.local.", - "My Service Name._http._tcp.local.", - socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) + desc = {"version": "0.10", "a": "test value", "b": "another value"} + addresses = [socket.inet_aton("127.0.0.1")] + expected = {"127.0.0.1"} + if socket.has_ipv6: + addresses.append(socket.inet_pton(socket.AF_INET6, "::1")) + expected.add("::1") + info = ServiceInfo( + "_http._tcp.local.", + "My Service Name._http._tcp.local.", + addresses=addresses, + port=1234, + properties=desc, + ) print(" Registering service...") r.register_service(info) print(" Registration done.") print("2. Testing query of service information...") - print(" Getting ZOE service: %s" % ( - r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local."))) + print(f" Getting ZOE service: {r.get_service_info('_http._tcp.local.', 'ZOE._http._tcp.local.')}") print(" Query done.") print("3. Testing query of own service...") queried_info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.") assert queried_info - print(" Getting self: %s" % (queried_info,)) + assert set(queried_info.parsed_addresses()) == expected + print(f" Getting self: {queried_info}") print(" Query done.") print("4. Testing unregister of service information...") r.unregister_service(info) diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 000000000..9215b275c --- /dev/null +++ b/poetry.lock @@ -0,0 +1,1048 @@ +# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand. + +[[package]] +name = "alabaster" +version = "0.7.16" +description = "A light, configurable Sphinx theme" +optional = false +python-versions = ">=3.9" +groups = ["docs"] +files = [ + {file = "alabaster-0.7.16-py3-none-any.whl", hash = "sha256:b46733c07dce03ae4e150330b975c75737fa60f0a7c591b6c8bf4928a28e2c92"}, + {file = "alabaster-0.7.16.tar.gz", hash = "sha256:75a8b99c28a5dad50dd7f8ccdd447a121ddb3892da9e53d1ca5cca3106d58d65"}, +] + +[[package]] +name = "babel" +version = "2.17.0" +description = "Internationalization utilities" +optional = false +python-versions = ">=3.8" +groups = ["docs"] +files = [ + {file = "babel-2.17.0-py3-none-any.whl", hash = "sha256:4d0b53093fdfb4b21c92b5213dba5a1b23885afa8383709427046b21c366e5f2"}, + {file = "babel-2.17.0.tar.gz", hash = "sha256:0c54cffb19f690cdcc52a3b50bcbf71e07a808d1c80d549f2459b9d2cf0afb9d"}, +] + +[package.extras] +dev = ["backports.zoneinfo ; python_version < \"3.9\"", "freezegun (>=1.0,<2.0)", "jinja2 (>=3.0)", "pytest (>=6.0)", "pytest-cov", "pytz", "setuptools", "tzdata ; sys_platform == \"win32\""] + +[[package]] +name = "backports-asyncio-runner" +version = "1.2.0" +description = "Backport of asyncio.Runner, a context manager that controls event loop life cycle." +optional = false +python-versions = "<3.11,>=3.8" +groups = ["dev"] +markers = "python_version == \"3.10\"" +files = [ + {file = "backports_asyncio_runner-1.2.0-py3-none-any.whl", hash = "sha256:0da0a936a8aeb554eccb426dc55af3ba63bcdc69fa1a600b5bb305413a4477b5"}, + {file = "backports_asyncio_runner-1.2.0.tar.gz", hash = "sha256:a5aa7b2b7d8f8bfcaa2b57313f70792df84e32a2a746f585213373f900b42162"}, +] + +[[package]] +name = "blockbuster" +version = "1.5.26" +description = "Utility to detect blocking calls in the async event loop" +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "blockbuster-1.5.26-py3-none-any.whl", hash = "sha256:f8e53fb2dd4b6c6ec2f04907ddbd063ca7cd1ef587d24448ef4e50e81e3a79bb"}, + {file = "blockbuster-1.5.26.tar.gz", hash = "sha256:cc3ce8c70fa852a97ee3411155f31e4ad2665cd1c6c7d2f8bb1851dab61dc629"}, +] + +[package.dependencies] +forbiddenfruit = {version = ">=0.1.4", markers = "implementation_name == \"cpython\""} + +[[package]] +name = "certifi" +version = "2025.1.31" +description = "Python package for providing Mozilla's CA Bundle." +optional = false +python-versions = ">=3.6" +groups = ["docs"] +files = [ + {file = "certifi-2025.1.31-py3-none-any.whl", hash = "sha256:ca78db4565a652026a4db2bcdf68f2fb589ea80d0be70e03929ed730746b84fe"}, + {file = "certifi-2025.1.31.tar.gz", hash = "sha256:3d5da6925056f6f18f119200434a4780a94263f10d1c21d032a6f6b2baa20651"}, +] + +[[package]] +name = "charset-normalizer" +version = "3.4.1" +description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet." +optional = false +python-versions = ">=3.7" +groups = ["docs"] +files = [ + {file = "charset_normalizer-3.4.1-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:91b36a978b5ae0ee86c394f5a54d6ef44db1de0815eb43de826d41d21e4af3de"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:7461baadb4dc00fd9e0acbe254e3d7d2112e7f92ced2adc96e54ef6501c5f176"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e218488cd232553829be0664c2292d3af2eeeb94b32bea483cf79ac6a694e037"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:80ed5e856eb7f30115aaf94e4a08114ccc8813e6ed1b5efa74f9f82e8509858f"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b010a7a4fd316c3c484d482922d13044979e78d1861f0e0650423144c616a46a"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:4532bff1b8421fd0a320463030c7520f56a79c9024a4e88f01c537316019005a"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:d973f03c0cb71c5ed99037b870f2be986c3c05e63622c017ea9816881d2dd247"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:3a3bd0dcd373514dcec91c411ddb9632c0d7d92aed7093b8c3bbb6d69ca74408"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-musllinux_1_2_ppc64le.whl", hash = "sha256:d9c3cdf5390dcd29aa8056d13e8e99526cda0305acc038b96b30352aff5ff2bb"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-musllinux_1_2_s390x.whl", hash = "sha256:2bdfe3ac2e1bbe5b59a1a63721eb3b95fc9b6817ae4a46debbb4e11f6232428d"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:eab677309cdb30d047996b36d34caeda1dc91149e4fdca0b1a039b3f79d9a807"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-win32.whl", hash = "sha256:c0429126cf75e16c4f0ad00ee0eae4242dc652290f940152ca8c75c3a4b6ee8f"}, + {file = "charset_normalizer-3.4.1-cp310-cp310-win_amd64.whl", hash = "sha256:9f0b8b1c6d84c8034a44893aba5e767bf9c7a211e313a9605d9c617d7083829f"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:8bfa33f4f2672964266e940dd22a195989ba31669bd84629f05fab3ef4e2d125"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:28bf57629c75e810b6ae989f03c0828d64d6b26a5e205535585f96093e405ed1"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f08ff5e948271dc7e18a35641d2f11a4cd8dfd5634f55228b691e62b37125eb3"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:234ac59ea147c59ee4da87a0c0f098e9c8d169f4dc2a159ef720f1a61bbe27cd"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:fd4ec41f914fa74ad1b8304bbc634b3de73d2a0889bd32076342a573e0779e00"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:eea6ee1db730b3483adf394ea72f808b6e18cf3cb6454b4d86e04fa8c4327a12"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:c96836c97b1238e9c9e3fe90844c947d5afbf4f4c92762679acfe19927d81d77"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:4d86f7aff21ee58f26dcf5ae81a9addbd914115cdebcbb2217e4f0ed8982e146"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-musllinux_1_2_ppc64le.whl", hash = "sha256:09b5e6733cbd160dcc09589227187e242a30a49ca5cefa5a7edd3f9d19ed53fd"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-musllinux_1_2_s390x.whl", hash = "sha256:5777ee0881f9499ed0f71cc82cf873d9a0ca8af166dfa0af8ec4e675b7df48e6"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:237bdbe6159cff53b4f24f397d43c6336c6b0b42affbe857970cefbb620911c8"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-win32.whl", hash = "sha256:8417cb1f36cc0bc7eaba8ccb0e04d55f0ee52df06df3ad55259b9a323555fc8b"}, + {file = "charset_normalizer-3.4.1-cp311-cp311-win_amd64.whl", hash = "sha256:d7f50a1f8c450f3925cb367d011448c39239bb3eb4117c36a6d354794de4ce76"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:73d94b58ec7fecbc7366247d3b0b10a21681004153238750bb67bd9012414545"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:dad3e487649f498dd991eeb901125411559b22e8d7ab25d3aeb1af367df5efd7"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c30197aa96e8eed02200a83fba2657b4c3acd0f0aa4bdc9f6c1af8e8962e0757"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:2369eea1ee4a7610a860d88f268eb39b95cb588acd7235e02fd5a5601773d4fa"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc2722592d8998c870fa4e290c2eec2c1569b87fe58618e67d38b4665dfa680d"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ffc9202a29ab3920fa812879e95a9e78b2465fd10be7fcbd042899695d75e616"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:804a4d582ba6e5b747c625bf1255e6b1507465494a40a2130978bda7b932c90b"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:0f55e69f030f7163dffe9fd0752b32f070566451afe180f99dbeeb81f511ad8d"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:c4c3e6da02df6fa1410a7680bd3f63d4f710232d3139089536310d027950696a"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:5df196eb874dae23dcfb968c83d4f8fdccb333330fe1fc278ac5ceeb101003a9"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:e358e64305fe12299a08e08978f51fc21fac060dcfcddd95453eabe5b93ed0e1"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-win32.whl", hash = "sha256:9b23ca7ef998bc739bf6ffc077c2116917eabcc901f88da1b9856b210ef63f35"}, + {file = "charset_normalizer-3.4.1-cp312-cp312-win_amd64.whl", hash = "sha256:6ff8a4a60c227ad87030d76e99cd1698345d4491638dfa6673027c48b3cd395f"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-macosx_10_13_universal2.whl", hash = "sha256:aabfa34badd18f1da5ec1bc2715cadc8dca465868a4e73a0173466b688f29dda"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:22e14b5d70560b8dd51ec22863f370d1e595ac3d024cb8ad7d308b4cd95f8313"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:8436c508b408b82d87dc5f62496973a1805cd46727c34440b0d29d8a2f50a6c9"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:2d074908e1aecee37a7635990b2c6d504cd4766c7bc9fc86d63f9c09af3fa11b"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:955f8851919303c92343d2f66165294848d57e9bba6cf6e3625485a70a038d11"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:44ecbf16649486d4aebafeaa7ec4c9fed8b88101f4dd612dcaf65d5e815f837f"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:0924e81d3d5e70f8126529951dac65c1010cdf117bb75eb02dd12339b57749dd"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:2967f74ad52c3b98de4c3b32e1a44e32975e008a9cd2a8cc8966d6a5218c5cb2"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:c75cb2a3e389853835e84a2d8fb2b81a10645b503eca9bcb98df6b5a43eb8886"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-musllinux_1_2_s390x.whl", hash = "sha256:09b26ae6b1abf0d27570633b2b078a2a20419c99d66fb2823173d73f188ce601"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:fa88b843d6e211393a37219e6a1c1df99d35e8fd90446f1118f4216e307e48cd"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-win32.whl", hash = "sha256:eb8178fe3dba6450a3e024e95ac49ed3400e506fd4e9e5c32d30adda88cbd407"}, + {file = "charset_normalizer-3.4.1-cp313-cp313-win_amd64.whl", hash = "sha256:b1ac5992a838106edb89654e0aebfc24f5848ae2547d22c2c3f66454daa11971"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f30bf9fd9be89ecb2360c7d94a711f00c09b976258846efe40db3d05828e8089"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:97f68b8d6831127e4787ad15e6757232e14e12060bec17091b85eb1486b91d8d"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:7974a0b5ecd505609e3b19742b60cee7aa2aa2fb3151bc917e6e2646d7667dcf"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:fc54db6c8593ef7d4b2a331b58653356cf04f67c960f584edb7c3d8c97e8f39e"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:311f30128d7d333eebd7896965bfcfbd0065f1716ec92bd5638d7748eb6f936a"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-musllinux_1_2_aarch64.whl", hash = "sha256:7d053096f67cd1241601111b698f5cad775f97ab25d81567d3f59219b5f1adbd"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-musllinux_1_2_i686.whl", hash = "sha256:807f52c1f798eef6cf26beb819eeb8819b1622ddfeef9d0977a8502d4db6d534"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-musllinux_1_2_ppc64le.whl", hash = "sha256:dccbe65bd2f7f7ec22c4ff99ed56faa1e9f785482b9bbd7c717e26fd723a1d1e"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-musllinux_1_2_s390x.whl", hash = "sha256:2fb9bd477fdea8684f78791a6de97a953c51831ee2981f8e4f583ff3b9d9687e"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-musllinux_1_2_x86_64.whl", hash = "sha256:01732659ba9b5b873fc117534143e4feefecf3b2078b0a6a2e925271bb6f4cfa"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-win32.whl", hash = "sha256:7a4f97a081603d2050bfaffdefa5b02a9ec823f8348a572e39032caa8404a487"}, + {file = "charset_normalizer-3.4.1-cp37-cp37m-win_amd64.whl", hash = "sha256:7b1bef6280950ee6c177b326508f86cad7ad4dff12454483b51d8b7d673a2c5d"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:ecddf25bee22fe4fe3737a399d0d177d72bc22be6913acfab364b40bce1ba83c"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:8c60ca7339acd497a55b0ea5d506b2a2612afb2826560416f6894e8b5770d4a9"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:b7b2d86dd06bfc2ade3312a83a5c364c7ec2e3498f8734282c6c3d4b07b346b8"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:dd78cfcda14a1ef52584dbb008f7ac81c1328c0f58184bf9a84c49c605002da6"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6e27f48bcd0957c6d4cb9d6fa6b61d192d0b13d5ef563e5f2ae35feafc0d179c"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:01ad647cdd609225c5350561d084b42ddf732f4eeefe6e678765636791e78b9a"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:619a609aa74ae43d90ed2e89bdd784765de0a25ca761b93e196d938b8fd1dbbd"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-musllinux_1_2_i686.whl", hash = "sha256:89149166622f4db9b4b6a449256291dc87a99ee53151c74cbd82a53c8c2f6ccd"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-musllinux_1_2_ppc64le.whl", hash = "sha256:7709f51f5f7c853f0fb938bcd3bc59cdfdc5203635ffd18bf354f6967ea0f824"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-musllinux_1_2_s390x.whl", hash = "sha256:345b0426edd4e18138d6528aed636de7a9ed169b4aaf9d61a8c19e39d26838ca"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:0907f11d019260cdc3f94fbdb23ff9125f6b5d1039b76003b5b0ac9d6a6c9d5b"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-win32.whl", hash = "sha256:ea0d8d539afa5eb2728aa1932a988a9a7af94f18582ffae4bc10b3fbdad0626e"}, + {file = "charset_normalizer-3.4.1-cp38-cp38-win_amd64.whl", hash = "sha256:329ce159e82018d646c7ac45b01a430369d526569ec08516081727a20e9e4af4"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:b97e690a2118911e39b4042088092771b4ae3fc3aa86518f84b8cf6888dbdb41"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:78baa6d91634dfb69ec52a463534bc0df05dbd546209b79a3880a34487f4b84f"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:1a2bc9f351a75ef49d664206d51f8e5ede9da246602dc2d2726837620ea034b2"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:75832c08354f595c760a804588b9357d34ec00ba1c940c15e31e96d902093770"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0af291f4fe114be0280cdd29d533696a77b5b49cfde5467176ecab32353395c4"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:0167ddc8ab6508fe81860a57dd472b2ef4060e8d378f0cc555707126830f2537"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:2a75d49014d118e4198bcee5ee0a6f25856b29b12dbf7cd012791f8a6cc5c496"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:363e2f92b0f0174b2f8238240a1a30142e3db7b957a5dd5689b0e75fb717cc78"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-musllinux_1_2_ppc64le.whl", hash = "sha256:ab36c8eb7e454e34e60eb55ca5d241a5d18b2c6244f6827a30e451c42410b5f7"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-musllinux_1_2_s390x.whl", hash = "sha256:4c0907b1928a36d5a998d72d64d8eaa7244989f7aaaf947500d3a800c83a3fd6"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:04432ad9479fa40ec0f387795ddad4437a2b50417c69fa275e212933519ff294"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-win32.whl", hash = "sha256:3bed14e9c89dcb10e8f3a29f9ccac4955aebe93c71ae803af79265c9ca5644c5"}, + {file = "charset_normalizer-3.4.1-cp39-cp39-win_amd64.whl", hash = "sha256:49402233c892a461407c512a19435d1ce275543138294f7ef013f0b63d5d3765"}, + {file = "charset_normalizer-3.4.1-py3-none-any.whl", hash = "sha256:d98b1668f06378c6dbefec3b92299716b931cd4e6061f3c875a71ced1780ab85"}, + {file = "charset_normalizer-3.4.1.tar.gz", hash = "sha256:44251f18cd68a75b56585dd00dae26183e102cd5e0f9f1466e6df5da2ed64ea3"}, +] + +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +groups = ["dev", "docs"] +markers = "sys_platform == \"win32\"" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "coverage" +version = "7.10.6" +description = "Code coverage measurement for Python" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "coverage-7.10.6-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:70e7bfbd57126b5554aa482691145f798d7df77489a177a6bef80de78860a356"}, + {file = "coverage-7.10.6-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:e41be6f0f19da64af13403e52f2dec38bbc2937af54df8ecef10850ff8d35301"}, + {file = "coverage-7.10.6-cp310-cp310-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:c61fc91ab80b23f5fddbee342d19662f3d3328173229caded831aa0bd7595460"}, + {file = "coverage-7.10.6-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:10356fdd33a7cc06e8051413140bbdc6f972137508a3572e3f59f805cd2832fd"}, + {file = "coverage-7.10.6-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:80b1695cf7c5ebe7b44bf2521221b9bb8cdf69b1f24231149a7e3eb1ae5fa2fb"}, + {file = "coverage-7.10.6-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:2e4c33e6378b9d52d3454bd08847a8651f4ed23ddbb4a0520227bd346382bbc6"}, + {file = "coverage-7.10.6-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:c8a3ec16e34ef980a46f60dc6ad86ec60f763c3f2fa0db6d261e6e754f72e945"}, + {file = "coverage-7.10.6-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:7d79dabc0a56f5af990cc6da9ad1e40766e82773c075f09cc571e2076fef882e"}, + {file = "coverage-7.10.6-cp310-cp310-win32.whl", hash = "sha256:86b9b59f2b16e981906e9d6383eb6446d5b46c278460ae2c36487667717eccf1"}, + {file = "coverage-7.10.6-cp310-cp310-win_amd64.whl", hash = "sha256:e132b9152749bd33534e5bd8565c7576f135f157b4029b975e15ee184325f528"}, + {file = "coverage-7.10.6-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:c706db3cabb7ceef779de68270150665e710b46d56372455cd741184f3868d8f"}, + {file = "coverage-7.10.6-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:8e0c38dc289e0508ef68ec95834cb5d2e96fdbe792eaccaa1bccac3966bbadcc"}, + {file = "coverage-7.10.6-cp311-cp311-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:752a3005a1ded28f2f3a6e8787e24f28d6abe176ca64677bcd8d53d6fe2ec08a"}, + {file = "coverage-7.10.6-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:689920ecfd60f992cafca4f5477d55720466ad2c7fa29bb56ac8d44a1ac2b47a"}, + {file = "coverage-7.10.6-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ec98435796d2624d6905820a42f82149ee9fc4f2d45c2c5bc5a44481cc50db62"}, + {file = "coverage-7.10.6-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:b37201ce4a458c7a758ecc4efa92fa8ed783c66e0fa3c42ae19fc454a0792153"}, + {file = "coverage-7.10.6-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:2904271c80898663c810a6b067920a61dd8d38341244a3605bd31ab55250dad5"}, + {file = "coverage-7.10.6-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:5aea98383463d6e1fa4e95416d8de66f2d0cb588774ee20ae1b28df826bcb619"}, + {file = "coverage-7.10.6-cp311-cp311-win32.whl", hash = "sha256:e3fb1fa01d3598002777dd259c0c2e6d9d5e10e7222976fc8e03992f972a2cba"}, + {file = "coverage-7.10.6-cp311-cp311-win_amd64.whl", hash = "sha256:f35ed9d945bece26553d5b4c8630453169672bea0050a564456eb88bdffd927e"}, + {file = "coverage-7.10.6-cp311-cp311-win_arm64.whl", hash = "sha256:99e1a305c7765631d74b98bf7dbf54eeea931f975e80f115437d23848ee8c27c"}, + {file = "coverage-7.10.6-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:5b2dd6059938063a2c9fee1af729d4f2af28fd1a545e9b7652861f0d752ebcea"}, + {file = "coverage-7.10.6-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:388d80e56191bf846c485c14ae2bc8898aa3124d9d35903fef7d907780477634"}, + {file = "coverage-7.10.6-cp312-cp312-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:90cb5b1a4670662719591aa92d0095bb41714970c0b065b02a2610172dbf0af6"}, + {file = "coverage-7.10.6-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:961834e2f2b863a0e14260a9a273aff07ff7818ab6e66d2addf5628590c628f9"}, + {file = "coverage-7.10.6-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:bf9a19f5012dab774628491659646335b1928cfc931bf8d97b0d5918dd58033c"}, + {file = "coverage-7.10.6-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:99c4283e2a0e147b9c9cc6bc9c96124de9419d6044837e9799763a0e29a7321a"}, + {file = "coverage-7.10.6-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:282b1b20f45df57cc508c1e033403f02283adfb67d4c9c35a90281d81e5c52c5"}, + {file = "coverage-7.10.6-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:8cdbe264f11afd69841bd8c0d83ca10b5b32853263ee62e6ac6a0ab63895f972"}, + {file = "coverage-7.10.6-cp312-cp312-win32.whl", hash = "sha256:a517feaf3a0a3eca1ee985d8373135cfdedfbba3882a5eab4362bda7c7cf518d"}, + {file = "coverage-7.10.6-cp312-cp312-win_amd64.whl", hash = "sha256:856986eadf41f52b214176d894a7de05331117f6035a28ac0016c0f63d887629"}, + {file = "coverage-7.10.6-cp312-cp312-win_arm64.whl", hash = "sha256:acf36b8268785aad739443fa2780c16260ee3fa09d12b3a70f772ef100939d80"}, + {file = "coverage-7.10.6-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:ffea0575345e9ee0144dfe5701aa17f3ba546f8c3bb48db62ae101afb740e7d6"}, + {file = "coverage-7.10.6-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:95d91d7317cde40a1c249d6b7382750b7e6d86fad9d8eaf4fa3f8f44cf171e80"}, + {file = "coverage-7.10.6-cp313-cp313-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:3e23dd5408fe71a356b41baa82892772a4cefcf758f2ca3383d2aa39e1b7a003"}, + {file = "coverage-7.10.6-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:0f3f56e4cb573755e96a16501a98bf211f100463d70275759e73f3cbc00d4f27"}, + {file = "coverage-7.10.6-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:db4a1d897bbbe7339946ffa2fe60c10cc81c43fab8b062d3fcb84188688174a4"}, + {file = "coverage-7.10.6-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:d8fd7879082953c156d5b13c74aa6cca37f6a6f4747b39538504c3f9c63d043d"}, + {file = "coverage-7.10.6-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:28395ca3f71cd103b8c116333fa9db867f3a3e1ad6a084aa3725ae002b6583bc"}, + {file = "coverage-7.10.6-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:61c950fc33d29c91b9e18540e1aed7d9f6787cc870a3e4032493bbbe641d12fc"}, + {file = "coverage-7.10.6-cp313-cp313-win32.whl", hash = "sha256:160c00a5e6b6bdf4e5984b0ef21fc860bc94416c41b7df4d63f536d17c38902e"}, + {file = "coverage-7.10.6-cp313-cp313-win_amd64.whl", hash = "sha256:628055297f3e2aa181464c3808402887643405573eb3d9de060d81531fa79d32"}, + {file = "coverage-7.10.6-cp313-cp313-win_arm64.whl", hash = "sha256:df4ec1f8540b0bcbe26ca7dd0f541847cc8a108b35596f9f91f59f0c060bfdd2"}, + {file = "coverage-7.10.6-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:c9a8b7a34a4de3ed987f636f71881cd3b8339f61118b1aa311fbda12741bff0b"}, + {file = "coverage-7.10.6-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:8dd5af36092430c2b075cee966719898f2ae87b636cefb85a653f1d0ba5d5393"}, + {file = "coverage-7.10.6-cp313-cp313t-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:b0353b0f0850d49ada66fdd7d0c7cdb0f86b900bb9e367024fd14a60cecc1e27"}, + {file = "coverage-7.10.6-cp313-cp313t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:d6b9ae13d5d3e8aeca9ca94198aa7b3ebbc5acfada557d724f2a1f03d2c0b0df"}, + {file = "coverage-7.10.6-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:675824a363cc05781b1527b39dc2587b8984965834a748177ee3c37b64ffeafb"}, + {file = "coverage-7.10.6-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:692d70ea725f471a547c305f0d0fc6a73480c62fb0da726370c088ab21aed282"}, + {file = "coverage-7.10.6-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:851430a9a361c7a8484a36126d1d0ff8d529d97385eacc8dfdc9bfc8c2d2cbe4"}, + {file = "coverage-7.10.6-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:d9369a23186d189b2fc95cc08b8160ba242057e887d766864f7adf3c46b2df21"}, + {file = "coverage-7.10.6-cp313-cp313t-win32.whl", hash = "sha256:92be86fcb125e9bda0da7806afd29a3fd33fdf58fba5d60318399adf40bf37d0"}, + {file = "coverage-7.10.6-cp313-cp313t-win_amd64.whl", hash = "sha256:6b3039e2ca459a70c79523d39347d83b73f2f06af5624905eba7ec34d64d80b5"}, + {file = "coverage-7.10.6-cp313-cp313t-win_arm64.whl", hash = "sha256:3fb99d0786fe17b228eab663d16bee2288e8724d26a199c29325aac4b0319b9b"}, + {file = "coverage-7.10.6-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:6008a021907be8c4c02f37cdc3ffb258493bdebfeaf9a839f9e71dfdc47b018e"}, + {file = "coverage-7.10.6-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:5e75e37f23eb144e78940b40395b42f2321951206a4f50e23cfd6e8a198d3ceb"}, + {file = "coverage-7.10.6-cp314-cp314-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:0f7cb359a448e043c576f0da00aa8bfd796a01b06aa610ca453d4dde09cc1034"}, + {file = "coverage-7.10.6-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:c68018e4fc4e14b5668f1353b41ccf4bc83ba355f0e1b3836861c6f042d89ac1"}, + {file = "coverage-7.10.6-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:cd4b2b0707fc55afa160cd5fc33b27ccbf75ca11d81f4ec9863d5793fc6df56a"}, + {file = "coverage-7.10.6-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:4cec13817a651f8804a86e4f79d815b3b28472c910e099e4d5a0e8a3b6a1d4cb"}, + {file = "coverage-7.10.6-cp314-cp314-musllinux_1_2_i686.whl", hash = "sha256:f2a6a8e06bbda06f78739f40bfb56c45d14eb8249d0f0ea6d4b3d48e1f7c695d"}, + {file = "coverage-7.10.6-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:081b98395ced0d9bcf60ada7661a0b75f36b78b9d7e39ea0790bb4ed8da14747"}, + {file = "coverage-7.10.6-cp314-cp314-win32.whl", hash = "sha256:6937347c5d7d069ee776b2bf4e1212f912a9f1f141a429c475e6089462fcecc5"}, + {file = "coverage-7.10.6-cp314-cp314-win_amd64.whl", hash = "sha256:adec1d980fa07e60b6ef865f9e5410ba760e4e1d26f60f7e5772c73b9a5b0713"}, + {file = "coverage-7.10.6-cp314-cp314-win_arm64.whl", hash = "sha256:a80f7aef9535442bdcf562e5a0d5a5538ce8abe6bb209cfbf170c462ac2c2a32"}, + {file = "coverage-7.10.6-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:0de434f4fbbe5af4fa7989521c655c8c779afb61c53ab561b64dcee6149e4c65"}, + {file = "coverage-7.10.6-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:6e31b8155150c57e5ac43ccd289d079eb3f825187d7c66e755a055d2c85794c6"}, + {file = "coverage-7.10.6-cp314-cp314t-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:98cede73eb83c31e2118ae8d379c12e3e42736903a8afcca92a7218e1f2903b0"}, + {file = "coverage-7.10.6-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:f863c08f4ff6b64fa8045b1e3da480f5374779ef187f07b82e0538c68cb4ff8e"}, + {file = "coverage-7.10.6-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:2b38261034fda87be356f2c3f42221fdb4171c3ce7658066ae449241485390d5"}, + {file = "coverage-7.10.6-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:0e93b1476b79eae849dc3872faeb0bf7948fd9ea34869590bc16a2a00b9c82a7"}, + {file = "coverage-7.10.6-cp314-cp314t-musllinux_1_2_i686.whl", hash = "sha256:ff8a991f70f4c0cf53088abf1e3886edcc87d53004c7bb94e78650b4d3dac3b5"}, + {file = "coverage-7.10.6-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:ac765b026c9f33044419cbba1da913cfb82cca1b60598ac1c7a5ed6aac4621a0"}, + {file = "coverage-7.10.6-cp314-cp314t-win32.whl", hash = "sha256:441c357d55f4936875636ef2cfb3bee36e466dcf50df9afbd398ce79dba1ebb7"}, + {file = "coverage-7.10.6-cp314-cp314t-win_amd64.whl", hash = "sha256:073711de3181b2e204e4870ac83a7c4853115b42e9cd4d145f2231e12d670930"}, + {file = "coverage-7.10.6-cp314-cp314t-win_arm64.whl", hash = "sha256:137921f2bac5559334ba66122b753db6dc5d1cf01eb7b64eb412bb0d064ef35b"}, + {file = "coverage-7.10.6-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:90558c35af64971d65fbd935c32010f9a2f52776103a259f1dee865fe8259352"}, + {file = "coverage-7.10.6-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:8953746d371e5695405806c46d705a3cd170b9cc2b9f93953ad838f6c1e58612"}, + {file = "coverage-7.10.6-cp39-cp39-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:c83f6afb480eae0313114297d29d7c295670a41c11b274e6bca0c64540c1ce7b"}, + {file = "coverage-7.10.6-cp39-cp39-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:7eb68d356ba0cc158ca535ce1381dbf2037fa8cb5b1ae5ddfc302e7317d04144"}, + {file = "coverage-7.10.6-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:5b15a87265e96307482746d86995f4bff282f14b027db75469c446da6127433b"}, + {file = "coverage-7.10.6-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:fc53ba868875bfbb66ee447d64d6413c2db91fddcfca57025a0e7ab5b07d5862"}, + {file = "coverage-7.10.6-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:efeda443000aa23f276f4df973cb82beca682fd800bb119d19e80504ffe53ec2"}, + {file = "coverage-7.10.6-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:9702b59d582ff1e184945d8b501ffdd08d2cee38d93a2206aa5f1365ce0b8d78"}, + {file = "coverage-7.10.6-cp39-cp39-win32.whl", hash = "sha256:2195f8e16ba1a44651ca684db2ea2b2d4b5345da12f07d9c22a395202a05b23c"}, + {file = "coverage-7.10.6-cp39-cp39-win_amd64.whl", hash = "sha256:f32ff80e7ef6a5b5b606ea69a36e97b219cd9dc799bcf2963018a4d8f788cfbf"}, + {file = "coverage-7.10.6-py3-none-any.whl", hash = "sha256:92c4ecf6bf11b2e85fd4d8204814dc26e6a19f0c9d938c207c5cb0eadfcabbe3"}, + {file = "coverage-7.10.6.tar.gz", hash = "sha256:f644a3ae5933a552a29dbb9aa2f90c677a875f80ebea028e5a52a4f429044b90"}, +] + +[package.dependencies] +tomli = {version = "*", optional = true, markers = "python_full_version <= \"3.11.0a6\" and extra == \"toml\""} + +[package.extras] +toml = ["tomli ; python_full_version <= \"3.11.0a6\""] + +[[package]] +name = "cython" +version = "3.2.4" +description = "The Cython compiler for writing C extensions in the Python language." +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "cython-3.2.4-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:02cb0cc0f23b9874ad262d7d2b9560aed9c7e2df07b49b920bda6f2cc9cb505e"}, + {file = "cython-3.2.4-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:f136f379a4a54246facd0eb6f1ee15c3837cb314ce87b677582ec014db4c6845"}, + {file = "cython-3.2.4-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:35ab0632186057406ec729374c737c37051d2eacad9d515d94e5a3b3e58a9b02"}, + {file = "cython-3.2.4-cp310-cp310-win_amd64.whl", hash = "sha256:ca2399dc75796b785f74fb85c938254fa10c80272004d573c455f9123eceed86"}, + {file = "cython-3.2.4-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:ff9af2134c05e3734064808db95b4dd7341a39af06e8945d05ea358e1741aaed"}, + {file = "cython-3.2.4-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:67922c9de058a0bfb72d2e75222c52d09395614108c68a76d9800f150296ddb3"}, + {file = "cython-3.2.4-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b362819d155fff1482575e804e43e3a8825332d32baa15245f4642022664a3f4"}, + {file = "cython-3.2.4-cp311-cp311-win_amd64.whl", hash = "sha256:1a64a112a34ec719b47c01395647e54fb4cf088a511613f9a3a5196694e8e382"}, + {file = "cython-3.2.4-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:64d7f71be3dd6d6d4a4c575bb3a4674ea06d1e1e5e4cd1b9882a2bc40ed3c4c9"}, + {file = "cython-3.2.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:869487ea41d004f8b92171f42271fbfadb1ec03bede3158705d16cd570d6b891"}, + {file = "cython-3.2.4-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:55b6c44cd30821f0b25220ceba6fe636ede48981d2a41b9bbfe3c7902ce44ea7"}, + {file = "cython-3.2.4-cp312-cp312-win_amd64.whl", hash = "sha256:767b143704bdd08a563153448955935844e53b852e54afdc552b43902ed1e235"}, + {file = "cython-3.2.4-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:28e8075087a59756f2d059273184b8b639fe0f16cf17470bd91c39921bc154e0"}, + {file = "cython-3.2.4-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:03893c88299a2c868bb741ba6513357acd104e7c42265809fd58dce1456a36fc"}, + {file = "cython-3.2.4-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:f81eda419b5ada7b197bbc3c5f4494090e3884521ffd75a3876c93fbf66c9ca8"}, + {file = "cython-3.2.4-cp313-cp313-win_amd64.whl", hash = "sha256:83266c356c13c68ffe658b4905279c993d8a5337bb0160fa90c8a3e297ea9a2e"}, + {file = "cython-3.2.4-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:d4b4fd5332ab093131fa6172e8362f16adef3eac3179fd24bbdc392531cb82fa"}, + {file = "cython-3.2.4-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:e3b5ac54e95f034bc7fb07313996d27cbf71abc17b229b186c1540942d2dc28e"}, + {file = "cython-3.2.4-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:90f43be4eaa6afd58ce20d970bb1657a3627c44e1760630b82aa256ba74b4acb"}, + {file = "cython-3.2.4-cp314-cp314-win_amd64.whl", hash = "sha256:983f9d2bb8a896e16fa68f2b37866ded35fa980195eefe62f764ddc5f9f5ef8e"}, + {file = "cython-3.2.4-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:55eb425c0baf1c8a46aa4424bc35b709db22f3c8a1de33adb3ecb8a3d54ea42a"}, + {file = "cython-3.2.4-cp38-cp38-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:f583cad7a7eed109f0babb5035e92d0c1260598f53add626a8568b57246b62c3"}, + {file = "cython-3.2.4-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:72e6c0bbd978e2678b45351395f6825b9b8466095402eae293f4f7a73e9a3e85"}, + {file = "cython-3.2.4-cp38-cp38-win_amd64.whl", hash = "sha256:14dae483ca2838b287085ff98bc206abd7a597b7bb16939a092f8e84d9062842"}, + {file = "cython-3.2.4-cp39-abi3-macosx_10_9_x86_64.whl", hash = "sha256:36bf3f5eb56d5281aafabecbaa6ed288bc11db87547bba4e1e52943ae6961ccf"}, + {file = "cython-3.2.4-cp39-abi3-manylinux1_i686.manylinux_2_28_i686.manylinux_2_5_i686.whl", hash = "sha256:6d5267f22b6451eb1e2e1b88f6f78a2c9c8733a6ddefd4520d3968d26b824581"}, + {file = "cython-3.2.4-cp39-abi3-manylinux2014_armv7l.manylinux_2_17_armv7l.manylinux_2_31_armv7l.whl", hash = "sha256:3b6e58f73a69230218d5381817850ce6d0da5bb7e87eb7d528c7027cbba40b06"}, + {file = "cython-3.2.4-cp39-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:e71efb20048358a6b8ec604a0532961c50c067b5e63e345e2e359fff72feaee8"}, + {file = "cython-3.2.4-cp39-abi3-musllinux_1_2_armv7l.whl", hash = "sha256:28b1e363b024c4b8dcf52ff68125e635cb9cb4b0ba997d628f25e32543a71103"}, + {file = "cython-3.2.4-cp39-abi3-musllinux_1_2_i686.whl", hash = "sha256:31a90b4a2c47bb6d56baeb926948348ec968e932c1ae2c53239164e3e8880ccf"}, + {file = "cython-3.2.4-cp39-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:e65e4773021f8dc8532010b4fbebe782c77f9a0817e93886e518c93bd6a44e9d"}, + {file = "cython-3.2.4-cp39-abi3-win32.whl", hash = "sha256:2b1f12c0e4798293d2754e73cd6f35fa5bbdf072bdc14bc6fc442c059ef2d290"}, + {file = "cython-3.2.4-cp39-abi3-win_arm64.whl", hash = "sha256:3b8e62049afef9da931d55de82d8f46c9a147313b69d5ff6af6e9121d545ce7a"}, + {file = "cython-3.2.4-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:f8d685a70bce39acc1d62ec3916d9b724b5ef665b0ce25ae55e1c85ee09747fc"}, + {file = "cython-3.2.4-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ca578c9cb872c7ecffbe14815dc4590a003bc13339e90b2633540c7e1a252839"}, + {file = "cython-3.2.4-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b84d4e3c875915545f77c88dba65ad3741afd2431e5cdee6c9a20cefe6905647"}, + {file = "cython-3.2.4-cp39-cp39-win_amd64.whl", hash = "sha256:fdfdd753ad7e18e5092b413e9f542e8d28b8a08203126090e1c15f7783b7fe57"}, + {file = "cython-3.2.4-py3-none-any.whl", hash = "sha256:732fc93bc33ae4b14f6afaca663b916c2fdd5dcbfad7114e17fb2434eeaea45c"}, + {file = "cython-3.2.4.tar.gz", hash = "sha256:84226ecd313b233da27dc2eb3601b4f222b8209c3a7216d8733b031da1dc64e6"}, +] + +[[package]] +name = "docutils" +version = "0.21.2" +description = "Docutils -- Python Documentation Utilities" +optional = false +python-versions = ">=3.9" +groups = ["docs"] +files = [ + {file = "docutils-0.21.2-py3-none-any.whl", hash = "sha256:dafca5b9e384f0e419294eb4d2ff9fa826435bf15f15b7bd45723e8ad76811b2"}, + {file = "docutils-0.21.2.tar.gz", hash = "sha256:3a6b18732edf182daa3cd12775bbb338cf5691468f91eeeb109deff6ebfa986f"}, +] + +[[package]] +name = "exceptiongroup" +version = "1.2.2" +description = "Backport of PEP 654 (exception groups)" +optional = false +python-versions = ">=3.7" +groups = ["dev"] +markers = "python_version == \"3.10\"" +files = [ + {file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"}, + {file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"}, +] + +[package.extras] +test = ["pytest (>=6)"] + +[[package]] +name = "forbiddenfruit" +version = "0.1.4" +description = "Patch python built-in objects" +optional = false +python-versions = "*" +groups = ["dev"] +markers = "implementation_name == \"cpython\"" +files = [ + {file = "forbiddenfruit-0.1.4.tar.gz", hash = "sha256:e3f7e66561a29ae129aac139a85d610dbf3dd896128187ed5454b6421f624253"}, +] + +[[package]] +name = "idna" +version = "3.15" +description = "Internationalized Domain Names in Applications (IDNA)" +optional = false +python-versions = ">=3.8" +groups = ["docs"] +files = [ + {file = "idna-3.15-py3-none-any.whl", hash = "sha256:048adeaf8c2d788c40fee287673ccaa74c24ffd8dcf09ffa555a2fbb59f10ac8"}, + {file = "idna-3.15.tar.gz", hash = "sha256:ca962446ea538f7092a95e057da437618e886f4d349216d2b1e294abfdb65fdc"}, +] + +[package.extras] +all = ["mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"] + +[[package]] +name = "ifaddr" +version = "0.2.0" +description = "Cross-platform network interface and IP address enumeration library" +optional = false +python-versions = "*" +groups = ["main"] +files = [ + {file = "ifaddr-0.2.0-py3-none-any.whl", hash = "sha256:085e0305cfe6f16ab12d72e2024030f5d52674afad6911bb1eee207177b8a748"}, + {file = "ifaddr-0.2.0.tar.gz", hash = "sha256:cc0cbfcaabf765d44595825fb96a99bb12c79716b73b44330ea38ee2b0c4aed4"}, +] + +[[package]] +name = "imagesize" +version = "1.4.1" +description = "Getting image size from png/jpeg/jpeg2000/gif file" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +groups = ["docs"] +files = [ + {file = "imagesize-1.4.1-py2.py3-none-any.whl", hash = "sha256:0d8d18d08f840c19d0ee7ca1fd82490fdc3729b7ac93f49870406ddde8ef8d8b"}, + {file = "imagesize-1.4.1.tar.gz", hash = "sha256:69150444affb9cb0d5cc5a92b3676f0b2fb7cd9ae39e947a5e11a36b4497cd4a"}, +] + +[[package]] +name = "iniconfig" +version = "2.0.0" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.7" +groups = ["dev"] +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + +[[package]] +name = "jinja2" +version = "3.1.6" +description = "A very fast and expressive template engine." +optional = false +python-versions = ">=3.7" +groups = ["docs"] +files = [ + {file = "jinja2-3.1.6-py3-none-any.whl", hash = "sha256:85ece4451f492d0c13c5dd7c13a64681a86afae63a5f347908daf103ce6d2f67"}, + {file = "jinja2-3.1.6.tar.gz", hash = "sha256:0137fb05990d35f1275a587e9aee6d56da821fc83491a0fb838183be43f66d6d"}, +] + +[package.dependencies] +MarkupSafe = ">=2.0" + +[package.extras] +i18n = ["Babel (>=2.7)"] + +[[package]] +name = "markdown-it-py" +version = "3.0.0" +description = "Python port of markdown-it. Markdown parsing, done right!" +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "markdown-it-py-3.0.0.tar.gz", hash = "sha256:e3f60a94fa066dc52ec76661e37c851cb232d92f9886b15cb560aaada2df8feb"}, + {file = "markdown_it_py-3.0.0-py3-none-any.whl", hash = "sha256:355216845c60bd96232cd8d8c40e8f9765cc86f46880e43a8fd22dc1a1a8cab1"}, +] + +[package.dependencies] +mdurl = ">=0.1,<1.0" + +[package.extras] +benchmarking = ["psutil", "pytest", "pytest-benchmark"] +code-style = ["pre-commit (>=3.0,<4.0)"] +compare = ["commonmark (>=0.9,<1.0)", "markdown (>=3.4,<4.0)", "mistletoe (>=1.0,<2.0)", "mistune (>=2.0,<3.0)", "panflute (>=2.3,<3.0)"] +linkify = ["linkify-it-py (>=1,<3)"] +plugins = ["mdit-py-plugins"] +profiling = ["gprof2dot"] +rtd = ["jupyter_sphinx", "mdit-py-plugins", "myst-parser", "pyyaml", "sphinx", "sphinx-copybutton", "sphinx-design", "sphinx_book_theme"] +testing = ["coverage", "pytest", "pytest-cov", "pytest-regressions"] + +[[package]] +name = "markupsafe" +version = "3.0.2" +description = "Safely add untrusted strings to HTML/XML markup." +optional = false +python-versions = ">=3.9" +groups = ["docs"] +files = [ + {file = "MarkupSafe-3.0.2-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:7e94c425039cde14257288fd61dcfb01963e658efbc0ff54f5306b06054700f8"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:9e2d922824181480953426608b81967de705c3cef4d1af983af849d7bd619158"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:38a9ef736c01fccdd6600705b09dc574584b89bea478200c5fbf112a6b0d5579"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bbcb445fa71794da8f178f0f6d66789a28d7319071af7a496d4d507ed566270d"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:57cb5a3cf367aeb1d316576250f65edec5bb3be939e9247ae594b4bcbc317dfb"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:3809ede931876f5b2ec92eef964286840ed3540dadf803dd570c3b7e13141a3b"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:e07c3764494e3776c602c1e78e298937c3315ccc9043ead7e685b7f2b8d47b3c"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:b424c77b206d63d500bcb69fa55ed8d0e6a3774056bdc4839fc9298a7edca171"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-win32.whl", hash = "sha256:fcabf5ff6eea076f859677f5f0b6b5c1a51e70a376b0579e0eadef8db48c6b50"}, + {file = "MarkupSafe-3.0.2-cp310-cp310-win_amd64.whl", hash = "sha256:6af100e168aa82a50e186c82875a5893c5597a0c1ccdb0d8b40240b1f28b969a"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:9025b4018f3a1314059769c7bf15441064b2207cb3f065e6ea1e7359cb46db9d"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:93335ca3812df2f366e80509ae119189886b0f3c2b81325d39efdb84a1e2ae93"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2cb8438c3cbb25e220c2ab33bb226559e7afb3baec11c4f218ffa7308603c832"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a123e330ef0853c6e822384873bef7507557d8e4a082961e1defa947aa59ba84"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1e084f686b92e5b83186b07e8a17fc09e38fff551f3602b249881fec658d3eca"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:d8213e09c917a951de9d09ecee036d5c7d36cb6cb7dbaece4c71a60d79fb9798"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:5b02fb34468b6aaa40dfc198d813a641e3a63b98c2b05a16b9f80b7ec314185e"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:0bff5e0ae4ef2e1ae4fdf2dfd5b76c75e5c2fa4132d05fc1b0dabcd20c7e28c4"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-win32.whl", hash = "sha256:6c89876f41da747c8d3677a2b540fb32ef5715f97b66eeb0c6b66f5e3ef6f59d"}, + {file = "MarkupSafe-3.0.2-cp311-cp311-win_amd64.whl", hash = "sha256:70a87b411535ccad5ef2f1df5136506a10775d267e197e4cf531ced10537bd6b"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:9778bd8ab0a994ebf6f84c2b949e65736d5575320a17ae8984a77fab08db94cf"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:846ade7b71e3536c4e56b386c2a47adf5741d2d8b94ec9dc3e92e5e1ee1e2225"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1c99d261bd2d5f6b59325c92c73df481e05e57f19837bdca8413b9eac4bd8028"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e17c96c14e19278594aa4841ec148115f9c7615a47382ecb6b82bd8fea3ab0c8"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:88416bd1e65dcea10bc7569faacb2c20ce071dd1f87539ca2ab364bf6231393c"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:2181e67807fc2fa785d0592dc2d6206c019b9502410671cc905d132a92866557"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:52305740fe773d09cffb16f8ed0427942901f00adedac82ec8b67752f58a1b22"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:ad10d3ded218f1039f11a75f8091880239651b52e9bb592ca27de44eed242a48"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-win32.whl", hash = "sha256:0f4ca02bea9a23221c0182836703cbf8930c5e9454bacce27e767509fa286a30"}, + {file = "MarkupSafe-3.0.2-cp312-cp312-win_amd64.whl", hash = "sha256:8e06879fc22a25ca47312fbe7c8264eb0b662f6db27cb2d3bbbc74b1df4b9b87"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-macosx_10_13_universal2.whl", hash = "sha256:ba9527cdd4c926ed0760bc301f6728ef34d841f405abf9d4f959c478421e4efd"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:f8b3d067f2e40fe93e1ccdd6b2e1d16c43140e76f02fb1319a05cf2b79d99430"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:569511d3b58c8791ab4c2e1285575265991e6d8f8700c7be0e88f86cb0672094"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:15ab75ef81add55874e7ab7055e9c397312385bd9ced94920f2802310c930396"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:f3818cb119498c0678015754eba762e0d61e5b52d34c8b13d770f0719f7b1d79"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:cdb82a876c47801bb54a690c5ae105a46b392ac6099881cdfb9f6e95e4014c6a"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:cabc348d87e913db6ab4aa100f01b08f481097838bdddf7c7a84b7575b7309ca"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:444dcda765c8a838eaae23112db52f1efaf750daddb2d9ca300bcae1039adc5c"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-win32.whl", hash = "sha256:bcf3e58998965654fdaff38e58584d8937aa3096ab5354d493c77d1fdd66d7a1"}, + {file = "MarkupSafe-3.0.2-cp313-cp313-win_amd64.whl", hash = "sha256:e6a2a455bd412959b57a172ce6328d2dd1f01cb2135efda2e4576e8a23fa3b0f"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-macosx_10_13_universal2.whl", hash = "sha256:b5a6b3ada725cea8a5e634536b1b01c30bcdcd7f9c6fff4151548d5bf6b3a36c"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:a904af0a6162c73e3edcb969eeeb53a63ceeb5d8cf642fade7d39e7963a22ddb"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4aa4e5faecf353ed117801a068ebab7b7e09ffb6e1d5e412dc852e0da018126c"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c0ef13eaeee5b615fb07c9a7dadb38eac06a0608b41570d8ade51c56539e509d"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d16a81a06776313e817c951135cf7340a3e91e8c1ff2fac444cfd75fffa04afe"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:6381026f158fdb7c72a168278597a5e3a5222e83ea18f543112b2662a9b699c5"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_i686.whl", hash = "sha256:3d79d162e7be8f996986c064d1c7c817f6df3a77fe3d6859f6f9e7be4b8c213a"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:131a3c7689c85f5ad20f9f6fb1b866f402c445b220c19fe4308c0b147ccd2ad9"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-win32.whl", hash = "sha256:ba8062ed2cf21c07a9e295d5b8a2a5ce678b913b45fdf68c32d95d6c1291e0b6"}, + {file = "MarkupSafe-3.0.2-cp313-cp313t-win_amd64.whl", hash = "sha256:e444a31f8db13eb18ada366ab3cf45fd4b31e4db1236a4448f68778c1d1a5a2f"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:eaa0a10b7f72326f1372a713e73c3f739b524b3af41feb43e4921cb529f5929a"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:48032821bbdf20f5799ff537c7ac3d1fba0ba032cfc06194faffa8cda8b560ff"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1a9d3f5f0901fdec14d8d2f66ef7d035f2157240a433441719ac9a3fba440b13"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:88b49a3b9ff31e19998750c38e030fc7bb937398b1f78cfa599aaef92d693144"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:cfad01eed2c2e0c01fd0ecd2ef42c492f7f93902e39a42fc9ee1692961443a29"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:1225beacc926f536dc82e45f8a4d68502949dc67eea90eab715dea3a21c1b5f0"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:3169b1eefae027567d1ce6ee7cae382c57fe26e82775f460f0b2778beaad66c0"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:eb7972a85c54febfb25b5c4b4f3af4dcc731994c7da0d8a0b4a6eb0640e1d178"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-win32.whl", hash = "sha256:8c4e8c3ce11e1f92f6536ff07154f9d49677ebaaafc32db9db4620bc11ed480f"}, + {file = "MarkupSafe-3.0.2-cp39-cp39-win_amd64.whl", hash = "sha256:6e296a513ca3d94054c2c881cc913116e90fd030ad1c656b3869762b754f5f8a"}, + {file = "markupsafe-3.0.2.tar.gz", hash = "sha256:ee55d3edf80167e48ea11a923c7386f4669df67d7994554387f84e7d8b0a2bf0"}, +] + +[[package]] +name = "mdurl" +version = "0.1.2" +description = "Markdown URL utilities" +optional = false +python-versions = ">=3.7" +groups = ["dev"] +files = [ + {file = "mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8"}, + {file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"}, +] + +[[package]] +name = "packaging" +version = "24.2" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.8" +groups = ["dev", "docs"] +files = [ + {file = "packaging-24.2-py3-none-any.whl", hash = "sha256:09abb1bccd265c01f4a3aa3f7a7db064b36514d2cba19a2f694fe6150451a759"}, + {file = "packaging-24.2.tar.gz", hash = "sha256:c228a6dc5e932d346bc5739379109d49e8853dd8223571c7c5b55260edc0b97f"}, +] + +[[package]] +name = "pluggy" +version = "1.5.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.8" +groups = ["dev"] +files = [ + {file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"}, + {file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"}, +] + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["pytest", "pytest-benchmark"] + +[[package]] +name = "pygments" +version = "2.19.1" +description = "Pygments is a syntax highlighting package written in Python." +optional = false +python-versions = ">=3.8" +groups = ["dev", "docs"] +files = [ + {file = "pygments-2.19.1-py3-none-any.whl", hash = "sha256:9ea1544ad55cecf4b8242fab6dd35a93bbce657034b0611ee383099054ab6d8c"}, + {file = "pygments-2.19.1.tar.gz", hash = "sha256:61c16d2a8576dc0649d9f39e089b5f02bcd27fba10d8fb4dcc28173f7a45151f"}, +] + +[package.extras] +windows-terminal = ["colorama (>=0.4.6)"] + +[[package]] +name = "pytest" +version = "9.0.3" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "pytest-9.0.3-py3-none-any.whl", hash = "sha256:2c5efc453d45394fdd706ade797c0a81091eccd1d6e4bccfcd476e2b8e0ab5d9"}, + {file = "pytest-9.0.3.tar.gz", hash = "sha256:b86ada508af81d19edeb213c681b1d48246c1a91d304c6c81a427674c17eb91c"}, +] + +[package.dependencies] +colorama = {version = ">=0.4", markers = "sys_platform == \"win32\""} +exceptiongroup = {version = ">=1", markers = "python_version < \"3.11\""} +iniconfig = ">=1.0.1" +packaging = ">=22" +pluggy = ">=1.5,<2" +pygments = ">=2.7.2" +tomli = {version = ">=1", markers = "python_version < \"3.11\""} + +[package.extras] +dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "requests", "setuptools", "xmlschema"] + +[[package]] +name = "pytest-asyncio" +version = "1.3.0" +description = "Pytest support for asyncio" +optional = false +python-versions = ">=3.10" +groups = ["dev"] +files = [ + {file = "pytest_asyncio-1.3.0-py3-none-any.whl", hash = "sha256:611e26147c7f77640e6d0a92a38ed17c3e9848063698d5c93d5aa7aa11cebff5"}, + {file = "pytest_asyncio-1.3.0.tar.gz", hash = "sha256:d7f52f36d231b80ee124cd216ffb19369aa168fc10095013c6b014a34d3ee9e5"}, +] + +[package.dependencies] +backports-asyncio-runner = {version = ">=1.1,<2", markers = "python_version < \"3.11\""} +pytest = ">=8.2,<10" +typing-extensions = {version = ">=4.12", markers = "python_version < \"3.13\""} + +[package.extras] +docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1)"] +testing = ["coverage (>=6.2)", "hypothesis (>=5.7.1)"] + +[[package]] +name = "pytest-codspeed" +version = "5.0.2" +description = "Pytest plugin to create CodSpeed benchmarks" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "pytest_codspeed-5.0.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:fbd1e86900e7ebbbf3cdf5a48124412d2b75283ab1378994ac27ba3308e262fc"}, + {file = "pytest_codspeed-5.0.2-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:d394d0d27ead72d0b00906e3832f4dcb9aadb81887a4f379c534c32c0ab965b7"}, + {file = "pytest_codspeed-5.0.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1ee33ac4c3bd7317b6956c0b6cb250f759e02072bb14fd0324de0df71d5d488f"}, + {file = "pytest_codspeed-5.0.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:3799ca9e54d6958d1b388371d00f928fcc4e1e68427d312348dd413a1bba5e0b"}, + {file = "pytest_codspeed-5.0.2-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:b42d2aae3ac94192b8843fa7578eae584223bcb6334c50ca9f0e9ebafd40053b"}, + {file = "pytest_codspeed-5.0.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:793d423dc76fd52b67495318681be18c541a7cfe30432ab2f272cd393422c56b"}, + {file = "pytest_codspeed-5.0.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:c20756925af58ad9d5b584d66a9b8dc709f9b243e6d8fd377e2a1b5a99bf9229"}, + {file = "pytest_codspeed-5.0.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:d6d24532a8fee7018b9a33df51e1a14e27ae6b2b0772e6ad477ce5c561ab06a5"}, + {file = "pytest_codspeed-5.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c2c09ec82a2def144816c6ffb311252c6ff0624189b3b5e674d889920b6d926c"}, + {file = "pytest_codspeed-5.0.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:d223b0fe74625e633c86934a1da3ed1607f694fb3981a598bcfc02811e54808e"}, + {file = "pytest_codspeed-5.0.2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:82d3c9db57ccaef5177e1096b4dbbf8f3fde8d25c568e38d31a259474c94e5b4"}, + {file = "pytest_codspeed-5.0.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0621a458c52e77aa113c8d6e14037b90ce3cb5a8dd10a7656b71641999baef8c"}, + {file = "pytest_codspeed-5.0.2-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:1b87b6a5e3c0e05ea043790aae08791dd6b3e7f487b18ec1bce145a60c78a130"}, + {file = "pytest_codspeed-5.0.2-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:22332fefae895fc80a36ac8a6d5b314663efcad9e833aed8452388441b95c50f"}, + {file = "pytest_codspeed-5.0.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:dedc9e4542832a3487aedf0b448217f34fdc794676b9e0daeaf408a343322c2b"}, + {file = "pytest_codspeed-5.0.2-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:0fd7db3e6fb6bd28abbf0059dd54ee6233f5faf5c08597b1e9624821417e8d99"}, + {file = "pytest_codspeed-5.0.2-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3a5ce30d2bcfbeb329b61f3435369720ed122caa1dd898464acbcd7edc63cf04"}, + {file = "pytest_codspeed-5.0.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:687e5aa0fd101adbfe98f36dc253cd4e3b77d90ad96260e6e7e78bde4319c357"}, + {file = "pytest_codspeed-5.0.2-cp315-cp315-macosx_11_0_arm64.whl", hash = "sha256:a14a6515cd315745b4b5b4739a72b287782c00a35f2927e55c310499b79d6bc2"}, + {file = "pytest_codspeed-5.0.2-cp315-cp315-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3658d3b42a15c6f40fa385629a8a8655dbedadd5d7bb5a01bc342b47f73da252"}, + {file = "pytest_codspeed-5.0.2-cp315-cp315-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:3d15eaa6ca380d0d7cb5b7b8692f362a8aac3832dff6867a0c7068fb8c7a4ef1"}, + {file = "pytest_codspeed-5.0.2-cp315-cp315t-macosx_11_0_arm64.whl", hash = "sha256:53473907ee2a7569b5ce6ffbfd2ba1793d284a37ff5c8670ed3149133c3ed37b"}, + {file = "pytest_codspeed-5.0.2-cp315-cp315t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:7b033d25f40c47733234f29c10629f14d004540c743a5c30718e2aa768d7cbb3"}, + {file = "pytest_codspeed-5.0.2-cp315-cp315t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:33245c1fd96b1a4299604f6791e7fded376605c140ad778db7032dcd46a74d1c"}, + {file = "pytest_codspeed-5.0.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:40b12cbf88eb69583d7063a4f5c986a7eed14f750a49764ef39a565ffa33d540"}, + {file = "pytest_codspeed-5.0.2-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:439cd9d87ad449b7db327724b8fdc4a1ae79090b166b77c4e5e15102a371f6c7"}, + {file = "pytest_codspeed-5.0.2-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:cd07e12c38f6974c969e76d070aba448c92fad66601cda4fd289afa52c81ef13"}, + {file = "pytest_codspeed-5.0.2-py3-none-any.whl", hash = "sha256:a88fcddd08bdb1afe043ac4f992e032baee92c88990a611111e0c00d77927cfe"}, + {file = "pytest_codspeed-5.0.2.tar.gz", hash = "sha256:93fea30b2d7266343dd505a182bdf1eb47f96f5fa2929f1d9aff01d3b60e1589"}, +] + +[package.dependencies] +pytest = ">=3.8" +rich = ">=13.8.1" + +[package.extras] +compat = ["pytest-benchmark (>=5.0.0,<5.1.0)", "pytest-xdist (>=3.6.1,<3.7.0)"] + +[[package]] +name = "pytest-cov" +version = "7.1.0" +description = "Pytest plugin for measuring coverage." +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "pytest_cov-7.1.0-py3-none-any.whl", hash = "sha256:a0461110b7865f9a271aa1b51e516c9a95de9d696734a2f71e3e78f46e1d4678"}, + {file = "pytest_cov-7.1.0.tar.gz", hash = "sha256:30674f2b5f6351aa09702a9c8c364f6a01c27aae0c1366ae8016160d1efc56b2"}, +] + +[package.dependencies] +coverage = {version = ">=7.10.6", extras = ["toml"]} +pluggy = ">=1.2" +pytest = ">=7" + +[package.extras] +testing = ["process-tests", "pytest-xdist", "virtualenv"] + +[[package]] +name = "pytest-timeout" +version = "2.4.0" +description = "pytest plugin to abort hanging tests" +optional = false +python-versions = ">=3.7" +groups = ["dev"] +files = [ + {file = "pytest_timeout-2.4.0-py3-none-any.whl", hash = "sha256:c42667e5cdadb151aeb5b26d114aff6bdf5a907f176a007a30b940d3d865b5c2"}, + {file = "pytest_timeout-2.4.0.tar.gz", hash = "sha256:7e68e90b01f9eff71332b25001f85c75495fc4e3a836701876183c4bcfd0540a"}, +] + +[package.dependencies] +pytest = ">=7.0.0" + +[[package]] +name = "requests" +version = "2.33.0" +description = "Python HTTP for Humans." +optional = false +python-versions = ">=3.10" +groups = ["docs"] +files = [ + {file = "requests-2.33.0-py3-none-any.whl", hash = "sha256:3324635456fa185245e24865e810cecec7b4caf933d7eb133dcde67d48cee69b"}, + {file = "requests-2.33.0.tar.gz", hash = "sha256:c7ebc5e8b0f21837386ad0e1c8fe8b829fa5f544d8df3b2253bff14ef29d7652"}, +] + +[package.dependencies] +certifi = ">=2023.5.7" +charset_normalizer = ">=2,<4" +idna = ">=2.5,<4" +urllib3 = ">=1.26,<3" + +[package.extras] +socks = ["PySocks (>=1.5.6,!=1.5.7)"] +test = ["PySocks (>=1.5.6,!=1.5.7)", "pytest (>=3)", "pytest-cov", "pytest-httpbin (==2.1.0)", "pytest-mock", "pytest-xdist"] +use-chardet-on-py3 = ["chardet (>=3.0.2,<8)"] + +[[package]] +name = "rich" +version = "13.9.4" +description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" +optional = false +python-versions = ">=3.8.0" +groups = ["dev"] +files = [ + {file = "rich-13.9.4-py3-none-any.whl", hash = "sha256:6049d5e6ec054bf2779ab3358186963bac2ea89175919d699e378b99738c2a90"}, + {file = "rich-13.9.4.tar.gz", hash = "sha256:439594978a49a09530cff7ebc4b5c7103ef57baf48d5ea3184f21d9a2befa098"}, +] + +[package.dependencies] +markdown-it-py = ">=2.2.0" +pygments = ">=2.13.0,<3.0.0" +typing-extensions = {version = ">=4.0.0,<5.0", markers = "python_version < \"3.11\""} + +[package.extras] +jupyter = ["ipywidgets (>=7.5.1,<9)"] + +[[package]] +name = "setuptools" +version = "82.0.1" +description = "Most extensible Python build backend with support for C/C++ extension modules" +optional = false +python-versions = ">=3.9" +groups = ["dev"] +files = [ + {file = "setuptools-82.0.1-py3-none-any.whl", hash = "sha256:a59e362652f08dcd477c78bb6e7bd9d80a7995bc73ce773050228a348ce2e5bb"}, + {file = "setuptools-82.0.1.tar.gz", hash = "sha256:7d872682c5d01cfde07da7bccc7b65469d3dca203318515ada1de5eda35efbf9"}, +] + +[package.extras] +check = ["pytest-checkdocs (>=2.4)", "pytest-ruff (>=0.2.1) ; sys_platform != \"cygwin\"", "ruff (>=0.13.0) ; sys_platform != \"cygwin\""] +core = ["importlib_metadata (>=6) ; python_version < \"3.10\"", "jaraco.functools (>=4)", "jaraco.text (>=3.7)", "more_itertools", "more_itertools (>=8.8)", "packaging (>=24.2)", "tomli (>=2.0.1) ; python_version < \"3.11\"", "wheel (>=0.43.0)"] +cover = ["pytest-cov"] +doc = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "pyproject-hooks (!=1.1)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-favicon", "sphinx-inline-tabs", "sphinx-lint", "sphinx-notfound-page (>=1,<2)", "sphinx-reredirects", "sphinxcontrib-towncrier", "towncrier (<24.7)"] +enabler = ["pytest-enabler (>=2.2)"] +test = ["build[virtualenv] (>=1.0.3)", "filelock (>=3.4.0)", "ini2toml[lite] (>=0.14)", "jaraco.develop (>=7.21) ; python_version >= \"3.9\" and sys_platform != \"cygwin\"", "jaraco.envs (>=2.2)", "jaraco.path (>=3.7.2)", "jaraco.test (>=5.5)", "packaging (>=24.2)", "pip (>=19.1)", "pyproject-hooks (!=1.1)", "pytest (>=6,!=8.1.*)", "pytest-home (>=0.5)", "pytest-perf ; sys_platform != \"cygwin\"", "pytest-subprocess", "pytest-timeout", "pytest-xdist (>=3)", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel (>=0.44.0)"] +type = ["importlib_metadata (>=7.0.2) ; python_version < \"3.10\"", "jaraco.develop (>=7.21) ; sys_platform != \"cygwin\"", "mypy (==1.18.*)", "pytest-mypy"] + +[[package]] +name = "snowballstemmer" +version = "2.2.0" +description = "This package provides 29 stemmers for 28 languages generated from Snowball algorithms." +optional = false +python-versions = "*" +groups = ["docs"] +files = [ + {file = "snowballstemmer-2.2.0-py2.py3-none-any.whl", hash = "sha256:c8e1716e83cc398ae16824e5572ae04e0d9fc2c6b985fb0f900f5f0c96ecba1a"}, + {file = "snowballstemmer-2.2.0.tar.gz", hash = "sha256:09b16deb8547d3412ad7b590689584cd0fe25ec8db3be37788be3810cbf19cb1"}, +] + +[[package]] +name = "sphinx" +version = "8.1.3" +description = "Python documentation generator" +optional = false +python-versions = ">=3.10" +groups = ["docs"] +files = [ + {file = "sphinx-8.1.3-py3-none-any.whl", hash = "sha256:09719015511837b76bf6e03e42eb7595ac8c2e41eeb9c29c5b755c6b677992a2"}, + {file = "sphinx-8.1.3.tar.gz", hash = "sha256:43c1911eecb0d3e161ad78611bc905d1ad0e523e4ddc202a58a821773dc4c927"}, +] + +[package.dependencies] +alabaster = ">=0.7.14" +babel = ">=2.13" +colorama = {version = ">=0.4.6", markers = "sys_platform == \"win32\""} +docutils = ">=0.20,<0.22" +imagesize = ">=1.3" +Jinja2 = ">=3.1" +packaging = ">=23.0" +Pygments = ">=2.17" +requests = ">=2.30.0" +snowballstemmer = ">=2.2" +sphinxcontrib-applehelp = ">=1.0.7" +sphinxcontrib-devhelp = ">=1.0.6" +sphinxcontrib-htmlhelp = ">=2.0.6" +sphinxcontrib-jsmath = ">=1.0.1" +sphinxcontrib-qthelp = ">=1.0.6" +sphinxcontrib-serializinghtml = ">=1.1.9" +tomli = {version = ">=2", markers = "python_version < \"3.11\""} + +[package.extras] +docs = ["sphinxcontrib-websupport"] +lint = ["flake8 (>=6.0)", "mypy (==1.11.1)", "pyright (==1.1.384)", "pytest (>=6.0)", "ruff (==0.6.9)", "sphinx-lint (>=0.9)", "tomli (>=2)", "types-Pillow (==10.2.0.20240822)", "types-Pygments (==2.18.0.20240506)", "types-colorama (==0.4.15.20240311)", "types-defusedxml (==0.7.0.20240218)", "types-docutils (==0.21.0.20241005)", "types-requests (==2.32.0.20240914)", "types-urllib3 (==1.26.25.14)"] +test = ["cython (>=3.0)", "defusedxml (>=0.7.1)", "pytest (>=8.0)", "setuptools (>=70.0)", "typing_extensions (>=4.9)"] + +[[package]] +name = "sphinx-rtd-theme" +version = "3.1.0" +description = "Read the Docs theme for Sphinx" +optional = false +python-versions = ">=3.8" +groups = ["docs"] +files = [ + {file = "sphinx_rtd_theme-3.1.0-py2.py3-none-any.whl", hash = "sha256:1785824ae8e6632060490f67cf3a72d404a85d2d9fc26bce3619944de5682b89"}, + {file = "sphinx_rtd_theme-3.1.0.tar.gz", hash = "sha256:b44276f2c276e909239a4f6c955aa667aaafeb78597923b1c60babc76db78e4c"}, +] + +[package.dependencies] +docutils = ">0.18,<0.23" +sphinx = ">=6,<10" +sphinxcontrib-jquery = ">=4,<5" + +[package.extras] +dev = ["bump2version", "transifex-client", "twine", "wheel"] + +[[package]] +name = "sphinxcontrib-applehelp" +version = "2.0.0" +description = "sphinxcontrib-applehelp is a Sphinx extension which outputs Apple help books" +optional = false +python-versions = ">=3.9" +groups = ["docs"] +files = [ + {file = "sphinxcontrib_applehelp-2.0.0-py3-none-any.whl", hash = "sha256:4cd3f0ec4ac5dd9c17ec65e9ab272c9b867ea77425228e68ecf08d6b28ddbdb5"}, + {file = "sphinxcontrib_applehelp-2.0.0.tar.gz", hash = "sha256:2f29ef331735ce958efa4734873f084941970894c6090408b079c61b2e1c06d1"}, +] + +[package.extras] +lint = ["mypy", "ruff (==0.5.5)", "types-docutils"] +standalone = ["Sphinx (>=5)"] +test = ["pytest"] + +[[package]] +name = "sphinxcontrib-devhelp" +version = "2.0.0" +description = "sphinxcontrib-devhelp is a sphinx extension which outputs Devhelp documents" +optional = false +python-versions = ">=3.9" +groups = ["docs"] +files = [ + {file = "sphinxcontrib_devhelp-2.0.0-py3-none-any.whl", hash = "sha256:aefb8b83854e4b0998877524d1029fd3e6879210422ee3780459e28a1f03a8a2"}, + {file = "sphinxcontrib_devhelp-2.0.0.tar.gz", hash = "sha256:411f5d96d445d1d73bb5d52133377b4248ec79db5c793ce7dbe59e074b4dd1ad"}, +] + +[package.extras] +lint = ["mypy", "ruff (==0.5.5)", "types-docutils"] +standalone = ["Sphinx (>=5)"] +test = ["pytest"] + +[[package]] +name = "sphinxcontrib-htmlhelp" +version = "2.1.0" +description = "sphinxcontrib-htmlhelp is a sphinx extension which renders HTML help files" +optional = false +python-versions = ">=3.9" +groups = ["docs"] +files = [ + {file = "sphinxcontrib_htmlhelp-2.1.0-py3-none-any.whl", hash = "sha256:166759820b47002d22914d64a075ce08f4c46818e17cfc9470a9786b759b19f8"}, + {file = "sphinxcontrib_htmlhelp-2.1.0.tar.gz", hash = "sha256:c9e2916ace8aad64cc13a0d233ee22317f2b9025b9cf3295249fa985cc7082e9"}, +] + +[package.extras] +lint = ["mypy", "ruff (==0.5.5)", "types-docutils"] +standalone = ["Sphinx (>=5)"] +test = ["html5lib", "pytest"] + +[[package]] +name = "sphinxcontrib-jquery" +version = "4.1" +description = "Extension to include jQuery on newer Sphinx releases" +optional = false +python-versions = ">=2.7" +groups = ["docs"] +files = [ + {file = "sphinxcontrib-jquery-4.1.tar.gz", hash = "sha256:1620739f04e36a2c779f1a131a2dfd49b2fd07351bf1968ced074365933abc7a"}, + {file = "sphinxcontrib_jquery-4.1-py2.py3-none-any.whl", hash = "sha256:f936030d7d0147dd026a4f2b5a57343d233f1fc7b363f68b3d4f1cb0993878ae"}, +] + +[package.dependencies] +Sphinx = ">=1.8" + +[[package]] +name = "sphinxcontrib-jsmath" +version = "1.0.1" +description = "A sphinx extension which renders display math in HTML via JavaScript" +optional = false +python-versions = ">=3.5" +groups = ["docs"] +files = [ + {file = "sphinxcontrib-jsmath-1.0.1.tar.gz", hash = "sha256:a9925e4a4587247ed2191a22df5f6970656cb8ca2bd6284309578f2153e0c4b8"}, + {file = "sphinxcontrib_jsmath-1.0.1-py2.py3-none-any.whl", hash = "sha256:2ec2eaebfb78f3f2078e73666b1415417a116cc848b72e5172e596c871103178"}, +] + +[package.extras] +test = ["flake8", "mypy", "pytest"] + +[[package]] +name = "sphinxcontrib-qthelp" +version = "2.0.0" +description = "sphinxcontrib-qthelp is a sphinx extension which outputs QtHelp documents" +optional = false +python-versions = ">=3.9" +groups = ["docs"] +files = [ + {file = "sphinxcontrib_qthelp-2.0.0-py3-none-any.whl", hash = "sha256:b18a828cdba941ccd6ee8445dbe72ffa3ef8cbe7505d8cd1fa0d42d3f2d5f3eb"}, + {file = "sphinxcontrib_qthelp-2.0.0.tar.gz", hash = "sha256:4fe7d0ac8fc171045be623aba3e2a8f613f8682731f9153bb2e40ece16b9bbab"}, +] + +[package.extras] +lint = ["mypy", "ruff (==0.5.5)", "types-docutils"] +standalone = ["Sphinx (>=5)"] +test = ["defusedxml (>=0.7.1)", "pytest"] + +[[package]] +name = "sphinxcontrib-serializinghtml" +version = "2.0.0" +description = "sphinxcontrib-serializinghtml is a sphinx extension which outputs \"serialized\" HTML files (json and pickle)" +optional = false +python-versions = ">=3.9" +groups = ["docs"] +files = [ + {file = "sphinxcontrib_serializinghtml-2.0.0-py3-none-any.whl", hash = "sha256:6e2cb0eef194e10c27ec0023bfeb25badbbb5868244cf5bc5bdc04e4464bf331"}, + {file = "sphinxcontrib_serializinghtml-2.0.0.tar.gz", hash = "sha256:e9d912827f872c029017a53f0ef2180b327c3f7fd23c87229f7a8e8b70031d4d"}, +] + +[package.extras] +lint = ["mypy", "ruff (==0.5.5)", "types-docutils"] +standalone = ["Sphinx (>=5)"] +test = ["pytest"] + +[[package]] +name = "tomli" +version = "2.2.1" +description = "A lil' TOML parser" +optional = false +python-versions = ">=3.8" +groups = ["dev", "docs"] +files = [ + {file = "tomli-2.2.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:678e4fa69e4575eb77d103de3df8a895e1591b48e740211bd1067378c69e8249"}, + {file = "tomli-2.2.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:023aa114dd824ade0100497eb2318602af309e5a55595f76b626d6d9f3b7b0a6"}, + {file = "tomli-2.2.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ece47d672db52ac607a3d9599a9d48dcb2f2f735c6c2d1f34130085bb12b112a"}, + {file = "tomli-2.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6972ca9c9cc9f0acaa56a8ca1ff51e7af152a9f87fb64623e31d5c83700080ee"}, + {file = "tomli-2.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c954d2250168d28797dd4e3ac5cf812a406cd5a92674ee4c8f123c889786aa8e"}, + {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:8dd28b3e155b80f4d54beb40a441d366adcfe740969820caf156c019fb5c7ec4"}, + {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:e59e304978767a54663af13c07b3d1af22ddee3bb2fb0618ca1593e4f593a106"}, + {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:33580bccab0338d00994d7f16f4c4ec25b776af3ffaac1ed74e0b3fc95e885a8"}, + {file = "tomli-2.2.1-cp311-cp311-win32.whl", hash = "sha256:465af0e0875402f1d226519c9904f37254b3045fc5084697cefb9bdde1ff99ff"}, + {file = "tomli-2.2.1-cp311-cp311-win_amd64.whl", hash = "sha256:2d0f2fdd22b02c6d81637a3c95f8cd77f995846af7414c5c4b8d0545afa1bc4b"}, + {file = "tomli-2.2.1-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:4a8f6e44de52d5e6c657c9fe83b562f5f4256d8ebbfe4ff922c495620a7f6cea"}, + {file = "tomli-2.2.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:8d57ca8095a641b8237d5b079147646153d22552f1c637fd3ba7f4b0b29167a8"}, + {file = "tomli-2.2.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4e340144ad7ae1533cb897d406382b4b6fede8890a03738ff1683af800d54192"}, + {file = "tomli-2.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:db2b95f9de79181805df90bedc5a5ab4c165e6ec3fe99f970d0e302f384ad222"}, + {file = "tomli-2.2.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:40741994320b232529c802f8bc86da4e1aa9f413db394617b9a256ae0f9a7f77"}, + {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:400e720fe168c0f8521520190686ef8ef033fb19fc493da09779e592861b78c6"}, + {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:02abe224de6ae62c19f090f68da4e27b10af2b93213d36cf44e6e1c5abd19fdd"}, + {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:b82ebccc8c8a36f2094e969560a1b836758481f3dc360ce9a3277c65f374285e"}, + {file = "tomli-2.2.1-cp312-cp312-win32.whl", hash = "sha256:889f80ef92701b9dbb224e49ec87c645ce5df3fa2cc548664eb8a25e03127a98"}, + {file = "tomli-2.2.1-cp312-cp312-win_amd64.whl", hash = "sha256:7fc04e92e1d624a4a63c76474610238576942d6b8950a2d7f908a340494e67e4"}, + {file = "tomli-2.2.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f4039b9cbc3048b2416cc57ab3bda989a6fcf9b36cf8937f01a6e731b64f80d7"}, + {file = "tomli-2.2.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:286f0ca2ffeeb5b9bd4fcc8d6c330534323ec51b2f52da063b11c502da16f30c"}, + {file = "tomli-2.2.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a92ef1a44547e894e2a17d24e7557a5e85a9e1d0048b0b5e7541f76c5032cb13"}, + {file = "tomli-2.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9316dc65bed1684c9a98ee68759ceaed29d229e985297003e494aa825ebb0281"}, + {file = "tomli-2.2.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e85e99945e688e32d5a35c1ff38ed0b3f41f43fad8df0bdf79f72b2ba7bc5272"}, + {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:ac065718db92ca818f8d6141b5f66369833d4a80a9d74435a268c52bdfa73140"}, + {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:d920f33822747519673ee656a4b6ac33e382eca9d331c87770faa3eef562aeb2"}, + {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:a198f10c4d1b1375d7687bc25294306e551bf1abfa4eace6650070a5c1ae2744"}, + {file = "tomli-2.2.1-cp313-cp313-win32.whl", hash = "sha256:d3f5614314d758649ab2ab3a62d4f2004c825922f9e370b29416484086b264ec"}, + {file = "tomli-2.2.1-cp313-cp313-win_amd64.whl", hash = "sha256:a38aa0308e754b0e3c67e344754dff64999ff9b513e691d0e786265c93583c69"}, + {file = "tomli-2.2.1-py3-none-any.whl", hash = "sha256:cb55c73c5f4408779d0cf3eef9f762b9c9f147a77de7b258bef0a5628adc85cc"}, + {file = "tomli-2.2.1.tar.gz", hash = "sha256:cd45e1dc79c835ce60f7404ec8119f2eb06d38b1deba146f07ced3bbc44505ff"}, +] +markers = {dev = "python_full_version <= \"3.11.0a6\"", docs = "python_version == \"3.10\""} + +[[package]] +name = "typing-extensions" +version = "4.12.2" +description = "Backported and Experimental Type Hints for Python 3.8+" +optional = false +python-versions = ">=3.8" +groups = ["dev"] +markers = "python_version < \"3.13\"" +files = [ + {file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"}, + {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, +] + +[[package]] +name = "urllib3" +version = "2.7.0" +description = "HTTP library with thread-safe connection pooling, file post, and more." +optional = false +python-versions = ">=3.10" +groups = ["docs"] +files = [ + {file = "urllib3-2.7.0-py3-none-any.whl", hash = "sha256:9fb4c81ebbb1ce9531cce37674bbc6f1360472bc18ca9a553ede278ef7276897"}, + {file = "urllib3-2.7.0.tar.gz", hash = "sha256:231e0ec3b63ceb14667c67be60f2f2c40a518cb38b03af60abc813da26505f4c"}, +] + +[package.extras] +brotli = ["brotli (>=1.2.0) ; platform_python_implementation == \"CPython\"", "brotlicffi (>=1.2.0.0) ; platform_python_implementation != \"CPython\""] +h2 = ["h2 (>=4,<5)"] +socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"] +zstd = ["backports-zstd (>=1.0.0) ; python_version < \"3.14\""] + +[metadata] +lock-version = "2.1" +python-versions = "^3.10" +content-hash = "c0d62f11cb94761d233c73a46ff3f626640ae3ef3af9a2ece9f37095a47d4c71" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..67badccfa --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,281 @@ +[build-system] +requires = ['setuptools>=77.0', 'Cython>=3.0.8', "poetry-core>=2.1.0"] +build-backend = "poetry.core.masonry.api" + +[project] +name = "zeroconf" +version = "0.149.16" +license = "LGPL-2.1-or-later" +description = "A pure python implementation of multicast DNS service discovery" +readme = "README.rst" +authors = [ + { name = "Paul Scott-Murphy" }, + { name = "William McBrine" }, + { name = "Jakub Stasiak" }, + { name = "J. Nick Koston" }, +] +requires-python = ">=3.10" + +[project.urls] +"Repository" = "https://github.com/python-zeroconf/python-zeroconf" +"Documentation" = "https://python-zeroconf.readthedocs.io" +"Bug Tracker" = "https://github.com/python-zeroconf/python-zeroconf/issues" +"Changelog" = "https://github.com/python-zeroconf/python-zeroconf/blob/master/CHANGELOG.md" + +[tool.poetry] +classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'Intended Audience :: System Administrators', + 'Operating System :: POSIX', + 'Operating System :: POSIX :: Linux', + 'Operating System :: MacOS :: MacOS X', + 'Topic :: Software Development :: Libraries', + 'Programming Language :: Python :: Implementation :: CPython', + 'Programming Language :: Python :: Implementation :: PyPy', +] +packages = [ + { include = "zeroconf", from = "src" }, +] +include = [ + { path = "CHANGELOG.md", format = "sdist" }, + { path = "COPYING", format = "sdist" }, + { path = "docs", format = "sdist" }, + { path = "tests", format = "sdist" }, +] +# Make sure we don't package temporary C files generated by the build process +exclude = [ "**/*.c" ] + +[tool.poetry.build] +generate-setup-file = true +script = "build_ext.py" + +[tool.semantic_release] +branch = "master" +version_toml = ["pyproject.toml:project.version"] +version_variables = [ + "src/zeroconf/__init__.py:__version__" +] +build_command = "pip install poetry && poetry build" +tag_format = "{version}" +allow_zero_version = true + +[tool.semantic_release.changelog] +exclude_commit_patterns = [ + "chore*", + "ci*", +] + +[tool.semantic_release.changelog.environment] +keep_trailing_newline = true + +[tool.semantic_release.branches.master] +match = "master" + +[tool.semantic_release.branches."release-0.x"] +match = "release-0.x" + +[tool.semantic_release.branches.noop] +match = "(?!(master|release-0.x)$)" +prerelease = true + +[tool.poetry.dependencies] +python = "^3.10" +ifaddr = ">=0.1.7" + +[tool.poetry.group.dev.dependencies] +pytest = ">=9.0.3,<10.0" +pytest-cov = ">=4,<8" +pytest-asyncio = ">=1.3.0,<1.4.0" +cython = "^3.2.4" +setuptools = ">=65.6.3,<83.0.0" +pytest-timeout = "^2.1.0" +pytest-codspeed = ">=5.0.2,<6.0" +blockbuster = ">=1.5.5,<2.0.0" + +[tool.poetry.group.docs.dependencies] +sphinx = "^7.4.7 || ^8.1.3" +sphinx-rtd-theme = "^3.1.0" + +[tool.ruff] +target-version = "py310" +line-length = 110 + +[tool.ruff.lint] +ignore = [ + "S101", # use of assert + "S104", # S104 Possible binding to all interfaces + "PLR0912", # too many to fix right now + "TID252", # skip + "PLR0913", # too late to make changes here + "PLR0911", # would be breaking change + "TRY003", # too many to fix + "SLF001", # design choice + "PLR2004" , # too many to fix + "PGH004", # too many to fix + "PGH003", # too many to fix + "SIM110", # this is slower + "PYI034", # enable when we drop Py3.10 + "PYI032", # breaks Cython + "PYI041", # breaks Cython + "PERF401", # Cython: closures inside cpdef functions not yet supported +] +select = [ + "ASYNC", # async rules + "B", # flake8-bugbear + "C4", # flake8-comprehensions + "S", # flake8-bandit + "F", # pyflake + "E", # pycodestyle + "W", # pycodestyle + "UP", # pyupgrade + "I", # isort + "RUF", # ruff specific + "FLY", # flynt + "G", # flake8-logging-format , + "PERF", # Perflint + "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PL", # pylint + "PT", # flake8-pytest-style + "PTH", # flake8-pathlib + "PYI", # flake8-pyi + "RET", # flake8-return + "RSE", # flake8-raise , + "SIM", # flake8-simplify + "SLF", # flake8-self + "SLOT", # flake8-slots + "T100", # Trace found: {name} used + "T20", # flake8-print + "TID", # Tidy imports + "TRY", # tryceratops +] + +[tool.ruff.lint.per-file-ignores] +"tests/**/*" = [ + "D100", + "D101", + "D102", + "D103", + "D104", + "S101", + "SLF001", + "PLR2004", # too many to fix right now + "PT011", # too many to fix right now + "PGH003", # too many to fix right now + "PT027", # too many to fix right now + "PLW0603" , # too many to fix right now + "PLR0915", # too many to fix right now + "FLY002", # too many to fix right now + "PT018", # too many to fix right now + "PLR0124", # too many to fix right now + "PT012" , # too many to fix right now + "TID252", # too many to fix right now + "PLR0913", # skip this one + "T201", # too many to fix right now + "PT004", # nice to have +] +"bench/**/*" = [ + "T201", # intended +] +"examples/**/*" = [ + "T201", # intended +] +"setup.py" = ["D100"] +"conftest.py" = ["D100"] +"docs/conf.py" = ["D100"] + +[tool.pylint.BASIC] +class-const-naming-style = "any" +good-names = [ + "e", + "er", + "h", + "i", + "id", + "ip", + "os", + "n", + "rr", + "rs", + "s", + "t", + "wr", + "zc", + "_GLOBAL_DONE", +] + +[tool.pylint."MESSAGES CONTROL"] +disable = [ + "duplicate-code", + "fixme", + "format", + "missing-class-docstring", + "missing-function-docstring", + "too-few-public-methods", + "too-many-arguments", + "too-many-instance-attributes", + "too-many-public-methods" +] + + +[tool.pytest.ini_options] +addopts = "-v -Wdefault --cov=zeroconf --cov-report=term-missing:skip-covered" +pythonpath = ["src"] + +[tool.coverage.run] +branch = true + +[tool.coverage.report] +exclude_lines = [ + "pragma: no cover", + "@overload", + "if TYPE_CHECKING", + "raise NotImplementedError", +] + + +[tool.isort] +profile = "black" +known_first_party = ["zeroconf", "tests"] + +[tool.mypy] +warn_unused_configs = true +check_untyped_defs = true +disallow_any_generics = false # turn this on when we drop 3.7/3.8 support +disallow_incomplete_defs = true +disallow_untyped_defs = true +warn_incomplete_stub = true +mypy_path = "src/" +show_error_codes = true +warn_redundant_casts = false # Activate for cleanup. +warn_return_any = true +warn_unreachable = true +warn_unused_ignores = false # Does not always work properly, activate for cleanup. +extra_checks = true +strict_equality = true +strict_bytes = true # Will be true by default with mypy v2 release. +exclude = [ + 'docs/*', + 'bench/*', +] + +[[tool.mypy.overrides]] +module = "tests.*" +allow_untyped_defs = true + +[[tool.mypy.overrides]] +module = "docs.*" +ignore_errors = true +allow_untyped_defs = true + +[[tool.mypy.overrides]] +module = "bench.*" +ignore_errors = true + +[tool.codespell] +ignore-words-list = ["additionals", "HASS"] + +[tool.cython-lint] +max-line-length = 110 +ignore = ['E501'] # too many to fix right now diff --git a/requirements-dev.txt b/requirements-dev.txt deleted file mode 100644 index ec443c0bf..000000000 --- a/requirements-dev.txt +++ /dev/null @@ -1,9 +0,0 @@ -autopep8 -coveralls -coverage -# Version restricted because of https://github.com/PyCQA/pycodestyle/issues/741 -flake8>=3.6.0 -flake8-import-order -ifaddr -nose -pep8-naming!=0.6.0 diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index d39bf999c..000000000 --- a/setup.cfg +++ /dev/null @@ -1,19 +0,0 @@ -[flake8] -show-source = 1 -application-import-names=zeroconf -max-line-length=110 - -[mypy] -ignore_missing_imports = true -follow_imports = error -check_untyped_defs = true -no_implicit_optional = true -warn_incomplete_stub = true -warn_no_return = true -warn_redundant_casts = true -warn_unused_configs = true -warn_unused_ignores = true -warn_return_any = true -# TODO: disallow untyped calls and defs once we have full type hint coverage -disallow_untyped_calls = false -disallow_untyped_defs = false diff --git a/setup.py b/setup.py deleted file mode 100755 index 1ed96da1d..000000000 --- a/setup.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python3 -from io import open -from os.path import abspath, dirname, join - -from setuptools import setup - -PROJECT_ROOT = abspath(dirname(__file__)) -with open(join(PROJECT_ROOT, 'README.rst'), encoding='utf-8') as f: - readme = f.read() - -version = ( - [l for l in open(join(PROJECT_ROOT, 'zeroconf.py')) if '__version__' in l][0] - .split('=')[-1] - .strip().strip('\'"') -) - -setup( - name='zeroconf', - version=version, - description='Pure Python Multicast DNS Service Discovery Library ' - '(Bonjour/Avahi compatible)', - long_description=readme, - author='Paul Scott-Murphy, William McBrine, Jakub Stasiak', - url='https://github.com/jstasiak/python-zeroconf', - py_modules=['zeroconf'], - platforms=['unix', 'linux', 'osx'], - license='LGPL', - zip_safe=False, - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Intended Audience :: Developers', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)', - 'Operating System :: POSIX', - 'Operating System :: POSIX :: Linux', - 'Operating System :: MacOS :: MacOS X', - 'Topic :: Software Development :: Libraries', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: Implementation :: CPython', - 'Programming Language :: Python :: Implementation :: PyPy', - ], - keywords=[ - 'Bonjour', 'Avahi', 'Zeroconf', 'Multicast DNS', 'Service Discovery', - 'mDNS', - ], - install_requires=[ - 'ifaddr', - 'typing;python_version<"3.5"' - ], -) diff --git a/src/zeroconf/__init__.py b/src/zeroconf/__init__.py new file mode 100644 index 000000000..5b79402c5 --- /dev/null +++ b/src/zeroconf/__init__.py @@ -0,0 +1,119 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from ._cache import DNSCache # noqa # import needed for backwards compat +from ._core import Zeroconf +from ._dns import ( # noqa # import needed for backwards compat + DNSAddress, + DNSEntry, + DNSHinfo, + DNSNsec, + DNSPointer, + DNSQuestion, + DNSQuestionType, + DNSRecord, + DNSService, + DNSText, +) +from ._exceptions import ( + AbstractMethodException, + BadTypeInNameException, + Error, + EventLoopBlocked, + IncomingDecodeError, + NamePartTooLongException, + NonUniqueNameException, + NotRunningException, + ServiceNameAlreadyRegistered, +) +from ._logger import QuietLogger, log # noqa # import needed for backwards compat +from ._protocol.incoming import DNSIncoming # noqa # import needed for backwards compat +from ._protocol.outgoing import DNSOutgoing # noqa # import needed for backwards compat +from ._record_update import RecordUpdate +from ._services import ( # noqa # import needed for backwards compat + ServiceListener, + ServiceStateChange, + Signal, + SignalRegistrationInterface, +) +from ._services.browser import ServiceBrowser +from ._services.info import ( # noqa # import needed for backwards compat + AddressResolver, + AddressResolverIPv4, + AddressResolverIPv6, + ServiceInfo, + instance_name_from_service_info, +) +from ._services.registry import ( # noqa # import needed for backwards compat + ServiceRegistry, +) +from ._services.types import ZeroconfServiceTypes +from ._updates import RecordUpdateListener +from ._utils.name import service_type_name # noqa # import needed for backwards compat +from ._utils.net import ( # noqa # import needed for backwards compat + InterfaceChoice, + InterfacesType, + IPVersion, + add_multicast_member, + autodetect_ip_version, + create_sockets, + get_all_addresses, + get_all_addresses_v6, +) +from ._utils.time import ( # noqa # import needed for backwards compat + current_time_millis, + millis_to_seconds, +) + +__author__ = "Paul Scott-Murphy, William McBrine" +__maintainer__ = "Jakub Stasiak " +__version__ = "0.149.16" +__license__ = "LGPL" + + +__all__ = [ + "AbstractMethodException", + "BadTypeInNameException", + "DNSQuestionType", + # Exceptions + "Error", + "EventLoopBlocked", + "IPVersion", + "IncomingDecodeError", + "InterfaceChoice", + "NamePartTooLongException", + "NonUniqueNameException", + "NotRunningException", + "RecordUpdate", + "RecordUpdateListener", + "ServiceBrowser", + "ServiceInfo", + "ServiceListener", + "ServiceNameAlreadyRegistered", + "ServiceStateChange", + "Zeroconf", + "ZeroconfServiceTypes", + "__version__", + "current_time_millis", +] diff --git a/src/zeroconf/_cache.pxd b/src/zeroconf/_cache.pxd new file mode 100644 index 000000000..023304bc4 --- /dev/null +++ b/src/zeroconf/_cache.pxd @@ -0,0 +1,96 @@ +import cython + +from ._dns cimport ( + DNSAddress, + DNSEntry, + DNSHinfo, + DNSNsec, + DNSPointer, + DNSRecord, + DNSService, + DNSText, +) + +cdef object heappop +cdef object heappush +cdef object heapify + +cdef object _UNIQUE_RECORD_TYPES +cdef unsigned int _TYPE_PTR +cdef cython.uint _ONE_SECOND +cdef unsigned int _MIN_SCHEDULED_RECORD_EXPIRATION +cdef unsigned int _MAX_CACHE_RECORDS + + +@cython.locals(record_cache=dict) +cdef _remove_key(cython.dict cache, object key, DNSRecord record) + + +cdef class DNSCache: + + cdef public cython.dict cache + cdef public cython.dict service_cache + cdef public list _expire_heap + cdef public dict _expirations + cdef public unsigned int _total_records + + cpdef bint async_add_records(self, object entries) + + cpdef void async_remove_records(self, object entries) + + @cython.locals(store=cython.dict) + cpdef DNSRecord async_get_unique(self, DNSRecord entry) + + @cython.locals(record=DNSRecord, when_record=tuple, when=double) + cpdef list async_expire(self, double now) + + @cython.locals(records=cython.dict, record=DNSRecord) + cpdef list async_all_by_details(self, str name, unsigned int type_, unsigned int class_) + + cpdef list async_entries_with_name(self, str name) + + cpdef list async_entries_with_server(self, str name) + + @cython.locals(cached_entry=DNSRecord, records=dict) + cpdef DNSRecord get_by_details(self, str name, unsigned int type_, unsigned int class_) + + @cython.locals(records=cython.dict, entry=DNSRecord) + cpdef cython.list get_all_by_details(self, str name, unsigned int type_, unsigned int class_) + + @cython.locals( + store=cython.dict, + service_store=cython.dict, + service_record=DNSService, + when=object, + new=bint, + is_new=bint + ) + cdef bint _async_add(self, DNSRecord record) + + @cython.locals(record=DNSRecord, when_record=tuple) + cdef void _async_evict_oldest(self) + + @cython.locals(expire_heap_len="unsigned int") + cdef void _maybe_rebuild_heap(self) + + @cython.locals(service_record=DNSService) + cdef void _async_remove(self, DNSRecord record) + + @cython.locals(record=DNSRecord, created_double=double) + cpdef void async_mark_unique_records_older_than_1s_to_expire(self, cython.set unique_types, object answers, double now) + + @cython.locals(entries=dict) + cpdef list entries_with_name(self, str name) + + @cython.locals(entries=dict) + cpdef list entries_with_server(self, str server) + + @cython.locals(record=DNSRecord, now=double) + cpdef current_entry_with_name_and_alias(self, str name, str alias) + + cpdef void _async_set_created_ttl( + self, + DNSRecord record, + double now, + unsigned int ttl + ) diff --git a/src/zeroconf/_cache.py b/src/zeroconf/_cache.py new file mode 100644 index 000000000..df60982b7 --- /dev/null +++ b/src/zeroconf/_cache.py @@ -0,0 +1,348 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from collections.abc import Iterable +from heapq import heapify, heappop, heappush +from typing import cast + +from ._dns import ( + DNSAddress, + DNSEntry, + DNSHinfo, + DNSNsec, + DNSPointer, + DNSRecord, + DNSService, + DNSText, +) +from ._utils.time import current_time_millis +from .const import _MAX_CACHE_RECORDS, _ONE_SECOND, _TYPE_PTR + +_UNIQUE_RECORD_TYPES = (DNSAddress, DNSHinfo, DNSPointer, DNSText, DNSService) +_UniqueRecordsType = DNSAddress | DNSHinfo | DNSPointer | DNSText | DNSService +_DNSRecordCacheType = dict[str, dict[DNSRecord, DNSRecord]] +_DNSRecord = DNSRecord +_str = str +_float = float +_int = int + +# The minimum number of scheduled record expirations before we start cleaning up +# the expiration heap. This is a performance optimization to avoid cleaning up the +# heap too often when there are only a few scheduled expirations. +_MIN_SCHEDULED_RECORD_EXPIRATION = 100 + + +def _remove_key(cache: _DNSRecordCacheType, key: _str, record: _DNSRecord) -> None: + """Remove a key from a DNSRecord cache + + This function must be run in from event loop. + """ + record_cache = cache[key] + del record_cache[record] + if not record_cache: + del cache[key] + + +class DNSCache: + """A cache of DNS entries.""" + + def __init__(self) -> None: + self.cache: _DNSRecordCacheType = {} + self._expire_heap: list[tuple[float, DNSRecord]] = [] + self._expirations: dict[DNSRecord, float] = {} + self.service_cache: _DNSRecordCacheType = {} + self._total_records: int = 0 + + # Functions prefixed with async_ are NOT threadsafe and must + # be run in the event loop. + + def _async_add(self, record: _DNSRecord) -> bool: + """Adds an entry. + + Returns true if the entry was not already in the cache. + + This function must be run in from event loop. + """ + # Previously storage of records was implemented as a list + # instead a dict. Since DNSRecords are now hashable, the implementation + # uses a dict to ensure that adding a new record to the cache + # replaces any existing records that are __eq__ to each other which + # removes the risk that accessing the cache from the wrong + # direction would return the old incorrect entry. + store = self.cache.get(record.key) + is_new = store is None or record not in store + # Bound total cache size; evict closest-to-expiration entry to + # make room before inserting a new record. Prevents a LAN-local + # flood of unique-name records from growing the cache without + # bound (RFC 6762 §10 advisory caching, defense-in-depth). + if is_new and self._total_records >= _MAX_CACHE_RECORDS: + self._async_evict_oldest() + # The victim may have been the last record under + # ``record.key``, in which case ``_remove_key`` deleted + # the bucket. Re-fetch before creating below. + store = self.cache.get(record.key) + if store is None: + store = self.cache[record.key] = {} + new = is_new and not isinstance(record, DNSNsec) + if is_new: + self._total_records += 1 + store[record] = record + when = record.created + (record.ttl * 1000) + if self._expirations.get(record) != when: + heappush(self._expire_heap, (when, record)) + self._expirations[record] = when + # Re-adds of an existing record with a new TTL push a fresh + # entry but leave the prior tuple behind as stale, so a peer + # that just replays cached records can grow ``_expire_heap`` + # without ever tripping the cap. Rebuild when stale entries + # dominate. + self._maybe_rebuild_heap() + + if isinstance(record, DNSService): + service_record = record + if (service_store := self.service_cache.get(service_record.server_key)) is None: + service_store = self.service_cache[service_record.server_key] = {} + service_store[service_record] = service_record + return new + + def _async_evict_oldest(self) -> None: + """Drop the closest-to-expiration record to make room for a new one.""" + while self._expire_heap: + when_record = heappop(self._expire_heap) + record = when_record[1] + if self._expirations.get(record) != when_record[0]: + continue + self._async_remove(record) + return + + def _maybe_rebuild_heap(self) -> None: + """Rebuild ``_expire_heap`` when stale entries dominate live ones.""" + expire_heap_len = len(self._expire_heap) + if ( + expire_heap_len > _MIN_SCHEDULED_RECORD_EXPIRATION + and expire_heap_len > len(self._expirations) * 2 + ): + self._expire_heap = [ + entry for entry in self._expire_heap if self._expirations.get(entry[1]) == entry[0] + ] + heapify(self._expire_heap) + + def async_add_records(self, entries: Iterable[DNSRecord]) -> bool: + """Add multiple records. + + Returns true if any of the records were not in the cache. + + This function must be run in from event loop. + """ + new = False + for entry in entries: + if self._async_add(entry): + new = True + return new + + def _async_remove(self, record: _DNSRecord) -> None: + """Removes an entry. + + This function must be run in from event loop. + """ + if isinstance(record, DNSService): + service_record = record + _remove_key(self.service_cache, service_record.server_key, service_record) + _remove_key(self.cache, record.key, record) + self._expirations.pop(record, None) + self._total_records -= 1 + + def async_remove_records(self, entries: Iterable[DNSRecord]) -> None: + """Remove multiple records. + + This function must be run in from event loop. + """ + for entry in entries: + self._async_remove(entry) + + def async_expire(self, now: _float) -> list[DNSRecord]: + """Purge expired entries from the cache. + + This function must be run in from event loop. + + :param now: The current time in milliseconds. + """ + if not self._expire_heap: + return [] + + expired: list[DNSRecord] = [] + while self._expire_heap: + when_record = self._expire_heap[0] + when = when_record[0] + if when > now: + break + heappop(self._expire_heap) + # Skip entries left behind by a TTL re-add; the live tuple is + # later in the heap and will be removed when it reaches the top. + record = when_record[1] + if self._expirations.get(record) == when: + expired.append(record) + + self._maybe_rebuild_heap() + self.async_remove_records(expired) + return expired + + def async_get_unique(self, entry: _UniqueRecordsType) -> DNSRecord | None: + """Gets a unique entry by key. Will return None if there is no + matching entry. + + This function is not threadsafe and must be called from + the event loop. + """ + store = self.cache.get(entry.key) + if store is None: + return None + return store.get(entry) + + def async_all_by_details(self, name: _str, type_: _int, class_: _int) -> list[DNSRecord]: + """Gets all matching entries by details. + + This function is not thread-safe and must be called from + the event loop. + """ + key = name.lower() + records = self.cache.get(key) + matches: list[DNSRecord] = [] + if records is None: + return matches + for record in records.values(): + if type_ == record.type and class_ == record.class_: + matches.append(record) + return matches + + def async_entries_with_name(self, name: str) -> list[DNSRecord]: + """Returns a dict of entries whose key matches the name. + + This function is not threadsafe and must be called from + the event loop. + """ + return self.entries_with_name(name) + + def async_entries_with_server(self, name: str) -> list[DNSRecord]: + """Returns a dict of entries whose key matches the server. + + This function is not threadsafe and must be called from + the event loop. + """ + return self.entries_with_server(name) + + # The below functions are threadsafe and do not need to be run in the + # event loop, however they all make copies so they significantly + # inefficient. + + def get(self, entry: DNSEntry) -> DNSRecord | None: + """Gets an entry by key. Will return None if there is no + matching entry.""" + if isinstance(entry, _UNIQUE_RECORD_TYPES): + return self.cache.get(entry.key, {}).get(entry) + for cached_entry in reversed(list(self.cache.get(entry.key, {}).values())): + if entry.__eq__(cached_entry): + return cached_entry + return None + + def get_by_details(self, name: str, type_: _int, class_: _int) -> DNSRecord | None: + """Gets the first matching entry by details. Returns None if no entries match. + + Calling this function is not recommended as it will only + return one record even if there are multiple entries. + + For example if there are multiple A or AAAA addresses this + function will return the last one that was added to the cache + which may not be the one you expect. + + Use get_all_by_details instead. + """ + key = name.lower() + records = self.cache.get(key) + if records is None: + return None + for cached_entry in reversed(list(records.values())): + 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]: + """Gets all matching entries by details.""" + key = name.lower() + records = self.cache.get(key) + if records is None: + return [] + return [entry for entry in list(records.values()) 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.""" + if entries := self.service_cache.get(server.lower()): + return list(entries.values()) + return [] + + def entries_with_name(self, name: str) -> list[DNSRecord]: + """Returns a list of entries whose key matches the name.""" + if entries := self.cache.get(name.lower()): + return list(entries.values()) + return [] + + def current_entry_with_name_and_alias(self, name: str, alias: str) -> DNSRecord | None: + now = current_time_millis() + for record in reversed(self.entries_with_name(name)): + if ( + record.type == _TYPE_PTR + and not record.is_expired(now) + and cast(DNSPointer, record).alias == alias + ): + return record + return None + + def names(self) -> list[str]: + """Return a copy of the list of current cache names.""" + return list(self.cache) + + def async_mark_unique_records_older_than_1s_to_expire( + self, + unique_types: set[tuple[_str, _int, _int]], + answers: Iterable[DNSRecord], + now: _float, + ) -> None: + # rfc6762#section-10.2 para 2 + # Since unique is set, all old records with that name, rrtype, + # and rrclass that were received more than one second ago are declared + # invalid, and marked to expire from the cache in one second. + answers_rrset = set(answers) + for name, type_, class_ in unique_types: + for record in self.async_all_by_details(name, type_, class_): + created_double = record.created + if (now - created_double > _ONE_SECOND) and record not in answers_rrset: + # Expire in 1s + self._async_set_created_ttl(record, now, 1) + + def _async_set_created_ttl(self, record: DNSRecord, now: _float, ttl: _int) -> None: + """Set the created time and ttl of a record.""" + # It would be better if we made a copy instead of mutating the record + # in place, but records currently don't have a copy method. + record._set_created_ttl(now, ttl) + self._async_add(record) diff --git a/src/zeroconf/_core.py b/src/zeroconf/_core.py new file mode 100644 index 000000000..f184b6fa5 --- /dev/null +++ b/src/zeroconf/_core.py @@ -0,0 +1,759 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import logging +import random +import sys +import threading +from collections.abc import Awaitable +from types import TracebackType + +from ._cache import DNSCache +from ._dns import DNSQuestion, DNSQuestionType +from ._engine import AsyncEngine +from ._exceptions import NonUniqueNameException, NotRunningException +from ._handlers.multicast_outgoing_queue import MulticastOutgoingQueue +from ._handlers.query_handler import QueryHandler +from ._handlers.record_manager import RecordManager +from ._history import QuestionHistory +from ._logger import QuietLogger, log +from ._protocol.outgoing import DNSOutgoing +from ._services import ServiceListener +from ._services.browser import ServiceBrowser +from ._services.info import ( + AsyncServiceInfo, + ServiceInfo, + instance_name_from_service_info, +) +from ._services.registry import ServiceRegistry +from ._transport import _WrappedTransport +from ._updates import RecordUpdateListener +from ._utils.asyncio import ( + _resolve_all_futures_to_none, + await_awaitable, + get_running_loop, + run_coro_with_timeout, + shutdown_loop, + wait_for_future_set_or_timeout, + wait_future_or_timeout, +) +from ._utils.name import service_type_name +from ._utils.net import ( + InterfaceChoice, + InterfacesType, + IPVersion, + autodetect_ip_version, + can_send_to, + create_sockets, +) +from ._utils.time import current_time_millis, millis_to_seconds +from .const import ( + _CHECK_TIME, + _CLASS_IN, + _CLASS_UNIQUE, + _FLAGS_AA, + _FLAGS_QR_QUERY, + _FLAGS_QR_RESPONSE, + _MAX_MSG_ABSOLUTE, + _MDNS_ADDR, + _MDNS_ADDR6, + _MDNS_PORT, + _ONE_SECOND, + _REGISTER_TIME, + _STARTUP_TIMEOUT, + _TYPE_PTR, + _UNREGISTER_TIME, +) + +# The maximum amount of time to delay a multicast +# response in order to aggregate answers +_AGGREGATION_DELAY = 500 # ms +# The maximum amount of time to delay a multicast +# response in order to aggregate answers after +# it has already been delayed to protect the network +# from excessive traffic. We use a shorter time +# window here as we want to _try_ to answer all +# queries in under 1350ms while protecting +# the network from excessive traffic to ensure +# a service info request with two questions +# can be answered in the default timeout of +# 3000ms +_PROTECTED_AGGREGATION_DELAY = 200 # ms + +_REGISTER_BROADCASTS = 3 + +# RFC 6762 §8.1 thundering-herd avoidance: wait a random +# 0-250ms before the first probe so simultaneously-started +# responders don't collide. We default to 150-250ms to +# preserve existing timing assumptions; tests on loopback +# may patch this lower via the `quick_timing` fixture. +_PROBE_RANDOM_DELAY_INTERVAL = (150, 250) # ms + + +def async_send_with_transport( + log_debug: bool, + transport: _WrappedTransport, + packet: bytes, + packet_num: int, + out: DNSOutgoing, + addr: str | None, + port: int, + v6_flow_scope: tuple[()] | tuple[int, int] = (), +) -> None: + ipv6_socket = transport.is_ipv6 + if addr is None: + real_addr = _MDNS_ADDR6 if ipv6_socket else _MDNS_ADDR + else: + real_addr = addr + if not can_send_to(ipv6_socket, real_addr): + return + if log_debug: + log.debug( + "Sending to (%s, %d) via [socket %s (%s)] (%d bytes #%d) %r as %r...", + real_addr, + port or _MDNS_PORT, + transport.fileno, + transport.sock_name, + len(packet), + packet_num + 1, + out, + packet, + ) + # Get flowinfo and scopeid for the IPV6 socket to create a complete IPv6 + # address tuple: https://docs.python.org/3.6/library/socket.html#socket-families + if ipv6_socket and not v6_flow_scope: + _, _, sock_flowinfo, sock_scopeid = transport.sock_name + v6_flow_scope = (sock_flowinfo, sock_scopeid) + transport.transport.sendto(packet, (real_addr, port or _MDNS_PORT, *v6_flow_scope)) + + +class Zeroconf(QuietLogger): + """Implementation of Zeroconf Multicast DNS Service Discovery + + Supports registration, unregistration, queries and browsing. + """ + + def __init__( + self, + interfaces: InterfacesType = InterfaceChoice.All, + unicast: bool = False, + ip_version: IPVersion | None = None, + apple_p2p: bool = False, + use_asyncio: bool | None = None, + ) -> None: + """Creates an instance of the Zeroconf class, establishing + multicast communications, listening and reaping threads. + + :param interfaces: :class:`InterfaceChoice` or a list of IP addresses + (IPv4 and IPv6) and interface indexes (IPv6 only). + + IPv6 notes for non-POSIX systems: + * `InterfaceChoice.All` is an alias for `InterfaceChoice.Default` + on Python versions before 3.8. + + Also listening on loopback (``::1``) doesn't work, use a real address. + :param ip_version: IP versions to support. If `choice` is a list, the default is detected + from it. Otherwise defaults to V4 only for backward compatibility. + :param apple_p2p: use AWDL interface (only macOS) + :param use_asyncio: explicitly control whether to attach to the running + asyncio event loop (``True``) or run an internal thread with its + own loop (``False``). ``None`` (default) keeps the historic + behavior: attach if an event loop is running, otherwise start a + thread. Set to ``False`` when running inside an environment that + already has an event loop (e.g. Jupyter) but you want blocking + semantics. ``True`` raises :class:`RuntimeError` immediately if no + running event loop is found, instead of falling back to the thread. + """ + if ip_version is None: + ip_version = autodetect_ip_version(interfaces) + + self.done = False + + if apple_p2p and sys.platform != "darwin": + raise RuntimeError("Option `apple_p2p` is not supported on non-Apple platforms.") + + if use_asyncio is True and get_running_loop() is None: + raise RuntimeError("use_asyncio=True requires a running asyncio event loop") + + self.unicast = unicast + self._use_asyncio = use_asyncio + listen_socket, respond_sockets = create_sockets(interfaces, unicast, ip_version, apple_p2p=apple_p2p) + log.debug("Listen socket %s, respond sockets %s", listen_socket, respond_sockets) + + self.engine = AsyncEngine(self, listen_socket, respond_sockets) + + self.browsers: dict[ServiceListener, ServiceBrowser] = {} + self.registry = ServiceRegistry() + self.cache = DNSCache() + self.question_history = QuestionHistory() + + self.out_queue = MulticastOutgoingQueue(self, 0, _AGGREGATION_DELAY) + self.out_delay_queue = MulticastOutgoingQueue(self, _ONE_SECOND, _PROTECTED_AGGREGATION_DELAY) + + self.query_handler = QueryHandler(self) + self.record_manager = RecordManager(self) + + self._notify_futures: set[asyncio.Future] = set() + self.loop: asyncio.AbstractEventLoop | None = None + self._loop_thread: threading.Thread | None = None + + self.start() + + @property + def started(self) -> bool: + """Check if the instance has started.""" + running_future = self.engine.running_future + return bool( + not self.done + and running_future + and running_future.done() + and not running_future.cancelled() + and not running_future.exception() + and running_future.result() + ) + + def start(self) -> None: + """Start Zeroconf.""" + self.loop = None if self._use_asyncio is False else get_running_loop() + if self.loop: + self.engine.setup(self.loop, None) + return + self._start_thread() + + def _start_thread(self) -> None: + """Start a thread with a running event loop.""" + loop_thread_ready = threading.Event() + + def _run_loop() -> None: + self.loop = asyncio.new_event_loop() + asyncio.set_event_loop(self.loop) + self.engine.setup(self.loop, loop_thread_ready) + self.loop.run_forever() + + self._loop_thread = threading.Thread(target=_run_loop, daemon=True) + self._loop_thread.start() + loop_thread_ready.wait() + + async def async_wait_for_start(self, timeout: float = _STARTUP_TIMEOUT) -> None: + """Wait for start up for actions that require a running Zeroconf instance. + + Throws NotRunningException if the instance is not running or could + not be started. + """ + if self.done: # If the instance was shutdown from under us, raise immediately + raise NotRunningException + assert self.engine.running_future is not None + await wait_future_or_timeout(self.engine.running_future, timeout=timeout) + if not self.started: + raise NotRunningException + + @property + def listeners(self) -> set[RecordUpdateListener]: + return self.record_manager.listeners + + async def async_wait(self, timeout: float) -> None: + """Calling task waits for a given number of milliseconds or until notified.""" + loop = self.loop + assert loop is not None + await wait_for_future_set_or_timeout(loop, self._notify_futures, timeout) + + def notify_all(self) -> None: + """Notifies all waiting threads and notify listeners.""" + assert self.loop is not None + self.loop.call_soon_threadsafe(self.async_notify_all) + + def async_notify_all(self) -> None: + """Schedule an async_notify_all.""" + notify_futures = self._notify_futures + if notify_futures: + _resolve_all_futures_to_none(notify_futures) + + def get_service_info( + self, + type_: str, + name: str, + timeout: int = 3000, + question_type: DNSQuestionType | None = None, + ) -> ServiceInfo | None: + """Returns network's service information for a particular + name and type, or None if no service matches by the timeout, + which defaults to 3 seconds. + + :param type_: fully qualified service type name + :param name: the name of the service + :param timeout: milliseconds to wait for a response + :param question_type: The type of questions to ask (DNSQuestionType.QM or DNSQuestionType.QU) + """ + info = ServiceInfo(type_, name) + if info.request(self, timeout, question_type): + return info + return None + + def add_service_listener(self, type_: str, listener: ServiceListener) -> None: + """Adds a listener for a particular service type. This object + will then have its add_service and remove_service methods called when + services of that type become available and unavailable.""" + self.remove_service_listener(listener) + self.browsers[listener] = ServiceBrowser(self, type_, listener) + + def remove_service_listener(self, listener: ServiceListener) -> None: + """Removes a listener from the set that is currently listening.""" + if listener in self.browsers: + self.browsers[listener].cancel() + del self.browsers[listener] + + def remove_all_service_listeners(self) -> None: + """Removes a listener from the set that is currently listening.""" + for listener in list(self.browsers): + self.remove_service_listener(listener) + + def register_service( + self, + info: ServiceInfo, + ttl: int | None = None, + allow_name_change: bool = False, + cooperating_responders: bool = False, + strict: bool = True, + ) -> None: + """Registers service information to the network with a default TTL. + Zeroconf will then respond to requests for information for that + service. The name of the service may be changed if needed to make + it unique on the network. Additionally multiple cooperating responders + can register the same service on the network for resilience + (if you want this behavior set `cooperating_responders` to `True`). + + While it is not expected during normal operation, + this function may raise EventLoopBlocked if the underlying + call to `register_service` cannot be completed. + """ + assert self.loop is not None + run_coro_with_timeout( + await_awaitable( + self.async_register_service(info, ttl, allow_name_change, cooperating_responders, strict) + ), + self.loop, + _REGISTER_TIME * _REGISTER_BROADCASTS, + ) + + async def async_register_service( + self, + info: ServiceInfo, + ttl: int | None = None, + allow_name_change: bool = False, + cooperating_responders: bool = False, + strict: bool = True, + ) -> Awaitable: + """Registers service information to the network with a default TTL. + Zeroconf will then respond to requests for information for that + service. The name of the service may be changed if needed to make + it unique on the network. Additionally multiple cooperating responders + can register the same service on the network for resilience + (if you want this behavior set `cooperating_responders` to `True`).""" + if ttl is not None: + # ttl argument is used to maintain backward compatibility + # Setting TTLs via ServiceInfo is preferred + info.host_ttl = ttl + info.other_ttl = ttl + + info.set_server_if_missing() + await self.async_wait_for_start() + await self.async_check_service(info, allow_name_change, cooperating_responders, strict) + self.registry.async_add(info) + return asyncio.ensure_future(self._async_broadcast_service(info, _REGISTER_TIME, None)) + + def update_service(self, info: ServiceInfo) -> None: + """Registers service information to the network with a default TTL. + Zeroconf will then respond to requests for information for that + service. + + While it is not expected during normal operation, + this function may raise EventLoopBlocked if the underlying + call to `async_update_service` cannot be completed. + """ + assert self.loop is not None + run_coro_with_timeout( + await_awaitable(self.async_update_service(info)), + self.loop, + _REGISTER_TIME * _REGISTER_BROADCASTS, + ) + + async def async_update_service(self, info: ServiceInfo) -> Awaitable: + """Registers service information to the network with a default TTL. + Zeroconf will then respond to requests for information for that + service.""" + self.registry.async_update(info) + return asyncio.ensure_future(self._async_broadcast_service(info, _REGISTER_TIME, None)) + + async def async_get_service_info( + self, + type_: str, + name: str, + timeout: int = 3000, + question_type: DNSQuestionType | None = None, + ) -> AsyncServiceInfo | None: + """Returns network's service information for a particular + name and type, or None if no service matches by the timeout, + which defaults to 3 seconds. + + :param type_: fully qualified service type name + :param name: the name of the service + :param timeout: milliseconds to wait for a response + :param question_type: The type of questions to ask (DNSQuestionType.QM or DNSQuestionType.QU) + """ + info = AsyncServiceInfo(type_, name) + if await info.async_request(self, timeout, question_type): + return info + return None + + async def _async_broadcast_service( + self, + info: ServiceInfo, + interval: int, + ttl: int | None, + broadcast_addresses: bool = True, + ) -> None: + """Send a broadcasts to announce a service at intervals.""" + for i in range(_REGISTER_BROADCASTS): + if i != 0: + await asyncio.sleep(millis_to_seconds(interval)) + self.async_send(self.generate_service_broadcast(info, ttl, broadcast_addresses)) + + def generate_service_broadcast( + self, + info: ServiceInfo, + ttl: int | None, + broadcast_addresses: bool = True, + ) -> DNSOutgoing: + """Generate a broadcast to announce a service.""" + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + self._add_broadcast_answer(out, info, ttl, broadcast_addresses) + return out + + def generate_service_query(self, info: ServiceInfo) -> DNSOutgoing: # pylint: disable=no-self-use + """Generate a query to lookup a service.""" + out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) + # https://datatracker.ietf.org/doc/html/rfc6762#section-8.1 + # Because of the mDNS multicast rate-limiting + # rules, the probes SHOULD be sent as "QU" questions with the unicast- + # response bit set, to allow a defending host to respond immediately + # via unicast, instead of potentially having to wait before replying + # via multicast. + # + # _CLASS_UNIQUE is the "QU" bit + out.add_question(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN | _CLASS_UNIQUE)) + out.add_authorative_answer(info.dns_pointer()) + return out + + def _add_broadcast_answer( # pylint: disable=no-self-use + self, + out: DNSOutgoing, + info: ServiceInfo, + override_ttl: int | None, + broadcast_addresses: bool = True, + ) -> None: + """Add answers to broadcast a service.""" + current_time_millis() + other_ttl = None if override_ttl is None else override_ttl + host_ttl = None if override_ttl is None else override_ttl + out.add_answer_at_time(info.dns_pointer(override_ttl=other_ttl), 0) + out.add_answer_at_time(info.dns_service(override_ttl=host_ttl), 0) + out.add_answer_at_time(info.dns_text(override_ttl=other_ttl), 0) + if broadcast_addresses: + for record in info.get_address_and_nsec_records(override_ttl=host_ttl): + out.add_answer_at_time(record, 0) + + def unregister_service(self, info: ServiceInfo) -> None: + """Unregister a service. + + While it is not expected during normal operation, + this function may raise EventLoopBlocked if the underlying + call to `async_unregister_service` cannot be completed. + """ + assert self.loop is not None + run_coro_with_timeout( + self.async_unregister_service(info), + self.loop, + _UNREGISTER_TIME * _REGISTER_BROADCASTS, + ) + + async def async_unregister_service(self, info: ServiceInfo) -> Awaitable: + """Unregister a service.""" + info.set_server_if_missing() + self.registry.async_remove(info) + # If another server uses the same addresses, we do not want to send + # goodbye packets for the address records + + assert info.server_key is not None + entries = self.registry.async_get_infos_server(info.server_key) + broadcast_addresses = not bool(entries) + return asyncio.ensure_future( + self._async_broadcast_service(info, _UNREGISTER_TIME, 0, broadcast_addresses) + ) + + def generate_unregister_all_services(self) -> DNSOutgoing | None: + """Generate a DNSOutgoing goodbye for all services and remove them from the registry.""" + service_infos = self.registry.async_get_service_infos() + if not service_infos: + return None + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + for info in service_infos: + self._add_broadcast_answer(out, info, 0) + self.registry.async_remove(service_infos) + return out + + async def async_unregister_all_services(self) -> None: + """Unregister all registered services. + + Unlike async_register_service and async_unregister_service, this + method does not return a future and is always expected to be + awaited since its only called at shutdown. + """ + # Send Goodbye packets https://datatracker.ietf.org/doc/html/rfc6762#section-10.1 + out = self.generate_unregister_all_services() + if not out: + return + for i in range(_REGISTER_BROADCASTS): + if i != 0: + await asyncio.sleep(millis_to_seconds(_UNREGISTER_TIME)) + self.async_send(out) + + def unregister_all_services(self) -> None: + """Unregister all registered services. + + While it is not expected during normal operation, + this function may raise EventLoopBlocked if the underlying + call to `async_unregister_all_services` cannot be completed. + """ + assert self.loop is not None + run_coro_with_timeout( + self.async_unregister_all_services(), + self.loop, + _UNREGISTER_TIME * _REGISTER_BROADCASTS, + ) + + async def async_check_service( + self, + info: ServiceInfo, + allow_name_change: bool, + cooperating_responders: bool = False, + strict: bool = True, + ) -> None: + """Checks the network for a unique service name, modifying the + ServiceInfo passed in if it is not unique.""" + instance_name = instance_name_from_service_info(info, strict=strict) + if cooperating_responders: + return + + # Wait a random amount of time up avoid collisions and avoid + # a thundering herd when multiple services are started on the network + await self.async_wait(random.randint(*_PROBE_RANDOM_DELAY_INTERVAL)) # noqa: S311 + + next_instance_number = 2 + next_time = now = current_time_millis() + i = 0 + while i < _REGISTER_BROADCASTS: + # check for a name conflict + while self.cache.current_entry_with_name_and_alias(info.type, info.name): + if not allow_name_change: + raise NonUniqueNameException + + # change the name and look for a conflict + info.name = f"{instance_name}-{next_instance_number}.{info.type}" + next_instance_number += 1 + service_type_name(info.name, strict=strict) + next_time = now + i = 0 + + if now < next_time: + await self.async_wait(next_time - now) + now = current_time_millis() + continue + + self.async_send(self.generate_service_query(info)) + i += 1 + next_time += _CHECK_TIME + + def add_listener( + self, + listener: RecordUpdateListener, + question: DNSQuestion | list[DNSQuestion] | None, + ) -> None: + """Adds a listener for a given question. The listener will have + its update_record method called when information is available to + answer the question(s). + + This function is threadsafe + """ + assert self.loop is not None + self.loop.call_soon_threadsafe(self.record_manager.async_add_listener, listener, question) + + def remove_listener(self, listener: RecordUpdateListener) -> None: + """Removes a listener. + + This function is threadsafe + """ + assert self.loop is not None + self.loop.call_soon_threadsafe(self.record_manager.async_remove_listener, listener) + + def async_add_listener( + self, + listener: RecordUpdateListener, + question: DNSQuestion | list[DNSQuestion] | None, + ) -> None: + """Adds a listener for a given question. The listener will have + its update_record method called when information is available to + answer the question(s). + + This function is not threadsafe and must be called in the eventloop. + """ + self.record_manager.async_add_listener(listener, question) + + def async_remove_listener(self, listener: RecordUpdateListener) -> None: + """Removes a listener. + + This function is not threadsafe and must be called in the eventloop. + """ + self.record_manager.async_remove_listener(listener) + + def send( + self, + out: DNSOutgoing, + addr: str | None = None, + port: int = _MDNS_PORT, + v6_flow_scope: tuple[()] | tuple[int, int] = (), + transport: _WrappedTransport | None = None, + ) -> None: + """Sends an outgoing packet threadsafe.""" + assert self.loop is not None + self.loop.call_soon_threadsafe(self.async_send, out, addr, port, v6_flow_scope, transport) + + def async_send( + self, + out: DNSOutgoing, + addr: str | None = None, + port: int = _MDNS_PORT, + v6_flow_scope: tuple[()] | tuple[int, int] = (), + transport: _WrappedTransport | None = None, + ) -> None: + """Sends an outgoing packet.""" + if self.done: + return + + # If no transport is specified, we send to all the ones + # with the same address family + transports = [transport] if transport else self.engine.senders + log_debug = log.isEnabledFor(logging.DEBUG) + + for packet_num, packet in enumerate(out.packets()): + if len(packet) > _MAX_MSG_ABSOLUTE: + self.log_warning_once( + "Dropping %r over-sized packet (%d bytes) %r", + out, + len(packet), + packet, + ) + return + for send_transport in transports: + async_send_with_transport( + log_debug, + send_transport, + packet, + packet_num, + out, + addr, + port, + v6_flow_scope, + ) + + def _close(self) -> None: + """Set global done and remove all service listeners.""" + if self.done: + return + self.remove_all_service_listeners() + self.done = True + + def _shutdown_threads(self) -> None: + """Shutdown any threads.""" + assert self.loop is not None + if self.loop.is_closed(): + # close() is documented as idempotent — a second call after the + # loop has been torn down must be a no-op rather than raising. + return + self.notify_all() + if not self._loop_thread: + return + shutdown_loop(self.loop) + self._loop_thread.join() + self._loop_thread = None + # The loop's selector (epoll FD on Linux) and self-pipe sockets stay + # open until loop.close() is called. We own this loop because + # _start_thread() created it, so close it here to avoid leaking + # those file descriptors across Zeroconf() construct/close cycles. + self.loop.close() + + def close(self) -> None: + """Ends the background threads, and prevent this instance from + servicing further queries. + + This method is idempotent and irreversible. + """ + assert self.loop is not None + if self.loop.is_running(): + if self.loop == get_running_loop(): + log.warning( + "unregister_all_services skipped as it does blocking i/o; use AsyncZeroconf with asyncio" + ) + else: + self.unregister_all_services() + self._close() + self.engine.close() + self._shutdown_threads() + + async def _async_close(self) -> None: + """Ends the background threads, and prevent this instance from + servicing further queries. + + This method is idempotent and irreversible. + + This call only intended to be used by AsyncZeroconf + + Callers are responsible for unregistering all services + before calling this function + """ + self._close() + await self.engine._async_close() # pylint: disable=protected-access + self._shutdown_threads() + + def __enter__(self) -> Zeroconf: + return self + + def __exit__( # pylint: disable=useless-return + self, + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> bool | None: + self.close() + return None diff --git a/src/zeroconf/_dns.pxd b/src/zeroconf/_dns.pxd new file mode 100644 index 000000000..7ef1dbec9 --- /dev/null +++ b/src/zeroconf/_dns.pxd @@ -0,0 +1,160 @@ + +import cython + +from ._protocol.outgoing cimport DNSOutgoing + + +cdef cython.uint _LEN_BYTE +cdef cython.uint _LEN_SHORT +cdef cython.uint _LEN_INT + +cdef cython.uint _NAME_COMPRESSION_MIN_SIZE +cdef cython.uint _BASE_MAX_SIZE + +cdef cython.uint _EXPIRE_FULL_TIME_MS +cdef cython.uint _EXPIRE_STALE_TIME_MS +cdef cython.uint _RECENT_TIME_MS + +cdef cython.uint _TYPE_ANY + +cdef cython.uint _CLASS_UNIQUE +cdef cython.uint _CLASS_MASK + +cdef object current_time_millis + +cdef class DNSEntry: + + cdef public str key + cdef public str name + cdef public cython.uint type + cdef public cython.uint class_ + cdef public bint unique + + cdef _fast_init_entry(self, str name, cython.uint type_, cython.uint class_) + + cdef bint _dns_entry_matches(self, DNSEntry other) + +cdef class DNSQuestion(DNSEntry): + + cdef public cython.int _hash + + cdef _fast_init(self, str name, cython.uint type_, cython.uint class_) + + cpdef bint answered_by(self, DNSRecord rec) + +cdef class DNSRecord(DNSEntry): + + cdef public unsigned int ttl + cdef public double created + + cdef _fast_init_record(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, double created) + + cdef bint _suppressed_by_answer(self, DNSRecord answer) + + @cython.locals( + answers=cython.list, + ) + cpdef bint suppressed_by(self, object msg) + + cpdef get_remaining_ttl(self, double now) + + cpdef double get_expiration_time(self, cython.uint percent) + + cpdef bint is_expired(self, double now) + + cpdef bint is_stale(self, double now) + + cpdef bint is_recent(self, double now) + + cdef _set_created_ttl(self, double now, unsigned int ttl) + +cdef class DNSAddress(DNSRecord): + + cdef public cython.int _hash + cdef public bytes address + cdef public object scope_id + + cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, bytes address, object scope_id, double created) + + cdef bint _eq(self, DNSAddress other) + + cpdef write(self, DNSOutgoing out) + + +cdef class DNSHinfo(DNSRecord): + + cdef public cython.int _hash + cdef public str cpu + cdef public str os + + cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, str cpu, str os, double created) + + cdef bint _eq(self, DNSHinfo other) + + cpdef write(self, DNSOutgoing out) + +cdef class DNSPointer(DNSRecord): + + cdef public cython.int _hash + cdef public str alias + cdef public str alias_key + + cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, str alias, double created) + + cdef bint _eq(self, DNSPointer other) + + cpdef write(self, DNSOutgoing out) + +cdef class DNSText(DNSRecord): + + cdef public cython.int _hash + cdef public bytes text + + cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, bytes text, double created) + + cdef bint _eq(self, DNSText other) + + cpdef write(self, DNSOutgoing out) + +cdef class DNSService(DNSRecord): + + cdef public cython.int _hash + cdef public cython.uint priority + cdef public cython.uint weight + cdef public cython.uint port + cdef public str server + cdef public str server_key + + cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, cython.uint priority, cython.uint weight, cython.uint port, str server, double created) + + cdef bint _eq(self, DNSService other) + + cpdef write(self, DNSOutgoing out) + +cdef class DNSNsec(DNSRecord): + + cdef public cython.int _hash + cdef public str next_name + cdef public cython.list rdtypes + + cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, str next_name, cython.list rdtypes, double created) + + cdef bint _eq(self, DNSNsec other) + + cpdef write(self, DNSOutgoing out) + +cdef class DNSRRSet: + + cdef cython.list _records + cdef cython.dict _lookup + + @cython.locals(other=DNSRecord) + cpdef bint suppresses(self, DNSRecord record) + + @cython.locals( + record=DNSRecord, + record_sets=cython.list, + ) + cdef cython.dict _get_lookup(self) + + cpdef cython.set lookup_set(self) diff --git a/src/zeroconf/_dns.py b/src/zeroconf/_dns.py new file mode 100644 index 000000000..93069eb3a --- /dev/null +++ b/src/zeroconf/_dns.py @@ -0,0 +1,642 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import enum +import socket +from typing import TYPE_CHECKING, Any, cast + +from ._exceptions import AbstractMethodException +from ._utils.net import _is_v6_address +from ._utils.time import current_time_millis +from .const import _CLASS_MASK, _CLASS_UNIQUE, _CLASSES, _TYPE_ANY, _TYPES + +_LEN_BYTE = 1 +_LEN_SHORT = 2 +_LEN_INT = 4 + +_BASE_MAX_SIZE = _LEN_SHORT + _LEN_SHORT + _LEN_INT + _LEN_SHORT # type # class # ttl # length +_NAME_COMPRESSION_MIN_SIZE = _LEN_BYTE * 2 + +_EXPIRE_FULL_TIME_MS = 1000 +_EXPIRE_STALE_TIME_MS = 500 +_RECENT_TIME_MS = 250 + +_float = float +_int = int + +if TYPE_CHECKING: + from ._protocol.incoming import DNSIncoming + from ._protocol.outgoing import DNSOutgoing + + +@enum.unique +class DNSQuestionType(enum.Enum): + """An MDNS question type. + + "QU" - questions requesting unicast responses + "QM" - questions requesting multicast responses + https://datatracker.ietf.org/doc/html/rfc6762#section-5.4 + """ + + QU = 1 + QM = 2 + + +class DNSEntry: # noqa: PLW1641 + """A DNS entry""" + + __slots__ = ("class_", "key", "name", "type", "unique") + + def __init__(self, name: str, type_: int, class_: int) -> None: + self._fast_init_entry(name, type_, class_) + + def _fast_init_entry(self, name: str, type_: _int, class_: _int) -> None: + """Fast init for reuse.""" + self.name = name + self.key = name.lower() + self.type = type_ + self.class_ = class_ & _CLASS_MASK + self.unique = (class_ & _CLASS_UNIQUE) != 0 + + def _dns_entry_matches(self, other: DNSEntry) -> bool: + return self.key == other.key and self.type == other.type and self.class_ == other.class_ + + def __eq__(self, other: Any) -> bool: + """Equality test on key (lowercase name), type, and class""" + return isinstance(other, DNSEntry) and self._dns_entry_matches(other) + + @staticmethod + def get_class_(class_: int) -> str: + """Class accessor""" + return _CLASSES.get(class_, f"?({class_})") + + @staticmethod + def get_type(t: int) -> str: + """Type accessor""" + return _TYPES.get(t, f"?({t})") + + def entry_to_string(self, hdr: str, other: bytes | str | None) -> str: + """String representation with additional information""" + return "{}[{},{}{},{}]{}".format( + hdr, + self.get_type(self.type), + self.get_class_(self.class_), + "-unique" if self.unique else "", + self.name, + f"={cast(Any, other)}" if other is not None else "", + ) + + +class DNSQuestion(DNSEntry): + """A DNS question entry""" + + __slots__ = ("_hash",) + + def __init__(self, name: str, type_: int, class_: int) -> None: + self._fast_init(name, type_, class_) + + def _fast_init(self, name: str, type_: _int, class_: _int) -> None: + """Fast init for reuse.""" + self._fast_init_entry(name, type_, class_) + self._hash = hash((self.key, type_, self.class_)) + + def answered_by(self, rec: DNSRecord) -> bool: + """Returns true if the question is answered by the record""" + return self.class_ == rec.class_ and self.type in (rec.type, _TYPE_ANY) and self.name == rec.name + + def __hash__(self) -> int: + return self._hash + + def __eq__(self, other: Any) -> bool: + """Tests equality on dns question.""" + return isinstance(other, DNSQuestion) and self._dns_entry_matches(other) + + @property + def max_size(self) -> int: + """Maximum size of the question in the packet.""" + return len(self.name.encode("utf-8")) + _LEN_BYTE + _LEN_SHORT + _LEN_SHORT + + @property + def unicast(self) -> bool: + """Returns true if the QU (not QM) is set. + + unique shares the same mask as the one + used for unicast. + """ + return self.unique + + @unicast.setter + def unicast(self, value: bool) -> None: + """Sets the QU bit (not QM).""" + self.unique = value + + def __repr__(self) -> str: + """String representation""" + return "{}[question,{},{},{}]".format( + self.get_type(self.type), + "QU" if self.unicast else "QM", + self.get_class_(self.class_), + self.name, + ) + + +class DNSRecord(DNSEntry): # noqa: PLW1641 + """A DNS record - like a DNS entry, but has a TTL""" + + __slots__ = ("created", "ttl") + + def __init__( + self, + name: str, + type_: int, + class_: int, + ttl: _int, + created: float | None = None, + ) -> None: + self._fast_init_record(name, type_, class_, ttl, created or current_time_millis()) + + def _fast_init_record(self, name: str, type_: _int, class_: _int, ttl: _int, created: _float) -> None: + """Fast init for reuse.""" + self._fast_init_entry(name, type_, class_) + self.ttl = ttl + self.created = created + + def __eq__(self, other: Any) -> bool: # pylint: disable=no-self-use + """Abstract method""" + raise AbstractMethodException + + def __lt__(self, other: DNSRecord) -> bool: + return self.ttl < other.ttl + + def suppressed_by(self, msg: DNSIncoming) -> bool: + """Returns true if any answer in a message can suffice for the + information held in this record.""" + answers = msg.answers() + for record in answers: + if self._suppressed_by_answer(record): + return True + return False + + def _suppressed_by_answer(self, other: DNSRecord) -> bool: + """Returns true if another record has same name, type and class, + and if its TTL is at least half of this record's.""" + return self == other and other.ttl > (self.ttl / 2) + + def get_expiration_time(self, percent: _int) -> float: + """Returns the time at which this record will have expired + by a certain percentage.""" + return self.created + (percent * self.ttl * 10) + + # TODO: Switch to just int here + def get_remaining_ttl(self, now: _float) -> int | float: + """Returns the remaining TTL in seconds.""" + remain = (self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) - now) / 1000.0 + return 0 if remain < 0 else remain + + def is_expired(self, now: _float) -> bool: + """Returns true if this record has expired.""" + return self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) <= now + + def is_stale(self, now: _float) -> bool: + """Returns true if this record is at least half way expired.""" + return self.created + (_EXPIRE_STALE_TIME_MS * self.ttl) <= now + + def is_recent(self, now: _float) -> bool: + """Returns true if the record more than one quarter of its TTL remaining.""" + return self.created + (_RECENT_TIME_MS * self.ttl) > now + + def _set_created_ttl(self, created: _float, ttl: _int) -> None: + """Set the created and ttl of a record.""" + # It would be better if we made a copy instead of mutating the record + # in place, but records currently don't have a copy method. + self.created = created + self.ttl = ttl + + def write(self, out: DNSOutgoing) -> None: # pylint: disable=no-self-use + """Abstract method""" + raise AbstractMethodException + + def to_string(self, other: bytes | str) -> str: + """String representation with additional information""" + arg = f"{self.ttl}/{int(self.get_remaining_ttl(current_time_millis()))},{cast(Any, other)}" + return DNSEntry.entry_to_string(self, "record", arg) + + +class DNSAddress(DNSRecord): + """A DNS address record""" + + __slots__ = ("_hash", "address", "scope_id") + + def __init__( + self, + name: str, + type_: int, + class_: int, + ttl: int, + address: bytes, + scope_id: int | None = None, + created: float | None = None, + ) -> None: + self._fast_init(name, type_, class_, ttl, address, scope_id, created or current_time_millis()) + + def _fast_init( + self, + name: str, + type_: _int, + class_: _int, + ttl: _int, + address: bytes, + scope_id: _int | None, + created: _float, + ) -> None: + """Fast init for reuse.""" + self._fast_init_record(name, type_, class_, ttl, created) + self.address = address + self.scope_id = scope_id + self._hash = hash((self.key, type_, self.class_, address, scope_id)) + + def write(self, out: DNSOutgoing) -> None: + """Used in constructing an outgoing packet""" + out.write_string(self.address) + + def __eq__(self, other: Any) -> bool: + """Tests equality on address""" + return isinstance(other, DNSAddress) and self._eq(other) + + def _eq(self, other: DNSAddress) -> bool: + return ( + self.address == other.address + and self.scope_id == other.scope_id + and self._dns_entry_matches(other) + ) + + def __hash__(self) -> int: + """Hash to compare like DNSAddresses.""" + return self._hash + + def __repr__(self) -> str: + """String representation""" + try: + return self.to_string( + socket.inet_ntop( + socket.AF_INET6 if _is_v6_address(self.address) else socket.AF_INET, + self.address, + ) + ) + except (ValueError, OSError): + return self.to_string(str(self.address)) + + +class DNSHinfo(DNSRecord): + """A DNS host information record""" + + __slots__ = ("_hash", "cpu", "os") + + def __init__( + self, + name: str, + type_: int, + class_: int, + ttl: int, + cpu: str, + os: str, + created: float | None = None, + ) -> None: + self._fast_init(name, type_, class_, ttl, cpu, os, created or current_time_millis()) + + def _fast_init( + self, name: str, type_: _int, class_: _int, ttl: _int, cpu: str, os: str, created: _float + ) -> None: + """Fast init for reuse.""" + self._fast_init_record(name, type_, class_, ttl, created) + self.cpu = cpu + self.os = os + self._hash = hash((self.key, type_, self.class_, cpu, os)) + + def write(self, out: DNSOutgoing) -> None: + """Used in constructing an outgoing packet""" + out.write_character_string(self.cpu.encode("utf-8")) + out.write_character_string(self.os.encode("utf-8")) + + def __eq__(self, other: Any) -> bool: + """Tests equality on cpu and os.""" + return isinstance(other, DNSHinfo) and self._eq(other) + + def _eq(self, other: DNSHinfo) -> bool: + """Tests equality on cpu and os.""" + return self.cpu == other.cpu and self.os == other.os and self._dns_entry_matches(other) + + def __hash__(self) -> int: + """Hash to compare like DNSHinfo.""" + return self._hash + + def __repr__(self) -> str: + """String representation""" + return self.to_string(self.cpu + " " + self.os) + + +class DNSPointer(DNSRecord): + """A DNS pointer record""" + + __slots__ = ("_hash", "alias", "alias_key") + + def __init__( + self, + name: str, + type_: int, + class_: int, + ttl: int, + alias: str, + created: float | None = None, + ) -> None: + self._fast_init(name, type_, class_, ttl, alias, created or current_time_millis()) + + def _fast_init( + self, name: str, type_: _int, class_: _int, ttl: _int, alias: str, created: _float + ) -> None: + self._fast_init_record(name, type_, class_, ttl, created) + self.alias = alias + self.alias_key = alias.lower() + self._hash = hash((self.key, type_, self.class_, self.alias_key)) + + @property + def max_size_compressed(self) -> int: + """Maximum size of the record in the packet assuming the name has been compressed.""" + return ( + _BASE_MAX_SIZE + + _NAME_COMPRESSION_MIN_SIZE + + (len(self.alias) - len(self.name)) + + _NAME_COMPRESSION_MIN_SIZE + ) + + def write(self, out: DNSOutgoing) -> None: + """Used in constructing an outgoing packet""" + out.write_name(self.alias) + + def __eq__(self, other: Any) -> bool: + """Tests equality on alias.""" + return isinstance(other, DNSPointer) and self._eq(other) + + def _eq(self, other: DNSPointer) -> bool: + """Tests equality on alias.""" + return self.alias_key == other.alias_key and self._dns_entry_matches(other) + + def __hash__(self) -> int: + """Hash to compare like DNSPointer.""" + return self._hash + + def __repr__(self) -> str: + """String representation""" + return self.to_string(self.alias) + + +class DNSText(DNSRecord): + """A DNS text record""" + + __slots__ = ("_hash", "text") + + def __init__( + self, + name: str, + type_: int, + class_: int, + ttl: int, + text: bytes, + created: float | None = None, + ) -> None: + self._fast_init(name, type_, class_, ttl, text, created or current_time_millis()) + + def _fast_init( + self, name: str, type_: _int, class_: _int, ttl: _int, text: bytes, created: _float + ) -> None: + self._fast_init_record(name, type_, class_, ttl, created) + self.text = text + self._hash = hash((self.key, type_, self.class_, text)) + + def write(self, out: DNSOutgoing) -> None: + """Used in constructing an outgoing packet""" + out.write_string(self.text) + + def __hash__(self) -> int: + """Hash to compare like DNSText.""" + return self._hash + + def __eq__(self, other: Any) -> bool: + """Tests equality on text.""" + return isinstance(other, DNSText) and self._eq(other) + + def _eq(self, other: DNSText) -> bool: + """Tests equality on text.""" + return self.text == other.text and self._dns_entry_matches(other) + + def __repr__(self) -> str: + """String representation""" + if len(self.text) > 10: + return self.to_string(self.text[:7]) + "..." + return self.to_string(self.text) + + +class DNSService(DNSRecord): + """A DNS service record""" + + __slots__ = ("_hash", "port", "priority", "server", "server_key", "weight") + + def __init__( + self, + name: str, + type_: int, + class_: int, + ttl: int, + priority: int, + weight: int, + port: int, + server: str, + created: float | None = None, + ) -> None: + self._fast_init( + name, type_, class_, ttl, priority, weight, port, server, created or current_time_millis() + ) + + def _fast_init( + self, + name: str, + type_: _int, + class_: _int, + ttl: _int, + priority: _int, + weight: _int, + port: _int, + server: str, + created: _float, + ) -> None: + self._fast_init_record(name, type_, class_, ttl, created) + self.priority = priority + self.weight = weight + self.port = port + self.server = server + self.server_key = server.lower() + self._hash = hash((self.key, type_, self.class_, priority, weight, port, self.server_key)) + + def write(self, out: DNSOutgoing) -> None: + """Used in constructing an outgoing packet""" + out.write_short(self.priority) + out.write_short(self.weight) + out.write_short(self.port) + out.write_name(self.server) + + def __eq__(self, other: Any) -> bool: + """Tests equality on priority, weight, port and server""" + return isinstance(other, DNSService) and self._eq(other) + + def _eq(self, other: DNSService) -> bool: + """Tests equality on priority, weight, port and server.""" + return ( + self.priority == other.priority + and self.weight == other.weight + and self.port == other.port + and self.server_key == other.server_key + and self._dns_entry_matches(other) + ) + + def __hash__(self) -> int: + """Hash to compare like DNSService.""" + return self._hash + + def __repr__(self) -> str: + """String representation""" + return self.to_string(f"{self.server}:{self.port}") + + +class DNSNsec(DNSRecord): + """A DNS NSEC record""" + + __slots__ = ("_hash", "next_name", "rdtypes") + + def __init__( + self, + name: str, + type_: int, + class_: int, + ttl: _int, + next_name: str, + rdtypes: list[int], + created: float | None = None, + ) -> None: + self._fast_init(name, type_, class_, ttl, next_name, rdtypes, created or current_time_millis()) + + def _fast_init( + self, + name: str, + type_: _int, + class_: _int, + ttl: _int, + next_name: str, + rdtypes: list[_int], + created: _float, + ) -> None: + self._fast_init_record(name, type_, class_, ttl, created) + self.next_name = next_name + self.rdtypes = sorted(rdtypes) + self._hash = hash((self.key, type_, self.class_, next_name, *self.rdtypes)) + + def write(self, out: DNSOutgoing) -> None: + """Used in constructing an outgoing packet.""" + bitmap = bytearray(b"\0" * 32) + total_octets = 0 + for rdtype in self.rdtypes: + if rdtype > 255: # mDNS only supports window 0 + raise ValueError(f"rdtype {rdtype} is too large for NSEC") + byte = rdtype // 8 + total_octets = byte + 1 + bitmap[byte] |= 0x80 >> (rdtype % 8) + if total_octets == 0: + # NSEC must have at least one rdtype + # Writing an empty bitmap is not allowed + raise ValueError("NSEC must have at least one rdtype") + out_bytes = bytes(bitmap[0:total_octets]) + out.write_name(self.next_name) + out._write_byte(0) # Always window 0 + out._write_byte(len(out_bytes)) + out.write_string(out_bytes) + + def __eq__(self, other: Any) -> bool: + """Tests equality on next_name and rdtypes.""" + return isinstance(other, DNSNsec) and self._eq(other) + + def _eq(self, other: DNSNsec) -> bool: + """Tests equality on next_name and rdtypes.""" + return ( + self.next_name == other.next_name + and self.rdtypes == other.rdtypes + and self._dns_entry_matches(other) + ) + + def __hash__(self) -> int: + """Hash to compare like DNSNSec.""" + return self._hash + + def __repr__(self) -> str: + """String representation""" + return self.to_string( + self.next_name + "," + "|".join([self.get_type(type_) for type_ in self.rdtypes]) + ) + + +_DNSRecord = DNSRecord + + +class DNSRRSet: + """A set of dns records with a lookup to get the ttl.""" + + __slots__ = ("_lookup", "_records") + + def __init__(self, records: list[DNSRecord]) -> None: + """Create an RRset from records sets.""" + self._records = records + self._lookup: dict[DNSRecord, DNSRecord] | None = None + + @property + def lookup(self) -> dict[DNSRecord, DNSRecord]: + """Return the lookup table.""" + return self._get_lookup() + + def lookup_set(self) -> set[DNSRecord]: + """Return the lookup table as aset.""" + return set(self._get_lookup()) + + def _get_lookup(self) -> dict[DNSRecord, DNSRecord]: + """Return the lookup table, building it if needed.""" + if self._lookup is None: + # Build the hash table so we can lookup the record ttl + self._lookup = {record: record for record in self._records} + return self._lookup + + def suppresses(self, record: _DNSRecord) -> bool: + """Returns true if any answer in the rrset can suffice for the + information held in this record.""" + lookup = self._get_lookup() + other = lookup.get(record) + if other is None: + return False + return other.ttl > (record.ttl / 2) diff --git a/src/zeroconf/_engine.py b/src/zeroconf/_engine.py new file mode 100644 index 000000000..0e1c01a15 --- /dev/null +++ b/src/zeroconf/_engine.py @@ -0,0 +1,195 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import itertools +import socket +import threading +from typing import TYPE_CHECKING, cast + +from ._record_update import RecordUpdate +from ._utils.asyncio import get_running_loop, run_coro_with_timeout +from ._utils.time import current_time_millis +from .const import _CACHE_CLEANUP_INTERVAL + +if TYPE_CHECKING: + from ._core import Zeroconf + + +from ._listener import AsyncListener +from ._transport import _WrappedTransport, make_wrapped_transport + +_CLOSE_TIMEOUT = 3000 # ms + + +class AsyncEngine: + """An engine wraps sockets in the event loop.""" + + __slots__ = ( + "_cleanup_timer", + "_listen_socket", + "_respond_sockets", + "_setup_task", + "loop", + "protocols", + "readers", + "running_future", + "senders", + "zc", + ) + + def __init__( + self, + zeroconf: Zeroconf, + listen_socket: socket.socket | None, + respond_sockets: list[socket.socket], + ) -> None: + self.loop: asyncio.AbstractEventLoop | None = None + self.zc = zeroconf + self.protocols: list[AsyncListener] = [] + self.readers: list[_WrappedTransport] = [] + self.senders: list[_WrappedTransport] = [] + self.running_future: asyncio.Future[bool | None] | None = None + self._listen_socket = listen_socket + self._respond_sockets = respond_sockets + self._cleanup_timer: asyncio.TimerHandle | None = None + self._setup_task: asyncio.Task[None] | None = None + + def setup( + self, + loop: asyncio.AbstractEventLoop, + loop_thread_ready: threading.Event | None, + ) -> None: + """Set up the instance.""" + self.loop = loop + self.running_future = loop.create_future() + self._setup_task = self.loop.create_task(self._async_setup(loop_thread_ready)) + + async def _async_setup(self, loop_thread_ready: threading.Event | None) -> None: + """Set up the instance.""" + self._async_schedule_next_cache_cleanup() + await self._async_create_endpoints() + assert self.running_future is not None + if not self.running_future.done(): + self.running_future.set_result(True) + if loop_thread_ready: + loop_thread_ready.set() + + async def _async_create_endpoints(self) -> None: + """Create endpoints to send and receive.""" + assert self.loop is not None + loop = self.loop + reader_sockets = [] + sender_sockets = [] + if self._listen_socket: + reader_sockets.append(self._listen_socket) + for s in self._respond_sockets: + if s not in reader_sockets: + reader_sockets.append(s) + sender_sockets.append(s) + + for s in reader_sockets: + transport, protocol = await loop.create_datagram_endpoint( # type: ignore[type-var] + lambda: AsyncListener(self.zc), # type: ignore[arg-type, return-value] + sock=s, + ) + # Register the wrapped transport before releasing the engine's + # handle so a concurrent shutdown always sees ``s`` in exactly + # one place; do not add an ``await`` between these two steps. + self.protocols.append(cast(AsyncListener, protocol)) + self.readers.append(make_wrapped_transport(cast(asyncio.DatagramTransport, transport))) + if s in sender_sockets: + self.senders.append(make_wrapped_transport(cast(asyncio.DatagramTransport, transport))) + if s is self._listen_socket: + self._listen_socket = None + if s in self._respond_sockets: + self._respond_sockets.remove(s) + + def _async_cache_cleanup(self) -> None: + """Periodic cache cleanup.""" + now = current_time_millis() + self.zc.question_history.async_expire(now) + self.zc.record_manager.async_updates( + now, + [RecordUpdate(record, record) for record in self.zc.cache.async_expire(now)], + ) + self.zc.record_manager.async_updates_complete(False) + self._async_schedule_next_cache_cleanup() + + def _async_schedule_next_cache_cleanup(self) -> None: + """Schedule the next cache cleanup.""" + loop = self.loop + assert loop is not None + self._cleanup_timer = loop.call_at(loop.time() + _CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup) + + async def _async_close(self) -> None: + """Cancel and wait for the cleanup task to finish.""" + assert self._setup_task is not None + # Swallow CancelledError only if the setup task itself was + # cancelled (close-before-start); outer-task cancellation must + # propagate. + try: + await self._setup_task + except asyncio.CancelledError: + if not self._setup_task.cancelled(): + raise + self._async_shutdown() + await asyncio.sleep(0) # flush out any call soons + if self._cleanup_timer is not None: + self._cleanup_timer.cancel() + + def _async_shutdown(self) -> None: + """Shutdown transports and sockets; safe to call repeatedly.""" + assert self.running_future is not None + assert self.loop is not None + self.running_future = self.loop.create_future() + # Cancel pending setup so it can't wrap fresh transports after + # shutdown has started. + if self._setup_task is not None and not self._setup_task.done(): + self._setup_task.cancel() + for wrapped_transport in itertools.chain(self.senders, self.readers): + wrapped_transport.transport.close() + # Anything still here was never adopted by a transport. + if self._listen_socket is not None: + self._listen_socket.close() + self._listen_socket = None + for s in self._respond_sockets: + s.close() + self._respond_sockets = [] + + def close(self) -> None: + """Close from sync context. + + While it is not expected during normal operation, + this function may raise EventLoopBlocked if the underlying + call to `_async_close` cannot be completed. + """ + assert self.loop is not None + # Guard against Zeroconf.close() being called from the eventloop + if get_running_loop() == self.loop: + self._async_shutdown() + return + if not self.loop.is_running(): + return + run_coro_with_timeout(self._async_close(), self.loop, _CLOSE_TIMEOUT) diff --git a/src/zeroconf/_exceptions.py b/src/zeroconf/_exceptions.py new file mode 100644 index 000000000..5fc812593 --- /dev/null +++ b/src/zeroconf/_exceptions.py @@ -0,0 +1,69 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + + +class Error(Exception): + """Base class for all zeroconf exceptions.""" + + +class IncomingDecodeError(Error): + """Exception when there is invalid data in an incoming packet.""" + + +class NonUniqueNameException(Error): + """Exception when the name is already registered.""" + + +class NamePartTooLongException(Error): + """Exception when the name is too long.""" + + +class AbstractMethodException(Error): + """Exception when a required method is not implemented.""" + + +class BadTypeInNameException(Error): + """Exception when the type in a name is invalid.""" + + +class ServiceNameAlreadyRegistered(Error): + """Exception when a service name is already registered.""" + + +class EventLoopBlocked(Error): + """Exception when the event loop is blocked. + + This exception is never expected to be thrown + during normal operation. It should only happen + when the cpu is maxed out or there is something blocking + the event loop. + """ + + +class NotRunningException(Error): + """Exception when an action is called with a zeroconf instance that is not running. + + The instance may not be running because it was already shutdown + or startup has failed in some unexpected way. + """ diff --git a/src/zeroconf/_handlers/__init__.py b/src/zeroconf/_handlers/__init__.py new file mode 100644 index 000000000..584a74eca --- /dev/null +++ b/src/zeroconf/_handlers/__init__.py @@ -0,0 +1,23 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations diff --git a/src/zeroconf/_handlers/answers.pxd b/src/zeroconf/_handlers/answers.pxd new file mode 100644 index 000000000..759905f27 --- /dev/null +++ b/src/zeroconf/_handlers/answers.pxd @@ -0,0 +1,34 @@ + +import cython + +from .._dns cimport DNSRecord +from .._protocol.outgoing cimport DNSOutgoing + + +cdef class QuestionAnswers: + + cdef public dict ucast + cdef public dict mcast_now + cdef public dict mcast_aggregate + cdef public dict mcast_aggregate_last_second + + +cdef class AnswerGroup: + + cdef public double send_after + cdef public double send_before + cdef public cython.dict answers + + +cdef object _FLAGS_QR_RESPONSE_AA +cdef object NAME_GETTER + +cpdef DNSOutgoing construct_outgoing_multicast_answers(cython.dict answers) + +cpdef DNSOutgoing construct_outgoing_unicast_answers( + cython.dict answers, bint ucast_source, cython.list questions, object id_ +) + + +@cython.locals(answer=DNSRecord, additionals=cython.set, additional=DNSRecord) +cdef void _add_answers_additionals(DNSOutgoing out, cython.dict answers) diff --git a/src/zeroconf/_handlers/answers.py b/src/zeroconf/_handlers/answers.py new file mode 100644 index 000000000..07b0a65ab --- /dev/null +++ b/src/zeroconf/_handlers/answers.py @@ -0,0 +1,125 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from operator import attrgetter + +from .._dns import DNSQuestion, DNSRecord +from .._protocol.outgoing import DNSOutgoing +from ..const import _FLAGS_AA, _FLAGS_QR_RESPONSE + +_AnswerWithAdditionalsType = dict[DNSRecord, set[DNSRecord]] + +int_ = int + + +MULTICAST_DELAY_RANDOM_INTERVAL = (20, 120) + +NAME_GETTER = attrgetter("name") + +_FLAGS_QR_RESPONSE_AA = _FLAGS_QR_RESPONSE | _FLAGS_AA + +float_ = float + + +class QuestionAnswers: + """A group of answers to a question.""" + + __slots__ = ("mcast_aggregate", "mcast_aggregate_last_second", "mcast_now", "ucast") + + def __init__( + self, + ucast: _AnswerWithAdditionalsType, + mcast_now: _AnswerWithAdditionalsType, + mcast_aggregate: _AnswerWithAdditionalsType, + mcast_aggregate_last_second: _AnswerWithAdditionalsType, + ) -> None: + """Initialize a QuestionAnswers.""" + self.ucast = ucast + self.mcast_now = mcast_now + self.mcast_aggregate = mcast_aggregate + self.mcast_aggregate_last_second = mcast_aggregate_last_second + + def __repr__(self) -> str: + """Return a string representation of this QuestionAnswers.""" + return ( + f"QuestionAnswers(ucast={self.ucast}, mcast_now={self.mcast_now}, " + f"mcast_aggregate={self.mcast_aggregate}, " + f"mcast_aggregate_last_second={self.mcast_aggregate_last_second})" + ) + + +class AnswerGroup: + """A group of answers scheduled to be sent at the same time.""" + + __slots__ = ("answers", "send_after", "send_before") + + def __init__( + self, + send_after: float_, + send_before: float_, + answers: _AnswerWithAdditionalsType, + ) -> None: + self.send_after = send_after # Must be sent after this time + self.send_before = send_before # Must be sent before this time + self.answers = answers + + +def construct_outgoing_multicast_answers( + answers: _AnswerWithAdditionalsType, +) -> DNSOutgoing: + """Add answers and additionals to a DNSOutgoing.""" + out = DNSOutgoing(_FLAGS_QR_RESPONSE_AA, True) + _add_answers_additionals(out, answers) + return out + + +def construct_outgoing_unicast_answers( + answers: _AnswerWithAdditionalsType, + ucast_source: bool, + questions: list[DNSQuestion], + id_: int_, +) -> DNSOutgoing: + """Add answers and additionals to a DNSOutgoing.""" + out = DNSOutgoing(_FLAGS_QR_RESPONSE_AA, False, id_) + # Adding the questions back when the source is legacy unicast behavior + if ucast_source: + for question in questions: + out.add_question(question) + _add_answers_additionals(out, answers) + return out + + +def _add_answers_additionals(out: DNSOutgoing, answers: _AnswerWithAdditionalsType) -> None: + # Find additionals and suppress any additionals that are already in answers + sending: set[DNSRecord] = set(answers) + # Answers are sorted to group names together to increase the chance + # that similar names will end up in the same packet and can reduce the + # overall size of the outgoing response via name compression + for answer in sorted(answers, key=NAME_GETTER): + out.add_answer_at_time(answer, 0) + additionals = answers[answer] + for additional in additionals: + if additional not in sending: + out.add_additional_answer(additional) + sending.add(additional) diff --git a/src/zeroconf/_handlers/multicast_outgoing_queue.pxd b/src/zeroconf/_handlers/multicast_outgoing_queue.pxd new file mode 100644 index 000000000..88cfdaa0e --- /dev/null +++ b/src/zeroconf/_handlers/multicast_outgoing_queue.pxd @@ -0,0 +1,27 @@ + +import cython + +from .._utils.time cimport current_time_millis, millis_to_seconds +from .answers cimport AnswerGroup, construct_outgoing_multicast_answers + + +cdef bint TYPE_CHECKING +cdef tuple MULTICAST_DELAY_RANDOM_INTERVAL +cdef object RAND_INT + +cdef class MulticastOutgoingQueue: + + cdef object zc + cdef public object queue + cdef public object _multicast_delay_random_min + cdef public object _multicast_delay_random_max + cdef object _additional_delay + cdef object _aggregation_delay + + @cython.locals(last_group=AnswerGroup, random_int=cython.uint) + cpdef void async_add(self, double now, cython.dict answers) + + @cython.locals(pending=AnswerGroup) + cdef void _remove_answers_from_queue(self, cython.dict answers) + + cpdef void async_ready(self) diff --git a/src/zeroconf/_handlers/multicast_outgoing_queue.py b/src/zeroconf/_handlers/multicast_outgoing_queue.py new file mode 100644 index 000000000..73d5ee431 --- /dev/null +++ b/src/zeroconf/_handlers/multicast_outgoing_queue.py @@ -0,0 +1,130 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import random +from collections import deque +from typing import TYPE_CHECKING + +from .._utils.time import current_time_millis, millis_to_seconds +from .answers import ( + MULTICAST_DELAY_RANDOM_INTERVAL, + AnswerGroup, + _AnswerWithAdditionalsType, + construct_outgoing_multicast_answers, +) + +RAND_INT = random.randint + +if TYPE_CHECKING: + from .._core import Zeroconf + +_float = float +_int = int + + +class MulticastOutgoingQueue: + """An outgoing queue used to aggregate multicast responses.""" + + __slots__ = ( + "_additional_delay", + "_aggregation_delay", + "_multicast_delay_random_max", + "_multicast_delay_random_min", + "queue", + "zc", + ) + + def __init__(self, zeroconf: Zeroconf, additional_delay: _int, max_aggregation_delay: _int) -> None: + self.zc = zeroconf + self.queue: deque[AnswerGroup] = deque() + # Additional delay is used to implement + # Protect the network against excessive packet flooding + # https://datatracker.ietf.org/doc/html/rfc6762#section-14 + self._multicast_delay_random_min = MULTICAST_DELAY_RANDOM_INTERVAL[0] + self._multicast_delay_random_max = MULTICAST_DELAY_RANDOM_INTERVAL[1] + self._additional_delay = additional_delay + self._aggregation_delay = max_aggregation_delay + + def async_add(self, now: _float, answers: _AnswerWithAdditionalsType) -> None: + """Add a group of answers with additionals to the outgoing queue.""" + loop = self.zc.loop + if TYPE_CHECKING: + assert loop is not None + random_int = RAND_INT(self._multicast_delay_random_min, self._multicast_delay_random_max) + random_delay = random_int + self._additional_delay + send_after = now + random_delay + send_before = now + self._aggregation_delay + self._additional_delay + if len(self.queue): + # If we calculate a random delay for the send after time + # that is less than the last group scheduled to go out, + # we instead add the answers to the last group as this + # allows aggregating additional responses + last_group = self.queue[-1] + if send_after <= last_group.send_after: + last_group.answers.update(answers) + return + else: + loop.call_at(loop.time() + millis_to_seconds(random_delay), self.async_ready) + self.queue.append(AnswerGroup(send_after, send_before, answers)) + + def _remove_answers_from_queue(self, answers: _AnswerWithAdditionalsType) -> None: + """Remove a set of answers from the outgoing queue.""" + for pending in self.queue: + for record in answers: + pending.answers.pop(record, None) + + def async_ready(self) -> None: + """Process anything in the queue that is ready.""" + zc = self.zc + loop = zc.loop + if TYPE_CHECKING: + assert loop is not None + now = current_time_millis() + + if len(self.queue) > 1 and self.queue[0].send_before > now: + # There is more than one answer in the queue, + # delay until we have to send it (first answer group reaches send_before) + loop.call_at( + loop.time() + millis_to_seconds(self.queue[0].send_before - now), + self.async_ready, + ) + return + + answers: _AnswerWithAdditionalsType = {} + # Add all groups that can be sent now + while len(self.queue) and self.queue[0].send_after <= now: + answers.update(self.queue.popleft().answers) + + if len(self.queue): + # If there are still groups in the queue that are not ready to send + # be sure we schedule them to go out later + loop.call_at( + loop.time() + millis_to_seconds(self.queue[0].send_after - now), + self.async_ready, + ) + + if answers: # pragma: no branch + # If we have the same answer scheduled to go out, remove them + self._remove_answers_from_queue(answers) + zc.async_send(construct_outgoing_multicast_answers(answers)) diff --git a/src/zeroconf/_handlers/query_handler.pxd b/src/zeroconf/_handlers/query_handler.pxd new file mode 100644 index 000000000..89a1f2b25 --- /dev/null +++ b/src/zeroconf/_handlers/query_handler.pxd @@ -0,0 +1,120 @@ + +import cython + +from .._cache cimport DNSCache +from .._dns cimport DNSAddress, DNSPointer, DNSQuestion, DNSRecord, DNSRRSet +from .._history cimport QuestionHistory +from .._protocol.incoming cimport DNSIncoming +from .._services.info cimport ServiceInfo +from .._services.registry cimport ServiceRegistry +from .answers cimport ( + QuestionAnswers, + construct_outgoing_multicast_answers, + construct_outgoing_unicast_answers, +) +from .multicast_outgoing_queue cimport MulticastOutgoingQueue + + +cdef bint TYPE_CHECKING +cdef cython.uint _ONE_SECOND, _TYPE_PTR, _TYPE_ANY, _TYPE_A, _TYPE_AAAA, _TYPE_SRV, _TYPE_TXT +cdef str _SERVICE_TYPE_ENUMERATION_NAME +cdef cython.set _RESPOND_IMMEDIATE_TYPES +cdef cython.set _ADDRESS_RECORD_TYPES +cdef object IPVersion, _IPVersion_ALL +cdef object _TYPE_PTR, _CLASS_IN, _DNS_OTHER_TTL + +cdef unsigned int _ANSWER_STRATEGY_SERVICE_TYPE_ENUMERATION +cdef unsigned int _ANSWER_STRATEGY_POINTER +cdef unsigned int _ANSWER_STRATEGY_ADDRESS +cdef unsigned int _ANSWER_STRATEGY_SERVICE +cdef unsigned int _ANSWER_STRATEGY_TEXT + +cdef list _EMPTY_SERVICES_LIST +cdef list _EMPTY_TYPES_LIST + +cdef class _AnswerStrategy: + + cdef public DNSQuestion question + cdef public unsigned int strategy_type + cdef public list types + cdef public list services + + +cdef class _QueryResponse: + + cdef bint _is_probe + cdef cython.list _questions + cdef double _now + cdef DNSCache _cache + cdef cython.dict _additionals + cdef cython.set _ucast + cdef cython.set _mcast_now + cdef cython.set _mcast_aggregate + cdef cython.set _mcast_aggregate_last_second + + @cython.locals(record=DNSRecord) + cdef void add_qu_question_response(self, cython.dict answers) + + cdef void add_ucast_question_response(self, cython.dict answers) + + @cython.locals(answer=DNSRecord, question=DNSQuestion) + cdef void add_mcast_question_response(self, cython.dict answers) + + @cython.locals(maybe_entry=DNSRecord) + cdef bint _has_mcast_within_one_quarter_ttl(self, DNSRecord record) + + @cython.locals(maybe_entry=DNSRecord) + cdef bint _has_mcast_record_in_last_second(self, DNSRecord record) + + cdef QuestionAnswers answers(self) + +cdef class QueryHandler: + + cdef object zc + cdef ServiceRegistry registry + cdef DNSCache cache + cdef QuestionHistory question_history + cdef MulticastOutgoingQueue out_queue + cdef MulticastOutgoingQueue out_delay_queue + + @cython.locals(service=ServiceInfo) + cdef void _add_service_type_enumeration_query_answers(self, list types, cython.dict answer_set, DNSRRSet known_answers) + + @cython.locals(service=ServiceInfo) + cdef void _add_pointer_answers(self, list services, cython.dict answer_set, DNSRRSet known_answers) + + @cython.locals(service=ServiceInfo, dns_address=DNSAddress) + cdef void _add_address_answers(self, list services, cython.dict answer_set, DNSRRSet known_answers, cython.uint type_) + + @cython.locals(question_lower_name=str, type_=cython.uint, service=ServiceInfo) + cdef cython.dict _answer_question(self, DNSQuestion question, unsigned int strategy_type, list types, list services, DNSRRSet known_answers) + + @cython.locals( + msg=DNSIncoming, + msgs=list, + strategy=_AnswerStrategy, + question=DNSQuestion, + answer_set=cython.dict, + known_answers=DNSRRSet, + known_answers_set=cython.set, + is_unicast=bint, + is_probe=object, + now=double + ) + cpdef QuestionAnswers async_response(self, cython.list msgs, cython.bint unicast_source) + + @cython.locals(name=str, question_lower_name=str) + cdef list _get_answer_strategies(self, DNSQuestion question) + + @cython.locals( + first_packet=DNSIncoming, + ucast_source=bint, + ) + cpdef void handle_assembled_query( + self, + list packets, + object addr, + object port, + object transport, + tuple v6_flow_scope + ) diff --git a/src/zeroconf/_handlers/query_handler.py b/src/zeroconf/_handlers/query_handler.py new file mode 100644 index 000000000..60209568a --- /dev/null +++ b/src/zeroconf/_handlers/query_handler.py @@ -0,0 +1,473 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, cast + +from .._cache import DNSCache, _UniqueRecordsType +from .._dns import DNSAddress, DNSPointer, DNSQuestion, DNSRecord, DNSRRSet +from .._protocol.incoming import DNSIncoming +from .._services.info import ServiceInfo +from .._transport import _WrappedTransport +from .._utils.net import IPVersion +from ..const import ( + _ADDRESS_RECORD_TYPES, + _CLASS_IN, + _DNS_OTHER_TTL, + _MDNS_PORT, + _ONE_SECOND, + _SERVICE_TYPE_ENUMERATION_NAME, + _TYPE_A, + _TYPE_AAAA, + _TYPE_ANY, + _TYPE_NSEC, + _TYPE_PTR, + _TYPE_SRV, + _TYPE_TXT, +) +from .answers import ( + QuestionAnswers, + _AnswerWithAdditionalsType, + construct_outgoing_multicast_answers, + construct_outgoing_unicast_answers, +) + +_RESPOND_IMMEDIATE_TYPES = {_TYPE_NSEC, _TYPE_SRV, *_ADDRESS_RECORD_TYPES} + +_EMPTY_SERVICES_LIST: list[ServiceInfo] = [] +_EMPTY_TYPES_LIST: list[str] = [] + +_IPVersion_ALL = IPVersion.All + +_int = int +_str = str + +_ANSWER_STRATEGY_SERVICE_TYPE_ENUMERATION = 0 +_ANSWER_STRATEGY_POINTER = 1 +_ANSWER_STRATEGY_ADDRESS = 2 +_ANSWER_STRATEGY_SERVICE = 3 +_ANSWER_STRATEGY_TEXT = 4 + +if TYPE_CHECKING: + from .._core import Zeroconf + + +class _AnswerStrategy: + __slots__ = ("question", "services", "strategy_type", "types") + + def __init__( + self, + question: DNSQuestion, + strategy_type: _int, + types: list[str], + services: list[ServiceInfo], + ) -> None: + """Create an answer strategy.""" + self.question = question + self.strategy_type = strategy_type + self.types = types + self.services = services + + +class _QueryResponse: + """A pair for unicast and multicast DNSOutgoing responses.""" + + __slots__ = ( + "_additionals", + "_cache", + "_is_probe", + "_mcast_aggregate", + "_mcast_aggregate_last_second", + "_mcast_now", + "_now", + "_questions", + "_ucast", + ) + + def __init__(self, cache: DNSCache, questions: list[DNSQuestion], is_probe: bool, now: float) -> None: + """Build a query response.""" + self._is_probe = is_probe + self._questions = questions + self._now = now + self._cache = cache + self._additionals: _AnswerWithAdditionalsType = {} + self._ucast: set[DNSRecord] = set() + self._mcast_now: set[DNSRecord] = set() + self._mcast_aggregate: set[DNSRecord] = set() + self._mcast_aggregate_last_second: set[DNSRecord] = set() + + def add_qu_question_response(self, answers: _AnswerWithAdditionalsType) -> None: + """Generate a response to a multicast QU query.""" + for record, additionals in answers.items(): + self._additionals[record] = additionals + if self._is_probe: + self._ucast.add(record) + if not self._has_mcast_within_one_quarter_ttl(record): + self._mcast_now.add(record) + elif not self._is_probe: + self._ucast.add(record) + + def add_ucast_question_response(self, answers: _AnswerWithAdditionalsType) -> None: + """Generate a response to a unicast query.""" + self._additionals.update(answers) + self._ucast.update(answers) + + def add_mcast_question_response(self, answers: _AnswerWithAdditionalsType) -> None: + """Generate a response to a multicast query.""" + self._additionals.update(answers) + for answer in answers: + if self._is_probe: + self._mcast_now.add(answer) + continue + + if self._has_mcast_record_in_last_second(answer): + self._mcast_aggregate_last_second.add(answer) + continue + + if len(self._questions) == 1: + question = self._questions[0] + if question.type in _RESPOND_IMMEDIATE_TYPES: + self._mcast_now.add(answer) + continue + + self._mcast_aggregate.add(answer) + + def answers( + self, + ) -> QuestionAnswers: + """Return answer sets that will be queued.""" + ucast = {r: self._additionals[r] for r in self._ucast} + mcast_now = {r: self._additionals[r] for r in self._mcast_now} + mcast_aggregate = {r: self._additionals[r] for r in self._mcast_aggregate} + mcast_aggregate_last_second = {r: self._additionals[r] for r in self._mcast_aggregate_last_second} + return QuestionAnswers(ucast, mcast_now, mcast_aggregate, mcast_aggregate_last_second) + + def _has_mcast_within_one_quarter_ttl(self, record: DNSRecord) -> bool: + """Check to see if a record has been mcasted recently. + + https://datatracker.ietf.org/doc/html/rfc6762#section-5.4 + When receiving a question with the unicast-response bit set, a + responder SHOULD usually respond with a unicast packet directed back + to the querier. However, if the responder has not multicast that + record recently (within one quarter of its TTL), then the responder + SHOULD instead multicast the response so as to keep all the peer + caches up to date + """ + if TYPE_CHECKING: + record = cast(_UniqueRecordsType, record) + maybe_entry = self._cache.async_get_unique(record) + return bool(maybe_entry is not None and maybe_entry.is_recent(self._now)) + + def _has_mcast_record_in_last_second(self, record: DNSRecord) -> bool: + """Check if an answer was seen in the last second. + Protect the network against excessive packet flooding + https://datatracker.ietf.org/doc/html/rfc6762#section-14 + """ + if TYPE_CHECKING: + record = cast(_UniqueRecordsType, record) + maybe_entry = self._cache.async_get_unique(record) + return bool(maybe_entry is not None and self._now - maybe_entry.created < _ONE_SECOND) + + +class QueryHandler: + """Query the ServiceRegistry.""" + + __slots__ = ( + "cache", + "out_delay_queue", + "out_queue", + "question_history", + "registry", + "zc", + ) + + def __init__(self, zc: Zeroconf) -> None: + """Init the query handler.""" + self.zc = zc + self.registry = zc.registry + self.cache = zc.cache + self.question_history = zc.question_history + self.out_queue = zc.out_queue + self.out_delay_queue = zc.out_delay_queue + + def _add_service_type_enumeration_query_answers( + self, + types: list[str], + answer_set: _AnswerWithAdditionalsType, + known_answers: DNSRRSet, + ) -> None: + """Provide an answer to a service type enumeration query. + + https://datatracker.ietf.org/doc/html/rfc6763#section-9 + """ + for stype in types: + dns_pointer = DNSPointer( + _SERVICE_TYPE_ENUMERATION_NAME, + _TYPE_PTR, + _CLASS_IN, + _DNS_OTHER_TTL, + stype, + 0.0, + ) + if not known_answers.suppresses(dns_pointer): + answer_set[dns_pointer] = set() + + def _add_pointer_answers( + self, + services: list[ServiceInfo], + answer_set: _AnswerWithAdditionalsType, + known_answers: DNSRRSet, + ) -> None: + """Answer PTR/ANY question.""" + for service in services: + # Add recommended additional answers according to + # https://tools.ietf.org/html/rfc6763#section-12.1. + dns_pointer = service._dns_pointer(None) + if known_answers.suppresses(dns_pointer): + continue + answer_set[dns_pointer] = { + service._dns_service(None), + service._dns_text(None), + *service._get_address_and_nsec_records(None), + } + + def _add_address_answers( + self, + services: list[ServiceInfo], + answer_set: _AnswerWithAdditionalsType, + known_answers: DNSRRSet, + type_: _int, + ) -> None: + """Answer A/AAAA/ANY question.""" + for service in services: + answers: list[DNSAddress] = [] + additionals: set[DNSRecord] = set() + seen_types: set[int] = set() + for dns_address in service._dns_addresses(None, _IPVersion_ALL): + seen_types.add(dns_address.type) + if dns_address.type != type_: + additionals.add(dns_address) + elif not known_answers.suppresses(dns_address): + answers.append(dns_address) + missing_types: set[int] = _ADDRESS_RECORD_TYPES - seen_types + if answers: + if missing_types: + assert service.server is not None, "Service server must be set for NSEC record." + additionals.add(service._dns_nsec(list(missing_types), None)) + for answer in answers: + answer_set[answer] = additionals + elif type_ in missing_types: + assert service.server is not None, "Service server must be set for NSEC record." + answer_set[service._dns_nsec(list(missing_types), None)] = set() + + def _answer_question( + self, + question: DNSQuestion, + strategy_type: _int, + types: list[str], + services: list[ServiceInfo], + known_answers: DNSRRSet, + ) -> _AnswerWithAdditionalsType: + """Answer a question.""" + answer_set: _AnswerWithAdditionalsType = {} + + if strategy_type == _ANSWER_STRATEGY_SERVICE_TYPE_ENUMERATION: + self._add_service_type_enumeration_query_answers(types, answer_set, known_answers) + elif strategy_type == _ANSWER_STRATEGY_POINTER: + self._add_pointer_answers(services, answer_set, known_answers) + elif strategy_type == _ANSWER_STRATEGY_ADDRESS: + self._add_address_answers(services, answer_set, known_answers, question.type) + elif strategy_type == _ANSWER_STRATEGY_SERVICE: + # Add recommended additional answers according to + # https://tools.ietf.org/html/rfc6763#section-12.2. + service = services[0] + dns_service = service._dns_service(None) + if not known_answers.suppresses(dns_service): + answer_set[dns_service] = service._get_address_and_nsec_records(None) + elif strategy_type == _ANSWER_STRATEGY_TEXT: # pragma: no branch + service = services[0] + dns_text = service._dns_text(None) + if not known_answers.suppresses(dns_text): + answer_set[dns_text] = set() + + return answer_set + + def async_response( # pylint: disable=unused-argument + self, msgs: list[DNSIncoming], ucast_source: bool + ) -> QuestionAnswers | None: + """Deal with incoming query packets. Provides a response if possible. + + This function must be run in the event loop as it is not + threadsafe. + """ + strategies: list[_AnswerStrategy] = [] + for msg in msgs: + for question in msg._questions: + strategies.extend(self._get_answer_strategies(question)) + + if not strategies: + # We have no way to answer the question because we have + # nothing in the ServiceRegistry that matches or we do not + # understand the question. + return None + + is_probe = False + msg = msgs[0] + questions = msg._questions + # Only decode known answers if we are not a probe and we have + # at least one answer strategy + answers: list[DNSRecord] = [] + for msg in msgs: + if msg.is_probe(): + is_probe = True + else: + answers.extend(msg.answers()) + + query_res = _QueryResponse(self.cache, questions, is_probe, msg.now) + known_answers = DNSRRSet(answers) + known_answers_set: set[DNSRecord] | None = None + now = msg.now + for strategy in strategies: + question = strategy.question + is_unicast = question.unique # unique and unicast are the same flag + if not is_unicast: + if known_answers_set is None: # pragma: no branch + known_answers_set = known_answers.lookup_set() + self.question_history.add_question_at_time(question, now, known_answers_set) + answer_set = self._answer_question( + question, + strategy.strategy_type, + strategy.types, + strategy.services, + known_answers, + ) + if not ucast_source and is_unicast: + query_res.add_qu_question_response(answer_set) + continue + if ucast_source: + query_res.add_ucast_question_response(answer_set) + # We always multicast as well even if its a unicast + # source as long as we haven't done it recently (75% of ttl) + query_res.add_mcast_question_response(answer_set) + + return query_res.answers() + + def _get_answer_strategies( + self, + question: DNSQuestion, + ) -> list[_AnswerStrategy]: + """Collect strategies to answer a question.""" + name = question.name + question_lower_name = name.lower() + type_ = question.type + strategies: list[_AnswerStrategy] = [] + + if type_ == _TYPE_PTR and question_lower_name == _SERVICE_TYPE_ENUMERATION_NAME: + types = self.registry.async_get_types() + if types: + strategies.append( + _AnswerStrategy( + question, + _ANSWER_STRATEGY_SERVICE_TYPE_ENUMERATION, + types, + _EMPTY_SERVICES_LIST, + ) + ) + return strategies + + if type_ in (_TYPE_PTR, _TYPE_ANY): + services = self.registry.async_get_infos_type(question_lower_name) + if services: + strategies.append( + _AnswerStrategy(question, _ANSWER_STRATEGY_POINTER, _EMPTY_TYPES_LIST, services) + ) + + if type_ in (_TYPE_A, _TYPE_AAAA, _TYPE_ANY): + services = self.registry.async_get_infos_server(question_lower_name) + if services: + strategies.append( + _AnswerStrategy(question, _ANSWER_STRATEGY_ADDRESS, _EMPTY_TYPES_LIST, services) + ) + + if type_ in (_TYPE_SRV, _TYPE_TXT, _TYPE_ANY): + service = self.registry.async_get_info_name(question_lower_name) + if service is not None: + if type_ in (_TYPE_SRV, _TYPE_ANY): + strategies.append( + _AnswerStrategy( + question, + _ANSWER_STRATEGY_SERVICE, + _EMPTY_TYPES_LIST, + [service], + ) + ) + if type_ in (_TYPE_TXT, _TYPE_ANY): + strategies.append( + _AnswerStrategy( + question, + _ANSWER_STRATEGY_TEXT, + _EMPTY_TYPES_LIST, + [service], + ) + ) + + return strategies + + def handle_assembled_query( + self, + packets: list[DNSIncoming], + addr: _str, + port: _int, + transport: _WrappedTransport, + v6_flow_scope: tuple[()] | tuple[int, int], + ) -> None: + """Respond to a (re)assembled query. + + If the protocol received packets with the TC bit set, it will + wait a bit for the rest of the packets and only call + handle_assembled_query once it has a complete set of packets + or the timer expires. If the TC bit is not set, a single + packet will be in packets. + """ + first_packet = packets[0] + ucast_source = port != _MDNS_PORT + question_answers = self.async_response(packets, ucast_source) + if question_answers is None: + return + if question_answers.ucast: + questions = first_packet._questions + id_ = first_packet.id + out = construct_outgoing_unicast_answers(question_answers.ucast, ucast_source, questions, id_) + # When sending unicast, only send back the reply + # via the same socket that it was received from + # as we know its reachable from that socket + self.zc.async_send(out, addr, port, v6_flow_scope, transport) + if question_answers.mcast_now: + self.zc.async_send(construct_outgoing_multicast_answers(question_answers.mcast_now)) + if question_answers.mcast_aggregate: + self.out_queue.async_add(first_packet.now, question_answers.mcast_aggregate) + if question_answers.mcast_aggregate_last_second: + # https://datatracker.ietf.org/doc/html/rfc6762#section-14 + # If we broadcast it in the last second, we have to delay + # at least a second before we send it again + self.out_delay_queue.async_add(first_packet.now, question_answers.mcast_aggregate_last_second) diff --git a/src/zeroconf/_handlers/record_manager.pxd b/src/zeroconf/_handlers/record_manager.pxd new file mode 100644 index 000000000..b9bde975d --- /dev/null +++ b/src/zeroconf/_handlers/record_manager.pxd @@ -0,0 +1,42 @@ + +import cython + +from .._cache cimport DNSCache +from .._dns cimport DNSQuestion, DNSRecord +from .._protocol.incoming cimport DNSIncoming +from .._updates cimport RecordUpdateListener +from .._utils.time cimport current_time_millis +from .._record_update cimport RecordUpdate + +cdef unsigned int _DNS_PTR_MIN_TTL +cdef cython.uint _TYPE_PTR +cdef object _ADDRESS_RECORD_TYPES +cdef bint TYPE_CHECKING +cdef object _TYPE_PTR + + +cdef class RecordManager: + + cdef public object zc + cdef public DNSCache cache + cdef public cython.set listeners + + cpdef void async_updates(self, object now, list records) + + cpdef void async_updates_complete(self, bint notify) + + @cython.locals( + cache=DNSCache, + record=DNSRecord, + answers=cython.list, + maybe_entry=DNSRecord, + rec_update=RecordUpdate + ) + cpdef void async_updates_from_response(self, DNSIncoming msg) + + cpdef void async_add_listener(self, RecordUpdateListener listener, object question) + + cpdef void async_remove_listener(self, RecordUpdateListener listener) + + @cython.locals(question=DNSQuestion, record=DNSRecord) + cdef void _async_update_matching_records(self, RecordUpdateListener listener, cython.list questions) diff --git a/src/zeroconf/_handlers/record_manager.py b/src/zeroconf/_handlers/record_manager.py new file mode 100644 index 000000000..566f0e8c9 --- /dev/null +++ b/src/zeroconf/_handlers/record_manager.py @@ -0,0 +1,221 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, cast + +from .._cache import _UniqueRecordsType +from .._dns import DNSQuestion, DNSRecord +from .._logger import log +from .._protocol.incoming import DNSIncoming +from .._record_update import RecordUpdate +from .._updates import RecordUpdateListener +from .._utils.time import current_time_millis +from ..const import _ADDRESS_RECORD_TYPES, _DNS_PTR_MIN_TTL, _TYPE_PTR + +if TYPE_CHECKING: + from .._core import Zeroconf + +_float = float + + +class RecordManager: + """Process records into the cache and notify listeners.""" + + __slots__ = ("cache", "listeners", "zc") + + def __init__(self, zeroconf: Zeroconf) -> None: + """Init the record manager.""" + self.zc = zeroconf + self.cache = zeroconf.cache + self.listeners: set[RecordUpdateListener] = set() + + def async_updates(self, now: _float, records: list[RecordUpdate]) -> None: + """Used to notify listeners of new information that has updated + a record. + + This method must be called before the cache is updated. + + This method will be run in the event loop. + """ + for listener in self.listeners.copy(): + listener.async_update_records(self.zc, now, records) + + def async_updates_complete(self, notify: bool) -> None: + """Used to notify listeners of new information that has updated + a record. + + This method must be called after the cache is updated. + + This method will be run in the event loop. + """ + for listener in self.listeners.copy(): + listener.async_update_records_complete() + if notify: + self.zc.async_notify_all() + + def async_updates_from_response(self, msg: DNSIncoming) -> None: + """Deal with incoming response packets. All answers + are held in the cache, and listeners are notified. + + This function must be run in the event loop as it is not + threadsafe. + """ + updates: list[RecordUpdate] = [] + address_adds: list[DNSRecord] = [] + other_adds: list[DNSRecord] = [] + removes: set[DNSRecord] = set() + now = msg.now + unique_types: set[tuple[str, int, int]] = set() + cache = self.cache + answers = msg.answers() + + for record in answers: + # Protect zeroconf from records that can cause denial of service. + # + # We enforce a minimum TTL for PTR records to avoid + # ServiceBrowsers generating excessive queries refresh queries. + # Apple uses a 15s minimum TTL, however we do not have the same + # level of rate limit and safe guards so we use 1/4 of the recommended value. + record_type = record.type + record_ttl = record.ttl + if record_ttl and record_type == _TYPE_PTR and record_ttl < _DNS_PTR_MIN_TTL: + log.debug( + "Increasing effective ttl of %s to minimum of %s to protect against excessive refreshes.", + record, + _DNS_PTR_MIN_TTL, + ) + # Safe because the record is never in the cache yet + record._set_created_ttl(record.created, _DNS_PTR_MIN_TTL) + + if record.unique: # https://tools.ietf.org/html/rfc6762#section-10.2 + unique_types.add((record.name, record_type, record.class_)) + + if TYPE_CHECKING: + record = cast(_UniqueRecordsType, record) + + maybe_entry = cache.async_get_unique(record) + if not record.is_expired(now): + if record_type in _ADDRESS_RECORD_TYPES: + address_adds.append(record) + else: + other_adds.append(record) + rec_update = RecordUpdate.__new__(RecordUpdate) + rec_update._fast_init(record, maybe_entry) + updates.append(rec_update) + # This is likely a goodbye since the record is + # expired and exists in the cache + elif maybe_entry is not None: + rec_update = RecordUpdate.__new__(RecordUpdate) + rec_update._fast_init(record, maybe_entry) + updates.append(rec_update) + removes.add(record) + + if unique_types: + cache.async_mark_unique_records_older_than_1s_to_expire(unique_types, answers, now) + + if updates: + self.async_updates(now, updates) + # The cache adds must be processed AFTER we trigger + # the updates since we compare existing data + # with the new data and updating the cache + # ahead of update_record will cause listeners + # to miss changes + # + # We must process address adds before non-addresses + # otherwise a fetch of ServiceInfo may miss an address + # because it thinks the cache is complete + # + # The cache is processed under the context manager to ensure + # that any ServiceBrowser that is going to call + # zc.get_service_info will see the cached value + # but ONLY after all the record updates have been + # processed. + new = False + if other_adds or address_adds: + new = cache.async_add_records(address_adds) + if cache.async_add_records(other_adds): + new = True + # Removes are processed last since + # ServiceInfo could generate an un-needed query + # because the data was not yet populated. + if removes: + cache.async_remove_records(removes) + if updates: + self.async_updates_complete(new) + + def async_add_listener( + self, + listener: RecordUpdateListener, + question: DNSQuestion | list[DNSQuestion] | None, + ) -> None: + """Adds a listener for a given question. The listener will have + its update_record method called when information is available to + answer the question(s). + + This function is not thread-safe and must be called in the eventloop. + """ + if not isinstance(listener, RecordUpdateListener): + log.error( # type: ignore[unreachable] + "listeners passed to async_add_listener must inherit from RecordUpdateListener;" + " In the future this will fail" + ) + + self.listeners.add(listener) + + if question is None: + return + + questions = [question] if isinstance(question, DNSQuestion) else question + self._async_update_matching_records(listener, questions) + + def _async_update_matching_records( + self, listener: RecordUpdateListener, questions: list[DNSQuestion] + ) -> None: + """Calls back any existing entries in the cache that answer the question. + + This function must be run from the event loop. + """ + now = current_time_millis() + records: list[RecordUpdate] = [ + RecordUpdate(record, None) + for question in questions + for record in self.cache.async_entries_with_name(question.name) + if not record.is_expired(now) and question.answered_by(record) + ] + if not records: + return + listener.async_update_records(self.zc, now, records) + listener.async_update_records_complete() + self.zc.async_notify_all() + + def async_remove_listener(self, listener: RecordUpdateListener) -> None: + """Removes a listener. + + This function is not threadsafe and must be called in the eventloop. + """ + try: + self.listeners.remove(listener) + self.zc.async_notify_all() + except ValueError as e: + log.exception("Failed to remove listener: %r", e) diff --git a/src/zeroconf/_history.pxd b/src/zeroconf/_history.pxd new file mode 100644 index 000000000..dab257e44 --- /dev/null +++ b/src/zeroconf/_history.pxd @@ -0,0 +1,23 @@ +import cython + +from ._dns cimport DNSQuestion + + +cdef cython.double _DUPLICATE_QUESTION_INTERVAL +cdef unsigned int _MAX_QUESTION_HISTORY_ENTRIES +cdef unsigned int _MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY + +cdef class QuestionHistory: + + cdef public cython.dict _history + + cpdef void add_question_at_time(self, DNSQuestion question, double now, cython.set known_answers) + + @cython.locals(oldest=DNSQuestion, oldest_entry=cython.tuple, oldest_than=double) + cdef void _evict_to_make_room(self, double now) + + @cython.locals(than=double, previous_question=cython.tuple, previous_known_answers=cython.set) + cpdef bint suppresses(self, DNSQuestion question, double now, cython.set known_answers) + + @cython.locals(than=double, now_known_answers=cython.tuple) + cpdef void async_expire(self, double now) diff --git a/src/zeroconf/_history.py b/src/zeroconf/_history.py new file mode 100644 index 000000000..10696f4f5 --- /dev/null +++ b/src/zeroconf/_history.py @@ -0,0 +1,108 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from ._dns import DNSQuestion, DNSRecord +from .const import ( + _DUPLICATE_QUESTION_INTERVAL, + _MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY, + _MAX_QUESTION_HISTORY_ENTRIES, +) + +# The QuestionHistory is used to implement Duplicate Question Suppression +# https://datatracker.ietf.org/doc/html/rfc6762#section-7.3 + +_float = float + + +class QuestionHistory: + """Remember questions and known answers.""" + + def __init__(self) -> None: + """Init a new QuestionHistory.""" + self._history: dict[DNSQuestion, tuple[float, set[DNSRecord]]] = {} + + def add_question_at_time(self, question: DNSQuestion, now: _float, known_answers: set[DNSRecord]) -> None: + """Remember a question with known answers.""" + if len(known_answers) > _MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY: + # Refuse to pin an attacker-sized known-answer payload. + # Any pre-existing entry for this question stays in place + # so legitimate suppression continues; the cost is missing + # one round of suppression for this (likely malicious) + # query. Truncating instead would over-suppress because + # `suppresses()` matches when the stored set is a subset + # of the incoming known-answers (smaller set, easier match). + return + if question not in self._history and len(self._history) >= _MAX_QUESTION_HISTORY_ENTRIES: + self._evict_to_make_room(now) + self._history[question] = (now, known_answers) + + def suppresses(self, question: DNSQuestion, now: _float, known_answers: set[DNSRecord]) -> bool: + """Check to see if a question should be suppressed. + + https://datatracker.ietf.org/doc/html/rfc6762#section-7.3 + When multiple queriers on the network are querying + for the same resource records, there is no need for them to all be + repeatedly asking the same question. + """ + previous_question = self._history.get(question) + # There was not previous question in the history + if not previous_question: + return False + than, previous_known_answers = previous_question + # The last question was older than 999ms + if now - than > _DUPLICATE_QUESTION_INTERVAL: + return False + # The last question has more known answers than + # we knew so we have to ask + return not previous_known_answers - known_answers + + def async_expire(self, now: _float) -> None: + """Expire the history of old questions.""" + removes: list[DNSQuestion] = [] + for question, now_known_answers in self._history.items(): + than, _ = now_known_answers + if now - than > _DUPLICATE_QUESTION_INTERVAL: + removes.append(question) + for question in removes: + del self._history[question] + + def clear(self) -> None: + """Clear the history.""" + self._history.clear() + + def _evict_to_make_room(self, now: _float) -> None: + """Drop expired or oldest entries when the history is at cap. + + Peeks at the oldest insertion (dict is ordered) — only runs the + full O(n) async_expire sweep if it could actually reclaim + something, else a sustained flood at cap turns each insert into + a wasted scan. Falls back to oldest-first eviction. + """ + oldest = next(iter(self._history)) + oldest_entry = self._history[oldest] + oldest_than = oldest_entry[0] + if now - oldest_than > _DUPLICATE_QUESTION_INTERVAL: + self.async_expire(now) + while len(self._history) >= _MAX_QUESTION_HISTORY_ENTRIES: + del self._history[next(iter(self._history))] diff --git a/src/zeroconf/_listener.pxd b/src/zeroconf/_listener.pxd new file mode 100644 index 000000000..260ba091c --- /dev/null +++ b/src/zeroconf/_listener.pxd @@ -0,0 +1,68 @@ + +import cython + +from ._handlers.query_handler cimport QueryHandler +from ._handlers.record_manager cimport RecordManager +from ._protocol.incoming cimport DNSIncoming +from ._services.registry cimport ServiceRegistry +from ._utils.time cimport current_time_millis, millis_to_seconds + + +cdef object log +cdef object DEBUG_ENABLED +cdef bint TYPE_CHECKING + +cdef cython.uint _MAX_MSG_ABSOLUTE +cdef cython.uint _DUPLICATE_PACKET_SUPPRESSION_INTERVAL +cdef cython.uint _RECENT_PACKETS_MAX +cdef cython.uint _MAX_DEFERRED_ADDRS +cdef cython.uint _MAX_DEFERRED_PER_ADDR + + +cdef class AsyncListener: + + cdef public object zc + cdef ServiceRegistry _registry + cdef RecordManager _record_manager + cdef QueryHandler _query_handler + cdef public cython.bytes data + cdef public double last_time + cdef public DNSIncoming last_message + cdef public object transport + cdef public object sock_description + cdef public cython.dict _deferred + cdef public cython.dict _timers + cdef public cython.dict _deferred_deadlines + cdef public cython.dict _recent_packets + + @cython.locals(now=double, debug=cython.bint) + cpdef datagram_received(self, cython.bytes bytes, cython.tuple addrs) + + @cython.locals(msg=DNSIncoming, recent_time=double) + cpdef _process_datagram_at_time(self, bint debug, cython.uint data_len, double now, bytes data, cython.tuple addrs) + + cdef _cancel_any_timers_for_addr(self, object addr) + + cdef _evict_oldest_deferred(self) + + @cython.locals(deadline=object, fire_at=double) + cdef double _compute_deferred_fire_at(self, object addr, double now, double delay) + + @cython.locals(incoming=DNSIncoming, deferred=list, now=double, delay=double, fire_at=double) + cpdef handle_query_or_defer( + self, + DNSIncoming msg, + object addr, + object port, + object transport, + tuple v6_flow_scope + ) + + cpdef _respond_query( + self, + DNSIncoming msg, + object addr, + object port, + object transport, + tuple v6_flow_scope + ) diff --git a/src/zeroconf/_listener.py b/src/zeroconf/_listener.py new file mode 100644 index 000000000..7be2a8281 --- /dev/null +++ b/src/zeroconf/_listener.py @@ -0,0 +1,359 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import logging +import random +from functools import partial +from typing import TYPE_CHECKING, cast + +from ._logger import QuietLogger, log +from ._protocol.incoming import DNSIncoming +from ._transport import _WrappedTransport, make_wrapped_transport +from ._utils.time import current_time_millis, millis_to_seconds +from .const import ( + _DUPLICATE_PACKET_SUPPRESSION_INTERVAL, + _MAX_DEFERRED_ADDRS, + _MAX_DEFERRED_PER_ADDR, + _MAX_MSG_ABSOLUTE, + _RECENT_PACKETS_MAX, +) + +if TYPE_CHECKING: + from ._core import Zeroconf + +_TC_DELAY_RANDOM_INTERVAL = (400, 500) + + +_bytes = bytes +_str = str +_int = int +_float = float + +DEBUG_ENABLED = partial(log.isEnabledFor, logging.DEBUG) + + +class AsyncListener: + """A Listener is used by this module to listen on the multicast + group to which DNS messages are sent, allowing the implementation + to cache information as it arrives. + + It requires registration with an Engine object in order to have + the read() method called when a socket is available for reading.""" + + __slots__ = ( + "_deferred", + "_deferred_deadlines", + "_query_handler", + "_recent_packets", + "_record_manager", + "_registry", + "_timers", + "data", + "last_message", + "last_time", + "sock_description", + "transport", + "zc", + ) + + def __init__(self, zc: Zeroconf) -> None: + self.zc = zc + self._registry = zc.registry + self._record_manager = zc.record_manager + self._query_handler = zc.query_handler + self.data: bytes | None = None + self.last_time: float = 0 + self.last_message: DNSIncoming | None = None + self.transport: _WrappedTransport | None = None + self.sock_description: str | None = None + self._deferred: dict[str, list[DNSIncoming]] = {} + self._timers: dict[str, asyncio.TimerHandle] = {} + self._deferred_deadlines: dict[str, float] = {} + # Bounded recency window so an alternating (A, B, A, B, ...) + # flood cannot defeat single-slot dedup; relies on dict insertion + # order so the oldest entry is evicted first. Only payloads + # without a QU question are cached so unicast replies still go + # out on every receipt. + self._recent_packets: dict[bytes, float] = {} + super().__init__() + + def datagram_received(self, data: _bytes, addrs: tuple[str, int] | tuple[str, int, int, int]) -> None: + data_len = len(data) + debug = DEBUG_ENABLED() + + if data_len > _MAX_MSG_ABSOLUTE: + # Guard against oversized packets to ensure bad implementations cannot overwhelm + # the system. + if debug: + log.debug( + "Discarding incoming packet with length %s, which is larger " + "than the absolute maximum size of %s", + data_len, + _MAX_MSG_ABSOLUTE, + ) + return + now = current_time_millis() + self._process_datagram_at_time(debug, data_len, now, data, addrs) + + def _process_datagram_at_time( + self, + debug: bool, + data_len: _int, + now: _float, + data: _bytes, + addrs: tuple[str, int] | tuple[str, int, int, int], + ) -> None: + if ( + self.data == data + and (now - _DUPLICATE_PACKET_SUPPRESSION_INTERVAL) < self.last_time + and self.last_message is not None + and not self.last_message.has_qu_question() + ): + # Guard against duplicate packets + if debug: + log.debug( + "Ignoring duplicate message with no unicast questions" + " received from %s [socket %s] (%d bytes) as [%r]", + addrs, + self.sock_description, + data_len, + data, + ) + return + + # `get(data, -1e30)` keeps the suppression compare a single C + # double compare; the sentinel is far below any real `now - + # _DUPLICATE_PACKET_SUPPRESSION_INTERVAL` so a cache miss never + # triggers the branch even when `now` is small (time.monotonic + # is allowed to start near zero, leaving `now - INTERVAL` + # negative for the first ~1s after boot). Only non-QU payloads + # are cached, so any hit here is safe to suppress without re- + # checking has_qu_question. + recent_time = self._recent_packets.get(data, -1e30) + if (now - _DUPLICATE_PACKET_SUPPRESSION_INTERVAL) < recent_time: + # No timestamp refresh on hit so the suppression window + # ends at first-observation + interval; one parse-and- + # dispatch fires per payload per interval, capping the + # CPU cost under a sustained alternating flood. + if debug: + log.debug( + "Ignoring duplicate message with no unicast questions" + " received from %s [socket %s] (%d bytes) as [%r]", + addrs, + self.sock_description, + data_len, + data, + ) + return + + if len(addrs) == 2: + v6_flow_scope: tuple[()] | tuple[int, int] = () + # https://github.com/python/mypy/issues/1178 + addr, port = addrs + addr_port = addrs + if TYPE_CHECKING: + addr_port = cast(tuple[str, int], addr_port) + scope = None + else: + # https://github.com/python/mypy/issues/1178 + addr, port, flow, scope = addrs + if debug: # pragma: no branch + log.debug("IPv6 scope_id %d associated to the receiving interface", scope) + v6_flow_scope = (flow, scope) + addr_port = (addr, port) + + msg = DNSIncoming(data, addr_port, scope, now) + self.data = data + self.last_time = now + self.last_message = msg + if not msg.has_qu_question(): + # Refresh LRU position when an entry exists but the + # suppression window has expired; otherwise evict the oldest + # entry once the window is full. + if data in self._recent_packets: + del self._recent_packets[data] + elif len(self._recent_packets) >= _RECENT_PACKETS_MAX: + del self._recent_packets[next(iter(self._recent_packets))] + self._recent_packets[data] = now + if msg.valid is True: + if debug: + log.debug( + "Received from %r:%r [socket %s]: %r (%d bytes) as [%r]", + addr, + port, + self.sock_description, + msg, + data_len, + data, + ) + else: + if debug: + log.debug( + "Received from %r:%r [socket %s]: (%d bytes) [%r]", + addr, + port, + self.sock_description, + data_len, + data, + ) + return + + if not msg.is_query(): + self._record_manager.async_updates_from_response(msg) + return + + if not self._registry.has_entries: + # If the registry is empty, we have no answers to give. + return + + if TYPE_CHECKING: + assert self.transport is not None + self.handle_query_or_defer(msg, addr, port, self.transport, v6_flow_scope) + + def handle_query_or_defer( + self, + msg: DNSIncoming, + addr: _str, + port: _int, + transport: _WrappedTransport, + v6_flow_scope: tuple[()] | tuple[int, int], + ) -> None: + """Deal with incoming query packets. Provides a response if + possible.""" + if not msg.truncated: + self._respond_query(msg, addr, port, transport, v6_flow_scope) + return + + if addr not in self._deferred and len(self._deferred) >= _MAX_DEFERRED_ADDRS: + # Bound total deferred addrs so a spoofed-source flood + # cannot keep adding distinct entries; evict the oldest + # (insertion-order) entry and discard its in-flight queue. + self._evict_oldest_deferred() + + deferred = self._deferred.setdefault(addr, []) + if len(deferred) >= _MAX_DEFERRED_PER_ADDR: + # Bound per-addr queue length; further fragments from the + # same source are dropped until the timer flushes. + return + # If we get the same packet we ignore it + for incoming in reversed(deferred): + if incoming.data == msg.data: + return + deferred.append(msg) + loop = self.zc.loop + assert loop is not None + now = loop.time() + delay = millis_to_seconds(random.randint(*_TC_DELAY_RANDOM_INTERVAL)) # noqa: S311 + fire_at = self._compute_deferred_fire_at(addr, now, delay) + if fire_at < 0.0: + # Sentinel: a new reset would push the flush past the + # per-addr reassembly deadline, so leave the existing + # TimerHandle in place rather than re-arming it. + return + self._cancel_any_timers_for_addr(addr) + self._timers[addr] = loop.call_at( + fire_at, + self._respond_query, + None, + addr, + port, + transport, + v6_flow_scope, + ) + + def _compute_deferred_fire_at(self, addr: _str, now: _float, delay: _float) -> _float: + """Return the bounded call_at time for a TC-deferred flush, or -1.0 to keep the existing timer.""" + # RFC 6762 §18.5 frames the random delay as a fixed reassembly budget + # starting at first arrival, not a sliding heartbeat. + deadline = self._deferred_deadlines.get(addr) + if deadline is None: + deadline = now + millis_to_seconds(_TC_DELAY_RANDOM_INTERVAL[1]) + self._deferred_deadlines[addr] = deadline + fire_at = now + delay + if fire_at >= deadline: + if addr in self._timers: + # Existing timer already fires at or before the deadline; + # signal the caller to leave it alone rather than reset it. + return -1.0 + # First packet for this addr already proposes a fire-time at + # or past the deadline — clamp to the deadline so the flush + # still happens within the reassembly budget. + return deadline + # Within budget: schedule at the proposed fire-time. + return fire_at + + def _cancel_any_timers_for_addr(self, addr: _str) -> None: + """Cancel any future truncated packet timers for the address.""" + if addr in self._timers: + self._timers.pop(addr).cancel() + + def _evict_oldest_deferred(self) -> None: + """Discard the oldest deferred addr's reassembly state. + + Used when ``_MAX_DEFERRED_ADDRS`` would be exceeded; the + evicted addr's queue and timer are dropped without firing, so + the bound holds even when an attacker rotates source IPs. + Eviction is FIFO (oldest by first-seen, via dict insertion + order) rather than LRU so an active flooder cannot pin its + slots by re-sending into the same addr. + """ + oldest_addr = next(iter(self._deferred)) + self._cancel_any_timers_for_addr(oldest_addr) + self._deferred_deadlines.pop(oldest_addr, None) + del self._deferred[oldest_addr] + + def _respond_query( + self, + msg: DNSIncoming | None, + addr: _str, + port: _int, + transport: _WrappedTransport, + v6_flow_scope: tuple[()] | tuple[int, int], + ) -> None: + """Respond to a query and reassemble any truncated deferred packets.""" + self._cancel_any_timers_for_addr(addr) + self._deferred_deadlines.pop(addr, None) + packets = self._deferred.pop(addr, []) + if msg: + packets.append(msg) + + self._query_handler.handle_assembled_query(packets, addr, port, transport, v6_flow_scope) + + def error_received(self, exc: Exception) -> None: + """Likely socket closed or IPv6.""" + # We preformat the message string with the socket as we want + # log_exception_once to log a warning message once PER EACH + # different socket in case there are problems with multiple + # sockets + msg_str = f"Error with socket {self.sock_description}): %s" + QuietLogger.log_exception_once(exc, msg_str, exc) + + def connection_made(self, transport: asyncio.BaseTransport) -> None: + wrapped_transport = make_wrapped_transport(cast(asyncio.DatagramTransport, transport)) + self.transport = wrapped_transport + self.sock_description = f"{wrapped_transport.fileno} ({wrapped_transport.sock_name})" + + def connection_lost(self, exc: Exception | None) -> None: + """Handle connection lost.""" diff --git a/src/zeroconf/_logger.py b/src/zeroconf/_logger.py new file mode 100644 index 000000000..99990cf68 --- /dev/null +++ b/src/zeroconf/_logger.py @@ -0,0 +1,111 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine + ) +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import logging +import sys +from typing import Any + +log = logging.getLogger(__name__.split(".", maxsplit=1)[0]) +log.addHandler(logging.NullHandler()) + + +def set_logger_level_if_unset() -> None: + if log.level == logging.NOTSET: + log.setLevel(logging.WARN) + + +set_logger_level_if_unset() + + +_MAX_SEEN_LOGS = 512 +_seen_logs: dict[str, None] = {} + + +def _evict_oldest(seen: dict[str, None]) -> bool: + """Pop the oldest entry from ``seen``; return False if it raced. + + Individual dict ops (``pop`` with a default, ``next``) are atomic + on the free-threaded build, but the compound ``iter`` → ``next`` + used to pick the FIFO victim can raise ``RuntimeError`` if + another thread mutates the dict between the two ops. The caller + breaks its drain loop on False so concurrent mutation can't make + it spin. + """ + try: + seen.pop(next(iter(seen)), None) + except (RuntimeError, StopIteration): + return False + return True + + +def _mark_seen(seen: dict[str, None], key: str) -> bool: + """Record ``key`` in ``seen`` and return True if it was newly added. + + Bounds the dict so callers passing attacker-influenced keys (peer + addresses, packet offsets) cannot grow it without bound. Evicts + the oldest entries on overflow (dict preserves insertion order on + Python 3.7+), so ``_MAX_SEEN_LOGS`` is a recency window. + + The dict is shared across all ``Zeroconf`` instances in the + process; on the free-threaded build (3.14t) and under multi- + instance sync use, callers can race the ``len < cap`` check and + both insert, leaving the dict transiently above the cap. The + drain loop runs on every call (steady-state-at-cap hits are a + single ``len`` + compare past the membership check because the + helper short-circuits) so a contention burst is corrected by the + next caller regardless of whether it's a hit or a miss. + """ + inserting = key not in seen + # Hit (``inserting`` is False): drain only if drifted above cap. + # Miss (``inserting`` is True): drain to ``cap - 1`` to make room + # for the new key. Bool subtracts as 0/1 to pick the right limit. + while len(seen) > _MAX_SEEN_LOGS - inserting and _evict_oldest(seen): + pass + if inserting: + seen[key] = None + return inserting + + +class QuietLogger: + @classmethod + def log_exception_warning(cls, *logger_data: Any) -> None: + first_time = _mark_seen(_seen_logs, str(sys.exc_info()[1])) + logger = log.warning if first_time else log.debug + logger(*(logger_data or ["Exception occurred"]), exc_info=True) + + @classmethod + def log_exception_debug(cls, *logger_data: Any) -> None: + first_time = _mark_seen(_seen_logs, str(sys.exc_info()[1])) + log.debug(*(logger_data or ["Exception occurred"]), exc_info=first_time) + + @classmethod + def log_warning_once(cls, *args: Any) -> None: + logger = log.warning if _mark_seen(_seen_logs, args[0]) else log.debug + logger(*args) + + @classmethod + def log_exception_once(cls, exc: Exception, *args: Any) -> None: + logger = log.warning if _mark_seen(_seen_logs, args[0]) else log.debug + logger(*args, exc_info=exc) diff --git a/src/zeroconf/_protocol/__init__.py b/src/zeroconf/_protocol/__init__.py new file mode 100644 index 000000000..584a74eca --- /dev/null +++ b/src/zeroconf/_protocol/__init__.py @@ -0,0 +1,23 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations diff --git a/src/zeroconf/_protocol/incoming.pxd b/src/zeroconf/_protocol/incoming.pxd new file mode 100644 index 000000000..ac8c6e21f --- /dev/null +++ b/src/zeroconf/_protocol/incoming.pxd @@ -0,0 +1,135 @@ + +import cython + + +cdef cython.uint DNS_COMPRESSION_HEADER_LEN +cdef cython.uint MAX_DNS_LABELS +cdef cython.uint DNS_COMPRESSION_POINTER_LEN +cdef cython.uint MAX_NAME_LENGTH + +cdef cython.uint _TYPE_A +cdef cython.uint _TYPE_CNAME +cdef cython.uint _TYPE_PTR +cdef cython.uint _TYPE_TXT +cdef cython.uint _TYPE_SRV +cdef cython.uint _TYPE_HINFO +cdef cython.uint _TYPE_AAAA +cdef cython.uint _TYPE_NSEC +cdef cython.uint _FLAGS_QR_MASK +cdef cython.uint _FLAGS_QR_MASK +cdef cython.uint _FLAGS_TC +cdef cython.uint _FLAGS_QR_QUERY +cdef cython.uint _FLAGS_QR_RESPONSE + +cdef object DECODE_EXCEPTIONS + +cdef object IncomingDecodeError + +from .._dns cimport ( + DNSAddress, + DNSEntry, + DNSHinfo, + DNSNsec, + DNSPointer, + DNSQuestion, + DNSRecord, + DNSService, + DNSText, +) +from .._utils.time cimport current_time_millis + + +cdef class DNSIncoming: + + cdef bint _did_read_others + cdef public unsigned int flags + cdef cython.uint offset + cdef public bytes data + cdef const unsigned char [:] view + cdef unsigned int _data_len + cdef cython.dict _name_cache + cdef cython.list _questions + cdef cython.list _answers + cdef public cython.uint id + cdef cython.uint _num_questions + cdef cython.uint _num_answers + cdef cython.uint _num_authorities + cdef cython.uint _num_additionals + cdef public bint valid + cdef public double now + cdef public object scope_id + cdef public object source + cdef bint _has_qu_question + + @cython.locals( + question=DNSQuestion + ) + cpdef bint has_qu_question(self) + + cpdef bint is_query(self) + + cpdef bint is_probe(self) + + cpdef list answers(self) + + cpdef bint is_response(self) + + @cython.locals( + off="unsigned int", + label_idx="unsigned int", + length="unsigned int", + link="unsigned int", + link_data="unsigned int", + link_py_int=object, + linked_labels=cython.list + ) + cdef unsigned int _decode_labels_at_offset(self, unsigned int off, cython.list labels, cython.set seen_pointers, unsigned int depth) + + @cython.locals(offset="unsigned int") + cdef void _read_header(self) + + cdef void _initial_parse(self) + + @cython.locals( + end="unsigned int", + length="unsigned int", + offset="unsigned int" + ) + cdef void _read_others(self) + + @cython.locals(offset="unsigned int", question=DNSQuestion) + cdef _read_questions(self) + + @cython.locals( + length="unsigned int", + ) + cdef str _read_character_string(self) + + cdef bytes _read_string(self, unsigned int length) + + @cython.locals( + name_start="unsigned int", + offset="unsigned int", + address_rec=DNSAddress, + pointer_rec=DNSPointer, + text_rec=DNSText, + srv_rec=DNSService, + hinfo_rec=DNSHinfo, + nsec_rec=DNSNsec, + ) + cdef _read_record(self, str domain, unsigned int type_, unsigned int class_, unsigned int ttl, unsigned int length) + + @cython.locals( + offset="unsigned int", + offset_plus_one="unsigned int", + offset_plus_two="unsigned int", + window="unsigned int", + bit="unsigned int", + byte="unsigned int", + i="unsigned int", + bitmap_length="unsigned int", + bitmap_end="unsigned int", + ) + cdef list _read_bitmap(self, unsigned int end) + + cdef str _read_name(self) diff --git a/src/zeroconf/_protocol/incoming.py b/src/zeroconf/_protocol/incoming.py new file mode 100644 index 000000000..9ef2631af --- /dev/null +++ b/src/zeroconf/_protocol/incoming.py @@ -0,0 +1,511 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import struct +import sys +from typing import Any + +from .._dns import ( + DNSAddress, + DNSHinfo, + DNSNsec, + DNSPointer, + DNSQuestion, + DNSRecord, + DNSService, + DNSText, +) +from .._exceptions import IncomingDecodeError +from .._logger import _mark_seen, log +from .._utils.time import current_time_millis +from ..const import ( + _FLAGS_QR_MASK, + _FLAGS_QR_QUERY, + _FLAGS_QR_RESPONSE, + _FLAGS_TC, + _TYPE_A, + _TYPE_AAAA, + _TYPE_CNAME, + _TYPE_HINFO, + _TYPE_NSEC, + _TYPE_PTR, + _TYPE_SRV, + _TYPE_TXT, + _TYPES, +) + +DNS_COMPRESSION_HEADER_LEN = 1 +DNS_COMPRESSION_POINTER_LEN = 2 +MAX_DNS_LABELS = 128 +MAX_NAME_LENGTH = 253 + +DECODE_EXCEPTIONS = (IndexError, struct.error, IncomingDecodeError, RecursionError) + + +_seen_logs: dict[str, None] = {} +_str = str +_int = int + + +class DNSIncoming: + """Object representation of an incoming DNS packet""" + + __slots__ = ( + "_answers", + "_data_len", + "_did_read_others", + "_has_qu_question", + "_name_cache", + "_num_additionals", + "_num_answers", + "_num_authorities", + "_num_questions", + "_questions", + "data", + "flags", + "id", + "now", + "offset", + "scope_id", + "source", + "valid", + "view", + ) + + def __init__( + self, + data: bytes, + source: tuple[str, int] | None = None, + scope_id: int | None = None, + now: float | None = None, + ) -> None: + """Constructor from string holding bytes of packet""" + self.flags = 0 + self.offset = 0 + self.data = data + self.view = data + self._data_len = len(data) + self._name_cache: dict[int, list[str]] = {} + self._questions: list[DNSQuestion] = [] + self._answers: list[DNSRecord] = [] + self.id = 0 + self._num_questions = 0 + self._num_answers = 0 + self._num_authorities = 0 + self._num_additionals = 0 + self.valid = False + self._did_read_others = False + self.now = now or current_time_millis() + self.source = source + self.scope_id = scope_id + self._has_qu_question = False + try: + self._initial_parse() + except DECODE_EXCEPTIONS: + self._log_exception_debug( + "Received invalid packet from %s at offset %d while unpacking %r", + self.source, + self.offset, + self.data, + ) + + def is_query(self) -> bool: + """Returns true if this is a query.""" + return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY + + def is_response(self) -> bool: + """Returns true if this is a response.""" + return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE + + def has_qu_question(self) -> bool: + """Returns true if any question is a QU question.""" + return self._has_qu_question + + @property + def truncated(self) -> bool: + """Returns true if this is a truncated.""" + return (self.flags & _FLAGS_TC) == _FLAGS_TC + + @property + def questions(self) -> list[DNSQuestion]: + """Questions in the packet.""" + return self._questions + + @property + def num_questions(self) -> int: + """Number of questions in the packet.""" + return self._num_questions + + @property + def num_answers(self) -> int: + """Number of answers in the packet.""" + return self._num_answers + + @property + def num_authorities(self) -> int: + """Number of authorities in the packet.""" + return self._num_authorities + + @property + def num_additionals(self) -> int: + """Number of additionals in the packet.""" + return self._num_additionals + + def _initial_parse(self) -> None: + """Parse the data needed to initialize the packet object.""" + self._read_header() + self._read_questions() + if not self._num_questions: + self._read_others() + self.valid = True + + @classmethod + def _log_exception_debug(cls, *logger_data: Any) -> None: + log_exc_info = _mark_seen(_seen_logs, str(sys.exc_info()[1])) + log.debug(*(logger_data or ["Exception occurred"]), exc_info=log_exc_info) + + def answers(self) -> list[DNSRecord]: + """Answers in the packet.""" + if not self._did_read_others: + try: + self._read_others() + except DECODE_EXCEPTIONS: + self._log_exception_debug( + "Received invalid packet from %s at offset %d while unpacking %r", + self.source, + self.offset, + self.data, + ) + return self._answers + + def is_probe(self) -> bool: + """Returns true if this is a probe.""" + return self._num_authorities > 0 + + def __repr__(self) -> str: + return "".format( + ", ".join( + [ + f"id={self.id}", + f"flags={self.flags}", + f"truncated={self.truncated}", + f"n_q={self._num_questions}", + f"n_ans={self._num_answers}", + f"n_auth={self._num_authorities}", + f"n_add={self._num_additionals}", + f"questions={self._questions}", + f"answers={self.answers()}", + ] + ) + ) + + def _read_header(self) -> None: + """Reads header portion of packet""" + view = self.view + offset = self.offset + self.offset += 12 + # The header has 6 unsigned shorts in network order + self.id = view[offset] << 8 | view[offset + 1] + self.flags = view[offset + 2] << 8 | view[offset + 3] + self._num_questions = view[offset + 4] << 8 | view[offset + 5] + self._num_answers = view[offset + 6] << 8 | view[offset + 7] + self._num_authorities = view[offset + 8] << 8 | view[offset + 9] + self._num_additionals = view[offset + 10] << 8 | view[offset + 11] + + def _read_questions(self) -> None: + """Reads questions section of packet""" + view = self.view + questions = self._questions + for _ in range(self._num_questions): + name = self._read_name() + offset = self.offset + self.offset += 4 + # The question has 2 unsigned shorts in network order + type_ = view[offset] << 8 | view[offset + 1] + class_ = view[offset + 2] << 8 | view[offset + 3] + question = DNSQuestion.__new__(DNSQuestion) + question._fast_init(name, type_, class_) + if question.unique: # QU questions use the same bit as unique + self._has_qu_question = True + questions.append(question) + + def _read_character_string(self) -> str: + """Reads a character string from the packet""" + length = self.view[self.offset] + self.offset += 1 + # Python slicing silently truncates when indices exceed the buffer, + # but self.offset still advances by the declared length below; without + # this check a record with an inflated character-string length would + # land in the cache carrying a payload shorter than the wire claimed + # and leave the parser pointed past _data_len for the next record. + if self.offset + length > self._data_len: + raise IncomingDecodeError( + f"Character string length {length} at offset {self.offset} overruns " + f"packet of {self._data_len} bytes from {self.source}" + ) + info = self.data[self.offset : self.offset + length].decode("utf-8", "replace") + self.offset += length + return info + + def _read_string(self, length: _int) -> bytes: + """Reads a string of a given length from the packet""" + if self.offset + length > self._data_len: + raise IncomingDecodeError( + f"String length {length} at offset {self.offset} overruns " + f"packet of {self._data_len} bytes from {self.source}" + ) + info = self.data[self.offset : self.offset + length] + self.offset += length + return info + + def _read_others(self) -> None: + """Reads the answers, authorities and additionals section of the + packet""" + self._did_read_others = True + view = self.view + n = self._num_answers + self._num_authorities + self._num_additionals + for _ in range(n): + domain = self._read_name() + offset = self.offset + self.offset += 10 + # type_, class_ and length are unsigned shorts in network order + # ttl is an unsigned long in network order https://www.rfc-editor.org/errata/eid2130 + type_ = view[offset] << 8 | view[offset + 1] + class_ = view[offset + 2] << 8 | view[offset + 3] + ttl = view[offset + 4] << 24 | view[offset + 5] << 16 | view[offset + 6] << 8 | view[offset + 7] + length = view[offset + 8] << 8 | view[offset + 9] + end = self.offset + length + rec = None + try: + rec = self._read_record(domain, type_, class_, ttl, length) + except DECODE_EXCEPTIONS: + # Skip records that fail to decode if we know the length + # If the packet is really corrupt read_name and the unpack + # above would fail and hit the exception catch in read_others + self.offset = end + log.debug( + "Unable to parse; skipping record for %s with type %s at offset %d while unpacking %r", + domain, + _TYPES.get(type_, type_), + self.offset, + self.data, + exc_info=True, + ) + if rec is not None and self.offset != end: + # The decoded record consumed a different number of bytes than + # rdlength advertised. The record is built from a slice that + # straddles its rdata boundary, so drop it and resync to the + # declared end so the next record header lands aligned. + log.debug( + "Record for %s with type %s did not consume exactly rdlength=%d; dropping", + domain, + _TYPES.get(type_, type_), + length, + ) + self.offset = end + rec = None + if rec is not None: + self._answers.append(rec) + + def _read_record( + self, domain: _str, type_: _int, class_: _int, ttl: _int, length: _int + ) -> DNSRecord | None: + """Read known records types and skip unknown ones.""" + if type_ == _TYPE_A: + address_rec = DNSAddress.__new__(DNSAddress) + address_rec._fast_init(domain, type_, class_, ttl, self._read_string(4), None, self.now) + return address_rec + if type_ in (_TYPE_CNAME, _TYPE_PTR): + pointer_rec = DNSPointer.__new__(DNSPointer) + pointer_rec._fast_init(domain, type_, class_, ttl, self._read_name(), self.now) + return pointer_rec + if type_ == _TYPE_TXT: + text_rec = DNSText.__new__(DNSText) + text_rec._fast_init(domain, type_, class_, ttl, self._read_string(length), self.now) + return text_rec + if type_ == _TYPE_SRV: + view = self.view + offset = self.offset + self.offset += 6 + # The SRV record has 3 unsigned shorts in network order + priority = view[offset] << 8 | view[offset + 1] + weight = view[offset + 2] << 8 | view[offset + 3] + port = view[offset + 4] << 8 | view[offset + 5] + srv_rec = DNSService.__new__(DNSService) + srv_rec._fast_init( + domain, + type_, + class_, + ttl, + priority, + weight, + port, + self._read_name(), + self.now, + ) + return srv_rec + if type_ == _TYPE_HINFO: + hinfo_rec = DNSHinfo.__new__(DNSHinfo) + hinfo_rec._fast_init( + domain, + type_, + class_, + ttl, + self._read_character_string(), + self._read_character_string(), + self.now, + ) + return hinfo_rec + if type_ == _TYPE_AAAA: + address_rec = DNSAddress.__new__(DNSAddress) + address_rec._fast_init( + domain, + type_, + class_, + ttl, + self._read_string(16), + self.scope_id, + self.now, + ) + return address_rec + if type_ == _TYPE_NSEC: + name_start = self.offset + nsec_rec = DNSNsec.__new__(DNSNsec) + nsec_rec._fast_init( + domain, + type_, + class_, + ttl, + self._read_name(), + self._read_bitmap(name_start + length), + self.now, + ) + return nsec_rec + # Try to ignore types we don't know about + # Skip the payload for the resource record so the next + # records can be parsed correctly + self.offset += length + return None + + def _read_bitmap(self, end: _int) -> list[int]: + """Reads an NSEC bitmap from the packet.""" + rdtypes = [] + view = self.view + while self.offset < end: + offset = self.offset + offset_plus_one = offset + 1 + offset_plus_two = offset + 2 + # RFC 4034 §4.1.2: each window block is window-number byte + + # bitmap-length byte (1..32) + bitmap. A bitmap_length that walks + # past the record's declared end would otherwise leave self.offset + # pointing inside (or past) the next record header, corrupting + # every subsequent record in the same packet. + if offset_plus_two > end: + raise IncomingDecodeError( + f"NSEC bitmap window header truncated at offset {offset} from {self.source}" + ) + window = view[offset] + bitmap_length = view[offset_plus_one] + bitmap_end = offset_plus_two + bitmap_length + if bitmap_length == 0 or bitmap_length > 32 or bitmap_end > end: + raise IncomingDecodeError( + f"NSEC bitmap length {bitmap_length} invalid or overruns record end " + f"at offset {offset} from {self.source}" + ) + for i, byte in enumerate(self.data[offset_plus_two:bitmap_end]): + for bit in range(8): + if byte & (0x80 >> bit): + rdtypes.append(bit + window * 256 + i * 8) + self.offset += 2 + bitmap_length + return rdtypes + + def _read_name(self) -> str: + """Reads a domain name from the packet.""" + labels: list[str] = [] + seen_pointers: set[int] = set() + original_offset = self.offset + self.offset = self._decode_labels_at_offset(original_offset, labels, seen_pointers, 0) + self._name_cache[original_offset] = labels + name = ".".join(labels) + "." + if len(name) > MAX_NAME_LENGTH: + raise IncomingDecodeError( + f"DNS name {name} exceeds maximum length of {MAX_NAME_LENGTH} from {self.source}" + ) + return name + + def _decode_labels_at_offset( + self, off: _int, labels: list[str], seen_pointers: set[int], depth: _int + ) -> int: + # This is a tight loop that is called frequently, small optimizations can make a difference. + if depth > MAX_DNS_LABELS: + raise IncomingDecodeError( + f"DNS compression pointer chain exceeds {MAX_DNS_LABELS} at {off} from {self.source}" + ) + view = self.view + while off < self._data_len: + length = view[off] + if length == 0: + return off + DNS_COMPRESSION_HEADER_LEN + + if length < 0x40: + label_idx = off + DNS_COMPRESSION_HEADER_LEN + labels.append(self.data[label_idx : label_idx + length].decode("utf-8", "replace")) + off += DNS_COMPRESSION_HEADER_LEN + length + continue + + if length < 0xC0: + raise IncomingDecodeError( + f"DNS compression type {length} is unknown at {off} from {self.source}" + ) + + # We have a DNS compression pointer + link_data = view[off + 1] + link = (length & 0x3F) * 256 + link_data + link_py_int = link + if link > self._data_len: + raise IncomingDecodeError( + f"DNS compression pointer at {off} points to {link} beyond packet from {self.source}" + ) + if link == off: + raise IncomingDecodeError( + f"DNS compression pointer at {off} points to itself from {self.source}" + ) + if link_py_int in seen_pointers: + raise IncomingDecodeError( + f"DNS compression pointer at {off} was seen again from {self.source}" + ) + linked_labels = self._name_cache.get(link_py_int) + if not linked_labels: + linked_labels = [] + seen_pointers.add(link_py_int) + self._decode_labels_at_offset(link, linked_labels, seen_pointers, depth + 1) + self._name_cache[link_py_int] = linked_labels + labels.extend(linked_labels) + if len(labels) > MAX_DNS_LABELS: + raise IncomingDecodeError( + f"Maximum dns labels reached while processing pointer at {off} from {self.source}" + ) + return off + DNS_COMPRESSION_POINTER_LEN + + raise IncomingDecodeError(f"Corrupt packet received while decoding name from {self.source}") diff --git a/src/zeroconf/_protocol/outgoing.pxd b/src/zeroconf/_protocol/outgoing.pxd new file mode 100644 index 000000000..bb9730b89 --- /dev/null +++ b/src/zeroconf/_protocol/outgoing.pxd @@ -0,0 +1,144 @@ + +import cython + +from .._dns cimport DNSEntry, DNSPointer, DNSQuestion, DNSRecord +from .incoming cimport DNSIncoming + + +cdef cython.uint _CLASS_UNIQUE +cdef cython.uint _DNS_PACKET_HEADER_LEN +cdef cython.uint _FLAGS_QR_MASK +cdef cython.uint _FLAGS_QR_QUERY +cdef cython.uint _FLAGS_QR_RESPONSE +cdef cython.uint _FLAGS_TC +cdef cython.uint _MAX_MSG_ABSOLUTE +cdef cython.uint _MAX_MSG_TYPICAL + + +cdef bint TYPE_CHECKING + +cdef unsigned int SHORT_CACHE_MAX + +cdef object PACK_BYTE +cdef object PACK_SHORT +cdef object PACK_LONG + +cdef unsigned int STATE_INIT +cdef unsigned int STATE_FINISHED + +cdef object LOGGING_IS_ENABLED_FOR +cdef object LOGGING_DEBUG + +cdef cython.tuple BYTE_TABLE +cdef cython.tuple SHORT_LOOKUP +cdef cython.dict LONG_LOOKUP + +cdef class DNSOutgoing: + + cdef public unsigned int flags + cdef public bint finished + cdef public object id + cdef public bint multicast + cdef public cython.list packets_data + cdef public cython.dict names + cdef public cython.list data + cdef public unsigned int size + cdef public bint allow_long + cdef public unsigned int state + cdef public cython.list questions + cdef public cython.list answers + cdef public cython.list authorities + cdef public cython.list additionals + + cpdef void _reset_for_next_packet(self) + + cdef void _write_byte(self, cython.uint value) + + cdef void _insert_short_at_start(self, unsigned int value) + + cdef void _replace_short(self, cython.uint index, cython.uint value) + + cdef _get_short(self, cython.uint value) + + cdef void _write_int(self, object value) + + cdef cython.bint _write_question(self, DNSQuestion question) + + @cython.locals( + d=cython.bytes, + data_view=cython.list, + index=cython.uint, + length=cython.uint + ) + cdef cython.bint _write_record(self, DNSRecord record, double now) + + @cython.locals(class_=cython.uint) + cdef void _write_record_class(self, DNSEntry record) + + @cython.locals( + start_size_int=object + ) + cdef cython.bint _check_data_limit_or_rollback(self, cython.uint start_data_length, cython.uint start_size) + + @cython.locals(questions_written=cython.uint) + cdef cython.uint _write_questions_from_offset(self, unsigned int questions_offset) + + @cython.locals(answers_written=cython.uint) + cdef cython.uint _write_answers_from_offset(self, unsigned int answer_offset) + + @cython.locals(records_written=cython.uint) + cdef cython.uint _write_records_from_offset(self, cython.list records, unsigned int offset) + + cdef bint _has_more_to_add(self, unsigned int questions_offset, unsigned int answer_offset, unsigned int authority_offset, unsigned int additional_offset) + + cdef void _write_ttl(self, DNSRecord record, double now) + + @cython.locals( + labels=cython.list, + label=cython.str, + index=cython.uint, + start_size=cython.uint, + name_length=cython.uint, + ) + cpdef void write_name(self, cython.str name) + + cdef void _write_link_to_name(self, unsigned int index) + + cpdef void write_short(self, cython.uint value) + + cpdef void write_string(self, cython.bytes value) + + cpdef void write_character_string(self, cython.bytes value) + + @cython.locals(utfstr=bytes) + cdef void _write_utf(self, cython.str value) + + @cython.locals( + debug_enable=bint, + made_progress=bint, + has_more_to_add=bint, + questions_offset="unsigned int", + answer_offset="unsigned int", + authority_offset="unsigned int", + additional_offset="unsigned int", + questions_written="unsigned int", + answers_written="unsigned int", + authorities_written="unsigned int", + additionals_written="unsigned int", + ) + cpdef packets(self) + + cpdef void add_question(self, DNSQuestion question) + + cpdef void add_answer(self, DNSIncoming inp, DNSRecord record) + + @cython.locals(now_double=double) + cpdef void add_answer_at_time(self, DNSRecord record, double now) + + cpdef void add_authorative_answer(self, DNSPointer record) + + cpdef void add_additional_answer(self, DNSRecord record) + + cpdef bint is_query(self) + + cpdef bint is_response(self) diff --git a/src/zeroconf/_protocol/outgoing.py b/src/zeroconf/_protocol/outgoing.py new file mode 100644 index 000000000..fd5e57a02 --- /dev/null +++ b/src/zeroconf/_protocol/outgoing.py @@ -0,0 +1,510 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import enum +import logging +from collections.abc import Sequence +from struct import Struct +from typing import TYPE_CHECKING + +from .._dns import DNSPointer, DNSQuestion, DNSRecord +from .._exceptions import NamePartTooLongException +from .._logger import log +from ..const import ( + _CLASS_UNIQUE, + _DNS_HOST_TTL, + _DNS_OTHER_TTL, + _DNS_PACKET_HEADER_LEN, + _FLAGS_QR_MASK, + _FLAGS_QR_QUERY, + _FLAGS_QR_RESPONSE, + _FLAGS_TC, + _MAX_MSG_ABSOLUTE, + _MAX_MSG_TYPICAL, +) +from .incoming import DNSIncoming + +str_ = str +float_ = float +int_ = int +bytes_ = bytes +DNSQuestion_ = DNSQuestion +DNSRecord_ = DNSRecord + + +PACK_BYTE = Struct(">B").pack +PACK_SHORT = Struct(">H").pack +PACK_LONG = Struct(">L").pack + +SHORT_CACHE_MAX = 128 + +BYTE_TABLE = tuple(PACK_BYTE(i) for i in range(256)) +SHORT_LOOKUP = tuple(PACK_SHORT(i) for i in range(SHORT_CACHE_MAX)) +LONG_LOOKUP = {i: PACK_LONG(i) for i in (_DNS_OTHER_TTL, _DNS_HOST_TTL, 0)} + + +class State(enum.Enum): + init = 0 + finished = 1 + + +STATE_INIT = State.init.value +STATE_FINISHED = State.finished.value + +LOGGING_IS_ENABLED_FOR = log.isEnabledFor +LOGGING_DEBUG = logging.DEBUG + + +class DNSOutgoing: + """Object representation of an outgoing packet""" + + __slots__ = ( + "additionals", + "allow_long", + "answers", + "authorities", + "data", + "finished", + "flags", + "id", + "multicast", + "names", + "packets_data", + "questions", + "size", + "state", + ) + + def __init__(self, flags: int, multicast: bool = True, id_: int = 0) -> None: + self.flags = flags + self.finished = False + self.id = id_ + self.multicast = multicast + self.packets_data: list[bytes] = [] + + # these 3 are per-packet -- see also _reset_for_next_packet() + self.names: dict[str, int] = {} + self.data: list[bytes] = [] + self.size: int = _DNS_PACKET_HEADER_LEN + self.allow_long: bool = True + + self.state = STATE_INIT + + self.questions: list[DNSQuestion] = [] + self.answers: list[tuple[DNSRecord, float]] = [] + self.authorities: list[DNSPointer] = [] + self.additionals: list[DNSRecord] = [] + + def is_query(self) -> bool: + """Returns true if this is a query.""" + return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY + + def is_response(self) -> bool: + """Returns true if this is a response.""" + return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE + + def _reset_for_next_packet(self) -> None: + self.names = {} + self.data = [] + self.size = _DNS_PACKET_HEADER_LEN + self.allow_long = True + + def __repr__(self) -> str: + return "".format( + ", ".join( + [ + f"multicast={self.multicast}", + f"flags={self.flags}", + f"questions={self.questions}", + f"answers={self.answers}", + f"authorities={self.authorities}", + f"additionals={self.additionals}", + ] + ) + ) + + def add_question(self, record: DNSQuestion) -> None: + """Adds a question""" + self.questions.append(record) + + def add_answer(self, inp: DNSIncoming, record: DNSRecord) -> None: + """Adds an answer""" + if not record.suppressed_by(inp): + self.add_answer_at_time(record, 0.0) + + def add_answer_at_time(self, record: DNSRecord | None, now: float_) -> None: + """Adds an answer if it does not expire by a certain time""" + now_double = now + if record is not None and (now_double == 0 or not record.is_expired(now_double)): + self.answers.append((record, now)) + + def add_authorative_answer(self, record: DNSPointer) -> None: + """Adds an authoritative answer""" + self.authorities.append(record) + + def add_additional_answer(self, record: DNSRecord) -> None: + """Adds an additional answer + + From: RFC 6763, DNS-Based Service Discovery, February 2013 + + 12. DNS Additional Record Generation + + DNS has an efficiency feature whereby a DNS server may place + additional records in the additional section of the DNS message. + These additional records are records that the client did not + explicitly request, but the server has reasonable grounds to expect + that the client might request them shortly, so including them can + save the client from having to issue additional queries. + + This section recommends which additional records SHOULD be generated + to improve network efficiency, for both Unicast and Multicast DNS-SD + responses. + + 12.1. PTR Records + + When including a DNS-SD Service Instance Enumeration or Selective + Instance Enumeration (subtype) PTR record in a response packet, the + server/responder SHOULD include the following additional records: + + o The SRV record(s) named in the PTR rdata. + o The TXT record(s) named in the PTR rdata. + o All address records (type "A" and "AAAA") named in the SRV rdata. + + 12.2. SRV Records + + When including an SRV record in a response packet, the + server/responder SHOULD include the following additional records: + + o All address records (type "A" and "AAAA") named in the SRV rdata. + + """ + self.additionals.append(record) + + def _write_byte(self, value: int_) -> None: + """Writes a single byte to the packet""" + self.data.append(BYTE_TABLE[value]) + self.size += 1 + + def _get_short(self, value: int_) -> bytes: + """Convert an unsigned short to 2 bytes.""" + return SHORT_LOOKUP[value] if value < SHORT_CACHE_MAX else PACK_SHORT(value) + + def _insert_short_at_start(self, value: int_) -> None: + """Inserts an unsigned short at the start of the packet""" + self.data.insert(0, self._get_short(value)) + + def _replace_short(self, index: int_, value: int_) -> None: + """Replaces an unsigned short in a certain position in the packet""" + self.data[index] = self._get_short(value) + + def write_short(self, value: int_) -> None: + """Writes an unsigned short to the packet""" + self.data.append(self._get_short(value)) + self.size += 2 + + def _write_int(self, value: float | int) -> None: + """Writes an unsigned integer to the packet""" + value_as_int = int(value) + long_bytes = LONG_LOOKUP.get(value_as_int) + if long_bytes is not None: + self.data.append(long_bytes) + else: + self.data.append(PACK_LONG(value_as_int)) + self.size += 4 + + def write_string(self, value: bytes_) -> None: + """Writes a string to the packet""" + if TYPE_CHECKING: + assert isinstance(value, bytes) + self.data.append(value) + self.size += len(value) + + def _write_utf(self, s: str_) -> None: + """Writes a UTF-8 string of a given length to the packet""" + utfstr = s.encode("utf-8") + length = len(utfstr) + if length > 64: + raise NamePartTooLongException + self._write_byte(length) + self.write_string(utfstr) + + def write_character_string(self, value: bytes) -> None: + if TYPE_CHECKING: + assert isinstance(value, bytes) + length = len(value) + if length > 256: + raise NamePartTooLongException + self._write_byte(length) + self.write_string(value) + + def write_name(self, name: str_) -> None: + """ + Write names to packet + + 18.14. Name Compression + + When generating Multicast DNS messages, implementations SHOULD use + name compression wherever possible to compress the names of resource + records, by replacing some or all of the resource record name with a + compact two-byte reference to an appearance of that data somewhere + earlier in the message [RFC1035]. + """ + + # split name into each label + if name and name[-1] == ".": + name = name[:-1] + + index = self.names.get(name, 0) + if index: + self._write_link_to_name(index) + return + + start_size = self.size + labels = name.split(".") + # Write each new label or a pointer to the existing one in the packet + self.names[name] = start_size + self._write_utf(labels[0]) + + name_length = 0 + for count in range(1, len(labels)): + partial_name = ".".join(labels[count:]) + index = self.names.get(partial_name, 0) + if index: + self._write_link_to_name(index) + return + if name_length == 0: + name_length = len(name.encode("utf-8")) + self.names[partial_name] = start_size + name_length - len(partial_name.encode("utf-8")) + self._write_utf(labels[count]) + + # this is the end of a name + self._write_byte(0) + + def _write_link_to_name(self, index: int_) -> None: + # If part of the name already exists in the packet, + # create a pointer to it + self._write_byte((index >> 8) | 0xC0) + self._write_byte(index & 0xFF) + + def _write_question(self, question: DNSQuestion_) -> bool: + """Writes a question to the packet""" + start_data_length = len(self.data) + start_size = self.size + self.write_name(question.name) + self.write_short(question.type) + self._write_record_class(question) + return self._check_data_limit_or_rollback(start_data_length, start_size) + + def _write_record_class(self, record: DNSQuestion_ | DNSRecord_) -> None: + """Write out the record class including the unique/unicast (QU) bit.""" + class_ = record.class_ + if record.unique is True and self.multicast: + self.write_short(class_ | _CLASS_UNIQUE) + else: + self.write_short(class_) + + def _write_ttl(self, record: DNSRecord_, now: float_) -> None: + """Write out the record ttl.""" + self._write_int(record.ttl if now == 0 else record.get_remaining_ttl(now)) + + def _write_record(self, record: DNSRecord_, now: float_) -> bool: + """Writes a record (answer, authoritative answer, additional) to + the packet. Returns True on success, or False if we did not + because the packet because the record does not fit.""" + start_data_length = len(self.data) + start_size = self.size + self.write_name(record.name) + self.write_short(record.type) + self._write_record_class(record) + self._write_ttl(record, now) + index = len(self.data) + self.write_short(0) # Will get replaced with the actual size + record.write(self) + # Adjust size for the short we will write before this record + length = 0 + for d in self.data[index + 1 :]: + length += len(d) + # Here we replace the 0 length short we wrote + # before with the actual length + self._replace_short(index, length) + return self._check_data_limit_or_rollback(start_data_length, start_size) + + def _check_data_limit_or_rollback(self, start_data_length: int_, start_size: int_) -> bool: + """Check data limit, if we go over, then rollback and return False.""" + len_limit = _MAX_MSG_ABSOLUTE if self.allow_long else _MAX_MSG_TYPICAL + self.allow_long = False + + if self.size <= len_limit: + return True + + if LOGGING_IS_ENABLED_FOR(LOGGING_DEBUG): # pragma: no branch + log.debug( + "Reached data limit (size=%d) > (limit=%d) - rolling back", + self.size, + len_limit, + ) + del self.data[start_data_length:] + self.size = start_size + + start_size_int = start_size + rollback_names = [name for name, idx in self.names.items() if idx >= start_size_int] + for name in rollback_names: + del self.names[name] + return False + + def _write_questions_from_offset(self, questions_offset: int_) -> int: + questions_written = 0 + for question in self.questions[questions_offset:]: + if not self._write_question(question): + break + questions_written += 1 + return questions_written + + def _write_answers_from_offset(self, answer_offset: int_) -> int: + answers_written = 0 + for answer, time_ in self.answers[answer_offset:]: + if not self._write_record(answer, time_): + break + answers_written += 1 + return answers_written + + def _write_records_from_offset(self, records: Sequence[DNSRecord], offset: int_) -> int: + records_written = 0 + for record in records[offset:]: + if not self._write_record(record, 0): + break + records_written += 1 + return records_written + + def _has_more_to_add( + self, + questions_offset: int_, + answer_offset: int_, + authority_offset: int_, + additional_offset: int_, + ) -> bool: + """Check if all questions, answers, authority, and additionals have been written to the packet.""" + return ( + questions_offset < len(self.questions) + or answer_offset < len(self.answers) + or authority_offset < len(self.authorities) + or additional_offset < len(self.additionals) + ) + + def packets(self) -> list[bytes]: + """Returns a list of bytestrings containing the packets' bytes + + No further parts should be added to the packet once this + is done. The packets are each restricted to _MAX_MSG_TYPICAL + or less in length, except for the case of a single answer which + will be written out to a single oversized packet no more than + _MAX_MSG_ABSOLUTE in length (and hence will be subject to IP + fragmentation potentially).""" + packets_data = self.packets_data + + if self.state == STATE_FINISHED: + return packets_data + + questions_offset = 0 + answer_offset = 0 + authority_offset = 0 + additional_offset = 0 + # we have to at least write out the question + debug_enable = LOGGING_IS_ENABLED_FOR(LOGGING_DEBUG) is True + has_more_to_add = True + + while has_more_to_add: + if debug_enable: + log.debug( + "offsets = questions=%d, answers=%d, authorities=%d, additionals=%d", + questions_offset, + answer_offset, + authority_offset, + additional_offset, + ) + log.debug( + "lengths = questions=%d, answers=%d, authorities=%d, additionals=%d", + len(self.questions), + len(self.answers), + len(self.authorities), + len(self.additionals), + ) + + questions_written = self._write_questions_from_offset(questions_offset) + answers_written = self._write_answers_from_offset(answer_offset) + authorities_written = self._write_records_from_offset(self.authorities, authority_offset) + additionals_written = self._write_records_from_offset(self.additionals, additional_offset) + + made_progress = bool(self.data) + + self._insert_short_at_start(additionals_written) + self._insert_short_at_start(authorities_written) + self._insert_short_at_start(answers_written) + self._insert_short_at_start(questions_written) + + questions_offset += questions_written + answer_offset += answers_written + authority_offset += authorities_written + additional_offset += additionals_written + if debug_enable: + log.debug( + "now offsets = questions=%d, answers=%d, authorities=%d, additionals=%d", + questions_offset, + answer_offset, + authority_offset, + additional_offset, + ) + + has_more_to_add = self._has_more_to_add( + questions_offset, answer_offset, authority_offset, additional_offset + ) + + if has_more_to_add and self.is_query(): + # https://datatracker.ietf.org/doc/html/rfc6762#section-7.2 + if debug_enable: # pragma: no branch + log.debug("Setting TC flag") + self._insert_short_at_start(self.flags | _FLAGS_TC) + else: + self._insert_short_at_start(self.flags) + + if self.multicast: + self._insert_short_at_start(0) + else: + self._insert_short_at_start(self.id) + + packets_data.append(b"".join(self.data)) + + if not made_progress: + # Generating an empty packet is not a desirable outcome, but currently + # too many internals rely on this behavior. So, we'll just return an + # empty packet and log a warning until this can be refactored at a later + # date. + log.warning("packets() made no progress adding records; returning") + break + + if has_more_to_add: + self._reset_for_next_packet() + + self.state = STATE_FINISHED + return packets_data diff --git a/src/zeroconf/_record_update.pxd b/src/zeroconf/_record_update.pxd new file mode 100644 index 000000000..1562299b2 --- /dev/null +++ b/src/zeroconf/_record_update.pxd @@ -0,0 +1,12 @@ + +import cython + +from ._dns cimport DNSRecord + + +cdef class RecordUpdate: + + cdef public DNSRecord new + cdef public DNSRecord old + + cdef void _fast_init(self, object new, object old) diff --git a/src/zeroconf/_record_update.py b/src/zeroconf/_record_update.py new file mode 100644 index 000000000..497ee39df --- /dev/null +++ b/src/zeroconf/_record_update.py @@ -0,0 +1,48 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from ._dns import DNSRecord + +_DNSRecord = DNSRecord + + +class RecordUpdate: + __slots__ = ("new", "old") + + def __init__(self, new: DNSRecord, old: DNSRecord | None = None) -> None: + """RecordUpdate represents a change in a DNS record.""" + self._fast_init(new, old) + + def _fast_init(self, new: _DNSRecord, old: _DNSRecord | None) -> None: + """Fast init for RecordUpdate.""" + self.new = new + self.old = old + + def __getitem__(self, index: int) -> DNSRecord | None: + """Get the new or old record.""" + if index == 0: + return self.new + if index == 1: + return self.old + raise IndexError(index) diff --git a/src/zeroconf/_services/__init__.pxd b/src/zeroconf/_services/__init__.pxd new file mode 100644 index 000000000..46a75f3c5 --- /dev/null +++ b/src/zeroconf/_services/__init__.pxd @@ -0,0 +1,11 @@ + +import cython + + +cdef class Signal: + + cdef list _handlers + +cdef class SignalRegistrationInterface: + + cdef list _handlers diff --git a/src/zeroconf/_services/__init__.py b/src/zeroconf/_services/__init__.py new file mode 100644 index 000000000..cc794dc68 --- /dev/null +++ b/src/zeroconf/_services/__init__.py @@ -0,0 +1,78 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import enum +from collections.abc import Callable +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from .._core import Zeroconf + + +@enum.unique +class ServiceStateChange(enum.Enum): + Added = 1 + Removed = 2 + Updated = 3 + + +class ServiceListener: + def add_service(self, zc: Zeroconf, type_: str, name: str) -> None: + raise NotImplementedError + + def remove_service(self, zc: Zeroconf, type_: str, name: str) -> None: + raise NotImplementedError + + def update_service(self, zc: Zeroconf, type_: str, name: str) -> None: + raise NotImplementedError + + +class Signal: + __slots__ = ("_handlers",) + + def __init__(self) -> None: + self._handlers: list[Callable[..., None]] = [] + + def fire(self, **kwargs: Any) -> None: + for h in self._handlers[:]: + h(**kwargs) + + @property + def registration_interface(self) -> SignalRegistrationInterface: + return SignalRegistrationInterface(self._handlers) + + +class SignalRegistrationInterface: + __slots__ = ("_handlers",) + + def __init__(self, handlers: list[Callable[..., None]]) -> None: + self._handlers = handlers + + def register_handler(self, handler: Callable[..., None]) -> SignalRegistrationInterface: + self._handlers.append(handler) + return self + + def unregister_handler(self, handler: Callable[..., None]) -> SignalRegistrationInterface: + self._handlers.remove(handler) + return self diff --git a/src/zeroconf/_services/browser.pxd b/src/zeroconf/_services/browser.pxd new file mode 100644 index 000000000..ef9dcafc3 --- /dev/null +++ b/src/zeroconf/_services/browser.pxd @@ -0,0 +1,122 @@ + +import cython + +from .._cache cimport DNSCache +from .._history cimport QuestionHistory +from .._protocol.outgoing cimport DNSOutgoing, DNSPointer, DNSQuestion, DNSRecord +from .._record_update cimport RecordUpdate +from .._updates cimport RecordUpdateListener +from .._utils.time cimport current_time_millis, millis_to_seconds +from . cimport Signal, SignalRegistrationInterface + + +cdef bint TYPE_CHECKING +cdef object cached_possible_types +cdef cython.uint _EXPIRE_REFRESH_TIME_PERCENT, _MAX_MSG_TYPICAL, _DNS_PACKET_HEADER_LEN +cdef cython.uint _TYPE_PTR +cdef cython.uint _TYPE_NSEC +cdef object _CLASS_IN +cdef object SERVICE_STATE_CHANGE_ADDED, SERVICE_STATE_CHANGE_REMOVED, SERVICE_STATE_CHANGE_UPDATED +cdef cython.set _ADDRESS_RECORD_TYPES +cdef float RESCUE_RECORD_RETRY_TTL_PERCENTAGE + +cdef object _MDNS_PORT, _BROWSER_TIME + +cdef object QU_QUESTION + +cdef object _FLAGS_QR_QUERY + +cdef object heappop, heappush + +cdef class _ScheduledPTRQuery: + + cdef public str alias + cdef public str name + cdef public unsigned int ttl + cdef public bint cancelled + cdef public double expire_time_millis + cdef public double when_millis + +cdef class _DNSPointerOutgoingBucket: + + cdef public double now_millis + cdef public DNSOutgoing out + cdef public cython.uint bytes + + cpdef add(self, cython.uint max_compressed_size, DNSQuestion question, cython.set answers) + + +@cython.locals(cache=DNSCache, question_history=QuestionHistory, record=DNSRecord, qu_question=bint) +cpdef list generate_service_query( + object zc, + double now_millis, + set types_, + bint multicast, + object question_type +) + + +@cython.locals(answer=DNSPointer, query_buckets=list, question=DNSQuestion, max_compressed_size=cython.uint, max_bucket_size=cython.uint, query_bucket=_DNSPointerOutgoingBucket) +cdef list _group_ptr_queries_with_known_answers(double now_millis, bint multicast, cython.dict question_with_known_answers) + + +cdef class QueryScheduler: + + cdef object _zc + cdef set _types + cdef str _addr + cdef int _port + cdef bint _multicast + cdef tuple _first_random_delay_interval + cdef double _min_time_between_queries_millis + cdef object _loop + cdef unsigned int _startup_queries_sent + cdef public dict _next_scheduled_for_alias + cdef public list _query_heap + cdef object _next_run + cdef double _clock_resolution_millis + cdef object _question_type + + cdef void _schedule_ptr_refresh(self, DNSPointer pointer, double expire_time_millis, double refresh_time_millis) + + cdef void _schedule_ptr_query(self, _ScheduledPTRQuery scheduled_query) + + @cython.locals(scheduled=_ScheduledPTRQuery) + cpdef void cancel_ptr_refresh(self, DNSPointer pointer) + + @cython.locals(current=_ScheduledPTRQuery, expire_time=double) + cpdef void reschedule_ptr_first_refresh(self, DNSPointer pointer) + + @cython.locals(ttl_millis="unsigned int", additional_wait=double, next_query_time=double) + cpdef void schedule_rescue_query(self, _ScheduledPTRQuery query, double now_millis, float additional_percentage) + + cpdef void _process_startup_queries(self) + + @cython.locals(query=_ScheduledPTRQuery, next_scheduled=_ScheduledPTRQuery, next_when=double) + cpdef void _process_ready_types(self) + + cpdef void async_send_ready_queries(self, bint first_request, double now_millis, set ready_types) + + +cdef class _ServiceBrowserBase(RecordUpdateListener): + + cdef public cython.set types + cdef public object zc + cdef DNSCache _cache + cdef object _loop + cdef public cython.dict _pending_handlers + cdef public object _service_state_changed + cdef public QueryScheduler query_scheduler + cdef public bint done + cdef public object _query_sender_task + + cpdef void _enqueue_callback(self, object state_change, object type_, object name) + + @cython.locals(record_update=RecordUpdate, record=DNSRecord, cache=DNSCache, service=DNSRecord, pointer=DNSPointer) + cpdef void async_update_records(self, object zc, double now, cython.list records) + + cpdef cython.list _names_matching_types(self, object types) + + cpdef _fire_service_state_changed_event(self, cython.tuple event) + + cpdef void async_update_records_complete(self) diff --git a/src/zeroconf/_services/browser.py b/src/zeroconf/_services/browser.py new file mode 100644 index 000000000..ed70793f8 --- /dev/null +++ b/src/zeroconf/_services/browser.py @@ -0,0 +1,843 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import contextlib +import heapq +import queue +import random +import threading +import time +import warnings +from collections.abc import Callable, Iterable +from functools import partial +from types import TracebackType # used in type hints +from typing import ( + TYPE_CHECKING, + Any, + cast, +) + +from .._dns import DNSPointer, DNSQuestion, DNSQuestionType +from .._logger import log +from .._protocol.outgoing import DNSOutgoing +from .._record_update import RecordUpdate +from .._services import ( + ServiceListener, + ServiceStateChange, + Signal, + SignalRegistrationInterface, +) +from .._updates import RecordUpdateListener +from .._utils.name import cached_possible_types, service_type_name +from .._utils.time import current_time_millis, millis_to_seconds +from ..const import ( + _ADDRESS_RECORD_TYPES, + _BROWSER_TIME, + _CLASS_IN, + _DNS_PACKET_HEADER_LEN, + _EXPIRE_REFRESH_TIME_PERCENT, + _FLAGS_QR_QUERY, + _MAX_MSG_TYPICAL, + _MDNS_ADDR, + _MDNS_ADDR6, + _MDNS_PORT, + _TYPE_NSEC, + _TYPE_PTR, +) + +# https://datatracker.ietf.org/doc/html/rfc6762#section-5.2 +_FIRST_QUERY_DELAY_RANDOM_INTERVAL = (20, 120) # ms + +_ON_CHANGE_DISPATCH = { + ServiceStateChange.Added: "add_service", + ServiceStateChange.Removed: "remove_service", + ServiceStateChange.Updated: "update_service", +} + +SERVICE_STATE_CHANGE_ADDED = ServiceStateChange.Added +SERVICE_STATE_CHANGE_REMOVED = ServiceStateChange.Removed +SERVICE_STATE_CHANGE_UPDATED = ServiceStateChange.Updated + +QU_QUESTION = DNSQuestionType.QU + +STARTUP_QUERIES = 4 + +RESCUE_RECORD_RETRY_TTL_PERCENTAGE = 0.1 + +if TYPE_CHECKING: + from .._core import Zeroconf + +float_ = float +int_ = int +bool_ = bool +str_ = str + +_QuestionWithKnownAnswers = dict[DNSQuestion, set[DNSPointer]] + +heappop = heapq.heappop +heappush = heapq.heappush + + +class _ScheduledPTRQuery: # noqa: PLW1641 + __slots__ = ( + "alias", + "cancelled", + "expire_time_millis", + "name", + "ttl", + "when_millis", + ) + + def __init__( + self, + alias: str, + name: str, + ttl: int, + expire_time_millis: float, + when_millis: float, + ) -> None: + """Create a scheduled query.""" + self.alias = alias + self.name = name + self.ttl = ttl + # Since queries are stored in a heap we need to track if they are cancelled + # so we can remove them from the heap when they are cancelled as it would + # be too expensive to search the heap for the record to remove and instead + # we just mark it as cancelled and ignore it when we pop it off the heap + # when the query is due. + self.cancelled = False + # Expire time millis is the actual millisecond time the record will expire + self.expire_time_millis = expire_time_millis + # When millis is the millisecond time the query should be sent + # For the first query this is the refresh time which is 75% of the TTL + # + # For subsequent queries we increase the time by 10% of the TTL + # until we reach the expire time and then we stop because it means + # we failed to rescue the record. + self.when_millis = when_millis + + def __repr__(self) -> str: + """Return a string representation of the scheduled query.""" + return ( + f"<{self.__class__.__name__} " + f"alias={self.alias} " + f"name={self.name} " + f"ttl={self.ttl} " + f"cancelled={self.cancelled} " + f"expire_time_millis={self.expire_time_millis} " + f"when_millis={self.when_millis}" + ">" + ) + + def __lt__(self, other: _ScheduledPTRQuery) -> bool: + """Compare two scheduled queries.""" + if type(other) is _ScheduledPTRQuery: + return self.when_millis < other.when_millis + return NotImplemented + + def __le__(self, other: _ScheduledPTRQuery) -> bool: + """Compare two scheduled queries.""" + if type(other) is _ScheduledPTRQuery: + return self.when_millis < other.when_millis or self.__eq__(other) + return NotImplemented + + def __eq__(self, other: Any) -> bool: + """Compare two scheduled queries.""" + if type(other) is _ScheduledPTRQuery: + return self.when_millis == other.when_millis + return NotImplemented + + def __ge__(self, other: _ScheduledPTRQuery) -> bool: + """Compare two scheduled queries.""" + if type(other) is _ScheduledPTRQuery: + return self.when_millis > other.when_millis or self.__eq__(other) + return NotImplemented + + def __gt__(self, other: _ScheduledPTRQuery) -> bool: + """Compare two scheduled queries.""" + if type(other) is _ScheduledPTRQuery: + return self.when_millis > other.when_millis + return NotImplemented + + +class _DNSPointerOutgoingBucket: + """A DNSOutgoing bucket.""" + + __slots__ = ("bytes", "now_millis", "out") + + def __init__(self, now_millis: float, multicast: bool) -> None: + """Create a bucket to wrap a DNSOutgoing.""" + self.now_millis = now_millis + self.out = DNSOutgoing(_FLAGS_QR_QUERY, multicast) + self.bytes = 0 + + def add(self, max_compressed_size: int_, question: DNSQuestion, answers: set[DNSPointer]) -> None: + """Add a new set of questions and known answers to the outgoing.""" + self.out.add_question(question) + for answer in answers: + self.out.add_answer_at_time(answer, self.now_millis) + self.bytes += max_compressed_size + + +def group_ptr_queries_with_known_answers( + now: float_, + multicast: bool_, + question_with_known_answers: _QuestionWithKnownAnswers, +) -> list[DNSOutgoing]: + """Aggregate queries so that as many known answers as possible fit in the same packet + without having known answers spill over into the next packet unless the + question and known answers are always going to exceed the packet size. + + Some responders do not implement multi-packet known answer suppression + so we try to keep all the known answers in the same packet as the + questions. + """ + return _group_ptr_queries_with_known_answers(now, multicast, question_with_known_answers) + + +def _group_ptr_queries_with_known_answers( + now_millis: float_, + multicast: bool_, + question_with_known_answers: _QuestionWithKnownAnswers, +) -> list[DNSOutgoing]: + """Inner wrapper for group_ptr_queries_with_known_answers.""" + # This is the maximum size the query + known answers can be with name compression. + # The actual size of the query + known answers may be a bit smaller since other + # parts may be shared when the final DNSOutgoing packets are constructed. The + # goal of this algorithm is to quickly bucket the query + known answers without + # the overhead of actually constructing the packets. + query_by_size: dict[DNSQuestion, int] = { + question: (question.max_size + sum(answer.max_size_compressed for answer in known_answers)) + for question, known_answers in question_with_known_answers.items() + } + max_bucket_size = _MAX_MSG_TYPICAL - _DNS_PACKET_HEADER_LEN + query_buckets: list[_DNSPointerOutgoingBucket] = [] + for question in sorted( + query_by_size, + key=query_by_size.get, # type: ignore + reverse=True, + ): + max_compressed_size = query_by_size[question] + answers = question_with_known_answers[question] + for query_bucket in query_buckets: + if query_bucket.bytes + max_compressed_size <= max_bucket_size: + query_bucket.add(max_compressed_size, question, answers) + break + else: + # If a single question and known answers won't fit in a packet + # we will end up generating multiple packets, but there will never + # be multiple questions + query_bucket = _DNSPointerOutgoingBucket(now_millis, multicast) + query_bucket.add(max_compressed_size, question, answers) + query_buckets.append(query_bucket) + + return [query_bucket.out for query_bucket in query_buckets] + + +def generate_service_query( + zc: Zeroconf, + now_millis: float_, + types_: set[str], + multicast: bool, + question_type: DNSQuestionType | None, +) -> list[DNSOutgoing]: + """Generate a service query for sending with zeroconf.send.""" + questions_with_known_answers: _QuestionWithKnownAnswers = {} + qu_question = not multicast if question_type is None else question_type is QU_QUESTION + question_history = zc.question_history + cache = zc.cache + for type_ in types_: + question = DNSQuestion(type_, _TYPE_PTR, _CLASS_IN) + question.unicast = qu_question + known_answers = { + record + for record in cache.get_all_by_details(type_, _TYPE_PTR, _CLASS_IN) + if not record.is_stale(now_millis) + } + if not qu_question and question_history.suppresses(question, now_millis, known_answers): + log.debug("Asking %s was suppressed by the question history", question) + continue + if TYPE_CHECKING: + pointer_known_answers = cast(set[DNSPointer], known_answers) + else: + pointer_known_answers = known_answers + questions_with_known_answers[question] = pointer_known_answers + if not qu_question: + question_history.add_question_at_time(question, now_millis, known_answers) + + return _group_ptr_queries_with_known_answers(now_millis, multicast, questions_with_known_answers) + + +def _on_change_dispatcher( + listener: ServiceListener, + zeroconf: Zeroconf, + service_type: str, + name: str, + state_change: ServiceStateChange, +) -> None: + """Dispatch a service state change to a listener.""" + getattr(listener, _ON_CHANGE_DISPATCH[state_change])(zeroconf, service_type, name) + + +def _service_state_changed_from_listener( + listener: ServiceListener, +) -> Callable[..., None]: + """Generate a service_state_changed handlers from a listener.""" + assert listener is not None + if not hasattr(listener, "update_service"): + warnings.warn( + f"{listener!r} has no update_service method. Provide one (it can be empty if you " + "don't care about the updates), it'll become mandatory.", + FutureWarning, + stacklevel=1, + ) + return partial(_on_change_dispatcher, listener) + + +class QueryScheduler: + """Schedule outgoing PTR queries for Continuous Multicast DNS Querying + + https://datatracker.ietf.org/doc/html/rfc6762#section-5.2 + + """ + + __slots__ = ( + "_addr", + "_clock_resolution_millis", + "_first_random_delay_interval", + "_loop", + "_min_time_between_queries_millis", + "_multicast", + "_next_run", + "_next_scheduled_for_alias", + "_port", + "_query_heap", + "_question_type", + "_startup_queries_sent", + "_types", + "_zc", + ) + + def __init__( + self, + zc: Zeroconf, + types: set[str], + addr: str | None, + port: int, + multicast: bool, + delay: int, + first_random_delay_interval: tuple[int, int], + question_type: DNSQuestionType | None, + ) -> None: + self._zc = zc + self._types = types + self._addr = addr + self._port = port + self._multicast = multicast + self._first_random_delay_interval = first_random_delay_interval + self._min_time_between_queries_millis = delay + self._loop: asyncio.AbstractEventLoop | None = None + self._startup_queries_sent = 0 + self._next_scheduled_for_alias: dict[str, _ScheduledPTRQuery] = {} + self._query_heap: list[_ScheduledPTRQuery] = [] + self._next_run: asyncio.TimerHandle | None = None + self._clock_resolution_millis = time.get_clock_info("monotonic").resolution * 1000 + self._question_type = question_type + + def start(self, loop: asyncio.AbstractEventLoop) -> None: + """Start the scheduler. + + https://datatracker.ietf.org/doc/html/rfc6762#section-5.2 + To avoid accidental synchronization when, for some reason, multiple + clients begin querying at exactly the same moment (e.g., because of + some common external trigger event), a Multicast DNS querier SHOULD + also delay the first query of the series by a randomly chosen amount + in the range 20-120 ms. + """ + start_delay = millis_to_seconds(random.randint(*self._first_random_delay_interval)) # noqa: S311 + self._loop = loop + self._next_run = loop.call_later(start_delay, self._process_startup_queries) + + def stop(self) -> None: + """Stop the scheduler.""" + if self._next_run is not None: + self._next_run.cancel() + self._next_run = None + self._next_scheduled_for_alias.clear() + self._query_heap.clear() + + def _schedule_ptr_refresh( + self, + pointer: DNSPointer, + expire_time_millis: float_, + refresh_time_millis: float_, + ) -> None: + """Schedule a query for a pointer.""" + scheduled_ptr_query = _ScheduledPTRQuery( + pointer.alias, pointer.name, pointer.ttl, expire_time_millis, refresh_time_millis + ) + self._schedule_ptr_query(scheduled_ptr_query) + + def _schedule_ptr_query(self, scheduled_query: _ScheduledPTRQuery) -> None: + """Schedule a query for a pointer.""" + self._next_scheduled_for_alias[scheduled_query.alias] = scheduled_query + heappush(self._query_heap, scheduled_query) + + def cancel_ptr_refresh(self, pointer: DNSPointer) -> None: + """Cancel a query for a pointer.""" + scheduled = self._next_scheduled_for_alias.pop(pointer.alias, None) + if scheduled: + scheduled.cancelled = True + + def reschedule_ptr_first_refresh(self, pointer: DNSPointer) -> None: + """Reschedule a query for a pointer.""" + current = self._next_scheduled_for_alias.get(pointer.alias) + refresh_time_millis = pointer.get_expiration_time(_EXPIRE_REFRESH_TIME_PERCENT) + if current is not None: + # If the expire time is within self._min_time_between_queries_millis + # of the current scheduled time avoid churn by not rescheduling + if ( + -self._min_time_between_queries_millis + <= refresh_time_millis - current.when_millis + <= self._min_time_between_queries_millis + ): + return + current.cancelled = True + del self._next_scheduled_for_alias[pointer.alias] + expire_time_millis = pointer.get_expiration_time(100) + self._schedule_ptr_refresh(pointer, expire_time_millis, refresh_time_millis) + + def schedule_rescue_query( + self, + query: _ScheduledPTRQuery, + now_millis: float_, + additional_percentage: float_, + ) -> None: + """Reschedule a query for a pointer at an additional percentage of expiration.""" + ttl_millis = query.ttl * 1000 + additional_wait = ttl_millis * additional_percentage + next_query_time = now_millis + additional_wait + if next_query_time >= query.expire_time_millis: + # If we would schedule past the expire time + # there is no point in scheduling as we already + # tried to rescue the record and failed + return + scheduled_ptr_query = _ScheduledPTRQuery( + query.alias, + query.name, + query.ttl, + query.expire_time_millis, + next_query_time, + ) + self._schedule_ptr_query(scheduled_ptr_query) + + def _process_startup_queries(self) -> None: + if TYPE_CHECKING: + assert self._loop is not None + # This is a safety to ensure we stop sending queries if Zeroconf instance + # is stopped without the browser being cancelled + if self._zc.done: + return + + now_millis = current_time_millis() + + # At first we will send STARTUP_QUERIES queries to get the cache populated + self.async_send_ready_queries(self._startup_queries_sent == 0, now_millis, self._types) + self._startup_queries_sent += 1 + + # Once we finish sending the initial queries we will + # switch to a strategy of sending queries only when we + # need to refresh records that are about to expire + if self._startup_queries_sent >= STARTUP_QUERIES: + self._next_run = self._loop.call_at( + millis_to_seconds(now_millis + self._min_time_between_queries_millis), + self._process_ready_types, + ) + return + + self._next_run = self._loop.call_later(self._startup_queries_sent**2, self._process_startup_queries) + + def _process_ready_types(self) -> None: + """Generate a list of ready types that is due and schedule the next time.""" + if TYPE_CHECKING: + assert self._loop is not None + # This is a safety to ensure we stop sending queries if Zeroconf instance + # is stopped without the browser being cancelled + if self._zc.done: + return + + now_millis = current_time_millis() + # Refresh records that are about to expire (aka + # _EXPIRE_REFRESH_TIME_PERCENT which is currently 75% of the TTL) and + # additional rescue queries if the 75% query failed to refresh the record + # with a minimum time between queries of _min_time_between_queries + # which defaults to 10s + + ready_types: set[str] = set() + next_scheduled: _ScheduledPTRQuery | None = None + end_time_millis = now_millis + self._clock_resolution_millis + schedule_rescue: list[_ScheduledPTRQuery] = [] + + while self._query_heap: + query = self._query_heap[0] + if query.cancelled: + heappop(self._query_heap) + continue + if query.when_millis > end_time_millis: + next_scheduled = query + break + query = heappop(self._query_heap) + ready_types.add(query.name) + del self._next_scheduled_for_alias[query.alias] + # If there is still more than 10% of the TTL remaining + # schedule a query again to try to rescue the record + # from expiring. If the record is refreshed before + # the query, the query will get cancelled. + schedule_rescue.append(query) + + for query in schedule_rescue: + self.schedule_rescue_query(query, now_millis, RESCUE_RECORD_RETRY_TTL_PERCENTAGE) + + if ready_types: + self.async_send_ready_queries(False, now_millis, ready_types) + + next_time_millis = now_millis + self._min_time_between_queries_millis + + if next_scheduled is not None and next_scheduled.when_millis > next_time_millis: + next_when_millis = next_scheduled.when_millis + else: + next_when_millis = next_time_millis + + self._next_run = self._loop.call_at(millis_to_seconds(next_when_millis), self._process_ready_types) + + def async_send_ready_queries( + self, first_request: bool, now_millis: float_, ready_types: set[str] + ) -> None: + """Send any ready queries.""" + # If they did not specify and this is the first request, ask QU questions + # https://datatracker.ietf.org/doc/html/rfc6762#section-5.4 since we are + # just starting up and we know our cache is likely empty. This ensures + # the next outgoing will be sent with the known answers list. + question_type = QU_QUESTION if self._question_type is None and first_request else self._question_type + outs = generate_service_query(self._zc, now_millis, ready_types, self._multicast, question_type) + if outs: + for out in outs: + self._zc.async_send(out, self._addr, self._port) + + +class _ServiceBrowserBase(RecordUpdateListener): + """Base class for ServiceBrowser.""" + + __slots__ = ( + "_cache", + "_loop", + "_pending_handlers", + "_query_sender_task", + "_service_state_changed", + "done", + "query_scheduler", + "types", + "zc", + ) + + def __init__( + self, + zc: Zeroconf, + type_: str | list, + handlers: ServiceListener | list[Callable[..., None]] | None = None, + listener: ServiceListener | None = None, + addr: str | None = None, + port: int = _MDNS_PORT, + delay: int = _BROWSER_TIME, + question_type: DNSQuestionType | None = None, + ) -> None: + """Used to browse for a service for specific type(s). + + Constructor parameters are as follows: + + * `zc`: A Zeroconf instance + * `type_`: fully qualified service type name + * `handler`: ServiceListener or Callable that knows how to process ServiceStateChange events + * `listener`: ServiceListener + * `addr`: address to send queries (will default to multicast) + * `port`: port to send queries (will default to mdns 5353) + * `delay`: The initial delay between answering questions + * `question_type`: The type of questions to ask (DNSQuestionType.QM or DNSQuestionType.QU) + + The listener object will have its add_service() and + remove_service() methods called when this browser + discovers changes in the services availability. + """ + assert handlers or listener, "You need to specify at least one handler" + self.types: set[str] = set(type_ if isinstance(type_, list) else [type_]) + for check_type_ in self.types: + # Will generate BadTypeInNameException on a bad name + service_type_name(check_type_, strict=False) + self.zc = zc + self._cache = zc.cache + assert zc.loop is not None + self._loop = zc.loop + self._pending_handlers: dict[tuple[str, str], ServiceStateChange] = {} + self._service_state_changed = Signal() + self.query_scheduler = QueryScheduler( + zc, + self.types, + addr, + port, + addr in (None, _MDNS_ADDR, _MDNS_ADDR6), + delay, + _FIRST_QUERY_DELAY_RANDOM_INTERVAL, + question_type, + ) + self.done = False + self._query_sender_task: asyncio.Task | None = None + + if hasattr(handlers, "add_service"): + listener = cast(ServiceListener, handlers) + handlers = None + + handlers = cast(list[Callable[..., None]], handlers or []) + + if listener: + handlers.append(_service_state_changed_from_listener(listener)) + + for h in handlers: + self.service_state_changed.register_handler(h) + + def _async_start(self) -> None: + """Generate the next time and setup listeners. + + Must be called by uses of this base class after they + have finished setting their properties. + """ + self.zc.async_add_listener(self, [DNSQuestion(type_, _TYPE_PTR, _CLASS_IN) for type_ in self.types]) + # Only start queries after the listener is installed + self._query_sender_task = asyncio.ensure_future(self._async_start_query_sender()) + + @property + def service_state_changed(self) -> SignalRegistrationInterface: + return self._service_state_changed.registration_interface + + def _names_matching_types(self, names: Iterable[str]) -> list[tuple[str, str]]: + """Return the type and name for records matching the types we are browsing.""" + return [ + (type_, name) for name in names for type_ in self.types.intersection(cached_possible_types(name)) + ] + + def _enqueue_callback( + self, + state_change: ServiceStateChange, + type_: str_, + name: str_, + ) -> None: + # Code to ensure we only do a single update message + # Precedence is; Added, Remove, Update + key = (name, type_) + if ( + state_change is SERVICE_STATE_CHANGE_ADDED + or ( + state_change is SERVICE_STATE_CHANGE_REMOVED + and self._pending_handlers.get(key) is not SERVICE_STATE_CHANGE_ADDED + ) + or (state_change is SERVICE_STATE_CHANGE_UPDATED and key not in self._pending_handlers) + ): + self._pending_handlers[key] = state_change + + def async_update_records(self, zc: Zeroconf, now: float_, records: list[RecordUpdate]) -> None: + """Callback invoked by Zeroconf when new information arrives. + + Updates information required by browser in the Zeroconf cache. + + Ensures that there is are no unnecessary duplicates in the list. + + This method will be run in the event loop. + """ + for record_update in records: + record = record_update.new + old_record = record_update.old + record_type = record.type + + # NSEC records assert non-existence of a record type + # (RFC 6762 §6.1); skip so we do not fire spurious updates. + if record_type == _TYPE_NSEC: + continue + + if record_type == _TYPE_PTR: + if TYPE_CHECKING: + record = cast(DNSPointer, record) + pointer = record + for type_ in self.types.intersection(cached_possible_types(pointer.name)): + if old_record is None: + self._enqueue_callback(SERVICE_STATE_CHANGE_ADDED, type_, pointer.alias) + self.query_scheduler.reschedule_ptr_first_refresh(pointer) + elif pointer.is_expired(now): + self._enqueue_callback(SERVICE_STATE_CHANGE_REMOVED, type_, pointer.alias) + self.query_scheduler.cancel_ptr_refresh(pointer) + else: + self.query_scheduler.reschedule_ptr_first_refresh(pointer) + continue + + # If its expired or already exists in the cache it cannot be updated. + if old_record is not None or record.is_expired(now): + continue + + if record_type in _ADDRESS_RECORD_TYPES: + cache = self._cache + names = {service.name for service in cache.async_entries_with_server(record.name)} + # Iterate through the DNSCache and callback any services that use this address + for type_, name in self._names_matching_types(names): + self._enqueue_callback(SERVICE_STATE_CHANGE_UPDATED, type_, name) + continue + + for type_, name in self._names_matching_types((record.name,)): + self._enqueue_callback(SERVICE_STATE_CHANGE_UPDATED, type_, name) + + def async_update_records_complete(self) -> None: + """Called when a record update has completed for all handlers. + + At this point the cache will have the new records. + + This method will be run in the event loop. + + This method is expected to be overridden by subclasses. + """ + for pending in self._pending_handlers.items(): + self._fire_service_state_changed_event(pending) + self._pending_handlers.clear() + + def _fire_service_state_changed_event(self, event: tuple[tuple[str, str], ServiceStateChange]) -> None: + """Fire a service state changed event. + + When running with ServiceBrowser, this will happen in the dedicated + thread. + + When running with AsyncServiceBrowser, this will happen in the event loop. + """ + name_type = event[0] + state_change = event[1] + self._service_state_changed.fire( + zeroconf=self.zc, + service_type=name_type[1], + name=name_type[0], + state_change=state_change, + ) + + def _async_cancel(self) -> None: + """Cancel the browser.""" + self.done = True + self.query_scheduler.stop() + self.zc.async_remove_listener(self) + assert self._query_sender_task is not None, "Attempted to cancel a browser that was not started" + self._query_sender_task.cancel() + self._query_sender_task = None + + async def _async_start_query_sender(self) -> None: + """Start scheduling queries.""" + if not self.zc.started: + await self.zc.async_wait_for_start() + self.query_scheduler.start(self._loop) + + +class ServiceBrowser(_ServiceBrowserBase, threading.Thread): + """Used to browse for a service of a specific type. + + The listener object will have its add_service() and + remove_service() methods called when this browser + discovers changes in the services availability.""" + + def __init__( + self, + zc: Zeroconf, + type_: str | list, + handlers: ServiceListener | list[Callable[..., None]] | None = None, + listener: ServiceListener | None = None, + addr: str | None = None, + port: int = _MDNS_PORT, + delay: int = _BROWSER_TIME, + question_type: DNSQuestionType | None = None, + ) -> None: + assert zc.loop is not None + if not zc.loop.is_running(): + raise RuntimeError("The event loop is not running") + threading.Thread.__init__(self) + super().__init__(zc, type_, handlers, listener, addr, port, delay, question_type) + # Add the queue before the listener is installed in _setup + # to ensure that events run in the dedicated thread and do + # not block the event loop + self.queue: queue.SimpleQueue = queue.SimpleQueue() + self.daemon = True + self.start() + zc.loop.call_soon_threadsafe(self._async_start) + self.name = "zeroconf-ServiceBrowser-{}-{}".format( + "-".join([type_[:-7] for type_ in self.types]), + getattr(self, "native_id", self.ident), + ) + + def cancel(self) -> None: + """Cancel the browser.""" + assert self.zc.loop is not None + self.queue.put(None) + # While the loop is running, _async_cancel stops the query scheduler + # and cancels the query-sender task — that is the normal cleanup + # path. Skip scheduling solely because the loop is closed: a closed + # loop rejects call_soon_threadsafe with RuntimeError. The + # is_closed() check narrows the common case (loop already closed by + # Zeroconf.close()) without paying for raise/catch; suppress covers + # the residual is_closed() -> call_soon_threadsafe race window. + with contextlib.suppress(RuntimeError): + if not self.zc.loop.is_closed(): + self.zc.loop.call_soon_threadsafe(self._async_cancel) + self.join() + + def run(self) -> None: + """Run the browser thread.""" + while True: + event = self.queue.get() + if event is None: + return + self._fire_service_state_changed_event(event) + + def async_update_records_complete(self) -> None: + """Called when a record update has completed for all handlers. + + At this point the cache will have the new records. + + This method will be run in the event loop. + """ + for pending in self._pending_handlers.items(): + self.queue.put(pending) + self._pending_handlers.clear() + + def __enter__(self) -> ServiceBrowser: + return self + + def __exit__( # pylint: disable=useless-return + self, + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> bool | None: + self.cancel() + return None diff --git a/src/zeroconf/_services/info.pxd b/src/zeroconf/_services/info.pxd new file mode 100644 index 000000000..7fc85f9a7 --- /dev/null +++ b/src/zeroconf/_services/info.pxd @@ -0,0 +1,173 @@ + +import cython + +from .._cache cimport DNSCache +from .._dns cimport ( + DNSAddress, + DNSNsec, + DNSPointer, + DNSQuestion, + DNSRecord, + DNSService, + DNSText, +) +from .._history cimport QuestionHistory +from .._protocol.outgoing cimport DNSOutgoing +from .._record_update cimport RecordUpdate +from .._updates cimport RecordUpdateListener +from .._utils.ipaddress cimport ( + get_ip_address_object_from_record, + ip_bytes_and_scope_to_address, + str_without_scope_id, +) +from .._utils.time cimport current_time_millis + +cdef cython.set _TYPE_AAAA_RECORDS +cdef cython.set _TYPE_A_RECORDS +cdef cython.set _TYPE_A_AAAA_RECORDS + +cdef object _resolve_all_futures_to_none + +cdef object _TYPE_SRV +cdef object _TYPE_TXT +cdef object _TYPE_A +cdef object _TYPE_AAAA +cdef object _TYPE_PTR +cdef object _TYPE_NSEC +cdef object _CLASS_IN +cdef object _FLAGS_QR_QUERY + +cdef object service_type_name + +cdef object QU_QUESTION +cdef object QM_QUESTION + +cdef object _IPVersion_All_value +cdef object _IPVersion_V4Only_value + +cdef cython.set _ADDRESS_RECORD_TYPES + +cdef unsigned int _DUPLICATE_QUESTION_INTERVAL + +cdef bint TYPE_CHECKING +cdef object cached_ip_addresses + +cdef object randint + +cdef class ServiceInfo(RecordUpdateListener): + + cdef public cython.bytes text + cdef public str type + cdef str _name + cdef public str key + cdef public cython.list _ipv4_addresses + cdef public cython.list _ipv6_addresses + cdef public object port + cdef public object weight + cdef public object priority + cdef public str server + cdef public str server_key + cdef public cython.dict _properties + cdef public cython.dict _decoded_properties + cdef public object host_ttl + cdef public object other_ttl + cdef public object interface_index + cdef public cython.set _new_records_futures + cdef public DNSPointer _dns_pointer_cache + cdef public DNSService _dns_service_cache + cdef public DNSText _dns_text_cache + cdef public cython.list _dns_address_cache + cdef public cython.set _get_address_and_nsec_records_cache + cdef public cython.set _query_record_types + + @cython.locals(record_update=RecordUpdate, update=bint, cache=DNSCache) + cpdef void async_update_records(self, object zc, double now, cython.list records) + + @cython.locals(cache=DNSCache) + cpdef bint _load_from_cache(self, object zc, double now) + + @cython.locals(length="unsigned char", index="unsigned int", key_value=bytes, key_sep_value=tuple) + cdef void _unpack_text_into_properties(self) + + @cython.locals(k=bytes, v=bytes) + cdef void _generate_decoded_properties(self) + + @cython.locals(properties_contain_str=bint) + cpdef void _set_properties(self, cython.dict properties) + + cdef void _set_text(self, cython.bytes text) + + @cython.locals(record=DNSAddress) + cdef _get_ip_addresses_from_cache_lifo(self, object zc, double now, object type) + + @cython.locals( + dns_service_record=DNSService, + dns_text_record=DNSText, + dns_address_record=DNSAddress + ) + cdef bint _process_record_threadsafe(self, object zc, DNSRecord record, double now) + + @cython.locals(existing_idx=int, existing=object) + cdef bint _upsert_ipv6_address(self, object ip_addr) + + @cython.locals(cache=DNSCache) + cdef cython.list _get_address_records_from_cache_by_type(self, object zc, object _type) + + cdef void _set_ipv4_addresses_from_cache(self, object zc, double now) + + cdef void _set_ipv6_addresses_from_cache(self, object zc, double now) + + cdef cython.list _ip_addresses_by_version_value(self, object version_value) + + cpdef addresses_by_version(self, object version) + + cpdef ip_addresses_by_version(self, object version) + + @cython.locals(cacheable=cython.bint) + cdef cython.list _dns_addresses(self, object override_ttls, object version) + + @cython.locals(cacheable=cython.bint) + cdef DNSPointer _dns_pointer(self, object override_ttl) + + @cython.locals(cacheable=cython.bint) + cdef DNSService _dns_service(self, object override_ttl) + + @cython.locals(cacheable=cython.bint) + cdef DNSText _dns_text(self, object override_ttl) + + cdef DNSNsec _dns_nsec(self, cython.list missing_types, object override_ttl) + + @cython.locals(cacheable=cython.bint) + cdef cython.set _get_address_and_nsec_records(self, object override_ttl) + + cpdef void async_clear_cache(self) + + @cython.locals(cache=DNSCache, history=QuestionHistory, out=DNSOutgoing, qu_question=bint) + cdef DNSOutgoing _generate_request_query(self, object zc, double now, object question_type) + + @cython.locals(question=DNSQuestion, answer=DNSRecord) + cdef void _add_question_with_known_answers( + self, + DNSOutgoing out, + bint qu_question, + QuestionHistory question_history, + DNSCache cache, + double now, + str name, + object type_, + object class_, + bint skip_if_known_answers + ) + + cdef double _get_initial_delay(self) + + cdef double _get_random_delay(self) + +cdef class AddressResolver(ServiceInfo): + pass + +cdef class AddressResolverIPv6(ServiceInfo): + pass + +cdef class AddressResolverIPv4(ServiceInfo): + pass diff --git a/src/zeroconf/_services/info.py b/src/zeroconf/_services/info.py new file mode 100644 index 000000000..d080761d2 --- /dev/null +++ b/src/zeroconf/_services/info.py @@ -0,0 +1,1067 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import random +from collections.abc import Sequence +from typing import TYPE_CHECKING, cast + +from .._cache import DNSCache +from .._dns import ( + DNSAddress, + DNSNsec, + DNSPointer, + DNSQuestion, + DNSQuestionType, + DNSRecord, + DNSService, + DNSText, +) +from .._exceptions import BadTypeInNameException +from .._history import QuestionHistory +from .._logger import log +from .._protocol.outgoing import DNSOutgoing +from .._record_update import RecordUpdate +from .._updates import RecordUpdateListener +from .._utils.asyncio import ( + _resolve_all_futures_to_none, + get_running_loop, + run_coro_with_timeout, + wait_for_future_set_or_timeout, +) +from .._utils.ipaddress import ( + ZeroconfIPv4Address, + ZeroconfIPv6Address, + cached_ip_addresses, + get_ip_address_object_from_record, + ip_bytes_and_scope_to_address, + str_without_scope_id, +) +from .._utils.name import service_type_name +from .._utils.net import IPVersion, _encode_address +from .._utils.time import current_time_millis +from ..const import ( + _ADDRESS_RECORD_TYPES, + _CLASS_IN, + _CLASS_IN_UNIQUE, + _DNS_HOST_TTL, + _DNS_OTHER_TTL, + _DUPLICATE_QUESTION_INTERVAL, + _FLAGS_QR_QUERY, + _LISTENER_TIME, + _MDNS_PORT, + _TYPE_A, + _TYPE_AAAA, + _TYPE_NSEC, + _TYPE_PTR, + _TYPE_SRV, + _TYPE_TXT, +) + +_IPVersion_All_value = IPVersion.All.value +_IPVersion_V4Only_value = IPVersion.V4Only.value +# https://datatracker.ietf.org/doc/html/rfc6762#section-5.2 +# The most common case for calling ServiceInfo is from a +# ServiceBrowser. After the first request we add a few random +# milliseconds to the delay between requests to reduce the chance +# that there are multiple ServiceBrowser callbacks running on +# the network that are firing at the same time when they +# see the same multicast response and decide to refresh +# the A/AAAA/SRV records for a host. +_AVOID_SYNC_DELAY_RANDOM_INTERVAL = (20, 120) + +_TYPE_AAAA_RECORDS = {_TYPE_AAAA} +_TYPE_A_RECORDS = {_TYPE_A} +_TYPE_A_AAAA_RECORDS = {_TYPE_A, _TYPE_AAAA} + +bytes_ = bytes +float_ = float +int_ = int +str_ = str + +QU_QUESTION = DNSQuestionType.QU +QM_QUESTION = DNSQuestionType.QM + +randint = random.randint + +if TYPE_CHECKING: + from .._core import Zeroconf + + +def _index_of_same_address( + addresses: Sequence[ZeroconfIPv4Address | ZeroconfIPv6Address], + ip_addr: ZeroconfIPv4Address | ZeroconfIPv6Address, +) -> int: + """Return the index of an existing entry with the same packed bytes, or -1. + + Match by ``zc_integer`` so IPv6 addresses that differ only in + scope_id (one observed without scope on an IPv4 socket, another + observed with scope on an IPv6 socket) collapse to a single entry. + """ + target = ip_addr.zc_integer + for idx, existing in enumerate(addresses): + if existing.zc_integer == target: + return idx + return -1 + + +def _has_more_scope_info( + new_addr: ZeroconfIPv4Address | ZeroconfIPv6Address, + existing: ZeroconfIPv4Address | ZeroconfIPv6Address, +) -> bool: + """True if ``new_addr`` carries a scope_id the ``existing`` entry lacks.""" + if new_addr.version != 6: + return False + if TYPE_CHECKING: + assert isinstance(new_addr, ZeroconfIPv6Address) + assert isinstance(existing, ZeroconfIPv6Address) + return new_addr.scope_id is not None and existing.scope_id is None + + +def instance_name_from_service_info(info: ServiceInfo, strict: bool = True) -> str: + """Calculate the instance name from the ServiceInfo.""" + # This is kind of funky because of the subtype based tests + # need to make subtypes a first class citizen + service_name = service_type_name(info.name, strict=strict) + if not info.type.endswith(service_name): + raise BadTypeInNameException + return info.name[: -len(service_name) - 1] + + +class ServiceInfo(RecordUpdateListener): + """Service information. + + Constructor parameters are as follows: + + * `type_`: fully qualified service type name + * `name`: fully qualified service name + * `port`: port that the service runs on + * `weight`: weight of the service + * `priority`: priority of the service + * `properties`: dictionary of properties (or a bytes object holding the contents of the `text` field). + converted to str and then encoded to bytes using UTF-8. Keys with `None` values are converted to + value-less attributes. + * `server`: fully qualified name for service host (defaults to name) + * `host_ttl`: ttl used for A/SRV records + * `other_ttl`: ttl used for PTR/TXT records + * `addresses` and `parsed_addresses`: List of IP addresses (either as bytes, network byte order, + or in parsed form as text; at most one of those parameters can be provided) + * interface_index: scope_id or zone_id for IPv6 link-local addresses i.e. an identifier of the interface + where the peer is connected to + """ + + __slots__ = ( + "_decoded_properties", + "_dns_address_cache", + "_dns_pointer_cache", + "_dns_service_cache", + "_dns_text_cache", + "_get_address_and_nsec_records_cache", + "_ipv4_addresses", + "_ipv6_addresses", + "_name", + "_new_records_futures", + "_properties", + "_query_record_types", + "host_ttl", + "interface_index", + "key", + "other_ttl", + "port", + "priority", + "server", + "server_key", + "text", + "type", + "weight", + ) + + def __init__( + self, + type_: str, + name: str, + port: int | None = None, + weight: int = 0, + priority: int = 0, + properties: bytes | dict = b"", + server: str | None = None, + host_ttl: int = _DNS_HOST_TTL, + other_ttl: int = _DNS_OTHER_TTL, + *, + addresses: list[bytes] | None = None, + parsed_addresses: list[str] | None = None, + interface_index: int | None = None, + ) -> None: + # Accept both none, or one, but not both. + if addresses is not None and parsed_addresses is not None: + raise TypeError("addresses and parsed_addresses cannot be provided together") + if not type_.endswith(service_type_name(name, strict=False)): + raise BadTypeInNameException + self.interface_index = interface_index + self.text = b"" + self.type = type_ + self._name = name + self.key = name.lower() + self._ipv4_addresses: list[ZeroconfIPv4Address] = [] + self._ipv6_addresses: list[ZeroconfIPv6Address] = [] + if addresses is not None: + self.addresses = addresses + elif parsed_addresses is not None: + self.addresses = [_encode_address(a) for a in parsed_addresses] + self.port = port + self.weight = weight + self.priority = priority + self.server = server if server else None + self.server_key = server.lower() if server else None + self._properties: dict[bytes, bytes | None] | None = None + self._decoded_properties: dict[str, str | None] | None = None + if isinstance(properties, bytes): + self._set_text(properties) + else: + self._set_properties(properties) + self.host_ttl = host_ttl + self.other_ttl = other_ttl + self._new_records_futures: set[asyncio.Future] | None = None + self._dns_address_cache: list[DNSAddress] | None = None + self._dns_pointer_cache: DNSPointer | None = None + self._dns_service_cache: DNSService | None = None + self._dns_text_cache: DNSText | None = None + self._get_address_and_nsec_records_cache: set[DNSRecord] | None = None + self._query_record_types = {_TYPE_SRV, _TYPE_TXT, _TYPE_A, _TYPE_AAAA} + + @property + def name(self) -> str: + """The name of the service.""" + return self._name + + @name.setter + def name(self, name: str) -> None: + """Replace the name and reset the key.""" + self._name = name + self.key = name.lower() + self._dns_service_cache = None + self._dns_pointer_cache = None + self._dns_text_cache = None + + @property + def addresses(self) -> list[bytes]: + """IPv4 addresses of this service. + + Only IPv4 addresses are returned for backward compatibility. + Use :meth:`addresses_by_version` or :meth:`parsed_addresses` to + include IPv6 addresses as well. + """ + return self.addresses_by_version(IPVersion.V4Only) + + @addresses.setter + def addresses(self, value: list[bytes]) -> None: + """Replace the addresses list. + + This replaces all currently stored addresses, both IPv4 and IPv6. + """ + self._ipv4_addresses.clear() + self._ipv6_addresses.clear() + self._dns_address_cache = None + self._get_address_and_nsec_records_cache = None + + for address in value: + if len(address) == 16 and self.interface_index is not None: + addr = ip_bytes_and_scope_to_address(address, self.interface_index) + else: + addr = cached_ip_addresses(address) + if addr is None: + raise TypeError( + "Addresses must either be IPv4 or IPv6 strings, bytes, or integers;" + f" got {address!r}. Hint: convert string addresses with socket.inet_pton" + ) + if addr.version == 4: + if TYPE_CHECKING: + assert isinstance(addr, ZeroconfIPv4Address) + self._ipv4_addresses.append(addr) + else: + if TYPE_CHECKING: + assert isinstance(addr, ZeroconfIPv6Address) + self._ipv6_addresses.append(addr) + + @property + def properties(self) -> dict[bytes, bytes | None]: + """Return properties as bytes.""" + if self._properties is None: + self._unpack_text_into_properties() + if TYPE_CHECKING: + assert self._properties is not None + return self._properties + + @property + def decoded_properties(self) -> dict[str, str | None]: + """Return properties as strings.""" + if self._decoded_properties is None: + self._generate_decoded_properties() + if TYPE_CHECKING: + assert self._decoded_properties is not None + return self._decoded_properties + + def async_clear_cache(self) -> None: + """Clear the cache for this service info.""" + self._dns_address_cache = None + self._dns_pointer_cache = None + self._dns_service_cache = None + self._dns_text_cache = None + self._get_address_and_nsec_records_cache = None + + async def async_wait(self, timeout: float, loop: asyncio.AbstractEventLoop | None = None) -> None: + """Calling task waits for a given number of milliseconds or until notified.""" + if not self._new_records_futures: + self._new_records_futures = set() + await wait_for_future_set_or_timeout( + loop or asyncio.get_running_loop(), self._new_records_futures, timeout + ) + + def addresses_by_version(self, version: IPVersion) -> list[bytes]: + """List addresses matching IP version. + + Addresses are guaranteed to be returned in LIFO (last in, first out) + order with IPv4 addresses first and IPv6 addresses second. + + This means the first address will always be the most recently added + address of the given IP version. + """ + version_value = version.value + if version_value == _IPVersion_All_value: + ip_v4_packed = [addr.packed for addr in self._ipv4_addresses] + ip_v6_packed = [addr.packed for addr in self._ipv6_addresses] + return [*ip_v4_packed, *ip_v6_packed] + if version_value == _IPVersion_V4Only_value: + return [addr.packed for addr in self._ipv4_addresses] + return [addr.packed for addr in self._ipv6_addresses] + + def ip_addresses_by_version( + self, version: IPVersion + ) -> list[ZeroconfIPv4Address] | list[ZeroconfIPv6Address]: + """List ip_address objects matching IP version. + + Addresses are guaranteed to be returned in LIFO (last in, first out) + order with IPv4 addresses first and IPv6 addresses second. + + This means the first address will always be the most recently added + address of the given IP version. + """ + return self._ip_addresses_by_version_value(version.value) + + def _ip_addresses_by_version_value( + self, version_value: int_ + ) -> list[ZeroconfIPv4Address] | list[ZeroconfIPv6Address]: + """Backend for addresses_by_version that uses the raw value.""" + if version_value == _IPVersion_All_value: + return [*self._ipv4_addresses, *self._ipv6_addresses] # type: ignore[return-value] + if version_value == _IPVersion_V4Only_value: + return self._ipv4_addresses + return self._ipv6_addresses + + def parsed_addresses(self, version: IPVersion = IPVersion.All) -> list[str]: + """List addresses in their parsed string form. + + Addresses are guaranteed to be returned in LIFO (last in, first out) + order with IPv4 addresses first and IPv6 addresses second. + + This means the first address will always be the most recently added + address of the given IP version. + """ + return [str_without_scope_id(addr) for addr in self._ip_addresses_by_version_value(version.value)] + + def parsed_scoped_addresses(self, version: IPVersion = IPVersion.All) -> list[str]: + """Equivalent to parsed_addresses, with the exception that IPv6 Link-Local + addresses are qualified with % when available + + Addresses are guaranteed to be returned in LIFO (last in, first out) + order with IPv4 addresses first and IPv6 addresses second. + + This means the first address will always be the most recently added + address of the given IP version. + """ + return [str(addr) for addr in self._ip_addresses_by_version_value(version.value)] + + def _set_properties(self, properties: dict[str | bytes, str | bytes | None]) -> None: + """Sets properties and text of this info from a dictionary""" + list_: list[bytes] = [] + properties_contain_str = False + result = b"" + for key, value in properties.items(): + if isinstance(key, str): + key = key.encode("utf-8") # noqa: PLW2901 + properties_contain_str = True + + record = key + if value is not None: + if not isinstance(value, bytes): + value = str(value).encode("utf-8") # noqa: PLW2901 + properties_contain_str = True + record += b"=" + value + list_.append(record) + for item in list_: + result = b"".join((result, bytes((len(item),)), item)) + if not properties_contain_str: + # If there are no str keys or values, we can use the properties + # as-is, without decoding them, otherwise calling + # self.properties will lazy decode them, which is expensive. + if TYPE_CHECKING: + self._properties = cast(dict[bytes, bytes | None], properties) + else: + self._properties = properties + self.text = result + + def _set_text(self, text: bytes) -> None: + """Sets properties and text given a text field""" + if text == self.text: + return + self.text = text + # Clear the properties cache + self._properties = None + self._decoded_properties = None + + def _generate_decoded_properties(self) -> None: + """Generates decoded properties from the properties""" + self._decoded_properties = { + k.decode("ascii", "replace"): None if v is None else v.decode("utf-8", "replace") + for k, v in self.properties.items() + } + + def _unpack_text_into_properties(self) -> None: + """Unpacks the text field into properties""" + text = self.text + end = len(text) + if end == 0: + # Properties should be set atomically + # in case another thread is reading them + self._properties = {} + return + + index = 0 + properties: dict[bytes, bytes | None] = {} + while index < end: + length = text[index] + index += 1 + key_value = text[index : index + length] + key_sep_value = key_value.partition(b"=") + key = key_sep_value[0] + if key not in properties: + properties[key] = key_sep_value[2] or None + index += length + + self._properties = properties + + def get_name(self) -> str: + """Name accessor""" + return self._name[: len(self._name) - len(self.type) - 1] + + def _get_ip_addresses_from_cache_lifo( + self, zc: Zeroconf, now: float_, type: int_ + ) -> list[ZeroconfIPv4Address | ZeroconfIPv6Address]: + """Set IPv6 addresses from the cache.""" + address_list: list[ZeroconfIPv4Address | ZeroconfIPv6Address] = [] + for record in self._get_address_records_from_cache_by_type(zc, type): + if record.is_expired(now): + continue + ip_addr = get_ip_address_object_from_record(record) + if ip_addr is None: + continue + # The cache keeps scoped and unscoped link-local AAAA + # records as separate entries because DNSAddress equality + # includes scope_id. Collapse them here so each address + # appears once; the scoped variant wins so callers of + # parsed_scoped_addresses() get a %- + # qualified link-local address when one was observed. + existing_idx = _index_of_same_address(address_list, ip_addr) + if existing_idx == -1: + address_list.append(ip_addr) + continue + # Move the re-seen address to the end so the later observation + # wins both in value (scope) and in LIFO position after reverse. + existing = address_list.pop(existing_idx) + address_list.append(ip_addr if _has_more_scope_info(ip_addr, existing) else existing) + address_list.reverse() # Reverse to get LIFO order + return address_list + + def _upsert_ipv6_address(self, ip_addr: ZeroconfIPv6Address) -> bool: + """Insert or update an IPv6 address in LIFO order. + + Compares by integer (not IPv6Address equality, which respects + scope_id) so the same link-local address received first without + scope (IPv4 socket) and then with scope (IPv6 socket) collapses + to one entry. The scoped variant wins so parsed_scoped_addresses() + can return a qualified address. + """ + ipv6_addresses = self._ipv6_addresses + existing_idx = _index_of_same_address(ipv6_addresses, ip_addr) + if existing_idx == -1: + ipv6_addresses.insert(0, ip_addr) + return True + existing = ipv6_addresses[existing_idx] + if _has_more_scope_info(ip_addr, existing): + ipv6_addresses.pop(existing_idx) + ipv6_addresses.insert(0, ip_addr) + return True + if existing_idx != 0: + ipv6_addresses.pop(existing_idx) + ipv6_addresses.insert(0, existing) + return False + + def _set_ipv6_addresses_from_cache(self, zc: Zeroconf, now: float_) -> None: + """Set IPv6 addresses from the cache.""" + if TYPE_CHECKING: + self._ipv6_addresses = cast( + list[ZeroconfIPv6Address], + self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_AAAA), + ) + else: + self._ipv6_addresses = self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_AAAA) + + def _set_ipv4_addresses_from_cache(self, zc: Zeroconf, now: float_) -> None: + """Set IPv4 addresses from the cache.""" + if TYPE_CHECKING: + self._ipv4_addresses = cast( + list[ZeroconfIPv4Address], + self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_A), + ) + else: + self._ipv4_addresses = self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_A) + + def async_update_records(self, zc: Zeroconf, now: float_, records: list[RecordUpdate]) -> None: + """Updates service information from a DNS record. + + This method will be run in the event loop. + """ + new_records_futures = self._new_records_futures + updated: bool = False + for record_update in records: + updated |= self._process_record_threadsafe(zc, record_update.new, now) + if updated and new_records_futures: + _resolve_all_futures_to_none(new_records_futures) + + def _process_record_threadsafe(self, zc: Zeroconf, record: DNSRecord, now: float_) -> bool: + """Thread safe record updating. + + Returns True if a new record was added. + """ + if record.is_expired(now): + return False + + record_key = record.key + record_type = type(record) + if record_type is DNSAddress and record_key == self.server_key: + dns_address_record = record + if TYPE_CHECKING: + assert isinstance(dns_address_record, DNSAddress) + ip_addr = get_ip_address_object_from_record(dns_address_record) + if ip_addr is None: + log.warning( + "Encountered invalid address while processing %s: %s", + dns_address_record, + dns_address_record.address, + ) + return False + + if ip_addr.version == 4: + if TYPE_CHECKING: + assert isinstance(ip_addr, ZeroconfIPv4Address) + ipv4_addresses = self._ipv4_addresses + if ip_addr not in ipv4_addresses: + ipv4_addresses.insert(0, ip_addr) + return True + # Use int() to compare the addresses as integers + # since by default IPv4Address.__eq__ compares the + # the addresses on version and int which more than + # we need here since we know the version is 4. + if ip_addr.zc_integer != ipv4_addresses[0].zc_integer: + ipv4_addresses.remove(ip_addr) + ipv4_addresses.insert(0, ip_addr) + + return False + + if TYPE_CHECKING: + assert isinstance(ip_addr, ZeroconfIPv6Address) + return self._upsert_ipv6_address(ip_addr) + + if record_key != self.key: + return False + + if record_type is DNSText: + dns_text_record = record + if TYPE_CHECKING: + assert isinstance(dns_text_record, DNSText) + self._set_text(dns_text_record.text) + return True + + if record_type is DNSService: + dns_service_record = record + if TYPE_CHECKING: + assert isinstance(dns_service_record, DNSService) + old_server_key = self.server_key + self._name = dns_service_record.name + self.key = dns_service_record.key + self.server = dns_service_record.server + self.server_key = dns_service_record.server_key + self.port = dns_service_record.port + self.weight = dns_service_record.weight + self.priority = dns_service_record.priority + if old_server_key != self.server_key: + self._set_ipv4_addresses_from_cache(zc, now) + self._set_ipv6_addresses_from_cache(zc, now) + return True + + return False + + def dns_addresses( + self, + override_ttl: int_ | None = None, + version: IPVersion = IPVersion.All, + ) -> list[DNSAddress]: + """Return matching DNSAddress from ServiceInfo.""" + return self._dns_addresses(override_ttl, version) + + def _dns_addresses( + self, + override_ttl: int_ | None, + version: IPVersion, + ) -> list[DNSAddress]: + """Return matching DNSAddress from ServiceInfo.""" + cacheable = version is IPVersion.All and override_ttl is None + if self._dns_address_cache is not None and cacheable: + return self._dns_address_cache + name = self.server or self._name + ttl = override_ttl if override_ttl is not None else self.host_ttl + class_ = _CLASS_IN_UNIQUE + version_value = version.value + records = [ + DNSAddress( + name, + _TYPE_AAAA if ip_addr.version == 6 else _TYPE_A, + class_, + ttl, + ip_addr.packed, + created=0.0, + ) + for ip_addr in self._ip_addresses_by_version_value(version_value) + ] + if cacheable: + self._dns_address_cache = records + return records + + def dns_pointer(self, override_ttl: int_ | None = None) -> DNSPointer: + """Return DNSPointer from ServiceInfo.""" + return self._dns_pointer(override_ttl) + + def _dns_pointer(self, override_ttl: int_ | None) -> DNSPointer: + """Return DNSPointer from ServiceInfo.""" + cacheable = override_ttl is None + if self._dns_pointer_cache is not None and cacheable: + return self._dns_pointer_cache + record = DNSPointer( + self.type, + _TYPE_PTR, + _CLASS_IN, + override_ttl if override_ttl is not None else self.other_ttl, + self._name, + 0.0, + ) + if cacheable: + self._dns_pointer_cache = record + return record + + def dns_service(self, override_ttl: int_ | None = None) -> DNSService: + """Return DNSService from ServiceInfo.""" + return self._dns_service(override_ttl) + + def _dns_service(self, override_ttl: int_ | None) -> DNSService: + """Return DNSService from ServiceInfo.""" + cacheable = override_ttl is None + if self._dns_service_cache is not None and cacheable: + return self._dns_service_cache + port = self.port + if TYPE_CHECKING: + assert isinstance(port, int) + record = DNSService( + self._name, + _TYPE_SRV, + _CLASS_IN_UNIQUE, + override_ttl if override_ttl is not None else self.host_ttl, + self.priority, + self.weight, + port, + self.server or self._name, + 0.0, + ) + if cacheable: + self._dns_service_cache = record + return record + + def dns_text(self, override_ttl: int_ | None = None) -> DNSText: + """Return DNSText from ServiceInfo.""" + return self._dns_text(override_ttl) + + def _dns_text(self, override_ttl: int_ | None) -> DNSText: + """Return DNSText from ServiceInfo.""" + cacheable = override_ttl is None + if self._dns_text_cache is not None and cacheable: + return self._dns_text_cache + record = DNSText( + self._name, + _TYPE_TXT, + _CLASS_IN_UNIQUE, + override_ttl if override_ttl is not None else self.other_ttl, + self.text, + 0.0, + ) + if cacheable: + self._dns_text_cache = record + return record + + def dns_nsec(self, missing_types: list[int], override_ttl: int_ | None = None) -> DNSNsec: + """Return DNSNsec from ServiceInfo.""" + return self._dns_nsec(missing_types, override_ttl) + + def _dns_nsec(self, missing_types: list[int], override_ttl: int_ | None) -> DNSNsec: + """Return DNSNsec from ServiceInfo.""" + return DNSNsec( + self._name, + _TYPE_NSEC, + _CLASS_IN_UNIQUE, + override_ttl if override_ttl is not None else self.host_ttl, + self._name, + missing_types, + 0.0, + ) + + def get_address_and_nsec_records(self, override_ttl: int_ | None = None) -> set[DNSRecord]: + """Build a set of address records and NSEC records for non-present record types.""" + return self._get_address_and_nsec_records(override_ttl) + + def _get_address_and_nsec_records(self, override_ttl: int_ | None) -> set[DNSRecord]: + """Build a set of address records and NSEC records for non-present record types.""" + cacheable = override_ttl is None + if self._get_address_and_nsec_records_cache is not None and cacheable: + return self._get_address_and_nsec_records_cache + missing_types: set[int] = _ADDRESS_RECORD_TYPES.copy() + records: set[DNSRecord] = set() + for dns_address in self._dns_addresses(override_ttl, IPVersion.All): + missing_types.discard(dns_address.type) + records.add(dns_address) + if missing_types: + assert self.server is not None, "Service server must be set for NSEC record." + records.add(self._dns_nsec(list(missing_types), override_ttl)) + if cacheable: + self._get_address_and_nsec_records_cache = records + return records + + def _get_address_records_from_cache_by_type(self, zc: Zeroconf, _type: int_) -> list[DNSAddress]: + """Get the addresses from the cache.""" + if self.server_key is None: + return [] + cache = zc.cache + if TYPE_CHECKING: + records = cast( + list[DNSAddress], + cache.get_all_by_details(self.server_key, _type, _CLASS_IN), + ) + else: + records = cache.get_all_by_details(self.server_key, _type, _CLASS_IN) + return records + + def set_server_if_missing(self) -> None: + """Set the server if it is missing. + + This function is for backwards compatibility. + """ + if self.server is None: + self.server = self._name + self.server_key = self.key + + def load_from_cache(self, zc: Zeroconf, now: float_ | None = None) -> bool: + """Populate the service info from the cache. + + This method is designed to be threadsafe. + """ + return self._load_from_cache(zc, now or current_time_millis()) + + def _load_from_cache(self, zc: Zeroconf, now: float_) -> bool: + """Populate the service info from the cache. + + This method is designed to be threadsafe. + """ + cache = zc.cache + original_server_key = self.server_key + cached_srv_record = cache.get_by_details(self._name, _TYPE_SRV, _CLASS_IN) + if cached_srv_record: + self._process_record_threadsafe(zc, cached_srv_record, now) + cached_txt_record = cache.get_by_details(self._name, _TYPE_TXT, _CLASS_IN) + if cached_txt_record: + self._process_record_threadsafe(zc, cached_txt_record, now) + if original_server_key == self.server_key: + # If there is a srv which changes the server_key, + # A and AAAA will already be loaded from the cache + # and we do not want to do it twice + for record in self._get_address_records_from_cache_by_type(zc, _TYPE_A): + self._process_record_threadsafe(zc, record, now) + for record in self._get_address_records_from_cache_by_type(zc, _TYPE_AAAA): + self._process_record_threadsafe(zc, record, now) + return self._is_complete + + @property + def _is_complete(self) -> bool: + """The ServiceInfo has all expected properties.""" + return bool(self.text is not None and (self._ipv4_addresses or self._ipv6_addresses)) + + def request( + self, + zc: Zeroconf, + timeout: float, + question_type: DNSQuestionType | None = None, + addr: str | None = None, + port: int = _MDNS_PORT, + ) -> bool: + """Returns true if the service could be discovered on the + network, and updates this object with details discovered. + + While it is not expected during normal operation, + this function may raise EventLoopBlocked if the underlying + call to `async_request` cannot be completed. + + :param zc: Zeroconf instance + :param timeout: time in milliseconds to wait for a response + :param question_type: question type to ask + :param addr: address to send the request to + :param port: port to send the request to + """ + assert zc.loop is not None, "Zeroconf instance must have a loop, was it not started?" + assert zc.loop.is_running(), "Zeroconf instance loop must be running, was it already stopped?" + if zc.loop == get_running_loop(): + raise RuntimeError("Use AsyncServiceInfo.async_request from the event loop") + return bool( + run_coro_with_timeout( + self.async_request(zc, timeout, question_type, addr, port), + zc.loop, + timeout, + ) + ) + + def _get_initial_delay(self) -> float_: + return _LISTENER_TIME + + def _get_random_delay(self) -> int_: + return randint(*_AVOID_SYNC_DELAY_RANDOM_INTERVAL) + + async def async_request( + self, + zc: Zeroconf, + timeout: float, + question_type: DNSQuestionType | None = None, + addr: str | None = None, + port: int = _MDNS_PORT, + ) -> bool: + """Returns true if the service could be discovered on the + network, and updates this object with details discovered. + + This method will be run in the event loop. + + Passing addr and port is optional, and will default to the + mDNS multicast address and port. This is useful for directing + requests to a specific host that may be able to respond across + subnets. + + :param zc: Zeroconf instance + :param timeout: time in milliseconds to wait for a response + :param question_type: question type to ask + :param addr: address to send the request to + :param port: port to send the request to + """ + if not zc.started: + await zc.async_wait_for_start() + + now = current_time_millis() + + if self._load_from_cache(zc, now): + return True + + if TYPE_CHECKING: + assert zc.loop is not None + + first_request = True + delay = self._get_initial_delay() + next_ = now + last = now + timeout + try: + zc.async_add_listener(self, None) + while not self._is_complete: + if last <= now: + return False + if next_ <= now: + this_question_type = question_type or (QU_QUESTION if first_request else QM_QUESTION) + out = self._generate_request_query(zc, now, this_question_type) + first_request = False + if out.questions: + # All questions may have been suppressed + # by the question history, so nothing to send, + # but keep waiting for answers in case another + # client on the network is asking the same + # question or they have not arrived yet. + zc.async_send(out, addr, port) + next_ = now + delay + next_ += self._get_random_delay() + if this_question_type is QM_QUESTION and delay < _DUPLICATE_QUESTION_INTERVAL: + # If we just asked a QM question, we need to + # wait at least the duplicate question interval + # before asking another QM question otherwise + # its likely to be suppressed by the question + # history of the remote responder. + delay = _DUPLICATE_QUESTION_INTERVAL + + await self.async_wait(min(next_, last) - now, zc.loop) + now = current_time_millis() + finally: + zc.async_remove_listener(self) + + return True + + def _add_question_with_known_answers( + self, + out: DNSOutgoing, + qu_question: bool, + question_history: QuestionHistory, + cache: DNSCache, + now: float_, + name: str_, + type_: int_, + class_: int_, + skip_if_known_answers: bool, + ) -> None: + """Add a question with known answers if its not suppressed.""" + known_answers = { + answer for answer in cache.get_all_by_details(name, type_, class_) if not answer.is_stale(now) + } + if skip_if_known_answers and known_answers: + return + question = DNSQuestion(name, type_, class_) + if qu_question: + question.unicast = True + elif question_history.suppresses(question, now, known_answers): + return + else: + question_history.add_question_at_time(question, now, known_answers) + out.add_question(question) + for answer in known_answers: + out.add_answer_at_time(answer, now) + + def _generate_request_query( + self, zc: Zeroconf, now: float_, question_type: DNSQuestionType + ) -> DNSOutgoing: + """Generate the request query.""" + out = DNSOutgoing(_FLAGS_QR_QUERY) + name = self._name + server = self.server or name + cache = zc.cache + history = zc.question_history + qu_question = question_type is QU_QUESTION + if _TYPE_SRV in self._query_record_types: + self._add_question_with_known_answers( + out, qu_question, history, cache, now, name, _TYPE_SRV, _CLASS_IN, True + ) + if _TYPE_TXT in self._query_record_types: + self._add_question_with_known_answers( + out, qu_question, history, cache, now, name, _TYPE_TXT, _CLASS_IN, True + ) + if _TYPE_A in self._query_record_types: + self._add_question_with_known_answers( + out, qu_question, history, cache, now, server, _TYPE_A, _CLASS_IN, False + ) + if _TYPE_AAAA in self._query_record_types: + self._add_question_with_known_answers( + out, qu_question, history, cache, now, server, _TYPE_AAAA, _CLASS_IN, False + ) + return out + + def __repr__(self) -> str: + """String representation""" + return "{}({})".format( + type(self).__name__, + ", ".join( + f"{name}={getattr(self, name)!r}" + for name in ( + "type", + "name", + "addresses", + "port", + "weight", + "priority", + "server", + "properties", + "interface_index", + ) + ), + ) + + +class AsyncServiceInfo(ServiceInfo): + """An async version of ServiceInfo.""" + + +class AddressResolver(ServiceInfo): + """Resolve a host name to an IP address.""" + + def __init__(self, server: str) -> None: + """Initialize the AddressResolver.""" + super().__init__(server, server, server=server) + self._query_record_types = _TYPE_A_AAAA_RECORDS + + @property + def _is_complete(self) -> bool: + """The ServiceInfo has all expected properties.""" + return bool(self._ipv4_addresses) or bool(self._ipv6_addresses) + + +class AddressResolverIPv6(ServiceInfo): + """Resolve a host name to an IPv6 address.""" + + def __init__(self, server: str) -> None: + """Initialize the AddressResolver.""" + super().__init__(server, server, server=server) + self._query_record_types = _TYPE_AAAA_RECORDS + + @property + def _is_complete(self) -> bool: + """The ServiceInfo has all expected properties.""" + return bool(self._ipv6_addresses) + + +class AddressResolverIPv4(ServiceInfo): + """Resolve a host name to an IPv4 address.""" + + def __init__(self, server: str) -> None: + """Initialize the AddressResolver.""" + super().__init__(server, server, server=server) + self._query_record_types = _TYPE_A_RECORDS + + @property + def _is_complete(self) -> bool: + """The ServiceInfo has all expected properties.""" + return bool(self._ipv4_addresses) diff --git a/src/zeroconf/_services/registry.pxd b/src/zeroconf/_services/registry.pxd new file mode 100644 index 000000000..6f9017db7 --- /dev/null +++ b/src/zeroconf/_services/registry.pxd @@ -0,0 +1,33 @@ + +import cython + +from .info cimport ServiceInfo + + +cdef class ServiceRegistry: + + cdef cython.dict _services + cdef public cython.dict types + cdef public cython.dict servers + cdef public bint has_entries + + @cython.locals( + record_list=cython.list, + ) + cdef cython.list _async_get_by_index(self, cython.dict records, str key) + + cdef _add(self, ServiceInfo info) + + @cython.locals( + info=ServiceInfo, + old_service_info=ServiceInfo + ) + cdef _remove(self, cython.list infos) + + cpdef ServiceInfo async_get_info_name(self, str name) + + cpdef cython.list async_get_types(self) + + cpdef cython.list async_get_infos_type(self, str type_) + + cpdef cython.list async_get_infos_server(self, str server) diff --git a/src/zeroconf/_services/registry.py b/src/zeroconf/_services/registry.py new file mode 100644 index 000000000..937992eb0 --- /dev/null +++ b/src/zeroconf/_services/registry.py @@ -0,0 +1,112 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from .._exceptions import ServiceNameAlreadyRegistered +from .info import ServiceInfo + +_str = str + + +class ServiceRegistry: + """A registry to keep track of services. + + The registry must only be accessed from + the event loop as it is not thread safe. + """ + + __slots__ = ("_services", "has_entries", "servers", "types") + + def __init__( + self, + ) -> None: + """Create the ServiceRegistry class.""" + self._services: dict[str, ServiceInfo] = {} + self.types: dict[str, list] = {} + self.servers: dict[str, list] = {} + self.has_entries: bool = False + + def async_add(self, info: ServiceInfo) -> None: + """Add a new service to the registry.""" + self._add(info) + + def async_remove(self, info: list[ServiceInfo] | ServiceInfo) -> None: + """Remove a new service from the registry.""" + self._remove(info if isinstance(info, list) else [info]) + + def async_update(self, info: ServiceInfo) -> None: + """Update new service in the registry.""" + self._remove([info]) + self._add(info) + + def async_get_service_infos(self) -> list[ServiceInfo]: + """Return all ServiceInfo.""" + return list(self._services.values()) + + def async_get_info_name(self, name: str) -> ServiceInfo | None: + """Return all ServiceInfo for the name.""" + return self._services.get(name) + + def async_get_types(self) -> list[str]: + """Return all types.""" + return list(self.types) + + def async_get_infos_type(self, type_: str) -> list[ServiceInfo]: + """Return all ServiceInfo matching type.""" + return self._async_get_by_index(self.types, type_) + + def async_get_infos_server(self, server: str) -> list[ServiceInfo]: + """Return all ServiceInfo matching server.""" + return self._async_get_by_index(self.servers, server) + + def _async_get_by_index(self, records: dict[str, list], key: _str) -> list[ServiceInfo]: + """Return all ServiceInfo matching the index.""" + record_list = records.get(key) + if record_list is None: + return [] + return [self._services[name] for name in record_list] + + def _add(self, info: ServiceInfo) -> None: + """Add a new service under the lock.""" + assert info.server_key is not None, "ServiceInfo must have a server" + if info.key in self._services: + raise ServiceNameAlreadyRegistered + + info.async_clear_cache() + self._services[info.key] = info + self.types.setdefault(info.type.lower(), []).append(info.key) + self.servers.setdefault(info.server_key, []).append(info.key) + self.has_entries = True + + def _remove(self, infos: list[ServiceInfo]) -> None: + """Remove a services under the lock.""" + for info in infos: + old_service_info = self._services.get(info.key) + if old_service_info is None: + continue + assert old_service_info.server_key is not None + self.types[old_service_info.type.lower()].remove(info.key) + self.servers[old_service_info.server_key].remove(info.key) + del self._services[info.key] + + self.has_entries = bool(self._services) diff --git a/src/zeroconf/_services/types.py b/src/zeroconf/_services/types.py new file mode 100644 index 000000000..af25dc6db --- /dev/null +++ b/src/zeroconf/_services/types.py @@ -0,0 +1,84 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import time + +from .._core import Zeroconf +from .._services import ServiceListener +from .._utils.net import InterfaceChoice, InterfacesType, IPVersion +from ..const import _SERVICE_TYPE_ENUMERATION_NAME +from .browser import ServiceBrowser + + +class ZeroconfServiceTypes(ServiceListener): + """ + Return all of the advertised services on any local networks + """ + + def __init__(self) -> None: + """Keep track of found services in a set.""" + self.found_services: set[str] = set() + + def add_service(self, zc: Zeroconf, type_: str, name: str) -> None: + """Service added.""" + self.found_services.add(name) + + def update_service(self, zc: Zeroconf, type_: str, name: str) -> None: + """Service updated.""" + + def remove_service(self, zc: Zeroconf, type_: str, name: str) -> None: + """Service removed.""" + + @classmethod + def find( + cls, + zc: Zeroconf | None = None, + timeout: int | float = 5, + interfaces: InterfacesType = InterfaceChoice.All, + ip_version: IPVersion | None = None, + ) -> tuple[str, ...]: + """ + Return all of the advertised services on any local networks. + + :param zc: Zeroconf() instance. Pass in if already have an + instance running or if non-default interfaces are needed + :param timeout: seconds to wait for any responses + :param interfaces: interfaces to listen on. + :param ip_version: IP protocol version to use. + :return: tuple of service type strings + """ + local_zc = zc or Zeroconf(interfaces=interfaces, ip_version=ip_version) + listener = cls() + browser = ServiceBrowser(local_zc, _SERVICE_TYPE_ENUMERATION_NAME, listener=listener) + + # wait for responses + time.sleep(timeout) + + browser.cancel() + + # close down anything we opened + if zc is None: + local_zc.close() + + return tuple(sorted(listener.found_services)) diff --git a/src/zeroconf/_transport.py b/src/zeroconf/_transport.py new file mode 100644 index 000000000..c8d7699b9 --- /dev/null +++ b/src/zeroconf/_transport.py @@ -0,0 +1,68 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import socket + + +class _WrappedTransport: + """A wrapper for transports.""" + + __slots__ = ( + "fileno", + "is_ipv6", + "sock", + "sock_name", + "transport", + ) + + def __init__( + self, + transport: asyncio.DatagramTransport, + is_ipv6: bool, + sock: socket.socket, + fileno: int, + sock_name: tuple, + ) -> None: + """Initialize the wrapped transport. + + These attributes are used when sending packets. + """ + self.transport = transport + self.is_ipv6 = is_ipv6 + self.sock = sock + self.fileno = fileno + self.sock_name = sock_name + + +def make_wrapped_transport(transport: asyncio.DatagramTransport) -> _WrappedTransport: + """Make a wrapped transport.""" + sock: socket.socket = transport.get_extra_info("socket") + return _WrappedTransport( + transport=transport, + is_ipv6=sock.family == socket.AF_INET6, + sock=sock, + fileno=sock.fileno(), + sock_name=sock.getsockname(), + ) diff --git a/src/zeroconf/_updates.pxd b/src/zeroconf/_updates.pxd new file mode 100644 index 000000000..3547d7295 --- /dev/null +++ b/src/zeroconf/_updates.pxd @@ -0,0 +1,9 @@ + +import cython + + +cdef class RecordUpdateListener: + + cpdef void async_update_records(self, object zc, double now, cython.list records) + + cpdef void async_update_records_complete(self) diff --git a/src/zeroconf/_updates.py b/src/zeroconf/_updates.py new file mode 100644 index 000000000..c0bf9b8c9 --- /dev/null +++ b/src/zeroconf/_updates.py @@ -0,0 +1,81 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from ._dns import DNSRecord +from ._record_update import RecordUpdate + +if TYPE_CHECKING: + from ._core import Zeroconf + + +float_ = float + + +class RecordUpdateListener: + """Base call for all record listeners. + + All listeners passed to async_add_listener should use RecordUpdateListener + as a base class. In the future it will be required. + """ + + def update_record( # pylint: disable=no-self-use + self, zc: Zeroconf, now: float, record: DNSRecord + ) -> None: + """Update a single record. + + This method is deprecated and will be removed in a future version. + update_records should be implemented instead. + """ + raise RuntimeError("update_record is deprecated and will be removed in a future version.") + + def async_update_records(self, zc: Zeroconf, now: float_, records: list[RecordUpdate]) -> None: + """Update multiple records in one shot. + + All records that are received in a single packet are passed + to update_records. + + This implementation is a compatibility shim to ensure older code + that uses RecordUpdateListener as a base class will continue to + get calls to update_record. This method will raise + NotImplementedError in a future version. + + At this point the cache will not have the new records + + Records are passed as a list of RecordUpdate. This + allows consumers of async_update_records to avoid cache lookups. + + This method will be run in the event loop. + """ + for record in records: + self.update_record(zc, now, record.new) + + def async_update_records_complete(self) -> None: + """Called when a record update has completed for all handlers. + + At this point the cache will have the new records. + + This method will be run in the event loop. + """ diff --git a/src/zeroconf/_utils/__init__.py b/src/zeroconf/_utils/__init__.py new file mode 100644 index 000000000..584a74eca --- /dev/null +++ b/src/zeroconf/_utils/__init__.py @@ -0,0 +1,23 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations diff --git a/src/zeroconf/_utils/asyncio.py b/src/zeroconf/_utils/asyncio.py new file mode 100644 index 000000000..860906017 --- /dev/null +++ b/src/zeroconf/_utils/asyncio.py @@ -0,0 +1,141 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import concurrent.futures +import contextlib +import sys +from collections.abc import Awaitable, Coroutine +from typing import Any + +from .._exceptions import EventLoopBlocked +from ..const import _LOADED_SYSTEM_TIMEOUT +from .time import millis_to_seconds + +# The combined timeouts should be lower than _CLOSE_TIMEOUT + _WAIT_FOR_LOOP_TASKS_TIMEOUT +_TASK_AWAIT_TIMEOUT = 1 +_GET_ALL_TASKS_TIMEOUT = 3 +_WAIT_FOR_LOOP_TASKS_TIMEOUT = 3 # Must be larger than _TASK_AWAIT_TIMEOUT + + +def _set_future_none_if_not_done(fut: asyncio.Future) -> None: + """Set a future to None if it is not done.""" + if not fut.done(): # pragma: no branch + fut.set_result(None) + + +def _resolve_all_futures_to_none(futures: set[asyncio.Future]) -> None: + """Resolve all futures to None.""" + for fut in futures: + _set_future_none_if_not_done(fut) + futures.clear() + + +async def wait_for_future_set_or_timeout( + loop: asyncio.AbstractEventLoop, future_set: set[asyncio.Future], timeout: float +) -> None: + """Wait for a future or timeout (in milliseconds).""" + future = loop.create_future() + future_set.add(future) + handle = loop.call_later(millis_to_seconds(timeout), _set_future_none_if_not_done, future) + try: + await future + finally: + handle.cancel() + future_set.discard(future) + + +async def wait_future_or_timeout(future: asyncio.Future[bool | None], timeout: float) -> None: + """Wait for a future or timeout.""" + loop = asyncio.get_running_loop() + handle = loop.call_later(timeout, _set_future_none_if_not_done, future) + try: + await future + except asyncio.CancelledError: + if sys.version_info >= (3, 11) and (task := asyncio.current_task()) and task.cancelling(): + raise + finally: + handle.cancel() + + +async def _async_get_all_tasks(loop: asyncio.AbstractEventLoop) -> set[asyncio.Task]: + """Return all tasks running.""" + await asyncio.sleep(0) # flush out any call_soon_threadsafe + # If there are multiple event loops running, all_tasks is not + # safe EVEN WHEN CALLED FROM THE EVENTLOOP + # under PyPy so we have to try a few times. + for _ in range(3): + with contextlib.suppress(RuntimeError): + return asyncio.all_tasks(loop) + return set() + + +async def _wait_for_loop_tasks(wait_tasks: set[asyncio.Task]) -> None: + """Wait for the event loop thread we started to shutdown.""" + await asyncio.wait(wait_tasks, timeout=_TASK_AWAIT_TIMEOUT) + + +async def await_awaitable(aw: Awaitable) -> None: + """Wait on an awaitable and the task it returns.""" + task = await aw + await task + + +def run_coro_with_timeout(aw: Coroutine, loop: asyncio.AbstractEventLoop, timeout: float) -> Any: + """Run a coroutine with a timeout. + + The timeout should only be used as a safeguard to prevent + the program from blocking forever. The timeout should + never be expected to be reached during normal operation. + + While not expected during normal operations, the + function raises `EventLoopBlocked` if the coroutine takes + longer to complete than the timeout. + """ + try: + return asyncio.run_coroutine_threadsafe(aw, loop).result( + millis_to_seconds(timeout) + _LOADED_SYSTEM_TIMEOUT + ) + except concurrent.futures.TimeoutError as ex: + raise EventLoopBlocked from ex + + +def shutdown_loop(loop: asyncio.AbstractEventLoop) -> None: + """Wait for pending tasks and stop an event loop.""" + pending_tasks = set( + asyncio.run_coroutine_threadsafe(_async_get_all_tasks(loop), loop).result(_GET_ALL_TASKS_TIMEOUT) + ) + pending_tasks -= {task for task in pending_tasks if task.done()} + if pending_tasks: + asyncio.run_coroutine_threadsafe(_wait_for_loop_tasks(pending_tasks), loop).result( + _WAIT_FOR_LOOP_TASKS_TIMEOUT + ) + loop.call_soon_threadsafe(loop.stop) + + +def get_running_loop() -> asyncio.AbstractEventLoop | None: + """Check if an event loop is already running.""" + with contextlib.suppress(RuntimeError): + return asyncio.get_running_loop() + return None diff --git a/src/zeroconf/_utils/ipaddress.pxd b/src/zeroconf/_utils/ipaddress.pxd new file mode 100644 index 000000000..78bbdfbdd --- /dev/null +++ b/src/zeroconf/_utils/ipaddress.pxd @@ -0,0 +1,16 @@ +from .._dns cimport DNSAddress + +cdef bint TYPE_CHECKING + + +cpdef get_ip_address_object_from_record(DNSAddress record) + + +@cython.locals(address_str=str) +cpdef str_without_scope_id(object addr) + + +cpdef ip_bytes_and_scope_to_address(object addr, object scope_id) + + +cdef object cached_ip_addresses_wrapper diff --git a/src/zeroconf/_utils/ipaddress.py b/src/zeroconf/_utils/ipaddress.py new file mode 100644 index 000000000..d172d0c9f --- /dev/null +++ b/src/zeroconf/_utils/ipaddress.py @@ -0,0 +1,155 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from functools import cache, lru_cache +from ipaddress import AddressValueError, IPv4Address, IPv6Address, NetmaskValueError +from typing import Any + +from .._dns import DNSAddress +from ..const import _TYPE_AAAA + +bytes_ = bytes +int_ = int + + +class ZeroconfIPv4Address(IPv4Address): + __slots__ = ("__hash__", "_is_link_local", "_is_loopback", "_is_unspecified", "_str", "zc_integer") + + def __init__(self, *args: Any, **kwargs: Any) -> None: + """Initialize a new IPv4 address.""" + super().__init__(*args, **kwargs) + self._str = super().__str__() + self._is_link_local = super().is_link_local + self._is_unspecified = super().is_unspecified + self._is_loopback = super().is_loopback + self.__hash__ = cache(lambda: IPv4Address.__hash__(self)) # type: ignore[method-assign] + self.zc_integer = int(self) + + def __str__(self) -> str: + """Return the string representation of the IPv4 address.""" + return self._str + + @property + def is_link_local(self) -> bool: + """Return True if this is a link-local address.""" + return self._is_link_local + + @property + def is_unspecified(self) -> bool: + """Return True if this is an unspecified address.""" + return self._is_unspecified + + @property + def is_loopback(self) -> bool: + """Return True if this is a loop back.""" + return self._is_loopback + + +class ZeroconfIPv6Address(IPv6Address): + __slots__ = ("__hash__", "_is_link_local", "_is_loopback", "_is_unspecified", "_str", "zc_integer") + + def __init__(self, *args: Any, **kwargs: Any) -> None: + """Initialize a new IPv6 address.""" + super().__init__(*args, **kwargs) + self._str = super().__str__() + self._is_link_local = super().is_link_local + self._is_unspecified = super().is_unspecified + self._is_loopback = super().is_loopback + self.__hash__ = cache(lambda: IPv6Address.__hash__(self)) # type: ignore[method-assign] + self.zc_integer = int(self) + + def __str__(self) -> str: + """Return the string representation of the IPv6 address.""" + return self._str + + @property + def is_link_local(self) -> bool: + """Return True if this is a link-local address.""" + return self._is_link_local + + @property + def is_unspecified(self) -> bool: + """Return True if this is an unspecified address.""" + return self._is_unspecified + + @property + def is_loopback(self) -> bool: + """Return True if this is a loop back.""" + return self._is_loopback + + +@lru_cache(maxsize=512) +def _cached_ip_addresses( + address: str | bytes | int, +) -> ZeroconfIPv4Address | ZeroconfIPv6Address | None: + """Cache IP addresses.""" + try: + return ZeroconfIPv4Address(address) + except (AddressValueError, NetmaskValueError): + pass + + try: + return ZeroconfIPv6Address(address) + except (AddressValueError, NetmaskValueError): + return None + + +cached_ip_addresses_wrapper = _cached_ip_addresses +cached_ip_addresses = cached_ip_addresses_wrapper + + +def get_ip_address_object_from_record( + record: DNSAddress, +) -> ZeroconfIPv4Address | ZeroconfIPv6Address | None: + """Get the IP address object from the record.""" + if record.type == _TYPE_AAAA and record.scope_id: + return ip_bytes_and_scope_to_address(record.address, record.scope_id) + return cached_ip_addresses_wrapper(record.address) + + +def ip_bytes_and_scope_to_address( + address: bytes_, scope: int_ +) -> ZeroconfIPv4Address | ZeroconfIPv6Address | None: + """Convert the bytes and scope to an IP address object.""" + base_address = cached_ip_addresses_wrapper(address) + if base_address is not None and base_address.is_link_local: + # Avoid expensive __format__ call by using PyUnicode_Join + return cached_ip_addresses_wrapper("".join((str(base_address), "%", str(scope)))) + return base_address + + +def str_without_scope_id(addr: ZeroconfIPv4Address | ZeroconfIPv6Address) -> str: + """Return the string representation of the address without the scope id.""" + if addr.version == 6: + address_str = str(addr) + return address_str.partition("%")[0] + return str(addr) + + +__all__ = ( + "cached_ip_addresses", + "get_ip_address_object_from_record", + "ip_bytes_and_scope_to_address", + "str_without_scope_id", +) diff --git a/src/zeroconf/_utils/name.py b/src/zeroconf/_utils/name.py new file mode 100644 index 000000000..165ce99d7 --- /dev/null +++ b/src/zeroconf/_utils/name.py @@ -0,0 +1,181 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +from functools import lru_cache + +from .._exceptions import BadTypeInNameException +from ..const import ( + _HAS_A_TO_Z, + _HAS_ASCII_CONTROL_CHARS, + _HAS_ONLY_A_TO_Z_NUM_HYPHEN, + _HAS_ONLY_A_TO_Z_NUM_HYPHEN_UNDERSCORE, + _LOCAL_TRAILER, + _NONTCP_PROTOCOL_LOCAL_TRAILER, + _TCP_PROTOCOL_LOCAL_TRAILER, +) + + +@lru_cache(maxsize=512) +def service_type_name(type_: str, *, strict: bool = True) -> str: # pylint: disable=too-many-branches + """ + Validate a fully qualified service name, instance or subtype. [rfc6763] + + Returns fully qualified service name. + + Domain names used by mDNS-SD take the following forms: + + . <_tcp|_udp> . local. + . . <_tcp|_udp> . local. + ._sub . . <_tcp|_udp> . local. + + 1) must end with 'local.' + + This is true because we are implementing mDNS and since the 'm' means + multi-cast, the 'local.' domain is mandatory. + + 2) local is preceded with either '_udp.' or '_tcp.' unless + strict is False + + 3) service name precedes <_tcp|_udp> unless + strict is False + + The rules for Service Names [RFC6335] state that they may be no more + than fifteen characters long (not counting the mandatory underscore), + consisting of only letters, digits, and hyphens, must begin and end + with a letter or digit, must not contain consecutive hyphens, and + must contain at least one letter. + + The instance name and sub type may be up to 63 bytes. + + The portion of the Service Instance Name is a user- + friendly name consisting of arbitrary Net-Unicode text [RFC5198]. It + MUST NOT contain ASCII control characters (byte values 0x00-0x1F and + 0x7F) [RFC20] but otherwise is allowed to contain any characters, + without restriction, including spaces, uppercase, lowercase, + punctuation -- including dots -- accented characters, non-Roman text, + and anything else that may be represented using Net-Unicode. + + :param type_: Type, SubType or service name to validate + :return: fully qualified service name (eg: _http._tcp.local.) + """ + if len(type_) > 256: + # https://datatracker.ietf.org/doc/html/rfc6763#section-7.2 + raise BadTypeInNameException(f"Full name ({type_}) must be > 256 bytes") + + # RFC 1035 §2.3.3 / RFC 6762 §16 — DNS name comparisons are case-insensitive. + type_lower = type_.lower() + if type_lower.endswith((_TCP_PROTOCOL_LOCAL_TRAILER, _NONTCP_PROTOCOL_LOCAL_TRAILER)): + remaining = type_[: -len(_TCP_PROTOCOL_LOCAL_TRAILER)].split(".") + trailer = type_[-len(_TCP_PROTOCOL_LOCAL_TRAILER) :] + has_protocol = True + elif strict: + raise BadTypeInNameException( + f"Type '{type_}' must end with " + f"'{_TCP_PROTOCOL_LOCAL_TRAILER}' or '{_NONTCP_PROTOCOL_LOCAL_TRAILER}'" + ) + elif type_lower.endswith(_LOCAL_TRAILER): + remaining = type_[: -len(_LOCAL_TRAILER)].split(".") + trailer = type_[-len(_LOCAL_TRAILER) + 1 :] + has_protocol = False + else: + raise BadTypeInNameException(f"Type '{type_}' must end with '{_LOCAL_TRAILER}'") + + if strict or has_protocol: + service_name = remaining.pop() + if not service_name: + raise BadTypeInNameException("No Service name found") + + if len(remaining) == 1 and len(remaining[0]) == 0: + raise BadTypeInNameException(f"Type '{type_}' must not start with '.'") + + if service_name[0] != "_": + raise BadTypeInNameException(f"Service name ({service_name}) must start with '_'") + + test_service_name = service_name[1:] + + if strict and len(test_service_name) > 15: + # https://datatracker.ietf.org/doc/html/rfc6763#section-7.2 + raise BadTypeInNameException(f"Service name ({test_service_name}) must be <= 15 bytes") + + if "--" in test_service_name: + raise BadTypeInNameException(f"Service name ({test_service_name}) must not contain '--'") + + if "-" in (test_service_name[0], test_service_name[-1]): + raise BadTypeInNameException(f"Service name ({test_service_name}) may not start or end with '-'") + + if not _HAS_A_TO_Z.search(test_service_name): + raise BadTypeInNameException( + f"Service name ({test_service_name}) must contain at least one letter (eg: 'A-Z')" + ) + + allowed_characters_re = ( + _HAS_ONLY_A_TO_Z_NUM_HYPHEN if strict else _HAS_ONLY_A_TO_Z_NUM_HYPHEN_UNDERSCORE + ) + + if not allowed_characters_re.search(test_service_name): + raise BadTypeInNameException( + f"Service name ({test_service_name if strict else ''}) " + "must contain only these characters: " + "A-Z, a-z, 0-9, hyphen ('-')" + ", underscore ('_')" + if strict + else "" + ) + else: + service_name = "" + + if remaining and remaining[-1] == "_sub": + remaining.pop() + if len(remaining) == 0 or len(remaining[0]) == 0: + raise BadTypeInNameException("_sub requires a subtype name") + + if len(remaining) > 1: + remaining = [".".join(remaining)] + + if remaining: + length = len(remaining[0].encode("utf-8")) + if length > 63: + raise BadTypeInNameException(f"Too long: '{remaining[0]}'") + + if _HAS_ASCII_CONTROL_CHARS.search(remaining[0]): + raise BadTypeInNameException( + f"Ascii control character 0x00-0x1F and 0x7F illegal in '{remaining[0]}'" + ) + + return service_name + trailer + + +def possible_types(name: str) -> set[str]: + """Build a set of all possible types from a fully qualified name.""" + labels = name.split(".") + label_count = len(labels) + types = set() + for count in range(label_count): + parts = labels[label_count - count - 4 :] + if not parts[0].startswith("_"): + break + types.add(".".join(parts)) + return types + + +cached_possible_types = lru_cache(maxsize=256)(possible_types) diff --git a/src/zeroconf/_utils/net.py b/src/zeroconf/_utils/net.py new file mode 100644 index 000000000..01c5b040d --- /dev/null +++ b/src/zeroconf/_utils/net.py @@ -0,0 +1,505 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import enum +import errno +import ipaddress +import socket +import struct +import sys +import warnings +from collections.abc import Iterable, Sequence +from typing import Any, cast + +import ifaddr + +from .._logger import log +from ..const import _IPPROTO_IPV6, _MDNS_ADDR, _MDNS_ADDR6, _MDNS_PORT + + +@enum.unique +class InterfaceChoice(enum.Enum): + Default = 1 + All = 2 + + +InterfacesType = Sequence[str | int | tuple[tuple[str, int, int], int]] | InterfaceChoice + + +@enum.unique +class ServiceStateChange(enum.Enum): + Added = 1 + Removed = 2 + Updated = 3 + + +@enum.unique +class IPVersion(enum.Enum): + V4Only = 1 + V6Only = 2 + All = 3 + + +# utility functions + + +def _is_v6_address(addr: bytes) -> bool: + return len(addr) == 16 + + +def _encode_address(address: str) -> bytes: + is_ipv6 = ":" in address + address_family = socket.AF_INET6 if is_ipv6 else socket.AF_INET + return socket.inet_pton(address_family, address) + + +def get_all_addresses_ipv4(adapters: Iterable[ifaddr.Adapter]) -> list[str]: + return list({addr.ip for iface in adapters for addr in iface.ips if addr.is_IPv4}) # type: ignore[misc] + + +def get_all_addresses_ipv6(adapters: Iterable[ifaddr.Adapter]) -> list[tuple[tuple[str, int, int], int]]: + # IPv6 multicast uses positive indexes for interfaces + # TODO: What about multi-address interfaces? + return list( + {(addr.ip, iface.index) for iface in adapters for addr in iface.ips if addr.is_IPv6} # type: ignore[misc] + ) + + +def get_all_addresses() -> list[str]: + warnings.warn( + "get_all_addresses is deprecated, and will be removed in a future version. Use ifaddr" + "directly instead to get a list of adapters.", + DeprecationWarning, + stacklevel=2, + ) + return get_all_addresses_ipv4(ifaddr.get_adapters()) + + +def get_all_addresses_v6() -> list[tuple[tuple[str, int, int], int]]: + warnings.warn( + "get_all_addresses_v6 is deprecated, and will be removed in a future version. Use ifaddr" + "directly instead to get a list of adapters.", + DeprecationWarning, + stacklevel=2, + ) + return get_all_addresses_ipv6(ifaddr.get_adapters()) + + +def ip6_to_address_and_index(adapters: Iterable[ifaddr.Adapter], ip: str) -> tuple[tuple[str, int, int], int]: + if "%" in ip: + ip = ip[: ip.index("%")] # Strip scope_id. + ipaddr = ipaddress.ip_address(ip) + for adapter in adapters: + for adapter_ip in adapter.ips: + # IPv6 addresses are represented as tuples + if ( + adapter.index is not None + and isinstance(adapter_ip.ip, tuple) + and ipaddress.ip_address(adapter_ip.ip[0]) == ipaddr + ): + return (adapter_ip.ip, adapter.index) + + raise RuntimeError(f"No adapter found for IP address {ip}") + + +def interface_index_to_ip6_address(adapters: Iterable[ifaddr.Adapter], index: int) -> tuple[str, int, int]: + for adapter in adapters: + if adapter.index == index: + for adapter_ip in adapter.ips: + # IPv6 addresses are represented as tuples + if isinstance(adapter_ip.ip, tuple): + return adapter_ip.ip + + raise RuntimeError(f"No adapter found for index {index}") + + +def ip6_addresses_to_indexes( + interfaces: Sequence[str | int | tuple[tuple[str, int, int], int]], +) -> list[tuple[tuple[str, int, int], int]]: + """Convert IPv6 interface addresses to interface indexes. + + IPv4 addresses are ignored. + + :param interfaces: List of IP addresses and indexes. + :returns: List of indexes. + """ + result = [] + adapters = ifaddr.get_adapters() + + for iface in interfaces: + if isinstance(iface, int): + result.append((interface_index_to_ip6_address(adapters, iface), iface)) # type: ignore[arg-type] + elif isinstance(iface, str) and ipaddress.ip_address(iface).version == 6: + result.append(ip6_to_address_and_index(adapters, iface)) # type: ignore[arg-type] + + return result + + +def normalize_interface_choice( + choice: InterfacesType, ip_version: IPVersion = IPVersion.V4Only +) -> list[str | tuple[tuple[str, int, int], int]]: + """Convert the interfaces choice into internal representation. + + :param choice: `InterfaceChoice` or list of interface addresses or indexes (IPv6 only). + :param ip_address: IP version to use (ignored if `choice` is a list). + :returns: List of IP addresses (for IPv4) and indexes (for IPv6). + """ + result: list[str | tuple[tuple[str, int, int], int]] = [] + if choice is InterfaceChoice.Default: + if ip_version != IPVersion.V4Only: + # IPv6 multicast uses interface 0 to mean the default. However, + # the default interface can't be used for outgoing IPv6 multicast + # requests. In a way, interface choice default isn't really working + # with IPv6. Inform the user accordingly. + message = ( + "IPv6 multicast requests can't be sent using default interface. " + "Use V4Only, InterfaceChoice.All or an explicit list of interfaces." + ) + log.error(message) + warnings.warn(message, DeprecationWarning, stacklevel=2) + result.append((("::", 0, 0), 0)) + if ip_version != IPVersion.V6Only: + result.append("0.0.0.0") + elif choice is InterfaceChoice.All: + adapters = ifaddr.get_adapters() + if ip_version != IPVersion.V4Only: + result.extend(get_all_addresses_ipv6(adapters)) + if ip_version != IPVersion.V6Only: + result.extend(get_all_addresses_ipv4(adapters)) + if not result: + raise RuntimeError( + f"No interfaces to listen on, check that any interfaces have IP version {ip_version}" + ) + elif isinstance(choice, list): + # First, take IPv4 addresses. + result = [i for i in choice if isinstance(i, str) and ipaddress.ip_address(i).version == 4] + # Unlike IP_ADD_MEMBERSHIP, IPV6_JOIN_GROUP requires interface indexes. + result += ip6_addresses_to_indexes(choice) + else: + raise TypeError(f"choice must be a list or InterfaceChoice, got {choice!r}") + return result + + +def disable_ipv6_only_or_raise(s: socket.socket) -> None: + """Make V6 sockets work for both V4 and V6 (required for Windows).""" + try: + s.setsockopt(_IPPROTO_IPV6, socket.IPV6_V6ONLY, False) + except OSError: + log.error("Support for dual V4-V6 sockets is not present, use IPVersion.V4 or IPVersion.V6") + raise + + +def set_so_reuseport_if_available(s: socket.socket) -> None: + """Set SO_REUSEADDR on a socket if available.""" + # SO_REUSEADDR should be equivalent to SO_REUSEPORT for + # multicast UDP sockets (p 731, "TCP/IP Illustrated, + # Volume 2"), but some BSD-derived systems require + # SO_REUSEPORT to be specified explicitly. Also, not all + # versions of Python have SO_REUSEPORT available. + # Catch OSError and socket.error for kernel versions <3.9 because lacking + # SO_REUSEPORT support. + if not hasattr(socket, "SO_REUSEPORT"): + return + + try: + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) # pylint: disable=no-member + except OSError as err: + if err.errno != errno.ENOPROTOOPT: + raise + + +def set_respond_socket_multicast_options( + s: socket.socket, + ip_version: IPVersion, +) -> None: + """Set ttl/hops and loop for mDNS respond socket.""" + if ip_version == IPVersion.V4Only: + # OpenBSD needs the ttl and loop values for the IP_MULTICAST_TTL and + # IP_MULTICAST_LOOP socket options as an unsigned char. + ttl = struct.pack(b"B", 255) + loop = struct.pack(b"B", 1) + s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl) + s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, loop) + elif ip_version == IPVersion.V6Only: + # However, char doesn't work here (at least on Linux) + s.setsockopt(_IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 255) + s.setsockopt(_IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, True) + else: + # A shared sender socket is not really possible, especially with link-local + # multicast addresses (ff02::/16), the kernel needs to know which interface + # to use for routing. + # + # It seems that macOS even refuses to take IPv4 socket options if this is an + # AF_INET6 socket. + # + # In theory we could reconfigure the socket on each send, but that is not + # really practical for Python Zerconf. + raise RuntimeError("Dual-stack responder socket not supported") + + +def new_socket( + bind_addr: tuple[str] | tuple[str, int, int], + port: int = _MDNS_PORT, + ip_version: IPVersion = IPVersion.V4Only, + apple_p2p: bool = False, +) -> socket.socket | None: + log.debug( + "Creating new socket with port %s, ip_version %s, apple_p2p %s and bind_addr %r", + port, + ip_version, + apple_p2p, + bind_addr, + ) + socket_family = socket.AF_INET if ip_version == IPVersion.V4Only else socket.AF_INET6 + s = socket.socket(socket_family, socket.SOCK_DGRAM) + + if ip_version == IPVersion.All: + disable_ipv6_only_or_raise(s) + + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + set_so_reuseport_if_available(s) + + if apple_p2p: + # SO_RECV_ANYIF = 0x1104 + # https://opensource.apple.com/source/xnu/xnu-4570.41.2/bsd/sys/socket.h + s.setsockopt(socket.SOL_SOCKET, 0x1104, 1) + + # Bind expects (address, port) for AF_INET and (address, port, flowinfo, scope_id) for AF_INET6 + bind_tup = (bind_addr[0], port, *bind_addr[1:]) + try: + s.bind(bind_tup) + except OSError as ex: + if ex.errno == errno.EADDRNOTAVAIL: + log.warning( + "Address not available when binding to %s, it is expected to happen on some systems", + bind_tup, + ) + return None + if ex.errno == errno.EADDRINUSE: + if sys.platform.startswith("darwin") or sys.platform.startswith("freebsd"): + log.error( + "Address in use when binding to %s; " + "On BSD based systems sharing the same port with another " + "stack may require processes to run with the same UID; " + "When using avahi, make sure disallow-other-stacks is set" + " to no in avahi-daemon.conf", + bind_tup, + ) + else: + log.error( + "Address in use when binding to %s; " + "When using avahi, make sure disallow-other-stacks is set" + " to no in avahi-daemon.conf", + bind_tup, + ) + # This is still a fatal error as its not going to work + # if we can't hear the traffic coming in. + raise + log.debug("Created socket %s", s) + return s + + +def add_multicast_member( + listen_socket: socket.socket, + interface: str | tuple[tuple[str, int, int], int], +) -> bool: + # This is based on assumptions in normalize_interface_choice + is_v6 = isinstance(interface, tuple) + err_einval = {errno.EINVAL} + if sys.platform == "win32": + # No WSAEINVAL definition in typeshed + err_einval |= {cast(Any, errno).WSAEINVAL} # pylint: disable=no-member + log.debug("Adding %r (socket %d) to multicast group", interface, listen_socket.fileno()) + try: + if is_v6: + try: + mdns_addr6_bytes = socket.inet_pton(socket.AF_INET6, _MDNS_ADDR6) + except OSError: + log.info( + "Unable to translate IPv6 address when adding %s to multicast group, " + "this can happen if IPv6 is disabled on the system", + interface, + ) + return False + iface_bin = struct.pack("@I", cast(int, interface[1])) + _value = mdns_addr6_bytes + iface_bin + listen_socket.setsockopt(_IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, _value) + else: + _value = socket.inet_aton(_MDNS_ADDR) + socket.inet_aton(cast(str, interface)) + listen_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, _value) + except OSError as e: + _errno = get_errno(e) + if _errno == errno.EADDRINUSE: + log.info( + "Address in use when adding %s to multicast group, it is expected to happen on some systems", + interface, + ) + return False + if _errno == errno.ENOBUFS: + # https://github.com/python-zeroconf/python-zeroconf/issues/1510 + if not is_v6 and sys.platform.startswith("linux"): + log.warning( + "No buffer space available when adding %s to multicast group, " + "try increasing `net.ipv4.igmp_max_memberships` to `1024` in sysctl.conf", + interface, + ) + else: + log.warning( + "No buffer space available when adding %s to multicast group.", + interface, + ) + return False + if _errno == errno.EADDRNOTAVAIL: + log.info( + "Address not available when adding %s to multicast " + "group, it is expected to happen on some systems", + interface, + ) + return False + if _errno in err_einval: + log.info( + "Interface of %s does not support multicast, it is expected in WSL", + interface, + ) + return False + if _errno == errno.ENOPROTOOPT: + log.info( + "Failed to set socket option on %s, this can happen if " + "the network adapter is in a disconnected state", + interface, + ) + return False + if is_v6 and _errno == errno.ENODEV: + log.info( + "Address in use when adding %s to multicast group, " + "it is expected to happen when the device does not have ipv6", + interface, + ) + return False + raise + return True + + +def new_respond_socket( + interface: str | tuple[tuple[str, int, int], int], + apple_p2p: bool = False, + unicast: bool = False, +) -> socket.socket | None: + """Create interface specific socket for responding to multicast queries.""" + is_v6 = isinstance(interface, tuple) + + # For response sockets: + # - Bind explicitly to the interface address + # - Use ephemeral ports if in unicast mode + # - Create socket according to the interface IP type (IPv4 or IPv6) + respond_socket = new_socket( + bind_addr=cast(tuple[tuple[str, int, int], int], interface)[0] if is_v6 else (cast(str, interface),), + port=0 if unicast else _MDNS_PORT, + ip_version=(IPVersion.V6Only if is_v6 else IPVersion.V4Only), + apple_p2p=apple_p2p, + ) + if unicast: + return respond_socket + + if not respond_socket: + return None + + log.debug("Configuring socket %s with multicast interface %s", respond_socket, interface) + if is_v6: + iface_bin = struct.pack("@I", cast(int, interface[1])) + respond_socket.setsockopt(_IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, iface_bin) + else: + respond_socket.setsockopt( + socket.IPPROTO_IP, + socket.IP_MULTICAST_IF, + socket.inet_aton(cast(str, interface)), + ) + set_respond_socket_multicast_options(respond_socket, IPVersion.V6Only if is_v6 else IPVersion.V4Only) + return respond_socket + + +def create_sockets( + interfaces: InterfacesType = InterfaceChoice.All, + unicast: bool = False, + ip_version: IPVersion = IPVersion.V4Only, + apple_p2p: bool = False, +) -> tuple[socket.socket | None, list[socket.socket]]: + if unicast: + listen_socket = None + else: + listen_socket = new_socket(bind_addr=("",), ip_version=ip_version, apple_p2p=apple_p2p) + + normalized_interfaces = normalize_interface_choice(interfaces, ip_version) + + # If we are using InterfaceChoice.Default with only IPv4 or only IPv6, we can use + # a single socket to listen and respond. + if not unicast and interfaces is InterfaceChoice.Default and ip_version != IPVersion.All: + for interface in normalized_interfaces: + add_multicast_member(cast(socket.socket, listen_socket), interface) + # Sent responder socket options to the dual-use listen socket + set_respond_socket_multicast_options(cast(socket.socket, listen_socket), ip_version) + return listen_socket, [cast(socket.socket, listen_socket)] + + respond_sockets = [] + + for interface in normalized_interfaces: + # Only create response socket if unicast or becoming multicast member was successful + if not unicast and not add_multicast_member(cast(socket.socket, listen_socket), interface): + continue + + respond_socket = new_respond_socket(interface, apple_p2p=apple_p2p, unicast=unicast) + + if respond_socket is not None: + respond_sockets.append(respond_socket) + + return listen_socket, respond_sockets + + +def get_errno(e: OSError) -> int: + return cast(int, e.args[0]) + + +def can_send_to(ipv6_socket: bool, address: str) -> bool: + """Check if the address type matches the socket type. + + This function does not validate if the address is a valid + ipv6 or ipv4 address. + """ + return ":" in address if ipv6_socket else ":" not in address + + +def autodetect_ip_version(interfaces: InterfacesType) -> IPVersion: + """Auto detect the IP version when it is not provided.""" + if isinstance(interfaces, list): + has_v6 = any( + isinstance(i, int) or (isinstance(i, str) and ipaddress.ip_address(i).version == 6) + for i in interfaces + ) + has_v4 = any(isinstance(i, str) and ipaddress.ip_address(i).version == 4 for i in interfaces) + if has_v4 and has_v6: + return IPVersion.All + if has_v6: + return IPVersion.V6Only + + return IPVersion.V4Only diff --git a/src/zeroconf/_utils/time.pxd b/src/zeroconf/_utils/time.pxd new file mode 100644 index 000000000..f6e70fe75 --- /dev/null +++ b/src/zeroconf/_utils/time.pxd @@ -0,0 +1,4 @@ + +cpdef double current_time_millis() + +cpdef millis_to_seconds(double millis) diff --git a/src/zeroconf/_utils/time.py b/src/zeroconf/_utils/time.py new file mode 100644 index 000000000..4057f0630 --- /dev/null +++ b/src/zeroconf/_utils/time.py @@ -0,0 +1,43 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import time + +_float = float + + +def current_time_millis() -> _float: + """Current time in milliseconds. + + The current implementation uses `time.monotonic` + but may change in the future. + + The design requires the time to match asyncio.loop.time() + """ + return time.monotonic() * 1000 + + +def millis_to_seconds(millis: _float) -> _float: + """Convert milliseconds to seconds.""" + return millis / 1000.0 diff --git a/src/zeroconf/asyncio.py b/src/zeroconf/asyncio.py new file mode 100644 index 000000000..45aac67ad --- /dev/null +++ b/src/zeroconf/asyncio.py @@ -0,0 +1,284 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import contextlib +from collections.abc import Awaitable, Callable +from types import TracebackType # used in type hints + +from ._core import Zeroconf +from ._dns import DNSQuestionType +from ._exceptions import NotRunningException +from ._services import ServiceListener +from ._services.browser import _ServiceBrowserBase +from ._services.info import AsyncServiceInfo, ServiceInfo +from ._services.types import ZeroconfServiceTypes +from ._utils.net import InterfaceChoice, InterfacesType, IPVersion +from .const import _BROWSER_TIME, _MDNS_PORT, _SERVICE_TYPE_ENUMERATION_NAME + +__all__ = [ + "AsyncServiceBrowser", + "AsyncServiceInfo", + "AsyncZeroconf", + "AsyncZeroconfServiceTypes", +] + + +class AsyncServiceBrowser(_ServiceBrowserBase): + """Used to browse for a service for specific type(s). + + Constructor parameters are as follows: + + * `zc`: A Zeroconf instance + * `type_`: fully qualified service type name + * `handler`: ServiceListener or Callable that knows how to process ServiceStateChange events + * `listener`: ServiceListener + * `addr`: address to send queries (will default to multicast) + * `port`: port to send queries (will default to mdns 5353) + * `delay`: The initial delay between answering questions + * `question_type`: The type of questions to ask (DNSQuestionType.QM or DNSQuestionType.QU) + + The listener object will have its add_service() and + remove_service() methods called when this browser + discovers changes in the services availability. + """ + + def __init__( + self, + zeroconf: Zeroconf, + type_: str | list, + handlers: ServiceListener | list[Callable[..., None]] | None = None, + listener: ServiceListener | None = None, + addr: str | None = None, + port: int = _MDNS_PORT, + delay: int = _BROWSER_TIME, + question_type: DNSQuestionType | None = None, + ) -> None: + super().__init__(zeroconf, type_, handlers, listener, addr, port, delay, question_type) + self._async_start() + + async def async_cancel(self) -> None: + """Cancel the browser.""" + self._async_cancel() + + async def __aenter__(self) -> AsyncServiceBrowser: + return self + + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> bool | None: + await self.async_cancel() + return None + + +class AsyncZeroconfServiceTypes(ZeroconfServiceTypes): + """An async version of ZeroconfServiceTypes.""" + + @classmethod + async def async_find( + cls, + aiozc: AsyncZeroconf | None = None, + timeout: int | float = 5, + interfaces: InterfacesType = InterfaceChoice.All, + ip_version: IPVersion | None = None, + ) -> tuple[str, ...]: + """ + Return all of the advertised services on any local networks. + + :param aiozc: AsyncZeroconf() instance. Pass in if already have an + instance running or if non-default interfaces are needed + :param timeout: seconds to wait for any responses + :param interfaces: interfaces to listen on. + :param ip_version: IP protocol version to use. + :return: tuple of service type strings + """ + local_zc = aiozc or AsyncZeroconf(interfaces=interfaces, ip_version=ip_version) + listener = cls() + async_browser = AsyncServiceBrowser( + local_zc.zeroconf, _SERVICE_TYPE_ENUMERATION_NAME, listener=listener + ) + + # wait for responses + await asyncio.sleep(timeout) + + await async_browser.async_cancel() + + # close down anything we opened + if aiozc is None: + await local_zc.async_close() + + return tuple(sorted(listener.found_services)) + + +class AsyncZeroconf: + """Implementation of Zeroconf Multicast DNS Service Discovery + + Supports registration, unregistration, queries and browsing. + + The async version is currently a wrapper around Zeroconf which + is now also async. It is expected that an asyncio event loop + is already running before creating the AsyncZeroconf object. + """ + + def __init__( + self, + interfaces: InterfacesType = InterfaceChoice.All, + unicast: bool = False, + ip_version: IPVersion | None = None, + apple_p2p: bool = False, + zc: Zeroconf | None = None, + ) -> None: + """Creates an instance of the Zeroconf class, establishing + multicast communications, and listening. + + :param interfaces: :class:`InterfaceChoice` or a list of IP addresses + (IPv4 and IPv6) and interface indexes (IPv6 only). + + IPv6 notes for non-POSIX systems: + * `InterfaceChoice.All` is an alias for `InterfaceChoice.Default` + on Python versions before 3.8. + + Also listening on loopback (``::1``) doesn't work, use a real address. + :param ip_version: IP versions to support. If `choice` is a list, the default is detected + from it. Otherwise defaults to V4 only for backward compatibility. + :param apple_p2p: use AWDL interface (only macOS) + """ + self.zeroconf = zc or Zeroconf( + interfaces=interfaces, + unicast=unicast, + ip_version=ip_version, + apple_p2p=apple_p2p, + ) + self.async_browsers: dict[ServiceListener, AsyncServiceBrowser] = {} + + async def async_register_service( + self, + info: ServiceInfo, + ttl: int | None = None, + allow_name_change: bool = False, + cooperating_responders: bool = False, + strict: bool = True, + ) -> Awaitable: + """Registers service information to the network with a default TTL. + Zeroconf will then respond to requests for information for that + service. The name of the service may be changed if needed to make + it unique on the network. Additionally multiple cooperating responders + can register the same service on the network for resilience + (if you want this behavior set `cooperating_responders` to `True`). + + The service will be broadcast in a task. This task is returned + and therefore can be awaited if necessary. + """ + return await self.zeroconf.async_register_service( + info, ttl, allow_name_change, cooperating_responders, strict + ) + + async def async_unregister_all_services(self) -> None: + """Unregister all registered services. + + Unlike async_register_service and async_unregister_service, this + method does not return a future and is always expected to be + awaited since its only called at shutdown. + """ + await self.zeroconf.async_unregister_all_services() + + async def async_unregister_service(self, info: ServiceInfo) -> Awaitable: + """Unregister a service. + + The service will be broadcast in a task. This task is returned + and therefore can be awaited if necessary. + """ + return await self.zeroconf.async_unregister_service(info) + + async def async_update_service(self, info: ServiceInfo) -> Awaitable: + """Registers service information to the network with a default TTL. + Zeroconf will then respond to requests for information for that + service. + + The service will be broadcast in a task. This task is returned + and therefore can be awaited if necessary. + """ + return await self.zeroconf.async_update_service(info) + + async def async_close(self) -> None: + """Ends the background threads, and prevent this instance from + servicing further queries.""" + if not self.zeroconf.done: + with contextlib.suppress(NotRunningException): + await self.zeroconf.async_wait_for_start(timeout=1.0) + await self.async_remove_all_service_listeners() + await self.async_unregister_all_services() + await self.zeroconf._async_close() # pylint: disable=protected-access + + async def async_get_service_info( + self, + type_: str, + name: str, + timeout: int = 3000, + question_type: DNSQuestionType | None = None, + ) -> AsyncServiceInfo | None: + """Returns network's service information for a particular + name and type, or None if no service matches by the timeout, + which defaults to 3 seconds. + + :param type_: fully qualified service type name + :param name: the name of the service + :param timeout: milliseconds to wait for a response + :param question_type: The type of questions to ask (DNSQuestionType.QM or DNSQuestionType.QU) + """ + return await self.zeroconf.async_get_service_info(type_, name, timeout, question_type) + + async def async_add_service_listener(self, type_: str, listener: ServiceListener) -> None: + """Adds a listener for a particular service type. This object + will then have its add_service and remove_service methods called when + services of that type become available and unavailable.""" + await self.async_remove_service_listener(listener) + self.async_browsers[listener] = AsyncServiceBrowser(self.zeroconf, type_, listener) + + async def async_remove_service_listener(self, listener: ServiceListener) -> None: + """Removes a listener from the set that is currently listening.""" + if listener in self.async_browsers: + await self.async_browsers[listener].async_cancel() + del self.async_browsers[listener] + + async def async_remove_all_service_listeners(self) -> None: + """Removes a listener from the set that is currently listening.""" + await asyncio.gather( + *(self.async_remove_service_listener(listener) for listener in list(self.async_browsers)) + ) + + async def __aenter__(self) -> AsyncZeroconf: + return self + + async def __aexit__( + self, + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, + ) -> bool | None: + await self.async_close() + return None diff --git a/src/zeroconf/const.py b/src/zeroconf/const.py new file mode 100644 index 000000000..f1b43be59 --- /dev/null +++ b/src/zeroconf/const.py @@ -0,0 +1,208 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import re +import socket + +# Some timing constants + +_UNREGISTER_TIME = 125 # ms +_CHECK_TIME = 500 # ms +_REGISTER_TIME = 225 # ms +_LISTENER_TIME = 200 # ms +_BROWSER_TIME = 10000 # ms +_DUPLICATE_PACKET_SUPPRESSION_INTERVAL = 1000 # ms +# Per-listener bounded recency window. 16 is large enough to defeat +# the alternating-payload bypass (RFC 6762 §6.2, issue #1724 — even a +# rotation of a dozen distinct payloads still dedups), and small +# enough that the dict bookkeeping per miss stays cheap under a +# hostile flood. +_RECENT_PACKETS_MAX = 16 +_DUPLICATE_QUESTION_INTERVAL = 999 # ms # Must be 1ms less than _DUPLICATE_PACKET_SUPPRESSION_INTERVAL +_CACHE_CLEANUP_INTERVAL = 10 # s +_LOADED_SYSTEM_TIMEOUT = 10 # s +_STARTUP_TIMEOUT = 9 # s must be lower than _LOADED_SYSTEM_TIMEOUT +_ONE_SECOND = 1000 # ms + +# If the system is loaded or the event +# loop was blocked by another task that was doing I/O in the loop +# (shouldn't happen but it does in practice) we need to give +# a buffer timeout to ensure a coroutine can finish before +# the future times out + +# Some DNS constants + +_MDNS_ADDR = "224.0.0.251" +_MDNS_ADDR6 = "ff02::fb" +_MDNS_PORT = 5353 +_DNS_PORT = 53 +_DNS_HOST_TTL = 120 # two minute for host records (A, SRV etc) as-per RFC6762 +_DNS_OTHER_TTL = 4500 # 75 minutes for non-host records (PTR, TXT etc) as-per RFC6762 +# Currently we enforce a minimum TTL for PTR records to avoid +# ServiceBrowsers generating excessive queries refresh queries. +# Apple uses a 15s minimum TTL, however we do not have the same +# level of rate limit and safe guards so we use 1/4 of the recommended value +_DNS_PTR_MIN_TTL = 1125 + +# Upper bound on the number of records the DNSCache will hold before it +# starts evicting the closest-to-expiration entry to make room for new +# arrivals. Bounds the memory a malicious LAN peer can force the cache +# to retain by multicasting many unique-name records. +_MAX_CACHE_RECORDS = 10000 + +# Upper bound on the number of entries QuestionHistory will hold between +# the periodic 10s cache-cleanup ticks. Bounds the memory a malicious LAN +# peer can force the duplicate-question-suppression history to retain by +# flooding distinct questions (RFC 6762 §7.3, defense-in-depth). +_MAX_QUESTION_HISTORY_ENTRIES = 10000 + +# Per-entry cap on the number of known-answer records QuestionHistory +# will retain. Each TC-deferred reassembly can carry up to ~12k records +# (~750 records/packet x _MAX_DEFERRED_PER_ADDR fragments), and the +# resulting set is stored by reference under each non-unicast question +# in the history dict; without a per-entry cap a LAN attacker can pin +# hundreds of MB across the _MAX_QUESTION_HISTORY_ENTRIES dimension. +# 256 is well above any RFC-realistic known-answer list for a single +# question; oversized payloads are dropped from the history (no +# suppression for that one query) rather than truncated, since a +# truncated stored set would over-suppress legitimate follow-up +# queries (`suppresses()` returns True when stored set is a subset of +# the incoming known-answers, so a smaller stored set matches more +# easily). +_MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY = 256 + +# Per-addr cap on the number of truncated (TC-bit) packets retained for +# RFC 6762 §18.5 reassembly. The spec anticipates only a handful of +# segments per truncated query; 16 is well above legitimate need and +# keeps the per-arrival dedup scan a constant-time cost under a flood. +_MAX_DEFERRED_PER_ADDR = 16 + +# Per-listener cap on the number of distinct addrs with in-flight +# TC-deferral state. Each entry can hold up to _MAX_DEFERRED_PER_ADDR +# packets of up to _MAX_MSG_ABSOLUTE bytes; 512 leaves headroom for a +# legitimate burst (LAN-wide power-resume / boot storm where many +# devices announce at once) while bounding worst-case memory at +# ~72 MB even when a peer floods with spoofed source IPs. +_MAX_DEFERRED_ADDRS = 512 + +_DNS_PACKET_HEADER_LEN = 12 + +_MAX_MSG_TYPICAL = 1460 # unused +_MAX_MSG_ABSOLUTE = 8966 + +_FLAGS_QR_MASK = 0x8000 # query response mask +_FLAGS_QR_QUERY = 0x0000 # query +_FLAGS_QR_RESPONSE = 0x8000 # response + +_FLAGS_AA = 0x0400 # Authoritative answer +_FLAGS_TC = 0x0200 # Truncated +_FLAGS_RD = 0x0100 # Recursion desired +_FLAGS_RA = 0x8000 # Recursion available + +_FLAGS_Z = 0x0040 # Zero +_FLAGS_AD = 0x0020 # Authentic data +_FLAGS_CD = 0x0010 # Checking disabled + +_CLASS_IN = 1 +_CLASS_CS = 2 +_CLASS_CH = 3 +_CLASS_HS = 4 +_CLASS_NONE = 254 +_CLASS_ANY = 255 +_CLASS_MASK = 0x7FFF +_CLASS_UNIQUE = 0x8000 +_CLASS_IN_UNIQUE = _CLASS_IN | _CLASS_UNIQUE + +_TYPE_A = 1 +_TYPE_NS = 2 +_TYPE_MD = 3 +_TYPE_MF = 4 +_TYPE_CNAME = 5 +_TYPE_SOA = 6 +_TYPE_MB = 7 +_TYPE_MG = 8 +_TYPE_MR = 9 +_TYPE_NULL = 10 +_TYPE_WKS = 11 +_TYPE_PTR = 12 +_TYPE_HINFO = 13 +_TYPE_MINFO = 14 +_TYPE_MX = 15 +_TYPE_TXT = 16 +_TYPE_AAAA = 28 +_TYPE_SRV = 33 +_TYPE_NSEC = 47 +_TYPE_ANY = 255 + +# Mapping constants to names + +_CLASSES = { + _CLASS_IN: "in", + _CLASS_CS: "cs", + _CLASS_CH: "ch", + _CLASS_HS: "hs", + _CLASS_NONE: "none", + _CLASS_ANY: "any", +} + +_TYPES = { + _TYPE_A: "a", + _TYPE_NS: "ns", + _TYPE_MD: "md", + _TYPE_MF: "mf", + _TYPE_CNAME: "cname", + _TYPE_SOA: "soa", + _TYPE_MB: "mb", + _TYPE_MG: "mg", + _TYPE_MR: "mr", + _TYPE_NULL: "null", + _TYPE_WKS: "wks", + _TYPE_PTR: "ptr", + _TYPE_HINFO: "hinfo", + _TYPE_MINFO: "minfo", + _TYPE_MX: "mx", + _TYPE_TXT: "txt", + _TYPE_AAAA: "quada", + _TYPE_SRV: "srv", + _TYPE_ANY: "any", + _TYPE_NSEC: "nsec", +} + +_ADDRESS_RECORD_TYPES = {_TYPE_A, _TYPE_AAAA} + +_HAS_A_TO_Z = re.compile(r"[A-Za-z]") +_HAS_ONLY_A_TO_Z_NUM_HYPHEN = re.compile(r"^[A-Za-z0-9\-]+$") +_HAS_ONLY_A_TO_Z_NUM_HYPHEN_UNDERSCORE = re.compile(r"^[A-Za-z0-9\-\_]+$") +_HAS_ASCII_CONTROL_CHARS = re.compile(r"[\x00-\x1f\x7f]") + +_EXPIRE_REFRESH_TIME_PERCENT = 75 + +_LOCAL_TRAILER = ".local." +_TCP_PROTOCOL_LOCAL_TRAILER = "._tcp.local." +_NONTCP_PROTOCOL_LOCAL_TRAILER = "._udp.local." + +# https://datatracker.ietf.org/doc/html/rfc6763#section-9 +_SERVICE_TYPE_ENUMERATION_NAME = "_services._dns-sd._udp.local." + +_IPPROTO_IPV6 = socket.IPPROTO_IPV6 diff --git a/src/zeroconf/py.typed b/src/zeroconf/py.typed new file mode 100644 index 000000000..e69de29bb diff --git a/test_zeroconf.py b/test_zeroconf.py deleted file mode 100644 index 0c4d771ab..000000000 --- a/test_zeroconf.py +++ /dev/null @@ -1,882 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - - -""" Unit tests for zeroconf.py """ - -import copy -import logging -import socket -import struct -import time -import unittest -from threading import Event -from typing import Dict, Optional # noqa # used in type hints -from typing import cast - - -import zeroconf as r -from zeroconf import ( - DNSHinfo, - DNSText, - ServiceBrowser, - ServiceInfo, - ServiceStateChange, - Zeroconf, - ZeroconfServiceTypes, -) - -log = logging.getLogger('zeroconf') -original_logging_level = logging.NOTSET - - -def setup_module(): - global original_logging_level - original_logging_level = log.level - log.setLevel(logging.DEBUG) - - -def teardown_module(): - if original_logging_level != logging.NOTSET: - log.setLevel(original_logging_level) - - -class TestDunder(unittest.TestCase): - - def test_dns_text_repr(self): - # There was an issue on Python 3 that prevented DNSText's repr - # from working when the text was longer than 10 bytes - text = DNSText('irrelevant', None, 0, 0, b'12345678901') - repr(text) - - text = DNSText('irrelevant', None, 0, 0, b'123') - repr(text) - - def test_dns_hinfo_repr_eq(self): - hinfo = DNSHinfo('irrelevant', r._TYPE_HINFO, 0, 0, 'cpu', 'os') - assert hinfo == hinfo - repr(hinfo) - - def test_dns_pointer_repr(self): - pointer = r.DNSPointer( - 'irrelevant', r._TYPE_PTR, r._CLASS_IN, r._DNS_TTL, '123') - repr(pointer) - - def test_dns_address_repr(self): - address = r.DNSAddress('irrelevant', r._TYPE_SOA, r._CLASS_IN, 1, b'a') - repr(address) - - def test_dns_question_repr(self): - question = r.DNSQuestion( - 'irrelevant', r._TYPE_SRV, r._CLASS_IN | r._CLASS_UNIQUE) - repr(question) - assert not question != question - - def test_dns_service_repr(self): - service = r.DNSService( - 'irrelevant', r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL, 0, 0, 80, b'a') - repr(service) - - def test_dns_record_abc(self): - record = r.DNSRecord('irrelevant', r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL) - self.assertRaises(r.AbstractMethodException, record.__eq__, record) - self.assertRaises(r.AbstractMethodException, record.write, None) - - def test_service_info_dunder(self): - type_ = "_test-srvc-type._tcp.local." - name = "xxxyyy" - registration_name = "%s.%s" % (name, type_) - info = ServiceInfo( - type_, registration_name, - socket.inet_aton("10.0.1.2"), 80, 0, 0, - None, "ash-2.local.") - - assert not info != info - repr(info) - - def test_service_info_text_properties_not_given(self): - type_ = "_test-srvc-type._tcp.local." - name = "xxxyyy" - registration_name = "%s.%s" % (name, type_) - info = ServiceInfo( - type_=type_, name=registration_name, - address=socket.inet_aton("10.0.1.2"), - port=80, server="ash-2.local.") - - assert isinstance(info.text, bytes) - repr(info) - - def test_dns_outgoing_repr(self): - dns_outgoing = r.DNSOutgoing(r._FLAGS_QR_QUERY) - repr(dns_outgoing) - - -class PacketGeneration(unittest.TestCase): - - def test_parse_own_packet_simple(self): - generated = r.DNSOutgoing(0) - r.DNSIncoming(generated.packet()) - - def test_parse_own_packet_simple_unicast(self): - generated = r.DNSOutgoing(0, 0) - r.DNSIncoming(generated.packet()) - - def test_parse_own_packet_flags(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - r.DNSIncoming(generated.packet()) - - def test_parse_own_packet_question(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - generated.add_question(r.DNSQuestion("testname.local.", r._TYPE_SRV, - r._CLASS_IN)) - r.DNSIncoming(generated.packet()) - - def test_parse_own_packet_response(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - generated.add_answer_at_time(r.DNSService( - "æøå.local.", r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL, 0, 0, 80, "foo.local."), 0) - parsed = r.DNSIncoming(generated.packet()) - self.assertEqual(len(generated.answers), 1) - self.assertEqual(len(generated.answers), len(parsed.answers)) - - def test_match_question(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - generated.add_question(question) - parsed = r.DNSIncoming(generated.packet()) - self.assertEqual(len(generated.questions), 1) - self.assertEqual(len(generated.questions), len(parsed.questions)) - self.assertEqual(question, parsed.questions[0]) - - def test_suppress_answer(self): - query_generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - query_generated.add_question(question) - answer1 = r.DNSService( - "testname1.local.", r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL, 0, 0, 80, "foo.local.") - staleanswer2 = r.DNSService( - "testname2.local.", r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL/2, 0, 0, 80, "foo.local.") - answer2 = r.DNSService( - "testname2.local.", r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL, 0, 0, 80, "foo.local.") - query_generated.add_answer_at_time(answer1, 0) - query_generated.add_answer_at_time(staleanswer2, 0) - query = r.DNSIncoming(query_generated.packet()) - - # Should be suppressed - response = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - response.add_answer(query, answer1) - assert len(response.answers) == 0 - - # Should not be suppressed, TTL in query is too short - response.add_answer(query, answer2) - assert len(response.answers) == 1 - - # Should not be suppressed, name is different - tmp = copy.copy(answer1) - tmp.name = "testname3.local." - response.add_answer(query, tmp) - assert len(response.answers) == 2 - - # Should not be suppressed, type is different - tmp = copy.copy(answer1) - tmp.type = r._TYPE_A - response.add_answer(query, tmp) - assert len(response.answers) == 3 - - # Should not be suppressed, class is different - tmp = copy.copy(answer1) - tmp.class_ = r._CLASS_NONE - response.add_answer(query, tmp) - assert len(response.answers) == 4 - - # ::TODO:: could add additional tests for DNSAddress, DNSHinfo, DNSPointer, DNSText, DNSService - - def test_dns_hinfo(self): - generated = r.DNSOutgoing(0) - generated.add_additional_answer( - DNSHinfo('irrelevant', r._TYPE_HINFO, 0, 0, 'cpu', 'os')) - parsed = r.DNSIncoming(generated.packet()) - answer = cast(r.DNSHinfo, parsed.answers[0]) - self.assertEqual(answer.cpu, u'cpu') - self.assertEqual(answer.os, u'os') - - generated = r.DNSOutgoing(0) - generated.add_additional_answer( - DNSHinfo('irrelevant', r._TYPE_HINFO, 0, 0, 'cpu', 'x' * 257)) - self.assertRaises(r.NamePartTooLongException, generated.packet) - - -class PacketForm(unittest.TestCase): - - def test_transaction_id(self): - """ID must be zero in a DNS-SD packet""" - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - bytes = generated.packet() - id = bytes[0] << 8 | bytes[1] - self.assertEqual(id, 0) - - def test_query_header_bits(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - bytes = generated.packet() - flags = bytes[2] << 8 | bytes[3] - self.assertEqual(flags, 0x0) - - def test_response_header_bits(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - bytes = generated.packet() - flags = bytes[2] << 8 | bytes[3] - self.assertEqual(flags, 0x8000) - - def test_numbers(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - bytes = generated.packet() - (num_questions, num_answers, num_authorities, - num_additionals) = struct.unpack('!4H', bytes[4:12]) - self.assertEqual(num_questions, 0) - self.assertEqual(num_answers, 0) - self.assertEqual(num_authorities, 0) - self.assertEqual(num_additionals, 0) - - def test_numbers_questions(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - for i in range(10): - generated.add_question(question) - bytes = generated.packet() - (num_questions, num_answers, num_authorities, - num_additionals) = struct.unpack('!4H', bytes[4:12]) - self.assertEqual(num_questions, 10) - self.assertEqual(num_answers, 0) - self.assertEqual(num_authorities, 0) - self.assertEqual(num_additionals, 0) - - -class Names(unittest.TestCase): - - def test_long_name(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", - r._TYPE_SRV, r._CLASS_IN) - generated.add_question(question) - r.DNSIncoming(generated.packet()) - - def test_exceedingly_long_name(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - name = "%slocal." % ("part." * 1000) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.add_question(question) - r.DNSIncoming(generated.packet()) - - def test_exceedingly_long_name_part(self): - name = "%s.local." % ("a" * 1000) - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.add_question(question) - self.assertRaises(r.NamePartTooLongException, generated.packet) - - def test_same_name(self): - name = "paired.local." - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.add_question(question) - generated.add_question(question) - r.DNSIncoming(generated.packet()) - - def test_lots_of_names(self): - - # instantiate a zeroconf instance - zc = Zeroconf(interfaces=['127.0.0.1']) - - # create a bunch of servers - type_ = "_my-service._tcp.local." - name = 'a wonderful service' - server_count = 300 - self.generate_many_hosts(zc, type_, name, server_count) - - # verify that name changing works - self.verify_name_change(zc, type_, name, server_count) - - # we are going to monkey patch the zeroconf send to check packet sizes - old_send = zc.send - - longest_packet_len = 0 - longest_packet = None # type: Optional[r.DNSOutgoing] - - def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT): - """Sends an outgoing packet.""" - packet = out.packet() - nonlocal longest_packet_len, longest_packet - if longest_packet_len < len(packet): - longest_packet_len = len(packet) - longest_packet = out - old_send(out, addr=addr, port=port) - - # monkey patch the zeroconf send - setattr(zc, "send", send) - - # dummy service callback - def on_service_state_change(zeroconf, service_type, state_change, name): - pass - - # start a browser - browser = ServiceBrowser(zc, type_, [on_service_state_change]) - - # wait until the browse request packet has maxed out in size - sleep_count = 0 - while sleep_count < 100 and \ - longest_packet_len < r._MAX_MSG_ABSOLUTE - 100: - sleep_count += 1 - time.sleep(0.1) - - browser.cancel() - time.sleep(0.5) - - import zeroconf - zeroconf.log.debug('sleep_count %d, sized %d', - sleep_count, longest_packet_len) - - # now the browser has sent at least one request, verify the size - assert longest_packet_len <= r._MAX_MSG_ABSOLUTE - assert longest_packet_len >= r._MAX_MSG_ABSOLUTE - 100 - - # mock zeroconf's logger warning() and debug() - from unittest.mock import patch - patch_warn = patch('zeroconf.log.warning') - patch_debug = patch('zeroconf.log.debug') - mocked_log_warn = patch_warn.start() - mocked_log_debug = patch_debug.start() - - # now that we have a long packet in our possession, let's verify the - # exception handling. - out = longest_packet - assert out is not None - out.data.append(b'\0' * 1000) - - # mock the zeroconf logger and check for the correct logging backoff - call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count - # try to send an oversized packet - zc.send(out) - assert mocked_log_warn.call_count == call_counts[0] + 1 - assert mocked_log_debug.call_count == call_counts[0] - zc.send(out) - assert mocked_log_warn.call_count == call_counts[0] + 1 - assert mocked_log_debug.call_count == call_counts[0] + 1 - - # force a receive of an oversized packet - packet = out.packet() - s = zc._respond_sockets[0] - - # mock the zeroconf logger and check for the correct logging backoff - call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count - # force receive on oversized packet - s.sendto(packet, 0, (r._MDNS_ADDR, r._MDNS_PORT)) - s.sendto(packet, 0, (r._MDNS_ADDR, r._MDNS_PORT)) - time.sleep(2.0) - zeroconf.log.debug('warn %d debug %d was %s', - mocked_log_warn.call_count, - mocked_log_debug.call_count, - call_counts) - assert mocked_log_debug.call_count > call_counts[0] - - # close our zeroconf which will close the sockets - zc.close() - - # pop the big chunk off the end of the data and send on a closed socket - out.data.pop() - zc._GLOBAL_DONE = False - - # mock the zeroconf logger and check for the correct logging backoff - call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count - # send on a closed socket (force a socket error) - zc.send(out) - zeroconf.log.debug('warn %d debug %d was %s', - mocked_log_warn.call_count, - mocked_log_debug.call_count, - call_counts) - assert mocked_log_warn.call_count > call_counts[0] - assert mocked_log_debug.call_count > call_counts[0] - zc.send(out) - zeroconf.log.debug('warn %d debug %d was %s', - mocked_log_warn.call_count, - mocked_log_debug.call_count, - call_counts) - assert mocked_log_debug.call_count > call_counts[0] + 2 - - mocked_log_warn.stop() - mocked_log_debug.stop() - - def verify_name_change(self, zc, type_, name, number_hosts): - desc = {'path': '/~paulsm/'} - info_service = ServiceInfo( - type_, '%s.%s' % (name, type_), socket.inet_aton("10.0.1.2"), - 80, 0, 0, desc, "ash-2.local.") - - # verify name conflict - self.assertRaises( - r.NonUniqueNameException, - zc.register_service, info_service) - - zc.register_service(info_service, allow_name_change=True) - assert info_service.name.split('.')[0] == '%s-%d' % ( - name, number_hosts + 1) - - def generate_many_hosts(self, zc, type_, name, number_hosts): - records_per_server = 2 - block_size = 25 - number_hosts = int(((number_hosts - 1) / block_size + 1)) * block_size - for i in range(1, number_hosts + 1): - next_name = name if i == 1 else '%s-%d' % (name, i) - self.generate_host(zc, next_name, type_) - if i % block_size == 0: - sleep_count = 0 - while sleep_count < 40 and \ - i * records_per_server > len( - zc.cache.entries_with_name(type_)): - sleep_count += 1 - time.sleep(0.05) - - @staticmethod - def generate_host(zc, host_name, type_): - name = '.'.join((host_name, type_)) - out = r.DNSOutgoing(r._FLAGS_QR_RESPONSE | r._FLAGS_AA) - out.add_answer_at_time( - r.DNSPointer(type_, r._TYPE_PTR, r._CLASS_IN, - r._DNS_TTL, name), 0) - out.add_answer_at_time( - r.DNSService(type_, r._TYPE_SRV, r._CLASS_IN, - r._DNS_TTL, 0, 0, 80, - name), 0) - zc.send(out) - - -class Framework(unittest.TestCase): - - def test_launch_and_close(self): - rv = r.Zeroconf(interfaces=r.InterfaceChoice.All) - rv.close() - rv = r.Zeroconf(interfaces=r.InterfaceChoice.Default) - rv.close() - - -class Exceptions(unittest.TestCase): - - browser = None - - @classmethod - def setUpClass(cls): - cls.browser = Zeroconf(interfaces=['127.0.0.1']) - - @classmethod - def tearDownClass(cls): - cls.browser.close() - cls.browser = None - - def test_bad_service_info_name(self): - self.assertRaises( - r.BadTypeInNameException, - self.browser.get_service_info, "type", "type_not") - - def test_bad_service_names(self): - bad_names_to_try = ( - '', - 'local', - '_tcp.local.', - '_udp.local.', - '._udp.local.', - '_@._tcp.local.', - '_A@._tcp.local.', - '_x--x._tcp.local.', - '_-x._udp.local.', - '_x-._tcp.local.', - '_22._udp.local.', - '_2-2._tcp.local.', - '_1234567890-abcde._udp.local.', - '\x00._x._udp.local.', - ) - for name in bad_names_to_try: - self.assertRaises( - r.BadTypeInNameException, - self.browser.get_service_info, name, 'x.' + name) - - def test_good_instance_names(self): - good_names_to_try = ( - '.._x._tcp.local.', - 'x.sub._http._tcp.local.', - '6d86f882b90facee9170ad3439d72a4d6ee9f511._zget._http._tcp.local.' - ) - for name in good_names_to_try: - r.service_type_name(name) - - def test_bad_types(self): - bad_names_to_try = ( - '._x._tcp.local.', - 'a' * 64 + '._sub._http._tcp.local.', - 'a' * 62 + u'â._sub._http._tcp.local.', - ) - for name in bad_names_to_try: - self.assertRaises( - r.BadTypeInNameException, r.service_type_name, name) - - def test_bad_sub_types(self): - bad_names_to_try = ( - '_sub._http._tcp.local.', - '._sub._http._tcp.local.', - '\x7f._sub._http._tcp.local.', - '\x1f._sub._http._tcp.local.', - ) - for name in bad_names_to_try: - self.assertRaises( - r.BadTypeInNameException, r.service_type_name, name) - - def test_good_service_names(self): - good_names_to_try = ( - '_x._tcp.local.', - '_x._udp.local.', - '_12345-67890-abc._udp.local.', - 'x._sub._http._tcp.local.', - 'a' * 63 + '._sub._http._tcp.local.', - 'a' * 61 + u'â._sub._http._tcp.local.', - ) - for name in good_names_to_try: - r.service_type_name(name) - - r.service_type_name('_one_two._tcp.local.', allow_underscores=True) - - -class TestDnsIncoming(unittest.TestCase): - - def test_incoming_exception_handling(self): - generated = r.DNSOutgoing(0) - packet = generated.packet() - packet = packet[:8] + b'deadbeef' + packet[8:] - parsed = r.DNSIncoming(packet) - parsed = r.DNSIncoming(packet) - assert parsed.valid is False - - def test_incoming_unknown_type(self): - generated = r.DNSOutgoing(0) - answer = r.DNSAddress('a', r._TYPE_SOA, r._CLASS_IN, 1, b'a') - generated.add_additional_answer(answer) - packet = generated.packet() - parsed = r.DNSIncoming(packet) - assert len(parsed.answers) == 0 - assert parsed.is_query() != parsed.is_response() - - def test_incoming_ipv6(self): - # ::TODO:: could use a test here if we add IPV6 record handling - # ie: _TYPE_AAAA - pass - - -class TestRegistrar(unittest.TestCase): - - def test_ttl(self): - - # instantiate a zeroconf instance - zc = Zeroconf(interfaces=['127.0.0.1']) - - # service definition - type_ = "_test-srvc-type._tcp.local." - name = "xxxyyy" - registration_name = "%s.%s" % (name, type_) - - desc = {'path': '/~paulsm/'} - info = ServiceInfo( - type_, registration_name, - socket.inet_aton("10.0.1.2"), 80, 0, 0, - desc, "ash-2.local.") - - # we are going to monkey patch the zeroconf send to check packet sizes - old_send = zc.send - - nbr_answers = nbr_additionals = nbr_authorities = 0 - - def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT): - """Sends an outgoing packet.""" - nonlocal nbr_answers, nbr_additionals, nbr_authorities - for answer, time_ in out.answers: - nbr_answers += 1 - assert answer.ttl == expected_ttl - for answer in out.additionals: - nbr_additionals += 1 - assert answer.ttl == expected_ttl - for answer in out.authorities: - nbr_authorities += 1 - assert answer.ttl == expected_ttl - old_send(out, addr=addr, port=port) - - # monkey patch the zeroconf send - setattr(zc, "send", send) - - # register service with default TTL - expected_ttl = r._DNS_TTL - zc.register_service(info) - assert nbr_answers == 12 and nbr_additionals == 0 and nbr_authorities == 3 - nbr_answers = nbr_additionals = nbr_authorities = 0 - - # query - query = r.DNSOutgoing(r._FLAGS_QR_QUERY | r._FLAGS_AA) - query.add_question(r.DNSQuestion(info.type, r._TYPE_PTR, r._CLASS_IN)) - query.add_question(r.DNSQuestion(info.name, r._TYPE_SRV, r._CLASS_IN)) - query.add_question(r.DNSQuestion(info.name, r._TYPE_TXT, r._CLASS_IN)) - query.add_question(r.DNSQuestion(info.server, r._TYPE_A, r._CLASS_IN)) - zc.handle_query(r.DNSIncoming(query.packet()), r._MDNS_ADDR, r._MDNS_PORT) - assert nbr_answers == 4 and nbr_additionals == 1 and nbr_authorities == 0 - nbr_answers = nbr_additionals = nbr_authorities = 0 - - # unregister - expected_ttl = 0 - zc.unregister_service(info) - assert nbr_answers == 12 and nbr_additionals == 0 and nbr_authorities == 0 - nbr_answers = nbr_additionals = nbr_authorities = 0 - - # register service with custom TTL - expected_ttl = r._DNS_TTL * 2 - assert expected_ttl != r._DNS_TTL - zc.register_service(info, ttl=expected_ttl) - assert nbr_answers == 12 and nbr_additionals == 0 and nbr_authorities == 3 - nbr_answers = nbr_additionals = nbr_authorities = 0 - - # query - query = r.DNSOutgoing(r._FLAGS_QR_QUERY | r._FLAGS_AA) - query.add_question(r.DNSQuestion(info.type, r._TYPE_PTR, r._CLASS_IN)) - query.add_question(r.DNSQuestion(info.name, r._TYPE_SRV, r._CLASS_IN)) - query.add_question(r.DNSQuestion(info.name, r._TYPE_TXT, r._CLASS_IN)) - query.add_question(r.DNSQuestion(info.server, r._TYPE_A, r._CLASS_IN)) - zc.handle_query(r.DNSIncoming(query.packet()), r._MDNS_ADDR, r._MDNS_PORT) - assert nbr_answers == 4 and nbr_additionals == 1 and nbr_authorities == 0 - nbr_answers = nbr_additionals = nbr_authorities = 0 - - # unregister - expected_ttl = 0 - zc.unregister_service(info) - assert nbr_answers == 12 and nbr_additionals == 0 and nbr_authorities == 0 - nbr_answers = nbr_additionals = nbr_authorities = 0 - - -class TestDNSCache(unittest.TestCase): - - def test_order(self): - record1 = r.DNSAddress('a', r._TYPE_SOA, r._CLASS_IN, 1, b'a') - record2 = r.DNSAddress('a', r._TYPE_SOA, r._CLASS_IN, 1, b'b') - cache = r.DNSCache() - cache.add(record1) - cache.add(record2) - entry = r.DNSEntry('a', r._TYPE_SOA, r._CLASS_IN) - cached_record = cache.get(entry) - self.assertEqual(cached_record, record2) - - -class ServiceTypesQuery(unittest.TestCase): - - def test_integration_with_listener(self): - - type_ = "_test-srvc-type._tcp.local." - name = "xxxyyy" - registration_name = "%s.%s" % (name, type_) - - zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1']) - desc = {'path': '/~paulsm/'} - info = ServiceInfo( - type_, registration_name, - socket.inet_aton("10.0.1.2"), 80, 0, 0, - desc, "ash-2.local.") - zeroconf_registrar.register_service(info) - - try: - service_types = ZeroconfServiceTypes.find( - interfaces=['127.0.0.1'], timeout=0.5) - assert type_ in service_types - service_types = ZeroconfServiceTypes.find( - zc=zeroconf_registrar, timeout=0.5) - assert type_ in service_types - - finally: - zeroconf_registrar.close() - - def test_integration_with_subtype_and_listener(self): - subtype_ = "_subtype._sub" - type_ = "_type._tcp.local." - name = "xxxyyy" - # Note: discovery returns only DNS-SD type not subtype - discovery_type = "%s.%s" % (subtype_, type_) - registration_name = "%s.%s" % (name, type_) - - zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1']) - desc = {'path': '/~paulsm/'} - info = ServiceInfo( - discovery_type, registration_name, - socket.inet_aton("10.0.1.2"), 80, 0, 0, - desc, "ash-2.local.") - zeroconf_registrar.register_service(info) - - try: - service_types = ZeroconfServiceTypes.find( - interfaces=['127.0.0.1'], timeout=0.5) - assert discovery_type in service_types - service_types = ZeroconfServiceTypes.find( - zc=zeroconf_registrar, timeout=0.5) - assert discovery_type in service_types - - finally: - zeroconf_registrar.close() - - -class ListenerTest(unittest.TestCase): - - def test_integration_with_listener_class(self): - - service_added = Event() - service_removed = Event() - - subtype_name = "My special Subtype" - type_ = "_http._tcp.local." - subtype = subtype_name + "._sub." + type_ - name = "xxxyyyæøå" - registration_name = "%s.%s" % (name, type_) - - class MyListener(r.ServiceListener): - def add_service(self, zeroconf, type, name): - zeroconf.get_service_info(type, name) - service_added.set() - - def remove_service(self, zeroconf, type, name): - service_removed.set() - - listener = MyListener() - zeroconf_browser = Zeroconf(interfaces=['127.0.0.1']) - zeroconf_browser.add_service_listener(subtype, listener) - - properties = dict( - prop_none=None, - prop_string=b'a_prop', - prop_float=1.0, - prop_blank=b'a blanked string', - prop_true=1, - prop_false=0, - ) - - zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1']) - desc = {'path': '/~paulsm/'} # type: r.ServicePropertiesType - desc.update(properties) - info_service = ServiceInfo( - subtype, registration_name, - socket.inet_aton("10.0.1.2"), 80, 0, 0, - desc, "ash-2.local.") - zeroconf_registrar.register_service(info_service) - - try: - service_added.wait(1) - assert service_added.is_set() - - # short pause to allow multicast timers to expire - time.sleep(2) - - # clear the answer cache to force query - for record in zeroconf_browser.cache.entries(): - zeroconf_browser.cache.remove(record) - - # get service info without answer cache - info = zeroconf_browser.get_service_info(type_, registration_name) - assert info is not None - assert info.properties[b'prop_none'] is False - assert info.properties[b'prop_string'] == properties['prop_string'] - assert info.properties[b'prop_float'] is False - assert info.properties[b'prop_blank'] == properties['prop_blank'] - assert info.properties[b'prop_true'] is True - assert info.properties[b'prop_false'] is False - - info = zeroconf_browser.get_service_info(subtype, registration_name) - assert info is not None - assert info.properties[b'prop_none'] is False - - zeroconf_registrar.unregister_service(info_service) - service_removed.wait(1) - assert service_removed.is_set() - finally: - zeroconf_registrar.close() - zeroconf_browser.remove_service_listener(listener) - zeroconf_browser.close() - - -def test_integration(): - service_added = Event() - service_removed = Event() - unexpected_ttl = Event() - got_query = Event() - - type_ = "_http._tcp.local." - registration_name = "xxxyyy.%s" % type_ - - def on_service_state_change(zeroconf, service_type, state_change, name): - if name == registration_name: - if state_change is ServiceStateChange.Added: - service_added.set() - elif state_change is ServiceStateChange.Removed: - service_removed.set() - - zeroconf_browser = Zeroconf(interfaces=['127.0.0.1']) - - # we are going to monkey patch the zeroconf send to check packet sizes - old_send = zeroconf_browser.send - - time_offset = 0.0 - - def current_time_millis(): - """Current system time in milliseconds""" - return time.time() * 1000 + time_offset * 1000 - - expected_ttl = r._DNS_TTL - - nbr_queries = 0 - - def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT): - """Sends an outgoing packet.""" - pout = r.DNSIncoming(out.packet()) - nonlocal nbr_queries - for answer in pout.answers: - nbr_queries += 1 - if not answer.ttl > expected_ttl / 2: - unexpected_ttl.set() - - got_query.set() - old_send(out, addr=addr, port=port) - - # monkey patch the zeroconf send - setattr(zeroconf_browser, "send", send) - - # monkey patch the zeroconf current_time_millis - r.current_time_millis = current_time_millis - - service_added = Event() - service_removed = Event() - - browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) - - zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1']) - desc = {'path': '/~paulsm/'} - info = ServiceInfo( - type_, registration_name, - socket.inet_aton("10.0.1.2"), 80, 0, 0, - desc, "ash-2.local.") - zeroconf_registrar.register_service(info) - - try: - service_added.wait(1) - assert service_added.is_set() - - sleep_count = 0 - while nbr_queries < 50: - time_offset += expected_ttl / 4 - zeroconf_browser.notify_all() - sleep_count += 1 - got_query.wait(1) - got_query.clear() - assert not unexpected_ttl.is_set() - - # Don't remove service, allow close() to cleanup - - finally: - zeroconf_registrar.close() - browser.cancel() - zeroconf_browser.close() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..d68444d0e --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,174 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations + +import asyncio +import platform +import socket +import time +from collections.abc import Iterable +from functools import cache +from unittest import mock + +import ifaddr + +from zeroconf import DNSIncoming, DNSOutgoing, DNSQuestion, DNSRecord, Zeroconf, const +from zeroconf._history import QuestionHistory + +_MONOTONIC_RESOLUTION = time.get_clock_info("monotonic").resolution + +_IS_PYPY = platform.python_implementation() == "PyPy" + +# get_service_info / async_request timeout for tests using the +# `quick_request_timing` fixture. The fixture cuts the initial-query +# delay to ~15ms (10ms _LISTENER_TIME + 1-5ms jitter), so 50ms is +# ample headroom for tests that only need to observe the first one +# or two queries. +QUICK_REQUEST_TIMEOUT_MS = 50 + +# Timeout for ZeroconfServiceTypes.find() / AsyncZeroconfServiceTypes.async_find() +# in loopback integration tests. `find()` is just `time.sleep(timeout)` — +# it doesn't short-circuit on the first matching response — so the +# timeout becomes a lower bound on the test runtime. Callers MUST use +# the `quick_timing` fixture, which shrinks the browser's first-query +# delay from RFC 6762 §5.2's 20-120ms window to 1-5ms; with that shave +# the registrar's response lands inside ~10ms and 75ms is ~7x headroom. +# PyPy's JIT is still warming up the first time this path runs early in +# the suite, so the round trip is too slow for 75ms; give it more room. +LOOPBACK_FIND_TIMEOUT = 0.3 if _IS_PYPY else 0.075 + +# IPv6-only `find()` on Linux GitHub runners can hit `[Errno 101] Network +# is unreachable` on the `::1` socket and falls back to the `fe80::` link- +# local interface, which adds latency the IPv4 loopback path never pays. +# PyPy widens that further with JIT warmup. The 75ms budget that works on +# IPv4 loopback is too tight for the V6Only path under those conditions +# — give it more headroom. +IPV6_LOOPBACK_FIND_TIMEOUT = 0.5 + + +class QuestionHistoryWithoutSuppression(QuestionHistory): + def suppresses(self, question: DNSQuestion, now: float, known_answers: set[DNSRecord]) -> bool: + return False + + +def mock_incoming_msg(records: Iterable[DNSRecord]) -> DNSIncoming: + """Build a `DNSIncoming` response message from a list of `DNSRecord`s.""" + generated = DNSOutgoing(const._FLAGS_QR_RESPONSE) + for record in records: + generated.add_answer_at_time(record, 0) + return DNSIncoming(generated.packets()[0]) + + +def _inject_responses(zc: Zeroconf, msgs: list[DNSIncoming]) -> None: + """Inject a DNSIncoming response.""" + assert zc.loop is not None + + async def _wait_for_response(): + for msg in msgs: + zc.record_manager.async_updates_from_response(msg) + + asyncio.run_coroutine_threadsafe(_wait_for_response(), zc.loop).result() + + +def _inject_response(zc: Zeroconf, msg: DNSIncoming) -> None: + """Inject a DNSIncoming response.""" + _inject_responses(zc, [msg]) + + +def _wait_for_start(zc: Zeroconf) -> None: + """Wait for all sockets to be up and running.""" + assert zc.loop is not None + asyncio.run_coroutine_threadsafe(zc.async_wait_for_start(), zc.loop).result() + + +@cache +def has_working_ipv6(): + """Return True if the system can bind an IPv6 address.""" + if not socket.has_ipv6: + return False + + sock = None + try: + sock = socket.socket(socket.AF_INET6) + sock.bind(("::1", 0)) + except Exception: + return False + finally: + if sock: + sock.close() + + for iface in ifaddr.get_adapters(): + for addr in iface.ips: + if addr.is_IPv6 and iface.index is not None: + return True + return False + + +def _clear_cache(zc: Zeroconf) -> None: + zc.cache.cache.clear() + zc.question_history.clear() + # Reset per-listener dedup state so identical packets sent in the + # next phase of the test are not suppressed by the bounded recency + # window populated during the previous phase. + if zc.engine is not None: + for protocol in zc.engine.protocols: + protocol._recent_packets.clear() + protocol.data = None + protocol.last_time = 0 + + +def _backdate_cache(zc: Zeroconf, ms: int = 1100) -> None: + """Backdate every cached record's `created` time by `ms` milliseconds. + + rfc6762#section-10.2 keys off "received more than one second ago", so + backdating is equivalent to sleeping `ms` in real time without the + wall-clock wait. + + Iterate `store.values()`, not the dict directly — when a record is + re-added with an equal hash, the key stays the original object while + the value is replaced with the latest; mutating the key would update + stale objects no one reads. + """ + for store in zc.cache.cache.values(): + for record in store.values(): + record.created -= ms + + +def time_changed_millis(millis: float | None = None) -> None: + """Call all scheduled events for a time.""" + loop = asyncio.get_running_loop() + loop_time = loop.time() + mock_seconds_into_future = millis / 1000 if millis is not None else loop_time + + with mock.patch("time.monotonic", return_value=mock_seconds_into_future): + for task in list(loop._scheduled): # type: ignore[attr-defined] + if not isinstance(task, asyncio.TimerHandle): + continue + if task.cancelled(): + continue + + future_seconds = task.when() - (loop_time + _MONOTONIC_RESOLUTION) + + if mock_seconds_into_future >= future_seconds: + task._run() + task.cancel() diff --git a/tests/benchmarks/__init__.py b/tests/benchmarks/__init__.py new file mode 100644 index 000000000..9d48db4f9 --- /dev/null +++ b/tests/benchmarks/__init__.py @@ -0,0 +1 @@ +from __future__ import annotations diff --git a/tests/benchmarks/helpers.py b/tests/benchmarks/helpers.py new file mode 100644 index 000000000..4f5f7d66c --- /dev/null +++ b/tests/benchmarks/helpers.py @@ -0,0 +1,155 @@ +"""Benchmark helpers.""" + +from __future__ import annotations + +import socket + +from zeroconf import DNSAddress, DNSOutgoing, DNSService, DNSText, const + + +def generate_packets() -> DNSOutgoing: + out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) + address = socket.inet_pton(socket.AF_INET, "192.168.208.5") + + additionals = [ + { + "name": "HASS Bridge ZJWH FF5137._hap._tcp.local.", + "address": address, + "port": 51832, + "text": b"\x13md=HASS Bridge" + b" ZJWH\x06pv=1.0\x14id=01:6B:30:FF:51:37\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=L0m/aQ==", + }, + { + "name": "HASS Bridge 3K9A C2582A._hap._tcp.local.", + "address": address, + "port": 51834, + "text": b"\x13md=HASS Bridge" + b" 3K9A\x06pv=1.0\x14id=E2:AA:5B:C2:58:2A\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b2CnzQ==", + }, + { + "name": "Master Bed TV CEDB27._hap._tcp.local.", + "address": address, + "port": 51830, + "text": b"\x10md=Master Bed" + b" TV\x06pv=1.0\x14id=9E:B7:44:CE:DB:27\x05c#=18\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=CVj1kw==", + }, + { + "name": "Living Room TV 921B77._hap._tcp.local.", + "address": address, + "port": 51833, + "text": b"\x11md=Living Room" + b" TV\x06pv=1.0\x14id=11:61:E7:92:1B:77\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=qU77SQ==", + }, + { + "name": "HASS Bridge ZC8X FF413D._hap._tcp.local.", + "address": address, + "port": 51829, + "text": b"\x13md=HASS Bridge" + b" ZC8X\x06pv=1.0\x14id=96:14:45:FF:41:3D\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b0QZlg==", + }, + { + "name": "HASS Bridge WLTF 4BE61F._hap._tcp.local.", + "address": address, + "port": 51837, + "text": b"\x13md=HASS Bridge" + b" WLTF\x06pv=1.0\x14id=E0:E7:98:4B:E6:1F\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=ahAISA==", + }, + { + "name": "FrontdoorCamera 8941D1._hap._tcp.local.", + "address": address, + "port": 54898, + "text": b"\x12md=FrontdoorCamera\x06pv=1.0\x14id=9F:B7:DC:89:41:D1\x04c#=2\x04" + b"s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=0+MXmA==", + }, + { + "name": "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + "address": address, + "port": 51836, + "text": b"\x13md=HASS Bridge" + b" W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=6fLM5A==", + }, + { + "name": "HASS Bridge Y9OO EFF0A7._hap._tcp.local.", + "address": address, + "port": 51838, + "text": b"\x13md=HASS Bridge" + b" Y9OO\x06pv=1.0\x14id=D3:FE:98:EF:F0:A7\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=u3bdfw==", + }, + { + "name": "Snooze Room TV 6B89B0._hap._tcp.local.", + "address": address, + "port": 51835, + "text": b"\x11md=Snooze Room" + b" TV\x06pv=1.0\x14id=5F:D5:70:6B:89:B0\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=xNTqsg==", + }, + { + "name": "AlexanderHomeAssistant 74651D._hap._tcp.local.", + "address": address, + "port": 54811, + "text": b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05" + b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==", + }, + { + "name": "HASS Bridge OS95 39C053._hap._tcp.local.", + "address": address, + "port": 51831, + "text": b"\x13md=HASS Bridge" + b" OS95\x06pv=1.0\x14id=7E:8C:E6:39:C0:53\x05c#=12\x04s#=1\x04ff=0\x04ci=2" + b"\x04sf=0\x0bsh=Xfe5LQ==", + }, + ] + + out.add_answer_at_time( + DNSText( + "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1" + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + + for record in additionals: + out.add_additional_answer( + DNSService( + record["name"], # type: ignore + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + record["port"], # type: ignore + record["name"], # type: ignore + ) + ) + out.add_additional_answer( + DNSText( + record["name"], # type: ignore + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + record["text"], # type: ignore + ) + ) + out.add_additional_answer( + DNSAddress( + record["name"], # type: ignore + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + record["address"], # type: ignore + ) + ) + + return out diff --git a/tests/benchmarks/test_cache.py b/tests/benchmarks/test_cache.py new file mode 100644 index 000000000..7813f6798 --- /dev/null +++ b/tests/benchmarks/test_cache.py @@ -0,0 +1,50 @@ +from __future__ import annotations + +from pytest_codspeed import BenchmarkFixture + +from zeroconf import DNSCache, DNSPointer, current_time_millis +from zeroconf.const import _CLASS_IN, _TYPE_PTR + + +def test_add_expire_1000_records(benchmark: BenchmarkFixture) -> None: + """Benchmark for DNSCache to expire 10000 records.""" + cache = DNSCache() + now = current_time_millis() + records = [ + DNSPointer( + name=f"test{id}.local.", + type_=_TYPE_PTR, + class_=_CLASS_IN, + ttl=60, + alias=f"test{id}.local.", + created=now + id, + ) + for id in range(1000) + ] + + @benchmark + def _expire_records() -> None: + cache.async_add_records(records) + cache.async_expire(now + 100_000) + + +def test_expire_no_records_to_expire(benchmark: BenchmarkFixture) -> None: + """Benchmark for DNSCache with 1000 records none to expire.""" + cache = DNSCache() + now = current_time_millis() + cache.async_add_records( + DNSPointer( + name=f"test{id}.local.", + type_=_TYPE_PTR, + class_=_CLASS_IN, + ttl=60, + alias=f"test{id}.local.", + created=now + id, + ) + for id in range(1000) + ) + cache.async_expire(now) + + @benchmark + def _expire_records() -> None: + cache.async_expire(now) diff --git a/tests/benchmarks/test_cache_bound.py b/tests/benchmarks/test_cache_bound.py new file mode 100644 index 000000000..774129e3b --- /dev/null +++ b/tests/benchmarks/test_cache_bound.py @@ -0,0 +1,68 @@ +"""Benchmark for the DNSCache record-count bound + overflow eviction.""" + +from __future__ import annotations + +from collections.abc import Iterator +from itertools import count + +from pytest_codspeed import BenchmarkFixture + +from zeroconf import DNSAddress, DNSCache, current_time_millis +from zeroconf.const import _CLASS_IN, _MAX_CACHE_RECORDS, _TYPE_A + + +def _make_records(count_: int, now: float, prefix: str = "bench") -> list[DNSAddress]: + return [ + DNSAddress( + f"{prefix}-{i}.local.", + _TYPE_A, + _CLASS_IN, + 120, + bytes(((i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF)), + created=now + i, + ) + for i in range(count_) + ] + + +def _unbounded_records(now: float, prefix: str = "evict") -> Iterator[DNSAddress]: + """Unbounded generator of unique-name DNSAddress records.""" + for i in count(): + yield DNSAddress( + f"{prefix}-{i}.local.", + _TYPE_A, + _CLASS_IN, + 120, + bytes(((i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF)), + created=now + i, + ) + + +def test_cache_add_below_cap(benchmark: BenchmarkFixture) -> None: + """Adding records while the cache is well below the cap (no eviction).""" + now = current_time_millis() + records = _make_records(1000, now) + + @benchmark + def _add() -> None: + cache = DNSCache() + cache.async_add_records(records) + + +def test_cache_add_at_cap_evicts(benchmark: BenchmarkFixture) -> None: + """Steady-state add at the cap: every measured insert forces one eviction. + + Pre-fills the cache to ``_MAX_CACHE_RECORDS`` outside the timed body so + only the eviction-path adds are measured. Each benchmark iteration + pulls one fresh unique record from an unbounded generator, keeping the + cache permanently at the cap. The generator avoids the iteration-count + cap that a pre-built pool would impose for very fast operations. + """ + now = current_time_millis() + cache = DNSCache() + cache.async_add_records(_make_records(_MAX_CACHE_RECORDS, now, prefix="fill")) + pool = _unbounded_records(now + _MAX_CACHE_RECORDS) + + @benchmark + def _evict_one() -> None: + cache.async_add_records([next(pool)]) diff --git a/tests/benchmarks/test_incoming.py b/tests/benchmarks/test_incoming.py new file mode 100644 index 000000000..6d31e51e5 --- /dev/null +++ b/tests/benchmarks/test_incoming.py @@ -0,0 +1,186 @@ +"""Benchmark for DNSIncoming.""" + +from __future__ import annotations + +import socket + +from pytest_codspeed import BenchmarkFixture + +from zeroconf import ( + DNSAddress, + DNSIncoming, + DNSNsec, + DNSOutgoing, + DNSService, + DNSText, + const, +) + + +def generate_packets() -> list[bytes]: + out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) + address = socket.inet_pton(socket.AF_INET, "192.168.208.5") + + additionals = [ + { + "name": "HASS Bridge ZJWH FF5137._hap._tcp.local.", + "address": address, + "port": 51832, + "text": b"\x13md=HASS Bridge" + b" ZJWH\x06pv=1.0\x14id=01:6B:30:FF:51:37\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=L0m/aQ==", + }, + { + "name": "HASS Bridge 3K9A C2582A._hap._tcp.local.", + "address": address, + "port": 51834, + "text": b"\x13md=HASS Bridge" + b" 3K9A\x06pv=1.0\x14id=E2:AA:5B:C2:58:2A\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b2CnzQ==", + }, + { + "name": "Master Bed TV CEDB27._hap._tcp.local.", + "address": address, + "port": 51830, + "text": b"\x10md=Master Bed" + b" TV\x06pv=1.0\x14id=9E:B7:44:CE:DB:27\x05c#=18\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=CVj1kw==", + }, + { + "name": "Living Room TV 921B77._hap._tcp.local.", + "address": address, + "port": 51833, + "text": b"\x11md=Living Room" + b" TV\x06pv=1.0\x14id=11:61:E7:92:1B:77\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=qU77SQ==", + }, + { + "name": "HASS Bridge ZC8X FF413D._hap._tcp.local.", + "address": address, + "port": 51829, + "text": b"\x13md=HASS Bridge" + b" ZC8X\x06pv=1.0\x14id=96:14:45:FF:41:3D\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b0QZlg==", + }, + { + "name": "HASS Bridge WLTF 4BE61F._hap._tcp.local.", + "address": address, + "port": 51837, + "text": b"\x13md=HASS Bridge" + b" WLTF\x06pv=1.0\x14id=E0:E7:98:4B:E6:1F\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=ahAISA==", + }, + { + "name": "FrontdoorCamera 8941D1._hap._tcp.local.", + "address": address, + "port": 54898, + "text": b"\x12md=FrontdoorCamera\x06pv=1.0\x14id=9F:B7:DC:89:41:D1\x04c#=2\x04" + b"s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=0+MXmA==", + }, + { + "name": "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + "address": address, + "port": 51836, + "text": b"\x13md=HASS Bridge" + b" W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=6fLM5A==", + }, + { + "name": "HASS Bridge Y9OO EFF0A7._hap._tcp.local.", + "address": address, + "port": 51838, + "text": b"\x13md=HASS Bridge" + b" Y9OO\x06pv=1.0\x14id=D3:FE:98:EF:F0:A7\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=u3bdfw==", + }, + { + "name": "Snooze Room TV 6B89B0._hap._tcp.local.", + "address": address, + "port": 51835, + "text": b"\x11md=Snooze Room" + b" TV\x06pv=1.0\x14id=5F:D5:70:6B:89:B0\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=xNTqsg==", + }, + { + "name": "AlexanderHomeAssistant 74651D._hap._tcp.local.", + "address": address, + "port": 54811, + "text": b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05" + b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==", + }, + { + "name": "HASS Bridge OS95 39C053._hap._tcp.local.", + "address": address, + "port": 51831, + "text": b"\x13md=HASS Bridge" + b" OS95\x06pv=1.0\x14id=7E:8C:E6:39:C0:53\x05c#=12\x04s#=1\x04ff=0\x04ci=2" + b"\x04sf=0\x0bsh=Xfe5LQ==", + }, + ] + + out.add_answer_at_time( + DNSText( + "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1" + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + + for record in additionals: + out.add_additional_answer( + DNSService( + record["name"], # type: ignore + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + record["port"], # type: ignore + record["name"], # type: ignore + ) + ) + out.add_additional_answer( + DNSText( + record["name"], # type: ignore + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + record["text"], # type: ignore + ) + ) + out.add_additional_answer( + DNSAddress( + record["name"], # type: ignore + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + record["address"], # type: ignore + ) + ) + out.add_additional_answer( + DNSNsec( + record["name"], # type: ignore + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + record["name"], # type: ignore + [const._TYPE_TXT, const._TYPE_SRV], + ) + ) + + return out.packets() + + +packets = generate_packets() + + +def test_parse_incoming_message(benchmark: BenchmarkFixture) -> None: + @benchmark + def parse_incoming_message() -> None: + for packet in packets: + DNSIncoming(packet).answers # noqa: B018 + break diff --git a/tests/benchmarks/test_ipaddress.py b/tests/benchmarks/test_ipaddress.py new file mode 100644 index 000000000..60aea89c7 --- /dev/null +++ b/tests/benchmarks/test_ipaddress.py @@ -0,0 +1,48 @@ +"""Benchmarks for zeroconf._utils.ipaddress address objects.""" + +from __future__ import annotations + +from pytest_codspeed import BenchmarkFixture + +from zeroconf._utils.ipaddress import ZeroconfIPv4Address, ZeroconfIPv6Address + +_IPV4_STRS = [f"10.{(i >> 8) & 0xFF}.{i & 0xFF}.1" for i in range(1000)] +_IPV6_BYTES = [(0x20010DB8 << 96 | i).to_bytes(16, "big") for i in range(1000)] + + +def test_create_ipv4_addresses(benchmark: BenchmarkFixture) -> None: + """Benchmark constructing 1000 distinct IPv4 address objects.""" + + @benchmark + def _create() -> None: + for addr in _IPV4_STRS: + ZeroconfIPv4Address(addr) + + +def test_create_ipv6_addresses(benchmark: BenchmarkFixture) -> None: + """Benchmark constructing 1000 distinct IPv6 address objects.""" + + @benchmark + def _create() -> None: + for addr in _IPV6_BYTES: + ZeroconfIPv6Address(addr) + + +def test_hash_ipv4_address(benchmark: BenchmarkFixture) -> None: + """Benchmark hashing the same IPv4 address object 1000 times.""" + addr = ZeroconfIPv4Address("10.0.0.1") + + @benchmark + def _hash() -> None: + for _ in range(1000): + hash(addr) + + +def test_hash_ipv6_address(benchmark: BenchmarkFixture) -> None: + """Benchmark hashing the same IPv6 address object 1000 times.""" + addr = ZeroconfIPv6Address(b"\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01") + + @benchmark + def _hash() -> None: + for _ in range(1000): + hash(addr) diff --git a/tests/benchmarks/test_listener_dedup.py b/tests/benchmarks/test_listener_dedup.py new file mode 100644 index 000000000..b1b500ba2 --- /dev/null +++ b/tests/benchmarks/test_listener_dedup.py @@ -0,0 +1,128 @@ +"""Benchmarks for the listener duplicate-packet suppression hot path. + +These pin the cost of ``AsyncListener._process_datagram_at_time`` under +three packet-stream shapes that exercise the dedup branch differently: + +- ``test_dedup_hit_same_payload`` — N copies of one payload (steady-state + dedup hit). +- ``test_alternating_payloads`` — A, B, A, B, ... The single-slot + remembered-last-packet dedup misses on every packet because each one + differs from its immediate predecessor; a bounded recency window + dedups after the second packet. This is the flood shape from + issue #1724. +- ``test_unique_payloads`` — N distinct payloads (no dedup hit possible + on either implementation). Measures the store/evict overhead on the + miss path. + +Downstream work is held constant across implementations by overriding +``handle_query_or_defer`` on a subclass with a no-op, so the only +remaining variable is the dedup decision itself. +""" + +from __future__ import annotations + +import pytest +from pytest_codspeed import BenchmarkFixture + +from zeroconf import DNSOutgoing, DNSQuestion, const +from zeroconf._listener import AsyncListener +from zeroconf._utils.time import current_time_millis +from zeroconf.asyncio import AsyncZeroconf + + +class _InertListener(AsyncListener): + """AsyncListener that skips response generation. + + The dedup branch is the only piece that diverges between the + single-slot and bounded-window implementations. Stubbing query + handling keeps the per-packet cost outside the dedup branch + constant so the benchmark isolates the change under test. + """ + + def handle_query_or_defer(self, *args: object, **kwargs: object) -> None: # type: ignore[override] + return None + + +def _make_query_packet(name: str) -> bytes: + out = DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + out.add_question(DNSQuestion(name, const._TYPE_PTR, const._CLASS_IN)) + return out.packets()[0] + + +_ITERATIONS = 200 +_ADDRS: tuple[str, int] = ("192.0.2.1", 5353) + + +def _build_listener(aiozc: AsyncZeroconf) -> _InertListener: + zc = aiozc.zeroconf + # A non-empty registry keeps the realistic code path live (the early + # ``has_entries`` exit would otherwise bypass the per-packet work we + # want to measure). Toggling the flag directly avoids the event-loop + # round-trip that ``async_register_service`` would impose. + zc.registry.has_entries = True + listener = _InertListener(zc) + listener.transport = object() # type: ignore[assignment] + return listener + + +@pytest.mark.asyncio +async def test_dedup_hit_same_payload(benchmark: BenchmarkFixture) -> None: + """Steady-state dedup hit: same payload repeated.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + listener = _build_listener(aiozc) + packet = _make_query_packet("a._http._tcp.local.") + data_len = len(packet) + # Prime the dedup state so the first iteration is already a hit. + listener._process_datagram_at_time(False, data_len, current_time_millis(), packet, _ADDRS) + + @benchmark + def _run() -> None: + # Single fresh timestamp keeps every call inside the + # suppression interval so each one is a dedup hit. + t = current_time_millis() + for _ in range(_ITERATIONS): + listener._process_datagram_at_time(False, data_len, t, packet, _ADDRS) + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_alternating_payloads(benchmark: BenchmarkFixture) -> None: + """Flood shape from issue #1724: A, B, A, B, ...""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + listener = _build_listener(aiozc) + packet_a = _make_query_packet("a._http._tcp.local.") + packet_b = _make_query_packet("b._http._tcp.local.") + len_a = len(packet_a) + len_b = len(packet_b) + + @benchmark + def _run() -> None: + t = current_time_millis() + for i in range(_ITERATIONS): + if i & 1: + listener._process_datagram_at_time(False, len_b, t, packet_b, _ADDRS) + else: + listener._process_datagram_at_time(False, len_a, t, packet_a, _ADDRS) + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_unique_payloads(benchmark: BenchmarkFixture) -> None: + """Stream of distinct payloads — no dedup hit on either implementation.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + listener = _build_listener(aiozc) + packets = [_make_query_packet(f"x{i}._http._tcp.local.") for i in range(_ITERATIONS)] + lengths = [len(p) for p in packets] + + @benchmark + def _run() -> None: + t = current_time_millis() + for packet, data_len in zip(packets, lengths, strict=True): + listener._process_datagram_at_time(False, data_len, t, packet, _ADDRS) + + await aiozc.async_close() diff --git a/tests/benchmarks/test_mark_seen.py b/tests/benchmarks/test_mark_seen.py new file mode 100644 index 000000000..4f82da8cd --- /dev/null +++ b/tests/benchmarks/test_mark_seen.py @@ -0,0 +1,39 @@ +"""Benchmark for _logger._mark_seen.""" + +from __future__ import annotations + +from pytest_codspeed import BenchmarkFixture + +from zeroconf._logger import _MAX_SEEN_LOGS, _mark_seen + + +def test_mark_seen_hit(benchmark: BenchmarkFixture) -> None: + """Benchmark the cache-hit path (same key repeated).""" + seen: dict[str, None] = {"warm": None} + + @benchmark + def _hit() -> None: + for _ in range(1000): + _mark_seen(seen, "warm") + + +def test_mark_seen_fill(benchmark: BenchmarkFixture) -> None: + """Benchmark filling from empty up to the cap (no evictions).""" + keys = [f"key-{i}" for i in range(_MAX_SEEN_LOGS)] + + @benchmark + def _fill() -> None: + seen: dict[str, None] = {} + for k in keys: + _mark_seen(seen, k) + + +def test_mark_seen_churn(benchmark: BenchmarkFixture) -> None: + """Benchmark sustained eviction (every call past the cap drops oldest).""" + keys = [f"churn-{i}" for i in range(_MAX_SEEN_LOGS * 4)] + + @benchmark + def _churn() -> None: + seen: dict[str, None] = {} + for k in keys: + _mark_seen(seen, k) diff --git a/tests/benchmarks/test_outgoing.py b/tests/benchmarks/test_outgoing.py new file mode 100644 index 000000000..a8db4d6f8 --- /dev/null +++ b/tests/benchmarks/test_outgoing.py @@ -0,0 +1,20 @@ +"""Benchmark for DNSOutgoing.""" + +from __future__ import annotations + +from pytest_codspeed import BenchmarkFixture + +from zeroconf._protocol.outgoing import State + +from .helpers import generate_packets + + +def test_parse_outgoing_message(benchmark: BenchmarkFixture) -> None: + out = generate_packets() + + @benchmark + def make_outgoing_message() -> None: + out.packets() + out.state = State.init.value + out.finished = False + out._reset_for_next_packet() diff --git a/tests/benchmarks/test_send.py b/tests/benchmarks/test_send.py new file mode 100644 index 000000000..596662a2b --- /dev/null +++ b/tests/benchmarks/test_send.py @@ -0,0 +1,24 @@ +"""Benchmark for sending packets.""" + +from __future__ import annotations + +import pytest +from pytest_codspeed import BenchmarkFixture + +from zeroconf.asyncio import AsyncZeroconf + +from .helpers import generate_packets + + +@pytest.mark.asyncio +async def test_sending_packets(benchmark: BenchmarkFixture) -> None: + """Benchmark sending packets.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + out = generate_packets() + + @benchmark + def _send_packets() -> None: + aiozc.zeroconf.async_send(out) + + await aiozc.async_close() diff --git a/tests/benchmarks/test_txt_properties.py b/tests/benchmarks/test_txt_properties.py new file mode 100644 index 000000000..72afa0b65 --- /dev/null +++ b/tests/benchmarks/test_txt_properties.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +from pytest_codspeed import BenchmarkFixture + +from zeroconf import ServiceInfo + +info = ServiceInfo( + "_test._tcp.local.", + "test._test._tcp.local.", + properties=( + b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05" + b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==" + ), +) + + +def test_txt_properties(benchmark: BenchmarkFixture) -> None: + @benchmark + def process_properties() -> None: + info._properties = None + info.properties # noqa: B018 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..573b93949 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,208 @@ +"""conftest for zeroconf tests.""" + +from __future__ import annotations + +import threading +from collections.abc import AsyncGenerator, Generator, Iterator +from unittest.mock import patch + +import pytest +import pytest_asyncio + +from zeroconf import Zeroconf, _core, const +from zeroconf._handlers import query_handler +from zeroconf._services import browser as service_browser +from zeroconf._services import info as service_info +from zeroconf.asyncio import AsyncZeroconf + +try: + from blockbuster import BlockBuster, blockbuster_ctx +except ImportError: # platforms without blockbuster (e.g. PyPy under QEMU) + BlockBuster = None # type: ignore[assignment,misc] + blockbuster_ctx = None # type: ignore[assignment] + +_BENCHMARKS_DIR = "tests/benchmarks" + +# Tests that perform sync IO inside the asyncio event loop and trip +# blockbuster. Marked xfail (strict=False) so CI stays green; pop +# entries as the underlying blocking calls get fixed. Most of the +# `test_async_service_registration*` and `test_async_tasks` entries +# share a single root cause: `Zeroconf.async_close()` -> ... -> +# `ServiceBrowser.cancel()` calls `Thread.join()` to drain the +# dedicated browser thread, and on Python 3.10-3.12 the thread is +# still alive when the join happens. `test_use_asyncio_false_*` is +# by design (sync bootstrap when `use_asyncio=False` is requested from +# inside a running loop); `test_run_coro_with_timeout` exercises the +# sync-from-thread bridge intentionally. The strict=False marker keeps +# the suite green on the Python versions where the race resolves the +# other way. +_KNOWN_BLOCKING: frozenset[str] = frozenset( + { + "tests/test_asyncio.py::test_async_service_registration", + "tests/test_asyncio.py::test_async_service_registration_with_server_missing", + "tests/test_asyncio.py::test_async_service_registration_same_server_different_ports", + "tests/test_asyncio.py::test_async_service_registration_same_server_same_ports", + "tests/test_asyncio.py::test_async_tasks", + "tests/test_core.py::Framework::test_use_asyncio_false_forces_thread_when_loop_running", + "tests/utils/test_asyncio.py::test_run_coro_with_timeout", + } +) + + +def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None: + """Mark known-blocking tests xfail so blockbuster doesn't fail the suite.""" + if blockbuster_ctx is None: + return + marker = pytest.mark.xfail( + reason="blockbuster: blocking call in asyncio path", + strict=False, + ) + for item in items: + if item.nodeid in _KNOWN_BLOCKING: + item.add_marker(marker) + + +@pytest.fixture(autouse=True) +def blockbuster( + request: pytest.FixtureRequest, +) -> Iterator[BlockBuster | None]: + """Fail any test that performs a blocking call inside the asyncio loop.""" + if blockbuster_ctx is None or _BENCHMARKS_DIR in str(request.node.fspath): + yield None + return + with blockbuster_ctx() as bb: + yield bb + + +@pytest.fixture(autouse=True) +def verify_threads_ended(): + """Verify that the threads are not running after the test.""" + threads_before = frozenset(threading.enumerate()) + yield + threads = frozenset(threading.enumerate()) - threads_before + assert not threads + + +@pytest.fixture +def zc_loopback() -> Generator[Zeroconf]: + """Yield a loopback `Zeroconf` and close it on teardown. + + Replaces the inline `zc = Zeroconf(interfaces=["127.0.0.1"])` + + explicit `zc.close()` pattern duplicated across the suite. Calling + `zc.close()` inside a test is still safe — `close()` is idempotent. + """ + zc = Zeroconf(interfaces=["127.0.0.1"]) + try: + yield zc + finally: + zc.close() + + +@pytest_asyncio.fixture +async def aiozc_loopback() -> AsyncGenerator[AsyncZeroconf]: + """Yield a loopback `AsyncZeroconf` and close it on teardown. + + Replaces the inline `aiozc = AsyncZeroconf(interfaces=["127.0.0.1"])` + + explicit `await aiozc.async_close()` pattern duplicated across the + suite. Calling `async_close()` inside a test is still safe. + """ + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + try: + yield aiozc + finally: + await aiozc.async_close() + + +@pytest.fixture +def run_isolated(): + """Change the mDNS port to run the test in isolation.""" + with ( + patch.object(query_handler, "_MDNS_PORT", 5454), + patch.object(_core, "_MDNS_PORT", 5454), + patch.object(const, "_MDNS_PORT", 5454), + ): + yield + + +@pytest.fixture +def disable_duplicate_packet_suppression(): + """Disable duplicate packet suppress. + + Some tests run too slowly because of the duplicate + packet suppression. + """ + with patch.object(const, "_DUPLICATE_PACKET_SUPPRESSION_INTERVAL", 0): + yield + + +@pytest.fixture +def quick_timing() -> Generator[None]: + """Shorten the probe/announce/goodbye/first-query intervals for tests on loopback. + + The production values (_CHECK_TIME=500ms, _REGISTER_TIME=225ms, + _UNREGISTER_TIME=125ms, _PROBE_RANDOM_DELAY_INTERVAL=150-250ms, + _FIRST_QUERY_DELAY_RANDOM_INTERVAL=20-120ms) exist for RFC 6762 + interop on real networks (§8.1 thundering-herd avoidance for + probing, §5.2 for the initial-query delay). Tests on 127.0.0.1 + do not need them and pay 1-2s per register/unregister cycle, + 150-250ms per probe, and 20-120ms per ServiceBrowser startup + without this fixture. Opt in either by adding `quick_timing` + to a test's argument list or via + `@pytest.mark.usefixtures("quick_timing")` on the test or + its class. + """ + with ( + patch.object(_core, "_CHECK_TIME", 10), + patch.object(_core, "_REGISTER_TIME", 10), + patch.object(_core, "_UNREGISTER_TIME", 10), + patch.object(_core, "_PROBE_RANDOM_DELAY_INTERVAL", (1, 5)), + patch.object(service_browser, "_FIRST_QUERY_DELAY_RANDOM_INTERVAL", (1, 5)), + ): + yield + + +@pytest.fixture +def quick_aggregation_timing() -> Generator[None]: + """Scale multicast aggregation / network-protection delays 10x for tests. + + The aggregation tests in `tests/test_handlers.py` verify timing- + dependent behaviour of `MulticastOutgoingQueue`: aggregation window, + network protection (~1s), and protected aggregation. The behaviour + under test is a ratio of these constants — the exact wall-clock + values are not the contract — so scaling them down and the test + sleeps in lock-step preserves what is tested while dropping each + test from ~3s to ~0.3s. + + The patches must be in place before `AsyncZeroconf(...)` is + constructed because `MulticastOutgoingQueue` reads the constants at + init time and stashes them on the instance. The per-queue + `_multicast_delay_random_min` / `_max` jitter (1-5ms here) can + still be set on the queue instance after construction by the test + itself — those slots are `cdef public` in the .pxd. + """ + with ( + patch.object(_core, "_AGGREGATION_DELAY", 50), + patch.object(_core, "_PROTECTED_AGGREGATION_DELAY", 20), + patch.object(_core, "_ONE_SECOND", 100), + ): + yield + + +@pytest.fixture +def quick_request_timing() -> Generator[None]: + """Shorten the initial-query delay used by AsyncServiceInfo.async_request. + + The 200ms `_LISTENER_TIME` and 20-120ms random jitter (RFC 6762 + §5.2) help spread queries from multiple clients on real networks. + On loopback they're pure overhead — get_service_info-style tests + wait ~250ms before the first query even fires. Opt in either by + adding `quick_request_timing` to a test's argument list or via + `@pytest.mark.usefixtures("quick_request_timing")` on the test + or its class, then drop the test's own timeouts (which had to + accommodate that delay). + """ + with ( + patch.object(service_info, "_LISTENER_TIME", 10), + patch.object(service_info, "_AVOID_SYNC_DELAY_RANDOM_INTERVAL", (1, 5)), + ): + yield diff --git a/tests/services/__init__.py b/tests/services/__init__.py new file mode 100644 index 000000000..584a74eca --- /dev/null +++ b/tests/services/__init__.py @@ -0,0 +1,23 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations diff --git a/tests/services/test_browser.py b/tests/services/test_browser.py new file mode 100644 index 000000000..28b3d12e3 --- /dev/null +++ b/tests/services/test_browser.py @@ -0,0 +1,1791 @@ +"""Unit tests for zeroconf._services.browser.""" + +from __future__ import annotations + +import asyncio +import logging +import os +import socket +import time +import unittest +from threading import Event +from typing import cast +from unittest.mock import patch + +import pytest + +import zeroconf as r +import zeroconf._services.browser as _services_browser +from zeroconf import ( + DNSPointer, + DNSQuestion, + Zeroconf, + _engine, + const, + current_time_millis, + millis_to_seconds, +) +from zeroconf._services import ServiceStateChange +from zeroconf._services.browser import ServiceBrowser, _ScheduledPTRQuery +from zeroconf._services.info import ServiceInfo +from zeroconf.asyncio import AsyncServiceBrowser, AsyncZeroconf + +from .. import ( + QuestionHistoryWithoutSuppression, + _inject_response, + _wait_for_start, + has_working_ipv6, + mock_incoming_msg, + time_changed_millis, +) + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +def test_service_browser_cancel_multiple_times(): + """Test we can cancel a ServiceBrowser multiple times before close.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_hap._tcp.local." + + class MyServiceListener(r.ServiceListener): + pass + + listener = MyServiceListener() + + browser = r.ServiceBrowser(zc, type_, None, listener) + + browser.cancel() + browser.cancel() + browser.cancel() + + zc.close() + + +def test_service_browser_cancel_context_manager(): + """Test we can cancel a ServiceBrowser with it being used as a context manager.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_hap._tcp.local." + + class MyServiceListener(r.ServiceListener): + pass + + listener = MyServiceListener() + + browser = r.ServiceBrowser(zc, type_, None, listener) + + assert cast(bool, browser.done) is False + + with browser: + pass + + # ensure call_soon_threadsafe in ServiceBrowser.cancel is run + assert zc.loop is not None + asyncio.run_coroutine_threadsafe(asyncio.sleep(0), zc.loop).result() + + assert cast(bool, browser.done) is True + + zc.close() + + +def test_service_browser_cancel_multiple_times_after_close(): + """Test we can cancel a ServiceBrowser multiple times after close.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_hap._tcp.local." + + class MyServiceListener(r.ServiceListener): + pass + + listener = MyServiceListener() + + browser = r.ServiceBrowser(zc, type_, None, listener) + + zc.close() + + browser.cancel() + browser.cancel() + browser.cancel() + + +def test_service_browser_started_after_zeroconf_closed(): + """Test starting a ServiceBrowser after close raises RuntimeError.""" + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_hap._tcp.local." + + class MyServiceListener(r.ServiceListener): + pass + + listener = MyServiceListener() + zc.close() + + with pytest.raises(RuntimeError): + r.ServiceBrowser(zc, type_, None, listener) + + +def test_multiple_instances_running_close(): + """Test we can shutdown multiple instances.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + zc2 = Zeroconf(interfaces=["127.0.0.1"]) + zc3 = Zeroconf(interfaces=["127.0.0.1"]) + + assert zc.loop != zc2.loop + assert zc.loop != zc3.loop + + class MyServiceListener(r.ServiceListener): + pass + + listener = MyServiceListener() + + zc2.add_service_listener("zca._hap._tcp.local.", listener) + + zc.close() + zc2.remove_service_listener(listener) + zc2.close() + zc3.close() + + +class TestServiceBrowser(unittest.TestCase): + def test_update_record(self): + enable_ipv6 = has_working_ipv6() and not os.environ.get("SKIP_IPV6") + + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + service_server = "ash-1.local." + service_text = b"path=/~matt1/" + service_address = "10.0.1.2" + service_v6_address = "2001:db8::1" + service_v6_second_address = "6001:db8::1" + + service_added_count = 0 + service_removed_count = 0 + service_updated_count = 0 + service_add_event = Event() + service_removed_event = Event() + service_updated_event = Event() + + class MyServiceListener(r.ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + nonlocal service_added_count + service_added_count += 1 + service_add_event.set() + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + nonlocal service_removed_count + service_removed_count += 1 + service_removed_event.set() + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + nonlocal service_updated_count + service_updated_count += 1 + service_info = zc.get_service_info(type_, name) + assert socket.inet_aton(service_address) in service_info.addresses + if enable_ipv6: + assert socket.inet_pton( + socket.AF_INET6, service_v6_address + ) in service_info.addresses_by_version(r.IPVersion.V6Only) + assert socket.inet_pton( + socket.AF_INET6, service_v6_second_address + ) in service_info.addresses_by_version(r.IPVersion.V6Only) + assert service_info.text == service_text + assert service_info.server.lower() == service_server.lower() + service_updated_event.set() + + def mock_record_update_incoming_msg( + service_state_change: r.ServiceStateChange, + ) -> r.DNSIncoming: + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + assert generated.is_response() is True + + ttl = 0 if service_state_change == r.ServiceStateChange.Removed else 120 + + generated.add_answer_at_time( + r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + service_text, + ), + 0, + ) + + generated.add_answer_at_time( + r.DNSService( + service_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + 0, + 0, + 80, + service_server, + ), + 0, + ) + + # Send the IPv6 address first since we previously + # had a bug where the IPv4 would be missing if the + # IPv6 was seen first + if enable_ipv6: + generated.add_answer_at_time( + r.DNSAddress( + service_server, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + socket.inet_pton(socket.AF_INET6, service_v6_address), + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + service_server, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + socket.inet_pton(socket.AF_INET6, service_v6_second_address), + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + service_server, + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + socket.inet_aton(service_address), + ), + 0, + ) + + generated.add_answer_at_time( + r.DNSPointer(service_type, const._TYPE_PTR, const._CLASS_IN, ttl, service_name), + 0, + ) + + return r.DNSIncoming(generated.packets()[0]) + + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + service_browser = r.ServiceBrowser(zeroconf, service_type, listener=MyServiceListener()) + + try: + wait_time = 3 + + # service added + _inject_response(zeroconf, mock_record_update_incoming_msg(r.ServiceStateChange.Added)) + service_add_event.wait(wait_time) + assert service_added_count == 1 + assert service_updated_count == 0 + assert service_removed_count == 0 + + # service SRV updated + service_updated_event.clear() + service_server = "ash-2.local." + _inject_response(zeroconf, mock_record_update_incoming_msg(r.ServiceStateChange.Updated)) + service_updated_event.wait(wait_time) + assert service_added_count == 1 + assert service_updated_count == 1 + assert service_removed_count == 0 + + # service TXT updated + service_updated_event.clear() + service_text = b"path=/~matt2/" + _inject_response(zeroconf, mock_record_update_incoming_msg(r.ServiceStateChange.Updated)) + service_updated_event.wait(wait_time) + assert service_added_count == 1 + assert service_updated_count == 2 + assert service_removed_count == 0 + + # service TXT updated - duplicate update should not trigger another service_updated + service_updated_event.clear() + service_text = b"path=/~matt2/" + _inject_response(zeroconf, mock_record_update_incoming_msg(r.ServiceStateChange.Updated)) + # Negative assertion: a duplicate update must NOT fire the listener. The wait + # always times out, so keep the budget short rather than reusing wait_time. + service_updated_event.wait(0.3) + assert service_added_count == 1 + assert service_updated_count == 2 + assert service_removed_count == 0 + + # service A updated + service_updated_event.clear() + service_address = "10.0.1.3" + # Verify we match on uppercase + service_server = service_server.upper() + _inject_response(zeroconf, mock_record_update_incoming_msg(r.ServiceStateChange.Updated)) + service_updated_event.wait(wait_time) + assert service_added_count == 1 + assert service_updated_count == 3 + assert service_removed_count == 0 + + # service all updated + service_updated_event.clear() + service_server = "ash-3.local." + service_text = b"path=/~matt3/" + service_address = "10.0.1.3" + _inject_response(zeroconf, mock_record_update_incoming_msg(r.ServiceStateChange.Updated)) + service_updated_event.wait(wait_time) + assert service_added_count == 1 + assert service_updated_count == 4 + assert service_removed_count == 0 + + # service removed + _inject_response(zeroconf, mock_record_update_incoming_msg(r.ServiceStateChange.Removed)) + service_removed_event.wait(wait_time) + assert service_added_count == 1 + assert service_updated_count == 4 + assert service_removed_count == 1 + + finally: + assert len(zeroconf.listeners) == 1 + service_browser.cancel() + time.sleep(0.2) + assert len(zeroconf.listeners) == 0 + zeroconf.remove_all_service_listeners() + zeroconf.close() + + +class TestServiceBrowserMultipleTypes(unittest.TestCase): + def test_update_record(self): + service_names = [ + "name2._type2._tcp.local.", + "name._type._tcp.local.", + "name._type._udp.local", + ] + service_types = ["_type2._tcp.local.", "_type._tcp.local.", "_type._udp.local."] + + service_added_count = 0 + service_removed_count = 0 + service_add_event = Event() + service_removed_event = Event() + + class MyServiceListener(r.ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + nonlocal service_added_count + service_added_count += 1 + if service_added_count == 3: + service_add_event.set() + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + nonlocal service_removed_count + service_removed_count += 1 + if service_removed_count == 3: + service_removed_event.set() + + def mock_record_update_incoming_msg( + service_state_change: r.ServiceStateChange, + service_type: str, + service_name: str, + ttl: int, + ) -> r.DNSIncoming: + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSPointer(service_type, const._TYPE_PTR, const._CLASS_IN, ttl, service_name), + 0, + ) + return r.DNSIncoming(generated.packets()[0]) + + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + service_browser = r.ServiceBrowser(zeroconf, service_types, listener=MyServiceListener()) + + try: + wait_time = 3 + + # all three services added + _inject_response( + zeroconf, + mock_record_update_incoming_msg( + r.ServiceStateChange.Added, service_types[0], service_names[0], 120 + ), + ) + _inject_response( + zeroconf, + mock_record_update_incoming_msg( + r.ServiceStateChange.Added, service_types[1], service_names[1], 120 + ), + ) + time.sleep(0.1) + + called_with_refresh_time_check = False + + def _mock_get_expiration_time(self, percent): + nonlocal called_with_refresh_time_check + if percent == const._EXPIRE_REFRESH_TIME_PERCENT: + called_with_refresh_time_check = True + return 0 + return self.created + (percent * self.ttl * 10) + + # Set an expire time that will force a refresh + with patch("zeroconf.DNSRecord.get_expiration_time", new=_mock_get_expiration_time): + _inject_response( + zeroconf, + mock_record_update_incoming_msg( + r.ServiceStateChange.Added, + service_types[0], + service_names[0], + 120, + ), + ) + # Add the last record after updating the first one + # to ensure the service_add_event only gets set + # after the update + _inject_response( + zeroconf, + mock_record_update_incoming_msg( + r.ServiceStateChange.Added, + service_types[2], + service_names[2], + 120, + ), + ) + service_add_event.wait(wait_time) + assert called_with_refresh_time_check is True + assert service_added_count == 3 + assert service_removed_count == 0 + + _inject_response( + zeroconf, + mock_record_update_incoming_msg( + r.ServiceStateChange.Updated, service_types[0], service_names[0], 0 + ), + ) + + # all three services removed + _inject_response( + zeroconf, + mock_record_update_incoming_msg( + r.ServiceStateChange.Removed, service_types[0], service_names[0], 0 + ), + ) + _inject_response( + zeroconf, + mock_record_update_incoming_msg( + r.ServiceStateChange.Removed, service_types[1], service_names[1], 0 + ), + ) + _inject_response( + zeroconf, + mock_record_update_incoming_msg( + r.ServiceStateChange.Removed, service_types[2], service_names[2], 0 + ), + ) + service_removed_event.wait(wait_time) + assert service_added_count == 3 + assert service_removed_count == 3 + except TypeError: + # Cannot be patched with cython as get_expiration_time is immutable + pass + + finally: + assert len(zeroconf.listeners) == 1 + service_browser.cancel() + time.sleep(0.2) + assert len(zeroconf.listeners) == 0 + zeroconf.remove_all_service_listeners() + zeroconf.close() + + +def test_first_query_delay(): + """Verify the first query is delayed. + + https://datatracker.ietf.org/doc/html/rfc6762#section-5.2 + """ + type_ = "_http._tcp.local." + zeroconf_browser = Zeroconf(interfaces=["127.0.0.1"]) + _wait_for_start(zeroconf_browser) + + # we are going to patch the zeroconf send to check query transmission + old_send = zeroconf_browser.async_send + + first_query_time = None + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT): + """Sends an outgoing packet.""" + nonlocal first_query_time + if first_query_time is None: + first_query_time = current_time_millis() + old_send(out, addr=addr, port=port) + + # patch the zeroconf send + with patch.object(zeroconf_browser, "async_send", send): + # dummy service callback + def on_service_state_change(zeroconf, service_type, state_change, name): + pass + + start_time = current_time_millis() + browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) + time.sleep(millis_to_seconds(_services_browser._FIRST_QUERY_DELAY_RANDOM_INTERVAL[1] + 5)) + try: + assert ( + current_time_millis() - start_time > _services_browser._FIRST_QUERY_DELAY_RANDOM_INTERVAL[0] + ) + finally: + browser.cancel() + zeroconf_browser.close() + + +@pytest.mark.asyncio +async def test_asking_default_is_asking_qm_questions_after_the_first_qu(quick_timing: None) -> None: + """Verify the service browser's first questions are QU and refresh queries are QM.""" + service_added = asyncio.Event() + service_removed = asyncio.Event() + unexpected_ttl = asyncio.Event() + got_query = asyncio.Event() + + type_ = "_http._tcp.local." + registration_name = f"xxxyyy.{type_}" + + def on_service_state_change(zeroconf, service_type, state_change, name): + if name == registration_name: + if state_change is ServiceStateChange.Added: + service_added.set() + elif state_change is ServiceStateChange.Removed: + service_removed.set() + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_browser = aiozc.zeroconf + zeroconf_browser.question_history = QuestionHistoryWithoutSuppression() + await zeroconf_browser.async_wait_for_start() + + # we are going to patch the zeroconf send to check packet sizes + old_send = zeroconf_browser.async_send + + expected_ttl = const._DNS_OTHER_TTL + questions: list[list[DNSQuestion]] = [] + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + pout = r.DNSIncoming(out.packets()[0]) + questions.append(pout.questions) + got_query.set() + old_send(out, addr=addr, port=port, v6_flow_scope=v6_flow_scope) + + assert len(zeroconf_browser.engine.protocols) == 2 + + aio_zeroconf_registrar = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_registrar = aio_zeroconf_registrar.zeroconf + await aio_zeroconf_registrar.zeroconf.async_wait_for_start() + + assert len(zeroconf_registrar.engine.protocols) == 2 + # patch the zeroconf send so we can capture what is being sent + with patch.object(zeroconf_browser, "async_send", send): + service_added = asyncio.Event() + service_removed = asyncio.Event() + + browser = AsyncServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aio_zeroconf_registrar.async_register_service(info) + await task + loop = asyncio.get_running_loop() + try: + await asyncio.wait_for(service_added.wait(), 1) + assert service_added.is_set() + # Make sure the startup queries are sent + original_now = loop.time() + now_millis = original_now * 1000 + for query_count in range(_services_browser.STARTUP_QUERIES): + now_millis += (2**query_count) * 1000 + time_changed_millis(now_millis) + + got_query.clear() + now_millis = original_now * 1000 + assert not unexpected_ttl.is_set() + # Move time forward past when the TTL is no longer + # fresh (AKA 75% of the TTL) + now_millis += (expected_ttl * 1000) * 0.80 + time_changed_millis(now_millis) + + await asyncio.wait_for(got_query.wait(), 1) + assert not unexpected_ttl.is_set() + + assert len(questions) == _services_browser.STARTUP_QUERIES + 1 + # The first question should be QU to try to + # populate the known answers and limit the impact + # of the QM questions that follow. We still + # have to ask QM questions for the startup queries + # because some devices will not respond to QU + assert questions[0][0].unicast is True + # The remaining questions should be QM questions + for question in questions[1:]: + assert question[0].unicast is False + # Don't remove service, allow close() to cleanup + finally: + await aio_zeroconf_registrar.async_close() + await asyncio.wait_for(service_removed.wait(), 1) + assert service_removed.is_set() + await browser.async_cancel() + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_ttl_refresh_cancelled_rescue_query(quick_timing: None) -> None: + """Verify seeing a name again cancels the rescue query.""" + service_added = asyncio.Event() + service_removed = asyncio.Event() + unexpected_ttl = asyncio.Event() + got_query = asyncio.Event() + + type_ = "_http._tcp.local." + registration_name = f"xxxyyy.{type_}" + + def on_service_state_change(zeroconf, service_type, state_change, name): + if name == registration_name: + if state_change is ServiceStateChange.Added: + service_added.set() + elif state_change is ServiceStateChange.Removed: + service_removed.set() + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_browser = aiozc.zeroconf + zeroconf_browser.question_history = QuestionHistoryWithoutSuppression() + await zeroconf_browser.async_wait_for_start() + + # we are going to patch the zeroconf send to check packet sizes + old_send = zeroconf_browser.async_send + + expected_ttl = const._DNS_OTHER_TTL + packets = [] + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + pout = r.DNSIncoming(out.packets()[0]) + packets.append(pout) + got_query.set() + old_send(out, addr=addr, port=port, v6_flow_scope=v6_flow_scope) + + assert len(zeroconf_browser.engine.protocols) == 2 + + aio_zeroconf_registrar = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_registrar = aio_zeroconf_registrar.zeroconf + await aio_zeroconf_registrar.zeroconf.async_wait_for_start() + + assert len(zeroconf_registrar.engine.protocols) == 2 + # patch the zeroconf send so we can capture what is being sent + with patch.object(zeroconf_browser, "async_send", send): + service_added = asyncio.Event() + service_removed = asyncio.Event() + + browser = AsyncServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aio_zeroconf_registrar.async_register_service(info) + await task + loop = asyncio.get_running_loop() + try: + await asyncio.wait_for(service_added.wait(), 1) + assert service_added.is_set() + # Make sure the startup queries are sent + original_now = loop.time() + now_millis = original_now * 1000 + for query_count in range(_services_browser.STARTUP_QUERIES): + now_millis += (2**query_count) * 1000 + time_changed_millis(now_millis) + + now_millis = original_now * 1000 + assert not unexpected_ttl.is_set() + await asyncio.wait_for(got_query.wait(), 1) + got_query.clear() + assert len(packets) == _services_browser.STARTUP_QUERIES + packets.clear() + + # Move time forward past when the TTL is no longer + # fresh (AKA 75% of the TTL) + now_millis += (expected_ttl * 1000) * 0.80 + # Inject a response that will reschedule + # the rescue query so it does not happen + with patch("time.monotonic", return_value=now_millis / 1000): + zeroconf_browser.record_manager.async_updates_from_response( + mock_incoming_msg([info.dns_pointer()]), + ) + + time_changed_millis(now_millis) + await asyncio.sleep(0) + + # Verify we did not send a rescue query + assert not packets + + # We should still get a rescue query once the rescheduled + # query time is reached + now_millis += (expected_ttl * 1000) * 0.76 + time_changed_millis(now_millis) + await asyncio.wait_for(got_query.wait(), 1) + assert len(packets) == 1 + # Don't remove service, allow close() to cleanup + finally: + await aio_zeroconf_registrar.async_close() + await asyncio.wait_for(service_removed.wait(), 1) + assert service_removed.is_set() + await browser.async_cancel() + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_asking_qm_questions(): + """Verify explicitly asking QM questions.""" + type_ = "_quservice._tcp.local." + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_browser = aiozc.zeroconf + await zeroconf_browser.async_wait_for_start() + # we are going to patch the zeroconf send to check query transmission + old_send = zeroconf_browser.async_send + + first_outgoing = None + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT): + """Sends an outgoing packet.""" + nonlocal first_outgoing + if first_outgoing is None: + first_outgoing = out + old_send(out, addr=addr, port=port) + + # patch the zeroconf send + with patch.object(zeroconf_browser, "async_send", send): + # dummy service callback + def on_service_state_change(zeroconf, service_type, state_change, name): + pass + + browser = AsyncServiceBrowser( + zeroconf_browser, + type_, + [on_service_state_change], + question_type=r.DNSQuestionType.QM, + ) + await asyncio.sleep(millis_to_seconds(_services_browser._FIRST_QUERY_DELAY_RANDOM_INTERVAL[1] + 5)) + try: + assert first_outgoing.questions[0].unicast is False # type: ignore[union-attr] + finally: + await browser.async_cancel() + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_asking_qu_questions(): + """Verify the service browser can ask QU questions.""" + type_ = "_quservice._tcp.local." + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_browser = aiozc.zeroconf + await zeroconf_browser.async_wait_for_start() + + # we are going to patch the zeroconf send to check query transmission + old_send = zeroconf_browser.async_send + + first_outgoing = None + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT): + """Sends an outgoing packet.""" + nonlocal first_outgoing + if first_outgoing is None: + first_outgoing = out + old_send(out, addr=addr, port=port) + + # patch the zeroconf send + with patch.object(zeroconf_browser, "async_send", send): + # dummy service callback + def on_service_state_change(zeroconf, service_type, state_change, name): + pass + + browser = AsyncServiceBrowser( + zeroconf_browser, + type_, + [on_service_state_change], + question_type=r.DNSQuestionType.QU, + ) + await asyncio.sleep(millis_to_seconds(_services_browser._FIRST_QUERY_DELAY_RANDOM_INTERVAL[1] + 5)) + try: + assert first_outgoing.questions[0].unicast is True # type: ignore[union-attr] + finally: + await browser.async_cancel() + await aiozc.async_close() + + +def test_legacy_record_update_listener(quick_timing: None) -> None: + """Test a RecordUpdateListener that does not implement update_records.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + with pytest.raises(RuntimeError): + r.RecordUpdateListener().update_record( + zc, + 0, + r.DNSRecord("irrelevant", const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL), + ) + + updates = [] + + class LegacyRecordUpdateListener(r.RecordUpdateListener): + """A RecordUpdateListener that does not implement update_records.""" + + def update_record(self, zc: Zeroconf, now: float, record: r.DNSRecord) -> None: + updates.append(record) + + listener = LegacyRecordUpdateListener() + + zc.add_listener(listener, None) + + # dummy service callback + def on_service_state_change(zeroconf, service_type, state_change, name): + pass + + # start a browser + type_ = "_homeassistant._tcp.local." + name = "MyTestHome" + browser = ServiceBrowser(zc, type_, [on_service_state_change]) + + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + zc.register_service(info_service) + + time.sleep(0.001) + + browser.cancel() + + assert updates + assert len([isinstance(update, r.DNSPointer) and update.name == type_ for update in updates]) >= 1 + + zc.remove_listener(listener) + # Removing a second time should not throw + zc.remove_listener(listener) + + zc.close() + + +def test_service_browser_is_aware_of_port_changes(): + """Test that the ServiceBrowser is aware of port changes.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_hap._tcp.local." + registration_name = f"xxxyyy.{type_}" + + callbacks = [] + + # dummy service callback + def on_service_state_change(zeroconf, service_type, state_change, name): + """Dummy callback.""" + if name == registration_name: + callbacks.append((service_type, state_change, name)) + + browser = ServiceBrowser(zc, type_, [on_service_state_change]) + + desc = {"path": "/~paulsm/"} + address_parsed = "10.0.1.2" + address = socket.inet_aton(address_parsed) + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address]) + + _inject_response( + zc, + mock_incoming_msg( + [ + info.dns_pointer(), + info.dns_service(), + info.dns_text(), + *info.dns_addresses(), + ] + ), + ) + time.sleep(0.1) + + assert callbacks == [("_hap._tcp.local.", ServiceStateChange.Added, "xxxyyy._hap._tcp.local.")] + service_info = zc.get_service_info(type_, registration_name) + assert service_info is not None + assert service_info.port == 80 + + info.port = 400 + info._dns_service_cache = None # we are mutating the record so clear the cache + + _inject_response( + zc, + mock_incoming_msg([info.dns_service()]), + ) + time.sleep(0.1) + + assert callbacks == [ + ("_hap._tcp.local.", ServiceStateChange.Added, "xxxyyy._hap._tcp.local."), + ("_hap._tcp.local.", ServiceStateChange.Updated, "xxxyyy._hap._tcp.local."), + ] + service_info = zc.get_service_info(type_, registration_name) + assert service_info is not None + assert service_info.port == 400 + browser.cancel() + + zc.close() + + +def test_service_browser_listeners_update_service(): + """Test that the ServiceBrowser ServiceListener that implements update_service.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_hap._tcp.local." + registration_name = f"xxxyyy.{type_}" + callbacks = [] + + class MyServiceListener(r.ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("add", type_, name)) + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("remove", type_, name)) + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("update", type_, name)) + + listener = MyServiceListener() + + browser = r.ServiceBrowser(zc, type_, None, listener) + + desc = {"path": "/~paulsm/"} + address_parsed = "10.0.1.2" + address = socket.inet_aton(address_parsed) + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address]) + + _inject_response( + zc, + mock_incoming_msg( + [ + info.dns_pointer(), + info.dns_service(), + info.dns_text(), + *info.dns_addresses(), + ] + ), + ) + time.sleep(0.2) + info._dns_service_cache = None # we are mutating the record so clear the cache + + info.port = 400 + _inject_response( + zc, + mock_incoming_msg([info.dns_service()]), + ) + time.sleep(0.2) + + assert callbacks == [ + ("add", type_, registration_name), + ("update", type_, registration_name), + ] + browser.cancel() + + zc.close() + + +def test_service_browser_listeners_no_update_service(): + """A listener that ignores update events records only add/remove callbacks.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_hap._tcp.local." + registration_name = f"xxxyyy.{type_}" + callbacks = [] + + class MyServiceListener(r.ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("add", type_, name)) + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("remove", type_, name)) + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + pass + + listener = MyServiceListener() + + browser = r.ServiceBrowser(zc, type_, None, listener) + + desc = {"path": "/~paulsm/"} + address_parsed = "10.0.1.2" + address = socket.inet_aton(address_parsed) + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address]) + + _inject_response( + zc, + mock_incoming_msg( + [ + info.dns_pointer(), + info.dns_service(), + info.dns_text(), + *info.dns_addresses(), + ] + ), + ) + time.sleep(0.2) + info.port = 400 + info._dns_service_cache = None # we are mutating the record so clear the cache + + _inject_response( + zc, + mock_incoming_msg([info.dns_service()]), + ) + time.sleep(0.2) + + assert callbacks == [ + ("add", type_, registration_name), + ] + browser.cancel() + + zc.close() + + +def test_service_browser_nsec_record_does_not_trigger_update(): + """NSEC records assert non-existence and must not fire ServiceStateChange.Updated.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_hap._tcp.local." + registration_name = f"xxxyyy.{type_}" + callbacks: list[tuple[str, str, str]] = [] + service_added = Event() + + class MyServiceListener(r.ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("add", type_, name)) + service_added.set() + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("remove", type_, name)) + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("update", type_, name)) + + listener = MyServiceListener() + browser = r.ServiceBrowser(zc, type_, None, listener) + try: + desc = {"path": "/~paulsm/"} + address = socket.inet_aton("10.0.1.2") + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address]) + + _inject_response( + zc, + mock_incoming_msg( + [ + info.dns_pointer(), + info.dns_service(), + info.dns_text(), + *info.dns_addresses(), + ] + ), + ) + assert service_added.wait(timeout=5), "add_service callback never fired" + + # NSEC inject runs synchronously through the event loop; once + # _inject_response returns, async_update_records has already + # decided not to enqueue a callback for the NSEC record. + _inject_response( + zc, + mock_incoming_msg( + [ + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_AAAA], + ), + ] + ), + ) + + assert callbacks == [("add", type_, registration_name)] + finally: + browser.cancel() + zc.close() + + +def test_service_browser_uses_non_strict_names(): + """Verify we can look for technically invalid names as we cannot change what others do.""" + + # dummy service callback + def on_service_state_change(zeroconf, service_type, state_change, name): + pass + + zc = r.Zeroconf(interfaces=["127.0.0.1"]) + browser = ServiceBrowser(zc, ["_tivo-videostream._tcp.local."], [on_service_state_change]) + browser.cancel() + + # Still fail on completely invalid + with pytest.raises(r.BadTypeInNameException): + browser = ServiceBrowser(zc, ["tivo-videostream._tcp.local."], [on_service_state_change]) + zc.close() + + +def test_group_ptr_queries_with_known_answers(): + questions_with_known_answers: _services_browser._QuestionWithKnownAnswers = {} + now = current_time_millis() + for i in range(120): + name = f"_hap{i}._tcp._local." + questions_with_known_answers[DNSQuestion(name, const._TYPE_PTR, const._CLASS_IN)] = { + DNSPointer( + name, + const._TYPE_PTR, + const._CLASS_IN, + 4500, + f"zoo{counter}.{name}", + ) + for counter in range(i) + } + outs = _services_browser.group_ptr_queries_with_known_answers(now, True, questions_with_known_answers) + for out in outs: + packets = out.packets() + # If we generate multiple packets there must + # only be one question + assert len(packets) == 1 or len(out.questions) == 1 + + +# This test uses asyncio because it needs to access the cache directly +# which is not threadsafe +@pytest.mark.asyncio +async def test_generate_service_query_suppress_duplicate_questions(): + """Generate a service query for sending with zeroconf.send.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + now = current_time_millis() + name = "_suppresstest._tcp.local." + question = r.DNSQuestion(name, const._TYPE_PTR, const._CLASS_IN) + answer = r.DNSPointer( + name, + const._TYPE_PTR, + const._CLASS_IN, + 10000, + f"known-to-other.{name}", + ) + other_known_answers: set[r.DNSRecord] = {answer} + zc.question_history.add_question_at_time(question, now, other_known_answers) + assert zc.question_history.suppresses(question, now, other_known_answers) + + # The known answer list is different, do not suppress + outs = _services_browser.generate_service_query(zc, now, {name}, multicast=True, question_type=None) + assert outs + + zc.cache.async_add_records([answer]) + # The known answer list contains all the asked questions in the history + # we should suppress + + outs = _services_browser.generate_service_query(zc, now, {name}, multicast=True, question_type=None) + assert not outs + + # We do not suppress once the question history expires + outs = _services_browser.generate_service_query( + zc, now + 1000, {name}, multicast=True, question_type=None + ) + assert outs + + # We do not suppress QU queries ever + outs = _services_browser.generate_service_query(zc, now, {name}, multicast=False, question_type=None) + assert outs + + zc.question_history.async_expire(now + 2000) + # No suppression after clearing the history + outs = _services_browser.generate_service_query(zc, now, {name}, multicast=True, question_type=None) + assert outs + + # The previous query we just sent is still remembered and + # the next one is suppressed + outs = _services_browser.generate_service_query(zc, now, {name}, multicast=True, question_type=None) + assert not outs + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_query_scheduler(): + delay = const._BROWSER_TIME + types_ = {"_hap._tcp.local.", "_http._tcp.local."} + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + zc = aiozc.zeroconf + sends: list[r.DNSIncoming] = [] + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + pout = r.DNSIncoming(out.packets()[0]) + sends.append(pout) + + query_scheduler = _services_browser.QueryScheduler(zc, types_, None, 0, True, delay, (0, 0), None) + loop = asyncio.get_running_loop() + + # patch the zeroconf send so we can capture what is being sent + with patch.object(zc, "async_send", send): + query_scheduler.start(loop) + + original_now = loop.time() + now_millis = original_now * 1000 + for query_count in range(_services_browser.STARTUP_QUERIES): + now_millis += (2**query_count) * 1000 + time_changed_millis(now_millis) + + ptr_record = r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "zoomer._hap._tcp.local.", + ) + ptr2_record = r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "disappear._hap._tcp.local.", + ) + + query_scheduler.reschedule_ptr_first_refresh(ptr_record) + expected_when_time = ptr_record.get_expiration_time(const._EXPIRE_REFRESH_TIME_PERCENT) + expected_expire_time = ptr_record.get_expiration_time(100) + ptr_query = _ScheduledPTRQuery( + ptr_record.alias, + ptr_record.name, + int(ptr_record.ttl), + expected_expire_time, + expected_when_time, + ) + assert query_scheduler._query_heap == [ptr_query] + + query_scheduler.reschedule_ptr_first_refresh(ptr2_record) + expected_when_time = ptr2_record.get_expiration_time(const._EXPIRE_REFRESH_TIME_PERCENT) + expected_expire_time = ptr2_record.get_expiration_time(100) + ptr2_query = _ScheduledPTRQuery( + ptr2_record.alias, + ptr2_record.name, + int(ptr2_record.ttl), + expected_expire_time, + expected_when_time, + ) + + assert query_scheduler._query_heap == [ptr_query, ptr2_query] + + # Simulate PTR one goodbye + + query_scheduler.cancel_ptr_refresh(ptr_record) + ptr_query.cancelled = True + + assert query_scheduler._query_heap == [ptr_query, ptr2_query] + assert query_scheduler._query_heap[0].cancelled is True + assert query_scheduler._query_heap[1].cancelled is False + + # Move time forward past when the TTL is no longer + # fresh (AKA 75% of the TTL) + now_millis += (ptr2_record.ttl * 1000) * 0.80 + time_changed_millis(now_millis) + assert len(query_scheduler._query_heap) == 1 + first_heap = query_scheduler._query_heap[0] + assert first_heap.cancelled is False + assert first_heap.alias == ptr2_record.alias + + # Move time forward past when the record expires + now_millis += (ptr2_record.ttl * 1000) * 0.20 + time_changed_millis(now_millis) + assert len(query_scheduler._query_heap) == 0 + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_query_scheduler_rescue_records(): + delay = const._BROWSER_TIME + types_ = {"_hap._tcp.local.", "_http._tcp.local."} + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + zc = aiozc.zeroconf + sends: list[r.DNSIncoming] = [] + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + pout = r.DNSIncoming(out.packets()[0]) + sends.append(pout) + + query_scheduler = _services_browser.QueryScheduler(zc, types_, None, 0, True, delay, (0, 0), None) + loop = asyncio.get_running_loop() + + # patch the zeroconf send so we can capture what is being sent + with patch.object(zc, "async_send", send): + query_scheduler.start(loop) + + original_now = loop.time() + now_millis = original_now * 1000 + for query_count in range(_services_browser.STARTUP_QUERIES): + now_millis += (2**query_count) * 1000 + time_changed_millis(now_millis) + + ptr_record = r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "zoomer._hap._tcp.local.", + ) + + query_scheduler.reschedule_ptr_first_refresh(ptr_record) + expected_when_time = ptr_record.get_expiration_time(const._EXPIRE_REFRESH_TIME_PERCENT) + expected_expire_time = ptr_record.get_expiration_time(100) + ptr_query = _ScheduledPTRQuery( + ptr_record.alias, + ptr_record.name, + int(ptr_record.ttl), + expected_expire_time, + expected_when_time, + ) + assert query_scheduler._query_heap == [ptr_query] + assert query_scheduler._query_heap[0].cancelled is False + + # Move time forward past when the TTL is no longer + # fresh (AKA 75% of the TTL) + now_millis += (ptr_record.ttl * 1000) * 0.76 + time_changed_millis(now_millis) + assert len(query_scheduler._query_heap) == 1 + new_when = query_scheduler._query_heap[0].when_millis + assert query_scheduler._query_heap[0].cancelled is False + assert new_when >= expected_when_time + + # Move time forward again, but not enough to expire the + # record to make sure we try to rescue it + now_millis += (ptr_record.ttl * 1000) * 0.11 + time_changed_millis(now_millis) + assert len(query_scheduler._query_heap) == 1 + second_new_when = query_scheduler._query_heap[0].when_millis + assert query_scheduler._query_heap[0].cancelled is False + assert second_new_when >= new_when + + # Move time forward again, enough that we will no longer + # try to rescue the record + now_millis += (ptr_record.ttl * 1000) * 0.11 + time_changed_millis(now_millis) + assert len(query_scheduler._query_heap) == 0 + + await aiozc.async_close() + + +def test_service_browser_matching(): + """Test that the ServiceBrowser matching does not match partial names.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_http._tcp.local." + registration_name = f"xxxyyy.{type_}" + not_match_type_ = "_asustor-looksgood_http._tcp.local." + not_match_registration_name = f"xxxyyy.{not_match_type_}" + callbacks = [] + + class MyServiceListener(r.ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("add", type_, name)) + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("remove", type_, name)) + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("update", type_, name)) + + listener = MyServiceListener() + + browser = r.ServiceBrowser(zc, type_, None, listener) + + desc = {"path": "/~paulsm/"} + address_parsed = "10.0.1.2" + address = socket.inet_aton(address_parsed) + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address]) + should_not_match = ServiceInfo( + not_match_type_, + not_match_registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[address], + ) + + _inject_response( + zc, + mock_incoming_msg( + [ + info.dns_pointer(), + info.dns_service(), + info.dns_text(), + *info.dns_addresses(), + ] + ), + ) + _inject_response( + zc, + mock_incoming_msg( + [ + should_not_match.dns_pointer(), + should_not_match.dns_service(), + should_not_match.dns_text(), + *should_not_match.dns_addresses(), + ] + ), + ) + time.sleep(0.2) + info.port = 400 + info._dns_service_cache = None # we are mutating the record so clear the cache + + _inject_response( + zc, + mock_incoming_msg([info.dns_service()]), + ) + should_not_match.port = 400 + _inject_response( + zc, + mock_incoming_msg([should_not_match.dns_service()]), + ) + time.sleep(0.2) + + assert callbacks == [ + ("add", type_, registration_name), + ("update", type_, registration_name), + ] + browser.cancel() + + zc.close() + + +@patch.object(_engine, "_CACHE_CLEANUP_INTERVAL", 0.01) +def test_service_browser_expire_callbacks(): + """Test that the ServiceBrowser matching does not match partial names.""" + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + # start a browser + type_ = "_old._tcp.local." + registration_name = f"uniquezip323.{type_}" + callbacks = [] + + class MyServiceListener(r.ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("add", type_, name)) + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("remove", type_, name)) + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("update", type_, name)) + + listener = MyServiceListener() + + browser = r.ServiceBrowser(zc, type_, None, listener) + + desc = {"path": "/~paul2/"} + address_parsed = "10.0.1.3" + address = socket.inet_aton(address_parsed) + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "newname-2.local.", + host_ttl=1, + other_ttl=1, + addresses=[address], + ) + + _inject_response( + zc, + mock_incoming_msg( + [ + info.dns_pointer(), + info.dns_service(), + info.dns_text(), + *info.dns_addresses(), + ] + ), + ) + # Force the ttl to be 1 second + now = current_time_millis() + for cache_record in list(zc.cache.cache.values()): + for record in cache_record.values(): + zc.cache._async_set_created_ttl(record, now, 1) + + # Wait for the add callback to fire from the original inject_response. + for _ in range(30): + time.sleep(0.01) + if len(callbacks) == 1: + break + + info.port = 400 + info._dns_service_cache = None # we are mutating the record so clear the cache + + _inject_response( + zc, + mock_incoming_msg([info.dns_service()]), + ) + + for _ in range(30): + time.sleep(0.01) + if len(callbacks) == 2: + break + + assert callbacks == [ + ("add", type_, registration_name), + ("update", type_, registration_name), + ] + + # Re-add every cached record with `created` in the past so the + # next reaper tick (0.01s) expires them and fires the remove + # callback, instead of waiting the full TTL in real time. + # Going through `_async_set_created_ttl` updates the expiration + # heap; mutating `record.created` directly would leave the heap + # entry pointing at the original `when` so the reaper never wakes. + past = current_time_millis() - 2000 + for cache_record in list(zc.cache.cache.values()): + for record in list(cache_record.values()): + zc.cache._async_set_created_ttl(record, past, 1) + + for _ in range(30): + time.sleep(0.01) + if len(callbacks) == 3: + break + + assert callbacks == [ + ("add", type_, registration_name), + ("update", type_, registration_name), + ("remove", type_, registration_name), + ] + browser.cancel() + + zc.close() + + +def test_scheduled_ptr_query_dunder_methods(): + query75 = _ScheduledPTRQuery("zoomy._hap._tcp.local.", "_hap._tcp.local.", 120, 120, 75) + query80 = _ScheduledPTRQuery("zoomy._hap._tcp.local.", "_hap._tcp.local.", 120, 120, 80) + query75_2 = _ScheduledPTRQuery("zoomy._hap._tcp.local.", "_hap._tcp.local.", 120, 140, 75) + other = object() + stringified = str(query75) + assert "zoomy._hap._tcp.local." in stringified + assert "120" in stringified + assert "75" in stringified + assert "ScheduledPTRQuery" in stringified + + assert query75 == query75 + assert query75 != query80 + assert query75 == query75_2 + assert query75 < query80 + assert query75 <= query80 + assert query80 > query75 + assert query80 >= query75 + + assert query75 != other + with pytest.raises(TypeError): + assert query75 < other # type: ignore[operator] + with pytest.raises(TypeError): + assert query75 <= other # type: ignore[operator] + with pytest.raises(TypeError): + assert query75 > other # type: ignore[operator] + with pytest.raises(TypeError): + assert query75 >= other # type: ignore[operator] + + +@pytest.mark.asyncio +async def test_close_zeroconf_without_browser_before_start_up_queries(quick_timing: None) -> None: + """Test that we stop sending startup queries if zeroconf is closed out from under the browser.""" + service_added = asyncio.Event() + type_ = "_http._tcp.local." + registration_name = f"xxxyyy.{type_}" + + def on_service_state_change(zeroconf, service_type, state_change, name): + if name == registration_name and state_change is ServiceStateChange.Added: + service_added.set() + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_browser = aiozc.zeroconf + zeroconf_browser.question_history = QuestionHistoryWithoutSuppression() + await zeroconf_browser.async_wait_for_start() + + sends: list[r.DNSIncoming] = [] + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + pout = r.DNSIncoming(out.packets()[0]) + sends.append(pout) + + assert len(zeroconf_browser.engine.protocols) == 2 + + aio_zeroconf_registrar = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_registrar = aio_zeroconf_registrar.zeroconf + await aio_zeroconf_registrar.zeroconf.async_wait_for_start() + + assert len(zeroconf_registrar.engine.protocols) == 2 + # patch the zeroconf send so we can capture what is being sent + with patch.object(zeroconf_browser, "async_send", send): + service_added = asyncio.Event() + + browser = AsyncServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aio_zeroconf_registrar.async_register_service(info) + await task + loop = asyncio.get_running_loop() + try: + await asyncio.wait_for(service_added.wait(), 1) + assert service_added.is_set() + await aiozc.async_close() + sends.clear() + # Make sure the startup queries are sent + original_now = loop.time() + now_millis = original_now * 1000 + for query_count in range(_services_browser.STARTUP_QUERIES): + now_millis += (2**query_count) * 1000 + time_changed_millis(now_millis) + + # We should not send any queries after close + assert not sends + finally: + await aio_zeroconf_registrar.async_close() + await browser.async_cancel() + + +@pytest.mark.asyncio +async def test_close_zeroconf_without_browser_after_start_up_queries(quick_timing: None) -> None: + """Test that we stop sending rescue queries if zeroconf is closed out from under the browser.""" + service_added = asyncio.Event() + + type_ = "_http._tcp.local." + registration_name = f"xxxyyy.{type_}" + + def on_service_state_change(zeroconf, service_type, state_change, name): + if name == registration_name and state_change is ServiceStateChange.Added: + service_added.set() + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_browser = aiozc.zeroconf + zeroconf_browser.question_history = QuestionHistoryWithoutSuppression() + await zeroconf_browser.async_wait_for_start() + + sends: list[r.DNSIncoming] = [] + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + pout = r.DNSIncoming(out.packets()[0]) + sends.append(pout) + + assert len(zeroconf_browser.engine.protocols) == 2 + + aio_zeroconf_registrar = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_registrar = aio_zeroconf_registrar.zeroconf + await aio_zeroconf_registrar.zeroconf.async_wait_for_start() + + assert len(zeroconf_registrar.engine.protocols) == 2 + # patch the zeroconf send so we can capture what is being sent + with patch.object(zeroconf_browser, "async_send", send): + service_added = asyncio.Event() + browser = AsyncServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) + expected_ttl = const._DNS_OTHER_TTL + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aio_zeroconf_registrar.async_register_service(info) + await task + loop = asyncio.get_running_loop() + try: + await asyncio.wait_for(service_added.wait(), 1) + assert service_added.is_set() + sends.clear() + # Make sure the startup queries are sent + original_now = loop.time() + now_millis = original_now * 1000 + for query_count in range(_services_browser.STARTUP_QUERIES): + now_millis += (2**query_count) * 1000 + time_changed_millis(now_millis) + + # We should not send any queries after close + assert sends + + await aiozc.async_close() + sends.clear() + + now_millis = original_now * 1000 + # Move time forward past when the TTL is no longer + # fresh (AKA 75% of the TTL) + now_millis += (expected_ttl * 1000) * 0.80 + time_changed_millis(now_millis) + + # We should not send the query after close + assert not sends + finally: + await aio_zeroconf_registrar.async_close() + await browser.async_cancel() diff --git a/tests/services/test_info.py b/tests/services/test_info.py new file mode 100644 index 000000000..219f52262 --- /dev/null +++ b/tests/services/test_info.py @@ -0,0 +1,2082 @@ +"""Unit tests for zeroconf._services.info.""" + +from __future__ import annotations + +import asyncio +import logging +import os +import socket +import threading +import unittest +from ipaddress import ip_address +from threading import Event +from unittest.mock import patch + +import pytest + +import zeroconf as r +from zeroconf import DNSAddress, RecordUpdate, const +from zeroconf._protocol.outgoing import DNSOutgoing +from zeroconf._services import info +from zeroconf._services.info import ServiceInfo, _has_more_scope_info +from zeroconf._utils.ipaddress import ZeroconfIPv4Address +from zeroconf._utils.net import IPVersion +from zeroconf.asyncio import AsyncZeroconf + +from .. import QUICK_REQUEST_TIMEOUT_MS, _inject_response, has_working_ipv6, mock_incoming_msg + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +class TestServiceInfo(unittest.TestCase): + def test_get_name(self): + """Verify the name accessor can strip the type.""" + desc = {"path": "/~paulsm/"} + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + service_server = "ash-1.local." + service_address = socket.inet_aton("10.0.1.2") + info = ServiceInfo( + service_type, + service_name, + 22, + 0, + 0, + desc, + service_server, + addresses=[service_address], + ) + assert info.get_name() == "name" + + def test_service_info_rejects_non_matching_updates(self): + """Verify records with the wrong name are rejected.""" + + zc = r.Zeroconf(interfaces=["127.0.0.1"]) + desc = {"path": "/~paulsm/"} + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + service_server = "ash-1.local." + service_address = socket.inet_aton("10.0.1.2") + ttl = 120 + now = r.current_time_millis() + info = ServiceInfo( + service_type, + service_name, + 22, + 0, + 0, + desc, + service_server, + addresses=[service_address], + ) + # Verify backwards compatibility with calling with None + info.async_update_records(zc, now, []) + # Matching updates + info.async_update_records( + zc, + now, + [ + RecordUpdate( + r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + None, + ) + ], + ) + assert info.properties[b"ci"] == b"2" + info.async_update_records( + zc, + now, + [ + RecordUpdate( + r.DNSService( + service_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + 0, + 0, + 80, + "ASH-2.local.", + ), + None, + ) + ], + ) + assert info.server_key == "ash-2.local." + assert info.server == "ASH-2.local." + new_address = socket.inet_aton("10.0.1.3") + info.async_update_records( + zc, + now, + [ + RecordUpdate( + r.DNSAddress( + "ASH-2.local.", + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + new_address, + ), + None, + ) + ], + ) + assert new_address in info.addresses + # Non-matching updates + info.async_update_records( + zc, + now, + [ + RecordUpdate( + r.DNSText( + "incorrect.name.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + b"\x04ff=0\x04ci=3\x04sf=0\x0bsh=6fLM5A==", + ), + None, + ) + ], + ) + assert info.properties[b"ci"] == b"2" + info.async_update_records( + zc, + now, + [ + RecordUpdate( + r.DNSService( + "incorrect.name.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + 0, + 0, + 80, + "ASH-2.local.", + ), + None, + ) + ], + ) + assert info.server_key == "ash-2.local." + assert info.server == "ASH-2.local." + new_address = socket.inet_aton("10.0.1.4") + info.async_update_records( + zc, + now, + [ + RecordUpdate( + r.DNSAddress( + "incorrect.name.", + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + new_address, + ), + None, + ) + ], + ) + assert new_address not in info.addresses + zc.close() + + def test_service_info_rejects_expired_records(self): + """Verify records that are expired are rejected.""" + zc = r.Zeroconf(interfaces=["127.0.0.1"]) + desc = {"path": "/~paulsm/"} + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + service_server = "ash-1.local." + service_address = socket.inet_aton("10.0.1.2") + ttl = 120 + now = r.current_time_millis() + info = ServiceInfo( + service_type, + service_name, + 22, + 0, + 0, + desc, + service_server, + addresses=[service_address], + ) + # Matching updates + info.async_update_records( + zc, + now, + [ + RecordUpdate( + r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + None, + ) + ], + ) + assert info.properties[b"ci"] == b"2" + # Expired record + expired_record = r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + b"\x04ff=0\x04ci=3\x04sf=0\x0bsh=6fLM5A==", + ) + zc.cache._async_set_created_ttl(expired_record, 1000, 1) + info.async_update_records(zc, now, [RecordUpdate(expired_record, None)]) + assert info.properties[b"ci"] == b"2" + zc.close() + + @unittest.skipIf(not has_working_ipv6(), "Requires IPv6") + @unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") + @pytest.mark.usefixtures("quick_request_timing") + def test_get_info_partial(self): + zc = r.Zeroconf(interfaces=["127.0.0.1"]) + + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + service_server = "ash-1.local." + service_text = b"path=/~matt1/" + service_address = "10.0.1.2" + service_address_v6_ll = "fe80::52e:c2f2:bc5f:e9c6" + service_scope_id = 12 + + service_info = None + send_event = Event() + service_info_event = Event() + + last_sent: r.DNSOutgoing | None = None + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + nonlocal last_sent + + last_sent = out + send_event.set() + + # patch the zeroconf send + with patch.object(zc, "async_send", send): + + def get_service_info_helper(zc, type, name): + nonlocal service_info + service_info = zc.get_service_info(type, name) + service_info_event.set() + + try: + ttl = 120 + helper_thread = threading.Thread( + target=get_service_info_helper, + args=(zc, service_type, service_name), + ) + helper_thread.start() + wait_time = 1 + + # Expect query for SRV, TXT, A, AAAA + send_event.wait(wait_time) + assert last_sent is not None + assert len(last_sent.questions) == 4 + assert r.DNSQuestion(service_name, const._TYPE_SRV, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_TXT, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_A, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_AAAA, const._CLASS_IN) in last_sent.questions + assert service_info is None + + # Expect query for SRV, A, AAAA + last_sent = None + send_event.clear() + _inject_response( + zc, + mock_incoming_msg( + [ + r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + service_text, + ) + ] + ), + ) + send_event.wait(wait_time) + assert last_sent is not None + assert len(last_sent.questions) == 3 # type: ignore[unreachable] + assert r.DNSQuestion(service_name, const._TYPE_SRV, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_A, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_AAAA, const._CLASS_IN) in last_sent.questions + assert service_info is None + + # Expect query for A, AAAA + last_sent = None + send_event.clear() + _inject_response( + zc, + mock_incoming_msg( + [ + r.DNSService( + service_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + 0, + 0, + 80, + service_server, + ) + ] + ), + ) + send_event.wait(wait_time) + assert last_sent is not None + assert len(last_sent.questions) == 2 + assert r.DNSQuestion(service_server, const._TYPE_A, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_server, const._TYPE_AAAA, const._CLASS_IN) in last_sent.questions + last_sent = None + assert service_info is None + + # Expect no further queries + last_sent = None + send_event.clear() + _inject_response( + zc, + mock_incoming_msg( + [ + r.DNSAddress( + service_server, + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + socket.inet_pton(socket.AF_INET, service_address), + ), + r.DNSAddress( + service_server, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + socket.inet_pton(socket.AF_INET6, service_address_v6_ll), + scope_id=service_scope_id, + ), + ] + ), + ) + send_event.wait(wait_time) + assert last_sent is None + assert service_info is not None + + finally: + helper_thread.join() + zc.remove_all_service_listeners() + zc.close() + + @unittest.skipIf(not has_working_ipv6(), "Requires IPv6") + @unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") + def test_get_info_suppressed_by_question_history(self): + zc = r.Zeroconf(interfaces=["127.0.0.1"]) + + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + + service_info = None + send_event = Event() + service_info_event = Event() + + last_sent: r.DNSOutgoing | None = None + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + nonlocal last_sent + + last_sent = out + send_event.set() + + # patch the zeroconf send + with patch.object(zc, "async_send", send): + + def get_service_info_helper(zc, type, name, timeout): + nonlocal service_info + service_info = zc.get_service_info(type, name, timeout) + service_info_event.set() + + try: + # Seed TXT/A/AAAA with a far-future `than` before the + # helper thread starts. The first (QU) query bypasses + # suppression so phase 1 still observes 4 questions; the + # second (QM) query fires ~220-320ms after the first, too + # tight a window to seed reliably from the test thread on + # slow runners. async_expire only removes entries where + # now - than > _DUPLICATE_QUESTION_INTERVAL, so future- + # dated entries persist for the duration of the test. + seed_history_questions = ( + r.DNSQuestion(service_name, const._TYPE_A, const._CLASS_IN), + r.DNSQuestion(service_name, const._TYPE_AAAA, const._CLASS_IN), + r.DNSQuestion(service_name, const._TYPE_TXT, const._CLASS_IN), + ) + far_future = r.current_time_millis() + 60_000 + for question in seed_history_questions: + zc.question_history.add_question_at_time(question, far_future, set()) + + # No answers ever come back (all queries are suppressed), + # so cap the helper at the worst-case sum of the three + # phase waits below plus margin instead of the 3000ms + # default. Phase 3 waits ~1.6s (the 999ms QM gap plus + # jitter and 500ms buffer); 1500ms covers it. + helper_thread = threading.Thread( + target=get_service_info_helper, + args=(zc, service_type, service_name, 1500), + ) + helper_thread.start() + wait_time = (const._LISTENER_TIME + info._AVOID_SYNC_DELAY_RANDOM_INTERVAL[1] + 500) / 1000 + + # Expect query for SRV, TXT, A, AAAA + send_event.wait(wait_time) + assert last_sent is not None + assert len(last_sent.questions) == 4 + assert r.DNSQuestion(service_name, const._TYPE_SRV, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_TXT, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_A, const._CLASS_IN) in last_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_AAAA, const._CLASS_IN) in last_sent.questions + assert service_info is None + + # Expect query for SRV only as A, AAAA, and TXT are suppressed + # by the question history + last_sent = None + send_event.clear() + send_event.wait(wait_time) + assert last_sent is not None + assert len(last_sent.questions) == 1 # type: ignore[unreachable] + assert r.DNSQuestion(service_name, const._TYPE_SRV, const._CLASS_IN) in last_sent.questions + assert service_info is None + + # Future-date SRV too: the SRV entry added by the previous + # QM query has `than = now`, so it expires after + # _DUPLICATE_QUESTION_INTERVAL — before the next scheduled + # QM query (~1s + jitter later). + zc.question_history.add_question_at_time( + r.DNSQuestion(service_name, const._TYPE_SRV, const._CLASS_IN), + r.current_time_millis() + 60_000, + set(), + ) + + wait_time = ( + const._DUPLICATE_QUESTION_INTERVAL + info._AVOID_SYNC_DELAY_RANDOM_INTERVAL[1] + 500 + ) / 1000 + # Expect no queries as all are suppressed by the question history + last_sent = None + send_event.clear() + send_event.wait(wait_time) + # All questions are suppressed so no query should be sent + assert last_sent is None + assert service_info is None + + finally: + helper_thread.join() + zc.remove_all_service_listeners() + zc.close() + + @pytest.mark.usefixtures("quick_request_timing") + def test_get_info_single(self): + zc = r.Zeroconf(interfaces=["127.0.0.1"]) + + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + service_server = "ash-1.local." + service_text = b"path=/~matt1/" + service_address = "10.0.1.2" + + service_info = None + service_info_event = Event() + + ttl = 120 + response_records = [ + r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + service_text, + ), + r.DNSService( + service_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + 0, + 0, + 80, + service_server, + ), + r.DNSAddress( + service_server, + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + socket.inet_pton(socket.AF_INET, service_address), + ), + ] + + sent_queries: list[r.DNSOutgoing] = [] + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Capture each query and, on the first one, fill the cache + inline so the next iteration of `async_request` finds + `_is_complete=True` and exits without sending another query. + + Running the inject from inside `send` keeps it on the event + loop thread and atomic with the first send — eliminating the + test-thread → `run_coroutine_threadsafe` race that flaked + under PyPy + use_cython when `quick_request_timing` shortens + the inter-iteration delay to ~15ms. + """ + sent_queries.append(out) + if len(sent_queries) == 1: + zc.record_manager.async_updates_from_response(mock_incoming_msg(response_records)) + + def get_service_info_helper(zc, type, name): + nonlocal service_info + service_info = zc.get_service_info(type, name) + service_info_event.set() + + # patch the zeroconf send + with patch.object(zc, "async_send", send): + try: + helper_thread = threading.Thread( + target=get_service_info_helper, + args=(zc, service_type, service_name), + ) + helper_thread.start() + + # Helper should complete promptly — the inline inject in + # `send` populates the cache before the request loop's + # next iteration. + service_info_event.wait(1) + assert service_info is not None + + # First (and only) query: QU for SRV/TXT/A/AAAA. + assert len(sent_queries) == 1 + first_sent = sent_queries[0] + assert len(first_sent.questions) == 4 + assert r.DNSQuestion(service_name, const._TYPE_SRV, const._CLASS_IN) in first_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_TXT, const._CLASS_IN) in first_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_A, const._CLASS_IN) in first_sent.questions + assert r.DNSQuestion(service_name, const._TYPE_AAAA, const._CLASS_IN) in first_sent.questions + + finally: + helper_thread.join() + zc.remove_all_service_listeners() + zc.close() + + def test_service_info_duplicate_properties_txt_records(self): + """Verify the first property is always used when there are duplicates in a txt record.""" + + zc = r.Zeroconf(interfaces=["127.0.0.1"]) + desc = {"path": "/~paulsm/"} + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + service_server = "ash-1.local." + service_address = socket.inet_aton("10.0.1.2") + ttl = 120 + now = r.current_time_millis() + info = ServiceInfo( + service_type, + service_name, + 22, + 0, + 0, + desc, + service_server, + addresses=[service_address], + ) + info.async_update_records( + zc, + now, + [ + r.RecordUpdate( + r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==\x04dd=0\x04jl=2\x04qq=0\x0brr=6fLM5A==\x04ci=3", + ), + None, + ) + ], + ) + assert info.properties[b"dd"] == b"0" + assert info.properties[b"jl"] == b"2" + assert info.properties[b"ci"] == b"2" + zc.close() + + +def test_multiple_addresses(): + type_ = "_http._tcp.local." + registration_name = f"xxxyyy.{type_}" + desc = {"path": "/~paulsm/"} + address_parsed = "10.0.1.2" + address = socket.inet_aton(address_parsed) + + # New kwarg way + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[address, address], + ) + + assert info.addresses == [address, address] + assert info.parsed_addresses() == [address_parsed, address_parsed] + assert info.parsed_scoped_addresses() == [address_parsed, address_parsed] + + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + parsed_addresses=[address_parsed, address_parsed], + ) + assert info.addresses == [address, address] + assert info.parsed_addresses() == [address_parsed, address_parsed] + assert info.parsed_scoped_addresses() == [address_parsed, address_parsed] + + if has_working_ipv6() and not os.environ.get("SKIP_IPV6"): + address_v6_parsed = "2001:db8::1" + address_v6 = socket.inet_pton(socket.AF_INET6, address_v6_parsed) + address_v6_ll_parsed = "fe80::52e:c2f2:bc5f:e9c6" + address_v6_ll_scoped_parsed = "fe80::52e:c2f2:bc5f:e9c6%12" + address_v6_ll = socket.inet_pton(socket.AF_INET6, address_v6_ll_parsed) + interface_index = 12 + infos = [ + ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[address, address_v6, address_v6_ll], + interface_index=interface_index, + ), + ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + parsed_addresses=[ + address_parsed, + address_v6_parsed, + address_v6_ll_parsed, + ], + interface_index=interface_index, + ), + ] + for info in infos: + assert info.addresses == [address] + assert info.addresses_by_version(r.IPVersion.All) == [ + address, + address_v6, + address_v6_ll, + ] + assert info.ip_addresses_by_version(r.IPVersion.All) == [ + ip_address(address), + ip_address(address_v6), + ip_address(address_v6_ll_scoped_parsed), + ] + assert info.addresses_by_version(r.IPVersion.V4Only) == [address] + assert info.ip_addresses_by_version(r.IPVersion.V4Only) == [ip_address(address)] + assert info.addresses_by_version(r.IPVersion.V6Only) == [ + address_v6, + address_v6_ll, + ] + assert info.ip_addresses_by_version(r.IPVersion.V6Only) == [ + ip_address(address_v6), + ip_address(address_v6_ll_scoped_parsed), + ] + assert info.parsed_addresses() == [ + address_parsed, + address_v6_parsed, + address_v6_ll_parsed, + ] + assert info.parsed_addresses(r.IPVersion.V4Only) == [address_parsed] + assert info.parsed_addresses(r.IPVersion.V6Only) == [ + address_v6_parsed, + address_v6_ll_parsed, + ] + assert info.parsed_scoped_addresses() == [ + address_parsed, + address_v6_parsed, + address_v6_ll_scoped_parsed, + ] + assert info.parsed_scoped_addresses(r.IPVersion.V4Only) == [address_parsed] + assert info.parsed_scoped_addresses(r.IPVersion.V6Only) == [ + address_v6_parsed, + address_v6_ll_scoped_parsed, + ] + + +def test_scoped_addresses_from_cache(): + type_ = "_http._tcp.local." + registration_name = f"scoped.{type_}" + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + host = "scoped.local." + + zeroconf.cache.async_add_records( + [ + r.DNSPointer( + type_, + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + registration_name, + ), + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + 0, + 0, + 80, + host, + ), + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6"), + scope_id=12, + ), + ] + ) + + # New kwarg way + info = ServiceInfo(type_, registration_name) + info.load_from_cache(zeroconf) + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6%12"] + assert info.ip_addresses_by_version(r.IPVersion.V6Only) == [ip_address("fe80::52e:c2f2:bc5f:e9c6%12")] + zeroconf.close() + + +def test_scoped_address_preferred_when_unscoped_arrives_first_in_cache(): + """A scoped AAAA in the cache wins over an earlier unscoped copy of the same address.""" + type_ = "_http._tcp.local." + registration_name = f"scoped-first.{type_}" + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + host = "scoped-first.local." + packed = socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6") + + zeroconf.cache.async_add_records( + [ + r.DNSPointer( + type_, + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + registration_name, + ), + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + 0, + 0, + 80, + host, + ), + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + packed, + scope_id=None, + ), + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + packed, + scope_id=7, + ), + ] + ) + + info = ServiceInfo(type_, registration_name) + info.load_from_cache(zeroconf) + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6%7"] + assert info.ip_addresses_by_version(r.IPVersion.V6Only) == [ip_address("fe80::52e:c2f2:bc5f:e9c6%7")] + zeroconf.close() + + +@pytest.mark.asyncio +async def test_scoped_address_replaces_unscoped_in_live_update(): + """A late-arriving scoped AAAA replaces a previously-stored unscoped variant.""" + type_ = "_http._tcp.local." + registration_name = f"scoped-live.{type_}" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "scoped-live.local." + packed = socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6") + + info = ServiceInfo(type_, registration_name, server=host) + now = r.current_time_millis() + unscoped = r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + packed, + scope_id=None, + ) + scoped = r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + packed, + scope_id=9, + ) + info.async_update_records(aiozc.zeroconf, now, [RecordUpdate(unscoped, None)]) + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6"] + info.async_update_records(aiozc.zeroconf, now, [RecordUpdate(scoped, unscoped)]) + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6%9"] + await aiozc.async_close() + + +def test_scoped_address_kept_when_unscoped_arrives_after_in_cache(): + """Scoped AAAA seen first in iteration keeps its scope when an unscoped duplicate follows.""" + type_ = "_http._tcp.local." + registration_name = f"scoped-after.{type_}" + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + host = "scoped-after.local." + packed = socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6") + + zeroconf.cache.async_add_records( + [ + r.DNSPointer( + type_, + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + registration_name, + ), + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + 0, + 0, + 80, + host, + ), + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + packed, + scope_id=5, + ), + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + packed, + scope_id=None, + ), + ] + ) + + info = ServiceInfo(type_, registration_name) + info.load_from_cache(zeroconf) + assert info.parsed_scoped_addresses() == ["fe80::52e:c2f2:bc5f:e9c6%5"] + assert info.ip_addresses_by_version(r.IPVersion.V6Only) == [ip_address("fe80::52e:c2f2:bc5f:e9c6%5")] + zeroconf.close() + + +def test_has_more_scope_info_returns_false_for_ipv4(): + """The scope_id helper short-circuits for IPv4 since A records carry no scope.""" + ip4 = ZeroconfIPv4Address("192.0.2.1") + assert _has_more_scope_info(ip4, ip4) is False + + +def test_scope_upgrade_preserves_lifo_recency_order(): + """A scoped AAAA that upgrades an earlier entry becomes the most recent in LIFO order.""" + type_ = "_http._tcp.local." + registration_name = f"reorder.{type_}" + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + host = "reorder.local." + link_local = socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6") + ula = socket.inet_pton(socket.AF_INET6, "fdc8:d776:7cca:46ed::2") + + zeroconf.cache.async_add_records( + [ + r.DNSPointer( + type_, + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + registration_name, + ), + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + 0, + 0, + 80, + host, + ), + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + link_local, + scope_id=None, + ), + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + ula, + scope_id=None, + ), + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 120, + link_local, + scope_id=11, + ), + ] + ) + + info = ServiceInfo(type_, registration_name) + info.load_from_cache(zeroconf) + # The scoped link-local upgrade is the most recent observation, so it + # has to come first in LIFO order, ahead of the earlier unrelated ULA. + assert info.ip_addresses_by_version(r.IPVersion.V6Only) == [ + ip_address("fe80::52e:c2f2:bc5f:e9c6%11"), + ip_address("fdc8:d776:7cca:46ed::2"), + ] + assert info.parsed_scoped_addresses() == [ + "fe80::52e:c2f2:bc5f:e9c6%11", + "fdc8:d776:7cca:46ed::2", + ] + zeroconf.close() + + +# This test uses asyncio because it needs to access the cache directly +# which is not threadsafe +@pytest.mark.asyncio +async def test_multiple_a_addresses_newest_address_first(): + """Test that info.addresses returns the newest seen address first.""" + type_ = "_http._tcp.local." + registration_name = f"multiarec.{type_}" + desc = {"path": "/~paulsm/"} + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + cache = aiozc.zeroconf.cache + host = "multahost.local." + record1 = r.DNSAddress(host, const._TYPE_A, const._CLASS_IN, 1000, b"\x7f\x00\x00\x01") + record2 = r.DNSAddress(host, const._TYPE_A, const._CLASS_IN, 1000, b"\x7f\x00\x00\x02") + cache.async_add_records([record1, record2]) + + # New kwarg way + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, host) + info.load_from_cache(aiozc.zeroconf) + assert info.addresses == [b"\x7f\x00\x00\x02", b"\x7f\x00\x00\x01"] + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_invalid_a_addresses(caplog): + type_ = "_http._tcp.local." + registration_name = f"multiarec.{type_}" + desc = {"path": "/~paulsm/"} + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + cache = aiozc.zeroconf.cache + host = "multahost.local." + record1 = r.DNSAddress(host, const._TYPE_A, const._CLASS_IN, 1000, b"a") + record2 = r.DNSAddress(host, const._TYPE_A, const._CLASS_IN, 1000, b"b") + cache.async_add_records([record1, record2]) + + # New kwarg way + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, host) + info.load_from_cache(aiozc.zeroconf) + assert not info.addresses + assert "Encountered invalid address while processing record" in caplog.text + + await aiozc.async_close() + + +@unittest.skipIf(not has_working_ipv6(), "Requires IPv6") +@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") +def test_filter_address_by_type_from_service_info(): + """Verify dns_addresses can filter by ipversion.""" + desc = {"path": "/~paulsm/"} + type_ = "_homeassistant._tcp.local." + name = "MyTestHome" + registration_name = f"{name}.{type_}" + ipv4 = socket.inet_aton("10.0.1.2") + ipv6 = socket.inet_pton(socket.AF_INET6, "2001:db8::1") + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[ipv4, ipv6]) + + def dns_addresses_to_addresses(dns_address: list[DNSAddress]) -> list[bytes]: + return [address.address for address in dns_address] + + assert dns_addresses_to_addresses(info.dns_addresses()) == [ipv4, ipv6] + assert dns_addresses_to_addresses(info.dns_addresses(version=r.IPVersion.All)) == [ + ipv4, + ipv6, + ] + assert dns_addresses_to_addresses(info.dns_addresses(version=r.IPVersion.V4Only)) == [ipv4] + assert dns_addresses_to_addresses(info.dns_addresses(version=r.IPVersion.V6Only)) == [ipv6] + + +def test_changing_name_updates_serviceinfo_key(): + """Verify a name change will adjust the underlying key value.""" + type_ = "_homeassistant._tcp.local." + name = "MyTestHome" + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + assert info_service.key == "mytesthome._homeassistant._tcp.local." + info_service.name = "YourTestHome._homeassistant._tcp.local." + assert info_service.key == "yourtesthome._homeassistant._tcp.local." + + +def test_serviceinfo_address_updates(): + """Verify adding/removing/setting addresses on ServiceInfo.""" + type_ = "_homeassistant._tcp.local." + name = "MyTestHome" + + # Verify addresses and parsed_addresses are mutually exclusive + with pytest.raises(TypeError): + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + parsed_addresses=["10.0.1.2"], + ) + + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info_service.addresses = [socket.inet_aton("10.0.1.3")] + assert info_service.addresses == [socket.inet_aton("10.0.1.3")] + + +def test_serviceinfo_accepts_bytes_or_string_dict(): + """Verify a bytes or string dict can be passed to ServiceInfo.""" + type_ = "_homeassistant._tcp.local." + name = "MyTestHome" + addresses = [socket.inet_aton("10.0.1.2")] + server_name = "ash-2.local." + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {b"path": b"/~paulsm/"}, + server_name, + addresses=addresses, + ) + assert info_service.dns_text().text == b"\x0epath=/~paulsm/" + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + server_name, + addresses=addresses, + ) + assert info_service.dns_text().text == b"\x0epath=/~paulsm/" + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {b"path": "/~paulsm/"}, + server_name, + addresses=addresses, + ) + assert info_service.dns_text().text == b"\x0epath=/~paulsm/" + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": b"/~paulsm/"}, + server_name, + addresses=addresses, + ) + assert info_service.dns_text().text == b"\x0epath=/~paulsm/" + + +def test_asking_qu_questions(quick_request_timing): + """Verify explicitly asking QU questions.""" + type_ = "_quservice._tcp.local." + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + + # we are going to patch the zeroconf send to check query transmission + old_send = zeroconf.async_send + + first_outgoing = None + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT): + """Sends an outgoing packet.""" + nonlocal first_outgoing + if first_outgoing is None: + first_outgoing = out + old_send(out, addr=addr, port=port) + + # patch the zeroconf send + with patch.object(zeroconf, "async_send", send): + zeroconf.get_service_info( + f"name.{type_}", type_, QUICK_REQUEST_TIMEOUT_MS, question_type=r.DNSQuestionType.QU + ) + assert first_outgoing.questions[0].unicast is True # type: ignore[union-attr] + zeroconf.close() + + +def test_asking_qm_questions(quick_request_timing): + """Verify explicitly asking QM questions.""" + type_ = "_quservice._tcp.local." + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + + # we are going to patch the zeroconf send to check query transmission + old_send = zeroconf.async_send + + first_outgoing = None + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT): + """Sends an outgoing packet.""" + nonlocal first_outgoing + if first_outgoing is None: + first_outgoing = out + old_send(out, addr=addr, port=port) + + # patch the zeroconf send + with patch.object(zeroconf, "async_send", send): + zeroconf.get_service_info( + f"name.{type_}", type_, QUICK_REQUEST_TIMEOUT_MS, question_type=r.DNSQuestionType.QM + ) + assert first_outgoing.questions[0].unicast is False # type: ignore[union-attr] + zeroconf.close() + + +def test_request_timeout(): + """Test that the timeout does not throw an exception and finishes close to the actual timeout.""" + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + start_time = r.current_time_millis() + assert zeroconf.get_service_info("_notfound.local.", "notthere._notfound.local.", timeout=200) is None + end_time = r.current_time_millis() + zeroconf.close() + # 200ms for the timeout passed above + # 1000ms for loaded systems + schedule overhead + assert (end_time - start_time) < 200 + 1000 + + +@pytest.mark.asyncio +async def test_we_try_four_times_with_random_delay(): + """Verify we try four times even with the random delay.""" + type_ = "_typethatisnothere._tcp.local." + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + + # we are going to patch the zeroconf send to check query transmission + request_count = 0 + + def async_send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT): + """Sends an outgoing packet.""" + nonlocal request_count + request_count += 1 + + # patch the zeroconf send + with patch.object(aiozc.zeroconf, "async_send", async_send): + await aiozc.async_get_service_info(f"willnotbefound.{type_}", type_) + + await aiozc.async_close() + + assert request_count == 4 + + +@pytest.mark.asyncio +async def test_release_wait_when_new_recorded_added(): + """Test that async_request returns as soon as new matching records are added to the cache.""" + type_ = "_http._tcp.local." + registration_name = f"multiarec.{type_}" + desc = {"path": "/~paulsm/"} + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahost.local." + + # New kwarg way + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, host) + task = asyncio.create_task(info.async_request(aiozc.zeroconf, timeout=200)) + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_AAAA], + ), + 0, + ) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + await aiozc.zeroconf.async_wait_for_start() + await asyncio.sleep(0) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + assert await asyncio.wait_for(task, timeout=2) + assert info.addresses == [b"\x7f\x00\x00\x01"] + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_port_changes_are_seen(): + """Test that port changes are seen by async_request.""" + type_ = "_http._tcp.local." + registration_name = f"multiarec.{type_}" + desc = {"path": "/~paulsm/"} + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahost.local." + + # New kwarg way + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_AAAA], + ), + 0, + ) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + await aiozc.zeroconf.async_wait_for_start() + await asyncio.sleep(0) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 90, + 90, + 81, + host, + ), + 0, + ) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + info = ServiceInfo(type_, registration_name, 80, 10, 10, desc, host) + await info.async_request(aiozc.zeroconf, timeout=200) + assert info.port == 81 + assert info.priority == 90 + assert info.weight == 90 + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_port_changes_are_seen_with_directed_request(): + """Test that port changes are seen by async_request with a directed request.""" + type_ = "_http._tcp.local." + registration_name = f"multiarec.{type_}" + desc = {"path": "/~paulsm/"} + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahost.local." + + # New kwarg way + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_AAAA], + ), + 0, + ) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + await aiozc.zeroconf.async_wait_for_start() + await asyncio.sleep(0) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 90, + 90, + 81, + host, + ), + 0, + ) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + info = ServiceInfo(type_, registration_name, 80, 10, 10, desc, host) + await info.async_request(aiozc.zeroconf, timeout=200, addr="127.0.0.1", port=5353) + assert info.port == 81 + assert info.priority == 90 + assert info.weight == 90 + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_ipv4_changes_are_seen(): + """Test that ipv4 changes are seen by async_request.""" + type_ = "_http._tcp.local." + registration_name = f"multiaipv4rec.{type_}" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahost.local." + + # New kwarg way + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_AAAA], + ), + 0, + ) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + await aiozc.zeroconf.async_wait_for_start() + await asyncio.sleep(0) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + info = ServiceInfo(type_, registration_name) + info.load_from_cache(aiozc.zeroconf) + assert info.addresses_by_version(IPVersion.V4Only) == [b"\x7f\x00\x00\x01"] + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x02", + ), + 0, + ) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + info = ServiceInfo(type_, registration_name) + info.load_from_cache(aiozc.zeroconf) + assert info.addresses_by_version(IPVersion.V4Only) == [ + b"\x7f\x00\x00\x02", + b"\x7f\x00\x00\x01", + ] + await info.async_request(aiozc.zeroconf, timeout=200) + assert info.addresses_by_version(IPVersion.V4Only) == [ + b"\x7f\x00\x00\x02", + b"\x7f\x00\x00\x01", + ] + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_ipv6_changes_are_seen(): + """Test that ipv6 changes are seen by async_request.""" + type_ = "_http._tcp.local." + registration_name = f"multiaipv6rec.{type_}" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahost.local." + + # New kwarg way + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_A], + ), + 0, + ) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN, + 10000, + b"\xde\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + await aiozc.zeroconf.async_wait_for_start() + await asyncio.sleep(0) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + info = ServiceInfo(type_, registration_name) + info.load_from_cache(aiozc.zeroconf) + assert info.addresses_by_version(IPVersion.V6Only) == [ + b"\xde\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + ] + info.load_from_cache(aiozc.zeroconf) + assert info.addresses_by_version(IPVersion.V6Only) == [ + b"\xde\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + ] + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_AAAA, + const._CLASS_IN, + 10000, + b"\x00\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + ), + 0, + ) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + info = ServiceInfo(type_, registration_name) + info.load_from_cache(aiozc.zeroconf) + assert info.addresses_by_version(IPVersion.V6Only) == [ + b"\x00\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + b"\xde\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + ] + await info.async_request(aiozc.zeroconf, timeout=200) + assert info.addresses_by_version(IPVersion.V6Only) == [ + b"\x00\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + b"\xde\xad\xbe\xef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + ] + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_bad_ip_addresses_ignored_in_cache(): + """Test that bad ip address in the cache are ignored async_request.""" + type_ = "_http._tcp.local." + registration_name = f"multiarec.{type_}" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahost.local." + + # New kwarg way + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + # Manually add a bad record to the cache + aiozc.zeroconf.cache.async_add_records([DNSAddress(host, const._TYPE_A, const._CLASS_IN, 10000, b"\x00")]) + + await aiozc.zeroconf.async_wait_for_start() + await asyncio.sleep(0) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + info = ServiceInfo(type_, registration_name) + info.load_from_cache(aiozc.zeroconf) + assert info.addresses_by_version(IPVersion.V4Only) == [b"\x7f\x00\x00\x01"] + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_service_name_change_as_seen_has_ip_in_cache(): + """Test that service name changes are seen by async_request when the ip is in the cache.""" + type_ = "_http._tcp.local." + registration_name = f"multiarec.{type_}" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahost.local." + + # New kwarg way + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_AAAA], + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + registration_name, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x02", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + await aiozc.zeroconf.async_wait_for_start() + await asyncio.sleep(0) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + info = ServiceInfo(type_, registration_name) + await info.async_request(aiozc.zeroconf, timeout=200) + assert info.addresses_by_version(IPVersion.V4Only) == [] + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + info = ServiceInfo(type_, registration_name) + await info.async_request(aiozc.zeroconf, timeout=200) + assert info.addresses_by_version(IPVersion.V4Only) == [b"\x7f\x00\x00\x02"] + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_service_name_change_as_seen_ip_not_in_cache(): + """Test that service name changes are seen by async_request when the ip is not in the cache.""" + type_ = "_http._tcp.local." + registration_name = f"multiarec.{type_}" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahost.local." + + # New kwarg way + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_AAAA], + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + registration_name, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + await aiozc.zeroconf.async_wait_for_start() + await asyncio.sleep(0) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + info = ServiceInfo(type_, registration_name) + await info.async_request(aiozc.zeroconf, timeout=200) + assert info.addresses_by_version(IPVersion.V4Only) == [] + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x02", + ), + 0, + ) + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + + info = ServiceInfo(type_, registration_name) + await info.async_request(aiozc.zeroconf, timeout=200) + assert info.addresses_by_version(IPVersion.V4Only) == [b"\x7f\x00\x00\x02"] + + await aiozc.async_close() + + +@pytest.mark.asyncio +@patch.object(info, "_LISTENER_TIME", 10000000) +async def test_release_wait_when_new_recorded_added_concurrency(): + """Test that concurrent async_request returns as soon as new matching records are added to the cache.""" + type_ = "_http._tcp.local." + registration_name = f"multiareccon.{type_}" + desc = {"path": "/~paulsm/"} + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + host = "multahostcon.local." + await aiozc.zeroconf.async_wait_for_start() + + # New kwarg way + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, host) + tasks = [asyncio.create_task(info.async_request(aiozc.zeroconf, timeout=200000)) for _ in range(10)] + await asyncio.sleep(0.1) + for task in tasks: + assert not task.done() + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSNsec( + registration_name, + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + registration_name, + [const._TYPE_AAAA], + ), + 0, + ) + generated.add_answer_at_time( + r.DNSService( + registration_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + 0, + 0, + 80, + host, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + host, + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + registration_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 10000, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + await asyncio.sleep(0) + for task in tasks: + assert not task.done() + aiozc.zeroconf.record_manager.async_updates_from_response(r.DNSIncoming(generated.packets()[0])) + _, pending = await asyncio.wait(tasks, timeout=2) + assert not pending + assert info.addresses == [b"\x7f\x00\x00\x01"] + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_service_info_nsec_records(): + """Test we can generate nsec records from ServiceInfo.""" + type_ = "_http._tcp.local." + registration_name = f"multiareccon.{type_}" + desc = {"path": "/~paulsm/"} + host = "multahostcon.local." + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, host) + nsec_record = info.dns_nsec([const._TYPE_A, const._TYPE_AAAA], 50) + assert nsec_record.name == registration_name + assert nsec_record.type == const._TYPE_NSEC + assert nsec_record.ttl == 50 + assert nsec_record.rdtypes == [const._TYPE_A, const._TYPE_AAAA] + + +@pytest.mark.asyncio +async def test_address_resolver(): + """Test that the address resolver works.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + resolver = r.AddressResolver("address_resolver_test.local.") + resolve_task = asyncio.create_task(resolver.async_request(aiozc.zeroconf, 3000)) + outgoing = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + outgoing.add_answer_at_time( + r.DNSAddress( + "address_resolver_test.local.", + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + + aiozc.zeroconf.async_send(outgoing) + assert await resolve_task + assert resolver.addresses == [b"\x7f\x00\x00\x01"] + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_address_resolver_ipv4(): + """Test that the IPv4 address resolver works.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + resolver = r.AddressResolverIPv4("address_resolver_test_ipv4.local.") + resolve_task = asyncio.create_task(resolver.async_request(aiozc.zeroconf, 3000)) + outgoing = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + outgoing.add_answer_at_time( + r.DNSAddress( + "address_resolver_test_ipv4.local.", + const._TYPE_A, + const._CLASS_IN, + 10000, + b"\x7f\x00\x00\x01", + ), + 0, + ) + + aiozc.zeroconf.async_send(outgoing) + assert await resolve_task + assert resolver.addresses == [b"\x7f\x00\x00\x01"] + await aiozc.async_close() + + +@pytest.mark.asyncio +@unittest.skipIf(not has_working_ipv6(), "Requires IPv6") +@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") +async def test_address_resolver_ipv6(): + """Test that the IPv6 address resolver works.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + resolver = r.AddressResolverIPv6("address_resolver_test_ipv6.local.") + resolve_task = asyncio.create_task(resolver.async_request(aiozc.zeroconf, 3000)) + outgoing = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + outgoing.add_answer_at_time( + r.DNSAddress( + "address_resolver_test_ipv6.local.", + const._TYPE_AAAA, + const._CLASS_IN, + 10000, + socket.inet_pton(socket.AF_INET6, "fe80::52e:c2f2:bc5f:e9c6"), + ), + 0, + ) + + aiozc.zeroconf.async_send(outgoing) + assert await resolve_task + assert resolver.ip_addresses_by_version(IPVersion.All) == [ip_address("fe80::52e:c2f2:bc5f:e9c6")] + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_unicast_flag_if_requested() -> None: + """Verify we try four times even with the random delay.""" + type_ = "_typethatisnothere._tcp.local." + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + + def async_send(out: DNSOutgoing, addr: str | None = None, port: int = const._MDNS_PORT) -> None: + """Sends an outgoing packet.""" + for question in out.questions: + assert question.unicast + + # patch the zeroconf send + with patch.object(aiozc.zeroconf, "async_send", async_send): + await aiozc.async_get_service_info( + f"willnotbefound.{type_}", type_, timeout=200, question_type=r.DNSQuestionType.QU + ) + + await aiozc.async_close() diff --git a/tests/services/test_registry.py b/tests/services/test_registry.py new file mode 100644 index 000000000..c3ae3a28b --- /dev/null +++ b/tests/services/test_registry.py @@ -0,0 +1,153 @@ +"""Unit tests for zeroconf._services.registry.""" + +from __future__ import annotations + +import socket +import unittest + +import zeroconf as r +from zeroconf import ServiceInfo + + +class TestServiceRegistry(unittest.TestCase): + def test_only_register_once(self): + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + registry = r.ServiceRegistry() + registry.async_add(info) + self.assertRaises(r.ServiceNameAlreadyRegistered, registry.async_add, info) + registry.async_remove(info) + registry.async_add(info) + + def test_register_same_server(self): + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + name2 = "xxxyyy2" + registration_name = f"{name}.{type_}" + registration_name2 = f"{name2}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "same.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_, + registration_name2, + 80, + 0, + 0, + desc, + "same.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + registry = r.ServiceRegistry() + registry.async_add(info) + registry.async_add(info2) + assert registry.async_get_infos_server("same.local.") == [info, info2] + registry.async_remove(info) + assert registry.async_get_infos_server("same.local.") == [info2] + registry.async_remove(info2) + assert registry.async_get_infos_server("same.local.") == [] + + def test_unregister_multiple_times(self): + """Verify we can unregister a service multiple times. + + In production unregister_service and unregister_all_services + may happen at the same time during shutdown. We want to treat + this as non-fatal since its expected to happen and it is unlikely + that the callers know about each other. + """ + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + registry = r.ServiceRegistry() + registry.async_add(info) + self.assertRaises(r.ServiceNameAlreadyRegistered, registry.async_add, info) + registry.async_remove(info) + registry.async_remove(info) + + def test_lookups(self): + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + registry = r.ServiceRegistry() + registry.async_add(info) + + assert registry.async_get_service_infos() == [info] + assert registry.async_get_info_name(registration_name) == info + assert registry.async_get_infos_type(type_) == [info] + assert registry.async_get_infos_server("ash-2.local.") == [info] + assert registry.async_get_types() == [type_] + + def test_lookups_upper_case_by_lower_case(self): + type_ = "_test-SRVC-type._tcp.local." + name = "Xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ASH-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + registry = r.ServiceRegistry() + registry.async_add(info) + + assert registry.async_get_service_infos() == [info] + assert registry.async_get_info_name(registration_name.lower()) == info + assert registry.async_get_infos_type(type_.lower()) == [info] + assert registry.async_get_infos_server("ash-2.local.") == [info] + assert registry.async_get_types() == [type_.lower()] diff --git a/tests/services/test_types.py b/tests/services/test_types.py new file mode 100644 index 000000000..6e3fe70cd --- /dev/null +++ b/tests/services/test_types.py @@ -0,0 +1,159 @@ +"""Unit tests for zeroconf._services.types.""" + +from __future__ import annotations + +import logging +import os +import socket +import sys +import unittest + +import zeroconf as r +from zeroconf import ServiceInfo, Zeroconf, ZeroconfServiceTypes + +from .. import IPV6_LOOPBACK_FIND_TIMEOUT, LOOPBACK_FIND_TIMEOUT, _clear_cache, has_working_ipv6 + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +def test_integration_with_listener(quick_timing, disable_duplicate_packet_suppression): + type_ = "_test-listen-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + zeroconf_registrar = Zeroconf(interfaces=["127.0.0.1"]) + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + zeroconf_registrar.registry.async_add(info) + try: + service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT) + assert type_ in service_types + _clear_cache(zeroconf_registrar) + service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT) + assert type_ in service_types + + finally: + zeroconf_registrar.close() + + +@unittest.skipIf(not has_working_ipv6(), "Requires IPv6") +@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") +def test_integration_with_listener_v6_records(quick_timing, disable_duplicate_packet_suppression): + type_ = "_test-listenv6rec-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + addr = "2606:2800:220:1:248:1893:25c8:1946" # example.com + + zeroconf_registrar = Zeroconf(interfaces=["127.0.0.1"]) + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_pton(socket.AF_INET6, addr)], + ) + zeroconf_registrar.registry.async_add(info) + try: + service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT) + assert type_ in service_types + _clear_cache(zeroconf_registrar) + service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT) + assert type_ in service_types + + finally: + zeroconf_registrar.close() + + +@unittest.skipIf(not has_working_ipv6() or sys.platform == "win32", "Requires IPv6") +@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") +@unittest.skipIf( + sys.platform == "darwin" and os.environ.get("GITHUB_ACTIONS") == "true", + "IPv6 multicast not working on macOS GitHub Actions", +) +def test_integration_with_listener_ipv6(quick_timing, disable_duplicate_packet_suppression): + type_ = "_test-listenv6ip-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + addr = "2606:2800:220:1:248:1893:25c8:1946" # example.com + + zeroconf_registrar = Zeroconf(ip_version=r.IPVersion.V6Only) + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_pton(socket.AF_INET6, addr)], + ) + zeroconf_registrar.registry.async_add(info) + try: + service_types = ZeroconfServiceTypes.find( + ip_version=r.IPVersion.V6Only, timeout=IPV6_LOOPBACK_FIND_TIMEOUT + ) + assert type_ in service_types + _clear_cache(zeroconf_registrar) + service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=IPV6_LOOPBACK_FIND_TIMEOUT) + assert type_ in service_types + + finally: + zeroconf_registrar.close() + + +def test_integration_with_subtype_and_listener(quick_timing, disable_duplicate_packet_suppression): + subtype_ = "_subtype._sub" + type_ = "_listen._tcp.local." + name = "xxxyyy" + # Note: discovery returns only DNS-SD type not subtype + discovery_type = f"{subtype_}.{type_}" + registration_name = f"{name}.{type_}" + + zeroconf_registrar = Zeroconf(interfaces=["127.0.0.1"]) + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + discovery_type, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + zeroconf_registrar.registry.async_add(info) + try: + service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT) + assert discovery_type in service_types + _clear_cache(zeroconf_registrar) + service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT) + assert discovery_type in service_types + + finally: + zeroconf_registrar.close() diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py new file mode 100644 index 000000000..58f5aaab9 --- /dev/null +++ b/tests/test_asyncio.py @@ -0,0 +1,1415 @@ +"""Unit tests for aio.py.""" + +from __future__ import annotations + +import asyncio +import logging +import os +import socket +import threading +from typing import cast +from unittest.mock import ANY, call, patch + +import pytest + +import zeroconf._services.browser as _services_browser +from zeroconf import ( + DNSAddress, + DNSIncoming, + DNSOutgoing, + DNSPointer, + DNSQuestion, + DNSService, + DNSText, + NotRunningException, + ServiceStateChange, + Zeroconf, + const, +) +from zeroconf._exceptions import ( + BadTypeInNameException, + NonUniqueNameException, + ServiceNameAlreadyRegistered, +) +from zeroconf._services import ServiceListener +from zeroconf._services.info import ServiceInfo +from zeroconf._utils.time import current_time_millis +from zeroconf.asyncio import ( + AsyncServiceBrowser, + AsyncServiceInfo, + AsyncZeroconf, + AsyncZeroconfServiceTypes, +) +from zeroconf.const import _LISTENER_TIME + +from . import ( + LOOPBACK_FIND_TIMEOUT, + QuestionHistoryWithoutSuppression, + _clear_cache, + has_working_ipv6, + time_changed_millis, +) + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +@pytest.fixture(autouse=True) +def verify_threads_ended(): + """Verify that the threads are not running after the test.""" + threads_before = frozenset(threading.enumerate()) + yield + threads_after = frozenset(threading.enumerate()) + non_executor_threads = frozenset( + thread + for thread in threads_after + if "asyncio" not in thread.name and "ThreadPoolExecutor" not in thread.name + ) + threads = non_executor_threads - threads_before + assert not threads + + +@pytest.mark.asyncio +async def test_async_basic_usage() -> None: + """Test we can create and close the instance.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_close_twice() -> None: + """Test we can close twice.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.async_close() + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_with_sync_passed_in() -> None: + """Test we can create and close the instance when passing in a sync Zeroconf.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + aiozc = AsyncZeroconf(zc=zc) + assert aiozc.zeroconf is zc + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_with_sync_passed_in_closed_in_async() -> None: + """Test caller closes the sync version in async.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + aiozc = AsyncZeroconf(zc=zc) + assert aiozc.zeroconf is zc + zc.close() + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_sync_within_event_loop_executor() -> None: + """Test sync version still works from an executor within an event loop.""" + + def sync_code(): + zc = Zeroconf(interfaces=["127.0.0.1"]) + assert zc.get_service_info("_neverused._tcp.local.", "xneverused._neverused._tcp.local.", 10) is None + zc.close() + + await asyncio.get_event_loop().run_in_executor(None, sync_code) + + +@pytest.mark.asyncio +async def test_async_service_registration(quick_timing: None) -> None: + """Test registering services broadcasts the registration by default.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test1-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + calls = [] + + class MyListener(ServiceListener): + def add_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("add", type, name)) + + def remove_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("remove", type, name)) + + def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("update", type, name)) + + listener = MyListener() + + aiozc.zeroconf.add_service_listener(type_, listener) + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aiozc.async_register_service(info) + await task + new_info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + task = await aiozc.async_update_service(new_info) + await task + assert new_info.dns_service().server_key == "ash-2.local." + new_info.server = "ash-3.local." + task = await aiozc.async_update_service(new_info) + await task + assert new_info.dns_service().server_key == "ash-3.local." + + task = await aiozc.async_unregister_service(new_info) + await task + await aiozc.async_close() + + assert calls == [ + ("add", type_, registration_name), + ("update", type_, registration_name), + ("update", type_, registration_name), + ("remove", type_, registration_name), + ] + + +@pytest.mark.asyncio +async def test_async_service_registration_with_server_missing(quick_timing: None) -> None: + """Test registering a service with the server not specified. + + For backwards compatibility, the server should be set to the + name that was passed in. + """ + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test1-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + calls = [] + + class MyListener(ServiceListener): + def add_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("add", type, name)) + + def remove_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("remove", type, name)) + + def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("update", type, name)) + + listener = MyListener() + + aiozc.zeroconf.add_service_listener(type_, listener) + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aiozc.async_register_service(info) + await task + + assert info.server == registration_name + assert info.server_key == registration_name + new_info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + task = await aiozc.async_update_service(new_info) + await task + + task = await aiozc.async_unregister_service(new_info) + await task + await aiozc.async_close() + + assert calls == [ + ("add", type_, registration_name), + ("update", type_, registration_name), + ("remove", type_, registration_name), + ] + + +@pytest.mark.asyncio +async def test_async_service_registration_same_server_different_ports(quick_timing: None) -> None: + """Test registering services with the same server with different srv records.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test1-srvc-type._tcp.local." + name = "xxxyyy" + name2 = "xxxyyy2" + + registration_name = f"{name}.{type_}" + registration_name2 = f"{name2}.{type_}" + + calls = [] + + class MyListener(ServiceListener): + def add_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("add", type, name)) + + def remove_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("remove", type, name)) + + def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("update", type, name)) + + listener = MyListener() + + aiozc.zeroconf.add_service_listener(type_, listener) + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_, + registration_name2, + 81, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + tasks = [] + tasks.append(await aiozc.async_register_service(info)) + tasks.append(await aiozc.async_register_service(info2)) + await asyncio.gather(*tasks) + + task = await aiozc.async_unregister_service(info) + await task + entries = aiozc.zeroconf.cache.async_entries_with_server("ash-2.local.") + assert len(entries) == 1 + assert info2.dns_service() in entries + await aiozc.async_close() + assert calls == [ + ("add", type_, registration_name), + ("add", type_, registration_name2), + ("remove", type_, registration_name), + ("remove", type_, registration_name2), + ] + + +@pytest.mark.asyncio +async def test_async_service_registration_same_server_same_ports(quick_timing: None) -> None: + """Test registering services with the same server with the exact same srv record.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test1-srvc-type._tcp.local." + name = "xxxyyy" + name2 = "xxxyyy2" + + registration_name = f"{name}.{type_}" + registration_name2 = f"{name2}.{type_}" + + calls = [] + + class MyListener(ServiceListener): + def add_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("add", type, name)) + + def remove_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("remove", type, name)) + + def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("update", type, name)) + + listener = MyListener() + + aiozc.zeroconf.add_service_listener(type_, listener) + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_, + registration_name2, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + tasks = [] + tasks.append(await aiozc.async_register_service(info)) + tasks.append(await aiozc.async_register_service(info2)) + await asyncio.gather(*tasks) + + task = await aiozc.async_unregister_service(info) + await task + entries = aiozc.zeroconf.cache.async_entries_with_server("ash-2.local.") + assert len(entries) == 1 + assert info2.dns_service() in entries + await aiozc.async_close() + assert calls == [ + ("add", type_, registration_name), + ("add", type_, registration_name2), + ("remove", type_, registration_name), + ("remove", type_, registration_name2), + ] + + +@pytest.mark.asyncio +async def test_async_service_registration_name_conflict(quick_timing: None) -> None: + """Test registering services throws on name conflict.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test-srvc2-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aiozc.async_register_service(info) + await task + + with pytest.raises(NonUniqueNameException): + task = await aiozc.async_register_service(info) + await task + + with pytest.raises(ServiceNameAlreadyRegistered): + task = await aiozc.async_register_service(info, cooperating_responders=True) + await task + + conflicting_info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-3.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + + with pytest.raises(NonUniqueNameException): + task = await aiozc.async_register_service(conflicting_info) + await task + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_service_registration_name_does_not_match_type() -> None: + """Test registering services throws when the name does not match the type.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test-srvc3-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info.type = "_wrong._tcp.local." + with pytest.raises(BadTypeInNameException): + task = await aiozc.async_register_service(info) + await task + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_service_registration_name_strict_check(quick_timing: None) -> None: + """Test registering services throws when the name does not comply.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_ibisip_http._tcp.local." + name = "CustomerInformationService-F4D4895E9EEB" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + with pytest.raises(BadTypeInNameException): + await zc.async_check_service(info, allow_name_change=False) + + with pytest.raises(BadTypeInNameException): + task = await aiozc.async_register_service(info) + await task + + await zc.async_check_service(info, allow_name_change=False, strict=False) + task = await aiozc.async_register_service(info, strict=False) + await task + + await aiozc.async_unregister_service(info) + await aiozc.async_close() + zc.close() + + +@pytest.mark.asyncio +async def test_async_tasks(quick_timing: None) -> None: + """Test awaiting broadcast tasks""" + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test-srvc4-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + calls = [] + + class MyListener(ServiceListener): + def add_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("add", type, name)) + + def remove_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("remove", type, name)) + + def update_service(self, zeroconf: Zeroconf, type: str, name: str) -> None: + calls.append(("update", type, name)) + + listener = MyListener() + aiozc.zeroconf.add_service_listener(type_, listener) + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aiozc.async_register_service(info) + assert isinstance(task, asyncio.Task) + await task + + new_info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + task = await aiozc.async_update_service(new_info) + assert isinstance(task, asyncio.Task) + await task + + task = await aiozc.async_unregister_service(new_info) + assert isinstance(task, asyncio.Task) + await task + + await aiozc.async_close() + + assert calls == [ + ("add", type_, registration_name), + ("update", type_, registration_name), + ("remove", type_, registration_name), + ] + + +@pytest.mark.asyncio +async def test_async_wait_unblocks_on_update(quick_timing: None) -> None: + """Test async_wait will unblock on update.""" + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test-srvc4-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aiozc.async_register_service(info) + + # Should unblock due to update from the + # registration + now = current_time_millis() + await aiozc.zeroconf.async_wait(50000) + assert current_time_millis() - now < 3000 + await task + + now = current_time_millis() + await aiozc.zeroconf.async_wait(50) + assert current_time_millis() - now < 1000 + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_service_info_async_request(quick_timing: None, quick_request_timing: None) -> None: + """Test registering services broadcasts and query with AsyncServceInfo.async_request.""" + if not has_working_ipv6() or os.environ.get("SKIP_IPV6"): + pytest.skip("Requires IPv6") + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test1-srvc-type._tcp.local." + name = "xxxyyy" + name2 = "abc" + registration_name = f"{name}.{type_}" + registration_name2 = f"{name2}.{type_}" + + # Start a tasks BEFORE the registration that will keep trying + # and see the registration a bit later + get_service_info_task1 = asyncio.ensure_future(aiozc.async_get_service_info(type_, registration_name)) + await asyncio.sleep(_LISTENER_TIME / 1000 / 2) + get_service_info_task2 = asyncio.ensure_future(aiozc.async_get_service_info(type_, registration_name)) + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-1.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_, + registration_name2, + 80, + 0, + 0, + desc, + "ash-5.local.", + addresses=[socket.inet_aton("10.0.1.5")], + ) + tasks = [] + tasks.append(await aiozc.async_register_service(info)) + tasks.append(await aiozc.async_register_service(info2)) + await asyncio.gather(*tasks) + + aiosinfo = await get_service_info_task1 + assert aiosinfo is not None + assert aiosinfo.addresses == [socket.inet_aton("10.0.1.2")] + + aiosinfo = await get_service_info_task2 + assert aiosinfo is not None + assert aiosinfo.addresses == [socket.inet_aton("10.0.1.2")] + + aiosinfo = await aiozc.async_get_service_info(type_, registration_name) + assert aiosinfo is not None + assert aiosinfo.addresses == [socket.inet_aton("10.0.1.2")] + + new_info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[ + socket.inet_aton("10.0.1.3"), + socket.inet_pton(socket.AF_INET6, "6001:db8::1"), + ], + ) + + task = await aiozc.async_update_service(new_info) + await task + + aiosinfo = await aiozc.async_get_service_info(type_, registration_name) + assert aiosinfo is not None + assert aiosinfo.addresses == [socket.inet_aton("10.0.1.3")] + + aiosinfo = await aiozc.zeroconf.async_get_service_info(type_, registration_name) + assert aiosinfo is not None + assert aiosinfo.addresses == [socket.inet_aton("10.0.1.3")] + + aiosinfos = await asyncio.gather( + aiozc.async_get_service_info(type_, registration_name), + aiozc.async_get_service_info(type_, registration_name2), + ) + assert aiosinfos[0] is not None + assert aiosinfos[0].addresses == [socket.inet_aton("10.0.1.3")] + assert aiosinfos[1] is not None + assert aiosinfos[1].addresses == [socket.inet_aton("10.0.1.5")] + + # Drop pending multicast responses queued under the original + # `info` / `info2` registrations. Their snapshots predate the + # `async_update_service` swap, so if they flushed after + # `_clear_cache` below they would poison `aiosinfo.server` + # with `ash-1.local.` and the `_is_complete=False` loop would + # then keep asking for an A record nobody answers. The pending + # `loop.call_at` for each queue fires harmlessly on the empty + # deque. + aiozc.zeroconf.out_queue.queue.clear() + aiozc.zeroconf.out_delay_queue.queue.clear() + aiosinfo = AsyncServiceInfo(type_, registration_name) + _clear_cache(aiozc.zeroconf) + # Generating the race condition is almost impossible without + # patching since it's a TOCTOU race. Under `quick_request_timing` + # the first QU query fires at ~10ms and the QM follow-up at ~15ms; + # 300ms leaves plenty of margin for the loopback response to land + # before the loop times out. + with patch("zeroconf.asyncio.AsyncServiceInfo._is_complete", False): + await aiosinfo.async_request(aiozc.zeroconf, 300) + assert aiosinfo is not None + assert aiosinfo.addresses == [socket.inet_aton("10.0.1.3")] + + task = await aiozc.async_unregister_service(new_info) + await task + + # Cap timeout: the service is gone, so this is expected to return None; + # waiting the default 3000ms is pure overhead. + aiosinfo = await aiozc.async_get_service_info(type_, registration_name, timeout=200) + assert aiosinfo is None + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_service_browser(quick_timing: None) -> None: + """Test AsyncServiceBrowser.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test9-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + calls = [] + + class MyListener(ServiceListener): + def add_service(self, aiozc: Zeroconf, type: str, name: str) -> None: + calls.append(("add", type, name)) + + def remove_service(self, aiozc: Zeroconf, type: str, name: str) -> None: + calls.append(("remove", type, name)) + + def update_service(self, aiozc: Zeroconf, type: str, name: str) -> None: + calls.append(("update", type, name)) + + listener = MyListener() + await aiozc.async_add_service_listener(type_, listener) + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aiozc.async_register_service(info) + await task + new_info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + task = await aiozc.async_update_service(new_info) + await task + task = await aiozc.async_unregister_service(new_info) + await task + await aiozc.zeroconf.async_wait(1) + await aiozc.async_close() + + assert calls == [ + ("add", type_, registration_name), + ("update", type_, registration_name), + ("remove", type_, registration_name), + ] + + +@pytest.mark.asyncio +async def test_async_context_manager(quick_timing: None) -> None: + """Test using an async context manager.""" + type_ = "_test10-sr-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + async with AsyncZeroconf(interfaces=["127.0.0.1"]) as aiozc: + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await aiozc.async_register_service(info) + await task + aiosinfo = await aiozc.async_get_service_info(type_, registration_name) + assert aiosinfo is not None + + +@pytest.mark.asyncio +async def test_service_browser_cancel_async_context_manager(): + """Test we can cancel an AsyncServiceBrowser with it being used as an async context manager.""" + + # instantiate a zeroconf instance + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + type_ = "_hap._tcp.local." + + class MyServiceListener(ServiceListener): + pass + + listener = MyServiceListener() + + browser = AsyncServiceBrowser(zc, type_, None, listener) + + assert cast(bool, browser.done) is False + + async with browser: + pass + + assert cast(bool, browser.done) is True + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_unregister_all_services(quick_timing: None) -> None: + """Test unregistering all services.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + type_ = "_test1-srvc-type._tcp.local." + name = "xxxyyy" + name2 = "abc" + registration_name = f"{name}.{type_}" + registration_name2 = f"{name2}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-1.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_, + registration_name2, + 80, + 0, + 0, + desc, + "ash-5.local.", + addresses=[socket.inet_aton("10.0.1.5")], + ) + tasks = [] + tasks.append(await aiozc.async_register_service(info)) + tasks.append(await aiozc.async_register_service(info2)) + await asyncio.gather(*tasks) + + tasks = [] + tasks.append(aiozc.async_get_service_info(type_, registration_name)) + tasks.append(aiozc.async_get_service_info(type_, registration_name2)) + results = await asyncio.gather(*tasks) + assert results[0] is not None + assert results[1] is not None + + await aiozc.async_unregister_all_services() + _clear_cache(aiozc.zeroconf) + + tasks = [] + tasks.append(aiozc.async_get_service_info(type_, registration_name, timeout=200)) + tasks.append(aiozc.async_get_service_info(type_, registration_name2, timeout=200)) + results = await asyncio.gather(*tasks) + assert results[0] is None + assert results[1] is None + + # Verify we can call again + await aiozc.async_unregister_all_services() + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_zeroconf_service_types(quick_timing: None) -> None: + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + zeroconf_registrar = AsyncZeroconf(interfaces=["127.0.0.1"]) + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + task = await zeroconf_registrar.async_register_service(info) + await task + # Wait for the last announce broadcast before clearing. With + # `quick_timing` the broadcasts use _REGISTER_TIME=10ms apart so + # 50ms is plenty. + await asyncio.sleep(0.05) + _clear_cache(zeroconf_registrar.zeroconf) + try: + service_types = await AsyncZeroconfServiceTypes.async_find( + interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT + ) + assert type_ in service_types + _clear_cache(zeroconf_registrar.zeroconf) + service_types = await AsyncZeroconfServiceTypes.async_find( + aiozc=zeroconf_registrar, timeout=LOOPBACK_FIND_TIMEOUT + ) + assert type_ in service_types + + finally: + await zeroconf_registrar.async_close() + + +@pytest.mark.asyncio +async def test_guard_against_running_serviceinfo_request_event_loop() -> None: + """Test that running ServiceInfo.request from the event loop throws.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + + service_info = AsyncServiceInfo("_hap._tcp.local.", "doesnotmatter._hap._tcp.local.") + with pytest.raises(RuntimeError): + service_info.request(aiozc.zeroconf, 3000) + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_service_browser_instantiation_generates_add_events_from_cache(): + """Test that the ServiceBrowser will generate Add events with the existing cache when starting.""" + + # instantiate a zeroconf instance + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + type_ = "_hap._tcp.local." + registration_name = f"xxxyyy.{type_}" + callbacks = [] + + class MyServiceListener(ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("add", type_, name)) + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("remove", type_, name)) + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("update", type_, name)) + + listener = MyServiceListener() + + desc = {"path": "/~paulsm/"} + address_parsed = "10.0.1.2" + address = socket.inet_aton(address_parsed) + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address]) + zc.cache.async_add_records( + [info.dns_pointer(), info.dns_service(), *info.dns_addresses(), info.dns_text()] + ) + + browser = AsyncServiceBrowser(zc, type_, None, listener) + + await asyncio.sleep(0) + + assert callbacks == [ + ("add", type_, registration_name), + ] + await browser.async_cancel() + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_integration(quick_timing: None) -> None: + service_added = asyncio.Event() + service_removed = asyncio.Event() + unexpected_ttl = asyncio.Event() + got_query = asyncio.Event() + + type_ = "_http._tcp.local." + registration_name = f"xxxyyy.{type_}" + + def on_service_state_change(zeroconf, service_type, state_change, name): + if name == registration_name: + if state_change is ServiceStateChange.Added: + service_added.set() + elif state_change is ServiceStateChange.Removed: + service_removed.set() + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_browser = aiozc.zeroconf + zeroconf_browser.question_history = QuestionHistoryWithoutSuppression() + await zeroconf_browser.async_wait_for_start() + + # we are going to patch the zeroconf send to check packet sizes + old_send = zeroconf_browser.async_send + + expected_ttl = const._DNS_OTHER_TTL + nbr_answers = 0 + answers = [] + packets = [] + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): + """Sends an outgoing packet.""" + pout = DNSIncoming(out.packets()[0]) + packets.append(pout) + last_answers = pout.answers() + answers.append(last_answers) + + nonlocal nbr_answers + for answer in last_answers: + nbr_answers += 1 + if not answer.ttl > expected_ttl / 2: + unexpected_ttl.set() + + got_query.set() + + old_send(out, addr=addr, port=port, v6_flow_scope=v6_flow_scope) + + assert len(zeroconf_browser.engine.protocols) == 2 + + aio_zeroconf_registrar = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_registrar = aio_zeroconf_registrar.zeroconf + await aio_zeroconf_registrar.zeroconf.async_wait_for_start() + + assert len(zeroconf_registrar.engine.protocols) == 2 + # patch the zeroconf send so we can capture what is being sent + with patch.object(zeroconf_browser, "async_send", send): + service_added = asyncio.Event() + service_removed = asyncio.Event() + + browser = AsyncServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + # Wait for the browser's first startup query to land (with an empty + # cache) before registering — otherwise on fast loopback the register + # may finish before the first query fires, and answers[0] picks up + # the known PTR via RFC 6762 §7.1 known-answer suppression. + await asyncio.wait_for(got_query.wait(), 1) + # Snapshot the first query's answers and reset the captures so the + # subsequent assertions don't have to predict whether further startup + # queries fire in real time (before the manual time_changed_millis + # loop) or under mock time. + first_answers = answers[0] + packets.clear() + answers.clear() + got_query.clear() + task = await aio_zeroconf_registrar.async_register_service(info) + await task + loop = asyncio.get_running_loop() + try: + await asyncio.wait_for(service_added.wait(), 1) + assert service_added.is_set() + # Make sure the startup queries are sent + original_now = loop.time() + start_millis = original_now * 1000 + + now_millis = start_millis + for query_count in range(_services_browser.STARTUP_QUERIES): + now_millis += (2**query_count) * 1000 + time_changed_millis(now_millis) + + got_query.clear() + assert not unexpected_ttl.is_set() + + # The first startup query was captured separately, so only the + # remaining STARTUP_QUERIES - 1 land here. + assert len(packets) == _services_browser.STARTUP_QUERIES - 1 + packets.clear() + + # Wait for the first refresh query + # Move time forward past when the TTL is no longer + # fresh (AKA ~75% of the TTL) + now_millis = start_millis + ((expected_ttl * 1000) * 0.76) + time_changed_millis(now_millis) + + await asyncio.wait_for(got_query.wait(), 1) + assert not unexpected_ttl.is_set() + assert len(packets) == 1 + packets.clear() + + assert len(answers) == _services_browser.STARTUP_QUERIES + # The first question (captured separately) should have no known answers + assert len(first_answers) == 0 + # The rest of the startup questions should have + # known answers + for answer_list in answers[:-2]: + # Allow 0 or 1 answers due to random delays and timing + assert len(answer_list) <= 1 + # Once the TTL is reached, the last question should have no known answers + assert len(answers[-1]) == 0 + + got_query.clear() + packets.clear() + # Move time forward past when the TTL is no longer + # fresh (AKA 85% of the TTL) to ensure we try + # to rescue the record + now_millis = start_millis + ((expected_ttl * 1000) * 0.87) + time_changed_millis(now_millis) + + await asyncio.wait_for(got_query.wait(), 1) + assert len(packets) == 1 + assert not unexpected_ttl.is_set() + + packets.clear() + got_query.clear() + # Move time forward past when the TTL is no longer + # fresh (AKA 95% of the TTL). At this point + # nothing should get scheduled rescued because the rescue + # would exceed the TTL + now_millis = start_millis + ((expected_ttl * 1000) * 0.98) + + # Verify we don't send a query for a record that is + # past the TTL as we should not try to rescue it + # once its past the TTL + time_changed_millis(now_millis) + await asyncio.wait_for(got_query.wait(), 1) + assert len(packets) == 1 + + # Don't remove service, allow close() to cleanup + finally: + await aio_zeroconf_registrar.async_close() + await asyncio.wait_for(service_removed.wait(), 1) + assert service_removed.is_set() + await browser.async_cancel() + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_info_asking_default_is_asking_qm_questions_after_the_first_qu(quick_request_timing): + """Verify the service info first question is QU and subsequent ones are QM questions.""" + type_ = "_quservice._tcp.local." + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf_info = aiozc.zeroconf + + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + zeroconf_info.registry.async_add(info) + + # we are going to patch the zeroconf send to check query transmission + old_send = zeroconf_info.async_send + + first_outgoing = None + second_outgoing = None + + def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT): + """Sends an outgoing packet.""" + nonlocal first_outgoing + nonlocal second_outgoing + if out.questions: + if first_outgoing is not None and second_outgoing is None: # type: ignore[unreachable] + second_outgoing = out # type: ignore[unreachable] + if first_outgoing is None: + first_outgoing = out + old_send(out, addr=addr, port=port) + + # patch the zeroconf send + with patch.object(zeroconf_info, "async_send", send): + aiosinfo = AsyncServiceInfo(type_, registration_name) + # Patch _is_complete so we send multiple times. Under + # `quick_request_timing` the QU query fires at 0ms and the QM + # follow-up at ~11-15ms (10ms _LISTENER_TIME + 1-5ms jitter); + # 300ms absorbs macOS short-sleep quantization so the QM wake + # lands before the loop times out. + with patch("zeroconf.asyncio.AsyncServiceInfo._is_complete", False): + await aiosinfo.async_request(aiozc.zeroconf, 300) + try: + assert first_outgoing.questions[0].unicast is True # type: ignore[union-attr] + assert second_outgoing.questions[0].unicast is False # type: ignore[attr-defined] + finally: + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_service_browser_ignores_unrelated_updates(): + """Test that the ServiceBrowser ignores unrelated updates.""" + + # instantiate a zeroconf instance + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + type_ = "_veryuniqueone._tcp.local." + registration_name = f"xxxyyy.{type_}" + callbacks = [] + + class MyServiceListener(ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("add", type_, name)) + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("remove", type_, name)) + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + if name == registration_name: + callbacks.append(("update", type_, name)) + + listener = MyServiceListener() + + desc = {"path": "/~paulsm/"} + address_parsed = "10.0.1.2" + address = socket.inet_aton(address_parsed) + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[address]) + zc.cache.async_add_records( + [ + info.dns_pointer(), + info.dns_service(), + *info.dns_addresses(), + info.dns_text(), + DNSService( + "zoom._unrelated._tcp.local.", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 81, + "unrelated.local.", + ), + ] + ) + + browser = AsyncServiceBrowser(zc, type_, None, listener) + + generated = DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + DNSPointer( + "_unrelated._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "zoom._unrelated._tcp.local.", + ), + 0, + ) + generated.add_answer_at_time( + DNSAddress( + "unrelated.local.", + const._TYPE_A, + const._CLASS_IN, + const._DNS_HOST_TTL, + b"1234", + ), + 0, + ) + generated.add_answer_at_time( + DNSText( + "zoom._unrelated._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"zoom", + ), + 0, + ) + + zc.record_manager.async_updates_from_response(DNSIncoming(generated.packets()[0])) + + await browser.async_cancel() + await asyncio.sleep(0) + + assert callbacks == [ + ("add", type_, registration_name), + ] + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_request_timeout(): + """Test that the timeout does not throw an exception and finishes close to the actual timeout.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + start_time = current_time_millis() + assert ( + await aiozc.async_get_service_info("_notfound.local.", "notthere._notfound.local.", timeout=200) + is None + ) + end_time = current_time_millis() + await aiozc.async_close() + # 200ms for the timeout passed above + # 1000ms for loaded systems + schedule overhead + assert (end_time - start_time) < 200 + 1000 + + +@pytest.mark.asyncio +async def test_async_request_non_running_instance(): + """Test that the async_request throws when zeroconf is not running.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.async_close() + with pytest.raises(NotRunningException): + await aiozc.async_get_service_info("_notfound.local.", "notthere._notfound.local.") + + +@pytest.mark.asyncio +async def test_legacy_unicast_response(run_isolated): + """Verify legacy unicast responses include questions and correct id.""" + type_ = "_mservice._tcp.local." + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + aiozc.zeroconf.registry.async_add(info) + query = DNSOutgoing(const._FLAGS_QR_QUERY, multicast=False, id_=888) + question = DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + query.add_question(question) + protocol = aiozc.zeroconf.engine.protocols[0] + + with patch.object(aiozc.zeroconf, "async_send") as send_mock: + protocol.datagram_received(query.packets()[0], ("127.0.0.1", 6503)) + + calls = send_mock.mock_calls + # Verify the response is sent back on the socket it was received from + assert calls == [call(ANY, "127.0.0.1", 6503, (), protocol.transport)] + outgoing = send_mock.call_args[0][0] + assert isinstance(outgoing, DNSOutgoing) + assert outgoing.questions == [question] + assert outgoing.id == query.id + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_update_with_uppercase_names(run_isolated): + """Test an ip update from a shelly which uses uppercase names.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + + callbacks = [] + + class MyServiceListener(ServiceListener): + def add_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + callbacks.append(("add", type_, name)) + + def remove_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + callbacks.append(("remove", type_, name)) + + def update_service(self, zc, type_, name) -> None: # type: ignore[no-untyped-def] + callbacks.append(("update", type_, name)) + + listener = MyServiceListener() + browser = AsyncServiceBrowser(aiozc.zeroconf, "_http._tcp.local.", None, listener) + protocol = aiozc.zeroconf.engine.protocols[0] + + packet = b"\x00\x00\x84\x80\x00\x00\x00\n\x00\x00\x00\x00\t_services\x07_dns-sd\x04_udp\x05local\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00\x14\x07_shelly\x04_tcp\x05local\x00\t_services\x07_dns-sd\x04_udp\x05local\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00\x12\x05_http\x04_tcp\x05local\x00\x07_shelly\x04_tcp\x05local\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00.\x19shellypro4pm-94b97ec07650\x07_shelly\x04_tcp\x05local\x00\x19shellypro4pm-94b97ec07650\x07_shelly\x04_tcp\x05local\x00\x00!\x80\x01\x00\x00\x00x\x00'\x00\x00\x00\x00\x00P\x19ShellyPro4PM-94B97EC07650\x05local\x00\x19shellypro4pm-94b97ec07650\x07_shelly\x04_tcp\x05local\x00\x00\x10\x80\x01\x00\x00\x00x\x00\"\napp=Pro4PM\x10ver=0.10.0-beta5\x05gen=2\x05_http\x04_tcp\x05local\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00,\x19ShellyPro4PM-94B97EC07650\x05_http\x04_tcp\x05local\x00\x19ShellyPro4PM-94B97EC07650\x05_http\x04_tcp\x05local\x00\x00!\x80\x01\x00\x00\x00x\x00'\x00\x00\x00\x00\x00P\x19ShellyPro4PM-94B97EC07650\x05local\x00\x19ShellyPro4PM-94B97EC07650\x05_http\x04_tcp\x05local\x00\x00\x10\x80\x01\x00\x00\x00x\x00\x06\x05gen=2\x19ShellyPro4PM-94B97EC07650\x05local\x00\x00\x01\x80\x01\x00\x00\x00x\x00\x04\xc0\xa8\xbc=\x19ShellyPro4PM-94B97EC07650\x05local\x00\x00/\x80\x01\x00\x00\x00x\x00$\x19ShellyPro4PM-94B97EC07650\x05local\x00\x00\x01@" # noqa: E501 + protocol.datagram_received(packet, ("127.0.0.1", 6503)) + await asyncio.sleep(0) + packet = b"\x00\x00\x84\x80\x00\x00\x00\n\x00\x00\x00\x00\t_services\x07_dns-sd\x04_udp\x05local\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00\x14\x07_shelly\x04_tcp\x05local\x00\t_services\x07_dns-sd\x04_udp\x05local\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00\x12\x05_http\x04_tcp\x05local\x00\x07_shelly\x04_tcp\x05local\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00.\x19shellypro4pm-94b97ec07650\x07_shelly\x04_tcp\x05local\x00\x19shellypro4pm-94b97ec07650\x07_shelly\x04_tcp\x05local\x00\x00!\x80\x01\x00\x00\x00x\x00'\x00\x00\x00\x00\x00P\x19ShellyPro4PM-94B97EC07650\x05local\x00\x19shellypro4pm-94b97ec07650\x07_shelly\x04_tcp\x05local\x00\x00\x10\x80\x01\x00\x00\x00x\x00\"\napp=Pro4PM\x10ver=0.10.0-beta5\x05gen=2\x05_http\x04_tcp\x05local\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00,\x19ShellyPro4PM-94B97EC07650\x05_http\x04_tcp\x05local\x00\x19ShellyPro4PM-94B97EC07650\x05_http\x04_tcp\x05local\x00\x00!\x80\x01\x00\x00\x00x\x00'\x00\x00\x00\x00\x00P\x19ShellyPro4PM-94B97EC07650\x05local\x00\x19ShellyPro4PM-94B97EC07650\x05_http\x04_tcp\x05local\x00\x00\x10\x80\x01\x00\x00\x00x\x00\x06\x05gen=2\x19ShellyPro4PM-94B97EC07650\x05local\x00\x00\x01\x80\x01\x00\x00\x00x\x00\x04\xc0\xa8\xbcA\x19ShellyPro4PM-94B97EC07650\x05local\x00\x00/\x80\x01\x00\x00\x00x\x00$\x19ShellyPro4PM-94B97EC07650\x05local\x00\x00\x01@" # noqa: E501 + protocol.datagram_received(packet, ("127.0.0.1", 6503)) + await browser.async_cancel() + await aiozc.async_close() + + assert callbacks == [ + ("add", "_http._tcp.local.", "ShellyPro4PM-94B97EC07650._http._tcp.local."), + ("update", "_http._tcp.local.", "ShellyPro4PM-94B97EC07650._http._tcp.local."), + ] diff --git a/tests/test_cache.py b/tests/test_cache.py new file mode 100644 index 000000000..aeb3a2abf --- /dev/null +++ b/tests/test_cache.py @@ -0,0 +1,721 @@ +"""Unit tests for zeroconf._cache.""" + +from __future__ import annotations + +import logging +import unittest.mock +from heapq import heapify, heappop + +import pytest + +import zeroconf as r +from zeroconf import const + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +class TestDNSCache(unittest.TestCase): + def test_order(self): + record1 = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"b") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + entry = r.DNSEntry("a", const._TYPE_SOA, const._CLASS_IN) + cached_record = cache.get(entry) + assert cached_record == record2 + + def test_adding_same_record_to_cache_different_ttls_with_get(self): + """We should always get back the last entry we added if there are different TTLs. + + This ensures we only have one source of truth for TTLs as a record cannot + be both expired and not expired. + """ + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 10, b"a") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + entry = r.DNSEntry(record2.name, const._TYPE_A, const._CLASS_IN) + cached_record = cache.get(entry) + assert cached_record == record2 + + def test_adding_same_record_to_cache_different_ttls_with_get_all(self): + """Verify we only get one record back. + + The last record added should replace the previous since two + records with different ttls are __eq__. This ensures we + only have one source of truth for TTLs as a record cannot + be both expired and not expired. + """ + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 10, b"a") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + cached_records = cache.get_all_by_details("a", const._TYPE_A, const._CLASS_IN) + assert cached_records == [record2] + + def test_cache_empty_does_not_leak_memory_by_leaving_empty_list(self): + record1 = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"b") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert "a" in cache.cache + cache.async_remove_records([record1, record2]) + assert "a" not in cache.cache + + def test_cache_empty_multiple_calls(self): + record1 = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"b") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert "a" in cache.cache + cache.async_remove_records([record1, record2]) + assert "a" not in cache.cache + + +class TestDNSAsyncCacheAPI(unittest.TestCase): + def test_async_get_unique(self): + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"b") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert cache.async_get_unique(record1) == record1 + assert cache.async_get_unique(record2) == record2 + + def test_async_all_by_details(self): + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"b") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert set(cache.async_all_by_details("a", const._TYPE_A, const._CLASS_IN)) == { + record1, + record2, + } + + def test_async_entries_with_server(self): + record1 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 85, + "ab", + ) + record2 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "ab", + ) + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert set(cache.async_entries_with_server("ab")) == {record1, record2} + assert set(cache.async_entries_with_server("AB")) == {record1, record2} + + def test_async_entries_with_name(self): + record1 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 85, + "ab", + ) + record2 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "ab", + ) + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert set(cache.async_entries_with_name("irrelevant")) == {record1, record2} + assert set(cache.async_entries_with_name("Irrelevant")) == {record1, record2} + + +# These functions have been seen in other projects so +# we try to maintain a stable API for all the threadsafe getters +class TestDNSCacheAPI(unittest.TestCase): + def test_get(self): + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"b") + record3 = r.DNSAddress("a", const._TYPE_AAAA, const._CLASS_IN, 1, b"ipv6") + cache = r.DNSCache() + cache.async_add_records([record1, record2, record3]) + assert cache.get(record1) == record1 + assert cache.get(record2) == record2 + assert cache.get(r.DNSEntry("a", const._TYPE_A, const._CLASS_IN)) == record2 + assert cache.get(r.DNSEntry("a", const._TYPE_AAAA, const._CLASS_IN)) == record3 + assert cache.get(r.DNSEntry("notthere", const._TYPE_A, const._CLASS_IN)) is None + + def test_get_by_details(self): + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"b") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert cache.get_by_details("a", const._TYPE_A, const._CLASS_IN) == record2 + + def test_get_all_by_details(self): + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a") + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"b") + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert set(cache.get_all_by_details("a", const._TYPE_A, const._CLASS_IN)) == { + record1, + record2, + } + + def test_entries_with_server(self): + record1 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 85, + "ab", + ) + record2 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "ab", + ) + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert set(cache.entries_with_server("ab")) == {record1, record2} + assert set(cache.entries_with_server("AB")) == {record1, record2} + + def test_entries_with_name(self): + record1 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 85, + "ab", + ) + record2 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "ab", + ) + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert set(cache.entries_with_name("irrelevant")) == {record1, record2} + assert set(cache.entries_with_name("Irrelevant")) == {record1, record2} + + def test_current_entry_with_name_and_alias(self): + record1 = r.DNSPointer( + "irrelevant", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "x.irrelevant", + ) + record2 = r.DNSPointer( + "irrelevant", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "y.irrelevant", + ) + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert cache.current_entry_with_name_and_alias("irrelevant", "x.irrelevant") == record1 + + def test_name(self): + record1 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 85, + "ab", + ) + record2 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "ab", + ) + cache = r.DNSCache() + cache.async_add_records([record1, record2]) + assert cache.names() == ["irrelevant"] + + +def test_async_entries_with_name_returns_newest_record(): + cache = r.DNSCache() + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0) + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0) + cache.async_add_records([record1]) + cache.async_add_records([record2]) + assert next(iter(cache.async_entries_with_name("a"))) is record2 + + +def test_async_entries_with_server_returns_newest_record(): + cache = r.DNSCache() + record1 = r.DNSService("a", const._TYPE_SRV, const._CLASS_IN, 1, 1, 1, 1, "a", created=1.0) + record2 = r.DNSService("a", const._TYPE_SRV, const._CLASS_IN, 1, 1, 1, 1, "a", created=2.0) + cache.async_add_records([record1]) + cache.async_add_records([record2]) + assert next(iter(cache.async_entries_with_server("a"))) is record2 + + +def test_async_get_returns_newest_record(): + cache = r.DNSCache() + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0) + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0) + cache.async_add_records([record1]) + cache.async_add_records([record2]) + assert cache.get(record2) is record2 + + +def test_async_get_returns_newest_nsec_record(): + cache = r.DNSCache() + record1 = r.DNSNsec("a", const._TYPE_NSEC, const._CLASS_IN, 1, "a", [], created=1.0) + record2 = r.DNSNsec("a", const._TYPE_NSEC, const._CLASS_IN, 1, "a", [], created=2.0) + cache.async_add_records([record1]) + cache.async_add_records([record2]) + assert cache.get(record2) is record2 + + +def test_get_by_details_returns_newest_record(): + cache = r.DNSCache() + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0) + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0) + cache.async_add_records([record1]) + cache.async_add_records([record2]) + assert cache.get_by_details("a", const._TYPE_A, const._CLASS_IN) is record2 + + +def test_get_all_by_details_returns_newest_record(): + cache = r.DNSCache() + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0) + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0) + cache.async_add_records([record1]) + cache.async_add_records([record2]) + records = cache.get_all_by_details("a", const._TYPE_A, const._CLASS_IN) + assert len(records) == 1 + assert records[0] is record2 + + +def test_async_get_all_by_details_returns_newest_record(): + cache = r.DNSCache() + record1 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=1.0) + record2 = r.DNSAddress("a", const._TYPE_A, const._CLASS_IN, 1, b"a", created=2.0) + cache.async_add_records([record1]) + cache.async_add_records([record2]) + records = cache.async_all_by_details("a", const._TYPE_A, const._CLASS_IN) + assert len(records) == 1 + assert records[0] is record2 + + +def test_async_get_unique_returns_newest_record(): + cache = r.DNSCache() + record1 = r.DNSPointer("a", const._TYPE_PTR, const._CLASS_IN, 1, "a", created=1.0) + record2 = r.DNSPointer("a", const._TYPE_PTR, const._CLASS_IN, 1, "a", created=2.0) + cache.async_add_records([record1]) + cache.async_add_records([record2]) + record = cache.async_get_unique(record1) + assert record is record2 + record = cache.async_get_unique(record2) + assert record is record2 + + +@pytest.mark.asyncio +async def test_cache_heap_cleanup() -> None: + """Test that the heap gets cleaned up when there are many old expirations.""" + cache = r.DNSCache() + # The heap should not be cleaned up when there are less than 100 expiration changes + min_records_to_cleanup = 100 + now = r.current_time_millis() + name = "heap.local." + ttl_seconds = 100 + ttl_millis = ttl_seconds * 1000 + + for i in range(min_records_to_cleanup): + record = r.DNSAddress(name, const._TYPE_A, const._CLASS_IN, ttl_seconds, b"1", created=now + i) + cache.async_add_records([record]) + + assert len(cache._expire_heap) == min_records_to_cleanup + assert len(cache.async_entries_with_name(name)) == 1 + + # Now that we reached the minimum number of cookies to cleanup, + # add one more cookie to trigger the cleanup + record = r.DNSAddress( + name, const._TYPE_A, const._CLASS_IN, ttl_seconds, b"1", created=now + min_records_to_cleanup + ) + expected_expire_time = record.created + ttl_millis + cache.async_add_records([record]) + assert len(cache.async_entries_with_name(name)) == 1 + entry = next(iter(cache.async_entries_with_name(name))) + assert (entry.created + ttl_millis) == expected_expire_time + assert entry is record + + # Verify that the heap has been cleaned up + assert len(cache.async_entries_with_name(name)) == 1 + cache.async_expire(now) + + heap_copy = cache._expire_heap.copy() + heapify(heap_copy) + # Ensure heap order is maintained + assert cache._expire_heap == heap_copy + + # The heap should have been cleaned up + assert len(cache._expire_heap) == 1 + assert len(cache.async_entries_with_name(name)) == 1 + + entry = next(iter(cache.async_entries_with_name(name))) + assert entry is record + + assert (entry.created + ttl_millis) == expected_expire_time + + cache.async_expire(expected_expire_time) + assert not cache.async_entries_with_name(name), cache._expire_heap + + +@pytest.mark.asyncio +async def test_cache_heap_multi_name_cleanup() -> None: + """Test cleanup with multiple names.""" + cache = r.DNSCache() + # The heap should not be cleaned up when there are less than 100 expiration changes + min_records_to_cleanup = 100 + now = r.current_time_millis() + name = "heap.local." + name2 = "heap2.local." + ttl_seconds = 100 + ttl_millis = ttl_seconds * 1000 + + for i in range(min_records_to_cleanup): + record = r.DNSAddress(name, const._TYPE_A, const._CLASS_IN, ttl_seconds, b"1", created=now + i) + cache.async_add_records([record]) + expected_expire_time = record.created + ttl_millis + + for i in range(5): + record = r.DNSAddress( + name2, const._TYPE_A, const._CLASS_IN, ttl_seconds, bytes((i,)), created=now + i + ) + cache.async_add_records([record]) + + # ``_async_add`` rebuilds ``_expire_heap`` proactively when stale entries + # dominate (heap > 2x expirations), so the heap is already capped at + # ~one entry per unique record long before ``async_expire`` is called. + assert len(cache.async_entries_with_name(name)) == 1 + assert len(cache.async_entries_with_name(name2)) == 5 + + cache.async_expire(now) + # The heap and expirations should have been cleaned up + assert len(cache._expire_heap) == 1 + 5 + assert len(cache._expirations) == 1 + 5 + + cache.async_expire(expected_expire_time) + assert not cache.async_entries_with_name(name), cache._expire_heap + + +@pytest.mark.asyncio +async def test_cache_heap_pops_order() -> None: + """Test cache heap is popped in order.""" + cache = r.DNSCache() + # The heap should not be cleaned up when there are less than 100 expiration changes + min_records_to_cleanup = 100 + now = r.current_time_millis() + name = "heap.local." + name2 = "heap2.local." + ttl_seconds = 100 + + for i in range(min_records_to_cleanup): + record = r.DNSAddress(name, const._TYPE_A, const._CLASS_IN, ttl_seconds, b"1", created=now + i) + cache.async_add_records([record]) + + for i in range(5): + record = r.DNSAddress( + name2, const._TYPE_A, const._CLASS_IN, ttl_seconds, bytes((i,)), created=now + i + ) + cache.async_add_records([record]) + + # ``_async_add`` proactively rebuilds the heap when stale entries dominate, + # so the heap holds only one entry per unique record by this point. + assert len(cache.async_entries_with_name(name)) == 1 + assert len(cache.async_entries_with_name(name2)) == 5 + + start_ts = 0.0 + while cache._expire_heap: + ts, _ = heappop(cache._expire_heap) + assert ts >= start_ts + start_ts = ts + + +def _addr(name: str, idx: int, *, ttl: int = 120, created: float | None = None) -> r.DNSAddress: + """Build a DNSAddress with idx-derived payload for the bound/eviction tests.""" + return r.DNSAddress( + name, + const._TYPE_A, + const._CLASS_IN, + ttl, + bytes((idx & 0xFF, (idx >> 8) & 0xFF, 0, 1)), + created=r.current_time_millis() if created is None else created, + ) + + +def test_cache_size_is_bounded() -> None: + """A flood of unique-name records is capped at ``_MAX_CACHE_RECORDS``.""" + cache = r.DNSCache() + now = r.current_time_millis() + overflow = 1000 + flood_size = const._MAX_CACHE_RECORDS + overflow + + cache.async_add_records(_addr(f"flood-{i}.local.", i, created=now + i) for i in range(flood_size)) + + total = sum(len(store) for store in cache.cache.values()) + assert total == const._MAX_CACHE_RECORDS + assert cache._total_records == const._MAX_CACHE_RECORDS + # FIFO-ish: the earliest-created records (closest to expiration) get + # evicted first, so the names that remain are from the tail. + for i in range(overflow): + assert f"flood-{i}.local." not in cache.cache + for i in range(flood_size - overflow, flood_size): + assert f"flood-{i}.local." in cache.cache + + +def test_cache_eviction_empty_heap_returns_without_evicting() -> None: + """Eviction tolerates an empty ``_expire_heap`` (invariant-violation safety net).""" + cache = r.DNSCache() + # By the cache invariant every record in ``_total_records`` has a heap + # entry, so eviction should never see an empty heap. Force the broken + # state directly to pin the defensive behaviour: ``_async_evict_oldest`` + # returns without raising and the subsequent insert still lands. Since + # eviction can't free space, the counter is allowed to drift past the + # cap by exactly one — pinned so a future change to the recovery + # semantics (e.g., refusing the add or clamping) fails this test. + cache._total_records = const._MAX_CACHE_RECORDS + cache._expire_heap = [] + cache.async_add_records([_addr("post-empty.local.", 0)]) + assert "post-empty.local." in cache.cache + assert cache._total_records == const._MAX_CACHE_RECORDS + 1 + + +def test_cache_eviction_skips_stale_heap_entries() -> None: + """Eviction skips stale heap entries left by TTL re-adds.""" + cache = r.DNSCache() + now = r.current_time_millis() + cache.async_add_records( + _addr(f"stale-{i}.local.", i, created=now + i) for i in range(const._MAX_CACHE_RECORDS) + ) + assert cache._total_records == const._MAX_CACHE_RECORDS + + # Re-add the closest-to-expiration record with a longer TTL; the prior + # ``(when, record)`` tuple stays as stale, eviction must skip it. + victim_name = "stale-0.local." + cache.async_add_records([_addr(victim_name, 0, ttl=7200, created=now)]) + assert cache._total_records == const._MAX_CACHE_RECORDS + + cache.async_add_records([_addr("trigger.local.", 0xFFFF, created=now + const._MAX_CACHE_RECORDS)]) + assert cache._total_records == const._MAX_CACHE_RECORDS + assert victim_name in cache.cache + assert "stale-1.local." not in cache.cache + + +def test_cache_eviction_victim_shares_key_with_new_record() -> None: + """Inserting a record whose key collides with the eviction victim keeps it reachable.""" + cache = r.DNSCache() + now = r.current_time_millis() + cache.async_add_records( + _addr(f"filler-{i}.local.", i, created=now + 1000 + i) for i in range(const._MAX_CACHE_RECORDS - 1) + ) + + # Insert at "shared.local." with the earliest expiration so eviction + # picks it. ``_remove_key`` then deletes ``cache["shared.local."]``. + shared_key = "shared.local." + cache.async_add_records([_addr(shared_key, 0x0102, created=now)]) + assert cache._total_records == const._MAX_CACHE_RECORDS + + # Adding a new record under the SAME key: a pre-eviction-captured + # ``store`` would write into an orphaned dict; the fix re-resolves. + new_shared = _addr(shared_key, 0x0506, created=now + 999) + cache.async_add_records([new_shared]) + + assert shared_key in cache.cache, "new record orphaned: cache bucket missing" + assert new_shared in cache.cache[shared_key] + assert cache.async_get_unique(new_shared) == new_shared + total = sum(len(store) for store in cache.cache.values()) + assert total == cache._total_records + + +def test_cache_dnsnsec_at_cap_evicts_prior_record() -> None: + """A single DNSNsec arriving at the cap evicts one prior record and stays reachable.""" + cache = r.DNSCache() + now = r.current_time_millis() + cache.async_add_records( + _addr(f"fill-{i}.local.", i, created=now + i) for i in range(const._MAX_CACHE_RECORDS) + ) + assert cache._total_records == const._MAX_CACHE_RECORDS + + nsec = r.DNSNsec( + "nsec-arrival.local.", + const._TYPE_NSEC, + const._CLASS_IN, + 120, + "nsec-arrival.local.", + [const._TYPE_A], + ) + cache.async_add_records([nsec]) + + assert cache._total_records == const._MAX_CACHE_RECORDS + assert nsec in cache.cache[nsec.key] + # The earliest-created fill record is gone (FIFO-ish eviction). + assert "fill-0.local." not in cache.cache + + +def test_cache_dnsnsec_flood_is_bounded() -> None: + """DNSNsec records honour ``_MAX_CACHE_RECORDS`` (no bypass via the ``new`` flag).""" + cache = r.DNSCache() + overflow = 100 + cache.async_add_records( + r.DNSNsec( + f"nsec-{i}.local.", + const._TYPE_NSEC, + const._CLASS_IN, + 120, + f"nsec-{i}.local.", + [const._TYPE_A], + ) + for i in range(const._MAX_CACHE_RECORDS + overflow) + ) + assert cache._total_records == const._MAX_CACHE_RECORDS + total = sum(len(store) for store in cache.cache.values()) + assert total == const._MAX_CACHE_RECORDS + + +def test_cache_re_add_flood_does_not_grow_heap_unbounded() -> None: + """Replaying cached records with shifting TTLs cannot grow ``_expire_heap`` unbounded.""" + cache = r.DNSCache() + now = r.current_time_millis() + # Stay below the cache cap so eviction never fires; the attack here is + # heap growth via re-add, not cap saturation. Clear the + # ``_MIN_SCHEDULED_RECORD_EXPIRATION`` floor so the rebuild engages. + record_count = 200 + cache.async_add_records(_addr(f"flood-{i}.local.", i, created=now) for i in range(record_count)) + assert cache._total_records == record_count + + # 10 cycles x ``record_count`` stale pushes each. Without + # ``_maybe_rebuild_heap`` firing inside ``_async_add``, the heap would + # grow to ~11 x record_count. + for cycle in range(10): + cache.async_add_records( + _addr(f"flood-{i}.local.", i, ttl=7200 + cycle, created=now) for i in range(record_count) + ) + + # Heap is bounded near the rebuild threshold; ``+ record_count`` of slack + # to stay resilient to where in a re-add cycle the rebuild last fired. + assert len(cache._expire_heap) <= 2 * len(cache._expirations) + record_count + assert cache._total_records == record_count + + +def test_cache_eviction_decrements_total_records() -> None: + """Natural removal (goodbyes, expirations) keeps ``_total_records`` in sync.""" + cache = r.DNSCache() + now = r.current_time_millis() + records = [_addr(f"sync-{i}.local.", i, created=now) for i in range(50)] + cache.async_add_records(records) + assert cache._total_records == 50 + + cache.async_remove_records(records[:20]) + assert cache._total_records == 30 + + cache.async_expire(now + (200 * 1000)) + assert cache._total_records == 0 + assert not cache.cache + + +def test_cache_total_records_invariant_under_mixed_ops() -> None: + """``_total_records`` stays equal to the sum of bucket sizes across all touched paths.""" + cache = r.DNSCache() + now = r.current_time_millis() + + def actual() -> int: + return sum(len(store) for store in cache.cache.values()) + + addrs = [_addr(f"mix-{i}.local.", i, created=now + i) for i in range(20)] + cache.async_add_records(addrs) + assert cache._total_records == actual() == 20 + + # Re-add of an identical record: no increment. + cache.async_add_records([addrs[0]]) + assert cache._total_records == actual() == 20 + + # DNSService writes service_cache too — counter still matches cache size. + svc = r.DNSService("svc.local.", const._TYPE_SRV, const._CLASS_IN, 120, 0, 0, 80, "host.local.") + cache.async_add_records([svc]) + assert cache._total_records == actual() == 21 + cache.async_remove_records([svc]) + assert cache._total_records == actual() == 20 + + # DNSNsec is stored but excluded from the "new" return; counter tracks it anyway. + nsec = r.DNSNsec("nsec.local.", const._TYPE_NSEC, const._CLASS_IN, 120, "nsec.local.", [const._TYPE_A]) + cache.async_add_records([nsec]) + assert cache._total_records == actual() == 21 + cache.async_remove_records([nsec]) + assert cache._total_records == actual() == 20 + + # Shared-key insert/remove: emptying the bucket drops the cache key but + # counter decrements only by the records that left. + shared_a = _addr("shared.local.", 0x0101, created=now) + shared_b = _addr("shared.local.", 0x0202, created=now) + cache.async_add_records([shared_a, shared_b]) + assert cache._total_records == actual() == 22 + cache.async_remove_records([shared_a, shared_b]) + assert cache._total_records == actual() == 20 + assert "shared.local." not in cache.cache + + cache.async_expire(now + (200 * 1000)) + assert cache._total_records == actual() == 0 + assert not cache.cache + + # Full-cap eviction loop: counter never grows past the cap, never drifts. + cap_records = [_addr(f"cap-{i}.local.", i, created=now + i) for i in range(const._MAX_CACHE_RECORDS + 50)] + for rec in cap_records: + cache.async_add_records([rec]) + assert cache._total_records == actual() + assert cache._total_records == const._MAX_CACHE_RECORDS diff --git a/tests/test_circular_imports.py b/tests/test_circular_imports.py new file mode 100644 index 000000000..74ed1f124 --- /dev/null +++ b/tests/test_circular_imports.py @@ -0,0 +1,32 @@ +"""Test to check for circular imports.""" + +from __future__ import annotations + +import asyncio +import sys + +import pytest + + +@pytest.mark.asyncio +@pytest.mark.timeout(30) # cloud can take > 9s +@pytest.mark.parametrize( + "module", + [ + "zeroconf", + "zeroconf.asyncio", + "zeroconf._protocol.incoming", + "zeroconf._protocol.outgoing", + "zeroconf.const", + "zeroconf._logger", + "zeroconf._transport", + "zeroconf._record_update", + "zeroconf._services.browser", + "zeroconf._services.info", + ], +) +async def test_circular_imports(module: str) -> None: + """Check that components can be imported without circular imports.""" + process = await asyncio.create_subprocess_exec(sys.executable, "-c", f"import {module}") + await process.communicate() + assert process.returncode == 0 diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 000000000..1bc85b1e1 --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,978 @@ +"""Unit tests for zeroconf._core""" + +from __future__ import annotations + +import asyncio +import logging +import os +import socket +import sys +import threading +import time +import unittest +import unittest.mock +import warnings +from pathlib import Path +from typing import cast +from unittest.mock import AsyncMock, Mock, patch + +import pytest + +import zeroconf as r +from zeroconf import NotRunningException, Zeroconf, _listener, const, current_time_millis +from zeroconf._listener import _TC_DELAY_RANDOM_INTERVAL, AsyncListener, _WrappedTransport +from zeroconf._protocol.incoming import DNSIncoming +from zeroconf.asyncio import AsyncZeroconf + +from . import _backdate_cache, _clear_cache, _inject_response, _wait_for_start, has_working_ipv6 + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +def threadsafe_query( + zc: Zeroconf, + protocol: AsyncListener, + msg: DNSIncoming, + addr: str, + port: int, + transport: _WrappedTransport, + v6_flow_scope: tuple[()] | tuple[int, int], +) -> None: + async def make_query(): + protocol.handle_query_or_defer(msg, addr, port, transport, v6_flow_scope) + + assert zc.loop is not None + asyncio.run_coroutine_threadsafe(make_query(), zc.loop).result() + + +class Framework(unittest.TestCase): + def test_launch_and_close(self): + rv = r.Zeroconf(interfaces=r.InterfaceChoice.All) + rv.close() + rv = r.Zeroconf(interfaces=r.InterfaceChoice.Default) + rv.close() + + def test_launch_and_close_context_manager(self): + with r.Zeroconf(interfaces=r.InterfaceChoice.All) as rv: + assert rv.done is False + assert rv.done is True + + with r.Zeroconf(interfaces=r.InterfaceChoice.Default) as rv: # type: ignore[unreachable] + assert rv.done is False + assert rv.done is True + + def test_launch_and_close_unicast(self): + rv = r.Zeroconf(interfaces=r.InterfaceChoice.All, unicast=True) + rv.close() + rv = r.Zeroconf(interfaces=r.InterfaceChoice.Default, unicast=True) + rv.close() + + def test_close_multiple_times(self): + rv = r.Zeroconf(interfaces=r.InterfaceChoice.Default) + rv.close() + rv.close() + + def test_close_releases_owned_event_loop(self): + """Closing a Zeroconf that started its own loop thread closes that loop. + + Regression test for issue #1589 — without loop.close(), the selector + (epoll on Linux) and its self-pipe sockets stay open across each + Zeroconf construct/close cycle and the process eventually exhausts + its FD limit. + """ + rv = r.Zeroconf(interfaces=["127.0.0.1"]) + loop = rv.loop + assert loop is not None + assert loop.is_running() + rv.close() + assert loop.is_closed() + + @unittest.skipUnless(sys.platform.startswith("linux"), "Requires /proc//fd") + @unittest.skipUnless(Path(f"/proc/{os.getpid()}/fd").is_dir(), "/proc//fd not available") + def test_close_does_not_leak_file_descriptors(self): + """Tight loops of Zeroconf()/close() do not leak FDs (issue #1589).""" + fd_dir = Path(f"/proc/{os.getpid()}/fd") + + def _fd_count() -> int: + return sum(1 for _ in fd_dir.iterdir()) + + # Warm-up cycle so any one-shot import-time FDs land before measuring. + r.Zeroconf(interfaces=["127.0.0.1"]).close() + baseline = _fd_count() + for _ in range(10): + r.Zeroconf(interfaces=["127.0.0.1"]).close() + # Allow tiny slack for unrelated FDs the test harness may open + # (e.g. coverage), but reject the per-cycle linear growth pattern + # the bug produced (~3 FDs per cycle, so >=30 over 10 cycles). + assert _fd_count() - baseline < 10 + + @unittest.skipIf(not has_working_ipv6(), "Requires IPv6") + @unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") + def test_launch_and_close_v4_v6(self): + rv = r.Zeroconf(interfaces=r.InterfaceChoice.All, ip_version=r.IPVersion.All) + rv.close() + with warnings.catch_warnings(record=True) as warned: + rv = r.Zeroconf(interfaces=r.InterfaceChoice.Default, ip_version=r.IPVersion.All) + rv.close() + first_warning = warned[0] + assert "IPv6 multicast requests can't be sent using default interface" in str( + first_warning.message + ) + + @unittest.skipIf(not has_working_ipv6(), "Requires IPv6") + @unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") + def test_launch_and_close_v6_only(self): + rv = r.Zeroconf(interfaces=r.InterfaceChoice.All, ip_version=r.IPVersion.V6Only) + rv.close() + with warnings.catch_warnings(record=True) as warned: + rv = r.Zeroconf(interfaces=r.InterfaceChoice.Default, ip_version=r.IPVersion.V6Only) + rv.close() + first_warning = warned[0] + assert "IPv6 multicast requests can't be sent using default interface" in str( + first_warning.message + ) + + @unittest.skipIf(sys.platform == "darwin", reason="apple_p2p failure path not testable on mac") + def test_launch_and_close_apple_p2p_not_mac(self): + with pytest.raises(RuntimeError): + r.Zeroconf(apple_p2p=True) + + @unittest.skipIf(sys.platform != "darwin", reason="apple_p2p happy path only testable on mac") + def test_launch_and_close_apple_p2p_on_mac(self): + rv = r.Zeroconf(apple_p2p=True) + rv.close() + + def test_use_asyncio_false_forces_thread_when_loop_running(self): + """use_asyncio=False starts a thread even with a running event loop.""" + + async def run() -> r.Zeroconf: + return r.Zeroconf(interfaces=["127.0.0.1"], use_asyncio=False) + + loop = asyncio.new_event_loop() + zc: r.Zeroconf | None = None + try: + zc = loop.run_until_complete(run()) + assert zc._loop_thread is not None + assert zc.loop is not loop + finally: + if zc is not None: + zc.close() + loop.close() + + def test_use_asyncio_true_requires_running_loop(self): + """use_asyncio=True without a running loop raises RuntimeError.""" + with pytest.raises(RuntimeError, match="requires a running asyncio event loop"): + r.Zeroconf(interfaces=["127.0.0.1"], use_asyncio=True) + + def test_use_asyncio_default_starts_thread_without_loop(self): + """use_asyncio=None (default) keeps the historic auto-detect behavior.""" + zc = r.Zeroconf(interfaces=["127.0.0.1"]) + try: + assert zc._loop_thread is not None + finally: + zc.close() + + def test_async_updates_from_response(self): + def mock_incoming_msg( + service_state_change: r.ServiceStateChange, + ) -> r.DNSIncoming: + ttl = 120 + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + + if service_state_change == r.ServiceStateChange.Updated: + generated.add_answer_at_time( + r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + service_text, + ), + 0, + ) + return r.DNSIncoming(generated.packets()[0]) + + if service_state_change == r.ServiceStateChange.Removed: + ttl = 0 + + generated.add_answer_at_time( + r.DNSPointer(service_type, const._TYPE_PTR, const._CLASS_IN, ttl, service_name), + 0, + ) + generated.add_answer_at_time( + r.DNSService( + service_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + 0, + 0, + 80, + service_server, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSText( + service_name, + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + service_text, + ), + 0, + ) + generated.add_answer_at_time( + r.DNSAddress( + service_server, + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + socket.inet_aton(service_address), + ), + 0, + ) + + return r.DNSIncoming(generated.packets()[0]) + + def mock_split_incoming_msg( + service_state_change: r.ServiceStateChange, + ) -> r.DNSIncoming: + """Mock an incoming message for the case where the packet is split.""" + ttl = 120 + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSAddress( + service_server, + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + socket.inet_aton(service_address), + ), + 0, + ) + generated.add_answer_at_time( + r.DNSService( + service_name, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + ttl, + 0, + 0, + 80, + service_server, + ), + 0, + ) + return r.DNSIncoming(generated.packets()[0]) + + service_name = "name._type._tcp.local." + service_type = "_type._tcp.local." + service_server = "ash-2.local." + service_text = b"path=/~paulsm/" + service_address = "10.0.1.2" + + zeroconf = r.Zeroconf(interfaces=["127.0.0.1"]) + + try: + # service added + _inject_response(zeroconf, mock_incoming_msg(r.ServiceStateChange.Added)) + dns_text = zeroconf.cache.get_by_details(service_name, const._TYPE_TXT, const._CLASS_IN) + assert dns_text is not None + assert cast(r.DNSText, dns_text).text == service_text # service_text is b'path=/~paulsm/' + all_dns_text = zeroconf.cache.get_all_by_details(service_name, const._TYPE_TXT, const._CLASS_IN) + assert [dns_text] == all_dns_text + + # https://tools.ietf.org/html/rfc6762#section-10.2 + # Instead of merging this new record additively into the cache in addition + # to any previous records with the same name, rrtype, and rrclass, + # all old records with that name, rrtype, and rrclass that were received + # more than one second ago are declared invalid, + # and marked to expire from the cache in one second. + _backdate_cache(zeroconf) + + # service updated. currently only text record can be updated + service_text = b"path=/~humingchun/" + _inject_response(zeroconf, mock_incoming_msg(r.ServiceStateChange.Updated)) + dns_text = zeroconf.cache.get_by_details(service_name, const._TYPE_TXT, const._CLASS_IN) + assert dns_text is not None + assert cast(r.DNSText, dns_text).text == service_text # service_text is b'path=/~humingchun/' + + _backdate_cache(zeroconf) + + # The split message only has a SRV and A record. + # This should not evict TXT records from the cache + _inject_response(zeroconf, mock_split_incoming_msg(r.ServiceStateChange.Updated)) + _backdate_cache(zeroconf) + dns_text = zeroconf.cache.get_by_details(service_name, const._TYPE_TXT, const._CLASS_IN) + assert dns_text is not None + assert cast(r.DNSText, dns_text).text == service_text # service_text is b'path=/~humingchun/' + + # service removed + _inject_response(zeroconf, mock_incoming_msg(r.ServiceStateChange.Removed)) + dns_text = zeroconf.cache.get_by_details(service_name, const._TYPE_TXT, const._CLASS_IN) + assert dns_text is not None + assert dns_text.is_expired(current_time_millis() + 1000) + + finally: + zeroconf.close() + + +def test_generate_service_query_set_qu_bit(): + """Test generate_service_query sets the QU bit.""" + + zeroconf_registrar = Zeroconf(interfaces=["127.0.0.1"]) + desc = {"path": "/~paulsm/"} + type_ = "._hap._tcp.local." + registration_name = "this-host-is-not-used._hap._tcp.local." + info = r.ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + out = zeroconf_registrar.generate_service_query(info) + assert out.questions[0].unicast is True + zeroconf_registrar.close() + + +def test_invalid_packets_ignored_and_does_not_cause_loop_exception(): + """Ensure an invalid packet cannot cause the loop to collapse.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + generated = r.DNSOutgoing(0) + packet = generated.packets()[0] + packet = packet[:8] + b"deadbeef" + packet[8:] + parsed = r.DNSIncoming(packet) + assert parsed.valid is False + + # Invalid Packet + mock_out = unittest.mock.Mock() + mock_out.packets = lambda: [packet] + zc.send(mock_out) + + # Invalid oversized packet + mock_out = unittest.mock.Mock() + mock_out.packets = lambda: [packet * 1000] + zc.send(mock_out) + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + entry = r.DNSText( + "didnotcrashincoming._crash._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 500, + b"path=/~paulsm/", + ) + assert isinstance(entry, r.DNSText) + assert isinstance(entry, r.DNSRecord) + assert isinstance(entry, r.DNSEntry) + + generated.add_answer_at_time(entry, 0) + zc.send(generated) + time.sleep(0.2) + zc.close() + assert zc.cache.get(entry) is not None + + +def test_goodbye_all_services(): + """Verify generating the goodbye query does not change with time.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + out = zc.generate_unregister_all_services() + assert out is None + type_ = "_http._tcp.local." + registration_name = f"xxxyyy.{type_}" + desc = {"path": "/~paulsm/"} + info = r.ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + out = zc.generate_unregister_all_services() + assert out is not None + first_packet = out.packets() + zc.registry.async_add(info) + out2 = zc.generate_unregister_all_services() + assert out2 is not None + second_packet = out.packets() + assert second_packet == first_packet + + # Verify the registry is empty + out3 = zc.generate_unregister_all_services() + assert out3 is None + assert zc.registry.async_get_service_infos() == [] + + zc.close() + + +def test_register_service_with_custom_ttl(quick_timing: None) -> None: + """Test a registering a service with a custom ttl.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # start a browser + type_ = "_homeassistant._tcp.local." + name = "MyTestHome" + info_service = r.ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-90.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + zc.register_service(info_service, ttl=3000) + record = zc.cache.get(info_service.dns_pointer()) + assert record is not None + assert record.ttl == 3000 + zc.close() + + +def test_logging_packets(caplog: pytest.LogCaptureFixture, quick_timing: None) -> None: + """Test packets are only logged with debug logging.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # start a browser + type_ = "_logging._tcp.local." + name = "TLD" + info_service = r.ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-90.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + caplog.clear() + zc.register_service(info_service, ttl=3000) + assert "Sending to" in caplog.text + record = zc.cache.get(info_service.dns_pointer()) + assert record is not None + assert record.ttl == 3000 + logging.getLogger("zeroconf").setLevel(logging.INFO) + caplog.clear() + zc.unregister_service(info_service) + assert "Sending to" not in caplog.text + logging.getLogger("zeroconf").setLevel(logging.DEBUG) + + zc.close() + + +def test_get_service_info_failure_path(): + """Verify get_service_info return None when the underlying call returns False.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + assert zc.get_service_info("_neverused._tcp.local.", "xneverused._neverused._tcp.local.", 10) is None + zc.close() + + +@pytest.mark.skipif( + sys.platform == "win32", + reason="multicast loopback on the 127.0.0.1-only socket is unreliable on GH Actions Windows runners", +) +def test_sending_unicast(): + """Test sending unicast response.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + entry = r.DNSText( + "didnotcrashincoming._crash._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 500, + b"path=/~paulsm/", + ) + generated.add_answer_at_time(entry, 0) + zc.send(generated, "2001:db8::1", const._MDNS_PORT) # https://www.iana.org/go/rfc3849 + time.sleep(0.2) + assert zc.cache.get(entry) is None + + zc.send(generated, "198.51.100.0", const._MDNS_PORT) # Documentation (TEST-NET-2) + time.sleep(0.2) + assert zc.cache.get(entry) is None + + zc.send(generated) + for _ in range(10): + time.sleep(0.05) + if zc.cache.get(entry) is not None: + break + + assert zc.cache.get(entry) is not None + + zc.close() + + +def test_tc_bit_defers(): + zc = Zeroconf(interfaces=["127.0.0.1"]) + _wait_for_start(zc) + type_ = "_tcbitdefer._tcp.local." + name = "knownname" + name2 = "knownname2" + name3 = "knownname3" + + registration_name = f"{name}.{type_}" + registration2_name = f"{name2}.{type_}" + registration3_name = f"{name3}.{type_}" + + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + server_name2 = "ash-3.local." + server_name3 = "ash-4.local." + + info = r.ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = r.ServiceInfo( + type_, + registration2_name, + 80, + 0, + 0, + desc, + server_name2, + addresses=[socket.inet_aton("10.0.1.2")], + ) + info3 = r.ServiceInfo( + type_, + registration3_name, + 80, + 0, + 0, + desc, + server_name3, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + zc.registry.async_add(info2) + zc.registry.async_add(info3) + + protocol = zc.engine.protocols[0] + now = r.current_time_millis() + _clear_cache(zc) + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + for _ in range(300): + # Add so many answers we end up with another packet + generated.add_answer_at_time(info.dns_pointer(), now) + generated.add_answer_at_time(info2.dns_pointer(), now) + generated.add_answer_at_time(info3.dns_pointer(), now) + packets = generated.packets() + assert len(packets) == 4 + expected_deferred = [] + source_ip = "203.0.113.13" + + next_packet = r.DNSIncoming(packets.pop(0)) + expected_deferred.append(next_packet) + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._deferred[source_ip] == expected_deferred + assert source_ip in protocol._timers + + next_packet = r.DNSIncoming(packets.pop(0)) + expected_deferred.append(next_packet) + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._deferred[source_ip] == expected_deferred + assert source_ip in protocol._timers + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._deferred[source_ip] == expected_deferred + assert source_ip in protocol._timers + + next_packet = r.DNSIncoming(packets.pop(0)) + expected_deferred.append(next_packet) + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._deferred[source_ip] == expected_deferred + assert source_ip in protocol._timers + + next_packet = r.DNSIncoming(packets.pop(0)) + expected_deferred.append(next_packet) + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert source_ip not in protocol._deferred + assert source_ip not in protocol._timers + + # unregister + zc.unregister_service(info) + zc.close() + + +def test_tc_bit_defers_last_response_missing(): + zc = Zeroconf(interfaces=["127.0.0.1"]) + _wait_for_start(zc) + type_ = "_knowndefer._tcp.local." + name = "knownname" + name2 = "knownname2" + name3 = "knownname3" + + registration_name = f"{name}.{type_}" + registration2_name = f"{name2}.{type_}" + registration3_name = f"{name3}.{type_}" + + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + server_name2 = "ash-3.local." + server_name3 = "ash-4.local." + + info = r.ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = r.ServiceInfo( + type_, + registration2_name, + 80, + 0, + 0, + desc, + server_name2, + addresses=[socket.inet_aton("10.0.1.2")], + ) + info3 = r.ServiceInfo( + type_, + registration3_name, + 80, + 0, + 0, + desc, + server_name3, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + zc.registry.async_add(info2) + zc.registry.async_add(info3) + + protocol = zc.engine.protocols[0] + now = r.current_time_millis() + _clear_cache(zc) + source_ip = "203.0.113.12" + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + for _ in range(300): + # Add so many answers we end up with another packet + generated.add_answer_at_time(info.dns_pointer(), now) + generated.add_answer_at_time(info2.dns_pointer(), now) + generated.add_answer_at_time(info3.dns_pointer(), now) + packets = generated.packets() + assert len(packets) == 4 + expected_deferred = [] + + # Pin per-packet delay to the minimum so each successive fire_at lands + # before the deadline established by the first packet — keeps the + # timer-replacement assertions deterministic under bounded TC-deferral. + min_delay_ms = _TC_DELAY_RANDOM_INTERVAL[0] + with patch("zeroconf._listener.random.randint", return_value=min_delay_ms): + next_packet = r.DNSIncoming(packets.pop(0)) + expected_deferred.append(next_packet) + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._deferred[source_ip] == expected_deferred + timer1 = protocol._timers[source_ip] + + next_packet = r.DNSIncoming(packets.pop(0)) + expected_deferred.append(next_packet) + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._deferred[source_ip] == expected_deferred + timer2 = protocol._timers[source_ip] + assert timer1.cancelled() + assert timer2 != timer1 + + # Send the same packet again to similar multi interfaces + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._deferred[source_ip] == expected_deferred + assert source_ip in protocol._timers + timer3 = protocol._timers[source_ip] + assert not timer3.cancelled() + assert timer3 == timer2 + + next_packet = r.DNSIncoming(packets.pop(0)) + expected_deferred.append(next_packet) + threadsafe_query(zc, protocol, next_packet, source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._deferred[source_ip] == expected_deferred + assert source_ip in protocol._timers + timer4 = protocol._timers[source_ip] + assert timer3.cancelled() + assert timer4 != timer3 + + for _ in range(8): + time.sleep(0.1) + if source_ip not in protocol._timers and source_ip not in protocol._deferred: + break + + assert source_ip not in protocol._deferred + assert source_ip not in protocol._timers + + # unregister + zc.registry.async_remove(info) + zc.close() + + +def test_tc_bit_defer_window_is_bounded(): + """TC-deferral assembly window must not slide past first_arrival + max delay.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + _wait_for_start(zc) + type_ = "_boundeddefer._tcp.local." + registration_name = f"knownname.{type_}" + + info = r.ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + + protocol = zc.engine.protocols[0] + now_ms = r.current_time_millis() + _clear_cache(zc) + source_ip = "203.0.113.99" + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + generated.add_question(r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN)) + for _ in range(300): + generated.add_answer_at_time(info.dns_pointer(), now_ms) + packets = generated.packets() + assert len(packets) >= 3 + + # Pin the per-packet delay at its maximum so any subsequent reset would + # land past the deadline established by the first packet. + max_delay_ms = _TC_DELAY_RANDOM_INTERVAL[1] + with patch("zeroconf._listener.random.randint", return_value=max_delay_ms): + threadsafe_query(zc, protocol, r.DNSIncoming(packets[0]), source_ip, const._MDNS_PORT, Mock(), ()) + first_when = protocol._timers[source_ip].when() + + for raw in packets[1:-1]: + threadsafe_query(zc, protocol, r.DNSIncoming(raw), source_ip, const._MDNS_PORT, Mock(), ()) + assert protocol._timers[source_ip].when() <= first_when + + zc.registry.async_remove(info) + zc.close() + + +def _make_distinct_tc_packets(count: int, name_prefix: str = "q") -> list[bytes]: + """Generate ``count`` byte-distinct TC-flagged query packets for flood inputs.""" + packets = [] + for i in range(count): + out = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_TC) + out.add_question(r.DNSQuestion(f"{name_prefix}{i}._tcp.local.", const._TYPE_PTR, const._CLASS_IN)) + packets.append(out.packets()[0]) + return packets + + +def _synthetic_source_ip(i: int) -> str: + """Distinct synthetic source IPs from the documentation ranges.""" + if i < 256: + return f"203.0.113.{i}" + if i < 512: + return f"198.51.100.{i - 256}" + return f"192.0.2.{i - 512}" + + +def test_tc_bit_per_addr_queue_is_bounded(quick_timing: None) -> None: + """Per-addr deferred queue must not grow past ``_MAX_DEFERRED_PER_ADDR``.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + _wait_for_start(zc) + protocol = zc.engine.protocols[0] + source_ip = "203.0.113.21" + + extra = 4 + packets = _make_distinct_tc_packets(const._MAX_DEFERRED_PER_ADDR + extra) + + # Push the reassembly timer well past any possible test runtime + # so the bound under test is the only thing that can drop entries. + with patch.object(_listener, "_TC_DELAY_RANDOM_INTERVAL", (60_000, 60_001)): + for raw in packets: + threadsafe_query(zc, protocol, r.DNSIncoming(raw), source_ip, const._MDNS_PORT, Mock(), ()) + + assert len(protocol._deferred[source_ip]) == const._MAX_DEFERRED_PER_ADDR + # Last ``extra`` packets must have been dropped, not displaced; the + # earlier ``_MAX_DEFERRED_PER_ADDR`` entries are the ones retained. + retained = [incoming.data for incoming in protocol._deferred[source_ip]] + assert retained == packets[: const._MAX_DEFERRED_PER_ADDR] + + zc.close() + + +def test_tc_bit_total_addrs_is_bounded(quick_timing: None) -> None: + """Distinct addrs with deferred state must not exceed ``_MAX_DEFERRED_ADDRS``.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + _wait_for_start(zc) + protocol = zc.engine.protocols[0] + + raw = _make_distinct_tc_packets(1)[0] + extra = 4 + addrs = [_synthetic_source_ip(i) for i in range(const._MAX_DEFERRED_ADDRS + extra)] + + # Push the reassembly timer well past any possible test runtime + # so the bound under test is the only thing that can drop entries; + # without this, PyPy / slow runners can fire timers between the + # last enqueue and the assertion. + with patch.object(_listener, "_TC_DELAY_RANDOM_INTERVAL", (60_000, 60_001)): + for source_ip in addrs: + threadsafe_query(zc, protocol, r.DNSIncoming(raw), source_ip, const._MDNS_PORT, Mock(), ()) + + assert len(protocol._deferred) == const._MAX_DEFERRED_ADDRS + assert len(protocol._timers) == const._MAX_DEFERRED_ADDRS + + zc.close() + + +def test_tc_bit_eviction_drops_oldest_addr(quick_timing: None) -> None: + """Adding a new addr at capacity drops the oldest insertion (FIFO).""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + _wait_for_start(zc) + protocol = zc.engine.protocols[0] + + raw = _make_distinct_tc_packets(1)[0] + fillers = [_synthetic_source_ip(i) for i in range(const._MAX_DEFERRED_ADDRS)] + new_addr = _synthetic_source_ip(const._MAX_DEFERRED_ADDRS) + oldest = fillers[0] + + with patch.object(_listener, "_TC_DELAY_RANDOM_INTERVAL", (60_000, 60_001)): + for source_ip in fillers: + threadsafe_query(zc, protocol, r.DNSIncoming(raw), source_ip, const._MDNS_PORT, Mock(), ()) + assert len(protocol._deferred) == const._MAX_DEFERRED_ADDRS + assert oldest in protocol._deferred + + # One more distinct addr must evict the oldest insertion-order entry. + threadsafe_query(zc, protocol, r.DNSIncoming(raw), new_addr, const._MDNS_PORT, Mock(), ()) + assert oldest not in protocol._deferred + assert oldest not in protocol._timers + assert new_addr in protocol._deferred + assert len(protocol._deferred) == const._MAX_DEFERRED_ADDRS + + zc.close() + + +@pytest.mark.asyncio +async def test_open_close_twice_from_async() -> None: + """Test we can close twice from a coroutine when using Zeroconf. + + Ideally callers switch to using AsyncZeroconf, however there will + be a period where they still call the sync wrapper that we want + to ensure will not deadlock on shutdown. + + This test is expected to throw warnings about tasks being destroyed + since we force shutdown right away since we don't want to block + callers event loops and since they aren't using the AsyncZeroconf + version they won't yield with an await like async_close we don't + have much choice but to force things down. + """ + zc = Zeroconf(interfaces=["127.0.0.1"]) + zc.close() + zc.close() + await asyncio.sleep(0) + + +@pytest.mark.asyncio +async def test_multiple_sync_instances_stared_from_async_close(): + """Test we can shutdown multiple sync instances from async.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + zc2 = Zeroconf(interfaces=["127.0.0.1"]) + assert zc.loop is not None + assert zc2.loop is not None + + assert zc.loop == zc2.loop + zc.close() + assert zc.loop.is_running() + zc2.close() + assert zc2.loop.is_running() + + zc3 = Zeroconf(interfaces=["127.0.0.1"]) + assert zc3.loop == zc2.loop + + zc3.close() + assert zc3.loop.is_running() + + await asyncio.sleep(0) + + +def test_shutdown_while_register_in_process(quick_timing: None) -> None: + """Test we can shutdown while registering a service in another thread.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # start a browser + type_ = "_homeassistant._tcp.local." + name = "MyTestHome" + info_service = r.ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-90.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + def _background_register(): + zc.register_service(info_service) + + bgthread = threading.Thread(target=_background_register, daemon=True) + bgthread.start() + time.sleep(0.3) + + zc.close() + bgthread.join() + + +@pytest.mark.asyncio +@patch("zeroconf._core.AsyncEngine._async_setup", new_callable=AsyncMock) +async def test_event_loop_blocked(mock_start): + """Test we raise NotRunningException when waiting for startup that times out.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + try: + with pytest.raises(NotRunningException): + await aiozc.zeroconf.async_wait_for_start(timeout=0) + assert aiozc.zeroconf.started is False + finally: + await aiozc.async_close() diff --git a/tests/test_dns.py b/tests/test_dns.py new file mode 100644 index 000000000..0af88c1a6 --- /dev/null +++ b/tests/test_dns.py @@ -0,0 +1,527 @@ +"""Unit tests for zeroconf._dns.""" + +from __future__ import annotations + +import logging +import os +import socket +import unittest.mock + +import pytest + +import zeroconf as r +from zeroconf import DNSHinfo, DNSText, ServiceInfo, const, current_time_millis +from zeroconf._dns import DNSRRSet + +from . import has_working_ipv6 + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +class TestDunder(unittest.TestCase): + def test_dns_text_repr(self): + # There was an issue on Python 3 that prevented DNSText's repr + # from working when the text was longer than 10 bytes + text = DNSText("irrelevant", 0, 0, 0, b"12345678901") + repr(text) + + text = DNSText("irrelevant", 0, 0, 0, b"123") + repr(text) + + def test_dns_hinfo_repr_eq(self): + hinfo = DNSHinfo("irrelevant", const._TYPE_HINFO, 0, 0, "cpu", "os") + assert hinfo == hinfo + repr(hinfo) + + def test_dns_pointer_repr(self): + pointer = r.DNSPointer("irrelevant", const._TYPE_PTR, const._CLASS_IN, const._DNS_OTHER_TTL, "123") + repr(pointer) + + @unittest.skipIf(not has_working_ipv6(), "Requires IPv6") + @unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") + def test_dns_address_repr(self): + address = r.DNSAddress("irrelevant", const._TYPE_SOA, const._CLASS_IN, 1, b"a") + assert repr(address).endswith("b'a'") + + address_ipv4 = r.DNSAddress( + "irrelevant", + const._TYPE_SOA, + const._CLASS_IN, + 1, + socket.inet_pton(socket.AF_INET, "127.0.0.1"), + ) + assert repr(address_ipv4).endswith("127.0.0.1") + + address_ipv6 = r.DNSAddress( + "irrelevant", + const._TYPE_SOA, + const._CLASS_IN, + 1, + socket.inet_pton(socket.AF_INET6, "::1"), + ) + assert repr(address_ipv6).endswith("::1") + + def test_dns_question_repr(self): + question = r.DNSQuestion("irrelevant", const._TYPE_SRV, const._CLASS_IN | const._CLASS_UNIQUE) + repr(question) + assert (question != question) is False + + def test_dns_service_repr(self): + service = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "a", + ) + repr(service) + + def test_dns_record_abc(self): + record = r.DNSRecord("irrelevant", const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL) + self.assertRaises(r.AbstractMethodException, record.__eq__, record) + with pytest.raises((r.AbstractMethodException, TypeError)): + record.write(None) # type: ignore[arg-type] + + def test_service_info_dunder(self): + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + b"", + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + assert (info != info) is False + repr(info) + + def test_service_info_text_properties_not_given(self): + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + info = ServiceInfo( + type_=type_, + name=registration_name, + addresses=[socket.inet_aton("10.0.1.2")], + port=80, + server="ash-2.local.", + ) + + assert isinstance(info.text, bytes) + repr(info) + + def test_dns_outgoing_repr(self): + dns_outgoing = r.DNSOutgoing(const._FLAGS_QR_QUERY) + repr(dns_outgoing) + + def test_dns_record_is_expired(self): + record = r.DNSRecord("irrelevant", const._TYPE_SRV, const._CLASS_IN, 8) + now = current_time_millis() + assert record.is_expired(now) is False + assert record.is_expired(now + (8 / 2 * 1000)) is False + assert record.is_expired(now + (8 * 1000)) is True + + def test_dns_record_is_stale(self): + record = r.DNSRecord("irrelevant", const._TYPE_SRV, const._CLASS_IN, 8) + now = current_time_millis() + assert record.is_stale(now) is False + assert record.is_stale(now + (8 / 4.1 * 1000)) is False + assert record.is_stale(now + (8 / 1.9 * 1000)) is True + assert record.is_stale(now + (8 * 1000)) is True + + def test_dns_record_is_recent(self): + now = current_time_millis() + record = r.DNSRecord("irrelevant", const._TYPE_SRV, const._CLASS_IN, 8) + assert record.is_recent(now + (8 / 4.2 * 1000)) is True + assert record.is_recent(now + (8 / 3 * 1000)) is False + assert record.is_recent(now + (8 / 2 * 1000)) is False + assert record.is_recent(now + (8 * 1000)) is False + + +def test_dns_question_hashablity(): + """Test DNSQuestions are hashable.""" + + record1 = r.DNSQuestion("irrelevant", const._TYPE_A, const._CLASS_IN) + record2 = r.DNSQuestion("irrelevant", const._TYPE_A, const._CLASS_IN) + + record_set = {record1, record2} + assert len(record_set) == 1 + + record_set.add(record1) + assert len(record_set) == 1 + + record3_dupe = r.DNSQuestion("irrelevant", const._TYPE_A, const._CLASS_IN) + assert record2 == record3_dupe + assert record2.__hash__() == record3_dupe.__hash__() + + record_set.add(record3_dupe) + assert len(record_set) == 1 + + record4_dupe = r.DNSQuestion("notsame", const._TYPE_A, const._CLASS_IN) + assert record2 != record4_dupe + assert record2.__hash__() != record4_dupe.__hash__() + + record_set.add(record4_dupe) + assert len(record_set) == 2 + + +def test_dns_record_hashablity_does_not_consider_ttl(): + """Test DNSRecord are hashable.""" + + # Verify the TTL is not considered in the hash + record1 = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, const._DNS_OTHER_TTL, b"same") + record2 = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, const._DNS_HOST_TTL, b"same") + + record_set = {record1, record2} + assert len(record_set) == 1 + + record_set.add(record1) + assert len(record_set) == 1 + + record3_dupe = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, const._DNS_HOST_TTL, b"same") + assert record2 == record3_dupe + assert record2.__hash__() == record3_dupe.__hash__() + + record_set.add(record3_dupe) + assert len(record_set) == 1 + + +def test_dns_record_hashablity_does_not_consider_created(): + """Test DNSRecord are hashable and created is not considered.""" + + # Verify the TTL is not considered in the hash + record1 = r.DNSAddress( + "irrelevant", const._TYPE_A, const._CLASS_IN, const._DNS_HOST_TTL, b"same", created=1.0 + ) + record2 = r.DNSAddress( + "irrelevant", const._TYPE_A, const._CLASS_IN, const._DNS_HOST_TTL, b"same", created=2.0 + ) + + record_set = {record1, record2} + assert len(record_set) == 1 + + record_set.add(record1) + assert len(record_set) == 1 + + record3_dupe = r.DNSAddress( + "irrelevant", const._TYPE_A, const._CLASS_IN, const._DNS_HOST_TTL, b"same", created=3.0 + ) + assert record2 == record3_dupe + assert record2.__hash__() == record3_dupe.__hash__() + + record_set.add(record3_dupe) + assert len(record_set) == 1 + + +def test_dns_record_hashablity_does_not_consider_unique(): + """Test DNSRecord are hashable and unique is ignored.""" + + # Verify the unique value is not considered in the hash + record1 = r.DNSAddress( + "irrelevant", + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"same", + ) + record2 = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, const._DNS_OTHER_TTL, b"same") + + assert record1.class_ == record2.class_ + assert record1.__hash__() == record2.__hash__() + record_set = {record1, record2} + assert len(record_set) == 1 + + +def test_dns_address_record_hashablity(): + """Test DNSAddress are hashable.""" + address1 = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 1, b"a") + address2 = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 1, b"b") + address3 = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 1, b"c") + address4 = r.DNSAddress("irrelevant", const._TYPE_AAAA, const._CLASS_IN, 1, b"c") + + record_set = {address1, address2, address3, address4} + assert len(record_set) == 4 + + record_set.add(address1) + assert len(record_set) == 4 + + address3_dupe = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 1, b"c") + + record_set.add(address3_dupe) + assert len(record_set) == 4 + + # Verify we can remove records + additional_set = {address1, address2} + record_set -= additional_set + assert record_set == {address3, address4} + + +def test_dns_hinfo_record_hashablity(): + """Test DNSHinfo are hashable.""" + hinfo1 = r.DNSHinfo("irrelevant", const._TYPE_HINFO, 0, 0, "cpu1", "os") + hinfo2 = r.DNSHinfo("irrelevant", const._TYPE_HINFO, 0, 0, "cpu2", "os") + + record_set = {hinfo1, hinfo2} + assert len(record_set) == 2 + + record_set.add(hinfo1) + assert len(record_set) == 2 + + hinfo2_dupe = r.DNSHinfo("irrelevant", const._TYPE_HINFO, 0, 0, "cpu2", "os") + assert hinfo2 == hinfo2_dupe + assert hinfo2.__hash__() == hinfo2_dupe.__hash__() + + record_set.add(hinfo2_dupe) + assert len(record_set) == 2 + + +def test_dns_pointer_record_hashablity(): + """Test DNSPointer are hashable.""" + ptr1 = r.DNSPointer("irrelevant", const._TYPE_PTR, const._CLASS_IN, const._DNS_OTHER_TTL, "123") + ptr2 = r.DNSPointer("irrelevant", const._TYPE_PTR, const._CLASS_IN, const._DNS_OTHER_TTL, "456") + + record_set = {ptr1, ptr2} + assert len(record_set) == 2 + + record_set.add(ptr1) + assert len(record_set) == 2 + + ptr2_dupe = r.DNSPointer("irrelevant", const._TYPE_PTR, const._CLASS_IN, const._DNS_OTHER_TTL, "456") + assert ptr2 == ptr2 + assert ptr2.__hash__() == ptr2_dupe.__hash__() + + record_set.add(ptr2_dupe) + assert len(record_set) == 2 + + +def test_dns_pointer_comparison_is_case_insensitive(): + """Test DNSPointer comparison is case insensitive.""" + ptr1 = r.DNSPointer("irrelevant", const._TYPE_PTR, const._CLASS_IN, const._DNS_OTHER_TTL, "123") + ptr2 = r.DNSPointer( + "irrelevant".upper(), + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "123", + ) + + assert ptr1 == ptr2 + + +def test_dns_text_record_hashablity(): + """Test DNSText are hashable.""" + text1 = r.DNSText("irrelevant", 0, 0, const._DNS_OTHER_TTL, b"12345678901") + text2 = r.DNSText("irrelevant", 1, 0, const._DNS_OTHER_TTL, b"12345678901") + text3 = r.DNSText("irrelevant", 0, 1, const._DNS_OTHER_TTL, b"12345678901") + text4 = r.DNSText("irrelevant", 0, 0, const._DNS_OTHER_TTL, b"ABCDEFGHIJK") + + record_set = {text1, text2, text3, text4} + + assert len(record_set) == 4 + + record_set.add(text1) + assert len(record_set) == 4 + + text1_dupe = r.DNSText("irrelevant", 0, 0, const._DNS_OTHER_TTL, b"12345678901") + assert text1 == text1_dupe + assert text1.__hash__() == text1_dupe.__hash__() + + record_set.add(text1_dupe) + assert len(record_set) == 4 + + +def test_dns_service_record_hashablity(): + """Test DNSService are hashable.""" + srv1 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "a", + ) + srv2 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 1, + 80, + "a", + ) + srv3 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 81, + "a", + ) + srv4 = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "ab", + ) + + record_set = {srv1, srv2, srv3, srv4} + + assert len(record_set) == 4 + + record_set.add(srv1) + assert len(record_set) == 4 + + srv1_dupe = r.DNSService( + "irrelevant", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "a", + ) + assert srv1 == srv1_dupe + assert srv1.__hash__() == srv1_dupe.__hash__() + + record_set.add(srv1_dupe) + assert len(record_set) == 4 + + +def test_dns_service_server_key(): + """Test DNSService server_key is lowercase.""" + srv1 = r.DNSService( + "X._tcp._http.local.", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "X.local.", + ) + assert srv1.name == "X._tcp._http.local." + assert srv1.key == "x._tcp._http.local." + assert srv1.server == "X.local." + assert srv1.server_key == "x.local." + + +def test_dns_service_server_comparison_is_case_insensitive(): + """Test DNSService server comparison is case insensitive.""" + srv1 = r.DNSService( + "X._tcp._http.local.", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "X.local.", + ) + srv2 = r.DNSService( + "X._tcp._http.local.", + const._TYPE_SRV, + const._CLASS_IN, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "x.local.", + ) + assert srv1 == srv2 + + +def test_dns_nsec_record_hashablity(): + """Test DNSNsec are hashable.""" + nsec1 = r.DNSNsec( + "irrelevant", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "irrelevant", + [1, 2, 3], + ) + nsec2 = r.DNSNsec( + "irrelevant", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "irrelevant", + [1, 2], + ) + + record_set = {nsec1, nsec2} + assert len(record_set) == 2 + + record_set.add(nsec1) + assert len(record_set) == 2 + + nsec2_dupe = r.DNSNsec( + "irrelevant", + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + "irrelevant", + [1, 2], + ) + assert nsec2 == nsec2_dupe + assert nsec2.__hash__() == nsec2_dupe.__hash__() + + record_set.add(nsec2_dupe) + assert len(record_set) == 2 + + +def test_rrset_does_not_consider_ttl(): + """Test DNSRRSet does not consider the ttl in the hash.""" + + longarec = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 100, b"same") + shortarec = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 10, b"same") + longaaaarec = r.DNSAddress("irrelevant", const._TYPE_AAAA, const._CLASS_IN, 100, b"same") + shortaaaarec = r.DNSAddress("irrelevant", const._TYPE_AAAA, const._CLASS_IN, 10, b"same") + + rrset = DNSRRSet([longarec, shortaaaarec]) + + assert rrset.suppresses(longarec) + assert rrset.suppresses(shortarec) + assert not rrset.suppresses(longaaaarec) + assert rrset.suppresses(shortaaaarec) + + verylongarec = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 1000, b"same") + longarec = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 100, b"same") + mediumarec = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 60, b"same") + shortarec = r.DNSAddress("irrelevant", const._TYPE_A, const._CLASS_IN, 10, b"same") + + rrset2 = DNSRRSet([mediumarec]) + assert not rrset2.suppresses(verylongarec) + assert rrset2.suppresses(longarec) + assert rrset2.suppresses(mediumarec) + assert rrset2.suppresses(shortarec) diff --git a/tests/test_engine.py b/tests/test_engine.py new file mode 100644 index 000000000..66cfa3566 --- /dev/null +++ b/tests/test_engine.py @@ -0,0 +1,122 @@ +"""Unit tests for zeroconf._engine""" + +from __future__ import annotations + +import asyncio +import itertools +import logging +from unittest.mock import patch + +import pytest + +import zeroconf as r +from zeroconf import _engine, const +from zeroconf.asyncio import AsyncZeroconf + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +# This test uses asyncio because it needs to access the cache directly +# which is not threadsafe +@pytest.mark.asyncio +async def test_reaper(): + with patch.object(_engine, "_CACHE_CLEANUP_INTERVAL", 0.01): + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf = aiozc.zeroconf + cache = zeroconf.cache + original_entries = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names()))) + record_with_10s_ttl = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 10, b"a") + record_with_1s_ttl = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"b") + # Backdate the short-lived record so it expires at the next + # reaper tick instead of waiting the full TTL in real time. + record_with_1s_ttl.created -= 2000 + zeroconf.cache.async_add_records([record_with_10s_ttl, record_with_1s_ttl]) + question = r.DNSQuestion("_hap._tcp._local.", const._TYPE_PTR, const._CLASS_IN) + now = r.current_time_millis() + # Add the question at `past` so the reaper's next tick will see + # `current_time - past > _DUPLICATE_QUESTION_INTERVAL` and prune it, + # while the initial `suppresses(now, ...)` check still sees the + # question as recent (since `now - past == 999`, not strictly `> 999`). + past = now - 999 + other_known_answers: set[r.DNSRecord] = { + r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + 10000, + "known-to-other._hap._tcp.local.", + ) + } + zeroconf.question_history.add_question_at_time(question, past, other_known_answers) + assert zeroconf.question_history.suppresses(question, now, other_known_answers) + entries_with_cache = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names()))) + await asyncio.sleep(0.1) + entries = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names()))) + assert zeroconf.cache.get(record_with_1s_ttl) is None + await aiozc.async_close() + assert not zeroconf.question_history.suppresses(question, now, other_known_answers) + assert entries != original_entries + assert entries_with_cache != original_entries + assert record_with_10s_ttl in entries + assert record_with_1s_ttl not in entries + + +@pytest.mark.asyncio +async def test_setup_releases_socket_ownership(aiozc_loopback: AsyncZeroconf) -> None: + """Engine releases its pending-socket refs once each socket has a transport.""" + await aiozc_loopback.zeroconf.async_wait_for_start() + engine = aiozc_loopback.zeroconf.engine + assert engine._listen_socket is None + assert engine._respond_sockets == [] + assert engine.readers + assert engine.senders + + +@pytest.mark.asyncio +async def test_async_close_propagates_outer_cancellation(aiozc_loopback: AsyncZeroconf) -> None: + """Outer-task cancellation while awaiting setup propagates to the caller.""" + await aiozc_loopback.zeroconf.async_wait_for_start() + engine = aiozc_loopback.zeroconf.engine + loop = asyncio.get_running_loop() + original_task = engine._setup_task + fake_task = loop.create_future() + fake_task.set_exception(asyncio.CancelledError()) + engine._setup_task = fake_task # type: ignore[assignment] + try: + with pytest.raises(asyncio.CancelledError): + await engine._async_close() + finally: + engine._setup_task = original_task + + +@pytest.mark.asyncio +async def test_reaper_aborts_when_done(): + """Ensure cache cleanup stops when zeroconf is done.""" + with patch.object(_engine, "_CACHE_CLEANUP_INTERVAL", 0.01): + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zeroconf = aiozc.zeroconf + record_with_10s_ttl = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 10, b"a") + record_with_1s_ttl = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"b") + zeroconf.cache.async_add_records([record_with_10s_ttl, record_with_1s_ttl]) + assert zeroconf.cache.get(record_with_10s_ttl) is not None + assert zeroconf.cache.get(record_with_1s_ttl) is not None + await aiozc.async_close() + # Backdate to immediate expiry so we don't have to wait the full + # TTL; the assertion is that the reaper has stopped, so a + # short sleep is enough to give it a chance to (incorrectly) run. + record_with_1s_ttl.created -= 2000 + await asyncio.sleep(0.1) + assert zeroconf.cache.get(record_with_10s_ttl) is not None + assert zeroconf.cache.get(record_with_1s_ttl) is not None diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py new file mode 100644 index 000000000..94a407c19 --- /dev/null +++ b/tests/test_exceptions.py @@ -0,0 +1,157 @@ +"""Unit tests for zeroconf._exceptions""" + +from __future__ import annotations + +import logging +import unittest.mock + +import zeroconf as r +from zeroconf import ServiceInfo, Zeroconf + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +class Exceptions(unittest.TestCase): + browser: Zeroconf + + @classmethod + def setUpClass(cls): + cls.browser = Zeroconf(interfaces=["127.0.0.1"]) + + @classmethod + def tearDownClass(cls): + cls.browser.close() + del cls.browser + + def test_bad_service_info_name(self): + self.assertRaises(r.BadTypeInNameException, self.browser.get_service_info, "type", "type_not") + + def test_bad_service_names(self): + bad_names_to_try = ( + "", + "local", + "_tcp.local.", + "_udp.local.", + "._udp.local.", + "_@._tcp.local.", + "_A@._tcp.local.", + "_x--x._tcp.local.", + "_-x._udp.local.", + "_x-._tcp.local.", + "_22._udp.local.", + "_2-2._tcp.local.", + "\x00._x._udp.local.", + ) + for name in bad_names_to_try: + self.assertRaises( + r.BadTypeInNameException, + self.browser.get_service_info, + name, + "x." + name, + ) + + def test_bad_local_names_for_get_service_info(self): + bad_names_to_try = ( + "homekitdev._nothttp._tcp.local.", + "homekitdev._http._udp.local.", + ) + for name in bad_names_to_try: + self.assertRaises( + r.BadTypeInNameException, + self.browser.get_service_info, + "_http._tcp.local.", + name, + ) + + def test_good_instance_names(self): + assert r.service_type_name(".._x._tcp.local.") == "_x._tcp.local." + assert r.service_type_name("x.y._http._tcp.local.") == "_http._tcp.local." + assert r.service_type_name("1.2.3._mqtt._tcp.local.") == "_mqtt._tcp.local." + assert r.service_type_name("x.sub._http._tcp.local.") == "_http._tcp.local." + assert ( + r.service_type_name("6d86f882b90facee9170ad3439d72a4d6ee9f511._zget._http._tcp.local.") + == "_http._tcp.local." + ) + + def test_good_instance_names_without_protocol(self): + good_names_to_try = ( + "Rachio-C73233.local.", + "YeelightColorBulb-3AFD.local.", + "YeelightTunableBulb-7220.local.", + "AlexanderHomeAssistant 74651D.local.", + "iSmartGate-152.local.", + "MyQ-FGA.local.", + "lutron-02c4392a.local.", + "WICED-hap-3E2734.local.", + "MyHost.local.", + "MyHost.sub.local.", + ) + for name in good_names_to_try: + assert r.service_type_name(name, strict=False) == "local." + + for name in good_names_to_try: + # Raises without strict=False + self.assertRaises(r.BadTypeInNameException, r.service_type_name, name) + + def test_bad_types(self): + bad_names_to_try = ( + "._x._tcp.local.", + "a" * 64 + "._sub._http._tcp.local.", + "a" * 62 + "â._sub._http._tcp.local.", + ) + for name in bad_names_to_try: + self.assertRaises(r.BadTypeInNameException, r.service_type_name, name) + + def test_bad_sub_types(self): + bad_names_to_try = ( + "_sub._http._tcp.local.", + "._sub._http._tcp.local.", + "\x7f._sub._http._tcp.local.", + "\x1f._sub._http._tcp.local.", + ) + for name in bad_names_to_try: + self.assertRaises(r.BadTypeInNameException, r.service_type_name, name) + + def test_good_service_names(self): + good_names_to_try = ( + ("_x._tcp.local.", "_x._tcp.local."), + ("_x._udp.local.", "_x._udp.local."), + ("_12345-67890-abc._udp.local.", "_12345-67890-abc._udp.local."), + ("x._sub._http._tcp.local.", "_http._tcp.local."), + ("a" * 63 + "._sub._http._tcp.local.", "_http._tcp.local."), + ("a" * 61 + "â._sub._http._tcp.local.", "_http._tcp.local."), + ) + + for name, result in good_names_to_try: + assert r.service_type_name(name) == result + + assert r.service_type_name("_one_two._tcp.local.", strict=False) == "_one_two._tcp.local." + + def test_invalid_addresses(self): + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + bad = (b"127.0.0.1", b"::1") + for addr in bad: + self.assertRaisesRegex( + TypeError, + "Addresses must either ", + ServiceInfo, + type_, + registration_name, + port=80, + addresses=[addr], + ) diff --git a/tests/test_handlers.py b/tests/test_handlers.py new file mode 100644 index 000000000..69f3c826a --- /dev/null +++ b/tests/test_handlers.py @@ -0,0 +1,2109 @@ +"""Unit tests for zeroconf._handlers""" + +from __future__ import annotations + +import asyncio +import logging +import os +import socket +import time +import unittest +import unittest.mock +from typing import cast +from unittest.mock import patch + +import pytest + +import zeroconf as r +from zeroconf import ServiceInfo, Zeroconf, const, current_time_millis +from zeroconf._handlers.multicast_outgoing_queue import ( + MulticastOutgoingQueue, + construct_outgoing_multicast_answers, +) +from zeroconf._utils.time import millis_to_seconds +from zeroconf.asyncio import AsyncZeroconf + +from . import _clear_cache, _inject_response, has_working_ipv6 + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +class TestRegistrar(unittest.TestCase): + def test_ttl(self): + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # service definition + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + nbr_answers = nbr_additionals = nbr_authorities = 0 + + def get_ttl(record_type): + if expected_ttl is not None: + return expected_ttl + if record_type in [const._TYPE_A, const._TYPE_SRV, const._TYPE_NSEC]: + return const._DNS_HOST_TTL + return const._DNS_OTHER_TTL + + def _process_outgoing_packet(out): + """Sends an outgoing packet.""" + nonlocal nbr_answers, nbr_additionals, nbr_authorities + + for answer, _ in out.answers: + nbr_answers += 1 + assert answer.ttl == get_ttl(answer.type) + for answer in out.additionals: + nbr_additionals += 1 + assert answer.ttl == get_ttl(answer.type) + for answer in out.authorities: + nbr_authorities += 1 + assert answer.ttl == get_ttl(answer.type) + + # register service with default TTL + expected_ttl = None + for _ in range(3): + _process_outgoing_packet(zc.generate_service_query(info)) + zc.registry.async_add(info) + for _ in range(3): + _process_outgoing_packet(zc.generate_service_broadcast(info, None)) + assert nbr_answers == 15 and nbr_additionals == 0 and nbr_authorities == 3 + nbr_answers = nbr_additionals = nbr_authorities = 0 + + # query + query = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_AA) + assert query.is_query() is True + query.add_question(r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN)) + query.add_question(r.DNSQuestion(info.name, const._TYPE_SRV, const._CLASS_IN)) + query.add_question(r.DNSQuestion(info.name, const._TYPE_TXT, const._CLASS_IN)) + query.add_question(r.DNSQuestion(info.server or info.name, const._TYPE_A, const._CLASS_IN)) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + _process_outgoing_packet(construct_outgoing_multicast_answers(question_answers.mcast_aggregate)) + + # The additonals should all be suppressed since they are all in the answers section + # There will be one NSEC additional to indicate the lack of AAAA record + # + assert nbr_answers == 4 and nbr_additionals == 1 and nbr_authorities == 0 + nbr_answers = nbr_additionals = nbr_authorities = 0 + + # unregister + expected_ttl = 0 + zc.registry.async_remove(info) + for _ in range(3): + _process_outgoing_packet(zc.generate_service_broadcast(info, 0)) + assert nbr_answers == 15 and nbr_additionals == 0 and nbr_authorities == 0 + nbr_answers = nbr_additionals = nbr_authorities = 0 + + expected_ttl = None + for _ in range(3): + _process_outgoing_packet(zc.generate_service_query(info)) + zc.registry.async_add(info) + # register service with custom TTL + expected_ttl = const._DNS_HOST_TTL * 2 + assert expected_ttl != const._DNS_HOST_TTL + for _ in range(3): + _process_outgoing_packet(zc.generate_service_broadcast(info, expected_ttl)) + assert nbr_answers == 15 and nbr_additionals == 0 and nbr_authorities == 3 + nbr_answers = nbr_additionals = nbr_authorities = 0 + + # query + expected_ttl = None + query = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_AA) + query.add_question(r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN)) + query.add_question(r.DNSQuestion(info.name, const._TYPE_SRV, const._CLASS_IN)) + query.add_question(r.DNSQuestion(info.name, const._TYPE_TXT, const._CLASS_IN)) + query.add_question(r.DNSQuestion(info.server or info.name, const._TYPE_A, const._CLASS_IN)) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + _process_outgoing_packet(construct_outgoing_multicast_answers(question_answers.mcast_aggregate)) + + # There will be one NSEC additional to indicate the lack of AAAA record + assert nbr_answers == 4 and nbr_additionals == 1 and nbr_authorities == 0 + nbr_answers = nbr_additionals = nbr_authorities = 0 + + # unregister + expected_ttl = 0 + zc.registry.async_remove(info) + for _ in range(3): + _process_outgoing_packet(zc.generate_service_broadcast(info, 0)) + assert nbr_answers == 15 and nbr_additionals == 0 and nbr_authorities == 0 + nbr_answers = nbr_additionals = nbr_authorities = 0 + zc.close() + + @pytest.mark.usefixtures("quick_timing") + def test_name_conflicts(self): + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_homeassistant._tcp.local." + name = "Home" + registration_name = f"{name}.{type_}" + + info = ServiceInfo( + type_, + name=registration_name, + server="random123.local.", + addresses=[socket.inet_pton(socket.AF_INET, "1.2.3.4")], + port=80, + properties={"version": "1.0"}, + ) + zc.register_service(info) + + conflicting_info = ServiceInfo( + type_, + name=registration_name, + server="random456.local.", + addresses=[socket.inet_pton(socket.AF_INET, "4.5.6.7")], + port=80, + properties={"version": "1.0"}, + ) + with pytest.raises(r.NonUniqueNameException): + zc.register_service(conflicting_info) + zc.close() + + @pytest.mark.usefixtures("quick_timing") + def test_register_and_lookup_type_by_uppercase_name(self): + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_mylowertype._tcp.local." + name = "Home" + registration_name = f"{name}.{type_}" + + info = ServiceInfo( + type_, + name=registration_name, + server="random123.local.", + addresses=[socket.inet_pton(socket.AF_INET, "1.2.3.4")], + port=80, + properties={"version": "1.0"}, + ) + zc.register_service(info) + _clear_cache(zc) + info = ServiceInfo(type_, registration_name) + info.load_from_cache(zc) + assert info.addresses == [] + + out = r.DNSOutgoing(const._FLAGS_QR_QUERY) + out.add_question(r.DNSQuestion(type_.upper(), const._TYPE_PTR, const._CLASS_IN)) + zc.send(out) + info = ServiceInfo(type_, registration_name) + for _ in range(50): + time.sleep(0.02) + info.load_from_cache(zc) + # Wait for both A and TXT records — they arrive as separate + # cache updates and the listener may schedule the assertions + # between the two. Breaking on just `info.addresses` makes + # the test flaky under PyPy / skip_cython. + if info.addresses and info.properties: + break + assert info.addresses == [socket.inet_pton(socket.AF_INET, "1.2.3.4")] + assert info.properties == {b"version": b"1.0"} + zc.close() + + +def test_ptr_optimization(quick_timing: None) -> None: + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # service definition + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + # register + zc.register_service(info) + + # Verify we won't respond for 1s with the same multicast + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + query.add_question(r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN)) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + # Since we sent the PTR in the last second, they + # should end up in the delayed at least one second bucket + assert question_answers.mcast_aggregate_last_second + + # Clear the cache to allow responding again + _clear_cache(zc) + + # Verify we will now respond + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + query.add_question(r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN)) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate_last_second + has_srv = has_txt = has_a = False + nbr_additionals = 0 + nbr_answers = len(question_answers.mcast_aggregate) + additionals = set().union(*question_answers.mcast_aggregate.values()) + for answer in additionals: + nbr_additionals += 1 + if answer.type == const._TYPE_SRV: + has_srv = True + elif answer.type == const._TYPE_TXT: + has_txt = True + elif answer.type == const._TYPE_A: + has_a = True + assert nbr_answers == 1 and nbr_additionals == 4 + # There will be one NSEC additional to indicate the lack of AAAA record + + assert has_srv and has_txt and has_a + + # unregister + zc.unregister_service(info) + zc.close() + + +@unittest.skipIf(not has_working_ipv6(), "Requires IPv6") +@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") +def test_any_query_for_ptr(): + """Test that queries for ANY will return PTR records and the response is aggregated.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_anyptr._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + ipv6_address = socket.inet_pton(socket.AF_INET6, "2001:db8::1") + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, server_name, addresses=[ipv6_address]) + zc.registry.async_add(info) + + _clear_cache(zc) + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(type_, const._TYPE_ANY, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + mcast_answers = list(question_answers.mcast_aggregate) + assert mcast_answers[0].name == type_ + assert mcast_answers[0].alias == registration_name # type: ignore[attr-defined] + # unregister + zc.registry.async_remove(info) + zc.close() + + +@unittest.skipIf(not has_working_ipv6(), "Requires IPv6") +@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") +def test_aaaa_query(): + """Test that queries for AAAA records work and should respond right away.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_knownaaaservice._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + ipv6_address = socket.inet_pton(socket.AF_INET6, "2001:db8::1") + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, server_name, addresses=[ipv6_address]) + zc.registry.async_add(info) + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(server_name, const._TYPE_AAAA, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + mcast_answers = list(question_answers.mcast_now) + assert mcast_answers[0].address == ipv6_address # type: ignore[attr-defined] + # unregister + zc.registry.async_remove(info) + zc.close() + + +@unittest.skipIf(not has_working_ipv6(), "Requires IPv6") +@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") +def test_aaaa_query_upper_case(): + """Test that queries for AAAA records work and should respond right away with an upper case name.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_knownaaaservice._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + ipv6_address = socket.inet_pton(socket.AF_INET6, "2001:db8::1") + info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, server_name, addresses=[ipv6_address]) + zc.registry.async_add(info) + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(server_name.upper(), const._TYPE_AAAA, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + mcast_answers = list(question_answers.mcast_now) + assert mcast_answers[0].address == ipv6_address # type: ignore[attr-defined] + # unregister + zc.registry.async_remove(info) + zc.close() + + +@unittest.skipIf(not has_working_ipv6(), "Requires IPv6") +@unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") +def test_a_and_aaaa_record_fate_sharing(): + """Test that queries for AAAA always return A records in the additionals and should respond right away.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_a-and-aaaa-service._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + ipv6_address = socket.inet_pton(socket.AF_INET6, "2001:db8::1") + ipv4_address = socket.inet_aton("10.0.1.2") + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[ipv6_address, ipv4_address], + ) + aaaa_record = info.dns_addresses(version=r.IPVersion.V6Only)[0] + a_record = info.dns_addresses(version=r.IPVersion.V4Only)[0] + + zc.registry.async_add(info) + + # Test AAAA query + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(server_name, const._TYPE_AAAA, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + additionals = set().union(*question_answers.mcast_now.values()) + assert aaaa_record in question_answers.mcast_now + assert a_record in additionals + assert len(question_answers.mcast_now) == 1 + assert len(additionals) == 1 + + # Test A query + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(server_name, const._TYPE_A, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + additionals = set().union(*question_answers.mcast_now.values()) + assert a_record in question_answers.mcast_now + assert aaaa_record in additionals + assert len(question_answers.mcast_now) == 1 + assert len(additionals) == 1 + + # unregister + zc.registry.async_remove(info) + zc.close() + + +def test_unicast_response(): + """Ensure we send a unicast response when the source port is not the MDNS port.""" + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # service definition + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + # register + zc.registry.async_add(info) + _clear_cache(zc) + + # query + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + query.add_question(r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN)) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], True + ) + assert question_answers + for answers in (question_answers.ucast, question_answers.mcast_aggregate): + has_srv = has_txt = has_a = has_aaaa = has_nsec = False + nbr_additionals = 0 + nbr_answers = len(answers) + additionals = set().union(*answers.values()) + for answer in additionals: + nbr_additionals += 1 + if answer.type == const._TYPE_SRV: + has_srv = True + elif answer.type == const._TYPE_TXT: + has_txt = True + elif answer.type == const._TYPE_A: + has_a = True + elif answer.type == const._TYPE_AAAA: + has_aaaa = True + elif answer.type == const._TYPE_NSEC: + has_nsec = True + # There will be one NSEC additional to indicate the lack of AAAA record + assert nbr_answers == 1 and nbr_additionals == 4 + assert has_srv and has_txt and has_a and has_nsec + assert not has_aaaa + + # unregister + zc.registry.async_remove(info) + zc.close() + + +@pytest.mark.asyncio +async def test_probe_answered_immediately(): + """Verify probes are responded to immediately.""" + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # service definition + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + query.add_question(question) + query.add_authorative_answer(info.dns_pointer()) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + assert question_answers.mcast_now + + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True + query.add_question(question) + query.add_authorative_answer(info.dns_pointer()) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert question_answers.ucast + assert question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + zc.close() + + +@pytest.mark.asyncio +async def test_probe_answered_immediately_with_uppercase_name(): + """Verify probes are responded to immediately with an uppercase name.""" + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # service definition + type_ = "_test-srvc-type._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type.upper(), const._TYPE_PTR, const._CLASS_IN) + query.add_question(question) + query.add_authorative_answer(info.dns_pointer()) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + assert question_answers.mcast_now + + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True + query.add_question(question) + query.add_authorative_answer(info.dns_pointer()) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert question_answers.ucast + assert question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + zc.close() + + +def test_qu_response(quick_timing: None) -> None: + """Handle multicast incoming with the QU bit set.""" + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # service definition + type_ = "_test-srvc-type._tcp.local." + other_type_ = "_notthesame._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + registration_name2 = f"{name}.{other_type_}" + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + other_type_, + registration_name2, + 80, + 0, + 0, + desc, + "ash-other.local.", + addresses=[socket.inet_aton("10.0.4.2")], + ) + # register + zc.register_service(info) + + def _validate_complete_response(answers): + has_srv = has_txt = has_a = has_aaaa = has_nsec = False + nbr_answers = len(answers) + additionals = set().union(*answers.values()) + nbr_additionals = len(additionals) + + for answer in additionals: + if answer.type == const._TYPE_SRV: + has_srv = True + elif answer.type == const._TYPE_TXT: + has_txt = True + elif answer.type == const._TYPE_A: + has_a = True + elif answer.type == const._TYPE_AAAA: + has_aaaa = True + elif answer.type == const._TYPE_NSEC: + has_nsec = True + assert nbr_answers == 1 and nbr_additionals == 4 + assert has_srv and has_txt and has_a and has_nsec + assert not has_aaaa + + # With QU should respond to only unicast when the answer has been recently multicast + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + _validate_complete_response(question_answers.ucast) + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + _clear_cache(zc) + # With QU should respond to only multicast since the response hasn't been seen since 75% of the ttl + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate + _validate_complete_response(question_answers.mcast_now) + + # With QU set and an authoritative answer (probe) should respond to both unitcast + # and multicast since the response hasn't been seen since 75% of the ttl + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + query.add_authorative_answer(info2.dns_pointer()) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + _validate_complete_response(question_answers.ucast) + _validate_complete_response(question_answers.mcast_now) + + _inject_response( + zc, + r.DNSIncoming(construct_outgoing_multicast_answers(question_answers.mcast_now).packets()[0]), + ) + # With the cache repopulated; should respond to only unicast when the answer has been recently multicast + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + _validate_complete_response(question_answers.ucast) + # unregister + zc.unregister_service(info) + zc.close() + + +def test_known_answer_supression(): + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_knownanswersv8._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + + now = current_time_millis() + _clear_cache(zc) + # Test PTR suppression + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + generated.add_answer_at_time(info.dns_pointer(), now) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + # Test A suppression + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(server_name, const._TYPE_A, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(server_name, const._TYPE_A, const._CLASS_IN) + generated.add_question(question) + for dns_address in info.dns_addresses(): + generated.add_answer_at_time(dns_address, now) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + # Test NSEC record returned when there is no AAAA record and we expectly ask + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(server_name, const._TYPE_AAAA, const._CLASS_IN) + generated.add_question(question) + for dns_address in info.dns_addresses(): + generated.add_answer_at_time(dns_address, now) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + expected_nsec_record = cast(r.DNSNsec, next(iter(question_answers.mcast_now))) + assert const._TYPE_A not in expected_nsec_record.rdtypes + assert const._TYPE_AAAA in expected_nsec_record.rdtypes + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + # Test SRV suppression + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(registration_name, const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(registration_name, const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + generated.add_answer_at_time(info.dns_service(), now) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + # Test TXT suppression + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(registration_name, const._TYPE_TXT, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(registration_name, const._TYPE_TXT, const._CLASS_IN) + generated.add_question(question) + generated.add_answer_at_time(info.dns_text(), now) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + # unregister + zc.registry.async_remove(info) + zc.close() + + +def test_multi_packet_known_answer_supression(): + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_handlermultis._tcp.local." + name = "knownname" + name2 = "knownname2" + name3 = "knownname3" + + registration_name = f"{name}.{type_}" + registration2_name = f"{name2}.{type_}" + registration3_name = f"{name3}.{type_}" + + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + server_name2 = "ash-3.local." + server_name3 = "ash-4.local." + + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_, + registration2_name, + 80, + 0, + 0, + desc, + server_name2, + addresses=[socket.inet_aton("10.0.1.2")], + ) + info3 = ServiceInfo( + type_, + registration3_name, + 80, + 0, + 0, + desc, + server_name3, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + zc.registry.async_add(info2) + zc.registry.async_add(info3) + + now = current_time_millis() + _clear_cache(zc) + # Test PTR suppression + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + for _ in range(1000): + # Add so many answers we end up with another packet + generated.add_answer_at_time(info.dns_pointer(), now) + generated.add_answer_at_time(info2.dns_pointer(), now) + generated.add_answer_at_time(info3.dns_pointer(), now) + packets = generated.packets() + assert len(packets) > 1 + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + # unregister + zc.registry.async_remove(info) + zc.registry.async_remove(info2) + zc.registry.async_remove(info3) + zc.close() + + +def test_known_answer_supression_service_type_enumeration_query(): + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_otherknown._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + + type_2 = "_otherknown2._tcp.local." + name = "knownname" + registration_name2 = f"{name}.{type_2}" + desc = {"path": "/~paulsm/"} + server_name2 = "ash-3.local." + info2 = ServiceInfo( + type_2, + registration_name2, + 80, + 0, + 0, + desc, + server_name2, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info2) + now = current_time_millis() + _clear_cache(zc) + + # Test PTR suppression + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(const._SERVICE_TYPE_ENUMERATION_NAME, const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(const._SERVICE_TYPE_ENUMERATION_NAME, const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + generated.add_answer_at_time( + r.DNSPointer( + const._SERVICE_TYPE_ENUMERATION_NAME, + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + type_, + ), + now, + ) + generated.add_answer_at_time( + r.DNSPointer( + const._SERVICE_TYPE_ENUMERATION_NAME, + const._TYPE_PTR, + const._CLASS_IN, + const._DNS_OTHER_TTL, + type_2, + ), + now, + ) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + # unregister + zc.registry.async_remove(info) + zc.registry.async_remove(info2) + zc.close() + + +def test_upper_case_enumeration_query(): + zc = Zeroconf(interfaces=["127.0.0.1"]) + type_ = "_otherknown._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + + type_2 = "_otherknown2._tcp.local." + name = "knownname" + registration_name2 = f"{name}.{type_2}" + desc = {"path": "/~paulsm/"} + server_name2 = "ash-3.local." + info2 = ServiceInfo( + type_2, + registration_name2, + 80, + 0, + 0, + desc, + server_name2, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info2) + _clear_cache(zc) + + # Test PTR suppression + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(const._SERVICE_TYPE_ENUMERATION_NAME.upper(), const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + # unregister + zc.registry.async_remove(info) + zc.registry.async_remove(info2) + zc.close() + + +def test_enumeration_query_with_no_registered_services(): + zc = Zeroconf(interfaces=["127.0.0.1"]) + _clear_cache(zc) + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(const._SERVICE_TYPE_ENUMERATION_NAME.upper(), const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert not question_answers + # unregister + zc.close() + + +# This test uses asyncio because it needs to access the cache directly +# which is not threadsafe +@pytest.mark.asyncio +async def test_qu_response_only_sends_additionals_if_sends_answer(): + """Test that a QU response does not send additionals unless it sends the answer as well.""" + # instantiate a zeroconf instance + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + + type_ = "_addtest1._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "ash-2.local." + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info) + + type_2 = "_addtest2._tcp.local." + name = "knownname" + registration_name2 = f"{name}.{type_2}" + desc = {"path": "/~paulsm/"} + server_name2 = "ash-3.local." + info2 = ServiceInfo( + type_2, + registration_name2, + 80, + 0, + 0, + desc, + server_name2, + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.registry.async_add(info2) + + ptr_record = info.dns_pointer() + + # Add the PTR record to the cache + zc.cache.async_add_records([ptr_record]) + + # Add the A record to the cache with 50% ttl remaining + a_record = info.dns_addresses()[0] + zc.cache._async_set_created_ttl(a_record, current_time_millis() - (a_record.ttl * 1000 / 2), a_record.ttl) + assert not a_record.is_recent(current_time_millis()) + info._dns_address_cache = None # we are mutating the record so clear the cache + + # With QU should respond to only unicast when the answer has been recently multicast + # even if the additional has not been recently multicast + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + additionals = set().union(*question_answers.ucast.values()) + assert a_record in additionals + assert ptr_record in question_answers.ucast + + # Remove the 50% A record and add a 100% A record + zc.cache.async_remove_records([a_record]) + a_record = info.dns_addresses()[0] + assert a_record.is_recent(current_time_millis()) + zc.cache.async_add_records([a_record]) + # With QU should respond to only unicast when the answer has been recently multicast + # even if the additional has not been recently multicast + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + additionals = set().union(*question_answers.ucast.values()) + assert a_record in additionals + assert ptr_record in question_answers.ucast + + # Remove the 100% PTR record and add a 50% PTR record + zc.cache.async_remove_records([ptr_record]) + zc.cache._async_set_created_ttl( + ptr_record, current_time_millis() - (ptr_record.ttl * 1000 / 2), ptr_record.ttl + ) + assert not ptr_record.is_recent(current_time_millis()) + # With QU should respond to only multicast since the has less + # than 75% of its ttl remaining + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + additionals = set().union(*question_answers.mcast_now.values()) + assert a_record in additionals + assert info.dns_text() in additionals + assert info.dns_service() in additionals + assert ptr_record in question_answers.mcast_now + + # Ask 2 QU questions, with info the PTR is at 50%, with info2 the PTR is at 100% + # We should get back a unicast reply for info2, but info should be + # multicasted since its within 75% of its TTL + # With QU should respond to only multicast since the has less + # than 75% of its ttl remaining + query = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + + question = r.DNSQuestion(info2.type, const._TYPE_PTR, const._CLASS_IN) + question.unicast = True # Set the QU bit + assert question.unicast is True + query.add_question(question) + zc.cache.async_add_records([info2.dns_pointer()]) # Add 100% TTL for info2 to the cache + + question_answers = zc.query_handler.async_response( + [r.DNSIncoming(packet) for packet in query.packets()], False + ) + assert question_answers + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + + mcast_now_additionals = set().union(*question_answers.mcast_now.values()) + assert a_record in mcast_now_additionals + assert info.dns_text() in mcast_now_additionals + assert info.dns_addresses()[0] in mcast_now_additionals + assert info.dns_pointer() in question_answers.mcast_now + + ucast_additionals = set().union(*question_answers.ucast.values()) + assert info2.dns_pointer() in question_answers.ucast + assert info2.dns_text() in ucast_additionals + assert info2.dns_service() in ucast_additionals + assert info2.dns_addresses()[0] in ucast_additionals + + # unregister + zc.registry.async_remove(info) + await aiozc.async_close() + + +# This test uses asyncio because it needs to access the cache directly +# which is not threadsafe +@pytest.mark.asyncio +async def test_cache_flush_bit(): + """Test that the cache flush bit sets the TTL to one for matching records.""" + # instantiate a zeroconf instance + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + + type_ = "_cacheflush._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "server-uu1.local." + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + a_record = info.dns_addresses()[0] + zc.cache.async_add_records([info.dns_pointer(), a_record, info.dns_text(), info.dns_service()]) + + info.addresses = [socket.inet_aton("10.0.1.5"), socket.inet_aton("10.0.1.6")] + new_records = info.dns_addresses() + for new_record in new_records: + assert new_record.unique is True + + original_a_record = zc.cache.async_get_unique(a_record) + # Do the run within 1s to verify the original record is not going to be expired + out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA, multicast=True) + for answer in new_records: + out.add_answer_at_time(answer, 0) + for packet in out.packets(): + zc.record_manager.async_updates_from_response(r.DNSIncoming(packet)) + assert zc.cache.async_get_unique(a_record) is original_a_record + assert original_a_record is not None + assert original_a_record.ttl != 1 + for record in new_records: + assert zc.cache.async_get_unique(record) is not None + + original_a_record.created = current_time_millis() - 1500 + + # Do the run within 1s to verify the original record is not going to be expired + out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA, multicast=True) + for answer in new_records: + out.add_answer_at_time(answer, 0) + for packet in out.packets(): + zc.record_manager.async_updates_from_response(r.DNSIncoming(packet)) + assert original_a_record.ttl == 1 + for record in new_records: + assert zc.cache.async_get_unique(record) is not None + + cached_record_group = [ + zc.cache.async_all_by_details(record.name, record.type, record.class_) for record in new_records + ] + for cached_records in cached_record_group: + for cached_record in cached_records: + assert cached_record is not None + cached_record.created = current_time_millis() - 1500 + + fresh_address = socket.inet_aton("4.4.4.4") + info.addresses = [fresh_address] + # Do the run within 1s to verify the two new records get marked as expired + out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA, multicast=True) + for answer in info.dns_addresses(): + out.add_answer_at_time(answer, 0) + for packet in out.packets(): + zc.record_manager.async_updates_from_response(r.DNSIncoming(packet)) + + cached_record_group = [ + zc.cache.async_all_by_details(record.name, record.type, record.class_) for record in new_records + ] + for cached_records in cached_record_group: + for cached_record in cached_records: + # the new record should not be set to 1 + if cached_record == answer: + assert cached_record.ttl != 1 + continue + assert cached_record is not None + assert cached_record.ttl == 1 + + for entry in zc.cache.async_all_by_details(server_name, const._TYPE_A, const._CLASS_IN): + assert isinstance(entry, r.DNSAddress) + if entry.address == fresh_address: + assert entry.ttl > 1 + else: + assert entry.ttl == 1 + + # Backdate the ttl=1 records so they are already expired when + # load_from_cache runs — equivalent to sleeping 1.1s without the wait. + for store in zc.cache.cache.values(): + for cached in store.values(): + if cached.ttl == 1: + cached.created -= 1100 + + loaded_info = r.ServiceInfo(type_, registration_name) + loaded_info.load_from_cache(zc) + assert loaded_info.addresses == info.addresses + + await aiozc.async_close() + + +# This test uses asyncio because it needs to access the cache directly +# which is not threadsafe +@pytest.mark.asyncio +async def test_record_update_manager_add_listener_callsback_existing_records(): + """Test that the RecordUpdateManager will callback existing records.""" + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc: Zeroconf = aiozc.zeroconf + updated = [] + + class MyListener(r.RecordUpdateListener): + """A RecordUpdateListener that does not implement update_records.""" + + def async_update_records(self, zc: Zeroconf, now: float, records: list[r.RecordUpdate]) -> None: + """Update multiple records in one shot.""" + updated.extend(records) + + type_ = "_cacheflush._tcp.local." + name = "knownname" + registration_name = f"{name}.{type_}" + desc = {"path": "/~paulsm/"} + server_name = "server-uu1.local." + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + server_name, + addresses=[socket.inet_aton("10.0.1.2")], + ) + a_record = info.dns_addresses()[0] + ptr_record = info.dns_pointer() + zc.cache.async_add_records([ptr_record, a_record, info.dns_text(), info.dns_service()]) + + listener = MyListener() + + zc.add_listener( + listener, + [ + r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN), + r.DNSQuestion(server_name, const._TYPE_A, const._CLASS_IN), + ], + ) + await asyncio.sleep(0) # flush out the call_soon_threadsafe + + assert {record.new for record in updated} == {ptr_record, a_record} + + # The old records should be None so we trigger Add events + # in service browsers instead of Update events + assert {record.old for record in updated} == {None} + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_questions_query_handler_populates_the_question_history_from_qm_questions(): + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + now = current_time_millis() + _clear_cache(zc) + + aiozc.zeroconf.registry.async_add( + ServiceInfo( + "_hap._tcp.local.", + "other._hap._tcp.local.", + 80, + 0, + 0, + {"md": "known"}, + "ash-2.local.", + addresses=[socket.inet_aton("1.2.3.4")], + ) + ) + services = aiozc.zeroconf.registry.async_get_infos_type("_hap._tcp.local.") + assert len(services) == 1 + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion("_hap._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + question.unicast = False + known_answer = r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + 10000, + "known-to-other._hap._tcp.local.", + ) + generated.add_question(question) + generated.add_answer_at_time(known_answer, 0) + now = r.current_time_millis() + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert not question_answers.ucast + assert not question_answers.mcast_now + assert question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + assert zc.question_history.suppresses(question, now, {known_answer}) + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_questions_query_handler_does_not_put_qu_questions_in_history(): + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + now = current_time_millis() + _clear_cache(zc) + info = ServiceInfo( + "_hap._tcp.local.", + "qu._hap._tcp.local.", + 80, + 0, + 0, + {"md": "known"}, + "ash-2.local.", + addresses=[socket.inet_aton("1.2.3.4")], + ) + aiozc.zeroconf.registry.async_add(info) + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion("_hap._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + question.unicast = True + known_answer = r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + 10000, + "notqu._hap._tcp.local.", + ) + generated.add_question(question) + generated.add_answer_at_time(known_answer, 0) + now = r.current_time_millis() + packets = generated.packets() + question_answers = zc.query_handler.async_response([r.DNSIncoming(packet) for packet in packets], False) + assert question_answers + assert "qu._hap._tcp.local." in str(question_answers) + assert not question_answers.ucast # has not multicast recently + assert question_answers.mcast_now + assert not question_answers.mcast_aggregate + assert not question_answers.mcast_aggregate_last_second + assert not zc.question_history.suppresses(question, now, {known_answer}) + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_guard_against_low_ptr_ttl(): + """Ensure we enforce a min for PTR record ttls to avoid excessive refresh queries from ServiceBrowsers. + + Some poorly designed IoT devices can set excessively low PTR + TTLs would will cause ServiceBrowsers to flood the network + with excessive refresh queries. + """ + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + # Apple uses a 15s minimum TTL, however we do not have the same + # level of rate limit and safe guards so we use 1/4 of the recommended value + answer_with_low_ttl = r.DNSPointer( + "myservicelow_tcp._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + 2, + "low.local.", + ) + answer_with_normal_ttl = r.DNSPointer( + "myservicelow_tcp._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "normal.local.", + ) + good_bye_answer = r.DNSPointer( + "myservicelow_tcp._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + 0, + "goodbye.local.", + ) + # TTL should be adjusted to a safe value + response = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + response.add_answer_at_time(answer_with_low_ttl, 0) + response.add_answer_at_time(answer_with_normal_ttl, 0) + response.add_answer_at_time(good_bye_answer, 0) + incoming = r.DNSIncoming(response.packets()[0]) + zc.record_manager.async_updates_from_response(incoming) + + incoming_answer_low = zc.cache.async_get_unique(answer_with_low_ttl) + assert incoming_answer_low is not None + assert incoming_answer_low.ttl == const._DNS_PTR_MIN_TTL + incoming_answer_normal = zc.cache.async_get_unique(answer_with_normal_ttl) + assert incoming_answer_normal is not None + assert incoming_answer_normal.ttl == const._DNS_OTHER_TTL + assert zc.cache.async_get_unique(good_bye_answer) is None + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_duplicate_goodbye_answers_in_packet(): + """Ensure we do not throw an exception when there are duplicate goodbye records in a packet.""" + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc = aiozc.zeroconf + answer_with_normal_ttl = r.DNSPointer( + "myservicelow_tcp._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "host.local.", + ) + good_bye_answer = r.DNSPointer( + "myservicelow_tcp._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + 0, + "host.local.", + ) + response = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + response.add_answer_at_time(answer_with_normal_ttl, 0) + incoming = r.DNSIncoming(response.packets()[0]) + zc.record_manager.async_updates_from_response(incoming) + + response = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + response.add_answer_at_time(good_bye_answer, 0) + response.add_answer_at_time(good_bye_answer, 0) + incoming = r.DNSIncoming(response.packets()[0]) + zc.record_manager.async_updates_from_response(incoming) + await aiozc.async_close() + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("quick_aggregation_timing") +async def test_response_aggregation_timings(run_isolated: None) -> None: + """Verify multicast responses are aggregated. + + Aggregation / network-protection constants are scaled 10x by + ``quick_aggregation_timing``; the asserted ratios are unchanged + but each phase finishes in ~1/10 the wall time. + """ + type_ = "_mservice._tcp.local." + type_2 = "_mservice2._tcp.local." + type_3 = "_mservice3._tcp.local." + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + for queue in (aiozc.zeroconf.out_queue, aiozc.zeroconf.out_delay_queue): + queue._multicast_delay_random_min = 1 + queue._multicast_delay_random_max = 5 + + name = "xxxyyy" + registration_name = f"{name}.{type_}" + registration_name2 = f"{name}.{type_2}" + registration_name3 = f"{name}.{type_3}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_2, + registration_name2, + 80, + 0, + 0, + desc, + "ash-4.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + info3 = ServiceInfo( + type_3, + registration_name3, + 80, + 0, + 0, + desc, + "ash-4.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + aiozc.zeroconf.registry.async_add(info) + aiozc.zeroconf.registry.async_add(info2) + aiozc.zeroconf.registry.async_add(info3) + + query = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + question = r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN) + query.add_question(question) + + query2 = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + question2 = r.DNSQuestion(info2.type, const._TYPE_PTR, const._CLASS_IN) + query2.add_question(question2) + + query3 = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + question3 = r.DNSQuestion(info3.type, const._TYPE_PTR, const._CLASS_IN) + query3.add_question(question3) + + query4 = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + query4.add_question(question) + query4.add_question(question2) + + zc = aiozc.zeroconf + protocol = zc.engine.protocols[0] + + with patch.object(aiozc.zeroconf, "async_send") as send_mock: + protocol.datagram_received(query.packets()[0], ("127.0.0.1", const._MDNS_PORT)) + protocol.datagram_received(query2.packets()[0], ("127.0.0.1", const._MDNS_PORT)) + protocol.datagram_received(query.packets()[0], ("127.0.0.1", const._MDNS_PORT)) + await asyncio.sleep(0.07) + + # Should aggregate into a single answer with up to a 50ms + 5ms delay + # (scaled from 500ms + 120ms by `quick_aggregation_timing`). + calls = send_mock.mock_calls + assert len(calls) == 1 + outgoing = send_mock.call_args[0][0] + incoming = r.DNSIncoming(outgoing.packets()[0]) + zc.record_manager.async_updates_from_response(incoming) + assert info.dns_pointer() in incoming.answers() + assert info2.dns_pointer() in incoming.answers() + send_mock.reset_mock() + + protocol.datagram_received(query3.packets()[0], ("127.0.0.1", const._MDNS_PORT)) + await asyncio.sleep(0.03) + + # Should send within 12ms (scaled max random delay) since there are + # no other answers to aggregate with. + calls = send_mock.mock_calls + assert len(calls) == 1 + outgoing = send_mock.call_args[0][0] + incoming = r.DNSIncoming(outgoing.packets()[0]) + zc.record_manager.async_updates_from_response(incoming) + assert info3.dns_pointer() in incoming.answers() + send_mock.reset_mock() + + # Because the response was sent in the last 100ms (scaled 1s) we + # need to make sure the next answer is delayed at least that long. + aiozc.zeroconf.engine.protocols[0].datagram_received( + query4.packets()[0], ("127.0.0.1", const._MDNS_PORT) + ) + await asyncio.sleep(0.05) + + # After 50ms it should not have been sent. + # Protect the network against excessive packet flooding + # https://datatracker.ietf.org/doc/html/rfc6762#section-14 + calls = send_mock.mock_calls + assert len(calls) == 0 + send_mock.reset_mock() + + await asyncio.sleep(0.12) + calls = send_mock.mock_calls + assert len(calls) == 1 + outgoing = send_mock.call_args[0][0] + incoming = r.DNSIncoming(outgoing.packets()[0]) + assert info.dns_pointer() in incoming.answers() + + await aiozc.async_close() + + +@pytest.mark.asyncio +@pytest.mark.usefixtures("quick_aggregation_timing") +async def test_response_aggregation_timings_multiple( + run_isolated: None, disable_duplicate_packet_suppression: None +) -> None: + """Verify multicast responses that are aggregated do not take longer than 62ms to send. + + Aggregation / network-protection constants are scaled 10x by + ``quick_aggregation_timing`` (500ms→50ms, 200ms→20ms, 1000ms→100ms) + and the per-queue jitter is set to 1-5ms below. The asserted + ratios are the same as the production behaviour the test pins — + aggregation window, network protection, protected aggregation — + only the absolute durations are scaled. + """ + type_2 = "_mservice2._tcp.local." + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + await aiozc.zeroconf.async_wait_for_start() + # Scale the queues' random jitter to match the 10x scaled + # additional / aggregation delays; without this, the 20-120ms + # jitter would dominate the scaled window and make timing assertions + # unreliable. + for queue in (aiozc.zeroconf.out_queue, aiozc.zeroconf.out_delay_queue): + queue._multicast_delay_random_min = 1 + queue._multicast_delay_random_max = 5 + + name = "xxxyyy" + registration_name2 = f"{name}.{type_2}" + + desc = {"path": "/~paulsm/"} + info2 = ServiceInfo( + type_2, + registration_name2, + 80, + 0, + 0, + desc, + "ash-4.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + aiozc.zeroconf.registry.async_add(info2) + + query2 = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + question2 = r.DNSQuestion(info2.type, const._TYPE_PTR, const._CLASS_IN) + query2.add_question(question2) + + zc = aiozc.zeroconf + protocol = zc.engine.protocols[0] + + with patch.object(aiozc.zeroconf, "async_send") as send_mock: + send_mock.reset_mock() + protocol.datagram_received(query2.packets()[0], ("127.0.0.1", const._MDNS_PORT)) + protocol.last_time = 0 # manually reset to avoid duplicate packet suppression + protocol._recent_packets.clear() + await asyncio.sleep(0.02) + calls = send_mock.mock_calls + assert len(calls) == 1 + outgoing = send_mock.call_args[0][0] + incoming = r.DNSIncoming(outgoing.packets()[0]) + zc.record_manager.async_updates_from_response(incoming) + assert info2.dns_pointer() in incoming.answers() + + send_mock.reset_mock() + protocol.datagram_received(query2.packets()[0], ("127.0.0.1", const._MDNS_PORT)) + protocol.last_time = 0 # manually reset to avoid duplicate packet suppression + protocol._recent_packets.clear() + await asyncio.sleep(0.12) + calls = send_mock.mock_calls + assert len(calls) == 1 + outgoing = send_mock.call_args[0][0] + incoming = r.DNSIncoming(outgoing.packets()[0]) + zc.record_manager.async_updates_from_response(incoming) + assert info2.dns_pointer() in incoming.answers() + + send_mock.reset_mock() + protocol.datagram_received(query2.packets()[0], ("127.0.0.1", const._MDNS_PORT)) + protocol.last_time = 0 # manually reset to avoid duplicate packet suppression + protocol._recent_packets.clear() + protocol.datagram_received(query2.packets()[0], ("127.0.0.1", const._MDNS_PORT)) + protocol.last_time = 0 # manually reset to avoid duplicate packet suppression + protocol._recent_packets.clear() + # Scaled: minimum protected send_after is 100ms + 1-5ms random; + # sleep well under that so coarse timers on slow runners cannot + # push the send into this window and flake the assertion. + await asyncio.sleep(0.05) + calls = send_mock.mock_calls + assert len(calls) == 0 + + # 100ms (scaled 1s network protection) + # - 50ms (already slept) + # + 5ms (scaled maximum random delay) + # + 20ms (scaled protected aggregation delay) + # + 5ms (execution slack) + await asyncio.sleep(millis_to_seconds(100 - 50 + 5 + 20 + 5)) + calls = send_mock.mock_calls + assert len(calls) == 1 + outgoing = send_mock.call_args[0][0] + incoming = r.DNSIncoming(outgoing.packets()[0]) + zc.record_manager.async_updates_from_response(incoming) + assert info2.dns_pointer() in incoming.answers() + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_response_aggregation_random_delay(): + """Verify the random delay for outgoing multicast will coalesce into a single group + + When the random delay is shorter than the last outgoing group, + the groups should be combined. + """ + type_ = "_mservice._tcp.local." + type_2 = "_mservice2._tcp.local." + type_3 = "_mservice3._tcp.local." + type_4 = "_mservice4._tcp.local." + type_5 = "_mservice5._tcp.local." + + name = "xxxyyy" + registration_name = f"{name}.{type_}" + registration_name2 = f"{name}.{type_2}" + registration_name3 = f"{name}.{type_3}" + registration_name4 = f"{name}.{type_4}" + registration_name5 = f"{name}.{type_5}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-1.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_2, + registration_name2, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + info3 = ServiceInfo( + type_3, + registration_name3, + 80, + 0, + 0, + desc, + "ash-3.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info4 = ServiceInfo( + type_4, + registration_name4, + 80, + 0, + 0, + desc, + "ash-4.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info5 = ServiceInfo( + type_5, + registration_name5, + 80, + 0, + 0, + desc, + "ash-5.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + mocked_zc = unittest.mock.MagicMock() + mocked_zc.loop = asyncio.get_running_loop() + outgoing_queue = MulticastOutgoingQueue(mocked_zc, 0, 500) + + now = current_time_millis() + outgoing_queue._multicast_delay_random_min = 500 + outgoing_queue._multicast_delay_random_max = 600 + outgoing_queue.async_add(now, {info.dns_pointer(): set()}) + + # The second group should always be coalesced into first group since it will always come before + outgoing_queue._multicast_delay_random_min = 300 + outgoing_queue._multicast_delay_random_max = 400 + outgoing_queue.async_add(now, {info2.dns_pointer(): set()}) + + # The third group should always be coalesced into first group since it will always come before + outgoing_queue._multicast_delay_random_min = 100 + outgoing_queue._multicast_delay_random_max = 200 + outgoing_queue.async_add(now, {info3.dns_pointer(): set(), info4.dns_pointer(): set()}) + + assert len(outgoing_queue.queue) == 1 + assert info.dns_pointer() in outgoing_queue.queue[0].answers + assert info2.dns_pointer() in outgoing_queue.queue[0].answers + assert info3.dns_pointer() in outgoing_queue.queue[0].answers + assert info4.dns_pointer() in outgoing_queue.queue[0].answers + + # The forth group should not be coalesced because its scheduled after the last group in the queue + outgoing_queue._multicast_delay_random_min = 700 + outgoing_queue._multicast_delay_random_max = 800 + outgoing_queue.async_add(now, {info5.dns_pointer(): set()}) + + assert len(outgoing_queue.queue) == 2 + assert info.dns_pointer() not in outgoing_queue.queue[1].answers + assert info2.dns_pointer() not in outgoing_queue.queue[1].answers + assert info3.dns_pointer() not in outgoing_queue.queue[1].answers + assert info4.dns_pointer() not in outgoing_queue.queue[1].answers + assert info5.dns_pointer() in outgoing_queue.queue[1].answers + + +@pytest.mark.asyncio +async def test_future_answers_are_removed_on_send(): + """Verify any future answers scheduled to be sent are removed when we send.""" + type_ = "_mservice._tcp.local." + type_2 = "_mservice2._tcp.local." + name = "xxxyyy" + registration_name = f"{name}.{type_}" + registration_name2 = f"{name}.{type_2}" + + desc = {"path": "/~paulsm/"} + info = ServiceInfo( + type_, + registration_name, + 80, + 0, + 0, + desc, + "ash-1.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + info2 = ServiceInfo( + type_2, + registration_name2, + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.3")], + ) + mocked_zc = unittest.mock.MagicMock() + mocked_zc.loop = asyncio.get_running_loop() + outgoing_queue = MulticastOutgoingQueue(mocked_zc, 0, 0) + + now = current_time_millis() + outgoing_queue._multicast_delay_random_min = 1 + outgoing_queue._multicast_delay_random_max = 1 + outgoing_queue.async_add(now, {info.dns_pointer(): set()}) + + assert len(outgoing_queue.queue) == 1 + + outgoing_queue._multicast_delay_random_min = 2 + outgoing_queue._multicast_delay_random_max = 2 + outgoing_queue.async_add(now, {info.dns_pointer(): set()}) + + assert len(outgoing_queue.queue) == 2 + + outgoing_queue._multicast_delay_random_min = 1000 + outgoing_queue._multicast_delay_random_max = 1000 + outgoing_queue.async_add(now, {info2.dns_pointer(): set()}) + outgoing_queue.async_add(now, {info.dns_pointer(): set()}) + + assert len(outgoing_queue.queue) == 3 + + await asyncio.sleep(0.1) + outgoing_queue.async_ready() + + assert len(outgoing_queue.queue) == 1 + # The answer should get removed because we just sent it + assert info.dns_pointer() not in outgoing_queue.queue[0].answers + + # But the one we have not sent yet should still go out later + assert info2.dns_pointer() in outgoing_queue.queue[0].answers + + +@pytest.mark.asyncio +async def test_add_listener_warns_when_not_using_record_update_listener(caplog): + """Log when a listener is added that is not using RecordUpdateListener as a base class.""" + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc: Zeroconf = aiozc.zeroconf + updated = [] + + class MyListener: + """A RecordUpdateListener that does not implement update_records.""" + + def async_update_records(self, zc: Zeroconf, now: float, records: list[r.RecordUpdate]) -> None: + """Update multiple records in one shot.""" + updated.extend(records) + + zc.add_listener(MyListener(), None) # type: ignore[arg-type] + await asyncio.sleep(0) # flush out any call soons + assert ( + "listeners passed to async_add_listener must inherit from RecordUpdateListener" in caplog.text + or "TypeError: Argument 'listener' has incorrect type" in caplog.text + ) + + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_updates_iteration_safe(): + """Ensure we can safely iterate over the async_updates.""" + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc: Zeroconf = aiozc.zeroconf + updated = [] + good_bye_answer = r.DNSPointer( + "myservicelow_tcp._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + 0, + "goodbye.local.", + ) + + class OtherListener(r.RecordUpdateListener): + """A RecordUpdateListener that does not implement update_records.""" + + def async_update_records(self, zc: Zeroconf, now: float, records: list[r.RecordUpdate]) -> None: + """Update multiple records in one shot.""" + updated.extend(records) + + other = OtherListener() + + class ListenerThatAddsListener(r.RecordUpdateListener): + """A RecordUpdateListener that does not implement update_records.""" + + def async_update_records(self, zc: Zeroconf, now: float, records: list[r.RecordUpdate]) -> None: + """Update multiple records in one shot.""" + updated.extend(records) + zc.async_add_listener(other, None) + + zc.async_add_listener(ListenerThatAddsListener(), None) + await asyncio.sleep(0) # flush out any call soons + + # This should not raise RuntimeError: set changed size during iteration + zc.record_manager.async_updates( + now=current_time_millis(), records=[r.RecordUpdate(good_bye_answer, None)] + ) + + assert len(updated) == 1 + await aiozc.async_close() + + +@pytest.mark.asyncio +async def test_async_updates_complete_iteration_safe(): + """Ensure we can safely iterate over the async_updates_complete.""" + + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + zc: Zeroconf = aiozc.zeroconf + + class OtherListener(r.RecordUpdateListener): + """A RecordUpdateListener that does not implement update_records.""" + + def async_update_records_complete(self) -> None: + """Update multiple records in one shot.""" + + other = OtherListener() + + class ListenerThatAddsListener(r.RecordUpdateListener): + """A RecordUpdateListener that does not implement update_records.""" + + def async_update_records_complete(self) -> None: + """Update multiple records in one shot.""" + zc.async_add_listener(other, None) + + zc.async_add_listener(ListenerThatAddsListener(), None) + await asyncio.sleep(0) # flush out any call soons + + # This should not raise RuntimeError: set changed size during iteration + zc.record_manager.async_updates_complete(False) + await aiozc.async_close() diff --git a/tests/test_history.py b/tests/test_history.py new file mode 100644 index 000000000..0dd40a06b --- /dev/null +++ b/tests/test_history.py @@ -0,0 +1,194 @@ +"""Unit tests for _history.py.""" + +from __future__ import annotations + +import zeroconf as r +from zeroconf import const +from zeroconf._history import QuestionHistory + + +def test_question_suppression(): + history = QuestionHistory() + + question = r.DNSQuestion("_hap._tcp._local.", const._TYPE_PTR, const._CLASS_IN) + now = r.current_time_millis() + other_known_answers: set[r.DNSRecord] = { + r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + 10000, + "known-to-other._hap._tcp.local.", + ) + } + our_known_answers: set[r.DNSRecord] = { + r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + 10000, + "known-to-us._hap._tcp.local.", + ) + } + + history.add_question_at_time(question, now, other_known_answers) + + # Verify the question is suppressed if the known answers are the same + assert history.suppresses(question, now, other_known_answers) + + # Verify the question is suppressed if we know the answer to all the known answers + assert history.suppresses(question, now, other_known_answers | our_known_answers) + + # Verify the question is not suppressed if our known answers do no include the ones in the last question + assert not history.suppresses(question, now, set()) + + # Verify the question is not suppressed if our known answers do no include the ones in the last question + assert not history.suppresses(question, now, our_known_answers) + + # Verify the question is no longer suppressed after 1s + assert not history.suppresses(question, now + 1000, other_known_answers) + + +def test_question_expire(): + history = QuestionHistory() + + now = r.current_time_millis() + question = r.DNSQuestion("_hap._tcp._local.", const._TYPE_PTR, const._CLASS_IN) + other_known_answers: set[r.DNSRecord] = { + r.DNSPointer( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + 10000, + "known-to-other._hap._tcp.local.", + created=now, + ) + } + history.add_question_at_time(question, now, other_known_answers) + + # Verify the question is suppressed if the known answers are the same + assert history.suppresses(question, now, other_known_answers) + + history.async_expire(now) + + # Verify the question is suppressed if the known answers are the same since the cache hasn't expired + assert history.suppresses(question, now, other_known_answers) + + history.async_expire(now + 1000) + + # Verify the question not longer suppressed since the cache has expired + assert not history.suppresses(question, now, other_known_answers) + + +def test_question_history_bounded(): + """History keeps a hard cap so a LAN flood cannot grow it without bound.""" + history = QuestionHistory() + now = r.current_time_millis() + answers: set[r.DNSRecord] = set() + + cap = const._MAX_QUESTION_HISTORY_ENTRIES + for i in range(cap + 500): + q = r.DNSQuestion(f"_svc{i}._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + history.add_question_at_time(q, now, answers) + + assert len(history._history) <= cap + + +def test_question_history_evicts_oldest_first(): + """When at cap, the oldest insertion is dropped first.""" + history = QuestionHistory() + now = r.current_time_millis() + answers: set[r.DNSRecord] = set() + + cap = const._MAX_QUESTION_HISTORY_ENTRIES + first = r.DNSQuestion("_first._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + history.add_question_at_time(first, now, answers) + + # Add `cap` more fresh, non-expired entries — one past the cap — so the + # final insertion forces oldest-first eviction of `first`. + for i in range(cap): + q = r.DNSQuestion(f"_svc{i}._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + history.add_question_at_time(q, now, answers) + + assert first not in history._history + assert len(history._history) <= cap + + +def test_question_history_opportunistic_expire(): + """Adding past the cap first drops expired entries before evicting fresh ones.""" + history = QuestionHistory() + old = r.current_time_millis() + answers: set[r.DNSRecord] = set() + + cap = const._MAX_QUESTION_HISTORY_ENTRIES + for i in range(cap): + q = r.DNSQuestion(f"_stale{i}._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + history.add_question_at_time(q, old, answers) + + # All prior entries are now stale (>999ms old). Adding one more should + # trigger opportunistic expiry rather than evicting only the oldest one. + fresh_now = old + const._DUPLICATE_QUESTION_INTERVAL + 1 + fresh = r.DNSQuestion("_fresh._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + history.add_question_at_time(fresh, fresh_now, answers) + + assert fresh in history._history + assert len(history._history) == 1 + + +def _make_known_answers(count: int) -> set[r.DNSRecord]: + """Build a set of ``count`` distinct PTR records for use as known-answers.""" + return { + r.DNSPointer( + "_svc._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN, + 10000, + f"target{i}._svc._tcp.local.", + ) + for i in range(count) + } + + +def test_question_history_oversized_known_answers_dropped(): + """Known-answer sets above the per-entry cap are not stored.""" + history = QuestionHistory() + now = r.current_time_millis() + question = r.DNSQuestion("_svc._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + + oversized = _make_known_answers(const._MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY + 1) + history.add_question_at_time(question, now, oversized) + + assert question not in history._history + + +def test_question_history_oversized_preserves_existing_entry(): + """An oversized payload must not displace a pre-existing small entry.""" + history = QuestionHistory() + now = r.current_time_millis() + question = r.DNSQuestion("_svc._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + + small = _make_known_answers(2) + history.add_question_at_time(question, now, small) + assert history.suppresses(question, now, small) + + # An oversized follow-up must be ignored; the small entry stays and + # continues to drive suppression. + oversized = _make_known_answers(const._MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY + 1) + history.add_question_at_time(question, now, oversized) + + stored_set = history._history[question][1] + assert stored_set is small + assert history.suppresses(question, now, small) + + +def test_question_history_at_cap_known_answers_is_stored(): + """A known-answer set exactly at the per-entry cap is retained.""" + history = QuestionHistory() + now = r.current_time_millis() + question = r.DNSQuestion("_svc._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + + at_cap = _make_known_answers(const._MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY) + history.add_question_at_time(question, now, at_cap) + + assert question in history._history + assert history._history[question][1] is at_cap diff --git a/tests/test_init.py b/tests/test_init.py new file mode 100644 index 000000000..37534ad74 --- /dev/null +++ b/tests/test_init.py @@ -0,0 +1,193 @@ +"""Unit tests for zeroconf.py""" + +from __future__ import annotations + +import logging +import socket +import time +import unittest.mock +from unittest.mock import patch + +import pytest + +import zeroconf as r +from zeroconf import ServiceInfo, Zeroconf, const + +from . import _inject_responses + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +class Names(unittest.TestCase): + def test_long_name(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + question = r.DNSQuestion( + "this.is.a.very.long.name.with.lots.of.parts.in.it.local.", + const._TYPE_SRV, + const._CLASS_IN, + ) + generated.add_question(question) + r.DNSIncoming(generated.packets()[0]) + + def test_exceedingly_long_name(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + name = f"{'part.' * 1000}local." + question = r.DNSQuestion(name, const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + r.DNSIncoming(generated.packets()[0]) + + def test_extra_exceedingly_long_name(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + name = f"{'part.' * 4000}local." + question = r.DNSQuestion(name, const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + r.DNSIncoming(generated.packets()[0]) + + def test_exceedingly_long_name_part(self): + name = f"{'a' * 1000}.local." + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + question = r.DNSQuestion(name, const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + self.assertRaises(r.NamePartTooLongException, generated.packets) + + def test_same_name(self): + name = "paired.local." + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + question = r.DNSQuestion(name, const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + generated.add_question(question) + r.DNSIncoming(generated.packets()[0]) + + @pytest.mark.usefixtures("quick_timing") + def test_verify_name_change_with_lots_of_names(self): + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + # create a bunch of servers + type_ = "_my-service._tcp.local." + name = "a wonderful service" + server_count = 300 + self.generate_many_hosts(zc, type_, name, server_count) + + # verify that name changing works + self.verify_name_change(zc, type_, name, server_count) + + zc.close() + + def test_large_packet_exception_log_handling(self): + """Verify we downgrade debug after warning.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + with ( + patch("zeroconf._logger.log.warning") as mocked_log_warn, + patch("zeroconf._logger.log.debug") as mocked_log_debug, + ): + # now that we have a long packet in our possession, let's verify the + # exception handling. + out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) + out.data.append(b"\0" * 10000) + + # mock the zeroconf logger and check for the correct logging backoff + call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count + # try to send an oversized packet + zc.send(out) + assert mocked_log_warn.call_count == call_counts[0] + zc.send(out) + assert mocked_log_warn.call_count == call_counts[0] + + # mock the zeroconf logger and check for the correct logging backoff + call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count + # force receive on oversized packet + zc.send(out, const._MDNS_ADDR, const._MDNS_PORT) + zc.send(out, const._MDNS_ADDR, const._MDNS_PORT) + time.sleep(0.3) + r.log.debug( + "warn %d debug %d was %s", + mocked_log_warn.call_count, + mocked_log_debug.call_count, + call_counts, + ) + assert mocked_log_debug.call_count > call_counts[0] + + # close our zeroconf which will close the sockets + zc.close() + + def verify_name_change(self, zc, type_, name, number_hosts): + desc = {"path": "/~paulsm/"} + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + # verify name conflict + self.assertRaises(r.NonUniqueNameException, zc.register_service, info_service) + + # verify no name conflict https://tools.ietf.org/html/rfc6762#section-6.6 + zc.register_service(info_service, cooperating_responders=True) + + # Create a new object since allow_name_change will mutate the + # original object and then we will have the wrong service + # in the registry + info_service2 = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + desc, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + zc.register_service(info_service2, allow_name_change=True) + assert info_service2.name.split(".")[0] == f"{name}-{number_hosts + 1}" + + def generate_many_hosts(self, zc, type_, name, number_hosts): + block_size = 25 + number_hosts = int((number_hosts - 1) / block_size + 1) * block_size + out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) + for i in range(1, number_hosts + 1): + next_name = name if i == 1 else f"{name}-{i}" + self.generate_host(out, next_name, type_) + + _inject_responses(zc, [r.DNSIncoming(packet) for packet in out.packets()]) + + @staticmethod + def generate_host(out, host_name, type_): + name = ".".join((host_name, type_)) + out.add_answer_at_time( + r.DNSPointer(type_, const._TYPE_PTR, const._CLASS_IN, const._DNS_OTHER_TTL, name), + 0, + ) + out.add_answer_at_time( + r.DNSService( + type_, + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + 80, + name, + ), + 0, + ) diff --git a/tests/test_listener.py b/tests/test_listener.py new file mode 100644 index 000000000..9f076be0f --- /dev/null +++ b/tests/test_listener.py @@ -0,0 +1,439 @@ +"""Unit tests for zeroconf._listener""" + +from __future__ import annotations + +import logging +import unittest +import unittest.mock +from unittest.mock import MagicMock, patch + +import zeroconf as r +from zeroconf import ( + ServiceInfo, + Zeroconf, + _engine, + _listener, + const, + current_time_millis, +) +from zeroconf._protocol import outgoing +from zeroconf._protocol.incoming import DNSIncoming + +from . import QuestionHistoryWithoutSuppression + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +def test_guard_against_oversized_packets(): + """Ensure we do not process oversized packets. + + These packets can quickly overwhelm the system. + """ + zc = Zeroconf(interfaces=["127.0.0.1"]) + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + + for _i in range(5000): + generated.add_answer_at_time( + r.DNSText( + "packet{i}.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 500, + b"path=/~paulsm/", + ), + 0, + ) + + try: + # We are patching to generate an oversized packet + with ( + patch.object(outgoing, "_MAX_MSG_ABSOLUTE", 100000), + patch.object(outgoing, "_MAX_MSG_TYPICAL", 100000), + ): + over_sized_packet = generated.packets()[0] + assert len(over_sized_packet) > const._MAX_MSG_ABSOLUTE + except AttributeError: + # cannot patch with cython + zc.close() + return + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + okpacket_record = r.DNSText( + "okpacket.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 500, + b"path=/~paulsm/", + ) + + generated.add_answer_at_time( + okpacket_record, + 0, + ) + ok_packet = generated.packets()[0] + + # We cannot test though the network interface as some operating systems + # will guard against the oversized packet and we won't see it. + listener = _listener.AsyncListener(zc) + listener.transport = unittest.mock.MagicMock() + + listener.datagram_received(ok_packet, ("127.0.0.1", const._MDNS_PORT)) + assert zc.cache.async_get_unique(okpacket_record) is not None + + listener.datagram_received(over_sized_packet, ("127.0.0.1", const._MDNS_PORT)) + assert ( + zc.cache.async_get_unique( + r.DNSText( + "packet0.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 500, + b"path=/~paulsm/", + ) + ) + is None + ) + + logging.getLogger("zeroconf").setLevel(logging.INFO) + + listener.datagram_received(over_sized_packet, ("::1", const._MDNS_PORT, 1, 1)) + assert ( + zc.cache.async_get_unique( + r.DNSText( + "packet0.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 500, + b"path=/~paulsm/", + ) + ) + is None + ) + + zc.close() + + +def test_guard_against_duplicate_packets(): + """Ensure we do not process duplicate packets. + These packets can quickly overwhelm the system. + """ + zc = Zeroconf(interfaces=["127.0.0.1"]) + zc.registry.async_add( + ServiceInfo( + "_http._tcp.local.", + "Test._http._tcp.local.", + server="Test._http._tcp.local.", + port=4, + ) + ) + zc.question_history = QuestionHistoryWithoutSuppression() + + class SubListener(_listener.AsyncListener): + def handle_query_or_defer( + self, + msg: DNSIncoming, + addr: str, + port: int, + transport: _engine._WrappedTransport, + v6_flow_scope: tuple[()] | tuple[int, int] = (), + ) -> None: + """Handle a query or defer it for later processing.""" + super().handle_query_or_defer(msg, addr, port, transport, v6_flow_scope) + + listener = SubListener(zc) + listener.transport = MagicMock() + + query = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + question = r.DNSQuestion("x._http._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + query.add_question(question) + packet_with_qm_question = query.packets()[0] + + query3 = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + question3 = r.DNSQuestion("x._ay._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + query3.add_question(question3) + packet_with_qm_question2 = query3.packets()[0] + + query2 = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + question2 = r.DNSQuestion("x._http._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + question2.unicast = True + query2.add_question(question2) + packet_with_qu_question = query2.packets()[0] + + addrs = ("1.2.3.4", 43) + + with patch.object(listener, "handle_query_or_defer") as _handle_query_or_defer: + start_time = current_time_millis() + + listener._process_datagram_at_time( + False, + len(packet_with_qm_question), + start_time, + packet_with_qm_question, + addrs, + ) + _handle_query_or_defer.assert_called_once() + _handle_query_or_defer.reset_mock() + + # Now call with the same packet again and handle_query_or_defer should not fire + listener._process_datagram_at_time( + False, + len(packet_with_qm_question), + start_time, + packet_with_qm_question, + addrs, + ) + _handle_query_or_defer.assert_not_called() + _handle_query_or_defer.reset_mock() + + # Now walk time forward 1100 milliseconds + new_time = start_time + 1100 + # Now call with the same packet again and handle_query_or_defer should fire + listener._process_datagram_at_time( + False, + len(packet_with_qm_question), + new_time, + packet_with_qm_question, + addrs, + ) + _handle_query_or_defer.assert_called_once() + _handle_query_or_defer.reset_mock() + + # Now call with the different packet and handle_query_or_defer should fire + listener._process_datagram_at_time( + False, + len(packet_with_qm_question2), + new_time, + packet_with_qm_question2, + addrs, + ) + _handle_query_or_defer.assert_called_once() + _handle_query_or_defer.reset_mock() + + # Replay the first packet — the recency window remembers more than + # just the most recent payload, so this is a duplicate. + listener._process_datagram_at_time( + False, + len(packet_with_qm_question), + new_time, + packet_with_qm_question, + addrs, + ) + _handle_query_or_defer.assert_not_called() + _handle_query_or_defer.reset_mock() + + # Now call with the different packet with qu question and handle_query_or_defer should fire + listener._process_datagram_at_time( + False, + len(packet_with_qu_question), + new_time, + packet_with_qu_question, + addrs, + ) + _handle_query_or_defer.assert_called_once() + _handle_query_or_defer.reset_mock() + + # Now call again with the same packet that has a qu question and handle_query_or_defer should fire + listener._process_datagram_at_time( + False, + len(packet_with_qu_question), + new_time, + packet_with_qu_question, + addrs, + ) + _handle_query_or_defer.assert_called_once() + _handle_query_or_defer.reset_mock() + + log.setLevel(logging.WARNING) + + # Replay the QM packet with debug disabled — suppression must hold + # off the debug-log path too. + listener._process_datagram_at_time( + False, + len(packet_with_qm_question), + new_time, + packet_with_qm_question, + addrs, + ) + _handle_query_or_defer.assert_not_called() + _handle_query_or_defer.reset_mock() + + # Now call with garbage + listener._process_datagram_at_time(False, len(b"garbage"), new_time, b"garbage", addrs) + _handle_query_or_defer.assert_not_called() + _handle_query_or_defer.reset_mock() + + zc.close() + + +def test_guard_against_alternating_duplicate_packets() -> None: + """Alternating two distinct payloads must not bypass duplicate suppression.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + zc.registry.async_add( + ServiceInfo( + "_http._tcp.local.", + "Test._http._tcp.local.", + server="Test._http._tcp.local.", + port=4, + ) + ) + zc.question_history = QuestionHistoryWithoutSuppression() + + class SubListener(_listener.AsyncListener): + def handle_query_or_defer( + self, + msg: DNSIncoming, + addr: str, + port: int, + transport: _engine._WrappedTransport, + v6_flow_scope: tuple[()] | tuple[int, int] = (), + ) -> None: + super().handle_query_or_defer(msg, addr, port, transport, v6_flow_scope) + + listener = SubListener(zc) + listener.transport = MagicMock() + + query_a = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + query_a.add_question(r.DNSQuestion("a._http._tcp.local.", const._TYPE_PTR, const._CLASS_IN)) + packet_a = query_a.packets()[0] + + query_b = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + query_b.add_question(r.DNSQuestion("b._http._tcp.local.", const._TYPE_PTR, const._CLASS_IN)) + packet_b = query_b.packets()[0] + + assert packet_a != packet_b + + addrs = ("1.2.3.4", 43) + + with patch.object(listener, "handle_query_or_defer") as _handle_query_or_defer: + now = current_time_millis() + + # Prime both payloads. + listener._process_datagram_at_time(False, len(packet_a), now, packet_a, addrs) + listener._process_datagram_at_time(False, len(packet_b), now, packet_b, addrs) + assert _handle_query_or_defer.call_count == 2 + _handle_query_or_defer.reset_mock() + + for _ in range(4): + listener._process_datagram_at_time(False, len(packet_a), now, packet_a, addrs) + listener._process_datagram_at_time(False, len(packet_b), now, packet_b, addrs) + _handle_query_or_defer.assert_not_called() + + zc.close() + + +def test_recent_packets_window_is_bounded() -> None: + """Distinct payloads beyond the recency window evict oldest entries.""" + zc = Zeroconf(interfaces=["127.0.0.1"]) + zc.registry.async_add( + ServiceInfo( + "_http._tcp.local.", + "Test._http._tcp.local.", + server="Test._http._tcp.local.", + port=4, + ) + ) + zc.question_history = QuestionHistoryWithoutSuppression() + + class SubListener(_listener.AsyncListener): + def handle_query_or_defer( + self, + msg: DNSIncoming, + addr: str, + port: int, + transport: _engine._WrappedTransport, + v6_flow_scope: tuple[()] | tuple[int, int] = (), + ) -> None: + super().handle_query_or_defer(msg, addr, port, transport, v6_flow_scope) + + listener = SubListener(zc) + listener.transport = MagicMock() + + addrs = ("1.2.3.4", 43) + now = current_time_millis() + + packets = [] + for i in range(const._RECENT_PACKETS_MAX + 4): + query = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + query.add_question(r.DNSQuestion(f"n{i}._http._tcp.local.", const._TYPE_PTR, const._CLASS_IN)) + packets.append(query.packets()[0]) + + with patch.object(listener, "handle_query_or_defer") as _handle_query_or_defer: + for packet in packets: + listener._process_datagram_at_time(False, len(packet), now, packet, addrs) + assert _handle_query_or_defer.call_count == len(packets) + _handle_query_or_defer.reset_mock() + + # The newest _RECENT_PACKETS_MAX entries are still in the + # window; replaying them must be suppressed. Checked before + # replaying the evicted ones below since that would mutate the + # window and could mask an off-by-one in eviction. + kept = packets[-const._RECENT_PACKETS_MAX :] + for packet in kept: + listener._process_datagram_at_time(False, len(packet), now, packet, addrs) + _handle_query_or_defer.assert_not_called() + + # The oldest packets should have been evicted and now replay. + evicted = packets[: len(packets) - const._RECENT_PACKETS_MAX] + for packet in evicted: + listener._process_datagram_at_time(False, len(packet), now, packet, addrs) + assert _handle_query_or_defer.call_count == len(evicted) + + zc.close() + + +def test_recent_packets_miss_with_small_now_is_not_suppressed() -> None: + """A cache miss must not trigger suppression when `now` is below the suppression interval.""" + # time.monotonic() can start near zero on freshly booted systems, so + # `now - _DUPLICATE_PACKET_SUPPRESSION_INTERVAL` is negative for the + # first second of process lifetime. A 0.0 default on the recency + # dict would let any negative `now - INTERVAL` satisfy the compare + # and suppress legitimate traffic. + zc = Zeroconf(interfaces=["127.0.0.1"]) + zc.registry.async_add( + ServiceInfo( + "_http._tcp.local.", + "Test._http._tcp.local.", + server="Test._http._tcp.local.", + port=4, + ) + ) + zc.question_history = QuestionHistoryWithoutSuppression() + + class SubListener(_listener.AsyncListener): + def handle_query_or_defer( + self, + msg: DNSIncoming, + addr: str, + port: int, + transport: _engine._WrappedTransport, + v6_flow_scope: tuple[()] | tuple[int, int] = (), + ) -> None: + super().handle_query_or_defer(msg, addr, port, transport, v6_flow_scope) + + listener = SubListener(zc) + listener.transport = MagicMock() + + query = r.DNSOutgoing(const._FLAGS_QR_QUERY, multicast=True) + query.add_question(r.DNSQuestion("a._http._tcp.local.", const._TYPE_PTR, const._CLASS_IN)) + packet = query.packets()[0] + + addrs = ("1.2.3.4", 43) + + with patch.object(listener, "handle_query_or_defer") as _handle_query_or_defer: + listener._process_datagram_at_time(False, len(packet), 0.0, packet, addrs) + _handle_query_or_defer.assert_called_once() + + zc.close() diff --git a/tests/test_logger.py b/tests/test_logger.py new file mode 100644 index 000000000..8042e49c3 --- /dev/null +++ b/tests/test_logger.py @@ -0,0 +1,185 @@ +"""Unit tests for logger.py.""" + +from __future__ import annotations + +import logging +from unittest.mock import call, patch + +from zeroconf import _logger +from zeroconf._logger import _MAX_SEEN_LOGS, QuietLogger, _mark_seen, set_logger_level_if_unset + + +def test_loading_logger(): + """Test loading logger does not change level unless it is unset.""" + log = logging.getLogger("zeroconf") + log.setLevel(logging.CRITICAL) + set_logger_level_if_unset() + log = logging.getLogger("zeroconf") + assert log.level == logging.CRITICAL + + log = logging.getLogger("zeroconf") + log.setLevel(logging.NOTSET) + set_logger_level_if_unset() + log = logging.getLogger("zeroconf") + assert log.level == logging.WARNING + + +def test_log_warning_once(): + """Test we only log with warning level once.""" + _logger._seen_logs.clear() + quiet_logger = QuietLogger() + with ( + patch("zeroconf._logger.log.warning") as mock_log_warning, + patch("zeroconf._logger.log.debug") as mock_log_debug, + ): + quiet_logger.log_warning_once("the warning") + + assert mock_log_warning.mock_calls + assert not mock_log_debug.mock_calls + + with ( + patch("zeroconf._logger.log.warning") as mock_log_warning, + patch("zeroconf._logger.log.debug") as mock_log_debug, + ): + quiet_logger.log_warning_once("the warning") + + assert not mock_log_warning.mock_calls + assert mock_log_debug.mock_calls + + +def test_log_exception_warning(): + """Test we only log with warning level once.""" + _logger._seen_logs.clear() + quiet_logger = QuietLogger() + with ( + patch("zeroconf._logger.log.warning") as mock_log_warning, + patch("zeroconf._logger.log.debug") as mock_log_debug, + ): + quiet_logger.log_exception_warning("the exception warning") + + assert mock_log_warning.mock_calls + assert not mock_log_debug.mock_calls + + with ( + patch("zeroconf._logger.log.warning") as mock_log_warning, + patch("zeroconf._logger.log.debug") as mock_log_debug, + ): + quiet_logger.log_exception_warning("the exception warning") + + assert not mock_log_warning.mock_calls + assert mock_log_debug.mock_calls + + +def test_llog_exception_debug(): + """Test we only log with a trace once.""" + _logger._seen_logs.clear() + quiet_logger = QuietLogger() + with patch("zeroconf._logger.log.debug") as mock_log_debug: + quiet_logger.log_exception_debug("the exception") + + assert mock_log_debug.mock_calls == [call("the exception", exc_info=True)] + + with patch("zeroconf._logger.log.debug") as mock_log_debug: + quiet_logger.log_exception_debug("the exception") + + assert mock_log_debug.mock_calls == [call("the exception", exc_info=False)] + + +def test_mark_seen_absorbs_runtime_error_during_eviction() -> None: + """Concurrent mutation can make ``iter(seen)`` raise ``RuntimeError``. + + Free-threaded (3.14t) and multi-instance sync callers share + ``_seen_logs``; if another thread mutates it between ``iter()`` + and ``next()`` the iterator raises ``RuntimeError``. + ``_mark_seen`` must absorb that and still insert the new key. + """ + + class RacyDict(dict[str, None]): + def __iter__(self): # type: ignore[override] + raise RuntimeError("dictionary changed size during iteration") + + seen: dict[str, None] = RacyDict() + for i in range(_MAX_SEEN_LOGS): + seen[f"k-{i}"] = None + assert _mark_seen(seen, "new-key") is True + assert "new-key" in seen + + +def test_mark_seen_drains_drift_above_cap() -> None: + """``_mark_seen`` drains a drifted-over-cap dict back to the cap. + + Concurrent inserts on the free-threaded build can leave the dict + transiently above ``_MAX_SEEN_LOGS`` (e.g. two threads both passed + the ``len < cap`` check and both inserted). The next non-racing + call must drain the accumulated overshoot, not just evict one + entry — otherwise the cap silently inflates with thread count. + """ + seen: dict[str, None] = {} + drift = 10 + for i in range(_MAX_SEEN_LOGS + drift): + seen[f"k-{i}"] = None + assert len(seen) == _MAX_SEEN_LOGS + drift + assert _mark_seen(seen, "new-key") is True + assert len(seen) == _MAX_SEEN_LOGS + assert "new-key" in seen + for i in range(drift + 1): + assert f"k-{i}" not in seen + + +def test_mark_seen_drains_drift_on_hit_path() -> None: + """``_mark_seen`` drains drift even when ``key`` is already cached. + + A hit-heavy workload after a contention burst (e.g. the same + exception text deduplicated repeatedly) must still correct the + overshoot — otherwise the dict can sit permanently above the cap + until a miss happens to come along. + """ + seen: dict[str, None] = {} + drift = 10 + for i in range(_MAX_SEEN_LOGS + drift): + seen[f"k-{i}"] = None + # Hit on a non-oldest key — survives the drift drain. + hit_key = f"k-{_MAX_SEEN_LOGS}" + assert _mark_seen(seen, hit_key) is False + assert len(seen) == _MAX_SEEN_LOGS + assert hit_key in seen + for i in range(drift): + assert f"k-{i}" not in seen + + +def test_seen_logs_is_bounded() -> None: + """``_seen_logs`` stays at the cap and evicts oldest-first (FIFO).""" + _logger._seen_logs.clear() + overflow = 5 + with patch("zeroconf._logger.log.warning"), patch("zeroconf._logger.log.debug"): + for i in range(_MAX_SEEN_LOGS + overflow): + QuietLogger.log_warning_once(f"warning-{i}") + assert len(_logger._seen_logs) == _MAX_SEEN_LOGS + for i in range(overflow): + assert f"warning-{i}" not in _logger._seen_logs + for i in range(_MAX_SEEN_LOGS, _MAX_SEEN_LOGS + overflow): + assert f"warning-{i}" in _logger._seen_logs + + +def test_log_exception_once(): + """Test we only log with warning level once.""" + _logger._seen_logs.clear() + quiet_logger = QuietLogger() + exc = Exception() + with ( + patch("zeroconf._logger.log.warning") as mock_log_warning, + patch("zeroconf._logger.log.debug") as mock_log_debug, + ): + quiet_logger.log_exception_once(exc, "the exceptional exception warning") + + assert mock_log_warning.mock_calls + assert not mock_log_debug.mock_calls + + with ( + patch("zeroconf._logger.log.warning") as mock_log_warning, + patch("zeroconf._logger.log.debug") as mock_log_debug, + ): + quiet_logger.log_exception_once(exc, "the exceptional exception warning") + + assert not mock_log_warning.mock_calls + assert mock_log_debug.mock_calls diff --git a/tests/test_protocol.py b/tests/test_protocol.py new file mode 100644 index 000000000..903c66927 --- /dev/null +++ b/tests/test_protocol.py @@ -0,0 +1,1301 @@ +"""Unit tests for zeroconf._protocol""" + +from __future__ import annotations + +import copy +import logging +import os +import socket +import struct +import unittest.mock +from typing import cast + +import pytest + +import zeroconf as r +from zeroconf import DNSHinfo, DNSIncoming, DNSText, const, current_time_millis +from zeroconf._logger import _MAX_SEEN_LOGS +from zeroconf._protocol import incoming as _incoming_module + +from . import has_working_ipv6 + +log = logging.getLogger("zeroconf") +original_logging_level = logging.NOTSET + + +def setup_module(): + global original_logging_level + original_logging_level = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + if original_logging_level != logging.NOTSET: + log.setLevel(original_logging_level) + + +class PacketGeneration(unittest.TestCase): + def test_parse_own_packet_simple(self): + generated = r.DNSOutgoing(0) + r.DNSIncoming(generated.packets()[0]) + + def test_parse_own_packet_simple_unicast(self): + generated = r.DNSOutgoing(0, False) + r.DNSIncoming(generated.packets()[0]) + + def test_parse_own_packet_flags(self): + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + r.DNSIncoming(generated.packets()[0]) + + def test_parse_own_packet_question(self): + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + generated.add_question(r.DNSQuestion("testname.local.", const._TYPE_SRV, const._CLASS_IN)) + r.DNSIncoming(generated.packets()[0]) + + def test_parse_own_packet_nsec(self): + answer = r.DNSNsec( + "eufy HomeBase2-2464._hap._tcp.local.", + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "eufy HomeBase2-2464._hap._tcp.local.", + [const._TYPE_TXT, const._TYPE_SRV], + ) + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time(answer, 0) + parsed = r.DNSIncoming(generated.packets()[0]) + assert answer in parsed.answers() + + # Now with the higher RD type first + answer = r.DNSNsec( + "eufy HomeBase2-2464._hap._tcp.local.", + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "eufy HomeBase2-2464._hap._tcp.local.", + [const._TYPE_SRV, const._TYPE_TXT], + ) + + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time(answer, 0) + parsed = r.DNSIncoming(generated.packets()[0]) + assert answer in parsed.answers() + + # Types > 255 should raise an exception + answer_invalid_types = r.DNSNsec( + "eufy HomeBase2-2464._hap._tcp.local.", + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "eufy HomeBase2-2464._hap._tcp.local.", + [const._TYPE_TXT, const._TYPE_SRV, 1000], + ) + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time(answer_invalid_types, 0) + with pytest.raises(ValueError, match="rdtype 1000 is too large for NSEC"): + generated.packets() + + # Empty rdtypes are not allowed + answer_invalid_types = r.DNSNsec( + "eufy HomeBase2-2464._hap._tcp.local.", + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "eufy HomeBase2-2464._hap._tcp.local.", + [], + ) + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time(answer_invalid_types, 0) + with pytest.raises(ValueError, match="NSEC must have at least one rdtype"): + generated.packets() + + def test_parse_own_packet_response(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSService( + "æøå.local.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "foo.local.", + ), + 0, + ) + parsed = r.DNSIncoming(generated.packets()[0]) + assert len(generated.answers) == 1 + assert len(generated.answers) == len(parsed.answers()) + + def test_adding_empty_answer(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + None, + 0, + ) + generated.add_answer_at_time( + r.DNSService( + "æøå.local.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "foo.local.", + ), + 0, + ) + parsed = r.DNSIncoming(generated.packets()[0]) + assert len(generated.answers) == 1 + assert len(generated.answers) == len(parsed.answers()) + + def test_adding_expired_answer(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + generated.add_answer_at_time( + r.DNSService( + "æøå.local.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "foo.local.", + ), + current_time_millis() + 1000000, + ) + parsed = r.DNSIncoming(generated.packets()[0]) + assert len(generated.answers) == 0 + assert len(generated.answers) == len(parsed.answers()) + + def test_match_question(self): + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion("testname.local.", const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + parsed = r.DNSIncoming(generated.packets()[0]) + assert len(generated.questions) == 1 + assert len(generated.questions) == len(parsed.questions) + assert question == parsed.questions[0] + + def test_suppress_answer(self): + query_generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + question = r.DNSQuestion("testname.local.", const._TYPE_SRV, const._CLASS_IN) + query_generated.add_question(question) + answer1 = r.DNSService( + "testname1.local.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "foo.local.", + ) + staleanswer2 = r.DNSService( + "testname2.local.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + int(const._DNS_HOST_TTL / 2), + 0, + 0, + 80, + "foo.local.", + ) + answer2 = r.DNSService( + "testname2.local.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "foo.local.", + ) + query_generated.add_answer_at_time(answer1, 0) + query_generated.add_answer_at_time(staleanswer2, 0) + query = r.DNSIncoming(query_generated.packets()[0]) + + # Should be suppressed + response = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + response.add_answer(query, answer1) + assert len(response.answers) == 0 + + # Should not be suppressed, TTL in query is too short + response.add_answer(query, answer2) + assert len(response.answers) == 1 + + # Should not be suppressed, name is different + tmp = copy.copy(answer1) + tmp.key = "testname3.local." + tmp.name = "testname3.local." + response.add_answer(query, tmp) + assert len(response.answers) == 2 + + # Should not be suppressed, type is different + tmp = copy.copy(answer1) + tmp.type = const._TYPE_A + response.add_answer(query, tmp) + assert len(response.answers) == 3 + + # Should not be suppressed, class is different + tmp = copy.copy(answer1) + tmp.class_ = const._CLASS_NONE + response.add_answer(query, tmp) + assert len(response.answers) == 4 + + # ::TODO:: could add additional tests for DNSAddress, DNSHinfo, DNSPointer, DNSText, DNSService + + def test_dns_hinfo(self): + generated = r.DNSOutgoing(0) + generated.add_additional_answer(DNSHinfo("irrelevant", const._TYPE_HINFO, 0, 0, "cpu", "os")) + parsed = r.DNSIncoming(generated.packets()[0]) + answer = cast(r.DNSHinfo, parsed.answers()[0]) + assert answer.cpu == "cpu" + assert answer.os == "os" + + generated = r.DNSOutgoing(0) + generated.add_additional_answer(DNSHinfo("irrelevant", const._TYPE_HINFO, 0, 0, "cpu", "x" * 257)) + self.assertRaises(r.NamePartTooLongException, generated.packets) + + def test_many_questions(self): + """Test many questions get separated into multiple packets.""" + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + questions = [] + for i in range(100): + question = r.DNSQuestion(f"testname{i}.local.", const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + questions.append(question) + assert len(generated.questions) == 100 + + packets = generated.packets() + assert len(packets) == 2 + assert len(packets[0]) < const._MAX_MSG_TYPICAL + assert len(packets[1]) < const._MAX_MSG_TYPICAL + + parsed1 = r.DNSIncoming(packets[0]) + assert len(parsed1.questions) == 85 + parsed2 = r.DNSIncoming(packets[1]) + assert len(parsed2.questions) == 15 + + def test_many_questions_with_many_known_answers(self): + """Test many questions and known answers get separated into multiple packets.""" + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + questions = [] + for _ in range(30): + question = r.DNSQuestion("_hap._tcp.local.", const._TYPE_PTR, const._CLASS_IN) + generated.add_question(question) + questions.append(question) + assert len(generated.questions) == 30 + now = current_time_millis() + for _ in range(200): + known_answer = r.DNSPointer( + "myservice{i}_tcp._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "123.local.", + ) + generated.add_answer_at_time(known_answer, now) + packets = generated.packets() + assert len(packets) == 3 + assert len(packets[0]) <= const._MAX_MSG_TYPICAL + assert len(packets[1]) <= const._MAX_MSG_TYPICAL + assert len(packets[2]) <= const._MAX_MSG_TYPICAL + + parsed1 = r.DNSIncoming(packets[0]) + assert len(parsed1.questions) == 30 + assert len(parsed1.answers()) == 88 + assert parsed1.truncated + parsed2 = r.DNSIncoming(packets[1]) + assert len(parsed2.questions) == 0 + assert len(parsed2.answers()) == 101 + assert parsed2.truncated + parsed3 = r.DNSIncoming(packets[2]) + assert len(parsed3.questions) == 0 + assert len(parsed3.answers()) == 11 + assert not parsed3.truncated + + def test_massive_probe_packet_split(self): + """Test probe with many authoritative answers.""" + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_AA) + questions = [] + for _ in range(30): + question = r.DNSQuestion( + "_hap._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + ) + generated.add_question(question) + questions.append(question) + assert len(generated.questions) == 30 + for _ in range(200): + authorative_answer = r.DNSPointer( + "myservice{i}_tcp._tcp.local.", + const._TYPE_PTR, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "123.local.", + ) + generated.add_authorative_answer(authorative_answer) + packets = generated.packets() + assert len(packets) == 3 + assert len(packets[0]) <= const._MAX_MSG_TYPICAL + assert len(packets[1]) <= const._MAX_MSG_TYPICAL + assert len(packets[2]) <= const._MAX_MSG_TYPICAL + + parsed1 = r.DNSIncoming(packets[0]) + assert parsed1.questions[0].unicast is True + assert len(parsed1.questions) == 30 + assert parsed1.num_questions == 30 + assert parsed1.num_authorities == 88 + assert parsed1.truncated + parsed2 = r.DNSIncoming(packets[1]) + assert len(parsed2.questions) == 0 + assert parsed2.num_authorities == 101 + assert parsed2.truncated + parsed3 = r.DNSIncoming(packets[2]) + assert len(parsed3.questions) == 0 + assert parsed3.num_authorities == 11 + assert not parsed3.truncated + + def test_only_one_answer_can_by_large(self): + """Test that only the first answer in each packet can be large. + + https://datatracker.ietf.org/doc/html/rfc6762#section-17 + """ + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + query = r.DNSIncoming(r.DNSOutgoing(const._FLAGS_QR_QUERY).packets()[0]) + for _i in range(3): + generated.add_answer( + query, + r.DNSText( + "zoom._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + 1200, + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==" * 100, + ), + ) + generated.add_answer( + query, + r.DNSService( + "testname1.local.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + 80, + "foo.local.", + ), + ) + assert len(generated.answers) == 4 + + packets = generated.packets() + assert len(packets) == 4 + assert len(packets[0]) <= const._MAX_MSG_ABSOLUTE + assert len(packets[0]) > const._MAX_MSG_TYPICAL + + assert len(packets[1]) <= const._MAX_MSG_ABSOLUTE + assert len(packets[1]) > const._MAX_MSG_TYPICAL + + assert len(packets[2]) <= const._MAX_MSG_ABSOLUTE + assert len(packets[2]) > const._MAX_MSG_TYPICAL + + assert len(packets[3]) <= const._MAX_MSG_TYPICAL + + for packet in packets: + parsed = r.DNSIncoming(packet) + assert len(parsed.answers()) == 1 + + def test_questions_do_not_end_up_every_packet(self): + """Test that questions are not sent again when multiple packets are needed. + + https://datatracker.ietf.org/doc/html/rfc6762#section-7.2 + Sometimes a Multicast DNS querier will already have too many answers + to fit in the Known-Answer Section of its query packets.... It MUST + immediately follow the packet with another query packet containing no + questions and as many more Known-Answer records as will fit. + """ + + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + for i in range(35): + question = r.DNSQuestion(f"testname{i}.local.", const._TYPE_SRV, const._CLASS_IN) + generated.add_question(question) + answer = r.DNSService( + f"testname{i}.local.", + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + 80, + f"foo{i}.local.", + ) + generated.add_answer_at_time(answer, 0) + + assert len(generated.questions) == 35 + assert len(generated.answers) == 35 + + packets = generated.packets() + assert len(packets) == 2 + assert len(packets[0]) <= const._MAX_MSG_TYPICAL + assert len(packets[1]) <= const._MAX_MSG_TYPICAL + + parsed1 = r.DNSIncoming(packets[0]) + assert len(parsed1.questions) == 35 + assert len(parsed1.answers()) == 33 + + parsed2 = r.DNSIncoming(packets[1]) + assert len(parsed2.questions) == 0 + assert len(parsed2.answers()) == 2 + + +class PacketForm(unittest.TestCase): + def test_transaction_id(self): + """ID must be zero in a DNS-SD packet""" + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + bytes = generated.packets()[0] + id = bytes[0] << 8 | bytes[1] + assert id == 0 + + def test_setting_id(self): + """Test setting id in the constructor""" + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY, id_=4444) + assert generated.id == 4444 + + def test_query_header_bits(self): + generated = r.DNSOutgoing(const._FLAGS_QR_QUERY) + bytes = generated.packets()[0] + flags = bytes[2] << 8 | bytes[3] + assert flags == 0x0 + + def test_response_header_bits(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + bytes = generated.packets()[0] + flags = bytes[2] << 8 | bytes[3] + assert flags == 0x8000 + + def test_numbers(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + bytes = generated.packets()[0] + (num_questions, num_answers, num_authorities, num_additionals) = struct.unpack("!4H", bytes[4:12]) + assert num_questions == 0 + assert num_answers == 0 + assert num_authorities == 0 + assert num_additionals == 0 + + def test_numbers_questions(self): + generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE) + question = r.DNSQuestion("testname.local.", const._TYPE_SRV, const._CLASS_IN) + for _i in range(10): + generated.add_question(question) + bytes = generated.packets()[0] + (num_questions, num_answers, num_authorities, num_additionals) = struct.unpack("!4H", bytes[4:12]) + assert num_questions == 10 + assert num_answers == 0 + assert num_authorities == 0 + assert num_additionals == 0 + + +class TestDnsIncoming(unittest.TestCase): + def test_incoming_exception_handling(self): + generated = r.DNSOutgoing(0) + packet = generated.packets()[0] + packet = packet[:8] + b"deadbeef" + packet[8:] + parsed = r.DNSIncoming(packet) + parsed = r.DNSIncoming(packet) + assert parsed.valid is False + + def test_incoming_unknown_type(self): + generated = r.DNSOutgoing(0) + answer = r.DNSAddress("a", const._TYPE_SOA, const._CLASS_IN, 1, b"a") + generated.add_additional_answer(answer) + packet = generated.packets()[0] + parsed = r.DNSIncoming(packet) + assert len(parsed.answers()) == 0 + assert parsed.is_query() != parsed.is_response() + + def test_incoming_circular_reference(self): + assert not r.DNSIncoming( + bytes.fromhex( + "01005e0000fb542a1bf0577608004500006897934000ff11d81bc0a86a31e00000fb" + "14e914e90054f9b2000084000000000100000000095f7365727669636573075f646e" + "732d7364045f756470056c6f63616c00000c0001000011940018105f73706f746966" + "792d636f6e6e656374045f746370c023" + ) + ).valid + + @unittest.skipIf(not has_working_ipv6(), "Requires IPv6") + @unittest.skipIf(os.environ.get("SKIP_IPV6"), "IPv6 tests disabled") + def test_incoming_ipv6(self): + addr = "2606:2800:220:1:248:1893:25c8:1946" # example.com + packed = socket.inet_pton(socket.AF_INET6, addr) + generated = r.DNSOutgoing(0) + answer = r.DNSAddress("domain", const._TYPE_AAAA, const._CLASS_IN | const._CLASS_UNIQUE, 1, packed) + generated.add_additional_answer(answer) + packet = generated.packets()[0] + parsed = r.DNSIncoming(packet) + record = parsed.answers()[0] + assert isinstance(record, r.DNSAddress) + assert record.address == packed + + +def test_dns_compression_rollback_for_corruption(): + """Verify rolling back does not lead to dns compression corruption.""" + out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) + address = socket.inet_pton(socket.AF_INET, "192.168.208.5") + + additionals = [ + { + "name": "HASS Bridge ZJWH FF5137._hap._tcp.local.", + "address": address, + "port": 51832, + "text": b"\x13md=HASS Bridge" + b" ZJWH\x06pv=1.0\x14id=01:6B:30:FF:51:37\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=L0m/aQ==", + }, + { + "name": "HASS Bridge 3K9A C2582A._hap._tcp.local.", + "address": address, + "port": 51834, + "text": b"\x13md=HASS Bridge" + b" 3K9A\x06pv=1.0\x14id=E2:AA:5B:C2:58:2A\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b2CnzQ==", + }, + { + "name": "Master Bed TV CEDB27._hap._tcp.local.", + "address": address, + "port": 51830, + "text": b"\x10md=Master Bed" + b" TV\x06pv=1.0\x14id=9E:B7:44:CE:DB:27\x05c#=18\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=CVj1kw==", + }, + { + "name": "Living Room TV 921B77._hap._tcp.local.", + "address": address, + "port": 51833, + "text": b"\x11md=Living Room" + b" TV\x06pv=1.0\x14id=11:61:E7:92:1B:77\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=qU77SQ==", + }, + { + "name": "HASS Bridge ZC8X FF413D._hap._tcp.local.", + "address": address, + "port": 51829, + "text": b"\x13md=HASS Bridge" + b" ZC8X\x06pv=1.0\x14id=96:14:45:FF:41:3D\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=b0QZlg==", + }, + { + "name": "HASS Bridge WLTF 4BE61F._hap._tcp.local.", + "address": address, + "port": 51837, + "text": b"\x13md=HASS Bridge" + b" WLTF\x06pv=1.0\x14id=E0:E7:98:4B:E6:1F\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=ahAISA==", + }, + { + "name": "FrontdoorCamera 8941D1._hap._tcp.local.", + "address": address, + "port": 54898, + "text": b"\x12md=FrontdoorCamera\x06pv=1.0\x14id=9F:B7:DC:89:41:D1\x04c#=2\x04" + b"s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=0+MXmA==", + }, + { + "name": "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + "address": address, + "port": 51836, + "text": b"\x13md=HASS Bridge" + b" W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=6fLM5A==", + }, + { + "name": "HASS Bridge Y9OO EFF0A7._hap._tcp.local.", + "address": address, + "port": 51838, + "text": b"\x13md=HASS Bridge" + b" Y9OO\x06pv=1.0\x14id=D3:FE:98:EF:F0:A7\x04c#=2\x04s#=1\x04ff=0\x04" + b"ci=2\x04sf=0\x0bsh=u3bdfw==", + }, + { + "name": "Snooze Room TV 6B89B0._hap._tcp.local.", + "address": address, + "port": 51835, + "text": b"\x11md=Snooze Room" + b" TV\x06pv=1.0\x14id=5F:D5:70:6B:89:B0\x05c#=17\x04s#=1\x04ff=0\x05" + b"ci=31\x04sf=0\x0bsh=xNTqsg==", + }, + { + "name": "AlexanderHomeAssistant 74651D._hap._tcp.local.", + "address": address, + "port": 54811, + "text": b"\x19md=AlexanderHomeAssistant\x06pv=1.0\x14id=59:8A:0B:74:65:1D\x05" + b"c#=14\x04s#=1\x04ff=0\x04ci=2\x04sf=0\x0bsh=ccZLPA==", + }, + { + "name": "HASS Bridge OS95 39C053._hap._tcp.local.", + "address": address, + "port": 51831, + "text": b"\x13md=HASS Bridge" + b" OS95\x06pv=1.0\x14id=7E:8C:E6:39:C0:53\x05c#=12\x04s#=1\x04ff=0\x04ci=2" + b"\x04sf=0\x0bsh=Xfe5LQ==", + }, + ] + + out.add_answer_at_time( + DNSText( + "HASS Bridge W9DN 5B5CC5._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1" + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + + for record in additionals: + out.add_additional_answer( + r.DNSService( + record["name"], # type: ignore + const._TYPE_SRV, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + 0, + 0, + record["port"], # type: ignore + record["name"], # type: ignore + ) + ) + out.add_additional_answer( + r.DNSText( + record["name"], # type: ignore + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + record["text"], # type: ignore + ) + ) + out.add_additional_answer( + r.DNSAddress( + record["name"], # type: ignore + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_HOST_TTL, + record["address"], # type: ignore + ) + ) + + for packet in out.packets(): + # Verify we can process the packets we created to + # ensure there is no corruption with the dns compression + incoming = r.DNSIncoming(packet) + assert incoming.valid is True + assert ( + len(incoming.answers()) + == incoming.num_answers + incoming.num_authorities + incoming.num_additionals + ) + + +def test_tc_bit_in_query_packet(): + """Verify the TC bit is set when known answers exceed the packet size.""" + out = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_AA) + type_ = "_hap._tcp.local." + out.add_question(r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN)) + + for i in range(30): + out.add_answer_at_time( + DNSText( + f"HASS Bridge W9DN {i}._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1" + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + + packets = out.packets() + assert len(packets) == 3 + + first_packet = r.DNSIncoming(packets[0]) + assert first_packet.truncated + assert first_packet.valid is True + + second_packet = r.DNSIncoming(packets[1]) + assert second_packet.truncated + assert second_packet.valid is True + + third_packet = r.DNSIncoming(packets[2]) + assert not third_packet.truncated + assert third_packet.valid is True + + +def test_tc_bit_not_set_in_answer_packet(): + """Verify the TC bit is not set when there are no questions and answers exceed the packet size.""" + out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) + for i in range(30): + out.add_answer_at_time( + DNSText( + f"HASS Bridge W9DN {i}._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1" + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + + packets = out.packets() + assert len(packets) == 3 + + first_packet = r.DNSIncoming(packets[0]) + assert not first_packet.truncated + assert first_packet.valid is True + + second_packet = r.DNSIncoming(packets[1]) + assert not second_packet.truncated + assert second_packet.valid is True + + third_packet = r.DNSIncoming(packets[2]) + assert not third_packet.truncated + assert third_packet.valid is True + + +# MDNS 76 Standard query 0xffc4 PTR _raop._tcp.local, "QM" question +def test_qm_packet_parser(): + """Test we can parse a query packet with the QM bit.""" + qm_packet = ( + b"\xff\xc4\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x05_raop\x04_tcp\x05local\x00\x00\x0c\x00\x01" + ) + parsed = DNSIncoming(qm_packet) + assert parsed.questions[0].unicast is False + assert ",QM," in str(parsed.questions[0]) + + +# MDNS 115 Standard query 0x0000 PTR _companion-link._tcp.local, "QU" question OPT +def test_qu_packet_parser(): + """Test we can parse a query packet with the QU bit.""" + qu_packet = ( + b"\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01\x0f_companion-link\x04_tcp\x05local" + b"\x00\x00\x0c\x80\x01\x00\x00)\x05\xa0\x00\x00\x11\x94\x00\x12\x00\x04\x00\x0e\x00dz{\x8a6\x9czF\x84,\xcaQ\xff" + ) + parsed = DNSIncoming(qu_packet) + assert parsed.questions[0].unicast is True + assert ",QU," in str(parsed.questions[0]) + + +def test_parse_packet_with_nsec_record(): + """Test we can parse a packet with an NSEC record.""" + nsec_packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x03\x08_meshcop\x04_udp\x05local\x00\x00\x0c\x00" + b"\x01\x00\x00\x11\x94\x00\x0f\x0cMyHome54 (2)\xc0\x0c\xc0+\x00\x10\x80\x01\x00\x00\x11\x94\x00" + b")\x0bnn=MyHome54\x13xp=695034D148CC4784\x08tv=0.0.0\xc0+\x00!\x80\x01\x00\x00\x00x\x00\x15\x00" + b"\x00\x00\x00\xc0'\x0cMaster-Bed-2\xc0\x1a\xc0+\x00/\x80\x01\x00\x00\x11\x94\x00\t\xc0+\x00\x05" + b"\x00\x00\x80\x00@" + ) + parsed = DNSIncoming(nsec_packet) + nsec_record = cast(r.DNSNsec, parsed.answers()[3]) + assert "nsec," in str(nsec_record) + assert nsec_record.rdtypes == [16, 33] + assert nsec_record.next_name == "MyHome54 (2)._meshcop._udp.local." + + +def test_nsec_bitmap_length_overruns_record_end(): + """Reject NSEC bitmap whose declared length runs past the record boundary.""" + # 0 questions, 2 answers. Answer 1 is a malformed NSEC: rdlength=9, but the + # bitmap window claims length=255 — overrunning the record. Answer 2 is a + # PTR that must still parse because the offset for the next record stays + # pinned to the NSEC's declared end. + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x02\x00\x00\x00\x00" + b"\x04test\x05local\x00" + b"\x00\x2f\x80\x01" + b"\x00\x00\x11\x94" + b"\x00\x09" + b"\xc0\x0c" + b"\x00\xff" + b"\x80\x00\x00\x00\x00" + b"\xc0\x0c" + b"\x00\x0c\x00\x01" + b"\x00\x00\x11\x94" + b"\x00\x02" + b"\xc0\x0c" + ) + parsed = r.DNSIncoming(packet) + answers = parsed.answers() + ptrs = [a for a in answers if isinstance(a, r.DNSPointer)] + assert len(ptrs) == 1 + assert ptrs[0].alias == "test.local." + # The malformed NSEC must not surface — if it did, it would carry rdtypes + # synthesized from bytes past the record boundary. + assert not any(isinstance(a, r.DNSNsec) for a in answers) + + +def test_nsec_bitmap_zero_length_window_rejected(): + """A bitmap window with length=0 violates RFC 4034 §4.1.2 and must be rejected.""" + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x02\x00\x00\x00\x00" + b"\x04test\x05local\x00" + b"\x00\x2f\x80\x01" + b"\x00\x00\x11\x94" + b"\x00\x04" + b"\xc0\x0c" + b"\x00\x00" + b"\xc0\x0c" + b"\x00\x0c\x00\x01" + b"\x00\x00\x11\x94" + b"\x00\x02" + b"\xc0\x0c" + ) + parsed = r.DNSIncoming(packet) + answers = parsed.answers() + ptrs = [a for a in answers if isinstance(a, r.DNSPointer)] + assert len(ptrs) == 1 + assert not any(isinstance(a, r.DNSNsec) for a in answers) + + +def test_nsec_bitmap_truncated_window_header_rejected(): + """Reject NSEC bitmap with a trailing byte too short to hold a window header.""" + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x02\x00\x00\x00\x00" + b"\x04test\x05local\x00" + b"\x00\x2f\x80\x01" + b"\x00\x00\x11\x94" + b"\x00\x06" + b"\xc0\x0c" + b"\x00\x01\x80" + b"\xff" + b"\xc0\x0c" + b"\x00\x0c\x00\x01" + b"\x00\x00\x11\x94" + b"\x00\x02" + b"\xc0\x0c" + ) + parsed = r.DNSIncoming(packet) + answers = parsed.answers() + ptrs = [a for a in answers if isinstance(a, r.DNSPointer)] + assert len(ptrs) == 1 + assert ptrs[0].alias == "test.local." + assert not any(isinstance(a, r.DNSNsec) for a in answers) + + +def test_txt_rdlength_overruns_packet_rejected(): + """A TXT record with rdlength past the buffer must not enter the cache. + + Python slicing silently truncates when the slice end exceeds the buffer, + so without a bounds check in ``_read_string`` a malformed wire frame + would land in the cache carrying a payload shorter than the rdlength + declared, leaving the parser desynchronized for downstream records. + """ + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x00" + b"\x04test\x05local\x00" + b"\x00\x10\x80\x01" + b"\x00\x00\x11\x94" + b"\xff\xff" + b"\x05hello" + ) + parsed = r.DNSIncoming(packet) + assert parsed.valid + assert parsed.answers() == [] + + +def test_hinfo_character_string_length_overruns_record_rejected(): + """A HINFO character string declaring more bytes than remain must be rejected.""" + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x00" + b"\x04test\x05local\x00" + b"\x00\x0d\x80\x01" + b"\x00\x00\x11\x94" + b"\x00\x07" + b"\x03cpu" + b"\xff\xff\xff" + ) + parsed = r.DNSIncoming(packet) + assert parsed.valid + assert not any(isinstance(a, r.DNSHinfo) for a in parsed.answers()) + + +def test_a_record_rdlength_overruns_packet_rejected(): + """An A record whose 4-byte address would walk past the buffer must be rejected.""" + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x00" + b"\x04test\x05local\x00" + b"\x00\x01\x80\x01" + b"\x00\x00\x11\x94" + b"\x00\x04" + b"\xc0\xa8" + ) + parsed = r.DNSIncoming(packet) + assert parsed.valid + assert not any(isinstance(a, r.DNSAddress) for a in parsed.answers()) + + +def test_record_consuming_more_than_rdlength_dropped_and_resyncs(): + """A record whose decoded fields overrun its rdlength must drop and resync. + + The first answer is a HINFO with ``rdlength=2`` and rdata ``\\x01x`` (one + char string ``"x"``). The second character string's length byte then comes + from the next record's name (``\\x00``, root domain), so the HINFO would + silently parse as ``cpu="x", os=""`` but leave the offset one byte past + the declared end, smearing the second record's framing. With the per-record + boundary check the bogus HINFO is dropped and the second record decodes. + """ + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x02\x00\x00\x00\x00" + b"\x04test\x05local\x00" + b"\x00\x0d\x80\x01" + b"\x00\x00\x11\x94" + b"\x00\x02" + b"\x01x" + b"\x00" + b"\x00\x0c\x00\x01" + b"\x00\x00\x11\x94" + b"\x00\x02" + b"\xc0\x0c" + ) + parsed = r.DNSIncoming(packet) + answers = parsed.answers() + assert not any(isinstance(a, r.DNSHinfo) for a in answers) + ptrs = [a for a in answers if isinstance(a, r.DNSPointer)] + assert len(ptrs) == 1 + assert ptrs[0].alias == "test.local." + + +def test_records_same_packet_share_fate(): + """Test records in the same packet all have the same created time.""" + out = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_AA) + type_ = "_hap._tcp.local." + out.add_question(r.DNSQuestion(type_, const._TYPE_PTR, const._CLASS_IN)) + + for i in range(30): + out.add_answer_at_time( + DNSText( + f"HASS Bridge W9DN {i}._hap._tcp.local.", + const._TYPE_TXT, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + b"\x13md=HASS Bridge W9DN\x06pv=1.0\x14id=11:8E:DB:5B:5C:C5\x05c#=12\x04s#=1" + b"\x04ff=0\x04ci=2\x04sf=0\x0bsh=6fLM5A==", + ), + 0, + ) + + for packet in out.packets(): + dnsin = DNSIncoming(packet) + first_time = dnsin.answers()[0].created + for answer in dnsin.answers(): + assert answer.created == first_time + + +def test_dns_compression_invalid_skips_bad_name_compress_in_question(): + """Test our wire parser can skip bad compression in questions.""" + packet = ( + b"\x00\x00\x00\x00\x00\x04\x00\x00\x00\x07\x00\x00\x11homeassistant1128\x05l" + b"ocal\x00\x00\xff\x00\x014homeassistant1128 [534a4794e5ed41879ecf012252d3e02" + b"a]\x0c_workstation\x04_tcp\xc0\x1e\x00\xff\x00\x014homeassistant1127 [534a47" + b"94e5ed41879ecf012252d3e02a]\xc0^\x00\xff\x00\x014homeassistant1123 [534a479" + b"4e5ed41879ecf012252d3e02a]\xc0^\x00\xff\x00\x014homeassistant1118 [534a4794" + b"e5ed41879ecf012252d3e02a]\xc0^\x00\xff\x00\x01\xc0\x0c\x00\x01\x80" + b"\x01\x00\x00\x00x\x00\x04\xc0\xa8<\xc3\xc0v\x00\x10\x80\x01\x00\x00\x00" + b"x\x00\x01\x00\xc0v\x00!\x80\x01\x00\x00\x00x\x00\x1f\x00\x00\x00\x00" + b"\x00\x00\x11homeassistant1127\x05local\x00\xc0\xb1\x00\x10\x80" + b"\x01\x00\x00\x00x\x00\x01\x00\xc0\xb1\x00!\x80\x01\x00\x00\x00x\x00\x1f" + b"\x00\x00\x00\x00\x00\x00\x11homeassistant1123\x05local\x00\xc0)\x00\x10\x80" + b"\x01\x00\x00\x00x\x00\x01\x00\xc0)\x00!\x80\x01\x00\x00\x00x\x00\x1f" + b"\x00\x00\x00\x00\x00\x00\x11homeassistant1128\x05local\x00" + ) + parsed = r.DNSIncoming(packet) + assert len(parsed.questions) == 4 + + +def test_dns_compression_all_invalid(caplog): + """Test our wire parser can skip all invalid data.""" + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x00!roborock-vacuum-s5e_miio416" + b"112328\x00\x00/\x80\x01\x00\x00\x00x\x00\t\xc0P\x00\x05@\x00\x00\x00\x00" + ) + parsed = r.DNSIncoming(packet, ("2.4.5.4", 5353)) + assert len(parsed.questions) == 0 + assert len(parsed.answers()) == 0 + + assert " Unable to parse; skipping record" in caplog.text + + +def test_invalid_next_name_ignored(): + """Test our wire parser does not throw an an invalid next name. + + The RFC states it should be ignored when used with mDNS. + """ + packet = ( + b"\x00\x00\x00\x00\x00\x01\x00\x02\x00\x00\x00\x00\x07Android\x05local\x00\x00" + b"\xff\x00\x01\xc0\x0c\x00/\x00\x01\x00\x00\x00x\x00\x08\xc02\x00\x04@" + b"\x00\x00\x08\xc0\x0c\x00\x01\x00\x01\x00\x00\x00x\x00\x04\xc0\xa8X<" + ) + parsed = r.DNSIncoming(packet) + assert len(parsed.questions) == 1 + assert len(parsed.answers()) == 2 + + +def test_dns_compression_invalid_skips_record(): + """Test our wire parser can skip records we do not know how to parse.""" + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x06\x00\x00\x00\x00\x04_hap\x04_tcp\x05local\x00\x00\x0c" + b"\x00\x01\x00\x00\x11\x94\x00\x16\x13eufy HomeBase2-2464\xc0\x0c\x04Eufy\xc0\x16\x00/" + b"\x80\x01\x00\x00\x00x\x00\x08\xc0\xa6\x00\x04@\x00\x00\x08\xc0'\x00/\x80\x01\x00\x00" + b"\x11\x94\x00\t\xc0'\x00\x05\x00\x00\x80\x00@\xc0=\x00\x01\x80\x01\x00\x00\x00x\x00\x04" + b"\xc0\xa8Dp\xc0'\x00!\x80\x01\x00\x00\x00x\x00\x08\x00\x00\x00\x00\xd1_\xc0=\xc0'\x00" + b"\x10\x80\x01\x00\x00\x11\x94\x00K\x04c#=1\x04ff=2\x14id=38:71:4F:6B:76:00\x08md=T8010" + b"\x06pv=1.1\x05s#=75\x04sf=1\x04ci=2\x0bsh=xaQk4g==" + ) + parsed = r.DNSIncoming(packet) + answer = r.DNSNsec( + "eufy HomeBase2-2464._hap._tcp.local.", + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "eufy HomeBase2-2464._hap._tcp.local.", + [const._TYPE_TXT, const._TYPE_SRV], + ) + assert answer in parsed.answers() + + +def test_dns_compression_points_forward(): + """Test our wire parser can unpack nsec records with compression.""" + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x07\x00\x00\x00\x00\x0eTV Beneden (2)" + b"\x10_androidtvremote\x04_tcp\x05local\x00\x00\x10\x80\x01\x00\x00\x11" + b"\x94\x00\x15\x14bt=D8:13:99:AC:98:F1\xc0\x0c\x00/\x80\x01\x00\x00\x11" + b"\x94\x00\t\xc0\x0c\x00\x05\x00\x00\x80\x00@\tAndroid-3\xc01\x00/\x80" + b"\x01\x00\x00\x00x\x00\x08\xc0\x9c\x00\x04@\x00\x00\x08\xc0l\x00\x01\x80" + b"\x01\x00\x00\x00x\x00\x04\xc0\xa8X\x0f\xc0\x0c\x00!\x80\x01\x00\x00\x00" + b"x\x00\x08\x00\x00\x00\x00\x19B\xc0l\xc0\x1b\x00\x0c\x00\x01\x00\x00\x11" + b"\x94\x00\x02\xc0\x0c\t_services\x07_dns-sd\x04_udp\xc01\x00\x0c\x00\x01" + b"\x00\x00\x11\x94\x00\x02\xc0\x1b" + ) + parsed = r.DNSIncoming(packet) + answer = r.DNSNsec( + "TV Beneden (2)._androidtvremote._tcp.local.", + const._TYPE_NSEC, + const._CLASS_IN | const._CLASS_UNIQUE, + const._DNS_OTHER_TTL, + "TV Beneden (2)._androidtvremote._tcp.local.", + [const._TYPE_TXT, const._TYPE_SRV], + ) + assert answer in parsed.answers() + + +def test_dns_compression_points_to_itself(): + """Test our wire parser does not loop forever when a compression pointer points to itself.""" + packet = ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x06domain\x05local\x00\x00\x01" + b"\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05\xc0(\x00\x01\x80\x01\x00\x00\x00" + b"\x01\x00\x04\xc0\xa8\xd0\x06" + ) + parsed = r.DNSIncoming(packet) + assert len(parsed.answers()) == 1 + + +def test_dns_compression_points_beyond_packet(): + """Test our wire parser does not fail when the compression pointer points beyond the packet.""" + packet = ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x06domain\x05local\x00\x00\x01" + b"\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05\xe7\x0f\x00\x01\x80\x01\x00\x00" + b"\x00\x01\x00\x04\xc0\xa8\xd0\x06" + ) + parsed = r.DNSIncoming(packet) + assert len(parsed.answers()) == 1 + + +def test_dns_compression_generic_failure(caplog): + """Test our wire parser does not loop forever when dns compression is corrupt.""" + packet = ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x06domain\x05local\x00\x00\x01" + b"\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05-\x0c\x00\x01\x80\x01\x00\x00" + b"\x00\x01\x00\x04\xc0\xa8\xd0\x06" + ) + parsed = r.DNSIncoming(packet, ("1.2.3.4", 5353)) + assert len(parsed.answers()) == 1 + assert "Received invalid packet from ('1.2.3.4', 5353)" in caplog.text + + +def test_seen_logs_is_bounded(): + """Corrupt packets from varying peers fill ``_seen_logs`` exactly to the cap.""" + packet = ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x06domain\x05local\x00\x00\x01" + b"\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05-\x0c\x00\x01\x80\x01\x00\x00" + b"\x00\x01\x00\x04\xc0\xa8\xd0\x06" + ) + overflow = 5 + _incoming_module._seen_logs.clear() + # Snapshot the actual key the parser inserted per port. This is whatever + # ``str(exc_info()[1])`` produces today — the test stays agnostic to the + # exception text format so a future normalization of the message (see + # the discussion on #1714) doesn't break the assertions, while still + # pinning that the parser exception path actually entered the dict. + keys_per_port: list[str] = [] + for port in range(_MAX_SEEN_LOGS + overflow): + r.DNSIncoming(packet, ("1.2.3.4", port)) + keys_per_port.append(next(reversed(_incoming_module._seen_logs))) + # Bound is hit exactly. + assert len(_incoming_module._seen_logs) == _MAX_SEEN_LOGS + # Each port produced a distinct dedup key — a regression that dropped + # the per-packet-varying component (e.g. self.source) from the exception + # text would collapse all 517 calls to one key and fail this. + assert len(set(keys_per_port)) == _MAX_SEEN_LOGS + overflow + # FIFO eviction by key identity (no substring matching on the message + # format): the earliest ports' keys are gone, the latest ports' remain. + for port in range(overflow): + assert keys_per_port[port] not in _incoming_module._seen_logs + for port in range(_MAX_SEEN_LOGS, _MAX_SEEN_LOGS + overflow): + assert keys_per_port[port] in _incoming_module._seen_logs + + +def test_label_length_attack(): + """Test our wire parser does not loop forever when the name exceeds 253 chars.""" + packet = ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x01d\x01d\x01d\x01d\x01d\x01d" + b"\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d" + b"\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d" + b"\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d" + b"\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d" + b"\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d" + b"\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d" + b"\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d" + b"\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x01d\x00\x00\x01\x80" + b"\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05\xc0\x0c\x00\x01\x80\x01\x00\x00\x00" + b"\x01\x00\x04\xc0\xa8\xd0\x06" + ) + parsed = r.DNSIncoming(packet) + assert len(parsed.answers()) == 0 + + +def test_label_compression_attack(): + """Test our wire parser does not loop forever when exceeding the maximum number of labels.""" + packet = ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x03atk\x00\x00\x01\x80" + b"\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03" + b"atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\x03atk\xc0" + b"\x0c\x00\x01\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x06" + ) + parsed = r.DNSIncoming(packet) + assert len(parsed.answers()) == 1 + + +def test_dns_compression_pointer_chain_depth_attack() -> None: + """Test our wire parser rejects deeply chained compression pointers without recursing.""" + # Build a packet with one question whose name is a 1500-deep chain of forward + # compression pointers, ending in a root label. Each pointer is 2 bytes, + # so chain length easily exceeds CPython's default recursion limit. + header = b"\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00" + # Question at offset 12: pointer to offset 18 (past the question's type/class). + question_name = bytes([0xC0, 18]) + question_type_class = b"\x00\x01\x00\x01" + chain_depth = 1500 + chain = bytearray() + for i in range(chain_depth): + target = 18 + 2 * (i + 1) + chain.append(0xC0 | (target >> 8)) + chain.append(target & 0xFF) + chain.append(0x00) + packet = header + question_name + question_type_class + bytes(chain) + parsed = r.DNSIncoming(packet, ("1.2.3.4", 5353)) + assert parsed.valid is False + assert parsed.questions == [] + + +def test_dns_compression_loop_attack(): + """Test our wire parser does not loop forever when dns compression is in a loop.""" + packet = ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x03atk\x03dns\x05loc" + b"al\xc0\x10\x00\x01\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05\x04a" + b"tk2\x04dns2\xc0\x14\x00\x01\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05" + b"\x04atk3\xc0\x10\x00\x01\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0" + b"\x05\x04atk4\x04dns5\xc0\x14\x00\x01\x80\x01\x00\x00\x00\x01\x00\x04\xc0" + b"\xa8\xd0\x05\x04atk5\x04dns2\xc0^\x00\x01\x80\x01\x00\x00\x00\x01\x00" + b"\x04\xc0\xa8\xd0\x05\xc0s\x00\x01\x80\x01\x00\x00\x00\x01\x00" + b"\x04\xc0\xa8\xd0\x05\xc0s\x00\x01\x80\x01\x00\x00\x00\x01\x00" + b"\x04\xc0\xa8\xd0\x05" + ) + parsed = r.DNSIncoming(packet) + assert len(parsed.answers()) == 0 + + +def test_txt_after_invalid_nsec_name_still_usable(): + """Test that we can see the txt record after the invalid nsec record.""" + packet = ( + b"\x00\x00\x84\x00\x00\x00\x00\x06\x00\x00\x00\x00\x06_sonos\x04_tcp\x05loc" + b"al\x00\x00\x0c\x00\x01\x00\x00\x11\x94\x00\x15\x12Sonos-542A1BC9220E" + b"\xc0\x0c\x12Sonos-542A1BC9220E\xc0\x18\x00/\x80\x01\x00\x00\x00x\x00" + b"\x08\xc1t\x00\x04@\x00\x00\x08\xc0)\x00/\x80\x01\x00\x00\x11\x94\x00" + b"\t\xc0)\x00\x05\x00\x00\x80\x00@\xc0)\x00!\x80\x01\x00\x00\x00x" + b"\x00\x08\x00\x00\x00\x00\x05\xa3\xc0>\xc0>\x00\x01\x80\x01\x00\x00\x00x" + b"\x00\x04\xc0\xa8\x02:\xc0)\x00\x10\x80\x01\x00\x00\x11\x94\x01*2info=/api" + b"/v1/players/RINCON_542A1BC9220E01400/info\x06vers=3\x10protovers=1.24.1\nbo" + b"otseq=11%hhid=Sonos_rYn9K9DLXJe0f3LP9747lbvFvh;mhhid=Sonos_rYn9K9DLXJe0f3LP9" + b"747lbvFvh.Q45RuMaeC07rfXh7OJGm None: + """Test a RecordUpdateListener that does not implement update_records.""" + + # instantiate a zeroconf instance + zc = Zeroconf(interfaces=["127.0.0.1"]) + + with pytest.raises(RuntimeError): + r.RecordUpdateListener().update_record( + zc, + 0, + r.DNSRecord("irrelevant", const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL), + ) + + updates = [] + + class LegacyRecordUpdateListener(r.RecordUpdateListener): + """A RecordUpdateListener that does not implement update_records.""" + + def update_record(self, zc: Zeroconf, now: float, record: r.DNSRecord) -> None: + updates.append(record) + + listener = LegacyRecordUpdateListener() + + zc.add_listener(listener, None) + + # dummy service callback + def on_service_state_change(zeroconf, service_type, state_change, name): + pass + + # start a browser + type_ = "_homeassistant._tcp.local." + name = "MyTestHome" + browser = ServiceBrowser(zc, type_, [on_service_state_change]) + + info_service = ServiceInfo( + type_, + f"{name}.{type_}", + 80, + 0, + 0, + {"path": "/~paulsm/"}, + "ash-2.local.", + addresses=[socket.inet_aton("10.0.1.2")], + ) + + zc.register_service(info_service) + + time.sleep(0.001) + + browser.cancel() + + assert updates + assert len([isinstance(update, r.DNSPointer) and update.name == type_ for update in updates]) >= 1 + + zc.remove_listener(listener) + # Removing a second time should not throw + zc.remove_listener(listener) + + zc.close() + + +def test_record_update_compat(): + """Test a RecordUpdate can fetch by index.""" + new = r.DNSPointer("irrelevant", const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL, "new") + old = r.DNSPointer("irrelevant", const._TYPE_SRV, const._CLASS_IN, const._DNS_HOST_TTL, "old") + update = RecordUpdate(new, old) + assert update[0] == new + assert update[1] == old + with pytest.raises(IndexError): + update[2] + assert update.new == new + assert update.old == old diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py new file mode 100644 index 000000000..584a74eca --- /dev/null +++ b/tests/utils/__init__.py @@ -0,0 +1,23 @@ +"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine +Copyright 2003 Paul Scott-Murphy, 2014 William McBrine + +This module provides a framework for the use of DNS Service Discovery +using IP multicast. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA +""" + +from __future__ import annotations diff --git a/tests/utils/test_asyncio.py b/tests/utils/test_asyncio.py new file mode 100644 index 000000000..665bc8674 --- /dev/null +++ b/tests/utils/test_asyncio.py @@ -0,0 +1,175 @@ +"""Unit tests for zeroconf._utils.asyncio.""" + +from __future__ import annotations + +import asyncio +import concurrent.futures +import contextlib +import threading +import time +from unittest.mock import patch + +import pytest + +from zeroconf import EventLoopBlocked +from zeroconf._engine import _CLOSE_TIMEOUT +from zeroconf._utils import asyncio as aioutils +from zeroconf.const import _LOADED_SYSTEM_TIMEOUT + + +@pytest.mark.asyncio +async def test_async_get_all_tasks() -> None: + """Test we can get all tasks in the event loop. + + We make sure we handle RuntimeError here as + this is not thread safe under PyPy + """ + loop = aioutils.get_running_loop() + assert loop is not None + await aioutils._async_get_all_tasks(loop) + if not hasattr(asyncio, "all_tasks"): + return + with patch("zeroconf._utils.asyncio.asyncio.all_tasks", side_effect=RuntimeError): + await aioutils._async_get_all_tasks(loop) + + +@pytest.mark.asyncio +async def test_get_running_loop_from_async() -> None: + """Test we can get the event loop.""" + assert isinstance(aioutils.get_running_loop(), asyncio.AbstractEventLoop) + + +def test_get_running_loop_no_loop() -> None: + """Test we get None when there is no loop running.""" + assert aioutils.get_running_loop() is None + + +@pytest.mark.asyncio +async def test_wait_future_or_timeout_times_out() -> None: + """Test wait_future_or_timeout will timeout.""" + loop = asyncio.get_running_loop() + test_future = loop.create_future() + await aioutils.wait_future_or_timeout(test_future, 0.1) + + task = asyncio.ensure_future(test_future) + await asyncio.sleep(0.1) + + async def _async_wait_or_timeout(): + await aioutils.wait_future_or_timeout(test_future, 0.1) + + # Test high lock contention + await asyncio.gather(*[_async_wait_or_timeout() for _ in range(100)]) + + task.cancel() + with contextlib.suppress(asyncio.CancelledError): + await task + + +def test_shutdown_loop() -> None: + """Test shutting down an event loop.""" + loop = None + loop_thread_ready = threading.Event() + runcoro_thread_ready = threading.Event() + + def _run_loop() -> None: + nonlocal loop + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + loop_thread_ready.set() + loop.run_forever() + + loop_thread = threading.Thread(target=_run_loop, daemon=True) + loop_thread.start() + loop_thread_ready.wait() + + async def _still_running(): + await asyncio.sleep(5) + + def _run_coro() -> None: + assert loop is not None + future = asyncio.run_coroutine_threadsafe(_still_running(), loop) + runcoro_thread_ready.set() + with contextlib.suppress(concurrent.futures.TimeoutError): + future.result(0.1) + + runcoro_thread = threading.Thread(target=_run_coro, daemon=True) + runcoro_thread.start() + runcoro_thread_ready.wait() + + assert loop is not None + # Patch _TASK_AWAIT_TIMEOUT so the inner `asyncio.wait` returns + # within 50ms instead of blocking the full 1s on the deliberately + # never-completing _still_running() task. + with patch.object(aioutils, "_TASK_AWAIT_TIMEOUT", 0.05): + aioutils.shutdown_loop(loop) + for _ in range(5): + if not loop.is_running(): + break + time.sleep(0.05) + + assert loop.is_running() is False + runcoro_thread.join() + loop.close() + + +def test_cumulative_timeouts_less_than_close_plus_buffer(): + """Test that the combined async timeouts are shorter than the close timeout with the buffer. + + We want to make sure that the close timeout is the one that gets + raised if something goes wrong. + """ + assert ( + aioutils._TASK_AWAIT_TIMEOUT + aioutils._GET_ALL_TASKS_TIMEOUT + aioutils._WAIT_FOR_LOOP_TASKS_TIMEOUT + ) < 1 + _CLOSE_TIMEOUT + _LOADED_SYSTEM_TIMEOUT + + +@pytest.mark.asyncio +async def test_run_coro_with_timeout() -> None: + """Test running a coroutine with a timeout raises EventLoopBlocked.""" + loop = asyncio.get_event_loop() + task: asyncio.Task | None = None + task_created = asyncio.Event() + + async def _saved_sleep_task(): + nonlocal task + task = asyncio.create_task(asyncio.sleep(1)) + task_created.set() + await task + + def _run_in_loop(): + aioutils.run_coro_with_timeout(_saved_sleep_task(), loop, 50) + + with pytest.raises(EventLoopBlocked), patch.object(aioutils, "_LOADED_SYSTEM_TIMEOUT", 0.0): + await loop.run_in_executor(None, _run_in_loop) + + # The outer .result() can raise EventLoopBlocked before the loop + # has scheduled the coroutine — wait until the inner task is + # created before asserting on it. Use an asyncio.Event so the + # wait yields back to the loop instead of blocking it. + await asyncio.wait_for(task_created.wait(), 1.0) + assert task is not None + # ensure the thread is shutdown + task.cancel() + await asyncio.sleep(0) + await _shutdown_default_executor(loop) + + +# Remove this when we drop support for older python versions +# since we can use loop.shutdown_default_executor() in 3.9+ +async def _shutdown_default_executor(loop: asyncio.AbstractEventLoop) -> None: + """Backport of cpython 3.9 schedule the shutdown of the default executor.""" + future = loop.create_future() + + def _do_shutdown() -> None: + try: + loop._default_executor.shutdown(wait=True) # type: ignore # pylint: disable=protected-access + loop.call_soon_threadsafe(future.set_result, None) + except Exception as ex: # pylint: disable=broad-except + loop.call_soon_threadsafe(future.set_exception, ex) + + thread = threading.Thread(target=_do_shutdown) + thread.start() + try: + await future + finally: + thread.join() diff --git a/tests/utils/test_ipaddress.py b/tests/utils/test_ipaddress.py new file mode 100644 index 000000000..4379f458b --- /dev/null +++ b/tests/utils/test_ipaddress.py @@ -0,0 +1,99 @@ +"""Unit tests for zeroconf._utils.ipaddress.""" + +from __future__ import annotations + +from zeroconf import const +from zeroconf._dns import DNSAddress +from zeroconf._utils import ipaddress + + +def test_cached_ip_addresses_wrapper(): + """Test the cached_ip_addresses_wrapper.""" + assert ipaddress.cached_ip_addresses("") is None + assert ipaddress.cached_ip_addresses("foo") is None + assert ( + str(ipaddress.cached_ip_addresses(b"&\x06(\x00\x02 \x00\x01\x02H\x18\x93%\xc8\x19F")) + == "2606:2800:220:1:248:1893:25c8:1946" + ) + loop_back_ipv6 = ipaddress.cached_ip_addresses("::1") + assert loop_back_ipv6 == ipaddress.IPv6Address("::1") + assert loop_back_ipv6.is_loopback is True + + assert hash(loop_back_ipv6) == hash(ipaddress.IPv6Address("::1")) + + loop_back_ipv4 = ipaddress.cached_ip_addresses("127.0.0.1") + assert loop_back_ipv4 == ipaddress.IPv4Address("127.0.0.1") + assert loop_back_ipv4.is_loopback is True + + assert hash(loop_back_ipv4) == hash(ipaddress.IPv4Address("127.0.0.1")) + + ipv4 = ipaddress.cached_ip_addresses("169.254.0.0") + assert ipv4 is not None + assert ipv4.is_link_local is True + assert ipv4.is_unspecified is False + + ipv4 = ipaddress.cached_ip_addresses("0.0.0.0") + assert ipv4 is not None + assert ipv4.is_link_local is False + assert ipv4.is_unspecified is True + + ipv6 = ipaddress.cached_ip_addresses("fe80::1") + assert ipv6 is not None + assert ipv6.is_link_local is True + assert ipv6.is_unspecified is False + + ipv6 = ipaddress.cached_ip_addresses("0:0:0:0:0:0:0:0") + assert ipv6 is not None + assert ipv6.is_link_local is False + assert ipv6.is_unspecified is True + + +def test_get_ip_address_object_from_record(): + """Test the get_ip_address_object_from_record.""" + # not link local + packed = b"&\x06(\x00\x02 \x00\x01\x02H\x18\x93%\xc8\x19F" + record = DNSAddress( + "domain.local", + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 1, + packed, + scope_id=3, + ) + assert record.scope_id == 3 + assert ipaddress.get_ip_address_object_from_record(record) == ipaddress.IPv6Address( + "2606:2800:220:1:248:1893:25c8:1946" + ) + + # link local + packed = b"\xfe\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" + record = DNSAddress( + "domain.local", + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 1, + packed, + scope_id=3, + ) + assert record.scope_id == 3 + assert ipaddress.get_ip_address_object_from_record(record) == ipaddress.IPv6Address("fe80::1%3") + record = DNSAddress( + "domain.local", + const._TYPE_AAAA, + const._CLASS_IN | const._CLASS_UNIQUE, + 1, + packed, + ) + assert record.scope_id is None + assert ipaddress.get_ip_address_object_from_record(record) == ipaddress.IPv6Address("fe80::1") + record = DNSAddress( + "domain.local", + const._TYPE_A, + const._CLASS_IN | const._CLASS_UNIQUE, + 1, + packed, + scope_id=0, + ) + assert record.scope_id == 0 + # Ensure scope_id of 0 is not appended to the address + assert ipaddress.get_ip_address_object_from_record(record) == ipaddress.IPv6Address("fe80::1") diff --git a/tests/utils/test_name.py b/tests/utils/test_name.py new file mode 100644 index 000000000..31830fae3 --- /dev/null +++ b/tests/utils/test_name.py @@ -0,0 +1,105 @@ +"""Unit tests for zeroconf._utils.name.""" + +from __future__ import annotations + +import socket + +import pytest + +from zeroconf import BadTypeInNameException +from zeroconf._services.info import ServiceInfo, instance_name_from_service_info +from zeroconf._utils import name as nameutils + + +def test_service_type_name_overlong_type(): + """Test overlong service_type_name type.""" + with pytest.raises(BadTypeInNameException): + nameutils.service_type_name("Tivo1._tivo-videostream._tcp.local.") + nameutils.service_type_name("Tivo1._tivo-videostream._tcp.local.", strict=False) + + +def test_service_type_name_overlong_full_name(): + """Test overlong service_type_name full name.""" + long_name = "Tivo1Tivo1Tivo1Tivo1Tivo1Tivo1Tivo1Tivo1" * 100 + with pytest.raises(BadTypeInNameException): + nameutils.service_type_name(f"{long_name}._tivo-videostream._tcp.local.") + with pytest.raises(BadTypeInNameException): + nameutils.service_type_name(f"{long_name}._tivo-videostream._tcp.local.", strict=False) + + +@pytest.mark.parametrize( + ("instance_name", "service_type"), + [ + ("CustomerInformationService-F4D4885E9EEB", "_ibisip_http._tcp.local."), + ("DeviceManagementService_F4D4885E9EEB", "_ibisip_http._tcp.local."), + ], +) +def test_service_type_name_non_strict_compliant_names(instance_name, service_type): + """Test service_type_name for valid names, but not strict-compliant.""" + desc = {"path": "/~paulsm/"} + service_name = f"{instance_name}.{service_type}" + service_server = "ash-1.local." + service_address = socket.inet_aton("10.0.1.2") + info = ServiceInfo( + service_type, + service_name, + 22, + 0, + 0, + desc, + service_server, + addresses=[service_address], + ) + assert info.get_name() == instance_name + + with pytest.raises(BadTypeInNameException): + nameutils.service_type_name(service_name) + with pytest.raises(BadTypeInNameException): + instance_name_from_service_info(info) + + nameutils.service_type_name(service_name, strict=False) + assert instance_name_from_service_info(info, strict=False) == instance_name + + +@pytest.mark.parametrize( + ("type_", "expected"), + [ + ("_http._tcp.LOCAL.", "_http._tcp.LOCAL."), + ("_http._TCP.local.", "_http._TCP.local."), + ("_HTTP._tcp.local.", "_HTTP._tcp.local."), + ("Instance._http._tcp.LOCAL.", "_http._tcp.LOCAL."), + ("_ntp._udp.LOCAL.", "_ntp._udp.LOCAL."), + ("_ntp._UDP.local.", "_ntp._UDP.local."), + ("Instance._ntp._udp.LOCAL.", "_ntp._udp.LOCAL."), + ], +) +def test_service_type_name_uppercase_trailer(type_, expected): + """RFC 1035 §2.3.3 / RFC 6762 §16 — DNS names are case-insensitive.""" + assert nameutils.service_type_name(type_) == expected + + +def test_service_type_name_uppercase_local_non_strict(): + """Non-strict mode accepts uppercase .LOCAL. trailer without a protocol label.""" + assert nameutils.service_type_name("Localhost.LOCAL.", strict=False) == "LOCAL." + + +def test_possible_types(): + """Test possible types from name.""" + assert nameutils.possible_types(".") == set() + assert nameutils.possible_types("local.") == set() + assert nameutils.possible_types("_tcp.local.") == set() + assert nameutils.possible_types("_test-srvc-type._tcp.local.") == {"_test-srvc-type._tcp.local."} + assert nameutils.possible_types("_any._tcp.local.") == {"_any._tcp.local."} + assert nameutils.possible_types(".._x._tcp.local.") == {"_x._tcp.local."} + assert nameutils.possible_types("x.y._http._tcp.local.") == {"_http._tcp.local."} + assert nameutils.possible_types("1.2.3._mqtt._tcp.local.") == {"_mqtt._tcp.local."} + assert nameutils.possible_types("x.sub._http._tcp.local.") == {"_http._tcp.local."} + assert nameutils.possible_types("6d86f882b90facee9170ad3439d72a4d6ee9f511._zget._http._tcp.local.") == { + "_http._tcp.local.", + "_zget._http._tcp.local.", + } + assert nameutils.possible_types("my._printer._sub._http._tcp.local.") == { + "_http._tcp.local.", + "_sub._http._tcp.local.", + "_printer._sub._http._tcp.local.", + } diff --git a/tests/utils/test_net.py b/tests/utils/test_net.py new file mode 100644 index 000000000..311e95e6e --- /dev/null +++ b/tests/utils/test_net.py @@ -0,0 +1,435 @@ +"""Unit tests for zeroconf._utils.net.""" + +from __future__ import annotations + +import errno +import socket +import sys +import unittest +import warnings +from unittest.mock import MagicMock, Mock, call, patch + +import ifaddr +import pytest + +import zeroconf as r +from zeroconf import get_all_addresses, get_all_addresses_v6 +from zeroconf._utils import net as netutils + + +def _generate_mock_adapters(): + mock_lo0 = Mock(spec=ifaddr.Adapter) + mock_lo0.nice_name = "lo0" + mock_lo0.ips = [ifaddr.IP("127.0.0.1", 8, "lo0"), ifaddr.IP(("::1", 0, 0), 128, "lo")] + mock_lo0.index = 0 + mock_eth0 = Mock(spec=ifaddr.Adapter) + mock_eth0.nice_name = "eth0" + mock_eth0.ips = [ifaddr.IP(("2001:db8::", 1, 1), 8, "eth0"), ifaddr.IP(("fd00:db8::", 1, 1), 8, "eth0")] + mock_eth0.index = 1 + mock_eth1 = Mock(spec=ifaddr.Adapter) + mock_eth1.nice_name = "eth1" + mock_eth1.ips = [ifaddr.IP("192.168.1.5", 23, "eth1")] + mock_eth1.index = 2 + mock_vtun0 = Mock(spec=ifaddr.Adapter) + mock_vtun0.nice_name = "vtun0" + mock_vtun0.ips = [ifaddr.IP("169.254.3.2", 16, "vtun0")] + mock_vtun0.index = 3 + return [mock_eth0, mock_lo0, mock_eth1, mock_vtun0] + + +def test_get_all_addresses() -> None: + """Test public get_all_addresses API.""" + with ( + patch( + "zeroconf._utils.net.ifaddr.get_adapters", + return_value=_generate_mock_adapters(), + ), + warnings.catch_warnings(record=True) as warned, + ): + addresses = get_all_addresses() + assert isinstance(addresses, list) + assert len(addresses) == 3 + assert len(warned) == 1 + first_warning = warned[0] + assert "get_all_addresses is deprecated" in str(first_warning.message) + + +def test_get_all_addresses_v6() -> None: + """Test public get_all_addresses_v6 API.""" + with ( + patch( + "zeroconf._utils.net.ifaddr.get_adapters", + return_value=_generate_mock_adapters(), + ), + warnings.catch_warnings(record=True) as warned, + ): + addresses = get_all_addresses_v6() + assert isinstance(addresses, list) + assert len(addresses) == 3 + assert len(warned) == 1 + first_warning = warned[0] + assert "get_all_addresses_v6 is deprecated" in str(first_warning.message) + + +def test_ip6_to_address_and_index(): + """Test we can extract from mocked adapters.""" + adapters = _generate_mock_adapters() + assert netutils.ip6_to_address_and_index(adapters, "2001:db8::") == ( + ("2001:db8::", 1, 1), + 1, + ) + assert netutils.ip6_to_address_and_index(adapters, "2001:db8::%1") == ( + ("2001:db8::", 1, 1), + 1, + ) + with pytest.raises(RuntimeError): + assert netutils.ip6_to_address_and_index(adapters, "2005:db8::") + + +def test_interface_index_to_ip6_address(): + """Test we can extract from mocked adapters.""" + adapters = _generate_mock_adapters() + assert netutils.interface_index_to_ip6_address(adapters, 1) == ("2001:db8::", 1, 1) + + # call with invalid adapter + with pytest.raises(RuntimeError): + assert netutils.interface_index_to_ip6_address(adapters, 6) + + # call with adapter that has ipv4 address only + with pytest.raises(RuntimeError): + assert netutils.interface_index_to_ip6_address(adapters, 2) + + +def test_ip6_addresses_to_indexes(): + """Test we can extract from mocked adapters.""" + interfaces = [1] + with patch( + "zeroconf._utils.net.ifaddr.get_adapters", + return_value=_generate_mock_adapters(), + ): + assert netutils.ip6_addresses_to_indexes(interfaces) == [(("2001:db8::", 1, 1), 1)] + + interfaces_2 = ["2001:db8::"] + with patch( + "zeroconf._utils.net.ifaddr.get_adapters", + return_value=_generate_mock_adapters(), + ): + assert netutils.ip6_addresses_to_indexes(interfaces_2) == [(("2001:db8::", 1, 1), 1)] + + +def test_normalize_interface_choice_errors(): + """Test we generate exception on invalid input.""" + with ( + patch("zeroconf._utils.net.get_all_addresses_ipv4", return_value=[]), + patch("zeroconf._utils.net.get_all_addresses_ipv6", return_value=[]), + pytest.raises(RuntimeError), + ): + netutils.normalize_interface_choice(r.InterfaceChoice.All) + + with pytest.raises(TypeError): + netutils.normalize_interface_choice("1.2.3.4") + + +@pytest.mark.parametrize( + ("errno", "expected_result"), + [ + (errno.EADDRINUSE, False), + (errno.EADDRNOTAVAIL, False), + (errno.EINVAL, False), + (0, True), + ], +) +def test_add_multicast_member_socket_errors(errno, expected_result): + """Test we handle socket errors when adding multicast members.""" + if errno: + setsockopt_mock = unittest.mock.Mock(side_effect=OSError(errno, f"Error: {errno}")) + else: + setsockopt_mock = unittest.mock.Mock() + fileno_mock = unittest.mock.PropertyMock(return_value=10) + socket_mock = unittest.mock.Mock(setsockopt=setsockopt_mock, fileno=fileno_mock) + assert r.add_multicast_member(socket_mock, "0.0.0.0") == expected_result + + +def test_autodetect_ip_version(): + """Tests for auto detecting IPVersion based on interface ips.""" + assert r.autodetect_ip_version(["1.3.4.5"]) is r.IPVersion.V4Only + assert r.autodetect_ip_version([]) is r.IPVersion.V4Only + assert r.autodetect_ip_version(["::1", "1.2.3.4"]) is r.IPVersion.All + assert r.autodetect_ip_version(["::1"]) is r.IPVersion.V6Only + + +def test_disable_ipv6_only_or_raise(): + """Test that IPV6_V6ONLY failing logs a nice error message and still raises.""" + errors_logged = [] + + def _log_error(*args): + errors_logged.append(args) + + with ( + socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock, + pytest.raises(OSError), + patch.object(netutils.log, "error", _log_error), + patch("socket.socket.setsockopt", side_effect=OSError), + ): + netutils.disable_ipv6_only_or_raise(sock) + + assert ( + errors_logged[0][0] + == "Support for dual V4-V6 sockets is not present, use IPVersion.V4 or IPVersion.V6" + ) + + +@pytest.mark.skipif(not hasattr(socket, "SO_REUSEPORT"), reason="System does not have SO_REUSEPORT") +def test_set_so_reuseport_if_available_is_present(): + """Test that setting socket.SO_REUSEPORT only OSError errno.ENOPROTOOPT is trapped.""" + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: + with pytest.raises(OSError), patch("socket.socket.setsockopt", side_effect=OSError): + netutils.set_so_reuseport_if_available(sock) + + with patch("socket.socket.setsockopt", side_effect=OSError(errno.ENOPROTOOPT, None)): + netutils.set_so_reuseport_if_available(sock) + + +@pytest.mark.skipif(hasattr(socket, "SO_REUSEPORT"), reason="System has SO_REUSEPORT") +def test_set_so_reuseport_if_available_not_present(): + """Test that we do not try to set SO_REUSEPORT if it is not present.""" + with ( + socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock, + patch("socket.socket.setsockopt", side_effect=OSError), + ): + netutils.set_so_reuseport_if_available(sock) + + +def test_set_respond_socket_multicast_options(): + """Test OSError with errno with EINVAL and bind address ''. + + from setsockopt IP_MULTICAST_TTL does not raise.""" + # Should raise on EINVAL always + with ( + socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock, + pytest.raises(OSError), + patch("socket.socket.setsockopt", side_effect=OSError(errno.EINVAL, None)), + ): + netutils.set_respond_socket_multicast_options(sock, r.IPVersion.V4Only) + + with pytest.raises(RuntimeError): + netutils.set_respond_socket_multicast_options(sock, r.IPVersion.All) + + +def test_add_multicast_member(caplog: pytest.LogCaptureFixture) -> None: + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: + interface = "127.0.0.1" + + # EPERM should always raise + with ( + pytest.raises(OSError), + patch("socket.socket.setsockopt", side_effect=OSError(errno.EPERM, None)), + ): + netutils.add_multicast_member(sock, interface) + + # EADDRINUSE should return False + with patch("socket.socket.setsockopt", side_effect=OSError(errno.EADDRINUSE, None)): + assert netutils.add_multicast_member(sock, interface) is False + + # EADDRNOTAVAIL should return False + with patch("socket.socket.setsockopt", side_effect=OSError(errno.EADDRNOTAVAIL, None)): + assert netutils.add_multicast_member(sock, interface) is False + + # EINVAL should return False + with patch("socket.socket.setsockopt", side_effect=OSError(errno.EINVAL, None)): + assert netutils.add_multicast_member(sock, interface) is False + + # ENOPROTOOPT should return False + with patch("socket.socket.setsockopt", side_effect=OSError(errno.ENOPROTOOPT, None)): + assert netutils.add_multicast_member(sock, interface) is False + + # ENODEV should raise for ipv4 + with ( + pytest.raises(OSError), + patch("socket.socket.setsockopt", side_effect=OSError(errno.ENODEV, None)), + ): + assert netutils.add_multicast_member(sock, interface) is False + + # ENODEV should return False for ipv6 + with patch("socket.socket.setsockopt", side_effect=OSError(errno.ENODEV, None)): + assert netutils.add_multicast_member(sock, ("2001:db8::", 1, 1)) is False # type: ignore[arg-type] + + # No IPv6 support should return False for IPv6 + with patch("socket.inet_pton", side_effect=OSError()): + assert netutils.add_multicast_member(sock, ("2001:db8::", 1, 1)) is False # type: ignore[arg-type] + + # No error should return True + with patch("socket.socket.setsockopt"): + assert netutils.add_multicast_member(sock, interface) is True + + # Ran out of IGMP memberships is forgiving and logs about igmp_max_memberships on linux + caplog.clear() + with ( + patch.object(sys, "platform", "linux"), + patch( + "socket.socket.setsockopt", side_effect=OSError(errno.ENOBUFS, "No buffer space available") + ), + ): + assert netutils.add_multicast_member(sock, interface) is False + assert "No buffer space available" in caplog.text + assert "net.ipv4.igmp_max_memberships" in caplog.text + + # Ran out of IGMP memberships is forgiving and logs + caplog.clear() + with ( + patch.object(sys, "platform", "darwin"), + patch( + "socket.socket.setsockopt", side_effect=OSError(errno.ENOBUFS, "No buffer space available") + ), + ): + assert netutils.add_multicast_member(sock, interface) is False + assert "No buffer space available" in caplog.text + assert "net.ipv4.igmp_max_memberships" not in caplog.text + + +def test_bind_raises_skips_address(): + """Test bind failing in new_socket returns None on EADDRNOTAVAIL.""" + err = errno.EADDRNOTAVAIL + + def _mock_socket(*args, **kwargs): + sock = MagicMock() + sock.bind = MagicMock(side_effect=OSError(err, f"Error: {err}")) + return sock + + with patch("socket.socket", _mock_socket): + assert netutils.new_socket(("0.0.0.0", 0)) is None # type: ignore[arg-type] + + err = errno.EAGAIN + with pytest.raises(OSError), patch("socket.socket", _mock_socket): + netutils.new_socket(("0.0.0.0", 0)) # type: ignore[arg-type] + + +def test_bind_raises_address_in_use(caplog: pytest.LogCaptureFixture) -> None: + """Test bind failing in new_socket returns None on EADDRINUSE.""" + + def _mock_socket(*args, **kwargs): + sock = MagicMock() + sock.bind = MagicMock(side_effect=OSError(errno.EADDRINUSE, f"Error: {errno.EADDRINUSE}")) + return sock + + with ( + pytest.raises(OSError), + patch.object(sys, "platform", "darwin"), + patch("socket.socket", _mock_socket), + ): + netutils.new_socket(("0.0.0.0", 0)) # type: ignore[arg-type] + assert ( + "On BSD based systems sharing the same port with " + "another stack may require processes to run with the same UID" + ) in caplog.text + assert ( + "When using avahi, make sure disallow-other-stacks is set to no in avahi-daemon.conf" in caplog.text + ) + + caplog.clear() + with pytest.raises(OSError), patch.object(sys, "platform", "linux"), patch("socket.socket", _mock_socket): + netutils.new_socket(("0.0.0.0", 0)) # type: ignore[arg-type] + assert ( + "On BSD based systems sharing the same port with " + "another stack may require processes to run with the same UID" + ) not in caplog.text + assert ( + "When using avahi, make sure disallow-other-stacks is set to no in avahi-daemon.conf" in caplog.text + ) + + +def test_new_respond_socket_new_socket_returns_none(): + """Test new_respond_socket returns None if new_socket returns None.""" + with patch.object(netutils, "new_socket", return_value=None): + assert netutils.new_respond_socket(("0.0.0.0", 0)) is None # type: ignore[arg-type] + + +def test_create_sockets_interfaces_all_unicast(): + """Test create_sockets with unicast.""" + + with ( + patch("zeroconf._utils.net.new_socket") as mock_new_socket, + patch( + "zeroconf._utils.net.ifaddr.get_adapters", + return_value=_generate_mock_adapters(), + ), + ): + mock_socket = Mock(spec=socket.socket) + mock_new_socket.return_value = mock_socket + + listen_socket, _respond_sockets = r.create_sockets( + interfaces=r.InterfaceChoice.All, unicast=True, ip_version=r.IPVersion.All + ) + + assert listen_socket is None + mock_new_socket.assert_any_call( + port=0, + ip_version=r.IPVersion.V6Only, + apple_p2p=False, + bind_addr=("2001:db8::", 1, 1), + ) + mock_new_socket.assert_any_call( + port=0, + ip_version=r.IPVersion.V4Only, + apple_p2p=False, + bind_addr=("192.168.1.5",), + ) + + +def test_create_sockets_interfaces_all() -> None: + """Test create_sockets with all interfaces. + + Tests if a responder socket is created for every successful multicast + join. + """ + adapters = _generate_mock_adapters() + + # Additional IPv6 addresses usually fail to add membership + failure_interface = ("fd00:db8::", 1, 1) + + expected_calls = [] + for adapter in adapters: + for ip in adapter.ips: + if ip.ip == failure_interface: + continue + + if ip.is_IPv4: + bind_addr = (ip.ip,) + ip_version = r.IPVersion.V4Only + else: + bind_addr = ip.ip + ip_version = r.IPVersion.V6Only + + expected_calls.append( + call( + port=5353, + ip_version=ip_version, + apple_p2p=False, + bind_addr=bind_addr, + ) + ) + + def _patched_add_multicast_member(sock, interface): + return interface[0] != failure_interface + + with ( + patch("zeroconf._utils.net.new_socket") as mock_new_socket, + patch( + "zeroconf._utils.net.ifaddr.get_adapters", + return_value=adapters, + ), + patch("zeroconf._utils.net.add_multicast_member", side_effect=_patched_add_multicast_member), + ): + mock_socket = Mock(spec=socket.socket) + mock_new_socket.return_value = mock_socket + + r.create_sockets(interfaces=r.InterfaceChoice.All, ip_version=r.IPVersion.All) + + def call_to_tuple(c): + return (c.args, tuple(sorted(c.kwargs.items()))) + + # Exclude first new_socket call as this is the listen socket + actual_calls_set = {call_to_tuple(c) for c in mock_new_socket.call_args_list[1:]} + expected_calls_set = {call_to_tuple(c) for c in expected_calls} + + assert actual_calls_set == expected_calls_set diff --git a/zeroconf.py b/zeroconf.py deleted file mode 100644 index 978c25464..000000000 --- a/zeroconf.py +++ /dev/null @@ -1,2141 +0,0 @@ -""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine - Copyright 2003 Paul Scott-Murphy, 2014 William McBrine - - This module provides a framework for the use of DNS Service Discovery - using IP multicast. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 - USA -""" - -import enum -import errno -import logging -import re -import select -import socket -import struct -import sys -import threading -import time -from functools import reduce -from typing import AnyStr, Dict, List, Optional, Union, cast -from typing import Callable, Set, Tuple # noqa # used in type hints - -import ifaddr - -__author__ = 'Paul Scott-Murphy, William McBrine' -__maintainer__ = 'Jakub Stasiak ' -__version__ = '0.21.3' -__license__ = 'LGPL' - - -__all__ = [ - "__version__", - "Zeroconf", "ServiceInfo", "ServiceBrowser", - "Error", "InterfaceChoice", "ServiceStateChange", -] - -if sys.version_info <= (3, 3): - raise ImportError(''' -Python version > 3.3 required for python-zeroconf. -If you need support for Python 2 or Python 3.3 please use version 19.1 - ''') - -log = logging.getLogger(__name__) -log.addHandler(logging.NullHandler()) - -if log.level == logging.NOTSET: - log.setLevel(logging.WARN) - -# Some timing constants - -_UNREGISTER_TIME = 125 -_CHECK_TIME = 175 -_REGISTER_TIME = 225 -_LISTENER_TIME = 200 -_BROWSER_TIME = 500 - -# Some DNS constants - -_MDNS_ADDR = '224.0.0.251' -_MDNS_PORT = 5353 -_DNS_PORT = 53 -_DNS_TTL = 120 # two minutes default TTL as recommended by RFC6762 - -_MAX_MSG_TYPICAL = 1460 # unused -_MAX_MSG_ABSOLUTE = 8966 - -_FLAGS_QR_MASK = 0x8000 # query response mask -_FLAGS_QR_QUERY = 0x0000 # query -_FLAGS_QR_RESPONSE = 0x8000 # response - -_FLAGS_AA = 0x0400 # Authoritative answer -_FLAGS_TC = 0x0200 # Truncated -_FLAGS_RD = 0x0100 # Recursion desired -_FLAGS_RA = 0x8000 # Recursion available - -_FLAGS_Z = 0x0040 # Zero -_FLAGS_AD = 0x0020 # Authentic data -_FLAGS_CD = 0x0010 # Checking disabled - -_CLASS_IN = 1 -_CLASS_CS = 2 -_CLASS_CH = 3 -_CLASS_HS = 4 -_CLASS_NONE = 254 -_CLASS_ANY = 255 -_CLASS_MASK = 0x7FFF -_CLASS_UNIQUE = 0x8000 - -_TYPE_A = 1 -_TYPE_NS = 2 -_TYPE_MD = 3 -_TYPE_MF = 4 -_TYPE_CNAME = 5 -_TYPE_SOA = 6 -_TYPE_MB = 7 -_TYPE_MG = 8 -_TYPE_MR = 9 -_TYPE_NULL = 10 -_TYPE_WKS = 11 -_TYPE_PTR = 12 -_TYPE_HINFO = 13 -_TYPE_MINFO = 14 -_TYPE_MX = 15 -_TYPE_TXT = 16 -_TYPE_AAAA = 28 -_TYPE_SRV = 33 -_TYPE_ANY = 255 - -# Mapping constants to names - -_CLASSES = {_CLASS_IN: "in", - _CLASS_CS: "cs", - _CLASS_CH: "ch", - _CLASS_HS: "hs", - _CLASS_NONE: "none", - _CLASS_ANY: "any"} - -_TYPES = {_TYPE_A: "a", - _TYPE_NS: "ns", - _TYPE_MD: "md", - _TYPE_MF: "mf", - _TYPE_CNAME: "cname", - _TYPE_SOA: "soa", - _TYPE_MB: "mb", - _TYPE_MG: "mg", - _TYPE_MR: "mr", - _TYPE_NULL: "null", - _TYPE_WKS: "wks", - _TYPE_PTR: "ptr", - _TYPE_HINFO: "hinfo", - _TYPE_MINFO: "minfo", - _TYPE_MX: "mx", - _TYPE_TXT: "txt", - _TYPE_AAAA: "quada", - _TYPE_SRV: "srv", - _TYPE_ANY: "any"} - -_HAS_A_TO_Z = re.compile(r'[A-Za-z]') -_HAS_ONLY_A_TO_Z_NUM_HYPHEN = re.compile(r'^[A-Za-z0-9\-]+$') -_HAS_ONLY_A_TO_Z_NUM_HYPHEN_UNDERSCORE = re.compile(r'^[A-Za-z0-9\-\_]+$') -_HAS_ASCII_CONTROL_CHARS = re.compile(r'[\x00-\x1f\x7f]') - -int2byte = struct.Struct(">B").pack - - -@enum.unique -class InterfaceChoice(enum.Enum): - Default = 1 - All = 2 - - -@enum.unique -class ServiceStateChange(enum.Enum): - Added = 1 - Removed = 2 - - -# utility functions - - -def current_time_millis() -> float: - """Current system time in milliseconds""" - return time.time() * 1000 - - -def service_type_name(type_, *, allow_underscores: bool = False): - """ - Validate a fully qualified service name, instance or subtype. [rfc6763] - - Returns fully qualified service name. - - Domain names used by mDNS-SD take the following forms: - - . <_tcp|_udp> . local. - . . <_tcp|_udp> . local. - ._sub . . <_tcp|_udp> . local. - - 1) must end with 'local.' - - This is true because we are implementing mDNS and since the 'm' means - multi-cast, the 'local.' domain is mandatory. - - 2) local is preceded with either '_udp.' or '_tcp.' - - 3) service name precedes <_tcp|_udp> - - The rules for Service Names [RFC6335] state that they may be no more - than fifteen characters long (not counting the mandatory underscore), - consisting of only letters, digits, and hyphens, must begin and end - with a letter or digit, must not contain consecutive hyphens, and - must contain at least one letter. - - The instance name and sub type may be up to 63 bytes. - - The portion of the Service Instance Name is a user- - friendly name consisting of arbitrary Net-Unicode text [RFC5198]. It - MUST NOT contain ASCII control characters (byte values 0x00-0x1F and - 0x7F) [RFC20] but otherwise is allowed to contain any characters, - without restriction, including spaces, uppercase, lowercase, - punctuation -- including dots -- accented characters, non-Roman text, - and anything else that may be represented using Net-Unicode. - - :param type_: Type, SubType or service name to validate - :return: fully qualified service name (eg: _http._tcp.local.) - """ - if not (type_.endswith('._tcp.local.') or type_.endswith('._udp.local.')): - raise BadTypeInNameException( - "Type '%s' must end with '._tcp.local.' or '._udp.local.'" % - type_) - - remaining = type_[:-len('._tcp.local.')].split('.') - name = remaining.pop() - if not name: - raise BadTypeInNameException("No Service name found") - - if len(remaining) == 1 and len(remaining[0]) == 0: - raise BadTypeInNameException( - "Type '%s' must not start with '.'" % type_) - - if name[0] != '_': - raise BadTypeInNameException( - "Service name (%s) must start with '_'" % name) - - # remove leading underscore - name = name[1:] - - if len(name) > 15: - raise BadTypeInNameException( - "Service name (%s) must be <= 15 bytes" % name) - - if '--' in name: - raise BadTypeInNameException( - "Service name (%s) must not contain '--'" % name) - - if '-' in (name[0], name[-1]): - raise BadTypeInNameException( - "Service name (%s) may not start or end with '-'" % name) - - if not _HAS_A_TO_Z.search(name): - raise BadTypeInNameException( - "Service name (%s) must contain at least one letter (eg: 'A-Z')" % - name) - - allowed_characters_re = ( - _HAS_ONLY_A_TO_Z_NUM_HYPHEN_UNDERSCORE if allow_underscores - else _HAS_ONLY_A_TO_Z_NUM_HYPHEN - ) - - if not allowed_characters_re.search(name): - raise BadTypeInNameException( - "Service name (%s) must contain only these characters: " - "A-Z, a-z, 0-9, hyphen ('-')%s" % (name, ", underscore ('_')" if allow_underscores else "")) - - if remaining and remaining[-1] == '_sub': - remaining.pop() - if len(remaining) == 0 or len(remaining[0]) == 0: - raise BadTypeInNameException( - "_sub requires a subtype name") - - if len(remaining) > 1: - remaining = ['.'.join(remaining)] - - if remaining: - length = len(remaining[0].encode('utf-8')) - if length > 63: - raise BadTypeInNameException("Too long: '%s'" % remaining[0]) - - if _HAS_ASCII_CONTROL_CHARS.search(remaining[0]): - raise BadTypeInNameException( - "Ascii control character 0x00-0x1F and 0x7F illegal in '%s'" % - remaining[0]) - - return '_' + name + type_[-len('._tcp.local.'):] - - -# Exceptions - - -class Error(Exception): - pass - - -class IncomingDecodeError(Error): - pass - - -class NonUniqueNameException(Error): - pass - - -class NamePartTooLongException(Error): - pass - - -class AbstractMethodException(Error): - pass - - -class BadTypeInNameException(Error): - pass - - -# implementation classes - - -class QuietLogger: - _seen_logs = {} # type: Dict[str, tuple] - - @classmethod - def log_exception_warning(cls, logger_data=None): - exc_info = sys.exc_info() - exc_str = str(exc_info[1]) - if exc_str not in cls._seen_logs: - # log at warning level the first time this is seen - cls._seen_logs[exc_str] = exc_info - logger = log.warning - else: - logger = log.debug - if logger_data is not None: - logger(*logger_data) - logger('Exception occurred:', exc_info=True) - - @classmethod - def log_warning_once(cls, *args): - msg_str = args[0] - if msg_str not in cls._seen_logs: - cls._seen_logs[msg_str] = 0 - logger = log.warning - else: - logger = log.debug - cls._seen_logs[msg_str] += 1 - logger(*args) - - -class DNSEntry: - - """A DNS entry""" - - def __init__(self, name: str, type_: int, class_): - self.key = name.lower() - self.name = name - self.type = type_ - self.class_ = class_ & _CLASS_MASK - self.unique = (class_ & _CLASS_UNIQUE) != 0 - - def __eq__(self, other): - """Equality test on name, type, and class""" - return (isinstance(other, DNSEntry) and - self.name == other.name and - self.type == other.type and - self.class_ == other.class_) - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - @staticmethod - def get_class_(class_): - """Class accessor""" - return _CLASSES.get(class_, "?(%s)" % class_) - - @staticmethod - def get_type(t): - """Type accessor""" - return _TYPES.get(t, "?(%s)" % t) - - def to_string(self, hdr, other) -> str: - """String representation with additional information""" - result = "%s[%s,%s" % (hdr, self.get_type(self.type), - self.get_class_(self.class_)) - if self.unique: - result += "-unique," - else: - result += "," - result += self.name - if other is not None: - result += ",%s]" % other - else: - result += "]" - return result - - -class DNSQuestion(DNSEntry): - - """A DNS question entry""" - - def __init__(self, name: str, type_: int, class_: int) -> None: - DNSEntry.__init__(self, name, type_, class_) - - def answered_by(self, rec: 'DNSRecord') -> bool: - """Returns true if the question is answered by the record""" - return (self.class_ == rec.class_ and - (self.type == rec.type or self.type == _TYPE_ANY) and - self.name == rec.name) - - def __repr__(self) -> str: - """String representation""" - return DNSEntry.to_string(self, "question", None) - - -class DNSRecord(DNSEntry): - - """A DNS record - like a DNS entry, but has a TTL""" - - def __init__(self, name, type_, class_, ttl: float): - DNSEntry.__init__(self, name, type_, class_) - self.ttl = ttl - self.created = current_time_millis() - - def __eq__(self, other): - """Abstract method""" - raise AbstractMethodException - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def suppressed_by(self, msg): - """Returns true if any answer in a message can suffice for the - information held in this record.""" - for record in msg.answers: - if self.suppressed_by_answer(record): - return True - return False - - def suppressed_by_answer(self, other): - """Returns true if another record has same name, type and class, - and if its TTL is at least half of this record's.""" - return self == other and other.ttl > (self.ttl / 2) - - def get_expiration_time(self, percent: float) -> float: - """Returns the time at which this record will have expired - by a certain percentage.""" - return self.created + (percent * self.ttl * 10) - - def get_remaining_ttl(self, now): - """Returns the remaining TTL in seconds.""" - return max(0, (self.get_expiration_time(100) - now) / 1000.0) - - def is_expired(self, now: float) -> bool: - """Returns true if this record has expired.""" - return self.get_expiration_time(100) <= now - - def is_stale(self, now): - """Returns true if this record is at least half way expired.""" - return self.get_expiration_time(50) <= now - - def reset_ttl(self, other): - """Sets this record's TTL and created time to that of - another record.""" - self.created = other.created - self.ttl = other.ttl - - def write(self, out): - """Abstract method""" - raise AbstractMethodException - - def to_string(self, other): - """String representation with additional information""" - arg = "%s/%s,%s" % ( - self.ttl, self.get_remaining_ttl(current_time_millis()), other) - return DNSEntry.to_string(self, "record", arg) - - -class DNSAddress(DNSRecord): - - """A DNS address record""" - - def __init__(self, name, type_, class_, ttl, address): - DNSRecord.__init__(self, name, type_, class_, ttl) - self.address = address - - def write(self, out): - """Used in constructing an outgoing packet""" - out.write_string(self.address) - - def __eq__(self, other): - """Tests equality on address""" - return (isinstance(other, DNSAddress) and DNSEntry.__eq__(self, other) and - self.address == other.address) - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def __repr__(self): - """String representation""" - try: - return str(socket.inet_ntoa(self.address)) - except Exception: # TODO stop catching all Exceptions - return str(self.address) - - -class DNSHinfo(DNSRecord): - - """A DNS host information record""" - - def __init__(self, name, type_, class_, ttl, cpu, os): - DNSRecord.__init__(self, name, type_, class_, ttl) - try: - self.cpu = cpu.decode('utf-8') - except AttributeError: - self.cpu = cpu - try: - self.os = os.decode('utf-8') - except AttributeError: - self.os = os - - def write(self, out): - """Used in constructing an outgoing packet""" - out.write_character_string(self.cpu.encode('utf-8')) - out.write_character_string(self.os.encode('utf-8')) - - def __eq__(self, other): - """Tests equality on cpu and os""" - return (isinstance(other, DNSHinfo) and DNSEntry.__eq__(self, other) and - self.cpu == other.cpu and self.os == other.os) - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def __repr__(self): - """String representation""" - return self.cpu + " " + self.os - - -class DNSPointer(DNSRecord): - - """A DNS pointer record""" - - def __init__(self, name, type_, class_, ttl, alias): - DNSRecord.__init__(self, name, type_, class_, ttl) - self.alias = alias - - def write(self, out): - """Used in constructing an outgoing packet""" - out.write_name(self.alias) - - def __eq__(self, other): - """Tests equality on alias""" - return (isinstance(other, DNSPointer) and DNSEntry.__eq__(self, other) and - self.alias == other.alias) - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def __repr__(self): - """String representation""" - return self.to_string(self.alias) - - -class DNSText(DNSRecord): - - """A DNS text record""" - - def __init__(self, name, type_, class_, ttl, text): - assert isinstance(text, (bytes, type(None))) - DNSRecord.__init__(self, name, type_, class_, ttl) - self.text = text - - def write(self, out): - """Used in constructing an outgoing packet""" - out.write_string(self.text) - - def __eq__(self, other): - """Tests equality on text""" - return (isinstance(other, DNSText) and DNSEntry.__eq__(self, other) and - self.text == other.text) - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def __repr__(self): - """String representation""" - if len(self.text) > 10: - return self.to_string(self.text[:7]) + "..." - else: - return self.to_string(self.text) - - -class DNSService(DNSRecord): - - """A DNS service record""" - - def __init__(self, name, type_, class_, ttl, - priority, weight, port, server): - DNSRecord.__init__(self, name, type_, class_, ttl) - self.priority = priority - self.weight = weight - self.port = port - self.server = server - - def write(self, out): - """Used in constructing an outgoing packet""" - out.write_short(self.priority) - out.write_short(self.weight) - out.write_short(self.port) - out.write_name(self.server) - - def __eq__(self, other): - """Tests equality on priority, weight, port and server""" - return (isinstance(other, DNSService) and - DNSEntry.__eq__(self, other) and - self.priority == other.priority and - self.weight == other.weight and - self.port == other.port and - self.server == other.server) - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def __repr__(self): - """String representation""" - return self.to_string("%s:%s" % (self.server, self.port)) - - -class DNSIncoming(QuietLogger): - - """Object representation of an incoming DNS packet""" - - def __init__(self, data): - """Constructor from string holding bytes of packet""" - self.offset = 0 - self.data = data - self.questions = [] # type: List[DNSQuestion] - self.answers = [] # type: List[DNSRecord] - self.id = 0 - self.flags = 0 # type: int - self.num_questions = 0 - self.num_answers = 0 - self.num_authorities = 0 - self.num_additionals = 0 - self.valid = False - - try: - self.read_header() - self.read_questions() - self.read_others() - self.valid = True - - except (IndexError, struct.error, IncomingDecodeError): - self.log_exception_warning(( - 'Choked at offset %d while unpacking %r', self.offset, data)) - - def unpack(self, format_): - length = struct.calcsize(format_) - info = struct.unpack( - format_, self.data[self.offset:self.offset + length]) - self.offset += length - return info - - def read_header(self): - """Reads header portion of packet""" - (self.id, self.flags, self.num_questions, self.num_answers, - self.num_authorities, self.num_additionals) = self.unpack(b'!6H') - - def read_questions(self): - """Reads questions section of packet""" - for i in range(self.num_questions): - name = self.read_name() - type_, class_ = self.unpack(b'!HH') - - question = DNSQuestion(name, type_, class_) - self.questions.append(question) - - # def read_int(self): - # """Reads an integer from the packet""" - # return self.unpack(b'!I')[0] - - def read_character_string(self): - """Reads a character string from the packet""" - length = self.data[self.offset] - self.offset += 1 - return self.read_string(length) - - def read_string(self, length): - """Reads a string of a given length from the packet""" - info = self.data[self.offset:self.offset + length] - self.offset += length - return info - - def read_unsigned_short(self): - """Reads an unsigned short from the packet""" - return self.unpack(b'!H')[0] - - def read_others(self): - """Reads the answers, authorities and additionals section of the - packet""" - n = self.num_answers + self.num_authorities + self.num_additionals - for i in range(n): - domain = self.read_name() - type_, class_, ttl, length = self.unpack(b'!HHiH') - - rec = None # type: Optional[DNSRecord] - if type_ == _TYPE_A: - rec = DNSAddress( - domain, type_, class_, ttl, self.read_string(4)) - elif type_ == _TYPE_CNAME or type_ == _TYPE_PTR: - rec = DNSPointer( - domain, type_, class_, ttl, self.read_name()) - elif type_ == _TYPE_TXT: - rec = DNSText( - domain, type_, class_, ttl, self.read_string(length)) - elif type_ == _TYPE_SRV: - rec = DNSService( - domain, type_, class_, ttl, - self.read_unsigned_short(), self.read_unsigned_short(), - self.read_unsigned_short(), self.read_name()) - elif type_ == _TYPE_HINFO: - rec = DNSHinfo( - domain, type_, class_, ttl, - self.read_character_string(), self.read_character_string()) - elif type_ == _TYPE_AAAA: - rec = DNSAddress( - domain, type_, class_, ttl, self.read_string(16)) - else: - # Try to ignore types we don't know about - # Skip the payload for the resource record so the next - # records can be parsed correctly - self.offset += length - - if rec is not None: - self.answers.append(rec) - - def is_query(self) -> bool: - """Returns true if this is a query""" - return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY - - def is_response(self): - """Returns true if this is a response""" - return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE - - def read_utf(self, offset, length): - """Reads a UTF-8 string of a given length from the packet""" - return str(self.data[offset:offset + length], 'utf-8', 'replace') - - def read_name(self): - """Reads a domain name from the packet""" - result = '' - off = self.offset - next_ = -1 - first = off - - while True: - length = self.data[off] - off += 1 - if length == 0: - break - t = length & 0xC0 - if t == 0x00: - result = ''.join((result, self.read_utf(off, length) + '.')) - off += length - elif t == 0xC0: - if next_ < 0: - next_ = off + 1 - off = ((length & 0x3F) << 8) | self.data[off] - if off >= first: - raise IncomingDecodeError( - "Bad domain name (circular) at %s" % (off,)) - first = off - else: - raise IncomingDecodeError("Bad domain name at %s" % (off,)) - - if next_ >= 0: - self.offset = next_ - else: - self.offset = off - - return result - - -class DNSOutgoing: - - """Object representation of an outgoing packet""" - - def __init__(self, flags, multicast=True): - self.finished = False - self.id = 0 - self.multicast = multicast - self.flags = flags - self.names = {} # type: Dict[str, int] - self.data = [] # type: List[bytes] - self.size = 12 - self.state = self.State.init - - self.questions = [] # type: List[DNSQuestion] - self.answers = [] # type: List[Tuple[DNSEntry, float]] - self.authorities = [] # type: List[DNSPointer] - self.additionals = [] # type: List[DNSAddress] - - def __repr__(self): - return '' % ', '.join([ - 'multicast=%s' % self.multicast, - 'flags=%s' % self.flags, - 'questions=%s' % self.questions, - 'answers=%s' % self.answers, - 'authorities=%s' % self.authorities, - 'additionals=%s' % self.additionals, - ]) - - class State(enum.Enum): - init = 0 - finished = 1 - - def add_question(self, record): - """Adds a question""" - self.questions.append(record) - - def add_answer(self, inp, record): - """Adds an answer""" - if not record.suppressed_by(inp): - self.add_answer_at_time(record, 0) - - def add_answer_at_time(self, record, now): - """Adds an answer if it does not expire by a certain time""" - if record is not None: - if now == 0 or not record.is_expired(now): - self.answers.append((record, now)) - - def add_authorative_answer(self, record): - """Adds an authoritative answer""" - self.authorities.append(record) - - def add_additional_answer(self, record): - """ Adds an additional answer - - From: RFC 6763, DNS-Based Service Discovery, February 2013 - - 12. DNS Additional Record Generation - - DNS has an efficiency feature whereby a DNS server may place - additional records in the additional section of the DNS message. - These additional records are records that the client did not - explicitly request, but the server has reasonable grounds to expect - that the client might request them shortly, so including them can - save the client from having to issue additional queries. - - This section recommends which additional records SHOULD be generated - to improve network efficiency, for both Unicast and Multicast DNS-SD - responses. - - 12.1. PTR Records - - When including a DNS-SD Service Instance Enumeration or Selective - Instance Enumeration (subtype) PTR record in a response packet, the - server/responder SHOULD include the following additional records: - - o The SRV record(s) named in the PTR rdata. - o The TXT record(s) named in the PTR rdata. - o All address records (type "A" and "AAAA") named in the SRV rdata. - - 12.2. SRV Records - - When including an SRV record in a response packet, the - server/responder SHOULD include the following additional records: - - o All address records (type "A" and "AAAA") named in the SRV rdata. - - """ - self.additionals.append(record) - - def pack(self, format_, value): - self.data.append(struct.pack(format_, value)) - self.size += struct.calcsize(format_) - - def write_byte(self, value): - """Writes a single byte to the packet""" - self.pack(b'!c', int2byte(value)) - - def insert_short(self, index, value): - """Inserts an unsigned short in a certain position in the packet""" - self.data.insert(index, struct.pack(b'!H', value)) - self.size += 2 - - def write_short(self, value): - """Writes an unsigned short to the packet""" - self.pack(b'!H', value) - - def write_int(self, value): - """Writes an unsigned integer to the packet""" - self.pack(b'!I', int(value)) - - def write_string(self, value): - """Writes a string to the packet""" - assert isinstance(value, bytes) - self.data.append(value) - self.size += len(value) - - def write_utf(self, s): - """Writes a UTF-8 string of a given length to the packet""" - utfstr = s.encode('utf-8') - length = len(utfstr) - if length > 64: - raise NamePartTooLongException - self.write_byte(length) - self.write_string(utfstr) - - def write_character_string(self, value): - assert isinstance(value, bytes) - length = len(value) - if length > 256: - raise NamePartTooLongException - self.write_byte(length) - self.write_string(value) - - def write_name(self, name): - """ - Write names to packet - - 18.14. Name Compression - - When generating Multicast DNS messages, implementations SHOULD use - name compression wherever possible to compress the names of resource - records, by replacing some or all of the resource record name with a - compact two-byte reference to an appearance of that data somewhere - earlier in the message [RFC1035]. - """ - - # split name into each label - parts = name.split('.') - if not parts[-1]: - parts.pop() - - # construct each suffix - name_suffices = ['.'.join(parts[i:]) for i in range(len(parts))] - - # look for an existing name or suffix - for count, sub_name in enumerate(name_suffices): - if sub_name in self.names: - break - else: - count = len(name_suffices) - - # note the new names we are saving into the packet - name_length = len(name.encode('utf-8')) - for suffix in name_suffices[:count]: - self.names[suffix] = self.size + name_length - len(suffix.encode('utf-8')) - 1 - - # write the new names out. - for part in parts[:count]: - self.write_utf(part) - - # if we wrote part of the name, create a pointer to the rest - if count != len(name_suffices): - # Found substring in packet, create pointer - index = self.names[name_suffices[count]] - self.write_byte((index >> 8) | 0xC0) - self.write_byte(index & 0xFF) - else: - # this is the end of a name - self.write_byte(0) - - def write_question(self, question): - """Writes a question to the packet""" - self.write_name(question.name) - self.write_short(question.type) - self.write_short(question.class_) - - def write_record(self, record, now): - """Writes a record (answer, authoritative answer, additional) to - the packet""" - if self.state == self.State.finished: - return 1 - - start_data_length, start_size = len(self.data), self.size - self.write_name(record.name) - self.write_short(record.type) - if record.unique and self.multicast: - self.write_short(record.class_ | _CLASS_UNIQUE) - else: - self.write_short(record.class_) - if now == 0: - self.write_int(record.ttl) - else: - self.write_int(record.get_remaining_ttl(now)) - index = len(self.data) - - # Adjust size for the short we will write before this record - self.size += 2 - record.write(self) - self.size -= 2 - - length = sum((len(d) for d in self.data[index:])) - # Here is the short we adjusted for - self.insert_short(index, length) - - # if we go over, then rollback and quit - if self.size > _MAX_MSG_ABSOLUTE: - while len(self.data) > start_data_length: - self.data.pop() - self.size = start_size - self.state = self.State.finished - return 1 - return 0 - - def packet(self) -> bytes: - """Returns a string containing the packet's bytes - - No further parts should be added to the packet once this - is done.""" - - overrun_answers, overrun_authorities, overrun_additionals = 0, 0, 0 - - if self.state != self.State.finished: - for question in self.questions: - self.write_question(question) - for answer, time_ in self.answers: - overrun_answers += self.write_record(answer, time_) - for authority in self.authorities: - overrun_authorities += self.write_record(authority, 0) - for additional in self.additionals: - overrun_additionals += self.write_record(additional, 0) - self.state = self.State.finished - - self.insert_short(0, len(self.additionals) - overrun_additionals) - self.insert_short(0, len(self.authorities) - overrun_authorities) - self.insert_short(0, len(self.answers) - overrun_answers) - self.insert_short(0, len(self.questions)) - self.insert_short(0, self.flags) - if self.multicast: - self.insert_short(0, 0) - else: - self.insert_short(0, self.id) - return b''.join(self.data) - - -class DNSCache: - - """A cache of DNS entries""" - - def __init__(self): - self.cache = {} # type: Dict[str, List[DNSEntry]] - - def add(self, entry): - """Adds an entry""" - # Insert first in list so get returns newest entry - self.cache.setdefault(entry.key, []).insert(0, entry) - - def remove(self, entry): - """Removes an entry""" - try: - list_ = self.cache[entry.key] - list_.remove(entry) - except (KeyError, ValueError): - pass - - def get(self, entry): - """Gets an entry by key. Will return None if there is no - matching entry.""" - try: - list_ = self.cache[entry.key] - for cached_entry in list_: - if entry.__eq__(cached_entry): - return cached_entry - except (KeyError, ValueError): - return None - - def get_by_details(self, name, type_, class_): - """Gets an entry by details. Will return None if there is - no matching entry.""" - entry = DNSEntry(name, type_, class_) - return self.get(entry) - - def entries_with_name(self, name): - """Returns a list of entries whose key matches the name.""" - try: - return self.cache[name.lower()] - except KeyError: - return [] - - def current_entry_with_name_and_alias(self, name, alias): - now = current_time_millis() - for record in self.entries_with_name(name): - if (record.type == _TYPE_PTR and - not record.is_expired(now) and - record.alias == alias): - return record - - def entries(self): - """Returns a list of all entries""" - if not self.cache: - return [] - else: - # avoid size change during iteration by copying the cache - values = list(self.cache.values()) - return reduce(lambda a, b: a + b, values) - - -class Engine(threading.Thread): - - """An engine wraps read access to sockets, allowing objects that - need to receive data from sockets to be called back when the - sockets are ready. - - A reader needs a handle_read() method, which is called when the socket - it is interested in is ready for reading. - - Writers are not implemented here, because we only send short - packets. - """ - - def __init__(self, zc): - threading.Thread.__init__(self, name='zeroconf-Engine') - self.daemon = True - self.zc = zc - self.readers = {} # type: Dict[socket.socket, Listener] - self.timeout = 5 - self.condition = threading.Condition() - self.start() - - def run(self): - while not self.zc.done: - with self.condition: - rs = self.readers.keys() - if len(rs) == 0: - # No sockets to manage, but we wait for the timeout - # or addition of a socket - self.condition.wait(self.timeout) - - if len(rs) != 0: - try: - rr, wr, er = select.select(rs, [], [], self.timeout) - if not self.zc.done: - for socket_ in rr: - reader = self.readers.get(socket_) - if reader: - reader.handle_read(socket_) - - except (select.error, socket.error) as e: - # If the socket was closed by another thread, during - # shutdown, ignore it and exit - if e.args[0] != socket.EBADF or not self.zc.done: - raise - - def add_reader(self, reader, socket_): - with self.condition: - self.readers[socket_] = reader - self.condition.notify() - - def del_reader(self, socket_): - with self.condition: - del self.readers[socket_] - self.condition.notify() - - -class Listener(QuietLogger): - - """A Listener is used by this module to listen on the multicast - group to which DNS messages are sent, allowing the implementation - to cache information as it arrives. - - It requires registration with an Engine object in order to have - the read() method called when a socket is available for reading.""" - - def __init__(self, zc): - self.zc = zc - self.data = None - - def handle_read(self, socket_): - try: - data, (addr, port) = socket_.recvfrom(_MAX_MSG_ABSOLUTE) - except Exception: - self.log_exception_warning() - return - - log.debug('Received from %r:%r: %r ', addr, port, data) - - self.data = data - msg = DNSIncoming(data) - if not msg.valid: - pass - - elif msg.is_query(): - # Always multicast responses - if port == _MDNS_PORT: - self.zc.handle_query(msg, _MDNS_ADDR, _MDNS_PORT) - - # If it's not a multicast query, reply via unicast - # and multicast - elif port == _DNS_PORT: - self.zc.handle_query(msg, addr, port) - self.zc.handle_query(msg, _MDNS_ADDR, _MDNS_PORT) - - else: - self.zc.handle_response(msg) - - -class Reaper(threading.Thread): - - """A Reaper is used by this module to remove cache entries that - have expired.""" - - def __init__(self, zc): - threading.Thread.__init__(self, name='zeroconf-Reaper') - self.daemon = True - self.zc = zc - self.start() - - def run(self): - while True: - self.zc.wait(10 * 1000) - if self.zc.done: - return - now = current_time_millis() - for record in self.zc.cache.entries(): - if record.is_expired(now): - self.zc.update_record(now, record) - self.zc.cache.remove(record) - - -class Signal: - def __init__(self): - self._handlers = [] # type: List[Callable[..., None]] - - def fire(self, **kwargs) -> None: - for h in list(self._handlers): - h(**kwargs) - - @property - def registration_interface(self) -> 'SignalRegistrationInterface': - return SignalRegistrationInterface(self._handlers) - - -class SignalRegistrationInterface: - - def __init__(self, handlers): - self._handlers = handlers - - def register_handler(self, handler): - self._handlers.append(handler) - return self - - def unregister_handler(self, handler): - self._handlers.remove(handler) - return self - - -class RecordUpdateListener: - def update_record(self, zc: 'Zeroconf', now: float, record: DNSRecord) -> None: - raise NotImplementedError() - - -class ServiceListener: - def add_service(self, zc, type_, name) -> None: - raise NotImplementedError() - - def remove_service(self, zc, type_, name) -> None: - raise NotImplementedError() - - -class ServiceBrowser(RecordUpdateListener, threading.Thread): - - """Used to browse for a service of a specific type. - - The listener object will have its add_service() and - remove_service() methods called when this browser - discovers changes in the services availability.""" - - def __init__(self, zc: 'Zeroconf', type_: str, handlers=None, listener=None, - addr: str = _MDNS_ADDR, port: int = _MDNS_PORT, delay: int = _BROWSER_TIME) -> None: - """Creates a browser for a specific type""" - assert handlers or listener, 'You need to specify at least one handler' - if not type_.endswith(service_type_name(type_, allow_underscores=True)): - raise BadTypeInNameException - threading.Thread.__init__( - self, name='zeroconf-ServiceBrowser_' + type_) - self.daemon = True - self.zc = zc - self.type = type_ - self.addr = addr - self.port = port - self.multicast = (self.addr == _MDNS_ADDR) - self.services = {} # type: Dict[str, DNSRecord] - self.next_time = current_time_millis() - self.delay = delay - self._handlers_to_call = [] # type: List[Callable[[Zeroconf], None]] - - self._service_state_changed = Signal() - - self.done = False - - if hasattr(handlers, 'add_service'): - listener = handlers - handlers = None - - handlers = handlers or [] - - if listener: - def on_change(zeroconf, service_type, name, state_change): - args = (zeroconf, service_type, name) - if state_change is ServiceStateChange.Added: - listener.add_service(*args) - elif state_change is ServiceStateChange.Removed: - listener.remove_service(*args) - else: - raise NotImplementedError(state_change) - handlers.append(on_change) - - for h in handlers: - self.service_state_changed.register_handler(h) - - self.start() - - @property - def service_state_changed(self) -> SignalRegistrationInterface: - return self._service_state_changed.registration_interface - - def update_record(self, zc: 'Zeroconf', now: float, record: DNSRecord) -> None: - """Callback invoked by Zeroconf when new information arrives. - - Updates information required by browser in the Zeroconf cache.""" - - def enqueue_callback(state_change: ServiceStateChange, name: str) -> None: - self._handlers_to_call.append( - lambda zeroconf: self._service_state_changed.fire( - zeroconf=zeroconf, - service_type=self.type, - name=name, - state_change=state_change, - )) - - if record.type == _TYPE_PTR and record.name == self.type: - assert isinstance(record, DNSPointer) - expired = record.is_expired(now) - service_key = record.alias.lower() - try: - old_record = self.services[service_key] - except KeyError: - if not expired: - self.services[service_key] = record - enqueue_callback(ServiceStateChange.Added, record.alias) - else: - if not expired: - old_record.reset_ttl(record) - else: - del self.services[service_key] - enqueue_callback(ServiceStateChange.Removed, record.alias) - return - - expires = record.get_expiration_time(75) - if expires < self.next_time: - self.next_time = expires - - def cancel(self): - self.done = True - self.zc.remove_listener(self) - self.join() - - def run(self): - self.zc.add_listener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) - - while True: - now = current_time_millis() - if len(self._handlers_to_call) == 0 and self.next_time > now: - self.zc.wait(self.next_time - now) - if self.zc.done or self.done: - return - now = current_time_millis() - if self.next_time <= now: - out = DNSOutgoing(_FLAGS_QR_QUERY, multicast=self.multicast) - out.add_question(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) - for record in self.services.values(): - if not record.is_stale(now): - out.add_answer_at_time(record, now) - - self.zc.send(out, addr=self.addr, port=self.port) - self.next_time = now + self.delay - self.delay = min(20 * 1000, self.delay * 2) - - if len(self._handlers_to_call) > 0 and not self.zc.done: - handler = self._handlers_to_call.pop(0) - handler(self.zc) - - -ServicePropertiesType = Dict[AnyStr, Union[None, bool, AnyStr, float]] - - -class ServiceInfo(RecordUpdateListener): - - """Service information""" - - def __init__(self, type_: str, name: str, address: Optional[bytes] = None, port: Optional[int] = None, - weight: int = 0, priority: int = 0, properties=b'', server: Optional[str] = None) -> None: - """Create a service description. - - type_: fully qualified service type name - name: fully qualified service name - address: IP address as unsigned short, network byte order - port: port that the service runs on - weight: weight of the service - priority: priority of the service - properties: dictionary of properties (or a string holding the - bytes for the text field) - server: fully qualified name for service host (defaults to name)""" - - if not type_.endswith(service_type_name(name, allow_underscores=True)): - raise BadTypeInNameException - self.type = type_ - self.name = name - self.address = address - self.port = port - self.weight = weight - self.priority = priority - if server: - self.server = server - else: - self.server = name - self._properties = {} # type: ServicePropertiesType - self._set_properties(properties) - # FIXME: this is here only so that mypy doesn't complain when we set and then use the attribute when - # registering services. See if setting this to None by default is the right way to go. - self.ttl = None # type: Optional[int] - - @property - def properties(self) -> ServicePropertiesType: - return self._properties - - def _set_properties(self, properties: Union[bytes, ServicePropertiesType]): - """Sets properties and text of this info from a dictionary""" - if isinstance(properties, dict): - self._properties = properties - list_ = [] - result = b'' - for key, value in properties.items(): - if isinstance(key, str): - key = key.encode('utf-8') - - if value is None: - suffix = b'' - elif isinstance(value, str): - suffix = value.encode('utf-8') - elif isinstance(value, bytes): - suffix = value - elif isinstance(value, int): - if value: - suffix = b'true' - else: - suffix = b'false' - else: - suffix = b'' - list_.append(b'='.join((key, suffix))) - for item in list_: - result = b''.join((result, int2byte(len(item)), item)) - self.text = result - else: - self.text = properties - - def _set_text(self, text): - """Sets properties and text given a text field""" - self.text = text - result = {} # type: ServicePropertiesType - end = len(text) - index = 0 - strs = [] - while index < end: - length = text[index] - index += 1 - strs.append(text[index:index + length]) - index += length - - for s in strs: - parts = s.split(b'=', 1) - try: - key, value = parts - except ValueError: - # No equals sign at all - key = s - value = False - else: - if value == b'true': - value = True - elif value == b'false' or not value: - value = False - - # Only update non-existent properties - if key and result.get(key) is None: - result[key] = value - - self._properties = result - - def get_name(self): - """Name accessor""" - if self.type is not None and self.name.endswith("." + self.type): - return self.name[:len(self.name) - len(self.type) - 1] - return self.name - - def update_record(self, zc: 'Zeroconf', now: float, record: DNSRecord) -> None: - """Updates service information from a DNS record""" - if record is not None and not record.is_expired(now): - if record.type == _TYPE_A: - assert isinstance(record, DNSAddress) - # if record.name == self.name: - if record.name == self.server: - self.address = record.address - elif record.type == _TYPE_SRV: - assert isinstance(record, DNSService) - if record.name == self.name: - self.server = record.server - self.port = record.port - self.weight = record.weight - self.priority = record.priority - # self.address = None - self.update_record( - zc, now, zc.cache.get_by_details( - self.server, _TYPE_A, _CLASS_IN)) - elif record.type == _TYPE_TXT: - assert isinstance(record, DNSText) - if record.name == self.name: - self._set_text(record.text) - - def request(self, zc: 'Zeroconf', timeout: float) -> bool: - """Returns true if the service could be discovered on the - network, and updates this object with details discovered. - """ - now = current_time_millis() - delay = _LISTENER_TIME - next_ = now + delay - last = now + timeout - - record_types_for_check_cache = [ - (_TYPE_SRV, _CLASS_IN), - (_TYPE_TXT, _CLASS_IN), - ] - if self.server is not None: - record_types_for_check_cache.append((_TYPE_A, _CLASS_IN)) - for record_type in record_types_for_check_cache: - cached = zc.cache.get_by_details(self.name, *record_type) - if cached: - self.update_record(zc, now, cached) - - if None not in (self.server, self.address, self.text): - return True - - try: - zc.add_listener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) - while None in (self.server, self.address, self.text): - if last <= now: - return False - if next_ <= now: - out = DNSOutgoing(_FLAGS_QR_QUERY) - out.add_question( - DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN)) - out.add_answer_at_time( - zc.cache.get_by_details( - self.name, _TYPE_SRV, _CLASS_IN), now) - - out.add_question( - DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN)) - out.add_answer_at_time( - zc.cache.get_by_details( - self.name, _TYPE_TXT, _CLASS_IN), now) - - if self.server is not None: - out.add_question( - DNSQuestion(self.server, _TYPE_A, _CLASS_IN)) - out.add_answer_at_time( - zc.cache.get_by_details( - self.server, _TYPE_A, _CLASS_IN), now) - zc.send(out) - next_ = now + delay - delay *= 2 - - zc.wait(min(next_, last) - now) - now = current_time_millis() - finally: - zc.remove_listener(self) - - return True - - def __eq__(self, other: object) -> bool: - """Tests equality of service name""" - return isinstance(other, ServiceInfo) and other.name == self.name - - def __ne__(self, other: object) -> bool: - """Non-equality test""" - return not self.__eq__(other) - - def __repr__(self) -> str: - """String representation""" - return '%s(%s)' % ( - type(self).__name__, - ', '.join( - '%s=%r' % (name, getattr(self, name)) - for name in ( - 'type', 'name', 'address', 'port', 'weight', 'priority', - 'server', 'properties', - ) - ) - ) - - -class ZeroconfServiceTypes(ServiceListener): - """ - Return all of the advertised services on any local networks - """ - def __init__(self): - self.found_services = set() # type: Set[str] - - def add_service(self, zc, type_, name): - self.found_services.add(name) - - def remove_service(self, zc, type_, name): - pass - - @classmethod - def find(cls, zc=None, timeout=5, interfaces=InterfaceChoice.All): - """ - Return all of the advertised services on any local networks. - - :param zc: Zeroconf() instance. Pass in if already have an - instance running or if non-default interfaces are needed - :param timeout: seconds to wait for any responses - :return: tuple of service type strings - """ - local_zc = zc or Zeroconf(interfaces=interfaces) - listener = cls() - browser = ServiceBrowser( - local_zc, '_services._dns-sd._udp.local.', listener=listener) - - # wait for responses - time.sleep(timeout) - - # close down anything we opened - if zc is None: - local_zc.close() - else: - browser.cancel() - - return tuple(sorted(listener.found_services)) - - -def get_all_addresses() -> List[str]: - return list(set( - addr.ip - for iface in ifaddr.get_adapters() - for addr in iface.ips - if addr.is_IPv4 and addr.network_prefix != 32 # Host only netmask 255.255.255.255 - )) - - -def normalize_interface_choice(choice: Union[List[str], InterfaceChoice]) -> List[str]: - if choice is InterfaceChoice.Default: - return ['0.0.0.0'] - elif choice is InterfaceChoice.All: - return get_all_addresses() - else: - assert isinstance(choice, list) - return choice - - -def new_socket(port: int = _MDNS_PORT) -> socket.socket: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - # SO_REUSEADDR should be equivalent to SO_REUSEPORT for - # multicast UDP sockets (p 731, "TCP/IP Illustrated, - # Volume 2"), but some BSD-derived systems require - # SO_REUSEPORT to be specified explicitly. Also, not all - # versions of Python have SO_REUSEPORT available. - # Catch OSError and socket.error for kernel versions <3.9 because lacking - # SO_REUSEPORT support. - try: - reuseport = socket.SO_REUSEPORT - except AttributeError: - pass - else: - try: - s.setsockopt(socket.SOL_SOCKET, reuseport, 1) - except (OSError, socket.error) as err: - # OSError on python 3, socket.error on python 2 - if not err.errno == errno.ENOPROTOOPT: - raise - - if port is _MDNS_PORT: - # OpenBSD needs the ttl and loop values for the IP_MULTICAST_TTL and - # IP_MULTICAST_LOOP socket options as an unsigned char. - ttl = struct.pack(b'B', 255) - s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl) - loop = struct.pack(b'B', 1) - s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, loop) - - s.bind(('', port)) - return s - - -def get_errno(e: Exception) -> int: - assert isinstance(e, socket.error) - return cast(int, e.args[0]) - - -class Zeroconf(QuietLogger): - - """Implementation of Zeroconf Multicast DNS Service Discovery - - Supports registration, unregistration, queries and browsing. - """ - - def __init__( - self, - interfaces: Union[List[str], InterfaceChoice] = InterfaceChoice.All, - unicast: bool = False - ) -> None: - """Creates an instance of the Zeroconf class, establishing - multicast communications, listening and reaping threads. - - :type interfaces: :class:`InterfaceChoice` or sequence of ip addresses - """ - # hook for threads - self._GLOBAL_DONE = False - self.unicast = unicast - - if not unicast: - self._listen_socket = new_socket() - interfaces = normalize_interface_choice(interfaces) - - self._respond_sockets = [] # type: List[socket.socket] - - for i in interfaces: - if not unicast: - log.debug('Adding %r to multicast group', i) - try: - _value = socket.inet_aton(_MDNS_ADDR) + socket.inet_aton(i) - self._listen_socket.setsockopt( - socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, _value) - except socket.error as e: - _errno = get_errno(e) - if _errno == errno.EADDRINUSE: - log.info( - 'Address in use when adding %s to multicast group, ' - 'it is expected to happen on some systems', i, - ) - elif _errno == errno.EADDRNOTAVAIL: - log.info( - 'Address not available when adding %s to multicast ' - 'group, it is expected to happen on some systems', i, - ) - continue - elif _errno == errno.EINVAL: - log.info( - 'Interface of %s does not support multicast, ' - 'it is expected in WSL', i - ) - continue - - else: - raise - - respond_socket = new_socket() - respond_socket.setsockopt( - socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(i)) - else: - respond_socket = new_socket(port=0) - - self._respond_sockets.append(respond_socket) - - self.listeners = [] # type: List[RecordUpdateListener] - self.browsers = {} # type: Dict[ServiceListener, ServiceBrowser] - self.services = {} # type: Dict[str, ServiceInfo] - self.servicetypes = {} # type: Dict[str, int] - - self.cache = DNSCache() - - self.condition = threading.Condition() - - self.engine = Engine(self) - self.listener = Listener(self) - if not unicast: - self.engine.add_reader(self.listener, self._listen_socket) - else: - for s in self._respond_sockets: - self.engine.add_reader(self.listener, s) - self.reaper = Reaper(self) - - self.debug = None # type: Optional[DNSOutgoing] - - @property - def done(self) -> bool: - return self._GLOBAL_DONE - - def wait(self, timeout: float) -> None: - """Calling thread waits for a given number of milliseconds or - until notified.""" - with self.condition: - self.condition.wait(timeout / 1000.0) - - def notify_all(self) -> None: - """Notifies all waiting threads""" - with self.condition: - self.condition.notify_all() - - def get_service_info(self, type_: str, name: str, timeout: int = 3000) -> Optional[ServiceInfo]: - """Returns network's service information for a particular - name and type, or None if no service matches by the timeout, - which defaults to 3 seconds.""" - info = ServiceInfo(type_, name) - if info.request(self, timeout): - return info - return None - - def add_service_listener(self, type_: str, listener: ServiceListener) -> None: - """Adds a listener for a particular service type. This object - will then have its add_service and remove_service methods called when - services of that type become available and unavailable.""" - self.remove_service_listener(listener) - self.browsers[listener] = ServiceBrowser(self, type_, listener) - - def remove_service_listener(self, listener: ServiceListener) -> None: - """Removes a listener from the set that is currently listening.""" - if listener in self.browsers: - self.browsers[listener].cancel() - del self.browsers[listener] - - def remove_all_service_listeners(self) -> None: - """Removes a listener from the set that is currently listening.""" - for listener in [k for k in self.browsers]: - self.remove_service_listener(listener) - - def register_service( - self, info: ServiceInfo, ttl: int = _DNS_TTL, allow_name_change: bool = False, - ) -> None: - """Registers service information to the network with a default TTL - of 60 seconds. Zeroconf will then respond to requests for - information for that service. The name of the service may be - changed if needed to make it unique on the network.""" - info.ttl = ttl - self.check_service(info, allow_name_change) - self.services[info.name.lower()] = info - if info.type in self.servicetypes: - self.servicetypes[info.type] += 1 - else: - self.servicetypes[info.type] = 1 - now = current_time_millis() - next_time = now - i = 0 - while i < 3: - if now < next_time: - self.wait(next_time - now) - now = current_time_millis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.add_answer_at_time( - DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0) - out.add_answer_at_time( - DNSService(info.name, _TYPE_SRV, _CLASS_IN, - ttl, info.priority, info.weight, info.port, - info.server), 0) - - out.add_answer_at_time( - DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0) - if info.address: - out.add_answer_at_time( - DNSAddress(info.server, _TYPE_A, _CLASS_IN, - ttl, info.address), 0) - self.send(out) - i += 1 - next_time += _REGISTER_TIME - - def unregister_service(self, info: ServiceInfo) -> None: - """Unregister a service.""" - try: - del self.services[info.name.lower()] - if self.servicetypes[info.type] > 1: - self.servicetypes[info.type] -= 1 - else: - del self.servicetypes[info.type] - except Exception as e: # TODO stop catching all Exceptions - log.exception('Unknown error, possibly benign: %r', e) - now = current_time_millis() - next_time = now - i = 0 - while i < 3: - if now < next_time: - self.wait(next_time - now) - now = current_time_millis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.add_answer_at_time( - DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.add_answer_at_time( - DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, - info.priority, info.weight, info.port, info.name), 0) - out.add_answer_at_time( - DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) - - if info.address: - out.add_answer_at_time( - DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, - info.address), 0) - self.send(out) - i += 1 - next_time += _UNREGISTER_TIME - - def unregister_all_services(self) -> None: - """Unregister all registered services.""" - if len(self.services) > 0: - now = current_time_millis() - next_time = now - i = 0 - while i < 3: - if now < next_time: - self.wait(next_time - now) - now = current_time_millis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - for info in self.services.values(): - out.add_answer_at_time(DNSPointer( - info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.add_answer_at_time(DNSService( - info.name, _TYPE_SRV, _CLASS_IN, 0, - info.priority, info.weight, info.port, info.server), 0) - out.add_answer_at_time(DNSText( - info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) - if info.address: - out.add_answer_at_time(DNSAddress( - info.server, _TYPE_A, _CLASS_IN, 0, - info.address), 0) - self.send(out) - i += 1 - next_time += _UNREGISTER_TIME - - def check_service(self, info: ServiceInfo, allow_name_change: bool) -> None: - """Checks the network for a unique service name, modifying the - ServiceInfo passed in if it is not unique.""" - - # This is kind of funky because of the subtype based tests - # need to make subtypes a first class citizen - service_name = service_type_name(info.name) - if not info.type.endswith(service_name): - raise BadTypeInNameException - - instance_name = info.name[:-len(service_name) - 1] - next_instance_number = 2 - - now = current_time_millis() - next_time = now - i = 0 - while i < 3: - # check for a name conflict - while self.cache.current_entry_with_name_and_alias( - info.type, info.name): - if not allow_name_change: - raise NonUniqueNameException - - # change the name and look for a conflict - info.name = '%s-%s.%s' % ( - instance_name, next_instance_number, info.type) - next_instance_number += 1 - service_type_name(info.name) - next_time = now - i = 0 - - if now < next_time: - self.wait(next_time - now) - now = current_time_millis() - continue - - out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) - self.debug = out - out.add_question(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) - out.add_authorative_answer(DNSPointer( - info.type, _TYPE_PTR, _CLASS_IN, info.ttl, info.name)) - self.send(out) - i += 1 - next_time += _CHECK_TIME - - def add_listener(self, listener: RecordUpdateListener, question: Optional[DNSQuestion]) -> None: - """Adds a listener for a given question. The listener will have - its update_record method called when information is available to - answer the question.""" - now = current_time_millis() - self.listeners.append(listener) - if question is not None: - for record in self.cache.entries_with_name(question.name): - if question.answered_by(record) and not record.is_expired(now): - listener.update_record(self, now, record) - self.notify_all() - - def remove_listener(self, listener: RecordUpdateListener) -> None: - """Removes a listener.""" - try: - self.listeners.remove(listener) - self.notify_all() - except Exception as e: # TODO stop catching all Exceptions - log.exception('Unknown error, possibly benign: %r', e) - - def update_record(self, now: float, rec: DNSRecord) -> None: - """Used to notify listeners of new information that has updated - a record.""" - for listener in self.listeners: - listener.update_record(self, now, rec) - self.notify_all() - - def handle_response(self, msg: DNSIncoming) -> None: - """Deal with incoming response packets. All answers - are held in the cache, and listeners are notified.""" - now = current_time_millis() - for record in msg.answers: - expired = record.is_expired(now) - if record in self.cache.entries(): - if expired: - self.cache.remove(record) - else: - entry = self.cache.get(record) - if entry is not None: - entry.reset_ttl(record) - else: - self.cache.add(record) - - for record in msg.answers: - self.update_record(now, record) - - def handle_query(self, msg: DNSIncoming, addr: str, port: int) -> None: - """Deal with incoming query packets. Provides a response if - possible.""" - out = None - - # Support unicast client responses - # - if port != _MDNS_PORT: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, multicast=False) - for question in msg.questions: - out.add_question(question) - - for question in msg.questions: - if question.type == _TYPE_PTR: - if question.name == "_services._dns-sd._udp.local.": - for stype in self.servicetypes.keys(): - if out is None: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.add_answer(msg, DNSPointer( - "_services._dns-sd._udp.local.", _TYPE_PTR, - _CLASS_IN, _DNS_TTL, stype)) - for service in self.services.values(): - if question.name == service.type: - if out is None: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.add_answer(msg, DNSPointer( - service.type, _TYPE_PTR, - _CLASS_IN, service.ttl, service.name)) - else: - try: - if out is None: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - - # Answer A record queries for any service addresses we know - if question.type in (_TYPE_A, _TYPE_ANY): - for service in self.services.values(): - if service.server == question.name.lower(): - out.add_answer(msg, DNSAddress( - question.name, _TYPE_A, - _CLASS_IN | _CLASS_UNIQUE, - service.ttl, service.address)) - - name_to_find = question.name.lower() - if name_to_find not in self.services: - continue - service = self.services[name_to_find] - - if question.type in (_TYPE_SRV, _TYPE_ANY): - out.add_answer(msg, DNSService( - question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, - service.ttl, service.priority, service.weight, - service.port, service.server)) - if question.type in (_TYPE_TXT, _TYPE_ANY): - out.add_answer(msg, DNSText( - question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, - service.ttl, service.text)) - if question.type == _TYPE_SRV: - out.add_additional_answer(DNSAddress( - service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, - service.ttl, service.address)) - except Exception: # TODO stop catching all Exceptions - self.log_exception_warning() - - if out is not None and out.answers: - out.id = msg.id - self.send(out, addr, port) - - def send(self, out: DNSOutgoing, addr: str = _MDNS_ADDR, port: int = _MDNS_PORT) -> None: - """Sends an outgoing packet.""" - packet = out.packet() - if len(packet) > _MAX_MSG_ABSOLUTE: - self.log_warning_once("Dropping %r over-sized packet (%d bytes) %r", - out, len(packet), packet) - return - log.debug('Sending %r (%d bytes) as %r...', out, len(packet), packet) - for s in self._respond_sockets: - if self._GLOBAL_DONE: - return - try: - bytes_sent = s.sendto(packet, 0, (addr, port)) - except Exception: # TODO stop catching all Exceptions - # on send errors, log the exception and keep going - self.log_exception_warning() - else: - if bytes_sent != len(packet): - self.log_warning_once( - '!!! sent %d out of %d bytes to %r' % ( - bytes_sent, len(packet), s)) - - def close(self) -> None: - """Ends the background threads, and prevent this instance from - servicing further queries.""" - if not self._GLOBAL_DONE: - self._GLOBAL_DONE = True - # remove service listeners - self.remove_all_service_listeners() - self.unregister_all_services() - - # shutdown recv socket and thread - if not self.unicast: - self.engine.del_reader(self._listen_socket) - self._listen_socket.close() - else: - for s in self._respond_sockets: - self.engine.del_reader(s) - self.engine.join() - - # shutdown the rest - self.notify_all() - self.reaper.join() - for s in self._respond_sockets: - s.close()