Этот коммит содержится в:
Christopher Speller
2017-11-13 09:09:58 -08:00
коммит произвёл GitHub
родитель 7304a61ef5
Коммит 1329aa51b6
474 изменённых файлов: 84159 добавлений и 8829 удалений

117
vendor/github.com/hashicorp/go-sockaddr/ifaddr_test.go сгенерированный поставляемый
Просмотреть файл

@@ -410,6 +410,114 @@ func TestIfAddrMath(t *testing.T) {
value: "+xyz",
wantFail: true,
},
{
name: "ipv4 mask operand equals input ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("10.20.30.40/8"),
},
operation: "mask",
value: "8",
expected: "10.0.0.0/8",
},
{
name: "ipv4 mask operand larger than input ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("192.168.10.20/24"),
},
operation: "mask",
value: "16",
expected: "192.168.0.0/16",
},
{
name: "ipv4 host upper bound mask operand larger than input ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("192.168.255.255/24"),
},
operation: "mask",
value: "16",
expected: "192.168.0.0/16",
},
{
name: "ipv4 mask operand smaller than ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("10.20.30.40/8"),
},
operation: "mask",
value: "16",
expected: "10.20.0.0/8",
},
{
name: "ipv4 host upper bound mask operand smaller than input ipv4 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("10.20.255.255/8"),
},
operation: "mask",
value: "16",
expected: "10.20.0.0/8",
},
{
name: "ipv4 mask bad value upper bound",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "mask",
value: "33",
wantFail: true,
},
{
name: "ipv4 mask bad value lower bound",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv4Addr("127.0.0.1/8"),
},
operation: "mask",
value: "-1",
wantFail: true,
},
{
name: "ipv6 mask operand equals input ipv6 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("2001:0db8:85a3::8a2e:0370:7334/64"),
},
operation: "mask",
value: "64",
expected: "2001:db8:85a3::/64",
},
{
name: "ipv6 mask operand larger than input ipv6 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("2001:0db8:85a3::8a2e:0370:7334/64"),
},
operation: "mask",
value: "32",
expected: "2001:db8::/32",
},
{
name: "ipv6 mask operand smaller than input ipv6 subnet mask",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("2001:0db8:85a3::8a2e:0370:7334/64"),
},
operation: "mask",
value: "96",
expected: "2001:db8:85a3::8a2e:0:0/64",
},
{
name: "ipv6 mask bad value upper bound",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "mask",
value: "129",
wantFail: true,
},
{
name: "ipv6 mask bad value lower bound",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustIPv6Addr("::1/128"),
},
operation: "mask",
value: "-1",
wantFail: true,
},
{
name: "unix unsupported operation",
ifAddr: sockaddr.IfAddr{
@@ -428,6 +536,15 @@ func TestIfAddrMath(t *testing.T) {
value: "+123",
wantFail: true,
},
{
name: "unix unsupported operation",
ifAddr: sockaddr.IfAddr{
SockAddr: sockaddr.MustUnixSock("/tmp/foo"),
},
operation: "mask",
value: "8",
wantFail: true,
},
}
for i, test := range tests {

75
vendor/github.com/hashicorp/go-sockaddr/ifaddrs.go сгенерированный поставляемый
Просмотреть файл

@@ -1,6 +1,7 @@
package sockaddr
import (
"encoding/binary"
"errors"
"fmt"
"math/big"
@@ -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)
}

8
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" }}