From 515908773fbaa10e34e00b630afb95476c0ba879 Mon Sep 17 00:00:00 2001 From: snow flurry Date: Mon, 21 Mar 2022 19:58:54 -0700 Subject: [PATCH] Further "rebase" tun_netbsd against tun_openbsd Cleaning out some stuff from the older version of wireguard-go. --- tun/tun_netbsd.go | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/tun/tun_netbsd.go b/tun/tun_netbsd.go index a5bc9db..0a24ed0 100644 --- a/tun/tun_netbsd.go +++ b/tun/tun_netbsd.go @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: MIT * - * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved. + * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. */ package tun @@ -8,7 +8,6 @@ package tun import ( "errors" "fmt" - "io/ioutil" "net" "os" "sync" @@ -26,11 +25,7 @@ type ifreq_mtu struct { Pad0 [12]byte } -const ( - _TUNGIFHEAD = 0x40047441 - _TUNSIFMODE = 0x80047458 - _TUNSIFHEAD = 0x80047442 -) +const _TUNSIFHEAD = 0x80047442 type NativeTun struct { 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) { ifIndex := -1 if name != "tun" { @@ -131,7 +116,7 @@ func CreateTUN(name string, mtu int) (Device, error) { if ifIndex != -1 { tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0) } 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) if err == nil || !errors.Is(err, syscall.EBUSY) { break @@ -175,7 +160,7 @@ func CreateTUN(name string, mtu int) (Device, error) { if err == nil && name == "tun" { fname := os.Getenv("WG_TUN_NAME_FILE") 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 { var err1, err2 error - tun.closeOnce.Do(func () { + tun.closeOnce.Do(func() { err1 = tun.tunFile.Close() if tun.routeSocket != -1 { unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR) @@ -315,7 +300,6 @@ func (tun *NativeTun) setMTU(n int) error { unix.SOCK_DGRAM, 0, ) - if err != nil { return err } @@ -350,7 +334,6 @@ func (tun *NativeTun) MTU() (int, error) { unix.SOCK_DGRAM, 0, ) - if err != nil { return 0, err }