Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

net.createConnection seems to assume IPv4 localhost address when no host given #8140

Closed
@misterdjules

Description

@misterdjules

It seems that Socket.prototype.connect assumes an IPv4 localhost address when no host is given. Here's the code in lib/net.js:

} else if (!options.host) {
    debug('connect: missing host');
    self._host = '127.0.0.1';
    connect(self, self._host, options.port, 4);

Should we instead set options.host to 'localhost' and fall through so that both use cases (host set in options or not) resolve to the same IP address?

There are other places where it seems IPv4 addresses are assumed:

  1. In Socket.prototype.listen, when no IP is passed, IPv4 is hardcoded:
} else if (util.isUndefined(arguments[1]) ||
             util.isFunction(arguments[1]) ||
             util.isNumber(arguments[1])) {
    // The first argument is the port, no IP given.
    listen(self, null, port, 4, backlog);
  1. There's another case in Socket.prototype.connect when we hard-code IPv4, I'm not sure what use case it covers:
} else {
        timers._unrefActive(self);

        addressType = addressType || 4;

        // node_net.cc handles null host names graciously but user land
        // expects remoteAddress to have a meaningful value
        ip = ip || (addressType === 4 ? '127.0.0.1' : '0:0:0:0:0:0:0:1');

        connect(self,
                ip,
                options.port,
                addressType,
                options.localAddress,
                options.localPort);
      }

/cc @cjihrig

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions