diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index 1c8c80e4..723bf8ff 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -52,7 +52,7 @@ func (a *admin) addHandler(name string, args []string, handler func(admin_info) } // init runs the initial admin setup. -func (a *admin) init(c *Core, listenaddr string) { +func (a *admin) init(c *Core) { a.core = c a.reconfigure = make(chan bool, 1) go func() { @@ -69,7 +69,9 @@ func (a *admin) init(c *Core, listenaddr string) { } } }() - a.listenaddr = listenaddr + a.core.configMutex.RLock() + a.listenaddr = a.core.config.AdminListen + a.core.configMutex.RUnlock() a.addHandler("list", []string{}, func(in admin_info) (admin_info, error) { handlers := make(map[string]interface{}) for _, handler := range a.handlers { diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index 9e4bb628..58d92b0d 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -19,7 +19,7 @@ var buildName string var buildVersion string type module interface { - init(*config.NodeConfig) error + init(*Core, *config.NodeConfig) error start() error } @@ -32,12 +32,10 @@ type Core struct { config config.NodeConfig // Active config configOld config.NodeConfig // Previous config configMutex sync.RWMutex // Protects both config and configOld - // Core-specific config - boxPub crypto.BoxPubKey - boxPriv crypto.BoxPrivKey - sigPub crypto.SigPubKey - sigPriv crypto.SigPrivKey - // Modules + boxPub crypto.BoxPubKey + boxPriv crypto.BoxPrivKey + sigPub crypto.SigPubKey + sigPriv crypto.SigPrivKey switchTable switchTable peers peers sessions sessions @@ -48,15 +46,11 @@ type Core struct { multicast multicast nodeinfo nodeinfo tcp tcpInterface - // Other bits - log *log.Logger - ifceExpr []*regexp.Regexp // the zone of link-local IPv6 peers must match this + log *log.Logger + ifceExpr []*regexp.Regexp // the zone of link-local IPv6 peers must match this } -func (c *Core) init(bpub *crypto.BoxPubKey, - bpriv *crypto.BoxPrivKey, - spub *crypto.SigPubKey, - spriv *crypto.SigPrivKey) { +func (c *Core) init() error { // TODO separate init and start functions // Init sets up structs // Start launches goroutines that depend on structs being set up @@ -64,16 +58,45 @@ func (c *Core) init(bpub *crypto.BoxPubKey, if c.log == nil { c.log = log.New(ioutil.Discard, "", 0) } - c.boxPub, c.boxPriv = *bpub, *bpriv - c.sigPub, c.sigPriv = *spub, *spriv - c.admin.core = c + + boxPubHex, err := hex.DecodeString(c.config.EncryptionPublicKey) + if err != nil { + return err + } + boxPrivHex, err := hex.DecodeString(c.config.EncryptionPrivateKey) + if err != nil { + return err + } + sigPubHex, err := hex.DecodeString(c.config.SigningPublicKey) + if err != nil { + return err + } + sigPrivHex, err := hex.DecodeString(c.config.SigningPrivateKey) + if err != nil { + return err + } + + copy(c.boxPub[:], boxPubHex) + copy(c.boxPriv[:], boxPrivHex) + copy(c.sigPub[:], sigPubHex) + copy(c.sigPriv[:], sigPrivHex) + + c.admin.init(c) + c.nodeinfo.init(c) c.searches.init(c) c.dht.init(c) c.sessions.init(c) c.multicast.init(c) c.peers.init(c) c.router.init(c) - c.switchTable.init(c, c.sigPub) // TODO move before peers? before router? + c.switchTable.init(c) // TODO move before peers? before router? + + if err := c.tcp.init(c); err != nil { + c.log.Println("Failed to start TCP interface") + return err + } + + return nil } // UpdateConfig updates the configuration in Core and then signals the @@ -133,42 +156,10 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error { c.configOld = c.config c.configMutex.Unlock() - var boxPub crypto.BoxPubKey - var boxPriv crypto.BoxPrivKey - var sigPub crypto.SigPubKey - var sigPriv crypto.SigPrivKey - boxPubHex, err := hex.DecodeString(nc.EncryptionPublicKey) - if err != nil { - return err - } - boxPrivHex, err := hex.DecodeString(nc.EncryptionPrivateKey) - if err != nil { - return err - } - sigPubHex, err := hex.DecodeString(nc.SigningPublicKey) - if err != nil { - return err - } - sigPrivHex, err := hex.DecodeString(nc.SigningPrivateKey) - if err != nil { - return err - } - copy(boxPub[:], boxPubHex) - copy(boxPriv[:], boxPrivHex) - copy(sigPub[:], sigPubHex) - copy(sigPriv[:], sigPrivHex) + c.init() - c.init(&boxPub, &boxPriv, &sigPub, &sigPriv) - c.admin.init(c, nc.AdminListen) - - c.nodeinfo.init(c) c.nodeinfo.setNodeInfo(nc.NodeInfo, nc.NodeInfoPrivacy) - if err := c.tcp.init(c, nc.Listen, nc.ReadTimeout); err != nil { - c.log.Println("Failed to start TCP interface") - return err - } - if nc.SwitchOptions.MaxTotalQueueSize >= SwitchQueueTotalMinSize { c.switchTable.queueTotalMaxSize = nc.SwitchOptions.MaxTotalQueueSize } @@ -201,7 +192,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error { } } for _, source := range nc.TunnelRouting.IPv6Sources { - if c.router.cryptokey.addSourceSubnet(source); err != nil { + if err := c.router.cryptokey.addSourceSubnet(source); err != nil { panic(err) } } @@ -211,7 +202,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error { } } for _, source := range nc.TunnelRouting.IPv4Sources { - if c.router.cryptokey.addSourceSubnet(source); err != nil { + if err := c.router.cryptokey.addSourceSubnet(source); err != nil { panic(err) } } diff --git a/src/yggdrasil/switch.go b/src/yggdrasil/switch.go index 420392b6..f3c95122 100644 --- a/src/yggdrasil/switch.go +++ b/src/yggdrasil/switch.go @@ -182,12 +182,12 @@ type switchTable struct { const SwitchQueueTotalMinSize = 4 * 1024 * 1024 // Initializes the switchTable struct. -func (t *switchTable) init(core *Core, key crypto.SigPubKey) { +func (t *switchTable) init(core *Core) { now := time.Now() t.core = core t.reconfigure = make(chan bool, 1) - t.key = key - locator := switchLocator{root: key, tstamp: now.Unix()} + t.key = t.core.sigPub + locator := switchLocator{root: t.key, tstamp: now.Unix()} peers := make(map[switchPort]peerInfo) t.data = switchData{locator: locator, peers: peers} t.updater.Store(&sync.Once{}) diff --git a/src/yggdrasil/tcp.go b/src/yggdrasil/tcp.go index 6d923440..c986dc62 100644 --- a/src/yggdrasil/tcp.go +++ b/src/yggdrasil/tcp.go @@ -40,6 +40,7 @@ type tcpInterface struct { core *Core serv net.Listener tcp_timeout time.Duration + tcp_addr string mutex sync.Mutex // Protecting the below calls map[string]struct{} conns map[tcpInfo](chan struct{}) @@ -80,15 +81,15 @@ func (iface *tcpInterface) connectSOCKS(socksaddr, peeraddr string) { } // Initializes the struct. -func (iface *tcpInterface) init(core *Core, addr string, readTimeout int32) (err error) { +func (iface *tcpInterface) init(core *Core) (err error) { iface.core = core - - iface.tcp_timeout = time.Duration(readTimeout) * time.Millisecond + iface.tcp_addr = iface.core.config.Listen + iface.tcp_timeout = time.Duration(iface.core.config.ReadTimeout) * time.Millisecond if iface.tcp_timeout >= 0 && iface.tcp_timeout < default_tcp_timeout { iface.tcp_timeout = default_tcp_timeout } - iface.serv, err = net.Listen("tcp", addr) + iface.serv, err = net.Listen("tcp", iface.tcp_addr) if err == nil { iface.calls = make(map[string]struct{}) iface.conns = make(map[tcpInfo](chan struct{}))