2018-07-21 04:56:12 +00:00
|
|
|
'use strict';
|
2018-07-21 05:17:21 +00:00
|
|
|
const { codes } = require('internal/errors');
|
2018-08-23 13:49:35 +00:00
|
|
|
const { UDP } = internalBinding('udp_wrap');
|
2018-07-29 14:41:11 +00:00
|
|
|
const { isInt32 } = require('internal/validators');
|
2018-08-23 14:20:12 +00:00
|
|
|
const TTYWrap = internalBinding('tty_wrap');
|
2018-08-06 21:40:30 +00:00
|
|
|
const { UV_EINVAL } = internalBinding('uv');
|
2018-07-21 05:17:21 +00:00
|
|
|
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
|
2018-07-21 04:56:12 +00:00
|
|
|
const kStateSymbol = Symbol('state symbol');
|
2018-07-21 05:17:21 +00:00
|
|
|
let dns; // Lazy load for startup performance.
|
2018-07-21 04:56:12 +00:00
|
|
|
|
2018-07-21 05:17:21 +00:00
|
|
|
|
|
|
|
function lookup4(lookup, address, callback) {
|
|
|
|
return lookup(address || '127.0.0.1', 4, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function lookup6(lookup, address, callback) {
|
|
|
|
return lookup(address || '::1', 6, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-29 14:41:11 +00:00
|
|
|
const guessHandleType = TTYWrap.guessHandleType;
|
|
|
|
|
|
|
|
|
2018-07-21 05:17:21 +00:00
|
|
|
function newHandle(type, lookup) {
|
|
|
|
if (lookup === undefined) {
|
|
|
|
if (dns === undefined) {
|
|
|
|
dns = require('dns');
|
|
|
|
}
|
|
|
|
|
|
|
|
lookup = dns.lookup;
|
|
|
|
} else if (typeof lookup !== 'function') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'udp4') {
|
|
|
|
const handle = new UDP();
|
|
|
|
|
|
|
|
handle.lookup = lookup4.bind(handle, lookup);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'udp6') {
|
|
|
|
const handle = new UDP();
|
|
|
|
|
|
|
|
handle.lookup = lookup6.bind(handle, lookup);
|
|
|
|
handle.bind = handle.bind6;
|
2019-03-16 22:03:48 +00:00
|
|
|
handle.connect = handle.connect6;
|
2018-07-21 05:17:21 +00:00
|
|
|
handle.send = handle.send6;
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ERR_SOCKET_BAD_TYPE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _createSocketHandle(address, port, addressType, fd, flags) {
|
|
|
|
const handle = newHandle(addressType);
|
2018-07-29 14:41:11 +00:00
|
|
|
let err;
|
|
|
|
|
|
|
|
if (isInt32(fd) && fd > 0) {
|
|
|
|
const type = guessHandleType(fd);
|
|
|
|
if (type !== 'UDP') {
|
|
|
|
err = UV_EINVAL;
|
|
|
|
} else {
|
|
|
|
err = handle.open(fd);
|
2018-07-21 05:17:21 +00:00
|
|
|
}
|
2018-07-29 14:41:11 +00:00
|
|
|
} else if (port || address) {
|
|
|
|
err = handle.bind(address, port || 0, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
handle.close();
|
|
|
|
return err;
|
2018-07-21 05:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-29 14:41:11 +00:00
|
|
|
module.exports = {
|
|
|
|
kStateSymbol,
|
|
|
|
_createSocketHandle,
|
|
|
|
newHandle,
|
|
|
|
guessHandleType,
|
|
|
|
};
|