Skip to content

Commit

Permalink
Implement pointer receivers for RouteDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Nov 13, 2019
1 parent 4f0e72b commit f404627
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
20 changes: 8 additions & 12 deletions pkg/routing/route_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ type RouteDescriptor [routeDescriptorSize]byte
func NewRouteDescriptor(srcPK, dstPK cipher.PubKey, srcPort, dstPort Port) RouteDescriptor {
var desc RouteDescriptor

// p := copy(desc[:pkSize], srcPK[:])
// p = copy(desc[p:p+pkSize], dstPK[:])
// binary.BigEndian.PutUint16(desc[p:p+2], uint16(srcPort))
// binary.BigEndian.PutUint16(desc[p+2:p+2*2], uint16(dstPort))
desc.setSrcPK(srcPK)
desc.setDstPK(dstPK)
desc.setSrcPort(srcPort)
Expand All @@ -27,23 +23,23 @@ func NewRouteDescriptor(srcPK, dstPK cipher.PubKey, srcPort, dstPort Port) Route
}

// Src returns source Addr from RouteDescriptor.
func (rd RouteDescriptor) Src() Addr {
func (rd *RouteDescriptor) Src() Addr {
return Addr{
PubKey: rd.SrcPK(),
Port: rd.SrcPort(),
}
}

// Dst returns destination Addr from RouteDescriptor.
func (rd RouteDescriptor) Dst() Addr {
func (rd *RouteDescriptor) Dst() Addr {
return Addr{
PubKey: rd.DstPK(),
Port: rd.DstPort(),
}
}

// SrcPK returns source public key from RouteDescriptor.
func (rd RouteDescriptor) SrcPK() cipher.PubKey {
func (rd *RouteDescriptor) SrcPK() cipher.PubKey {
var pk cipher.PubKey
copy(pk[:], rd[0:pkSize])
return pk
Expand All @@ -55,7 +51,7 @@ func (rd *RouteDescriptor) setSrcPK(pk cipher.PubKey) {
}

// DstPK returns destination public key from RouteDescriptor.
func (rd RouteDescriptor) DstPK() cipher.PubKey {
func (rd *RouteDescriptor) DstPK() cipher.PubKey {
var pk cipher.PubKey
copy(pk[:], rd[pkSize:pkSize*2])
return pk
Expand All @@ -67,7 +63,7 @@ func (rd *RouteDescriptor) setDstPK(pk cipher.PubKey) {
}

// SrcPort returns source port from RouteDescriptor.
func (rd RouteDescriptor) SrcPort() Port {
func (rd *RouteDescriptor) SrcPort() Port {
return Port(binary.BigEndian.Uint16(rd[pkSize*2 : pkSize*2+2]))
}

Expand All @@ -77,7 +73,7 @@ func (rd *RouteDescriptor) setSrcPort(port Port) {
}

// DstPort returns destination port from RouteDescriptor.
func (rd RouteDescriptor) DstPort() Port {
func (rd *RouteDescriptor) DstPort() Port {
return Port(binary.BigEndian.Uint16(rd[pkSize*2+2 : pkSize*2+2*2]))
}

Expand All @@ -87,10 +83,10 @@ func (rd *RouteDescriptor) setDstPort(port Port) {
}

// Invert inverts source and destination.
func (rd RouteDescriptor) Invert() RouteDescriptor {
func (rd *RouteDescriptor) Invert() RouteDescriptor {
return NewRouteDescriptor(rd.DstPK(), rd.SrcPK(), rd.DstPort(), rd.SrcPort())
}

func (rd RouteDescriptor) String() string {
func (rd *RouteDescriptor) String() string {
return fmt.Sprintf("rPK:%s, lPK:%s, rPort:%d, lPort:%d", rd.DstPK(), rd.SrcPK(), rd.DstPort(), rd.SrcPK())
}
17 changes: 9 additions & 8 deletions pkg/routing/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,23 +325,24 @@ func (r Rule) Summary() *RuleSummary {
Type: r.Type(),
KeyRouteID: r.KeyRouteID(),
}
rd := r.RouteDescriptor()
switch t := summary.Type; t {
case RuleConsume:
summary.ConsumeFields = &RuleConsumeFields{
RouteDescriptor: RouteDescriptorFields{
DstPK: r.RouteDescriptor().DstPK(),
SrcPK: r.RouteDescriptor().SrcPK(),
DstPort: r.RouteDescriptor().DstPort(),
SrcPort: r.RouteDescriptor().SrcPort(),
DstPK: rd.DstPK(),
SrcPK: rd.SrcPK(),
DstPort: rd.DstPort(),
SrcPort: rd.SrcPort(),
},
}
case RuleForward:
summary.ForwardFields = &RuleForwardFields{
RouteDescriptor: RouteDescriptorFields{
DstPK: r.RouteDescriptor().DstPK(),
SrcPK: r.RouteDescriptor().SrcPK(),
DstPort: r.RouteDescriptor().DstPort(),
SrcPort: r.RouteDescriptor().SrcPort(),
DstPK: rd.DstPK(),
SrcPK: rd.SrcPK(),
DstPort: rd.DstPort(),
SrcPort: rd.SrcPort(),
},
NextRID: r.NextRouteID(),
NextTID: r.NextTransportID(),
Expand Down
16 changes: 10 additions & 6 deletions pkg/routing/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ func TestConsumeRule(t *testing.T) {
assert.Equal(t, keepAlive, rule.KeepAlive())
assert.Equal(t, RuleConsume, rule.Type())
assert.Equal(t, RouteID(1), rule.KeyRouteID())
assert.Equal(t, pk, rule.RouteDescriptor().DstPK())
assert.Equal(t, Port(3), rule.RouteDescriptor().DstPort())
assert.Equal(t, Port(2), rule.RouteDescriptor().SrcPort())

rd := rule.RouteDescriptor()
assert.Equal(t, pk, rd.DstPK())
assert.Equal(t, Port(3), rd.DstPort())
assert.Equal(t, Port(2), rd.SrcPort())

rule.SetKeyRouteID(4)
assert.Equal(t, RouteID(4), rule.KeyRouteID())
Expand All @@ -38,9 +40,11 @@ func TestForwardRule(t *testing.T) {
assert.Equal(t, RouteID(1), rule.KeyRouteID())
assert.Equal(t, RouteID(2), rule.NextRouteID())
assert.Equal(t, trID, rule.NextTransportID())
assert.Equal(t, pk, rule.RouteDescriptor().DstPK())
assert.Equal(t, Port(4), rule.RouteDescriptor().DstPort())
assert.Equal(t, Port(3), rule.RouteDescriptor().SrcPort())

rd := rule.RouteDescriptor()
assert.Equal(t, pk, rd.DstPK())
assert.Equal(t, Port(4), rd.DstPort())
assert.Equal(t, Port(3), rd.SrcPort())

rule.SetKeyRouteID(5)
assert.Equal(t, RouteID(5), rule.KeyRouteID())
Expand Down

0 comments on commit f404627

Please sign in to comment.