2021-03-31 09:36:57 +00:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-15 13:59:05 +00:00
|
|
|
"strconv"
|
2021-04-07 13:36:38 +00:00
|
|
|
"strings"
|
2021-03-31 09:36:57 +00:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/agherr"
|
2021-04-07 13:36:38 +00:00
|
|
|
"golang.org/x/net/idna"
|
2021-03-31 09:36:57 +00:00
|
|
|
)
|
|
|
|
|
2021-04-19 13:04:40 +00:00
|
|
|
// IsValidHostOuterRune returns true if r is a valid initial or final rune for
|
|
|
|
// a hostname label.
|
|
|
|
func IsValidHostOuterRune(r rune) (ok bool) {
|
|
|
|
return (r >= 'a' && r <= 'z') ||
|
|
|
|
(r >= 'A' && r <= 'Z') ||
|
|
|
|
(r >= '0' && r <= '9')
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValidHostRune returns true if r is a valid rune for a hostname label.
|
|
|
|
func isValidHostRune(r rune) (ok bool) {
|
|
|
|
return r == '-' || IsValidHostOuterRune(r)
|
|
|
|
}
|
|
|
|
|
2021-03-31 09:36:57 +00:00
|
|
|
// ValidateHardwareAddress returns an error if hwa is not a valid EUI-48,
|
|
|
|
// EUI-64, or 20-octet InfiniBand link-layer address.
|
|
|
|
func ValidateHardwareAddress(hwa net.HardwareAddr) (err error) {
|
|
|
|
defer agherr.Annotate("validating hardware address %q: %w", &err, hwa)
|
|
|
|
|
|
|
|
switch l := len(hwa); l {
|
|
|
|
case 0:
|
|
|
|
return agherr.Error("address is empty")
|
|
|
|
case 6, 8, 20:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("bad len: %d", l)
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 13:36:38 +00:00
|
|
|
|
|
|
|
// maxDomainLabelLen is the maximum allowed length of a domain name label
|
|
|
|
// according to RFC 1035.
|
|
|
|
const maxDomainLabelLen = 63
|
|
|
|
|
|
|
|
// maxDomainNameLen is the maximum allowed length of a full domain name
|
|
|
|
// according to RFC 1035.
|
|
|
|
//
|
|
|
|
// See https://stackoverflow.com/a/32294443/1892060.
|
|
|
|
const maxDomainNameLen = 253
|
|
|
|
|
|
|
|
const invalidCharMsg = "invalid char %q at index %d in %q"
|
|
|
|
|
|
|
|
// ValidateDomainNameLabel returns an error if label is not a valid label of
|
|
|
|
// a domain name.
|
|
|
|
func ValidateDomainNameLabel(label string) (err error) {
|
2021-04-19 13:04:40 +00:00
|
|
|
l := len(label)
|
|
|
|
if l > maxDomainLabelLen {
|
2021-04-07 13:36:38 +00:00
|
|
|
return fmt.Errorf("%q is too long, max: %d", label, maxDomainLabelLen)
|
2021-04-19 13:04:40 +00:00
|
|
|
} else if l == 0 {
|
2021-04-07 13:36:38 +00:00
|
|
|
return agherr.Error("label is empty")
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:04:40 +00:00
|
|
|
if r := label[0]; !IsValidHostOuterRune(rune(r)) {
|
2021-04-07 13:36:38 +00:00
|
|
|
return fmt.Errorf(invalidCharMsg, r, 0, label)
|
2021-04-19 13:04:40 +00:00
|
|
|
} else if l == 1 {
|
|
|
|
return nil
|
2021-04-07 13:36:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 13:04:40 +00:00
|
|
|
for i, r := range label[1 : l-1] {
|
2021-04-07 13:36:38 +00:00
|
|
|
if !isValidHostRune(r) {
|
|
|
|
return fmt.Errorf(invalidCharMsg, r, i+1, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:04:40 +00:00
|
|
|
if r := label[l-1]; !IsValidHostOuterRune(rune(r)) {
|
|
|
|
return fmt.Errorf(invalidCharMsg, r, l-1, label)
|
|
|
|
}
|
|
|
|
|
2021-04-07 13:36:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateDomainName validates the domain name in accordance to RFC 952, RFC
|
|
|
|
// 1035, and with RFC-1123's inclusion of digits at the start of the host. It
|
|
|
|
// doesn't validate against two or more hyphens to allow punycode and
|
|
|
|
// internationalized domains.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): After making sure that this works correctly, port this into
|
|
|
|
// module golibs.
|
|
|
|
func ValidateDomainName(name string) (err error) {
|
|
|
|
name, err = idna.ToASCII(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := len(name)
|
2021-04-19 13:04:40 +00:00
|
|
|
if l == 0 {
|
|
|
|
return agherr.Error("domain name is empty")
|
|
|
|
} else if l > maxDomainNameLen {
|
2021-04-07 13:36:38 +00:00
|
|
|
return fmt.Errorf("%q is too long, max: %d", name, maxDomainNameLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := strings.Split(name, ".")
|
|
|
|
for i, l := range labels {
|
|
|
|
err = ValidateDomainNameLabel(l)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid domain name label at index %d: %w", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-15 13:59:05 +00:00
|
|
|
|
|
|
|
// The maximum lengths of generated hostnames for different IP versions.
|
|
|
|
const (
|
|
|
|
ipv4HostnameMaxLen = len("192-168-100-10-")
|
|
|
|
ipv6HostnameMaxLen = len("ff80-f076-0000-0000-0000-0000-0000-0010")
|
|
|
|
)
|
|
|
|
|
|
|
|
// generateIPv4Hostname generates the hostname for specific IP version.
|
|
|
|
func generateIPv4Hostname(ipv4 net.IP) (hostname string) {
|
|
|
|
hnData := make([]byte, 0, ipv4HostnameMaxLen)
|
|
|
|
for i, part := range ipv4 {
|
|
|
|
if i > 0 {
|
|
|
|
hnData = append(hnData, '-')
|
|
|
|
}
|
|
|
|
hnData = strconv.AppendUint(hnData, uint64(part), 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(hnData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateIPv6Hostname generates the hostname for specific IP version.
|
|
|
|
func generateIPv6Hostname(ipv6 net.IP) (hostname string) {
|
|
|
|
hnData := make([]byte, 0, ipv6HostnameMaxLen)
|
|
|
|
for i, partsNum := 0, net.IPv6len/2; i < partsNum; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
hnData = append(hnData, '-')
|
|
|
|
}
|
|
|
|
for _, val := range ipv6[i*2 : i*2+2] {
|
|
|
|
if val < 10 {
|
|
|
|
hnData = append(hnData, '0')
|
|
|
|
}
|
|
|
|
hnData = strconv.AppendUint(hnData, uint64(val), 16)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(hnData)
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:04:40 +00:00
|
|
|
// GenerateHostname generates the hostname from ip. In case of using IPv4 the
|
2021-04-15 13:59:05 +00:00
|
|
|
// result should be like:
|
|
|
|
//
|
|
|
|
// 192-168-10-1
|
|
|
|
//
|
|
|
|
// In case of using IPv6, the result is like:
|
|
|
|
//
|
|
|
|
// ff80-f076-0000-0000-0000-0000-0000-0010
|
|
|
|
//
|
2021-04-19 13:04:40 +00:00
|
|
|
func GenerateHostname(ip net.IP) (hostname string) {
|
2021-04-15 13:59:05 +00:00
|
|
|
if ipv4 := ip.To4(); ipv4 != nil {
|
|
|
|
return generateIPv4Hostname(ipv4)
|
|
|
|
} else if ipv6 := ip.To16(); ipv6 != nil {
|
|
|
|
return generateIPv6Hostname(ipv6)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|