2011-03-10 09:54:52 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
var util = require('util');
|
|
|
|
var fs = require('fs');
|
|
|
|
var events = require('events');
|
2010-06-12 11:40:24 +02:00
|
|
|
var dns = require('dns');
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
var IOWatcher = process.binding('io_watcher').IOWatcher;
|
|
|
|
var binding = process.binding('net');
|
|
|
|
var constants = process.binding('constants');
|
2010-10-23 03:28:26 +02:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
var socket = binding.socket;
|
|
|
|
var recvfrom = binding.recvfrom;
|
|
|
|
var close = binding.close;
|
2010-10-23 03:28:26 +02:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
var ENOENT = constants.ENOENT;
|
2010-06-12 11:40:24 +02:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
function isPort(x) { return parseInt(x) >= 0; }
|
2010-06-12 11:40:24 +02:00
|
|
|
var pool = null;
|
|
|
|
|
|
|
|
function getPool() {
|
|
|
|
/* TODO: this effectively limits you to 8kb maximum packet sizes */
|
|
|
|
var minPoolAvail = 1024 * 8;
|
|
|
|
|
|
|
|
var poolSize = 1024 * 64;
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
if (pool === null || (pool.used + minPoolAvail > pool.length)) {
|
2010-06-12 11:40:24 +02:00
|
|
|
pool = new Buffer(poolSize);
|
|
|
|
pool.used = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
2010-08-09 02:53:55 +02:00
|
|
|
function dnsLookup(type, hostname, callback) {
|
2010-12-02 02:29:11 +01:00
|
|
|
var family = (type ? ((type === 'udp6') ? 6 : 4) : null);
|
|
|
|
dns.lookup(hostname, family, function(err, ip, addressFamily) {
|
2010-08-09 02:53:55 +02:00
|
|
|
if (!err && family && addressFamily !== family) {
|
2010-12-02 02:29:11 +01:00
|
|
|
err = new Error('no address found in family ' + type +
|
|
|
|
' for ' + hostname);
|
2010-08-09 02:53:55 +02:00
|
|
|
}
|
|
|
|
callback(err, ip, addressFamily);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
function Socket(type, listener) {
|
2010-06-12 11:40:24 +02:00
|
|
|
events.EventEmitter.call(this);
|
|
|
|
var self = this;
|
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
self.type = type;
|
2010-12-02 02:29:11 +01:00
|
|
|
if (type === 'unix_dgram' || type === 'udp4' || type === 'udp6') {
|
|
|
|
self.fd = socket(self.type);
|
2010-07-05 22:38:13 +02:00
|
|
|
} else {
|
2010-12-02 02:29:11 +01:00
|
|
|
throw new Error('Bad socket type specified. Valid types are: ' +
|
|
|
|
'unix_dgram, udp4, udp6');
|
2010-06-16 19:42:55 +02:00
|
|
|
}
|
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
if (typeof listener === 'function') {
|
|
|
|
self.on('message', listener);
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.watcher = new IOWatcher();
|
|
|
|
self.watcher.host = self;
|
2010-12-02 02:29:11 +01:00
|
|
|
self.watcher.callback = function() {
|
2010-06-12 11:40:24 +02:00
|
|
|
while (self.fd) {
|
|
|
|
var p = getPool();
|
|
|
|
var rinfo = recvfrom(self.fd, p, p.used, p.length - p.used, 0);
|
|
|
|
|
|
|
|
if (!rinfo) return;
|
|
|
|
|
|
|
|
self.emit('message', p.slice(p.used, p.used + rinfo.size), rinfo);
|
|
|
|
|
|
|
|
p.used += rinfo.size;
|
|
|
|
}
|
|
|
|
};
|
2010-12-02 02:29:11 +01:00
|
|
|
|
|
|
|
if (self.type === 'udp4' || self.type === 'udp6') {
|
2010-07-05 22:38:13 +02:00
|
|
|
self._startWatcher();
|
|
|
|
}
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
2010-10-11 23:04:09 +02:00
|
|
|
util.inherits(Socket, events.EventEmitter);
|
2010-06-12 11:40:24 +02:00
|
|
|
exports.Socket = Socket;
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
exports.createSocket = function(type, listener) {
|
2010-07-05 22:38:13 +02:00
|
|
|
return new Socket(type, listener);
|
2010-06-12 11:40:24 +02:00
|
|
|
};
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
Socket.prototype.bind = function() {
|
2010-06-12 11:40:24 +02:00
|
|
|
var self = this;
|
2010-07-05 22:38:13 +02:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
if (this.type === 'unix_dgram') {
|
|
|
|
// bind(path)
|
|
|
|
if (typeof arguments[0] !== 'string') {
|
|
|
|
throw new Error('unix_dgram sockets must be bound to a path in ' +
|
|
|
|
'the filesystem');
|
2010-07-05 22:38:13 +02:00
|
|
|
}
|
|
|
|
this.path = arguments[0];
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
// unlink old file, OK if it doesn't exist
|
|
|
|
fs.unlink(this.path, function(err) {
|
2010-07-05 22:38:13 +02:00
|
|
|
if (err && err.errno !== ENOENT) {
|
|
|
|
throw err;
|
2010-06-12 11:40:24 +02:00
|
|
|
} else {
|
2010-07-05 22:38:13 +02:00
|
|
|
try {
|
|
|
|
binding.bind(self.fd, self.path);
|
|
|
|
self._startWatcher();
|
2010-12-02 02:29:11 +01:00
|
|
|
self.emit('listening');
|
2010-07-05 22:38:13 +02:00
|
|
|
} catch (err) {
|
2010-12-02 02:29:11 +01:00
|
|
|
console.log('Error in unix_dgram bind of ' + self.path);
|
2010-07-05 22:38:13 +02:00
|
|
|
console.log(err.stack);
|
|
|
|
throw err;
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2010-12-02 02:29:11 +01:00
|
|
|
} else if (this.type === 'udp4' || this.type === 'udp6') {
|
|
|
|
// bind(port, [address])
|
2010-07-05 22:38:13 +02:00
|
|
|
if (arguments[1] === undefined) {
|
|
|
|
// Not bind()ing a specific address. Use INADDR_ANY and OS will pick one.
|
|
|
|
// The address can be found with server.address()
|
|
|
|
binding.bind(self.fd, arguments[0]);
|
2010-12-02 02:29:11 +01:00
|
|
|
this.emit('listening');
|
2010-07-05 22:38:13 +02:00
|
|
|
} else {
|
|
|
|
// the first argument is the port, the second an address
|
|
|
|
this.port = arguments[0];
|
2010-12-02 02:29:11 +01:00
|
|
|
dnsLookup(this.type, arguments[1], function(err, ip, addressFamily) {
|
2010-08-09 02:53:55 +02:00
|
|
|
if (err) {
|
|
|
|
self.emit('error', err);
|
|
|
|
} else {
|
|
|
|
self.ip = ip;
|
|
|
|
binding.bind(self.fd, self.port, ip);
|
2010-12-02 02:29:11 +01:00
|
|
|
self.emit('listening');
|
2010-08-09 02:53:55 +02:00
|
|
|
}
|
|
|
|
});
|
2010-07-05 22:38:13 +02:00
|
|
|
}
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
Socket.prototype._startWatcher = function() {
|
2010-07-05 22:38:13 +02:00
|
|
|
if (! this._watcherStarted) {
|
2010-12-02 02:29:11 +01:00
|
|
|
// listen for read ready, not write ready
|
|
|
|
this.watcher.set(this.fd, true, false);
|
2010-07-05 22:38:13 +02:00
|
|
|
this.watcher.start();
|
|
|
|
this._watcherStarted = true;
|
|
|
|
}
|
2010-06-12 11:40:24 +02:00
|
|
|
};
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
Socket.prototype.address = function() {
|
2010-07-05 22:38:13 +02:00
|
|
|
return binding.getsockname(this.fd);
|
2010-06-12 11:40:24 +02:00
|
|
|
};
|
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
Socket.prototype.setBroadcast = function(arg) {
|
|
|
|
if (arg) {
|
|
|
|
return binding.setBroadcast(this.fd, 1);
|
|
|
|
} else {
|
|
|
|
return binding.setBroadcast(this.fd, 0);
|
|
|
|
}
|
|
|
|
};
|
2010-06-12 11:40:24 +02:00
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
Socket.prototype.setTTL = function(arg) {
|
|
|
|
var newttl = parseInt(arg);
|
2010-12-02 02:29:11 +01:00
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
if (newttl > 0 && newttl < 256) {
|
|
|
|
return binding.setTTL(this.fd, newttl);
|
|
|
|
} else {
|
2010-12-02 02:29:11 +01:00
|
|
|
throw new Error('New TTL must be between 1 and 255');
|
2010-07-05 22:38:13 +02:00
|
|
|
}
|
|
|
|
};
|
2010-06-16 20:00:43 +02:00
|
|
|
|
2011-01-24 08:39:54 +01:00
|
|
|
Socket.prototype.setMulticastTTL = function(arg) {
|
|
|
|
var newttl = parseInt(arg);
|
|
|
|
|
|
|
|
if (newttl >= 0 && newttl < 256) {
|
|
|
|
return binding.setMulticastTTL(this.fd, newttl);
|
|
|
|
} else {
|
|
|
|
throw new Error('New MulticastTTL must be between 0 and 255');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Socket.prototype.setMulticastLoopback = function(arg) {
|
|
|
|
if (arg) {
|
|
|
|
return binding.setMulticastLoopback(this.fd, 1);
|
|
|
|
} else {
|
|
|
|
return binding.setMulticastLoopback(this.fd, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Socket.prototype.addMembership = function(multicastAddress,
|
|
|
|
multicastInterface) {
|
|
|
|
var self = this;
|
|
|
|
dnsLookup(this.type, multicastAddress, function(err, ip, addressFamily) {
|
|
|
|
if (err) { // DNS error
|
|
|
|
self.emit('error', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
binding.addMembership(self.fd, multicastAddress, multicastInterface);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Socket.prototype.dropMembership = function(multicastAddress,
|
|
|
|
multicastInterface) {
|
|
|
|
var self = this;
|
|
|
|
dnsLookup(this.type, multicastAddress, function(err, ip, addressFamily) {
|
|
|
|
if (err) { // DNS error
|
|
|
|
self.emit('error', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
binding.dropMembership(self.fd, multicastAddress, multicastInterface);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2010-08-09 02:53:55 +02:00
|
|
|
// translate arguments from JS API into C++ API, possibly after DNS lookup
|
2010-07-05 22:38:13 +02:00
|
|
|
Socket.prototype.send = function(buffer, offset, length) {
|
|
|
|
var self = this;
|
2010-06-16 20:00:43 +02:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
if (typeof offset !== 'number' || typeof length !== 'number') {
|
|
|
|
throw new Error('send takes offset and length as args 2 and 3');
|
2010-07-05 22:38:13 +02:00
|
|
|
}
|
2010-06-16 20:00:43 +02:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
if (this.type === 'unix_dgram') {
|
|
|
|
// send(buffer, offset, length, path [, callback])
|
|
|
|
if (typeof arguments[3] !== 'string') {
|
|
|
|
throw new Error('unix_dgram sockets must send to a path ' +
|
|
|
|
'in the filesystem');
|
2010-07-05 22:38:13 +02:00
|
|
|
}
|
2010-06-16 20:00:43 +02:00
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
self.sendto(buffer, offset, length, arguments[3], null, arguments[4]);
|
2010-12-02 02:29:11 +01:00
|
|
|
} else if (this.type === 'udp4' || this.type === 'udp6') {
|
|
|
|
// send(buffer, offset, length, port, address [, callback])
|
|
|
|
if (typeof arguments[4] !== 'string') {
|
|
|
|
throw new Error(this.type + ' sockets must send to port, address');
|
2010-07-05 22:38:13 +02:00
|
|
|
}
|
2010-12-02 02:29:11 +01:00
|
|
|
|
2010-09-16 23:58:04 +02:00
|
|
|
if (binding.isIP(arguments[4])) {
|
2010-08-09 02:53:55 +02:00
|
|
|
self.sendto(arguments[0], arguments[1], arguments[2], arguments[3],
|
|
|
|
arguments[4], arguments[5]);
|
2010-07-05 22:38:13 +02:00
|
|
|
} else {
|
|
|
|
var port = arguments[3],
|
|
|
|
callback = arguments[5];
|
2010-12-02 02:29:11 +01:00
|
|
|
dnsLookup(this.type, arguments[4], function(err, ip, addressFamily) {
|
2010-07-05 22:38:13 +02:00
|
|
|
if (err) { // DNS error
|
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
self.emit('error', err);
|
|
|
|
return;
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
2010-07-05 22:38:13 +02:00
|
|
|
self.sendto(buffer, offset, length, port, ip, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2010-06-16 20:00:43 +02:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
Socket.prototype.sendto = function(buffer,
|
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
port,
|
|
|
|
addr,
|
|
|
|
callback) {
|
2010-07-05 22:38:13 +02:00
|
|
|
try {
|
|
|
|
var bytes = binding.sendto(this.fd, buffer, offset, length, 0, port, addr);
|
|
|
|
} catch (err) {
|
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback(null, bytes);
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
Socket.prototype.close = function() {
|
2010-06-12 11:40:24 +02:00
|
|
|
var self = this;
|
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
if (!this.fd) throw new Error('Not running');
|
2010-06-12 11:40:24 +02:00
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
this.watcher.stop();
|
|
|
|
this._watcherStarted = false;
|
2010-06-12 11:40:24 +02:00
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
close(this.fd);
|
|
|
|
this.fd = null;
|
2010-06-12 11:40:24 +02:00
|
|
|
|
2010-12-02 02:29:11 +01:00
|
|
|
if (this.type === 'unix_dgram' && this.path) {
|
|
|
|
fs.unlink(this.path, function() {
|
|
|
|
self.emit('close');
|
2010-06-12 11:40:24 +02:00
|
|
|
});
|
|
|
|
} else {
|
2010-12-02 02:29:11 +01:00
|
|
|
this.emit('close');
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
|
|
|
};
|