-
Notifications
You must be signed in to change notification settings - Fork 27
/
advertise.go
206 lines (192 loc) · 4.62 KB
/
advertise.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package ssdp
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"net/http"
"sync"
"github.com/koron/go-ssdp/internal/multicast"
"github.com/koron/go-ssdp/internal/ssdplog"
)
type message struct {
to net.Addr
data multicast.DataProvider
}
// Advertiser is a server to advertise a service.
type Advertiser struct {
st string
usn string
locProv LocationProvider
server string
maxAge int
conn *multicast.Conn
ch chan *message
wg sync.WaitGroup
wgS sync.WaitGroup
// addHost is an optional flag to add HOST header for M-SEARCH response.
// It is to support SmartThings.
// See https://github.com/koron/go-ssdp/issues/30 for details
addHost bool
}
// Advertise starts advertisement of service.
// location should be a string or a ssdp.LocationProvider.
func Advertise(st, usn string, location interface{}, server string, maxAge int, opts ...Option) (*Advertiser, error) {
locProv, err := toLocationProvider(location)
if err != nil {
return nil, err
}
cfg, err := opts2config(opts)
if err != nil {
return nil, err
}
conn, err := multicast.Listen(multicast.RecvAddrResolver, cfg.multicastConfig.options()...)
if err != nil {
return nil, err
}
ssdplog.Printf("SSDP advertise on: %s", conn.LocalAddr().String())
a := &Advertiser{
st: st,
usn: usn,
locProv: locProv,
server: server,
maxAge: maxAge,
conn: conn,
ch: make(chan *message),
addHost: cfg.advertiseConfig.addHost,
}
a.wg.Add(2)
a.wgS.Add(1)
go func() {
a.sendMain()
a.wgS.Done()
a.wg.Done()
}()
go func() {
a.recvMain()
a.wg.Done()
}()
return a, nil
}
func (a *Advertiser) recvMain() error {
// TODO: update listening interfaces of a.conn
err := a.conn.ReadPackets(0, func(addr net.Addr, data []byte) error {
if err := a.handleRaw(addr, data); err != nil {
ssdplog.Printf("failed to handle message: %s", err)
}
return nil
})
if err != nil && err != io.EOF {
return err
}
return nil
}
func (a *Advertiser) sendMain() {
for msg := range a.ch {
_, err := a.conn.WriteTo(msg.data, msg.to)
if err != nil {
ssdplog.Printf("failed to send: %s", err)
}
}
}
func (a *Advertiser) handleRaw(from net.Addr, raw []byte) error {
if !bytes.HasPrefix(raw, []byte("M-SEARCH ")) {
// unexpected method.
return nil
}
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(raw)))
if err != nil {
return err
}
var (
man = req.Header.Get("MAN")
st = req.Header.Get("ST")
)
if man != `"ssdp:discover"` {
return fmt.Errorf("unexpected MAN: %s", man)
}
if st != All && st != RootDevice && st != a.st {
// skip when ST is not matched/expected.
return nil
}
ssdplog.Printf("received M-SEARCH MAN=%s ST=%s from %s", man, st, from.String())
// build and send a response.
var host string
if a.addHost {
addr, err := multicast.SendAddr()
if err != nil {
return err
}
host = addr.String()
}
msg := buildOK(a.st, a.usn, a.locProv.Location(from, nil), a.server, a.maxAge, host)
a.ch <- &message{to: from, data: multicast.BytesDataProvider(msg)}
return nil
}
func buildOK(st, usn, location, server string, maxAge int, host string) []byte {
// bytes.Buffer#Write() is never fail, so we can omit error checks.
b := new(bytes.Buffer)
b.WriteString("HTTP/1.1 200 OK\r\n")
fmt.Fprintf(b, "EXT: \r\n")
fmt.Fprintf(b, "ST: %s\r\n", st)
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)
if host != "" {
fmt.Fprintf(b, "HOST: %s\r\n", host)
}
b.WriteString("\r\n")
return b.Bytes()
}
// Close stops advertisement.
func (a *Advertiser) Close() error {
if a.conn != nil {
// closing order is very important. be careful to change:
// stop sending loop by closing the channel and wait it.
close(a.ch)
a.wgS.Wait()
// stop receiving loop by closing the connection.
a.conn.Close()
a.wg.Wait()
a.conn = nil
}
return nil
}
// Alive announces ssdp:alive message.
func (a *Advertiser) Alive() error {
addr, err := multicast.SendAddr()
if err != nil {
return err
}
msg := &aliveDataProvider{
host: addr,
nt: a.st,
usn: a.usn,
location: a.locProv,
server: a.server,
maxAge: a.maxAge,
}
a.ch <- &message{to: addr, data: msg}
ssdplog.Printf("sent alive")
return nil
}
// Bye announces ssdp:byebye message.
func (a *Advertiser) Bye() error {
addr, err := multicast.SendAddr()
if err != nil {
return err
}
msg, err := buildBye(addr, a.st, a.usn)
if err != nil {
return err
}
a.ch <- &message{to: addr, data: multicast.BytesDataProvider(msg)}
ssdplog.Printf("sent bye")
return nil
}