GeezerVison is showing github.com — lib,src: add unix socket getsockname/getpeername · nodejs/node@f337595
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/net.markdown +15-5Lines changed: 15 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -255,6 +255,9 @@ Example: | 255 | 255 |
| 256 | 256 | Don't call `server.address()` until the `'listening'` event has been emitted. | 257 | 257 |
| | 258 | +This method used to return the file path as a string for UNIX sockets and | | 259 | +Windows pipes. As of io.js v1.5.0, it returns the expected object. | | 260 | + | 258 | 261 | ### server.unref() | 259 | 262 |
| 260 | 263 | Calling `unref` on a server will allow the program to exit if this is the only | @@ -508,25 +511,32 @@ Returns `socket`. | 508 | 511 | The string representation of the remote IP address. For example, | 509 | 512 | `'74.125.127.100'` or `'2001:4860:a005::68'`. | 510 | 513 |
| | 514 | +For UNIX sockets and Windows pipes, the file path the socket is connected | | 515 | +to. The remote address for server sockets is always `''`, the empty string. | | 516 | + | 511 | 517 | ### socket.remoteFamily | 512 | 518 |
| 513 | | -The string representation of the remote IP family. `'IPv4'` or `'IPv6'`. | | 519 | +The string representation of the remote IP family. `'IPv4'` or `'IPv6'` | | 520 | +for TCP sockets, `'pipe'` for UNIX sockets and Windows pipes. | 514 | 521 |
| 515 | 522 | ### socket.remotePort | 516 | 523 |
| 517 | | -The numeric representation of the remote port. For example, | 518 | | -`80` or `21`. | | 524 | +The numeric representation of the remote port. For example, `80` or `21`. | | 525 | +`undefined` for UNIX sockets and Windows pipes. | 519 | 526 |
| 520 | 527 | ### socket.localAddress | 521 | 528 |
| 522 | 529 | The string representation of the local IP address the remote client is | 523 | 530 | connecting on. For example, if you are listening on `'0.0.0.0'` and the | 524 | 531 | client connects on `'192.168.1.1'`, the value would be `'192.168.1.1'`. | 525 | 532 |
| | 533 | +For UNIX sockets and Windows pipes, the file path the socket is listening | | 534 | +on. The local address for client sockets is always `''`, the empty string. | | 535 | + | 526 | 536 | ### socket.localPort | 527 | 537 |
| 528 | | -The numeric representation of the local port. For example, | 529 | | -`80` or `21`. | | 538 | +The numeric representation of the local port. For example, `80` or `21`. | | 539 | +`undefined` for UNIX sockets and Windows pipes. | 530 | 540 |
| 531 | 541 | ### socket.bytesRead | 532 | 542 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/net.js +2-6Lines changed: 2 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1339,16 +1339,14 @@ Server.prototype.listen = function() { | 1339 | 1339 | else | 1340 | 1340 | listen(self, null, h.port | 0, 4, backlog, undefined, h.exclusive); | 1341 | 1341 | } else if (h.path && isPipeName(h.path)) { | 1342 | | - var pipeName = self._pipeName = h.path; | 1343 | | - listen(self, pipeName, -1, -1, backlog, undefined, h.exclusive); | | 1342 | + listen(self, h.path, -1, -1, backlog, undefined, h.exclusive); | 1344 | 1343 | } else { | 1345 | 1344 | throw new Error('Invalid listen argument: ' + h); | 1346 | 1345 | } | 1347 | 1346 | } | 1348 | 1347 | } else if (isPipeName(arguments[0])) { | 1349 | 1348 | // UNIX socket or Windows pipe. | 1350 | | - var pipeName = self._pipeName = arguments[0]; | 1351 | | - listen(self, pipeName, -1, -1, backlog); | | 1349 | + listen(self, arguments[0], -1, -1, backlog); | 1352 | 1350 |
| 1353 | 1351 | } else if (arguments[1] === undefined || | 1354 | 1352 | typeof arguments[1] === 'function' || | @@ -1381,8 +1379,6 @@ Server.prototype.address = function() { | 1381 | 1379 | this._handle.getsockname(out); | 1382 | 1380 | // TODO(bnoordhuis) Check err and throw? | 1383 | 1381 | return out; | 1384 | | - } else if (this._pipeName) { | 1385 | | - return this._pipeName; | 1386 | 1382 | } else { | 1387 | 1383 | return null; | 1388 | 1384 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/pipe_wrap.cc +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -89,6 +89,10 @@ void PipeWrap::Initialize(Handle<Object> target, | 89 | 89 | env->SetProtoMethod(t, "listen", Listen); | 90 | 90 | env->SetProtoMethod(t, "connect", Connect); | 91 | 91 | env->SetProtoMethod(t, "open", Open); | | 92 | + env->SetProtoMethod(t, "getpeername", | | 93 | + GetSockOrPeerName<uv_pipe_getpeername>); | | 94 | + env->SetProtoMethod(t, "getsockname", | | 95 | + GetSockOrPeerName<uv_pipe_getsockname>); | 92 | 96 |
| 93 | 97 | #ifdef _WIN32 | 94 | 98 | env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances); | @@ -276,6 +280,28 @@ void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) { | 276 | 280 | } | 277 | 281 |
| 278 | 282 |
| | 283 | +template <int (*F)(const uv_pipe_t*, char*, size_t*)> | | 284 | +void PipeWrap::GetSockOrPeerName( | | 285 | + const v8::FunctionCallbackInfo<v8::Value>& args) { | | 286 | + CHECK(args[0]->IsObject()); | | 287 | + char buffer[1024]; | | 288 | + size_t size = sizeof(buffer); | | 289 | + const PipeWrap* wrap = Unwrap<PipeWrap>(args.Holder()); | | 290 | + const int err = F(&wrap->handle_, buffer, &size); | | 291 | + if (err == 0) { | | 292 | + const uint8_t* data = reinterpret_cast<const uint8_t*>(buffer); | | 293 | + const String::NewStringType type = String::kNormalString; | | 294 | + Local<String> path = | | 295 | + String::NewFromOneByte(args.GetIsolate(), data, type, size); | | 296 | + Environment* env = Environment::GetCurrent(args); | | 297 | + Local<Object> out = args[0].As<Object>(); | | 298 | + out->Set(env->address_string(), path); | | 299 | + out->Set(env->family_string(), env->pipe_string()); | | 300 | + } | | 301 | + args.GetReturnValue().Set(err); | | 302 | +} | | 303 | + | | 304 | + | 279 | 305 | } // namespace node | 280 | 306 |
| 281 | 307 | NODE_MODULE_CONTEXT_AWARE_BUILTIN(pipe_wrap, node::PipeWrap::Initialize) |
Collapse file Copy file name to clipboardExpand all lines: src/pipe_wrap.h +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,9 @@ class PipeWrap : public StreamWrap { | 30 | 30 | static void Connect(const v8::FunctionCallbackInfo<v8::Value>& args); | 31 | 31 | static void Open(const v8::FunctionCallbackInfo<v8::Value>& args); | 32 | 32 |
| | 33 | + template <int (*F)(const uv_pipe_t*, char*, size_t*)> | | 34 | + static void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>&); | | 35 | + | 33 | 36 | #ifdef _WIN32 | 34 | 37 | static void SetPendingInstances( | 35 | 38 | const v8::FunctionCallbackInfo<v8::Value>& args); | |
Collapse file Copy file name to clipboardExpand all lines: test/common.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -134,7 +134,7 @@ Object.defineProperty(exports, 'hasCrypto', {get: function() { | 134 | 134 | }}); | 135 | 135 |
| 136 | 136 | if (exports.isWindows) { | 137 | | - exports.PIPE = '\\\\.\\pipe\\libuv-test'; | | 137 | + exports.PIPE = '\\\\?\\pipe\\libuv-test'; | 138 | 138 | } else { | 139 | 139 | exports.PIPE = exports.tmpDir + '/test.sock'; | 140 | 140 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cluster-http-pipe.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,8 +29,11 @@ if (cluster.isMaster) { | 29 | 29 | } | 30 | 30 |
| 31 | 31 | http.createServer(function(req, res) { | 32 | | - assert.equal(req.connection.remoteAddress, undefined); | 33 | | - assert.equal(req.connection.localAddress, undefined); // TODO common.PIPE? | | 32 | + assert.equal(req.connection.remoteAddress, ''); | | 33 | + assert.equal(req.connection.remoteFamily, 'pipe'); | | 34 | + assert.equal(req.connection.remotePort, undefined); | | 35 | + assert.equal(req.connection.localAddress, common.PIPE); | | 36 | + assert.equal(req.connection.localPort, undefined); | 34 | 37 | res.writeHead(200); | 35 | 38 | res.end('OK'); | 36 | 39 | }).listen(common.PIPE, function() { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http-unix-socket.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ var headers_ok = false; | 9 | 9 | var body_ok = false; | 10 | 10 |
| 11 | 11 | var server = http.createServer(function(req, res) { | | 12 | + assert.equal(req.socket.address().address, common.PIPE); | 12 | 13 | res.writeHead(200, { | 13 | 14 | 'Content-Type': 'text/plain', | 14 | 15 | 'Connection': 'close' | @@ -19,6 +20,7 @@ var server = http.createServer(function(req, res) { | 19 | 20 | }); | 20 | 21 |
| 21 | 22 | server.listen(common.PIPE, function() { | | 23 | + assert.equal(server.address().address, common.PIPE); | 22 | 24 |
| 23 | 25 | var options = { | 24 | 26 | socketPath: common.PIPE, | |
Collapse file Copy file name to clipboardExpand all lines: test/sequential/test-pipe-address.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,5 +15,6 @@ server.listen(common.PIPE, function() { | 15 | 15 | }); | 16 | 16 |
| 17 | 17 | process.on('exit', function() { | 18 | | - assert.equal(address, common.PIPE); | | 18 | + assert.equal(address.address, common.PIPE); | | 19 | + assert.equal(address.family, 'pipe'); | 19 | 20 | }); |
|