diff --git a/vendor/github.com/hashicorp/go-sockaddr/ifaddrs.go b/vendor/github.com/hashicorp/go-sockaddr/ifaddrs.go index b87589a222d..90b6576f2f2 100644 --- a/vendor/github.com/hashicorp/go-sockaddr/ifaddrs.go +++ b/vendor/github.com/hashicorp/go-sockaddr/ifaddrs.go @@ -1,6 +1,7 @@ package sockaddr import ( + "encoding/binary" "errors" "fmt" "math/big" @@ -15,7 +16,7 @@ var ( // Centralize all regexps and regexp.Copy() where necessary. signRE *regexp.Regexp = regexp.MustCompile(`^[\s]*[+-]`) whitespaceRE *regexp.Regexp = regexp.MustCompile(`[\s]+`) - ifNameRE *regexp.Regexp = regexp.MustCompile(`^Ethernet adapter ([^\s:]+):`) + ifNameRE *regexp.Regexp = regexp.MustCompile(`^Ethernet adapter ([^:]+):`) ipAddrRE *regexp.Regexp = regexp.MustCompile(`^ IPv[46] Address\. \. \. \. \. \. \. \. \. \. \. : ([^\s]+)`) ) @@ -866,6 +867,80 @@ func IfAddrMath(operation, value string, inputIfAddr IfAddr) (IfAddr, error) { default: return IfAddr{}, fmt.Errorf("unsupported type for operation %q: %T", operation, sockType) } + case "mask": + // "mask" operates on the IP address and returns the IP address on + // which the given integer mask has been applied. If the applied mask + // corresponds to a larger network than the mask of the IP address, + // the latter will be replaced by the former. + switch sockType := inputIfAddr.SockAddr.Type(); sockType { + case TypeIPv4: + i, err := strconv.ParseUint(value, 10, 32) + if err != nil { + return IfAddr{}, fmt.Errorf("unable to convert %q to int for operation %q: %v", value, operation, err) + } + + if i > 32 { + return IfAddr{}, fmt.Errorf("parameter for operation %q on ipv4 addresses must be between 0 and 32", operation) + } + + ipv4 := *ToIPv4Addr(inputIfAddr.SockAddr) + + ipv4Mask := net.CIDRMask(int(i), 32) + ipv4MaskUint32 := binary.BigEndian.Uint32(ipv4Mask) + + maskedIpv4 := ipv4.NetIP().Mask(ipv4Mask) + maskedIpv4Uint32 := binary.BigEndian.Uint32(maskedIpv4) + + maskedIpv4MaskUint32 := uint32(ipv4.Mask) + + if ipv4MaskUint32 < maskedIpv4MaskUint32 { + maskedIpv4MaskUint32 = ipv4MaskUint32 + } + + return IfAddr{ + SockAddr: IPv4Addr{ + Address: IPv4Address(maskedIpv4Uint32), + Mask: IPv4Mask(maskedIpv4MaskUint32), + }, + Interface: inputIfAddr.Interface, + }, nil + case TypeIPv6: + i, err := strconv.ParseUint(value, 10, 32) + if err != nil { + return IfAddr{}, fmt.Errorf("unable to convert %q to int for operation %q: %v", value, operation, err) + } + + if i > 128 { + return IfAddr{}, fmt.Errorf("parameter for operation %q on ipv6 addresses must be between 0 and 64", operation) + } + + ipv6 := *ToIPv6Addr(inputIfAddr.SockAddr) + + ipv6Mask := net.CIDRMask(int(i), 128) + ipv6MaskBigInt := new(big.Int) + ipv6MaskBigInt.SetBytes(ipv6Mask) + + maskedIpv6 := ipv6.NetIP().Mask(ipv6Mask) + maskedIpv6BigInt := new(big.Int) + maskedIpv6BigInt.SetBytes(maskedIpv6) + + maskedIpv6MaskBigInt := new(big.Int) + maskedIpv6MaskBigInt.Set(ipv6.Mask) + + if ipv6MaskBigInt.Cmp(maskedIpv6MaskBigInt) == -1 { + maskedIpv6MaskBigInt = ipv6MaskBigInt + } + + return IfAddr{ + SockAddr: IPv6Addr{ + Address: IPv6Address(maskedIpv6BigInt), + Mask: IPv6Mask(maskedIpv6MaskBigInt), + }, + Interface: inputIfAddr.Interface, + }, nil + default: + return IfAddr{}, fmt.Errorf("unsupported type for operation %q: %T", operation, sockType) + } default: return IfAddr{}, fmt.Errorf("unsupported math operation: %q", operation) } diff --git a/vendor/github.com/hashicorp/go-sockaddr/sockaddr.go b/vendor/github.com/hashicorp/go-sockaddr/sockaddr.go index 51389ebe9a6..826c91c2e3d 100644 --- a/vendor/github.com/hashicorp/go-sockaddr/sockaddr.go +++ b/vendor/github.com/hashicorp/go-sockaddr/sockaddr.go @@ -1,6 +1,7 @@ package sockaddr import ( + "encoding/json" "fmt" "strings" ) @@ -176,3 +177,30 @@ func sockAddrInit() { func SockAddrAttrs() []AttrName { return sockAddrAttrs } + +// Although this is pretty trivial to do in a program, having the logic here is +// useful all around. Note that this marshals into a *string* -- the underlying +// string representation of the sockaddr. If you then unmarshal into this type +// in Go, all will work as expected, but externally you can take what comes out +// and use the string value directly. +type SockAddrMarshaler struct { + SockAddr +} + +func (s *SockAddrMarshaler) MarshalJSON() ([]byte, error) { + return json.Marshal(s.SockAddr.String()) +} + +func (s *SockAddrMarshaler) UnmarshalJSON(in []byte) error { + var str string + err := json.Unmarshal(in, &str) + if err != nil { + return err + } + sa, err := NewSockAddr(str) + if err != nil { + return err + } + s.SockAddr = sa + return nil +} diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/doc.go b/vendor/github.com/hashicorp/go-sockaddr/template/doc.go index 90c8784a3f9..8cc6730a4f4 100644 --- a/vendor/github.com/hashicorp/go-sockaddr/template/doc.go +++ b/vendor/github.com/hashicorp/go-sockaddr/template/doc.go @@ -212,6 +212,13 @@ Supported operations include: from the network's broadcast address (e.g. 127.0.0.1 `"network" "-1"` will return "127.255.255.255"). Values that overflow the network size will safely wrap. + - `mask`: Applies the given network mask to the address. The network mask is + expressed as a decimal value (e.g. network mask "24" corresponds to + `255.255.255.0`). After applying the network mask, the network mask of the + resulting address will be either the applied network mask or the network mask + of the input address depending on which network is larger + (e.g. 192.168.10.20/24 `"mask" "16"` will return "192.168.0.0/16" but + 192.168.10.20/24 `"mask" "28"` will return "192.168.10.16/24"). Example: @@ -219,6 +226,7 @@ Example: {{ GetPrivateInterfaces | include "type" "IP" | math "address" "-256" | attr "address" }} {{ GetPrivateInterfaces | include "type" "IP" | math "network" "+2" | attr "address" }} {{ GetPrivateInterfaces | include "type" "IP" | math "network" "-2" | attr "address" }} + {{ GetPrivateInterfaces | include "type" "IP" | math "mask" "24" | attr "address" }} {{ GetPrivateInterfaces | include "flags" "forwardable|up" | include "type" "IPv4" | math "network" "+2" | attr "address" }} diff --git a/vendor/vendor.json b/vendor/vendor.json index 2912862e707..3a5bcc1a926 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -145,8 +145,8 @@ {"path":"github.com/hashicorp/go-plugin","checksumSHA1":"R6me0jVmcT/OPo80Fe0qo5fRwHc=","revision":"3e6d191694b5a3a2b99755f31b47fa209e4bcd09","revisionTime":"2017-08-28T02:45:49Z"}, {"path":"github.com/hashicorp/go-retryablehttp","checksumSHA1":"ErJHGU6AVPZM9yoY/xV11TwSjQs=","revision":"6e85be8fee1dcaa02c0eaaac2df5a8fbecf94145","revisionTime":"2016-09-30T03:51:02Z"}, {"path":"github.com/hashicorp/go-rootcerts","checksumSHA1":"A1PcINvF3UiwHRKn8UcgARgvGRs=","revision":"6bb64b370b90e7ef1fa532be9e591a81c3493e00","revisionTime":"2016-05-03T14:34:40Z"}, - {"path":"github.com/hashicorp/go-sockaddr","checksumSHA1":"X2tSlVe532du67MEIU1KHoCT68k=","revision":"e12d9401a74f025fe672cd1a84b2081c773990d3","revisionTime":"2017-06-22T20:44:38Z"}, - {"path":"github.com/hashicorp/go-sockaddr/template","checksumSHA1":"mIUCMmRHslN2bxQZ0uObMnXxk9E=","revision":"e12d9401a74f025fe672cd1a84b2081c773990d3","revisionTime":"2017-06-22T20:44:38Z"}, + {"path":"github.com/hashicorp/go-sockaddr","checksumSHA1":"pTsxSJWLs/MPY8yOHNsVoZ/0yaA=","revision":"7165ee14aff120ee3642aa2bcf2dea8eebef29c3","revisionTime":"2018-02-07T23:16:36Z"}, + {"path":"github.com/hashicorp/go-sockaddr/template","checksumSHA1":"PDp9DVLvf3KWxhs4G4DpIwauMSU=","revision":"7165ee14aff120ee3642aa2bcf2dea8eebef29c3","revisionTime":"2018-02-07T23:16:36Z"}, {"path":"github.com/hashicorp/go-syslog","revision":"42a2b573b664dbf281bd48c3cc12c086b17a39ba"}, {"path":"github.com/hashicorp/go-uuid","checksumSHA1":"mAkPa/RLuIwN53GbwIEMATexams=","revision":"64130c7a86d732268a38cb04cfbaf0cc987fda98","revisionTime":"2016-07-17T02:21:40Z"}, {"path":"github.com/hashicorp/go-version","checksumSHA1":"tUGxc7rfX0cmhOOUDhMuAZ9rWsA=","revision":"03c5bf6be031b6dd45afec16b1cf94fc8938bc77","revisionTime":"2017-02-02T08:07:59Z"},