2013-10-11 22:42:34 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2013-10-11 22:42:34 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-05-29 23:13:21 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var dgram = require('dgram');
|
2016-01-17 11:34:40 +01:00
|
|
|
var udpInputPortsInUse = {};
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// The Input Node
|
|
|
|
function UDPin(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.group = n.group;
|
|
|
|
this.port = n.port;
|
|
|
|
this.datatype = n.datatype;
|
|
|
|
this.iface = n.iface || null;
|
|
|
|
this.multicast = n.multicast;
|
2015-03-05 14:07:38 +01:00
|
|
|
this.ipv = n.ipv || "udp4";
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2016-09-17 15:05:26 +02:00
|
|
|
|
|
|
|
var opts = {type:node.ipv, reuseAddr:true};
|
|
|
|
if (process.version.indexOf("v0.10") === 0) { opts = node.ipv; }
|
|
|
|
var server;
|
|
|
|
|
2016-01-17 11:34:40 +01:00
|
|
|
if (!udpInputPortsInUse.hasOwnProperty(this.port)) {
|
2016-09-17 15:05:26 +02:00
|
|
|
server = dgram.createSocket(opts); // default to udp4
|
|
|
|
udpInputPortsInUse[this.port] = server;
|
2016-01-17 11:34:40 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn(RED._("udp.errors.alreadyused",node.port));
|
2016-09-17 15:05:26 +02:00
|
|
|
server = udpInputPortsInUse[this.port]; // re-use existing
|
2016-01-17 11:34:40 +01:00
|
|
|
}
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2015-12-01 15:52:15 +01:00
|
|
|
if (process.version.indexOf("v0.10") === 0) { opts = node.ipv; }
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
server.on("error", function (err) {
|
|
|
|
if ((err.code == "EACCES") && (node.port < 1024)) {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.error(RED._("udp.errors.access-error"));
|
2014-05-29 23:13:21 +02:00
|
|
|
} else {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.error(RED._("udp.errors.error",{error:err.code}));
|
2014-03-28 00:27:43 +01:00
|
|
|
}
|
2013-10-24 20:58:57 +02:00
|
|
|
server.close();
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
server.on('message', function (message, remote) {
|
|
|
|
var msg;
|
|
|
|
if (node.datatype =="base64") {
|
2015-02-24 14:22:48 +01:00
|
|
|
msg = { payload:message.toString('base64'), fromip:remote.address+':'+remote.port, ip:remote.address, port:remote.port };
|
2014-05-04 00:32:04 +02:00
|
|
|
} else if (node.datatype =="utf8") {
|
2015-02-24 14:22:48 +01:00
|
|
|
msg = { payload:message.toString('utf8'), fromip:remote.address+':'+remote.port, ip:remote.address, port:remote.port };
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
|
|
|
msg = { payload:message, fromip:remote.address+':'+remote.port, ip:remote.address, port:remote.port };
|
|
|
|
}
|
|
|
|
node.send(msg);
|
|
|
|
});
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
server.on('listening', function () {
|
|
|
|
var address = server.address();
|
2015-05-28 22:55:22 +02:00
|
|
|
node.log(RED._("udp.status.listener-at",{host:address.address,port:address.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
if (node.multicast == "true") {
|
|
|
|
server.setBroadcast(true);
|
2014-03-28 00:27:43 +01:00
|
|
|
try {
|
2014-05-04 00:32:04 +02:00
|
|
|
server.setMulticastTTL(128);
|
|
|
|
server.addMembership(node.group,node.iface);
|
2015-05-28 22:55:22 +02:00
|
|
|
node.log(RED._("udp.status.mc-group",{group:node.group}));
|
2014-03-28 00:27:43 +01:00
|
|
|
} catch (e) {
|
2014-05-29 23:13:21 +02:00
|
|
|
if (e.errno == "EINVAL") {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.error(RED._("udp.errors.bad-mcaddress"));
|
2014-03-31 00:05:59 +02:00
|
|
|
} else if (e.errno == "ENODEV") {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.error(RED._("udp.errors.interface"));
|
2014-03-31 00:05:59 +02:00
|
|
|
} else {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.error(RED._("udp.errors.error",{error:e.errno}));
|
2014-03-31 00:05:59 +02:00
|
|
|
}
|
2014-03-28 00:27:43 +01:00
|
|
|
}
|
2014-02-20 21:59:05 +01:00
|
|
|
}
|
|
|
|
});
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
node.on("close", function() {
|
2016-09-17 15:05:26 +02:00
|
|
|
if (udpInputPortsInUse.hasOwnProperty(node.port)) {
|
2016-01-17 11:34:40 +01:00
|
|
|
delete udpInputPortsInUse[node.port];
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
try {
|
|
|
|
server.close();
|
2015-05-28 22:55:22 +02:00
|
|
|
node.log(RED._("udp.status.listener-stopped"));
|
2014-05-04 00:32:04 +02:00
|
|
|
} catch (err) {
|
2016-09-17 15:05:26 +02:00
|
|
|
//node.error(err);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
});
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2016-09-17 15:05:26 +02:00
|
|
|
try { server.bind(node.port,node.iface); }
|
|
|
|
catch(e) { } // Don't worry if already bound
|
2014-03-28 00:27:43 +01:00
|
|
|
}
|
2016-11-10 21:19:55 +01:00
|
|
|
RED.httpAdmin.get('/udp-ports/:id', RED.auth.needsPermission('udp-ports.read'), function(req,res) {
|
2016-09-17 15:05:26 +02:00
|
|
|
res.json(Object.keys(udpInputPortsInUse));
|
2016-01-17 11:34:40 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("udp in",UDPin);
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2016-09-17 15:05:26 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// The Output Node
|
|
|
|
function UDPout(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
//this.group = n.group;
|
|
|
|
this.port = n.port;
|
|
|
|
this.outport = n.outport||"";
|
|
|
|
this.base64 = n.base64;
|
|
|
|
this.addr = n.addr;
|
|
|
|
this.iface = n.iface || null;
|
|
|
|
this.multicast = n.multicast;
|
2015-03-05 14:07:38 +01:00
|
|
|
this.ipv = n.ipv || "udp4";
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2015-12-01 15:52:15 +01:00
|
|
|
var opts = {type:node.ipv, reuseAddr:true};
|
|
|
|
if (process.version.indexOf("v0.10") === 0) { opts = node.ipv; }
|
2016-09-17 15:05:26 +02:00
|
|
|
|
|
|
|
var sock;
|
|
|
|
if (udpInputPortsInUse[this.outport]) {
|
|
|
|
sock = udpInputPortsInUse[this.outport];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sock = dgram.createSocket(opts); // default to udp4
|
2017-06-23 17:20:28 +02:00
|
|
|
sock.on("error", function(err) {
|
|
|
|
// Any async error will also get reported in the sock.send call.
|
|
|
|
// This handler is needed to ensure the error marked as handled to
|
|
|
|
// prevent it going to the global error handler and shutting node-red
|
|
|
|
// down.
|
|
|
|
});
|
2016-09-17 15:05:26 +02:00
|
|
|
udpInputPortsInUse[this.outport] = sock;
|
|
|
|
}
|
2015-11-16 01:25:26 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
if (node.multicast != "false") {
|
2016-09-17 15:05:26 +02:00
|
|
|
if (node.outport === "") { node.outport = node.port; }
|
2014-05-04 00:32:04 +02:00
|
|
|
sock.bind(node.outport, function() { // have to bind before you can enable broadcast...
|
|
|
|
sock.setBroadcast(true); // turn on broadcast
|
|
|
|
if (node.multicast == "multi") {
|
|
|
|
try {
|
|
|
|
sock.setMulticastTTL(128);
|
|
|
|
sock.addMembership(node.addr,node.iface); // Add to the multicast group
|
2015-05-28 22:55:22 +02:00
|
|
|
node.log(RED._("udp.status.mc-ready",{outport:node.outport,host:node.addr,port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
if (e.errno == "EINVAL") {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.error(RED._("udp.errors.bad-mcaddress"));
|
2014-05-04 00:32:04 +02:00
|
|
|
} else if (e.errno == "ENODEV") {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.error(RED._("udp.errors.interface"));
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.error(RED._("udp.errors.error",{error:e.errno}));
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-31 00:05:59 +02:00
|
|
|
} else {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.log(RED._("udp.status.bc-ready",{outport:node.outport,host:node.addr,port:node.port}));
|
2014-03-31 00:05:59 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2016-09-17 15:05:26 +02:00
|
|
|
} else if ((node.outport !== "") && (!udpInputPortsInUse[this.outport])) {
|
|
|
|
sock.bind(node.outport);
|
|
|
|
node.log(RED._("udp.status.ready",{outport:node.outport,host:node.addr,port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
2015-11-16 01:25:26 +01:00
|
|
|
node.log(RED._("udp.status.ready-nolocal",{host:node.addr,port:node.port}));
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
node.on("input", function(msg) {
|
2015-03-31 10:21:11 +02:00
|
|
|
if (msg.hasOwnProperty("payload")) {
|
2014-05-04 00:32:04 +02:00
|
|
|
var add = node.addr || msg.ip || "";
|
|
|
|
var por = node.port || msg.port || 0;
|
2016-09-17 15:05:26 +02:00
|
|
|
if (add === "") {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.warn(RED._("udp.errors.ip-notset"));
|
2016-09-17 15:05:26 +02:00
|
|
|
} else if (por === 0) {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.warn(RED._("udp.errors.port-notset"));
|
2014-05-04 00:32:04 +02:00
|
|
|
} else if (isNaN(por) || (por < 1) || (por > 65535)) {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.warn(RED._("udp.errors.port-invalid"));
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
|
|
|
var message;
|
|
|
|
if (node.base64) {
|
2014-09-08 21:11:12 +02:00
|
|
|
message = new Buffer(msg.payload, 'base64');
|
2014-05-04 00:32:04 +02:00
|
|
|
} else if (msg.payload instanceof Buffer) {
|
|
|
|
message = msg.payload;
|
|
|
|
} else {
|
|
|
|
message = new Buffer(""+msg.payload);
|
2014-03-31 00:05:59 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
sock.send(message, 0, message.length, por, add, function(err, bytes) {
|
|
|
|
if (err) {
|
2015-03-16 14:58:01 +01:00
|
|
|
node.error("udp : "+err,msg);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-06-17 21:06:54 +02:00
|
|
|
message = null;
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
2013-10-25 12:27:21 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-05-29 23:13:21 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
node.on("close", function() {
|
2016-09-17 15:05:26 +02:00
|
|
|
if (udpInputPortsInUse.hasOwnProperty(node.outport)) {
|
|
|
|
delete udpInputPortsInUse[node.outport];
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
try {
|
|
|
|
sock.close();
|
2015-05-28 22:55:22 +02:00
|
|
|
node.log(RED._("udp.status.output-stopped"));
|
2014-05-04 00:32:04 +02:00
|
|
|
} catch (err) {
|
2016-09-17 15:05:26 +02:00
|
|
|
//node.error(err);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("udp out",UDPout);
|
2013-10-11 22:42:34 +02:00
|
|
|
}
|