Skip to content

Commit

Permalink
all: imp code, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Apr 15, 2021
1 parent 77cdb12 commit 74e450c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to

### Added

- Hostname generating for DHCP clients which don't provide their own ([#2723]).
- New flag `--no-etc-hosts` to disable client domain name lookups in the
operating system's /etc/hosts files ([#1947]).
- The ability to set up custom upstreams to resolve PTR queries for local
Expand All @@ -30,7 +31,6 @@ and this project adheres to

### Changed

- Hostname generating for DHCP clients which don't provide their own ([#2723]).
- Normalization of hostnames with spaces sent by DHCP clients ([#2945]).
- The access to the private hosts is now forbidden for users from external
networks ([#2889]).
Expand Down
60 changes: 43 additions & 17 deletions internal/aghnet/addr.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aghnet

import (
"encoding/hex"
"fmt"
"net"
"strconv"
Expand Down Expand Up @@ -102,31 +101,58 @@ func ValidateDomainName(name string) (err error) {
return nil
}

// GenHostName builds the hostname from ip. In case of using IPv4 the
// 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)
}

// 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-0000-0000-0010
// ff80-f076-0000-0000-0000-0000-0000-0010
//
func GenHostName(ip net.IP) (hostname string) {
var parts []string
func GenerateHostName(ip net.IP) (hostname string) {
if ipv4 := ip.To4(); ipv4 != nil {
parts = make([]string, net.IPv4len)
for i := range parts {
parts[i] = strconv.Itoa(int(ipv4[i]))
}
return generateIPv4Hostname(ipv4)
} else if ipv6 := ip.To16(); ipv6 != nil {
partsNum := net.IPv6len / 2
parts = make([]string, partsNum)
dst := make([]byte, hex.EncodedLen(net.IPv6len))
hex.Encode(dst, ipv6)
for i := 0; i < partsNum; i++ {
parts[i] = string(dst[i*4 : (i+1)*4])
}
return generateIPv6Hostname(ipv6)
}

return strings.Join(parts, "-")
return ""
}
6 changes: 5 additions & 1 deletion internal/aghnet/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ func TestGenerateHostName(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, GenHostName(tc.ip))
if tc.name == "good_ipv6" {
t.Log("few")
}
hostname := GenerateHostName(tc.ip)
assert.Equal(t, tc.want, hostname)
})
}
}
2 changes: 1 addition & 1 deletion internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ func (s *v4Server) processRequest(req, resp *dhcpv4.DHCPv4) (lease *Lease, ok bo
}

if hostname == "" {
hostname = aghnet.GenHostName(reqIP)
hostname = aghnet.GenerateHostName(reqIP)
}

lease.Hostname = hostname
Expand Down

0 comments on commit 74e450c

Please sign in to comment.