2016-01-26 15:12:41 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-12-31 00:27:56 +01:00
|
|
|
const Buffer = require('buffer').Buffer;
|
2018-08-23 16:29:40 +02:00
|
|
|
const { writeBuffer } = internalBinding('fs');
|
2018-03-03 08:43:14 +01:00
|
|
|
const errors = require('internal/errors');
|
2017-12-31 00:27:56 +01:00
|
|
|
|
2018-09-03 14:28:31 +02:00
|
|
|
// IPv4 Segment
|
|
|
|
const v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
|
|
|
|
const v4Str = `(${v4Seg}[.]){3}${v4Seg}`;
|
|
|
|
const IPv4Reg = new RegExp(`^${v4Str}$`);
|
|
|
|
|
|
|
|
// IPv6 Segment
|
|
|
|
const v6Seg = '(?:[0-9a-fA-F]{1,4})';
|
|
|
|
const IPv6Reg = new RegExp('^(' +
|
|
|
|
`(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
|
|
|
|
`(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
|
|
|
|
`(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` +
|
|
|
|
`(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` +
|
|
|
|
`(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` +
|
|
|
|
`(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` +
|
|
|
|
`(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` +
|
|
|
|
`(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
|
|
|
|
')(%[0-9a-zA-Z]{1,})?$');
|
2018-01-26 18:39:10 +01:00
|
|
|
|
|
|
|
function isIPv4(s) {
|
2018-09-03 14:28:31 +02:00
|
|
|
return IPv4Reg.test(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isIPv6(s) {
|
|
|
|
return IPv6Reg.test(s);
|
2018-01-26 18:39:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isIP(s) {
|
|
|
|
if (isIPv4(s)) return 4;
|
|
|
|
if (isIPv6(s)) return 6;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:12:41 +01: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-16 04:46:53 +01:00
|
|
|
if ((typeof port !== 'number' && typeof port !== 'string') ||
|
|
|
|
(typeof port === 'string' && port.trim().length === 0))
|
2016-01-26 15:12:41 +01:00
|
|
|
return false;
|
2016-03-16 04:46:53 +01:00
|
|
|
return +port === (+port >>> 0) && port <= 0xFFFF;
|
2016-01-26 15:12:41 +01:00
|
|
|
}
|
2016-03-16 00:34:19 +01: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);
|
|
|
|
|
2018-03-17 17:52:57 +01:00
|
|
|
this._handle.bytesWritten += chunk.length;
|
2017-12-31 00:27:56 +01:00
|
|
|
|
2018-03-03 08:43:14 +01: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 08:43:14 +01:00
|
|
|
ex.errno = ex.code;
|
2017-12-31 00:27:56 +01:00
|
|
|
return cb(ex);
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-15 23:29:00 +01:00
|
|
|
module.exports = {
|
2018-01-26 18:39:10 +01:00
|
|
|
isIP,
|
|
|
|
isIPv4,
|
|
|
|
isIPv6,
|
2017-05-18 20:19:21 +02:00
|
|
|
isLegalPort,
|
2017-12-31 00:27:56 +01:00
|
|
|
makeSyncWrite,
|
2017-05-18 20:19:21 +02:00
|
|
|
normalizedArgsSymbol: Symbol('normalizedArgs')
|
2017-02-15 23:29:00 +01:00
|
|
|
};
|