Browse Source

Add UDP broadcast support

v0.7.4-release
David Siegel 15 years ago
committed by Ryan Dahl
parent
commit
81ad8106dc
  1. 34
      lib/dgram.js
  2. 16
      src/node_net.cc

34
lib/dgram.js

@ -4,14 +4,15 @@ var events = require("events");
var dns = require('dns'); var dns = require('dns');
var Buffer = require('buffer').Buffer; var Buffer = require('buffer').Buffer;
var IOWatcher = process.IOWatcher; var IOWatcher = process.IOWatcher;
var binding = process.binding('net'); var binding = process.binding('net');
var socket = binding.socket; var socket = binding.socket;
var bind = binding.bind; var bind = binding.bind;
var recvfrom = binding.recvfrom; var recvfrom = binding.recvfrom;
var sendto = binding.sendto; var sendto = binding.sendto;
var close = binding.close; var close = binding.close;
var ENOENT = binding.ENOENT; var ENOENT = binding.ENOENT;
var setBroadcast = binding.setBroadcast;
function isPort (x) { return parseInt(x) >= 0; } function isPort (x) { return parseInt(x) >= 0; }
var pool = null; var pool = null;
@ -30,10 +31,16 @@ function getPool() {
return pool; return pool;
} }
function Socket (listener) { function Socket (broadcast, listener) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
var self = this; var self = this;
if (typeof(broadcast) != 'boolean') {
listener = broadcast;
broadcast = false;
}
self.broadcast = broadcast;
if (listener) { if (listener) {
self.addListener('message', listener); self.addListener('message', listener);
} }
@ -57,8 +64,8 @@ function Socket (listener) {
sys.inherits(Socket, events.EventEmitter); sys.inherits(Socket, events.EventEmitter);
exports.Socket = Socket; exports.Socket = Socket;
exports.createSocket = function (listener) { exports.createSocket = function (broadcast, listener) {
return new Socket(listener); return new Socket(broadcast, listener);
}; };
Socket.prototype.bind = function () { Socket.prototype.bind = function () {
@ -145,8 +152,7 @@ Socket.prototype.send = function(port, addr, buffer, offset, length) {
self.fd = socket(self.type); self.fd = socket(self.type);
} }
sendto(self.fd, buffer, offset, length, 0, port, addr); sendto(self.fd, buffer, offset, length, 0, port, addr);
} } else {
else {
dns.lookup(arguments[1], function (err, ip, addressType) { dns.lookup(arguments[1], function (err, ip, addressType) {
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);
@ -154,6 +160,7 @@ Socket.prototype.send = function(port, addr, buffer, offset, length) {
if (!self.fd) { if (!self.fd) {
self.type = addressType == 4 ? 'udp4' : 'udp6'; self.type = addressType == 4 ? 'udp4' : 'udp6';
self.fd = socket(self.type); self.fd = socket(self.type);
setBroadcast(self.fd, self.broadcast);
process.nextTick(function() { process.nextTick(function() {
self._startWatcher(); self._startWatcher();
}); });
@ -182,3 +189,4 @@ Socket.prototype.close = function () {
self.emit("close"); self.emit("close");
} }
}; };

16
src/node_net.cc

@ -968,6 +968,21 @@ static Handle<Value> SetNoDelay(const Arguments& args) {
return Undefined(); return Undefined();
} }
static Handle<Value> 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<Value> SetKeepAlive(const Arguments& args) { static Handle<Value> SetKeepAlive(const Arguments& args) {
int r; int r;
@ -1065,6 +1080,7 @@ void InitNet(Handle<Object> target) {
NODE_SET_METHOD(target, "socketError", SocketError); NODE_SET_METHOD(target, "socketError", SocketError);
NODE_SET_METHOD(target, "toRead", ToRead); NODE_SET_METHOD(target, "toRead", ToRead);
NODE_SET_METHOD(target, "setNoDelay", SetNoDelay); NODE_SET_METHOD(target, "setNoDelay", SetNoDelay);
NODE_SET_METHOD(target, "setBroadcast", SetBroadcast);
NODE_SET_METHOD(target, "setKeepAlive", SetKeepAlive); NODE_SET_METHOD(target, "setKeepAlive", SetKeepAlive);
NODE_SET_METHOD(target, "getsockname", GetSockName); NODE_SET_METHOD(target, "getsockname", GetSockName);
NODE_SET_METHOD(target, "getpeername", GetPeerName); NODE_SET_METHOD(target, "getpeername", GetPeerName);

Loading…
Cancel
Save