chore: Refactor package structure
This commit is contained in:
83
pkg/leases/leases.go
Normal file
83
pkg/leases/leases.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package leases
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/adrianokf/go-dhcp/pkg/types"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type LeaseState uint8
|
||||
|
||||
const (
|
||||
Offered LeaseState = iota
|
||||
Requested
|
||||
)
|
||||
|
||||
type Lease struct {
|
||||
TransactionId types.TxId
|
||||
TTL time.Time
|
||||
State LeaseState
|
||||
ClientAddr types.HwAddr
|
||||
Address types.Ipv4Addr
|
||||
}
|
||||
|
||||
type ILeaseManager interface {
|
||||
Request(xid types.TxId, clientAddr types.HwAddr) (*Lease, error)
|
||||
Release(l Lease) error
|
||||
Lookup(xid types.TxId) error
|
||||
}
|
||||
|
||||
type LeaseManager struct {
|
||||
leases map[types.TxId]Lease
|
||||
count byte
|
||||
}
|
||||
|
||||
func NewLeaseManager() *LeaseManager {
|
||||
m := &LeaseManager{
|
||||
leases: make(map[types.TxId]Lease),
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *LeaseManager) Request(xid types.TxId, clientAddr types.HwAddr) (*Lease, error) {
|
||||
zap.S().Debugf("LeaseManager.Request(%v, %v)", xid, clientAddr)
|
||||
|
||||
if m.count > 254 {
|
||||
return nil, errors.New("lease address pool exhausted")
|
||||
}
|
||||
|
||||
assigned := [4]byte{10, 0, 0, m.count + 2}
|
||||
lease := Lease{
|
||||
TransactionId: xid,
|
||||
TTL: time.Now().Add(1 * time.Hour),
|
||||
State: Requested,
|
||||
ClientAddr: clientAddr,
|
||||
Address: assigned,
|
||||
}
|
||||
m.leases[xid] = lease
|
||||
|
||||
m.count += 1
|
||||
|
||||
zap.S().Debug("lease=", lease)
|
||||
return &lease, nil
|
||||
}
|
||||
|
||||
func (m LeaseManager) Release(l Lease) error {
|
||||
_, found := m.leases[l.TransactionId]
|
||||
if !found {
|
||||
return fmt.Errorf("invalid lease %v", l)
|
||||
}
|
||||
delete(m.leases, l.TransactionId)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m LeaseManager) Lookup(xid types.TxId) (*Lease, error) {
|
||||
lease, found := m.leases[xid]
|
||||
if found {
|
||||
return &lease, nil
|
||||
}
|
||||
return nil, fmt.Errorf("no lease found for xid %v", xid)
|
||||
}
|
||||
235
pkg/messages/messages.go
Normal file
235
pkg/messages/messages.go
Normal file
@@ -0,0 +1,235 @@
|
||||
package messages
|
||||
|
||||
import (
|
||||
"github.com/adrianokf/go-dhcp/pkg/types"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Message interface {
|
||||
Marshal() ([]byte, error)
|
||||
Unmarshal([]byte) (Message, error)
|
||||
}
|
||||
|
||||
type OptionCode byte
|
||||
|
||||
// DHCPv4 Options
|
||||
const (
|
||||
OptionPad OptionCode = 0
|
||||
OptionSubnetMask OptionCode = 1
|
||||
OptionTimeOffset OptionCode = 2
|
||||
OptionRouter OptionCode = 3
|
||||
OptionTimeServer OptionCode = 4
|
||||
OptionNameServer OptionCode = 5
|
||||
OptionDomainNameServer OptionCode = 6
|
||||
OptionLogServer OptionCode = 7
|
||||
OptionQuoteServer OptionCode = 8
|
||||
OptionLPRServer OptionCode = 9
|
||||
OptionImpressServer OptionCode = 10
|
||||
OptionResourceLocationServer OptionCode = 11
|
||||
OptionHostName OptionCode = 12
|
||||
OptionBootFileSize OptionCode = 13
|
||||
OptionMeritDumpFile OptionCode = 14
|
||||
OptionDomainName OptionCode = 15
|
||||
OptionSwapServer OptionCode = 16
|
||||
OptionRootPath OptionCode = 17
|
||||
OptionExtensionsPath OptionCode = 18
|
||||
OptionIPForwarding OptionCode = 19
|
||||
OptionNonLocalSourceRouting OptionCode = 20
|
||||
OptionPolicyFilter OptionCode = 21
|
||||
OptionMaximumDatagramAssemblySize OptionCode = 22
|
||||
OptionDefaultIPTTL OptionCode = 23
|
||||
OptionPathMTUAgingTimeout OptionCode = 24
|
||||
OptionPathMTUPlateauTable OptionCode = 25
|
||||
OptionInterfaceMTU OptionCode = 26
|
||||
OptionAllSubnetsAreLocal OptionCode = 27
|
||||
OptionBroadcastAddress OptionCode = 28
|
||||
OptionPerformMaskDiscovery OptionCode = 29
|
||||
OptionMaskSupplier OptionCode = 30
|
||||
OptionPerformRouterDiscovery OptionCode = 31
|
||||
OptionRouterSolicitationAddress OptionCode = 32
|
||||
OptionStaticRoutingTable OptionCode = 33
|
||||
OptionTrailerEncapsulation OptionCode = 34
|
||||
OptionArpCacheTimeout OptionCode = 35
|
||||
OptionEthernetEncapsulation OptionCode = 36
|
||||
OptionDefaulTCPTTL OptionCode = 37
|
||||
OptionTCPKeepaliveInterval OptionCode = 38
|
||||
OptionTCPKeepaliveGarbage OptionCode = 39
|
||||
OptionNetworkInformationServiceDomain OptionCode = 40
|
||||
OptionNetworkInformationServers OptionCode = 41
|
||||
OptionNTPServers OptionCode = 42
|
||||
OptionVendorSpecificInformation OptionCode = 43
|
||||
OptionNetBIOSOverTCPIPNameServer OptionCode = 44
|
||||
OptionNetBIOSOverTCPIPDatagramDistributionServer OptionCode = 45
|
||||
OptionNetBIOSOverTCPIPNodeType OptionCode = 46
|
||||
OptionNetBIOSOverTCPIPScope OptionCode = 47
|
||||
OptionXWindowSystemFontServer OptionCode = 48
|
||||
OptionXWindowSystemDisplayManger OptionCode = 49
|
||||
OptionRequestedIPAddress OptionCode = 50
|
||||
OptionIPAddressLeaseTime OptionCode = 51
|
||||
OptionOptionOverload OptionCode = 52
|
||||
OptionDHCPMessageType OptionCode = 53
|
||||
OptionServerIdentifier OptionCode = 54
|
||||
OptionParameterRequestList OptionCode = 55
|
||||
OptionMessage OptionCode = 56
|
||||
OptionMaximumDHCPMessageSize OptionCode = 57
|
||||
OptionRenewTimeValue OptionCode = 58
|
||||
OptionRebindingTimeValue OptionCode = 59
|
||||
OptionClassIdentifier OptionCode = 60
|
||||
OptionClientIdentifier OptionCode = 61
|
||||
OptionNetWareIPDomainName OptionCode = 62
|
||||
OptionNetWareIPInformation OptionCode = 63
|
||||
OptionNetworkInformationServicePlusDomain OptionCode = 64
|
||||
OptionNetworkInformationServicePlusServers OptionCode = 65
|
||||
OptionTFTPServerName OptionCode = 66
|
||||
OptionBootfileName OptionCode = 67
|
||||
OptionMobileIPHomeAgent OptionCode = 68
|
||||
OptionSimpleMailTransportProtocolServer OptionCode = 69
|
||||
OptionPostOfficeProtocolServer OptionCode = 70
|
||||
OptionNetworkNewsTransportProtocolServer OptionCode = 71
|
||||
OptionDefaultWorldWideWebServer OptionCode = 72
|
||||
OptionDefaultFingerServer OptionCode = 73
|
||||
OptionDefaultInternetRelayChatServer OptionCode = 74
|
||||
OptionStreetTalkServer OptionCode = 75
|
||||
OptionStreetTalkDirectoryAssistanceServer OptionCode = 76
|
||||
OptionUserClassInformation OptionCode = 77
|
||||
OptionSLPDirectoryAgent OptionCode = 78
|
||||
OptionSLPServiceScope OptionCode = 79
|
||||
OptionRapidCommit OptionCode = 80
|
||||
OptionFQDN OptionCode = 81
|
||||
OptionRelayAgentInformation OptionCode = 82
|
||||
OptionInternetStorageNameService OptionCode = 83
|
||||
// Option 84 returned in RFC 3679
|
||||
OptionNDSServers OptionCode = 85
|
||||
OptionNDSTreeName OptionCode = 86
|
||||
OptionNDSContext OptionCode = 87
|
||||
OptionBCMCSControllerDomainNameList OptionCode = 88
|
||||
OptionBCMCSControllerIPv4AddressList OptionCode = 89
|
||||
OptionAuthentication OptionCode = 90
|
||||
OptionClientLastTransactionTime OptionCode = 91
|
||||
OptionAssociatedIP OptionCode = 92
|
||||
OptionClientSystemArchitectureType OptionCode = 93
|
||||
OptionClientNetworkInterfaceIdentifier OptionCode = 94
|
||||
OptionLDAP OptionCode = 95
|
||||
// Option 96 returned in RFC 3679
|
||||
OptionClientMachineIdentifier OptionCode = 97
|
||||
OptionOpenGroupUserAuthentication OptionCode = 98
|
||||
OptionGeoConfCivic OptionCode = 99
|
||||
OptionIEEE10031TZString OptionCode = 100
|
||||
OptionReferenceToTZDatabase OptionCode = 101
|
||||
// Options 102-111 returned in RFC 3679
|
||||
OptionNetInfoParentServerAddress OptionCode = 112
|
||||
OptionNetInfoParentServerTag OptionCode = 113
|
||||
OptionURL OptionCode = 114
|
||||
// Option 115 returned in RFC 3679
|
||||
OptionAutoConfigure OptionCode = 116
|
||||
OptionNameServiceSearch OptionCode = 117
|
||||
OptionSubnetSelection OptionCode = 118
|
||||
OptionDNSDomainSearchList OptionCode = 119
|
||||
OptionSIPServers OptionCode = 120
|
||||
OptionClasslessStaticRoute OptionCode = 121
|
||||
OptionCCC OptionCode = 122
|
||||
OptionGeoConf OptionCode = 123
|
||||
OptionVendorIdentifyingVendorClass OptionCode = 124
|
||||
OptionVendorIdentifyingVendorSpecific OptionCode = 125
|
||||
// Options 126-127 returned in RFC 3679
|
||||
OptionTFTPServerIPAddress OptionCode = 128
|
||||
OptionCallServerIPAddress OptionCode = 129
|
||||
OptionDiscriminationString OptionCode = 130
|
||||
OptionRemoteStatisticsServerIPAddress OptionCode = 131
|
||||
Option8021PVLANID OptionCode = 132
|
||||
Option8021QL2Priority OptionCode = 133
|
||||
OptionDiffservCodePoint OptionCode = 134
|
||||
OptionHTTPProxyForPhoneSpecificApplications OptionCode = 135
|
||||
OptionPANAAuthenticationAgent OptionCode = 136
|
||||
OptionLoSTServer OptionCode = 137
|
||||
OptionCAPWAPAccessControllerAddresses OptionCode = 138
|
||||
OptionOPTIONIPv4AddressMoS OptionCode = 139
|
||||
OptionOPTIONIPv4FQDNMoS OptionCode = 140
|
||||
OptionSIPUAConfigurationServiceDomains OptionCode = 141
|
||||
OptionOPTIONIPv4AddressANDSF OptionCode = 142
|
||||
OptionOPTIONIPv6AddressANDSF OptionCode = 143
|
||||
// Options 144-149 returned in RFC 3679
|
||||
OptionTFTPServerAddress OptionCode = 150
|
||||
OptionStatusCode OptionCode = 151
|
||||
OptionBaseTime OptionCode = 152
|
||||
OptionStartTimeOfState OptionCode = 153
|
||||
OptionQueryStartTime OptionCode = 154
|
||||
OptionQueryEndTime OptionCode = 155
|
||||
OptionDHCPState OptionCode = 156
|
||||
OptionDataSource OptionCode = 157
|
||||
// Options 158-174 returned in RFC 3679
|
||||
OptionEtherboot OptionCode = 175
|
||||
OptionIPTelephone OptionCode = 176
|
||||
OptionEtherbootPacketCableAndCableHome OptionCode = 177
|
||||
// Options 178-207 returned in RFC 3679
|
||||
OptionPXELinuxMagicString OptionCode = 208
|
||||
OptionPXELinuxConfigFile OptionCode = 209
|
||||
OptionPXELinuxPathPrefix OptionCode = 210
|
||||
OptionPXELinuxRebootTime OptionCode = 211
|
||||
OptionOPTION6RD OptionCode = 212
|
||||
OptionOPTIONv4AccessDomain OptionCode = 213
|
||||
// Options 214-219 returned in RFC 3679
|
||||
OptionSubnetAllocation OptionCode = 220
|
||||
OptionVirtualSubnetAllocation OptionCode = 221
|
||||
// Options 222-223 returned in RFC 3679
|
||||
// Options 224-254 are reserved for private use
|
||||
OptionEnd OptionCode = 255
|
||||
)
|
||||
|
||||
type DhcpOp byte
|
||||
|
||||
const (
|
||||
BOOTREQUEST DhcpOp = iota + 1
|
||||
BOOTREPLY
|
||||
)
|
||||
|
||||
// MessageType represents the possible DHCP message types - DISCOVER, OFFER, etc
|
||||
type MessageType byte
|
||||
|
||||
// DHCP message types
|
||||
const (
|
||||
// MessageTypeNone is not a real message type, it is used by certain
|
||||
// functions to signal that no explicit message type is requested
|
||||
MessageTypeNone MessageType = iota
|
||||
MessageTypeDiscover
|
||||
MessageTypeOffer
|
||||
MessageTypeRequest
|
||||
MessageTypeDecline
|
||||
MessageTypeAck
|
||||
MessageTypeNak
|
||||
MessageTypeRelease
|
||||
MessageTypeInform
|
||||
)
|
||||
|
||||
type DhcpMessage struct {
|
||||
Op DhcpOp
|
||||
Htype MessageType
|
||||
Hlen byte
|
||||
Hops byte
|
||||
Xid types.TxId
|
||||
Secs uint16
|
||||
Flags uint16
|
||||
Ciaddr types.Ipv4Addr
|
||||
Yiaddr types.Ipv4Addr
|
||||
Siaddr types.Ipv4Addr
|
||||
Giaddr types.Ipv4Addr
|
||||
Chaddr types.HwAddr
|
||||
Sname [64]byte
|
||||
File [128]byte
|
||||
Magic [4]byte
|
||||
}
|
||||
|
||||
func (dhcp DhcpMessage) Debug(s *zap.SugaredLogger) {
|
||||
s.Debugf("op=%x, htype=%x, hlen=%x, hops=%x", dhcp.Op, dhcp.Htype, dhcp.Hlen, dhcp.Hops)
|
||||
s.Debugf("xid=%x", dhcp.Xid)
|
||||
s.Debugf("secs=%d, flags=%x", dhcp.Secs, dhcp.Flags)
|
||||
|
||||
s.Debugf("ciaddr=%x", dhcp.Ciaddr)
|
||||
s.Debugf("siaddr=%x", dhcp.Siaddr)
|
||||
s.Debugf("giaddr=%x", dhcp.Giaddr)
|
||||
s.Debugf("chaddr=%x", dhcp.Chaddr)
|
||||
|
||||
s.Debugf("sname=%s", string(dhcp.Sname[:]))
|
||||
s.Debugf("file=%s", string(dhcp.File[:]))
|
||||
s.Debugf("magic=%x", dhcp.Magic)
|
||||
}
|
||||
20
pkg/messages/options.go
Normal file
20
pkg/messages/options.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package messages
|
||||
|
||||
type Option struct {
|
||||
Code OptionCode
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func (o Option) Size() uint8 {
|
||||
if len(o.Data) > 0 {
|
||||
return uint8(o.Data[0])
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type Options map[OptionCode]Option
|
||||
|
||||
func (opts Options) HasOption(code OptionCode) bool {
|
||||
_, exists := opts[code]
|
||||
return exists
|
||||
}
|
||||
5
pkg/types/types.go
Normal file
5
pkg/types/types.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package types
|
||||
|
||||
type TxId [4]byte
|
||||
type Ipv4Addr [4]byte
|
||||
type HwAddr [16]byte
|
||||
Reference in New Issue
Block a user