github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/addrmgr/addrmanager.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package addrmgr
     7  
     8  import (
     9  	"container/list"
    10  	crand "crypto/rand" // for seeding
    11  	"encoding/base32"
    12  	"encoding/binary"
    13  	"encoding/json"
    14  	"fmt"
    15  	"io"
    16  	"math/rand"
    17  	"net"
    18  	"os"
    19  	"path/filepath"
    20  	"strconv"
    21  	"strings"
    22  	"sync"
    23  	"sync/atomic"
    24  	"time"
    25  
    26  	"github.com/dashpay/godash/wire"
    27  )
    28  
    29  // AddrManager provides a concurrency safe address manager for caching potential
    30  // peers on the bitcoin network.
    31  type AddrManager struct {
    32  	mtx            sync.Mutex
    33  	peersFile      string
    34  	lookupFunc     func(string) ([]net.IP, error)
    35  	rand           *rand.Rand
    36  	key            [32]byte
    37  	addrIndex      map[string]*KnownAddress // address key to ka for all addrs.
    38  	addrNew        [newBucketCount]map[string]*KnownAddress
    39  	addrTried      [triedBucketCount]*list.List
    40  	started        int32
    41  	shutdown       int32
    42  	wg             sync.WaitGroup
    43  	quit           chan struct{}
    44  	nTried         int
    45  	nNew           int
    46  	lamtx          sync.Mutex
    47  	localAddresses map[string]*localAddress
    48  }
    49  
    50  type serializedKnownAddress struct {
    51  	Addr        string
    52  	Src         string
    53  	Attempts    int
    54  	TimeStamp   int64
    55  	LastAttempt int64
    56  	LastSuccess int64
    57  	// no refcount or tried, that is available from context.
    58  }
    59  
    60  type serializedAddrManager struct {
    61  	Version      int
    62  	Key          [32]byte
    63  	Addresses    []*serializedKnownAddress
    64  	NewBuckets   [newBucketCount][]string // string is NetAddressKey
    65  	TriedBuckets [triedBucketCount][]string
    66  }
    67  
    68  type localAddress struct {
    69  	na    *wire.NetAddress
    70  	score AddressPriority
    71  }
    72  
    73  // AddressPriority type is used to describe the hierarchy of local address
    74  // discovery methods.
    75  type AddressPriority int
    76  
    77  const (
    78  	// InterfacePrio signifies the address is on a local interface
    79  	InterfacePrio AddressPriority = iota
    80  
    81  	// BoundPrio signifies the address has been explicitly bounded to.
    82  	BoundPrio
    83  
    84  	// UpnpPrio signifies the address was obtained from UPnP.
    85  	UpnpPrio
    86  
    87  	// HTTPPrio signifies the address was obtained from an external HTTP service.
    88  	HTTPPrio
    89  
    90  	// ManualPrio signifies the address was provided by --externalip.
    91  	ManualPrio
    92  )
    93  
    94  const (
    95  	// needAddressThreshold is the number of addresses under which the
    96  	// address manager will claim to need more addresses.
    97  	needAddressThreshold = 1000
    98  
    99  	newAddressBufferSize = 50
   100  
   101  	// dumpAddressInterval is the interval used to dump the address
   102  	// cache to disk for future use.
   103  	dumpAddressInterval = time.Minute * 10
   104  
   105  	// triedBucketSize is the maximum number of addresses in each
   106  	// tried address bucket.
   107  	triedBucketSize = 256
   108  
   109  	// triedBucketCount is the number of buckets we split tried
   110  	// addresses over.
   111  	triedBucketCount = 64
   112  
   113  	// newBucketSize is the maximum number of addresses in each new address
   114  	// bucket.
   115  	newBucketSize = 64
   116  
   117  	// newBucketCount is the number of buckets that we spread new addresses
   118  	// over.
   119  	newBucketCount = 1024
   120  
   121  	// triedBucketsPerGroup is the number of tried buckets over which an
   122  	// address group will be spread.
   123  	triedBucketsPerGroup = 8
   124  
   125  	// newBucketsPerGroup is the number of new buckets over which an
   126  	// source address group will be spread.
   127  	newBucketsPerGroup = 64
   128  
   129  	// newBucketsPerAddress is the number of buckets a frequently seen new
   130  	// address may end up in.
   131  	newBucketsPerAddress = 8
   132  
   133  	// numMissingDays is the number of days before which we assume an
   134  	// address has vanished if we have not seen it announced  in that long.
   135  	numMissingDays = 30
   136  
   137  	// numRetries is the number of tried without a single success before
   138  	// we assume an address is bad.
   139  	numRetries = 3
   140  
   141  	// maxFailures is the maximum number of failures we will accept without
   142  	// a success before considering an address bad.
   143  	maxFailures = 10
   144  
   145  	// minBadDays is the number of days since the last success before we
   146  	// will consider evicting an address.
   147  	minBadDays = 7
   148  
   149  	// getAddrMax is the most addresses that we will send in response
   150  	// to a getAddr (in practise the most addresses we will return from a
   151  	// call to AddressCache()).
   152  	getAddrMax = 2500
   153  
   154  	// getAddrPercent is the percentage of total addresses known that we
   155  	// will share with a call to AddressCache.
   156  	getAddrPercent = 23
   157  
   158  	// serialisationVersion is the current version of the on-disk format.
   159  	serialisationVersion = 1
   160  )
   161  
   162  // updateAddress is a helper function to either update an address already known
   163  // to the address manager, or to add the address if not already known.
   164  func (a *AddrManager) updateAddress(netAddr, srcAddr *wire.NetAddress) {
   165  	// Filter out non-routable addresses. Note that non-routable
   166  	// also includes invalid and local addresses.
   167  	if !IsRoutable(netAddr) {
   168  		return
   169  	}
   170  
   171  	addr := NetAddressKey(netAddr)
   172  	ka := a.find(netAddr)
   173  	if ka != nil {
   174  		// TODO(oga) only update addresses periodically.
   175  		// Update the last seen time and services.
   176  		// note that to prevent causing excess garbage on getaddr
   177  		// messages the netaddresses in addrmaanger are *immutable*,
   178  		// if we need to change them then we replace the pointer with a
   179  		// new copy so that we don't have to copy every na for getaddr.
   180  		if netAddr.Timestamp.After(ka.na.Timestamp) ||
   181  			(ka.na.Services&netAddr.Services) !=
   182  				netAddr.Services {
   183  
   184  			naCopy := *ka.na
   185  			naCopy.Timestamp = netAddr.Timestamp
   186  			naCopy.AddService(netAddr.Services)
   187  			ka.na = &naCopy
   188  		}
   189  
   190  		// If already in tried, we have nothing to do here.
   191  		if ka.tried {
   192  			return
   193  		}
   194  
   195  		// Already at our max?
   196  		if ka.refs == newBucketsPerAddress {
   197  			return
   198  		}
   199  
   200  		// The more entries we have, the less likely we are to add more.
   201  		// likelihood is 2N.
   202  		factor := int32(2 * ka.refs)
   203  		if a.rand.Int31n(factor) != 0 {
   204  			return
   205  		}
   206  	} else {
   207  		// Make a copy of the net address to avoid races since it is
   208  		// updated elsewhere in the addrmanager code and would otherwise
   209  		// change the actual netaddress on the peer.
   210  		netAddrCopy := *netAddr
   211  		ka = &KnownAddress{na: &netAddrCopy, srcAddr: srcAddr}
   212  		a.addrIndex[addr] = ka
   213  		a.nNew++
   214  		// XXX time penalty?
   215  	}
   216  
   217  	bucket := a.getNewBucket(netAddr, srcAddr)
   218  
   219  	// Already exists?
   220  	if _, ok := a.addrNew[bucket][addr]; ok {
   221  		return
   222  	}
   223  
   224  	// Enforce max addresses.
   225  	if len(a.addrNew[bucket]) > newBucketSize {
   226  		log.Tracef("new bucket is full, expiring old")
   227  		a.expireNew(bucket)
   228  	}
   229  
   230  	// Add to new bucket.
   231  	ka.refs++
   232  	a.addrNew[bucket][addr] = ka
   233  
   234  	log.Tracef("Added new address %s for a total of %d addresses", addr,
   235  		a.nTried+a.nNew)
   236  }
   237  
   238  // expireNew makes space in the new buckets by expiring the really bad entries.
   239  // If no bad entries are available we look at a few and remove the oldest.
   240  func (a *AddrManager) expireNew(bucket int) {
   241  	// First see if there are any entries that are so bad we can just throw
   242  	// them away. otherwise we throw away the oldest entry in the cache.
   243  	// Bitcoind here chooses four random and just throws the oldest of
   244  	// those away, but we keep track of oldest in the initial traversal and
   245  	// use that information instead.
   246  	var oldest *KnownAddress
   247  	for k, v := range a.addrNew[bucket] {
   248  		if v.isBad() {
   249  			log.Tracef("expiring bad address %v", k)
   250  			delete(a.addrNew[bucket], k)
   251  			v.refs--
   252  			if v.refs == 0 {
   253  				a.nNew--
   254  				delete(a.addrIndex, k)
   255  			}
   256  			continue
   257  		}
   258  		if oldest == nil {
   259  			oldest = v
   260  		} else if !v.na.Timestamp.After(oldest.na.Timestamp) {
   261  			oldest = v
   262  		}
   263  	}
   264  
   265  	if oldest != nil {
   266  		key := NetAddressKey(oldest.na)
   267  		log.Tracef("expiring oldest address %v", key)
   268  
   269  		delete(a.addrNew[bucket], key)
   270  		oldest.refs--
   271  		if oldest.refs == 0 {
   272  			a.nNew--
   273  			delete(a.addrIndex, key)
   274  		}
   275  	}
   276  }
   277  
   278  // pickTried selects an address from the tried bucket to be evicted.
   279  // We just choose the eldest. Bitcoind selects 4 random entries and throws away
   280  // the older of them.
   281  func (a *AddrManager) pickTried(bucket int) *list.Element {
   282  	var oldest *KnownAddress
   283  	var oldestElem *list.Element
   284  	for e := a.addrTried[bucket].Front(); e != nil; e = e.Next() {
   285  		ka := e.Value.(*KnownAddress)
   286  		if oldest == nil || oldest.na.Timestamp.After(ka.na.Timestamp) {
   287  			oldestElem = e
   288  			oldest = ka
   289  		}
   290  
   291  	}
   292  	return oldestElem
   293  }
   294  
   295  func (a *AddrManager) getNewBucket(netAddr, srcAddr *wire.NetAddress) int {
   296  	// bitcoind:
   297  	// doublesha256(key + sourcegroup + int64(doublesha256(key + group + sourcegroup))%bucket_per_source_group) % num_new_buckets
   298  
   299  	data1 := []byte{}
   300  	data1 = append(data1, a.key[:]...)
   301  	data1 = append(data1, []byte(GroupKey(netAddr))...)
   302  	data1 = append(data1, []byte(GroupKey(srcAddr))...)
   303  	hash1 := wire.DoubleSha256(data1)
   304  	hash64 := binary.LittleEndian.Uint64(hash1)
   305  	hash64 %= newBucketsPerGroup
   306  	var hashbuf [8]byte
   307  	binary.LittleEndian.PutUint64(hashbuf[:], hash64)
   308  	data2 := []byte{}
   309  	data2 = append(data2, a.key[:]...)
   310  	data2 = append(data2, GroupKey(srcAddr)...)
   311  	data2 = append(data2, hashbuf[:]...)
   312  
   313  	hash2 := wire.DoubleSha256(data2)
   314  	return int(binary.LittleEndian.Uint64(hash2) % newBucketCount)
   315  }
   316  
   317  func (a *AddrManager) getTriedBucket(netAddr *wire.NetAddress) int {
   318  	// bitcoind hashes this as:
   319  	// doublesha256(key + group + truncate_to_64bits(doublesha256(key)) % buckets_per_group) % num_buckets
   320  	data1 := []byte{}
   321  	data1 = append(data1, a.key[:]...)
   322  	data1 = append(data1, []byte(NetAddressKey(netAddr))...)
   323  	hash1 := wire.DoubleSha256(data1)
   324  	hash64 := binary.LittleEndian.Uint64(hash1)
   325  	hash64 %= triedBucketsPerGroup
   326  	var hashbuf [8]byte
   327  	binary.LittleEndian.PutUint64(hashbuf[:], hash64)
   328  	data2 := []byte{}
   329  	data2 = append(data2, a.key[:]...)
   330  	data2 = append(data2, GroupKey(netAddr)...)
   331  	data2 = append(data2, hashbuf[:]...)
   332  
   333  	hash2 := wire.DoubleSha256(data2)
   334  	return int(binary.LittleEndian.Uint64(hash2) % triedBucketCount)
   335  }
   336  
   337  // addressHandler is the main handler for the address manager.  It must be run
   338  // as a goroutine.
   339  func (a *AddrManager) addressHandler() {
   340  	dumpAddressTicker := time.NewTicker(dumpAddressInterval)
   341  	defer dumpAddressTicker.Stop()
   342  out:
   343  	for {
   344  		select {
   345  		case <-dumpAddressTicker.C:
   346  			a.savePeers()
   347  
   348  		case <-a.quit:
   349  			break out
   350  		}
   351  	}
   352  	a.savePeers()
   353  	a.wg.Done()
   354  	log.Trace("Address handler done")
   355  }
   356  
   357  // savePeers saves all the known addresses to a file so they can be read back
   358  // in at next run.
   359  func (a *AddrManager) savePeers() {
   360  	a.mtx.Lock()
   361  	defer a.mtx.Unlock()
   362  
   363  	// First we make a serialisable datastructure so we can encode it to
   364  	// json.
   365  	sam := new(serializedAddrManager)
   366  	sam.Version = serialisationVersion
   367  	copy(sam.Key[:], a.key[:])
   368  
   369  	sam.Addresses = make([]*serializedKnownAddress, len(a.addrIndex))
   370  	i := 0
   371  	for k, v := range a.addrIndex {
   372  		ska := new(serializedKnownAddress)
   373  		ska.Addr = k
   374  		ska.TimeStamp = v.na.Timestamp.Unix()
   375  		ska.Src = NetAddressKey(v.srcAddr)
   376  		ska.Attempts = v.attempts
   377  		ska.LastAttempt = v.lastattempt.Unix()
   378  		ska.LastSuccess = v.lastsuccess.Unix()
   379  		// Tried and refs are implicit in the rest of the structure
   380  		// and will be worked out from context on unserialisation.
   381  		sam.Addresses[i] = ska
   382  		i++
   383  	}
   384  	for i := range a.addrNew {
   385  		sam.NewBuckets[i] = make([]string, len(a.addrNew[i]))
   386  		j := 0
   387  		for k := range a.addrNew[i] {
   388  			sam.NewBuckets[i][j] = k
   389  			j++
   390  		}
   391  	}
   392  	for i := range a.addrTried {
   393  		sam.TriedBuckets[i] = make([]string, a.addrTried[i].Len())
   394  		j := 0
   395  		for e := a.addrTried[i].Front(); e != nil; e = e.Next() {
   396  			ka := e.Value.(*KnownAddress)
   397  			sam.TriedBuckets[i][j] = NetAddressKey(ka.na)
   398  			j++
   399  		}
   400  	}
   401  
   402  	w, err := os.Create(a.peersFile)
   403  	if err != nil {
   404  		log.Errorf("Error opening file %s: %v", a.peersFile, err)
   405  		return
   406  	}
   407  	enc := json.NewEncoder(w)
   408  	defer w.Close()
   409  	if err := enc.Encode(&sam); err != nil {
   410  		log.Errorf("Failed to encode file %s: %v", a.peersFile, err)
   411  		return
   412  	}
   413  }
   414  
   415  // loadPeers loads the known address from the saved file.  If empty, missing, or
   416  // malformed file, just don't load anything and start fresh
   417  func (a *AddrManager) loadPeers() {
   418  	a.mtx.Lock()
   419  	defer a.mtx.Unlock()
   420  
   421  	err := a.deserializePeers(a.peersFile)
   422  	if err != nil {
   423  		log.Errorf("Failed to parse file %s: %v", a.peersFile, err)
   424  		// if it is invalid we nuke the old one unconditionally.
   425  		err = os.Remove(a.peersFile)
   426  		if err != nil {
   427  			log.Warnf("Failed to remove corrupt peers file %s: %v",
   428  				a.peersFile, err)
   429  		}
   430  		a.reset()
   431  		return
   432  	}
   433  	log.Infof("Loaded %d addresses from file '%s'", a.numAddresses(), a.peersFile)
   434  }
   435  
   436  func (a *AddrManager) deserializePeers(filePath string) error {
   437  
   438  	_, err := os.Stat(filePath)
   439  	if os.IsNotExist(err) {
   440  		return nil
   441  	}
   442  	r, err := os.Open(filePath)
   443  	if err != nil {
   444  		return fmt.Errorf("%s error opening file: %v", filePath, err)
   445  	}
   446  	defer r.Close()
   447  
   448  	var sam serializedAddrManager
   449  	dec := json.NewDecoder(r)
   450  	err = dec.Decode(&sam)
   451  	if err != nil {
   452  		return fmt.Errorf("error reading %s: %v", filePath, err)
   453  	}
   454  
   455  	if sam.Version != serialisationVersion {
   456  		return fmt.Errorf("unknown version %v in serialized "+
   457  			"addrmanager", sam.Version)
   458  	}
   459  	copy(a.key[:], sam.Key[:])
   460  
   461  	for _, v := range sam.Addresses {
   462  		ka := new(KnownAddress)
   463  		ka.na, err = a.DeserializeNetAddress(v.Addr)
   464  		if err != nil {
   465  			return fmt.Errorf("failed to deserialize netaddress "+
   466  				"%s: %v", v.Addr, err)
   467  		}
   468  		ka.srcAddr, err = a.DeserializeNetAddress(v.Src)
   469  		if err != nil {
   470  			return fmt.Errorf("failed to deserialize netaddress "+
   471  				"%s: %v", v.Src, err)
   472  		}
   473  		ka.attempts = v.Attempts
   474  		ka.lastattempt = time.Unix(v.LastAttempt, 0)
   475  		ka.lastsuccess = time.Unix(v.LastSuccess, 0)
   476  		a.addrIndex[NetAddressKey(ka.na)] = ka
   477  	}
   478  
   479  	for i := range sam.NewBuckets {
   480  		for _, val := range sam.NewBuckets[i] {
   481  			ka, ok := a.addrIndex[val]
   482  			if !ok {
   483  				return fmt.Errorf("newbucket contains %s but "+
   484  					"none in address list", val)
   485  			}
   486  
   487  			if ka.refs == 0 {
   488  				a.nNew++
   489  			}
   490  			ka.refs++
   491  			a.addrNew[i][val] = ka
   492  		}
   493  	}
   494  	for i := range sam.TriedBuckets {
   495  		for _, val := range sam.TriedBuckets[i] {
   496  			ka, ok := a.addrIndex[val]
   497  			if !ok {
   498  				return fmt.Errorf("Newbucket contains %s but "+
   499  					"none in address list", val)
   500  			}
   501  
   502  			ka.tried = true
   503  			a.nTried++
   504  			a.addrTried[i].PushBack(ka)
   505  		}
   506  	}
   507  
   508  	// Sanity checking.
   509  	for k, v := range a.addrIndex {
   510  		if v.refs == 0 && !v.tried {
   511  			return fmt.Errorf("address %s after serialisation "+
   512  				"with no references", k)
   513  		}
   514  
   515  		if v.refs > 0 && v.tried {
   516  			return fmt.Errorf("address %s after serialisation "+
   517  				"which is both new and tried!", k)
   518  		}
   519  	}
   520  
   521  	return nil
   522  }
   523  
   524  // DeserializeNetAddress converts a given address string to a *wire.NetAddress
   525  func (a *AddrManager) DeserializeNetAddress(addr string) (*wire.NetAddress, error) {
   526  	host, portStr, err := net.SplitHostPort(addr)
   527  	if err != nil {
   528  		return nil, err
   529  	}
   530  	port, err := strconv.ParseUint(portStr, 10, 16)
   531  	if err != nil {
   532  		return nil, err
   533  	}
   534  
   535  	return a.HostToNetAddress(host, uint16(port), wire.SFNodeNetwork)
   536  }
   537  
   538  // Start begins the core address handler which manages a pool of known
   539  // addresses, timeouts, and interval based writes.
   540  func (a *AddrManager) Start() {
   541  	// Already started?
   542  	if atomic.AddInt32(&a.started, 1) != 1 {
   543  		return
   544  	}
   545  
   546  	log.Trace("Starting address manager")
   547  
   548  	// Load peers we already know about from file.
   549  	a.loadPeers()
   550  
   551  	// Start the address ticker to save addresses periodically.
   552  	a.wg.Add(1)
   553  	go a.addressHandler()
   554  }
   555  
   556  // Stop gracefully shuts down the address manager by stopping the main handler.
   557  func (a *AddrManager) Stop() error {
   558  	if atomic.AddInt32(&a.shutdown, 1) != 1 {
   559  		log.Warnf("Address manager is already in the process of " +
   560  			"shutting down")
   561  		return nil
   562  	}
   563  
   564  	log.Infof("Address manager shutting down")
   565  	close(a.quit)
   566  	a.wg.Wait()
   567  	return nil
   568  }
   569  
   570  // AddAddresses adds new addresses to the address manager.  It enforces a max
   571  // number of addresses and silently ignores duplicate addresses.  It is
   572  // safe for concurrent access.
   573  func (a *AddrManager) AddAddresses(addrs []*wire.NetAddress, srcAddr *wire.NetAddress) {
   574  	a.mtx.Lock()
   575  	defer a.mtx.Unlock()
   576  
   577  	for _, na := range addrs {
   578  		a.updateAddress(na, srcAddr)
   579  	}
   580  }
   581  
   582  // AddAddress adds a new address to the address manager.  It enforces a max
   583  // number of addresses and silently ignores duplicate addresses.  It is
   584  // safe for concurrent access.
   585  func (a *AddrManager) AddAddress(addr, srcAddr *wire.NetAddress) {
   586  	a.mtx.Lock()
   587  	defer a.mtx.Unlock()
   588  
   589  	a.updateAddress(addr, srcAddr)
   590  }
   591  
   592  // AddAddressByIP adds an address where we are given an ip:port and not a
   593  // wire.NetAddress.
   594  func (a *AddrManager) AddAddressByIP(addrIP string) error {
   595  	// Split IP and port
   596  	addr, portStr, err := net.SplitHostPort(addrIP)
   597  	if err != nil {
   598  		return err
   599  	}
   600  	// Put it in wire.Netaddress
   601  	var na wire.NetAddress
   602  	na.Timestamp = time.Now()
   603  	na.IP = net.ParseIP(addr)
   604  	if na.IP == nil {
   605  		return fmt.Errorf("invalid ip address %s", addr)
   606  	}
   607  	port, err := strconv.ParseUint(portStr, 10, 0)
   608  	if err != nil {
   609  		return fmt.Errorf("invalid port %s: %v", portStr, err)
   610  	}
   611  	na.Port = uint16(port)
   612  	a.AddAddress(&na, &na) // XXX use correct src address
   613  	return nil
   614  }
   615  
   616  // NumAddresses returns the number of addresses known to the address manager.
   617  func (a *AddrManager) numAddresses() int {
   618  	return a.nTried + a.nNew
   619  }
   620  
   621  // NumAddresses returns the number of addresses known to the address manager.
   622  func (a *AddrManager) NumAddresses() int {
   623  	a.mtx.Lock()
   624  	defer a.mtx.Unlock()
   625  
   626  	return a.numAddresses()
   627  }
   628  
   629  // NeedMoreAddresses returns whether or not the address manager needs more
   630  // addresses.
   631  func (a *AddrManager) NeedMoreAddresses() bool {
   632  	a.mtx.Lock()
   633  	defer a.mtx.Unlock()
   634  
   635  	return a.numAddresses() < needAddressThreshold
   636  }
   637  
   638  // AddressCache returns the current address cache.  It must be treated as
   639  // read-only (but since it is a copy now, this is not as dangerous).
   640  func (a *AddrManager) AddressCache() []*wire.NetAddress {
   641  	a.mtx.Lock()
   642  	defer a.mtx.Unlock()
   643  	if a.nNew+a.nTried == 0 {
   644  		return nil
   645  	}
   646  
   647  	allAddr := make([]*wire.NetAddress, a.nNew+a.nTried)
   648  	i := 0
   649  	// Iteration order is undefined here, but we randomise it anyway.
   650  	for _, v := range a.addrIndex {
   651  		allAddr[i] = v.na
   652  		i++
   653  	}
   654  
   655  	numAddresses := len(allAddr) * getAddrPercent / 100
   656  	if numAddresses > getAddrMax {
   657  		numAddresses = getAddrMax
   658  	}
   659  
   660  	// Fisher-Yates shuffle the array. We only need to do the first
   661  	// `numAddresses' since we are throwing the rest.
   662  	for i := 0; i < numAddresses; i++ {
   663  		// pick a number between current index and the end
   664  		j := rand.Intn(len(allAddr)-i) + i
   665  		allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
   666  	}
   667  
   668  	// slice off the limit we are willing to share.
   669  	return allAddr[:numAddresses]
   670  }
   671  
   672  // reset resets the address manager by reinitialising the random source
   673  // and allocating fresh empty bucket storage.
   674  func (a *AddrManager) reset() {
   675  
   676  	a.addrIndex = make(map[string]*KnownAddress)
   677  
   678  	// fill key with bytes from a good random source.
   679  	io.ReadFull(crand.Reader, a.key[:])
   680  	for i := range a.addrNew {
   681  		a.addrNew[i] = make(map[string]*KnownAddress)
   682  	}
   683  	for i := range a.addrTried {
   684  		a.addrTried[i] = list.New()
   685  	}
   686  }
   687  
   688  // HostToNetAddress returns a netaddress given a host address. If the address is
   689  // a tor .onion address this will be taken care of. else if the host is not an
   690  // IP address it will be resolved (via tor if required).
   691  func (a *AddrManager) HostToNetAddress(host string, port uint16, services wire.ServiceFlag) (*wire.NetAddress, error) {
   692  	// tor address is 16 char base32 + ".onion"
   693  	var ip net.IP
   694  	if len(host) == 22 && host[16:] == ".onion" {
   695  		// go base32 encoding uses capitals (as does the rfc
   696  		// but tor and bitcoind tend to user lowercase, so we switch
   697  		// case here.
   698  		data, err := base32.StdEncoding.DecodeString(
   699  			strings.ToUpper(host[:16]))
   700  		if err != nil {
   701  			return nil, err
   702  		}
   703  		prefix := []byte{0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43}
   704  		ip = net.IP(append(prefix, data...))
   705  	} else if ip = net.ParseIP(host); ip == nil {
   706  		ips, err := a.lookupFunc(host)
   707  		if err != nil {
   708  			return nil, err
   709  		}
   710  		if len(ips) == 0 {
   711  			return nil, fmt.Errorf("no addresses found for %s", host)
   712  		}
   713  		ip = ips[0]
   714  	}
   715  
   716  	return wire.NewNetAddressIPPort(ip, port, services), nil
   717  }
   718  
   719  // ipString returns a string for the ip from the provided NetAddress. If the
   720  // ip is in the range used for tor addresses then it will be transformed into
   721  // the relevant .onion address.
   722  func ipString(na *wire.NetAddress) string {
   723  	if IsOnionCatTor(na) {
   724  		// We know now that na.IP is long enogh.
   725  		base32 := base32.StdEncoding.EncodeToString(na.IP[6:])
   726  		return strings.ToLower(base32) + ".onion"
   727  	}
   728  
   729  	return na.IP.String()
   730  }
   731  
   732  // NetAddressKey returns a string key in the form of ip:port for IPv4 addresses
   733  // or [ip]:port for IPv6 addresses.
   734  func NetAddressKey(na *wire.NetAddress) string {
   735  	port := strconv.FormatUint(uint64(na.Port), 10)
   736  
   737  	return net.JoinHostPort(ipString(na), port)
   738  }
   739  
   740  // GetAddress returns a single address that should be routable.  It picks a
   741  // random one from the possible addresses with preference given to ones that
   742  // have not been used recently and should not pick 'close' addresses
   743  // consecutively.
   744  func (a *AddrManager) GetAddress(class string) *KnownAddress {
   745  	// Protect concurrent access.
   746  	a.mtx.Lock()
   747  	defer a.mtx.Unlock()
   748  
   749  	if a.numAddresses() == 0 {
   750  		return nil
   751  	}
   752  
   753  	// Use a 50% chance for choosing between tried and new table entries.
   754  	if a.nTried > 0 && (a.nNew == 0 || a.rand.Intn(2) == 0) {
   755  		// Tried entry.
   756  		large := 1 << 30
   757  		factor := 1.0
   758  		for {
   759  			// pick a random bucket.
   760  			bucket := a.rand.Intn(len(a.addrTried))
   761  			if a.addrTried[bucket].Len() == 0 {
   762  				continue
   763  			}
   764  
   765  			// Pick a random entry in the list
   766  			e := a.addrTried[bucket].Front()
   767  			for i :=
   768  				a.rand.Int63n(int64(a.addrTried[bucket].Len())); i > 0; i-- {
   769  				e = e.Next()
   770  			}
   771  			ka := e.Value.(*KnownAddress)
   772  			randval := a.rand.Intn(large)
   773  			if float64(randval) < (factor * ka.chance() * float64(large)) {
   774  				log.Tracef("Selected %v from tried bucket",
   775  					NetAddressKey(ka.na))
   776  				return ka
   777  			}
   778  			factor *= 1.2
   779  		}
   780  	} else {
   781  		// new node.
   782  		// XXX use a closure/function to avoid repeating this.
   783  		large := 1 << 30
   784  		factor := 1.0
   785  		for {
   786  			// Pick a random bucket.
   787  			bucket := a.rand.Intn(len(a.addrNew))
   788  			if len(a.addrNew[bucket]) == 0 {
   789  				continue
   790  			}
   791  			// Then, a random entry in it.
   792  			var ka *KnownAddress
   793  			nth := a.rand.Intn(len(a.addrNew[bucket]))
   794  			for _, value := range a.addrNew[bucket] {
   795  				if nth == 0 {
   796  					ka = value
   797  				}
   798  				nth--
   799  			}
   800  			randval := a.rand.Intn(large)
   801  			if float64(randval) < (factor * ka.chance() * float64(large)) {
   802  				log.Tracef("Selected %v from new bucket",
   803  					NetAddressKey(ka.na))
   804  				return ka
   805  			}
   806  			factor *= 1.2
   807  		}
   808  	}
   809  }
   810  
   811  func (a *AddrManager) find(addr *wire.NetAddress) *KnownAddress {
   812  	return a.addrIndex[NetAddressKey(addr)]
   813  }
   814  
   815  // Attempt increases the given address' attempt counter and updates
   816  // the last attempt time.
   817  func (a *AddrManager) Attempt(addr *wire.NetAddress) {
   818  	a.mtx.Lock()
   819  	defer a.mtx.Unlock()
   820  
   821  	// find address.
   822  	// Surely address will be in tried by now?
   823  	ka := a.find(addr)
   824  	if ka == nil {
   825  		return
   826  	}
   827  	// set last tried time to now
   828  	ka.attempts++
   829  	ka.lastattempt = time.Now()
   830  }
   831  
   832  // Connected Marks the given address as currently connected and working at the
   833  // current time.  The address must already be known to AddrManager else it will
   834  // be ignored.
   835  func (a *AddrManager) Connected(addr *wire.NetAddress) {
   836  	a.mtx.Lock()
   837  	defer a.mtx.Unlock()
   838  
   839  	ka := a.find(addr)
   840  	if ka == nil {
   841  		return
   842  	}
   843  
   844  	// Update the time as long as it has been 20 minutes since last we did
   845  	// so.
   846  	now := time.Now()
   847  	if now.After(ka.na.Timestamp.Add(time.Minute * 20)) {
   848  		// ka.na is immutable, so replace it.
   849  		naCopy := *ka.na
   850  		naCopy.Timestamp = time.Now()
   851  		ka.na = &naCopy
   852  	}
   853  }
   854  
   855  // Good marks the given address as good.  To be called after a successful
   856  // connection and version exchange.  If the address is unknown to the address
   857  // manager it will be ignored.
   858  func (a *AddrManager) Good(addr *wire.NetAddress) {
   859  	a.mtx.Lock()
   860  	defer a.mtx.Unlock()
   861  
   862  	ka := a.find(addr)
   863  	if ka == nil {
   864  		return
   865  	}
   866  
   867  	// ka.Timestamp is not updated here to avoid leaking information
   868  	// about currently connected peers.
   869  	now := time.Now()
   870  	ka.lastsuccess = now
   871  	ka.lastattempt = now
   872  	ka.attempts = 0
   873  
   874  	// move to tried set, optionally evicting other addresses if neeed.
   875  	if ka.tried {
   876  		return
   877  	}
   878  
   879  	// ok, need to move it to tried.
   880  
   881  	// remove from all new buckets.
   882  	// record one of the buckets in question and call it the `first'
   883  	addrKey := NetAddressKey(addr)
   884  	oldBucket := -1
   885  	for i := range a.addrNew {
   886  		// we check for existence so we can record the first one
   887  		if _, ok := a.addrNew[i][addrKey]; ok {
   888  			delete(a.addrNew[i], addrKey)
   889  			ka.refs--
   890  			if oldBucket == -1 {
   891  				oldBucket = i
   892  			}
   893  		}
   894  	}
   895  	a.nNew--
   896  
   897  	if oldBucket == -1 {
   898  		// What? wasn't in a bucket after all.... Panic?
   899  		return
   900  	}
   901  
   902  	bucket := a.getTriedBucket(ka.na)
   903  
   904  	// Room in this tried bucket?
   905  	if a.addrTried[bucket].Len() < triedBucketSize {
   906  		ka.tried = true
   907  		a.addrTried[bucket].PushBack(ka)
   908  		a.nTried++
   909  		return
   910  	}
   911  
   912  	// No room, we have to evict something else.
   913  	entry := a.pickTried(bucket)
   914  	rmka := entry.Value.(*KnownAddress)
   915  
   916  	// First bucket it would have been put in.
   917  	newBucket := a.getNewBucket(rmka.na, rmka.srcAddr)
   918  
   919  	// If no room in the original bucket, we put it in a bucket we just
   920  	// freed up a space in.
   921  	if len(a.addrNew[newBucket]) >= newBucketSize {
   922  		newBucket = oldBucket
   923  	}
   924  
   925  	// replace with ka in list.
   926  	ka.tried = true
   927  	entry.Value = ka
   928  
   929  	rmka.tried = false
   930  	rmka.refs++
   931  
   932  	// We don't touch a.nTried here since the number of tried stays the same
   933  	// but we decemented new above, raise it again since we're putting
   934  	// something back.
   935  	a.nNew++
   936  
   937  	rmkey := NetAddressKey(rmka.na)
   938  	log.Tracef("Replacing %s with %s in tried", rmkey, addrKey)
   939  
   940  	// We made sure there is space here just above.
   941  	a.addrNew[newBucket][rmkey] = rmka
   942  }
   943  
   944  // AddLocalAddress adds na to the list of known local addresses to advertise
   945  // with the given priority.
   946  func (a *AddrManager) AddLocalAddress(na *wire.NetAddress, priority AddressPriority) error {
   947  	if !IsRoutable(na) {
   948  		return fmt.Errorf("address %s is not routable", na.IP)
   949  	}
   950  
   951  	a.lamtx.Lock()
   952  	defer a.lamtx.Unlock()
   953  
   954  	key := NetAddressKey(na)
   955  	la, ok := a.localAddresses[key]
   956  	if !ok || la.score < priority {
   957  		if ok {
   958  			la.score = priority + 1
   959  		} else {
   960  			a.localAddresses[key] = &localAddress{
   961  				na:    na,
   962  				score: priority,
   963  			}
   964  		}
   965  	}
   966  	return nil
   967  }
   968  
   969  // getReachabilityFrom returns the relative reachability of the provided local
   970  // address to the provided remote address.
   971  func getReachabilityFrom(localAddr, remoteAddr *wire.NetAddress) int {
   972  	const (
   973  		Unreachable = 0
   974  		Default     = iota
   975  		Teredo
   976  		Ipv6Weak
   977  		Ipv4
   978  		Ipv6Strong
   979  		Private
   980  	)
   981  
   982  	if !IsRoutable(remoteAddr) {
   983  		return Unreachable
   984  	}
   985  
   986  	if IsOnionCatTor(remoteAddr) {
   987  		if IsOnionCatTor(localAddr) {
   988  			return Private
   989  		}
   990  
   991  		if IsRoutable(localAddr) && IsIPv4(localAddr) {
   992  			return Ipv4
   993  		}
   994  
   995  		return Default
   996  	}
   997  
   998  	if IsRFC4380(remoteAddr) {
   999  		if !IsRoutable(localAddr) {
  1000  			return Default
  1001  		}
  1002  
  1003  		if IsRFC4380(localAddr) {
  1004  			return Teredo
  1005  		}
  1006  
  1007  		if IsIPv4(localAddr) {
  1008  			return Ipv4
  1009  		}
  1010  
  1011  		return Ipv6Weak
  1012  	}
  1013  
  1014  	if IsIPv4(remoteAddr) {
  1015  		if IsRoutable(localAddr) && IsIPv4(localAddr) {
  1016  			return Ipv4
  1017  		}
  1018  		return Unreachable
  1019  	}
  1020  
  1021  	/* ipv6 */
  1022  	var tunnelled bool
  1023  	// Is our v6 is tunnelled?
  1024  	if IsRFC3964(localAddr) || IsRFC6052(localAddr) || IsRFC6145(localAddr) {
  1025  		tunnelled = true
  1026  	}
  1027  
  1028  	if !IsRoutable(localAddr) {
  1029  		return Default
  1030  	}
  1031  
  1032  	if IsRFC4380(localAddr) {
  1033  		return Teredo
  1034  	}
  1035  
  1036  	if IsIPv4(localAddr) {
  1037  		return Ipv4
  1038  	}
  1039  
  1040  	if tunnelled {
  1041  		// only prioritise ipv6 if we aren't tunnelling it.
  1042  		return Ipv6Weak
  1043  	}
  1044  
  1045  	return Ipv6Strong
  1046  }
  1047  
  1048  // GetBestLocalAddress returns the most appropriate local address to use
  1049  // for the given remote address.
  1050  func (a *AddrManager) GetBestLocalAddress(remoteAddr *wire.NetAddress) *wire.NetAddress {
  1051  	a.lamtx.Lock()
  1052  	defer a.lamtx.Unlock()
  1053  
  1054  	bestreach := 0
  1055  	var bestscore AddressPriority
  1056  	var bestAddress *wire.NetAddress
  1057  	for _, la := range a.localAddresses {
  1058  		reach := getReachabilityFrom(la.na, remoteAddr)
  1059  		if reach > bestreach ||
  1060  			(reach == bestreach && la.score > bestscore) {
  1061  			bestreach = reach
  1062  			bestscore = la.score
  1063  			bestAddress = la.na
  1064  		}
  1065  	}
  1066  	if bestAddress != nil {
  1067  		log.Debugf("Suggesting address %s:%d for %s:%d", bestAddress.IP,
  1068  			bestAddress.Port, remoteAddr.IP, remoteAddr.Port)
  1069  	} else {
  1070  		log.Debugf("No worthy address for %s:%d", remoteAddr.IP,
  1071  			remoteAddr.Port)
  1072  
  1073  		// Send something unroutable if nothing suitable.
  1074  		bestAddress = &wire.NetAddress{
  1075  			Timestamp: time.Now(),
  1076  			Services:  wire.SFNodeNetwork,
  1077  			Port:      0,
  1078  		}
  1079  		if !IsIPv4(remoteAddr) && !IsOnionCatTor(remoteAddr) {
  1080  			bestAddress.IP = net.IPv6zero
  1081  		} else {
  1082  			bestAddress.IP = net.IPv4zero
  1083  		}
  1084  	}
  1085  
  1086  	return bestAddress
  1087  }
  1088  
  1089  // New returns a new bitcoin address manager.
  1090  // Use Start to begin processing asynchronous address updates.
  1091  func New(dataDir string, lookupFunc func(string) ([]net.IP, error)) *AddrManager {
  1092  	am := AddrManager{
  1093  		peersFile:      filepath.Join(dataDir, "peers.json"),
  1094  		lookupFunc:     lookupFunc,
  1095  		rand:           rand.New(rand.NewSource(time.Now().UnixNano())),
  1096  		quit:           make(chan struct{}),
  1097  		localAddresses: make(map[string]*localAddress),
  1098  	}
  1099  	am.reset()
  1100  	return &am
  1101  }