mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-23 05:25:35 +03:00
c4a13b92d2
Merge in DNS/adguard-home from 3157-excessive-ptrs to master Updates #3157. Squashed commit of the following: commit 6803988240dca2f147bb80a5b3f78d7749d2fa14 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:50:01 2022 +0300 aghnet: and again commit 1a7f4d1dbc8fd4d3ae620349917526a75fa71b47 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:49:20 2022 +0300 aghnet: docs again commit d88da1fc7135f3cd03aff10b02d9957c8ffdfd30 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:47:36 2022 +0300 aghnet: imp docs commit c45dbc7800e882c6c4110aab640c32b03046f89a Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 14:41:19 2022 +0300 aghnet: keep alphabetical order commit b61781785d096ef43f60fb4f1905a4ed3cdf7c68 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 19 13:50:56 2022 +0300 aghnet: imp code quality commit 578dbd71ed2f2089c69343d7d4bf8bbc29150ace Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Apr 12 17:02:38 2022 +0300 aghnet: imp arp container
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package aghnet
|
|
|
|
import (
|
|
"net"
|
|
"strconv"
|
|
)
|
|
|
|
// The maximum lengths of generated hostnames for different IP versions.
|
|
const (
|
|
ipv4HostnameMaxLen = len("192-168-100-100")
|
|
ipv6HostnameMaxLen = len("ff80-f076-0000-0000-0000-0000-0000-0010")
|
|
)
|
|
|
|
// generateIPv4Hostname generates the hostname by IP address version 4.
|
|
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 by IP address version 6.
|
|
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)
|
|
}
|
|
|
|
// GenerateHostname generates the hostname from ip. In case of using IPv4 the
|
|
// 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
|
|
//
|
|
// ip must be either an IPv4 or an IPv6.
|
|
func GenerateHostname(ip net.IP) (hostname string) {
|
|
if ipv4 := ip.To4(); ipv4 != nil {
|
|
return generateIPv4Hostname(ipv4)
|
|
}
|
|
|
|
return generateIPv6Hostname(ip)
|
|
}
|