This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlease.go
168 lines (149 loc) · 3.62 KB
/
lease.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package models
import (
"fmt"
"net"
"time"
)
var hexDigit = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}
func Hexaddr(addr net.IP) string {
b := addr.To4()
s := make([]byte, len(b)*2)
for i, tn := range b {
s[i*2], s[i*2+1] = hexDigit[tn>>4], hexDigit[tn&0xf]
}
return string(s)
}
// Lease tracks DHCP leases.
// swagger:model
type Lease struct {
Validation
Access
Meta
Owned
Bundled
// Addr is the IP address that the lease handed out.
//
// required: true
// swagger:strfmt ipv4
Addr net.IP `index:",key"`
// NextServer is the IP address that we should have the machine talk to
// next. In most cases, this will be our address.
//
// required: false
// swagger:strfmt ipv4
NextServer net.IP
// Via is the IP address used to select which subnet the lease belongs to.
// It is either an address present on a local interface that dr-provision is
// listening on, or the GIADDR field of the DHCP request.
//
// required: false
// swagger:strfmt ipv4
Via net.IP
// Token is the unique token for this lease based on the
// Strategy this lease used.
//
// required: true
Token string
// Duration is the time in seconds for which a lease can be valid.
// ExpireTime is calculated from Duration.
Duration int32
// ExpireTime is the time at which the lease expires and is no
// longer valid The DHCP renewal time will be half this, and the
// DHCP rebind time will be three quarters of this.
//
// required: true
// swagger:strfmt date-time
ExpireTime time.Time
// Strategy is the leasing strategy that will be used determine what to use from
// the DHCP packet to handle lease management.
//
// required: true
Strategy string
// State is the current state of the lease. This field is for informational
// purposes only.
//
// read only: true
// required: true
State string
// Options are the DHCP options that the Lease is running with.
Options []DhcpOption
// ProvidedOptions are the DHCP options the last Discover or Offer packet
// for this lease provided to us.
ProvidedOptions []DhcpOption
// SkipBoot indicates that the DHCP system is allowed to offer
// boot options for whatever boot protocol the machine wants to
// use.
//
// read only: true
SkipBoot bool
}
func (l *Lease) String() string {
return fmt.Sprintf("%s %s:%s:%s %d", l.Addr, l.Strategy, l.Token, l.State, l.ExpireTime.Unix())
}
func (l *Lease) GetMeta() Meta {
return l.Meta
}
func (l *Lease) SetMeta(d Meta) {
l.Meta = d
}
func (l *Lease) Prefix() string {
return "leases"
}
func (l *Lease) Key() string {
return Hexaddr(l.Addr)
}
func (l *Lease) KeyName() string {
return "Addr"
}
func (l *Lease) Fill() {
if l.Meta == nil {
l.Meta = Meta{}
}
if l.NextServer == nil {
l.NextServer = net.IP{}
}
if l.Via == nil {
l.Via = net.IP{}
}
if l.Options == nil {
l.Options = []DhcpOption{}
}
if l.ProvidedOptions == nil {
l.ProvidedOptions = []DhcpOption{}
}
l.Validation.fill(l)
}
func (l *Lease) AuthKey() string {
return l.Key()
}
func (b *Lease) SliceOf() interface{} {
s := []*Lease{}
return &s
}
func (b *Lease) ToModels(obj interface{}) []Model {
items := obj.(*[]*Lease)
res := make([]Model, len(*items))
for i, item := range *items {
res[i] = Model(item)
}
return res
}
func (b *Lease) CanHaveActions() bool {
return true
}
func (l *Lease) Expired() bool {
return l.ExpireTime.Before(time.Now())
}
func (l *Lease) Fake() bool {
return l.State == "FAKE"
}
func (l *Lease) Expire() {
l.ExpireTime = time.Now()
l.State = "EXPIRED"
}
func (l *Lease) Invalidate() {
l.ExpireTime = time.Now().Add(10 * time.Minute)
l.Token = ""
l.Strategy = ""
l.State = "INVALID"
}