diff --git a/lib/dgram.js b/lib/dgram.js index 0a04767224..9a9c3483c2 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -4,14 +4,15 @@ var events = require("events"); var dns = require('dns'); var Buffer = require('buffer').Buffer; -var IOWatcher = process.IOWatcher; -var binding = process.binding('net'); -var socket = binding.socket; -var bind = binding.bind; -var recvfrom = binding.recvfrom; -var sendto = binding.sendto; -var close = binding.close; -var ENOENT = binding.ENOENT; +var IOWatcher = process.IOWatcher; +var binding = process.binding('net'); +var socket = binding.socket; +var bind = binding.bind; +var recvfrom = binding.recvfrom; +var sendto = binding.sendto; +var close = binding.close; +var ENOENT = binding.ENOENT; +var setBroadcast = binding.setBroadcast; function isPort (x) { return parseInt(x) >= 0; } var pool = null; @@ -30,10 +31,16 @@ function getPool() { return pool; } -function Socket (listener) { +function Socket (broadcast, listener) { events.EventEmitter.call(this); var self = this; + if (typeof(broadcast) != 'boolean') { + listener = broadcast; + broadcast = false; + } + self.broadcast = broadcast; + if (listener) { self.addListener('message', listener); } @@ -57,8 +64,8 @@ function Socket (listener) { sys.inherits(Socket, events.EventEmitter); exports.Socket = Socket; -exports.createSocket = function (listener) { - return new Socket(listener); +exports.createSocket = function (broadcast, listener) { + return new Socket(broadcast, listener); }; Socket.prototype.bind = function () { @@ -145,8 +152,7 @@ Socket.prototype.send = function(port, addr, buffer, offset, length) { self.fd = socket(self.type); } sendto(self.fd, buffer, offset, length, 0, port, addr); - } - else { + } else { dns.lookup(arguments[1], function (err, ip, addressType) { if (err) { self.emit('error', err); @@ -154,6 +160,7 @@ Socket.prototype.send = function(port, addr, buffer, offset, length) { if (!self.fd) { self.type = addressType == 4 ? 'udp4' : 'udp6'; self.fd = socket(self.type); + setBroadcast(self.fd, self.broadcast); process.nextTick(function() { self._startWatcher(); }); @@ -182,3 +189,4 @@ Socket.prototype.close = function () { self.emit("close"); } }; + diff --git a/src/node_net.cc b/src/node_net.cc index d7b75c8339..bbc1540bc8 100644 --- a/src/node_net.cc +++ b/src/node_net.cc @@ -968,6 +968,21 @@ static Handle SetNoDelay(const Arguments& args) { return Undefined(); } +static Handle SetBroadcast(const Arguments& args) { + int flags, r; + HandleScope scope; + + FD_ARG(args[0]) + + flags = args[1]->IsFalse() ? 0 : 1; + r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void *)&flags, sizeof(flags)); + + if (r < 0) { + return ThrowException(ErrnoException(errno, "setsockopt")); + } + return Undefined(); +} + static Handle SetKeepAlive(const Arguments& args) { int r; @@ -1065,6 +1080,7 @@ void InitNet(Handle target) { NODE_SET_METHOD(target, "socketError", SocketError); NODE_SET_METHOD(target, "toRead", ToRead); NODE_SET_METHOD(target, "setNoDelay", SetNoDelay); + NODE_SET_METHOD(target, "setBroadcast", SetBroadcast); NODE_SET_METHOD(target, "setKeepAlive", SetKeepAlive); NODE_SET_METHOD(target, "getsockname", GetSockName); NODE_SET_METHOD(target, "getpeername", GetPeerName);