dhcp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2016 License: BSD-3-Clause Imports: 15 Imported by: 0

README

PG-DHCP

GoDoc GitHub issues GitHub stars GitHub license

This is the DHCP server package backing the Packet Guardian captive portal. It has been separated into it's own repository to make development a bit easier, and to provide a better focus to the origin project. This package may be used completely independent of Packet Guardian.

Features:

  • RFC2131 DHCP protocol
  • The most used options are implement, more to come
  • Seperation of registered vs unregistered devices (known/unknown)
  • Storage independent (the calling project is responsible for storage)

Configuration File Format

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRegisteredByIP

func IsRegisteredByIP(s LeaseStore, ip net.IP) (bool, error)

IsRegisteredByIP checks if an IP is leased to a registered MAC address. It will return false if an error occurs as well as the error itself.

Types

type Config

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

A Config is the parsed object generated from a PG-DHCP configuration file.

func ParseFile

func ParseFile(path string) (*Config, error)

ParseFile takes the file name to a configuration file. It will attempt to parse the file using the PG-DHCP configuration format. If an error occures config will be nil.

type Device

type Device interface {
	SetLastSeen(time.Time)
	GetID() int
	GetMAC() net.HardwareAddr
	GetUsername() string
	IsBlacklisted() bool
	IsExpired() bool
	IsRegistered() bool
	Save() error
}

type DeviceStore

type DeviceStore interface {
	GetDeviceByMAC(net.HardwareAddr) (Device, error)
}

type Environment

type Environment string
const (
	EnvTesting Environment = "testing"
	EnvDev     Environment = "dev"
	EnvProd    Environment = "prod"
)

type Handler

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

A Handler processes all incoming DHCP packets.

func NewDHCPServer

func NewDHCPServer(conf *Config, s *ServerConfig) *Handler

NewDHCPServer creates and sets up a new DHCP Handler with the give configuration.

func (*Handler) GetPoolStats

func (h *Handler) GetPoolStats() []*PoolStat

func (*Handler) ListenAndServe

func (h *Handler) ListenAndServe() error

ListenAndServe starts the DHCP Handler listening on port 67 for packets. This is blocking like HTTP's ListenAndServe method.

func (*Handler) LoadLeases

func (h *Handler) LoadLeases() error

LoadLeases will import any current leases saved to the database.

func (*Handler) Readonly

func (h *Handler) Readonly()

Readonly will force the DHCP Handler into a mode where it will process a request as usual but will never return anything to the client. This can be used to perhaps test if traffic is getting to the server without actually doing anything.

func (*Handler) Respond

func (h *Handler) Respond()

Respond disables readonly mode.

func (*Handler) ServeDHCP

func (h *Handler) ServeDHCP(p dhcp4.Packet, msgType dhcp4.MessageType, options dhcp4.Options) dhcp4.Packet

ServeDHCP processes an incoming DHCP packet and returns a response.

type Lease

type Lease struct {
	ID          int
	IP          net.IP
	MAC         net.HardwareAddr
	Network     string
	Start       time.Time
	End         time.Time
	Hostname    string
	IsAbandoned bool
	Offered     bool
	Registered  bool
	// contains filtered or unexported fields
}

A Lease represents a single DHCP lease in a pool. It is bound to a particular pool and network.

func GetAllLeases

func GetAllLeases(s LeaseStore) ([]*Lease, error)

GetAllLeases will return a slice of all leases in the database.

func GetAllLeasesByMAC

func GetAllLeasesByMAC(s LeaseStore, mac net.HardwareAddr) ([]*Lease, error)

GetAllLeasesByMAC returns a slice of Lease given the mac address. If no leases exist, the slice will be nil.

func GetLeaseByIP

func GetLeaseByIP(s LeaseStore, ip net.IP) (*Lease, error)

GetLeaseByIP returns a Lease given the IP address. This method will always return a Lease. Make sure to check if error is nil. If a new lease object was created it will have an ID = 0.

func GetLeaseByMAC

func GetLeaseByMAC(s LeaseStore, mac net.HardwareAddr) (*Lease, error)

GetLeaseByMAC returns a Lease given the mac address. This method will always return a Lease. Make sure to check if error is nil. If a new lease object was created it will have an ID = 0. The lease returned will be the most recent least given to the provided MAC address.

func NewLease

func NewLease(s LeaseStore) *Lease

func SearchLeases

func SearchLeases(s LeaseStore, where string, vals ...interface{}) ([]*Lease, error)

func (*Lease) Delete

func (l *Lease) Delete() error

func (*Lease) IsExpired

func (l *Lease) IsExpired() bool

func (*Lease) IsFree

func (l *Lease) IsFree() bool

IsFree determines if the lease is expired and available for use

func (*Lease) Save

func (l *Lease) Save() error

type LeaseStore

type LeaseStore interface {
	GetAllLeases() ([]*Lease, error)
	GetLeaseByIP(net.IP) (*Lease, error)

	GetRecentLeaseByMAC(net.HardwareAddr) (*Lease, error)
	GetAllLeasesByMAC(net.HardwareAddr) ([]*Lease, error)

	CreateLease(*Lease) error
	UpdateLease(*Lease) error
	DeleteLease(*Lease) error

	SearchLeases(string, ...interface{}) ([]*Lease, error)
}

type PoolStat

type PoolStat struct {
	NetworkName, Subnet                     string
	Start                                   string
	End                                     string
	Registered                              bool
	Total, Active, Claimed, Abandoned, Free int
}

type ServerConfig

type ServerConfig struct {
	LeaseStore  LeaseStore
	DeviceStore DeviceStore
	Log         *verbose.Logger
	LogPath     string
	Env         Environment
}

func (*ServerConfig) IsDev

func (s *ServerConfig) IsDev() bool

func (*ServerConfig) IsProd

func (s *ServerConfig) IsProd() bool

func (*ServerConfig) IsTesting

func (s *ServerConfig) IsTesting() bool

Jump to

Keyboard shortcuts

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