mirror of
https://github.com/coredhcp/coredhcp.git
synced 2026-04-01 11:31:15 +02:00
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
// Copyright 2018-present the CoreDHCP Authors. All rights reserved
|
|
// This source code is licensed under the MIT license found in the
|
|
// LICENSE file in the root directory of this source tree.
|
|
|
|
package netmask
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"net"
|
|
|
|
"github.com/coredhcp/coredhcp/handler"
|
|
"github.com/coredhcp/coredhcp/logger"
|
|
"github.com/coredhcp/coredhcp/plugins"
|
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
|
"github.com/insomniacslk/dhcp/dhcpv6"
|
|
)
|
|
|
|
var log = logger.GetLogger("plugins/netmask")
|
|
|
|
func init() {
|
|
plugins.RegisterPlugin("netmask", setupNetmask6, setupNetmask4)
|
|
}
|
|
|
|
var (
|
|
netmask net.IPMask
|
|
)
|
|
|
|
func setupNetmask6(args ...string) (handler.Handler6, error) {
|
|
// TODO setup function for IPv6
|
|
log.Warning("not implemented for IPv6")
|
|
return Handler6, nil
|
|
}
|
|
|
|
func setupNetmask4(args ...string) (handler.Handler4, error) {
|
|
log.Printf("loaded plugin for DHCPv4.")
|
|
if len(args) != 1 {
|
|
return nil, errors.New("need at least one netmask IP address")
|
|
}
|
|
netmaskIP := net.ParseIP(args[0])
|
|
if netmaskIP.IsUnspecified() {
|
|
return nil, errors.New("netmask is not valid, got: " + args[1])
|
|
}
|
|
netmaskIP = netmaskIP.To4()
|
|
if netmaskIP == nil {
|
|
return nil, errors.New("expected an netmask address, got: " + args[1])
|
|
}
|
|
netmask = net.IPv4Mask(netmaskIP[0], netmaskIP[1], netmaskIP[2], netmaskIP[3])
|
|
if !checkValidNetmask(netmask) {
|
|
return nil, errors.New("netmask is not valid, got: " + args[1])
|
|
}
|
|
log.Printf("loaded client netmask")
|
|
return Handler4, nil
|
|
}
|
|
|
|
// Handler6 not implemented only IPv4
|
|
func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
|
|
// TODO add IPv6 netmask to the response
|
|
return resp, false
|
|
}
|
|
|
|
//Handler4 handles DHCPv4 packets for the netmask plugin
|
|
func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
|
|
resp.Options.Update(dhcpv4.OptSubnetMask(netmask))
|
|
return resp, false
|
|
}
|
|
func checkValidNetmask(netmask net.IPMask) bool {
|
|
netmaskInt := binary.BigEndian.Uint32(netmask)
|
|
x := ^netmaskInt
|
|
y := x + 1
|
|
return (y & x) == 0
|
|
}
|