JabberWerxC Library Developer Guide

C library for XMPP-based Presence and Instant Messaging

Introduction

The JabberWerxC library is a C XMPP library that allows you to integrate basic presence and messaging capailities into other C-based applications.

JabberWerxC is comprised of the following modules:


Core Types


DOM

JWC has a simplified DOM that represents the minimum XML required by XMPP. It supports namespaces, attributes, elements, and character data; but does not provide for comments, processing instructions, or internal or external entity references.

Naming

Names in jw_dom use Clark notation in the form '{namespace-uri}local-name', with some convenience functions for getting just the namespace-URI and local-name.

Management and Creation

The starting point is jw_dom_ctx, which centralizes the memory management for DOM nodes. Multiple jw_dom_ctx instances can be in use; memory is not shared between jw_dom_ctxs. All nodes for a jw_dom_ctx are freed by calling jw_dom_context_destroy(). A node can be copied from one jw_dom_ctx to another. All nodes are represented by the jw_dom type.

To create nodes:

Basic Operations

The API supports all of the basic operations for a node:

Traversal and Manipulation

Node lists are done using an iterative process. From an element, a number of lists are obtained:

The next node in the list is retrieved using jw_dom_get_sibling(n). The parent is retrieved using jw_dom_get_parent(n). A node can be removed from its parent using jw_dom_detach(n).

As a convenience, the following are available:

To modify an element:

More Information

Detailed API documentation can be found here.


Eventing

Basics

The event loop is managed using libevent's event_base. Every asynchronous function in JWC (e.g. jw_client_connect()) does not actually perform its operation; instead it validates inputs and prepares for the next stage, but the operations are not performed until event_base_dispatch() or one of its alternatives is called.

JWC supports eventing using the jw_event type. It allows the user to bind a function to a particular event with user-specific data, and will be called when the condition is triggered.

Event Information

Each time a jw_event is triggered, each callback receives a jw_event_data structure which has at least the following read-only information:

Eventing Lifecycle

Event Chaining

JWC treats the events for received stanzas ("iqReceived", "messageReceived", and "presenceReceived") as a chain of events: a "before" event (e.g. "beforeIqReceived"), the "on" event (e.g. "iqReceived"), and the "after" event (e.g. "afterIqReceived"). For these events, all of the callbacks for a given level are triggered; if any callback sets the jw_event_data structure's handled flag to true, then the subsequent events in the chain are not triggered. For instance, if a callback for "beforeIqReceived" sets handled to true, then the "iqReceived" and "afterIqReceived" events are not triggered.

More Information

Detailed API documentation can be found here.


Error Handling

There are two levels of error-handling in JWC: return values and the jw_err structure. Most functions that can fail return bool. true means the function succeeded while false means an error was encountered. The jw_err (if provided by the user) provides more detail on the failure, such as a type (enumerated in jw_errcode), and a pre-canned textual message.


SASL Authentication

In the process of establishing a connection to an XMPP server, a jw_client instance will participate in a SASL authentication sequence. Normally, users of jw_client need only provide a jid (JW_CLIENT_CONFIG_USERJID) and password (JW_CLIENT_CONFIG_USERPW) in the jw_htable config passed to the jw_client_connect() function and everything will "just work". However, the process can be customized for users that require non-default authentication mechanisms.

SASL Factories

A jw_sasl_factory instance manages a set of SASL mechanisms and assists in choosing the "best" one given a specification for what the remote endpoint supports. As far as the jw_sasl_factory instance is concerned, the "best", or "most preferred" mechanism is the one most recently registered with the factory. The rule is: when registering mechanisms with a factory, register them in reverse order of preference. The factory should then be added to the jw_htable config passed to the jw_client_connect function with the key JW_CLIENT_CONFIG_SASL_FACTORY.

SASL Mechanisms

Creating a SASL mechanism is as simple as mapping the stages of the mechanism sequence to a function table. Specifically, there is an init function, called before SASL authentication begins; a cleanup function, called after it completes; a start function, called upon initiation of the authentication sequence; and a step function, called for each authentication step thereafter. The framework provides the mechanism with storage for a single void* pointer so the mechanism can keep private state. JWC already has implementations for default, "Manditory-To-Implement" mechanisms (PLAIN and MD5) which can be used in any custom factory sets that require them.

The mechanisms handle only raw, base64-decoded data in their start and step functions. Higher-level communications, such as <success/>and <failure/> elements, are handled by the framework and do not reach the mechanism implementations themselves. The data the mechansisms produce for output does not need to be base64-encoded -- the framework will do that automatically if not explicitly told not to. See the implementation of SASL PLAIN (src/sasl/sasl_mech_plain.c) for a complete example.


Sample Applications

A walkthrough for creating a simple client can be seen in the Getting Started Guide.


© 2012 Cisco Systems, Inc. All rights reserved.