Skip to content

Commit

Permalink
snet: test for Windows-specific error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
lschulz committed Dec 7, 2024
1 parent a118737 commit 8eeea5f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
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)
}

0 comments on commit 8eeea5f

Please sign in to comment.