1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00
wireguard-go/conn/bind_std.go

181 lines
3.8 KiB
Go
Raw Normal View History

2019-01-02 01:55:51 +01:00
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
*/
package conn
2017-08-25 14:53:23 +02:00
import (
"errors"
2017-08-25 14:53:23 +02:00
"net"
2018-06-11 19:04:38 +02:00
"syscall"
2017-08-25 14:53:23 +02:00
)
// StdNetBind is meant to be a temporary solution on platforms for which
// the sticky socket / source caching behavior has not yet been implemented.
// It uses the Go's net package to implement networking.
// See LinuxSocketBind for a proper implementation on the Linux platform.
type StdNetBind 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
}
func NewStdNetBind() Bind { return &StdNetBind{} }
2017-11-19 00:21:58 +01:00
type StdNetEndpoint net.UDPAddr
2017-11-19 00:21:58 +01:00
var _ Bind = (*StdNetBind)(nil)
var _ Endpoint = (*StdNetEndpoint)(nil)
func (*StdNetBind) ParseEndpoint(s string) (Endpoint, error) {
2017-11-19 00:21:58 +01:00
addr, err := parseEndpoint(s)
return (*StdNetEndpoint)(addr), err
2017-11-19 00:21:58 +01:00
}
func (*StdNetEndpoint) ClearSrc() {}
2017-11-19 00:21:58 +01:00
func (e *StdNetEndpoint) DstIP() net.IP {
2017-11-19 00:21:58 +01:00
return (*net.UDPAddr)(e).IP
}
func (e *StdNetEndpoint) SrcIP() net.IP {
2017-11-19 00:21:58 +01:00
return nil // not supported
}
func (e *StdNetEndpoint) DstToBytes() []byte {
2017-11-19 00:21:58 +01:00
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
}
func (e *StdNetEndpoint) DstToString() string {
2017-11-19 00:21:58 +01:00
return (*net.UDPAddr)(e).String()
}
func (e *StdNetEndpoint) SrcToString() string {
2017-11-19 00:21:58 +01:00
return ""
}
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
}
// Retrieve port.
2017-11-19 00:21:58 +01:00
laddr := conn.LocalAddr()
uaddr, err := net.ResolveUDPAddr(
2017-11-19 00:21:58 +01:00
laddr.Network(),
laddr.String(),
)
if err != nil {
return nil, 0, err
}
2017-11-19 00:21:58 +01:00
return conn, uaddr.Port, nil
}
func (bind *StdNetBind) Open(uport uint16) (uint16, error) {
var err error
var tries int
if bind.ipv4 != nil || bind.ipv6 != nil {
return 0, ErrBindAlreadyOpen
}
again:
port := int(uport)
bind.ipv4, port, err = listenNet("udp4", port)
if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) {
bind.ipv4 = nil
return 0, err
}
bind.ipv6, port, err = listenNet("udp6", port)
if uport == 0 && err != nil && errors.Is(err, syscall.EADDRINUSE) && tries < 100 {
bind.ipv4.Close()
bind.ipv4 = nil
bind.ipv6 = nil
tries++
goto again
}
if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) {
bind.ipv4.Close()
2018-06-11 19:04:38 +02:00
bind.ipv4 = nil
bind.ipv6 = nil
return 0, err
}
if bind.ipv4 == nil && bind.ipv6 == nil {
return 0, syscall.EAFNOSUPPORT
}
return uint16(port), nil
}
func (bind *StdNetBind) Close() error {
2018-06-11 19:04:38 +02:00
var err1, err2 error
if bind.ipv4 != nil {
err1 = bind.ipv4.Close()
bind.ipv4 = nil
2018-06-11 19:04:38 +02:00
}
if bind.ipv6 != nil {
err2 = bind.ipv6.Close()
bind.ipv6 = nil
2018-06-11 19:04:38 +02:00
}
if err1 != nil {
return err1
}
return err2
}
func (bind *StdNetBind) ReceiveIPv4(buff []byte) (int, Endpoint, error) {
2018-06-11 19:04:38 +02:00
if bind.ipv4 == nil {
return 0, nil, syscall.EAFNOSUPPORT
}
n, endpoint, err := bind.ipv4.ReadFromUDP(buff)
2018-05-18 05:02:35 +02:00
if endpoint != nil {
endpoint.IP = endpoint.IP.To4()
}
return n, (*StdNetEndpoint)(endpoint), err
}
func (bind *StdNetBind) ReceiveIPv6(buff []byte) (int, Endpoint, error) {
2018-06-11 19:04:38 +02:00
if bind.ipv6 == nil {
return 0, nil, syscall.EAFNOSUPPORT
}
n, endpoint, err := bind.ipv6.ReadFromUDP(buff)
return n, (*StdNetEndpoint)(endpoint), err
}
func (bind *StdNetBind) Send(buff []byte, endpoint Endpoint) error {
var err error
nend, ok := endpoint.(*StdNetEndpoint)
if !ok {
return ErrWrongEndpointType
}
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
}
_, 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))
}
return err
}