aboutsummaryrefslogtreecommitdiffstats
path: root/tun/tun_openbsd.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-23 02:10:54 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-23 03:58:27 +0200
commit0a63188afab1dd49380f916963307f9b2efdcac1 (patch)
tree37dac3b29a2f89a99e1df1cafd1cbbc2a0e803f0 /tun/tun_openbsd.go
parentAvoid sticky sockets on Android (diff)
downloadwireguard-go-0a63188afab1dd49380f916963307f9b2efdcac1.tar.xz
wireguard-go-0a63188afab1dd49380f916963307f9b2efdcac1.zip
Move tun to subpackage
Diffstat (limited to '')
-rw-r--r--tun/tun_openbsd.go (renamed from tun_openbsd.go)41
1 files changed, 20 insertions, 21 deletions
diff --git a/tun_openbsd.go b/tun/tun_openbsd.go
index 932404e..74b25e5 100644
--- a/tun_openbsd.go
+++ b/tun/tun_openbsd.go
@@ -3,10 +3,10 @@
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
-package main
+package tun
import (
- "./rwcancel"
+ "../rwcancel"
"errors"
"fmt"
"golang.org/x/net/ipv6"
@@ -27,7 +27,7 @@ type ifreq_mtu struct {
const _TUNSIFMODE = 0x8004745d
-type NativeTun struct {
+type nativeTun struct {
name string
fd *os.File
rwcancel *rwcancel.RWCancel
@@ -36,7 +36,7 @@ type NativeTun struct {
routeSocket int
}
-func (tun *NativeTun) RoutineRouteListener(tunIfindex int) {
+func (tun *nativeTun) routineRouteListener(tunIfindex int) {
var (
statusUp bool
statusMTU int
@@ -100,7 +100,7 @@ func errorIsEBUSY(err error) bool {
return false
}
-func CreateTUN(name string) (TUNDevice, error) {
+func CreateTUN(name string, mtu int) (TUNDevice, error) {
ifIndex := -1
if name != "tun" {
_, err := fmt.Sscanf(name, "tun%d", &ifIndex)
@@ -140,21 +140,21 @@ func CreateTUN(name string) (TUNDevice, error) {
return nil, fmt.Errorf("error %s", errno.Error())
}
- tun, err := CreateTUNFromFile(tunfile)
+ tun, err := CreateTUNFromFile(tunfile, mtu)
if err == nil && name == "tun" {
fname := os.Getenv("WG_TUN_NAME_FILE")
if fname != "" {
- ioutil.WriteFile(fname, []byte(tun.(*NativeTun).name+"\n"), 0400)
+ ioutil.WriteFile(fname, []byte(tun.(*nativeTun).name+"\n"), 0400)
}
}
return tun, err
}
-func CreateTUNFromFile(file *os.File) (TUNDevice, error) {
+func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
- tun := &NativeTun{
+ tun := &nativeTun{
fd: file,
events: make(chan TUNEvent, 10),
errors: make(chan error, 1),
@@ -190,10 +190,9 @@ func CreateTUNFromFile(file *os.File) (TUNDevice, error) {
return nil, err
}
- go tun.RoutineRouteListener(tunIfindex)
+ go tun.routineRouteListener(tunIfindex)
- // set default MTU
- err = tun.setMTU(DefaultMTU)
+ err = tun.setMTU(mtu)
if err != nil {
tun.Close()
return nil, err
@@ -202,7 +201,7 @@ func CreateTUNFromFile(file *os.File) (TUNDevice, error) {
return tun, nil
}
-func (tun *NativeTun) Name() (string, error) {
+func (tun *nativeTun) Name() (string, error) {
gostat, err := tun.fd.Stat()
if err != nil {
tun.name = ""
@@ -213,15 +212,15 @@ func (tun *NativeTun) Name() (string, error) {
return tun.name, nil
}
-func (tun *NativeTun) File() *os.File {
+func (tun *nativeTun) File() *os.File {
return tun.fd
}
-func (tun *NativeTun) Events() chan TUNEvent {
+func (tun *nativeTun) Events() chan TUNEvent {
return tun.events
}
-func (tun *NativeTun) doRead(buff []byte, offset int) (int, error) {
+func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
select {
case err := <-tun.errors:
return 0, err
@@ -235,7 +234,7 @@ func (tun *NativeTun) doRead(buff []byte, offset int) (int, error) {
}
}
-func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
+func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
for {
n, err := tun.doRead(buff, offset)
if err == nil || !rwcancel.ErrorIsEAGAIN(err) {
@@ -247,7 +246,7 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
}
}
-func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
+func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
// reserve space for header
@@ -270,7 +269,7 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
return tun.fd.Write(buff)
}
-func (tun *NativeTun) Close() error {
+func (tun *nativeTun) Close() error {
var err3 error
err1 := tun.rwcancel.Cancel()
err2 := tun.fd.Close()
@@ -290,7 +289,7 @@ func (tun *NativeTun) Close() error {
return err3
}
-func (tun *NativeTun) setMTU(n int) error {
+func (tun *nativeTun) setMTU(n int) error {
// open datagram socket
var fd int
@@ -327,7 +326,7 @@ func (tun *NativeTun) setMTU(n int) error {
return nil
}
-func (tun *NativeTun) MTU() (int, error) {
+func (tun *nativeTun) MTU() (int, error) {
// open datagram socket
fd, err := unix.Socket(