wireguard-go/src/conn.go

133 lines
2.7 KiB
Go
Raw Normal View History

package main
import (
"errors"
2017-11-14 15:27:53 +00:00
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"net"
)
2017-10-08 20:03:32 +00:00
type UDPBind interface {
SetMark(value uint32) error
ReceiveIPv6(buff []byte, end *Endpoint) (int, error)
ReceiveIPv4(buff []byte, end *Endpoint) (int, error)
Send(buff []byte, end *Endpoint) error
Close() error
}
/* An Endpoint maintains the source/destination caching for a peer
*
* dst : the remote address of a peer
* src : the local address from which datagrams originate going to the peer
*
*/
type UDPEndpoint interface {
ClearSrc() // clears the source address
ClearDst() // clears the destination address
SrcToString() string // returns the local source address (ip:port)
DstToString() string // returns the destination address (ip:port)
DstToBytes() []byte // used for mac2 cookie calculations
DstIP() net.IP
SrcIP() net.IP
}
func parseEndpoint(s string) (*net.UDPAddr, error) {
// ensure that the host is an IP address
host, _, err := net.SplitHostPort(s)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip == nil {
return nil, errors.New("Failed to parse IP address: " + host)
}
// parse address and port
addr, err := net.ResolveUDPAddr("udp", s)
if err != nil {
return nil, err
}
return addr, err
}
2017-11-11 14:43:55 +00:00
/* Must hold device and net lock
*/
func unsafeCloseUDPListener(device *Device) error {
2017-11-11 22:26:44 +00:00
var err error
2017-11-11 14:43:55 +00:00
netc := &device.net
if netc.bind != nil {
2017-11-11 22:26:44 +00:00
err = netc.bind.Close()
2017-11-11 14:43:55 +00:00
netc.bind = nil
}
2017-11-11 22:26:44 +00:00
return err
2017-11-11 14:43:55 +00:00
}
// must inform all listeners
func UpdateUDPListener(device *Device) error {
device.mutex.Lock()
defer device.mutex.Unlock()
netc := &device.net
netc.mutex.Lock()
2017-08-17 10:58:18 +00:00
defer netc.mutex.Unlock()
// close existing sockets
2017-11-11 14:43:55 +00:00
if err := unsafeCloseUDPListener(device); err != nil {
return err
}
2017-11-11 22:26:44 +00:00
// assumption: netc.update WaitGroup should be exactly 1
2017-11-11 14:43:55 +00:00
// open new sockets
if device.tun.isUp.Get() {
2017-08-17 10:58:18 +00:00
2017-11-14 15:27:53 +00:00
device.log.Debug.Println("UDP bind updating")
2017-10-08 20:03:32 +00:00
// bind to new port
var err error
netc.bind, netc.port, err = CreateUDPBind(netc.port)
if err != nil {
2017-11-11 14:43:55 +00:00
netc.bind = nil
2017-10-08 20:03:32 +00:00
return err
}
2017-08-17 10:58:18 +00:00
2017-10-08 20:03:32 +00:00
// set mark
err = netc.bind.SetMark(netc.fwmark)
if err != nil {
return err
2017-08-22 15:22:45 +00:00
}
// clear cached source addresses
for _, peer := range device.peers {
peer.mutex.Lock()
peer.endpoint.value.ClearSrc()
peer.mutex.Unlock()
}
2017-11-11 14:43:55 +00:00
2017-11-11 22:26:44 +00:00
// decrease waitgroup to 0
2017-11-11 14:43:55 +00:00
2017-11-14 15:27:53 +00:00
go device.RoutineReceiveIncomming(ipv4.Version, netc.bind)
go device.RoutineReceiveIncomming(ipv6.Version, netc.bind)
2017-11-11 22:26:44 +00:00
device.log.Debug.Println("UDP bind has been updated")
}
2017-08-17 10:58:18 +00:00
return nil
}
func CloseUDPListener(device *Device) error {
2017-11-11 14:43:55 +00:00
device.mutex.Lock()
device.net.mutex.Lock()
err := unsafeCloseUDPListener(device)
device.net.mutex.Unlock()
device.mutex.Unlock()
return err
}