【问题标题】:How can I detect problems with user/password in the connection to MQTT broker at connection time using the libmosquitto library?如何使用 libmosquitto 库在连接时检测到与 MQTT 代理的连接中的用户/密码问题?
【发布时间】:2021-11-26 13:02:42
【问题描述】:

我正在用这个小程序测试 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);
}

我在 localhost 的 1883 端口运行 MQTT 代理(mosquitto broker 版本 1.6.10)。

当我运行程序时,我得到"connection success",并在蚊子日志中看到:

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_SUCCESS 作为mosquitto_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.

【问题讨论】:

标签: mqtt mosquitto libmosquitto


【解决方案1】:

我终于用下面的程序解决了:

/*
  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.)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 2019-11-20
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多