gpt4 book ai didi

c - C 中的 mosquitto 和 SSL/TLS

转载 作者:太空宇宙 更新时间:2023-11-04 04:30:25 26 4
gpt4 key购买 nike

<分区>

我正在尝试使用 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()

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com