-
Notifications
You must be signed in to change notification settings - Fork 2
Adds IPv6 support to Sparoid::Server
#17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
antondalgren
wants to merge
24
commits into
main
Choose a base branch
from
server-ipv6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
564e140
Adds ipv6 support to server. It introduces a new message format to al…
antondalgren 109a2a9
Update test with proper cidrrange
antondalgren a2434b0
Validate cidr range, and make nftablesv6 usable on it own
antondalgren 3ed4afb
Better boolean description
antondalgren 7dfa36d
return nil if we cant resolve the ip
antondalgren a766b00
Merge branch 'main' into server-ipv6
antondalgren 1893de2
Remove redundant test
antondalgren 31f17a5
Test server backwards compatibility with v1 and v2 messages
antondalgren ec8a24d
Client only sends v2 messages
antondalgren d7a400b
Lint: use short block syntax
antondalgren b627f6f
Refactor IP encoding to use StaticArray directly
carlhoerberg a9c3e3a
Remove DNS-based public IP lookup, strip IPs at source
carlhoerberg 0f82d2a
Store V2 IP as StaticArray(UInt8, 16), detect IPv4 by ::ffff prefix
carlhoerberg 23aed61
Store V2 IP and range in IPv6 notation internally
carlhoerberg 76244e6
Simplify client: use WaitGroup, clean up UDP send logic
carlhoerberg 7387984
Add V2.from_ip(String) overload, simplify client IP handling
carlhoerberg 34f1075
Strip range from V2 IPv4 ip_string, validate server-side
antondalgren 84c8534
Narrow down msg type
antondalgren f313189
Simplify IPv6 detection: use UDP connect instead of getifaddrs/netlink
antondalgren 20e22f4
Remove range from V2 message
antondalgren 4f684ad
If local target host is ipv6, send ipv6 address
antondalgren d5b1f9f
Fix formatting and ameba lint
antondalgren 87cb4f5
Replace V1/V2 with single Message struct, strip IPv4-mapped IPv6
antondalgren b15a486
Add comprehensive IPv6 global address check
antondalgren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| require "./spec_helper" | ||
|
|
||
| describe Sparoid::Message do | ||
| describe ".from_ip" do | ||
| it "creates message from IPv4 string" do | ||
| msg = Sparoid::Message.from_ip("192.168.1.100") | ||
| msg.family.should eq Socket::Family::INET | ||
| msg.ip_string.should eq "192.168.1.100" | ||
| msg.ip.size.should eq 4 | ||
| end | ||
|
|
||
| it "creates message from IPv6 string" do | ||
| msg = Sparoid::Message.from_ip("2001:0db8:85a3::8a2e:0370:7334") | ||
| msg.family.should eq Socket::Family::INET6 | ||
| msg.ip_string.should eq "2001:0db8:85a3:0000:0000:8a2e:0370:7334" | ||
| msg.ip.size.should eq 16 | ||
| end | ||
|
|
||
| it "strips IPv4-mapped IPv6 to plain IPv4" do | ||
| msg = Sparoid::Message.from_ip("::ffff:192.168.1.1") | ||
| msg.family.should eq Socket::Family::INET | ||
| msg.ip_string.should eq "192.168.1.1" | ||
| msg.ip.size.should eq 4 | ||
| end | ||
|
|
||
| it "raises on invalid string" do | ||
| expect_raises(Exception, "Invalid IP address: not-an-ip") do | ||
| Sparoid::Message.from_ip("not-an-ip") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#ip_string" do | ||
| it "formats localhost" do | ||
| msg = Sparoid::Message.from_ip("127.0.0.1") | ||
| msg.ip_string.should eq "127.0.0.1" | ||
| end | ||
|
|
||
| it "formats 0.0.0.0" do | ||
| msg = Sparoid::Message.from_ip("0.0.0.0") | ||
| msg.ip_string.should eq "0.0.0.0" | ||
| end | ||
|
|
||
| it "formats 255.255.255.255" do | ||
| msg = Sparoid::Message.from_ip("255.255.255.255") | ||
| msg.ip_string.should eq "255.255.255.255" | ||
| end | ||
|
|
||
| it "formats ::1 (loopback)" do | ||
| msg = Sparoid::Message.from_ip("::1") | ||
| msg.ip_string.should eq "0000:0000:0000:0000:0000:0000:0000:0001" | ||
| end | ||
|
|
||
| it "formats :: (all zeros)" do | ||
| msg = Sparoid::Message.from_ip("::") | ||
| msg.ip_string.should eq "0000:0000:0000:0000:0000:0000:0000:0000" | ||
| end | ||
|
|
||
| it "formats 2001:db8::" do | ||
| msg = Sparoid::Message.from_ip("2001:db8::") | ||
| msg.ip_string.should eq "2001:0db8:0000:0000:0000:0000:0000:0000" | ||
| end | ||
|
|
||
| it "formats fe80::1 (link-local)" do | ||
| msg = Sparoid::Message.from_ip("fe80::1") | ||
| msg.ip_string.should eq "fe80:0000:0000:0000:0000:0000:0000:0001" | ||
| end | ||
|
|
||
| it "formats ff02::1 (multicast)" do | ||
| msg = Sparoid::Message.from_ip("ff02::1") | ||
| msg.ip_string.should eq "ff02:0000:0000:0000:0000:0000:0000:0001" | ||
| end | ||
| end | ||
|
|
||
| describe "serialization round-trip" do | ||
| it "serializes and deserializes IPv4" do | ||
| original = Sparoid::Message.from_ip("10.20.30.40") | ||
| slice = original.to_slice(IO::ByteFormat::NetworkEndian) | ||
| slice.size.should eq 32 | ||
|
|
||
| io = IO::Memory.new(slice) | ||
| parsed = Sparoid::Message.from_io(io, IO::ByteFormat::NetworkEndian) | ||
| parsed.family.should eq Socket::Family::INET | ||
| parsed.ip_string.should eq "10.20.30.40" | ||
| parsed.ts.should eq original.ts | ||
| parsed.nounce.should eq original.nounce | ||
| end | ||
|
|
||
| it "serializes and deserializes IPv6" do | ||
| original = Sparoid::Message.from_ip("2001:db8::1") | ||
| slice = original.to_slice(IO::ByteFormat::NetworkEndian) | ||
| slice.size.should eq 44 | ||
|
|
||
| io = IO::Memory.new(slice) | ||
| parsed = Sparoid::Message.from_io(io, IO::ByteFormat::NetworkEndian) | ||
| parsed.family.should eq Socket::Family::INET6 | ||
| parsed.ip_string.should eq "2001:0db8:0000:0000:0000:0000:0000:0001" | ||
| parsed.ts.should eq original.ts | ||
| parsed.nounce.should eq original.nounce | ||
| end | ||
|
|
||
| it "round-trips IPv4-mapped IPv6 as plain IPv4" do | ||
| original = Sparoid::Message.from_ip("::ffff:10.20.30.40") | ||
| slice = original.to_slice(IO::ByteFormat::NetworkEndian) | ||
| slice.size.should eq 32 | ||
|
|
||
| io = IO::Memory.new(slice) | ||
| parsed = Sparoid::Message.from_io(io, IO::ByteFormat::NetworkEndian) | ||
| parsed.family.should eq Socket::Family::INET | ||
| parsed.ip_string.should eq "10.20.30.40" | ||
| parsed.ts.should eq original.ts | ||
| parsed.nounce.should eq original.nounce | ||
| end | ||
|
|
||
| it "preserves timestamp and nonce" do | ||
| original = Sparoid::Message.from_ip("1.2.3.4") | ||
| slice = original.to_slice(IO::ByteFormat::NetworkEndian) | ||
| io = IO::Memory.new(slice) | ||
| parsed = Sparoid::Message.from_io(io, IO::ByteFormat::NetworkEndian) | ||
| parsed.ts.should eq original.ts | ||
| parsed.nounce.should eq original.nounce | ||
| parsed.ip.should eq original.ip | ||
| end | ||
| end | ||
|
|
||
| describe ".from_io" do | ||
| it "raises on unsupported version" do | ||
| slice = Bytes.new(32) | ||
| IO::ByteFormat::NetworkEndian.encode(99_i32, slice[0, 4]) | ||
| io = IO::Memory.new(slice) | ||
| expect_raises(Exception, "Unsupported message version: 99") do | ||
| Sparoid::Message.from_io(io, IO::ByteFormat::NetworkEndian) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "timestamp and nonce" do | ||
| it "generates unique nonces" do | ||
| msg1 = Sparoid::Message.from_ip("1.2.3.4") | ||
| msg2 = Sparoid::Message.from_ip("1.2.3.4") | ||
| msg1.nounce.should_not eq msg2.nounce | ||
| end | ||
|
|
||
| it "generates timestamps close to current time" do | ||
| before = Time.utc.to_unix_ms | ||
| msg = Sparoid::Message.from_ip("1.2.3.4") | ||
| after = Time.utc.to_unix_ms | ||
| msg.ts.should be >= before | ||
| msg.ts.should be <= after | ||
| end | ||
| end | ||
| end |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could do something like this if we would like to reduce config options, but I guess its clearer seperating them anyway