0

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

/*
  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);
}

我在 localhost 的 1883 端口运行 MQTT 代理(mosquitto 代理版本 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').

我知道在这种情况下连接是正确的。到目前为止,一切都很好。

但是,如果我使用了错误的用户/密码(例如mosquitto_username_pw_set(mosq, "user1", "xxxxx"))或者不使用用户/密码(即删除mosquitto_username_pw_set()地图),我会进入 mosquitto 代理日志:

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_SUCCESSmosquitto_connect()不是MOSQ_ERR_ERRNO.

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

请问如何在连接时使用 libmosquitto 库检测连接中的用户/密码问题?

提前致谢!

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

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

1 回答 1

0

我终于使用以下程序解决了它:

/*
  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.)
于 2021-10-13T08:18:57.797 回答