mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-06 20:10:11 +02:00
5f903d1fbf
- syscall is deprecated and is not kept up-to-date - each OS is inherently different hence the syscall being deprecated
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
//go:build linux || darwin
|
|
|
|
package tcp
|
|
|
|
import (
|
|
"net/netip"
|
|
"time"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// fileDescriptor is a platform-independent type for socket file descriptors.
|
|
type fileDescriptor int
|
|
|
|
func socket(domain int, typ int, proto int) (fd int, err error) {
|
|
return unix.Socket(domain, typ, proto)
|
|
}
|
|
|
|
func closeSocket(fd int) error {
|
|
return unix.Close(fd)
|
|
}
|
|
|
|
func sendTo(fd fileDescriptor, p []byte, flags int, to unix.Sockaddr) (err error) {
|
|
return unix.Sendto(int(fd), p, flags, to)
|
|
}
|
|
|
|
func setSocketTimeout(fd fileDescriptor, timeout time.Duration) (err error) {
|
|
timeval := unix.NsecToTimeval(timeout.Nanoseconds())
|
|
return unix.SetsockoptTimeval(int(fd), unix.SOL_SOCKET, unix.SO_RCVTIMEO, &timeval)
|
|
}
|
|
|
|
func recvFrom(fd fileDescriptor, p []byte, flags int) (n int, from unix.Sockaddr, err error) {
|
|
return unix.Recvfrom(int(fd), p, flags)
|
|
}
|
|
|
|
func setNonBlock(fd int) error {
|
|
return unix.SetNonblock(fd, true)
|
|
}
|
|
|
|
func makeSockAddr(addr netip.AddrPort) unix.Sockaddr {
|
|
if addr.Addr().Is4() {
|
|
return &unix.SockaddrInet4{
|
|
Port: int(addr.Port()),
|
|
Addr: addr.Addr().As4(),
|
|
}
|
|
}
|
|
return &unix.SockaddrInet6{
|
|
Port: int(addr.Port()),
|
|
Addr: addr.Addr().As16(),
|
|
}
|
|
}
|