Code Monkey home page Code Monkey logo

ipaddr's People

Contributors

cixtor avatar mikioh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ipaddr's Issues

Iteration over a prefix with a single IP does not work

If you call ipaddr.Parse with input that is only a single IP (127.0.0.1, 127.0.0.1/32) the supplied cursor will not return any entries via Next(). Getting the value via First() works fine.

I believe Next() should return the first, and only entry, and then return nil.

I am parsing a mix of single hosts and networks both in CIDR notation and noticed that anything with a /32 prefix was being dropped because of Next() returning nil immediately.

Here is an example program to show the issue.

https://play.golang.org/p/xZEOyiLCXYN

`package main

import (
"fmt"
"github.com/mikioh/ipaddr"
)

func printIPs(ip string) {
cursor, err := ipaddr.Parse(ip)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(cursor.Pos(), cursor.First(), cursor.Last(), cursor.List())
fmt.Println("First is: ", cursor.First().IP.String())
fmt.Println("All Members:")
for pos := cursor.Next(); pos != nil; pos = cursor.Next() {
fmt.Println(pos.IP.String())
}
}
}

func main() {
printIPs("192.168.168.77/32")
printIPs("127.0.0.1/32")
printIPs("127.0.0.0/29")
}
`

Aggregation of large volumes of subnets causes null pointer dereference

There is an issue when aggregating a large volume of subnets which causes a null pointer panic

This though does not occur when using Python's standard library to collapse addresses.

Example output:

Converting subnets to Prefixes
Aggregating subnets
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10b2f1a]

goroutine 1 [running]:
github.com/mikioh/ipaddr.aggregateByBF(0xc000694000, 0x19233, 0x19233, 0x1283ef8, 0x1283f08, 0x4, 0x19233, 0x19233)
        /Users/kkirsche/git/go/pkg/mod/github.com/mikioh/[email protected]/prefix.go:329 +0x69a
github.com/mikioh/ipaddr.Aggregate(0xc000694000, 0x19233, 0x19233, 0x1, 0x1, 0x14)
        /Users/kkirsche/git/go/pkg/mod/github.com/mikioh/[email protected]/prefix.go:302 +0x103
main.main()
        /Users/kkirsche/git/go/src/github.com/kkirsche/ipaddr-aggregation-error/main.go:27 +0x18a
exit status 2

I've created a repository with a python and a go version showing that one works and one does not.

https://github.com/kkirsche/ipaddr-aggregation-error

Aggregation incorrect with different seq of prefix

Aggregation incorrect with different seq of prefix

results of {"8.8.8.0/25","8.8.8.128/25", "9.9.9.0/25"} is [8.8.8.0/24 9.9.9.0/25], which is correct
but if you change the seq of your input to {"8.8.8.0/25", "9.9.9.0/25","8.8.8.128/25"}, and result is [8.0.0.0/7] , which is wrong

Aggregate does not aggregate correctly

Given the input:

127.156.0.0/15
127.159.0.0/16

Aggregate produces:

127.156.0.0/14

But that includes the range 127.158.0.0/16 which is not in the input.

I also think there is an infinite loop somewhere with some inputs but I haven't managed to narrow down a test case yet.

Infinity loop at branchingFactorIPv4 function

It seems like on some sets of prefixes (128.0.0.0/1 and 0.0.0.0/1 in my case), it causes infinity loop:

func branchingFactorIPv4(ps []Prefix) (int, bool) {
	var lastBF, lastN int
	base := ipToIPv4Int(ps[0].IP.Mask(ps[0].Mask))
	mask := ipMaskToIPv4Int(ps[0].Mask)
	l := ps[0].Len()
	for bf := 1; bf < IPv4PrefixLen; bf++ {
		n, nfull := 0, 1<<uint(bf)
		max := ipv4Int(1 << uint(bf))
		aggrMask := mask << uint(bf)
		for pat := ipv4Int(0); pat < max; pat++ {
			aggr := base&aggrMask | pat<<uint(IPv4PrefixLen-l)
			for _, p := range ps {
				i := ipToIPv4Int(p.IP)
				if aggr == i&mask {
					n++
				}
			}
		}
		if n < nfull {
			break
		}
		lastBF = bf
		lastN = n
	}
	n := 1 << uint(lastBF)
	return n, lastN >= n
}

After some calculation it causes panic:

runtime error: slice bounds out of range [:2147483648] with capacity 24

Aggregate function cause error

Aggregate function with specific prefix list cause the error. Interesting thing is that commenting any of subnets makes program work correct.

https://go.dev/play/p/a83OovyO0mV

package main

import (
	"net"

	"github.com/mikioh/ipaddr"
)

func main() {
	subnets := []string{
		"109.233.200.0/22",
		"109.233.204.0/22",
		"109.233.208.0/21",
		"109.233.224.0/21",
		"109.234.36.0/22",
		"109.234.128.0/21",
		"109.234.152.0/21",
		"109.235.24.0/21",
		"109.235.88.0/21",
		"109.235.160.0/21",
		"109.235.184.0/21",
		"146.185.208.0/22",
		"178.213.75.0/24",
	}

	var prefixes []ipaddr.Prefix

	for _, v := range subnets {
		_, cidr, _ := net.ParseCIDR(v)
		p := ipaddr.NewPrefix(cidr)
		prefixes = append(prefixes, *p)
	}
	ipaddr.Aggregate(prefixes)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.