Skip to content

Commit

Permalink
underlay: support SockOptInt on all platforms incl Windows (#4610)
Browse files Browse the repository at this point in the history
There is an issue that `syscall.GetsockoptInt` and `syscall.SetsockoptInt` have different signatures on windows than on all other platforms. One fix is to have extra windows code to support this. In the end it would be great to have Windows support for all SCION services, too. The issue popped up when building the router, not sure if it affects other services.
  • Loading branch information
martenwallewein authored Sep 9, 2024
1 parent e88ddce commit 79e7080
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 5 deletions.
48 changes: 47 additions & 1 deletion private/underlay/sockctrl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,55 @@ go_library(
name = "go_default_library",
srcs = [
"sockctrl.go",
"sockctrl_windows.go",
"sockopt.go",
"sockopt_windows.go",
],
importpath = "github.com/scionproto/scion/private/underlay/sockctrl",
visibility = ["//visibility:public"],
deps = ["//pkg/private/serrors:go_default_library"],
deps = select({
"@io_bazel_rules_go//go/platform:aix": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:android": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:darwin": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:dragonfly": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:freebsd": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:illumos": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:ios": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:js": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:linux": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:netbsd": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:openbsd": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:plan9": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:solaris": [
"//pkg/private/serrors:go_default_library",
],
"@io_bazel_rules_go//go/platform:windows": [
"//pkg/private/serrors:go_default_library",
],
"//conditions:default": [],
}),
)
6 changes: 2 additions & 4 deletions private/underlay/sockctrl/sockctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.9
// +build go1.9
// In Windows, SetSockOptInt and GetSockOptInt require syscall.Handle instead of int.
//go:build !windows

// This version of sockctrl is for Go versions >= 1.9, where the socket FDs are
// accessible via RawConn.Control().
package sockctrl

import (
Expand Down
43 changes: 43 additions & 0 deletions private/underlay/sockctrl/sockctrl_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 ETH Zurich
//
// 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.

// In Windows, SetSockOptInt and GetSockOptInt require syscall.Handle instead of int.
//go:build windows

package sockctrl

import (
"net"
"syscall"

"github.com/scionproto/scion/pkg/private/serrors"
)

func SockControl(c *net.UDPConn, f func(syscall.Handle) error) error {
rawConn, err := c.SyscallConn()
if err != nil {
return serrors.Wrap("sockctrl: error accessing raw connection", err)
}
var ctrlErr error
err = rawConn.Control(func(fd uintptr) {
ctrlErr = f(syscall.Handle(fd))
})
if err != nil {
return serrors.Wrap("sockctrl: RawConn.Control error", err)
}
if ctrlErr != nil {
return serrors.Wrap("sockctrl: control function error", ctrlErr)
}
return nil
}
3 changes: 3 additions & 0 deletions private/underlay/sockctrl/sockopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// In Windows, SetSockOptInt and GetSockOptInt require syscall.Handle instead of int.
//go:build !windows

package sockctrl

import (
Expand Down
39 changes: 39 additions & 0 deletions private/underlay/sockctrl/sockopt_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 ETH Zurich
//
// 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.

// In Windows, SetSockOptInt and GetSockOptInt require syscall.Handle instead of int.
//go:build windows

package sockctrl

import (
"net"
"syscall"
)

func GetsockoptInt(c *net.UDPConn, level, opt int) (int, error) {
var val int
err := SockControl(c, func(fd syscall.Handle) error {
var err error
val, err = syscall.GetsockoptInt(fd, level, opt)
return err
})
return val, err
}

func SetsockoptInt(c *net.UDPConn, level, opt, value int) error {
return SockControl(c, func(fd syscall.Handle) error {
return syscall.SetsockoptInt(fd, level, opt, value)
})
}

0 comments on commit 79e7080

Please sign in to comment.