From b5bad585066e0b86a45515cf7c638f00d88f18e2 Mon Sep 17 00:00:00 2001 From: Marten Gartner Date: Thu, 5 Sep 2024 20:53:52 +0200 Subject: [PATCH 1/4] Support SockOptInt on all platforms --- private/underlay/sockctrl/sockctrl.go | 7 ++- private/underlay/sockctrl/sockctrl_windows.go | 44 +++++++++++++++++++ private/underlay/sockctrl/sockopt.go | 4 ++ private/underlay/sockctrl/sockopt_windows.go | 40 +++++++++++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 private/underlay/sockctrl/sockctrl_windows.go create mode 100644 private/underlay/sockctrl/sockopt_windows.go diff --git a/private/underlay/sockctrl/sockctrl.go b/private/underlay/sockctrl/sockctrl.go index 2faeef054a..ad47bbe3a0 100644 --- a/private/underlay/sockctrl/sockctrl.go +++ b/private/underlay/sockctrl/sockctrl.go @@ -12,11 +12,10 @@ // 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 +// +build !windows -// This version of sockctrl is for Go versions >= 1.9, where the socket FDs are -// accessible via RawConn.Control(). package sockctrl import ( diff --git a/private/underlay/sockctrl/sockctrl_windows.go b/private/underlay/sockctrl/sockctrl_windows.go new file mode 100644 index 0000000000..aca6d8e56d --- /dev/null +++ b/private/underlay/sockctrl/sockctrl_windows.go @@ -0,0 +1,44 @@ +// Copyright 2017 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 +// +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 +} diff --git a/private/underlay/sockctrl/sockopt.go b/private/underlay/sockctrl/sockopt.go index cbf9339f06..bc0bd76a04 100644 --- a/private/underlay/sockctrl/sockopt.go +++ b/private/underlay/sockctrl/sockopt.go @@ -12,6 +12,10 @@ // 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 +// +build !windows + package sockctrl import ( diff --git a/private/underlay/sockctrl/sockopt_windows.go b/private/underlay/sockctrl/sockopt_windows.go new file mode 100644 index 0000000000..a425a0c554 --- /dev/null +++ b/private/underlay/sockctrl/sockopt_windows.go @@ -0,0 +1,40 @@ +// Copyright 2017 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 +// +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) + }) +} From 23207a15ee0970a7bc5b2fb714e604f2a881cd20 Mon Sep 17 00:00:00 2001 From: Marten Gartner Date: Fri, 6 Sep 2024 10:57:11 +0200 Subject: [PATCH 2/4] Remove obsolete build directive, use go:build --- private/underlay/sockctrl/sockctrl.go | 1 - private/underlay/sockctrl/sockctrl_windows.go | 1 - private/underlay/sockctrl/sockopt.go | 1 - private/underlay/sockctrl/sockopt_windows.go | 1 - 4 files changed, 4 deletions(-) diff --git a/private/underlay/sockctrl/sockctrl.go b/private/underlay/sockctrl/sockctrl.go index ad47bbe3a0..af9dc41692 100644 --- a/private/underlay/sockctrl/sockctrl.go +++ b/private/underlay/sockctrl/sockctrl.go @@ -14,7 +14,6 @@ // In Windows, SetSockOptInt and GetSockOptInt require syscall.Handle instead of int. //go:build !windows -// +build !windows package sockctrl diff --git a/private/underlay/sockctrl/sockctrl_windows.go b/private/underlay/sockctrl/sockctrl_windows.go index aca6d8e56d..61f7bf53b1 100644 --- a/private/underlay/sockctrl/sockctrl_windows.go +++ b/private/underlay/sockctrl/sockctrl_windows.go @@ -14,7 +14,6 @@ // In Windows, SetSockOptInt and GetSockOptInt require syscall.Handle instead of int. //go:build windows -// +build windows package sockctrl diff --git a/private/underlay/sockctrl/sockopt.go b/private/underlay/sockctrl/sockopt.go index bc0bd76a04..d5b56e5cfc 100644 --- a/private/underlay/sockctrl/sockopt.go +++ b/private/underlay/sockctrl/sockopt.go @@ -14,7 +14,6 @@ // In Windows, SetSockOptInt and GetSockOptInt require syscall.Handle instead of int. //go:build !windows -// +build !windows package sockctrl diff --git a/private/underlay/sockctrl/sockopt_windows.go b/private/underlay/sockctrl/sockopt_windows.go index a425a0c554..fff1266bee 100644 --- a/private/underlay/sockctrl/sockopt_windows.go +++ b/private/underlay/sockctrl/sockopt_windows.go @@ -14,7 +14,6 @@ // In Windows, SetSockOptInt and GetSockOptInt require syscall.Handle instead of int. //go:build windows -// +build windows package sockctrl From a4545c46e4ff2056f98908e8e464d5e8df75a92e Mon Sep 17 00:00:00 2001 From: Marten Gartner Date: Sat, 7 Sep 2024 22:11:32 +0200 Subject: [PATCH 3/4] Update copyright of windows sockopts files --- private/underlay/sockctrl/sockctrl_windows.go | 2 +- private/underlay/sockctrl/sockopt_windows.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/private/underlay/sockctrl/sockctrl_windows.go b/private/underlay/sockctrl/sockctrl_windows.go index 61f7bf53b1..81fd84c7bb 100644 --- a/private/underlay/sockctrl/sockctrl_windows.go +++ b/private/underlay/sockctrl/sockctrl_windows.go @@ -1,4 +1,4 @@ -// Copyright 2017 ETH Zurich +// 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. diff --git a/private/underlay/sockctrl/sockopt_windows.go b/private/underlay/sockctrl/sockopt_windows.go index fff1266bee..8a9ceb07b8 100644 --- a/private/underlay/sockctrl/sockopt_windows.go +++ b/private/underlay/sockctrl/sockopt_windows.go @@ -1,4 +1,4 @@ -// Copyright 2017 ETH Zurich +// 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. From ed4975ed05632f6ce2012d215bb6bf064167591a Mon Sep 17 00:00:00 2001 From: Marten Gartner Date: Sat, 7 Sep 2024 22:18:40 +0200 Subject: [PATCH 4/4] Ran make gazelle to fix CI --- private/underlay/sockctrl/BUILD.bazel | 48 ++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/private/underlay/sockctrl/BUILD.bazel b/private/underlay/sockctrl/BUILD.bazel index 67e19ae27d..f1d0dfe0db 100644 --- a/private/underlay/sockctrl/BUILD.bazel +++ b/private/underlay/sockctrl/BUILD.bazel @@ -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": [], + }), )