Started migration to sub-packages

This commit is contained in:
Mathias Hall-Andersen
2018-02-09 18:56:00 +01:00
parent 51a6001bb9
commit b461343171
6 changed files with 69 additions and 29 deletions

14
tun.go
View File

@@ -1,14 +1,13 @@
package main
import (
"git.zx2c4.com/wireguard-go/internal/events"
"os"
"sync/atomic"
)
const DefaultMTU = 1420
type TUNEvent int
const (
TUNEventUp = 1 << iota
TUNEventDown
@@ -21,7 +20,7 @@ type TUNDevice interface {
Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
MTU() (int, error) // returns the MTU of the device
Name() string // returns the current name
Events() chan TUNEvent // returns a constant channel of events related to the device
Events() chan events.Event // returns a constant channel of events related to the device
Close() error // stops the device and closes the event channel
}
@@ -30,7 +29,8 @@ func (device *Device) RoutineTUNEventReader() {
logError := device.log.Error
for event := range device.tun.device.Events() {
if event&TUNEventMTUUpdate != 0 {
if event.Contains(TUNEventMTUUpdate) {
mtu, err := device.tun.device.MTU()
old := atomic.LoadInt32(&device.tun.mtu)
if err != nil {
@@ -45,14 +45,16 @@ func (device *Device) RoutineTUNEventReader() {
}
}
if event&TUNEventUp != 0 && !device.isUp.Get() {
if event.Contains(TUNEventUp) && !device.isUp.Get() {
logInfo.Println("Interface set up")
device.Up()
}
if event&TUNEventDown != 0 && device.isUp.Get() {
if event.Contains(TUNEventDown) && device.isUp.Get() {
logInfo.Println("Interface set down")
device.Down()
}
event.Processed()
}
}