-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68a9d30
commit 4f0e72b
Showing
3 changed files
with
127 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package routing | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/SkycoinProject/dmsg/cipher" | ||
) | ||
|
||
// RouteDescriptor describes a route (from the perspective of the source and destination edges). | ||
type RouteDescriptor [routeDescriptorSize]byte | ||
|
||
// NewRouteDescriptor returns a new RouteDescriptor. | ||
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) | ||
desc.setDstPort(dstPort) | ||
|
||
return desc | ||
} | ||
|
||
// Src returns source Addr from RouteDescriptor. | ||
func (rd RouteDescriptor) Src() Addr { | ||
return Addr{ | ||
PubKey: rd.SrcPK(), | ||
Port: rd.SrcPort(), | ||
} | ||
} | ||
|
||
// Dst returns destination Addr from RouteDescriptor. | ||
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 { | ||
var pk cipher.PubKey | ||
copy(pk[:], rd[0:pkSize]) | ||
return pk | ||
} | ||
|
||
// setSrcPK sets source public key of a rule. | ||
func (rd *RouteDescriptor) setSrcPK(pk cipher.PubKey) { | ||
copy(rd[:pkSize], pk[:]) | ||
} | ||
|
||
// DstPK returns destination public key from RouteDescriptor. | ||
func (rd RouteDescriptor) DstPK() cipher.PubKey { | ||
var pk cipher.PubKey | ||
copy(pk[:], rd[pkSize:pkSize*2]) | ||
return pk | ||
} | ||
|
||
// setDstPK sets destination public key of a rule. | ||
func (rd *RouteDescriptor) setDstPK(pk cipher.PubKey) { | ||
copy(rd[pkSize:pkSize*2], pk[:]) | ||
} | ||
|
||
// SrcPort returns source port from RouteDescriptor. | ||
func (rd RouteDescriptor) SrcPort() Port { | ||
return Port(binary.BigEndian.Uint16(rd[pkSize*2 : pkSize*2+2])) | ||
} | ||
|
||
// setSrcPort sets source port of a rule. | ||
func (rd *RouteDescriptor) setSrcPort(port Port) { | ||
binary.BigEndian.PutUint16(rd[pkSize*2:pkSize*2+2], uint16(port)) | ||
} | ||
|
||
// DstPort returns destination port from RouteDescriptor. | ||
func (rd RouteDescriptor) DstPort() Port { | ||
return Port(binary.BigEndian.Uint16(rd[pkSize*2+2 : pkSize*2+2*2])) | ||
} | ||
|
||
// setDstPort sets destination port of a rule. | ||
func (rd *RouteDescriptor) setDstPort(port Port) { | ||
binary.BigEndian.PutUint16(rd[pkSize*2+2:pkSize*2+2*2], uint16(port)) | ||
} | ||
|
||
// Invert inverts source and destination. | ||
func (rd RouteDescriptor) Invert() RouteDescriptor { | ||
return NewRouteDescriptor(rd.DstPK(), rd.SrcPK(), rd.DstPort(), rd.SrcPort()) | ||
} | ||
|
||
func (rd RouteDescriptor) String() string { | ||
return fmt.Sprintf("rPK:%s, lPK:%s, rPort:%d, lPort:%d", rd.DstPK(), rd.SrcPK(), rd.DstPort(), rd.SrcPK()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package routing | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/SkycoinProject/dmsg/cipher" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRouteDescriptor(t *testing.T) { | ||
pk1, _ := cipher.GenerateKeyPair() | ||
pk2, _ := cipher.GenerateKeyPair() | ||
port1 := Port(1) | ||
port2 := Port(2) | ||
|
||
rd := NewRouteDescriptor(pk1, pk2, port1, port2) | ||
|
||
require.Len(t, rd, routeDescriptorSize) | ||
require.Equal(t, pk1, rd.SrcPK()) | ||
require.Equal(t, pk2, rd.DstPK()) | ||
require.Equal(t, port1, rd.SrcPort()) | ||
require.Equal(t, port2, rd.DstPort()) | ||
|
||
inverted := rd.Invert() | ||
|
||
require.Len(t, inverted, routeDescriptorSize) | ||
require.Equal(t, pk2, inverted.SrcPK()) | ||
require.Equal(t, pk1, inverted.DstPK()) | ||
require.Equal(t, port2, inverted.SrcPort()) | ||
require.Equal(t, port1, inverted.DstPort()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters