dhcp

package
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2021 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllConnectionTypes = []string{
	UDP.String(),
	Dual.String(),
	Fritzbox.String(),
	Packet.String(),
	Broken.String(),
}

AllConnectionTypes is a list of all possible connection types

Functions

func GenerateXID

func GenerateXID() uint32

GenerateXID generates a random uint32 value

Types

type Connection

type Connection interface {
	Close() error
	Send(ctx context.Context, dhcp *DHCP4) (chan int, chan error)
	Receive(ctx context.Context) (chan *DHCP4, chan error)
	Local() *net.UDPAddr
	Remote() *net.UDPAddr

	Block(ctx context.Context) chan bool
}

Connection is an interface for a DHCP connection

func NewDualConn

func NewDualConn(local *net.UDPAddr, remote *net.UDPAddr, fixPort bool, out net.PacketConn, in UDPPacketConn, logger logger.Logger) Connection

NewDualConn initializes a new connection

func NewRawConn

func NewRawConn(local *net.UDPAddr, remote *net.UDPAddr, conn net.PacketConn, logger logger.Logger) Connection

NewRawConn initializes a new connection

func NewUDPConn

func NewUDPConn(local *net.UDPAddr, remote *net.UDPAddr, conn UDPPacketConn, logger logger.Logger) Connection

NewUDPConn initializes a new udp connection

type ConnectionResolver added in v1.2.0

type ConnectionResolver interface {
	GetConnection(local net.IP, remote net.IP, t ConnectionType, logger logger.Logger) Connection
}

type ConnectionType

type ConnectionType string

ConnectionType is enumeration of teh connection types

const (
	AutoDetect ConnectionType = "auto"
	UDP        ConnectionType = "udp"
	Dual       ConnectionType = "dual"
	Fritzbox   ConnectionType = "fritzbox"
	Broken     ConnectionType = "broken"
	Packet     ConnectionType = "packet"
)

Existing connection types

func (ConnectionType) MarshalJSON

func (c ConnectionType) MarshalJSON() ([]byte, error)

MarshalJSON custom marshal for JSON

func (ConnectionType) MarshalXML

func (c ConnectionType) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML custom marshal for XML

func (ConnectionType) MarshalYAML

func (c ConnectionType) MarshalYAML() (interface{}, error)

func (*ConnectionType) Parse

func (c *ConnectionType) Parse(txt string) error

Parse a string

func (ConnectionType) String

func (c ConnectionType) String() string

func (*ConnectionType) UnmarshalJSON

func (c *ConnectionType) UnmarshalJSON(b []byte) error

UnmarshalJSON custom unmarshal for JSON

func (*ConnectionType) UnmarshalXML

func (c *ConnectionType) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML custom unmarshal for XML

func (*ConnectionType) UnmarshalYAML

func (c *ConnectionType) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML custom unmarshal for YAMLv3

type DHCP4

type DHCP4 struct {
	*layers.DHCPv4
}

DHCP4 extends layers.DHCPv4 with helper functions

func NewPackage

func NewPackage(msgType layers.DHCPMsgType, xid uint32, chaddr net.HardwareAddr, options layers.DHCPOptions) *DHCP4

NewPackage creates a DHCPv4 package

func (*DHCP4) GetDNS

func (d *DHCP4) GetDNS() net.IP

GetDNS return the option layers.DHCPOptDNS

func (*DHCP4) GetMsgType

func (d *DHCP4) GetMsgType() layers.DHCPMsgType

GetMsgType returns the DHCP message type option

func (*DHCP4) GetOption

func (d *DHCP4) GetOption(option layers.DHCPOpt) *layers.DHCPOption

GetOption returns a requested DHCP option

func (*DHCP4) GetRouter

func (d *DHCP4) GetRouter() net.IP

GetRouter return the option layers.DHCPOptRouter

func (*DHCP4) GetSubnetMask

func (d *DHCP4) GetSubnetMask() net.IP

GetSubnetMask return the option layers.DHCPOptSubnetMask

func (*DHCP4) SetHostname

func (d *DHCP4) SetHostname(hostname string)

SetHostname sets the DHCP option layers.DHCPOptHostname

func (*DHCP4) SetMsgType

func (d *DHCP4) SetMsgType(msgType layers.DHCPMsgType)

SetMsgType sets the DHCP message type option

func (*DHCP4) SetOption

func (d *DHCP4) SetOption(opt layers.DHCPOpt, data []byte)

SetOption sets a DHCP option

func (*DHCP4) ToJSON added in v1.2.0

func (d *DHCP4) ToJSON() []byte

ToJSON converts the dhcp block to JSON

func (*DHCP4) ToYAML added in v1.2.0

func (d *DHCP4) ToYAML() []byte

ToYAML converts the dhcp block to YAML

type DHCPClient added in v1.2.0

type DHCPClient interface {
	Start() chan bool
	Stop()

	GetLease(ctx context.Context, hostname string, chaddr net.HardwareAddr) chan *Lease
	Renew(ctx context.Context, hostname string, chaddr net.HardwareAddr, ip net.IP) chan *Lease
	Release(ctx context.Context, hostname string, chaddr net.HardwareAddr, ip net.IP) chan error
}

func NewClient

func NewClient(resolver IPResolver, connResolver ConnectionResolver, connType ConnectionType, timeout time.Duration, retry time.Duration, logger logger.Logger) DHCPClient

NewClient initialize a new client

type DefaultConnectioneResolver added in v1.2.0

type DefaultConnectioneResolver struct {
}

func NewDefaultConnectioneResolver added in v1.2.0

func NewDefaultConnectioneResolver() *DefaultConnectioneResolver

func (*DefaultConnectioneResolver) GetConnection added in v1.2.0

func (r *DefaultConnectioneResolver) GetConnection(local net.IP, remote net.IP, t ConnectionType, logger logger.Logger) Connection

type DualConn

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

DualConn is a udp listener on a local port and a packet connection to use free src port for outgoing messages

func (*DualConn) Block

func (c *DualConn) Block(ctx context.Context) chan bool

Block outgoing traffic until context is finished

func (*DualConn) Close

func (c *DualConn) Close() error

Close the connection

func (*DualConn) GetPort added in v1.2.0

func (c *DualConn) GetPort() layers.UDPPort

func (*DualConn) Local

func (c *DualConn) Local() *net.UDPAddr

Local returns the local udp address

func (*DualConn) Receive

func (c *DualConn) Receive(ctx context.Context) (chan *DHCP4, chan error)

Receive a DHCP data packet

func (*DualConn) Remote

func (c *DualConn) Remote() *net.UDPAddr

Remote returns the remote udp address

func (*DualConn) Send

func (c *DualConn) Send(ctx context.Context, dhcp *DHCP4) (chan int, chan error)

Send a DHCP data packet

type IPResolver added in v1.2.0

type IPResolver interface {
	GetRelayIP(ctx context.Context) (net.IP, error)
	GetLocalIP(remote net.IP) (net.IP, error)
	GetServerIP() (net.IP, error)
}

type KubeServiceConfig added in v1.2.0

type KubeServiceConfig struct {
	Config    string `yaml:"config,omitempty" json:"config,omitempty" xml:"config,omitempty"`
	Namespace string `yaml:"namespace,omitempty" json:"namespace,omitempty" xml:"namespace,omitempty"`
	Service   string `yaml:"service,omitempty" json:"service,omitempty" xml:"service,omitempty"`
}

KubeServiceConfig is the optional configuration to read the relay ip

func (*KubeServiceConfig) String added in v1.2.0

func (c *KubeServiceConfig) String() string

type KubernetesExternalIPResolver added in v1.2.0

type KubernetesExternalIPResolver struct {
	LocalIPResolver
	// contains filtered or unexported fields
}

func NewKubernetesExternalIPResolver added in v1.2.0

func NewKubernetesExternalIPResolver(local net.IP, remote net.IP, config *KubeServiceConfig, client kubernetes.KubeClient, logger logger.Logger) *KubernetesExternalIPResolver

func (*KubernetesExternalIPResolver) GetRelayIP added in v1.2.0

func (r *KubernetesExternalIPResolver) GetRelayIP(ctx context.Context) (net.IP, error)

type Lease

type Lease struct {
	*DHCP4
	Timestamp time.Time
	Hostname  string
	Done      chan bool
	// contains filtered or unexported fields
}

Lease extends DHCP4 with extra fields and an error

func NewLease

func NewLease(msgType layers.DHCPMsgType, xid uint32, chaddr net.HardwareAddr, options layers.DHCPOptions) *Lease

NewLease creates a lease object

func NewLeaseError

func NewLeaseError(err error) *Lease

NewLeaseError creates an empty lease with an error

func (*Lease) CheckResponseType

func (l *Lease) CheckResponseType(dhcp *DHCP4) bool

CheckResponseType checks if a new status is allowed

func (*Lease) Error

func (l *Lease) Error() error

func (*Lease) GetExpireTime added in v1.2.0

func (l *Lease) GetExpireTime() time.Time

GetExpireTime return the option layers.DHCPOptLeaseTime

func (*Lease) GetRebindTime added in v1.2.0

func (l *Lease) GetRebindTime() time.Time

GetRebindTime return the option layers.DHCPOptT2

func (*Lease) GetRenewalTime added in v1.2.0

func (l *Lease) GetRenewalTime() time.Time

GetRenewalTime return the option layers.DHCPOptT1

func (*Lease) GetRequest

func (l *Lease) GetRequest(msgType layers.DHCPMsgType, options layers.DHCPOptions) *Lease

GetRequest creates a DHCP request out of an lease

func (*Lease) Lock added in v1.2.0

func (l *Lease) Lock()

func (*Lease) Ok

func (l *Lease) Ok() bool

Ok returns false if there is an error

func (*Lease) SetError

func (l *Lease) SetError(err error)

SetError adds an error

func (*Lease) SetHostname

func (l *Lease) SetHostname(hostname string)

SetHostname sets the DHCP option layers.DHCPOptHostname and the hostname field

func (*Lease) Touch

func (l *Lease) Touch()

Touch updates the timestamp

func (*Lease) Unlock added in v1.2.0

func (l *Lease) Unlock()

type LeaseStore

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

LeaseStore is thread-safe store for temporary DHCP lease packets with a TTL

func NewStore

func NewStore(ttl time.Duration, logger logger.Logger) *LeaseStore

NewStore creates a new lease store

func (*LeaseStore) Clean

func (l *LeaseStore) Clean()

Clean removes outdated entries from the store

func (*LeaseStore) Get

func (l *LeaseStore) Get(xid uint32) (*Lease, bool, context.CancelFunc)

Get a value from the store

func (*LeaseStore) GetItem added in v1.2.0

func (l *LeaseStore) GetItem(xid uint32) (*LeaseStoreItem, bool)

GetItem returns an item from the store

func (*LeaseStore) Has

func (l *LeaseStore) Has(xid uint32) bool

Has checks if a xid is in the store

func (*LeaseStore) HasStatus added in v1.2.0

func (l *LeaseStore) HasStatus(xid uint32, status layers.DHCPMsgType) bool

Touch extends the expire time of an entry

func (*LeaseStore) Remove

func (l *LeaseStore) Remove(xid uint32) bool

Remove a value from the store

func (*LeaseStore) Run

func (l *LeaseStore) Run(ctx context.Context)

Run the process to clean the store periodically

func (*LeaseStore) Set

func (l *LeaseStore) Set(lease *Lease) error

Set or add a lease to the store

func (*LeaseStore) Touch

func (l *LeaseStore) Touch(xid uint32) bool

Touch extends the expire time of an entry

type LeaseStoreItem

type LeaseStoreItem struct {
	Lease  *Lease
	Expire time.Time
	Status layers.DHCPMsgType
}

LeaseStoreItem describes a lease store item

type LocalIPResolver added in v1.2.0

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

func (*LocalIPResolver) GetLocalIP added in v1.2.0

func (l *LocalIPResolver) GetLocalIP(remote net.IP) (net.IP, error)

func (*LocalIPResolver) GetServerIP added in v1.2.0

func (l *LocalIPResolver) GetServerIP() (net.IP, error)

type RawConn

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

RawConn is a packet connection between local and remote

func (*RawConn) Block

func (c *RawConn) Block(ctx context.Context) chan bool

Block outgoing traffic until context is finished

func (*RawConn) Close

func (c *RawConn) Close() error

Close the connection

func (*RawConn) Local

func (c *RawConn) Local() *net.UDPAddr

Local returns the local udp address

func (*RawConn) Receive

func (c *RawConn) Receive(ctx context.Context) (chan *DHCP4, chan error)

Receive a DHCP data packet

func (*RawConn) Remote

func (c *RawConn) Remote() *net.UDPAddr

Remote returns the remote udp address

func (*RawConn) Send

func (c *RawConn) Send(ctx context.Context, dhcp *DHCP4) (chan int, chan error)

Send a DHCP data packet

type StaticIPResolver added in v1.2.0

type StaticIPResolver struct {
	LocalIPResolver
	// contains filtered or unexported fields
}

func NewStaticIPResolver added in v1.2.0

func NewStaticIPResolver(local net.IP, remote net.IP, relay net.IP, logger logger.Logger) *StaticIPResolver

func (*StaticIPResolver) GetRelayIP added in v1.2.0

func (r *StaticIPResolver) GetRelayIP(ctx context.Context) (net.IP, error)

type UDPConn

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

UDPConn is a simple upd connection using the same src and destination port

func (*UDPConn) Block

func (c *UDPConn) Block(ctx context.Context) chan bool

Block outgoing traffic until context is finished

func (*UDPConn) Close

func (c *UDPConn) Close() error

Close the connection

func (*UDPConn) Local

func (c *UDPConn) Local() *net.UDPAddr

Local returns the local udp address

func (*UDPConn) Receive

func (c *UDPConn) Receive(ctx context.Context) (chan *DHCP4, chan error)

Receive a DHCP data packet

func (*UDPConn) Remote

func (c *UDPConn) Remote() *net.UDPAddr

Remote returns the local udp address

func (*UDPConn) Send

func (c *UDPConn) Send(ctx context.Context, dhcp *DHCP4) (chan int, chan error)

Send a DHCP data packet

type UDPPacketConn added in v1.2.0

type UDPPacketConn interface {
	net.PacketConn
	net.Conn
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	ReadFromUDP(b []byte) (int, *net.UDPAddr, error)
}

Jump to

Keyboard shortcuts

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