diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index 62803395..d0d4cc90 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -577,9 +577,9 @@ func (a *admin) addPeer(addr string, sintf string) error { if err == nil { switch strings.ToLower(u.Scheme) { case "tcp": - a.core.tcp.connect(u.Host, sintf) + a.core.link.tcp.connect(u.Host, sintf) case "socks": - a.core.tcp.connectSOCKS(u.Host, u.Path[1:]) + a.core.link.tcp.connectSOCKS(u.Host, u.Path[1:]) default: return errors.New("invalid peer: " + addr) } diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index 04433282..b2a85ec5 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -44,7 +44,6 @@ type Core struct { admin admin searches searches multicast multicast - tcp tcpInterface link link log *log.Logger } @@ -144,7 +143,7 @@ func (c *Core) UpdateConfig(config *config.NodeConfig) { c.router.tun.reconfigure, c.router.cryptokey.reconfigure, c.switchTable.reconfigure, - c.tcp.reconfigure, + // c.link.reconfigure, c.multicast.reconfigure, } @@ -205,11 +204,6 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error { c.init() - if err := c.tcp.init(c); err != nil { - c.log.Errorln("Failed to start TCP interface") - return err - } - if err := c.link.init(c); err != nil { c.log.Errorln("Failed to start link interfaces") return err diff --git a/src/yggdrasil/link.go b/src/yggdrasil/link.go index 6fc7687b..8c03e086 100644 --- a/src/yggdrasil/link.go +++ b/src/yggdrasil/link.go @@ -8,6 +8,7 @@ import ( "net" "strings" "sync" + //"sync/atomic" "time" @@ -20,7 +21,8 @@ type link struct { core *Core mutex sync.RWMutex // protects interfaces below interfaces map[linkInfo]*linkInterface - awdl awdl // AWDL interface support + awdl awdl // AWDL interface support + tcp tcpInterface // TCP interface support // TODO timeout (to remove from switch), read from config.ReadTimeout } @@ -58,6 +60,11 @@ func (l *link) init(c *Core) error { l.interfaces = make(map[linkInfo]*linkInterface) l.mutex.Unlock() + if err := l.tcp.init(l); err != nil { + c.log.Errorln("Failed to start TCP interface") + return err + } + if err := l.awdl.init(l); err != nil { l.core.log.Errorln("Failed to start AWDL interface") return err diff --git a/src/yggdrasil/multicast.go b/src/yggdrasil/multicast.go index b20ee94d..f416ea26 100644 --- a/src/yggdrasil/multicast.go +++ b/src/yggdrasil/multicast.go @@ -27,7 +27,7 @@ func (m *multicast) init(core *Core) { for { e := <-m.reconfigure m.myAddrMutex.Lock() - m.myAddr = m.core.tcp.getAddr() + m.myAddr = m.core.link.tcp.getAddr() m.myAddrMutex.Unlock() e <- nil } @@ -109,7 +109,7 @@ func (m *multicast) interfaces() []net.Interface { func (m *multicast) announce() { var anAddr net.TCPAddr m.myAddrMutex.Lock() - m.myAddr = m.core.tcp.getAddr() + m.myAddr = m.core.link.tcp.getAddr() m.myAddrMutex.Unlock() groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr) if err != nil { @@ -183,6 +183,6 @@ func (m *multicast) listen() { } addr.Zone = from.Zone saddr := addr.String() - m.core.tcp.connect(saddr, addr.Zone) + m.core.link.tcp.connect(saddr, addr.Zone) } } diff --git a/src/yggdrasil/tcp.go b/src/yggdrasil/tcp.go index c65e2e66..989480db 100644 --- a/src/yggdrasil/tcp.go +++ b/src/yggdrasil/tcp.go @@ -32,7 +32,7 @@ const tcp_ping_interval = (default_timeout * 2 / 3) // The TCP listener and information about active TCP connections, to avoid duplication. type tcpInterface struct { - core *Core + link *link reconfigure chan chan error serv net.Listener stop chan bool @@ -77,16 +77,16 @@ func (iface *tcpInterface) connectSOCKS(socksaddr, peeraddr string) { } // Initializes the struct. -func (iface *tcpInterface) init(core *Core) (err error) { - iface.core = core +func (iface *tcpInterface) init(l *link) (err error) { + iface.link = l iface.stop = make(chan bool, 1) iface.reconfigure = make(chan chan error, 1) go func() { for { e := <-iface.reconfigure - iface.core.configMutex.RLock() - updated := iface.core.config.Listen != iface.core.configOld.Listen - iface.core.configMutex.RUnlock() + iface.link.core.configMutex.RLock() + updated := iface.link.core.config.Listen != iface.link.core.configOld.Listen + iface.link.core.configMutex.RUnlock() if updated { iface.stop <- true iface.serv.Close() @@ -103,9 +103,9 @@ func (iface *tcpInterface) init(core *Core) (err error) { func (iface *tcpInterface) listen() error { var err error - iface.core.configMutex.RLock() - iface.addr = iface.core.config.Listen - iface.core.configMutex.RUnlock() + iface.link.core.configMutex.RLock() + iface.addr = iface.link.core.config.Listen + iface.link.core.configMutex.RUnlock() ctx := context.Background() lc := net.ListenConfig{ @@ -127,16 +127,16 @@ func (iface *tcpInterface) listen() error { // Runs the listener, which spawns off goroutines for incoming connections. func (iface *tcpInterface) listener() { defer iface.serv.Close() - iface.core.log.Infoln("Listening for TCP on:", iface.serv.Addr().String()) + iface.link.core.log.Infoln("Listening for TCP on:", iface.serv.Addr().String()) for { sock, err := iface.serv.Accept() if err != nil { - iface.core.log.Errorln("Failed to accept connection:", err) + iface.link.core.log.Errorln("Failed to accept connection:", err) return } select { case <-iface.stop: - iface.core.log.Errorln("Stopping listener") + iface.link.core.log.Errorln("Stopping listener") return default: if err != nil { @@ -280,12 +280,12 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) { remote, _, _ := net.SplitHostPort(sock.RemoteAddr().String()) remotelinklocal := net.ParseIP(remote).IsLinkLocalUnicast() name := "tcp://" + sock.RemoteAddr().String() - link, err := iface.core.link.create(&stream, name, "tcp", local, remote, incoming, remotelinklocal) + link, err := iface.link.core.link.create(&stream, name, "tcp", local, remote, incoming, remotelinklocal) if err != nil { - iface.core.log.Println(err) + iface.link.core.log.Println(err) panic(err) } - iface.core.log.Debugln("DEBUG: starting handler for", name) + iface.link.core.log.Debugln("DEBUG: starting handler for", name) err = link.handler() - iface.core.log.Debugln("DEBUG: stopped handler for", name, err) + iface.link.core.log.Debugln("DEBUG: stopped handler for", name, err) }