Browse Source

addrbook: toggle strict routability

pull/456/head
Ethan Buchman 8 years ago
parent
commit
2cee364692
2 changed files with 34 additions and 30 deletions
  1. +26
    -24
      addrbook.go
  2. +8
    -6
      addrbook_test.go

+ 26
- 24
addrbook.go View File

@ -77,17 +77,18 @@ const (
type AddrBook struct { type AddrBook struct {
QuitService QuitService
mtx sync.Mutex
filePath string
rand *rand.Rand
key string
ourAddrs map[string]*NetAddress
addrLookup map[string]*knownAddress // new & old
addrNew []map[string]*knownAddress
addrOld []map[string]*knownAddress
wg sync.WaitGroup
nOld int
nNew int
mtx sync.Mutex
filePath string
routabilityStrict bool
rand *rand.Rand
key string
ourAddrs map[string]*NetAddress
addrLookup map[string]*knownAddress // new & old
addrNew []map[string]*knownAddress
addrOld []map[string]*knownAddress
wg sync.WaitGroup
nOld int
nNew int
} }
const ( const (
@ -96,12 +97,13 @@ const (
) )
// Use Start to begin processing asynchronous address updates. // Use Start to begin processing asynchronous address updates.
func NewAddrBook(filePath string) *AddrBook {
func NewAddrBook(filePath string, routabilityStrict bool) *AddrBook {
am := &AddrBook{ am := &AddrBook{
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
ourAddrs: make(map[string]*NetAddress),
addrLookup: make(map[string]*knownAddress),
filePath: filePath,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
ourAddrs: make(map[string]*NetAddress),
addrLookup: make(map[string]*knownAddress),
filePath: filePath,
routabilityStrict: routabilityStrict,
} }
am.init() am.init()
am.QuitService = *NewQuitService(log, "AddrBook", am) am.QuitService = *NewQuitService(log, "AddrBook", am)
@ -513,7 +515,7 @@ func (a *AddrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress {
} }
func (a *AddrBook) addAddress(addr, src *NetAddress) { func (a *AddrBook) addAddress(addr, src *NetAddress) {
if !addr.Routable() {
if a.routabilityStrict && !addr.Routable() {
log.Warn(Fmt("Cannot add non-routable address %v", addr)) log.Warn(Fmt("Cannot add non-routable address %v", addr))
return return
} }
@ -616,8 +618,8 @@ func (a *AddrBook) moveToOld(ka *knownAddress) {
func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int { func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int {
data1 := []byte{} data1 := []byte{}
data1 = append(data1, []byte(a.key)...) data1 = append(data1, []byte(a.key)...)
data1 = append(data1, []byte(groupKey(addr))...)
data1 = append(data1, []byte(groupKey(src))...)
data1 = append(data1, []byte(a.groupKey(addr))...)
data1 = append(data1, []byte(a.groupKey(src))...)
hash1 := doubleSha256(data1) hash1 := doubleSha256(data1)
hash64 := binary.BigEndian.Uint64(hash1) hash64 := binary.BigEndian.Uint64(hash1)
hash64 %= newBucketsPerGroup hash64 %= newBucketsPerGroup
@ -625,7 +627,7 @@ func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int {
binary.BigEndian.PutUint64(hashbuf[:], hash64) binary.BigEndian.PutUint64(hashbuf[:], hash64)
data2 := []byte{} data2 := []byte{}
data2 = append(data2, []byte(a.key)...) data2 = append(data2, []byte(a.key)...)
data2 = append(data2, groupKey(src)...)
data2 = append(data2, a.groupKey(src)...)
data2 = append(data2, hashbuf[:]...) data2 = append(data2, hashbuf[:]...)
hash2 := doubleSha256(data2) hash2 := doubleSha256(data2)
@ -645,7 +647,7 @@ func (a *AddrBook) calcOldBucket(addr *NetAddress) int {
binary.BigEndian.PutUint64(hashbuf[:], hash64) binary.BigEndian.PutUint64(hashbuf[:], hash64)
data2 := []byte{} data2 := []byte{}
data2 = append(data2, []byte(a.key)...) data2 = append(data2, []byte(a.key)...)
data2 = append(data2, groupKey(addr)...)
data2 = append(data2, a.groupKey(addr)...)
data2 = append(data2, hashbuf[:]...) data2 = append(data2, hashbuf[:]...)
hash2 := doubleSha256(data2) hash2 := doubleSha256(data2)
@ -656,11 +658,11 @@ func (a *AddrBook) calcOldBucket(addr *NetAddress) int {
// This is the /16 for IPv6, the /32 (/36 for he.net) for IPv6, the string // This is the /16 for IPv6, the /32 (/36 for he.net) for IPv6, the string
// "local" for a local address and the string "unroutable for an unroutable // "local" for a local address and the string "unroutable for an unroutable
// address. // address.
func groupKey(na *NetAddress) string {
if na.Local() {
func (a *AddrBook) groupKey(na *NetAddress) string {
if a.routabilityStrict && na.Local() {
return "local" return "local"
} }
if !na.Routable() {
if a.routabilityStrict && !na.Routable() {
return "unroutable" return "unroutable"
} }


+ 8
- 6
addrbook_test.go View File

@ -7,6 +7,8 @@ import (
"testing" "testing"
) )
const addrBookStrict = true
func createTempFileName(prefix string) string { func createTempFileName(prefix string) string {
f, err := ioutil.TempFile("", prefix) f, err := ioutil.TempFile("", prefix)
if err != nil { if err != nil {
@ -25,10 +27,10 @@ func TestEmpty(t *testing.T) {
// t.Logf("New tempfile name: %v", fname) // t.Logf("New tempfile name: %v", fname)
// Save an empty book & load it // Save an empty book & load it
book := NewAddrBook(fname)
book := NewAddrBook(fname, addrBookStrict)
book.saveToFile(fname) book.saveToFile(fname)
book = NewAddrBook(fname)
book = NewAddrBook(fname, addrBookStrict)
book.loadFromFile(fname) book.loadFromFile(fname)
if book.Size() != 0 { if book.Size() != 0 {
@ -74,7 +76,7 @@ func TestSaveAddresses(t *testing.T) {
} }
// Create the book & populate & save // Create the book & populate & save
book := NewAddrBook(fname)
book := NewAddrBook(fname, addrBookStrict)
for _, addrSrc := range randAddrs { for _, addrSrc := range randAddrs {
book.AddAddress(addrSrc.addr, addrSrc.src) book.AddAddress(addrSrc.addr, addrSrc.src)
} }
@ -84,7 +86,7 @@ func TestSaveAddresses(t *testing.T) {
book.saveToFile(fname) book.saveToFile(fname)
// Reload the book // Reload the book
book = NewAddrBook(fname)
book = NewAddrBook(fname, addrBookStrict)
book.loadFromFile(fname) book.loadFromFile(fname)
// Test ... // Test ...
@ -128,7 +130,7 @@ func TestPromoteToOld(t *testing.T) {
} }
// Create the book & populate & save // Create the book & populate & save
book := NewAddrBook(fname)
book := NewAddrBook(fname, addrBookStrict)
for _, addrSrc := range randAddrs { for _, addrSrc := range randAddrs {
book.AddAddress(addrSrc.addr, addrSrc.src) book.AddAddress(addrSrc.addr, addrSrc.src)
} }
@ -145,7 +147,7 @@ func TestPromoteToOld(t *testing.T) {
book.saveToFile(fname) book.saveToFile(fname)
// Reload the book // Reload the book
book = NewAddrBook(fname)
book = NewAddrBook(fname, addrBookStrict)
book.loadFromFile(fname) book.loadFromFile(fname)
// Test ... // Test ...


Loading…
Cancel
Save