11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  RegExp,
51cb0ef41Sopenharmony_ci  RegExpPrototypeTest,
61cb0ef41Sopenharmony_ci  Symbol,
71cb0ef41Sopenharmony_ci} = primordials;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst Buffer = require('buffer').Buffer;
101cb0ef41Sopenharmony_ciconst { writeBuffer } = internalBinding('fs');
111cb0ef41Sopenharmony_ciconst errors = require('internal/errors');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// IPv4 Segment
141cb0ef41Sopenharmony_ciconst v4Seg = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
151cb0ef41Sopenharmony_ciconst v4Str = `(${v4Seg}[.]){3}${v4Seg}`;
161cb0ef41Sopenharmony_ciconst IPv4Reg = new RegExp(`^${v4Str}$`);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// IPv6 Segment
191cb0ef41Sopenharmony_ciconst v6Seg = '(?:[0-9a-fA-F]{1,4})';
201cb0ef41Sopenharmony_ciconst IPv6Reg = new RegExp('^(' +
211cb0ef41Sopenharmony_ci  `(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
221cb0ef41Sopenharmony_ci  `(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
231cb0ef41Sopenharmony_ci  `(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|` +
241cb0ef41Sopenharmony_ci  `(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|` +
251cb0ef41Sopenharmony_ci  `(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|` +
261cb0ef41Sopenharmony_ci  `(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|` +
271cb0ef41Sopenharmony_ci  `(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|` +
281cb0ef41Sopenharmony_ci  `(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
291cb0ef41Sopenharmony_ci')(%[0-9a-zA-Z-.:]{1,})?$');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cifunction isIPv4(s) {
321cb0ef41Sopenharmony_ci  // TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it
331cb0ef41Sopenharmony_ci  // no longer creates a perf regression in the dns benchmark.
341cb0ef41Sopenharmony_ci  // eslint-disable-next-line node-core/avoid-prototype-pollution
351cb0ef41Sopenharmony_ci  return RegExpPrototypeTest(IPv4Reg, s);
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cifunction isIPv6(s) {
391cb0ef41Sopenharmony_ci  // TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it
401cb0ef41Sopenharmony_ci  // no longer creates a perf regression in the dns benchmark.
411cb0ef41Sopenharmony_ci  // eslint-disable-next-line node-core/avoid-prototype-pollution
421cb0ef41Sopenharmony_ci  return RegExpPrototypeTest(IPv6Reg, s);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cifunction isIP(s) {
461cb0ef41Sopenharmony_ci  if (isIPv4(s)) return 4;
471cb0ef41Sopenharmony_ci  if (isIPv6(s)) return 6;
481cb0ef41Sopenharmony_ci  return 0;
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cifunction makeSyncWrite(fd) {
521cb0ef41Sopenharmony_ci  return function(chunk, enc, cb) {
531cb0ef41Sopenharmony_ci    if (enc !== 'buffer')
541cb0ef41Sopenharmony_ci      chunk = Buffer.from(chunk, enc);
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    this._handle.bytesWritten += chunk.length;
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    const ctx = {};
591cb0ef41Sopenharmony_ci    writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
601cb0ef41Sopenharmony_ci    if (ctx.errno !== undefined) {
611cb0ef41Sopenharmony_ci      const ex = errors.uvException(ctx);
621cb0ef41Sopenharmony_ci      ex.errno = ctx.errno;
631cb0ef41Sopenharmony_ci      return cb(ex);
641cb0ef41Sopenharmony_ci    }
651cb0ef41Sopenharmony_ci    cb();
661cb0ef41Sopenharmony_ci  };
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cimodule.exports = {
701cb0ef41Sopenharmony_ci  kReinitializeHandle: Symbol('reinitializeHandle'),
711cb0ef41Sopenharmony_ci  isIP,
721cb0ef41Sopenharmony_ci  isIPv4,
731cb0ef41Sopenharmony_ci  isIPv6,
741cb0ef41Sopenharmony_ci  makeSyncWrite,
751cb0ef41Sopenharmony_ci  normalizedArgsSymbol: Symbol('normalizedArgs'),
761cb0ef41Sopenharmony_ci};
77