5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 15:20:30 +00:00
This commit is contained in:
Neil Alexander 2019-03-28 19:09:19 +00:00
parent fd0b614f9c
commit dd05a7f2a8
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
5 changed files with 49 additions and 45 deletions

View File

@ -29,7 +29,7 @@ type Core = yggdrasil.Core
type node struct { type node struct {
core Core core Core
tun tuntap.TunAdapter tuntap tuntap.TunAdapter
multicast multicast.Multicast multicast multicast.Multicast
} }
@ -251,13 +251,14 @@ func main() {
// Now that we have a working configuration, we can now actually start // Now that we have a working configuration, we can now actually start
// Yggdrasil. This will start the router, switch, DHT node, TCP and UDP // Yggdrasil. This will start the router, switch, DHT node, TCP and UDP
// sockets, TUN/TAP adapter and multicast discovery port. // sockets, TUN/TAP adapter and multicast discovery port.
n.core.SetRouterAdapter(&n.tun) n.core.SetRouterAdapter(&n.tuntap)
if err := n.core.Start(cfg, logger); err != nil { state, err := n.core.Start(cfg, logger)
if err != nil {
logger.Errorln("An error occurred during startup") logger.Errorln("An error occurred during startup")
panic(err) panic(err)
} }
// Start the multicast interface // Start the multicast interface
n.multicast.Init(&n.core, cfg, logger) n.multicast.Init(&n.core, state, logger, nil)
if err := n.multicast.Start(); err != nil { if err := n.multicast.Start(); err != nil {
logger.Errorln("An error occurred starting multicast:", err) logger.Errorln("An error occurred starting multicast:", err)
} }

View File

@ -16,7 +16,7 @@ import (
type Multicast struct { type Multicast struct {
core *yggdrasil.Core core *yggdrasil.Core
config *config.NodeConfig config *config.NodeState
log *log.Logger log *log.Logger
reconfigure chan chan error reconfigure chan chan error
sock *ipv6.PacketConn sock *ipv6.PacketConn
@ -25,13 +25,13 @@ type Multicast struct {
listenPort uint16 listenPort uint16
} }
func (m *Multicast) Init(core *yggdrasil.Core, config *config.NodeConfig, log *log.Logger) { func (m *Multicast) Init(core *yggdrasil.Core, state *config.NodeState, log *log.Logger, options interface{}) error {
m.core = core m.core = core
m.config = config m.config = state
m.log = log m.log = log
m.reconfigure = make(chan chan error, 1) m.reconfigure = make(chan chan error, 1)
m.listeners = make(map[string]*yggdrasil.TcpListener) m.listeners = make(map[string]*yggdrasil.TcpListener)
current := m.config //.Get() current, _ := m.config.Get()
m.listenPort = current.LinkLocalTCPPort m.listenPort = current.LinkLocalTCPPort
go func() { go func() {
for { for {
@ -44,6 +44,7 @@ func (m *Multicast) Init(core *yggdrasil.Core, config *config.NodeConfig, log *l
if count := len(m.interfaces()); count != 0 { if count := len(m.interfaces()); count != 0 {
m.log.Infoln("Found", count, "multicast interface(s)") m.log.Infoln("Found", count, "multicast interface(s)")
} }
return nil
} }
func (m *Multicast) Start() error { func (m *Multicast) Start() error {
@ -75,10 +76,13 @@ func (m *Multicast) Start() error {
return nil return nil
} }
func (m *Multicast) Stop() error {
return nil
}
func (m *Multicast) interfaces() map[string]net.Interface { func (m *Multicast) interfaces() map[string]net.Interface {
// Get interface expressions from config // Get interface expressions from config
//current, _ := m.config.Get() current, _ := m.config.Get()
current := m.config
exprs := current.MulticastInterfaces exprs := current.MulticastInterfaces
// Ask the system for network interfaces // Ask the system for network interfaces
interfaces := make(map[string]net.Interface) interfaces := make(map[string]net.Interface)
@ -197,7 +201,6 @@ func (m *Multicast) announce() {
if l, ok := m.listeners[iface.Name]; !ok || l.Listener == nil { if l, ok := m.listeners[iface.Name]; !ok || l.Listener == nil {
// No listener was found - let's create one // No listener was found - let's create one
listenaddr := fmt.Sprintf("[%s%%%s]:%d", addrIP, iface.Name, m.listenPort) listenaddr := fmt.Sprintf("[%s%%%s]:%d", addrIP, iface.Name, m.listenPort)
//if li, err := m.core.link.tcp.listen(listenaddr); err == nil {
if li, err := m.core.ListenTCP(listenaddr); err == nil { if li, err := m.core.ListenTCP(listenaddr); err == nil {
m.log.Debugln("Started multicasting on", iface.Name) m.log.Debugln("Started multicasting on", iface.Name)
// Store the listener so that we can stop it later if needed // Store the listener so that we can stop it later if needed
@ -261,7 +264,6 @@ func (m *Multicast) listen() {
continue continue
} }
addr.Zone = "" addr.Zone = ""
//if err := m.core.link.call("tcp://"+addr.String(), from.Zone); err != nil {
if err := m.core.CallPeer("tcp://"+addr.String(), from.Zone); err != nil { if err := m.core.CallPeer("tcp://"+addr.String(), from.Zone); err != nil {
m.log.Debugln("Call from multicast failed:", err) m.log.Debugln("Call from multicast failed:", err)
} }

View File

@ -17,11 +17,6 @@ import (
var buildName string var buildName string
var buildVersion string var buildVersion string
type module interface {
init(*Core, *config.NodeConfig) error
start() error
}
// The Core object represents the Yggdrasil node. You should create a Core // The Core object represents the Yggdrasil node. You should create a Core
// object for each Yggdrasil node you plan to run. // object for each Yggdrasil node you plan to run.
type Core struct { type Core struct {
@ -173,14 +168,14 @@ func GetBuildVersion() string {
// Set the router adapter // Set the router adapter
func (c *Core) SetRouterAdapter(adapter adapterImplementation) { func (c *Core) SetRouterAdapter(adapter adapterImplementation) {
c.router.tun = adapter c.router.adapter = adapter
} }
// Starts up Yggdrasil using the provided NodeState, and outputs debug logging // Starts up Yggdrasil using the provided NodeState, and outputs debug logging
// through the provided log.Logger. The started stack will include TCP and UDP // through the provided log.Logger. The started stack will include TCP and UDP
// sockets, a multicast discovery socket, an admin socket, router, switch and // sockets, a multicast discovery socket, an admin socket, router, switch and
// DHT node. // DHT node.
func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error { func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState, error) {
c.log = log c.log = log
c.config = config.NodeState{ c.config = config.NodeState{
@ -201,7 +196,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
if err := c.link.init(c); err != nil { if err := c.link.init(c); err != nil {
c.log.Errorln("Failed to start link interfaces") c.log.Errorln("Failed to start link interfaces")
return err return nil, err
} }
c.config.Mutex.RLock() c.config.Mutex.RLock()
@ -212,34 +207,34 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
if err := c.switchTable.start(); err != nil { if err := c.switchTable.start(); err != nil {
c.log.Errorln("Failed to start switch") c.log.Errorln("Failed to start switch")
return err return nil, err
} }
if err := c.router.start(); err != nil { if err := c.router.start(); err != nil {
c.log.Errorln("Failed to start router") c.log.Errorln("Failed to start router")
return err return nil, err
} }
if err := c.admin.start(); err != nil { if err := c.admin.start(); err != nil {
c.log.Errorln("Failed to start admin socket") c.log.Errorln("Failed to start admin socket")
return err return nil, err
} }
if err := c.router.tun.Start(c.router.addr, c.router.subnet); err != nil { if err := c.router.adapter.Start(c.router.addr, c.router.subnet); err != nil {
c.log.Errorln("Failed to start TUN/TAP") c.log.Errorln("Failed to start TUN/TAP")
return err return nil, err
} }
go c.addPeerLoop() go c.addPeerLoop()
c.log.Infoln("Startup complete") c.log.Infoln("Startup complete")
return nil return &c.config, nil
} }
// Stops the Yggdrasil node. // Stops the Yggdrasil node.
func (c *Core) Stop() { func (c *Core) Stop() {
c.log.Infoln("Stopping...") c.log.Infoln("Stopping...")
c.router.tun.Close() c.router.adapter.Close()
c.admin.close() c.admin.close()
} }
@ -283,6 +278,12 @@ func (c *Core) GetSubnet() *net.IPNet {
return &net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)} return &net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)}
} }
// GetRouterAddresses returns the raw address and subnet types as used by the
// router
func (c *Core) GetRouterAddresses() (address.Address, address.Subnet) {
return c.router.addr, c.router.subnet
}
// Gets the nodeinfo. // Gets the nodeinfo.
func (c *Core) GetNodeInfo() nodeinfoPayload { func (c *Core) GetNodeInfo() nodeinfoPayload {
return c.router.nodeinfo.getNodeInfo() return c.router.nodeinfo.getNodeInfo()
@ -350,11 +351,11 @@ func (c *Core) GetTUNDefaultIfTAPMode() bool {
// Gets the current TUN/TAP interface name. // Gets the current TUN/TAP interface name.
func (c *Core) GetTUNIfName() string { func (c *Core) GetTUNIfName() string {
//return c.router.tun.iface.Name() //return c.router.tun.iface.Name()
return c.router.tun.Name() return c.router.adapter.Name()
} }
// Gets the current TUN/TAP interface MTU. // Gets the current TUN/TAP interface MTU.
func (c *Core) GetTUNIfMTU() int { func (c *Core) GetTUNIfMTU() int {
//return c.router.tun.mtu //return c.router.tun.mtu
return c.router.tun.MTU() return c.router.adapter.MTU()
} }

View File

@ -5,7 +5,7 @@ package yggdrasil
// TODO clean up old/unused code, maybe improve comments on whatever is left // TODO clean up old/unused code, maybe improve comments on whatever is left
// Send: // Send:
// Receive a packet from the tun // Receive a packet from the adapter
// Look up session (if none exists, trigger a search) // Look up session (if none exists, trigger a search)
// Hand off to session (which encrypts, etc) // Hand off to session (which encrypts, etc)
// Session will pass it back to router.out, which hands it off to the self peer // Session will pass it back to router.out, which hands it off to the self peer
@ -20,7 +20,7 @@ package yggdrasil
// If it's dht/seach/etc. traffic, the router passes it to that part // If it's dht/seach/etc. traffic, the router passes it to that part
// If it's an encapsulated IPv6 packet, the router looks up the session for it // If it's an encapsulated IPv6 packet, the router looks up the session for it
// The packet is passed to the session, which decrypts it, router.recvPacket // The packet is passed to the session, which decrypts it, router.recvPacket
// The router then runs some sanity checks before passing it to the tun // The router then runs some sanity checks before passing it to the adapter
import ( import (
"bytes" "bytes"
@ -31,7 +31,7 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/util" "github.com/yggdrasil-network/yggdrasil-go/src/util"
) )
// The router struct has channels to/from the tun/tap device and a self peer (0), which is how messages are passed between this node and the peers/switch layer. // The router struct has channels to/from the adapter device and a self peer (0), which is how messages are passed between this node and the peers/switch layer.
// The router's mainLoop goroutine is responsible for managing all information related to the dht, searches, and crypto sessions. // The router's mainLoop goroutine is responsible for managing all information related to the dht, searches, and crypto sessions.
type router struct { type router struct {
core *Core core *Core
@ -41,17 +41,17 @@ type router struct {
in <-chan []byte // packets we received from the network, link to peer's "out" in <-chan []byte // packets we received from the network, link to peer's "out"
out func([]byte) // packets we're sending to the network, link to peer's "in" out func([]byte) // packets we're sending to the network, link to peer's "in"
toRecv chan router_recvPacket // packets to handle via recvPacket() toRecv chan router_recvPacket // packets to handle via recvPacket()
tun adapterImplementation // TUN/TAP adapter adapter adapterImplementation // TUN/TAP adapter
recv chan<- []byte // place where the tun pulls received packets from recv chan<- []byte // place where the adapter pulls received packets from
send <-chan []byte // place where the tun puts outgoing packets send <-chan []byte // place where the adapter puts outgoing packets
reject chan<- RejectedPacket // place where we send error packets back to tun reject chan<- RejectedPacket // place where we send error packets back to adapter
reset chan struct{} // signal that coords changed (re-init sessions/dht) reset chan struct{} // signal that coords changed (re-init sessions/dht)
admin chan func() // pass a lambda for the admin socket to query stuff admin chan func() // pass a lambda for the admin socket to query stuff
cryptokey cryptokey cryptokey cryptokey
nodeinfo nodeinfo nodeinfo nodeinfo
} }
// Packet and session info, used to check that the packet matches a valid IP range or CKR prefix before sending to the tun. // Packet and session info, used to check that the packet matches a valid IP range or CKR prefix before sending to the adapter.
type router_recvPacket struct { type router_recvPacket struct {
bs []byte bs []byte
sinfo *sessionInfo sinfo *sessionInfo
@ -70,7 +70,7 @@ type RejectedPacket struct {
Detail interface{} Detail interface{}
} }
// Initializes the router struct, which includes setting up channels to/from the tun/tap. // Initializes the router struct, which includes setting up channels to/from the adapter.
func (r *router) init(core *Core) { func (r *router) init(core *Core) {
r.core = core r.core = core
r.reconfigure = make(chan chan error, 1) r.reconfigure = make(chan chan error, 1)
@ -128,7 +128,7 @@ func (r *router) init(core *Core) {
r.nodeinfo.setNodeInfo(r.core.config.Current.NodeInfo, r.core.config.Current.NodeInfoPrivacy) r.nodeinfo.setNodeInfo(r.core.config.Current.NodeInfo, r.core.config.Current.NodeInfoPrivacy)
r.core.config.Mutex.RUnlock() r.core.config.Mutex.RUnlock()
r.cryptokey.init(r.core) r.cryptokey.init(r.core)
r.tun.Init(&r.core.config, r.core.log, send, recv, reject) r.adapter.Init(&r.core.config, r.core.log, send, recv, reject)
} }
// Starts the mainLoop goroutine. // Starts the mainLoop goroutine.
@ -138,7 +138,7 @@ func (r *router) start() error {
return nil return nil
} }
// Takes traffic from the tun/tap and passes it to router.send, or from r.in and handles incoming traffic. // Takes traffic from the adapter and passes it to router.send, or from r.in and handles incoming traffic.
// Also adds new peer info to the DHT. // Also adds new peer info to the DHT.
// Also resets the DHT and sesssions in the event of a coord change. // Also resets the DHT and sesssions in the event of a coord change.
// Also does periodic maintenance stuff. // Also does periodic maintenance stuff.
@ -179,7 +179,7 @@ func (r *router) mainLoop() {
// If a session to the destination exists, gets the session and passes the packet to it. // If a session to the destination exists, gets the session and passes the packet to it.
// If no session exists, it triggers (or continues) a search. // If no session exists, it triggers (or continues) a search.
// If the session hasn't responded recently, it triggers a ping or search to keep things alive or deal with broken coords *relatively* quickly. // If the session hasn't responded recently, it triggers a ping or search to keep things alive or deal with broken coords *relatively* quickly.
// It also deals with oversized packets if there are MTU issues by calling into icmpv6.go to spoof PacketTooBig traffic, or DestinationUnreachable if the other side has their tun/tap disabled. // It also deals with oversized packets if there are MTU issues by calling into icmpv6.go to spoof PacketTooBig traffic, or DestinationUnreachable if the other side has their adapter disabled.
func (r *router) sendPacket(bs []byte) { func (r *router) sendPacket(bs []byte) {
var sourceAddr address.Address var sourceAddr address.Address
var destAddr address.Address var destAddr address.Address
@ -339,7 +339,7 @@ func (r *router) sendPacket(bs []byte) {
} }
// Called for incoming traffic by the session worker for that connection. // Called for incoming traffic by the session worker for that connection.
// Checks that the IP address is correct (matches the session) and passes the packet to the tun/tap. // Checks that the IP address is correct (matches the session) and passes the packet to the adapter.
func (r *router) recvPacket(bs []byte, sinfo *sessionInfo) { func (r *router) recvPacket(bs []byte, sinfo *sessionInfo) {
// Note: called directly by the session worker, not the router goroutine // Note: called directly by the session worker, not the router goroutine
if len(bs) < 24 { if len(bs) < 24 {
@ -402,7 +402,7 @@ func (r *router) handleIn(packet []byte) {
} }
// Handles incoming traffic, i.e. encapuslated ordinary IPv6 packets. // Handles incoming traffic, i.e. encapuslated ordinary IPv6 packets.
// Passes them to the crypto session worker to be decrypted and sent to the tun/tap. // Passes them to the crypto session worker to be decrypted and sent to the adapter.
func (r *router) handleTraffic(packet []byte) { func (r *router) handleTraffic(packet []byte) {
defer util.PutBytes(packet) defer util.PutBytes(packet)
p := wire_trafficPacket{} p := wire_trafficPacket{}
@ -436,7 +436,7 @@ func (r *router) handleProto(packet []byte) {
return return
} }
// Now do something with the bytes in bs... // Now do something with the bytes in bs...
// send dht messages to dht, sessionRefresh to sessions, data to tun... // send dht messages to dht, sessionRefresh to sessions, data to adapter...
// For data, should check that key and IP match... // For data, should check that key and IP match...
bsType, bsTypeLen := wire_decode_uint64(bs) bsType, bsTypeLen := wire_decode_uint64(bs)
if bsTypeLen == 0 { if bsTypeLen == 0 {

View File

@ -277,7 +277,7 @@ func (ss *sessions) createSession(theirPermKey *crypto.BoxPubKey) *sessionInfo {
sinfo.mySesPriv = *priv sinfo.mySesPriv = *priv
sinfo.myNonce = *crypto.NewBoxNonce() sinfo.myNonce = *crypto.NewBoxNonce()
sinfo.theirMTU = 1280 sinfo.theirMTU = 1280
sinfo.myMTU = uint16(ss.core.router.tun.MTU()) sinfo.myMTU = uint16(ss.core.router.adapter.MTU())
now := time.Now() now := time.Now()
sinfo.time = now sinfo.time = now
sinfo.mtuTime = now sinfo.mtuTime = now