kdhcp/server/ifi_linux.go

42 lines
669 B
Go
Raw Normal View History

2023-05-02 23:46:04 +00:00
//go:build linux
package server
import (
2023-05-15 13:09:12 +00:00
"fmt"
"net"
"syscall"
2023-05-15 13:09:12 +00:00
log "git.wntrmute.dev/kyle/goutils/log"
)
func BindInterface(ip net.IP, port int, dev string) (net.PacketConn, error) {
2023-05-15 13:09:12 +00:00
udpAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf(":%d", port))
if err != nil {
return nil, err
}
2023-05-15 13:09:12 +00:00
log.Debugf("listen on %s", udpAddr)
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
}