Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: IPV6 connectivity broke in 5.5.4 #1870

Labels

Comments

@ytimenkov
Copy link

MQTTjs Version

5.5.4

Broker

mosquitto

Environment

NodeJS

Description

MQTT.js confuses ipv6 address with UNIX socket.

Was discovered in zigbee2mqtt Koenkk/zigbee2mqtt#22390

Minimal Reproduction

  1. Start mosquitto and make sure it listens on ipv6 endpoint (it does in default installation in OpenSUSE):
1716396210: Opening ipv4 listen socket on port 1883.
1716396210: Opening ipv6 listen socket on port 1883.
1716396210: mosquitto version 2.0.18 running
  1. The sample from README.md, just with changed host to ::1 (localhost)
const mqtt = require("mqtt");
const client = mqtt.connect("mqtt://[::1]");

client.on("connect", () => {
    client.subscribe("presence", (err) => {
        if (!err) {
            client.publish("presence", "Hello mqtt");
        }
    });
});

client.on("message", (topic, message) => {
    // message is Buffer
    console.log(message.toString());
    client.end();
});
  1. Try 5.5.3 - works, creates AF_INET6 socket:
$ npm install [email protected]

$ strace -e trace=%net node test.js 
socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 20
connect(20, {sa_family=AF_INET6, sin6_port=htons(1883), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "::1", &sin6_addr), sin6_scope_id=0}, 28) = -1 EINPROGRESS (Operation now in progress)
getsockopt(20, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
Hello mqtt
shutdown(20, SHUT_WR)                   = 0
+++ exited with 0 +++
  1. Try 5.5.4 - tries AF_UNIX and fails:
$ npm install [email protected]
$  strace -e trace=%net node test.js 
socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 19
connect(19, {sa_family=AF_UNIX, sun_path="/"}, 110) = -1 EACCES (Permission denied)
socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 19
connect(19, {sa_family=AF_UNIX, sun_path="/"}, 110) = -1 EACCES (Permission denied)
socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 19
connect(19, {sa_family=AF_UNIX, sun_path="/"}, 110) = -1 EACCES (Permission denied)

Debug logs

$ env DEBUG=mqttjs* node test.js 
  mqttjs connecting to an MQTT broker... +0ms
  mqttjs:client MqttClient :: version: 5.5.4 +0ms
  mqttjs:client MqttClient :: environment node +0ms
  mqttjs:client MqttClient :: options.protocol mqtt +0ms
  mqttjs:client MqttClient :: options.protocolVersion 4 +0ms
  mqttjs:client MqttClient :: options.username undefined +0ms
  mqttjs:client MqttClient :: options.keepalive 60 +0ms
  mqttjs:client MqttClient :: options.reconnectPeriod 1000 +0ms
  mqttjs:client MqttClient :: options.rejectUnauthorized undefined +0ms
  mqttjs:client MqttClient :: options.properties.topicAliasMaximum undefined +0ms
  mqttjs:client MqttClient :: clientId mqttjs_56f3a523 +0ms
  mqttjs:client MqttClient :: setting up stream +0ms
  mqttjs:client connect :: calling method to clear reconnect +1ms
  mqttjs:client _clearReconnect : clearing reconnect timer +0ms
  mqttjs:client connect :: using streamBuilder provided to client to create stream +0ms
  mqttjs calling streambuilder for mqtt +2ms
  mqttjs:tcp port 1883 and host ::1 +0ms
  mqttjs:client connect :: pipe stream to writable stream +0ms
  mqttjs:client connect: sending packet `connect` +1ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'connect',
  mqttjs:client   protocolId: 'MQTT',
  mqttjs:client   protocolVersion: 4,
  mqttjs:client   clean: true,
  mqttjs:client   clientId: 'mqttjs_56f3a523',
  mqttjs:client   keepalive: 60,
  mqttjs:client   username: undefined,
  mqttjs:client   password: undefined,
  mqttjs:client   properties: undefined
  mqttjs:client } +0ms
  mqttjs:client _writePacket :: emitting `packetsend` +0ms
  mqttjs:client _writePacket :: writing to stream +0ms
  mqttjs:client _writePacket :: writeToStream result true +10ms
  mqttjs:client streamErrorHandler :: error connect EACCES / +1ms
  mqttjs:client streamErrorHandler :: emitting error +0ms
  mqttjs:client (mqttjs_56f3a523)stream :: on close +1ms
  mqttjs:client _flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function +0ms
  mqttjs:client stream: emit close to MqttClient +0ms
  mqttjs:client close :: connected set to `false` +0ms
  mqttjs:client close :: clearing connackTimer +0ms
  mqttjs:client close :: destroy ping timer +0ms
  mqttjs:client close :: calling _setupReconnect +0ms
  mqttjs:client _setupReconnect :: emit `offline` state +0ms
  mqttjs:client _setupReconnect :: set `reconnecting` to `true` +0ms
  mqttjs:client _setupReconnect :: setting reconnectTimer for 1000 ms +0ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment