vendor: update github.com/hashicorp/go-sockaddr (#3633)
Pull in changes for * hashicorp/go-sockaddr#12 * hashicorp/go-sockaddr#13 * hashicorp/go-sockaddr#14 * hashicorp/go-sockaddr#16
This commit is contained in:
parent
80ddc085c9
commit
bdb8c8f73e
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -5,10 +5,6 @@ import (
|
|||
"os/exec"
|
||||
)
|
||||
|
||||
var cmds map[string][]string = map[string][]string{
|
||||
"ip": {"/sbin/ip", "route"},
|
||||
}
|
||||
|
||||
type routeInfo struct {
|
||||
cmds map[string][]string
|
||||
}
|
||||
|
@ -16,15 +12,22 @@ type routeInfo struct {
|
|||
// NewRouteInfo returns a Linux-specific implementation of the RouteInfo
|
||||
// interface.
|
||||
func NewRouteInfo() (routeInfo, error) {
|
||||
// CoreOS Container Linux moved ip to /usr/bin/ip, so look it up on
|
||||
// $PATH and fallback to /sbin/ip on error.
|
||||
path, _ := exec.LookPath("ip")
|
||||
if path == "" {
|
||||
path = "/sbin/ip"
|
||||
}
|
||||
|
||||
return routeInfo{
|
||||
cmds: cmds,
|
||||
cmds: map[string][]string{"ip": {path, "route"}},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetDefaultInterfaceName returns the interface name attached to the default
|
||||
// route on the default interface.
|
||||
func (ri routeInfo) GetDefaultInterfaceName() (string, error) {
|
||||
out, err := exec.Command(cmds["ip"][0], cmds["ip"][1:]...).Output()
|
||||
out, err := exec.Command(ri.cmds["ip"][0], ri.cmds["ip"][1:]...).Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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" }}
|
||||
|
||||
|
||||
|
|
|
@ -40,8 +40,8 @@
|
|||
{"path":"github.com/hashicorp/go-multierror","checksumSHA1":"lrSl49G23l6NhfilxPM0XFs5rZo=","revision":"d30f09973e19c1dfcd120b2d9c4f168e68d6b5d5","revisionTime":"2015-09-16T20:57:42Z"},
|
||||
{"path":"github.com/hashicorp/go-retryablehttp","checksumSHA1":"yzoWV7yrS/TvOrKy5ZrdUjsYaOA=","revision":"794af36148bf63c118d6db80eb902a136b907e71","revisionTime":"2017-08-24T18:08:59Z"},
|
||||
{"path":"github.com/hashicorp/go-rootcerts","checksumSHA1":"A1PcINvF3UiwHRKn8UcgARgvGRs=","revision":"6bb64b370b90e7ef1fa532be9e591a81c3493e00","revisionTime":"2016-05-03T14:34:40Z"},
|
||||
{"path":"github.com/hashicorp/go-sockaddr","checksumSHA1":"GP24Vz4EmZAL1ZH2TYTkDiiCO94=","revision":"2d10d7c10258d11196c0ebf2943509e4afd06cd4","revisionTime":"2017-05-23T22:50:28Z"},
|
||||
{"path":"github.com/hashicorp/go-sockaddr/template","checksumSHA1":"mIUCMmRHslN2bxQZ0uObMnXxk9E=","revision":"2d10d7c10258d11196c0ebf2943509e4afd06cd4","revisionTime":"2017-05-23T22:50:28Z"},
|
||||
{"path":"github.com/hashicorp/go-sockaddr","checksumSHA1":"eCWvhgknHMj5K19ePPjIA3l401Q=","revision":"9b4c5fa5b10a683339a270d664474b9f4aee62fc","revisionTime":"2017-10-30T10:43:12Z"},
|
||||
{"path":"github.com/hashicorp/go-sockaddr/template","checksumSHA1":"PDp9DVLvf3KWxhs4G4DpIwauMSU=","revision":"9b4c5fa5b10a683339a270d664474b9f4aee62fc","revisionTime":"2017-10-30T10:43:12Z"},
|
||||
{"path":"github.com/hashicorp/go-syslog","checksumSHA1":"xZ7Ban1x//6uUIU1xtrTbCYNHBc=","revision":"42a2b573b664dbf281bd48c3cc12c086b17a39ba","revisionTime":"2015-02-18T18:19:46Z"},
|
||||
{"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"},
|
||||
|
|
Loading…
Reference in New Issue