func (v *validator) validateGateway(idx int, gateway string, ipNet maybeIPNet) []error { if v.isBlank(gateway) { return []error{bosherr.Errorf("networks[%d].subnets[0].gateway must be provided", idx)} } else { errors := []error{} ipNet.Try(func(ipNet *net.IPNet) error { gatewayIp := net.ParseIP(gateway) if gatewayIp == nil { errors = append(errors, bosherr.Errorf("networks[%d].subnets[0].gateway must be an ip", idx)) } if !ipNet.Contains(gatewayIp) { errors = append(errors, bosherr.Errorf("subnet gateway '%s' must be within the specified range '%s'", gateway, ipNet)) } if ipNet.IP.Equal(gatewayIp) { errors = append(errors, bosherr.Errorf("subnet gateway can't be the network address '%s'", gatewayIp)) } if binet.LastAddress(ipNet).Equal(gatewayIp) { errors = append(errors, bosherr.Errorf("subnet gateway can't be the broadcast address '%s'", gatewayIp)) } return nil }) return errors } }
package net_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "net" binet "github.com/cloudfoundry/bosh-init/common/net" ) var _ = Describe("common.net ip helpers", func() { Describe("LastAddress", func() { It("Returns the last address in the network range", func() { Expect( binet.LastAddress(netFor("10.0.0.0/24")), ).To(Equal(net.ParseIP("10.0.0.255"))) Expect( binet.LastAddress(netFor("10.0.0.20/24")), ).To(Equal(net.ParseIP("10.0.0.255"))) Expect( binet.LastAddress(netFor("10.1.0.0/24")), ).To(Equal(net.ParseIP("10.1.0.255"))) Expect( binet.LastAddress(netFor("10.10.0.0/8")), ).To(Equal(net.ParseIP("10.255.255.255"))) Expect( binet.LastAddress(netFor("2001:db8:1234::/48")),