-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcast.go
69 lines (61 loc) · 1.63 KB
/
bcast.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
package main
import (
"flag"
"fmt"
"log"
"net"
"strings"
"time"
)
var bcastEnabled = flag.Bool("broadcast", true, "enable broadcast")
var bcastPort = flag.Int("broadcast_port", 9786, "broadcast port")
var SV_BCAST_MSG = []byte(fmt.Sprintf(
"NOTIFY * HTTP/1.1\r\nHost: 255.255.255.255:%d\r\nX-263A-Capabilities: sousvide\r\n", *bcastPort))
var SV_BCAST_FIELD_STREAM = "X-263A-Stream: %v:%d"
func StartBroadcast() {
if (!*bcastEnabled) {
log.Printf("broadcast disabled")
return
}
log.Printf("broadcast enabled, starting broadcast loop")
sock, err := net.DialUDP("udp4", nil, &net.UDPAddr{
IP: net.IPv4bcast, Port: *bcastPort,
})
if err != nil {
log.Panicf("error creating broadcast port: %v", err)
}
defer sock.Close()
var addr string
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Panicf("error getting interface addresses: %v", err)
}
for _, a := range addrs {
ipAddr := a.String()
ipAddr = ipAddr[0:strings.Index(ipAddr, "/")]
ip := net.ParseIP(ipAddr)
if ip != nil && !ip.IsLoopback() && ip.To4() != nil {
log.Printf("broadcasting address %v", ipAddr)
addr = ipAddr
}
}
field := []byte(fmt.Sprintf(SV_BCAST_FIELD_STREAM, addr, *SockPort))
msg := make([]byte, len(SV_BCAST_MSG) + len(field))
copy(msg, SV_BCAST_MSG)
for i := 0; i < len(field); i++ {
msg[i + len(SV_BCAST_MSG)] = field[i]
}
msg_len := len(msg)
tick := time.Tick(5 * time.Second)
for _ = range tick {
n, err := sock.Write(msg)
if err != nil {
log.Printf("warning, could not send broadcast: %v", err)
continue
}
if n != msg_len {
log.Printf("warning, sent %d bytes of %d in broadcast", n, msg_len)
continue
}
}
}