diff --git a/src/yggdrasil/adapter.go b/src/yggdrasil/adapter.go new file mode 100644 index 0000000..4a43209 --- /dev/null +++ b/src/yggdrasil/adapter.go @@ -0,0 +1,25 @@ +package yggdrasil + +// Defines the minimum required functions for an adapter type. +type AdapterInterface interface { + init(core *Core, send chan<- []byte, recv <-chan []byte) + read() error + write() error + close() error +} + +// Defines the minimum required struct members for an adapter type (this is +// now the base type for tunAdapter in tun.go) +type Adapter struct { + AdapterInterface + core *Core + send chan<- []byte + recv <-chan []byte +} + +// Initialises the adapter. +func (adapter *Adapter) init(core *Core, send chan<- []byte, recv <-chan []byte) { + adapter.core = core + adapter.send = send + adapter.recv = recv +} diff --git a/src/yggdrasil/router.go b/src/yggdrasil/router.go index dd9f05d..62480bc 100644 --- a/src/yggdrasil/router.go +++ b/src/yggdrasil/router.go @@ -30,12 +30,6 @@ import ( "golang.org/x/net/ipv6" ) -type adapter struct { - core *Core - send chan<- []byte - recv <-chan []byte -} - // 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's mainLoop goroutine is responsible for managing all information related to the dht, searches, and crypto sessions. type router struct { @@ -45,8 +39,8 @@ type router struct { 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" toRecv chan router_recvPacket // packets to handle via recvPacket() - tun tunAdapter // TUN/TAP adapter - adapters []adapter // Other adapters + tun tunAdapter // TUN/TAP adapter + adapters []Adapter // Other adapters recv chan<- []byte // place where the tun pulls received packets from send <-chan []byte // place where the tun puts outgoing packets reset chan struct{} // signal that coords changed (re-init sessions/dht) diff --git a/src/yggdrasil/tun.go b/src/yggdrasil/tun.go index 223092e..26d32c0 100644 --- a/src/yggdrasil/tun.go +++ b/src/yggdrasil/tun.go @@ -18,7 +18,7 @@ const tun_ETHER_HEADER_LENGTH = 14 // Represents a running TUN/TAP interface. type tunAdapter struct { - adapter + Adapter icmpv6 icmpv6 mtu int iface *water.Interface @@ -35,10 +35,8 @@ func getSupportedMTU(mtu int) int { // Initialises the TUN/TAP adapter. func (tun *tunAdapter) init(core *Core, send chan<- []byte, recv <-chan []byte) { - tun.core = core + tun.Adapter.init(core, send, recv) tun.icmpv6.init(tun) - tun.send = send - tun.recv = recv } // Starts the setup process for the TUN/TAP adapter, and if successful, starts