chore(all): replace net.IP with netip.Addr

This commit is contained in:
Quentin McGaw
2023-05-20 19:58:18 +00:00
parent 00ee6ff9a7
commit 0a29337c3b
91 changed files with 525 additions and 590 deletions

View File

@@ -1,15 +1,20 @@
package resolver
import "net"
import (
"net/netip"
)
func uniqueIPsToSlice(uniqueIPs map[string]struct{}) (ips []net.IP) {
ips = make([]net.IP, 0, len(uniqueIPs))
func uniqueIPsToSlice(uniqueIPs map[string]struct{}) (ips []netip.Addr) {
ips = make([]netip.Addr, 0, len(uniqueIPs))
for key := range uniqueIPs {
IP := net.ParseIP(key)
if IPv4 := IP.To4(); IPv4 != nil {
IP = IPv4
ip, err := netip.ParseAddr(key)
if err != nil {
panic(err)
}
ips = append(ips, IP)
if ip.Is4In6() {
ip = netip.AddrFrom4(ip.As4())
}
ips = append(ips, ip)
}
return ips
}

View File

@@ -1,7 +1,7 @@
package resolver
import (
"net"
"net/netip"
"testing"
"github.com/stretchr/testify/assert"
@@ -11,23 +11,23 @@ func Test_uniqueIPsToSlice(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
inputIPs map[string]struct{}
outputIPs []net.IP
outputIPs []netip.Addr
}{
"nil": {
inputIPs: nil,
outputIPs: []net.IP{},
outputIPs: []netip.Addr{},
},
"empty": {
inputIPs: map[string]struct{}{},
outputIPs: []net.IP{},
outputIPs: []netip.Addr{},
},
"single IPv4": {
inputIPs: map[string]struct{}{"1.1.1.1": {}},
outputIPs: []net.IP{{1, 1, 1, 1}},
outputIPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})},
},
"two IPv4s": {
inputIPs: map[string]struct{}{"1.1.1.1": {}, "1.1.2.1": {}},
outputIPs: []net.IP{{1, 1, 1, 1}, {1, 1, 2, 1}},
outputIPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{1, 1, 2, 1})},
},
}
for name, testCase := range testCases {

View File

@@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/netip"
)
type Parallel struct {
@@ -31,7 +31,7 @@ type ParallelSettings struct {
type parallelResult struct {
host string
IPs []net.IP
IPs []netip.Addr
}
var (
@@ -40,7 +40,7 @@ var (
)
func (pr *Parallel) Resolve(ctx context.Context, settings ParallelSettings) (
hostToIPs map[string][]net.IP, warnings []string, err error) {
hostToIPs map[string][]netip.Addr, warnings []string, err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
@@ -53,7 +53,7 @@ func (pr *Parallel) Resolve(ctx context.Context, settings ParallelSettings) (
go pr.resolveAsync(ctx, host, settings.Repeat, results, errors)
}
hostToIPs = make(map[string][]net.IP, len(settings.Hosts))
hostToIPs = make(map[string][]netip.Addr, len(settings.Hosts))
maxFails := int(settings.MaxFailRatio * float64(len(settings.Hosts)))
for range settings.Hosts {

View File

@@ -1,11 +1,11 @@
package resolver
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"net/netip"
"sort"
"time"
)
@@ -31,7 +31,7 @@ type RepeatSettings struct {
}
func (r *Repeat) Resolve(ctx context.Context, host string, settings RepeatSettings) (
ips []net.IP, err error) {
ips []netip.Addr, err error) {
timedCtx, cancel := context.WithTimeout(ctx, settings.MaxDuration)
defer cancel()
@@ -54,7 +54,7 @@ func (r *Repeat) Resolve(ctx context.Context, host string, settings RepeatSettin
if settings.SortIPs {
sort.Slice(ips, func(i, j int) bool {
return bytes.Compare(ips[i], ips[j]) < 1
return ips[i].Compare(ips[j]) < 1
})
}
@@ -121,15 +121,15 @@ func (r *Repeat) resolveOnce(ctx, timedCtx context.Context, host string,
}
}
func (r *Repeat) lookupIPs(ctx context.Context, host string) (ips []net.IP, err error) {
func (r *Repeat) lookupIPs(ctx context.Context, host string) (ips []netip.Addr, err error) {
addresses, err := r.resolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, err
}
ips = make([]net.IP, 0, len(addresses))
ips = make([]netip.Addr, 0, len(addresses))
for i := range addresses {
ip := addresses[i].IP
if ip == nil {
ip, ok := netip.AddrFromSlice(addresses[i].IP)
if !ok {
continue
}
ips = append(ips, ip)