2019-08-20 14:48:51 +03:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-09-03 04:58:17 +03:00
|
|
|
"container/list"
|
2019-08-20 23:12:21 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-08-20 23:15:02 +03:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-08-20 14:48:51 +03:00
|
|
|
"github.com/micro/go-micro/client"
|
2019-08-21 21:16:18 +03:00
|
|
|
rtr "github.com/micro/go-micro/client/selector/router"
|
2019-08-28 22:11:19 +03:00
|
|
|
pbNet "github.com/micro/go-micro/network/proto"
|
2019-08-20 23:12:21 +03:00
|
|
|
"github.com/micro/go-micro/proxy"
|
|
|
|
"github.com/micro/go-micro/router"
|
2019-08-28 22:11:19 +03:00
|
|
|
pbRtr "github.com/micro/go-micro/router/proto"
|
2019-08-20 14:48:51 +03:00
|
|
|
"github.com/micro/go-micro/server"
|
2019-08-20 23:12:21 +03:00
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
"github.com/micro/go-micro/tunnel"
|
2019-08-30 02:04:46 +03:00
|
|
|
tun "github.com/micro/go-micro/tunnel/transport"
|
2019-08-20 23:12:21 +03:00
|
|
|
"github.com/micro/go-micro/util/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-28 22:11:19 +03:00
|
|
|
// NetworkChannel is the name of the tunnel channel for passing network messages
|
|
|
|
NetworkChannel = "network"
|
|
|
|
// ControlChannel is the name of the tunnel channel for passing control message
|
2019-08-21 21:16:18 +03:00
|
|
|
ControlChannel = "control"
|
2019-08-27 13:36:46 +03:00
|
|
|
// DefaultLink is default network link
|
|
|
|
DefaultLink = "network"
|
2019-08-20 14:48:51 +03:00
|
|
|
)
|
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// node is network node
|
|
|
|
type node struct {
|
2019-09-03 18:39:27 +03:00
|
|
|
sync.RWMutex
|
2019-08-29 01:11:26 +03:00
|
|
|
// id is node id
|
|
|
|
id string
|
|
|
|
// address is node address
|
|
|
|
address string
|
2019-09-02 14:39:26 +03:00
|
|
|
// neighbours maps the node neighbourhood
|
2019-08-29 01:11:26 +03:00
|
|
|
neighbours map[string]*node
|
2019-09-03 12:00:14 +03:00
|
|
|
// network returns the node network
|
2019-09-02 14:39:26 +03:00
|
|
|
network Network
|
2019-09-03 12:00:14 +03:00
|
|
|
// lastSeen stores the time the node has been seen last time
|
|
|
|
lastSeen time.Time
|
2019-09-02 14:39:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Id is node ide
|
|
|
|
func (n *node) Id() string {
|
|
|
|
return n.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns node address
|
|
|
|
func (n *node) Address() string {
|
|
|
|
return n.address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns node network
|
|
|
|
func (n *node) Network() Network {
|
|
|
|
return n.network
|
2019-08-28 22:11:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-02 19:06:21 +03:00
|
|
|
// Neighbourhood returns node neighbourhood
|
|
|
|
func (n *node) Neighbourhood() []Node {
|
|
|
|
var nodes []Node
|
2019-09-03 18:39:27 +03:00
|
|
|
n.RLock()
|
|
|
|
for _, neighbourNode := range n.neighbours {
|
|
|
|
// make a copy of the node
|
|
|
|
n := &node{
|
|
|
|
id: neighbourNode.id,
|
|
|
|
address: neighbourNode.address,
|
|
|
|
network: neighbourNode.network,
|
|
|
|
}
|
|
|
|
// NOTE: we do not care about neighbour's neighbours
|
|
|
|
nodes = append(nodes, n)
|
2019-09-02 19:06:21 +03:00
|
|
|
}
|
2019-09-03 18:39:27 +03:00
|
|
|
n.RUnlock()
|
2019-09-02 19:06:21 +03:00
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
// network implements Network interface
|
|
|
|
type network struct {
|
2019-09-02 14:39:26 +03:00
|
|
|
// node is network node
|
|
|
|
*node
|
2019-08-20 14:48:51 +03:00
|
|
|
// options configure the network
|
|
|
|
options Options
|
2019-08-20 23:12:21 +03:00
|
|
|
// rtr is network router
|
|
|
|
router.Router
|
|
|
|
// prx is network proxy
|
|
|
|
proxy.Proxy
|
|
|
|
// tun is network tunnel
|
|
|
|
tunnel.Tunnel
|
2019-08-28 01:08:35 +03:00
|
|
|
// server is network server
|
|
|
|
server server.Server
|
2019-08-20 23:12:21 +03:00
|
|
|
// client is network client
|
|
|
|
client client.Client
|
|
|
|
|
2019-09-02 13:42:45 +03:00
|
|
|
// tunClient is a map of tunnel clients keyed over tunnel channel names
|
2019-08-29 01:11:26 +03:00
|
|
|
tunClient map[string]transport.Client
|
2019-08-28 22:11:19 +03:00
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
sync.RWMutex
|
|
|
|
// connected marks the network as connected
|
|
|
|
connected bool
|
|
|
|
// closed closes the network
|
|
|
|
closed chan bool
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newNetwork returns a new network node
|
|
|
|
func newNetwork(opts ...Option) Network {
|
|
|
|
options := DefaultOptions()
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// init tunnel address to the network bind address
|
|
|
|
options.Tunnel.Init(
|
|
|
|
tunnel.Address(options.Address),
|
2019-08-29 16:53:30 +03:00
|
|
|
tunnel.Nodes(options.Nodes...),
|
2019-08-20 23:12:21 +03:00
|
|
|
)
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// init router Id to the network id
|
|
|
|
options.Router.Init(
|
|
|
|
router.Id(options.Id),
|
|
|
|
)
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// create tunnel client with tunnel transport
|
2019-08-30 02:04:46 +03:00
|
|
|
tunTransport := tun.NewTransport(
|
|
|
|
tun.WithTunnel(options.Tunnel),
|
2019-08-20 23:12:21 +03:00
|
|
|
)
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// server is network server
|
|
|
|
server := server.NewServer(
|
|
|
|
server.Id(options.Id),
|
2019-08-27 15:21:36 +03:00
|
|
|
server.Address(options.Address),
|
2019-08-21 21:22:50 +03:00
|
|
|
server.Name(options.Name),
|
2019-08-20 23:12:21 +03:00
|
|
|
server.Transport(tunTransport),
|
|
|
|
)
|
|
|
|
|
|
|
|
// client is network client
|
|
|
|
client := client.NewClient(
|
|
|
|
client.Transport(tunTransport),
|
2019-08-21 21:16:18 +03:00
|
|
|
client.Selector(
|
|
|
|
rtr.NewSelector(
|
|
|
|
rtr.WithRouter(options.Router),
|
|
|
|
),
|
|
|
|
),
|
2019-08-20 23:12:21 +03:00
|
|
|
)
|
|
|
|
|
2019-09-02 14:39:26 +03:00
|
|
|
network := &network{
|
|
|
|
node: &node{
|
|
|
|
id: options.Id,
|
|
|
|
address: options.Address,
|
|
|
|
neighbours: make(map[string]*node),
|
|
|
|
},
|
|
|
|
options: options,
|
|
|
|
Router: options.Router,
|
|
|
|
Proxy: options.Proxy,
|
|
|
|
Tunnel: options.Tunnel,
|
|
|
|
server: server,
|
|
|
|
client: client,
|
|
|
|
tunClient: make(map[string]transport.Client),
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
2019-09-02 14:39:26 +03:00
|
|
|
|
|
|
|
network.node.network = network
|
|
|
|
|
|
|
|
return network
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
|
2019-09-02 13:42:45 +03:00
|
|
|
// Options returns network options
|
|
|
|
func (n *network) Options() Options {
|
|
|
|
n.Lock()
|
|
|
|
options := n.options
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
// Name returns network name
|
|
|
|
func (n *network) Name() string {
|
|
|
|
return n.options.Name
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// Address returns network bind address
|
|
|
|
func (n *network) Address() string {
|
2019-08-21 21:16:18 +03:00
|
|
|
return n.Tunnel.Address()
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// resolveNodes resolves network nodes to addresses
|
2019-08-20 23:12:21 +03:00
|
|
|
func (n *network) resolveNodes() ([]string, error) {
|
|
|
|
// resolve the network address to network nodes
|
|
|
|
records, err := n.options.Resolver.Resolve(n.options.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:53:30 +03:00
|
|
|
nodeMap := make(map[string]bool)
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// collect network node addresses
|
2019-08-29 16:53:30 +03:00
|
|
|
var nodes []string
|
2019-08-29 17:09:01 +03:00
|
|
|
for _, record := range records {
|
|
|
|
nodes = append(nodes, record.Address)
|
2019-08-29 16:53:30 +03:00
|
|
|
nodeMap[record.Address] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// append seed nodes if we have them
|
|
|
|
for _, node := range n.options.Nodes {
|
|
|
|
if _, ok := nodeMap[node]; !ok {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// resolve continuously resolves network nodes and initializes network tunnel with resolved addresses
|
2019-08-20 23:12:21 +03:00
|
|
|
func (n *network) resolve() {
|
|
|
|
resolve := time.NewTicker(ResolveTime)
|
|
|
|
defer resolve.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
case <-resolve.C:
|
|
|
|
nodes, err := n.resolveNodes()
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// initialize the tunnel
|
|
|
|
n.Tunnel.Init(
|
|
|
|
tunnel.Nodes(nodes...),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// handleNetConn handles network announcement messages
|
2019-08-30 22:05:00 +03:00
|
|
|
func (n *network) handleNetConn(sess tunnel.Session, msg chan *transport.Message) {
|
2019-08-20 23:12:21 +03:00
|
|
|
for {
|
|
|
|
m := new(transport.Message)
|
2019-08-30 22:05:00 +03:00
|
|
|
if err := sess.Recv(m); err != nil {
|
2019-08-20 23:12:21 +03:00
|
|
|
// TODO: should we bail here?
|
2019-08-28 22:11:19 +03:00
|
|
|
log.Debugf("Network tunnel [%s] receive error: %v", NetworkChannel, err)
|
2019-08-20 23:12:21 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-22 15:17:32 +03:00
|
|
|
select {
|
|
|
|
case msg <- m:
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 23:12:21 +03:00
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// acceptNetConn accepts connections from NetworkChannel
|
|
|
|
func (n *network) acceptNetConn(l tunnel.Listener, recv chan *transport.Message) {
|
|
|
|
for {
|
|
|
|
// accept a connection
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: handle this
|
|
|
|
log.Debugf("Network tunnel [%s] accept error: %v", NetworkChannel, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// go handle NetworkChannel connection
|
|
|
|
go n.handleNetConn(conn, recv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processNetChan processes messages received on NetworkChannel
|
|
|
|
func (n *network) processNetChan(l tunnel.Listener) {
|
|
|
|
// receive network message queue
|
2019-08-22 15:17:32 +03:00
|
|
|
recv := make(chan *transport.Message, 128)
|
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// accept NetworkChannel connections
|
|
|
|
go n.acceptNetConn(l, recv)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-recv:
|
|
|
|
// switch on type of message and take action
|
|
|
|
switch m.Header["Micro-Method"] {
|
|
|
|
case "connect":
|
|
|
|
pbNetConnect := &pbNet.Connect{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbNetConnect); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] connect unmarshal error: %v", NetworkChannel, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// don't process your own messages
|
|
|
|
if pbNetConnect.Node.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-03 18:39:27 +03:00
|
|
|
n.Lock()
|
|
|
|
// if the entry already exists skip adding it
|
|
|
|
if _, ok := n.neighbours[pbNetConnect.Node.Id]; ok {
|
|
|
|
n.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// add a new neighbour;
|
|
|
|
// NOTE: new node does not have any neighbours
|
|
|
|
n.neighbours[pbNetConnect.Node.Id] = &node{
|
2019-08-29 01:11:26 +03:00
|
|
|
id: pbNetConnect.Node.Id,
|
|
|
|
address: pbNetConnect.Node.Address,
|
|
|
|
neighbours: make(map[string]*node),
|
2019-08-28 22:11:19 +03:00
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
case "neighbour":
|
|
|
|
pbNetNeighbour := &pbNet.Neighbour{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbNetNeighbour); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] neighbour unmarshal error: %v", NetworkChannel, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// don't process your own messages
|
|
|
|
if pbNetNeighbour.Node.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n.Lock()
|
2019-09-03 18:39:27 +03:00
|
|
|
// only add the neighbour if it's not already in the neighbourhood
|
|
|
|
if _, ok := n.neighbours[pbNetNeighbour.Node.Id]; !ok {
|
|
|
|
neighbour := &node{
|
|
|
|
id: pbNetNeighbour.Node.Id,
|
|
|
|
address: pbNetNeighbour.Node.Address,
|
|
|
|
neighbours: make(map[string]*node),
|
|
|
|
lastSeen: time.Now(),
|
|
|
|
}
|
|
|
|
n.neighbours[pbNetNeighbour.Node.Id] = neighbour
|
|
|
|
}
|
|
|
|
// update/store the neighbour node neighbours
|
2019-08-28 22:11:19 +03:00
|
|
|
for _, pbNeighbour := range pbNetNeighbour.Neighbours {
|
2019-08-29 01:11:26 +03:00
|
|
|
neighbourNode := &node{
|
|
|
|
id: pbNeighbour.Id,
|
|
|
|
address: pbNeighbour.Address,
|
2019-08-28 22:11:19 +03:00
|
|
|
}
|
2019-09-03 18:39:27 +03:00
|
|
|
n.neighbours[pbNetNeighbour.Node.Id].neighbours[neighbourNode.id] = neighbourNode
|
2019-08-28 22:11:19 +03:00
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
case "close":
|
|
|
|
pbNetClose := &pbNet.Close{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbNetClose); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] close unmarshal error: %v", NetworkChannel, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// don't process your own messages
|
|
|
|
if pbNetClose.Node.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n.Lock()
|
2019-09-03 12:00:14 +03:00
|
|
|
if err := n.pruneNode(pbNetClose.Node.Id); err != nil {
|
|
|
|
log.Debugf("Network failed to prune the node %s: %v", pbNetClose.Node.Id, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-28 22:11:19 +03:00
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
2019-08-22 15:17:32 +03:00
|
|
|
}
|
2019-08-28 22:11:19 +03:00
|
|
|
}
|
2019-08-22 15:17:32 +03:00
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// announce announces node neighbourhood to the network
|
|
|
|
func (n *network) announce(client transport.Client) {
|
|
|
|
announce := time.NewTicker(AnnounceTime)
|
|
|
|
defer announce.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
case <-announce.C:
|
|
|
|
n.RLock()
|
|
|
|
nodes := make([]*pbNet.Node, len(n.neighbours))
|
|
|
|
i := 0
|
2019-08-29 01:11:26 +03:00
|
|
|
for id, _ := range n.neighbours {
|
2019-08-29 17:42:07 +03:00
|
|
|
nodes[i] = &pbNet.Node{
|
2019-08-29 01:11:26 +03:00
|
|
|
Id: id,
|
|
|
|
Address: n.neighbours[id].address,
|
2019-08-28 22:11:19 +03:00
|
|
|
}
|
2019-08-29 17:42:07 +03:00
|
|
|
i++
|
2019-08-28 22:11:19 +03:00
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
|
|
|
node := &pbNet.Node{
|
|
|
|
Id: n.options.Id,
|
|
|
|
Address: n.options.Address,
|
|
|
|
}
|
|
|
|
pbNetNeighbour := &pbNet.Neighbour{
|
|
|
|
Node: node,
|
|
|
|
Neighbours: nodes,
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := proto.Marshal(pbNetNeighbour)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: should we bail here?
|
|
|
|
log.Debugf("Network failed to marshal neighbour message: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// create transport message and chuck it down the pipe
|
|
|
|
m := transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Micro-Method": "neighbour",
|
|
|
|
},
|
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Send(&m); err != nil {
|
|
|
|
log.Debugf("Network failed to send neighbour messsage: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 12:00:14 +03:00
|
|
|
// pruneNode removes a node with given id from the list of neighbours. It also removes all routes originted by this node.
|
|
|
|
// NOTE: this method is not thread-safe; when calling it make sure you lock the particular code segment
|
|
|
|
func (n *network) pruneNode(id string) error {
|
|
|
|
delete(n.neighbours, id)
|
|
|
|
// lookup all the routes originated at this node
|
|
|
|
q := router.NewQuery(
|
|
|
|
router.QueryRouter(id),
|
|
|
|
)
|
|
|
|
routes, err := n.Router.Table().Query(q)
|
|
|
|
if err != nil && err != router.ErrRouteNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// delete the found routes
|
|
|
|
for _, route := range routes {
|
|
|
|
if err := n.Router.Table().Delete(route); err != nil && err != router.ErrRouteNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prune the nodes that have not been seen for certain period of time defined by PruneTime
|
|
|
|
// Additionally, prune also removes all the routes originated by these nodes
|
|
|
|
func (n *network) prune() {
|
|
|
|
prune := time.NewTicker(PruneTime)
|
|
|
|
defer prune.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
case <-prune.C:
|
|
|
|
n.Lock()
|
|
|
|
for id, node := range n.neighbours {
|
|
|
|
nodeAge := time.Since(node.lastSeen)
|
|
|
|
if nodeAge > PruneTime {
|
|
|
|
log.Debugf("Network deleting node %s: reached prune time threshold", id)
|
|
|
|
if err := n.pruneNode(id); err != nil {
|
|
|
|
log.Debugf("Network failed to prune the node %s: %v", id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// handleCtrlConn handles ControlChannel connections
|
2019-08-30 22:05:00 +03:00
|
|
|
func (n *network) handleCtrlConn(sess tunnel.Session, msg chan *transport.Message) {
|
2019-08-28 22:11:19 +03:00
|
|
|
for {
|
|
|
|
m := new(transport.Message)
|
2019-08-30 22:05:00 +03:00
|
|
|
if err := sess.Recv(m); err != nil {
|
2019-08-28 22:11:19 +03:00
|
|
|
// TODO: should we bail here?
|
|
|
|
log.Debugf("Network tunnel advert receive error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case msg <- m:
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// acceptCtrlConn accepts connections from ControlChannel
|
|
|
|
func (n *network) acceptCtrlConn(l tunnel.Listener, recv chan *transport.Message) {
|
|
|
|
for {
|
|
|
|
// accept a connection
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: handle this
|
|
|
|
log.Debugf("Network tunnel [%s] accept error: %v", ControlChannel, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// go handle ControlChannel connection
|
|
|
|
go n.handleCtrlConn(conn, recv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 13:45:47 +03:00
|
|
|
// setRouteMetric calculates metric of the route and updates it in place
|
|
|
|
// - Local route metric is 1
|
|
|
|
// - Routes with ID of adjacent neighbour are 10
|
|
|
|
// - Routes of neighbours of the advertiser are 100
|
|
|
|
// - Routes beyond your neighbourhood are 1000
|
|
|
|
func (n *network) setRouteMetric(route *router.Route) {
|
|
|
|
// we are the origin of the route
|
|
|
|
if route.Router == n.options.Id {
|
|
|
|
route.Metric = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n.RLock()
|
|
|
|
// check if the route origin is our neighbour
|
|
|
|
if _, ok := n.neighbours[route.Router]; ok {
|
|
|
|
route.Metric = 10
|
|
|
|
n.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the route origin is the neighbour of our neighbour
|
|
|
|
for _, node := range n.neighbours {
|
|
|
|
for id, _ := range node.neighbours {
|
|
|
|
if route.Router == id {
|
|
|
|
route.Metric = 100
|
|
|
|
n.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
|
|
|
// the origin of the route is beyond our neighbourhood
|
|
|
|
route.Metric = 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
// processCtrlChan processes messages received on ControlChannel
|
2019-08-28 22:11:19 +03:00
|
|
|
func (n *network) processCtrlChan(l tunnel.Listener) {
|
|
|
|
// receive control message queue
|
|
|
|
recv := make(chan *transport.Message, 128)
|
|
|
|
|
|
|
|
// accept ControlChannel cconnections
|
|
|
|
go n.acceptCtrlConn(l, recv)
|
2019-08-22 15:17:32 +03:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-recv:
|
|
|
|
// switch on type of message and take action
|
|
|
|
switch m.Header["Micro-Method"] {
|
|
|
|
case "advert":
|
2019-08-28 22:11:19 +03:00
|
|
|
pbRtrAdvert := &pbRtr.Advert{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbRtrAdvert); err != nil {
|
2019-08-30 02:04:46 +03:00
|
|
|
log.Debugf("Network fail to unmarshal advert message: %v", err)
|
2019-08-22 15:17:32 +03:00
|
|
|
continue
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
2019-08-22 15:17:32 +03:00
|
|
|
|
2019-08-30 02:04:46 +03:00
|
|
|
// loookup advertising node in our neighbourhood
|
|
|
|
n.RLock()
|
|
|
|
advertNode, ok := n.neighbours[pbRtrAdvert.Id]
|
|
|
|
if !ok {
|
|
|
|
// advertising node has not been registered as our neighbour, yet
|
|
|
|
// let's add it to the map of our neighbours
|
|
|
|
advertNode = &node{
|
|
|
|
id: pbRtrAdvert.Id,
|
|
|
|
neighbours: make(map[string]*node),
|
|
|
|
}
|
|
|
|
n.neighbours[pbRtrAdvert.Id] = advertNode
|
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
2019-08-22 15:17:32 +03:00
|
|
|
var events []*router.Event
|
2019-08-28 22:11:19 +03:00
|
|
|
for _, event := range pbRtrAdvert.Events {
|
2019-08-30 02:04:46 +03:00
|
|
|
// set the address of the advertising node
|
|
|
|
// we know Route.Gateway is the address of advertNode
|
|
|
|
// NOTE: this is true only when advertNode had not been registered
|
|
|
|
// as our neighbour when we received the advert from it
|
|
|
|
if advertNode.address == "" {
|
|
|
|
advertNode.address = event.Route.Gateway
|
|
|
|
}
|
|
|
|
// if advertising node id is not the same as Route.Router
|
|
|
|
// we know the advertising node is not the origin of the route
|
|
|
|
if advertNode.id != event.Route.Router {
|
|
|
|
// if the origin router is not in the advertising node neighbourhood
|
|
|
|
// we can't rule out potential routing loops so we bail here
|
|
|
|
if _, ok := advertNode.neighbours[event.Route.Router]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 15:17:32 +03:00
|
|
|
route := router.Route{
|
|
|
|
Service: event.Route.Service,
|
|
|
|
Address: event.Route.Address,
|
|
|
|
Gateway: event.Route.Gateway,
|
|
|
|
Network: event.Route.Network,
|
2019-08-28 10:41:19 +03:00
|
|
|
Router: event.Route.Router,
|
2019-08-22 15:17:32 +03:00
|
|
|
Link: event.Route.Link,
|
|
|
|
Metric: int(event.Route.Metric),
|
|
|
|
}
|
2019-08-29 13:45:47 +03:00
|
|
|
// set the route metric
|
|
|
|
n.setRouteMetric(&route)
|
|
|
|
// throw away metric bigger than 1000
|
|
|
|
if route.Metric > 1000 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// create router event
|
2019-08-22 15:17:32 +03:00
|
|
|
e := &router.Event{
|
|
|
|
Type: router.EventType(event.Type),
|
2019-08-28 22:11:19 +03:00
|
|
|
Timestamp: time.Unix(0, pbRtrAdvert.Timestamp),
|
2019-08-22 15:17:32 +03:00
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
advert := &router.Advert{
|
2019-08-28 22:11:19 +03:00
|
|
|
Id: pbRtrAdvert.Id,
|
|
|
|
Type: router.AdvertType(pbRtrAdvert.Type),
|
|
|
|
Timestamp: time.Unix(0, pbRtrAdvert.Timestamp),
|
|
|
|
TTL: time.Duration(pbRtrAdvert.Ttl),
|
2019-08-22 15:17:32 +03:00
|
|
|
Events: events,
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 15:17:32 +03:00
|
|
|
if err := n.Router.Process(advert); err != nil {
|
|
|
|
log.Debugf("Network failed to process advert %s: %v", advert.Id, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
2019-08-22 15:17:32 +03:00
|
|
|
case <-n.closed:
|
|
|
|
return
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// advertise advertises routes to the network
|
|
|
|
func (n *network) advertise(client transport.Client, advertChan <-chan *router.Advert) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// process local adverts and randomly fire them at other nodes
|
|
|
|
case advert := <-advertChan:
|
|
|
|
// create a proto advert
|
2019-08-28 22:11:19 +03:00
|
|
|
var events []*pbRtr.Event
|
2019-08-20 23:12:21 +03:00
|
|
|
for _, event := range advert.Events {
|
2019-08-23 23:08:18 +03:00
|
|
|
// NOTE: we override the Gateway and Link fields here
|
2019-08-28 22:11:19 +03:00
|
|
|
route := &pbRtr.Route{
|
2019-08-20 23:12:21 +03:00
|
|
|
Service: event.Route.Service,
|
|
|
|
Address: event.Route.Address,
|
2019-08-23 18:01:57 +03:00
|
|
|
Gateway: n.options.Address,
|
2019-08-23 23:08:18 +03:00
|
|
|
Network: event.Route.Network,
|
2019-08-28 10:41:19 +03:00
|
|
|
Router: event.Route.Router,
|
2019-08-27 13:36:46 +03:00
|
|
|
Link: DefaultLink,
|
2019-08-20 23:12:21 +03:00
|
|
|
Metric: int64(event.Route.Metric),
|
|
|
|
}
|
2019-08-28 22:11:19 +03:00
|
|
|
e := &pbRtr.Event{
|
|
|
|
Type: pbRtr.EventType(event.Type),
|
2019-08-20 23:12:21 +03:00
|
|
|
Timestamp: event.Timestamp.UnixNano(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
2019-08-28 22:11:19 +03:00
|
|
|
pbRtrAdvert := &pbRtr.Advert{
|
2019-08-20 23:12:21 +03:00
|
|
|
Id: advert.Id,
|
2019-08-28 22:11:19 +03:00
|
|
|
Type: pbRtr.AdvertType(advert.Type),
|
2019-08-20 23:12:21 +03:00
|
|
|
Timestamp: advert.Timestamp.UnixNano(),
|
|
|
|
Events: events,
|
|
|
|
}
|
2019-08-28 22:11:19 +03:00
|
|
|
body, err := proto.Marshal(pbRtrAdvert)
|
2019-08-20 23:12:21 +03:00
|
|
|
if err != nil {
|
|
|
|
// TODO: should we bail here?
|
2019-08-28 22:11:19 +03:00
|
|
|
log.Debugf("Network failed to marshal advert message: %v", err)
|
2019-08-20 23:12:21 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// create transport message and chuck it down the pipe
|
|
|
|
m := transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Micro-Method": "advert",
|
|
|
|
},
|
|
|
|
Body: body,
|
|
|
|
}
|
2019-08-23 18:01:57 +03:00
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
if err := client.Send(&m); err != nil {
|
2019-08-28 22:11:19 +03:00
|
|
|
log.Debugf("Network failed to send advert %s: %v", pbRtrAdvert.Id, err)
|
2019-08-20 23:12:21 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
// Connect connects the network
|
|
|
|
func (n *network) Connect() error {
|
2019-08-20 23:12:21 +03:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
// return if already connected
|
|
|
|
if n.connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to resolve network nodes
|
|
|
|
nodes, err := n.resolveNodes()
|
|
|
|
if err != nil {
|
2019-08-23 19:48:14 +03:00
|
|
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// connect network tunnel
|
|
|
|
if err := n.Tunnel.Connect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the tunnel to resolved nodes
|
|
|
|
n.Tunnel.Init(
|
|
|
|
tunnel.Nodes(nodes...),
|
|
|
|
)
|
|
|
|
|
|
|
|
// dial into ControlChannel to send route adverts
|
2019-08-28 22:11:19 +03:00
|
|
|
ctrlClient, err := n.Tunnel.Dial(ControlChannel)
|
2019-08-20 23:12:21 +03:00
|
|
|
if err != nil {
|
2019-08-22 15:17:32 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:11:26 +03:00
|
|
|
n.tunClient[ControlChannel] = ctrlClient
|
2019-08-28 22:11:19 +03:00
|
|
|
|
2019-08-22 15:17:32 +03:00
|
|
|
// listen on ControlChannel
|
2019-08-28 22:11:19 +03:00
|
|
|
ctrlListener, err := n.Tunnel.Listen(ControlChannel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// dial into NetworkChannel to send network messages
|
|
|
|
netClient, err := n.Tunnel.Dial(NetworkChannel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:11:26 +03:00
|
|
|
n.tunClient[NetworkChannel] = netClient
|
2019-08-28 22:11:19 +03:00
|
|
|
|
|
|
|
// listen on NetworkChannel
|
|
|
|
netListener, err := n.Tunnel.Listen(NetworkChannel)
|
2019-08-22 15:17:32 +03:00
|
|
|
if err != nil {
|
2019-08-20 23:12:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create closed channel
|
|
|
|
n.closed = make(chan bool)
|
|
|
|
|
2019-08-21 21:16:18 +03:00
|
|
|
// start the router
|
|
|
|
if err := n.options.Router.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// start advertising routes
|
|
|
|
advertChan, err := n.options.Router.Advertise()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-21 21:16:18 +03:00
|
|
|
// start the server
|
2019-08-28 01:08:35 +03:00
|
|
|
if err := n.server.Start(); err != nil {
|
2019-08-21 21:16:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// send connect message to NetworkChannel
|
2019-09-03 21:51:52 +03:00
|
|
|
// NOTE: in theory we could do this as soon as
|
|
|
|
// Dial to NetworkChannel succeeds, but instead
|
|
|
|
// we initialize all other node resources first
|
2019-08-28 22:11:19 +03:00
|
|
|
node := &pbNet.Node{
|
|
|
|
Id: n.options.Id,
|
|
|
|
Address: n.options.Address,
|
|
|
|
}
|
|
|
|
pbNetConnect := &pbNet.Connect{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
|
|
|
|
// only proceed with sending to NetworkChannel if marshal succeeds
|
|
|
|
if body, err := proto.Marshal(pbNetConnect); err == nil {
|
|
|
|
m := transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Micro-Method": "connect",
|
|
|
|
},
|
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := netClient.Send(&m); err != nil {
|
|
|
|
log.Debugf("Network failed to send connect messsage: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:51:52 +03:00
|
|
|
// go resolving network nodes
|
|
|
|
go n.resolve()
|
|
|
|
// broadcast neighbourhood
|
|
|
|
go n.announce(netClient)
|
|
|
|
// prune stale nodes
|
|
|
|
go n.prune()
|
|
|
|
// listen to network messages
|
|
|
|
go n.processNetChan(netListener)
|
|
|
|
// advertise service routes
|
|
|
|
go n.advertise(ctrlClient, advertChan)
|
|
|
|
// accept and process routes
|
|
|
|
go n.processCtrlChan(ctrlListener)
|
|
|
|
|
|
|
|
// set connected to true
|
|
|
|
n.connected = true
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-02 14:39:26 +03:00
|
|
|
// Nodes returns a list of all network nodes
|
|
|
|
func (n *network) Nodes() []Node {
|
2019-09-03 04:58:17 +03:00
|
|
|
//track the visited nodes
|
2019-09-02 19:06:21 +03:00
|
|
|
visited := make(map[string]*node)
|
|
|
|
// queue of the nodes to visit
|
2019-09-03 04:58:17 +03:00
|
|
|
queue := list.New()
|
|
|
|
// push network node to the back of queue
|
|
|
|
queue.PushBack(n.node)
|
|
|
|
// mark the node as visited
|
2019-09-02 19:06:21 +03:00
|
|
|
visited[n.node.id] = n.node
|
|
|
|
|
2019-09-03 04:58:17 +03:00
|
|
|
// keep iterating over the queue until its empty
|
|
|
|
for qnode := queue.Front(); qnode != nil; qnode = qnode.Next() {
|
|
|
|
queue.Remove(qnode)
|
2019-09-02 19:06:21 +03:00
|
|
|
// iterate through all of its neighbours
|
|
|
|
// mark the visited nodes; enqueue the non-visted
|
2019-09-03 04:58:17 +03:00
|
|
|
for id, node := range qnode.Value.(*node).neighbours {
|
2019-09-02 19:06:21 +03:00
|
|
|
if _, ok := visited[id]; !ok {
|
|
|
|
visited[id] = node
|
2019-09-03 04:58:17 +03:00
|
|
|
queue.PushBack(node)
|
2019-09-02 19:06:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 17:02:30 +03:00
|
|
|
var nodes []Node
|
2019-09-03 04:58:17 +03:00
|
|
|
// collect all the nodes and return them
|
2019-09-02 19:06:21 +03:00
|
|
|
for _, node := range visited {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
2019-09-02 14:39:26 +03:00
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
func (n *network) close() error {
|
2019-08-21 21:16:18 +03:00
|
|
|
// stop the server
|
2019-08-28 01:08:35 +03:00
|
|
|
if err := n.server.Stop(); err != nil {
|
2019-08-21 21:16:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// stop the router
|
|
|
|
if err := n.Router.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the tunnel
|
|
|
|
if err := n.Tunnel.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes network connection
|
|
|
|
func (n *network) Close() error {
|
2019-08-20 23:12:21 +03:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
if !n.connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
2019-08-28 22:11:19 +03:00
|
|
|
// TODO: send close message to the network channel
|
2019-08-20 23:12:21 +03:00
|
|
|
close(n.closed)
|
|
|
|
// set connected to false
|
|
|
|
n.connected = false
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:11:19 +03:00
|
|
|
// send close message only if we managed to connect to NetworkChannel
|
2019-08-29 01:11:26 +03:00
|
|
|
if netClient, ok := n.tunClient[NetworkChannel]; ok {
|
2019-08-28 22:11:19 +03:00
|
|
|
// send connect message to NetworkChannel
|
|
|
|
node := &pbNet.Node{
|
|
|
|
Id: n.options.Id,
|
|
|
|
Address: n.options.Address,
|
|
|
|
}
|
|
|
|
pbNetClose := &pbNet.Close{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
|
|
|
|
// only proceed with sending to NetworkChannel if marshal succeeds
|
|
|
|
if body, err := proto.Marshal(pbNetClose); err == nil {
|
|
|
|
// create transport message and chuck it down the pipe
|
|
|
|
m := transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Micro-Method": "close",
|
|
|
|
},
|
|
|
|
Body: body,
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:11:26 +03:00
|
|
|
if err := netClient.Send(&m); err != nil {
|
2019-08-28 22:11:19 +03:00
|
|
|
log.Debugf("Network failed to send close messsage: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
return n.close()
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns network client
|
|
|
|
func (n *network) Client() client.Client {
|
2019-08-20 23:12:21 +03:00
|
|
|
return n.client
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server returns network server
|
|
|
|
func (n *network) Server() server.Server {
|
2019-08-28 01:08:35 +03:00
|
|
|
return n.server
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|