diff --git a/net/ip.go b/net/ip.go index 90628ece21c0edd8dd0df4dbb7a1c86ecdf4d7c0..8a532a2c09f36033fbb27d0e8aa69a9e10942492 100644 --- a/net/ip.go +++ b/net/ip.go @@ -182,5 +182,13 @@ func (ip IP) bitAtPositionIPv4(pos uint8) bool { } func (ip IP) bitAtPositionIPv6(pos uint8) bool { - panic("No IPv6 support yet!") + if pos > 128 { + return false + } + + if pos <= 64 { + return (ip.higher & (1 << (64 - pos))) != 0 + } + + return (ip.lower & (1 << (128 - pos))) != 0 } diff --git a/net/ip_test.go b/net/ip_test.go index f1edd4688cb83e8277b723e45e32cacfc0b8e33e..03f78ec9e8154b5292e5eaf824f74af020f7355f 100644 --- a/net/ip_test.go +++ b/net/ip_test.go @@ -313,17 +313,41 @@ func TestBitAtPosition(t *testing.T) { expected bool }{ { - name: "Bit 8 from 1.0.0.0 -> 0", + name: "IPv4: Bit 8 from 1.0.0.0 -> 0", input: IPv4FromOctets(10, 0, 0, 0), position: 8, expected: false, }, { - name: "Bit 8 from 11.0.0.0 -> 1", + name: "IPv4: Bit 8 from 11.0.0.0 -> 1", input: IPv4FromOctets(11, 0, 0, 0), position: 8, expected: true, }, + { + name: "IPv6: Bit 16 from 2001:678:1e0:: -> 1", + input: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0), + position: 16, + expected: true, + }, + { + name: "IPv6: Bit 17 from 2001:678:1e0:: -> 0", + input: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0), + position: 17, + expected: false, + }, + { + name: "IPv6: Bit 113 from 2001:678:1e0::cafe -> 1", + input: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0xcafe), + position: 113, + expected: true, + }, + { + name: "IPv6: Bit 115 from 2001:678:1e0::cafe -> 0", + input: IPv6FromBlocks(0x2001, 0x678, 0x1e0, 0, 0, 0, 0, 0xcafe), + position: 115, + expected: false, + }, } for _, test := range tests {