-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
72 lines (55 loc) · 1.29 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"flag"
"log"
"net"
"syscall"
)
func getConfPath() string {
conf := flag.String("conf", "", "Path to configuration yaml file")
flag.Parse()
return *conf
}
func main() {
var err error
confPath := getConfPath()
if confPath == "" {
log.Fatalf("Configuration file path not given")
}
conf, err := ParseConf(confPath)
if err != nil {
log.Fatalf("Failed parsing conf: %v", err)
}
app := NewApp()
err = app.InitConf(conf)
if err != nil {
log.Fatalf("Failed initializing: %v", err)
}
addr := net.UDPAddr{
Port: 67,
IP: net.ParseIP("0.0.0.0"),
}
ln, err := net.ListenUDP("udp", &addr)
if err != nil {
log.Fatalf("Failed listening: %v", err)
}
defer ln.Close()
// Boilerplate to get additional OOB data with each incoming packet, which
// includes the ID of the incoming interface
file, err := ln.File()
if err != nil {
log.Fatalf("Failed getting socket descriptor: %v", err)
}
syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IP, syscall.IP_PKTINFO, 1)
ln.SetReadBuffer(1048576)
buf := make([]byte, 1024)
oob := make([]byte, 1024)
for {
len, ooblen, _, remote, err := ln.ReadMsgUDP(buf, oob)
if err != nil {
log.Printf("Failed accepting: %v", err)
continue
}
go app.DispatchMessage(buf[:len], oob[:ooblen], remote, ln)
}
}