2023-05-11 01:09:18 -06:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2024-09-05 08:46:20 -06:00
|
|
|
"cmp"
|
2023-05-11 01:09:18 -06:00
|
|
|
"context"
|
|
|
|
"net"
|
2024-09-05 08:46:20 -06:00
|
|
|
"net/netip"
|
2023-05-11 01:09:18 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func GrpcSocketDialer(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
var d net.Dialer
|
|
|
|
|
|
|
|
return d.DialContext(ctx, "unix", addr)
|
|
|
|
}
|
2024-09-05 08:46:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
// TODO(kradalby): Remove after go 1.24, will be in stdlib.
|
|
|
|
// Compare returns an integer comparing two prefixes.
|
|
|
|
// The result will be 0 if p == p2, -1 if p < p2, and +1 if p > p2.
|
|
|
|
// Prefixes sort first by validity (invalid before valid), then
|
|
|
|
// address family (IPv4 before IPv6), then prefix length, then
|
|
|
|
// address.
|
|
|
|
func ComparePrefix(p, p2 netip.Prefix) int {
|
|
|
|
if c := cmp.Compare(p.Addr().BitLen(), p2.Addr().BitLen()); c != 0 {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
if c := cmp.Compare(p.Bits(), p2.Bits()); c != 0 {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
return p.Addr().Compare(p2.Addr())
|
|
|
|
}
|