为 openwrt 编译 libstrophe 以支持 TLS
compile libstrophe for openwrt to support TLS
我使用 libstrophe 版本 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
我的代码或 libstrophe 有什么问题?
您只能使用 strophe.h
提供的 API。此外 strophe.h
不包含 xmpp_conn_t
、xmpp_ctx_t
等的定义。因此,您无法在程序中访问它们的字段。
如果 libstrophe 构建时支持 TLS(默认为 openssl),则在 xmpp 服务器支持时建立 TLS 会话。这是隐式完成的。在 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
部分。如果 libstrophe 构建正确并且 xmpp 服务器支持,TLS 会话将自动建立。
P.S。官方存储库已移至 https://github.com/strophe/libstrophe.
我使用 libstrophe 版本 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
我的代码或 libstrophe 有什么问题?
您只能使用 strophe.h
提供的 API。此外 strophe.h
不包含 xmpp_conn_t
、xmpp_ctx_t
等的定义。因此,您无法在程序中访问它们的字段。
如果 libstrophe 构建时支持 TLS(默认为 openssl),则在 xmpp 服务器支持时建立 TLS 会话。这是隐式完成的。在 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
部分。如果 libstrophe 构建正确并且 xmpp 服务器支持,TLS 会话将自动建立。
P.S。官方存储库已移至 https://github.com/strophe/libstrophe.