Skip to content

Commit

Permalink
host: use default preference value for single host IP
Browse files Browse the repository at this point in the history
Fixes gortc#6
  • Loading branch information
ernado committed Sep 5, 2018
1 parent ff09bc3 commit 085c91f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
41 changes: 30 additions & 11 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ func isV6Only(addrs []gather.Addr) bool {
return v6Only
}

func filterValid(gathered []gather.Addr) []gather.Addr {
valid := make([]gather.Addr, 0, len(gathered))
v6Only := isV6Only(gathered)
for _, addr := range gathered {
if !IsHostIPValid(addr.IP, v6Only) {
continue
}
valid = append(valid, addr)
}
return valid
}

const (
// When there is only a single IP address, this value SHOULD be
// set to 65535.
singleIPAddrPreference = 65535
)

// HostAddresses returns valid host addresses from gathered addresses with
// calculated local preference.
//
Expand All @@ -105,19 +123,20 @@ func HostAddresses(gathered []gather.Addr) ([]HostAddr, error) {
if len(gathered) == 0 {
return []HostAddr{}, nil
}
var (
v6Only = isV6Only(gathered)
validOnly = make([]gather.Addr, 0, len(gathered))
)
for _, addr := range gathered {
if !IsHostIPValid(addr.IP, v6Only) {
continue
}
validOnly = append(validOnly, addr)
}
validOnly := filterValid(gathered)
if len(validOnly) == 0 {
return []HostAddr{}, nil
}
if len(validOnly) == 1 {
// Setting local preference for single IP as defined
// in RFC 8445 Section 5.1.2.1.
return []HostAddr{
{
IP: validOnly[0].IP,
LocalPreference: singleIPAddrPreference,
},
}, nil
}
var (
v6Addrs, v4Addrs []gather.Addr
)
Expand All @@ -129,7 +148,7 @@ func HostAddresses(gathered []gather.Addr) ([]HostAddr, error) {
}
}
if len(v4Addrs) == 0 || len(v6Addrs) == 0 {
// Single-stack, possibly multi-homed.
// Single-stack and multi-homed.
hostAddrs := make([]HostAddr, 0, len(validOnly))
for i, a := range validOnly {
hostAddrs = append(hostAddrs, HostAddr{
Expand Down
18 changes: 18 additions & 0 deletions host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func TestGatherHostAddresses(t *testing.T) {
"127.0.0.1",
},
},
{
Name: "Single IPv4",
Input: []string{
"1.1.1.1",
},
Output: []outputRow{
{"1.1.1.1", 65535},
},
},
{
Name: "IPv4",
Input: []string{
Expand All @@ -92,6 +101,15 @@ func TestGatherHostAddresses(t *testing.T) {
{"1.1.1.2", 1},
},
},
{
Name: "Single IPv6",
Input: []string{
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa1",
},
Output: []outputRow{
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa1", 65535},
},
},
{
Name: "IPv6",
Input: []string{
Expand Down

0 comments on commit 085c91f

Please sign in to comment.