From 08cacffe536d2c69bb2235155653a2a4cb9341ce Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 17 Jul 2019 10:01:30 +0300 Subject: [PATCH] Ensure non-pointer usage of `routing.LoopDescriptor` --- pkg/router/router.go | 4 ++-- pkg/router/router_test.go | 6 +++--- pkg/routing/loop.go | 6 +++--- pkg/setup/node.go | 30 +++++++++++++++--------------- pkg/setup/node_test.go | 4 ++-- pkg/setup/protocol.go | 2 +- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pkg/router/router.go b/pkg/router/router.go index db9230b721..9da1e93896 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -287,7 +287,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing return routing.Addr{}, fmt.Errorf("route finder: %s", err) } - l := &routing.LoopDescriptor{ + ld := routing.LoopDescriptor{ Loop: routing.Loop{ Local: laddr, Remote: raddr, @@ -303,7 +303,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr routing.Addr) (routing } defer tr.Close() - if err := setup.CreateLoop(proto, l); err != nil { + if err := setup.CreateLoop(proto, ld); err != nil { return routing.Addr{}, fmt.Errorf("route setup: %s", err) } diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index b5967c4e0d..f26dc33d08 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -503,13 +503,13 @@ func TestRouterSetupLoop(t *testing.T) { return } - var l routing.LoopDescriptor - if err := json.Unmarshal(data, &l); err != nil { + var ld routing.LoopDescriptor + if err := json.Unmarshal(data, &ld); err != nil { errCh <- err return } - if l.Loop.Local.Port != 10 || l.Loop.Remote.Port != 6 { + if ld.Loop.Local.Port != 10 || ld.Loop.Remote.Port != 6 { errCh <- errors.New("invalid payload") return } diff --git a/pkg/routing/loop.go b/pkg/routing/loop.go index 4e7248280a..308f3f1ff1 100644 --- a/pkg/routing/loop.go +++ b/pkg/routing/loop.go @@ -27,7 +27,7 @@ type LoopDescriptor struct { } // Initiator returns initiator of the Loop. -func (l *LoopDescriptor) Initiator() cipher.PubKey { +func (l LoopDescriptor) Initiator() cipher.PubKey { if len(l.Forward) == 0 { panic("empty forward route") } @@ -36,7 +36,7 @@ func (l *LoopDescriptor) Initiator() cipher.PubKey { } // Responder returns responder of the Loop. -func (l *LoopDescriptor) Responder() cipher.PubKey { +func (l LoopDescriptor) Responder() cipher.PubKey { if len(l.Reverse) == 0 { panic("empty reverse route") } @@ -44,7 +44,7 @@ func (l *LoopDescriptor) Responder() cipher.PubKey { return l.Reverse[0].From } -func (l *LoopDescriptor) String() string { +func (l LoopDescriptor) String() string { return fmt.Sprintf("lport: %d. rport: %d. routes: %s/%s. expire at %s", l.Loop.Local.Port, l.Loop.Remote.Port, l.Forward, l.Reverse, l.Expiry) } diff --git a/pkg/setup/node.go b/pkg/setup/node.go index 4f87b83e77..9b49d02ab9 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -102,34 +102,34 @@ func (sn *Node) Serve(ctx context.Context) error { return sn.tm.Serve(ctx) } -func (sn *Node) createLoop(l *routing.LoopDescriptor) error { - sn.Logger.Infof("Creating new Loop %s", l) - rRouteID, err := sn.createRoute(l.Expiry, l.Reverse, l.Loop.Local.Port, l.Loop.Remote.Port) +func (sn *Node) createLoop(ld routing.LoopDescriptor) error { + sn.Logger.Infof("Creating new Loop %s", ld) + rRouteID, err := sn.createRoute(ld.Expiry, ld.Reverse, ld.Loop.Local.Port, ld.Loop.Remote.Port) if err != nil { return err } - fRouteID, err := sn.createRoute(l.Expiry, l.Forward, l.Loop.Remote.Port, l.Loop.Local.Port) + fRouteID, err := sn.createRoute(ld.Expiry, ld.Forward, ld.Loop.Remote.Port, ld.Loop.Local.Port) if err != nil { return err } - if len(l.Forward) == 0 || len(l.Reverse) == 0 { + if len(ld.Forward) == 0 || len(ld.Reverse) == 0 { return nil } - initiator := l.Initiator() - responder := l.Responder() + initiator := ld.Initiator() + responder := ld.Responder() ldR := &routing.LoopData{ Loop: routing.Loop{ Remote: routing.Addr{ PubKey: initiator, - Port: l.Loop.Local.Port, + Port: ld.Loop.Local.Port, }, Local: routing.Addr{ PubKey: responder, - Port: l.Loop.Remote.Port, + Port: ld.Loop.Remote.Port, }, }, RouteID: rRouteID, @@ -143,11 +143,11 @@ func (sn *Node) createLoop(l *routing.LoopDescriptor) error { Loop: routing.Loop{ Remote: routing.Addr{ PubKey: responder, - Port: l.Loop.Remote.Port, + Port: ld.Loop.Remote.Port, }, Local: routing.Addr{ PubKey: initiator, - Port: l.Loop.Local.Port, + Port: ld.Loop.Local.Port, }, }, RouteID: fRouteID, @@ -161,7 +161,7 @@ func (sn *Node) createLoop(l *routing.LoopDescriptor) error { return fmt.Errorf("loop connect: %s", err) } - sn.Logger.Infof("Created Loop %s", l) + sn.Logger.Infof("Created Loop %s", ld) return nil } @@ -222,9 +222,9 @@ func (sn *Node) serveTransport(tr transport.Transport) error { startTime := time.Now() switch sp { case PacketCreateLoop: - var loopDp routing.LoopDescriptor - if err = json.Unmarshal(data, &loopDp); err == nil { - err = sn.createLoop(&loopDp) + var ld routing.LoopDescriptor + if err = json.Unmarshal(data, &ld); err == nil { + err = sn.createLoop(ld) } case PacketCloseLoop: var ld routing.LoopData diff --git a/pkg/setup/node_test.go b/pkg/setup/node_test.go index a4b973231e..1531a87598 100644 --- a/pkg/setup/node_test.go +++ b/pkg/setup/node_test.go @@ -99,7 +99,7 @@ func TestCreateLoop(t *testing.T) { lPK, _ := cipher.GenerateKeyPair() rPK, _ := cipher.GenerateKeyPair() - l := &routing.LoopDescriptor{Loop: routing.Loop{Local: routing.Addr{PubKey: lPK, Port: 1}, Remote: routing.Addr{PubKey: rPK, Port: 2}}, Expiry: time.Now().Add(time.Hour), + ld := routing.LoopDescriptor{Loop: routing.Loop{Local: routing.Addr{PubKey: lPK, Port: 1}, Remote: routing.Addr{PubKey: rPK, Port: 2}}, Expiry: time.Now().Add(time.Hour), Forward: routing.Route{ &routing.Hop{From: pk1, To: pk2, Transport: tr1.Entry.ID}, &routing.Hop{From: pk2, To: pk3, Transport: tr3.Entry.ID}, @@ -122,7 +122,7 @@ func TestCreateLoop(t *testing.T) { require.NoError(t, err) proto := NewSetupProtocol(tr) - require.NoError(t, CreateLoop(proto, l)) + require.NoError(t, CreateLoop(proto, ld)) rules := n1.getRules() require.Len(t, rules, 2) diff --git a/pkg/setup/protocol.go b/pkg/setup/protocol.go index 94ce60408f..9b69c98b3d 100644 --- a/pkg/setup/protocol.go +++ b/pkg/setup/protocol.go @@ -132,7 +132,7 @@ func DeleteRule(p *Protocol, routeID routing.RouteID) error { } // CreateLoop sends CreateLoop setup request. -func CreateLoop(p *Protocol, ld *routing.LoopDescriptor) error { +func CreateLoop(p *Protocol, ld routing.LoopDescriptor) error { if err := p.WritePacket(PacketCreateLoop, ld); err != nil { return err }