From 86d7c47c0ab6239c261e368b184e6d2c86c60252 Mon Sep 17 00:00:00 2001 From: tmshn Date: Wed, 19 Apr 2017 20:26:49 +0900 Subject: [PATCH] Change cidrhost() to get IP from end of the range when negative number given Ref: https://github.com/apparentlymart/go-cidr/pull/2 --- config/interpolate_funcs_test.go | 15 +++++++++++++++ .../apparentlymart/go-cidr/cidr/cidr.go | 9 ++++++++- vendor/vendor.json | 5 +++-- .../docs/configuration/interpolation.html.md | 6 ++++-- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 78816b6dd8..801be6dbb3 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -541,11 +541,26 @@ func TestInterpolateFuncCidrHost(t *testing.T) { "192.168.1.5", 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)}`, nil, 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)}`, nil, diff --git a/vendor/github.com/apparentlymart/go-cidr/cidr/cidr.go b/vendor/github.com/apparentlymart/go-cidr/cidr/cidr.go index 1583d6382b..a31cdec773 100644 --- a/vendor/github.com/apparentlymart/go-cidr/cidr/cidr.go +++ b/vendor/github.com/apparentlymart/go-cidr/cidr/cidr.go @@ -61,7 +61,14 @@ func Host(base *net.IPNet, num int) (net.IP, error) { hostLen := addrLen - parentLen maxHostNum := uint64(1< 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) } diff --git a/vendor/vendor.json b/vendor/vendor.json index b4fd8b29d6..e6ff9097cb 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -446,9 +446,10 @@ "revisionTime": "2016-08-22T23:00:20Z" }, { - "checksumSHA1": "kn+zdUr5TNsoAX8BgjOaWYtMT5U=", + "checksumSHA1": "FIL83loX9V9APvGQIjJpbxq53F0=", "path": "github.com/apparentlymart/go-cidr/cidr", - "revision": "a3ebdb999b831ecb6ab8a226e31b07b2b9061c47" + "revision": "7e4b007599d4e2076d9a81be723b3912852dda2c", + "revisionTime": "2017-04-18T07:21:50Z" }, { "checksumSHA1": "yicZ9OtLcy3iCgraWO015yeoO5E=", diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index 5958aef709..1b4b69c5c0 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -157,8 +157,10 @@ The supported built-in functions are: * `chomp(string)` - Removes trailing newlines from the given string. * `cidrhost(iprange, hostnum)` - Takes an IP address range in CIDR notation - and creates an IP address with the given host number. For example, - `cidrhost("10.0.0.0/8", 2)` returns `10.0.0.2`. + and creates an IP address with the given host number. If given host + 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 and returns the address-formatted subnet mask format that some