【发布时间】:2016-05-02 15:54:08
【问题描述】:
我正在尝试使用 libmosquitto(C 语言)连接到 SSL/TLS 中的 MQTT 代理。我确信它在服务器端工作,因为我在命令行中使用 mosquitto_sub(或 mosquitto_pub)成功连接到 borker,并使用我在代码中使用的相同证书。
当我尝试使用我的 C 程序进行连接时,我总是收到以下错误:“错误:无法创建 TLS 上下文。”
我在 Linux 上使用 libmosquitto 1.4.8。这是我正在使用的代码:
#include <mosquitto.h>
static struct SomeStruct *data = NULL;
// The variable mosq is included in the struct
// The struct has been created somewhere else
void foo(void)
{
// I usually check the return values but removed
// it to make the code easier to read
mosquitto_lib_init();
data->mosq = mosquitto_new("foobar", true, data);
// Connect the callbacks
mosquitto_username_pw_set(data->mosq, "user", "pass");
mosquitto_tls_set(data->mosq, "/path/to/ca/file.crt,
NULL, NULL, NULL, NULL);
mosquitto_tls_insecure_set(data->mosq, 1)
mosquitto_tls_opts_set(data->mosq,
1, // also tried 0
NULL, // also tried "tlsv1.2"
NULL);
// All the return values are correct up to here
mosquitto_connect(data->mosq, "mqtt.example.com", 8883, 30); // Fails
// Logs : Error: Unable to create TLS context.
// A TLS error occurred.
}
有谁知道可能是什么问题?
干杯,
安东尼
编辑:我忘了补充一点,我没有使用 mosquitto 的主循环,因为我使用的另一个库已经有一个,而且我需要的线程数量非常有限。因此,每次文件描述符更改时,我都会调用mosquitto_loop()。
【问题讨论】:
-
我们需要真实数据,例如服务器名称,而不是虚假数据,例如
mqtt.example.com。 -
我看不出服务器名称与我遇到的问题有什么关系——我试图尽可能简化代码,但所有对 mosquitto lib 的调用都在这里
标签: c openssl mqtt mosquitto libmosquitto