-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates AdguardTeam/AdGuardHome#4262. Squashed commit of the following: commit 0f2c311 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Apr 3 17:39:16 2023 +0300 all: add ip proto pref
- Loading branch information
Showing
8 changed files
with
142 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Package netutil contains network-related utilities common among dnsproxy | ||
// packages. | ||
// | ||
// TODO(a.garipov): Move improved versions of these into netutil in module | ||
// golibs. | ||
package netutil | ||
|
||
import ( | ||
"net" | ||
|
||
glnetutil "github.com/AdguardTeam/golibs/netutil" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
// SortIPAddrs sorts addrs in accordance with the protocol preferences. Invalid | ||
// addresses are sorted near the end. Zones are ignored. | ||
// | ||
// TODO(a.garipov): Use netip.Addr instead of net.IPAddr everywhere where this | ||
// is called. | ||
func SortIPAddrs(addrs []net.IPAddr, preferIPv6 bool) { | ||
l := len(addrs) | ||
if l <= 1 { | ||
return | ||
} | ||
|
||
slices.SortStableFunc(addrs, func(addrA, addrB net.IPAddr) (sortsBefore bool) { | ||
// Assume that len(addrs) is mostly small, so these conversions aren't | ||
// as expensive as they could have been. | ||
a, err := glnetutil.IPToAddrNoMapped(addrA.IP) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
b, err := glnetutil.IPToAddrNoMapped(addrB.IP) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
aIs4 := a.Is4() | ||
bIs4 := b.Is4() | ||
if aIs4 != bIs4 { | ||
if aIs4 { | ||
return !preferIPv6 | ||
} | ||
|
||
return preferIPv6 | ||
} | ||
|
||
return a.Less(b) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package netutil_test | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/AdguardTeam/dnsproxy/internal/netutil" | ||
) | ||
|
||
func ExampleSortIPAddrs() { | ||
printAddrs := func(header string, addrs []net.IPAddr) { | ||
fmt.Printf("%s:\n", header) | ||
for i, a := range addrs { | ||
fmt.Printf("%d: %s\n", i+1, a.IP) | ||
} | ||
|
||
fmt.Println() | ||
} | ||
|
||
addrs := []net.IPAddr{{ | ||
IP: net.ParseIP("1.2.3.4"), | ||
}, { | ||
IP: net.ParseIP("1.2.3.5"), | ||
}, { | ||
IP: net.ParseIP("2a00::1234"), | ||
}, { | ||
IP: net.ParseIP("2a00::1235"), | ||
}, { | ||
IP: nil, | ||
}} | ||
netutil.SortIPAddrs(addrs, false) | ||
printAddrs("IPv4 preferred", addrs) | ||
|
||
netutil.SortIPAddrs(addrs, true) | ||
printAddrs("IPv6 preferred", addrs) | ||
|
||
// Output: | ||
// | ||
// IPv4 preferred: | ||
// 1: 1.2.3.4 | ||
// 2: 1.2.3.5 | ||
// 3: 2a00::1234 | ||
// 4: 2a00::1235 | ||
// 5: <nil> | ||
// | ||
// IPv6 preferred: | ||
// 1: 2a00::1234 | ||
// 2: 2a00::1235 | ||
// 3: 1.2.3.4 | ||
// 4: 1.2.3.5 | ||
// 5: <nil> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters