-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathssdp.js
161 lines (129 loc) · 4.33 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
var dgram = require('dgram'),
util = require('util'),
os = require('os'),
EventEmitter = require('events').EventEmitter;
var ssdp = exports;
function Ssdp(opts) {
EventEmitter.call(this);
this._opts = opts || {};
this._sourcePort = this._opts.sourcePort || 0;
this.multicast = '239.255.255.250';
this.port = 1900;
this._bound = false;
this._boundCount = 0;
this._closed = false;
this._queue = [];
// Create sockets on all external interfaces
this.createSockets();
}
util.inherits(Ssdp, EventEmitter);
ssdp.create = function create() {
return new Ssdp();
};
Ssdp.parseMimeHeader = function (headerStr) {
var lines = headerStr.split(/\r\n/g);
// Parse headers from lines to hashmap
return lines.reduce(function(headers, line) {
line.replace(/^([^:]*)\s*:\s*(.*)$/, function (a, key, value) {
headers[key.toLowerCase()] = value;
});
return headers;
}, {});
};
Ssdp.prototype.createSockets = function createSockets() {
var self = this,
interfaces = os.networkInterfaces();
this.sockets = Object.keys(interfaces).reduce(function(a, key) {
return a.concat(interfaces[key].filter(function(item) {
return !item.internal;
}).map(function(item) {
return self.createSocket(item);
}));
}, []);
};
Ssdp.prototype.search = function search(device, promise) {
if (!promise) {
promise = new EventEmitter();
promise._ended = false;
promise.once('end', function() {
promise._ended = true;
});
}
if (!this._bound) {
this._queue.push({ action: 'search', device: device, promise: promise });
return promise;
}
// If promise was ended before binding - do not send queries
if (promise._ended) { return; }
var self = this,
query = new Buffer('M-SEARCH * HTTP/1.1\r\n' +
'HOST: ' + this.multicast + ':' + this.port + '\r\n' +
'MAN: "ssdp:discover"\r\n' +
'MX: 1\r\n' +
'ST: ' + device + '\r\n' +
'\r\n');
// Send query on each socket
this.sockets.forEach(function(socket) {
socket.send(query, 0, query.length, this.port, this.multicast);
}, this);
function ondevice(info, address) {
if (promise._ended) { return; }
if (info.st !== device) { return; }
promise.emit('device', info, address);
}
this.on('_device', ondevice);
// Detach listener after receiving 'end' event
promise.once('end', function() {
self.removeListener('_device', ondevice);
});
return promise;
};
Ssdp.prototype.createSocket = function createSocket(interface) {
var self = this,
socket = dgram.createSocket(interface.family === 'IPv4' ?
'udp4' : 'udp6');
socket.on('message', function (message, info) {
// Ignore messages after closing sockets
if (self._closed) { return; }
// Parse response
self.parseResponse(message.toString(), socket.address, info);
});
// Bind in next tick (sockets should be me in this.sockets array)
process.nextTick(function() {
// Unqueue this._queue once all sockets are ready
function onready() {
if (self._boundCount < self.sockets.length) { return; }
self._bound = true;
self._queue.forEach(function(item) {
return self[item.action](item.device, item.promise);
});
}
socket.on('listening', function() {
self._boundCount += 1;
onready();
});
// On error - remove socket from list and execute items from queue
socket.once('error', function() {
self.sockets.splice(self.sockets.indexOf(socket), 1);
onready();
});
socket.address = interface.address;
socket.bind(self._sourcePort, interface.address);
});
return socket;
};
// TODO create separate logic for parsing unsolicited upnp broadcasts, if and when that need arises
Ssdp.prototype.parseResponse = function parseResponse(response, addr, remote) {
// Ignore incorrect packets
if (!/^(HTTP|NOTIFY)/m.test(response)) { return; }
var headers = Ssdp.parseMimeHeader(response);
// We are only interested in messages that can be matched against the original search target
if (!headers.st) { return; }
this.emit('_device', headers, remote.address);
};
Ssdp.prototype.close = function close() {
this.sockets.forEach(function(socket) {
socket.close();
});
this._closed = true;
};