Files
wireguard-go/src/conn.go
T

49 lines
795 B
Go
Raw Normal View History

2017-08-11 16:18:20 +02:00
package main
import (
"net"
)
func updateUDPConn(device *Device) error {
netc := &device.net
netc.mutex.Lock()
2017-08-17 12:58:18 +02:00
defer netc.mutex.Unlock()
2017-08-11 16:18:20 +02:00
// close existing connection
if netc.conn != nil {
netc.conn.Close()
}
// open new connection
if device.tun.isUp.Get() {
2017-08-17 12:58:18 +02:00
// listen on new address
2017-08-11 16:18:20 +02:00
conn, err := net.ListenUDP("udp", netc.addr)
2017-08-17 12:58:18 +02:00
if err != nil {
return err
2017-08-11 16:18:20 +02:00
}
2017-08-17 12:58:18 +02:00
// retrieve port (may have been chosen by kernel)
addr := conn.LocalAddr()
netc.conn = conn
netc.addr, _ = net.ResolveUDPAddr(addr.Network(), addr.String())
signalSend(device.signal.newUDPConn)
2017-08-11 16:18:20 +02:00
}
2017-08-17 12:58:18 +02:00
return nil
2017-08-11 16:18:20 +02:00
}
func closeUDPConn(device *Device) {
netc := &device.net
netc.mutex.Lock()
if netc.conn != nil {
netc.conn.Close()
}
netc.mutex.Unlock()
signalSend(device.signal.newUDPConn)
}