diff options
author | Anatole Denis <natolumin@unverle.fr> | 2019-10-15 10:16:05 +0200 |
---|---|---|
committer | Anatole Denis <natolumin@unverle.fr> | 2019-10-15 11:19:43 +0200 |
commit | 486c8612a71ba5e1f251fbbbf08a91e3d8f77bfa (patch) | |
tree | c139f883153fe0d47502f94789eea7a5619202f4 /dhcpv4/server4/conn.go | |
parent | 7cac2b06400c7608c0552462bc34f1856917b12e (diff) |
server4: Respect listen address
NewIPv4UDPConn doesn't support listening on a specific address, only on
the wildcard address. This extends it to allow listening on an address,
and at the same time homogenizes the function signature with the
NewIPv6UDPConn server6 equivalent. It modifies NewServer() to pass the
full address given to it instead of just the port as well
Note that listening on a non-wildcard interface is seldom useful as the
socket won't receive broadcasts, so it is useless in a direct-attached
server. It can be useful in a server only used behind relays
This breaks API compatibility for NewIPv4UDPConn, which as far as I know
nobody uses (yet)
Signed-off-by: Anatole Denis <natolumin@unverle.fr>
Diffstat (limited to 'dhcpv4/server4/conn.go')
-rw-r--r-- | dhcpv4/server4/conn.go | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/dhcpv4/server4/conn.go b/dhcpv4/server4/conn.go index 0a4c73a..d62a5ac 100644 --- a/dhcpv4/server4/conn.go +++ b/dhcpv4/server4/conn.go @@ -14,7 +14,7 @@ import ( // given based on a IPv4 DGRAM socket. The UDP connection allows broadcasting. // // The interface must already be configured. -func NewIPv4UDPConn(iface string, port int) (*net.UDPConn, error) { +func NewIPv4UDPConn(iface string, addr *net.UDPAddr) (*net.UDPConn, error) { fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_UDP) if err != nil { return nil, fmt.Errorf("cannot get a UDP socket: %v", err) @@ -37,9 +37,15 @@ func NewIPv4UDPConn(iface string, port int) (*net.UDPConn, error) { return nil, fmt.Errorf("cannot bind to interface %s: %v", iface, err) } } + + if addr == nil { + addr = &net.UDPAddr{Port: dhcpv4.ServerPort} + } // Bind to the port. - if err := unix.Bind(fd, &unix.SockaddrInet4{Port: port}); err != nil { - return nil, fmt.Errorf("cannot bind to port %d: %v", port, err) + saddr := unix.SockaddrInet4{Port: addr.Port} + copy(saddr.Addr[:], addr.IP.To4()) + if err := unix.Bind(fd, &saddr); err != nil { + return nil, fmt.Errorf("cannot bind to port %d: %v", addr.Port, err) } conn, err := net.FilePacketConn(f) |