MQTT-C icon indicating copy to clipboard operation
MQTT-C copied to clipboard

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

Open PitHerm opened this issue 1 year ago • 2 comments

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.

PitHerm avatar Oct 25 '23 04:10 PitHerm

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

conggalam12 avatar Jun 14 '24 09:06 conggalam12

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:

  • MQTT_ERROR_CONNECT_CLIENT_ID_REFUSED or
  • MQTT_ERROR_CONNECTION_REFUSED

mwetterw avatar Aug 01 '24 15:08 mwetterw