本文整理匯總了Golang中github.com/mikioh/ipaddr.NewPrefix函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewPrefix函數的具體用法?Golang NewPrefix怎麽用?Golang NewPrefix使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewPrefix函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestOverlaps
func TestOverlaps(t *testing.T) {
for i, tt := range overlapsTests {
p1, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
if err != nil {
t.Fatalf("ipaddr.NewPrefix failed: %v", err)
}
var nn []*net.IPNet
for _, s := range tt.others {
_, n, err := net.ParseCIDR(s)
if err != nil {
t.Fatalf("net.ParseCIDR failed: %v", err)
}
nn = append(nn, n)
}
var others []ipaddr.Prefix
for _, n := range nn {
prefixLen, _ := n.Mask.Size()
p, err := ipaddr.NewPrefix(n.IP, prefixLen)
if err != nil {
t.Fatalf("ipaddr.NewPrefix failed: %v", err)
}
others = append(others, p)
}
p2 := ipaddr.SummaryPrefix(others)
if ok := p1.Overlaps(p2); ok != tt.ok {
t.Fatalf("#%v: got %v; expected %v", i, ok, tt.ok)
}
if ok := p2.Overlaps(p1); ok != tt.ok {
t.Fatalf("#%v: got %v; expected %v", i, ok, tt.ok)
}
}
}
示例2: BenchmarkIPv4Exclude
func BenchmarkIPv4Exclude(b *testing.B) {
p, _ := ipaddr.NewPrefix(net.ParseIP("10.1.0.0"), 16)
p1, _ := ipaddr.NewPrefix(net.ParseIP("10.1.1.1"), 32)
for i := 0; i < b.N; i++ {
p.Exclude(p1)
}
}
示例3: BenchmarkIPv4ComparePrefix
func BenchmarkIPv4ComparePrefix(b *testing.B) {
p1, _ := ipaddr.NewPrefix(net.ParseIP("192.168.1.0"), 24)
p2, _ := ipaddr.NewPrefix(net.ParseIP("192.168.2.0"), 24)
for i := 0; i < b.N; i++ {
ipaddr.ComparePrefix(p1, p2)
}
}
示例4: BenchmarkIPv6Exclude
func BenchmarkIPv6Exclude(b *testing.B) {
p, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8::"), 64)
p1, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8::1:1:1:1"), 128)
for i := 0; i < b.N; i++ {
p.Exclude(p1)
}
}
示例5: BenchmarkIPv4Equal
func BenchmarkIPv4Equal(b *testing.B) {
p1, _ := ipaddr.NewPrefix(net.ParseIP("192.168.1.0"), 24)
p2, _ := ipaddr.NewPrefix(net.ParseIP("192.168.2.0"), 24)
for i := 0; i < b.N; i++ {
p1.Equal(p2)
}
}
示例6: BenchmarkIPv6ComparePrefix
func BenchmarkIPv6ComparePrefix(b *testing.B) {
p1, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f002::"), 64)
p2, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f003::"), 64)
for i := 0; i < b.N; i++ {
ipaddr.ComparePrefix(p1, p2)
}
}
示例7: BenchmarkIPv6Equal
func BenchmarkIPv6Equal(b *testing.B) {
p1, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f002::"), 64)
p2, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f003::"), 64)
for i := 0; i < b.N; i++ {
p1.Equal(p2)
}
}
示例8: TestExclude
func TestExclude(t *testing.T) {
for i, tt := range excludeTests {
p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
if err != nil {
t.Fatalf("ipaddr.NewPrefix failed: %v", err)
}
excl, err := ipaddr.NewPrefix(tt.exclAddr, tt.exclPrefixLen)
if err != nil {
t.Fatalf("ipaddr.NewPrefix failed: %v", err)
}
subs := p.Exclude(excl)
if len(subs) != tt.exclPrefixLen-tt.prefixLen {
for _, s := range subs {
t.Logf("subnet: %v", s)
}
t.Fatalf("#%v: got %v; expected %v", i, len(subs), tt.exclPrefixLen-tt.prefixLen)
}
diff, sum := big.NewInt(0), big.NewInt(0)
diff.Sub(p.NumAddr(), excl.NumAddr())
for _, p := range subs {
sum.Add(sum, p.NumAddr())
}
if diff.String() != sum.String() {
for _, s := range subs {
t.Logf("subnet: %v", s)
}
t.Fatalf("#%v: got %v; expected %v", i, sum.String(), diff.String())
}
}
}
示例9: Overlaps
// Overlaps returns a bool stating whether or not the entry
// overlaps with another CIDR block.
func (e Entry) Overlaps(cidr *net.IPNet) bool {
xMaskSize, _ := e.CIDR.Mask.Size()
x, _ := ipaddr.NewPrefix(e.CIDR.IP, xMaskSize)
yMaskSize, _ := cidr.Mask.Size()
y, _ := ipaddr.NewPrefix(cidr.IP, yMaskSize)
return x.Overlaps(y)
}
示例10: makePrefix
func makePrefix(network string) (*ipaddr.Prefix, error) {
_, ipn, err := net.ParseCIDR(network)
if err != nil {
return nil, err
}
return ipaddr.NewPrefix(ipn), nil
}
示例11: TestSummaryPrefix
func TestSummaryPrefix(t *testing.T) {
for i, tt := range summaryPrefixTests {
var nn []*net.IPNet
for _, s := range tt.subs {
_, n, err := net.ParseCIDR(s)
if err != nil {
t.Fatalf("net.ParseCIDR failed: %v", err)
}
nn = append(nn, n)
}
var subs []ipaddr.Prefix
for _, n := range nn {
prefixLen, _ := n.Mask.Size()
p, err := ipaddr.NewPrefix(n.IP, prefixLen)
if err != nil {
t.Fatalf("ipaddr.NewPrefix failed: %v", err)
}
subs = append(subs, p)
}
sum := ipaddr.SummaryPrefix(subs)
if sum == nil && tt.ok || sum != nil && !tt.ok {
t.Fatalf("#%v: got %v, %v; expected %v", i, sum, sum != nil, tt.ok)
}
if tt.ok {
for _, s := range subs {
if !sum.Contains(s.Addr()) {
t.Fatalf("#%v: %v does not contain %v", i, sum, s)
}
}
}
}
}
示例12: ExamplePrefix_subnettingAndAggregation
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]
}
示例13: ExamplePrefix_subnettingAndSupernetting
func ExamplePrefix_subnettingAndSupernetting() {
_, n, err := net.ParseCIDR("203.0.113.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.Supernet(ps))
fmt.Println(ipaddr.Supernet(ps[1:7]))
fmt.Println(ipaddr.Supernet(ps[:4]))
fmt.Println(ipaddr.Supernet(ps[4:8]))
// Output:
// 203.0.113.0 203.0.113.255 24 ffffff00 000000ff
//
// 203.0.113.0/27
// 203.0.113.32/27
// 203.0.113.64/27
// 203.0.113.96/27
// 203.0.113.128/27
// 203.0.113.160/27
// 203.0.113.192/27
// 203.0.113.224/27
//
// 203.0.113.0/24
// 203.0.113.0/24
// 203.0.113.0/25
// 203.0.113.128/25
}
示例14: BenchmarkIPv4Contains
func BenchmarkIPv4Contains(b *testing.B) {
p, _ := ipaddr.NewPrefix(net.ParseIP("192.168.255.0"), 24)
ip := net.ParseIP("192.168.255.255")
for i := 0; i < b.N; i++ {
p.Contains(ip)
}
}
示例15: BenchmarkIPv6Contains
func BenchmarkIPv6Contains(b *testing.B) {
p, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f002::"), 64)
ip := net.ParseIP("2001:db8:f001:f002::1")
for i := 0; i < b.N; i++ {
p.Contains(ip)
}
}