plugin.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package clientport
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net"
  9. "strings"
  10. "bitbucket.org/insomniacslk/coredhcp/handler"
  11. "bitbucket.org/insomniacslk/coredhcp/plugins"
  12. "github.com/insomniacslk/dhcp/dhcpv4"
  13. "github.com/insomniacslk/dhcp/dhcpv6"
  14. )
  15. func init() {
  16. plugins.RegisterPlugin("file", setupFile6, setupFile4)
  17. }
  18. // StaticRecords holds a MAC -> IP address mapping
  19. var StaticRecords map[string]net.IP
  20. // DHCPv6Records and DHCPv4Records are mappings between MAC addresses in
  21. // form of a string, to network configurations.
  22. var (
  23. DHCPv6Records map[string]net.IP
  24. DHCPv4Records map[string]net.IP
  25. )
  26. var serverID *dhcpv6.OptServerId
  27. // LoadDHCPv6Records loads the DHCPv6Records global map with records stored on
  28. // the specified file. The records have to be one per line, a mac address and an
  29. // IPv6 address.
  30. func LoadDHCPv6Records(filename string) (map[string]net.IP, error) {
  31. log.Printf("plugins/file: reading leases from %s", filename)
  32. data, err := ioutil.ReadFile(filename)
  33. if err != nil {
  34. return nil, err
  35. }
  36. records := make(map[string]net.IP, 0)
  37. // TODO ignore comments
  38. for _, lineBytes := range bytes.Split(data, []byte{'\n'}) {
  39. line := string(lineBytes)
  40. if len(line) == 0 {
  41. continue
  42. }
  43. tokens := strings.Fields(line)
  44. if len(tokens) != 2 {
  45. return nil, fmt.Errorf("plugins/file: malformed line: %s", line)
  46. }
  47. hwaddr, err := net.ParseMAC(tokens[0])
  48. if err != nil {
  49. return nil, fmt.Errorf("plugins/file: malformed hardware address: %s", tokens[0])
  50. }
  51. ipaddr := net.ParseIP(tokens[1])
  52. if ipaddr.To16() == nil {
  53. return nil, fmt.Errorf("plugins/file: expected an IPv6 address, got: %v", ipaddr)
  54. }
  55. records[hwaddr.String()] = ipaddr
  56. }
  57. return records, nil
  58. }
  59. // Handler6 handles DHCPv6 packets for the file plugin
  60. func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  61. mac, err := dhcpv6.ExtractMAC(req)
  62. if err != nil {
  63. return nil, false
  64. }
  65. ipaddr, ok := StaticRecords[mac.String()]
  66. if !ok {
  67. return nil, false
  68. }
  69. log.Printf("Found IP address %s for MAC %s", ipaddr, mac)
  70. if resp == nil {
  71. resp, err = dhcpv6.NewAdvertiseFromSolicit(req)
  72. if err != nil {
  73. return nil, false
  74. }
  75. }
  76. // TODO add an OptIANA based on the above data
  77. return resp, true
  78. }
  79. // Handler4 handles DHCPv4 packets for the file plugin
  80. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  81. // TODO check the MAC address in the request
  82. // if it is present in StaticRecords, forge a response
  83. // and stop processing.
  84. return resp, true
  85. }
  86. func setupFile6(args ...string) (handler.Handler6, error) {
  87. h6, _, err := setupFile(true, args...)
  88. return h6, err
  89. }
  90. func setupFile4(args ...string) (handler.Handler4, error) {
  91. log.Print("plugins/file: loading `file` plugin for DHCPv4")
  92. return nil, nil
  93. }
  94. func setupFile(v6 bool, args ...string) (handler.Handler6, handler.Handler4, error) {
  95. if len(args) < 1 {
  96. return nil, nil, errors.New("plugins/file: need a file name")
  97. }
  98. filename := args[0]
  99. if filename == "" {
  100. return nil, nil, errors.New("plugins/file: got empty file name")
  101. }
  102. records, err := LoadDHCPv6Records(filename)
  103. if err != nil {
  104. return nil, nil, fmt.Errorf("plugins/file: failed to load DHCPv6 records: %v", err)
  105. }
  106. log.Printf("plugins/file: loaded %d leases from %s", len(records), filename)
  107. StaticRecords = records
  108. return Handler6, Handler4, nil
  109. }