From 166336a41822b9e4021bde2870636a7e9bc6abbc Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 2 Jun 2021 14:19:32 +0100 Subject: [PATCH 1/3] Remove config.NodeState (hot reconfig is no longer supported) --- cmd/yggdrasil/main.go | 27 +++++++++++------------ src/admin/admin.go | 7 +++--- src/config/config.go | 32 +-------------------------- src/core/core.go | 44 +++++++++++++------------------------- src/core/core_test.go | 6 ++---- src/core/link.go | 4 +++- src/core/tcp.go | 6 +++--- src/module/module.go | 2 +- src/multicast/multicast.go | 16 +++++++------- src/tuntap/tun.go | 21 +++++++++++------- 10 files changed, 63 insertions(+), 102 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index e0d79fc4..6484edf9 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -35,7 +35,7 @@ import ( type node struct { core core.Core - state *config.NodeState + config *config.NodeConfig tuntap module.Module // tuntap.TunAdapter multicast module.Module // multicast.Multicast admin module.Module // admin.AdminSocket @@ -272,8 +272,7 @@ func main() { n := node{} // Now start Yggdrasil - this starts the DHT, router, switch and other core // components needed for Yggdrasil to operate - n.state, err = n.core.Start(cfg, logger) - if err != nil { + if err = n.core.Start(cfg, logger); err != nil { logger.Errorln("An error occurred during startup") panic(err) } @@ -284,19 +283,19 @@ func main() { n.tuntap = &tuntap.TunAdapter{} n.tuntap.(*tuntap.TunAdapter).SetSessionGatekeeper(n.sessionFirewall) // Start the admin socket - n.admin.Init(&n.core, n.state, logger, nil) + n.admin.Init(&n.core, cfg, logger, nil) if err := n.admin.Start(); err != nil { logger.Errorln("An error occurred starting admin socket:", err) } n.admin.SetupAdminHandlers(n.admin.(*admin.AdminSocket)) // Start the multicast interface - n.multicast.Init(&n.core, n.state, logger, nil) + n.multicast.Init(&n.core, cfg, logger, nil) if err := n.multicast.Start(); err != nil { logger.Errorln("An error occurred starting multicast:", err) } n.multicast.SetupAdminHandlers(n.admin.(*admin.AdminSocket)) // Start the TUN/TAP interface - n.tuntap.Init(&n.core, n.state, logger, nil) + n.tuntap.Init(&n.core, cfg, logger, nil) if err := n.tuntap.Start(); err != nil { logger.Errorln("An error occurred starting TUN/TAP:", err) } @@ -324,16 +323,16 @@ func (n *node) shutdown() { } func (n *node) sessionFirewall(pubkey ed25519.PublicKey, initiator bool) bool { - n.state.Mutex.RLock() - defer n.state.Mutex.RUnlock() + n.config.RLock() + defer n.config.RUnlock() // Allow by default if the session firewall is disabled - if !n.state.Current.SessionFirewall.Enable { + if !n.config.SessionFirewall.Enable { return true } // Reject blacklisted nodes - for _, b := range n.state.Current.SessionFirewall.BlacklistPublicKeys { + for _, b := range n.config.SessionFirewall.BlacklistPublicKeys { key, err := hex.DecodeString(b) if err == nil { if bytes.Equal(key, pubkey) { @@ -343,7 +342,7 @@ func (n *node) sessionFirewall(pubkey ed25519.PublicKey, initiator bool) bool { } // Allow whitelisted nodes - for _, b := range n.state.Current.SessionFirewall.WhitelistPublicKeys { + for _, b := range n.config.SessionFirewall.WhitelistPublicKeys { key, err := hex.DecodeString(b) if err == nil { if bytes.Equal(key, pubkey) { @@ -353,7 +352,7 @@ func (n *node) sessionFirewall(pubkey ed25519.PublicKey, initiator bool) bool { } // Allow outbound sessions if appropriate - if n.state.Current.SessionFirewall.AlwaysAllowOutbound { + if n.config.SessionFirewall.AlwaysAllowOutbound { if initiator { return true } @@ -369,12 +368,12 @@ func (n *node) sessionFirewall(pubkey ed25519.PublicKey, initiator bool) bool { } // Allow direct peers if appropriate - if n.state.Current.SessionFirewall.AllowFromDirect && isDirectPeer { + if n.config.SessionFirewall.AllowFromDirect && isDirectPeer { return true } // Allow remote nodes if appropriate - if n.state.Current.SessionFirewall.AllowFromRemote && !isDirectPeer { + if n.config.SessionFirewall.AllowFromRemote && !isDirectPeer { return true } diff --git a/src/admin/admin.go b/src/admin/admin.go index 8322b06f..7d5c66d1 100644 --- a/src/admin/admin.go +++ b/src/admin/admin.go @@ -63,12 +63,13 @@ func (a *AdminSocket) AddHandler(name string, args []string, handlerfunc func(js } // Init runs the initial admin setup. -func (a *AdminSocket) Init(c *core.Core, state *config.NodeState, log *log.Logger, options interface{}) error { +func (a *AdminSocket) Init(c *core.Core, nc *config.NodeConfig, log *log.Logger, options interface{}) error { a.core = c a.log = log a.handlers = make(map[string]handler) - current := state.GetCurrent() - a.listenaddr = current.AdminListen + nc.RLock() + a.listenaddr = nc.AdminListen + nc.RUnlock() a.done = make(chan struct{}) close(a.done) // Start in a done / not-started state _ = a.AddHandler("list", []string{}, func(_ json.RawMessage) (interface{}, error) { diff --git a/src/config/config.go b/src/config/config.go index 073e1597..af47face 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -24,41 +24,11 @@ import ( "github.com/yggdrasil-network/yggdrasil-go/src/defaults" ) -// NodeState represents the active and previous configuration of an Yggdrasil -// node. A NodeState object is returned when starting an Yggdrasil node. Note -// that this structure and related functions are likely to disappear soon. -type NodeState struct { - Current NodeConfig - Previous NodeConfig - Mutex sync.RWMutex -} - -// Current returns the active node configuration. -func (s *NodeState) GetCurrent() NodeConfig { - s.Mutex.RLock() - defer s.Mutex.RUnlock() - return s.Current -} - -// Previous returns the previous node configuration. -func (s *NodeState) GetPrevious() NodeConfig { - s.Mutex.RLock() - defer s.Mutex.RUnlock() - return s.Previous -} - -// Replace the node configuration with new configuration. -func (s *NodeState) Replace(n NodeConfig) { - s.Mutex.Lock() - defer s.Mutex.Unlock() - s.Previous = s.Current - s.Current = n -} - // NodeConfig is the main configuration structure, containing configuration // options that are necessary for an Yggdrasil node to run. You will need to // supply one of these structs to the Yggdrasil core when starting a node. type NodeConfig struct { + sync.RWMutex Peers []string `comment:"List of connection strings for outbound peer connections in URI format,\ne.g. tcp://a.b.c.d:e or socks://a.b.c.d:e/f.g.h.i:j. These connections\nwill obey the operating system routing table, therefore you should\nuse this section when you may connect via different interfaces."` InterfacePeers map[string][]string `comment:"List of connection strings for outbound peer connections in URI format,\narranged by source interface, e.g. { \"eth0\": [ tcp://a.b.c.d:e ] }.\nNote that SOCKS peerings will NOT be affected by this option and should\ngo in the \"Peers\" section instead."` Listen []string `comment:"Listen addresses for incoming connections. You will need to add\nlisteners in order to accept incoming peerings from non-local nodes.\nMulticast peer discovery will work regardless of any listeners set\nhere. Each listener should be specified in URI format as above, e.g.\ntcp://0.0.0.0:0 or tcp://[::]:0 to listen on all interfaces."` diff --git a/src/core/core.go b/src/core/core.go index f3942cdb..b98776b9 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -25,7 +25,7 @@ type Core struct { // guarantee that it will be covered by the mutex phony.Inbox *iw.PacketConn - config config.NodeState // Config + config *config.NodeConfig // Config secret ed25519.PrivateKey public ed25519.PublicKey links links @@ -42,9 +42,9 @@ func (c *Core) _init() error { c.log = log.New(ioutil.Discard, "", 0) } - current := c.config.GetCurrent() - - sigPriv, err := hex.DecodeString(current.PrivateKey) + c.config.RLock() + sigPriv, err := hex.DecodeString(c.config.PrivateKey) + c.config.RUnlock() if err != nil { return err } @@ -64,11 +64,11 @@ func (c *Core) _init() error { // configure them. The loop ensures that disconnected peers will eventually // be reconnected with. func (c *Core) _addPeerLoop() { - // Get the peers from the config - these could change! - current := c.config.GetCurrent() + c.config.RLock() + defer c.config.RUnlock() // Add peers from the Peers section - for _, peer := range current.Peers { + for _, peer := range c.config.Peers { go func(peer string, intf string) { u, err := url.Parse(peer) if err != nil { @@ -81,7 +81,7 @@ func (c *Core) _addPeerLoop() { } // Add peers from the InterfacePeers section - for intf, intfpeers := range current.InterfacePeers { + for intf, intfpeers := range c.config.InterfacePeers { for _, peer := range intfpeers { go func(peer string, intf string) { u, err := url.Parse(peer) @@ -107,21 +107,17 @@ func (c *Core) _addPeerLoop() { // TCP and UDP sockets, a multicast discovery socket, an admin socket, router, // switch and DHT node. A config.NodeState is returned which contains both the // current and previous configurations (from reconfigures). -func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (conf *config.NodeState, err error) { +func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (err error) { phony.Block(c, func() { - conf, err = c._start(nc, log) + err = c._start(nc, log) }) return } // This function is unsafe and should only be ran by the core actor. -func (c *Core) _start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState, error) { +func (c *Core) _start(nc *config.NodeConfig, log *log.Logger) error { c.log = log - - c.config = config.NodeState{ - Current: *nc, - Previous: *nc, - } + c.config = nc if name := version.BuildName(); name != "unknown" { c.log.Infoln("Build name:", name) @@ -133,30 +129,20 @@ func (c *Core) _start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState c.log.Infoln("Starting up...") if err := c._init(); err != nil { c.log.Errorln("Failed to initialize core") - return nil, err + return err } if err := c.links.init(c); err != nil { c.log.Errorln("Failed to start link interfaces") - return nil, err + return err } - //if err := c.switchTable.start(); err != nil { - // c.log.Errorln("Failed to start switch") - // return nil, err - //} - - //if err := c.router.start(); err != nil { - // c.log.Errorln("Failed to start router") - // return nil, err - //} - c.addPeerTimer = time.AfterFunc(0, func() { c.Act(nil, c._addPeerLoop) }) c.log.Infoln("Startup complete") - return &c.config, nil + return nil } // Stop shuts down the Yggdrasil node. diff --git a/src/core/core_test.go b/src/core/core_test.go index 74012333..7e7f32d1 100644 --- a/src/core/core_test.go +++ b/src/core/core_test.go @@ -40,14 +40,12 @@ func GetLoggerWithPrefix(prefix string, verbose bool) *log.Logger { // Verbosity flag is passed to logger. func CreateAndConnectTwo(t testing.TB, verbose bool) (nodeA *Core, nodeB *Core) { nodeA = new(Core) - _, err := nodeA.Start(GenerateConfig(), GetLoggerWithPrefix("A: ", verbose)) - if err != nil { + if err := nodeA.Start(GenerateConfig(), GetLoggerWithPrefix("A: ", verbose)); err != nil { t.Fatal(err) } nodeB = new(Core) - _, err = nodeB.Start(GenerateConfig(), GetLoggerWithPrefix("B: ", verbose)) - if err != nil { + if err := nodeB.Start(GenerateConfig(), GetLoggerWithPrefix("B: ", verbose)); err != nil { t.Fatal(err) } diff --git a/src/core/link.go b/src/core/link.go index 80bd0c30..b4cd15ff 100644 --- a/src/core/link.go +++ b/src/core/link.go @@ -188,7 +188,9 @@ func (intf *link) handler() (chan struct{}, error) { } } // Check if we're authorized to connect to this key / IP - allowed := intf.links.core.config.GetCurrent().AllowedPublicKeys + intf.links.core.config.RLock() + allowed := intf.links.core.config.AllowedPublicKeys + intf.links.core.config.RUnlock() isallowed := len(allowed) == 0 for _, k := range allowed { if k == hex.EncodeToString(meta.key) { // TODO: this is yuck diff --git a/src/core/tcp.go b/src/core/tcp.go index eced7726..17e37660 100644 --- a/src/core/tcp.go +++ b/src/core/tcp.go @@ -104,9 +104,9 @@ func (t *tcp) init(l *links) error { t.listeners = make(map[string]*TcpListener) t.mutex.Unlock() - t.links.core.config.Mutex.RLock() - defer t.links.core.config.Mutex.RUnlock() - for _, listenaddr := range t.links.core.config.Current.Listen { + t.links.core.config.RLock() + defer t.links.core.config.RUnlock() + for _, listenaddr := range t.links.core.config.Listen { u, err := url.Parse(listenaddr) if err != nil { t.links.core.log.Errorln("Failed to parse listener: listener", listenaddr, "is not correctly formatted, ignoring") diff --git a/src/module/module.go b/src/module/module.go index d13b8cd2..8f915005 100644 --- a/src/module/module.go +++ b/src/module/module.go @@ -11,7 +11,7 @@ import ( // Module is an interface that defines which functions must be supported by a // given Yggdrasil module. type Module interface { - Init(core *core.Core, state *config.NodeState, log *log.Logger, options interface{}) error + Init(core *core.Core, state *config.NodeConfig, log *log.Logger, options interface{}) error Start() error Stop() error SetupAdminHandlers(a *admin.AdminSocket) diff --git a/src/multicast/multicast.go b/src/multicast/multicast.go index c1b87890..02ff0c44 100644 --- a/src/multicast/multicast.go +++ b/src/multicast/multicast.go @@ -23,7 +23,7 @@ import ( type Multicast struct { phony.Inbox core *core.Core - config *config.NodeState + config *config.NodeConfig log *log.Logger sock *ipv6.PacketConn groupAddr string @@ -45,14 +45,13 @@ type listenerInfo struct { } // Init prepares the multicast interface for use. -func (m *Multicast) Init(core *core.Core, state *config.NodeState, log *log.Logger, options interface{}) error { +func (m *Multicast) Init(core *core.Core, nc *config.NodeConfig, log *log.Logger, options interface{}) error { m.core = core - m.config = state + m.config = nc m.log = log m.listeners = make(map[string]*listenerInfo) m._interfaces = make(map[string]interfaceInfo) - current := m.config.GetCurrent() - m.listenPort = current.LinkLocalTCPPort + m.listenPort = m.config.LinkLocalTCPPort m.groupAddr = "[ff02::114]:9001" return nil } @@ -73,7 +72,9 @@ func (m *Multicast) _start() error { if m.isOpen { return fmt.Errorf("multicast module is already started") } - if len(m.config.GetCurrent().MulticastInterfaces) == 0 { + m.config.RLock() + defer m.config.RUnlock() + if len(m.config.MulticastInterfaces) == 0 { return nil } m.log.Infoln("Starting multicast module") @@ -161,8 +162,7 @@ func (m *Multicast) Interfaces() map[string]net.Interface { func (m *Multicast) getAllowedInterfaces() map[string]net.Interface { interfaces := make(map[string]net.Interface) // Get interface expressions from config - current := m.config.GetCurrent() - exprs := current.MulticastInterfaces + exprs := m.config.MulticastInterfaces // Ask the system for network interfaces allifaces, err := net.Interfaces() if err != nil { diff --git a/src/tuntap/tun.go b/src/tuntap/tun.go index 49d8e426..ecc7c06c 100644 --- a/src/tuntap/tun.go +++ b/src/tuntap/tun.go @@ -35,7 +35,7 @@ type MTU uint16 type TunAdapter struct { core *core.Core store keyStore - config *config.NodeState + config *config.NodeConfig log *log.Logger addr address.Address subnet address.Subnet @@ -103,13 +103,17 @@ func MaximumMTU() uint64 { // Init initialises the TUN module. You must have acquired a Listener from // the Yggdrasil core before this point and it must not be in use elsewhere. -func (tun *TunAdapter) Init(core *core.Core, config *config.NodeState, log *log.Logger, options interface{}) error { +func (tun *TunAdapter) Init(core *core.Core, config *config.NodeConfig, log *log.Logger, options interface{}) error { tun.core = core tun.store.init(tun) tun.config = config tun.log = log tun.proto.init(tun) - tun.proto.nodeinfo.setNodeInfo(config.Current.NodeInfo, config.Current.NodeInfoPrivacy) + tun.config.RLock() + if err := tun.proto.nodeinfo.setNodeInfo(tun.config.NodeInfo, tun.config.NodeInfoPrivacy); err != nil { + return fmt.Errorf("tun.proto.nodeinfo.setNodeInfo: %w", err) + } + tun.config.RUnlock() if err := tun.core.SetOutOfBandHandler(tun.oobHandler); err != nil { return fmt.Errorf("tun.core.SetOutOfBandHander: %w", err) } @@ -130,7 +134,8 @@ func (tun *TunAdapter) _start() error { if tun.isOpen { return errors.New("TUN module is already started") } - current := tun.config.GetCurrent() + tun.config.RLock() + defer tun.config.RUnlock() if tun.config == nil { return errors.New("no configuration available to TUN") } @@ -139,21 +144,21 @@ func (tun *TunAdapter) _start() error { tun.addr = *address.AddrForKey(pk) tun.subnet = *address.SubnetForKey(pk) addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1) - if current.IfName == "none" || current.IfName == "dummy" { + if tun.config.IfName == "none" || tun.config.IfName == "dummy" { tun.log.Debugln("Not starting TUN as ifname is none or dummy") tun.isEnabled = false go tun.write() return nil } - mtu := current.IfMTU + mtu := tun.config.IfMTU if tun.maxSessionMTU() < mtu { mtu = tun.maxSessionMTU() } - if err := tun.setup(current.IfName, addr, mtu); err != nil { + if err := tun.setup(tun.config.IfName, addr, mtu); err != nil { return err } if tun.MTU() != mtu { - tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", current.IfMTU, tun.MTU(), MaximumMTU()) + tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", tun.config.IfMTU, tun.MTU(), MaximumMTU()) } tun.isOpen = true tun.isEnabled = true From 8932ab051916416eb71452e7febc4de0ab8b8378 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 2 Jun 2021 14:40:09 +0100 Subject: [PATCH 2/3] Fix lint errors --- cmd/yggdrasil/main.go | 25 +++++++++++++++---------- cmd/yggdrasilctl/main.go | 4 ++-- src/core/core.go | 2 +- src/core/link.go | 12 ------------ src/core/tcp.go | 4 ++-- src/multicast/multicast.go | 6 +++--- src/multicast/multicast_darwin.go | 2 -- src/tuntap/iface.go | 6 +----- src/tuntap/keystore.go | 8 ++++---- src/tuntap/nodeinfo.go | 4 ++-- src/tuntap/proto.go | 2 +- src/tuntap/tun.go | 4 ++-- src/tuntap/tun_darwin.go | 15 +++++++++------ src/tuntap/types.go | 4 ++-- 14 files changed, 44 insertions(+), 54 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 6484edf9..0f830bb2 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -107,7 +107,9 @@ func readConfig(useconf *bool, useconffile *string, normaliseconf *bool) *config if err != nil { panic(err) } - json.Unmarshal(confJson, &cfg) + if err := json.Unmarshal(confJson, &cfg); err != nil { + panic(err) + } // Overlay our newly mapped configuration onto the autoconf node config that // we generated above. if err = mapstructure.Decode(dat, &cfg); err != nil { @@ -283,20 +285,23 @@ func main() { n.tuntap = &tuntap.TunAdapter{} n.tuntap.(*tuntap.TunAdapter).SetSessionGatekeeper(n.sessionFirewall) // Start the admin socket - n.admin.Init(&n.core, cfg, logger, nil) - if err := n.admin.Start(); err != nil { + if err := n.admin.Init(&n.core, cfg, logger, nil); err != nil { + logger.Errorln("An error occured initialising admin socket:", err) + } else if err := n.admin.Start(); err != nil { logger.Errorln("An error occurred starting admin socket:", err) } n.admin.SetupAdminHandlers(n.admin.(*admin.AdminSocket)) // Start the multicast interface - n.multicast.Init(&n.core, cfg, logger, nil) - if err := n.multicast.Start(); err != nil { + if err := n.multicast.Init(&n.core, cfg, logger, nil); err != nil { + logger.Errorln("An error occured initialising multicast:", err) + } else if err := n.multicast.Start(); err != nil { logger.Errorln("An error occurred starting multicast:", err) } n.multicast.SetupAdminHandlers(n.admin.(*admin.AdminSocket)) // Start the TUN/TAP interface - n.tuntap.Init(&n.core, cfg, logger, nil) - if err := n.tuntap.Start(); err != nil { + if err := n.tuntap.Init(&n.core, cfg, logger, nil); err != nil { + logger.Errorln("An error occurred initialising TUN/TAP:", err) + } else if err := n.tuntap.Start(); err != nil { logger.Errorln("An error occurred starting TUN/TAP:", err) } n.tuntap.SetupAdminHandlers(n.admin.(*admin.AdminSocket)) @@ -316,9 +321,9 @@ func main() { } func (n *node) shutdown() { - n.admin.Stop() - n.multicast.Stop() - n.tuntap.Stop() + _ = n.admin.Stop() + _ = n.multicast.Stop() + _ = n.tuntap.Stop() n.core.Stop() } diff --git a/cmd/yggdrasilctl/main.go b/cmd/yggdrasilctl/main.go index 38d935a6..884656db 100644 --- a/cmd/yggdrasilctl/main.go +++ b/cmd/yggdrasilctl/main.go @@ -47,8 +47,8 @@ func run() int { fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] command [key=value] [key=value] ...\n\n", os.Args[0]) fmt.Println("Options:") flag.PrintDefaults() - fmt.Println("\nPlease note that options must always specified BEFORE the command\non the command line or they will be ignored.\n") - fmt.Println("Commands:\n - Use \"list\" for a list of available commands\n") + fmt.Println("\nPlease note that options must always specified BEFORE the command\non the command line or they will be ignored.\n") // nolint:govet + fmt.Println("Commands:\n - Use \"list\" for a list of available commands\n") // nolint:govet fmt.Println("Examples:") fmt.Println(" - ", os.Args[0], "list") fmt.Println(" - ", os.Args[0], "getPeers") diff --git a/src/core/core.go b/src/core/core.go index b98776b9..de916c1b 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -158,7 +158,7 @@ func (c *Core) _stop() { c.addPeerTimer.Stop() c.addPeerTimer = nil } - c.links.stop() + _ = c.links.stop() /* FIXME this deadlocks, need a waitgroup or something to coordinate shutdown for _, peer := range c.GetPeers() { c.DisconnectPeer(peer.Port) diff --git a/src/core/link.go b/src/core/link.go index b4cd15ff..51b679a6 100644 --- a/src/core/link.go +++ b/src/core/link.go @@ -250,15 +250,3 @@ func (intf *link) close() { func (intf *link) name() string { return intf.lname } - -func (intf *link) local() string { - return intf.info.local -} - -func (intf *link) remote() string { - return intf.info.remote -} - -func (intf *link) interfaceType() string { - return intf.info.linkType -} diff --git a/src/core/tcp.go b/src/core/tcp.go index 17e37660..21a3ba79 100644 --- a/src/core/tcp.go +++ b/src/core/tcp.go @@ -67,7 +67,7 @@ type tcpOptions struct { } func (l *TcpListener) Stop() { - defer func() { recover() }() + defer func() { _ = recover() }() close(l.stop) } @@ -75,7 +75,7 @@ func (l *TcpListener) Stop() { func (t *tcp) setExtraOptions(c net.Conn) { switch sock := c.(type) { case *net.TCPConn: - sock.SetNoDelay(true) + _ = sock.SetNoDelay(true) // TODO something for socks5 default: } diff --git a/src/multicast/multicast.go b/src/multicast/multicast.go index 02ff0c44..8f8054dd 100644 --- a/src/multicast/multicast.go +++ b/src/multicast/multicast.go @@ -91,7 +91,7 @@ func (m *Multicast) _start() error { return err } m.sock = ipv6.NewPacketConn(conn) - if err = m.sock.SetControlMessage(ipv6.FlagDst, true); err != nil { + if err = m.sock.SetControlMessage(ipv6.FlagDst, true); err != nil { // nolint:staticcheck // Windows can't set this flag, so we need to handle it in other ways } @@ -269,7 +269,7 @@ func (m *Multicast) _announce() { continue } // Join the multicast group - m.sock.JoinGroup(&iface, groupAddr) + _ = m.sock.JoinGroup(&iface, groupAddr) // Try and see if we already have a TCP listener for this interface var info *listenerInfo if nfo, ok := m.listeners[iface.Name]; !ok || nfo.listener.Listener == nil { @@ -304,7 +304,7 @@ func (m *Multicast) _announce() { a.Zone = "" destAddr.Zone = iface.Name msg := []byte(a.String()) - m.sock.WriteTo(msg, nil, destAddr) + _, _ = m.sock.WriteTo(msg, nil, destAddr) } if info.interval.Seconds() < 15 { info.interval += time.Second diff --git a/src/multicast/multicast_darwin.go b/src/multicast/multicast_darwin.go index ceff5b44..e7075c0a 100644 --- a/src/multicast/multicast_darwin.go +++ b/src/multicast/multicast_darwin.go @@ -29,8 +29,6 @@ import ( "golang.org/x/sys/unix" ) -var awdlGoroutineStarted bool - func (m *Multicast) _multicastStarted() { if !m.isOpen { return diff --git a/src/tuntap/iface.go b/src/tuntap/iface.go index 81fbe4d4..da66e9d2 100644 --- a/src/tuntap/iface.go +++ b/src/tuntap/iface.go @@ -123,16 +123,12 @@ func (tun *TunAdapter) write() { continue // bad remote address/subnet } bs = buf[:TUN_OFFSET_BYTES+len(bs)] - n, err = tun.iface.Write(bs, TUN_OFFSET_BYTES) - if err != nil { + if _, err = tun.iface.Write(bs, TUN_OFFSET_BYTES); err != nil { tun.Act(nil, func() { if !tun.isOpen { tun.log.Errorln("TUN iface write error:", err) } }) } - if n != len(bs) { - // TODO some kind of error reporting for a partial write - } } } diff --git a/src/tuntap/keystore.go b/src/tuntap/keystore.go index 7974874c..03f429f7 100644 --- a/src/tuntap/keystore.go +++ b/src/tuntap/keystore.go @@ -50,7 +50,7 @@ func (k *keyStore) sendToAddress(addr address.Address, bs []byte) { if info := k.addrToInfo[addr]; info != nil { k.resetTimeout(info) k.mutex.Unlock() - k.tun.core.WriteTo(bs, iwt.Addr(info.key[:])) + _, _ = k.tun.core.WriteTo(bs, iwt.Addr(info.key[:])) } else { var buf *buffer if buf = k.addrBuffer[addr]; buf == nil { @@ -79,7 +79,7 @@ func (k *keyStore) sendToSubnet(subnet address.Subnet, bs []byte) { if info := k.subnetToInfo[subnet]; info != nil { k.resetTimeout(info) k.mutex.Unlock() - k.tun.core.WriteTo(bs, iwt.Addr(info.key[:])) + _, _ = k.tun.core.WriteTo(bs, iwt.Addr(info.key[:])) } else { var buf *buffer if buf = k.subnetBuffer[subnet]; buf == nil { @@ -132,13 +132,13 @@ func (k *keyStore) update(key ed25519.PublicKey) *keyInfo { k.mutex.Unlock() if buf := k.addrBuffer[info.address]; buf != nil { for _, bs := range buf.packets { - k.tun.core.WriteTo(bs, iwt.Addr(info.key[:])) + _, _ = k.tun.core.WriteTo(bs, iwt.Addr(info.key[:])) } delete(k.addrBuffer, info.address) } if buf := k.subnetBuffer[info.subnet]; buf != nil { for _, bs := range buf.packets { - k.tun.core.WriteTo(bs, iwt.Addr(info.key[:])) + _, _ = k.tun.core.WriteTo(bs, iwt.Addr(info.key[:])) } delete(k.subnetBuffer, info.subnet) } diff --git a/src/tuntap/nodeinfo.go b/src/tuntap/nodeinfo.go index 3249b20c..a61a4429 100644 --- a/src/tuntap/nodeinfo.go +++ b/src/tuntap/nodeinfo.go @@ -129,7 +129,7 @@ func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo NodeInfoPayload if callback != nil { m._addCallback(key, callback) } - m.proto.tun.core.WriteTo([]byte{typeSessionProto, typeProtoNodeInfoRequest}, iwt.Addr(key[:])) + _, _ = m.proto.tun.core.WriteTo([]byte{typeSessionProto, typeProtoNodeInfoRequest}, iwt.Addr(key[:])) } func (m *nodeinfo) handleReq(from phony.Actor, key keyArray) { @@ -146,7 +146,7 @@ func (m *nodeinfo) handleRes(from phony.Actor, key keyArray, info NodeInfoPayloa func (m *nodeinfo) _sendRes(key keyArray) { bs := append([]byte{typeSessionProto, typeProtoNodeInfoResponse}, m._getNodeInfo()...) - m.proto.tun.core.WriteTo(bs, iwt.Addr(key[:])) + _, _ = m.proto.tun.core.WriteTo(bs, iwt.Addr(key[:])) } // Admin socket stuff diff --git a/src/tuntap/proto.go b/src/tuntap/proto.go index 79d84b2e..62b21563 100644 --- a/src/tuntap/proto.go +++ b/src/tuntap/proto.go @@ -209,7 +209,7 @@ func (p *protoHandler) _handleGetDHTResponse(key keyArray, bs []byte) { func (p *protoHandler) _sendDebug(key keyArray, dType uint8, data []byte) { bs := append([]byte{typeSessionProto, typeProtoDebug, dType}, data...) - p.tun.core.WriteTo(bs, iwt.Addr(key[:])) + _, _ = p.tun.core.WriteTo(bs, iwt.Addr(key[:])) } // Admin socket stuff diff --git a/src/tuntap/tun.go b/src/tuntap/tun.go index ecc7c06c..c1b90e75 100644 --- a/src/tuntap/tun.go +++ b/src/tuntap/tun.go @@ -221,13 +221,13 @@ func (tun *TunAdapter) oobHandler(fromKey, toKey ed25519.PublicKey, data []byte) func (tun *TunAdapter) sendKeyLookup(partial ed25519.PublicKey) { sig := ed25519.Sign(tun.core.PrivateKey(), partial[:]) bs := append([]byte{typeKeyLookup}, sig...) - tun.core.SendOutOfBand(partial, bs) + _ = tun.core.SendOutOfBand(partial, bs) } func (tun *TunAdapter) sendKeyResponse(dest ed25519.PublicKey) { sig := ed25519.Sign(tun.core.PrivateKey(), dest[:]) bs := append([]byte{typeKeyResponse}, sig...) - tun.core.SendOutOfBand(dest, bs) + _ = tun.core.SendOutOfBand(dest, bs) } func (tun *TunAdapter) maxSessionMTU() uint64 { diff --git a/src/tuntap/tun_darwin.go b/src/tuntap/tun_darwin.go index 609b42e3..060ad144 100644 --- a/src/tuntap/tun_darwin.go +++ b/src/tuntap/tun_darwin.go @@ -40,26 +40,29 @@ const ( darwin_ND6_INFINITE_LIFETIME = 0xFFFFFFFF // netinet6/nd6.h ) +// nolint:structcheck type in6_addrlifetime struct { - ia6t_expire float64 - ia6t_preferred float64 + ia6t_expire float64 // nolint:unused + ia6t_preferred float64 // nolint:unused ia6t_vltime uint32 ia6t_pltime uint32 } +// nolint:structcheck type sockaddr_in6 struct { sin6_len uint8 sin6_family uint8 - sin6_port uint8 - sin6_flowinfo uint32 + sin6_port uint8 // nolint:unused + sin6_flowinfo uint32 // nolint:unused sin6_addr [8]uint16 - sin6_scope_id uint32 + sin6_scope_id uint32 // nolint:unused } +// nolint:structcheck type in6_aliasreq struct { ifra_name [16]byte ifra_addr sockaddr_in6 - ifra_dstaddr sockaddr_in6 + ifra_dstaddr sockaddr_in6 // nolint:unused ifra_prefixmask sockaddr_in6 ifra_flags uint32 ifra_lifetime in6_addrlifetime diff --git a/src/tuntap/types.go b/src/tuntap/types.go index 4d8bba1f..a8084e0c 100644 --- a/src/tuntap/types.go +++ b/src/tuntap/types.go @@ -2,14 +2,14 @@ package tuntap // Out-of-band packet types const ( - typeKeyDummy = iota + typeKeyDummy = iota // nolint:deadcode,varcheck typeKeyLookup typeKeyResponse ) // In-band packet types const ( - typeSessionDummy = iota + typeSessionDummy = iota // nolint:deadcode,varcheck typeSessionTraffic typeSessionProto ) From ff751a5409deb83544c5348c1c3c45acfd5b9b5f Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 2 Jun 2021 14:46:04 +0100 Subject: [PATCH 3/3] Fix lint error --- src/core/tcp_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/tcp_linux.go b/src/core/tcp_linux.go index 39e3ba7a..558b4e56 100644 --- a/src/core/tcp_linux.go +++ b/src/core/tcp_linux.go @@ -36,7 +36,7 @@ func (t *tcp) getControl(sintf string) func(string, string, syscall.RawConn) err btd := func(fd uintptr) { err = unix.BindToDevice(int(fd), sintf) } - c.Control(btd) + _ = c.Control(btd) if err != nil { t.links.core.log.Debugln("Failed to set SO_BINDTODEVICE:", sintf) }