Change cidrhost() to get IP from end of the range when negative number given

Ref: https://github.com/apparentlymart/go-cidr/pull/2
This commit is contained in:
tmshn 2017-04-19 20:26:49 +09:00 committed by Martin Atkins
parent 4a782583b6
commit 86d7c47c0a
4 changed files with 30 additions and 5 deletions

View File

@ -541,11 +541,26 @@ func TestInterpolateFuncCidrHost(t *testing.T) {
"192.168.1.5", "192.168.1.5",
false, false,
}, },
{
`${cidrhost("192.168.1.0/24", -5)}`,
"192.168.1.251",
false,
},
{
`${cidrhost("192.168.1.0/24", -256)}`,
"192.168.1.0",
false,
},
{ {
`${cidrhost("192.168.1.0/30", 255)}`, `${cidrhost("192.168.1.0/30", 255)}`,
nil, nil,
true, // 255 doesn't fit in two bits true, // 255 doesn't fit in two bits
}, },
{
`${cidrhost("192.168.1.0/30", -255)}`,
nil,
true, // 255 doesn't fit in two bits
},
{ {
`${cidrhost("not-a-cidr", 6)}`, `${cidrhost("not-a-cidr", 6)}`,
nil, nil,

View File

@ -61,7 +61,14 @@ func Host(base *net.IPNet, num int) (net.IP, error) {
hostLen := addrLen - parentLen hostLen := addrLen - parentLen
maxHostNum := uint64(1<<uint64(hostLen)) - 1 maxHostNum := uint64(1<<uint64(hostLen)) - 1
if uint64(num) > maxHostNum {
numUint64 := uint64(num)
if num < 0 {
numUint64 = uint64(-num) - 1
num = int(maxHostNum - numUint64)
}
if numUint64 > maxHostNum {
return nil, fmt.Errorf("prefix of %d does not accommodate a host numbered %d", parentLen, num) return nil, fmt.Errorf("prefix of %d does not accommodate a host numbered %d", parentLen, num)
} }

5
vendor/vendor.json vendored
View File

@ -446,9 +446,10 @@
"revisionTime": "2016-08-22T23:00:20Z" "revisionTime": "2016-08-22T23:00:20Z"
}, },
{ {
"checksumSHA1": "kn+zdUr5TNsoAX8BgjOaWYtMT5U=", "checksumSHA1": "FIL83loX9V9APvGQIjJpbxq53F0=",
"path": "github.com/apparentlymart/go-cidr/cidr", "path": "github.com/apparentlymart/go-cidr/cidr",
"revision": "a3ebdb999b831ecb6ab8a226e31b07b2b9061c47" "revision": "7e4b007599d4e2076d9a81be723b3912852dda2c",
"revisionTime": "2017-04-18T07:21:50Z"
}, },
{ {
"checksumSHA1": "yicZ9OtLcy3iCgraWO015yeoO5E=", "checksumSHA1": "yicZ9OtLcy3iCgraWO015yeoO5E=",

View File

@ -157,8 +157,10 @@ The supported built-in functions are:
* `chomp(string)` - Removes trailing newlines from the given string. * `chomp(string)` - Removes trailing newlines from the given string.
* `cidrhost(iprange, hostnum)` - Takes an IP address range in CIDR notation * `cidrhost(iprange, hostnum)` - Takes an IP address range in CIDR notation
and creates an IP address with the given host number. For example, and creates an IP address with the given host number. If given host
`cidrhost("10.0.0.0/8", 2)` returns `10.0.0.2`. number is negative, the count starts from the end of the range.
For example, `cidrhost("10.0.0.0/8", 2)` returns `10.0.0.2` and
`cidrhost("10.0.0.0/8", -2)` returns `10.255.255.254`.
* `cidrnetmask(iprange)` - Takes an IP address range in CIDR notation * `cidrnetmask(iprange)` - Takes an IP address range in CIDR notation
and returns the address-formatted subnet mask format that some and returns the address-formatted subnet mask format that some