2018-05-26 02:59:26 +02:00
|
|
|
// +build !linux android
|
2017-08-25 14:53:23 +02:00
|
|
|
|
2019-01-02 01:55:51 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 15:04:00 +02:00
|
|
|
*
|
2021-01-28 17:52:15 +01:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2018-05-03 15:04:00 +02:00
|
|
|
*/
|
|
|
|
|
2019-11-07 17:13:05 +01:00
|
|
|
package conn
|
2017-08-25 14:53:23 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2018-06-11 19:04:38 +02:00
|
|
|
"os"
|
|
|
|
"syscall"
|
2017-08-25 14:53:23 +02:00
|
|
|
)
|
|
|
|
|
2017-11-17 17:25:45 +01:00
|
|
|
/* This code is meant to be a temporary solution
|
|
|
|
* on platforms for which the sticky socket / source caching behavior
|
|
|
|
* has not yet been implemented.
|
|
|
|
*
|
|
|
|
* See conn_linux.go for an implementation on the linux platform.
|
|
|
|
*/
|
|
|
|
|
2019-03-03 05:01:06 +01:00
|
|
|
type nativeBind struct {
|
2019-10-21 13:29:57 +02:00
|
|
|
ipv4 *net.UDPConn
|
|
|
|
ipv6 *net.UDPConn
|
|
|
|
blackhole4 bool
|
|
|
|
blackhole6 bool
|
2017-11-19 00:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type NativeEndpoint net.UDPAddr
|
|
|
|
|
2019-03-03 05:01:06 +01:00
|
|
|
var _ Bind = (*nativeBind)(nil)
|
2017-11-19 00:21:58 +01:00
|
|
|
var _ Endpoint = (*NativeEndpoint)(nil)
|
|
|
|
|
|
|
|
func CreateEndpoint(s string) (Endpoint, error) {
|
|
|
|
addr, err := parseEndpoint(s)
|
2017-11-19 13:14:15 +01:00
|
|
|
return (*NativeEndpoint)(addr), err
|
2017-11-19 00:21:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-19 22:41:07 +01:00
|
|
|
func (*NativeEndpoint) ClearSrc() {}
|
2017-11-19 00:21:58 +01:00
|
|
|
|
|
|
|
func (e *NativeEndpoint) DstIP() net.IP {
|
|
|
|
return (*net.UDPAddr)(e).IP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *NativeEndpoint) SrcIP() net.IP {
|
|
|
|
return nil // not supported
|
|
|
|
}
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2017-11-19 00:21:58 +01:00
|
|
|
func (e *NativeEndpoint) DstToBytes() []byte {
|
|
|
|
addr := (*net.UDPAddr)(e)
|
2018-05-18 05:02:35 +02:00
|
|
|
out := addr.IP.To4()
|
|
|
|
if out == nil {
|
|
|
|
out = addr.IP
|
|
|
|
}
|
2017-11-19 00:21:58 +01:00
|
|
|
out = append(out, byte(addr.Port&0xff))
|
|
|
|
out = append(out, byte((addr.Port>>8)&0xff))
|
|
|
|
return out
|
|
|
|
}
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2017-11-19 00:21:58 +01:00
|
|
|
func (e *NativeEndpoint) DstToString() string {
|
|
|
|
return (*net.UDPAddr)(e).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *NativeEndpoint) SrcToString() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
func listenNet(network string, port int) (*net.UDPConn, int, error) {
|
|
|
|
conn, err := net.ListenUDP(network, &net.UDPAddr{Port: port})
|
2017-11-19 00:21:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2019-11-07 17:13:05 +01:00
|
|
|
// Retrieve port.
|
2017-11-19 00:21:58 +01:00
|
|
|
laddr := conn.LocalAddr()
|
2017-11-19 13:14:15 +01:00
|
|
|
uaddr, err := net.ResolveUDPAddr(
|
2017-11-19 00:21:58 +01:00
|
|
|
laddr.Network(),
|
|
|
|
laddr.String(),
|
|
|
|
)
|
2017-11-19 13:14:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2017-11-19 00:21:58 +01:00
|
|
|
return conn, uaddr.Port, nil
|
|
|
|
}
|
|
|
|
|
2018-06-11 19:04:38 +02:00
|
|
|
func extractErrno(err error) error {
|
|
|
|
opErr, ok := err.(*net.OpError)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
syscallErr, ok := opErr.Err.(*os.SyscallError)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return syscallErr.Err
|
|
|
|
}
|
|
|
|
|
2019-11-07 17:13:05 +01:00
|
|
|
func createBind(uport uint16) (Bind, uint16, error) {
|
2017-11-19 13:14:15 +01:00
|
|
|
var err error
|
2019-03-03 05:01:06 +01:00
|
|
|
var bind nativeBind
|
2021-02-09 18:45:12 +01:00
|
|
|
var tries int
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2021-02-09 18:45:12 +01:00
|
|
|
again:
|
2017-11-19 13:14:15 +01:00
|
|
|
port := int(uport)
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
bind.ipv4, port, err = listenNet("udp4", port)
|
2018-06-11 19:04:38 +02:00
|
|
|
if err != nil && extractErrno(err) != syscall.EAFNOSUPPORT {
|
2017-11-19 13:14:15 +01:00
|
|
|
return nil, 0, err
|
2017-11-17 17:25:45 +01:00
|
|
|
}
|
2017-11-19 13:14:15 +01:00
|
|
|
|
|
|
|
bind.ipv6, port, err = listenNet("udp6", port)
|
2021-02-09 18:45:12 +01:00
|
|
|
if uport == 0 && err != nil && extractErrno(err) == syscall.EADDRINUSE && tries < 100 {
|
|
|
|
tries++
|
|
|
|
goto again
|
|
|
|
}
|
2018-06-11 19:04:38 +02:00
|
|
|
if err != nil && extractErrno(err) != syscall.EAFNOSUPPORT {
|
2017-11-19 13:14:15 +01:00
|
|
|
bind.ipv4.Close()
|
2018-06-11 19:04:38 +02:00
|
|
|
bind.ipv4 = nil
|
2017-11-17 17:25:45 +01:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2017-11-19 13:14:15 +01:00
|
|
|
return &bind, uint16(port), nil
|
|
|
|
}
|
2017-11-17 17:25:45 +01:00
|
|
|
|
2019-03-03 05:01:06 +01:00
|
|
|
func (bind *nativeBind) Close() error {
|
2018-06-11 19:04:38 +02:00
|
|
|
var err1, err2 error
|
|
|
|
if bind.ipv4 != nil {
|
|
|
|
err1 = bind.ipv4.Close()
|
|
|
|
}
|
|
|
|
if bind.ipv6 != nil {
|
|
|
|
err2 = bind.ipv6.Close()
|
|
|
|
}
|
2017-11-19 13:14:15 +01:00
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
return err2
|
|
|
|
}
|
2019-11-07 17:13:05 +01:00
|
|
|
|
|
|
|
func (bind *nativeBind) LastMark() uint32 { return 0 }
|
2017-11-19 13:14:15 +01:00
|
|
|
|
2019-03-03 05:01:06 +01:00
|
|
|
func (bind *nativeBind) ReceiveIPv4(buff []byte) (int, Endpoint, error) {
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.ipv4 == nil {
|
|
|
|
return 0, nil, syscall.EAFNOSUPPORT
|
|
|
|
}
|
2017-11-19 13:14:15 +01:00
|
|
|
n, endpoint, err := bind.ipv4.ReadFromUDP(buff)
|
2018-05-18 05:02:35 +02:00
|
|
|
if endpoint != nil {
|
|
|
|
endpoint.IP = endpoint.IP.To4()
|
|
|
|
}
|
2017-11-19 13:14:15 +01:00
|
|
|
return n, (*NativeEndpoint)(endpoint), err
|
|
|
|
}
|
|
|
|
|
2019-03-03 05:01:06 +01:00
|
|
|
func (bind *nativeBind) ReceiveIPv6(buff []byte) (int, Endpoint, error) {
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.ipv6 == nil {
|
|
|
|
return 0, nil, syscall.EAFNOSUPPORT
|
|
|
|
}
|
2017-11-19 13:14:15 +01:00
|
|
|
n, endpoint, err := bind.ipv6.ReadFromUDP(buff)
|
|
|
|
return n, (*NativeEndpoint)(endpoint), err
|
|
|
|
}
|
|
|
|
|
2019-03-03 05:01:06 +01:00
|
|
|
func (bind *nativeBind) Send(buff []byte, endpoint Endpoint) error {
|
2017-11-19 13:14:15 +01:00
|
|
|
var err error
|
|
|
|
nend := endpoint.(*NativeEndpoint)
|
2018-05-18 05:02:35 +02:00
|
|
|
if nend.IP.To4() != nil {
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.ipv4 == nil {
|
|
|
|
return syscall.EAFNOSUPPORT
|
|
|
|
}
|
2019-10-21 13:29:57 +02:00
|
|
|
if bind.blackhole4 {
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-19 13:14:15 +01:00
|
|
|
_, err = bind.ipv4.WriteToUDP(buff, (*net.UDPAddr)(nend))
|
2018-05-18 05:02:35 +02:00
|
|
|
} else {
|
2018-06-11 19:04:38 +02:00
|
|
|
if bind.ipv6 == nil {
|
|
|
|
return syscall.EAFNOSUPPORT
|
|
|
|
}
|
2019-10-21 13:29:57 +02:00
|
|
|
if bind.blackhole6 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-18 05:02:35 +02:00
|
|
|
_, err = bind.ipv6.WriteToUDP(buff, (*net.UDPAddr)(nend))
|
2017-11-19 13:14:15 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|