Skip to content

Commit

Permalink
Feature: support mark on FreeBSD & OpenBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu committed Dec 1, 2022
1 parent 35f6888 commit 8d3c28a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
33 changes: 33 additions & 0 deletions component/dialer/sockopt_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dialer

import (
"net"
"syscall"

"golang.org/x/sys/unix"
)

func setSocketOptions(network, address string, c syscall.RawConn, opts *Options) (err error) {
if opts == nil || !isTCPSocket(network) && !isUDPSocket(network) {
return
}

var innerErr error
err = c.Control(func(fd uintptr) {
host, _, _ := net.SplitHostPort(address)
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
return
}

if opts.RoutingMark != 0 {
if innerErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_USER_COOKIE, opts.RoutingMark); innerErr != nil {
return
}
}
})

if innerErr != nil {
err = innerErr
}
return
}
33 changes: 33 additions & 0 deletions component/dialer/sockopt_openbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dialer

import (
"net"
"syscall"

"golang.org/x/sys/unix"
)

func setSocketOptions(network, address string, c syscall.RawConn, opts *Options) (err error) {
if opts == nil || !isTCPSocket(network) && !isUDPSocket(network) {
return
}

var innerErr error
err = c.Control(func(fd uintptr) {
host, _, _ := net.SplitHostPort(address)
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
return
}

if opts.RoutingMark != 0 {
if innerErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RTABLE, opts.RoutingMark); innerErr != nil {
return
}
}
})

if innerErr != nil {
err = innerErr
}
return
}
2 changes: 1 addition & 1 deletion component/dialer/sockopt_others.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux && !darwin && !windows
//go:build !unix && !windows

package dialer

Expand Down

0 comments on commit 8d3c28a

Please sign in to comment.