-
Notifications
You must be signed in to change notification settings - Fork 27
/
monitor.go
220 lines (190 loc) · 4.7 KB
/
monitor.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package ssdp
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net"
"net/http"
"sync"
"github.com/koron/go-ssdp/internal/multicast"
"github.com/koron/go-ssdp/internal/ssdplog"
)
// Monitor monitors SSDP's alive and byebye messages.
type Monitor struct {
Alive AliveHandler
Bye ByeHandler
Search SearchHandler
Options []Option
conn *multicast.Conn
wg sync.WaitGroup
}
// Start starts to monitor SSDP messages.
func (m *Monitor) Start() error {
cfg, err := opts2config(m.Options)
if err != nil {
return err
}
conn, err := multicast.Listen(multicast.RecvAddrResolver, cfg.multicastConfig.options()...)
if err != nil {
return err
}
ssdplog.Printf("monitoring on %s", conn.LocalAddr().String())
m.conn = conn
m.wg.Add(1)
go func() {
m.serve()
m.wg.Done()
}()
return nil
}
func (m *Monitor) serve() error {
// TODO: update listening interfaces of m.conn
err := m.conn.ReadPackets(0, func(addr net.Addr, data []byte) error {
msg := make([]byte, len(data))
copy(msg, data)
go m.handleRaw(addr, msg)
return nil
})
if err != nil && !errors.Is(err, io.EOF) {
return err
}
return nil
}
func (m *Monitor) handleRaw(addr net.Addr, raw []byte) error {
// Add newline to workaround buggy SSDP responses
if !bytes.HasSuffix(raw, endOfHeader) {
raw = bytes.Join([][]byte{raw, endOfHeader}, nil)
}
if bytes.HasPrefix(raw, []byte("M-SEARCH ")) {
return m.handleSearch(addr, raw)
}
if bytes.HasPrefix(raw, []byte("NOTIFY ")) {
return m.handleNotify(addr, raw)
}
n := bytes.Index(raw, []byte("\r\n"))
ssdplog.Printf("unexpected method: %q", string(raw[:n]))
return nil
}
func (m *Monitor) handleNotify(addr net.Addr, raw []byte) error {
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(raw)))
if err != nil {
return err
}
switch nts := req.Header.Get("NTS"); nts {
case "ssdp:alive":
if req.Method != "NOTIFY" {
return fmt.Errorf("unexpected method for %q: %s", "ssdp:alive", req.Method)
}
if h := m.Alive; h != nil {
h(&AliveMessage{
From: addr,
Type: req.Header.Get("NT"),
USN: req.Header.Get("USN"),
Location: req.Header.Get("LOCATION"),
Server: req.Header.Get("SERVER"),
rawHeader: req.Header,
})
}
case "ssdp:byebye":
if req.Method != "NOTIFY" {
return fmt.Errorf("unexpected method for %q: %s", "ssdp:byebye", req.Method)
}
if h := m.Bye; h != nil {
h(&ByeMessage{
From: addr,
Type: req.Header.Get("NT"),
USN: req.Header.Get("USN"),
rawHeader: req.Header,
})
}
default:
return fmt.Errorf("unknown NTS: %s", nts)
}
return nil
}
func (m *Monitor) handleSearch(addr net.Addr, raw []byte) error {
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(raw)))
if err != nil {
return err
}
man := req.Header.Get("MAN")
if man != `"ssdp:discover"` {
return fmt.Errorf("unexpected MAN: %s", man)
}
if h := m.Search; h != nil {
h(&SearchMessage{
From: addr,
Type: req.Header.Get("ST"),
rawHeader: req.Header,
})
}
return nil
}
// Close closes monitoring.
func (m *Monitor) Close() error {
if m.conn != nil {
m.conn.Close()
m.conn = nil
m.wg.Wait()
}
return nil
}
// AliveMessage represents SSDP's ssdp:alive message.
type AliveMessage struct {
// From is a sender of this message
From net.Addr
// Type is a property of "NT"
Type string
// USN is a property of "USN"
USN string
// Location is a property of "LOCATION"
Location string
// Server is a property of "SERVER"
Server string
rawHeader http.Header
maxAge *int
}
// Header returns all properties in alive message.
func (m *AliveMessage) Header() http.Header {
return m.rawHeader
}
// MaxAge extracts "max-age" value from "CACHE-CONTROL" property.
func (m *AliveMessage) MaxAge() int {
if m.maxAge == nil {
m.maxAge = new(int)
*m.maxAge = extractMaxAge(m.rawHeader.Get("CACHE-CONTROL"), -1)
}
return *m.maxAge
}
// AliveHandler is handler of Alive message.
type AliveHandler func(*AliveMessage)
// ByeMessage represents SSDP's ssdp:byebye message.
type ByeMessage struct {
// From is a sender of this message
From net.Addr
// Type is a property of "NT"
Type string
// USN is a property of "USN"
USN string
rawHeader http.Header
}
// Header returns all properties in bye message.
func (m *ByeMessage) Header() http.Header {
return m.rawHeader
}
// ByeHandler is handler of Bye message.
type ByeHandler func(*ByeMessage)
// SearchMessage represents SSDP's ssdp:discover message.
type SearchMessage struct {
From net.Addr
Type string
rawHeader http.Header
}
// Header returns all properties in search message.
func (s *SearchMessage) Header() http.Header {
return s.rawHeader
}
// SearchHandler is handler of Search message.
type SearchHandler func(*SearchMessage)