dhcpd

package
v0.108.0-b.25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2023 License: GPL-3.0 Imports: 39 Imported by: 0

README

DHCP server

Contents:

Test setup with Virtual Box

To set up a test environment for DHCP server you need:

  • Linux host machine
  • Virtual Box
  • Virtual machine (guest OS doesn't matter)
Configure client
  1. Install Virtual Box and run the following command to create a Host-Only network:

     $ VBoxManage hostonlyif create
    

    You can check its status by ip a command.

    You can also set up Host-Only network using Virtual Box menu:

     File -> Host Network Manager...
    
  2. Create your virtual machine and set up its network:

     VM Settings -> Network -> Host-only Adapter
    
  3. Start your VM, install an OS. Configure your network interface to use DHCP and the OS should ask for a IP address from our DHCP server.

  4. To see the current IP address on client OS you can use ip a command on Linux or ipconfig on Windows.

  5. To force the client OS to request an IP from DHCP server again, you can use dhclient on Linux or ipconfig /release on Windows.

Configure server
  1. Edit server configuration file 'AdGuardHome.yaml', for example:

     dhcp:
       enabled: true
       interface_name: vboxnet0
       dhcpv4:
         gateway_ip: 192.168.56.1
         subnet_mask: 255.255.255.0
         range_start: 192.168.56.2
         range_end: 192.168.56.2
         lease_duration: 86400
         icmp_timeout_msec: 1000
         options: []
       dhcpv6:
         range_start: 2001::1
         lease_duration: 86400
         ra_slaac_only: false
         ra_allow_slaac: false
    
  2. Start the server

     ./AdGuardHome
    

    There should be a message in log which shows that DHCP server is ready:

     [info] DHCP: listening on 0.0.0.0:67
    

Documentation

Overview

Package dhcpd provides a DHCP server.

Index

Constants

View Source
const (

	// DefaultDHCPLeaseTTL is the default time-to-live for leases.
	DefaultDHCPLeaseTTL = uint32(timeutil.Day / time.Second)

	// DefaultDHCPTimeoutICMP is the default timeout for waiting ICMP responses.
	DefaultDHCPTimeoutICMP = 1000
)
View Source
const (
	LeaseChangedAdded = iota
	LeaseChangedAddedStatic
	LeaseChangedRemovedStatic
	LeaseChangedRemovedAll

	LeaseChangedDBStore
)

flags for onLeaseChanged()

View Source
const ErrDupHostname = errors.Error("hostname is not unique")

ErrDupHostname is returned by addLease when the added lease has a not empty non-unique hostname.

View Source
const ErrUnconfigured errors.Error = "server is unconfigured"

ErrUnconfigured is returned from the server's method when it requires the server to be configured and it's not.

Variables

This section is empty.

Functions

func Create

func Create(conf *ServerConfig) (s *server, err error)

Create initializes and returns the DHCP server handling both address families. It also registers the corresponding HTTP API endpoints.

func OptionFQDN added in v0.107.12

func OptionFQDN(fqdn string) (opt dhcpv4.Option)

OptionFQDN returns a DHCPv4 option for sending the FQDN to the client requested another hostname.

See https://datatracker.ietf.org/doc/html/rfc4702.

Types

type DHCPServer

type DHCPServer interface {
	// ResetLeases resets leases.
	ResetLeases(leases []*Lease) (err error)
	// GetLeases returns deep clones of the current leases.
	GetLeases(flags GetLeasesFlags) (leases []*Lease)
	// AddStaticLease - add a static lease
	AddStaticLease(l *Lease) (err error)
	// RemoveStaticLease - remove a static lease
	RemoveStaticLease(l *Lease) (err error)
	// FindMACbyIP - find a MAC address by IP address in the currently active DHCP leases
	FindMACbyIP(ip net.IP) net.HardwareAddr

	// WriteDiskConfig4 - copy disk configuration
	WriteDiskConfig4(c *V4ServerConf)
	// WriteDiskConfig6 - copy disk configuration
	WriteDiskConfig6(c *V6ServerConf)

	// Start - start server
	Start() (err error)
	// Stop - stop server
	Stop() (err error)
	// contains filtered or unexported methods
}

DHCPServer - DHCP server interface

type GetLeasesFlags added in v0.107.0

type GetLeasesFlags uint8

GetLeasesFlags are the flags for GetLeases.

const (
	LeasesDynamic GetLeasesFlags = 0b01
	LeasesStatic  GetLeasesFlags = 0b10

	LeasesAll = LeasesDynamic | LeasesStatic
)

GetLeasesFlags values

type Interface added in v0.107.13

type Interface interface {
	Start() (err error)
	Stop() (err error)
	Enabled() (ok bool)

	Leases(flags GetLeasesFlags) (leases []*Lease)
	SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT)
	FindMACbyIP(ip net.IP) (mac net.HardwareAddr)

	WriteDiskConfig(c *ServerConfig)
}

Interface is the DHCP server that deals with both IP address families.

type Lease

type Lease struct {
	// Expiry is the expiration time of the lease.  The unix timestamp value
	// of 1 means that this is a static lease.
	Expiry time.Time `json:"expires"`

	Hostname string           `json:"hostname"`
	HWAddr   net.HardwareAddr `json:"mac"`
	IP       net.IP           `json:"ip"`
}

Lease contains the necessary information about a DHCP lease

func (*Lease) Clone added in v0.107.0

func (l *Lease) Clone() (clone *Lease)

Clone returns a deep copy of l.

func (*Lease) IsBlocklisted added in v0.107.0

func (l *Lease) IsBlocklisted() (ok bool)

IsBlocklisted returns true if the lease is blocklisted.

TODO(a.garipov): Just make it a boolean field.

func (*Lease) IsStatic added in v0.106.0

func (l *Lease) IsStatic() (ok bool)

IsStatic returns true if the lease is static.

TODO(a.garipov): Just make it a boolean field.

func (Lease) MarshalJSON added in v0.105.0

func (l Lease) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Lease.

func (*Lease) UnmarshalJSON added in v0.105.0

func (l *Lease) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface for *Lease.

type MockInterface added in v0.107.13

type MockInterface struct {
	OnStart             func() (err error)
	OnStop              func() (err error)
	OnEnabled           func() (ok bool)
	OnLeases            func(flags GetLeasesFlags) (leases []*Lease)
	OnSetOnLeaseChanged func(f OnLeaseChangedT)
	OnFindMACbyIP       func(ip net.IP) (mac net.HardwareAddr)
	OnWriteDiskConfig   func(c *ServerConfig)
}

MockInterface is a mock Interface implementation.

TODO(e.burkov): Move to aghtest when the API stabilized.

func (*MockInterface) Enabled added in v0.107.13

func (s *MockInterface) Enabled() (ok bool)

Enabled implements the Interface for *MockInterface.

func (*MockInterface) FindMACbyIP added in v0.107.13

func (s *MockInterface) FindMACbyIP(ip net.IP) (mac net.HardwareAddr)

FindMACbyIP implements the Interface for *MockInterface.

func (*MockInterface) Leases added in v0.107.13

func (s *MockInterface) Leases(flags GetLeasesFlags) (ls []*Lease)

Leases implements the Interface for *MockInterface.

func (*MockInterface) SetOnLeaseChanged added in v0.107.13

func (s *MockInterface) SetOnLeaseChanged(f OnLeaseChangedT)

SetOnLeaseChanged implements the Interface for *MockInterface.

func (*MockInterface) Start added in v0.107.13

func (s *MockInterface) Start() (err error)

Start implements the Interface for *MockInterface.

func (*MockInterface) Stop added in v0.107.13

func (s *MockInterface) Stop() (err error)

Stop implements the Interface for *MockInterface.

func (*MockInterface) WriteDiskConfig added in v0.107.13

func (s *MockInterface) WriteDiskConfig(c *ServerConfig)

WriteDiskConfig implements the Interface for *MockInterface.

type OnLeaseChangedT

type OnLeaseChangedT func(flags int)

OnLeaseChangedT is a callback for lease changes.

type ServerConfig

type ServerConfig struct {
	// Called when the configuration is changed by HTTP request
	ConfigModified func() `yaml:"-"`

	// Register an HTTP handler
	HTTPRegister aghhttp.RegisterFunc `yaml:"-"`

	Enabled       bool   `yaml:"enabled"`
	InterfaceName string `yaml:"interface_name"`

	// LocalDomainName is the domain name used for DHCP hosts.  For example,
	// a DHCP client with the hostname "myhost" can be addressed as "myhost.lan"
	// when LocalDomainName is "lan".
	LocalDomainName string `yaml:"local_domain_name"`

	Conf4 V4ServerConf `yaml:"dhcpv4"`
	Conf6 V6ServerConf `yaml:"dhcpv6"`

	WorkDir    string `yaml:"-"`
	DBFilePath string `yaml:"-"`
}

ServerConfig is the configuration for the DHCP server. The order of YAML fields is important, since the YAML configuration file follows it.

type V4ServerConf

type V4ServerConf struct {
	Enabled       bool   `yaml:"-" json:"-"`
	InterfaceName string `yaml:"-" json:"-"`

	GatewayIP  netip.Addr `yaml:"gateway_ip" json:"gateway_ip"`
	SubnetMask netip.Addr `yaml:"subnet_mask" json:"subnet_mask"`

	// The first & the last IP address for dynamic leases
	// Bytes [0..2] of the last allowed IP address must match the first IP
	RangeStart netip.Addr `yaml:"range_start" json:"range_start"`
	RangeEnd   netip.Addr `yaml:"range_end" json:"range_end"`

	LeaseDuration uint32 `yaml:"lease_duration" json:"lease_duration"` // in seconds

	// IP conflict detector: time (ms) to wait for ICMP reply
	// 0: disable
	ICMPTimeout uint32 `yaml:"icmp_timeout_msec" json:"-"`

	// Custom Options.
	//
	// Option with arbitrary hexadecimal data:
	//     DEC_CODE hex HEX_DATA
	// where DEC_CODE is a decimal DHCPv4 option code in range [1..255]
	//
	// Option with IP data (only 1 IP is supported):
	//     DEC_CODE ip IP_ADDR
	Options []string `yaml:"options" json:"-"`
	// contains filtered or unexported fields
}

V4ServerConf - server configuration

func (*V4ServerConf) Validate added in v0.107.13

func (c *V4ServerConf) Validate() (err error)

Validate returns an error if c is not a valid configuration.

TODO(e.burkov): Don't set the config fields when the server itself will stop containing the config.

type V6ServerConf

type V6ServerConf struct {
	Enabled       bool   `yaml:"-" json:"-"`
	InterfaceName string `yaml:"-" json:"-"`

	// The first IP address for dynamic leases
	// The last allowed IP address ends with 0xff byte
	RangeStart net.IP `yaml:"range_start" json:"range_start"`

	LeaseDuration uint32 `yaml:"lease_duration" json:"lease_duration"` // in seconds

	RASLAACOnly  bool `yaml:"ra_slaac_only" json:"-"`  // send ICMPv6.RA packets without MO flags
	RAAllowSLAAC bool `yaml:"ra_allow_slaac" json:"-"` // send ICMPv6.RA packets with MO flags
	// contains filtered or unexported fields
}

V6ServerConf - server configuration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL