Skip to content

Commit

Permalink
Introduce rwcancel
Browse files Browse the repository at this point in the history
  • Loading branch information
zx2c4 committed May 13, 2018
1 parent e21ea58 commit 7e10ebe
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: wireguard-go

wireguard-go: $(wildcard *.go)
wireguard-go: $(wildcard *.go) $(wildcard */*.go)
go build -o $@

clean:
Expand Down
2 changes: 1 addition & 1 deletion misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func toInt32(n uint32) int32 {
return int32(-(n & mask) + (n & ^mask))
}

func min(a uint, b uint) uint {
func min(a, b uint) uint {
if a > b {
return b
}
Expand Down
132 changes: 132 additions & 0 deletions rwcancel/rwcancel_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2017-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
*/

package rwcancel

import (
"errors"
"golang.org/x/sys/unix"
"os"
"runtime"
"syscall"
)

type RWCancel struct {
fd int
closingReader *os.File
closingWriter *os.File
}

type fdSet struct {
fdset unix.FdSet
}

func (fdset *fdSet) set(i int) {
bits := 32 << (^uint(0) >> 63)
fdset.fdset.Bits[i/bits] |= 1 << uint(i%bits)
}

func (fdset *fdSet) check(i int) bool {
bits := 32 << (^uint(0) >> 63)
return (fdset.fdset.Bits[i/bits] & (1 << uint(i%bits))) != 0
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func NewRWCancel(fd int) (*RWCancel, error) {
err := unix.SetNonblock(fd, true)
if err != nil {
return nil, err
}
rwcancel := RWCancel{fd: fd}

rwcancel.closingReader, rwcancel.closingWriter, err = os.Pipe()
if err != nil {
return nil, err
}

runtime.SetFinalizer(&rwcancel, func(rw *RWCancel) {
rw.Cancel()
})

return &rwcancel, nil
}

/* https://golang.org/src/crypto/rand/eagain.go */
func ErrorIsEAGAIN(err error) bool {
if pe, ok := err.(*os.PathError); ok {
if errno, ok := pe.Err.(syscall.Errno); ok && errno == syscall.EAGAIN {
return true
}
}
if errno, ok := err.(syscall.Errno); ok && errno == syscall.EAGAIN {
return true
}
return false
}

func (rw *RWCancel) ReadyRead() bool {
closeFd := int(rw.closingReader.Fd())
fdset := fdSet{}
fdset.set(rw.fd)
fdset.set(closeFd)
_, err := unix.Select(max(rw.fd, closeFd)+1, &fdset.fdset, nil, nil, nil)
if err != nil {
return false
}
if fdset.check(closeFd) {
return false
}
return fdset.check(rw.fd)
}

func (rw *RWCancel) ReadyWrite() bool {
closeFd := int(rw.closingReader.Fd())
fdset := fdSet{}
fdset.set(rw.fd)
fdset.set(closeFd)
_, err := unix.Select(max(rw.fd, closeFd)+1, nil, &fdset.fdset, nil, nil)
if err != nil {
return false
}
if fdset.check(closeFd) {
return false
}
return fdset.check(rw.fd)
}

func (rw *RWCancel) Read(p []byte) (n int, err error) {
for {
n, err := unix.Read(rw.fd, p)
if err == nil || !ErrorIsEAGAIN(err) {
return n, err
}
if !rw.ReadyRead() {
return 0, errors.New("fd closed")
}
}
}

func (rw *RWCancel) Write(p []byte) (n int, err error) {
for {
n, err := unix.Write(rw.fd, p)
if err == nil || !ErrorIsEAGAIN(err) {
return n, err
}
if !rw.ReadyWrite() {
return 0, errors.New("fd closed")
}
}
}

func (rw *RWCancel) Cancel() (err error) {
_, err = rw.closingWriter.Write([]byte{0})
return
}
79 changes: 13 additions & 66 deletions tun_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package main
*/

import (
"./rwcancel"
"bytes"
"encoding/binary"
"errors"
Expand All @@ -20,7 +21,6 @@ import (
"net"
"os"
"strconv"
"syscall"
"time"
"unsafe"
)
Expand All @@ -31,14 +31,13 @@ const (
)

type NativeTun struct {
fd *os.File
index int32 // if index
name string // name of interface
errors chan error // async error handling
events chan TUNEvent // device related events
nopi bool // the device was pased IFF_NO_PI
closingReader *os.File
closingWriter *os.File
fd *os.File
index int32 // if index
name string // name of interface
errors chan error // async error handling
events chan TUNEvent // device related events
nopi bool // the device was pased IFF_NO_PI
rwcancel *rwcancel.RWCancel
}

func (tun *NativeTun) File() *os.File {
Expand Down Expand Up @@ -305,43 +304,6 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
return tun.fd.Write(buff)
}

type FdSet struct {
fdset unix.FdSet
}

func (fdset *FdSet) set(i int) {
bits := 32 << (^uint(0) >> 63)
fdset.fdset.Bits[i/bits] |= 1 << uint(i%bits)
}

func (fdset *FdSet) check(i int) bool {
bits := 32 << (^uint(0) >> 63)
return (fdset.fdset.Bits[i/bits] & (1 << uint(i%bits))) != 0
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func (tun *NativeTun) readyRead() bool {
readFd := int(tun.fd.Fd())
closeFd := int(tun.closingReader.Fd())
fdset := FdSet{}
fdset.set(readFd)
fdset.set(closeFd)
_, err := unix.Select(max(readFd, closeFd)+1, &fdset.fdset, nil, nil, nil)
if err != nil {
return false
}
if fdset.check(closeFd) {
return false
}
return fdset.check(readFd)
}

func (tun *NativeTun) doRead(buff []byte, offset int) (int, error) {
select {
case err := <-tun.errors:
Expand All @@ -360,24 +322,14 @@ func (tun *NativeTun) doRead(buff []byte, offset int) (int, error) {
}
}

/* https://golang.org/src/crypto/rand/eagain.go */
func unixIsEAGAIN(err error) bool {
if pe, ok := err.(*os.PathError); ok {
if errno, ok := pe.Err.(syscall.Errno); ok && errno == syscall.EAGAIN {
return true
}
}
return false
}

func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
for {
n, err := tun.doRead(buff, offset)
if err == nil || !unixIsEAGAIN(err) {
if err == nil || !rwcancel.ErrorIsEAGAIN(err) {
return n, err
}
if !tun.readyRead() {
return 0, errors.New("Tun device closed")
if !tun.rwcancel.ReadyRead() {
return 0, errors.New("tun device closed")
}
}
}
Expand All @@ -391,7 +343,7 @@ func (tun *NativeTun) Close() error {
if err != nil {
return err
}
tun.closingWriter.Write([]byte{0})
tun.rwcancel.Cancel()
close(tun.events)
return nil
}
Expand Down Expand Up @@ -450,7 +402,7 @@ func CreateTUNFromFile(fd *os.File) (TUNDevice, error) {
}
var err error

err = unix.SetNonblock(int(fd.Fd()), true)
device.rwcancel, err = rwcancel.NewRWCancel(int(fd.Fd()))
if err != nil {
return nil, err
}
Expand All @@ -460,11 +412,6 @@ func CreateTUNFromFile(fd *os.File) (TUNDevice, error) {
return nil, err
}

device.closingReader, device.closingWriter, err = os.Pipe()
if err != nil {
return nil, err
}

// start event listener

device.index, err = getIFIndex(device.name)
Expand Down
30 changes: 23 additions & 7 deletions uapi_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package main

import (
"./rwcancel"
"errors"
"fmt"
"golang.org/x/sys/unix"
Expand All @@ -24,10 +25,11 @@ const (
)

type UAPIListener struct {
listener net.Listener // unix socket listener
connNew chan net.Conn
connErr chan error
inotifyFd int
listener net.Listener // unix socket listener
connNew chan net.Conn
connErr chan error
inotifyFd int
inotifyRWCancel *rwcancel.RWCancel
}

func (l *UAPIListener) Accept() (net.Conn, error) {
Expand All @@ -45,10 +47,14 @@ func (l *UAPIListener) Accept() (net.Conn, error) {
func (l *UAPIListener) Close() error {
err1 := unix.Close(l.inotifyFd)
err2 := l.listener.Close()
err3 := l.inotifyRWCancel.Cancel()
if err1 != nil {
return err1
}
return err2
if err2 != nil {
return err2
}
return err3
}

func (l *UAPIListener) Addr() net.Addr {
Expand Down Expand Up @@ -94,15 +100,25 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) {
return nil, err
}

uapi.inotifyRWCancel, err = rwcancel.NewRWCancel(uapi.inotifyFd)
if err != nil {
unix.Close(uapi.inotifyFd)
return nil, err
}

go func(l *UAPIListener) {
var buff [4096]byte
var buff [0]byte
for {
// start with lstat to avoid race condition
if _, err := os.Lstat(socketPath); os.IsNotExist(err) {
l.connErr <- err
return
}
unix.Read(uapi.inotifyFd, buff[:])
_, err := uapi.inotifyRWCancel.Read(buff[:])
if err != nil {
l.connErr <- err
return
}
}
}(uapi)

Expand Down

0 comments on commit 7e10ebe

Please sign in to comment.