mirror of https://github.com/nodejs/node.git
net: improve arbitrary tcp socket support
Consider this example: // fd 3 is a bound tcp socket var s = net.createServer(cb); s.listen({ fd: 3 }); console.log(s.address()); // prints null This commit makes net.Server#address() print the actual address. Ditto for non-listen sockets; properties like net.Socket#localAddress and net.Socket#remoteAddress now return the correct value. Fixes #5009.pull/24507/merge
parent
e99dff4617
commit
ca5022b8f1
41
lib/net.js
41
lib/net.js
|
@ -42,6 +42,15 @@ function createTCP() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function createHandle(fd) {
|
||||||
|
var tty = process.binding('tty_wrap');
|
||||||
|
var type = tty.guessHandleType(fd);
|
||||||
|
if (type === 'PIPE') return createPipe();
|
||||||
|
if (type === 'TCP') return createTCP();
|
||||||
|
throw new TypeError('Unsupported fd type: ' + type);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var debug;
|
var debug;
|
||||||
if (process.env.NODE_DEBUG && /net/.test(process.env.NODE_DEBUG)) {
|
if (process.env.NODE_DEBUG && /net/.test(process.env.NODE_DEBUG)) {
|
||||||
var pid = process.pid;
|
var pid = process.pid;
|
||||||
|
@ -144,7 +153,7 @@ function Socket(options) {
|
||||||
if (options.handle) {
|
if (options.handle) {
|
||||||
this._handle = options.handle; // private
|
this._handle = options.handle; // private
|
||||||
} else if (typeof options.fd !== 'undefined') {
|
} else if (typeof options.fd !== 'undefined') {
|
||||||
this._handle = createPipe();
|
this._handle = createHandle(options.fd);
|
||||||
this._handle.open(options.fd);
|
this._handle.open(options.fd);
|
||||||
this.readable = options.readable !== false;
|
this.readable = options.readable !== false;
|
||||||
this.writable = options.writable !== false;
|
this.writable = options.writable !== false;
|
||||||
|
@ -930,26 +939,18 @@ var createServerHandle = exports._createServerHandle =
|
||||||
var handle;
|
var handle;
|
||||||
|
|
||||||
if (typeof fd === 'number' && fd >= 0) {
|
if (typeof fd === 'number' && fd >= 0) {
|
||||||
var tty_wrap = process.binding('tty_wrap');
|
try {
|
||||||
var type = tty_wrap.guessHandleType(fd);
|
handle = createHandle(fd);
|
||||||
switch (type) {
|
|
||||||
case 'PIPE':
|
|
||||||
case 'TCP':
|
|
||||||
debug('listen pipe fd=' + fd);
|
|
||||||
// create a PipeWrap
|
|
||||||
handle = createPipe();
|
|
||||||
handle.open(fd);
|
|
||||||
handle.readable = true;
|
|
||||||
handle.writable = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// Not a fd we can listen on. This will trigger an error.
|
|
||||||
debug('listen invalid fd=' + fd + ' type=' + type);
|
|
||||||
process._errno = 'EINVAL'; // hack, callers expect that errno is set
|
|
||||||
handle = null;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
catch (e) {
|
||||||
|
// Not a fd we can listen on. This will trigger an error.
|
||||||
|
debug('listen invalid fd=' + fd + ': ' + e.message);
|
||||||
|
process._errno = 'EINVAL'; // hack, callers expect that errno is set
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
handle.open(fd);
|
||||||
|
handle.readable = true;
|
||||||
|
handle.writable = true;
|
||||||
return handle;
|
return handle;
|
||||||
|
|
||||||
} else if (port == -1 && addressType == -1) {
|
} else if (port == -1 && addressType == -1) {
|
||||||
|
|
|
@ -104,6 +104,7 @@ void TCPWrap::Initialize(Handle<Object> target) {
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
|
NODE_SET_PROTOTYPE_METHOD(t, "writeUtf8String", StreamWrap::WriteUtf8String);
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
|
NODE_SET_PROTOTYPE_METHOD(t, "writeUcs2String", StreamWrap::WriteUcs2String);
|
||||||
|
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(t, "open", Open);
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
|
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
|
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
|
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
|
||||||
|
@ -256,6 +257,15 @@ Handle<Value> TCPWrap::SetSimultaneousAccepts(const Arguments& args) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
Handle<Value> TCPWrap::Open(const Arguments& args) {
|
||||||
|
HandleScope scope;
|
||||||
|
UNWRAP(TCPWrap)
|
||||||
|
int fd = args[0]->IntegerValue();
|
||||||
|
uv_tcp_open(&wrap->handle_, fd);
|
||||||
|
return Null();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Handle<Value> TCPWrap::Bind(const Arguments& args) {
|
Handle<Value> TCPWrap::Bind(const Arguments& args) {
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue