Skip to content

Commit

Permalink
RegistrationID -> RequestRouteID
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Aug 20, 2019
1 parent 34079bc commit 7b2196d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
8 changes: 4 additions & 4 deletions pkg/router/route_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func (rm *routeManager) handleSetupConn(conn net.Conn) error {
err = rm.confirmLoop(body)
case setup.PacketLoopClosed:
err = rm.loopClosed(body)
case setup.PacketRequestRegistrationID:
respBody, err = rm.occupyRegistrationID()
case setup.PacketRequestRouteID:
respBody, err = rm.occupyRouteID()
default:
err = errors.New("unknown foundation packet")
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func (rm *routeManager) setRoutingRules(data []byte) error {
}

for _, rule := range rules {
routeID := rule.RegistrationID()
routeID := rule.RequestRouteID()
if err := rm.rt.SetRule(routeID, rule); err != nil {
return fmt.Errorf("routing table: %s", err)
}
Expand Down Expand Up @@ -307,7 +307,7 @@ func (rm *routeManager) loopClosed(data []byte) error {
return rm.conf.OnLoopClosed(ld.Loop)
}

func (rm *routeManager) occupyRegistrationID() ([]routing.RouteID, error) {
func (rm *routeManager) occupyRouteID() ([]routing.RouteID, error) {
routeID, err := rm.rt.AddRule(nil)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestNewRouteManager(t *testing.T) {
}()

// Emulate SetupNode sending RequestRegistrationID request.
id, err := setup.RequestRegistrationID(context.TODO(), setup.NewSetupProtocol(requestIDIn))
id, err := setup.RequestRouteID(context.TODO(), setup.NewSetupProtocol(requestIDIn))
require.NoError(t, err)

// Emulate SetupNode sending AddRule request.
Expand Down
24 changes: 12 additions & 12 deletions pkg/routing/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ func (r Rule) LocalPort() Port {
return Port(binary.BigEndian.Uint16(r[48:]))
}

// RegistrationID returns route ID which will be used to register this rule within
// RequestRouteID returns route ID which will be used to register this rule within
// the visor node.
func (r Rule) RegistrationID() RouteID {
func (r Rule) RequestRouteID() RouteID {
return RouteID(binary.BigEndian.Uint32(r[50:]))
}

// SetRegistrationID sets the route ID which will be used to register this rule within
// SetRequestRouteID sets the route ID which will be used to register this rule within
// the visor node.
func (r Rule) SetRegistrationID(id RouteID) {
func (r Rule) SetRequestRouteID(id RouteID) {
binary.BigEndian.PutUint32(r[50:], uint32(id))
}

Expand Down Expand Up @@ -142,18 +142,18 @@ type RuleSummary struct {
Type RuleType `json:"rule_type"`
AppFields *RuleAppFields `json:"app_fields,omitempty"`
ForwardFields *RuleForwardFields `json:"forward_fields,omitempty"`
RegistrationID RouteID `json:"registration_id"`
RequestRouteID RouteID `json:"request_route_id"`
}

// ToRule converts RoutingRuleSummary to RoutingRule.
func (rs *RuleSummary) ToRule() (Rule, error) {
if rs.Type == RuleApp && rs.AppFields != nil && rs.ForwardFields == nil {
f := rs.AppFields
return AppRule(rs.ExpireAt, f.RespRID, f.RemotePK, f.RemotePort, f.LocalPort, rs.RegistrationID), nil
return AppRule(rs.ExpireAt, f.RespRID, f.RemotePK, f.RemotePort, f.LocalPort, rs.RequestRouteID), nil
}
if rs.Type == RuleForward && rs.AppFields == nil && rs.ForwardFields != nil {
f := rs.ForwardFields
return ForwardRule(rs.ExpireAt, f.NextRID, f.NextTID, rs.RegistrationID), nil
return ForwardRule(rs.ExpireAt, f.NextRID, f.NextTID, rs.RequestRouteID), nil
}
return nil, errors.New("invalid routing rule summary")
}
Expand All @@ -163,7 +163,7 @@ func (r Rule) Summary() *RuleSummary {
summary := RuleSummary{
ExpireAt: r.Expiry(),
Type: r.Type(),
RegistrationID: r.RegistrationID(),
RequestRouteID: r.RequestRouteID(),
}
if summary.Type == RuleApp {
summary.AppFields = &RuleAppFields{
Expand All @@ -183,7 +183,7 @@ func (r Rule) Summary() *RuleSummary {

// AppRule constructs a new consume RoutingRule.
func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remotePort, localPort Port,
registrationID RouteID) Rule {
requestRouteID RouteID) Rule {
rule := make([]byte, RuleHeaderSize)
if expireAt.Unix() <= time.Now().Unix() {
binary.BigEndian.PutUint64(rule[0:], 0)
Expand All @@ -197,12 +197,12 @@ func AppRule(expireAt time.Time, respRoute RouteID, remotePK cipher.PubKey, remo
rule = append(rule, 0, 0, 0, 0, 0, 0, 0, 0)
binary.BigEndian.PutUint16(rule[46:], uint16(remotePort))
binary.BigEndian.PutUint16(rule[48:], uint16(localPort))
binary.BigEndian.PutUint32(rule[50:], uint32(registrationID))
binary.BigEndian.PutUint32(rule[50:], uint32(requestRouteID))
return Rule(rule)
}

// ForwardRule constructs a new forward RoutingRule.
func ForwardRule(expireAt time.Time, nextRoute RouteID, nextTrID uuid.UUID, registrationID RouteID) Rule {
func ForwardRule(expireAt time.Time, nextRoute RouteID, nextTrID uuid.UUID, requestRouteID RouteID) Rule {
rule := make([]byte, RuleHeaderSize)
if expireAt.Unix() <= time.Now().Unix() {
binary.BigEndian.PutUint64(rule[0:], 0)
Expand All @@ -214,6 +214,6 @@ func ForwardRule(expireAt time.Time, nextRoute RouteID, nextTrID uuid.UUID, regi
binary.BigEndian.PutUint32(rule[9:], uint32(nextRoute))
rule = append(rule, nextTrID[:]...)
rule = append(rule, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
binary.BigEndian.PutUint32(rule[50:], uint32(registrationID))
binary.BigEndian.PutUint32(rule[50:], uint32(requestRouteID))
return Rule(rule)
}
50 changes: 25 additions & 25 deletions pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (sn *Node) createRoute(ctx context.Context, expireAt time.Time, route routi
sn.Logger.Infof("Creating new Route %s", route)

// add the initiating node to the start of the route. We need to loop over all the visor nodes
// along the route to apply rules including the initating one
// along the route to apply rules including the initiating one
r := make(routing.Route, len(route)+1)
r[0] = &routing.Hop{
Transport: route[0].Transport,
Expand All @@ -240,15 +240,15 @@ func (sn *Node) createRoute(ctx context.Context, expireAt time.Time, route routi

// indicate errors occurred during rules setup
rulesSetupErrs := make(chan error, len(r))
// regIDsCh is an array of chans used to pass the requested route IDs around the goroutines.
// reqIDsCh is an array of chans used to pass the requested route IDs around the goroutines.
// We do it in a fan fashion here. We create as many goroutines as there are rules to be applied.
// Goroutine[i] requests visor node for a free route ID. It passes this route ID through a chan to
// a goroutine[i-1]. In turn, goroutine[i-1] waits for a route ID from chan[i].
// Thus, goroutine[len(r)] doesn't get a route ID and uses 0 instead, goroutine[0] doesn't pass
// its route ID to anyone
regIDsCh := make([]chan routing.RouteID, 0, len(r))
reqIDsCh := make([]chan routing.RouteID, 0, len(r))
for range r {
regIDsCh = append(regIDsCh, make(chan routing.RouteID, 2))
reqIDsCh = append(reqIDsCh, make(chan routing.RouteID, 2))
}

// chan to receive the resulting route ID from a goroutine
Expand All @@ -257,27 +257,27 @@ func (sn *Node) createRoute(ctx context.Context, expireAt time.Time, route routi
// context to cancel rule setup in case of errors
ctx, cancel := context.WithCancel(context.Background())
for i := len(r) - 1; i >= 0; i-- {
var regIDChIn, regIDChOut chan routing.RouteID
var reqIDChIn, reqIDChOut chan routing.RouteID
// goroutine[0] doesn't need to pass the route ID from the 1st step to anyone
if i > 0 {
regIDChOut = regIDsCh[i-1]
reqIDChOut = reqIDsCh[i-1]
}
var (
nextTpID uuid.UUID
rule routing.Rule
)
// goroutine[len(r)-1] uses 0 as the route ID from the 1st step
if i != len(r)-1 {
regIDChIn = regIDsCh[i]
reqIDChIn = reqIDsCh[i]
nextTpID = r[i+1].Transport
rule = routing.ForwardRule(expireAt, 0, nextTpID, 0)
} else {
rule = routing.AppRule(expireAt, 0, init, lport, rport, 0)
}

go func(idx int, pk cipher.PubKey, rule routing.Rule, regIDChIn <-chan routing.RouteID,
regIDChOut chan<- routing.RouteID) {
routeID, err := sn.setupRule(ctx, pk, rule, regIDChIn, regIDChOut)
go func(i int, pk cipher.PubKey, rule routing.Rule, reqIDChIn <-chan routing.RouteID,
reqIDChOut chan<- routing.RouteID) {
routeID, err := sn.setupRule(ctx, pk, rule, reqIDChIn, reqIDChOut)
if err != nil {
// filter out context cancellation errors
if err == context.Canceled {
Expand All @@ -290,12 +290,12 @@ func (sn *Node) createRoute(ctx context.Context, expireAt time.Time, route routi
}

// adding rule for initiator must result with a route ID for the overall route
if idx == 0 {
if i == 0 {
resultingRouteIDCh <- routeID
}

rulesSetupErrs <- nil
}(i, r[i].To, rule, regIDChIn, regIDChOut)
}(i, r[i].To, rule, reqIDChIn, reqIDChOut)
}

var rulesSetupErr error
Expand All @@ -312,7 +312,7 @@ func (sn *Node) createRoute(ctx context.Context, expireAt time.Time, route routi

// close chan to avoid leaks
close(rulesSetupErrs)
for _, ch := range regIDsCh {
for _, ch := range reqIDsCh {
close(ch)
}
if rulesSetupErr != nil {
Expand Down Expand Up @@ -368,23 +368,23 @@ func (sn *Node) closeLoop(ctx context.Context, on cipher.PubKey, ld routing.Loop
}

func (sn *Node) setupRule(ctx context.Context, pk cipher.PubKey, rule routing.Rule,
regIDChIn <-chan routing.RouteID, regIDChOut chan<- routing.RouteID) (routing.RouteID, error) {
reqIDChIn <-chan routing.RouteID, reqIDChOut chan<- routing.RouteID) (routing.RouteID, error) {
sn.Logger.Debugf("trying to setup setup rule: %v with %s\n", rule, pk)
registrationID, err := sn.requestRegistrationID(ctx, pk)
requestRouteID, err := sn.requestRouteID(ctx, pk)
if err != nil {
return 0, err
}

if regIDChOut != nil {
regIDChOut <- registrationID
if reqIDChOut != nil {
reqIDChOut <- requestRouteID
}
var nextRouteID routing.RouteID
if regIDChIn != nil {
nextRouteID = <-regIDChIn
if reqIDChIn != nil {
nextRouteID = <-reqIDChIn
rule.SetRouteID(nextRouteID)
}

rule.SetRegistrationID(registrationID)
rule.SetRequestRouteID(requestRouteID)

sn.Logger.Debugf("dialing to %s to setup rule: %v\n", pk, rule)

Expand All @@ -394,24 +394,24 @@ func (sn *Node) setupRule(ctx context.Context, pk cipher.PubKey, rule routing.Ru

sn.Logger.Infof("Set rule of type %s on %s", rule.Type(), pk)

return registrationID, nil
return requestRouteID, nil
}

func (sn *Node) requestRegistrationID(ctx context.Context, pk cipher.PubKey) (routing.RouteID, error) {
func (sn *Node) requestRouteID(ctx context.Context, pk cipher.PubKey) (routing.RouteID, error) {
proto, err := sn.dialAndCreateProto(ctx, pk)
if err != nil {
return 0, err
}
defer sn.closeProto(proto)

registrationID, err := RequestRegistrationID(ctx, proto)
requestRouteID, err := RequestRouteID(ctx, proto)
if err != nil {
return 0, err
}

sn.Logger.Infof("Received route ID %d from %s", registrationID, pk)
sn.Logger.Infof("Received route ID %d from %s", requestRouteID, pk)

return registrationID, nil
return requestRouteID, nil
}

func (sn *Node) addRule(ctx context.Context, pk cipher.PubKey, rule routing.Rule) error {
Expand Down
10 changes: 5 additions & 5 deletions pkg/setup/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ const (
PacketCloseLoop
// PacketLoopClosed represents OnLoopClosed foundation packet.
PacketLoopClosed
// PacketRequestRegistrationID represents RequestRouteID foundation packet.
PacketRequestRegistrationID
// PacketRequestRouteID represents RequestRouteID foundation packet.
PacketRequestRouteID

// RespFailure represents failure response for a foundation packet.
RespFailure = 0xfe
Expand Down Expand Up @@ -113,9 +113,9 @@ func (p *Protocol) Close() error {
return nil
}

// RequestRegistrationID sends RequestRegistrationID request.
func RequestRegistrationID(ctx context.Context, p *Protocol) (routing.RouteID, error) {
if err := p.WritePacket(PacketRequestRegistrationID, nil); err != nil {
// RequestRouteID sends RequestRouteID request.
func RequestRouteID(ctx context.Context, p *Protocol) (routing.RouteID, error) {
if err := p.WritePacket(PacketRequestRouteID, nil); err != nil {
return 0, err
}
var res []routing.RouteID
Expand Down

0 comments on commit 7b2196d

Please sign in to comment.