EtherGuard-VPN/device/tun.go
Josh Bleecher Snyder 7139279cd0 device: change logging interface to use functions
This commit overhauls wireguard-go's logging.

The primary, motivating change is to use a function instead
of a *log.Logger as the basic unit of logging.
Using functions provides a lot more flexibility for
people to bring their own logging system.

It also introduces logging helper methods on Device.
These reduce line noise at the call site.
They also allow for log functions to be nil;
when nil, instead of generating a log line and throwing it away,
we don't bother generating it at all.
This spares allocation and pointless work.

This is a breaking change, although the fix required
of clients is fairly straightforward.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-01-26 22:40:20 +01:00

52 lines
1.1 KiB
Go

/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
*/
package device
import (
"sync/atomic"
"golang.zx2c4.com/wireguard/tun"
)
const DefaultMTU = 1420
func (device *Device) RoutineTUNEventReader() {
setUp := false
device.debugf("Routine: event worker - started")
for event := range device.tun.device.Events() {
if event&tun.EventMTUUpdate != 0 {
mtu, err := device.tun.device.MTU()
old := atomic.LoadInt32(&device.tun.mtu)
if err != nil {
device.errorf("Failed to load updated MTU of device: %v", err)
} else if int(old) != mtu {
if mtu+MessageTransportSize > MaxMessageSize {
device.infof("MTU updated: %v (too large)", mtu)
} else {
device.infof("MTU updated: %v", mtu)
}
atomic.StoreInt32(&device.tun.mtu, int32(mtu))
}
}
if event&tun.EventUp != 0 && !setUp {
device.infof("Interface set up")
setUp = true
device.Up()
}
if event&tun.EventDown != 0 && setUp {
device.infof("Interface set down")
setUp = false
device.Down()
}
}
device.debugf("Routine: event worker - stopped")
device.state.stopping.Done()
}