kdhcp/server/ifi.go

40 lines
544 B
Go
Raw Normal View History

2023-05-02 16:27:27 +00:00
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
}