mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-12-23 04:05:39 +00:00
Export NodeInfoPayload type, rename some API functions
This commit is contained in:
parent
377f88512b
commit
24f4754f2b
@ -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) {
|
||||
ip := c.Address().String()
|
||||
subnet := c.Subnet()
|
||||
return Info{
|
||||
"self": Info{
|
||||
ip: Info{
|
||||
"box_pub_key": c.BoxPubKey(),
|
||||
"box_pub_key": c.EncryptionPublicKey(),
|
||||
"build_name": yggdrasil.BuildName(),
|
||||
"build_version": yggdrasil.BuildVersion(),
|
||||
"coords": fmt.Sprintf("%v", c.Coords()),
|
||||
"subnet": c.Subnet().String(),
|
||||
"subnet": subnet.String(),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
|
@ -59,7 +59,7 @@ type DHTRes struct {
|
||||
}
|
||||
|
||||
// NodeInfoPayload represents a RequestNodeInfo response, in bytes.
|
||||
type NodeInfoPayload nodeinfoPayload
|
||||
type NodeInfoPayload []byte
|
||||
|
||||
// SwitchQueues represents information from the switch related to link
|
||||
// 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.
|
||||
func (c *Core) SigPubKey() string {
|
||||
func (c *Core) SigningPublicKey() string {
|
||||
return hex.EncodeToString(c.sigPub[:])
|
||||
}
|
||||
|
||||
// BoxPubKey gets the node's encryption public key.
|
||||
func (c *Core) BoxPubKey() string {
|
||||
func (c *Core) EncryptionPublicKey() string {
|
||||
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.
|
||||
func (c *Core) Address() *net.IP {
|
||||
func (c *Core) Address() net.IP {
|
||||
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
|
||||
// /64 subnet.
|
||||
func (c *Core) Subnet() *net.IPNet {
|
||||
func (c *Core) Subnet() net.IPNet {
|
||||
subnet := address.SubnetForNodeID(c.NodeID())[:]
|
||||
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.
|
||||
func (c *Core) MyNodeInfo() nodeinfoPayload {
|
||||
// MyNodeInfo gets the currently configured nodeinfo.
|
||||
func (c *Core) MyNodeInfo() NodeInfoPayload {
|
||||
return c.router.nodeinfo.getNodeInfo()
|
||||
}
|
||||
|
||||
@ -355,7 +355,7 @@ func (c *Core) GetNodeInfo(keyString, coordString string, nocache bool) (NodeInf
|
||||
}
|
||||
if !nocache {
|
||||
if response, err := c.router.nodeinfo.getCachedNodeInfo(key); err == nil {
|
||||
return NodeInfoPayload(response), nil
|
||||
return response, nil
|
||||
}
|
||||
}
|
||||
var coords []byte
|
||||
@ -370,9 +370,9 @@ func (c *Core) GetNodeInfo(keyString, coordString string, nocache bool) (NodeInf
|
||||
coords = append(coords, uint8(u64))
|
||||
}
|
||||
}
|
||||
response := make(chan *nodeinfoPayload, 1)
|
||||
response := make(chan *NodeInfoPayload, 1)
|
||||
sendNodeInfoRequest := func() {
|
||||
c.router.nodeinfo.addCallback(key, func(nodeinfo *nodeinfoPayload) {
|
||||
c.router.nodeinfo.addCallback(key, func(nodeinfo *NodeInfoPayload) {
|
||||
defer func() { recover() }()
|
||||
select {
|
||||
case response <- nodeinfo:
|
||||
@ -387,7 +387,7 @@ func (c *Core) GetNodeInfo(keyString, coordString string, nocache bool) (NodeInf
|
||||
close(response)
|
||||
}()
|
||||
for res := range response {
|
||||
return NodeInfoPayload(*res), nil
|
||||
return *res, nil
|
||||
}
|
||||
return NodeInfoPayload{}, errors.New(fmt.Sprintf("getNodeInfo timeout: %s", keyString))
|
||||
}
|
||||
@ -455,16 +455,6 @@ func (c *Core) DisconnectPeer(port uint64) error {
|
||||
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
|
||||
// peer connections.
|
||||
func (c *Core) GetAllowedEncryptionPublicKeys() []string {
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
|
||||
type nodeinfo struct {
|
||||
core *Core
|
||||
myNodeInfo nodeinfoPayload
|
||||
myNodeInfo NodeInfoPayload
|
||||
myNodeInfoMutex sync.RWMutex
|
||||
callbacks map[crypto.BoxPubKey]nodeinfoCallback
|
||||
callbacksMutex sync.Mutex
|
||||
@ -21,15 +21,13 @@ type nodeinfo struct {
|
||||
cacheMutex sync.RWMutex
|
||||
}
|
||||
|
||||
type nodeinfoPayload []byte
|
||||
|
||||
type nodeinfoCached struct {
|
||||
payload nodeinfoPayload
|
||||
payload NodeInfoPayload
|
||||
created time.Time
|
||||
}
|
||||
|
||||
type nodeinfoCallback struct {
|
||||
call func(nodeinfo *nodeinfoPayload)
|
||||
call func(nodeinfo *NodeInfoPayload)
|
||||
created time.Time
|
||||
}
|
||||
|
||||
@ -38,7 +36,7 @@ type nodeinfoReqRes struct {
|
||||
SendPermPub crypto.BoxPubKey // Sender's permanent key
|
||||
SendCoords []byte // Sender's coords
|
||||
IsResponse bool
|
||||
NodeInfo nodeinfoPayload
|
||||
NodeInfo NodeInfoPayload
|
||||
}
|
||||
|
||||
// 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
|
||||
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()
|
||||
defer m.callbacksMutex.Unlock()
|
||||
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
|
||||
func (m *nodeinfo) callback(sender crypto.BoxPubKey, nodeinfo nodeinfoPayload) {
|
||||
func (m *nodeinfo) callback(sender crypto.BoxPubKey, nodeinfo NodeInfoPayload) {
|
||||
m.callbacksMutex.Lock()
|
||||
defer m.callbacksMutex.Unlock()
|
||||
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
|
||||
func (m *nodeinfo) getNodeInfo() nodeinfoPayload {
|
||||
func (m *nodeinfo) getNodeInfo() NodeInfoPayload {
|
||||
m.myNodeInfoMutex.RLock()
|
||||
defer m.myNodeInfoMutex.RUnlock()
|
||||
return m.myNodeInfo
|
||||
@ -135,7 +133,7 @@ func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) error {
|
||||
}
|
||||
|
||||
// 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()
|
||||
defer m.cacheMutex.Unlock()
|
||||
m.cache[key] = nodeinfoCached{
|
||||
@ -145,13 +143,13 @@ func (m *nodeinfo) addCachedNodeInfo(key crypto.BoxPubKey, payload nodeinfoPaylo
|
||||
}
|
||||
|
||||
// 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()
|
||||
defer m.cacheMutex.RUnlock()
|
||||
if nodeinfo, ok := m.cache[key]; ok {
|
||||
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
|
||||
|
@ -394,7 +394,7 @@ func (p *nodeinfoReqRes) decode(bs []byte) bool {
|
||||
if len(bs) == 0 {
|
||||
return false
|
||||
}
|
||||
p.NodeInfo = make(nodeinfoPayload, len(bs))
|
||||
p.NodeInfo = make(NodeInfoPayload, len(bs))
|
||||
if !wire_chop_slice(p.NodeInfo[:], &bs) {
|
||||
return false
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user