5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-25 17:11:37 +00:00

debugging and cleanup

This commit is contained in:
Arceliar 2018-05-06 19:01:52 -05:00
parent 80f893aac3
commit 0b391b6e3a
2 changed files with 31 additions and 6 deletions

View File

@ -400,7 +400,7 @@ func (c *Core) DEBUG_setIfceExpr(expr *regexp.Regexp) {
func (c *Core) DEBUG_addAuthBoxPub(boxBytes []byte) { func (c *Core) DEBUG_addAuthBoxPub(boxBytes []byte) {
var box boxPubKey var box boxPubKey
copy(box[:], boxBytes) copy(box[:], boxBytes)
c.peers.authBoxPubs[box] = struct{}{} c.peers.addAuthBoxPub(&box)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -30,11 +30,12 @@ import "math"
//import "fmt" //import "fmt"
type peers struct { type peers struct {
core *Core core *Core
authBoxPubs map[boxPubKey]struct{} mutex sync.Mutex // Synchronize writes to atomic
mutex sync.Mutex // Synchronize writes to atomic ports atomic.Value //map[Port]*peer, use CoW semantics
ports atomic.Value //map[Port]*peer, use CoW semantics
//ports map[Port]*peer //ports map[Port]*peer
authMutex sync.RWMutex
authBoxPubs map[boxPubKey]struct{}
} }
func (ps *peers) init(c *Core) { func (ps *peers) init(c *Core) {
@ -46,10 +47,34 @@ func (ps *peers) init(c *Core) {
} }
func (ps *peers) isAuthBoxPub(box *boxPubKey) bool { func (ps *peers) isAuthBoxPub(box *boxPubKey) bool {
ps.authMutex.RLock()
defer ps.authMutex.RUnlock()
_, isIn := ps.authBoxPubs[*box] _, isIn := ps.authBoxPubs[*box]
return isIn || len(ps.authBoxPubs) == 0 return isIn || len(ps.authBoxPubs) == 0
} }
func (ps *peers) addAuthBoxPub(box *boxPubKey) {
ps.authMutex.Lock()
defer ps.authMutex.Unlock()
ps.authBoxPubs[*box] = struct{}{}
}
func (ps *peers) removeAuthBoxPub(box *boxPubKey) {
ps.authMutex.Lock()
defer ps.authMutex.Unlock()
delete(ps.authBoxPubs, *box)
}
func (ps *peers) getAuthBoxPubs() []boxPubKey {
ps.authMutex.RLock()
defer ps.authMutex.RUnlock()
keys := make([]boxPubKey, 0, len(ps.authBoxPubs))
for key := range ps.authBoxPubs {
keys = append(keys, key)
}
return keys
}
func (ps *peers) getPorts() map[switchPort]*peer { func (ps *peers) getPorts() map[switchPort]*peer {
return ps.ports.Load().(map[switchPort]*peer) return ps.ports.Load().(map[switchPort]*peer)
} }
@ -218,7 +243,7 @@ func (p *peer) handlePacket(packet []byte, linkIn chan<- []byte) {
} }
func (p *peer) handleTraffic(packet []byte, pTypeLen int) { func (p *peer) handleTraffic(packet []byte, pTypeLen int) {
if p.msgAnc == nil { if p.port != 0 && p.msgAnc == nil {
// Drop traffic until the peer manages to send us at least one anc // Drop traffic until the peer manages to send us at least one anc
return return
} }