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

adafruit_minimqtt connect is not consistent when checking username #129

Closed
flavio-fernandes opened this issue Nov 20, 2022 · 0 comments · Fixed by #130
Closed

adafruit_minimqtt connect is not consistent when checking username #129

flavio-fernandes opened this issue Nov 20, 2022 · 0 comments · Fixed by #130

Comments

@flavio-fernandes
Copy link
Contributor

flavio-fernandes commented Nov 20, 2022

I got in trouble with CONNECT in the following way:

import adafruit_minimqtt.adafruit_minimqtt as MQTT
...
client = MQTT.MQTT(
    broker="192.168.10.238",
    port=1883,
    username="",
    password="",
)

^^ Note that username and password are not None. They are an empty string "".

Attempting to MQTT connect to 192.168.10.238
460.552: DEBUG - Attempting to establish MQTT connection...
460.556: INFO - Establishing an INSECURE connection to 192.168.10.238:1883
460.582: DEBUG - Sending CONNECT to broker...
460.583: DEBUG - Fixed Header: bytearray(b'\x10\x13\x00')
Variable Header: bytearray(b'\x04MQTT\x04\x02\x00<')
460.737: DEBUG - Receiving CONNACK packet from broker
FATAL! Unable to MQTT connect to 192.168.10.238: No data received from broker for 10 seconds.

on the mqtt server side, I observe the following:

1668979633: New connection from 192.168.10.245:62670 on port 1883.
1668979633: New client connected from 192.168.10.245:62670 as cpy22457 (p2, c1, k60).
1668979633: Client cpy22457 disconnected due to protocol error.

It turns out, the existing logic in the code that checks if username is provided is not consistent when
determining if such needs to be sent or not:

and then, a few lines below:

in my case, this discrepancy caused the username/password ""/"" to be sent without MQTT header (var_header[6]) properly accounting for it in lines

if self._username:
remaining_length += (
2
+ len(self._username.encode("utf-8"))
+ 2
+ len(self._password.encode("utf-8"))
)
var_header[6] |= 0xC0
.

ouch! ;)

flavio-fernandes added a commit to flavio-fernandes/Adafruit_CircuitPython_MiniMQTT that referenced this issue Nov 21, 2022
Connect needs to distinguish None from an empty string when
populating and sending CONNECT to the MQTT broker.

Fixes adafruit#129

Signed-off-by: Flavio Fernandes <flavio@flaviof.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant