Reverted event changes

This feature was not needed for Android, upon further inspection.
This commit is contained in:
Mathias Hall-Andersen 2018-02-11 18:55:30 +01:00
parent b461343171
commit 73cb1a1155
3 changed files with 18 additions and 57 deletions

View file

@ -1,36 +0,0 @@
package events
import (
"sync"
)
type Event interface {
Contains(int) bool
Processed()
WaitForProcessed()
}
type EventStruct struct {
code int
lock sync.Mutex
}
func (event EventStruct) Contains(code int) bool {
return event.code&code != 0
}
func (event *EventStruct) WaitForProcessed() {
event.lock.Lock()
}
func (event *EventStruct) Processed() {
event.lock.Unlock()
}
func NewEvent(code int) Event {
event := &EventStruct{
code: code,
}
event.lock.Lock()
return event
}

14
tun.go
View file

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

View file

@ -7,7 +7,6 @@ import (
"encoding/binary"
"errors"
"fmt"
"git.zx2c4.com/wireguard-go/internal/events"
"golang.org/x/net/ipv6"
"golang.org/x/sys/unix"
"net"
@ -53,10 +52,10 @@ const (
type NativeTun struct {
fd *os.File
index int32 // if index
name string // name of interface
errors chan error // async error handling
events chan events.Event // device related events
index int32 // if index
name string // name of interface
errors chan error // async error handling
events chan TUNEvent // device related events
}
func (tun *NativeTun) File() *os.File {
@ -72,9 +71,9 @@ func (tun *NativeTun) RoutineHackListener() {
_, err := unix.Write(fd, nil)
switch err {
case unix.EINVAL:
tun.events <- events.NewEvent(TUNEventUp)
tun.events <- TUNEventUp
case unix.EIO:
tun.events <- events.NewEvent(TUNEventDown)
tun.events <- TUNEventDown
default:
}
time.Sleep(time.Second / 10)
@ -119,14 +118,14 @@ func (tun *NativeTun) RoutineNetlinkListener() {
}
if info.Flags&unix.IFF_RUNNING != 0 {
tun.events <- events.NewEvent(TUNEventUp)
tun.events <- TUNEventUp
}
if info.Flags&unix.IFF_RUNNING == 0 {
tun.events <- events.NewEvent(TUNEventDown)
tun.events <- TUNEventDown
}
tun.events <- events.NewEvent(TUNEventMTUUpdate)
tun.events <- TUNEventMTUUpdate
default:
remain = remain[hdr.Len:]
@ -289,7 +288,7 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
}
}
func (tun *NativeTun) Events() chan events.Event {
func (tun *NativeTun) Events() chan TUNEvent {
return tun.events
}
@ -301,7 +300,7 @@ func CreateTUNFromFile(name string, fd *os.File) (TUNDevice, error) {
device := &NativeTun{
fd: fd,
name: name,
events: make(chan events.Event, 5),
events: make(chan TUNEvent, 5),
errors: make(chan error, 5),
}
@ -358,7 +357,7 @@ func CreateTUN(name string) (TUNDevice, error) {
device := &NativeTun{
fd: fd,
name: newName,
events: make(chan events.Event, 5),
events: make(chan TUNEvent, 5),
errors: make(chan error, 5),
}