-
Notifications
You must be signed in to change notification settings - Fork 51
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
do not share CONNECT variable header #186
Conversation
Tested with CPython 3.11.2 using this code: #!/usr/bin/env python3
import logging
import socket
import ssl
import time
import pytest
import adafruit_minimqtt.adafruit_minimqtt as MQTT
def test_keepalive(keep_alive_timeout):
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
host = "172.40.0.3"
port = 1883
mqtt_client = MQTT.MQTT(
broker=host,
port=port,
socket_pool=socket,
ssl_context=ssl.create_default_context(),
connect_retries=1,
recv_timeout=5,
keep_alive=keep_alive_timeout,
)
logger.debug(f"connecting with keep alive = {keep_alive_timeout}")
mqtt_client.connect()
mqtt_client.disconnect()
test_keepalive(8)
test_keepalive(16) and checked that the keepalive value in the CONNECT packets match the 2 values used by the program. |
Tested with Adafruit CircuitPython 8.2.6 on 2023-09-12; Adafruit QT Py ESP32-S3 no psram with ESP32S3 using this code: #!/usr/bin/env python3
import adafruit_logging as logging
import socketpool
import ssl
import sys
import time
import wifi
from secrets import secrets
import adafruit_minimqtt.adafruit_minimqtt as MQTT
def main():
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.info("Connecting to wifi")
wifi.radio.connect(secrets["SSID"], secrets["password"], timeout=10)
logger.info(f"Connected to {secrets['SSID']}")
logger.debug(f"IP: {wifi.radio.ipv4_address}")
test_keepalive(8)
test_keepalive(16)
def test_keepalive(keep_alive_timeout):
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
pool = socketpool.SocketPool(wifi.radio)
host = "172.40.0.3"
port = 1883
mqtt_client = MQTT.MQTT(
broker=host,
port=port,
socket_pool=pool,
ssl_context=ssl.create_default_context(),
connect_retries=1,
recv_timeout=5,
keep_alive=keep_alive_timeout,
)
mqtt_client.logger = logger
logger.debug(f"connecting with keep alive = {keep_alive_timeout}")
mqtt_client.connect()
mqtt_client.disconnect()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0) The output on the console:
and the before/after diff: --- /tmp/before.out 2023-11-19 21:54:10.000000000 +0100
+++ /tmp/after.out 2023-11-19 21:54:10.000000000 +0100
@@ -18,8 +18,8 @@
DEBUG - Attempting to establish MQTT connection...
INFO - Establishing an INSECURE connection to 172.40.0.3:1883
DEBUG - Sending CONNECT to broker...
-DEBUG - Fixed Header: bytearray(b'\x10\x14\x00')
-DEBUG - Variable Header: bytearray(b'\x04MQTT\x04\x02\x00\x18')
+DEBUG - Fixed Header: bytearray(b'\x10\x13\x00')
+DEBUG - Variable Header: bytearray(b'\x04MQTT\x04\x02\x00\x10')
DEBUG - Receiving CONNACK packet from broker
DEBUG - Got message type: 0x20
DEBUG - Resetting reconnect backoff |
Resolves: #185 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix makes sense and looks good to me. I tested this version successfully on FunHouse 9.0.0-alpha.5
Updating https://github.com/adafruit/Adafruit_CircuitPython_HUSB238 to 1.0.1 from 1.0.0: > Merge pull request adafruit/Adafruit_CircuitPython_HUSB238#3 from millercommamatt/patch-2 > example typo Updating https://github.com/adafruit/Adafruit_CircuitPython_RFM69 to 2.1.18 from 2.1.17: > Merge pull request adafruit/Adafruit_CircuitPython_RFM69#48 from jerryneedell/jerryn_power Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.5.3 from 7.4.4: > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#186 from vladak/var_header_vs_bytearray > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#183 from vladak/wait_for_msg_pkt_type > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#182 from vladak/time_monotonic_ns > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#188 from vladak/user_data_public Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA: > Updated download stats for the libraries
This straightforward change prevents unwanted reuse of certain fields in the CONNECT variable header. Another way to fix this would be to use
copy.copy()
however that does not seem to be present in CP.