Move TCP under link.go

This commit is contained in:
Neil Alexander 2019-03-04 17:09:48 +00:00
parent ddd1ac4606
commit ae79246a66
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
5 changed files with 30 additions and 29 deletions

View File

@ -577,9 +577,9 @@ func (a *admin) addPeer(addr string, sintf string) error {
if err == nil { if err == nil {
switch strings.ToLower(u.Scheme) { switch strings.ToLower(u.Scheme) {
case "tcp": case "tcp":
a.core.tcp.connect(u.Host, sintf) a.core.link.tcp.connect(u.Host, sintf)
case "socks": case "socks":
a.core.tcp.connectSOCKS(u.Host, u.Path[1:]) a.core.link.tcp.connectSOCKS(u.Host, u.Path[1:])
default: default:
return errors.New("invalid peer: " + addr) return errors.New("invalid peer: " + addr)
} }

View File

@ -44,7 +44,6 @@ type Core struct {
admin admin admin admin
searches searches searches searches
multicast multicast multicast multicast
tcp tcpInterface
link link link link
log *log.Logger log *log.Logger
} }
@ -144,7 +143,7 @@ func (c *Core) UpdateConfig(config *config.NodeConfig) {
c.router.tun.reconfigure, c.router.tun.reconfigure,
c.router.cryptokey.reconfigure, c.router.cryptokey.reconfigure,
c.switchTable.reconfigure, c.switchTable.reconfigure,
c.tcp.reconfigure, // c.link.reconfigure,
c.multicast.reconfigure, c.multicast.reconfigure,
} }
@ -205,11 +204,6 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
c.init() 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 { if err := c.link.init(c); err != nil {
c.log.Errorln("Failed to start link interfaces") c.log.Errorln("Failed to start link interfaces")
return err return err

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"strings" "strings"
"sync" "sync"
//"sync/atomic" //"sync/atomic"
"time" "time"
@ -20,7 +21,8 @@ type link struct {
core *Core core *Core
mutex sync.RWMutex // protects interfaces below mutex sync.RWMutex // protects interfaces below
interfaces map[linkInfo]*linkInterface 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 // 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.interfaces = make(map[linkInfo]*linkInterface)
l.mutex.Unlock() 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 { if err := l.awdl.init(l); err != nil {
l.core.log.Errorln("Failed to start AWDL interface") l.core.log.Errorln("Failed to start AWDL interface")
return err return err

View File

@ -27,7 +27,7 @@ func (m *multicast) init(core *Core) {
for { for {
e := <-m.reconfigure e := <-m.reconfigure
m.myAddrMutex.Lock() m.myAddrMutex.Lock()
m.myAddr = m.core.tcp.getAddr() m.myAddr = m.core.link.tcp.getAddr()
m.myAddrMutex.Unlock() m.myAddrMutex.Unlock()
e <- nil e <- nil
} }
@ -109,7 +109,7 @@ func (m *multicast) interfaces() []net.Interface {
func (m *multicast) announce() { func (m *multicast) announce() {
var anAddr net.TCPAddr var anAddr net.TCPAddr
m.myAddrMutex.Lock() m.myAddrMutex.Lock()
m.myAddr = m.core.tcp.getAddr() m.myAddr = m.core.link.tcp.getAddr()
m.myAddrMutex.Unlock() m.myAddrMutex.Unlock()
groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr) groupAddr, err := net.ResolveUDPAddr("udp6", m.groupAddr)
if err != nil { if err != nil {
@ -183,6 +183,6 @@ func (m *multicast) listen() {
} }
addr.Zone = from.Zone addr.Zone = from.Zone
saddr := addr.String() saddr := addr.String()
m.core.tcp.connect(saddr, addr.Zone) m.core.link.tcp.connect(saddr, addr.Zone)
} }
} }

View File

@ -32,7 +32,7 @@ const tcp_ping_interval = (default_timeout * 2 / 3)
// The TCP listener and information about active TCP connections, to avoid duplication. // The TCP listener and information about active TCP connections, to avoid duplication.
type tcpInterface struct { type tcpInterface struct {
core *Core link *link
reconfigure chan chan error reconfigure chan chan error
serv net.Listener serv net.Listener
stop chan bool stop chan bool
@ -77,16 +77,16 @@ func (iface *tcpInterface) connectSOCKS(socksaddr, peeraddr string) {
} }
// Initializes the struct. // Initializes the struct.
func (iface *tcpInterface) init(core *Core) (err error) { func (iface *tcpInterface) init(l *link) (err error) {
iface.core = core iface.link = l
iface.stop = make(chan bool, 1) iface.stop = make(chan bool, 1)
iface.reconfigure = make(chan chan error, 1) iface.reconfigure = make(chan chan error, 1)
go func() { go func() {
for { for {
e := <-iface.reconfigure e := <-iface.reconfigure
iface.core.configMutex.RLock() iface.link.core.configMutex.RLock()
updated := iface.core.config.Listen != iface.core.configOld.Listen updated := iface.link.core.config.Listen != iface.link.core.configOld.Listen
iface.core.configMutex.RUnlock() iface.link.core.configMutex.RUnlock()
if updated { if updated {
iface.stop <- true iface.stop <- true
iface.serv.Close() iface.serv.Close()
@ -103,9 +103,9 @@ func (iface *tcpInterface) init(core *Core) (err error) {
func (iface *tcpInterface) listen() error { func (iface *tcpInterface) listen() error {
var err error var err error
iface.core.configMutex.RLock() iface.link.core.configMutex.RLock()
iface.addr = iface.core.config.Listen iface.addr = iface.link.core.config.Listen
iface.core.configMutex.RUnlock() iface.link.core.configMutex.RUnlock()
ctx := context.Background() ctx := context.Background()
lc := net.ListenConfig{ lc := net.ListenConfig{
@ -127,16 +127,16 @@ func (iface *tcpInterface) listen() error {
// Runs the listener, which spawns off goroutines for incoming connections. // Runs the listener, which spawns off goroutines for incoming connections.
func (iface *tcpInterface) listener() { func (iface *tcpInterface) listener() {
defer iface.serv.Close() 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 { for {
sock, err := iface.serv.Accept() sock, err := iface.serv.Accept()
if err != nil { if err != nil {
iface.core.log.Errorln("Failed to accept connection:", err) iface.link.core.log.Errorln("Failed to accept connection:", err)
return return
} }
select { select {
case <-iface.stop: case <-iface.stop:
iface.core.log.Errorln("Stopping listener") iface.link.core.log.Errorln("Stopping listener")
return return
default: default:
if err != nil { if err != nil {
@ -280,12 +280,12 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
remote, _, _ := net.SplitHostPort(sock.RemoteAddr().String()) remote, _, _ := net.SplitHostPort(sock.RemoteAddr().String())
remotelinklocal := net.ParseIP(remote).IsLinkLocalUnicast() remotelinklocal := net.ParseIP(remote).IsLinkLocalUnicast()
name := "tcp://" + sock.RemoteAddr().String() 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 { if err != nil {
iface.core.log.Println(err) iface.link.core.log.Println(err)
panic(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() err = link.handler()
iface.core.log.Debugln("DEBUG: stopped handler for", name, err) iface.link.core.log.Debugln("DEBUG: stopped handler for", name, err)
} }