func ExamplePrefix_subnettingAndAggregation() { _, n, err := net.ParseCIDR("192.0.2.0/24") if err != nil { log.Fatal(err) } p := ipaddr.NewPrefix(n) fmt.Println(p.IP, p.Last(), p.Len(), p.Mask, p.Hostmask()) fmt.Println() ps := p.Subnets(3) for _, p := range ps { fmt.Println(p) } fmt.Println() fmt.Println(ipaddr.Aggregate(ps)) fmt.Println(ipaddr.Aggregate(ps[1:7])) fmt.Println(ipaddr.Aggregate(ps[:4])) fmt.Println(ipaddr.Aggregate(ps[4:8])) // Output: // 192.0.2.0 192.0.2.255 24 ffffff00 000000ff // // 192.0.2.0/27 // 192.0.2.32/27 // 192.0.2.64/27 // 192.0.2.96/27 // 192.0.2.128/27 // 192.0.2.160/27 // 192.0.2.192/27 // 192.0.2.224/27 // // [192.0.2.0/24] // [192.0.2.32/27 192.0.2.64/26 192.0.2.64/27 192.0.2.192/27] // [192.0.2.0/25] // [192.0.2.128/25] }
func TestAggregate(t *testing.T) { for i, tt := range aggregateTests { in, orig, want := toPrefixes(tt.in), toPrefixes(tt.in), toPrefixes(tt.want) sort.Sort(byAscending(want)) out := ipaddr.Aggregate(in) if !reflect.DeepEqual(out, want) { t.Errorf("#%d: got %v; want %v", i, out, want) } if !reflect.DeepEqual(in, orig) { t.Errorf("#%d: %v is corrupted; want %v", i, in, orig) } } ipaddr.Aggregate(nil) }
func BenchmarkAggregateIPv6(b *testing.B) { in := toPrefixes([]string{"2001:db8::/64", "2001:db8:0:1::/64", "2001:db8:0:2::/64", "2001:db8:0:3::/64", "2001:db8:0:4::/64", "2001:db8::/64", "2001:db8::1/64"}) for i := 0; i < b.N; i++ { ipaddr.Aggregate(in) } }
func BenchmarkAggregateIPv4(b *testing.B) { in := toPrefixes([]string{"192.168.0.0/24", "192.168.1.0/24", "192.168.2.0/24", "192.168.3.0/24", "192.168.4.0/25", "192.168.101.0/26", "192.168.102.1/27"}) for i := 0; i < b.N; i++ { ipaddr.Aggregate(in) } }
func ExamplePrefix_subnettingAndAggregation() { _, n, err := net.ParseCIDR("192.168.0.0/24") if err != nil { log.Fatal(err) } p := ipaddr.NewPrefix(n) fmt.Println(p.IP, p.Last(), p.Len(), p.Mask, p.Hostmask()) ps := p.Subnets(2) for _, p := range ps { fmt.Println(p) } _, n, err = net.ParseCIDR("192.168.100.0/24") if err != nil { log.Fatal(err) } ps = append(ps, *ipaddr.NewPrefix(n)) fmt.Println(ipaddr.Aggregate(ps[2:])) // Output: // 192.168.0.0 192.168.0.255 24 ffffff00 000000ff // 192.168.0.0/26 // 192.168.0.64/26 // 192.168.0.128/26 // 192.168.0.192/26 // [192.168.0.128/25 192.168.100.0/24] }
func BenchmarkAggregateIPv6(b *testing.B) { for i := 0; i < b.N; i++ { ipaddr.Aggregate(aggregatablePrefixesIPv6) } }