Skip to content

Commit

Permalink
Ensure non-pointer usage of routing.LoopDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 17, 2019
1 parent 81ebea2 commit 08cacff
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/routing/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -36,15 +36,15 @@ 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")
}

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)
}
Expand Down
30 changes: 15 additions & 15 deletions pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/setup/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/setup/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 08cacff

Please sign in to comment.