为 openwrt 编译 libstrope 以支持 TLS

标签 c xmpp libstrophe

我使用 libstropice 版本 0.8.9 来开发我的 xmpp 客户端。

这里是我的xmmp客户端连接服务器的xmmp函数

#include <strophe.h>
    int xmpp_connect(void)
    {
        xmpp_ctx_t *ctx;
        xmpp_conn_t *conn;
        xmpp_log_t *log;
        int sta;
        static int retry = 0;

        xmpp_initialize();
        log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
        ctx = xmpp_ctx_new(NULL, log);
        conn = xmpp_conn_new(ctx);
        xmpp_conn_set_jid(conn, cur_xmmp_con.jid);
        xmpp_conn_set_pass(conn, cur_xmmp_con.password);

    #if 1
        if (!tls_start(conn->tls))
        {
            xmpp_debug(conn->ctx, "xmpp", "Couldn't start TLS! error %d", tls_error(conn->tls));
            tls_free(conn->tls);
            conn->tls = NULL;
            conn->tls_failed = 1;
            /* failed tls spoils the connection, so disconnect */
            xmpp_disconnect(conn);
        }
        else
        {
            conn->secured = 1;
            conn_prepare_reset(conn, NULL);
            conn_open_stream(conn);
        }
    #endif
        //
        sta = xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);
        xmpp_run(ctx);
        return 0;
    }

我收到这些错误

./src/test_xmpp.c:62:21: error: dereferencing pointer to incomplete type
  if (!tls_start(conn->tls))
                     ^
../src/test_xmpp.c:64:18: error: dereferencing pointer to incomplete type
   xmpp_debug(conn->ctx, "xmpp", "Couldn't start TLS! error %d", tls_error(conn->tls));
                  ^
../src/test_xmpp.c:64:79: error: dereferencing pointer to incomplete type
   xmpp_debug(conn->ctx, "xmpp", "Couldn't start TLS! error %d", tls_error(conn->tls));
                                                                               ^
../src/test_xmpp.c:65:16: error: dereferencing pointer to incomplete type
   tls_free(conn->tls);
                ^
../src/test_xmpp.c:66:7: error: dereferencing pointer to incomplete type
   conn->tls = NULL;
       ^
../src/test_xmpp.c:67:7: error: dereferencing pointer to incomplete type
   conn->tls_failed = 1;
       ^
../src/test_xmpp.c:73:7: error: dereferencing pointer to incomplete type
   conn->secured = 1;

虽然这些变量存在于这个文件中https://github.com/metajack/libstrophe/blob/master/src/common.h

我的代码或 libstropice 有什么问题吗?

最佳答案

您只能使用strope.h提供的API。此外,strope.h 不包含 xmpp_conn_txmpp_ctx_t 等的定义。因此,您无法在程序中访问它们的字段。

如果 libstrope 是使用 TLS 支持构建的(默认为 openssl),则当 xmpp 服务器支持它时,就会建立 TLS session 。这是隐式完成的。在 conn_handler() 中,您可以使用 xmpp_conn_is_secured() 检查连接是否安全。或者,您可以通过在 xmpp_connect_client() 之前调用下一个函数来仅接受安全连接:

    xmpp_conn_set_flags(conn, XMPP_CONN_FLAG_MANDATORY_TLS);

参见https://github.com/strophe/libstrophe/blob/master/examples/basic.c了解更多详情。

总之,您需要删除 #if-#endif 部分。如果 libstrope 正确构建并且 xmpp 服务器支持,TLS session 将自动建立。

附注官方存储库已移至https://github.com/strophe/libstrophe .

关于为 openwrt 编译 libstrope 以支持 TLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41964211/

相关文章:

c - 如何使弱链接与 GCC 一起工作?

android - XMPP在android中的实现

xmpp - 如何同步xmpp服务器openfire用户和iOS APP用户

xmpp - 使用(a)Smack在XMPP中创建类似WhatsApp,BBM的组

c - 使用 libstrope XMPP 自动重新连接

ssl - Strophe 不可恢复的 TLS 错误

javascript - strophe.js 组邀请 - 未触发处理程序

c - Fedora 和 ubuntu 中的库相同但名称不同?

java - 启用 C 应用程序作为 Web 服务

c - 如何从c中的sock结构中获取ip地址?