Pull request: 4337 Add del option

Merge in DNS/adguard-home from 4337-dhcp-cli-id to master

Updates #4337.

Squashed commit of the following:

commit c393bf7c5964b64f6b4528db0418e54416dd246c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Fri Aug 26 14:18:48 2022 +0300

    dhcpd: ip docs

commit bfeef3e881ed04eab6285c1ac62005c7cb57c11f
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Fri Aug 26 13:54:33 2022 +0300

    all: finish chlog fmt

commit e5fbb7385450825ca81fc7622feb7beb390a8bad
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Fri Aug 26 13:49:10 2022 +0300

    dhcpd: imp naming

commit cb49eeb536afd8dc1dd2ea9169dd024c7d4a3a25
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Fri Aug 26 12:23:54 2022 +0300

    dhcpd: imp docs

commit c4ea72a5e7572d40a885125c4f61ef6694e38170
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Aug 25 20:15:15 2022 +0300

    dhcpd: imp code, docs

commit 36d0e309e7ef0abdcdd94673e87f4d0af67b9cb3
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Aug 25 19:45:24 2022 +0300

    dhcpd: add del opt
This commit is contained in:
Eugene Burkov
2022-08-26 14:30:31 +03:00
parent 2410639123
commit 45bcc2c09a
5 changed files with 203 additions and 130 deletions

View File

@@ -18,17 +18,14 @@ import (
// The aliases for DHCP option types available for explicit declaration.
const (
hexTyp = "hex"
ipTyp = "ip"
ipsTyp = "ips"
textTyp = "text"
typHex = "hex"
typIP = "ip"
typIPs = "ips"
typText = "text"
typDel = "del"
)
// parseDHCPOptionHex parses a DHCP option as a hex-encoded string. For
// example:
//
// 252 hex 736f636b733a2f2f70726f78792e6578616d706c652e6f7267
//
// parseDHCPOptionHex parses a DHCP option as a hex-encoded string.
func parseDHCPOptionHex(s string) (val dhcpv4.OptionValue, err error) {
var data []byte
data, err = hex.DecodeString(s)
@@ -39,10 +36,7 @@ func parseDHCPOptionHex(s string) (val dhcpv4.OptionValue, err error) {
return dhcpv4.OptionGeneric{Data: data}, nil
}
// parseDHCPOptionIP parses a DHCP option as a single IP address. For example:
//
// 6 ip 192.168.1.1
//
// parseDHCPOptionIP parses a DHCP option as a single IP address.
func parseDHCPOptionIP(s string) (val dhcpv4.OptionValue, err error) {
var ip net.IP
// All DHCPv4 options require IPv4, so don't put the 16-byte version.
@@ -58,10 +52,7 @@ func parseDHCPOptionIP(s string) (val dhcpv4.OptionValue, err error) {
}
// parseDHCPOptionIPs parses a DHCP option as a comma-separates list of IP
// addresses. For example:
//
// 6 ips 192.168.1.1,192.168.1.2
//
// addresses.
func parseDHCPOptionIPs(s string) (val dhcpv4.OptionValue, err error) {
var ips dhcpv4.IPs
var ip net.IP
@@ -78,23 +69,53 @@ func parseDHCPOptionIPs(s string) (val dhcpv4.OptionValue, err error) {
}
// parseDHCPOptionText parses a DHCP option as a simple UTF-8 encoded
// text. For example:
//
// 252 text http://192.168.1.1/wpad.dat
//
// text.
func parseDHCPOptionText(s string) (val dhcpv4.OptionValue) {
return dhcpv4.OptionGeneric{Data: []byte(s)}
}
// parseDHCPOption parses an option. See the documentation of parseDHCPOption*
// for more info.
// parseDHCPOptionVal parses a DHCP option value considering typ. For the del
// option the value string is ignored. The examples of possible value pairs:
//
// - hex 736f636b733a2f2f70726f78792e6578616d706c652e6f7267
// - ip 192.168.1.1
// - ips 192.168.1.1,192.168.1.2
// - text http://192.168.1.1/wpad.dat
// - del
func parseDHCPOptionVal(typ, valStr string) (val dhcpv4.OptionValue, err error) {
switch typ {
case typHex:
val, err = parseDHCPOptionHex(valStr)
case typIP:
val, err = parseDHCPOptionIP(valStr)
case typIPs:
val, err = parseDHCPOptionIPs(valStr)
case typText:
val = parseDHCPOptionText(valStr)
case typDel:
val = dhcpv4.OptionGeneric{Data: nil}
default:
err = fmt.Errorf("unknown option type %q", typ)
}
return val, err
}
// parseDHCPOption parses an option. See the documentation of
// parseDHCPOptionVal for more info.
func parseDHCPOption(s string) (opt dhcpv4.Option, err error) {
defer func() { err = errors.Annotate(err, "invalid option string %q: %w", s) }()
s = strings.TrimSpace(s)
parts := strings.SplitN(s, " ", 3)
if len(parts) < 3 {
return opt, errors.Error("need at least three fields")
var valStr string
if pl := len(parts); pl < 3 {
if pl < 2 || parts[1] != typDel {
return opt, errors.Error("bad option format")
}
} else {
valStr = parts[2]
}
var code64 uint64
@@ -103,27 +124,16 @@ func parseDHCPOption(s string) (opt dhcpv4.Option, err error) {
return opt, fmt.Errorf("parsing option code: %w", err)
}
var optVal dhcpv4.OptionValue
switch typ, val := parts[1], parts[2]; typ {
case hexTyp:
optVal, err = parseDHCPOptionHex(val)
case ipTyp:
optVal, err = parseDHCPOptionIP(val)
case ipsTyp:
optVal, err = parseDHCPOptionIPs(val)
case textTyp:
optVal = parseDHCPOptionText(val)
default:
return opt, fmt.Errorf("unknown option type %q", typ)
}
val, err := parseDHCPOptionVal(parts[1], valStr)
if err != nil {
// Don't wrap an error since it's informative enough as is and there
// also the deferred annotation.
return opt, err
}
return dhcpv4.Option{
Code: dhcpv4.GenericOptionCode(code64),
Value: optVal,
Value: val,
}, nil
}
@@ -139,40 +149,45 @@ func prepareOptions(conf V4ServerConf) (opts dhcpv4.Options) {
// See also https://datatracker.ietf.org/doc/html/rfc1122,
// https://datatracker.ietf.org/doc/html/rfc1123, and
// https://datatracker.ietf.org/doc/html/rfc2132.
opts = dhcpv4.Options{
opts = dhcpv4.OptionsFromList(
// IP-Layer Per Host
dhcpv4.OptionNonLocalSourceRouting.Code(): []byte{0},
dhcpv4.OptGeneric(dhcpv4.OptionNonLocalSourceRouting, []byte{0}),
// Set the current recommended default time to live for the
// Internet Protocol which is 64, see
// https://datatracker.ietf.org/doc/html/rfc1700.
dhcpv4.OptionDefaultIPTTL.Code(): []byte{64},
dhcpv4.OptGeneric(dhcpv4.OptionDefaultIPTTL, []byte{0x40}),
// IP-Layer Per Interface
dhcpv4.OptionPerformMaskDiscovery.Code(): []byte{0},
dhcpv4.OptionMaskSupplier.Code(): []byte{0},
dhcpv4.OptionPerformRouterDiscovery.Code(): []byte{1},
dhcpv4.OptGeneric(dhcpv4.OptionPerformMaskDiscovery, []byte{0}),
dhcpv4.OptGeneric(dhcpv4.OptionMaskSupplier, []byte{0}),
dhcpv4.OptGeneric(dhcpv4.OptionPerformRouterDiscovery, []byte{1}),
// The all-routers address is preferred wherever possible, see
// https://datatracker.ietf.org/doc/html/rfc1256#section-5.1.
dhcpv4.OptionRouterSolicitationAddress.Code(): netutil.IPv4allrouter(),
dhcpv4.OptionBroadcastAddress.Code(): netutil.IPv4bcast(),
dhcpv4.Option{
Code: dhcpv4.OptionRouterSolicitationAddress,
Value: dhcpv4.IP(netutil.IPv4allrouter()),
},
dhcpv4.OptBroadcastAddress(netutil.IPv4bcast()),
// Link-Layer Per Interface
dhcpv4.OptionTrailerEncapsulation.Code(): []byte{0},
dhcpv4.OptionEthernetEncapsulation.Code(): []byte{0},
dhcpv4.OptGeneric(dhcpv4.OptionTrailerEncapsulation, []byte{0}),
dhcpv4.OptGeneric(dhcpv4.OptionEthernetEncapsulation, []byte{0}),
// TCP Per Host
dhcpv4.OptionTCPKeepaliveInterval.Code(): dhcpv4.Duration(0).ToBytes(),
dhcpv4.OptionTCPKeepaliveGarbage.Code(): []byte{0},
dhcpv4.Option{
Code: dhcpv4.OptionTCPKeepaliveInterval,
Value: dhcpv4.Duration(0),
},
dhcpv4.OptGeneric(dhcpv4.OptionTCPKeepaliveGarbage, []byte{0}),
// Values From Configuration
dhcpv4.OptionRouter.Code(): netutil.CloneIP(conf.subnet.IP),
dhcpv4.OptionSubnetMask.Code(): dhcpv4.IPMask(conf.subnet.Mask).ToBytes(),
}
dhcpv4.OptRouter(conf.subnet.IP),
dhcpv4.OptSubnetMask(conf.subnet.Mask),
)
// Set values for explicitly configured options.
for i, o := range conf.Options {