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

View file

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