2019-08-20 13:48:51 +02:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-09-05 18:18:16 +02:00
|
|
|
"errors"
|
2019-09-17 18:55:42 +02:00
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
2019-12-06 02:18:40 +02:00
|
|
|
"io"
|
2019-10-23 17:48:47 +02:00
|
|
|
"math"
|
2019-08-20 22:12:21 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-08-20 22:15:02 +02:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-08-20 13:48:51 +02:00
|
|
|
"github.com/micro/go-micro/client"
|
2019-08-21 20:16:18 +02:00
|
|
|
rtr "github.com/micro/go-micro/client/selector/router"
|
2019-08-28 21:11:19 +02:00
|
|
|
pbNet "github.com/micro/go-micro/network/proto"
|
2019-10-08 10:25:23 +02:00
|
|
|
"github.com/micro/go-micro/network/resolver/dns"
|
2019-08-20 22:12:21 +02:00
|
|
|
"github.com/micro/go-micro/proxy"
|
|
|
|
"github.com/micro/go-micro/router"
|
2019-08-28 21:11:19 +02:00
|
|
|
pbRtr "github.com/micro/go-micro/router/proto"
|
2019-08-20 13:48:51 +02:00
|
|
|
"github.com/micro/go-micro/server"
|
2019-08-20 22:12:21 +02:00
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
"github.com/micro/go-micro/tunnel"
|
2019-11-25 18:31:43 +02:00
|
|
|
bun "github.com/micro/go-micro/tunnel/broker"
|
2019-08-30 01:04:46 +02:00
|
|
|
tun "github.com/micro/go-micro/tunnel/transport"
|
2019-10-15 16:58:33 +02:00
|
|
|
"github.com/micro/go-micro/util/backoff"
|
2019-08-20 22:12:21 +02:00
|
|
|
"github.com/micro/go-micro/util/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-28 21:11:19 +02: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 20:16:18 +02:00
|
|
|
ControlChannel = "control"
|
2019-08-27 12:36:46 +02:00
|
|
|
// DefaultLink is default network link
|
|
|
|
DefaultLink = "network"
|
2019-10-08 16:15:50 +02:00
|
|
|
// MaxConnections is the max number of network client connections
|
|
|
|
MaxConnections = 3
|
2019-09-05 18:44:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-09-05 18:43:59 +02:00
|
|
|
// ErrClientNotFound is returned when client for tunnel channel could not be found
|
|
|
|
ErrClientNotFound = errors.New("client not found")
|
2019-10-22 21:48:51 +02:00
|
|
|
// ErrPeerLinkNotFound is returned when peer link could not be found in tunnel Links
|
|
|
|
ErrPeerLinkNotFound = errors.New("peer link not found")
|
2019-08-20 13:48:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// network implements Network interface
|
|
|
|
type network struct {
|
2019-09-02 13:39:26 +02:00
|
|
|
// node is network node
|
|
|
|
*node
|
2019-08-20 13:48:51 +02:00
|
|
|
// options configure the network
|
|
|
|
options Options
|
2019-08-20 22:12:21 +02:00
|
|
|
// rtr is network router
|
2019-09-25 13:56:52 +02:00
|
|
|
router router.Router
|
2019-08-20 22:12:21 +02:00
|
|
|
// prx is network proxy
|
2019-09-25 13:56:52 +02:00
|
|
|
proxy proxy.Proxy
|
2019-08-20 22:12:21 +02:00
|
|
|
// tun is network tunnel
|
2019-09-25 13:56:52 +02:00
|
|
|
tunnel tunnel.Tunnel
|
2019-08-28 00:08:35 +02:00
|
|
|
// server is network server
|
|
|
|
server server.Server
|
2019-08-20 22:12:21 +02:00
|
|
|
// client is network client
|
|
|
|
client client.Client
|
|
|
|
|
2019-09-02 12:42:45 +02:00
|
|
|
// tunClient is a map of tunnel clients keyed over tunnel channel names
|
2019-08-29 00:11:26 +02:00
|
|
|
tunClient map[string]transport.Client
|
2019-10-22 21:48:51 +02:00
|
|
|
// peerLinks is a map of links for each peer
|
|
|
|
peerLinks map[string]tunnel.Link
|
2019-08-28 21:11:19 +02:00
|
|
|
|
2019-08-20 22:12:21 +02:00
|
|
|
sync.RWMutex
|
|
|
|
// connected marks the network as connected
|
|
|
|
connected bool
|
|
|
|
// closed closes the network
|
|
|
|
closed chan bool
|
2019-12-07 21:54:29 +02:00
|
|
|
// whether we've discovered by the network
|
|
|
|
discovered chan bool
|
|
|
|
// solicted checks whether routes were solicited by one node
|
|
|
|
solicited chan string
|
|
|
|
}
|
|
|
|
|
|
|
|
// message is network message
|
|
|
|
type message struct {
|
|
|
|
// msg is transport message
|
|
|
|
msg *transport.Message
|
|
|
|
// session is tunnel session
|
|
|
|
session tunnel.Session
|
2019-08-20 13:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newNetwork returns a new network node
|
|
|
|
func newNetwork(opts ...Option) Network {
|
|
|
|
options := DefaultOptions()
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-10-02 16:22:44 +02:00
|
|
|
// set the address to a hashed address
|
|
|
|
hasher := fnv.New64()
|
|
|
|
hasher.Write([]byte(options.Address + options.Id))
|
|
|
|
address := fmt.Sprintf("%d", hasher.Sum64())
|
|
|
|
|
2019-09-18 19:56:02 +02:00
|
|
|
// set the address to advertise
|
2019-10-02 16:22:44 +02:00
|
|
|
var advertise string
|
2019-10-03 18:37:29 +02:00
|
|
|
var peerAddress string
|
2019-10-02 16:22:44 +02:00
|
|
|
|
2019-09-18 19:56:02 +02:00
|
|
|
if len(options.Advertise) > 0 {
|
2019-10-02 16:22:44 +02:00
|
|
|
advertise = options.Advertise
|
2019-10-03 18:37:29 +02:00
|
|
|
peerAddress = options.Advertise
|
2019-10-02 16:22:44 +02:00
|
|
|
} else {
|
|
|
|
advertise = options.Address
|
2019-10-03 18:37:29 +02:00
|
|
|
peerAddress = address
|
2019-09-18 19:56:02 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 00:34:06 +02:00
|
|
|
// init tunnel address to the network bind address
|
|
|
|
options.Tunnel.Init(
|
|
|
|
tunnel.Address(options.Address),
|
|
|
|
)
|
|
|
|
|
|
|
|
// init router Id to the network id
|
|
|
|
options.Router.Init(
|
|
|
|
router.Id(options.Id),
|
|
|
|
router.Address(peerAddress),
|
|
|
|
)
|
|
|
|
|
|
|
|
// create tunnel client with tunnel transport
|
|
|
|
tunTransport := tun.NewTransport(
|
|
|
|
tun.WithTunnel(options.Tunnel),
|
|
|
|
)
|
|
|
|
|
2019-11-25 18:31:43 +02:00
|
|
|
// create the tunnel broker
|
|
|
|
tunBroker := bun.NewBroker(
|
|
|
|
bun.WithTunnel(options.Tunnel),
|
|
|
|
)
|
|
|
|
|
2019-08-28 00:08:35 +02:00
|
|
|
// server is network server
|
|
|
|
server := server.NewServer(
|
|
|
|
server.Id(options.Id),
|
2019-10-03 19:35:54 +02:00
|
|
|
server.Address(peerAddress),
|
2019-10-02 16:22:44 +02:00
|
|
|
server.Advertise(advertise),
|
2019-08-21 20:22:50 +02:00
|
|
|
server.Name(options.Name),
|
2019-08-20 22:12:21 +02:00
|
|
|
server.Transport(tunTransport),
|
2019-11-25 18:31:43 +02:00
|
|
|
server.Broker(tunBroker),
|
2019-08-20 22:12:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// client is network client
|
|
|
|
client := client.NewClient(
|
2019-11-25 18:31:43 +02:00
|
|
|
client.Broker(tunBroker),
|
2019-08-20 22:12:21 +02:00
|
|
|
client.Transport(tunTransport),
|
2019-08-21 20:16:18 +02:00
|
|
|
client.Selector(
|
|
|
|
rtr.NewSelector(
|
|
|
|
rtr.WithRouter(options.Router),
|
|
|
|
),
|
|
|
|
),
|
2019-08-20 22:12:21 +02:00
|
|
|
)
|
|
|
|
|
2019-09-02 13:39:26 +02:00
|
|
|
network := &network{
|
|
|
|
node: &node{
|
2019-09-10 02:14:23 +02:00
|
|
|
id: options.Id,
|
2019-10-03 18:37:29 +02:00
|
|
|
address: peerAddress,
|
2019-09-10 02:14:23 +02:00
|
|
|
peers: make(map[string]*node),
|
2019-09-02 13:39:26 +02:00
|
|
|
},
|
2019-12-07 21:54:29 +02:00
|
|
|
options: options,
|
|
|
|
router: options.Router,
|
|
|
|
proxy: options.Proxy,
|
|
|
|
tunnel: options.Tunnel,
|
|
|
|
server: server,
|
|
|
|
client: client,
|
|
|
|
tunClient: make(map[string]transport.Client),
|
|
|
|
peerLinks: make(map[string]tunnel.Link),
|
|
|
|
discovered: make(chan bool, 1),
|
|
|
|
solicited: make(chan string, 1),
|
2019-08-20 13:48:51 +02:00
|
|
|
}
|
2019-09-02 13:39:26 +02:00
|
|
|
|
|
|
|
network.node.network = network
|
|
|
|
|
|
|
|
return network
|
2019-08-20 13:48:51 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 13:38:13 +02:00
|
|
|
func (n *network) Init(opts ...Option) error {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
// TODO: maybe only allow reinit of certain opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&n.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-02 12:42:45 +02:00
|
|
|
// Options returns network options
|
|
|
|
func (n *network) Options() Options {
|
2019-09-13 04:02:20 +02:00
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
2019-09-02 12:42:45 +02:00
|
|
|
options := n.options
|
|
|
|
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2019-08-20 13:48:51 +02:00
|
|
|
// Name returns network name
|
|
|
|
func (n *network) Name() string {
|
|
|
|
return n.options.Name
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
func (n *network) initNodes(startup bool) {
|
2019-12-08 14:12:20 +02:00
|
|
|
nodes, err := n.resolveNodes()
|
2019-12-08 16:37:17 +02:00
|
|
|
if err != nil && !startup {
|
2019-12-08 14:12:20 +02:00
|
|
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-12-08 16:37:17 +02:00
|
|
|
|
2019-12-08 14:12:20 +02:00
|
|
|
// initialize the tunnel
|
2019-12-08 16:37:17 +02:00
|
|
|
log.Tracef("Network initialising nodes %+v\n", nodes)
|
|
|
|
|
2019-12-08 14:12:20 +02:00
|
|
|
n.tunnel.Init(
|
|
|
|
tunnel.Nodes(nodes...),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// resolveNodes resolves network nodes to addresses
|
2019-08-20 22:12:21 +02:00
|
|
|
func (n *network) resolveNodes() ([]string, error) {
|
|
|
|
// resolve the network address to network nodes
|
|
|
|
records, err := n.options.Resolver.Resolve(n.options.Name)
|
2019-12-08 16:37:17 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep processing
|
2019-08-20 22:12:21 +02:00
|
|
|
|
2019-08-29 15:53:30 +02:00
|
|
|
nodeMap := make(map[string]bool)
|
|
|
|
|
2019-08-20 22:12:21 +02:00
|
|
|
// collect network node addresses
|
2019-12-03 21:59:44 +02:00
|
|
|
//nolint:prealloc
|
2019-08-29 15:53:30 +02:00
|
|
|
var nodes []string
|
2019-10-12 21:26:06 +02:00
|
|
|
var i int
|
2019-10-08 16:48:52 +02:00
|
|
|
|
2019-08-29 16:09:01 +02:00
|
|
|
for _, record := range records {
|
2019-10-12 21:26:06 +02:00
|
|
|
if _, ok := nodeMap[record.Address]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-29 15:53:30 +02:00
|
|
|
nodeMap[record.Address] = true
|
2019-10-12 21:26:06 +02:00
|
|
|
nodes = append(nodes, record.Address)
|
|
|
|
|
2019-10-08 16:48:52 +02:00
|
|
|
i++
|
2019-10-12 21:26:06 +02:00
|
|
|
|
2019-10-08 16:48:52 +02:00
|
|
|
// break once MaxConnection nodes has been reached
|
|
|
|
if i == MaxConnections {
|
|
|
|
break
|
|
|
|
}
|
2019-08-29 15:53:30 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 10:25:23 +02:00
|
|
|
// use the dns resolver to expand peers
|
|
|
|
dns := &dns.Resolver{}
|
|
|
|
|
2019-08-29 15:53:30 +02:00
|
|
|
// append seed nodes if we have them
|
2019-12-08 01:28:39 +02:00
|
|
|
for _, node := range n.options.Nodes {
|
2019-10-08 10:25:23 +02:00
|
|
|
// resolve anything that looks like a host name
|
|
|
|
records, err := dns.Resolve(node)
|
|
|
|
if err != nil {
|
2019-10-13 19:36:22 +02:00
|
|
|
log.Debugf("Failed to resolve %v %v", node, err)
|
2019-10-08 10:25:23 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// add to the node map
|
|
|
|
for _, record := range records {
|
|
|
|
if _, ok := nodeMap[record.Address]; !ok {
|
|
|
|
nodes = append(nodes, record.Address)
|
|
|
|
}
|
2019-08-29 15:53:30 +02:00
|
|
|
}
|
2019-08-20 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
return nodes, nil
|
2019-08-20 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// handleNetConn handles network announcement messages
|
2019-10-23 18:32:45 +02:00
|
|
|
func (n *network) handleNetConn(s tunnel.Session, msg chan *message) {
|
2019-08-20 22:12:21 +02:00
|
|
|
for {
|
|
|
|
m := new(transport.Message)
|
2019-10-22 21:48:51 +02:00
|
|
|
if err := s.Recv(m); err != nil {
|
2019-08-28 21:11:19 +02:00
|
|
|
log.Debugf("Network tunnel [%s] receive error: %v", NetworkChannel, err)
|
2019-12-08 01:28:39 +02:00
|
|
|
switch err {
|
|
|
|
case io.EOF, tunnel.ErrReadTimeout:
|
2019-12-06 02:18:40 +02:00
|
|
|
s.Close()
|
|
|
|
return
|
2019-10-07 20:07:56 +02:00
|
|
|
}
|
2019-12-06 02:18:40 +02:00
|
|
|
continue
|
2019-08-20 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// check if peer is set
|
|
|
|
peer := m.Header["Micro-Peer"]
|
|
|
|
|
|
|
|
// check who the message is intended for
|
|
|
|
if len(peer) > 0 && peer != n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-22 14:17:32 +02:00
|
|
|
select {
|
2019-10-23 18:32:45 +02:00
|
|
|
case msg <- &message{
|
2019-10-22 21:48:51 +02:00
|
|
|
msg: m,
|
|
|
|
session: s,
|
|
|
|
}:
|
2019-08-22 14:17:32 +02:00
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 22:12:21 +02:00
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// acceptNetConn accepts connections from NetworkChannel
|
2019-10-23 18:32:45 +02:00
|
|
|
func (n *network) acceptNetConn(l tunnel.Listener, recv chan *message) {
|
2019-10-15 16:58:33 +02:00
|
|
|
var i int
|
2019-08-28 21:11:19 +02:00
|
|
|
for {
|
|
|
|
// accept a connection
|
|
|
|
conn, err := l.Accept()
|
2019-10-15 16:58:33 +02:00
|
|
|
if err != nil {
|
|
|
|
sleep := backoff.Do(i)
|
|
|
|
log.Debugf("Network tunnel [%s] accept error: %v, backing off for %v", ControlChannel, err, sleep)
|
|
|
|
time.Sleep(sleep)
|
|
|
|
if i > 5 {
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
2019-08-28 21:11:19 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
2019-10-15 16:58:33 +02:00
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] failed to close connection: %v", NetworkChannel, err)
|
2019-10-15 16:03:47 +02:00
|
|
|
}
|
2019-08-28 21:11:19 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
// go handle NetworkChannel connection
|
|
|
|
go n.handleNetConn(conn, recv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processNetChan processes messages received on NetworkChannel
|
2019-10-16 21:44:22 +02:00
|
|
|
func (n *network) processNetChan(listener tunnel.Listener) {
|
2019-12-08 01:28:39 +02:00
|
|
|
defer listener.Close()
|
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// receive network message queue
|
2019-10-23 18:32:45 +02:00
|
|
|
recv := make(chan *message, 128)
|
2019-08-22 14:17:32 +02:00
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// accept NetworkChannel connections
|
2019-09-05 17:04:44 +02:00
|
|
|
go n.acceptNetConn(listener, recv)
|
2019-08-28 21:11:19 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-recv:
|
|
|
|
// switch on type of message and take action
|
2019-10-22 21:48:51 +02:00
|
|
|
switch m.msg.Header["Micro-Method"] {
|
2019-08-28 21:11:19 +02:00
|
|
|
case "connect":
|
2019-09-04 19:02:13 +02:00
|
|
|
// mark the time the message has been received
|
|
|
|
now := time.Now()
|
2019-08-28 21:11:19 +02:00
|
|
|
pbNetConnect := &pbNet.Connect{}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-22 21:48:51 +02:00
|
|
|
if err := proto.Unmarshal(m.msg.Body, pbNetConnect); err != nil {
|
2019-08-28 21:11:19 +02:00
|
|
|
log.Debugf("Network tunnel [%s] connect unmarshal error: %v", NetworkChannel, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// don't process your own messages
|
|
|
|
if pbNetConnect.Node.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-05 01:16:22 +02:00
|
|
|
log.Debugf("Network received connect message from: %s", pbNetConnect.Node.Id)
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
peer := &node{
|
2019-09-10 02:14:23 +02:00
|
|
|
id: pbNetConnect.Node.Id,
|
|
|
|
address: pbNetConnect.Node.Address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
lastSeen: now,
|
2019-08-28 21:11:19 +02:00
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-22 21:48:51 +02:00
|
|
|
// update peer links
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-12-08 02:53:55 +02:00
|
|
|
if err := n.updatePeerLinks(pbNetConnect.Node.Address, m); err != nil {
|
2019-10-22 21:48:51 +02:00
|
|
|
log.Debugf("Network failed updating peer links: %s", err)
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-22 23:46:25 +02:00
|
|
|
// add peer to the list of node peers
|
2019-09-16 20:22:55 +02:00
|
|
|
if err := n.node.AddPeer(peer); err == ErrPeerExists {
|
2019-09-13 04:02:20 +02:00
|
|
|
log.Debugf("Network peer exists, refreshing: %s", peer.id)
|
2019-09-13 19:46:24 +02:00
|
|
|
// update lastSeen time for the existing node
|
2019-09-16 20:22:55 +02:00
|
|
|
if err := n.RefreshPeer(peer.id, now); err != nil {
|
|
|
|
log.Debugf("Network failed refreshing peer %s: %v", peer.id, err)
|
2019-09-13 04:02:20 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
|
|
|
// we send the peer message because someone has sent connect
|
|
|
|
// and wants to know what's on the network. The faster we
|
|
|
|
// respond the faster we start to converge
|
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
// get node peers down to MaxDepth encoded in protobuf
|
|
|
|
msg := PeersToProto(n.node, MaxDepth)
|
2019-12-07 21:54:29 +02:00
|
|
|
node := pbNetConnect.Node.Id
|
|
|
|
|
2019-09-05 18:18:16 +02:00
|
|
|
// advertise yourself to the network
|
2019-12-07 21:54:29 +02:00
|
|
|
if err := n.sendTo("peer", NetworkChannel, node, msg); err != nil {
|
2019-09-10 02:14:23 +02:00
|
|
|
log.Debugf("Network failed to advertise peers: %v", err)
|
2019-09-05 17:04:44 +02:00
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-05 17:04:44 +02:00
|
|
|
// advertise all the routes when a new node has connected
|
2019-09-25 13:56:52 +02:00
|
|
|
if err := n.router.Solicit(); err != nil {
|
2019-09-05 17:04:44 +02:00
|
|
|
log.Debugf("Network failed to solicit routes: %s", err)
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
|
|
|
// specify that we're soliciting
|
|
|
|
select {
|
|
|
|
case n.solicited <- node:
|
|
|
|
default:
|
|
|
|
// don't block
|
|
|
|
}
|
2019-09-10 02:14:23 +02:00
|
|
|
case "peer":
|
2019-09-04 19:02:13 +02:00
|
|
|
// mark the time the message has been received
|
|
|
|
now := time.Now()
|
2019-09-10 02:14:23 +02:00
|
|
|
pbNetPeer := &pbNet.Peer{}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-22 21:48:51 +02:00
|
|
|
if err := proto.Unmarshal(m.msg.Body, pbNetPeer); err != nil {
|
2019-09-10 02:14:23 +02:00
|
|
|
log.Debugf("Network tunnel [%s] peer unmarshal error: %v", NetworkChannel, err)
|
2019-08-28 21:11:19 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// don't process your own messages
|
2019-09-10 02:14:23 +02:00
|
|
|
if pbNetPeer.Node.Id == n.options.Id {
|
2019-08-28 21:11:19 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-13 19:36:22 +02:00
|
|
|
log.Debugf("Network received peer message from: %s %s", pbNetPeer.Node.Id, pbNetPeer.Node.Address)
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
peer := &node{
|
|
|
|
id: pbNetPeer.Node.Id,
|
|
|
|
address: pbNetPeer.Node.Address,
|
|
|
|
peers: make(map[string]*node),
|
|
|
|
lastSeen: now,
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-22 21:48:51 +02:00
|
|
|
// update peer links
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-12-08 02:53:55 +02:00
|
|
|
if err := n.updatePeerLinks(pbNetPeer.Node.Address, m); err != nil {
|
2019-10-22 21:48:51 +02:00
|
|
|
log.Debugf("Network failed updating peer links: %s", err)
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-25 15:30:35 +02:00
|
|
|
if err := n.node.AddPeer(peer); err == nil {
|
2019-09-10 02:14:23 +02:00
|
|
|
// send a solicit message when discovering new peer
|
2019-09-11 16:55:32 +02:00
|
|
|
msg := &pbRtr.Solicit{
|
|
|
|
Id: n.options.Id,
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
|
|
|
node := pbNetPeer.Node.Id
|
|
|
|
|
|
|
|
// only solicit this peer
|
|
|
|
if err := n.sendTo("solicit", ControlChannel, node, msg); err != nil {
|
2019-09-10 00:34:27 +02:00
|
|
|
log.Debugf("Network failed to send solicit message: %s", err)
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-10 00:34:27 +02:00
|
|
|
continue
|
2019-09-25 15:30:35 +02:00
|
|
|
// we're expecting any error to be ErrPeerExists
|
|
|
|
} else if err != ErrPeerExists {
|
|
|
|
log.Debugf("Network got error adding peer %v", err)
|
|
|
|
continue
|
2019-09-04 19:02:13 +02:00
|
|
|
}
|
2019-09-25 15:30:35 +02:00
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
log.Debugf("Network peer exists, refreshing: %s", pbNetPeer.Node.Id)
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
// update lastSeen time for the peer
|
2019-09-16 20:22:55 +02:00
|
|
|
if err := n.RefreshPeer(pbNetPeer.Node.Id, now); err != nil {
|
|
|
|
log.Debugf("Network failed refreshing peer %s: %v", pbNetPeer.Node.Id, err)
|
2019-09-10 00:34:27 +02:00
|
|
|
}
|
2019-09-25 15:30:35 +02:00
|
|
|
|
2019-09-13 19:46:24 +02:00
|
|
|
// NOTE: we don't unpack MaxDepth toplogy
|
2019-09-13 04:02:20 +02:00
|
|
|
peer = UnpackPeerTopology(pbNetPeer, now, MaxDepth-1)
|
2019-11-03 18:12:17 +02:00
|
|
|
log.Tracef("Network updating topology of node: %s", n.node.id)
|
2019-09-16 20:22:55 +02:00
|
|
|
if err := n.node.UpdatePeer(peer); err != nil {
|
|
|
|
log.Debugf("Network failed to update peers: %v", err)
|
2019-09-03 17:39:27 +02:00
|
|
|
}
|
2019-12-06 02:18:40 +02:00
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// tell the connect loop that we've been discovered
|
|
|
|
// so it stops sending connect messages out
|
2019-12-06 02:18:40 +02:00
|
|
|
select {
|
2019-12-07 21:54:29 +02:00
|
|
|
case n.discovered <- true:
|
2019-12-06 02:18:40 +02:00
|
|
|
default:
|
2019-12-07 21:54:29 +02:00
|
|
|
// don't block here
|
2019-12-06 02:18:40 +02:00
|
|
|
}
|
2019-08-28 21:11:19 +02:00
|
|
|
case "close":
|
|
|
|
pbNetClose := &pbNet.Close{}
|
2019-10-22 21:48:51 +02:00
|
|
|
if err := proto.Unmarshal(m.msg.Body, pbNetClose); err != nil {
|
2019-08-28 21:11:19 +02:00
|
|
|
log.Debugf("Network tunnel [%s] close unmarshal error: %v", NetworkChannel, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// don't process your own messages
|
|
|
|
if pbNetClose.Node.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-05 01:16:22 +02:00
|
|
|
log.Debugf("Network received close message from: %s", pbNetClose.Node.Id)
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-15 13:16:08 +02:00
|
|
|
peer := &node{
|
|
|
|
id: pbNetClose.Node.Id,
|
|
|
|
address: pbNetClose.Node.Address,
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
if err := n.DeletePeerNode(peer.id); err != nil {
|
|
|
|
log.Debugf("Network failed to delete node %s routes: %v", peer.id, err)
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
if err := n.prunePeerRoutes(peer); err != nil {
|
|
|
|
log.Debugf("Network failed pruning peer %s routes: %v", peer.id, err)
|
2019-09-03 11:00:14 +02:00
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
|
|
|
// delete peer from the peerLinks
|
2019-10-22 21:48:51 +02:00
|
|
|
n.Lock()
|
|
|
|
delete(n.peerLinks, pbNetClose.Node.Address)
|
|
|
|
n.Unlock()
|
2019-08-28 21:11:19 +02:00
|
|
|
}
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
2019-08-22 14:17:32 +02:00
|
|
|
}
|
2019-08-28 21:11:19 +02:00
|
|
|
}
|
2019-08-22 14:17:32 +02:00
|
|
|
|
2019-09-15 13:16:08 +02:00
|
|
|
// pruneRoutes prunes routes return by given query
|
2019-10-09 18:13:52 +02:00
|
|
|
func (n *network) pruneRoutes(q ...router.QueryOption) error {
|
|
|
|
routes, err := n.router.Table().Query(q...)
|
2019-09-03 11:00:14 +02:00
|
|
|
if err != nil && err != router.ErrRouteNotFound {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-15 13:16:08 +02:00
|
|
|
|
2019-09-03 11:00:14 +02:00
|
|
|
for _, route := range routes {
|
2019-09-25 13:56:52 +02:00
|
|
|
if err := n.router.Table().Delete(route); err != nil && err != router.ErrRouteNotFound {
|
2019-09-03 11:00:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-15 13:16:08 +02:00
|
|
|
// pruneNodeRoutes prunes routes that were either originated by or routable via given node
|
|
|
|
func (n *network) prunePeerRoutes(peer *node) error {
|
|
|
|
// lookup all routes originated by router
|
2019-10-09 18:13:52 +02:00
|
|
|
q := []router.QueryOption{
|
2019-09-15 13:16:08 +02:00
|
|
|
router.QueryRouter(peer.id),
|
2019-10-09 18:13:52 +02:00
|
|
|
}
|
|
|
|
if err := n.pruneRoutes(q...); err != nil {
|
2019-09-15 13:16:08 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup all routes routable via gw
|
2019-10-09 18:13:52 +02:00
|
|
|
q = []router.QueryOption{
|
2019-10-10 12:25:28 +02:00
|
|
|
router.QueryGateway(peer.address),
|
2019-10-09 18:13:52 +02:00
|
|
|
}
|
|
|
|
if err := n.pruneRoutes(q...); err != nil {
|
2019-09-15 13:16:08 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// manage the process of announcing to peers and prune any peer nodes that have not been
|
|
|
|
// seen for a period of time. Also removes all the routes either originated by or routable
|
2019-12-08 14:12:20 +02:00
|
|
|
//by the stale nodes. it also resolves nodes periodically and adds them to the tunnel
|
2019-12-07 21:54:29 +02:00
|
|
|
func (n *network) manage() {
|
|
|
|
announce := time.NewTicker(AnnounceTime)
|
|
|
|
defer announce.Stop()
|
2019-09-03 11:00:14 +02:00
|
|
|
prune := time.NewTicker(PruneTime)
|
|
|
|
defer prune.Stop()
|
2019-12-08 14:12:20 +02:00
|
|
|
resolve := time.NewTicker(ResolveTime)
|
|
|
|
defer resolve.Stop()
|
2019-09-03 11:00:14 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
2019-12-07 21:54:29 +02:00
|
|
|
case <-announce.C:
|
|
|
|
msg := PeersToProto(n.node, MaxDepth)
|
|
|
|
// advertise yourself to the network
|
|
|
|
if err := n.sendMsg("peer", NetworkChannel, msg); err != nil {
|
|
|
|
log.Debugf("Network failed to advertise peers: %v", err)
|
|
|
|
}
|
2019-09-03 11:00:14 +02:00
|
|
|
case <-prune.C:
|
2019-12-07 21:54:29 +02:00
|
|
|
pruned := n.PruneStalePeers(PruneTime)
|
2019-12-08 14:12:20 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
for id, peer := range pruned {
|
|
|
|
log.Debugf("Network peer exceeded prune time: %s", id)
|
2019-12-08 14:12:20 +02:00
|
|
|
|
2019-10-23 18:29:03 +02:00
|
|
|
n.Lock()
|
|
|
|
delete(n.peerLinks, peer.address)
|
|
|
|
n.Unlock()
|
2019-12-08 14:12:20 +02:00
|
|
|
|
2019-09-16 20:22:55 +02:00
|
|
|
if err := n.prunePeerRoutes(peer); err != nil {
|
|
|
|
log.Debugf("Network failed pruning peer %s routes: %v", id, err)
|
2019-09-03 11:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-10 16:25:54 +02:00
|
|
|
// get a list of all routes
|
|
|
|
routes, err := n.options.Router.Table().List()
|
|
|
|
if err != nil {
|
2019-10-23 18:29:03 +02:00
|
|
|
log.Debugf("Network failed listing routes when pruning peers: %v", err)
|
2019-10-10 16:25:54 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-10 16:25:54 +02:00
|
|
|
// collect all the router IDs in the routing table
|
|
|
|
routers := make(map[string]bool)
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-10 16:25:54 +02:00
|
|
|
for _, route := range routes {
|
2019-12-07 21:54:29 +02:00
|
|
|
// check if its been processed
|
|
|
|
if _, ok := routers[route.Router]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark as processed
|
|
|
|
routers[route.Router] = true
|
|
|
|
|
|
|
|
// if the router is NOT in our peer graph, delete all routes originated by it
|
|
|
|
if peer := n.node.GetPeerNode(route.Router); peer != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n.pruneRoutes(router.QueryRouter(route.Router)); err != nil {
|
|
|
|
log.Debugf("Network failed deleting routes by %s: %v", route.Router, err)
|
2019-10-10 16:25:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-08 14:12:20 +02:00
|
|
|
case <-resolve.C:
|
2019-12-08 16:37:17 +02:00
|
|
|
n.initNodes(false)
|
2019-09-03 11:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// sendTo sends a message to a specific node as a one off.
|
|
|
|
// we need this because when links die, we have no discovery info,
|
|
|
|
// and sending to an existing multicast link doesn't immediately work
|
|
|
|
func (n *network) sendTo(method, channel, peer string, msg proto.Message) error {
|
|
|
|
body, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c, err := n.tunnel.Dial(channel, tunnel.DialMode(tunnel.Multicast))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
log.Debugf("Network sending %s message from: %s to %s", method, n.options.Id, peer)
|
|
|
|
|
|
|
|
return c.Send(&transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Micro-Method": method,
|
|
|
|
"Micro-Peer": peer,
|
|
|
|
},
|
|
|
|
Body: body,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendMsg sends a message to the tunnel channel
|
|
|
|
func (n *network) sendMsg(method, channel string, msg proto.Message) error {
|
|
|
|
body, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the channel client is initialized
|
|
|
|
n.RLock()
|
|
|
|
client, ok := n.tunClient[channel]
|
|
|
|
if !ok || client == nil {
|
|
|
|
n.RUnlock()
|
|
|
|
return ErrClientNotFound
|
|
|
|
}
|
|
|
|
n.RUnlock()
|
|
|
|
|
|
|
|
log.Debugf("Network sending %s message from: %s", method, n.options.Id)
|
|
|
|
|
|
|
|
return client.Send(&transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Micro-Method": method,
|
|
|
|
},
|
|
|
|
Body: body,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-08 01:28:39 +02:00
|
|
|
// updatePeerLinks updates link for a given peer
|
2019-12-08 02:53:55 +02:00
|
|
|
func (n *network) updatePeerLinks(peerAddr string, m *message) error {
|
2019-12-08 01:28:39 +02:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2019-12-08 02:53:55 +02:00
|
|
|
linkId := m.msg.Header["Micro-Link"]
|
|
|
|
|
2019-12-08 01:28:39 +02:00
|
|
|
log.Tracef("Network looking up link %s in the peer links", linkId)
|
|
|
|
|
|
|
|
// lookup the peer link
|
|
|
|
var peerLink tunnel.Link
|
|
|
|
|
|
|
|
for _, link := range n.tunnel.Links() {
|
|
|
|
if link.Id() == linkId {
|
|
|
|
peerLink = link
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if peerLink == nil {
|
|
|
|
return ErrPeerLinkNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the peerLink is found in the returned links update peerLinks
|
|
|
|
log.Tracef("Network updating peer links for peer %s", peerAddr)
|
|
|
|
|
|
|
|
// add peerLink to the peerLinks map
|
|
|
|
if link, ok := n.peerLinks[peerAddr]; ok {
|
|
|
|
// if the existing has better Length then the new, replace it
|
|
|
|
if link.Length() < peerLink.Length() {
|
|
|
|
n.peerLinks[peerAddr] = peerLink
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n.peerLinks[peerAddr] = peerLink
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// handleCtrlConn handles ControlChannel connections
|
2019-10-23 18:32:45 +02:00
|
|
|
func (n *network) handleCtrlConn(s tunnel.Session, msg chan *message) {
|
2019-08-28 21:11:19 +02:00
|
|
|
for {
|
|
|
|
m := new(transport.Message)
|
2019-10-22 21:48:51 +02:00
|
|
|
if err := s.Recv(m); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] receive error: %v", ControlChannel, err)
|
2019-12-08 01:28:39 +02:00
|
|
|
switch err {
|
|
|
|
case io.EOF, tunnel.ErrReadTimeout:
|
2019-12-06 02:18:40 +02:00
|
|
|
s.Close()
|
|
|
|
return
|
2019-10-22 21:48:51 +02:00
|
|
|
}
|
2019-12-06 02:18:40 +02:00
|
|
|
continue
|
2019-08-28 21:11:19 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// check if peer is set
|
|
|
|
peer := m.Header["Micro-Peer"]
|
|
|
|
|
|
|
|
// check who the message is intended for
|
|
|
|
if len(peer) > 0 && peer != n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
select {
|
2019-10-23 18:32:45 +02:00
|
|
|
case msg <- &message{
|
2019-10-22 21:48:51 +02:00
|
|
|
msg: m,
|
|
|
|
session: s,
|
|
|
|
}:
|
2019-08-28 21:11:19 +02:00
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// acceptCtrlConn accepts connections from ControlChannel
|
2019-10-23 18:32:45 +02:00
|
|
|
func (n *network) acceptCtrlConn(l tunnel.Listener, recv chan *message) {
|
2019-10-15 16:58:33 +02:00
|
|
|
var i int
|
2019-08-28 21:11:19 +02:00
|
|
|
for {
|
|
|
|
// accept a connection
|
|
|
|
conn, err := l.Accept()
|
2019-10-15 16:58:33 +02:00
|
|
|
if err != nil {
|
|
|
|
sleep := backoff.Do(i)
|
|
|
|
log.Debugf("Network tunnel [%s] accept error: %v, backing off for %v", ControlChannel, err, sleep)
|
|
|
|
time.Sleep(sleep)
|
|
|
|
if i > 5 {
|
|
|
|
// reset the counter
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
2019-08-28 21:11:19 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
2019-10-15 16:58:33 +02:00
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
log.Debugf("Network tunnel [%s] failed to close connection: %v", ControlChannel, err)
|
2019-10-15 16:03:47 +02:00
|
|
|
}
|
2019-08-28 21:11:19 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
// go handle ControlChannel connection
|
|
|
|
go n.handleCtrlConn(conn, recv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 13:53:36 +02:00
|
|
|
// getHopCount queries network graph and returns hop count for given router
|
|
|
|
// - Routes for local services have hop count 1
|
|
|
|
// - Routes with ID of adjacent nodes have hop count 2
|
|
|
|
// - Routes by peers of the advertiser have hop count 3
|
|
|
|
// - Routes beyond node neighbourhood have hop count 4
|
|
|
|
func (n *network) getHopCount(rtr string) int {
|
|
|
|
// make sure node.peers are not modified
|
|
|
|
n.node.RLock()
|
|
|
|
defer n.node.RUnlock()
|
|
|
|
|
|
|
|
// we are the origin of the route
|
|
|
|
if rtr == n.options.Id {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// the route origin is our peer
|
|
|
|
if _, ok := n.peers[rtr]; ok {
|
2019-10-24 18:51:41 +02:00
|
|
|
return 10
|
2019-10-23 13:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// the route origin is the peer of our peer
|
|
|
|
for _, peer := range n.peers {
|
|
|
|
for id := range peer.peers {
|
|
|
|
if rtr == id {
|
2019-10-24 18:51:41 +02:00
|
|
|
return 100
|
2019-10-23 13:53:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// otherwise we are three hops away
|
2019-10-24 18:51:41 +02:00
|
|
|
return 1000
|
2019-10-23 13:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 18:29:03 +02:00
|
|
|
// getRouteMetric calculates router metric and returns it
|
|
|
|
// Route metric is calculated based on link status and route hopd count
|
2019-10-23 20:48:26 +02:00
|
|
|
func (n *network) getRouteMetric(router string, gateway string, link string) int64 {
|
2019-10-23 18:29:03 +02:00
|
|
|
// set the route metric
|
|
|
|
n.RLock()
|
|
|
|
defer n.RUnlock()
|
|
|
|
|
2019-10-23 20:48:26 +02:00
|
|
|
if link == "local" && gateway == "" {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if link == "local" && gateway != "" {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2019-11-03 18:12:17 +02:00
|
|
|
log.Tracef("Network looking up %s link to gateway: %s", link, gateway)
|
2019-10-23 18:29:03 +02:00
|
|
|
if link, ok := n.peerLinks[gateway]; ok {
|
|
|
|
// maka sure delay is non-zero
|
|
|
|
delay := link.Delay()
|
|
|
|
if delay == 0 {
|
|
|
|
delay = 1
|
|
|
|
}
|
|
|
|
// get the route hop count
|
|
|
|
hops := n.getHopCount(router)
|
|
|
|
// make sure length is non-zero
|
|
|
|
length := link.Length()
|
|
|
|
if length == 0 {
|
2019-10-24 18:51:41 +02:00
|
|
|
log.Debugf("Link length is 0 %v %v", link, link.Length())
|
|
|
|
length = 10e9
|
2019-10-23 18:29:03 +02:00
|
|
|
}
|
2019-11-03 18:12:17 +02:00
|
|
|
log.Tracef("Network calculated metric %v delay %v length %v distance %v", (delay*length*int64(hops))/10e6, delay, length, hops)
|
2019-10-24 18:51:41 +02:00
|
|
|
return (delay * length * int64(hops)) / 10e6
|
2019-10-23 18:29:03 +02:00
|
|
|
}
|
2019-10-24 18:51:41 +02:00
|
|
|
|
2019-10-23 20:48:26 +02:00
|
|
|
log.Debugf("Network failed to find a link to gateway: %s", gateway)
|
2019-10-24 18:51:41 +02:00
|
|
|
|
2019-10-23 18:29:03 +02:00
|
|
|
return math.MaxInt64
|
|
|
|
}
|
|
|
|
|
2019-08-29 12:45:47 +02:00
|
|
|
// processCtrlChan processes messages received on ControlChannel
|
2019-10-16 21:44:22 +02:00
|
|
|
func (n *network) processCtrlChan(listener tunnel.Listener) {
|
2019-12-08 01:28:39 +02:00
|
|
|
defer listener.Close()
|
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
// receive control message queue
|
2019-10-23 18:32:45 +02:00
|
|
|
recv := make(chan *message, 128)
|
2019-08-28 21:11:19 +02:00
|
|
|
|
|
|
|
// accept ControlChannel cconnections
|
2019-09-05 17:04:44 +02:00
|
|
|
go n.acceptCtrlConn(listener, recv)
|
2019-08-22 14:17:32 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-recv:
|
|
|
|
// switch on type of message and take action
|
2019-10-22 21:48:51 +02:00
|
|
|
switch m.msg.Header["Micro-Method"] {
|
2019-08-22 14:17:32 +02:00
|
|
|
case "advert":
|
2019-08-28 21:11:19 +02:00
|
|
|
pbRtrAdvert := &pbRtr.Advert{}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-22 21:48:51 +02:00
|
|
|
if err := proto.Unmarshal(m.msg.Body, pbRtrAdvert); err != nil {
|
2019-08-30 01:04:46 +02:00
|
|
|
log.Debugf("Network fail to unmarshal advert message: %v", err)
|
2019-08-22 14:17:32 +02:00
|
|
|
continue
|
2019-08-20 22:12:21 +02:00
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-04 19:02:13 +02:00
|
|
|
// don't process your own messages
|
|
|
|
if pbRtrAdvert.Id == n.options.Id {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-17 18:55:42 +02:00
|
|
|
log.Debugf("Network received advert message from: %s", pbRtrAdvert.Id)
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-13 19:46:24 +02:00
|
|
|
// loookup advertising node in our peer topology
|
|
|
|
advertNode := n.node.GetPeerNode(pbRtrAdvert.Id)
|
2019-09-13 04:02:20 +02:00
|
|
|
if advertNode == nil {
|
2019-09-13 19:46:24 +02:00
|
|
|
// if we can't find the node in our topology (MaxDepth) we skipp prcessing adverts
|
2019-09-13 04:02:20 +02:00
|
|
|
log.Debugf("Network skipping advert message from unknown peer: %s", pbRtrAdvert.Id)
|
|
|
|
continue
|
2019-08-30 01:04:46 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 14:17:32 +02:00
|
|
|
var events []*router.Event
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-08-28 21:11:19 +02:00
|
|
|
for _, event := range pbRtrAdvert.Events {
|
2019-08-30 01:04:46 +02:00
|
|
|
// we know the advertising node is not the origin of the route
|
2019-09-13 19:46:24 +02:00
|
|
|
if pbRtrAdvert.Id != event.Route.Router {
|
2019-09-10 02:14:23 +02:00
|
|
|
// if the origin router is not the advertising node peer
|
2019-08-30 01:04:46 +02:00
|
|
|
// we can't rule out potential routing loops so we bail here
|
2019-09-13 20:55:53 +02:00
|
|
|
if peer := advertNode.GetPeerNode(event.Route.Router); peer == nil {
|
2019-09-13 19:46:24 +02:00
|
|
|
log.Debugf("Network skipping advert message from peer: %s", pbRtrAdvert.Id)
|
2019-08-30 01:04:46 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-08-22 14:17:32 +02:00
|
|
|
route := router.Route{
|
|
|
|
Service: event.Route.Service,
|
|
|
|
Address: event.Route.Address,
|
|
|
|
Gateway: event.Route.Gateway,
|
|
|
|
Network: event.Route.Network,
|
2019-08-28 09:41:19 +02:00
|
|
|
Router: event.Route.Router,
|
2019-08-22 14:17:32 +02:00
|
|
|
Link: event.Route.Link,
|
2019-10-23 13:53:36 +02:00
|
|
|
Metric: event.Route.Metric,
|
2019-08-22 14:17:32 +02:00
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-10-23 16:33:51 +02:00
|
|
|
// calculate route metric and add to the advertised metric
|
2019-10-23 18:42:04 +02:00
|
|
|
// we need to make sure we do not overflow math.MaxInt64
|
2019-10-24 18:51:41 +02:00
|
|
|
metric := n.getRouteMetric(event.Route.Router, event.Route.Gateway, event.Route.Link)
|
2019-11-03 18:12:17 +02:00
|
|
|
log.Tracef("Network metric for router %s and gateway %s: %v", event.Route.Router, event.Route.Gateway, metric)
|
2019-10-24 18:51:41 +02:00
|
|
|
|
|
|
|
// check we don't overflow max int 64
|
2019-12-03 09:25:58 +02:00
|
|
|
if d := route.Metric + metric; d <= 0 {
|
2019-10-24 18:51:41 +02:00
|
|
|
// set to max int64 if we overflow
|
|
|
|
route.Metric = math.MaxInt64
|
2019-10-23 18:42:04 +02:00
|
|
|
} else {
|
2019-10-24 18:51:41 +02:00
|
|
|
// set the combined value of metrics otherwise
|
|
|
|
route.Metric = d
|
2019-10-23 18:42:04 +02:00
|
|
|
}
|
2019-10-24 18:51:41 +02:00
|
|
|
|
2019-08-29 12:45:47 +02:00
|
|
|
// create router event
|
2019-08-22 14:17:32 +02:00
|
|
|
e := &router.Event{
|
|
|
|
Type: router.EventType(event.Type),
|
2019-08-28 21:11:19 +02:00
|
|
|
Timestamp: time.Unix(0, pbRtrAdvert.Timestamp),
|
2019-08-22 14:17:32 +02:00
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-13 21:46:14 +02:00
|
|
|
// if no events are eligible for processing continue
|
|
|
|
if len(events) == 0 {
|
2019-11-03 18:12:17 +02:00
|
|
|
log.Tracef("Network no events to be processed by router: %s", n.options.Id)
|
2019-09-13 21:46:14 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
// create an advert and process it
|
2019-08-22 14:17:32 +02:00
|
|
|
advert := &router.Advert{
|
2019-08-28 21:11:19 +02:00
|
|
|
Id: pbRtrAdvert.Id,
|
|
|
|
Type: router.AdvertType(pbRtrAdvert.Type),
|
|
|
|
Timestamp: time.Unix(0, pbRtrAdvert.Timestamp),
|
|
|
|
TTL: time.Duration(pbRtrAdvert.Ttl),
|
2019-08-22 14:17:32 +02:00
|
|
|
Events: events,
|
2019-08-20 22:12:21 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 12:56:30 +02:00
|
|
|
log.Debugf("Network router %s processing advert: %s", n.Id(), advert.Id)
|
2019-09-25 13:56:52 +02:00
|
|
|
if err := n.router.Process(advert); err != nil {
|
2019-08-22 14:17:32 +02:00
|
|
|
log.Debugf("Network failed to process advert %s: %v", advert.Id, err)
|
|
|
|
}
|
2019-09-05 17:04:44 +02:00
|
|
|
case "solicit":
|
2019-09-11 16:55:32 +02:00
|
|
|
pbRtrSolicit := &pbRtr.Solicit{}
|
2019-10-22 21:48:51 +02:00
|
|
|
if err := proto.Unmarshal(m.msg.Body, pbRtrSolicit); err != nil {
|
2019-09-06 16:05:58 +02:00
|
|
|
log.Debugf("Network fail to unmarshal solicit message: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-11 16:55:32 +02:00
|
|
|
log.Debugf("Network received solicit message from: %s", pbRtrSolicit.Id)
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-11 16:55:32 +02:00
|
|
|
// ignore solicitation when requested by you
|
|
|
|
if pbRtrSolicit.Id == n.options.Id {
|
2019-09-06 16:05:58 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-13 04:02:20 +02:00
|
|
|
log.Debugf("Network router flushing routes for: %s", pbRtrSolicit.Id)
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-05 17:04:44 +02:00
|
|
|
// advertise all the routes when a new node has connected
|
2019-09-25 13:56:52 +02:00
|
|
|
if err := n.router.Solicit(); err != nil {
|
2019-09-05 17:04:44 +02:00
|
|
|
log.Debugf("Network failed to solicit routes: %s", err)
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
|
|
|
// specify that someone solicited the route
|
|
|
|
select {
|
|
|
|
case n.solicited <- pbRtrSolicit.Id:
|
|
|
|
default:
|
|
|
|
// don't block
|
|
|
|
}
|
2019-08-20 22:12:21 +02:00
|
|
|
}
|
2019-08-22 14:17:32 +02:00
|
|
|
case <-n.closed:
|
|
|
|
return
|
2019-08-20 22:12:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// advertise advertises routes to the network
|
2019-10-16 21:44:22 +02:00
|
|
|
func (n *network) advertise(advertChan <-chan *router.Advert) {
|
2019-09-17 18:55:42 +02:00
|
|
|
hasher := fnv.New64()
|
2019-08-20 22:12:21 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// process local adverts and randomly fire them at other nodes
|
|
|
|
case advert := <-advertChan:
|
|
|
|
// create a proto advert
|
2019-08-28 21:11:19 +02:00
|
|
|
var events []*pbRtr.Event
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-08-20 22:12:21 +02:00
|
|
|
for _, event := range advert.Events {
|
2019-09-20 10:40:55 +02:00
|
|
|
// the routes service address
|
|
|
|
address := event.Route.Address
|
|
|
|
|
|
|
|
// only hash the address if we're advertising our own local routes
|
|
|
|
if event.Route.Router == advert.Id {
|
|
|
|
// hash the service before advertising it
|
|
|
|
hasher.Reset()
|
2019-10-26 00:06:49 +02:00
|
|
|
// routes for multiple instances of a service will be collapsed here.
|
|
|
|
// TODO: once we store labels in the table this may need to change
|
|
|
|
// to include the labels in case they differ but highly unlikely
|
|
|
|
hasher.Write([]byte(event.Route.Service + n.node.Address()))
|
2019-09-20 10:40:55 +02:00
|
|
|
address = fmt.Sprintf("%d", hasher.Sum64())
|
|
|
|
}
|
2019-10-23 16:33:51 +02:00
|
|
|
// calculate route metric to advertise
|
2019-10-23 20:55:01 +02:00
|
|
|
metric := n.getRouteMetric(event.Route.Router, event.Route.Gateway, event.Route.Link)
|
2019-09-20 10:40:55 +02:00
|
|
|
// NOTE: we override Gateway, Link and Address here
|
2019-08-28 21:11:19 +02:00
|
|
|
route := &pbRtr.Route{
|
2019-08-20 22:12:21 +02:00
|
|
|
Service: event.Route.Service,
|
2019-09-20 10:40:55 +02:00
|
|
|
Address: address,
|
2019-10-02 16:52:31 +02:00
|
|
|
Gateway: n.node.Address(),
|
2019-08-23 22:08:18 +02:00
|
|
|
Network: event.Route.Network,
|
2019-08-28 09:41:19 +02:00
|
|
|
Router: event.Route.Router,
|
2019-08-27 12:36:46 +02:00
|
|
|
Link: DefaultLink,
|
2019-10-23 16:33:51 +02:00
|
|
|
Metric: metric,
|
2019-08-20 22:12:21 +02:00
|
|
|
}
|
2019-08-28 21:11:19 +02:00
|
|
|
e := &pbRtr.Event{
|
|
|
|
Type: pbRtr.EventType(event.Type),
|
2019-08-20 22:12:21 +02:00
|
|
|
Timestamp: event.Timestamp.UnixNano(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
2019-09-11 16:55:32 +02:00
|
|
|
msg := &pbRtr.Advert{
|
2019-08-20 22:12:21 +02:00
|
|
|
Id: advert.Id,
|
2019-08-28 21:11:19 +02:00
|
|
|
Type: pbRtr.AdvertType(advert.Type),
|
2019-08-20 22:12:21 +02:00
|
|
|
Timestamp: advert.Timestamp.UnixNano(),
|
|
|
|
Events: events,
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
|
|
|
// send the advert to all on the control channel
|
|
|
|
// since its not a solicitation
|
|
|
|
if advert.Type != router.Solicitation {
|
|
|
|
if err := n.sendMsg("advert", ControlChannel, msg); err != nil {
|
|
|
|
log.Debugf("Network failed to advertise routes: %v", err)
|
|
|
|
}
|
2019-08-20 22:12:21 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
|
|
|
// it's a solication, someone asked for it
|
|
|
|
// so we're going to pick off the node and send it
|
|
|
|
select {
|
|
|
|
case node := <-n.solicited:
|
|
|
|
// someone requested the route
|
|
|
|
n.sendTo("advert", ControlChannel, node, msg)
|
|
|
|
default:
|
|
|
|
// send to all since we can't get anything
|
|
|
|
n.sendMsg("advert", ControlChannel, msg)
|
|
|
|
}
|
2019-08-20 22:12:21 +02:00
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 19:36:22 +02:00
|
|
|
func (n *network) sendConnect() {
|
|
|
|
// send connect message to NetworkChannel
|
|
|
|
// NOTE: in theory we could do this as soon as
|
|
|
|
// Dial to NetworkChannel succeeds, but instead
|
|
|
|
// we initialize all other node resources first
|
|
|
|
msg := &pbNet.Connect{
|
|
|
|
Node: &pbNet.Node{
|
|
|
|
Id: n.node.id,
|
|
|
|
Address: n.node.address,
|
|
|
|
},
|
|
|
|
}
|
2019-12-07 21:54:29 +02:00
|
|
|
|
|
|
|
if err := n.sendMsg("connect", NetworkChannel, msg); err != nil {
|
2019-10-13 19:36:22 +02:00
|
|
|
log.Debugf("Network failed to send connect message: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// connect will wait for a link to be established and send the connect
|
|
|
|
// message. We're trying to ensure convergence pretty quickly. So we want
|
|
|
|
// to hear back. In the case we become completely disconnected we'll
|
|
|
|
// connect again once a new link is established
|
2019-12-06 02:18:40 +02:00
|
|
|
func (n *network) connect() {
|
2019-12-07 21:54:29 +02:00
|
|
|
// discovered lets us know what we received a peer message back
|
|
|
|
var discovered bool
|
|
|
|
var attempts int
|
|
|
|
|
|
|
|
// our advertise address
|
|
|
|
loopback := n.server.Options().Advertise
|
|
|
|
// actual address
|
|
|
|
address := n.tunnel.Address()
|
2019-12-06 02:18:40 +02:00
|
|
|
|
|
|
|
for {
|
2019-12-07 21:54:29 +02:00
|
|
|
// connected is used to define if the link is connected
|
|
|
|
var connected bool
|
|
|
|
|
|
|
|
// check the links state
|
2019-12-06 02:18:40 +02:00
|
|
|
for _, link := range n.tunnel.Links() {
|
2019-12-07 21:54:29 +02:00
|
|
|
// skip loopback
|
|
|
|
if link.Loopback() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if remote is ourselves
|
|
|
|
switch link.Remote() {
|
|
|
|
case loopback, address:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-06 02:18:40 +02:00
|
|
|
if link.State() == "connected" {
|
|
|
|
connected = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// if we're not connected wait
|
2019-12-06 02:18:40 +02:00
|
|
|
if !connected {
|
2019-12-07 21:54:29 +02:00
|
|
|
// reset discovered
|
|
|
|
discovered = false
|
|
|
|
// sleep for a second
|
2019-12-06 02:18:40 +02:00
|
|
|
time.Sleep(time.Second)
|
2019-12-07 21:54:29 +02:00
|
|
|
// now try again
|
2019-12-06 02:18:40 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// we're connected but are we discovered?
|
|
|
|
if !discovered {
|
|
|
|
// recreate the clients because all the tunnel links are gone
|
|
|
|
// so we haven't send discovery beneath
|
|
|
|
if err := n.createClients(); err != nil {
|
|
|
|
log.Debugf("Failed to recreate network/control clients: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// send the connect message
|
|
|
|
n.sendConnect()
|
|
|
|
}
|
2019-12-06 02:18:40 +02:00
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
log.Tracef("connected %v discovered %v backoff %v\n", connected, discovered, backoff.Do(attempts))
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// check if we've been discovered
|
2019-12-06 02:18:40 +02:00
|
|
|
select {
|
2019-12-07 21:54:29 +02:00
|
|
|
case <-n.discovered:
|
|
|
|
discovered = true
|
|
|
|
attempts = 0
|
|
|
|
case <-n.closed:
|
2019-12-06 02:18:40 +02:00
|
|
|
return
|
2019-12-07 21:54:29 +02:00
|
|
|
case <-time.After(time.Second + backoff.Do(attempts)):
|
|
|
|
// we have to try again
|
|
|
|
attempts++
|
|
|
|
|
|
|
|
// reset attempts 5 == ~2mins
|
|
|
|
if attempts > 5 {
|
|
|
|
attempts = 0
|
|
|
|
}
|
2019-12-06 02:18:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 13:48:51 +02:00
|
|
|
// Connect connects the network
|
|
|
|
func (n *network) Connect() error {
|
2019-09-13 19:46:24 +02:00
|
|
|
n.Lock()
|
2019-12-08 01:28:39 +02:00
|
|
|
defer n.Unlock()
|
2019-08-20 22:12:21 +02:00
|
|
|
|
2019-12-08 01:28:39 +02:00
|
|
|
// connect network tunnel
|
|
|
|
if err := n.tunnel.Connect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-13 19:36:22 +02:00
|
|
|
// return if already connected
|
|
|
|
if n.connected {
|
2019-12-08 16:37:17 +02:00
|
|
|
// initialise the nodes
|
|
|
|
n.initNodes(false)
|
2019-10-13 19:36:22 +02:00
|
|
|
// send the connect message
|
2019-12-08 16:37:17 +02:00
|
|
|
go n.sendConnect()
|
2019-10-13 19:36:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
// initialise the nodes
|
|
|
|
n.initNodes(true)
|
|
|
|
|
2019-09-17 16:40:00 +02:00
|
|
|
// set our internal node address
|
2019-09-18 20:04:22 +02:00
|
|
|
// if advertise address is not set
|
|
|
|
if len(n.options.Advertise) == 0 {
|
2019-09-25 13:56:52 +02:00
|
|
|
n.server.Init(server.Advertise(n.tunnel.Address()))
|
2019-09-18 20:04:22 +02:00
|
|
|
}
|
2019-09-17 16:40:00 +02:00
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
// listen on NetworkChannel
|
|
|
|
netListener, err := n.tunnel.Listen(
|
|
|
|
NetworkChannel,
|
|
|
|
tunnel.ListenMode(tunnel.Multicast),
|
|
|
|
tunnel.ListenTimeout(AnnounceTime*2),
|
|
|
|
)
|
2019-08-20 22:12:21 +02:00
|
|
|
if err != nil {
|
2019-08-22 14:17:32 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// listen on ControlChannel
|
2019-12-08 01:28:39 +02:00
|
|
|
ctrlListener, err := n.tunnel.Listen(
|
|
|
|
ControlChannel,
|
|
|
|
tunnel.ListenMode(tunnel.Multicast),
|
|
|
|
tunnel.ListenTimeout(router.AdvertiseTableTick*2),
|
|
|
|
)
|
2019-08-28 21:11:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
// dial into ControlChannel to send route adverts
|
|
|
|
ctrlClient, err := n.tunnel.Dial(ControlChannel, tunnel.DialMode(tunnel.Multicast))
|
2019-08-28 21:11:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
n.tunClient[ControlChannel] = ctrlClient
|
2019-08-28 21:11:19 +02:00
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
// dial into NetworkChannel to send network messages
|
|
|
|
netClient, err := n.tunnel.Dial(NetworkChannel, tunnel.DialMode(tunnel.Multicast))
|
2019-08-22 14:17:32 +02:00
|
|
|
if err != nil {
|
2019-08-20 22:12:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-08 16:37:17 +02:00
|
|
|
n.tunClient[NetworkChannel] = netClient
|
|
|
|
|
2019-08-20 22:12:21 +02:00
|
|
|
// create closed channel
|
|
|
|
n.closed = make(chan bool)
|
|
|
|
|
2019-08-21 20:16:18 +02:00
|
|
|
// start the router
|
|
|
|
if err := n.options.Router.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 22:12:21 +02:00
|
|
|
// start advertising routes
|
|
|
|
advertChan, err := n.options.Router.Advertise()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:16:18 +02:00
|
|
|
// start the server
|
2019-08-28 00:08:35 +02:00
|
|
|
if err := n.server.Start(); err != nil {
|
2019-08-21 20:16:18 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:51:52 +02:00
|
|
|
// advertise service routes
|
2019-10-16 21:44:22 +02:00
|
|
|
go n.advertise(advertChan)
|
2019-12-07 21:54:29 +02:00
|
|
|
// listen to network messages
|
|
|
|
go n.processNetChan(netListener)
|
2019-09-03 20:51:52 +02:00
|
|
|
// accept and process routes
|
2019-10-16 21:44:22 +02:00
|
|
|
go n.processCtrlChan(ctrlListener)
|
2019-12-08 16:37:17 +02:00
|
|
|
// manage connection once links are established
|
|
|
|
go n.connect()
|
|
|
|
// resolve nodes, broadcast announcements and prune stale nodes
|
|
|
|
go n.manage()
|
2019-09-03 20:51:52 +02:00
|
|
|
|
2019-12-08 01:28:39 +02:00
|
|
|
// we're now connected
|
2019-09-03 20:51:52 +02:00
|
|
|
n.connected = true
|
|
|
|
|
2019-08-20 22:12:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) close() error {
|
2019-08-21 20:16:18 +02:00
|
|
|
// stop the server
|
2019-08-28 00:08:35 +02:00
|
|
|
if err := n.server.Stop(); err != nil {
|
2019-08-21 20:16:18 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 22:12:21 +02:00
|
|
|
// stop the router
|
2019-09-25 13:56:52 +02:00
|
|
|
if err := n.router.Stop(); err != nil {
|
2019-08-20 22:12:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the tunnel
|
2019-09-25 13:56:52 +02:00
|
|
|
if err := n.tunnel.Close(); err != nil {
|
2019-08-20 22:12:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 13:48:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
// createClients is used to create new clients in the event we lose all the tunnels
|
|
|
|
func (n *network) createClients() error {
|
|
|
|
// dial into ControlChannel to send route adverts
|
|
|
|
ctrlClient, err := n.tunnel.Dial(ControlChannel, tunnel.DialMode(tunnel.Multicast))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// dial into NetworkChannel to send network messages
|
|
|
|
netClient, err := n.tunnel.Dial(NetworkChannel, tunnel.DialMode(tunnel.Multicast))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
// set the control client
|
|
|
|
c, ok := n.tunClient[ControlChannel]
|
|
|
|
if ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
n.tunClient[ControlChannel] = ctrlClient
|
|
|
|
|
|
|
|
// set the network client
|
|
|
|
c, ok = n.tunClient[NetworkChannel]
|
|
|
|
if ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
n.tunClient[NetworkChannel] = netClient
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-20 13:48:51 +02:00
|
|
|
// Close closes network connection
|
|
|
|
func (n *network) Close() error {
|
2019-08-20 22:12:21 +02:00
|
|
|
n.Lock()
|
|
|
|
|
|
|
|
if !n.connected {
|
2019-09-13 04:02:20 +02:00
|
|
|
n.Unlock()
|
2019-08-20 22:12:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
2019-09-13 04:02:20 +02:00
|
|
|
n.Unlock()
|
2019-08-20 22:12:21 +02:00
|
|
|
return nil
|
|
|
|
default:
|
2019-09-13 04:02:20 +02:00
|
|
|
// TODO: send close message to the network channel
|
|
|
|
close(n.closed)
|
|
|
|
// set connected to false
|
|
|
|
n.connected = false
|
|
|
|
|
|
|
|
// unlock the lock otherwise we'll deadlock sending the close
|
|
|
|
n.Unlock()
|
|
|
|
|
2019-09-11 16:55:32 +02:00
|
|
|
msg := &pbNet.Close{
|
|
|
|
Node: &pbNet.Node{
|
2019-09-17 16:40:00 +02:00
|
|
|
Id: n.node.id,
|
|
|
|
Address: n.node.address,
|
2019-09-11 16:55:32 +02:00
|
|
|
},
|
|
|
|
}
|
2019-12-08 01:28:39 +02:00
|
|
|
|
2019-12-07 21:54:29 +02:00
|
|
|
if err := n.sendMsg("close", NetworkChannel, msg); err != nil {
|
2019-09-05 18:18:16 +02:00
|
|
|
log.Debugf("Network failed to send close message: %s", err)
|
2019-08-28 21:11:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 22:12:21 +02:00
|
|
|
return n.close()
|
2019-08-20 13:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns network client
|
|
|
|
func (n *network) Client() client.Client {
|
2019-08-20 22:12:21 +02:00
|
|
|
return n.client
|
2019-08-20 13:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server returns network server
|
|
|
|
func (n *network) Server() server.Server {
|
2019-08-28 00:08:35 +02:00
|
|
|
return n.server
|
2019-08-20 13:48:51 +02:00
|
|
|
}
|