1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #1945 from node-red/UDP-Node-fixes

Udp node socket binding fixes
This commit is contained in:
Nick O'Leary 2018-10-26 21:37:39 +01:00 committed by GitHub
commit 4d27ba1bda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,17 +58,36 @@ module.exports = function(RED) {
if (process.version.indexOf("v0.10") === 0) { opts = node.ipv; } if (process.version.indexOf("v0.10") === 0) { opts = node.ipv; }
var server; var server;
if (!udpInputPortsInUse.hasOwnProperty(this.port)) { if (!udpInputPortsInUse.hasOwnProperty(node.port)) {
server = dgram.createSocket(opts); // default to udp4 server = dgram.createSocket(opts); // default to udp4
udpInputPortsInUse[this.port] = server; server.bind(node.port, function() {
if (node.multicast == "true") {
server.setBroadcast(true);
server.setMulticastLoopback(false);
try {
server.setMulticastTTL(128);
server.addMembership(node.group,node.iface);
if (node.iface) { node.status({text:n.iface+" : "+node.iface}); }
node.log(RED._("udp.status.mc-group",{group:node.group}));
} catch (e) {
if (e.errno == "EINVAL") {
node.error(RED._("udp.errors.bad-mcaddress"));
} else if (e.errno == "ENODEV") {
node.error(RED._("udp.errors.interface"));
} else {
node.error(RED._("udp.errors.error",{error:e.errno}));
}
}
}
});
udpInputPortsInUse[node.port] = server;
} }
else { else {
node.log(RED._("udp.errors.alreadyused",{port:node.port})); node.log(RED._("udp.errors.alreadyused",{port:node.port}));
server = udpInputPortsInUse[this.port]; // re-use existing server = udpInputPortsInUse[node.port]; // re-use existing
if (node.iface) { node.status({text:n.iface+" : "+node.iface}); }
} }
if (process.version.indexOf("v0.10") === 0) { opts = node.ipv; }
server.on("error", function (err) { server.on("error", function (err) {
if ((err.code == "EACCES") && (node.port < 1024)) { if ((err.code == "EACCES") && (node.port < 1024)) {
node.error(RED._("udp.errors.access-error")); node.error(RED._("udp.errors.access-error"));
@ -92,39 +111,24 @@ module.exports = function(RED) {
server.on('listening', function () { server.on('listening', function () {
var address = server.address(); var address = server.address();
node.log(RED._("udp.status.listener-at",{host:address.address,port:address.port})); node.log(RED._("udp.status.listener-at",{host:node.iface||address.address,port:address.port}));
if (node.multicast == "true") {
server.setBroadcast(true);
try {
server.setMulticastTTL(128);
server.addMembership(node.group,node.iface);
node.log(RED._("udp.status.mc-group",{group:node.group}));
} catch (e) {
if (e.errno == "EINVAL") {
node.error(RED._("udp.errors.bad-mcaddress"));
} else if (e.errno == "ENODEV") {
node.error(RED._("udp.errors.interface"));
} else {
node.error(RED._("udp.errors.error",{error:e.errno}));
}
}
}
}); });
node.on("close", function() { node.on("close", function() {
if (udpInputPortsInUse.hasOwnProperty(node.port)) {
delete udpInputPortsInUse[node.port];
}
try { try {
if (node.multicast == "true") { server.dropMembership(node.group); }
server.close(); server.close();
node.log(RED._("udp.status.listener-stopped")); node.log(RED._("udp.status.listener-stopped"));
} catch (err) { } catch (err) {
//node.error(err); //node.error(err);
} }
if (udpInputPortsInUse.hasOwnProperty(node.port)) {
delete udpInputPortsInUse[node.port];
}
node.status({});
}); });
try { server.bind(node.port,node.iface); }
catch(e) { } // Don't worry if already bound
} }
RED.httpAdmin.get('/udp-ports/:id', RED.auth.needsPermission('udp-ports.read'), function(req,res) { RED.httpAdmin.get('/udp-ports/:id', RED.auth.needsPermission('udp-ports.read'), function(req,res) {
res.json(Object.keys(udpInputPortsInUse)); res.json(Object.keys(udpInputPortsInUse));
@ -132,6 +136,7 @@ module.exports = function(RED) {
RED.nodes.registerType("udp in",UDPin); RED.nodes.registerType("udp in",UDPin);
// The Output Node // The Output Node
function UDPout(n) { function UDPout(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -169,31 +174,26 @@ module.exports = function(RED) {
} }
var opts = {type:node.ipv, reuseAddr:true}; var opts = {type:node.ipv, reuseAddr:true};
if (process.version.indexOf("v0.10") === 0) { opts = node.ipv; }
var sock; var sock;
var p = this.outport || this.port || "0"; var p = this.outport || this.port || "0";
node.tout = setTimeout(function() {
if (udpInputPortsInUse[p]) { if (udpInputPortsInUse[p]) {
sock = udpInputPortsInUse[p]; sock = udpInputPortsInUse[p];
node.log(RED._("udp.status.re-use",{outport:node.outport,host:node.addr,port:node.port})); node.log(RED._("udp.status.re-use",{outport:node.outport,host:node.addr,port:node.port}));
if (node.iface) { node.status({text:n.iface+" : "+node.iface}); }
} }
else { else {
sock = dgram.createSocket(opts); // default to udp4 sock = dgram.createSocket(opts); // default to udp4
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.
});
udpInputPortsInUse[p] = sock;
if (node.multicast != "false") { if (node.multicast != "false") {
sock.bind(node.outport, function() { // have to bind before you can enable broadcast... sock.bind(node.outport, function() { // have to bind before you can enable broadcast...
sock.setBroadcast(true); // turn on broadcast sock.setBroadcast(true); // turn on broadcast
sock.setMulticastLoopback(false); // turn off loopback
if (node.multicast == "multi") { if (node.multicast == "multi") {
try { try {
sock.setMulticastTTL(128); sock.setMulticastTTL(128);
sock.addMembership(node.addr,node.iface); // Add to the multicast group sock.addMembership(node.addr,node.iface); // Add to the multicast group
if (node.iface) { node.status({text:n.iface+" : "+node.iface}); }
node.log(RED._("udp.status.mc-ready",{iface:node.iface,outport:node.outport,host:node.addr,port:node.port})); node.log(RED._("udp.status.mc-ready",{iface:node.iface,outport:node.outport,host:node.addr,port:node.port}));
} catch (e) { } catch (e) {
if (e.errno == "EINVAL") { if (e.errno == "EINVAL") {
@ -214,6 +214,13 @@ module.exports = function(RED) {
} else { } else {
node.log(RED._("udp.status.ready-nolocal",{host:node.addr,port:node.port})); node.log(RED._("udp.status.ready-nolocal",{host:node.addr,port:node.port}));
} }
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.
});
udpInputPortsInUse[p] = sock;
} }
node.on("input", function(msg) { node.on("input", function(msg) {
@ -244,17 +251,21 @@ module.exports = function(RED) {
} }
} }
}); });
}, 150);
node.on("close", function() { node.on("close", function() {
if (udpInputPortsInUse.hasOwnProperty(p)) { if (node.tout) { clearTimeout(node.tout); }
delete udpInputPortsInUse[p];
}
try { try {
if (node.multicast == "multi") { sock.dropMembership(node.group); }
sock.close(); sock.close();
node.log(RED._("udp.status.output-stopped")); node.log(RED._("udp.status.output-stopped"));
} catch (err) { } catch (err) {
//node.error(err); //node.error(err);
} }
if (udpInputPortsInUse.hasOwnProperty(p)) {
delete udpInputPortsInUse[p];
}
node.status({});
}); });
} }
RED.nodes.registerType("udp out",UDPout); RED.nodes.registerType("udp out",UDPout);