dhcpd

package
v0.105.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2021 License: GPL-3.0 Imports: 31 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 (
	LeaseChangedAdded = iota
	LeaseChangedAddedStatic
	LeaseChangedRemovedStatic

	LeaseChangedDBStore
)

flags for onLeaseChanged()

View Source
const (
	LeasesDynamic = 1
	LeasesStatic  = 2
	LeasesAll     = LeasesDynamic | LeasesStatic
)

flags for Leases() function

Variables

This section is empty.

Functions

func CheckIfOtherDHCPServersPresentV4

func CheckIfOtherDHCPServersPresentV4(ifaceName string) (bool, error)

CheckIfOtherDHCPServersPresentV4 sends a DHCP request to the specified network interface, and waits for a response for a period defined by defaultDiscoverTime

func CheckIfOtherDHCPServersPresentV6

func CheckIfOtherDHCPServersPresentV6(ifaceName string) (bool, error)

CheckIfOtherDHCPServersPresentV6 sends a DHCP request to the specified network interface, and waits for a response for a period defined by defaultDiscoverTime

Types

type DHCPServer

type DHCPServer interface {
	// ResetLeases - reset leases
	ResetLeases(leases []*Lease)
	// GetLeases - get leases
	GetLeases(flags int) []Lease
	// GetLeasesRef - get reference to leases array
	GetLeasesRef() []*Lease
	// AddStaticLease - add a static lease
	AddStaticLease(lease Lease) error
	// RemoveStaticLease - remove a static lease
	RemoveStaticLease(l Lease) 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() error
	// Stop - stop server
	Stop()
}

DHCPServer - DHCP server interface

type Lease

type Lease struct {
	HWAddr   net.HardwareAddr `json:"mac"`
	IP       net.IP           `json:"ip"`
	Hostname string           `json:"hostname"`

	// Lease expiration time
	// 1: static lease
	Expiry time.Time `json:"expires"`
}

Lease contains the necessary information about a DHCP lease

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 OnLeaseChangedT

type OnLeaseChangedT func(flags int)

OnLeaseChangedT is a callback for lease changes.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server - the current state of the DHCP server

func Create

func Create(config ServerConfig) *Server

Create - create object

func (*Server) AddStaticLease

func (s *Server) AddStaticLease(lease Lease) error

AddStaticLease - add static v4 lease

func (*Server) FindMACbyIP

func (s *Server) FindMACbyIP(ip net.IP) net.HardwareAddr

FindMACbyIP - find a MAC address by IP address in the currently active DHCP leases

func (*Server) Leases

func (s *Server) Leases(flags int) []Lease

Leases returns the list of current DHCP leases (thread-safe)

func (*Server) SetOnLeaseChanged

func (s *Server) SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT)

SetOnLeaseChanged - set callback

func (*Server) Start

func (s *Server) Start() error

Start will listen on port 67 and serve DHCP requests.

func (*Server) Stop

func (s *Server) Stop()

Stop closes the listening UDP socket

func (*Server) WriteDiskConfig

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

WriteDiskConfig - write configuration

type ServerConfig

type ServerConfig struct {
	Enabled       bool   `yaml:"enabled"`
	InterfaceName string `yaml:"interface_name"`

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

	WorkDir    string `yaml:"-"`
	DBFilePath string `yaml:"-"` // path to DB file

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

	// Register an HTTP handler
	HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `yaml:"-"`
}

ServerConfig - DHCP server configuration field ordering is important -- yaml fields will mirror ordering from here

type ServerInterface

type ServerInterface interface {
	Leases(flags int) []Lease
	SetOnLeaseChanged(onLeaseChanged OnLeaseChangedT)
}

ServerInterface is an interface for servers.

type V4ServerConf

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

	GatewayIP  net.IP `yaml:"gateway_ip" json:"gateway_ip"`
	SubnetMask net.IP `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 net.IP `yaml:"range_start" json:"range_start"`
	RangeEnd   net.IP `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

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"`

	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

Directories

Path Synopsis
Package nclient4 is a small, minimum-functionality client for DHCPv4.
Package nclient4 is a small, minimum-functionality client for DHCPv4.

Jump to

Keyboard shortcuts

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