config.go 1.4 KB

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