usage of libstrophe library for xmpp

3.5k views Asked by At

I am trying to create a chat client in C using Libstrophe. I have referred to the following code example given at https://github.com/metajack/libstrophe/blob/master/examples/active.c The code has a call to xmpp_connect_client(...) to establish a connection with the xmpp server.

 int main(int argc, char **argv)
 {
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;

if (argc != 3) {
    fprintf(stderr, "Usage: active <jid> <pass>\n\n");
    return 1;
}

/* initialize lib */
xmpp_initialize();

/* create a context */
ctx = xmpp_ctx_new(NULL, NULL);

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

/* setup authentication information */
xmpp_conn_set_jid(conn, argv[1]);
xmpp_conn_set_pass(conn, argv[2]);

/* initiate connection */
xmpp_connect_client(conn, "talk.google.com", 0, conn_handler, ctx);

/* start the event loop */
xmpp_run(ctx);

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

/* shutdown lib */
xmpp_shutdown();

return 0;

} But where does the authentication take place? I looked up the source code for libstrophe and found C file auth.c https://github.com/metajack/libstrophe/blob/master/src/auth.c that has a function called _auth(..). I tried using _auth(..) in my code but it does not perform authentication properly. i.e. it does not notify me of wrong user-name or password. Can any one suggest me the right way to authenticate my entity.

1

There are 1 answers

6
MattJ On BEST ANSWER

libstrophe authenticates automatically when necessary. This happens inside xmpp_run(). The credentials it uses are set using these lines:

/* setup authentication information */
xmpp_conn_set_jid(conn, argv[1]);
xmpp_conn_set_pass(conn, argv[2]);

The jid is your address (such as "[email protected]", "[email protected]", "[email protected]", etc.), and pass is your password.

Your example is missing your conn_handler function, which is where authentication errors will be delivered to.

Your conn_handler function should have a signature like this:

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)

The parameters are:

conn - Your connection object.

status - One of XMPP_CONN_CONNECT, XMPP_CONN_DISCONNECT or XMPP_CONN_FAIL. When your connection handler function is called, this parameter tells you why it was called.

error - when disconnected (XMPP_CONN_FAIL), this contains the socket-level error code from the OS (otherwise it is 0).

stream_error - one of the possible stream errors, listed at strophe.h:171, and their meaning documented in RFC6120 section 4.9.3.

userdata - This contains whatever you passed as the userdata parameter to xmpp_connect_client(). It is useful if you have some per-connection state to keep, and you don't want to use global variables or have multiple connections.

Finally, you shouldn't have to specify "talk.google.com" in xmpp_connect_client(), I recommend passing NULL instead.