LiamBindle / MQTT-C

A portable MQTT C client for embedded systems and PCs alike.
https://liambindle.ca/MQTT-C
MIT License
760 stars 269 forks source link

mqtt_connect does not generate a client.error if wrong username and/or password is provided #184

Open PitHerm opened 10 months ago

PitHerm commented 10 months ago

i tried your simple publisher example and modified the parameter of mqtt_connect from:

/ Send connection request to the broker. / mqtt_connect(&client, client_id, NULL, NULL, 0, NULL, NULL, connect_flags, 400);

/* check that we don't have any errors */
if (client.error != MQTT_OK) {
    fprintf(stderr, "error: %s\n", mqtt_error_str(client.error));
    exit_example(EXIT_FAILURE, sockfd, NULL);
}

with the correct username and password to: / Send connection request to the broker. / mqtt_connect(&client, client_id, NULL, NULL, 0, "paul", "paula", connect_flags, 400);

/* check that we don't have any errors */
if (client.error != MQTT_OK) {
    fprintf(stderr, "error: %s\n", mqtt_error_str(client.error));
    exit_example(EXIT_FAILURE, sockfd, NULL);
}

i received no errors and a connection which worked. then i tried wrong username and wrong password

/ Send connection request to the broker. / mqtt_connect(&client, client_id, NULL, NULL, 0, "otto", "otto", connect_flags, 400);

/* check that we don't have any errors */
if (client.error != MQTT_OK) {
    fprintf(stderr, "error: %s\n", mqtt_error_str(client.error));
    exit_example(EXIT_FAILURE, sockfd, NULL);
}

i received no errors and no connection to the broker was established after pressing the ENTER key to send the time i received an error

./simple_publisher is ready to begin publishing the time. Press ENTER to publish the current time. Press CTRL-D (or any other key) to exit.

./simple_publisher published : "The time is 2023-10-25 06:55:24" datetimeThe time is 2023-10-25 06:55:24 error: MQTT_ERROR_SOCKET_ERROR

if no connection was established to the broker there should be an error message.

conggalam12 commented 2 months ago

@PitHerm I have the same error , do you fix it ?

mwetterw commented 3 weeks ago

If I'm not mistaken, this is because mqtt_connect seems to only queue the CONNECT message without actually sending it. It's the first mqtt_sync call after the mqtt_connect that will really send the pending CONNECT request.

So, when mqtt_connect returns, the library cannot possibly yet know if the username/password are correct. You can see that connect failed if mqtt_sync returns either: