Unify MTU datatypes across the codebase

The codebase uses int and unit16 to represent MTU randomly. This change
unifies it to a MTU type from types package, which is currently uint16.
This commit is contained in:
Adam Ruzicka 2020-01-05 17:27:54 +00:00
parent 4b16c325a3
commit 8358fe5c5c
13 changed files with 51 additions and 37 deletions

View file

@ -55,7 +55,7 @@ type tunReader struct {
func (r *tunReader) _read() {
// Get a slice to store the packet in
recvd := util.ResizeBytes(util.GetBytes(), r.tun.mtu+TUN_OFFSET_BYTES)
recvd := util.ResizeBytes(util.GetBytes(), int(r.tun.mtu)+TUN_OFFSET_BYTES)
// Wait for a packet to be delivered to us through the TUN adapter
n, err := r.tun.iface.Read(recvd, TUN_OFFSET_BYTES)
if n <= TUN_OFFSET_BYTES || err != nil {

View file

@ -25,8 +25,11 @@ import (
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
"github.com/yggdrasil-network/yggdrasil-go/src/types"
)
type MTU = types.MTU
const tun_IPv6_HEADER_LENGTH = 40
// TunAdapter represents a running TUN interface and extends the
@ -46,7 +49,7 @@ type TunAdapter struct {
subnet address.Subnet
ckr cryptokey
icmpv6 ICMPv6
mtu int
mtu MTU
iface tun.Device
phony.Inbox // Currently only used for _handlePacket from the reader, TODO: all the stuff that currently needs a mutex below
//mutex sync.RWMutex // Protects the below
@ -63,7 +66,7 @@ type TunOptions struct {
// Gets the maximum supported MTU for the platform based on the defaults in
// defaults.GetDefaults().
func getSupportedMTU(mtu int) int {
func getSupportedMTU(mtu MTU) MTU {
if mtu < 1280 {
return 1280
}
@ -85,7 +88,7 @@ func (tun *TunAdapter) Name() string {
// MTU gets the adapter's MTU. This can range between 1280 and 65535, although
// the maximum value is determined by your platform. The returned value will
// never exceed that of MaximumMTU().
func (tun *TunAdapter) MTU() int {
func (tun *TunAdapter) MTU() MTU {
return getSupportedMTU(tun.mtu)
}
@ -96,14 +99,14 @@ func DefaultName() string {
// DefaultMTU gets the default TUN interface MTU for your platform. This can
// be as high as MaximumMTU(), depending on platform, but is never lower than 1280.
func DefaultMTU() int {
func DefaultMTU() MTU {
return defaults.GetDefaults().DefaultIfMTU
}
// MaximumMTU returns the maximum supported TUN interface MTU for your
// platform. This can be as high as 65535, depending on platform, but is never
// lower than 1280.
func MaximumMTU() int {
func MaximumMTU() MTU {
return defaults.GetDefaults().MaximumIfMTU
}
@ -165,7 +168,7 @@ func (tun *TunAdapter) _start() error {
if tun.MTU() != current.IfMTU {
tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", current.IfMTU, tun.MTU(), MaximumMTU())
}
tun.core.SetMaximumSessionMTU(uint16(tun.MTU()))
tun.core.SetMaximumSessionMTU(tun.MTU())
tun.isOpen = true
go tun.handler()
tun.reader.Act(nil, tun.reader._read) // Start the reader

View file

@ -73,14 +73,14 @@ type in6_ifreq_lifetime struct {
}
// Configures the TUN adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
iface, err := wgtun.CreateTUN(ifname, mtu)
func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
iface, err := wgtun.CreateTUN(ifname, int(mtu))
if err != nil {
panic(err)
}
tun.iface = iface
if mtu, err := iface.MTU(); err == nil {
tun.mtu = getSupportedMTU(mtu)
tun.mtu = getSupportedMTU(MTU(mtu))
} else {
tun.mtu = 0
}

View file

@ -16,17 +16,17 @@ import (
)
// Configures the "utun" adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
if ifname == "auto" {
ifname = "utun"
}
iface, err := wgtun.CreateTUN(ifname, mtu)
iface, err := wgtun.CreateTUN(ifname, int(mtu))
if err != nil {
panic(err)
}
tun.iface = iface
if mtu, err := iface.MTU(); err == nil {
tun.mtu = getSupportedMTU(mtu)
tun.mtu = getSupportedMTU(MTU(mtu))
} else {
tun.mtu = 0
}

View file

@ -10,17 +10,17 @@ import (
)
// Configures the TUN adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
if ifname == "auto" {
ifname = "\000"
}
iface, err := wgtun.CreateTUN(ifname, mtu)
iface, err := wgtun.CreateTUN(ifname, int(mtu))
if err != nil {
panic(err)
}
tun.iface = iface
if mtu, err := iface.MTU(); err == nil {
tun.mtu = getSupportedMTU(mtu)
tun.mtu = getSupportedMTU(MTU(mtu))
} else {
tun.mtu = 0
}
@ -43,7 +43,7 @@ func (tun *TunAdapter) setupAddress(addr string) error {
if err := netlink.AddrAdd(nlintf, nladdr); err != nil {
return err
}
if err := netlink.LinkSetMTU(nlintf, tun.mtu); err != nil {
if err := netlink.LinkSetMTU(nlintf, int(tun.mtu)); err != nil {
return err
}
if err := netlink.LinkSetUp(nlintf); err != nil {

View file

@ -19,7 +19,7 @@ import (
// This is to catch Windows platforms
// Configures the TUN adapter with the correct IPv6 address and MTU.
func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
func (tun *TunAdapter) setup(ifname string, addr string, mtu MTU) error {
if ifname == "auto" {
ifname = defaults.GetDefaults().DefaultIfName
}
@ -30,7 +30,7 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
if guid, err = windows.GUIDFromString("{8f59971a-7872-4aa6-b2eb-061fc4e9d0a7}"); err != nil {
return err
}
if iface, err = wgtun.CreateTUNWithRequestedGUID(ifname, &guid, mtu); err != nil {
if iface, err = wgtun.CreateTUNWithRequestedGUID(ifname, &guid, int(mtu)); err != nil {
return err
}
tun.iface = iface
@ -42,15 +42,15 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu int) error {
tun.log.Errorln("Failed to set up TUN MTU:", err)
return err
}
if mtu, err = iface.MTU(); err == nil {
tun.mtu = mtu
if mtu, err := iface.MTU(); err == nil {
tun.mtu = MTU(mtu)
}
return nil
})
}
// Sets the MTU of the TAP adapter.
func (tun *TunAdapter) setupMTU(mtu int) error {
func (tun *TunAdapter) setupMTU(mtu MTU) error {
if tun.iface == nil || tun.Name() == "" {
return errors.New("Can't configure MTU as TUN adapter is not present")
}