2016-01-26 08:12:41 -06:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-30 16:55:29 +01:00
|
|
|
const {
|
2020-01-06 03:48:14 +01:00
|
|
|
RegExp,
|
2020-11-18 00:19:12 +01:00
|
|
|
RegExpPrototypeTest,
|
2019-11-30 16:55:29 +01:00
|
|
|
Symbol,
|
|
|
|
} = primordials;
|
|
|
|
|
2017-12-31 00:27:56 +01:00
|
|
|
const Buffer = require('buffer').Buffer;
|
2018-08-23 23:29:40 +09:00
|
|
|
const { writeBuffer } = internalBinding('fs');
|
2024-04-23 19:05:38 +02:00
|
|
|
const {
|
|
|
|
UVException,
|
|
|
|
} = require('internal/errors');
|
2017-12-31 00:27:56 +01:00
|
|
|
|
2018-09-03 20:28:31 +08:00
|
|
|
// IPv4 Segment
|
2023-09-13 22:01:36 +02:00
|
|
|
const v4Seg = '(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])';
|
|
|
|
const v4Str = `(?:${v4Seg}\\.){3}${v4Seg}`;
|
2018-09-03 20:28:31 +08:00
|
|
|
const IPv4Reg = new RegExp(`^${v4Str}$`);
|
|
|
|
|
|
|
|
// IPv6 Segment
|
|
|
|
const v6Seg = '(?:[0-9a-fA-F]{1,4})';
|
2023-09-13 22:01:36 +02:00
|
|
|
const IPv6Reg = new RegExp('^(?:' +
|
2018-09-03 20:28:31 +08:00
|
|
|
`(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
|
|
|
|
`(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
|
2023-09-13 22:01:36 +02:00
|
|
|
`(?:${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) {
|
2022-06-17 19:12:42 +02:00
|
|
|
// TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it
|
|
|
|
// no longer creates a perf regression in the dns benchmark.
|
|
|
|
// eslint-disable-next-line node-core/avoid-prototype-pollution
|
2020-11-18 00:19:12 +01:00
|
|
|
return RegExpPrototypeTest(IPv4Reg, s);
|
2018-09-03 20:28:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function isIPv6(s) {
|
2022-06-17 19:12:42 +02:00
|
|
|
// TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it
|
|
|
|
// no longer creates a perf regression in the dns benchmark.
|
|
|
|
// eslint-disable-next-line node-core/avoid-prototype-pollution
|
2020-11-18 00:19:12 +01:00
|
|
|
return RegExpPrototypeTest(IPv6Reg, s);
|
2018-01-26 18:39:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isIP(s) {
|
|
|
|
if (isIPv4(s)) return 4;
|
|
|
|
if (isIPv6(s)) return 6;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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 15:43:14 +08:00
|
|
|
const ctx = {};
|
|
|
|
writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
|
|
|
|
if (ctx.errno !== undefined) {
|
2024-04-23 19:05:38 +02:00
|
|
|
const ex = new UVException(ctx);
|
2019-06-09 21:04:51 +08:00
|
|
|
ex.errno = ctx.errno;
|
2017-12-31 00:27:56 +01:00
|
|
|
return cb(ex);
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-11-06 00:22:58 +02:00
|
|
|
/**
|
|
|
|
* https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
|
|
|
|
* https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
|
|
|
|
* https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml
|
|
|
|
*/
|
|
|
|
function isLoopback(host) {
|
|
|
|
const hostLower = host.toLowerCase();
|
|
|
|
|
|
|
|
return (
|
|
|
|
hostLower === 'localhost' ||
|
|
|
|
hostLower.startsWith('127.') ||
|
|
|
|
hostLower.startsWith('[::1]') ||
|
|
|
|
hostLower.startsWith('[0:0:0:0:0:0:0:1]')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:29:00 -08:00
|
|
|
module.exports = {
|
2023-02-23 10:55:04 +01:00
|
|
|
kReinitializeHandle: Symbol('kReinitializeHandle'),
|
2018-01-26 18:39:10 +01:00
|
|
|
isIP,
|
|
|
|
isIPv4,
|
|
|
|
isIPv6,
|
2017-12-31 00:27:56 +01:00
|
|
|
makeSyncWrite,
|
2023-02-26 11:34:02 +01:00
|
|
|
normalizedArgsSymbol: Symbol('normalizedArgs'),
|
2024-11-06 00:22:58 +02:00
|
|
|
isLoopback,
|
2017-02-15 14:29:00 -08:00
|
|
|
};
|