如何使用 libmosquitto 库在连接时检测与 MQTT 代理的连接中 user/password 的问题?

How can I detect problems with user/password in the connection to MQTT broker at connection time using the libmosquitto library?

我正在用这个小程序测试 MQTT mosquitto library

/*
  compile using:
  $ gcc -o libmosq libmosq.c -lmosquitto
*/
#include <stdio.h>
#include <mosquitto.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  struct mosquitto *mosq = NULL;
 
  mosquitto_lib_init();
  mosq = mosquitto_new(NULL, true, NULL);
  if(!mosq) {
     fprintf(stderr, "Error: Out of memory.\n");
     exit(1);
  } 
    
  mosquitto_username_pw_set(mosq, "user1", "passwd1");

  int resultCode = mosquitto_connect(mosq, "localhost", 1883, 60);
  if (resultCode != MOSQ_ERR_SUCCESS) {
    fprintf(stderr, "connection error\n");
    exit(1);
  }
  else {
    printf("connection success\n");
  }
  // wait until control+C is done
  sleep(1000000);
}

我在本地主机的 1883 端口 运行 设置 MQTT 代理(mosquitto broker 版本 1.6.10)。

当我 运行 我得到 "connection success" 的程序时,我在 mosquitto 日志中看到:

iot-mosquitto    | 2021-10-06T10:16:11: New connection from 172.17.0.1 on port 1883.
iot-mosquitto    | 2021-10-06T10:16:11: New client connected from 172.17.0.1 as auto-51085B64-A53B-DBE1-DBFB-A6D9D702B69C (p2, c1, k60, u'user1').

我了解在这种情况下连接是正确的。到目前为止一切顺利。

但是,如果我使用了错误的 user/pass(例如 mosquitto_username_pw_set(mosq, "user1", "xxxxx"))或者如果不使用 user/pass(即删除 mosquitto_username_pw_set() 地图),我会进入蚊子经纪人日志:

iot-mosquitto    | 2021-10-06T10:27:58: New connection from 172.17.0.1 on port 1883.
iot-mosquitto    | 2021-10-06T10:27:58: Socket error on client <unknown>, disconnecting.

很好。问题是在我的程序中我得到 "connection success" 而不是 "connection error"。换句话说,我得到 MOSQ_ERR_SUCCESS 作为 mosquitto_connect() 的 return 值,而不是 MOSQ_ERR_ERRNO

查看 MQTT 代理跟踪,就像我的程序已连接(这可以解释 MOSQ_ERR_SUCCESS)但它立即断开连接...

如何使用 libmosquitto 库在连接时检测连接中 user/password 的问题?

提前致谢!

编辑: 我知道有一些方法可以解决这个问题,因为 mosquitto_sub(我知道它基于同一个库)能够检测到。例如:

$ mosquitto_sub -p 1883 -t '#' -u user1 -P xxxxxx
Connection error: Connection Refused: not authorised.

我终于用下面的程序解决了这个问题:

/*
  compile using:
  $ gcc -o libmosq libmosq.c -lmosquitto
*/
#include <stdio.h>
#include <mosquitto.h>
#include <stdlib.h>
#include <unistd.h>

void connection_callback(struct mosquitto* mosq, void *obj, int rc)
{
  if (rc) {
    printf("connection error: %d (%s)\n", rc, mosquitto_connack_string(rc));
    exit(1);
  }
  else {
    printf("connection success\n");
  }
}

int main(int argc, char *argv[])
{
  struct mosquitto *mosq = NULL;
  
  mosquitto_lib_init();
  mosq = mosquitto_new(NULL, true, NULL);
  if(!mosq) {
     fprintf(stderr, "Error: Out of memory.\n");
     exit(1);
  }

  mosquitto_connect_callback_set(mosq, connection_callback);
  mosquitto_username_pw_set(mosq, "user1", "passwd1");

  int resultCode = mosquitto_connect(mosq, "localhost", 1883, 60);
  if (resultCode != MOSQ_ERR_SUCCESS) {
    fprintf(stderr, "error calling mosquitto_connect\n");
    exit(1);
  }

  int loop = mosquitto_loop_start(mosq);
  if(loop != MOSQ_ERR_SUCCESS){
    fprintf(stderr, "Unable to start loop: %i\n", loop);
    exit(1);
  }

  // hang until control+C is done
  sleep(1000000);
}

与第一版相比的主要区别:

  • 使用mosquitto_connect_callback_set()设置连接事件的回调函数
  • 使用mosquitto_loop_start()。如果我不添加此语句,则不会在连接时调用回调

使用此程序,连接成功后我会收到以下消息:

connection success

当密码错误或我删除 mosquitto_username_pw_set() 语句时,这一个:

connection error: 5 (Connection Refused: not authorised.)