Skip to content

Commit

Permalink
aghnet: setsockopt
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Aug 16, 2021
1 parent 394c2f6 commit f9871a4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
7 changes: 2 additions & 5 deletions internal/aghnet/dhcp_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ func discover4(iface *net.Interface, dstAddr *net.UDPAddr, hostname string) (ok
// It's also known that listening on the specified interface's address
// ignores broadcasted packets when reading.
var c net.PacketConn
if c, err = net.ListenPacket("udp4", ":68"); err != nil {
if c, err = ListenPacketReusable("udp4", ":68"); err != nil {
return false, fmt.Errorf("couldn't listen on :68: %w", err)
}
defer func() { err = errors.WithDeferred(err, c.Close()) }()

// Send to resolved broadcast.
// Send to broadcast.
if _, err = c.WriteTo(req.ToBytes(), dstAddr); err != nil {
return false, fmt.Errorf("couldn't send a packet to %s: %w", dstAddr, err)
}
Expand Down Expand Up @@ -154,9 +154,6 @@ func tryConn4(req *dhcpv4.DHCPv4, c net.PacketConn, iface *net.Interface) (ok, n

b := make([]byte, 1500)
n, _, err := c.ReadFrom(b)
if n > 0 {
log.Debug("received %d bytes: %v", n, b)
}
if err != nil {
if isTimeout(err) {
log.Debug("dhcpv4: didn't receive dhcp response")
Expand Down
6 changes: 6 additions & 0 deletions internal/aghnet/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,9 @@ func IfaceDNSIPAddrs(

return addrs, nil
}

// ListenPacketReusable announces on the local network address additionally
// configuring the socket to have a reusable binding.
func ListenPacketReusable(network, address string) (c net.PacketConn, err error) {
return listenPacketReusable(network, address)
}
30 changes: 30 additions & 0 deletions internal/aghnet/interfaces_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build linux
// +build linux

package aghnet

import (
"context"
"net"
"syscall"
)

// reuseAddrControl is the function to be set to net.ListenConfig.Control. It
// configures the socket to have a reusable port binding.
func reuseAddrControl(network, address string, c syscall.RawConn) (err error) {
cerr := c.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
})
if cerr != nil {
return cerr
}

return err
}

func listenPacketReusable(network, address string) (c net.PacketConn, err error) {
var lc net.ListenConfig
lc.Control = reuseAddrControl

return lc.ListenPacket(context.Background(), network, address)
}
34 changes: 34 additions & 0 deletions internal/aghnet/interfaces_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build aix || darwin || dragonfly || freebsd || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd netbsd openbsd solaris

package aghnet

import (
"context"
"net"
"os"
"syscall"
)

// reuseAddrCtrl is the function to be set to net.ListenConfig.Control. It
// configures the socket to have a reusable port binding.
func reuseAddrCtrl(network, address string, c syscall.RawConn) (err error) {
cerr := c.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
if err != nil {
err = os.NewSyscallError("setsockopt", err)
}
})
if cerr != nil {
return cerr
}

return err
}

func listenPacketReusable(network, address string) (c net.PacketConn, err error) {
var lc net.ListenConfig
lc.Control = reuseAddrCtrl

return lc.ListenPacket(context.Background(), network, address)
}
14 changes: 14 additions & 0 deletions internal/aghnet/interfaces_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build windows
// +build windows

package aghnet

import (
"net"

"github.com/AdguardTeam/AdGuardHome/internal/aghos"
)

func listenPacketReusable(network, address string) (c net.PacketConn, err error) {
return nil, aghos.Unsupported("listening packet reusable")
}

0 comments on commit f9871a4

Please sign in to comment.