storage.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. package rangeplugin
  5. import (
  6. "bufio"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "net"
  11. "os"
  12. "strings"
  13. "time"
  14. )
  15. // loadRecords loads the DHCPv6/v4 Records global map with records stored on
  16. // the specified file. The records have to be one per line, a mac address and an
  17. // IP address.
  18. func loadRecords(r io.Reader) (map[string]*Record, error) {
  19. sc := bufio.NewScanner(r)
  20. records := make(map[string]*Record)
  21. for sc.Scan() {
  22. line := sc.Text()
  23. if len(line) == 0 {
  24. continue
  25. }
  26. tokens := strings.Fields(line)
  27. if len(tokens) != 3 {
  28. return nil, fmt.Errorf("malformed line, want 3 fields, got %d: %s", len(tokens), line)
  29. }
  30. hwaddr, err := net.ParseMAC(tokens[0])
  31. if err != nil {
  32. return nil, fmt.Errorf("malformed hardware address: %s", tokens[0])
  33. }
  34. ipaddr := net.ParseIP(tokens[1])
  35. if ipaddr.To4() == nil {
  36. return nil, fmt.Errorf("expected an IPv4 address, got: %v", ipaddr)
  37. }
  38. expires, err := time.Parse(time.RFC3339, tokens[2])
  39. if err != nil {
  40. return nil, fmt.Errorf("expected time of exipry in RFC3339 format, got: %v", tokens[2])
  41. }
  42. records[hwaddr.String()] = &Record{IP: ipaddr, expires: expires}
  43. }
  44. return records, nil
  45. }
  46. func loadRecordsFromFile(filename string) (map[string]*Record, error) {
  47. reader, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0640)
  48. defer func() {
  49. if err := reader.Close(); err != nil {
  50. log.Warningf("Failed to close file %s: %v", filename, err)
  51. }
  52. }()
  53. if err != nil {
  54. return nil, fmt.Errorf("cannot open lease file %s: %w", filename, err)
  55. }
  56. return loadRecords(reader)
  57. }
  58. // saveIPAddress writes out a lease to storage
  59. func (p *PluginState) saveIPAddress(mac net.HardwareAddr, record *Record) error {
  60. _, err := p.leasefile.WriteString(mac.String() + " " + record.IP.String() + " " + record.expires.Format(time.RFC3339) + "\n")
  61. if err != nil {
  62. return err
  63. }
  64. err = p.leasefile.Sync()
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. }
  70. // registerBackingFile installs a file as the backing store for leases
  71. func (p *PluginState) registerBackingFile(filename string) error {
  72. if p.leasefile != nil {
  73. // This is TODO; swapping the file out is easy
  74. // but maintaining consistency with the in-memory state isn't
  75. return errors.New("cannot swap out a lease storage file while running")
  76. }
  77. // We never close this, but that's ok because plugins are never stopped/unregistered
  78. newLeasefile, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  79. if err != nil {
  80. return fmt.Errorf("failed to open lease file %s: %w", filename, err)
  81. }
  82. p.leasefile = newLeasefile
  83. return nil
  84. }