2018-07-21 04:56:12 +00:00
|
|
|
'use strict';
|
2019-11-30 15:55:29 +00:00
|
|
|
|
|
|
|
const {
|
2020-11-19 23:15:02 +00:00
|
|
|
FunctionPrototypeBind,
|
2019-11-30 15:55:29 +00:00
|
|
|
Symbol,
|
|
|
|
} = primordials;
|
|
|
|
|
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');
|
2019-04-18 02:46:39 +00:00
|
|
|
const { guessHandleType } = internalBinding('util');
|
2021-01-24 07:46:24 +00:00
|
|
|
const {
|
|
|
|
isInt32,
|
|
|
|
validateFunction,
|
|
|
|
} = require('internal/validators');
|
2018-08-06 21:40:30 +00:00
|
|
|
const { UV_EINVAL } = internalBinding('uv');
|
2021-01-24 07:46:24 +00:00
|
|
|
const {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function newHandle(type, lookup) {
|
|
|
|
if (lookup === undefined) {
|
|
|
|
if (dns === undefined) {
|
|
|
|
dns = require('dns');
|
|
|
|
}
|
|
|
|
|
|
|
|
lookup = dns.lookup;
|
2021-01-24 07:46:24 +00:00
|
|
|
} else {
|
|
|
|
validateFunction(lookup, 'lookup');
|
2018-07-21 05:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'udp4') {
|
|
|
|
const handle = new UDP();
|
|
|
|
|
2020-11-19 23:15:02 +00:00
|
|
|
handle.lookup = FunctionPrototypeBind(lookup4, handle, lookup);
|
2018-07-21 05:17:21 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'udp6') {
|
|
|
|
const handle = new UDP();
|
|
|
|
|
2020-11-19 23:15:02 +00:00
|
|
|
handle.lookup = FunctionPrototypeBind(lookup6, handle, lookup);
|
2018-07-21 05:17:21 +00:00
|
|
|
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,
|
2019-04-18 02:46:39 +00:00
|
|
|
newHandle
|
2018-07-29 14:41:11 +00:00
|
|
|
};
|