2018-07-21 06:56:12 +02:00
|
|
|
'use strict';
|
2018-07-21 07:17:21 +02:00
|
|
|
const { codes } = require('internal/errors');
|
2018-08-23 15:49:35 +02:00
|
|
|
const { UDP } = internalBinding('udp_wrap');
|
2019-04-18 04:46:39 +02:00
|
|
|
const { guessHandleType } = internalBinding('util');
|
2018-07-29 16:41:11 +02:00
|
|
|
const { isInt32 } = require('internal/validators');
|
2018-08-06 23:40:30 +02:00
|
|
|
const { UV_EINVAL } = internalBinding('uv');
|
2018-07-21 07:17:21 +02:00
|
|
|
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
|
2018-07-21 06:56:12 +02:00
|
|
|
const kStateSymbol = Symbol('state symbol');
|
2018-07-21 07:17:21 +02:00
|
|
|
let dns; // Lazy load for startup performance.
|
2018-07-21 06:56:12 +02:00
|
|
|
|
2018-07-21 07:17:21 +02: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;
|
|
|
|
} 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 23:03:48 +01:00
|
|
|
handle.connect = handle.connect6;
|
2018-07-21 07:17:21 +02: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 16:41:11 +02: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 07:17:21 +02:00
|
|
|
}
|
2018-07-29 16:41:11 +02:00
|
|
|
} else if (port || address) {
|
|
|
|
err = handle.bind(address, port || 0, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
handle.close();
|
|
|
|
return err;
|
2018-07-21 07:17:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-29 16:41:11 +02:00
|
|
|
module.exports = {
|
|
|
|
kStateSymbol,
|
|
|
|
_createSocketHandle,
|
2019-04-18 04:46:39 +02:00
|
|
|
newHandle
|
2018-07-29 16:41:11 +02:00
|
|
|
};
|