An open API service indexing awesome lists of open source software.

https://github.com/iiiceoo/iprange

IPv4/IPv6 addresses parser for IP range format.
https://github.com/iiiceoo/iprange

go iprange ipv4 ipv6 networking

Last synced: about 1 year ago
JSON representation

IPv4/IPv6 addresses parser for IP range format.

Awesome Lists containing this project

README

          

# iprange

[![GoDoc](https://godoc.org/github.com/iiiceoo/iprange?status.svg)](https://godoc.org/github.com/iiiceoo/iprange)
[![codecov](https://codecov.io/gh/iiiceoo/iprange/branch/main/graph/badge.svg?token=7STDXD53G0)](https://codecov.io/gh/iiiceoo/iprange)

*Package iprange parses IPv4/IPv6 addresses from strings in IP range format.*

## Supported IP range formats

- `172.18.0.1` / `fd00::1`
- `172.18.0.0/24` / `fd00::/64`
- `172.18.0.1-10` / `fd00::1-a`
- `172.18.0.1-172.18.1.10` / `fd00::1-fd00::1:a`

## Example

```go
package main

import (
"fmt"
"log"

"github.com/iiiceoo/iprange"
)

func main() {
// Parse IP ranges.
ranges, err := iprange.Parse(
"172.18.0.1",
"172.18.0.0/24",
"172.18.0.1-10",
"172.18.1.1-172.18.1.3",
)
if err != nil {
log.Fatalf("failed to parse IP ranges: %v\n", err)
}
fmt.Printf("%s IP ranges: %s\n", ranges.Version(), ranges)

// Merge IP ranges.
merged := ranges.Merge()
fmt.Printf("Merged IP ranges: %s\n", merged)

// Interval mathematics of IP ranges.
another, _ := iprange.Parse("172.18.0.0/24")
diff := merged.Diff(another)
fmt.Printf("The difference between two IP ranges: %s\n", diff)

// Convert IP ranges to IP addresses.
fmt.Printf("Iterate through all %d IP addresses:\n", diff.Size())
ipIter := diff.IPIterator()
for {
ip := ipIter.Next()
if ip == nil {
break
}
fmt.Println(ip)
}
}
```

## License

Package iprange is MIT-Licensed.