forked from j-keck/lsleases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcp_leases.go
121 lines (100 loc) · 2.33 KB
/
dhcp_leases.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
type DHCPLease struct {
Created time.Time
Expire time.Time
MissedPings int
IP string
Mac string
Name string
}
func (l *DHCPLease) String() string {
return fmt.Sprintf("%-15s %-17s %s", l.IP, l.Mac, l.Name)
}
type DHCPLeases struct {
Leases []*DHCPLease
}
type SortedByCreated DHCPLeases
func (ls SortedByCreated) Len() int {
return len(ls.Leases)
}
func (ls SortedByCreated) Swap(i, j int) {
ls.Leases[i], ls.Leases[j] = ls.Leases[j], ls.Leases[i]
}
func (ls SortedByCreated) Less(i, j int) bool {
return ls.Leases[i].Created.Before(ls.Leases[j].Created)
}
func (ls *DHCPLeases) Clear() {
ls.Leases = ls.Leases[:0]
}
func (ls *DHCPLeases) Delete(l *DHCPLease) {
if i, ok := ls.IndexOfMac(l.Mac); ok {
ls.Leases = append(ls.Leases[:i], ls.Leases[i+1:]...)
} else {
log.Printf("unable to delte lease: '%s' - not found\n", l.String())
}
}
func (ls *DHCPLeases) Foreach(f func(*DHCPLease)) {
for _, l := range ls.Leases {
f(l)
}
}
func (ls *DHCPLeases) Filter(f func(*DHCPLease) bool) *DHCPLeases {
var nls DHCPLeases
for _, l := range ls.Leases {
if f(l) {
nls.Leases = append(nls.Leases, l)
}
}
return &nls
}
func (ls *DHCPLeases) UpdateOrAdd(l *DHCPLease) {
if i, ok := ls.IndexOfMac(l.Mac); ok {
ls.Leases[i] = l
} else {
ls.Leases = append(ls.Leases, l)
}
}
func (ls *DHCPLeases) IndexOf(f func(*DHCPLease) bool) (int, bool) {
for i, l := range ls.Leases {
if f(l) {
return i, true
}
}
return -1, false
}
func (ls *DHCPLeases) IndexOfMac(mac string) (int, bool) {
return ls.IndexOf(func(l *DHCPLease) bool {
return l.Mac == mac
})
}
func (ls *DHCPLeases) SaveLeases() error {
j, err := json.Marshal(ls)
if err != nil {
return err
}
path := appDataPath + "/lsleases.json"
verboseLog.Printf("save leases under %s\n", path)
return ioutil.WriteFile(path, []byte(j), 0644)
}
func LoadLeases() (DHCPLeases, error) {
var leases DHCPLeases
path := appDataPath + "/lsleases.json"
verboseLog.Printf("load saved leases from %s\n", path)
if _, err := os.Stat(path); os.IsNotExist(err) {
return leases, fmt.Errorf("no persistence file found under %s\n", path)
}
b, err := ioutil.ReadFile(path)
if err != nil {
return leases, err
}
err = json.Unmarshal(b, &leases)
return leases, err
}