This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #782 from NebulousLabs/hostdb
Hostdb
- Loading branch information
Showing
11 changed files
with
223 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package modules | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
// A NetAddress contains the information needed to contact a peer. | ||
type NetAddress string | ||
|
||
// Host removes the port from a NetAddress, returning just the host. If the | ||
// address is invalid, the empty string is returned. | ||
func (na NetAddress) Host() string { | ||
host, _, err := net.SplitHostPort(string(na)) | ||
// 'host' is not always the empty string if an error is returned. | ||
if err != nil { | ||
return "" | ||
} | ||
return host | ||
} | ||
|
||
// Port returns the NetAddress object's port number. The empty string is | ||
// returned if the NetAddress is invalid. | ||
func (na NetAddress) Port() string { | ||
_, port, err := net.SplitHostPort(string(na)) | ||
// 'port' will not always be the empty string if an error is returned. | ||
if err != nil { | ||
return "" | ||
} | ||
return port | ||
} | ||
|
||
// IsLocal returns true for ip addresses that are on the same machine. | ||
func (na NetAddress) IsLocal() bool { | ||
if !na.IsValid() { | ||
return false | ||
} | ||
|
||
host := na.Host() | ||
if ip := net.ParseIP(host); ip != nil && ip.IsLoopback() { | ||
return true | ||
} | ||
if host == "localhost" { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// IsValid does nothing. Please ignore it. | ||
func (na NetAddress) IsValid() bool { | ||
_, _, err := net.SplitHostPort(string(na)) | ||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package modules | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// TestHost tests the Host method of the NetAddress type. | ||
func TestHost(t *testing.T) { | ||
testSet := []struct { | ||
query NetAddress | ||
desiredResponse string | ||
}{ | ||
{"localhost", ""}, | ||
{"localhost:1234", "localhost"}, | ||
{"127.0.0.1", ""}, | ||
{"127.0.0.1:6723", "127.0.0.1"}, | ||
{"bbc.com", ""}, | ||
{"bbc.com:6434", "bbc.com"}, | ||
{"bitcoin.ninja", ""}, | ||
{"bitcoin.ninja:6752", "bitcoin.ninja"}, | ||
{"garbage:64:77", ""}, | ||
{"::1:5856", ""}, | ||
{"[::1]:5856", "::1"}, | ||
{"[::1]", ""}, | ||
{"::1", ""}, | ||
} | ||
for _, test := range testSet { | ||
if test.query.Host() != test.desiredResponse { | ||
t.Error("test failed:", test, test.query.Host()) | ||
} | ||
} | ||
} | ||
|
||
// TestIsLocal tests the IsLocal method of the NetAddress type. | ||
func TestIsLocal(t *testing.T) { | ||
testSet := []struct { | ||
query NetAddress | ||
desiredResponse bool | ||
}{ | ||
// Networks such as 10.0.0.x have been omitted from testing - behavior | ||
// for these networks is currently undefined. | ||
|
||
// Localhost tests. | ||
{"localhost", false}, | ||
{"localhost:1234", true}, | ||
{"127.0.0.1", false}, | ||
{"127.0.0.1:6723", true}, | ||
{"::1", false}, | ||
{"[::1]:7124", true}, | ||
|
||
// Public name tests. | ||
{"hn.com", false}, | ||
{"hn.com:8811", false}, | ||
{"12.34.45.64", false}, | ||
{"12.34.45.64:7777", false}, | ||
{"::1:4646", false}, | ||
|
||
// Garbage name tests. | ||
{"", false}, | ||
{"garbage", false}, | ||
{"garbage:6432", false}, | ||
{"garbage:6146:616", false}, | ||
{"[::1]", false}, | ||
} | ||
for _, test := range testSet { | ||
if test.query.IsLocal() != test.desiredResponse { | ||
t.Error("test failed:", test, test.query.IsLocal()) | ||
} | ||
} | ||
} | ||
|
||
// TestIsValid checks where a netaddress matches the regex for what counts as a | ||
// valid hostname or ip address. | ||
func TestIsValid(t *testing.T) { | ||
testSet := []struct { | ||
query NetAddress | ||
desiredResponse bool | ||
}{ | ||
// Networks such as 10.0.0.x have been omitted from testing - behavior | ||
// for these networks is currently undefined. | ||
|
||
// Localhost tests. | ||
{"localhost", false}, | ||
{"localhost:1234", true}, | ||
{"127.0.0.1", false}, | ||
{"127.0.0.1:6723", true}, | ||
{"::1", false}, | ||
{"[::1]:7124", true}, | ||
|
||
// Public name tests. | ||
{"hn.com", false}, | ||
{"hn.com:8811", true}, | ||
{"12.34.45.64", false}, | ||
{"12.34.45.64:7777", true}, | ||
{"::1:4646", false}, | ||
{"plain", false}, | ||
{"plain:6432", true}, | ||
|
||
// Garbage name tests. | ||
{"", false}, | ||
{"garbage:6146:616", false}, | ||
{"[::1]", false}, | ||
// {"google.com:notAPort", false}, TODO: Failed test case. | ||
} | ||
for _, test := range testSet { | ||
if test.query.IsValid() != test.desiredResponse { | ||
t.Error("test failed:", test, test.query.IsValid()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.