From bc74dbcaefea6636eee0e51a228eac3b64547a75 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Fri, 5 Jul 2019 14:03:00 +0300 Subject: [PATCH] Make `routing.Loop` use `routing.Addr` --- pkg/app/app_test.go | 12 ++++++------ pkg/app/packet_test.go | 2 +- pkg/router/router.go | 2 +- pkg/router/router_test.go | 2 +- pkg/routing/loop.go | 12 ++++++------ pkg/setup/node.go | 8 ++++---- pkg/setup/node_test.go | 4 +++- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 5824279cf..94fa049bc 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -128,7 +128,7 @@ func TestAppWrite(t *testing.T) { appIn, appOut := net.Pipe() app := &App{proto: NewProtocol(in)} go app.handleProto() - go app.serveConn(&LoopAddr{2, routing.Addr{rpk, 3}}, appIn) + go app.serveConn(&LoopAddr{Port: 2, Remote: routing.Addr{PubKey: rpk, Port: 3}}, appIn) proto := NewProtocol(out) dataCh := make(chan []byte) @@ -162,7 +162,7 @@ func TestAppRead(t *testing.T) { pk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[LoopAddr]io.ReadWriteCloser{LoopAddr{2, routing.Addr{pk, 3}}: appIn}} + app := &App{proto: NewProtocol(in), conns: map[LoopAddr]io.ReadWriteCloser{LoopAddr{Port: 2, Remote: routing.Addr{PubKey: pk, Port: 3}}: appIn}} go app.handleProto() proto := NewProtocol(out) @@ -170,7 +170,7 @@ func TestAppRead(t *testing.T) { errCh := make(chan error) go func() { - errCh <- proto.Send(FrameSend, &Packet{&LoopAddr{2, routing.Addr{pk, 3}}, []byte("foo")}, nil) + errCh <- proto.Send(FrameSend, &Packet{&LoopAddr{Port: 2, Remote: routing.Addr{PubKey: pk, Port: 3}}, []byte("foo")}, nil) }() buf := make([]byte, 3) @@ -220,7 +220,7 @@ func TestAppCloseConn(t *testing.T) { pk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[LoopAddr]io.ReadWriteCloser{LoopAddr{2, routing.Addr{pk, 3}}: appIn}} + app := &App{proto: NewProtocol(in), conns: map[LoopAddr]io.ReadWriteCloser{LoopAddr{Port: 2, Remote: routing.Addr{PubKey: pk, Port: 3}}: appIn}} go app.handleProto() proto := NewProtocol(out) @@ -228,7 +228,7 @@ func TestAppCloseConn(t *testing.T) { errCh := make(chan error) go func() { - errCh <- proto.Send(FrameClose, &LoopAddr{2, routing.Addr{pk, 3}}, nil) + errCh <- proto.Send(FrameClose, &LoopAddr{Port: 2, Remote: routing.Addr{PubKey: pk, Port: 3}}, nil) }() _, err := appOut.Read(make([]byte, 3)) @@ -240,7 +240,7 @@ func TestAppClose(t *testing.T) { pk, _ := cipher.GenerateKeyPair() in, out := net.Pipe() appIn, appOut := net.Pipe() - app := &App{proto: NewProtocol(in), conns: map[LoopAddr]io.ReadWriteCloser{LoopAddr{2, routing.Addr{pk, 3}}: appIn}, doneChan: make(chan struct{})} + app := &App{proto: NewProtocol(in), conns: map[LoopAddr]io.ReadWriteCloser{LoopAddr{Port: 2, Remote: routing.Addr{PubKey: pk, Port: 3}}: appIn}, doneChan: make(chan struct{})} go app.handleProto() proto := NewProtocol(out) diff --git a/pkg/app/packet_test.go b/pkg/app/packet_test.go index 527c56095..1dabe98b0 100644 --- a/pkg/app/packet_test.go +++ b/pkg/app/packet_test.go @@ -10,7 +10,7 @@ import ( func ExamplePacket() { pk := cipher.PubKey{} - addr := routing.Addr{pk, 0} + addr := routing.Addr{PubKey: pk, Port: 0} loopAddr := LoopAddr{0, addr} fmt.Println(addr.Network()) diff --git a/pkg/router/router.go b/pkg/router/router.go index bd4f6e489..5bb0c4789 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -286,7 +286,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr *routing.Addr) (*routi return nil, fmt.Errorf("route finder: %s", err) } - l := &routing.Loop{LocalPort: laddr.Port, RemotePort: raddr.Port, + l := &routing.Loop{Local: *laddr, Remote: *raddr, Expiry: time.Now().Add(RouteTTL), Forward: forwardRoute, Reverse: reverseRoute} diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 767d02dd0..da18912cb 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -477,7 +477,7 @@ func TestRouterSetupLoop(t *testing.T) { return } - if l.LocalPort != 10 || l.RemotePort != 6 { + if l.Local.Port != 10 || l.Remote.Port != 6 { errCh <- errors.New("invalid payload") return } diff --git a/pkg/routing/loop.go b/pkg/routing/loop.go index 7fe922d4b..bcf016015 100644 --- a/pkg/routing/loop.go +++ b/pkg/routing/loop.go @@ -9,11 +9,11 @@ import ( // Loop defines a loop over a pair of routes. type Loop struct { - LocalPort uint16 - RemotePort uint16 - Forward Route - Reverse Route - Expiry time.Time + Local Addr + Remote Addr + Forward Route + Reverse Route + Expiry time.Time } // Initiator returns initiator of the Loop. @@ -36,5 +36,5 @@ func (l *Loop) Responder() cipher.PubKey { func (l *Loop) String() string { return fmt.Sprintf("lport: %d. rport: %d. routes: %s/%s. expire at %s", - l.LocalPort, l.RemotePort, l.Forward, l.Reverse, l.Expiry) + l.Local.Port, l.Remote.Port, l.Forward, l.Reverse, l.Expiry) } diff --git a/pkg/setup/node.go b/pkg/setup/node.go index dcf001787..9a62a2a10 100644 --- a/pkg/setup/node.go +++ b/pkg/setup/node.go @@ -104,12 +104,12 @@ func (sn *Node) Serve(ctx context.Context) error { func (sn *Node) createLoop(l *routing.Loop) error { sn.Logger.Infof("Creating new Loop %s", l) - rRouteID, err := sn.createRoute(l.Expiry, l.Reverse, l.LocalPort, l.RemotePort) + rRouteID, err := sn.createRoute(l.Expiry, l.Reverse, l.Local.Port, l.Remote.Port) if err != nil { return err } - fRouteID, err := sn.createRoute(l.Expiry, l.Forward, l.RemotePort, l.LocalPort) + fRouteID, err := sn.createRoute(l.Expiry, l.Forward, l.Remote.Port, l.Local.Port) if err != nil { return err } @@ -121,13 +121,13 @@ func (sn *Node) createLoop(l *routing.Loop) error { initiator := l.Initiator() responder := l.Responder() - ldR := &LoopData{RemotePK: initiator, RemotePort: l.LocalPort, LocalPort: l.RemotePort, RouteID: rRouteID} + ldR := &LoopData{RemotePK: initiator, RemotePort: l.Local.Port, LocalPort: l.Remote.Port, RouteID: rRouteID} if err := sn.connectLoop(responder, ldR); err != nil { sn.Logger.Warnf("Failed to confirm loop with responder: %s", err) return fmt.Errorf("loop connect: %s", err) } - ldI := &LoopData{RemotePK: responder, RemotePort: l.RemotePort, LocalPort: l.LocalPort, RouteID: fRouteID} + ldI := &LoopData{RemotePK: responder, RemotePort: l.Remote.Port, LocalPort: l.Local.Port, RouteID: fRouteID} if err := sn.connectLoop(initiator, ldI); err != nil { sn.Logger.Warnf("Failed to confirm loop with initiator: %s", err) if err := sn.closeLoop(responder, ldR); err != nil { diff --git a/pkg/setup/node_test.go b/pkg/setup/node_test.go index 0713fb7da..aa87f4748 100644 --- a/pkg/setup/node_test.go +++ b/pkg/setup/node_test.go @@ -97,7 +97,9 @@ func TestCreateLoop(t *testing.T) { tr3, err := m3.CreateTransport(context.TODO(), pk2, "mock2", true) require.NoError(t, err) - l := &routing.Loop{LocalPort: 1, RemotePort: 2, Expiry: time.Now().Add(time.Hour), + lPK, _ := cipher.GenerateKeyPair() + rPK, _ := cipher.GenerateKeyPair() + l := &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},