Net: honor the scope argument - #5105
Merged
Merged
Conversation
Net.__init__ accepted a scope argument but never read it, so the
documented Net("224.0.0.1", scope=conf.iface) form returned a Net whose
scope was None. Because __iter__ copies self.scope onto the addresses it
yields, such a Net produced unscoped addresses, and packets built from it
left through the default interface rather than the requested one. Net6
inherits __init__ and behaved the same way.
Pass the argument to ScopedIP, which resolves the interface and keeps the
precedence where an inline % overrides it.
The existing tests could not catch this because Net.__eq__ does not look
at scope, so the new test asserts on .scope itself.
AI-Assisted: yes (Claude Opus 5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Net.__init__takes ascopeargument and never reads it. The form printed inNet's own docstring returns an unscoped object:__iter__copiesself.scopeonto every address it yields, so aNetbuilt withscope=yields plain addresses and packets built from it leave through the default interface rather than the requested one.Net6inherits__init__, so IPv6 behaves the same way.The argument arrived with scope support in #4461. That commit wired
scopeinto the signature, into__iter__, into__repr__and into__hash__, but not into the body of__init__. This patch hands it toScopedIP, which does the interface resolution and keeps the existing precedence where an inline%overrides the argument.No test could have caught this.
Net.__eq__does not look atscope, soNet("224.0.0.1", scope=iface) == Net("224.0.0.1%iface")holds whether or not the argument works. The new test asserts on.scopeitself for that reason, across the mask form, the start/stop form and the addresses aNetgenerates.One related thing I did not touch:
__hash__includesscopebut__eq__does not, soNet("224.0.0.1%eth0")andNet("224.0.0.1")compare equal with different hashes, which makes them distinct keys in a dict. Fixing that shifts comparison semantics, so it seemed worth a separate discussion.