dhcplb

package
v0.0.0-...-91e9af5 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2017 License: BSD-3-Clause Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrUnknown  = "E_UNKNOWN"
	ErrPanic    = "E_PANIC"
	ErrRead     = "E_READ"
	ErrConnect  = "E_CONN"
	ErrWrite    = "E_WRITE"
	ErrGi0      = "E_GI_0"
	ErrParse    = "E_PARSE"
	ErrNoServer = "E_NO_SERVER"
	ErrConnRate = "E_CONN_RATE"
)

List of possible errors.

Variables

This section is empty.

Functions

func FormatID

func FormatID(buf []byte) string

FormatID takes a list of bytes and formats them for printing. E.g. []byte{0x12, 0x34, 0x56, 0x78, 0x9a} will be printed as "12:34:56:78:9a"

Types

type Config

type Config struct {
	Version              int
	Addr                 *net.UDPAddr
	Algorithm            dhcpBalancingAlgorithm
	ServerUpdateInterval time.Duration
	PacketBufSize        int
	HostSourcer          DHCPServerSourcer
	FreeConnTimeout      time.Duration
	RCRatio              uint32
	Overrides            map[string]Override
	Extras               interface{}
	TCacheSize           int
	TCacheRate           int
	TRate                int
}

Config represents the server configuration.

func LoadConfig

func LoadConfig(path, overridesPath string, version int, provider ConfigProvider) (*Config, error)

LoadConfig will take the path of the json file, the path of the override json file, an integer version and a ConfigProvider and will return a pointer to a Config object.

func ParseConfig

func ParseConfig(jsonConfig, jsonOverrides []byte, version int, provider ConfigProvider) (*Config, error)

ParseConfig will take JSON config files, a version and a ConfigProvider, and return a pointer to a Config struct

type ConfigBroadcaster

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

ConfigBroadcaster is a convenience struct that broadcasts its input channel to a list of receivers.

func NewConfigBroadcaster

func NewConfigBroadcaster(input <-chan *Config) *ConfigBroadcaster

func WatchConfig

func WatchConfig(
	configPath, overridesPath string, version int, provider ConfigProvider,
) (*ConfigBroadcaster, chan error, error)

WatchConfig will keep watching for changes to both config and override json files. It uses fsnotify library (it uses inotify in Linux), and call LoadConfig when it an inotify event signals the modification of the json files. It returns a configBroadcaster which the a goroutine in the main will use to reload the configuration when it changes.

func (*ConfigBroadcaster) NewReceiver

func (c *ConfigBroadcaster) NewReceiver() <-chan *Config

NewReceiver allows the caller to register to receive new Config messages when the server config changes. This is typically used by a main go routine to reload the server configuration.

type ConfigProvider

type ConfigProvider interface {
	NewHostSourcer(
		sourcerType, args string, version int) (DHCPServerSourcer, error)
	ParseExtras(extras json.RawMessage) (interface{}, error)
}

ConfigProvider is an interface which provides methods to fetch the HostSourcer and parse extra configuration.

type DHCPMessage

type DHCPMessage struct {
	XID      uint32
	Peer     *net.UDPAddr
	ClientID []byte
	Mac      []byte
}

DHCPMessage represents coordinates of a dhcp message.

type DHCPServer

type DHCPServer struct {
	Hostname string
	Address  net.IP
	Port     int

	IsRC bool
	// contains filtered or unexported fields
}

DHCPServer holds information about a single dhcp server

func NewDHCPServer

func NewDHCPServer(hostname string, ip net.IP, port int) *DHCPServer

NewDHCPServer returns an instance of DHCPServer

func (*DHCPServer) String

func (d *DHCPServer) String() string

type DHCPServerSourcer

type DHCPServerSourcer interface {
	GetStableServers() ([]*DHCPServer, error)
	GetRCServers() ([]*DHCPServer, error)
	GetServersFromTier(tier string) ([]*DHCPServer, error)
}

DHCPServerSourcer is an interface used to fetch stable, rc and servers from a "tier" (group of servers).

type DuidType

type DuidType uint16

DuidType is a uint16 integer, there can be 3 of them, see the enum below.

const (
	DuidLLT DuidType = iota + 1
	DuidEN
	DuidLL
	DuidUUID
)

there are 3 types of DUIDs

type FileSourcer

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

FileSourcer holds various information about json the config files, list of stable and rc servers, the fsnotify Watcher and stuff needed for synchronization.

func NewFileSourcer

func NewFileSourcer(stablePath, rcPath string, version int) (*FileSourcer, error)

NewFileSourcer returns a new FileSourcer, stablePath and rcPath are the paths of the text files containing list of servers. If rcPath is empty it will be ignored, stablePath must be not null, version is the protocol version and should be either 4 or 6.

func (*FileSourcer) GetRCServers

func (fs *FileSourcer) GetRCServers() ([]*DHCPServer, error)

GetRCServers returns a list of rc dhcp servers

func (*FileSourcer) GetServersFromTier

func (fs *FileSourcer) GetServersFromTier(path string) ([]*DHCPServer, error)

GetServersFromTier returns a list of DHCPServer from a file

func (*FileSourcer) GetStableServers

func (fs *FileSourcer) GetStableServers() ([]*DHCPServer, error)

GetStableServers returns a list of stable dhcp servers

type LogMessage

type LogMessage struct {
	Version      int
	Packet       []byte
	Peer         *net.UDPAddr
	Server       string
	ServerIsRC   bool
	Latency      time.Duration
	Success      bool
	ErrorName    string
	ErrorDetails error
}

LogMessage holds the info of a log line.

type MessageType

type MessageType byte

MessageType represents various dhcpv6 message types go:generate stringer -type=MessageType packet6.go

const (
	Solicit MessageType = iota + 1
	Request
	Confirm
	Renew
	Rebind
	Reply
	Release
	Decline
	Reconfigure
	InformationRequest
	RelayForw
	RelayRepl
)

Various message types for DHCPv6

func (MessageType) String

func (i MessageType) String() string

type OptionType

type OptionType uint16

OptionType represents various dhcpv6 option types

const (
	ClientID OptionType = iota + 1
	ServerID
	IdentAssocNonTempAddr
	IdentAssocTempAddr
	IaAddr
	OptionRequest
	Preference
	ElapsedTime
	RelayMessage
	Auth OptionType = iota + 2
	ServerUnicast
	StatusCode
	RapidCommit
	UserClass
	VendorClass
	VendorOpts
	InterfaceID
	ReconfigureMessage
	ReconfigureAccept
)

List of Option Types

func (OptionType) String

func (i OptionType) String() string

type Override

type Override struct {
	// note that Host override takes precedence over Tier
	Host       string `json:"host"`
	Tier       string `json:"tier"`
	Expiration string `json:"expiration"`
}

Override represents the dhcp server or the group of dhcp servers (tier) we want to send packets to.

type Overrides

type Overrides struct {
	V4 map[string]Override `json:"v4"`
	V6 map[string]Override `json:"v6"`
}

Overrides is a struct that holds v4 and v6 list of overrides. The keys of the map are mac addresses.

type Packet6

type Packet6 []byte

Packet6 contains raw bytes for a dhcpv6 packet

func (Packet6) Duid

func (p Packet6) Duid() ([]byte, error)

Duid returns the DUID field in Packet6

func (Packet6) DuidTypeName

func (p Packet6) DuidTypeName() (string, error)

func (Packet6) Encapsulate

func (p Packet6) Encapsulate(peer net.IP) Packet6

Encapsulate embeds this message in a relay-forward message in preparation for forwarding to a relay/server

func (Packet6) GetInnerMostPeerAddr

func (p Packet6) GetInnerMostPeerAddr() (net.IP, error)

GetInnerMostPeerAddress returns the peer address in the inner most relay info header, this is typically the mac address of the relay closer to the dhcp client making the request.

func (Packet6) Hops

func (p Packet6) Hops() (byte, error)

Hops returns the number of hops for a Packet6

func (Packet6) LinkAddr

func (p Packet6) LinkAddr() (net.IP, error)

LinkAddr returns the LinkAddr field in the RelayInfo header. Will return error if the message is not a RelayForw or RelayRepl.

func (Packet6) Mac

func (p Packet6) Mac() ([]byte, error)

Mac returns the Mac addressed embededded in the DUID, note that this only works with type DuidLL and DuidLLT. If the request is not using DUID-LL[T] then we look into the PeerAddr field in the RelayInfo header. This contains the EUI-64 address of the client making the request, populated by the dhcp relay, it is possible to extract the mac address from that IP. If a mac address cannot be fonud an error will be returned.

func (Packet6) PeerAddr

func (p Packet6) PeerAddr() (net.IP, error)

PeerAddr returns the PeerAddr field in the RelayInfo header. Will return error if the message is not a RelayForw or RelayRepl.

func (Packet6) Type

func (p Packet6) Type() (MessageType, error)

Type returns the MessageType for a Packet6

func (Packet6) Unwind

func (p Packet6) Unwind() (Packet6, net.IP, error)

Unwind strips off the relay-reply message from the outer Packet6 (p) to prepare for forwarding back to the client.

func (Packet6) XID

func (p Packet6) XID() (uint32, error)

XID returns the Transaction ID for a Packet6

type PersonalizedLogger

type PersonalizedLogger interface {
	Log(msg LogMessage) error
}

PersonalizedLogger is an interface used to log a LogMessage using your own logic. It will be used in loggerHelperImpl.

type Server

type Server interface {
	SetConfig(config *Config)
	ListenAndServe() error
	HasServers() bool
}

Server is the main interface implementing the DHCP server.

func NewServer

func NewServer(config *Config, version int, personalizedLogger PersonalizedLogger) (Server, error)

NewServer initialized a Server before returning it.

type Throttle

type Throttle interface {
	// Returns whether the rate is below max for a key
	OK(interface{}) (bool, error)
	// contains filtered or unexported methods
}

Throttle is interface that implements rate limiting per key

func NewThrottle

func NewThrottle(Capacity int, CacheRate int, MaxRatePerItem int) (Throttle, error)

NewThrottle returns a Throttle struct

Capacity:
    Maximum capacity of the LRU cache

CacheRate (per second):
    Maximum allowed rate for adding new items to the cache. By that way it
    prevents the cache invalidation to happen too soon for the existing rate
    items in the cache. Cache rate will be infinite for 0 or negative values.

MaxRatePerItem (per second):
    Maximum allowed requests rate for each key in the cache. Throttling will
    be disabled for 0 or negative values. No cache will be created in that case.

Jump to

Keyboard shortcuts

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