-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
44 lines (37 loc) · 829 Bytes
/
common.go
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
41
42
43
44
package learn_tun
import (
"log"
"net"
"os"
"os/signal"
"syscall"
)
func RegisterSignal() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
log.Printf("received %v - initiating shutdown\n", <-sigc)
os.Exit(1)
}
func ListenUdp(addr string) *net.UDPConn {
udpAddr := ParseUdpAddr(addr)
sock, err := net.ListenUDP("udp4", udpAddr)
if err != nil {
log.Fatalln("listen udp=", udpAddr, err)
}
return sock
}
func DialUdp(addr string) *net.UDPConn {
udpAddr := ParseUdpAddr(addr)
sock, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
log.Fatalln("listen udp=", udpAddr, err)
}
return sock
}
func ParseUdpAddr(addr string) *net.UDPAddr {
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
log.Fatalln("ResolveUDPAddr ", err)
}
return udpAddr
}