-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
44 lines (38 loc) · 878 Bytes
/
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
package main
import (
"encoding/binary"
"flag"
"log"
"net"
"time"
)
func main() {
addr := ":37"
flag.StringVar(&addr, "addr", ":37", "Interface address to bind and listen")
flag.Parse()
laddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
log.Fatal(err)
}
conn, err := net.ListenUDP("udp", laddr)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
log.Printf("listen on %v", addr)
// The UDP loop
b := make([]byte, 4)
for {
_, client, err := conn.ReadFromUDP(b)
if err != nil {
log.Fatal(err)
}
// Some of CMs may send non-empty datagram
// The @time.Now().Unix() returns epoch time
// The RFC868 specifies the time must be since 00:00 (midnight) 1 January 1900 GMT
now := time.Now().Unix() + 2208988800
binary.BigEndian.PutUint32(b, uint32(now))
conn.WriteToUDP(b, client)
go log.Printf("reply to %s", client)
}
}