-
Notifications
You must be signed in to change notification settings - Fork 47
/
ssdp.js
executable file
·253 lines (188 loc) · 6.32 KB
/
ssdp.js
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Extend SSDP server to handle multiple devices
*/
var Server = require('node-ssdp').Server;
var util = require('util');
var dgram = require('dgram');
var ip = require('ip');
var debug = require('debug')('ssdp');
var SsdpServer = module.exports = function SsdpServer(opts, sock){
var self = this;
// reuse address to prevent EBOUND error
var sock = dgram.createSocket({type:'udp4',reuseAddr:true});
Server.call(this, opts, sock);
this.bootId = Date.now();
this.configId = opts.configId || 1;
this.upnpVersion = opts.upnpVersion || 0;
this._devices = {};
this.ips = opts.ips;
// use servers bound to each interface to advertise
this.clients = {};
this.ips.forEach(function(host){
// create one socket for each ip
var client = self._createSocket();
client.on('error', function onSocketError(err) {
self._logger.error(err, 'Socker error for:' + host)
});
client.on('message', function onSocketMessage(msg, rinfo) {
// we are not realy listening here, only advertising
// self._parseMessage(msg, rinfo)
});
client.on('listening', function onSocketListening() {
var addr = self.sock.address()
self._logger.info('SSDP listening on ' + 'http://' + addr.address + ':' + addr.port)
client.addMembership(self._ssdpIp)
});
self.clients[host] = client;
})
}
util.inherits(SsdpServer, Server);
SsdpServer.prototype._stop = function () {
Server.prototype._stop.call(this);
for (var host in this.clients){
this.clients[host].close();
delete this.clients[host];
}
}
// [1] http://upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.1.pdf
SsdpServer.prototype.addRoot = function( uuid, usn, location, callback ){
var root = {
location: location,
usn:{},
processVendorHeaders:callback
};
// [1] page 20 table 1-1
root.usn['upnp:rootdevice'] = uuid + '::upnp:rootdevice';
root.usn[uuid] = uuid;
root.usn[usn] = uuid + '::' + usn;
this._devices[uuid] = root;
}
SsdpServer.prototype.addDevice = function( uuid, usn, location, callback){
var device = {
location: location,
usn:{},
processVendorHeaders:callback
};
// [1] page 20 table 1-2
device.usn[uuid] = uuid;
device.usn[usn] = uuid + '::' + usn;
this._devices[uuid] = device;
}
SsdpServer.prototype.addService = function( uuid, st){
// [1] page 20 table 1-3
this._devices[uuid].usn[st] = uuid + '::' + st;
}
/**
* Upnp device architecture v1.1 headers
*/
SsdpServer.prototype.addV1Headers = function(heads, alive){
var self = this;
if (self.upnpVersion > 0){
// required
heads['BOOTID.UPNP.ORG'] = self.bootId;
// parallel to description configId attribute
// required but optionnal for m-search response
heads['CONFIGID.UPNP.ORG'] = self.configId;
if (self.ipFamily === 'Ipv6'){
heads['OPT'] = '"http://schemas.upnp.org/upnp/1/0/"; ns=01';
heads['01-NLS'] = self.bootId;
}
if (alive){
// optionnal
heads['SEARCHPORT.UPNP.ORG'] = self._ssdpPort;
}
}
}
/**
*
* @param alive
*/
SsdpServer.prototype.advertise = function (alive) {
var self = this
if (alive === undefined) alive = true
Object.keys(self._devices).forEach(function (uuid) {
var device = self._devices[uuid];
Object.keys(device.usn).forEach(function(usn){
var udn = device.usn[usn];
// [1] page 22
var heads = {
'HOST':self._ssdpServerHost,
'NT':usn, // notification type, in this case same as ST
'NTS':(alive ? 'ssdp:alive' : 'ssdp:byebye'), // notification sub-type
'USN':udn
}
self.addV1Headers(heads, alive);
device.processVendorHeaders(heads, alive);
self.ips.forEach(function(host){
if (alive) {
heads['LOCATION'] = "http://" + host + device.location
heads['CACHE-CONTROL'] = 'max-age=1800'
heads['SERVER'] = self._ssdpSig // why not include this?
}
self._logger.trace('Sending an advertisement event')
var message = new Buffer(self._getSSDPHeader('NOTIFY', heads))
self.clients[host].send(message, 0, message.length, self._ssdpPort, self._ssdpIp, function (err, bytes) {
self._logger.trace({'message': message.toString()}, 'Advertise ', heads.NTS)
});
});
});
});
}
SsdpServer.prototype.findHostBySubnet = function(peer){
var mask = ['255.255.255.0','255.255.0.0','255.0.0.0'];
var len = this.ips.length;
for (var i=0; i < 3; i++){
var submask = mask[i];
var maskedPeer = ip.mask(peer, submask)
for (var j=0; j<len; j++){
var maskedIp = ip.mask(this.ips[j], submask);
if (maskedIp == maskedPeer){
return this.ips[j];
}
}
}
return this.ips[0];
}
// ssdpServer seem not listening for search when bound to an ip
SsdpServer.prototype._respondToSearch = function (serviceType, rinfo) {
var self = this
, peer = rinfo.address
, port = rinfo.port
if (!this.sock) return
// unwrap quoted string
if (serviceType[0] == '"' && serviceType[serviceType.length-1] == '"') {
serviceType = serviceType.slice(1, -1)
}
// NOTE:
// find an ip laying on same subnet as the rinfo
var host = self.findHostBySubnet(peer);
Object.keys(self._devices).forEach(function (uuid) {
var device = self._devices[uuid];
Object.keys(device.usn).forEach(function(usn){
var udn = device.usn[usn];
debug(peer+'----'+serviceType+":"+usn);
if (serviceType === 'ssdp:all' || usn === serviceType) {
var heads = {
'ST':usn,
'USN':udn,
'LOCATION':"http://" + host + device.location,
'CACHE-CONTROL':'max-age=' + self._ttl,
'DATE':new Date().toUTCString(),
'SERVER':self._ssdpSig,
'EXT':''
};
self.addV1Headers(heads, true);
device.processVendorHeaders(heads, true);
var pkt = self._getSSDPHeader(
'200 OK',
heads,
true
)
var message = new Buffer(pkt)
self._send(message, peer, port, function (err, bytes) {
self._logger.trace({'peer': peer, 'port': port, 'message': pkt}, 'Sent M-SEARCH response')
})
}
})
})
}