seancfoley / ipaddress-go Goto Github PK
View Code? Open in Web Editor NEWGo library for handling IP addresses and subnets, both IPv4 and IPv6
Home Page: https://seancfoley.github.io/IPAddress/
License: Apache License 2.0
Go library for handling IP addresses and subnets, both IPv4 and IPv6
Home Page: https://seancfoley.github.io/IPAddress/
License: Apache License 2.0
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
Hello, I used the MergeToPrefixBlocks method from this library to merge multiple subnets. However, during actual use, I found the following issue with the code:
var a []*ipaddr.IPAddress a = append(a, ipaddr.NewIPAddressString("0.0.0.0/0").GetAddress(), ipaddr.NewIPAddressString("0::0/0").GetAddress(), ipaddr.NewIPAddressString("::/0").GetAddress()) fmt.Println(a[0].MergeToPrefixBlocks(a[1:]...))
The returned result is [0.0.0.0/0]. Shouldn't it return [0.0.0.0/0, 0::0/0] or [0.0.0.0/0, ::/0], which would separately represent the entirety of IPv4 and IPv6? Is there any configuration that would allow me to obtain this result?
Thank you very much to the author for developing this library.
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
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.
I don't know if this is a problem with staticcheck or this package
I have code:
package mainimport (
"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 mainimport (
"fmt"
"testing"
)func TestOne(t *testing.T) {
fmt.Println("x")
}
and run a staticcheck, I get:
panic: nil underlyinggoroutine 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)
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.
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
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.
We upgraded from v1.5.4 to v1.5.5 and discovered that equal Tries are no longer deeply equal. Unexpected breaking change?
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
true
true
false
true
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
When printing the trie it comes with a pre-defined format and structure.
● 0.0.0.0/0 (4)
└─● 8.9.8.0/24 (3)
└─● 8.9.8.8/29 (2)
└─● 8.9.8.10 (1)
We have some use cases where we would like to output the trie in both CSV, and YAML for various reporting and integrations in to other systems. Obviously we could iterate over the entire trie, although a preferred approach would be to support providing a custom func for formatting.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.