Strophe: Connection management

Strophe 0.9 XMPP client library

Functions
Connection management

## Functions
xmpp_conn_t * | xmpp_conn_new (xmpp_ctx_t *const ctx)
| Create a new Strophe connection object. More...
xmpp_conn_t * | xmpp_conn_clone (xmpp_conn_t *const conn)
| Clone a Strophe connection object. More...
void | xmpp_conn_set_keepalive (xmpp_conn_t *const conn, int timeout, int interval)
| Set TCP keepalive parameters Turn on TCP keepalive and set timeout and interval. More...
int | xmpp_conn_release (xmpp_conn_t *const conn)
| Release a Strophe connection object. More...
const char * | xmpp_conn_get_jid (const xmpp_conn_t *const conn)
| Get the JID which is or will be bound to the connection. More...
const char * | xmpp_conn_get_bound_jid (const xmpp_conn_t *const conn)
| Get the JID discovered during binding time. More...
void | xmpp_conn_set_jid (xmpp_conn_t *const conn, const char *const jid)
| Set the JID of the user that will be bound to the connection. More...
const char * | xmpp_conn_get_pass (const xmpp_conn_t *const conn)
| Get the password used for authentication of a connection. More...
void | xmpp_conn_set_pass (xmpp_conn_t *const conn, const char *const pass)
| Set the password used to authenticate the connection. More...
xmpp_ctx_t * | xmpp_conn_get_context (xmpp_conn_t *const conn)
| Get the strophe context that the connection is associated with. More...
int | xmpp_connect_client (xmpp_conn_t *const conn, const char *const altdomain, unsigned short altport, xmpp_conn_handler callback, void *const userdata)
| Initiate a connection to the XMPP server. More...
int | xmpp_connect_component (xmpp_conn_t *const conn, const char *const server, unsigned short port, xmpp_conn_handler callback, void *const userdata)
| Initiate a component connection to server. More...
int | xmpp_connect_raw (xmpp_conn_t *const conn, const char *const altdomain, unsigned short altport, xmpp_conn_handler callback, void *const userdata)
| Initiate a raw connection to the XMPP server. More...
int | xmpp_conn_open_stream_default (xmpp_conn_t *const conn)
| Send the default opening stream tag. More...
int | xmpp_conn_open_stream (xmpp_conn_t *const conn, char **attributes, size_t attributes_len)
| Send an opening stream tag. More...
int | xmpp_conn_tls_start (xmpp_conn_t *const conn)
| Start synchronous TLS handshake with the server. More...
void | xmpp_disconnect (xmpp_conn_t *const conn)
| Initiate termination of the connection to the XMPP server. More...
void | xmpp_send_raw_string (xmpp_conn_t *const conn, const char *const fmt,...)
| Send a raw string to the XMPP server. More...
void | xmpp_send_raw (xmpp_conn_t *const conn, const char *const data, const size_t len)
| Send raw bytes to the XMPP server. More...
void | xmpp_send (xmpp_conn_t *const conn, xmpp_stanza_t *const stanza)
| Send an XML stanza to the XMPP server. More...
long | xmpp_conn_get_flags (const xmpp_conn_t *const conn)
| Return applied flags for the connection. More...
int | xmpp_conn_set_flags (xmpp_conn_t *const conn, long flags)
| Set flags for the connection. More...
void | xmpp_conn_disable_tls (xmpp_conn_t *const conn)
| Disable TLS for this connection, called by users of the library. More...
int | xmpp_conn_is_secured (xmpp_conn_t *const conn)
| Return whether TLS session is established or not. More...

## Detailed Description

## Function Documentation

## ◆ xmpp_conn_new()

xmpp_conn_t* xmpp_conn_new | ( | xmpp_ctx_t *const | ctx | ) |

Create a new Strophe connection object.

Parameters

  ctx | a Strophe context object

Returns
  a Strophe connection object or NULL on an error

## ◆ xmpp_conn_clone()

xmpp_conn_t* xmpp_conn_clone | ( | xmpp_conn_t *const | conn | ) |

Clone a Strophe connection object.

Parameters

  conn | a Strophe connection object

Returns
  the same conn object passed in with its reference count incremented by 1

## ◆ xmpp_conn_set_keepalive()

void xmpp_conn_set_keepalive | ( | xmpp_conn_t *const | conn,
|  | int | timeout,
|  | int | interval
| ) |  |

Set TCP keepalive parameters Turn on TCP keepalive and set timeout and interval.

Zero timeout disables TCP keepalives. The parameters are applied immediately for a non disconnected object. Also, they are applied when the connection object connects successfully.

Parameters

  conn | a Strophe connection object
  timeout | TCP keepalive timeout in seconds
  interval | TCP keepalive interval in seconds

## ◆ xmpp_conn_release()

int xmpp_conn_release | ( | xmpp_conn_t *const | conn | ) |

Release a Strophe connection object.

Decrement the reference count by one for a connection, freeing the connection object if the count reaches 0.

Parameters

  conn | a Strophe connection object

Returns
  TRUE if the connection object was freed and FALSE otherwise

## ◆ xmpp_conn_get_jid()

const char* xmpp_conn_get_jid | ( | const xmpp_conn_t *const | conn | ) |

Get the JID which is or will be bound to the connection.

Parameters

  conn | a Strophe connection object

Returns
  a string containing the full JID or NULL if it has not been set

## ◆ xmpp_conn_get_bound_jid()

const char* xmpp_conn_get_bound_jid | ( | const xmpp_conn_t *const | conn | ) |

Get the JID discovered during binding time.

This JID will contain the resource used by the current connection. This is useful in the case where a resource was not specified for binding.

Parameters

  conn | a Strophe connection object.

Returns
  a string containing the full JID or NULL if it's not been discovered

## ◆ xmpp_conn_set_jid()

void xmpp_conn_set_jid | ( | xmpp_conn_t *const | conn,
|  | const char *const | jid
| ) |  |

Set the JID of the user that will be bound to the connection.

If any JID was previously set, it will be discarded. This should not be be used after a connection is created. The function will make a copy of the JID string. If the supplied JID is missing the node, SASL ANONYMOUS authentication will be used.

Parameters

  conn | a Strophe connection object
  jid | a full or bare JID

## ◆ xmpp_conn_get_pass()

const char* xmpp_conn_get_pass | ( | const xmpp_conn_t *const | conn | ) |

Get the password used for authentication of a connection.

Parameters

  conn | a Strophe connection object

Returns
  a string containing the password or NULL if it has not been set

## ◆ xmpp_conn_set_pass()

void xmpp_conn_set_pass | ( | xmpp_conn_t *const | conn,
|  | const char *const | pass
| ) |  |

Set the password used to authenticate the connection.

If any password was previously set, it will be discarded. The function will make a copy of the password string.

Parameters

  conn | a Strophe connection object
  pass | the password

## ◆ xmpp_conn_get_context()

xmpp_ctx_t* xmpp_conn_get_context | ( | xmpp_conn_t *const | conn | ) |

Get the strophe context that the connection is associated with.

Parameters

  conn | a Strophe connection object

Returns
  a Strophe context

## ◆ xmpp_connect_client()

int xmpp_connect_client | ( | xmpp_conn_t *const | conn,
|  | const char *const | altdomain,
|  | unsigned short | altport,
|  | xmpp_conn_handler | callback,
|  | void *const | userdata
| ) |  |

Initiate a connection to the XMPP server.

This function returns immediately after starting the connection process to the XMPP server, and notifications of connection state changes will be sent to the callback function. The domain and port to connect to are usually determined by an SRV lookup for the xmpp-client service at the domain specified in the JID. If SRV lookup fails, altdomain and altport will be used instead if specified.

Parameters

  conn | a Strophe connection object
  altdomain | a string with domain to use if SRV lookup fails. If this is NULL, the domain from the JID will be used.
  altport | an integer port number to use if SRV lookup fails. If this is 0, the default port will be assumed.
  callback | a xmpp_conn_handler callback function that will receive notifications of connection status
  userdata | an opaque data pointer that will be passed to the callback

Returns
  XMPP_EOK (0) on success or a number less than 0 on failure

## ◆ xmpp_connect_component()

int xmpp_connect_component | ( | xmpp_conn_t *const | conn,
|  | const char *const | server,
|  | unsigned short | port,
|  | xmpp_conn_handler | callback,
|  | void *const | userdata
| ) |  |

Initiate a component connection to server.

This function returns immediately after starting the connection process to the XMPP server, and notifications of connection state changes will be sent to the internal callback function that will set up handler for the component handshake as defined in XEP-0114. The domain and port to connect to must be provided in this case as the JID provided to the call serves as component identifier to the server and is not subject to DNS resolution.

Parameters

  conn | a Strophe connection object
  server | a string with domain to use directly as the domain can't be extracted from the component name/JID. If this is not set, the call will fail.
  port | an integer port number to use to connect to server expecting an external component. If this is 0, the port 5347 will be assumed.
  callback | a xmpp_conn_handler callback function that will receive notifications of connection status
  userdata | an opaque data pointer that will be passed to the callback

Returns
  XMPP_EOK (0) on success or a number less than 0 on failure

## ◆ xmpp_connect_raw()

int xmpp_connect_raw | ( | xmpp_conn_t *const | conn,
|  | const char *const | altdomain,
|  | unsigned short | altport,
|  | xmpp_conn_handler | callback,
|  | void *const | userdata
| ) |  |

Initiate a raw connection to the XMPP server.

Arguments and behaviour of the function are similar to xmpp_connect_client(), but it skips authentication process. In opposite to xmpp_connect_client() during connection process two events are generated instead of one. User's callback is called with event XMPP_CONN_RAW_CONNECT when the TCP connection with the server is established. At this point user might want to open an XMPP stream with xmpp_conn_open_stream() or establish TLS session with xmpp_conn_tls_start(). Event XMPP_CONN_CONNECT is generated when the XMPP stream is opened successfully and user may send stanzas over the connection.

This function doesn't use password nor node part of a jid. Therefore, the only required configuration is a domain (or full jid) passed via xmpp_conn_set_jid().

See also
  xmpp_connect_client()

Returns
  XMPP_EOK (0) on success a number less than 0 on failure

## ◆ xmpp_conn_open_stream_default()

int xmpp_conn_open_stream_default | ( | xmpp_conn_t *const | conn | ) |

Send the default opening stream tag.

The default tag is the one sent by xmpp_connect_client(). User's connection handler is called with event XMPP_CONN_CONNECT when server replies with its opening tag.

Returns
  XMPP_EOK (0) on success a number less than 0 on failure

Note
  The connection must be connected with xmpp_connect_raw().

## ◆ xmpp_conn_open_stream()

int xmpp_conn_open_stream | ( | xmpp_conn_t *const | conn,
|  | char ** | attributes,
|  | size_t | attributes_len
| ) |  |

Send an opening stream tag.

User's connection handler is called with event XMPP_CONN_CONNECT when server replies with its opening tag.

Parameters

  conn | a Strophe connection object
  attributes | Array of strings in format: even index points to an attribute name and odd index points to its value
  attributes_len | Number of elements in the attributes array, it should be number of attributes multiplied by 2

Returns
  XMPP_EOK (0) on success a number less than 0 on failure

Note
  The connection must be connected with xmpp_connect_raw().

## ◆ xmpp_conn_tls_start()

int xmpp_conn_tls_start | ( | xmpp_conn_t *const | conn | ) |

Start synchronous TLS handshake with the server.

Returns
  XMPP_EOK (0) on success a number less than 0 on failure

## ◆ xmpp_disconnect()

void xmpp_disconnect | ( | xmpp_conn_t *const | conn | ) |

Initiate termination of the connection to the XMPP server.

This function starts the disconnection sequence by sending </stream:stream> to the XMPP server. This function will do nothing if the connection state is CONNECTING or CONNECTED.

Parameters

  conn | a Strophe connection object

## ◆ xmpp_send_raw_string()

void xmpp_send_raw_string | ( | xmpp_conn_t *const | conn,
|  | const char *const | fmt,
|  |  | ...
| ) |  |

Send a raw string to the XMPP server.

This function is a convenience function to send raw string data to the XMPP server. It is used by Strophe to send short messages instead of building up an XML stanza with DOM methods. This should be used with care as it does not validate the data; invalid data may result in immediate stream termination by the XMPP server.

Parameters

  conn | a Strophe connection object
  fmt | a printf-style format string followed by a variable list of arguments to format

## ◆ xmpp_send_raw()

void xmpp_send_raw | ( | xmpp_conn_t *const | conn,
|  | const char *const | data,
|  | const size_t | len
| ) |  |

Send raw bytes to the XMPP server.

This function is a convenience function to send raw bytes to the XMPP server. It is used primarily by xmpp_send_raw_string(). This function should be used with care as it does not validate the bytes and invalid data may result in stream termination by the XMPP server.

Parameters

  conn | a Strophe connection object
  data | a buffer of raw bytes
  len | the length of the data in the buffer

## ◆ xmpp_send()

void xmpp_send | ( | xmpp_conn_t *const | conn,
|  | xmpp_stanza_t *const | stanza
| ) |  |

Send an XML stanza to the XMPP server.

This is the main way to send data to the XMPP server. The function will terminate without action if the connection state is not CONNECTED.

Parameters

  conn | a Strophe connection object
  stanza | a Strophe stanza object

## ◆ xmpp_conn_get_flags()

long xmpp_conn_get_flags | ( | const xmpp_conn_t *const | conn | ) |

Return applied flags for the connection.

Parameters

  conn | a Strophe connection object

Returns
  ORed connection flags that are applied for the connection.

## ◆ xmpp_conn_set_flags()

int xmpp_conn_set_flags | ( | xmpp_conn_t *const | conn,
|  | long | flags
| ) |  |

Set flags for the connection.

This function applies set flags and resets unset ones. Default connection configuration is all flags unset. Flags can be applied only for a connection in disconnected state. All unsupported flags are ignored. If a flag is unset after successful set operation then the flag is not supported by current version.

Supported flags are:

- XMPP_CONN_FLAG_DISABLE_TLS
- XMPP_CONN_FLAG_MANDATORY_TLS
- XMPP_CONN_FLAG_LEGACY_SSL
- XMPP_CONN_FLAG_TRUST_TLS

Parameters

  conn | a Strophe connection object
  flags | ORed connection flags

Returns
  XMPP_EOK (0) on success or a number less than 0 on failure

## ◆ xmpp_conn_disable_tls()

void xmpp_conn_disable_tls | ( | xmpp_conn_t *const | conn | ) |

Disable TLS for this connection, called by users of the library.

Occasionally a server will be misconfigured to send the starttls feature, but will not support the handshake.

Parameters

  conn | a Strophe connection object

Note
  this function is deprecated

See also
  xmpp_conn_set_flags()

## ◆ xmpp_conn_is_secured()

int xmpp_conn_is_secured | ( | xmpp_conn_t *const | conn | ) |

Return whether TLS session is established or not.

Returns
  TRUE if TLS session is established and FALSE otherwise