Skip to content

Commit

Permalink
snet: fix UDP port selection on Windows (#4656)
Browse files Browse the repository at this point in the history
SNET is checking the wrong error code when probing for available UDP ports.
Windows socket error codes are completely different from Linux, thus different OSes must be distinguished.
  • Loading branch information
lschulz authored Feb 28, 2025
1 parent 32ca823 commit 588788f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
9 changes: 8 additions & 1 deletion pkg/snet/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ go_library(
"router.go",
"scmp.go",
"snet.go",
"sock_error_posix.go",
"sock_error_windows.go",
"svcaddr.go",
"udpaddr.go",
"writer.go",
Expand All @@ -37,7 +39,12 @@ go_library(
"//private/topology:go_default_library",
"//private/topology/underlay:go_default_library",
"@com_github_gopacket_gopacket//:go_default_library",
],
] + select({
"@io_bazel_rules_go//go/platform:windows": [
"@org_golang_x_sys//windows:go_default_library",
],
"//conditions:default": [],
}),
)

go_test(
Expand Down
8 changes: 3 additions & 5 deletions pkg/snet/snet.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ package snet

import (
"context"
"errors"
"net"
"net/netip"
"syscall"

"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/log"
Expand Down Expand Up @@ -195,7 +193,7 @@ func listenUDPRange(addr *net.UDPAddr, start, end uint16) (*net.UDPConn, error)
// by longer-lived applications, e.g., server applications.
//
// Ideally we would only take a standard ephemeral range, e.g., 32768-65535,
// Unfortunately, this range was ocuppied by the old dispatcher.
// Unfortunately, this range was occupied by the old dispatcher.
// The default range for the dispatched ports is 31000-32767.
// By configuration other port ranges may be defined and restricting to the default
// range for applications may cause problems.
Expand All @@ -213,12 +211,12 @@ func listenUDPRange(addr *net.UDPAddr, start, end uint16) (*net.UDPConn, error)
if err == nil {
return pconn, nil
}
if errors.Is(err, syscall.EADDRINUSE) {
if errorIsAddrUnavailable(err) {
continue
}
return nil, err
}
return nil, serrors.Wrap("binding to port range", syscall.EADDRINUSE,
return nil, serrors.Wrap("binding to port range", ErrAddrInUse,
"start", restrictedStart, "end", end)

}
32 changes: 32 additions & 0 deletions pkg/snet/sock_error_posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 OVGU Magdeburg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !windows

package snet

import (
"errors"
"syscall"
)

const (
ErrAddrInUse = syscall.EADDRINUSE
)

// errorIsAddrUnavailable checks whether the error returned from a syscall to
// bind indicates that the requested address is not available.
func errorIsAddrUnavailable(err error) bool {
return errors.Is(err, syscall.EADDRINUSE)
}
36 changes: 36 additions & 0 deletions pkg/snet/sock_error_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 OVGU Magdeburg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build windows

package snet

import (
"errors"

"golang.org/x/sys/windows"
)

const (
ErrAddrInUse = windows.WSAEADDRINUSE
)

// errorIsAddrUnavailable checks whether the error returned from a syscall to
// bind indicates that the requested address is not available.
func errorIsAddrUnavailable(err error) bool {
// WSAEADDRINUSE is returned if another socket is bound to the same address.
// WSAEACCES is returned if another process is bound to the same address
// with exclusive access.
return errors.Is(err, windows.WSAEADDRINUSE) || errors.Is(err, windows.WSAEACCES)
}

0 comments on commit 588788f

Please sign in to comment.