Skip to content

Commit

Permalink
Make routing.Loop use routing.Addr
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 5, 2019
1 parent 0711d12 commit bc74dbc
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
12 changes: 6 additions & 6 deletions pkg/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -162,15 +162,15 @@ 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)
go proto.Serve(nil) // nolint: errcheck

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)
Expand Down Expand Up @@ -220,15 +220,15 @@ 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)
go proto.Serve(nil) // nolint: errcheck

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))
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/routing/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
8 changes: 4 additions & 4 deletions pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion pkg/setup/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit bc74dbc

Please sign in to comment.