mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	Define generic adapter type, rename tunDevice to tunAdapter
This commit is contained in:
		
							parent
							
								
									f28360ce4d
								
							
						
					
					
						commit
						8045cb4dc3
					
				
					 8 changed files with 25 additions and 24 deletions
				
			
		| 
						 | 
				
			
			@ -24,7 +24,7 @@ type macAddress [6]byte
 | 
			
		|||
const len_ETHER = 14
 | 
			
		||||
 | 
			
		||||
type icmpv6 struct {
 | 
			
		||||
	tun      *tunDevice
 | 
			
		||||
	tun      *tunAdapter
 | 
			
		||||
	mylladdr net.IP
 | 
			
		||||
	mymac    macAddress
 | 
			
		||||
	peermacs map[address]neighbor
 | 
			
		||||
| 
						 | 
				
			
			@ -57,7 +57,7 @@ func ipv6Header_Marshal(h *ipv6.Header) ([]byte, error) {
 | 
			
		|||
// Initialises the ICMPv6 module by assigning our link-local IPv6 address and
 | 
			
		||||
// our MAC address. ICMPv6 messages will always appear to originate from these
 | 
			
		||||
// addresses.
 | 
			
		||||
func (i *icmpv6) init(t *tunDevice) {
 | 
			
		||||
func (i *icmpv6) init(t *tunAdapter) {
 | 
			
		||||
	i.tun = t
 | 
			
		||||
	i.peermacs = make(map[address]neighbor)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -31,7 +31,9 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
type adapter struct {
 | 
			
		||||
	tunDevice
 | 
			
		||||
	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.
 | 
			
		||||
| 
						 | 
				
			
			@ -43,7 +45,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       tunDevice              // TUN/TAP adapter
 | 
			
		||||
	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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,11 +17,9 @@ const tun_IPv6_HEADER_LENGTH = 40
 | 
			
		|||
const tun_ETHER_HEADER_LENGTH = 14
 | 
			
		||||
 | 
			
		||||
// Represents a running TUN/TAP interface.
 | 
			
		||||
type tunDevice struct {
 | 
			
		||||
	core   *Core
 | 
			
		||||
type tunAdapter struct {
 | 
			
		||||
	adapter
 | 
			
		||||
	icmpv6 icmpv6
 | 
			
		||||
	send   chan<- []byte
 | 
			
		||||
	recv   <-chan []byte
 | 
			
		||||
	mtu    int
 | 
			
		||||
	iface  *water.Interface
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -36,7 +34,7 @@ func getSupportedMTU(mtu int) int {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Initialises the TUN/TAP adapter.
 | 
			
		||||
func (tun *tunDevice) init(core *Core, send chan<- []byte, recv <-chan []byte) {
 | 
			
		||||
func (tun *tunAdapter) init(core *Core, send chan<- []byte, recv <-chan []byte) {
 | 
			
		||||
	tun.core = core
 | 
			
		||||
	tun.icmpv6.init(tun)
 | 
			
		||||
	tun.send = send
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +43,7 @@ func (tun *tunDevice) init(core *Core, send chan<- []byte, recv <-chan []byte) {
 | 
			
		|||
 | 
			
		||||
// Starts the setup process for the TUN/TAP adapter, and if successful, starts
 | 
			
		||||
// the read/write goroutines to handle packets on that interface.
 | 
			
		||||
func (tun *tunDevice) start(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
func (tun *tunAdapter) start(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
	if ifname == "none" {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -77,7 +75,7 @@ func (tun *tunDevice) start(ifname string, iftapmode bool, addr string, mtu int)
 | 
			
		|||
// Writes a packet to the TUN/TAP adapter. If the adapter is running in TAP
 | 
			
		||||
// mode then additional ethernet encapsulation is added for the benefit of the
 | 
			
		||||
// host operating system.
 | 
			
		||||
func (tun *tunDevice) write() error {
 | 
			
		||||
func (tun *tunAdapter) write() error {
 | 
			
		||||
	for {
 | 
			
		||||
		data := <-tun.recv
 | 
			
		||||
		if tun.iface == nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -166,7 +164,7 @@ func (tun *tunDevice) write() error {
 | 
			
		|||
// is running in TAP mode then the ethernet headers will automatically be
 | 
			
		||||
// processed and stripped if necessary. If an ICMPv6 packet is found, then
 | 
			
		||||
// the relevant helper functions in icmpv6.go are called.
 | 
			
		||||
func (tun *tunDevice) read() error {
 | 
			
		||||
func (tun *tunAdapter) read() error {
 | 
			
		||||
	mtu := tun.mtu
 | 
			
		||||
	if tun.iface.IsTAP() {
 | 
			
		||||
		mtu += tun_ETHER_HEADER_LENGTH
 | 
			
		||||
| 
						 | 
				
			
			@ -203,7 +201,7 @@ func (tun *tunDevice) read() error {
 | 
			
		|||
// Closes the TUN/TAP adapter. This is only usually called when the Yggdrasil
 | 
			
		||||
// process stops. Typically this operation will happen quickly, but on macOS
 | 
			
		||||
// it can block until a read operation is completed.
 | 
			
		||||
func (tun *tunDevice) close() error {
 | 
			
		||||
func (tun *tunAdapter) close() error {
 | 
			
		||||
	if tun.iface == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -77,7 +77,7 @@ type in6_ifreq_lifetime struct {
 | 
			
		|||
// a system socket and making syscalls to the kernel. This is not refined though
 | 
			
		||||
// and often doesn't work (if at all), therefore if a call fails, it resorts
 | 
			
		||||
// to calling "ifconfig" instead.
 | 
			
		||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
func (tun *tunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
	var config water.Config
 | 
			
		||||
	if ifname[:4] == "auto" {
 | 
			
		||||
		ifname = "/dev/tap0"
 | 
			
		||||
| 
						 | 
				
			
			@ -103,7 +103,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
 | 
			
		|||
	return tun.setupAddress(addr)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (tun *tunDevice) setupAddress(addr string) error {
 | 
			
		||||
func (tun *tunAdapter) setupAddress(addr string) error {
 | 
			
		||||
	var sfd int
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,7 +14,7 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
// Configures the "utun" adapter with the correct IPv6 address and MTU.
 | 
			
		||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
func (tun *tunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
	if iftapmode {
 | 
			
		||||
		tun.core.log.Printf("TAP mode is not supported on this platform, defaulting to TUN")
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -62,7 +62,7 @@ type ifreq struct {
 | 
			
		|||
 | 
			
		||||
// Sets the IPv6 address of the utun adapter. On Darwin/macOS this is done using
 | 
			
		||||
// a system socket and making direct syscalls to the kernel.
 | 
			
		||||
func (tun *tunDevice) setupAddress(addr string) error {
 | 
			
		||||
func (tun *tunAdapter) setupAddress(addr string) error {
 | 
			
		||||
	var fd int
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,7 +13,7 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
// Configures the TAP adapter with the correct IPv6 address and MTU.
 | 
			
		||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
func (tun *tunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
	var config water.Config
 | 
			
		||||
	if iftapmode {
 | 
			
		||||
		config = water.Config{DeviceType: water.TAP}
 | 
			
		||||
| 
						 | 
				
			
			@ -48,7 +48,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
 | 
			
		|||
// is used to do this, so there is not a hard requirement on "ip" or "ifconfig"
 | 
			
		||||
// to exist on the system, but this will fail if Netlink is not present in the
 | 
			
		||||
// kernel (it nearly always is).
 | 
			
		||||
func (tun *tunDevice) setupAddress(addr string) error {
 | 
			
		||||
func (tun *tunAdapter) setupAddress(addr string) error {
 | 
			
		||||
	// Set address
 | 
			
		||||
	var netIF *net.Interface
 | 
			
		||||
	ifces, err := net.Interfaces()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,7 @@ import water "github.com/yggdrasil-network/water"
 | 
			
		|||
 | 
			
		||||
// Creates the TUN/TAP adapter, if supported by the Water library. Note that
 | 
			
		||||
// no guarantees are made at this point on an unsupported platform.
 | 
			
		||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
func (tun *tunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
	var config water.Config
 | 
			
		||||
	if iftapmode {
 | 
			
		||||
		config = water.Config{DeviceType: water.TAP}
 | 
			
		||||
| 
						 | 
				
			
			@ -27,7 +27,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
 | 
			
		|||
 | 
			
		||||
// We don't know how to set the IPv6 address on an unknown platform, therefore
 | 
			
		||||
// write about it to stdout and don't try to do anything further.
 | 
			
		||||
func (tun *tunDevice) setupAddress(addr string) error {
 | 
			
		||||
func (tun *tunAdapter) setupAddress(addr string) error {
 | 
			
		||||
	tun.core.log.Println("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,7 +13,7 @@ import (
 | 
			
		|||
// Configures the TAP adapter with the correct IPv6 address and MTU. On Windows
 | 
			
		||||
// we don't make use of a direct operating system API to do this - we instead
 | 
			
		||||
// delegate the hard work to "netsh".
 | 
			
		||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
func (tun *tunAdapter) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
			
		||||
	if !iftapmode {
 | 
			
		||||
		tun.core.log.Printf("TUN mode is not supported on this platform, defaulting to TAP")
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -65,7 +65,7 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Sets the MTU of the TAP adapter.
 | 
			
		||||
func (tun *tunDevice) setupMTU(mtu int) error {
 | 
			
		||||
func (tun *tunAdapter) setupMTU(mtu int) error {
 | 
			
		||||
	// Set MTU
 | 
			
		||||
	cmd := exec.Command("netsh", "interface", "ipv6", "set", "subinterface",
 | 
			
		||||
		fmt.Sprintf("interface=%s", tun.iface.Name()),
 | 
			
		||||
| 
						 | 
				
			
			@ -82,7 +82,7 @@ func (tun *tunDevice) setupMTU(mtu int) error {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Sets the IPv6 address of the TAP adapter.
 | 
			
		||||
func (tun *tunDevice) setupAddress(addr string) error {
 | 
			
		||||
func (tun *tunAdapter) setupAddress(addr string) error {
 | 
			
		||||
	// Set address
 | 
			
		||||
	cmd := exec.Command("netsh", "interface", "ipv6", "add", "address",
 | 
			
		||||
		fmt.Sprintf("interface=%s", tun.iface.Name()),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue