https://github.com/3th1nk/cidr
golang to calculate CIDR network
https://github.com/3th1nk/cidr
cidr ipv4 ipv6
Last synced: 9 months ago
JSON representation
golang to calculate CIDR network
- Host: GitHub
- URL: https://github.com/3th1nk/cidr
- Owner: 3th1nk
- License: mit
- Created: 2019-07-18T08:42:13.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-25T06:31:04.000Z (10 months ago)
- Last Synced: 2025-04-02T14:00:42.670Z (10 months ago)
- Topics: cidr, ipv4, ipv6
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 94
- Watchers: 1
- Forks: 19
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CIDR
## Features
* easy to iterate through each ip in segment
* check ipv4 or ipv6 segment
* check whether segment contain ip
* segments sort、split、merge
* ip incr & decr
* ip compare
## Code Example
```
package main
import (
"fmt"
"github.com/3th1nk/cidr"
)
func main() {
// parses a network segment as a CIDR
c, _ := cidr.Parse("192.168.1.0/28")
fmt.Println("network:", c.Network(), "broadcast:", c.Broadcast(), "mask", c.Mask())
// ip range
beginIP, endIP := c.IPRange()
fmt.Println("ip range:", beginIP, endIP)
// iterate through each ip
fmt.Println("ip total:", c.IPCount())
c.Each(func(ip string) bool {
fmt.Println("\t", ip)
return true
})
c.EachFrom("192.168.1.10", func(ip string) bool {
fmt.Println("\t", ip)
return true
})
fmt.Println("subnet plan based on the subnets num:")
cs, _ := c.SubNetting(cidr.MethodSubnetNum, 4)
for _, c := range cs {
fmt.Println("\t", c.CIDR())
}
fmt.Println("subnet plan based on the hosts num:")
cs, _ = c.SubNetting(cidr.MethodHostNum, 4)
for _, c := range cs {
fmt.Println("\t", c.CIDR())
}
fmt.Println("merge network:")
c, _ = cidr.SuperNetting([]string{
"2001:db8::/66",
"2001:db8:0:0:8000::/66",
"2001:db8:0:0:4000::/66",
"2001:db8:0:0:c000::/66",
})
fmt.Println("\t", c.CIDR())
}
```