aboutsummaryrefslogtreecommitdiffstats
path: root/src/conn.go
blob: f6472e93140fb8cc1ee058774cbac071b924bd63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
	"net"
)

func updateUDPConn(device *Device) error {
	var err error
	netc := &device.net
	netc.mutex.Lock()

	// close existing connection

	if netc.conn != nil {
		netc.conn.Close()
	}

	// open new connection

	if device.tun.isUp.Get() {
		conn, err := net.ListenUDP("udp", netc.addr)
		if err == nil {
			netc.conn = conn
			signalSend(device.signal.newUDPConn)
		}
	}

	netc.mutex.Unlock()
	return err
}

func closeUDPConn(device *Device) {
	netc := &device.net
	netc.mutex.Lock()
	if netc.conn != nil {
		netc.conn.Close()
	}
	netc.mutex.Unlock()
	signalSend(device.signal.newUDPConn)
}