Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snet: fix UDP port selection on Windows #4656

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 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 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 @@ -190,7 +188,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 @@ -208,12 +206,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)
}