mirror of
https://github.com/safing/portmaster.git
synced 2026-12-04 17:43:11 +00:00
0761ba7b1e
Portmaster pre-authenticated a randomly chosen local port for its own DNS queries before binding it. When the port could not be bound, for example because it lies in a Hyper-V excluded port range on Windows or is held by another process, the query failed and counted against the resolver. Plain DNS now binds a socket first, with a port chosen by the OS, and pre-authenticates that port before sending. DNS over TCP and TLS retry with another port when binding fails, and the failed port is skipped by port selection for an hour. A bind failure no longer counts against the resolver or the network state, as nothing left the host. Pre-authentication entries that were never consumed are removed again. Co-authored-by: DannyA <11684554+dannyagostinelli@users.noreply.github.com>
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package netutils
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
var errTest = errors.New("test error")
|
|
|
|
func TestIsLocalBindError(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Occupy a UDP port, then try to dial from that same port.
|
|
held, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("failed to listen: %s", err)
|
|
}
|
|
defer func() { _ = held.Close() }()
|
|
heldAddr, ok := held.LocalAddr().(*net.UDPAddr)
|
|
if !ok {
|
|
t.Fatalf("unexpected local address type %T", held.LocalAddr())
|
|
}
|
|
heldPort := heldAddr.Port
|
|
|
|
dialer := &net.Dialer{
|
|
LocalAddr: &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: heldPort},
|
|
}
|
|
conn, err := dialer.Dial("udp4", "127.0.0.1:53")
|
|
if err == nil {
|
|
_ = conn.Close()
|
|
t.Fatalf("expected dialing from occupied port %d to fail", heldPort)
|
|
}
|
|
if !IsLocalBindError(err) {
|
|
t.Errorf("expected a bind error, got: %v", err)
|
|
}
|
|
|
|
// Other errors are not bind errors.
|
|
if IsLocalBindError(nil) {
|
|
t.Error("nil must not be a bind error")
|
|
}
|
|
if IsLocalBindError(errTest) {
|
|
t.Error("plain error must not be a bind error")
|
|
}
|
|
connectErr := &net.OpError{
|
|
Op: "dial",
|
|
Net: "udp",
|
|
Err: &os.SyscallError{Syscall: "connect", Err: errTest},
|
|
}
|
|
if IsLocalBindError(connectErr) {
|
|
t.Error("connect error must not be a bind error")
|
|
}
|
|
}
|