Giter Site home page Giter Site logo

seancfoley / ipaddress-go Goto Github PK

View Code? Open in Web Editor NEW
77.0 2.0 9.0 2.77 MB

Go library for handling IP addresses and subnets, both IPv4 and IPv6

Home Page: https://seancfoley.github.io/IPAddress/

License: Apache License 2.0

Go 100.00%
golang go ipv6 ipv4 ip-address subnet subnetting cidr ipv4-address ipv6-network

ipaddress-go's People

Contributors

seancfoley 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

Watchers

 avatar  avatar

ipaddress-go's Issues

"main redeclared in this block"

I tried to build this library go build ./... and got error:

# github.com/seancfoley/ipaddress-go/ipaddr/cmd
ipaddr/cmd/test.go:25:6: main redeclared in this block
        ipaddr/cmd/main.go:37:6: other declaration of main

How to Convert CIDR to IP Range ?

Hi.

I want to convert CIDR to IP Range, but I can't find any method like GetAllAddress().

Input:

192.168.1.0/24

OutPut:

192.168.1.1
192.168.1.2
192.168.1.3
......
192.168.1.253
192.168.1.254

Thanks for your help.

ipaddr v1.5.2 bintree error

Hi there,

I got this error after upgrading to version v1.5.1
GoVersion: 1.19

/Users/josh/.go/pkg/mod/github.com/seancfoley/[email protected]/tree/trienode.go:542:25: internal compiler error: method GetBitCount on *uint8 not found

internal compile rerror

The error is: vendor\github.com\seancfoley\bintree\tree\trienode.go:542:25: internal compiler error: method GetBitCount on *uint8 not found
when i run golang project

How to list free subnet can use in ipv6

Hi,
Thanks for your great work.
I have read the issue #1 and #2
But none of these match my needed
So like I have an root network is

2001:db8::/32

Then
I assigned two addresses
2001:db8:0:4::/63
2001:db8:0:2::/63
Then I wanna do kind of this


func main() {

	listSubnets("2001:db8::/32", 63)
}

func listSubnets(original string, newPrefix int) {
	subnet := ipaddr.NewIPAddressString(original).GetAddress()
	iterator := subnet.SetPrefixLen(newPrefix).PrefixIterator()
	for iterator.HasNext() {
		fmt.Println(iterator.Next(), " ")
	}
}

But now the listSubnets will input an array of IPAddresses that Taken and then will ignore it from the list (with newPrefix) when list
Some kind like i Choose the newPrefix is 64
it will not show the 2001:db8:0:4::/64 and 2001:db8:0:2::/64
Example exptected output
image

staticcheck problem

I don't know if this is a problem with staticcheck or this package

I have code:

package main

import (
"fmt"
"github.com/seancfoley/ipaddress-go/ipaddr"
)

type DB struct {
Net *ipaddr.IPAddress
}

func main() {
fmt.Println("XXX")
}

If I run staticcheck it is OK

If I add test file xx_test:

cat main_test.go
package main

import (
"fmt"
"testing"
)

func TestOne(t *testing.T) {
fmt.Println("x")
}

and run a staticcheck, I get:

panic: nil underlying

goroutine 173 [running]:
go/types.(*Named).under(0xc0001378f0)
/opt/go1.20.2/src/go/types/named.go:494 +0x3d8
go/types.under({0xa0b158?, 0xc0001378f0?})
/opt/go1.20.2/src/go/types/type.go:23 +0x45
go/types.computeInterfaceTypeSet(0x0, 0xc0002c4d90?, 0xc000714ff0)
/opt/go1.20.2/src/go/types/typeset.go:273 +0x3f0
go/types.(*TypeParam).iface(0xc000524ae0)
/opt/go1.20.2/src/go/types/typeparam.go:140 +0x1b2
go/types.(*TypeParam).SetConstraint(...)
/opt/go1.20.2/src/go/types/typeparam.go:88
golang.org/x/tools/internal/gcimporter.(*reader).typeParamNames.func1()
/home/karlik/go/pkg/mod/golang.org/x/[email protected]/internal/gcimporter/ureader_yes.go:688 +0x45
golang.org/x/tools/internal/gcimporter.readUnifiedPackage(0xc00028e100, 0x0, 0xc0004a4150, {0x1, 0x0, {0xc0000e62d0, 0x1}, {0xc0004c087c, 0x5bcf7}, {0xc00025a000, ...}, ...})
/home/karlik/go/pkg/mod/golang.org/x/[email protected]/internal/gcimporter/ureader_yes.go:117 +0x483
golang.org/x/tools/internal/gcimporter.UImportData(0xa09720?, 0xc000434018?, {0xc00091e001?, 0x67f4bf?, 0x1010000000000?}, {0xc0000e62d0, 0x1})
/home/karlik/go/pkg/mod/golang.org/x/[email protected]/internal/gcimporter/ureader_yes.go:68 +0x10a
golang.org/x/tools/go/gcexportdata.Read({0xa09720?, 0xc000434018?}, 0x0?, 0x30?, {0xc0000e62d0, 0x1})
/home/karlik/go/pkg/mod/golang.org/x/[email protected]/go/gcexportdata/gcexportdata.go:134 +0x2b0
honnef.co/go/tools/go/loader.(*program).loadFromExport(0xc000411a60, 0xc0001d7680)
/home/karlik/go/pkg/mod/honnef.co/go/[email protected]/go/loader/loader.go:199 +0xed
honnef.co/go/tools/go/loader.Load(0xc00020dcc0)
/home/karlik/go/pkg/mod/honnef.co/go/[email protected]/go/loader/loader.go:168 +0x166
honnef.co/go/tools/lintcmd/runner.(*subrunner).doUncached(0x0?, 0xc000242b40)
/home/karlik/go/pkg/mod/honnef.co/go/[email protected]/lintcmd/runner/runner.go:694 +0x7f
honnef.co/go/tools/lintcmd/runner.(*subrunner).do(0xc00054e780, {0xa0dae0?, 0xc000242b40})
/home/karlik/go/pkg/mod/honnef.co/go/[email protected]/lintcmd/runner/runner.go:577 +0x94e
honnef.co/go/tools/lintcmd/runner.(*Runner).Run.func2({0xa0dae0?, 0xc000242b40?})
/home/karlik/go/pkg/mod/honnef.co/go/[email protected]/lintcmd/runner/runner.go:1268 +0x30
honnef.co/go/tools/lintcmd/runner.genericHandle({0xa0dae0?, 0xc000242b40}, {0xa0dae0?, 0xc000131200?}, 0x0?, 0xc0001ea190, 0xc0004116c0)
/home/karlik/go/pkg/mod/honnef.co/go/[email protected]/lintcmd/runner/runner.go:817 +0x130
created by honnef.co/go/tools/lintcmd/runner.(*Runner).Run
/home/karlik/go/pkg/mod/honnef.co/go/[email protected]/lintcmd/runner/runner.go:1267 +0x512

I use go 1.20 and staticcheck 2023.1.3 (v0.4.3)

How to find remaining IP space in a CIDR

Hi Sean

I just wanted to present another idea I was looking into which was to find out all the remaining IP space available, if IP allocation was randomly done. This code uses breadth first search and is able to provide the information required. If this is also possible with ipaddress-go then it would be a really good addition.

#!/usr/bin/env python3.4

import ipaddress
import argparse
import queue


def get_available_subnets(main, taken):
    # we assume no subnets are available intially
    available = []
    q = queue.Queue()
    # add first node for expansion in the BFS process
    q.put(main)

    while q.qsize() > 0:
        subnet = q.get()
        for taken_subnet in taken:
            if taken_subnet.compare_networks(subnet) == 0:
                # found matching subnet in taken, stop expanding
                print("similar: %s and %s" % (subnet, taken_subnet))
                break
            if taken_subnet.overlaps(subnet):
                # still has overlaps somewhere in children, keep expanding
                print("overlaps: %s and %s" % (subnet, taken_subnet))
                for sub_subnet in subnet.subnets():
                    q.put(sub_subnet)
                break
        else:
            # no overlaps with taken - this subnet is entirely available
            available.append(subnet)

    return available

if "__main__" == __name__:
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--m', help='main subnet to check', required=True)
    parser.add_argument('-t', '--taken', nargs='+', help='taken subnets', required=True)
    args = parser.parse_args()

    taken = [ipaddress.IPv4Network(subnet) for subnet in args.taken]
    main = ipaddress.IPv4Network(args.m)
    available = get_available_subnets(main, taken)

    print("Available IP space:")
    for avl in available:
        print(ipaddress.ip_network(avl))
Output:

python script.py -m 10.255.255.0/24 -t 10.255.255.192/26 10.255.255.128/30

overlaps: 10.255.255.0/24 and 10.255.255.192/26
overlaps: 10.255.255.128/25 and 10.255.255.192/26
overlaps: 10.255.255.128/26 and 10.255.255.128/30
similar: 10.255.255.192/26 and 10.255.255.192/26
overlaps: 10.255.255.128/27 and 10.255.255.128/30
overlaps: 10.255.255.128/28 and 10.255.255.128/30
overlaps: 10.255.255.128/29 and 10.255.255.128/30
similar: 10.255.255.128/30 and 10.255.255.128/30

Available IP space:
10.255.255.0/25
10.255.255.160/27
10.255.255.144/28
10.255.255.136/29
10.255.255.132/30

Any help on how to work out a similar code would be much appreciated.

Trie is no longer DeepEqual after v1.5.5

We upgraded from v1.5.4 to v1.5.5 and discovered that equal Tries are no longer deeply equal. Unexpected breaking change?

Steps

main.go

package main

import (
	"fmt"
	"reflect"

	"github.com/seancfoley/ipaddress-go/ipaddr"
)

func main() {
	i := ipaddr.NewIPv4AddressTrie()
	j := ipaddr.NewIPv4AddressTrie()
	i.Add(ipaddr.NewIPAddressString("192.168.1.0/8").GetAddress().ToIPv4())
	j.Add(ipaddr.NewIPAddressString("192.168.1.0/8").GetAddress().ToIPv4())
	fmt.Println(reflect.DeepEqual(i, j))
	fmt.Println(i.Equal(j))
}

working go.mod

module play.ground

go 1.21.4

require github.com/seancfoley/ipaddress-go v1.5.4

require github.com/seancfoley/bintree v1.2.1 // indirect

not working go.mod

module play.ground

go 1.21.4

require github.com/seancfoley/ipaddress-go v1.5.5

require github.com/seancfoley/bintree v1.2.3 // indirect

Expected output

true
true

Actual output

false
true

How to allocate next available free subnet

Hi

Thank you for this tool you have built as it's quite useful and helping me to learn go as well in the process.

I had an issue as given if we have a number of subnets /16's, /20's, /22's, /24's, how can I be able to find from a list of subnets the last subnet that has been assigned and pick up the next available subnet to use. Would you be able to please help guide me with a code example if possible as your other examples were very useful to follow along.

Thanks again.

Mike.

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.