From 61774aed3bfe286422bdb4a92e5bd3e4f84848e0 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Mon, 4 Mar 2019 20:33:08 +0000 Subject: [PATCH] Show proto in admin socket, link linkInfo from peer, other fixes --- src/yggdrasil/admin.go | 6 ++++-- src/yggdrasil/debug.go | 10 +++++++++- src/yggdrasil/link.go | 2 +- src/yggdrasil/peer.go | 8 +++++--- src/yggdrasil/router.go | 10 +++++++++- src/yggdrasil/tcp.go | 34 ++++++---------------------------- 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index d0d4cc90..ae5b02a2 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -666,7 +666,8 @@ func (a *admin) getData_getPeers() []admin_nodeInfo { {"uptime", int(time.Since(p.firstSeen).Seconds())}, {"bytes_sent", atomic.LoadUint64(&p.bytesSent)}, {"bytes_recvd", atomic.LoadUint64(&p.bytesRecvd)}, - {"endpoint", p.endpoint}, + {"proto", p.intf.info.linkType}, + {"endpoint", p.intf.info.remote}, {"box_pub_key", hex.EncodeToString(p.box[:])}, } peerInfos = append(peerInfos, info) @@ -692,7 +693,8 @@ func (a *admin) getData_getSwitchPeers() []admin_nodeInfo { {"port", elem.port}, {"bytes_sent", atomic.LoadUint64(&peer.bytesSent)}, {"bytes_recvd", atomic.LoadUint64(&peer.bytesRecvd)}, - {"endpoint", peer.endpoint}, + {"proto", peer.intf.info.linkType}, + {"endpoint", peer.intf.info.remote}, {"box_pub_key", hex.EncodeToString(peer.box[:])}, } peerInfos = append(peerInfos, info) diff --git a/src/yggdrasil/debug.go b/src/yggdrasil/debug.go index 8405714a..e575b72d 100644 --- a/src/yggdrasil/debug.go +++ b/src/yggdrasil/debug.go @@ -97,7 +97,15 @@ func (c *Core) DEBUG_getPeers() *peers { } func (ps *peers) DEBUG_newPeer(box crypto.BoxPubKey, sig crypto.SigPubKey, link crypto.BoxSharedKey) *peer { - return ps.newPeer(&box, &sig, &link, "(simulator)", nil) + sim := linkInterface{ + name: "(simulator)", + info: linkInfo{ + local: "(simulator)", + remote: "(simulator)", + linkType: "sim", + }, + } + return ps.newPeer(&box, &sig, &link, &sim, nil) } /* diff --git a/src/yggdrasil/link.go b/src/yggdrasil/link.go index ac869389..4f7c6e1f 100644 --- a/src/yggdrasil/link.go +++ b/src/yggdrasil/link.go @@ -173,7 +173,7 @@ func (intf *linkInterface) handler() error { intf.link.mutex.Unlock() // Create peer shared := crypto.GetSharedKey(myLinkPriv, &meta.link) - intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf.name, func() { intf.msgIO.close() }) + intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf, func() { intf.msgIO.close() }) if intf.peer == nil { return errors.New("failed to create peer") } diff --git a/src/yggdrasil/peer.go b/src/yggdrasil/peer.go index 237d6f61..ce359368 100644 --- a/src/yggdrasil/peer.go +++ b/src/yggdrasil/peer.go @@ -98,6 +98,7 @@ type peer struct { bytesRecvd uint64 // To track bandwidth usage for getPeers // BUG: sync/atomic, 32 bit platforms need the above to be the first element core *Core + intf *linkInterface port switchPort box crypto.BoxPubKey sig crypto.SigPubKey @@ -113,18 +114,19 @@ type peer struct { } // Creates a new peer with the specified box, sig, and linkShared keys, using the lowest unoccupied port number. -func (ps *peers) newPeer(box *crypto.BoxPubKey, sig *crypto.SigPubKey, linkShared *crypto.BoxSharedKey, endpoint string, closer func()) *peer { +func (ps *peers) newPeer(box *crypto.BoxPubKey, sig *crypto.SigPubKey, linkShared *crypto.BoxSharedKey, intf *linkInterface, closer func()) *peer { now := time.Now() p := peer{box: *box, sig: *sig, shared: *crypto.GetSharedKey(&ps.core.boxPriv, box), linkShared: *linkShared, - endpoint: endpoint, firstSeen: now, doSend: make(chan struct{}, 1), dinfo: make(chan *dhtInfo, 1), close: closer, - core: ps.core} + core: ps.core, + intf: intf, + } ps.mutex.Lock() defer ps.mutex.Unlock() oldPorts := ps.getPorts() diff --git a/src/yggdrasil/router.go b/src/yggdrasil/router.go index 6acd4735..1d4c0771 100644 --- a/src/yggdrasil/router.go +++ b/src/yggdrasil/router.go @@ -67,7 +67,15 @@ func (r *router) init(core *Core) { r.addr = *address.AddrForNodeID(&r.core.dht.nodeID) r.subnet = *address.SubnetForNodeID(&r.core.dht.nodeID) in := make(chan []byte, 1) // TODO something better than this... - p := r.core.peers.newPeer(&r.core.boxPub, &r.core.sigPub, &crypto.BoxSharedKey{}, "(self)", nil) + self := linkInterface{ + name: "(self)", + info: linkInfo{ + local: "(self)", + remote: "(self)", + linkType: "self", + }, + } + p := r.core.peers.newPeer(&r.core.boxPub, &r.core.sigPub, &crypto.BoxSharedKey{}, &self, nil) p.out = func(packet []byte) { in <- packet } r.in = in out := make(chan []byte, 32) diff --git a/src/yggdrasil/tcp.go b/src/yggdrasil/tcp.go index 8dcd4c03..c0d568a5 100644 --- a/src/yggdrasil/tcp.go +++ b/src/yggdrasil/tcp.go @@ -24,7 +24,6 @@ import ( "golang.org/x/net/proxy" - "github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/util" ) @@ -39,16 +38,7 @@ type tcp struct { listeners map[string]net.Listener listenerstops map[string]chan bool calls map[string]struct{} - conns map[tcpInfo](chan struct{}) -} - -// This is used as the key to a map that tracks existing connections, to prevent multiple connections to the same keys and local/remote address pair from occuring. -// Different address combinations are allowed, so multi-homing is still technically possible (but not necessarily advisable). -type tcpInfo struct { - box crypto.BoxPubKey - sig crypto.SigPubKey - localAddr string - remoteAddr string + conns map[linkInfo](chan struct{}) } // Wrapper function to set additional options for specific connection types. @@ -90,7 +80,7 @@ func (t *tcp) init(l *link) error { t.reconfigure = make(chan chan error, 1) t.mutex.Lock() t.calls = make(map[string]struct{}) - t.conns = make(map[tcpInfo](chan struct{})) + t.conns = make(map[linkInfo](chan struct{})) t.listeners = make(map[string]net.Listener) t.listenerstops = make(map[string]chan bool) t.mutex.Unlock() @@ -167,20 +157,20 @@ func (t *tcp) listen(listenaddr string) error { // Runs the listener, which spawns off goroutines for incoming connections. func (t *tcp) listener(listenaddr string) { t.mutex.Lock() - listener, ok := t.listeners[listenaddr] + listener, ok1 := t.listeners[listenaddr] listenerstop, ok2 := t.listenerstops[listenaddr] t.mutex.Unlock() - if !ok || !ok2 { + if !ok1 || !ok2 { t.link.core.log.Errorln("Tried to start TCP listener for", listenaddr, "which doesn't exist") return } reallistenaddr := listener.Addr().String() defer listener.Close() t.link.core.log.Infoln("Listening for TCP on:", reallistenaddr) + accepted := make(chan bool) for { var sock net.Conn var err error - accepted := make(chan bool) go func() { sock, err = listener.Accept() accepted <- true @@ -191,26 +181,14 @@ func (t *tcp) listener(listenaddr string) { t.link.core.log.Errorln("Failed to accept connection:", err) return } + go t.handler(sock, true) case <-listenerstop: t.link.core.log.Errorln("Stopping TCP listener on:", reallistenaddr) return - default: - if err != nil { - panic(err) - } - go t.handler(sock, true) } } } -// Checks if we already have a connection to this node -func (t *tcp) isAlreadyConnected(info tcpInfo) bool { - t.mutex.Lock() - defer t.mutex.Unlock() - _, isIn := t.conns[info] - return isIn -} - // Checks if we already are calling this address func (t *tcp) isAlreadyCalling(saddr string) bool { t.mutex.Lock()