Further "rebase" tun_netbsd against tun_openbsd

Cleaning out some stuff from the older version of wireguard-go.
This commit is contained in:
snow flurry 2022-03-21 19:58:54 -07:00
parent 470f2f0248
commit 515908773f
1 changed files with 5 additions and 22 deletions

View File

@ -1,6 +1,6 @@
/* SPDX-License-Identifier: MIT /* SPDX-License-Identifier: MIT
* *
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved. * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
*/ */
package tun package tun
@ -8,7 +8,6 @@ package tun
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"sync" "sync"
@ -26,11 +25,7 @@ type ifreq_mtu struct {
Pad0 [12]byte Pad0 [12]byte
} }
const ( const _TUNSIFHEAD = 0x80047442
_TUNGIFHEAD = 0x40047441
_TUNSIFMODE = 0x80047458
_TUNSIFHEAD = 0x80047442
)
type NativeTun struct { type NativeTun struct {
name string name string
@ -106,16 +101,6 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
} }
} }
func errorIsEBUSY(err error) bool {
if pe, ok := err.(*os.PathError); ok {
err = pe.Err
}
if errno, ok := err.(syscall.Errno); ok && errno == syscall.EBUSY {
return true
}
return false
}
func CreateTUN(name string, mtu int) (Device, error) { func CreateTUN(name string, mtu int) (Device, error) {
ifIndex := -1 ifIndex := -1
if name != "tun" { if name != "tun" {
@ -131,7 +116,7 @@ func CreateTUN(name string, mtu int) (Device, error) {
if ifIndex != -1 { if ifIndex != -1 {
tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0) tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0)
} else { } else {
for ifIndex = 0; ifIndex < 256; ifIndex += 1 { for ifIndex = 0; ifIndex < 256; ifIndex++ {
tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0) tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0)
if err == nil || !errors.Is(err, syscall.EBUSY) { if err == nil || !errors.Is(err, syscall.EBUSY) {
break break
@ -175,7 +160,7 @@ func CreateTUN(name string, mtu int) (Device, error) {
if err == nil && name == "tun" { if err == nil && name == "tun" {
fname := os.Getenv("WG_TUN_NAME_FILE") fname := os.Getenv("WG_TUN_NAME_FILE")
if fname != "" { if fname != "" {
ioutil.WriteFile(fname, []byte(tun.(*NativeTun).name+"\n"), 0400) os.WriteFile(fname, []byte(tun.(*NativeTun).name+"\n"), 0o400)
} }
} }
@ -289,7 +274,7 @@ func (tun *NativeTun) Flush() error {
func (tun *NativeTun) Close() error { func (tun *NativeTun) Close() error {
var err1, err2 error var err1, err2 error
tun.closeOnce.Do(func () { tun.closeOnce.Do(func() {
err1 = tun.tunFile.Close() err1 = tun.tunFile.Close()
if tun.routeSocket != -1 { if tun.routeSocket != -1 {
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR) unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
@ -315,7 +300,6 @@ func (tun *NativeTun) setMTU(n int) error {
unix.SOCK_DGRAM, unix.SOCK_DGRAM,
0, 0,
) )
if err != nil { if err != nil {
return err return err
} }
@ -350,7 +334,6 @@ func (tun *NativeTun) MTU() (int, error) {
unix.SOCK_DGRAM, unix.SOCK_DGRAM,
0, 0,
) )
if err != nil { if err != nil {
return 0, err return 0, err
} }