mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
312 lines
8.8 KiB
312 lines
8.8 KiB
14 years ago
|
// 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.
|
||
|
|
||
14 years ago
|
var util = require('util');
|
||
|
var fs = require('fs');
|
||
|
var events = require('events');
|
||
15 years ago
|
var dns = require('dns');
|
||
|
|
||
14 years ago
|
var IOWatcher = process.binding('io_watcher').IOWatcher;
|
||
|
var binding = process.binding('net');
|
||
|
var constants = process.binding('constants');
|
||
14 years ago
|
|
||
14 years ago
|
var socket = binding.socket;
|
||
|
var recvfrom = binding.recvfrom;
|
||
|
var close = binding.close;
|
||
14 years ago
|
|
||
14 years ago
|
var ENOENT = constants.ENOENT;
|
||
15 years ago
|
|
||
14 years ago
|
function isPort(x) { return parseInt(x) >= 0; }
|
||
15 years ago
|
var pool = null;
|
||
|
|
||
|
function getPool() {
|
||
|
/* TODO: this effectively limits you to 8kb maximum packet sizes */
|
||
|
var minPoolAvail = 1024 * 8;
|
||
|
|
||
|
var poolSize = 1024 * 64;
|
||
|
|
||
14 years ago
|
if (pool === null || (pool.used + minPoolAvail > pool.length)) {
|
||
15 years ago
|
pool = new Buffer(poolSize);
|
||
|
pool.used = 0;
|
||
|
}
|
||
|
|
||
|
return pool;
|
||
|
}
|
||
|
|
||
15 years ago
|
function dnsLookup(type, hostname, callback) {
|
||
14 years ago
|
var family = (type ? ((type === 'udp6') ? 6 : 4) : null);
|
||
|
dns.lookup(hostname, family, function(err, ip, addressFamily) {
|
||
15 years ago
|
if (!err && family && addressFamily !== family) {
|
||
14 years ago
|
err = new Error('no address found in family ' + type +
|
||
|
' for ' + hostname);
|
||
15 years ago
|
}
|
||
|
callback(err, ip, addressFamily);
|
||
|
});
|
||
|
}
|
||
|
|
||
14 years ago
|
function Socket(type, listener) {
|
||
15 years ago
|
events.EventEmitter.call(this);
|
||
|
var self = this;
|
||
|
|
||
15 years ago
|
self.type = type;
|
||
14 years ago
|
if (type === 'unix_dgram' || type === 'udp4' || type === 'udp6') {
|
||
|
self.fd = socket(self.type);
|
||
15 years ago
|
} else {
|
||
14 years ago
|
throw new Error('Bad socket type specified. Valid types are: ' +
|
||
|
'unix_dgram, udp4, udp6');
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
if (typeof listener === 'function') {
|
||
|
self.on('message', listener);
|
||
15 years ago
|
}
|
||
|
|
||
|
self.watcher = new IOWatcher();
|
||
|
self.watcher.host = self;
|
||
14 years ago
|
self.watcher.callback = function() {
|
||
15 years ago
|
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;
|
||
|
}
|
||
|
};
|
||
14 years ago
|
|
||
|
if (self.type === 'udp4' || self.type === 'udp6') {
|
||
15 years ago
|
self._startWatcher();
|
||
|
}
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
util.inherits(Socket, events.EventEmitter);
|
||
15 years ago
|
exports.Socket = Socket;
|
||
|
|
||
14 years ago
|
exports.createSocket = function(type, listener) {
|
||
15 years ago
|
return new Socket(type, listener);
|
||
15 years ago
|
};
|
||
|
|
||
14 years ago
|
Socket.prototype.bind = function() {
|
||
15 years ago
|
var self = this;
|
||
15 years ago
|
|
||
14 years ago
|
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');
|
||
15 years ago
|
}
|
||
|
this.path = arguments[0];
|
||
|
|
||
14 years ago
|
// unlink old file, OK if it doesn't exist
|
||
|
fs.unlink(this.path, function(err) {
|
||
15 years ago
|
if (err && err.errno !== ENOENT) {
|
||
|
throw err;
|
||
15 years ago
|
} else {
|
||
15 years ago
|
try {
|
||
|
binding.bind(self.fd, self.path);
|
||
|
self._startWatcher();
|
||
14 years ago
|
self.emit('listening');
|
||
15 years ago
|
} catch (err) {
|
||
14 years ago
|
console.log('Error in unix_dgram bind of ' + self.path);
|
||
15 years ago
|
console.log(err.stack);
|
||
|
throw err;
|
||
15 years ago
|
}
|
||
|
}
|
||
|
});
|
||
14 years ago
|
} else if (this.type === 'udp4' || this.type === 'udp6') {
|
||
|
// bind(port, [address])
|
||
15 years ago
|
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]);
|
||
14 years ago
|
this.emit('listening');
|
||
15 years ago
|
} else {
|
||
|
// the first argument is the port, the second an address
|
||
|
this.port = arguments[0];
|
||
14 years ago
|
dnsLookup(this.type, arguments[1], function(err, ip, addressFamily) {
|
||
15 years ago
|
if (err) {
|
||
|
self.emit('error', err);
|
||
|
} else {
|
||
|
self.ip = ip;
|
||
|
binding.bind(self.fd, self.port, ip);
|
||
14 years ago
|
self.emit('listening');
|
||
15 years ago
|
}
|
||
|
});
|
||
15 years ago
|
}
|
||
15 years ago
|
}
|
||
|
};
|
||
|
|
||
14 years ago
|
Socket.prototype._startWatcher = function() {
|
||
15 years ago
|
if (! this._watcherStarted) {
|
||
14 years ago
|
// listen for read ready, not write ready
|
||
|
this.watcher.set(this.fd, true, false);
|
||
15 years ago
|
this.watcher.start();
|
||
|
this._watcherStarted = true;
|
||
|
}
|
||
15 years ago
|
};
|
||
|
|
||
14 years ago
|
Socket.prototype.address = function() {
|
||
15 years ago
|
return binding.getsockname(this.fd);
|
||
15 years ago
|
};
|
||
|
|
||
15 years ago
|
Socket.prototype.setBroadcast = function(arg) {
|
||
|
if (arg) {
|
||
|
return binding.setBroadcast(this.fd, 1);
|
||
|
} else {
|
||
|
return binding.setBroadcast(this.fd, 0);
|
||
|
}
|
||
|
};
|
||
15 years ago
|
|
||
15 years ago
|
Socket.prototype.setTTL = function(arg) {
|
||
|
var newttl = parseInt(arg);
|
||
14 years ago
|
|
||
15 years ago
|
if (newttl > 0 && newttl < 256) {
|
||
|
return binding.setTTL(this.fd, newttl);
|
||
|
} else {
|
||
14 years ago
|
throw new Error('New TTL must be between 1 and 255');
|
||
15 years ago
|
}
|
||
|
};
|
||
15 years ago
|
|
||
14 years ago
|
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);
|
||
|
});
|
||
|
};
|
||
|
|
||
15 years ago
|
// translate arguments from JS API into C++ API, possibly after DNS lookup
|
||
15 years ago
|
Socket.prototype.send = function(buffer, offset, length) {
|
||
|
var self = this;
|
||
15 years ago
|
|
||
14 years ago
|
if (typeof offset !== 'number' || typeof length !== 'number') {
|
||
|
throw new Error('send takes offset and length as args 2 and 3');
|
||
15 years ago
|
}
|
||
15 years ago
|
|
||
14 years ago
|
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');
|
||
15 years ago
|
}
|
||
15 years ago
|
|
||
15 years ago
|
self.sendto(buffer, offset, length, arguments[3], null, arguments[4]);
|
||
14 years ago
|
} 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');
|
||
15 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
if (binding.isIP(arguments[4])) {
|
||
15 years ago
|
self.sendto(arguments[0], arguments[1], arguments[2], arguments[3],
|
||
|
arguments[4], arguments[5]);
|
||
15 years ago
|
} else {
|
||
|
var port = arguments[3],
|
||
|
callback = arguments[5];
|
||
14 years ago
|
dnsLookup(this.type, arguments[4], function(err, ip, addressFamily) {
|
||
15 years ago
|
if (err) { // DNS error
|
||
|
if (callback) {
|
||
|
callback(err);
|
||
|
}
|
||
|
self.emit('error', err);
|
||
|
return;
|
||
15 years ago
|
}
|
||
15 years ago
|
self.sendto(buffer, offset, length, port, ip, callback);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
};
|
||
15 years ago
|
|
||
14 years ago
|
Socket.prototype.sendto = function(buffer,
|
||
|
offset,
|
||
|
length,
|
||
|
port,
|
||
|
addr,
|
||
|
callback) {
|
||
15 years ago
|
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);
|
||
15 years ago
|
}
|
||
|
};
|
||
|
|
||
14 years ago
|
Socket.prototype.close = function() {
|
||
15 years ago
|
var self = this;
|
||
|
|
||
15 years ago
|
if (!this.fd) throw new Error('Not running');
|
||
15 years ago
|
|
||
15 years ago
|
this.watcher.stop();
|
||
|
this._watcherStarted = false;
|
||
15 years ago
|
|
||
15 years ago
|
close(this.fd);
|
||
|
this.fd = null;
|
||
15 years ago
|
|
||
14 years ago
|
if (this.type === 'unix_dgram' && this.path) {
|
||
|
fs.unlink(this.path, function() {
|
||
|
self.emit('close');
|
||
15 years ago
|
});
|
||
|
} else {
|
||
14 years ago
|
this.emit('close');
|
||
15 years ago
|
}
|
||
|
};
|