forked from quic-go/quic-go
introduce a function to distinguish between IPv4 and IPv6 addresses
This commit is contained in:
10
internal/utils/ip.go
Normal file
10
internal/utils/ip.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package utils
|
||||
|
||||
import "net"
|
||||
|
||||
func IsIPv4(ip net.IP) bool {
|
||||
// If ip is not an IPv4 address, To4 returns nil.
|
||||
// Note that there might be some corner cases, where this is not correct.
|
||||
// See https://stackoverflow.com/questions/22751035/golang-distinguish-ipv4-ipv6.
|
||||
return ip.To4() != nil
|
||||
}
|
||||
17
internal/utils/ip_test.go
Normal file
17
internal/utils/ip_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("IP", func() {
|
||||
It("tells IPv4 and IPv6 addresses apart", func() {
|
||||
Expect(IsIPv4(net.IPv4(127, 0, 0, 1))).To(BeTrue())
|
||||
Expect(IsIPv4(net.IPv4zero)).To(BeTrue())
|
||||
Expect(IsIPv4(net.IPv6zero)).To(BeFalse())
|
||||
Expect(IsIPv4(net.IPv6loopback)).To(BeFalse())
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user