-
Notifications
You must be signed in to change notification settings - Fork 27
/
announce.go
118 lines (109 loc) · 2.91 KB
/
announce.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package ssdp
import (
"bytes"
"fmt"
"net"
"github.com/koron/go-ssdp/internal/multicast"
)
// AnnounceAlive sends ssdp:alive message.
// location should be a string or a ssdp.LocationProvider.
func AnnounceAlive(nt, usn string, location interface{}, server string, maxAge int, localAddr string, opts ...Option) error {
locProv, err := toLocationProvider(location)
if err != nil {
return err
}
cfg, err := opts2config(opts)
if err != nil {
return err
}
// dial multicast UDP packet.
conn, err := multicast.Listen(&multicast.AddrResolver{Addr: localAddr}, cfg.multicastConfig.options()...)
if err != nil {
return err
}
defer conn.Close()
// build and send message.
addr, err := multicast.SendAddr()
if err != nil {
return err
}
msg := &aliveDataProvider{
host: addr,
nt: nt,
usn: usn,
location: locProv,
server: server,
maxAge: maxAge,
}
if _, err := conn.WriteTo(msg, addr); err != nil {
return err
}
return nil
}
type aliveDataProvider struct {
host net.Addr
nt string
usn string
location LocationProvider
server string
maxAge int
}
func (p *aliveDataProvider) Bytes(ifi *net.Interface) []byte {
return buildAlive(p.host, p.nt, p.usn, p.location.Location(nil, ifi), p.server, p.maxAge)
}
var _ multicast.DataProvider = (*aliveDataProvider)(nil)
func buildAlive(raddr net.Addr, nt, usn, location, server string, maxAge int) []byte {
// bytes.Buffer#Write() is never fail, so we can omit error checks.
b := new(bytes.Buffer)
b.WriteString("NOTIFY * HTTP/1.1\r\n")
fmt.Fprintf(b, "HOST: %s\r\n", raddr.String())
fmt.Fprintf(b, "NT: %s\r\n", nt)
fmt.Fprintf(b, "NTS: %s\r\n", "ssdp:alive")
fmt.Fprintf(b, "USN: %s\r\n", usn)
if location != "" {
fmt.Fprintf(b, "LOCATION: %s\r\n", location)
}
if server != "" {
fmt.Fprintf(b, "SERVER: %s\r\n", server)
}
fmt.Fprintf(b, "CACHE-CONTROL: max-age=%d\r\n", maxAge)
b.WriteString("\r\n")
return b.Bytes()
}
// AnnounceBye sends ssdp:byebye message.
func AnnounceBye(nt, usn, localAddr string, opts...Option) error {
cfg, err := opts2config(opts)
if err != nil {
return err
}
// dial multicast UDP packet.
conn, err := multicast.Listen(&multicast.AddrResolver{Addr: localAddr}, cfg.multicastConfig.options()...)
if err != nil {
return err
}
defer conn.Close()
// build and send message.
addr, err := multicast.SendAddr()
if err != nil {
return err
}
msg, err := buildBye(addr, nt, usn)
if err != nil {
return err
}
if _, err := conn.WriteTo(multicast.BytesDataProvider(msg), addr); err != nil {
return err
}
return nil
}
func buildBye(raddr net.Addr, nt, usn string) ([]byte, error) {
b := new(bytes.Buffer)
// FIXME: error should be checked.
b.WriteString("NOTIFY * HTTP/1.1\r\n")
fmt.Fprintf(b, "HOST: %s\r\n", raddr.String())
fmt.Fprintf(b, "NT: %s\r\n", nt)
fmt.Fprintf(b, "NTS: %s\r\n", "ssdp:byebye")
fmt.Fprintf(b, "USN: %s\r\n", usn)
b.WriteString("\r\n")
return b.Bytes(), nil
}