2016-01-26 08:12:41 -06:00
|
|
|
'use strict';
|
|
|
|
|
2017-12-31 00:27:56 +01:00
|
|
|
const Buffer = require('buffer').Buffer;
|
2018-01-26 18:39:10 +01:00
|
|
|
const { isIPv6 } = process.binding('cares_wrap');
|
2017-12-31 00:27:56 +01:00
|
|
|
const { writeBuffer } = process.binding('fs');
|
2018-03-03 15:43:14 +08:00
|
|
|
const errors = require('internal/errors');
|
2017-12-31 00:27:56 +01:00
|
|
|
|
2018-01-26 18:39:10 +01:00
|
|
|
const octet = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
|
|
|
|
const re = new RegExp(`^${octet}[.]${octet}[.]${octet}[.]${octet}$`);
|
|
|
|
|
|
|
|
function isIPv4(s) {
|
|
|
|
return re.test(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isIP(s) {
|
|
|
|
if (isIPv4(s)) return 4;
|
|
|
|
if (isIPv6(s)) return 6;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-26 08:12:41 -06:00
|
|
|
// Check that the port number is not NaN when coerced to a number,
|
|
|
|
// is an integer and that it falls within the legal range of port numbers.
|
|
|
|
function isLegalPort(port) {
|
2016-03-15 20:46:53 -07:00
|
|
|
if ((typeof port !== 'number' && typeof port !== 'string') ||
|
|
|
|
(typeof port === 'string' && port.trim().length === 0))
|
2016-01-26 08:12:41 -06:00
|
|
|
return false;
|
2016-03-15 20:46:53 -07:00
|
|
|
return +port === (+port >>> 0) && port <= 0xFFFF;
|
2016-01-26 08:12:41 -06:00
|
|
|
}
|
2016-03-15 20:34:19 -03:00
|
|
|
|
2017-12-31 00:27:56 +01:00
|
|
|
function makeSyncWrite(fd) {
|
|
|
|
return function(chunk, enc, cb) {
|
|
|
|
if (enc !== 'buffer')
|
|
|
|
chunk = Buffer.from(chunk, enc);
|
|
|
|
|
|
|
|
this._bytesDispatched += chunk.length;
|
|
|
|
|
2018-03-03 15:43:14 +08:00
|
|
|
const ctx = {};
|
|
|
|
writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
|
|
|
|
if (ctx.errno !== undefined) {
|
|
|
|
const ex = errors.uvException(ctx);
|
2017-12-31 00:27:56 +01:00
|
|
|
// Legacy: net writes have .code === .errno, whereas writeBuffer gives the
|
|
|
|
// raw errno number in .errno.
|
2018-03-03 15:43:14 +08:00
|
|
|
ex.errno = ex.code;
|
2017-12-31 00:27:56 +01:00
|
|
|
return cb(ex);
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:29:00 -08:00
|
|
|
module.exports = {
|
2018-01-26 18:39:10 +01:00
|
|
|
isIP,
|
|
|
|
isIPv4,
|
|
|
|
isIPv6,
|
2017-05-18 14:19:21 -04:00
|
|
|
isLegalPort,
|
2017-12-31 00:27:56 +01:00
|
|
|
makeSyncWrite,
|
2017-05-18 14:19:21 -04:00
|
|
|
normalizedArgsSymbol: Symbol('normalizedArgs')
|
2017-02-15 14:29:00 -08:00
|
|
|
};
|