kdhcp/server/ifi_linux.go

42 lines
562 B
Go

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