forked from coredhcp/coredhcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
90 lines (84 loc) · 2.7 KB
/
storage.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
// Copyright 2018-present the CoreDHCP Authors. All rights reserved
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package rangeplugin
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"os"
"strings"
"time"
)
// loadRecords loads the DHCPv6/v4 Records global map with records stored on
// the specified file. The records have to be one per line, a mac address and an
// IP address.
func loadRecords(r io.Reader) (map[string]*Record, error) {
sc := bufio.NewScanner(r)
records := make(map[string]*Record)
for sc.Scan() {
line := sc.Text()
if len(line) == 0 {
continue
}
tokens := strings.Fields(line)
if len(tokens) != 3 {
return nil, fmt.Errorf("malformed line, want 3 fields, got %d: %s", len(tokens), line)
}
hwaddr, err := net.ParseMAC(tokens[0])
if err != nil {
return nil, fmt.Errorf("malformed hardware address: %s", tokens[0])
}
ipaddr := net.ParseIP(tokens[1])
if ipaddr.To4() == nil {
return nil, fmt.Errorf("expected an IPv4 address, got: %v", ipaddr)
}
expires, err := time.Parse(time.RFC3339, tokens[2])
if err != nil {
return nil, fmt.Errorf("expected time of exipry in RFC3339 format, got: %v", tokens[2])
}
records[hwaddr.String()] = &Record{IP: ipaddr, expires: expires}
}
return records, nil
}
func loadRecordsFromFile(filename string) (map[string]*Record, error) {
reader, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0640)
defer func() {
if err := reader.Close(); err != nil {
log.Warningf("Failed to close file %s: %v", filename, err)
}
}()
if err != nil {
return nil, fmt.Errorf("cannot open lease file %s: %w", filename, err)
}
return loadRecords(reader)
}
// saveIPAddress writes out a lease to storage
func saveIPAddress(leaseFile *os.File, mac net.HardwareAddr, record *Record) error {
_, err := leaseFile.WriteString(mac.String() + " " + record.IP.String() + " " + record.expires.Format(time.RFC3339) + "\n")
if err != nil {
return err
}
err = leaseFile.Sync()
if err != nil {
return err
}
return nil
}
// registerBackingFile installs a file as the backing store for leases
func registerBackingFile(current **os.File, newFileName string) error {
if *current != nil {
// This is TODO; swapping the file out is easy
// but maintaining consistency with the in-memory state isn't
return errors.New("cannot swap out a lease storage file while running")
}
// We never close this, but that's ok because plugins are never stopped/unregistered
newLeasefile, err := os.OpenFile(newFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open lease file %s: %w", newFileName, err)
}
*current = newLeasefile
return nil
}