0

我想使用 libstrophe 库创建一个简单的聊天客户端。我写的代码如下。与 gmail 服务器的连接已建立。但我收到一个 TLS 错误,我无法解决。请suugest是什么导致了错误。

 #include <stdio.h>
#include <string.h>
#include <strophe.h>

int handle_reply(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,void * const userdata)
{
xmpp_stanza_t *query, *item;
char *type;
type = xmpp_stanza_get_type(stanza);
if (strcmp(type, "error") == 0)
    fprintf(stderr, "ERROR: query failed\n");
else {
    query = xmpp_stanza_get_child_by_name(stanza, "query");
    printf("Active Sessions:\n");
    for (item = xmpp_stanza_get_children(query); item; 
         item = xmpp_stanza_get_next(item))
        printf("\t %s\n", xmpp_stanza_get_attribute(item, "jid"));
    printf("END OF LIST\n");
}

/* disconnect */
xmpp_disconnect(conn);

return 0;
}

/* define a handler for connection events */
void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
              const int error, xmpp_stream_error_t * const stream_error,
              void * const userdata)
{
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
xmpp_stanza_t *iq, *query;
if (status == XMPP_CONN_CONNECT) 
{
    fprintf(stderr, "DEBUG: connected\n");
     /* create iq stanza for request */
    iq = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(iq, "iq");
    xmpp_stanza_set_type(iq, "get");
    xmpp_stanza_set_id(iq, "active1");
    xmpp_stanza_set_attribute(iq, "to", "xxxxxxxxx.com");

    query = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(query, "query");
    xmpp_stanza_set_ns(query, XMPP_NS_DISCO_ITEMS);
    xmpp_stanza_set_attribute(query, "node", "sessions");

    xmpp_stanza_add_child(iq, query);

    /* we can release the stanza since it belongs to iq now */
    xmpp_stanza_release(query);

    /* set up reply handler */
    xmpp_id_handler_add(conn, handle_reply, "active1", ctx);

    /* send out the stanza */
    xmpp_send(conn, iq);

    /* release the stanza */
    xmpp_stanza_release(iq);

}
else {
    fprintf(stderr, "DEBUG: disconnected\n");
    xmpp_stop(ctx);
}
}

int main(int argc, char **argv)
{
int a;
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
char *jid, *pass, *host;



jid = "ACCOUNTNAME@gmail.com";
pass = "MYPASSWORD";
host = NULL;


/* init library */
xmpp_initialize();

/* create a context */
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
ctx = xmpp_ctx_new(NULL, log);

/* create a connection */
conn = xmpp_conn_new(ctx);

/* setup authentication information */
xmpp_conn_set_jid(conn, jid);
xmpp_conn_set_pass(conn, pass);

/* initiate connection */
xmpp_connect_client(conn, host, 0, conn_handler, ctx);

/* enter the event loop -
   our connect handler will trigger an exit */
xmpp_run(ctx);

/* release our connection and context */
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);

/* final shutdown of the library */
xmpp_shutdown();
scanf("%d",&a);
return 0;
}

cmd 提示符(输出)是:

xmpp DEBUG sock_connect to alt3.xmpp.l.google.com:5222 returned 304
xmpp DEBUG attempting to connect to alt3.xmpp.l.google.com
xmpp DEBUG connection successful
conn DEBUG SENT: <?xml version="1.0"?><stream:stream to="gmail.com" xml:lang="en
"   version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">
xmpp DEBUG RECV: <stream:stream from='gmail.com' id='4EDF7B71A4D88935' version='
1.0' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client'>
xmpp DEBUG RECV: <stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-t
ls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><
mechanism>X-OAUTH2</mechanism><mechanism>X-GOOGLE-TOKEN</mechanism></mechanisms>
</stream:features>
TLSS DEBUG QuerySecurityPackageInfo() success
TLSS DEBUG AcquireCredentialsHandle() success
conn DEBUG SENT: <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>, error: 0
xmpp DEBUG RECV: <proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
xmpp DEBUG handle proceedtls called for proceed
xmpp DEBUG proceeding with TLS
**TLSS DEBUG QuerySecurityPackageInfo() success
TLSS DEBUG AcquireCredentialsHandle() success
xmpp DEBUG Couldn't start TLS! error -2146893032
conn DEBUG SENT: </stream:stream>
xmpp DEBUG parse error, disconnecting**
xmpp DEBUG Closing socket.
DEBUG: disconnected
event DEBUG Stopping event loop.
event DEBUG Event loop completed.
4

0 回答 0