From 282beeddee6fc8617c7ebc16ee9490dbdae5d8eb Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Tue, 2 May 2023 16:40:12 -0700 Subject: [PATCH] Binding to an interface relies on Linux right now. --- server/ifi.go | 43 +++++++++++-------------------------------- server/ifi_linux.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 32 deletions(-) create mode 100644 server/ifi_linux.go diff --git a/server/ifi.go b/server/ifi.go index b70f388..50360b7 100644 --- a/server/ifi.go +++ b/server/ifi.go @@ -1,39 +1,18 @@ +// +build: !linux + package server import ( + "errors" + "fmt" "net" - "syscall" + "runtime" ) -func BindInterface(ip net.IP, port int, dev string) (net.PacketConn, error) { - if port == 0 { - port = 67 - } - - udpAddr := &net.UDPAddr{ - IP: ip, - Port: port, - } - - conn, err := net.ListenUDP("udp4", udpAddr) - if err != nil { - return nil, err - } - - sysconn, err := conn.SyscallConn() - if err != nil { - conn.Close() - return nil, err - } - - sysconn.Control(func(fd uintptr) { - err = syscall.BindToDevice(int(fd), dev) - }) - - if err != nil { - conn.Close() - return nil, err - } - - return conn, nil +func init() { + panic(fmt.Sprintf("%s is not a supported operating system", runtime.GOOS)) +} + +func BindInterface(ip net.IP, port int, dev string) (net.PacketConn, error) { + return nil, errors.New("server: %s does not support binding to an interface") } diff --git a/server/ifi_linux.go b/server/ifi_linux.go new file mode 100644 index 0000000..2a45433 --- /dev/null +++ b/server/ifi_linux.go @@ -0,0 +1,41 @@ +// +build: linux + +package server + +import ( + "net" + "syscall" +) + +func BindInterface(ip net.IP, port int, dev string) (net.PacketConn, error) { + if port == 0 { + port = 67 + } + + udpAddr := &net.UDPAddr{ + IP: ip, + Port: port, + } + + conn, err := net.ListenUDP("udp4", udpAddr) + if err != nil { + return nil, err + } + + sysconn, err := conn.SyscallConn() + if err != nil { + conn.Close() + return nil, err + } + + sysconn.Control(func(fd uintptr) { + err = syscall.BindToDevice(int(fd), dev) + }) + + if err != nil { + conn.Close() + return nil, err + } + + return conn, nil +}