Skip to content
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

Convert IPv4 mapped IPv6 subnet masks to 4 bytes from 16 bytes (using the last 4 bytes) #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions net/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ type Network struct {

// NewNetwork returns Network built using given net.IPNet.
func NewNetwork(ipNet net.IPNet) Network {
ipNet = getNetwork(ipNet)
return Network{
IPNet: ipNet,
Number: NewNetworkNumber(ipNet.IP),
Expand Down Expand Up @@ -270,3 +271,12 @@ func NextIP(ip net.IP) net.IP {
func PreviousIP(ip net.IP) net.IP {
return NewNetworkNumber(ip).Previous().ToIP()
}

// getNetwork converts IPv4 mapped IPv6 subnet masks to 4 bytes.
// E.g. ::ffff:d1ad:35a7/128 get converted to 209.173.54.167/32. /128 becomes /32, /120 becomes /24 and so on.
func getNetwork(network net.IPNet) net.IPNet {
if network.IP.To4() != nil && len(network.Mask) == net.IPv6len {
network.Mask = network.Mask[12:16]
}
return network
}
13 changes: 13 additions & 0 deletions net/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestNewNetworkNumber(t *testing.T) {
{nil, nil, "nil input"},
{net.IP([]byte{1, 1, 1, 1, 1}), nil, "bad input"},
{net.ParseIP("128.0.0.0"), NetworkNumber([]uint32{2147483648}), "IPv4"},
{net.ParseIP("::ffff:8000:0"), NetworkNumber([]uint32{2147483648}), "IPv4"},
{
net.ParseIP("2001:0db8::ff00:0042:8329"),
NetworkNumber([]uint32{536939960, 0, 65280, 4358953}),
Expand Down Expand Up @@ -126,6 +127,7 @@ func TestNetworkNumberNext(t *testing.T) {
{"0.0.0.0", "0.0.0.1", "IPv4 basic"},
{"0.0.0.255", "0.0.1.0", "IPv4 rollover"},
{"0.255.255.255", "1.0.0.0", "IPv4 consecutive rollover"},
{"::ffff:ff:ffff", "1.0.0.0", "IPv4 consecutive rollover"},
{"8000::0", "8000::1", "IPv6 basic"},
{"0::ffff", "0::1:0", "IPv6 rollover"},
{"0:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "1::", "IPv6 consecutive rollover"},
Expand All @@ -149,6 +151,7 @@ func TestNeworkNumberPrevious(t *testing.T) {
{"0.0.0.1", "0.0.0.0", "IPv4 basic"},
{"0.0.1.0", "0.0.0.255", "IPv4 rollover"},
{"1.0.0.0", "0.255.255.255", "IPv4 consecutive rollover"},
{"1.0.0.0", "::ffff:ff:ffff", "IPv4 consecutive rollover"},
{"8000::1", "8000::0", "IPv6 basic"},
{"0::1:0", "0::ffff", "IPv6 rollover"},
{"1::0", "0:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "IPv6 consecutive rollover"},
Expand Down Expand Up @@ -225,6 +228,15 @@ func TestNewNetwork(t *testing.T) {
assert.Equal(t, NetworkNumberMask{math.MaxUint32 - uint32(math.MaxUint8)}, n.Mask)
}

func TestNewNetwork2(t *testing.T) {
_, ipNet, _ := net.ParseCIDR("::ffff:c080:0/120")
n := NewNetwork(*ipNet)

assert.Equal(t, *&ipNet.IP, n.IPNet.IP)
assert.Equal(t, NetworkNumber{3229614080}, n.Number)
assert.Equal(t, NetworkNumberMask{math.MaxUint32 - uint32(math.MaxUint8)}, n.Mask)
}

func TestNetworkMasked(t *testing.T) {
cases := []struct {
network string
Expand Down Expand Up @@ -257,6 +269,7 @@ func TestNetworkEqual(t *testing.T) {
name string
}{
{"192.128.0.0/24", "192.128.0.0/24", true, "IPv4 equals"},
{"192.128.0.0/24", "::ffff:c080:0/120", true, "IPv4 equals"},
{"192.128.0.0/24", "192.128.0.0/23", false, "IPv4 not equals"},
{"8000::/24", "8000::/24", true, "IPv6 equals"},
{"8000::/24", "8000::/23", false, "IPv6 not equals"},
Expand Down