5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 17:41:36 +00:00

Export NodeInfoPayload type, rename some API functions

This commit is contained in:
Neil Alexander 2019-07-28 11:30:24 +01:00
parent 377f88512b
commit 24f4754f2b
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
4 changed files with 27 additions and 38 deletions

View File

@ -85,14 +85,15 @@ func (a *AdminSocket) Init(c *yggdrasil.Core, state *config.NodeState, log *log.
*/ */
a.AddHandler("getSelf", []string{}, func(in Info) (Info, error) { a.AddHandler("getSelf", []string{}, func(in Info) (Info, error) {
ip := c.Address().String() ip := c.Address().String()
subnet := c.Subnet()
return Info{ return Info{
"self": Info{ "self": Info{
ip: Info{ ip: Info{
"box_pub_key": c.BoxPubKey(), "box_pub_key": c.EncryptionPublicKey(),
"build_name": yggdrasil.BuildName(), "build_name": yggdrasil.BuildName(),
"build_version": yggdrasil.BuildVersion(), "build_version": yggdrasil.BuildVersion(),
"coords": fmt.Sprintf("%v", c.Coords()), "coords": fmt.Sprintf("%v", c.Coords()),
"subnet": c.Subnet().String(), "subnet": subnet.String(),
}, },
}, },
}, nil }, nil

View File

@ -59,7 +59,7 @@ type DHTRes struct {
} }
// NodeInfoPayload represents a RequestNodeInfo response, in bytes. // NodeInfoPayload represents a RequestNodeInfo response, in bytes.
type NodeInfoPayload nodeinfoPayload type NodeInfoPayload []byte
// SwitchQueues represents information from the switch related to link // SwitchQueues represents information from the switch related to link
// congestion and a list of switch queues created in response to congestion on a // congestion and a list of switch queues created in response to congestion on a
@ -301,12 +301,12 @@ func (c *Core) TreeID() *crypto.TreeID {
} }
// SigPubKey gets the node's signing public key. // SigPubKey gets the node's signing public key.
func (c *Core) SigPubKey() string { func (c *Core) SigningPublicKey() string {
return hex.EncodeToString(c.sigPub[:]) return hex.EncodeToString(c.sigPub[:])
} }
// BoxPubKey gets the node's encryption public key. // BoxPubKey gets the node's encryption public key.
func (c *Core) BoxPubKey() string { func (c *Core) EncryptionPublicKey() string {
return hex.EncodeToString(c.boxPub[:]) return hex.EncodeToString(c.boxPub[:])
} }
@ -318,21 +318,21 @@ func (c *Core) Coords() []byte {
// 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
// address. // address.
func (c *Core) Address() *net.IP { func (c *Core) Address() net.IP {
address := net.IP(address.AddrForNodeID(c.NodeID())[:]) address := net.IP(address.AddrForNodeID(c.NodeID())[:])
return &address return address
} }
// Subnet gets the routed IPv6 subnet of the Yggdrasil node. This is always a // Subnet gets the routed IPv6 subnet of the Yggdrasil node. This is always a
// /64 subnet. // /64 subnet.
func (c *Core) Subnet() *net.IPNet { func (c *Core) Subnet() net.IPNet {
subnet := address.SubnetForNodeID(c.NodeID())[:] subnet := address.SubnetForNodeID(c.NodeID())[:]
subnet = append(subnet, 0, 0, 0, 0, 0, 0, 0, 0) subnet = append(subnet, 0, 0, 0, 0, 0, 0, 0, 0)
return &net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)} return net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)}
} }
// NodeInfo gets the currently configured nodeinfo. // MyNodeInfo gets the currently configured nodeinfo.
func (c *Core) MyNodeInfo() nodeinfoPayload { func (c *Core) MyNodeInfo() NodeInfoPayload {
return c.router.nodeinfo.getNodeInfo() return c.router.nodeinfo.getNodeInfo()
} }
@ -355,7 +355,7 @@ func (c *Core) GetNodeInfo(keyString, coordString string, nocache bool) (NodeInf
} }
if !nocache { if !nocache {
if response, err := c.router.nodeinfo.getCachedNodeInfo(key); err == nil { if response, err := c.router.nodeinfo.getCachedNodeInfo(key); err == nil {
return NodeInfoPayload(response), nil return response, nil
} }
} }
var coords []byte var coords []byte
@ -370,9 +370,9 @@ func (c *Core) GetNodeInfo(keyString, coordString string, nocache bool) (NodeInf
coords = append(coords, uint8(u64)) coords = append(coords, uint8(u64))
} }
} }
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) {
defer func() { recover() }() defer func() { recover() }()
select { select {
case response <- nodeinfo: case response <- nodeinfo:
@ -387,7 +387,7 @@ func (c *Core) GetNodeInfo(keyString, coordString string, nocache bool) (NodeInf
close(response) close(response)
}() }()
for res := range response { for res := range response {
return NodeInfoPayload(*res), nil return *res, nil
} }
return NodeInfoPayload{}, errors.New(fmt.Sprintf("getNodeInfo timeout: %s", keyString)) return NodeInfoPayload{}, errors.New(fmt.Sprintf("getNodeInfo timeout: %s", keyString))
} }
@ -455,16 +455,6 @@ func (c *Core) DisconnectPeer(port uint64) error {
return nil return nil
} }
// RouterAddress returns the raw address as used by the router.
func (c *Core) RouterAddress() address.Address {
return c.router.addr
}
// RouterSubnet returns the raw address as used by the router.
func (c *Core) RouterSubnet() address.Subnet {
return c.router.subnet
}
// GetAllowedEncryptionPublicKeys returns the public keys permitted for incoming // GetAllowedEncryptionPublicKeys returns the public keys permitted for incoming
// peer connections. // peer connections.
func (c *Core) GetAllowedEncryptionPublicKeys() []string { func (c *Core) GetAllowedEncryptionPublicKeys() []string {

View File

@ -13,7 +13,7 @@ import (
type nodeinfo struct { type nodeinfo struct {
core *Core core *Core
myNodeInfo nodeinfoPayload myNodeInfo NodeInfoPayload
myNodeInfoMutex sync.RWMutex myNodeInfoMutex sync.RWMutex
callbacks map[crypto.BoxPubKey]nodeinfoCallback callbacks map[crypto.BoxPubKey]nodeinfoCallback
callbacksMutex sync.Mutex callbacksMutex sync.Mutex
@ -21,15 +21,13 @@ type nodeinfo struct {
cacheMutex sync.RWMutex cacheMutex sync.RWMutex
} }
type nodeinfoPayload []byte
type nodeinfoCached struct { type nodeinfoCached struct {
payload nodeinfoPayload payload NodeInfoPayload
created time.Time created time.Time
} }
type nodeinfoCallback struct { type nodeinfoCallback struct {
call func(nodeinfo *nodeinfoPayload) call func(nodeinfo *NodeInfoPayload)
created time.Time created time.Time
} }
@ -38,7 +36,7 @@ type nodeinfoReqRes struct {
SendPermPub crypto.BoxPubKey // Sender's permanent key SendPermPub crypto.BoxPubKey // Sender's permanent key
SendCoords []byte // Sender's coords SendCoords []byte // Sender's coords
IsResponse bool IsResponse bool
NodeInfo nodeinfoPayload NodeInfo NodeInfoPayload
} }
// Initialises the nodeinfo cache/callback maps, and starts a goroutine to keep // Initialises the nodeinfo cache/callback maps, and starts a goroutine to keep
@ -70,7 +68,7 @@ func (m *nodeinfo) init(core *Core) {
} }
// Add a callback for a nodeinfo lookup // Add a callback for a nodeinfo lookup
func (m *nodeinfo) addCallback(sender crypto.BoxPubKey, call func(nodeinfo *nodeinfoPayload)) { func (m *nodeinfo) addCallback(sender crypto.BoxPubKey, call func(nodeinfo *NodeInfoPayload)) {
m.callbacksMutex.Lock() m.callbacksMutex.Lock()
defer m.callbacksMutex.Unlock() defer m.callbacksMutex.Unlock()
m.callbacks[sender] = nodeinfoCallback{ m.callbacks[sender] = nodeinfoCallback{
@ -80,7 +78,7 @@ func (m *nodeinfo) addCallback(sender crypto.BoxPubKey, call func(nodeinfo *node
} }
// Handles the callback, if there is one // Handles the callback, if there is one
func (m *nodeinfo) callback(sender crypto.BoxPubKey, nodeinfo nodeinfoPayload) { func (m *nodeinfo) callback(sender crypto.BoxPubKey, nodeinfo NodeInfoPayload) {
m.callbacksMutex.Lock() m.callbacksMutex.Lock()
defer m.callbacksMutex.Unlock() defer m.callbacksMutex.Unlock()
if callback, ok := m.callbacks[sender]; ok { if callback, ok := m.callbacks[sender]; ok {
@ -90,7 +88,7 @@ func (m *nodeinfo) callback(sender crypto.BoxPubKey, nodeinfo nodeinfoPayload) {
} }
// Get the current node's nodeinfo // Get the current node's nodeinfo
func (m *nodeinfo) getNodeInfo() nodeinfoPayload { func (m *nodeinfo) getNodeInfo() NodeInfoPayload {
m.myNodeInfoMutex.RLock() m.myNodeInfoMutex.RLock()
defer m.myNodeInfoMutex.RUnlock() defer m.myNodeInfoMutex.RUnlock()
return m.myNodeInfo return m.myNodeInfo
@ -135,7 +133,7 @@ func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) error {
} }
// Add nodeinfo into the cache for a node // Add nodeinfo into the cache for a node
func (m *nodeinfo) addCachedNodeInfo(key crypto.BoxPubKey, payload nodeinfoPayload) { func (m *nodeinfo) addCachedNodeInfo(key crypto.BoxPubKey, payload NodeInfoPayload) {
m.cacheMutex.Lock() m.cacheMutex.Lock()
defer m.cacheMutex.Unlock() defer m.cacheMutex.Unlock()
m.cache[key] = nodeinfoCached{ m.cache[key] = nodeinfoCached{
@ -145,13 +143,13 @@ func (m *nodeinfo) addCachedNodeInfo(key crypto.BoxPubKey, payload nodeinfoPaylo
} }
// Get a nodeinfo entry from the cache // Get a nodeinfo entry from the cache
func (m *nodeinfo) getCachedNodeInfo(key crypto.BoxPubKey) (nodeinfoPayload, error) { func (m *nodeinfo) getCachedNodeInfo(key crypto.BoxPubKey) (NodeInfoPayload, error) {
m.cacheMutex.RLock() m.cacheMutex.RLock()
defer m.cacheMutex.RUnlock() defer m.cacheMutex.RUnlock()
if nodeinfo, ok := m.cache[key]; ok { if nodeinfo, ok := m.cache[key]; ok {
return nodeinfo.payload, nil return nodeinfo.payload, nil
} }
return nodeinfoPayload{}, errors.New("No cache entry found") return NodeInfoPayload{}, errors.New("No cache entry found")
} }
// Handles a nodeinfo request/response - called from the router // Handles a nodeinfo request/response - called from the router

View File

@ -394,7 +394,7 @@ func (p *nodeinfoReqRes) decode(bs []byte) bool {
if len(bs) == 0 { if len(bs) == 0 {
return false return false
} }
p.NodeInfo = make(nodeinfoPayload, len(bs)) p.NodeInfo = make(NodeInfoPayload, len(bs))
if !wire_chop_slice(p.NodeInfo[:], &bs) { if !wire_chop_slice(p.NodeInfo[:], &bs) {
return false return false
} }