Update API to represent coords as []uint64

This commit is contained in:
Neil Alexander 2019-08-05 10:17:19 +01:00
parent 37533f157d
commit 3a2ae9d902
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
6 changed files with 45 additions and 39 deletions

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -282,7 +282,7 @@ func (a *AdminSocket) Init(c *yggdrasil.Core, state *config.NodeState, log *log.
if in["nocache"] != nil { if in["nocache"] != nil {
nocache = in["nocache"].(string) == "true" nocache = in["nocache"].(string) == "true"
} }
var box_pub_key string var boxPubKey crypto.BoxPubKey
var coords []uint64 var coords []uint64
if in["box_pub_key"] == nil && in["coords"] == nil { if in["box_pub_key"] == nil && in["coords"] == nil {
nodeinfo := a.core.MyNodeInfo() nodeinfo := a.core.MyNodeInfo()
@ -295,10 +295,14 @@ func (a *AdminSocket) Init(c *yggdrasil.Core, state *config.NodeState, log *log.
} else if in["box_pub_key"] == nil || in["coords"] == nil { } else if in["box_pub_key"] == nil || in["coords"] == nil {
return Info{}, errors.New("Expecting both box_pub_key and coords") return Info{}, errors.New("Expecting both box_pub_key and coords")
} else { } else {
box_pub_key = in["box_pub_key"].(string) if b, err := hex.DecodeString(in["box_pub_key"].(string)); err == nil {
copy(boxPubKey[:], b[:])
} else {
return Info{}, err
}
coords = util.DecodeCoordString(in["coords"].(string)) coords = util.DecodeCoordString(in["coords"].(string))
} }
result, err := a.core.GetNodeInfo(box_pub_key, coords, nocache) result, err := a.core.GetNodeInfo(boxPubKey, coords, nocache)
if err == nil { if err == nil {
var m map[string]interface{} var m map[string]interface{}
if err = json.Unmarshal(result, &m); err == nil { if err = json.Unmarshal(result, &m); err == nil {

View File

@ -97,7 +97,7 @@ func Difference(a, b []string) []string {
} }
// DecodeCoordString decodes a string representing coordinates in [1 2 3] format // DecodeCoordString decodes a string representing coordinates in [1 2 3] format
// and returns a []byte. // and returns a []uint64.
func DecodeCoordString(in string) (out []uint64) { func DecodeCoordString(in string) (out []uint64) {
s := strings.Trim(in, "[]") s := strings.Trim(in, "[]")
t := strings.Split(s, " ") t := strings.Split(s, " ")

View File

@ -32,7 +32,7 @@ type Peer struct {
// to a given node. // to a given node.
type SwitchPeer struct { type SwitchPeer struct {
PublicKey crypto.BoxPubKey PublicKey crypto.BoxPubKey
Coords []byte Coords []uint64
BytesSent uint64 BytesSent uint64
BytesRecvd uint64 BytesRecvd uint64
Port uint64 Port uint64
@ -83,7 +83,7 @@ type SwitchQueue struct {
// Session represents an open session with another node. // Session represents an open session with another node.
type Session struct { type Session struct {
PublicKey crypto.BoxPubKey PublicKey crypto.BoxPubKey
Coords []byte Coords []uint64
BytesSent uint64 BytesSent uint64
BytesRecvd uint64 BytesRecvd uint64
MTU uint16 MTU uint16
@ -136,7 +136,7 @@ func (c *Core) GetSwitchPeers() []SwitchPeer {
} }
coords := elem.locator.getCoords() coords := elem.locator.getCoords()
info := SwitchPeer{ info := SwitchPeer{
Coords: append([]byte{}, coords...), Coords: append([]uint64{}, wire_coordsBytestoUint64s(coords)...),
BytesSent: atomic.LoadUint64(&peer.bytesSent), BytesSent: atomic.LoadUint64(&peer.bytesSent),
BytesRecvd: atomic.LoadUint64(&peer.bytesRecvd), BytesRecvd: atomic.LoadUint64(&peer.bytesRecvd),
Port: uint64(elem.port), Port: uint64(elem.port),
@ -164,7 +164,7 @@ func (c *Core) GetDHT() []DHTEntry {
}) })
for _, v := range dhtentry { for _, v := range dhtentry {
info := DHTEntry{ info := DHTEntry{
Coords: append([]uint64{}, coordsBytestoUint64s(v.coords)...), Coords: append([]uint64{}, wire_coordsBytestoUint64s(v.coords)...),
LastSeen: now.Sub(v.recv), LastSeen: now.Sub(v.recv),
} }
copy(info.PublicKey[:], v.key[:]) copy(info.PublicKey[:], v.key[:])
@ -212,7 +212,7 @@ func (c *Core) GetSessions() []Session {
var session Session var session Session
workerFunc := func() { workerFunc := func() {
session = Session{ session = Session{
Coords: append([]byte{}, sinfo.coords...), Coords: append([]uint64{}, wire_coordsBytestoUint64s(sinfo.coords)...),
MTU: sinfo.getMTU(), MTU: sinfo.getMTU(),
BytesSent: sinfo.bytesSent, BytesSent: sinfo.bytesSent,
BytesRecvd: sinfo.bytesRecvd, BytesRecvd: sinfo.bytesRecvd,
@ -309,9 +309,9 @@ func (c *Core) EncryptionPublicKey() string {
} }
// Coords returns the current coordinates of the node. // Coords returns the current coordinates of the node.
func (c *Core) Coords() []byte { func (c *Core) Coords() []uint64 {
table := c.switchTable.table.Load().(lookupTable) table := c.switchTable.table.Load().(lookupTable)
return table.self.getCoords() return wire_coordsBytestoUint64s(table.self.getCoords())
} }
// Address gets the IPv6 address of the Yggdrasil node. This is always a /128 // Address gets the IPv6 address of the Yggdrasil node. This is always a /128
@ -334,8 +334,8 @@ func (c *Core) MyNodeInfo() NodeInfoPayload {
return c.router.nodeinfo.getNodeInfo() return c.router.nodeinfo.getNodeInfo()
} }
// SetNodeInfo the lcal nodeinfo. Note that nodeinfo can be any value or struct, // SetNodeInfo sets the local nodeinfo. Note that nodeinfo can be any value or
// it will be serialised into JSON automatically. // struct, it will be serialised into JSON automatically.
func (c *Core) SetNodeInfo(nodeinfo interface{}, nodeinfoprivacy bool) { func (c *Core) SetNodeInfo(nodeinfo interface{}, nodeinfoprivacy bool) {
c.router.nodeinfo.setNodeInfo(nodeinfo, nodeinfoprivacy) c.router.nodeinfo.setNodeInfo(nodeinfo, nodeinfoprivacy)
} }
@ -344,7 +344,7 @@ func (c *Core) SetNodeInfo(nodeinfo interface{}, nodeinfoprivacy bool) {
// key and coordinates specified. The third parameter specifies whether a cached // key and coordinates specified. The third parameter specifies whether a cached
// result is acceptable - this results in less traffic being generated than is // result is acceptable - this results in less traffic being generated than is
// necessary when, e.g. crawling the network. // necessary when, e.g. crawling the network.
func (c *Core) GetNodeInfo(key crypto.BoxPubKey, coords []byte, nocache bool) (NodeInfoPayload, error) { func (c *Core) GetNodeInfo(key crypto.BoxPubKey, coords []uint64, nocache bool) (NodeInfoPayload, error) {
response := make(chan *NodeInfoPayload, 1) response := make(chan *NodeInfoPayload, 1)
sendNodeInfoRequest := func() { sendNodeInfoRequest := func() {
c.router.nodeinfo.addCallback(key, func(nodeinfo *NodeInfoPayload) { c.router.nodeinfo.addCallback(key, func(nodeinfo *NodeInfoPayload) {
@ -354,7 +354,7 @@ func (c *Core) GetNodeInfo(key crypto.BoxPubKey, coords []byte, nocache bool) (N
default: default:
} }
}) })
c.router.nodeinfo.sendNodeInfo(key, coords, false) c.router.nodeinfo.sendNodeInfo(key, wire_coordsUint64stoBytes(coords), false)
} }
c.router.doAdmin(sendNodeInfoRequest) c.router.doAdmin(sendNodeInfoRequest)
go func() { go func() {
@ -456,7 +456,7 @@ func (c *Core) DHTPing(key crypto.BoxPubKey, coords []uint64, target *crypto.Nod
resCh := make(chan *dhtRes, 1) resCh := make(chan *dhtRes, 1)
info := dhtInfo{ info := dhtInfo{
key: key, key: key,
coords: coordsUint64stoBytes(coords), coords: wire_coordsUint64stoBytes(coords),
} }
if target == nil { if target == nil {
target = info.getNodeID() target = info.getNodeID()
@ -473,12 +473,12 @@ func (c *Core) DHTPing(key crypto.BoxPubKey, coords []uint64, target *crypto.Nod
res := <-resCh res := <-resCh
if res != nil { if res != nil {
r := DHTRes{ r := DHTRes{
Coords: append([]uint64{}, coordsBytestoUint64s(res.Coords)...), Coords: append([]uint64{}, wire_coordsBytestoUint64s(res.Coords)...),
} }
copy(r.PublicKey[:], res.Key[:]) copy(r.PublicKey[:], res.Key[:])
for _, i := range res.Infos { for _, i := range res.Infos {
e := DHTEntry{ e := DHTEntry{
Coords: append([]uint64{}, coordsBytestoUint64s(i.coords)...), Coords: append([]uint64{}, wire_coordsBytestoUint64s(i.coords)...),
} }
copy(e.PublicKey[:], i.key[:]) copy(e.PublicKey[:], i.key[:])
r.Infos = append(r.Infos, e) r.Infos = append(r.Infos, e)

View File

@ -137,27 +137,6 @@ type peerInfo struct {
// This is just a uint64 with a named type for clarity reasons. // This is just a uint64 with a named type for clarity reasons.
type switchPort uint64 type switchPort uint64
func coordsUint64stoBytes(in []uint64) (out []byte) {
for _, coord := range in {
c := wire_encode_uint64(coord)
out = append(out, c...)
}
return out
}
func coordsBytestoUint64s(in []byte) (out []uint64) {
offset := 0
for {
coord, length := wire_decode_uint64(in[offset:])
if length == 0 {
break
}
out = append(out, coord)
offset += length
}
return out
}
// This is the subset of the information about a peer needed to make routing decisions, and it stored separately in an atomically accessed table, which gets hammered in the "hot loop" of the routing logic (see: peer.handleTraffic in peers.go). // This is the subset of the information about a peer needed to make routing decisions, and it stored separately in an atomically accessed table, which gets hammered in the "hot loop" of the routing logic (see: peer.handleTraffic in peers.go).
type tableElem struct { type tableElem struct {
port switchPort port switchPort

View File

@ -115,6 +115,29 @@ func wire_decode_coords(packet []byte) ([]byte, int) {
return packet[coordBegin:coordEnd], coordEnd return packet[coordBegin:coordEnd], coordEnd
} }
// Converts a []uint64 set of coords to a []byte set of coords.
func wire_coordsUint64stoBytes(in []uint64) (out []byte) {
for _, coord := range in {
c := wire_encode_uint64(coord)
out = append(out, c...)
}
return out
}
// Converts a []byte set of coords to a []uint64 set of coords.
func wire_coordsBytestoUint64s(in []byte) (out []uint64) {
offset := 0
for {
coord, length := wire_decode_uint64(in[offset:])
if length == 0 {
break
}
out = append(out, coord)
offset += length
}
return out
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Encodes a swtichMsg into its wire format. // Encodes a swtichMsg into its wire format.