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

underlay: support SockOptInt on all platforms incl Windows #4610

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 3 additions & 4 deletions private/underlay/sockctrl/sockctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
44 changes: 44 additions & 0 deletions private/underlay/sockctrl/sockctrl_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions private/underlay/sockctrl/sockopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
40 changes: 40 additions & 0 deletions private/underlay/sockctrl/sockopt_windows.go
Original file line number Diff line number Diff line change
@@ -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)
})
}