mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
Vendored go-sockaddr and go-sockaddr/template
This commit is contained in:
77
vendor/github.com/hashicorp/go-sockaddr/ifaddrs.go
generated
vendored
77
vendor/github.com/hashicorp/go-sockaddr/ifaddrs.go
generated
vendored
@@ -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)
|
||||
}
|
||||
|
||||
28
vendor/github.com/hashicorp/go-sockaddr/sockaddr.go
generated
vendored
28
vendor/github.com/hashicorp/go-sockaddr/sockaddr.go
generated
vendored
@@ -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
|
||||
}
|
||||
|
||||
8
vendor/github.com/hashicorp/go-sockaddr/template/doc.go
generated
vendored
8
vendor/github.com/hashicorp/go-sockaddr/template/doc.go
generated
vendored
@@ -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" }}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user