diff options
author | Ouyang Yadong <oyydoibh@gmail.com> | 2018-07-29 22:41:11 +0800 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-08-06 11:05:51 +0200 |
commit | 2bea9cefbc10ed1dd497bbae61c07d971da287dd (patch) | |
tree | 02bfcbc3fcf39fe7524f9271762617f23e107925 /lib/internal/dgram.js | |
parent | 214844ea4e561b0f77e97643d28b251cdc574089 (diff) | |
download | android-node-v8-2bea9cefbc10ed1dd497bbae61c07d971da287dd.tar.gz android-node-v8-2bea9cefbc10ed1dd497bbae61c07d971da287dd.tar.bz2 android-node-v8-2bea9cefbc10ed1dd497bbae61c07d971da287dd.zip |
dgram: implement socket.bind({ fd })
dgram: Implement binding an existing `fd`. Allow pass a `fd` property
to `socket.bind()` in dgram.
src: Add `UDPWrap::Open`
PR-URL: https://github.com/nodejs/node/pull/21745
Fixes: https://github.com/nodejs/node/issues/14961
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib/internal/dgram.js')
-rw-r--r-- | lib/internal/dgram.js | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/lib/internal/dgram.js b/lib/internal/dgram.js index 82b65294c2..e9b5364c8e 100644 --- a/lib/internal/dgram.js +++ b/lib/internal/dgram.js @@ -1,7 +1,9 @@ 'use strict'; -const assert = require('assert'); const { codes } = require('internal/errors'); const { UDP } = process.binding('udp_wrap'); +const { isInt32 } = require('internal/validators'); +const TTYWrap = process.binding('tty_wrap'); +const { UV_EINVAL } = process.binding('uv'); const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes; const kStateSymbol = Symbol('state symbol'); let dns; // Lazy load for startup performance. @@ -17,6 +19,9 @@ function lookup6(lookup, address, callback) { } +const guessHandleType = TTYWrap.guessHandleType; + + function newHandle(type, lookup) { if (lookup === undefined) { if (dns === undefined) { @@ -49,22 +54,32 @@ function newHandle(type, lookup) { function _createSocketHandle(address, port, addressType, fd, flags) { - // Opening an existing fd is not supported for UDP handles. - assert(typeof fd !== 'number' || fd < 0); - const handle = newHandle(addressType); - - if (port || address) { - const err = handle.bind(address, port || 0, flags); - - if (err) { - handle.close(); - return err; + let err; + + if (isInt32(fd) && fd > 0) { + const type = guessHandleType(fd); + if (type !== 'UDP') { + err = UV_EINVAL; + } else { + err = handle.open(fd); } + } else if (port || address) { + err = handle.bind(address, port || 0, flags); + } + + if (err) { + handle.close(); + return err; } return handle; } -module.exports = { kStateSymbol, _createSocketHandle, newHandle }; +module.exports = { + kStateSymbol, + _createSocketHandle, + newHandle, + guessHandleType, +}; |