mirror of
https://github.com/fumiama/water
synced 2025-12-24 12:12:32 +08:00
macOS: Allow to specify a name for the TUN interface (#79)
* macOS: Allow to specify a name for the TUN interface * Update syscalls_darwin.go Co-Authored-By: Song Gao <song@gao.io> * Add missing imports * Update syscalls_darwin.go Co-authored-by: Song Gao <song@gao.io>
This commit is contained in:
@@ -17,9 +17,11 @@ const (
|
||||
// Currently it is not possible to set the interface name in macOS.
|
||||
type PlatformSpecificParams struct {
|
||||
// Name is the name for the interface to be used.
|
||||
// e.g. "tap0"
|
||||
// Only valid if using TunTapOSXDriver.
|
||||
//
|
||||
// For TunTapOSXDriver, it should be something like "tap0".
|
||||
// For SystemDriver, the name should match `utun[0-9]+`, e.g. utun233
|
||||
Name string
|
||||
|
||||
// Driver should be set if an alternative driver is desired
|
||||
// e.g. TunTapOSXDriver
|
||||
Driver MacOSDriverProvider
|
||||
|
||||
@@ -4,7 +4,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
@@ -75,6 +77,19 @@ func openDevSystem(config Config) (ifce *Interface, err error) {
|
||||
if config.DeviceType != TUN {
|
||||
return nil, errors.New("only tun is implemented for SystemDriver, use TunTapOSXDriver for tap")
|
||||
}
|
||||
|
||||
ifIndex := -1
|
||||
if config.Name != "" {
|
||||
const utunPrefix = "utun"
|
||||
if !strings.HasPrefix(config.Name, utunPrefix) {
|
||||
return nil, fmt.Errorf("Interface name must be utun[0-9]+")
|
||||
}
|
||||
ifIndex, err = strconv.Atoi(config.Name[len(utunPrefix):])
|
||||
if err != nil || ifIndex < 0 || ifIndex > math.MaxUint32-1 {
|
||||
return nil, fmt.Errorf("Interface name must be utun[0-9]+")
|
||||
}
|
||||
}
|
||||
|
||||
var fd int
|
||||
// Supposed to be socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), but ...
|
||||
//
|
||||
@@ -106,7 +121,7 @@ func openDevSystem(config Config) (ifce *Interface, err error) {
|
||||
ssSysaddr: 2,
|
||||
|
||||
scID: ctlInfo.ctlID,
|
||||
scUnit: 0,
|
||||
scUnit: uint32(ifIndex) + 1,
|
||||
})
|
||||
if _, _, errno := syscall.RawSyscall(syscall.SYS_CONNECT, uintptr(fd), uintptr(addrP), uintptr(sockaddrCtlSize)); errno != 0 {
|
||||
err = errno
|
||||
|
||||
Reference in New Issue
Block a user