Skip to content

Commit

Permalink
Remove noise handshake from routing establishement
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jul 3, 2019
1 parent 91283de commit 92df154
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 81 deletions.
36 changes: 14 additions & 22 deletions pkg/router/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

type setupCallbacks struct {
ConfirmLoop func(addr *app.LoopAddr, rule routing.Rule, noiseMsg []byte) (noiseRes []byte, err error)
ConfirmLoop func(addr *app.LoopAddr, rule routing.Rule) (err error)
LoopClosed func(addr *app.LoopAddr) error
}

Expand Down Expand Up @@ -89,15 +89,14 @@ func (rm *routeManager) Serve(rw io.ReadWriter) error {
case setup.PacketDeleteRules:
respBody, err = rm.deleteRoutingRules(body)
case setup.PacketConfirmLoop:
respBody, err = rm.confirmLoop(body)
err = rm.confirmLoop(body)
case setup.PacketLoopClosed:
err = rm.loopClosed(body)
default:
err = errors.New("unknown foundation packet")
}

if err != nil {

rm.Logger.Infof("Setup request with type %s failed: %s", t, err)
return proto.WritePacket(setup.RespFailure, err.Error())
}
Expand Down Expand Up @@ -141,17 +140,17 @@ func (rm *routeManager) deleteRoutingRules(data []byte) ([]routing.RouteID, erro
return ruleIDs, nil
}

func (rm *routeManager) confirmLoop(data []byte) (noiseRes []byte, err error) {
func (rm *routeManager) confirmLoop(data []byte) error {
ld := setup.LoopData{}
if err = json.Unmarshal(data, &ld); err != nil {
return
if err := json.Unmarshal(data, &ld); err != nil {
return err
}

raddr := &app.Addr{PubKey: ld.RemotePK, Port: ld.RemotePort}

var appRouteID routing.RouteID
var appRule routing.Rule
err = rm.rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool {
err := rm.rt.RangeRules(func(routeID routing.RouteID, rule routing.Rule) bool {
if rule.Type() != routing.RuleApp || rule.RemotePK() != ld.RemotePK ||
rule.RemotePort() != ld.RemotePort || rule.LocalPort() != ld.LocalPort {
return true
Expand All @@ -163,41 +162,34 @@ func (rm *routeManager) confirmLoop(data []byte) (noiseRes []byte, err error) {
return false
})
if err != nil {
err = fmt.Errorf("routing table: %s", err)
return
return fmt.Errorf("routing table: %s", err)
}

if appRule == nil {
err = errors.New("unknown loop")
return
return errors.New("unknown loop")
}

rule, err := rm.rt.Rule(ld.RouteID)
if err != nil {
err = fmt.Errorf("routing table: %s", err)
return
return fmt.Errorf("routing table: %s", err)
}

if rule.Type() != routing.RuleForward {
err = errors.New("reverse rule is not forward")
return
return errors.New("reverse rule is not forward")
}

msg, err := rm.callbacks.ConfirmLoop(&app.LoopAddr{Port: ld.LocalPort, Remote: *raddr}, rule, ld.NoiseMessage)
if err != nil {
err = fmt.Errorf("confirm: %s", err)
return
if err = rm.callbacks.ConfirmLoop(&app.LoopAddr{Port: ld.LocalPort, Remote: *raddr}, rule); err != nil {
return fmt.Errorf("confirm: %s", err)
}

rm.Logger.Infof("Setting reverse route ID %d for rule with ID %d", ld.RouteID, appRouteID)
appRule.SetRouteID(ld.RouteID)
if rErr := rm.rt.SetRule(appRouteID, appRule); rErr != nil {
err = fmt.Errorf("routing table: %s", rErr)
return
return fmt.Errorf("routing table: %s", rErr)
}

rm.Logger.Infof("Confirmed loop with %s:%d", ld.RemotePK, ld.RemotePort)
return msg, nil
return nil
}

func (rm *routeManager) loopClosed(data []byte) error {
Expand Down
7 changes: 2 additions & 5 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,11 @@ func TestRouteManagerConfirmLoop(t *testing.T) {
rt := manageRoutingTable(routing.InMemoryRoutingTable())
var inAddr *app.LoopAddr
var inRule routing.Rule
var noiseMsg []byte
callbacks := &setupCallbacks{
ConfirmLoop: func(addr *app.LoopAddr, rule routing.Rule, nMsg []byte) (noiseRes []byte, err error) {
ConfirmLoop: func(addr *app.LoopAddr, rule routing.Rule) (err error) {
inAddr = addr
inRule = rule
noiseMsg = nMsg
return []byte("foo"), nil
return nil
},
}
rm := &routeManager{logging.MustGetLogger("routesetup"), rt, callbacks}
Expand Down Expand Up @@ -158,7 +156,6 @@ func TestRouteManagerConfirmLoop(t *testing.T) {
noiseRes, err := setup.ConfirmLoop(proto, ld)
require.NoError(t, err)
assert.Equal(t, []byte("foo"), noiseRes)
assert.Equal(t, []byte("bar"), noiseMsg)
assert.Equal(t, rule, inRule)
assert.Equal(t, uint16(2), inAddr.Port)
assert.Equal(t, uint16(3), inAddr.Remote.Port)
Expand Down
55 changes: 13 additions & 42 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,6 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr *app.Addr) (*app.Addr,
return nil, fmt.Errorf("noise: %s", err)
}

msg, err := ni.HandshakeMessage()
if err != nil {
return nil, fmt.Errorf("noise handshake: %s", err)
}

lport := r.pm.Alloc(appConn)
if err := r.pm.SetLoop(lport, raddr, &loop{noise: ni}); err != nil {
return nil, err
Expand All @@ -309,7 +304,7 @@ func (r *Router) requestLoop(appConn *app.Protocol, raddr *app.Addr) (*app.Addr,
}

l := &routing.Loop{LocalPort: laddr.Port, RemotePort: raddr.Port,
NoiseMessage: msg, Expiry: time.Now().Add(RouteTTL),
Expiry: time.Now().Add(RouteTTL),
Forward: forwardRoute, Reverse: reverseRoute}

proto, tr, err := r.setupProto(context.Background())
Expand Down Expand Up @@ -340,27 +335,33 @@ func (r *Router) confirmLocalLoop(laddr, raddr *app.Addr) error {
return nil
}

func (r *Router) confirmLoop(addr *app.LoopAddr, rule routing.Rule, noiseMsg []byte) ([]byte, error) {
func (r *Router) confirmLoop(addr *app.LoopAddr, rule routing.Rule) error {
b, err := r.pm.Get(addr.Port)
if err != nil {
return nil, err
return err
}

ni, msg, err := r.advanceNoiseHandshake(addr, noiseMsg)
nConf := noise.Config{
LocalSK: r.config.SecKey,
LocalPK: r.config.PubKey,
RemotePK: addr.Remote.PubKey,
Initiator: false,
}
ni, err := noise.KKAndSecp256k1(nConf)
if err != nil {
return nil, fmt.Errorf("noise handshake: %s", err)
return err
}

if err := r.pm.SetLoop(addr.Port, &addr.Remote, &loop{rule.TransportID(), rule.RouteID(), ni}); err != nil {
return nil, err
return err
}

addrs := [2]*app.Addr{&app.Addr{PubKey: r.config.PubKey, Port: addr.Port}, &addr.Remote}
if err = b.conn.Send(app.FrameConfirmLoop, addrs, nil); err != nil {
r.Logger.Warnf("Failed to notify App about new loop: %s", err)
}

return msg, nil
return nil
}

func (r *Router) closeLoop(appConn *app.Protocol, addr *app.LoopAddr) error {
Expand Down Expand Up @@ -451,36 +452,6 @@ fetchRoutesAgain:
return fwdRoutes[0], revRoutes[0], nil
}

func (r *Router) advanceNoiseHandshake(addr *app.LoopAddr, noiseMsg []byte) (ni *noise.Noise, noiseRes []byte, err error) {
var l *loop
l, _ = r.pm.GetLoop(addr.Port, &addr.Remote) // nolint: errcheck

if l != nil && l.routeID != 0 {
err = errors.New("loop already exist")
return
}

if l != nil && l.noise != nil {
return l.noise, nil, l.noise.ProcessMessage(noiseMsg)
}

nConf := noise.Config{
LocalSK: r.config.SecKey,
LocalPK: r.config.PubKey,
RemotePK: addr.Remote.PubKey,
Initiator: false,
}
ni, err = noise.KKAndSecp256k1(nConf)
if err != nil {
return
}
if err = ni.ProcessMessage(noiseMsg); err != nil {
return
}
noiseRes, err = ni.HandshakeMessage()
return
}

// IsSetupTransport checks whether `tr` is running in the `setup` mode.
func (r *Router) IsSetupTransport(tr *transport.ManagedTransport) bool {
for _, pk := range r.config.SetupNodes {
Expand Down
11 changes: 5 additions & 6 deletions pkg/routing/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +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
NoiseMessage []byte
LocalPort uint16
RemotePort uint16
Forward Route
Reverse Route
Expiry time.Time
}

// Initiator returns initiator of the Loop.
Expand Down
11 changes: 5 additions & 6 deletions pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +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, NoiseMessage: l.NoiseMessage}
noiseRes, err := sn.connectLoop(responder, ldR)
if err != nil {
ldR := &LoopData{RemotePK: initiator, RemotePort: l.LocalPort, LocalPort: l.RemotePort, 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, NoiseMessage: noiseRes}
ldI := &LoopData{RemotePK: responder, RemotePort: l.RemotePort, LocalPort: l.LocalPort, 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 Expand Up @@ -222,7 +221,7 @@ func (sn *Node) serveTransport(tr transport.Transport) error {
return proto.WritePacket(RespSuccess, nil)
}

func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) (noiseRes []byte, err error) {
func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) (res []byte, err error) {
tr, err := sn.tm.CreateTransport(context.Background(), on, dmsg.Type, false)
if err != nil {
err = fmt.Errorf("transport: %s", err)
Expand All @@ -231,7 +230,7 @@ func (sn *Node) connectLoop(on cipher.PubKey, ld *LoopData) (noiseRes []byte, er
defer tr.Close()

proto := NewSetupProtocol(tr)
res, err := ConfirmLoop(proto, ld)
res, err = ConfirmLoop(proto, ld)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 92df154

Please sign in to comment.