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

UDP node fixes. Allow fixing of outbound port.

Fixes #178
Also fixed multicast binding to work more correctly.
Note: - if you fix the outbound port it will then be unvailable for input - as we are not setting up a pool.
This commit is contained in:
Dave C-J 2014-03-27 23:27:43 +00:00
parent 0409a14a84
commit e5e457a410
2 changed files with 39 additions and 17 deletions

View File

@ -30,7 +30,7 @@
</div> </div>
<div class="form-row node-input-iface"> <div class="form-row node-input-iface">
<label for="node-input-iface"><i class="icon-random"></i> Interface</label> <label for="node-input-iface"><i class="icon-random"></i> Interface</label>
<input type="text" id="node-input-iface" placeholder="eth0"> <input type="text" id="node-input-iface" placeholder="(optional) ip address of eth0">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-datatype"><i class="icon-file"></i> Output</label> <label for="node-input-datatype"><i class="icon-file"></i> Output</label>
@ -103,7 +103,7 @@
<option value="broad">broadcast message</option> <option value="broad">broadcast message</option>
<option value="multi">multicast message</option> <option value="multi">multicast message</option>
</select> </select>
to port <input type="text" id="node-input-port" placeholder="Port" style="width: 45px"> to port <input type="text" id="node-input-port" placeholder="Port" style="width: 70px">
</div> </div>
<div class="form-row node-input-addr"> <div class="form-row node-input-addr">
<label for="node-input-addr" id="node-input-addr-label"><i class="icon-list"></i> Address</label> <label for="node-input-addr" id="node-input-addr-label"><i class="icon-list"></i> Address</label>
@ -111,11 +111,15 @@
</div> </div>
<div class="form-row node-input-iface"> <div class="form-row node-input-iface">
<label for="node-input-iface"><i class="icon-random"></i> Interface</label> <label for="node-input-iface"><i class="icon-random"></i> Interface</label>
<input type="text" id="node-input-iface" placeholder="eth0"> <input type="text" id="node-input-iface" placeholder="(optional) ip address of eth0">
</div>
<div class="form-row">
<label for="node-input-outport"><i class="icon-random"></i> optional</label>
output port <input type="text" id="node-input-outport" style="width: 70px;" placeholder="not fixed">
</div> </div>
<div class="form-row"> <div class="form-row">
<label>&nbsp;</label> <label>&nbsp;</label>
<input type="checkbox" id="node-input-base64" placeholder="base64" style="display: inline-block; width: auto; vertical-align: top;"> <input type="checkbox" id="node-input-base64" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-base64" style="width: 70%;">Decode Base64 encoded payload ?</label> <label for="node-input-base64" style="width: 70%;">Decode Base64 encoded payload ?</label>
</div> </div>
<div class="form-row"> <div class="form-row">
@ -160,6 +164,7 @@
addr: {value:""}, addr: {value:""},
iface: {value:""}, iface: {value:""},
port: {value:""}, port: {value:""},
outport: {value:""},
base64: {value:false,required:true}, base64: {value:false,required:true},
multicast: {value:"false"} multicast: {value:"false"}
}, },

View File

@ -49,9 +49,15 @@ function UDPin(n) {
node.log('udp listener at ' + address.address + ":" + address.port); node.log('udp listener at ' + address.address + ":" + address.port);
if (node.multicast == "true") { if (node.multicast == "true") {
server.setBroadcast(true); server.setBroadcast(true);
try {
server.setMulticastTTL(128); server.setMulticastTTL(128);
server.addMembership(node.group,node.iface); server.addMembership(node.group,node.iface);
node.log("udp multicast group "+node.group); node.log("udp multicast group "+node.group);
} catch (e) {
if (e.errno == "EINVAL") { node.error("Bad Multicast Address"); }
else if (e.errno == "ENODEV") { node.error("Must be ip address of the required interface"); }
else { node.error("Error :"+e.errno); }
}
} }
}); });
@ -63,7 +69,7 @@ function UDPin(n) {
catch (err) { console.log(err); } catch (err) { console.log(err); }
}); });
server.bind(node.port); server.bind(node.port,node.iface);
} }
RED.nodes.registerType("udp in",UDPin); RED.nodes.registerType("udp in",UDPin);
@ -73,6 +79,7 @@ function UDPout(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
//this.group = n.group; //this.group = n.group;
this.port = n.port; this.port = n.port;
this.outport = n.outport||"";
this.base64 = n.base64; this.base64 = n.base64;
this.addr = n.addr; this.addr = n.addr;
this.iface = n.iface || null; this.iface = n.iface || null;
@ -81,18 +88,28 @@ function UDPout(n) {
var sock = dgram.createSocket('udp4'); // only use ipv4 for now var sock = dgram.createSocket('udp4'); // only use ipv4 for now
if (this.multicast != "false") { if (node.multicast != "false") {
sock.bind(node.port, function() { // have to bind before you can enable broadcast... if (node.outport == "") { node.outport = node.port; }
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
if (this.multicast == "multi") { if (node.multicast == "multi") {
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
node.log('udp multicast ready : '+node.addr+":"+node.port); node.log('udp multicast ready : '+node.outport+' -> '+node.addr+":"+node.port);
} catch (e) {
if (e.errno == "EINVAL") { node.error("Bad Multicast Address"); }
else if (e.errno == "ENODEV") { node.error("Must be ip address of the required interface"); }
else { node.error("Error :"+e.errno); }
} }
else node.log('udp broadcast ready : '+node.addr+":"+node.port); }
else node.log('udp broadcast ready : '+node.outport+' -> '+node.addr+":"+node.port);
}); });
} }
else if (node.outport != "") {
sock.bind(node.outport);
node.log('udp ready : '+node.outport+' -> '+node.addr+":"+node.port);
}
else node.log('udp ready : '+node.addr+":"+node.port); else node.log('udp ready : '+node.addr+":"+node.port);
node.on("input", function(msg) { node.on("input", function(msg) {