summaryrefslogtreecommitdiffhomepage
path: root/net/udprelay/server_linux.go
blob: 3a734f9c7550570edbb4682e0bfc6a2c390eca42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

//go:build linux

package udprelay

import (
	"net"
	"syscall"

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

func trySetReusePort(_ string, _ string, c syscall.RawConn) {
	c.Control(func(fd uintptr) {
		unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
	})
}

func isReusableSocket(uc *net.UDPConn) bool {
	rc, err := uc.SyscallConn()
	if err != nil {
		return false
	}
	var reusable bool
	rc.Control(func(fd uintptr) {
		val, err := unix.GetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT)
		if err == nil && val == 1 {
			reusable = true
		}
	})
	return reusable
}