From be9dc867a8648ea3c9f56359c6952232ec912409 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Tue, 9 Jul 2019 13:07:28 +0300 Subject: [PATCH] More minor improvements --- pkg/app/app.go | 14 +++++++------- pkg/router/app_manager_test.go | 6 +++--- pkg/router/loop_list.go | 2 +- pkg/router/route_manager.go | 2 +- pkg/router/router.go | 5 +++-- pkg/router/router_test.go | 12 ++++++------ pkg/setup/node.go | 10 +++++----- pkg/setup/node_test.go | 2 +- pkg/setup/protocol.go | 16 ++++++++-------- 9 files changed, 35 insertions(+), 34 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 95f6a22f8..c3d2fdb5b 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -120,12 +120,12 @@ func (app *App) Accept() (net.Conn, error) { laddr := addrs[0] raddr := addrs[1] - addr := &routing.Loop{Local: *laddr, Remote: *raddr} + loop := routing.Loop{Local: *laddr, Remote: *raddr} conn, out := net.Pipe() app.mu.Lock() - app.conns[*addr] = conn + app.conns[loop] = conn app.mu.Unlock() - go app.serveConn(addr, conn) + go app.serveConn(&loop, conn) return newAppConn(out, laddr, raddr), nil } @@ -136,12 +136,12 @@ func (app *App) Dial(raddr *routing.Addr) (net.Conn, error) { if err != nil { return nil, err } - addr := &routing.Loop{Local: laddr, Remote: *raddr} + loop := routing.Loop{Local: laddr, Remote: *raddr} conn, out := net.Pipe() app.mu.Lock() - app.conns[*addr] = conn + app.conns[loop] = conn app.mu.Unlock() - go app.serveConn(addr, conn) + go app.serveConn(&loop, conn) return newAppConn(out, &laddr, raddr), nil } @@ -228,7 +228,7 @@ func (app *App) closeConn(data []byte) error { } func (app *App) confirmLoop(data []byte) error { - addrs := [2]*routing.Addr{} + var addrs [2]*routing.Addr if err := json.Unmarshal(data, &addrs); err != nil { return err } diff --git a/pkg/router/app_manager_test.go b/pkg/router/app_manager_test.go index 3218bc2f7..45d560a54 100644 --- a/pkg/router/app_manager_test.go +++ b/pkg/router/app_manager_test.go @@ -71,10 +71,10 @@ func TestAppManagerSetupLoop(t *testing.T) { proto := app.NewProtocol(in) go proto.Serve(nil) // nolint: errcheck - var laddr *routing.Addr + var laddr routing.Addr pk, _ := cipher.GenerateKeyPair() - raddr := &routing.Addr{PubKey: pk, Port: 3} - err := proto.Send(app.FrameCreateLoop, raddr, &laddr) + raddr := routing.Addr{PubKey: pk, Port: 3} + err := proto.Send(app.FrameCreateLoop, &raddr, &laddr) require.NoError(t, err) assert.Equal(t, raddr, laddr) diff --git a/pkg/router/loop_list.go b/pkg/router/loop_list.go index 2c231cecc..aa5d96bee 100644 --- a/pkg/router/loop_list.go +++ b/pkg/router/loop_list.go @@ -44,8 +44,8 @@ func (ll *loopList) remove(addr *routing.Addr) { } func (ll *loopList) dropAll() []routing.Addr { - r := make([]routing.Addr, 0) ll.Lock() + r := make([]routing.Addr, 0, len(ll.loops)) for addr := range ll.loops { r = append(r, addr) } diff --git a/pkg/router/route_manager.go b/pkg/router/route_manager.go index 66f0e1afd..6c817fc9f 100644 --- a/pkg/router/route_manager.go +++ b/pkg/router/route_manager.go @@ -140,7 +140,7 @@ func (rm *routeManager) deleteRoutingRules(data []byte) ([]routing.RouteID, erro } func (rm *routeManager) confirmLoop(data []byte) error { - ld := routing.LoopData{} + var ld routing.LoopData if err := json.Unmarshal(data, &ld); err != nil { return err } diff --git a/pkg/router/router.go b/pkg/router/router.go index 7db787fdc..6c622a9fc 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -217,9 +217,10 @@ func (r *Router) forwardPacket(payload []byte, rule routing.Rule) error { } func (r *Router) consumePacket(payload []byte, rule routing.Rule) error { - raddr := &routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()} + laddr := routing.Addr{Port: rule.LocalPort()} + raddr := routing.Addr{PubKey: rule.RemotePK(), Port: rule.RemotePort()} - p := &app.Packet{Loop: &routing.Loop{Local: routing.Addr{Port: rule.LocalPort()}, Remote: *raddr}, Payload: payload} + p := &app.Packet{Loop: &routing.Loop{Local: laddr, Remote: raddr}, Payload: payload} b, _ := r.pm.Get(rule.LocalPort()) // nolint: errcheck if err := b.conn.Send(app.FrameSend, p, nil); err != nil { return err diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 1ee66e739..15cd44179 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -367,7 +367,7 @@ func TestRouterSetup(t *testing.T) { assert.Equal(t, tr.Entry.ID, loop.trID) assert.Equal(t, routing.RouteID(2), loop.routeID) - addrs := [2]*routing.Addr{} + var addrs [2]*routing.Addr require.NoError(t, json.Unmarshal(<-dataCh, &addrs)) require.NoError(t, err) assert.Equal(t, pk1, addrs[0].PubKey) @@ -407,7 +407,7 @@ func TestRouterSetup(t *testing.T) { assert.Equal(t, tr.Entry.ID, l.trID) assert.Equal(t, routing.RouteID(2), l.routeID) - addrs := [2]*routing.Addr{} + var addrs [2]*routing.Addr require.NoError(t, json.Unmarshal(<-dataCh, &addrs)) require.NoError(t, err) assert.Equal(t, pk1, addrs[0].PubKey) @@ -503,8 +503,8 @@ func TestRouterSetupLoop(t *testing.T) { return } - l := &routing.LoopDescriptor{} - if err := json.Unmarshal(data, l); err != nil { + var l routing.LoopDescriptor + if err := json.Unmarshal(data, &l); err != nil { errCh <- err return } @@ -614,8 +614,8 @@ func TestRouterCloseLoop(t *testing.T) { return } - ld := &routing.LoopData{} - if err := json.Unmarshal(data, ld); err != nil { + var ld routing.LoopData + if err := json.Unmarshal(data, &ld); err != nil { errCh <- err return } diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 67995dd24..4f87b83e7 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -222,13 +222,13 @@ func (sn *Node) serveTransport(tr transport.Transport) error { startTime := time.Now() switch sp { case PacketCreateLoop: - loop := &routing.LoopDescriptor{} - if err = json.Unmarshal(data, loop); err == nil { - err = sn.createLoop(loop) + var loopDp routing.LoopDescriptor + if err = json.Unmarshal(data, &loopDp); err == nil { + err = sn.createLoop(&loopDp) } case PacketCloseLoop: - ld := &routing.LoopData{} - if err = json.Unmarshal(data, ld); err == nil { + var ld routing.LoopData + if err = json.Unmarshal(data, &ld); err == nil { remote, ok := sn.tm.Remote(tr.Edges()) if !ok { return errors.New("configured PubKey not found in edges") diff --git a/pkg/setup/node_test.go b/pkg/setup/node_test.go index 13b8a2380..a4b973231 100644 --- a/pkg/setup/node_test.go +++ b/pkg/setup/node_test.go @@ -354,7 +354,7 @@ func (n *mockNode) serveTransport(tr transport.Transport) error { } } case PacketConfirmLoop: - ld := routing.LoopData{} + var ld routing.LoopData json.Unmarshal(data, &ld) // nolint: errcheck for _, rule := range n.rules { if rule.Type() == routing.RuleApp && rule.RemotePK() == ld.Loop.Remote.PubKey && diff --git a/pkg/setup/protocol.go b/pkg/setup/protocol.go index b22816a59..94ce60408 100644 --- a/pkg/setup/protocol.go +++ b/pkg/setup/protocol.go @@ -132,32 +132,32 @@ func DeleteRule(p *Protocol, routeID routing.RouteID) error { } // CreateLoop sends CreateLoop setup request. -func CreateLoop(p *Protocol, l *routing.LoopDescriptor) error { - if err := p.WritePacket(PacketCreateLoop, l); err != nil { +func CreateLoop(p *Protocol, ld *routing.LoopDescriptor) error { + if err := p.WritePacket(PacketCreateLoop, ld); err != nil { return err } return readAndDecodePacket(p, nil) // TODO: data race. } // ConfirmLoop sends ConfirmLoop setup request. -func ConfirmLoop(p *Protocol, l *routing.LoopData) error { - if err := p.WritePacket(PacketConfirmLoop, l); err != nil { +func ConfirmLoop(p *Protocol, ld *routing.LoopData) error { + if err := p.WritePacket(PacketConfirmLoop, ld); err != nil { return err } return readAndDecodePacket(p, nil) } // CloseLoop sends CloseLoop setup request. -func CloseLoop(p *Protocol, l *routing.LoopData) error { - if err := p.WritePacket(PacketCloseLoop, l); err != nil { +func CloseLoop(p *Protocol, ld *routing.LoopData) error { + if err := p.WritePacket(PacketCloseLoop, ld); err != nil { return err } return readAndDecodePacket(p, nil) } // LoopClosed sends LoopClosed setup request. -func LoopClosed(p *Protocol, l *routing.LoopData) error { - if err := p.WritePacket(PacketLoopClosed, l); err != nil { +func LoopClosed(p *Protocol, ld *routing.LoopData) error { + if err := p.WritePacket(PacketLoopClosed, ld); err != nil { return err } return readAndDecodePacket(p, nil)