Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions addrmgr/addrmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ type AddrManager struct {
type addrTypeFilter struct {
wantIPv4 bool
wantIPv6 bool
wantTORv3 bool
wantTorV3 bool
}

// bucketStats tracks the number of addresses by type within a single bucket.
type bucketStats struct {
numIPv4 uint16
numIPv6 uint16
numTORv3 uint16
numTorV3 uint16
}

// serializedKnownAddress is used to represent the serializable state of a
Expand Down Expand Up @@ -249,8 +249,8 @@ func (bs *bucketStats) increment(addrType NetAddressType) {
bs.numIPv4++
case IPv6Address:
bs.numIPv6++
case TORv3Address:
bs.numTORv3++
case TorV3Address:
bs.numTorV3++
}
}

Expand All @@ -261,8 +261,8 @@ func (bs *bucketStats) decrement(addrType NetAddressType) {
bs.numIPv4--
case IPv6Address:
bs.numIPv6--
case TORv3Address:
bs.numTORv3--
case TorV3Address:
bs.numTorV3--
}
}

Expand All @@ -275,8 +275,8 @@ func (bs *bucketStats) total(filter addrTypeFilter) int {
if filter.wantIPv6 {
sum += int(bs.numIPv6)
}
if filter.wantTORv3 {
sum += int(bs.numTORv3)
if filter.wantTorV3 {
sum += int(bs.numTorV3)
}
return sum
}
Expand All @@ -285,14 +285,14 @@ func (bs *bucketStats) total(filter addrTypeFilter) int {
func (bs *bucketStats) matches(filter addrTypeFilter) bool {
return (filter.wantIPv4 && bs.numIPv4 > 0) ||
(filter.wantIPv6 && bs.numIPv6 > 0) ||
(filter.wantTORv3 && bs.numTORv3 > 0)
(filter.wantTorV3 && bs.numTorV3 > 0)
}

// matches returns true if the address type matches the filter criteria.
func (f addrTypeFilter) matches(addrType NetAddressType) bool {
return (f.wantIPv4 && addrType == IPv4Address) ||
(f.wantIPv6 && addrType == IPv6Address) ||
(f.wantTORv3 && addrType == TORv3Address)
(f.wantTorV3 && addrType == TorV3Address)
}

// addOrUpdateAddress is a helper function to either update an address already known
Expand Down Expand Up @@ -829,16 +829,16 @@ func (a *AddrManager) reset() {
// returns the result. If the host string is not recognized as any known type,
// then an unknown address type is returned without error.
func EncodeHost(host string) (NetAddressType, []byte) {
// Check if this is a valid TORv3 address.
// Check if this is a valid TorV3 address.
if len(host) == 62 && strings.HasSuffix(host, ".onion") {
// TORv3 addresses tend to be lowercase by convention, but
// TorV3 addresses tend to be lowercase by convention, but
// Go's base32.StdEncoding.DecodeString expects uppercase
// input. Convert to uppercase for successful decoding.
torAddressBytes, err := base32.StdEncoding.DecodeString(
strings.ToUpper(host[:56]))
if err == nil {
if pubkey, valid := isTORv3(torAddressBytes); valid {
return TORv3Address, pubkey[:]
if pubkey, valid := isTorV3(torAddressBytes); valid {
return TorV3Address, pubkey[:]
}
}
}
Expand Down Expand Up @@ -872,10 +872,10 @@ func (a *AddrManager) GetAddress(filterFn NetAddressTypeFilter) *KnownAddress {
filter := addrTypeFilter{
wantIPv4: filterFn(IPv4Address),
wantIPv6: filterFn(IPv6Address),
wantTORv3: filterFn(TORv3Address),
wantTorV3: filterFn(TorV3Address),
}

if !filter.wantIPv4 && !filter.wantIPv6 && !filter.wantTORv3 {
if !filter.wantIPv4 && !filter.wantIPv6 && !filter.wantTorV3 {
return nil
}

Expand Down Expand Up @@ -1241,7 +1241,7 @@ const (
// Ipv6Strong represents a connection state between two IPv6 addresses.
Ipv6Strong

// Private represents a connection state between two TORv3 addresses.
// Private represents a connection state between two TorV3 addresses.
Private
)

Expand All @@ -1254,9 +1254,9 @@ func getRemoteReachabilityFromLocal(localAddr, remoteAddr *NetAddress) NetAddres
case !remoteAddr.IsRoutable():
return Unreachable

case remoteAddr.Type == TORv3Address:
case remoteAddr.Type == TorV3Address:
switch {
case localAddr.Type == TORv3Address:
case localAddr.Type == TorV3Address:
return Private
case localAddr.IsRoutable() && localAddr.Type == IPv4Address:
return Ipv4
Expand All @@ -1280,7 +1280,7 @@ func getRemoteReachabilityFromLocal(localAddr, remoteAddr *NetAddress) NetAddres
switch {
case localAddr.IsRoutable() && localAddr.Type == IPv4Address:
return Ipv4
case localAddr.Type == TORv3Address:
case localAddr.Type == TorV3Address:
return Ipv4
default:
return Unreachable
Expand All @@ -1294,7 +1294,7 @@ func getRemoteReachabilityFromLocal(localAddr, remoteAddr *NetAddress) NetAddres
return Teredo
case localAddr.Type == IPv4Address:
return Ipv4
case localAddr.Type == TORv3Address:
case localAddr.Type == TorV3Address:
return Ipv6Strong

// Is our IPv6 tunneled?
Expand Down
24 changes: 12 additions & 12 deletions addrmgr/addrmanager_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2014 The btcsuite developers
// Copyright (c) 2015-2025 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -41,9 +41,9 @@ func natfOnlyIPv6(addrType NetAddressType) bool {
return addrType == IPv6Address
}

// natfOnlyTORv3 defines a filter that will only allow TORv3 netAddrs.
func natfOnlyTORv3(addrType NetAddressType) bool {
return addrType == TORv3Address
// natfOnlyTorV3 defines a filter that will only allow TorV3 netAddrs.
func natfOnlyTorV3(addrType NetAddressType) bool {
return addrType == TorV3Address
}

// addAddressByIP is a convenience function that adds an address to the
Expand Down Expand Up @@ -708,7 +708,7 @@ func TestAddLocalAddress(t *testing.T) {
priority: InterfacePrio,
valid: true,
}, {
name: "routable TORv3 address",
name: "routable TorV3 address",
host: torv3Host,
priority: ManualPrio,
valid: true,
Expand Down Expand Up @@ -787,12 +787,12 @@ func TestGetBestLocalAddress(t *testing.T) {
newAddressFromIP(net.ParseIP("2001:470::1")),
}

// TORv3 address.
// TorV3 address.
torAddrType, torAddrBytes := EncodeHost(torv3Host)
torAddr, err := NewNetAddressFromParams(torAddrType, torAddrBytes, 0,
time.Unix(time.Now().Unix(), 0), wire.SFNodeNetwork)
if err != nil {
t.Fatalf("failed to create TORv3 NetAddress: %v", err)
t.Fatalf("failed to create TorV3 NetAddress: %v", err)
}

var tests = []struct {
Expand Down Expand Up @@ -892,7 +892,7 @@ func TestGetBestLocalAddress(t *testing.T) {
}
}

// Test4: Add TORv3 address with ManualPrio
// Test4: Add TorV3 address with ManualPrio
amgr.AddLocalAddress(torAddr, ManualPrio)
for x, test := range tests {
remoteAddr := test.remoteAddr
Expand Down Expand Up @@ -1120,10 +1120,10 @@ func TestGetAddressWithFilter(t *testing.T) {
filter: natfOnlyIPv6,
wantType: IPv6Address,
}, {
name: "returns address matching TORv3 filter",
name: "returns address matching TorV3 filter",
addresses: []*NetAddress{ipv4Addr, ipv6Addr, torv3Addr},
filter: natfOnlyTORv3,
wantType: TORv3Address,
filter: natfOnlyTorV3,
wantType: TorV3Address,
}, {
name: "returns nil when no matching IPv4 addresses",
addresses: []*NetAddress{ipv6Addr},
Expand All @@ -1136,7 +1136,7 @@ func TestGetAddressWithFilter(t *testing.T) {
wantNil: true,
}, {
name: "returns nil when address manager empty",
addresses: []*NetAddress{},
addresses: nil,
filter: natfAny,
wantNil: true,
}}
Expand Down
10 changes: 5 additions & 5 deletions addrmgr/netaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type NetAddress struct {
// IsRoutable returns a boolean indicating whether the network address is
// routable.
func (netAddr *NetAddress) IsRoutable() bool {
if netAddr.Type == TORv3Address {
if netAddr.Type == TorV3Address {
return true
}
return IsRoutable(netAddr.IP)
Expand All @@ -53,10 +53,10 @@ func (netAddr *NetAddress) ipString() string {
return net.IP(netIP).String()
case IPv4Address:
return net.IP(netIP).String()
case TORv3Address:
case TorV3Address:
var publicKey [32]byte
copy(publicKey[:], netIP)
checksum := calcTORv3Checksum(publicKey)
checksum := calcTorV3Checksum(publicKey)
var torAddressBytes [35]byte
copy(torAddressBytes[:32], publicKey[:])
copy(torAddressBytes[32:34], checksum[:])
Expand Down Expand Up @@ -105,8 +105,8 @@ func deriveNetAddressType(claimedType NetAddressType, addrBytes []byte) (NetAddr
return IPv4Address, nil
case len == 16:
return IPv6Address, nil
case len == 32 && claimedType == TORv3Address:
return TORv3Address, nil
case len == 32 && claimedType == TorV3Address:
return TorV3Address, nil
}
str := fmt.Sprintf("unable to determine address type from raw network "+
"address bytes: %v", addrBytes)
Expand Down
6 changes: 3 additions & 3 deletions addrmgr/netaddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestKey(t *testing.T) {
{host: "fee2::3:3", port: 8335, want: "[fee2::3:3]:8335"},
{host: "fef3::4:4", port: 8336, want: "[fef3::4:4]:8336"},

// TORv3
// TorV3
{host: torAddress, port: 8333, want: torAddress + ":8333"},
}

Expand Down Expand Up @@ -220,14 +220,14 @@ func TestNewNetAddressFromParams(t *testing.T) {
},
{
name: "32 byte torv3 address stored in 32 bytes",
addrType: TORv3Address,
addrType: TorV3Address,
addrBytes: torAddressBytes,
want: &NetAddress{
IP: torAddressBytes,
Port: port,
Services: services,
Timestamp: timestamp,
Type: TORv3Address,
Type: TorV3Address,
},
error_expected: false,
},
Expand Down
16 changes: 8 additions & 8 deletions addrmgr/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const (
IPv4Address NetAddressType = 1
IPv6Address NetAddressType = 2
// TorV2Address NetAddressType = 3 // No longer supported
TORv3Address NetAddressType = 4
TorV3Address NetAddressType = 4
)

// NetAddressTypeFilter represents a function that returns whether a particular
Expand Down Expand Up @@ -212,9 +212,9 @@ func isRFC6598(netIP net.IP) bool {
return rfc6598Net.Contains(netIP)
}

// calcTORv3Checksum returns the checksum bytes given a 32 byte
// TORv3 public key.
func calcTORv3Checksum(publicKey [32]byte) [2]byte {
// calcTorV3Checksum returns the checksum bytes given a 32-byte TorV3 public
// key.
func calcTorV3Checksum(publicKey [32]byte) [2]byte {
const (
prefix = ".onion checksum"
prefixLen = len(prefix)
Expand All @@ -231,10 +231,10 @@ func calcTORv3Checksum(publicKey [32]byte) [2]byte {
return result
}

// isTORv3 returns whether or not the passed address is a valid TORv3 address
// isTorV3 returns whether or not the passed address is a valid TorV3 address
// with the checksum and version bytes. If it is valid, it also returns the
// public key of the tor v3 address.
func isTORv3(addressBytes []byte) ([32]byte, bool) {
func isTorV3(addressBytes []byte) ([32]byte, bool) {
var publicKey [32]byte
if len(addressBytes) != 35 {
return publicKey, false
Expand All @@ -246,7 +246,7 @@ func isTORv3(addressBytes []byte) ([32]byte, bool) {
}

copy(publicKey[:], addressBytes[:32])
computedChecksum := calcTORv3Checksum(publicKey)
computedChecksum := calcTorV3Checksum(publicKey)

var checksum [2]byte
copy(checksum[:], addressBytes[32:34])
Expand Down Expand Up @@ -284,7 +284,7 @@ func IsRoutable(netIP net.IP) bool {
// address.
func (na *NetAddress) GroupKey() string {
netIP := net.IP(na.IP)
if na.Type == TORv3Address {
if na.Type == TorV3Address {
// Group is keyed off the first 4 bits of the public key.
return fmt.Sprintf("torv3:%d", netIP[0]&0xf)
}
Expand Down
2 changes: 1 addition & 1 deletion addrmgr/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestGroupKey(t *testing.T) {
{name: "ipv6 hurricane electric", host: "2001:470:1f10:a1::2", expected: "2001:470:1000::"},
{name: "ipv6 hurricane electric 2", host: "2001:0470:1f10:a1::2", expected: "2001:470:1000::"},

// TORv3
// TorV3
{
name: "torv3",
host: "xa4r2iadxm55fbnqgwwi5mymqdcofiu3w6rpbtqn7b2dyn7mgwj64jyd.onion",
Expand Down
13 changes: 5 additions & 8 deletions peer/peer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2016-2025 The Decred developers
// Copyright (c) 2016-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -30,7 +30,7 @@ import (

const (
// MaxProtocolVersion is the max protocol version the peer supports.
MaxProtocolVersion = wire.TORv3Version
MaxProtocolVersion = wire.AddrV2Version

// outputBufferSize is the number of elements the output channels use.
outputBufferSize = 5000
Expand Down Expand Up @@ -898,10 +898,7 @@ func (p *Peer) PushAddrV2Msg(addresses []wire.NetAddressV2) []wire.NetAddressV2
addrs = addrs[:wire.MaxAddrPerV2Msg]
}

msg := wire.NewMsgAddrV2()
msg.AddrList = addrs

p.QueueMessage(msg, nil)
p.QueueMessage(wire.NewMsgAddrV2(addrs), nil)
return addrs
}

Expand Down Expand Up @@ -2077,14 +2074,14 @@ func (p *Peer) localVersionMsg() (*wire.MsgVersion, error) {
if p.cfg.Proxy != "" {
proxyaddress, _, err := net.SplitHostPort(p.cfg.Proxy)
// invalid proxy means poorly configured, be on the safe side.
if err != nil || net.IP(p.na.IP).String() == proxyaddress {
if err != nil || net.IP(p.na.EncodedAddr).String() == proxyaddress {
theirNA = wire.NewNetAddressIPPort(net.IP([]byte{0, 0, 0, 0}), 0,
peerNA.Services)
}
}
if theirNA == nil {
theirNA = wire.NewNetAddressTimestamp(peerNA.Timestamp, peerNA.Services,
peerNA.IP, peerNA.Port)
peerNA.EncodedAddr, peerNA.Port)
}

// Create a wire.NetAddress with only the services set to use as the
Expand Down
Loading