config.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package subnet
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/coreos/rudder/pkg/ip"
  6. )
  7. type Config struct {
  8. Network ip.IP4Net
  9. SubnetMin ip.IP4
  10. SubnetMax ip.IP4
  11. SubnetLen uint
  12. Backend json.RawMessage
  13. }
  14. func ParseConfig(s string) (*Config, error) {
  15. cfg := new(Config)
  16. err := json.Unmarshal([]byte(s), cfg)
  17. if err != nil {
  18. return nil, err
  19. }
  20. if cfg.SubnetLen > 0 {
  21. if cfg.SubnetLen < cfg.Network.PrefixLen {
  22. return nil, errors.New("HostSubnet is larger network than Network")
  23. }
  24. } else {
  25. // try to give each host a /24 but if the whole network
  26. // is /24 or smaller, half the network
  27. if cfg.Network.PrefixLen < 24 {
  28. cfg.SubnetLen = 24
  29. } else {
  30. cfg.SubnetLen = cfg.Network.PrefixLen + 1
  31. }
  32. }
  33. subnetSize := ip.IP4(1 << (32 - cfg.SubnetLen))
  34. if cfg.SubnetMin == ip.IP4(0) {
  35. // skip over the first subnet otherwise it causes problems. e.g.
  36. // if Network is 10.100.0.0/16, having an interface with 10.0.0.0
  37. // makes ping think it's a broadcast address (not sure why)
  38. cfg.SubnetMin = cfg.Network.IP + subnetSize
  39. } else if !cfg.Network.Contains(cfg.SubnetMin) {
  40. return nil, errors.New("SubnetMin is not in the range of the Network")
  41. }
  42. if cfg.SubnetMax == ip.IP4(0) {
  43. cfg.SubnetMax = cfg.Network.Next().IP - subnetSize
  44. } else if !cfg.Network.Contains(cfg.SubnetMax) {
  45. return nil, errors.New("SubnetMax is not in the range of the Network")
  46. }
  47. return cfg, nil
  48. }