Skip to content

fix: keep renamed first labels within 63 bytes on the wire - #554

Merged
keepsimple1 merged 4 commits into
keepsimple1:mainfrom
Sealdot:fix-rename-label-too-long
Nov 25, 2026
Merged

keepsimple1 merged 4 commits into
keepsimple1:mainfrom
Sealdot:fix-rename-label-too-long

Conversation

@Sealdot

@Sealdot Sealdot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What

When a probe conflict triggers name_change / hostname_change, the appended (n) / -n suffix can push the first label past 63 bytes. write_name then returns NameTooLong for every probe and announcement, so the renamed service never reaches the network and the app gets no error.

This makes the rename shorten the base so the label with its new suffix still fits, like mDNSResponder does:

  • MAX_LABEL_BYTES is now pub(crate) and reused by service_daemon.
  • New escape-aware helpers in dns_parser.rs, following the same rules as parse_escaped_name (\. and \\ each count as one wire byte; any other \x keeps its backslash literal): truncate_label_to_wire_len (never cuts inside an escape pair or a UTF-8 character) and escaped_label_wire_len.
  • The base is shortened to 63 - suffix.len() wire bytes on every rename, since (9) -> (10) and -9 -> -10 grow the suffix. A trailing space (name_change) or - (hostname_change) left by the cut is trimmed, avoiding Foo (2) / foo--2.
  • register() runs check_instance_label_length (built on split_first_label + escaped_label_wire_len) and returns the error directly, so an instance label over 63 wire bytes fails at API time instead of silently at announcement time.
  • check_hostname now rejects any label over 63 wire bytes — the same per-label condition write_name enforces — fixing a pre-existing gap where a hostname with an oversized label registered fine and was never sent. Covers register() and resolve_hostname().

Fixes #549.

Testing

New regressions in service_daemon::tests and dns_parser::tests, all red before the fix:

  • 63-byte first label -> 59 + " (2)"; (9) -> (10) at the limit trims one more base byte; -9 -> -10 likewise.
  • A multi-byte character or an escape pair at the cut point is kept whole or dropped whole — no split UTF-8, no dangling backslash.
  • hostname_change with a 63-byte first label; trailing - trimmed.
  • check_instance_label_length: ok at 63, err at 64, and escaped-dot boundary cases.
  • check_hostname label cases, and register() rejecting oversized instance / hostname labels.
  • Wire-length assertions on the renamed labels confirm <= 63 bytes.

cargo test --lib 117/117, cargo test --doc 5/5, cargo clippy --all-targets clean, cargo fmt applied (cargo 1.97.1, macOS arm64). MSRV is Rust 1.71; only long-stable APIs are used, and MSRV CI covers it. Physical multi-host conflict renames are not covered here.

Notes

AI assistance: Cognition Devin CLI agent (SWE-2 High).

@Sealdot
Sealdot force-pushed the fix-rename-label-too-long branch from 65c519b to 674afe4 Compare November 19, 2026 12:56
@keepsimple1

Copy link
Copy Markdown
Owner

Thank you for working on this. When it's ready for review, please click "Ready for review". Here are some preliminary comments:

  • Hostname labels aren’t checked. A hostname with a first label over 63 bytes still registers fine and then never gets sent. This bug was already there; the PR fixes it for instance names only. The fix is the same length check in check_hostname.
  • The new instance-name check only reports through the monitor channel. It runs inside the daemon, so register() still returns Ok and apps that don’t watch for errors never hear about it. Running it in register() itself would return the error directly.
  • Trailing spaces and dashes are always trimmed. The comments mismatches with the actual implementation.

Sealdot added a commit to Sealdot/mdns-sd that referenced this pull request Nov 21, 2026
…ister()

Addresses review on keepsimple1#554.

- check_hostname now rejects any label over MAX_LABEL_BYTES on the wire, the same condition write_name enforces per label. This fixes the pre-existing silent failure where a hostname with an oversized label registered fine and was never sent. Labels are measured via EscapedLabels + escaped_label_wire_len (de-cfg(test)) so \. and \\ resolve like parse_escaped_name.
- register() now runs check_instance_label_length up front and returns the error directly, instead of only reporting it on the monitor channel from inside the daemon. The daemon-side check is kept for internal call paths.
- Corrected the name_change/hostname_change comments: trailing whitespace and '-' are trimmed unconditionally, not only to fix a double-space/double-dash artifact.
- Tests: check_hostname label cases (the previous 255-byte single-label "valid" case could never be written on the wire; replaced with an equal-length multi-label name), and register() rejecting oversized instance/hostname labels.

AI assistance: Cognition Devin CLI agent (SWE-2 High).
@Sealdot

Sealdot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the preliminary review — all three points addressed in eadab8e.

  1. check_hostname now rejects any label over MAX_LABEL_BYTES on the wire — the same per-label condition write_name enforces — so an oversized hostname label fails at API time instead of registering fine and never being sent. Labels are measured escape-aware (EscapedLabels + escaped_label_wire_len), so \. and \\ resolve like parse_escaped_name. Note the previous "valid" test case (a 248-byte single label totalling 255 bytes) could never actually be written on the wire; it's now an equal-length multi-label hostname.

  2. register() now runs check_instance_label_length up front and returns the error directly. The daemon-side check in register_service is kept for internal call paths.

  3. Comments corrected — trailing whitespace and - are trimmed unconditionally, not only to fix the double-space/double-dash artifact.

(AI assistance: Cognition Devin CLI agent.)

@Sealdot
Sealdot marked this pull request as ready for review November 22, 2026 03:25
When a probe conflict triggers name_change/hostname_change, the appended ` (n)`/`-n` suffix can push the first label past MAX_LABEL_BYTES, after which write_name rejects every packet and the service silently vanishes.

Shorten the base so the label with the new suffix still fits, like mDNSResponder: measure wire bytes via escape-aware helpers in dns_parser.rs (\. and \\ count as one byte, never cut inside a pair or a UTF-8 char), trim a trailing space/`-`, and recompute on every rename as ` (9)` -> ` (10)` grows the suffix. register_service now rejects an instance label over 63 wire bytes up front instead of failing silently on the network.

Fixes keepsimple1#549.

Rebased onto main @ e5ee843 (keepsimple1#553, keepsimple1#555): test imports union upstream's new_multicast_socket with this change's check_instance_label_length.

AI assistance: Cognition Devin CLI agent (SWE-2 High).
…ister()

Addresses review on keepsimple1#554.

- check_hostname now rejects any label over MAX_LABEL_BYTES on the wire, the same condition write_name enforces per label. This fixes the pre-existing silent failure where a hostname with an oversized label registered fine and was never sent. Labels are measured via EscapedLabels + escaped_label_wire_len (de-cfg(test)) so \. and \\ resolve like parse_escaped_name.
- register() now runs check_instance_label_length up front and returns the error directly, instead of only reporting it on the monitor channel from inside the daemon. The daemon-side check is kept for internal call paths.
- Corrected the name_change/hostname_change comments: trailing whitespace and '-' are trimmed unconditionally, not only to fix a double-space/double-dash artifact.
- Tests: check_hostname label cases (the previous 255-byte single-label "valid" case could never be written on the wire; replaced with an equal-length multi-label name), and register() rejecting oversized instance/hostname labels.

Rebased onto main @ e5ee843.

AI assistance: Cognition Devin CLI agent (SWE-2 High).
@Sealdot
Sealdot force-pushed the fix-rename-label-too-long branch from eadab8e to cb4ab7c Compare November 22, 2026 03:33
Fix rustdoc `private_intra_doc_links` failure: drop the intra-doc link brackets around `MAX_LABEL_BYTES` in the public `register`/`resolve_hostname` error docs (the constant is pub(crate), so the links do not resolve under `-D warnings`).

AI assistance: Cognition Devin CLI agent (SWE-2 High).

@keepsimple1 keepsimple1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good! Please see inline comments, and consider refresh PR description once the diff updates. Thanks!

Comment thread src/service_daemon.rs Outdated
/// - `hostname` is exactly `.local.` (the label before `.local.` is empty);
/// - `hostname` is longer than 255 bytes.
/// - `hostname` is longer than 255 bytes;
/// - a label of `hostname` is longer than `MAX_LABEL_BYTES` bytes on the wire.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: MAX_LABEL_BYTES is not public so the user cannot look it up. Writing 63 bytes would be clearer.

Comment thread src/service_daemon.rs Resolved
Comment thread src/service_daemon.rs Resolved
Comment thread src/dns_parser.rs Resolved
Comment thread src/service_daemon.rs Resolved
- Write the literal limit (63 bytes) in the public doc comments for register() and resolve_hostname() instead of the crate-private MAX_LABEL_BYTES, which users cannot look up.
- Drop the redundant instance-label check inside register_service; register() now rejects oversized labels before the command reaches the daemon.
- Delete first_label_wire_len, which duplicated escaped_label_wire_len; check_instance_label_length now uses split_first_label() + escaped_label_wire_len as suggested.

AI assistance: Cognition Devin CLI agent (SWE-2 High).
@Sealdot

Sealdot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review — all five comments addressed in 8a39320.

  1. The public docs for register() and resolve_hostname() now state the literal 63 bytes instead of the crate-private MAX_LABEL_BYTES.
  2. The redundant check inside register_service is removed; register() rejects oversized instance labels before the command reaches the daemon.
  3. first_label_wire_len is deleted — check_instance_label_length now uses split_first_label() + escaped_label_wire_len as suggested.
  4. PR description refreshed to cover the current diff (hostname label validation and the up-front register() check were also missing from it).

cargo test --lib 117/117, doc 5/5, clippy and fmt clean; the 6 CI checks on the new head are green.

@keepsimple1 keepsimple1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@keepsimple1
keepsimple1 merged commit 27f1214 into keepsimple1:main Nov 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

name_change/hostname_change can make a first label longer than 63 bytes

2 participants