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 +}