Pull request: dhcpd: add ips and text option types

Updates #2385.

Squashed commit of the following:

commit ce8467f1c013c6b3fef59667084e2c6569a7213c
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Mar 15 19:02:17 2021 +0300

    dhcpd: add ips and text option types
This commit is contained in:
Ainar Garipov
2021-03-15 19:24:26 +03:00
parent a818666294
commit e6a8fe452c
7 changed files with 263 additions and 122 deletions

View File

@@ -2,18 +2,14 @@
package dhcpd
import (
"encoding/hex"
"encoding/json"
"fmt"
"net"
"net/http"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/log"
)
@@ -283,48 +279,3 @@ func (s *Server) FindMACbyIP(ip net.IP) net.HardwareAddr {
func (s *Server) AddStaticLease(lease Lease) error {
return s.srv4.AddStaticLease(lease)
}
// Parse option string
// Format:
// CODE TYPE VALUE
func parseOptionString(s string) (uint8, []byte) {
s = strings.TrimSpace(s)
scode := util.SplitNext(&s, ' ')
t := util.SplitNext(&s, ' ')
sval := util.SplitNext(&s, ' ')
code, err := strconv.Atoi(scode)
if err != nil || code <= 0 || code > 255 {
return 0, nil
}
var val []byte
switch t {
case "hex":
val, err = hex.DecodeString(sval)
if err != nil {
return 0, nil
}
case "ip":
ip := net.ParseIP(sval)
if ip == nil {
return 0, nil
}
// Most DHCP options require IPv4, so do not put the 16-byte
// version if we can. Otherwise, the clients will receive weird
// data that looks like four IPv4 addresses.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2688.
if ip4 := ip.To4(); ip4 != nil {
val = ip4
} else {
val = ip
}
default:
return 0, nil
}
return uint8(code), val
}