Unify coding style

@sjaeckel integrated clang-format with formal coding style. Run his
script and commit changes.

There are pros and cons of this commit.

Mixed coding style is a "broken window". A good single style simplifies
reading and writing code.

On the other hand, this is a big change which will lead to conflicts.
This commit is contained in:
Dmitry Podgorny
2020-01-03 22:02:22 +02:00
parent eef07cef36
commit 562a06425b
62 changed files with 3972 additions and 3746 deletions

View File

@@ -48,32 +48,35 @@
#define CONNECT_TIMEOUT 5000 /* 5 seconds */
#endif
static int _disconnect_cleanup(xmpp_conn_t * const conn,
void * const userdata);
static char *_conn_build_stream_tag(xmpp_conn_t * const conn,
char **attributes, size_t attributes_len);
static void _conn_attributes_new(xmpp_conn_t *conn, char **attrs,
char ***attributes, size_t *attributes_len);
static void _conn_attributes_destroy(xmpp_conn_t *conn, char **attributes,
static int _disconnect_cleanup(xmpp_conn_t *const conn, void *const userdata);
static char *_conn_build_stream_tag(xmpp_conn_t *const conn,
char **attributes,
size_t attributes_len);
static void _conn_attributes_new(xmpp_conn_t *conn,
char **attrs,
char ***attributes,
size_t *attributes_len);
static void _conn_attributes_destroy(xmpp_conn_t *conn,
char **attributes,
size_t attributes_len);
static void _handle_stream_start(char *name, char **attrs,
void * const userdata);
static void _handle_stream_end(char *name,
void * const userdata);
static void _handle_stream_stanza(xmpp_stanza_t *stanza,
void * const userdata);
static unsigned short _conn_default_port(xmpp_conn_t * const conn,
static void
_handle_stream_start(char *name, char **attrs, void *const userdata);
static void _handle_stream_end(char *name, void *const userdata);
static void _handle_stream_stanza(xmpp_stanza_t *stanza, void *const userdata);
static unsigned short _conn_default_port(xmpp_conn_t *const conn,
xmpp_conn_type_t type);
static void _conn_reset(xmpp_conn_t * const conn);
static int _conn_connect(xmpp_conn_t * const conn,
const char * const domain,
const char * const host,
static void _conn_reset(xmpp_conn_t *const conn);
static int _conn_connect(xmpp_conn_t *const conn,
const char *const domain,
const char *const host,
unsigned short port,
xmpp_conn_type_t type,
xmpp_conn_handler callback,
void * const userdata);
void *const userdata);
void xmpp_send_error(xmpp_conn_t * const conn, xmpp_error_type_t const type, char * const text)
void xmpp_send_error(xmpp_conn_t *const conn,
xmpp_error_type_t const type,
char *const text)
{
xmpp_stanza_t *error = xmpp_error_new(conn->ctx, type, text);
@@ -90,12 +93,13 @@ void xmpp_send_error(xmpp_conn_t * const conn, xmpp_error_type_t const type, cha
*
* @ingroup Connections
*/
xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx)
xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t *const ctx)
{
xmpp_conn_t *conn = NULL;
xmpp_connlist_t *tail, *item;
if (ctx == NULL) return NULL;
if (ctx == NULL)
return NULL;
conn = xmpp_alloc(ctx, sizeof(xmpp_conn_t));
if (conn != NULL) {
@@ -146,11 +150,9 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx)
conn->bind_required = 0;
conn->session_required = 0;
conn->parser = parser_new(conn->ctx,
_handle_stream_start,
_handle_stream_end,
_handle_stream_stanza,
conn);
conn->parser =
parser_new(conn->ctx, _handle_stream_start, _handle_stream_end,
_handle_stream_stanza, conn);
conn->reset_parser = 0;
conn->authenticated = 0;
@@ -166,7 +168,8 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx)
/* add connection to ctx->connlist */
tail = conn->ctx->connlist;
while (tail && tail->next) tail = tail->next;
while (tail && tail->next)
tail = tail->next;
item = xmpp_alloc(conn->ctx, sizeof(xmpp_connlist_t));
if (!item) {
@@ -179,8 +182,10 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx)
item->conn = conn;
item->next = NULL;
if (tail) tail->next = item;
else conn->ctx->connlist = item;
if (tail)
tail->next = item;
else
conn->ctx->connlist = item;
}
}
@@ -196,7 +201,7 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx)
*
* @ingroup Connections
*/
xmpp_conn_t *xmpp_conn_clone(xmpp_conn_t * const conn)
xmpp_conn_t *xmpp_conn_clone(xmpp_conn_t *const conn)
{
conn->ref++;
return conn;
@@ -214,7 +219,7 @@ xmpp_conn_t *xmpp_conn_clone(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
void xmpp_conn_set_keepalive(xmpp_conn_t * const conn, int timeout, int interval)
void xmpp_conn_set_keepalive(xmpp_conn_t *const conn, int timeout, int interval)
{
int ret = 0;
@@ -240,7 +245,7 @@ void xmpp_conn_set_keepalive(xmpp_conn_t * const conn, int timeout, int interval
*
* @ingroup Connections
*/
int xmpp_conn_release(xmpp_conn_t * const conn)
int xmpp_conn_release(xmpp_conn_t *const conn)
{
xmpp_ctx_t *ctx;
xmpp_connlist_t *item, *prev;
@@ -255,8 +260,7 @@ int xmpp_conn_release(xmpp_conn_t * const conn)
ctx = conn->ctx;
if (conn->state == XMPP_STATE_CONNECTING ||
conn->state == XMPP_STATE_CONNECTED)
{
conn->state == XMPP_STATE_CONNECTED) {
conn_disconnect(conn);
}
@@ -317,17 +321,23 @@ int xmpp_conn_release(xmpp_conn_t * const conn)
thli = hlitem;
hlitem = hlitem->next;
if (thli->u.ns) xmpp_free(ctx, thli->u.ns);
if (thli->u.name) xmpp_free(ctx, thli->u.name);
if (thli->u.type) xmpp_free(ctx, thli->u.type);
if (thli->u.ns)
xmpp_free(ctx, thli->u.ns);
if (thli->u.name)
xmpp_free(ctx, thli->u.name);
if (thli->u.type)
xmpp_free(ctx, thli->u.type);
xmpp_free(ctx, thli);
}
parser_free(conn->parser);
if (conn->jid) xmpp_free(ctx, conn->jid);
if (conn->pass) xmpp_free(ctx, conn->pass);
if (conn->lang) xmpp_free(ctx, conn->lang);
if (conn->jid)
xmpp_free(ctx, conn->jid);
if (conn->pass)
xmpp_free(ctx, conn->pass);
if (conn->lang)
xmpp_free(ctx, conn->lang);
xmpp_free(ctx, conn);
released = 1;
}
@@ -343,7 +353,7 @@ int xmpp_conn_release(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn)
const char *xmpp_conn_get_jid(const xmpp_conn_t *const conn)
{
return conn->jid;
}
@@ -361,7 +371,7 @@ const char *xmpp_conn_get_jid(const xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
const char *xmpp_conn_get_bound_jid(const xmpp_conn_t * const conn)
const char *xmpp_conn_get_bound_jid(const xmpp_conn_t *const conn)
{
return conn->bound_jid;
}
@@ -377,9 +387,10 @@ const char *xmpp_conn_get_bound_jid(const xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
void xmpp_conn_set_jid(xmpp_conn_t * const conn, const char * const jid)
void xmpp_conn_set_jid(xmpp_conn_t *const conn, const char *const jid)
{
if (conn->jid) xmpp_free(conn->ctx, conn->jid);
if (conn->jid)
xmpp_free(conn->ctx, conn->jid);
conn->jid = xmpp_strdup(conn->ctx, jid);
}
@@ -391,7 +402,7 @@ void xmpp_conn_set_jid(xmpp_conn_t * const conn, const char * const jid)
*
* @ingroup Connections
*/
const char *xmpp_conn_get_pass(const xmpp_conn_t * const conn)
const char *xmpp_conn_get_pass(const xmpp_conn_t *const conn)
{
return conn->pass;
}
@@ -405,22 +416,23 @@ const char *xmpp_conn_get_pass(const xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
void xmpp_conn_set_pass(xmpp_conn_t * const conn, const char * const pass)
void xmpp_conn_set_pass(xmpp_conn_t *const conn, const char *const pass)
{
if (conn->pass) xmpp_free(conn->ctx, conn->pass);
if (conn->pass)
xmpp_free(conn->ctx, conn->pass);
conn->pass = xmpp_strdup(conn->ctx, pass);
}
/** Get the strophe context that the connection is associated with.
* @param conn a Strophe connection object
*
* @return a Strophe context
*
* @ingroup Connections
*/
xmpp_ctx_t* xmpp_conn_get_context(xmpp_conn_t * const conn)
* @param conn a Strophe connection object
*
* @return a Strophe context
*
* @ingroup Connections
*/
xmpp_ctx_t *xmpp_conn_get_context(xmpp_conn_t *const conn)
{
return conn->ctx;
return conn->ctx;
}
/** Initiate a connection to the XMPP server.
@@ -444,11 +456,11 @@ xmpp_ctx_t* xmpp_conn_get_context(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
int xmpp_connect_client(xmpp_conn_t * const conn,
const char * const altdomain,
int xmpp_connect_client(xmpp_conn_t *const conn,
const char *const altdomain,
unsigned short altport,
xmpp_conn_handler callback,
void * const userdata)
void *const userdata)
{
resolver_srv_rr_t *srv_rr_list = NULL;
resolver_srv_rr_t *rr;
@@ -459,7 +471,8 @@ int xmpp_connect_client(xmpp_conn_t * const conn,
int rc;
domain = xmpp_jid_domain(conn->ctx, conn->jid);
if (!domain) return XMPP_EMEM;
if (!domain)
return XMPP_EMEM;
if (altdomain != NULL) {
xmpp_debug(conn->ctx, "xmpp", "Connecting via altdomain.");
@@ -467,16 +480,17 @@ int xmpp_connect_client(xmpp_conn_t * const conn,
port = altport ? altport : _conn_default_port(conn, XMPP_CLIENT);
found = XMPP_DOMAIN_ALTDOMAIN;
/* SSL tunneled connection on 5223 port is legacy and doesn't
* have an SRV record. */
/* SSL tunneled connection on 5223 port is legacy and doesn't
* have an SRV record. */
} else if (!conn->tls_legacy_ssl) {
found = resolver_srv_lookup(conn->ctx, "xmpp-client", "tcp", domain,
&srv_rr_list);
}
if (XMPP_DOMAIN_NOT_FOUND == found) {
xmpp_debug(conn->ctx, "xmpp", "SRV lookup failed, "
"connecting via domain.");
xmpp_debug(conn->ctx, "xmpp",
"SRV lookup failed, "
"connecting via domain.");
host = domain;
port = altport ? altport : _conn_default_port(conn, XMPP_CLIENT);
found = XMPP_DOMAIN_ALTDOMAIN;
@@ -489,8 +503,8 @@ int xmpp_connect_client(xmpp_conn_t * const conn,
port = rr->port;
rr = rr->next;
}
rc = _conn_connect(conn, domain, host, port, XMPP_CLIENT,
callback, userdata);
rc = _conn_connect(conn, domain, host, port, XMPP_CLIENT, callback,
userdata);
} while (rc != 0 && rr != NULL);
xmpp_free(conn->ctx, domain);
@@ -522,18 +536,22 @@ int xmpp_connect_client(xmpp_conn_t * const conn,
*
* @ingroup Connections
*/
int xmpp_connect_component(xmpp_conn_t * const conn, const char * const server,
unsigned short port, xmpp_conn_handler callback,
void * const userdata)
int xmpp_connect_component(xmpp_conn_t *const conn,
const char *const server,
unsigned short port,
xmpp_conn_handler callback,
void *const userdata)
{
/* The server domain, jid and password MUST be specified. */
if (!(server && conn->jid && conn->pass)) return XMPP_EINVOP;
if (!(server && conn->jid && conn->pass))
return XMPP_EINVOP;
/* XEP-0114 does not support TLS */
xmpp_conn_disable_tls(conn);
if (!conn->tls_disabled) {
xmpp_error(conn->ctx, "conn", "Failed to disable TLS. "
"XEP-0114 does not support TLS");
xmpp_error(conn->ctx, "conn",
"Failed to disable TLS. "
"XEP-0114 does not support TLS");
return XMPP_EINT;
}
@@ -565,18 +583,18 @@ int xmpp_connect_component(xmpp_conn_t * const conn, const char * const server,
*
* @ingroup Connections
*/
int xmpp_connect_raw(xmpp_conn_t * const conn,
const char * const altdomain,
int xmpp_connect_raw(xmpp_conn_t *const conn,
const char *const altdomain,
unsigned short altport,
xmpp_conn_handler callback,
void * const userdata)
void *const userdata)
{
conn->is_raw = 1;
return xmpp_connect_client(conn, altdomain, altport, callback, userdata);
}
/* Called when tcp connection is established. */
void conn_established(xmpp_conn_t * const conn)
void conn_established(xmpp_conn_t *const conn)
{
if (conn->tls_legacy_ssl && !conn->is_raw) {
xmpp_debug(conn->ctx, "xmpp", "using legacy SSL connection");
@@ -591,7 +609,8 @@ void conn_established(xmpp_conn_t * const conn)
/* we skip authentication for a "raw" connection, but the event loop
ignores user's handlers when conn->authenticated is not set. */
conn->authenticated = 1;
conn->conn_handler(conn, XMPP_CONN_RAW_CONNECT, 0, NULL, conn->userdata);
conn->conn_handler(conn, XMPP_CONN_RAW_CONNECT, 0, NULL,
conn->userdata);
} else {
/* send stream init */
conn_open_stream(conn);
@@ -609,7 +628,7 @@ void conn_established(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
int xmpp_conn_open_stream_default(xmpp_conn_t * const conn)
int xmpp_conn_open_stream_default(xmpp_conn_t *const conn)
{
if (!conn->is_raw)
return XMPP_EINVOP;
@@ -636,7 +655,8 @@ int xmpp_conn_open_stream_default(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
int xmpp_conn_open_stream(xmpp_conn_t * const conn, char **attributes,
int xmpp_conn_open_stream(xmpp_conn_t *const conn,
char **attributes,
size_t attributes_len)
{
char *tag;
@@ -661,7 +681,7 @@ int xmpp_conn_open_stream(xmpp_conn_t * const conn, char **attributes,
*
* @ingroup Connections
*/
int xmpp_conn_tls_start(xmpp_conn_t * const conn)
int xmpp_conn_tls_start(xmpp_conn_t *const conn)
{
return conn_tls_start(conn);
}
@@ -672,7 +692,7 @@ int xmpp_conn_tls_start(xmpp_conn_t * const conn)
*
* @param conn a Strophe connection object
*/
void conn_disconnect_clean(xmpp_conn_t * const conn)
void conn_disconnect_clean(xmpp_conn_t *const conn)
{
/* remove the timed handler */
xmpp_timed_handler_delete(conn, _disconnect_cleanup);
@@ -686,7 +706,7 @@ void conn_disconnect_clean(xmpp_conn_t * const conn)
*
* @param conn a Strophe connection object
*/
void conn_disconnect(xmpp_conn_t * const conn)
void conn_disconnect(xmpp_conn_t *const conn)
{
xmpp_debug(conn->ctx, "xmpp", "Closing socket.");
conn->state = XMPP_STATE_DISCONNECTED;
@@ -704,14 +724,14 @@ void conn_disconnect(xmpp_conn_t * const conn)
/* prepares a parser reset. this is called from handlers. we can't
* reset the parser immediately as it is not re-entrant. */
void conn_prepare_reset(xmpp_conn_t * const conn, xmpp_open_handler handler)
void conn_prepare_reset(xmpp_conn_t *const conn, xmpp_open_handler handler)
{
conn->reset_parser = 1;
conn->open_handler = handler;
}
/* reset the parser */
void conn_parser_reset(xmpp_conn_t * const conn)
void conn_parser_reset(xmpp_conn_t *const conn)
{
conn->reset_parser = 0;
parser_reset(conn->parser);
@@ -726,7 +746,7 @@ void conn_parser_reset(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
void xmpp_disconnect(xmpp_conn_t * const conn)
void xmpp_disconnect(xmpp_conn_t *const conn)
{
if (conn->state != XMPP_STATE_CONNECTING &&
conn->state != XMPP_STATE_CONNECTED)
@@ -736,8 +756,7 @@ void xmpp_disconnect(xmpp_conn_t * const conn)
xmpp_send_raw_string(conn, "</stream:stream>");
/* setup timed handler in case disconnect takes too long */
handler_add_timed(conn, _disconnect_cleanup,
DISCONNECT_TIMEOUT, NULL);
handler_add_timed(conn, _disconnect_cleanup, DISCONNECT_TIMEOUT, NULL);
}
/** Send a raw string to the XMPP server.
@@ -753,8 +772,7 @@ void xmpp_disconnect(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
void xmpp_send_raw_string(xmpp_conn_t * const conn,
const char * const fmt, ...)
void xmpp_send_raw_string(xmpp_conn_t *const conn, const char *const fmt, ...)
{
va_list ap;
size_t len;
@@ -771,7 +789,8 @@ void xmpp_send_raw_string(xmpp_conn_t * const conn,
len++; /* account for trailing \0 */
bigbuf = xmpp_alloc(conn->ctx, len);
if (!bigbuf) {
xmpp_debug(conn->ctx, "xmpp", "Could not allocate memory for send_raw_string");
xmpp_debug(conn->ctx, "xmpp",
"Could not allocate memory for send_raw_string");
return;
}
va_start(ap, fmt);
@@ -802,16 +821,19 @@ void xmpp_send_raw_string(xmpp_conn_t * const conn,
*
* @ingroup Connections
*/
void xmpp_send_raw(xmpp_conn_t * const conn,
const char * const data, const size_t len)
void xmpp_send_raw(xmpp_conn_t *const conn,
const char *const data,
const size_t len)
{
xmpp_send_queue_t *item;
if (conn->state != XMPP_STATE_CONNECTED) return;
if (conn->state != XMPP_STATE_CONNECTED)
return;
/* create send queue item for queue */
item = xmpp_alloc(conn->ctx, sizeof(xmpp_send_queue_t));
if (!item) return;
if (!item)
return;
item->data = xmpp_alloc(conn->ctx, len);
if (!item->data) {
@@ -845,8 +867,7 @@ void xmpp_send_raw(xmpp_conn_t * const conn,
*
* @ingroup Connections
*/
void xmpp_send(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza)
void xmpp_send(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza)
{
char *buf;
size_t len;
@@ -866,23 +887,22 @@ void xmpp_send(xmpp_conn_t * const conn,
*
* @param conn a Strophe connection object
*/
void conn_open_stream(xmpp_conn_t * const conn)
void conn_open_stream(xmpp_conn_t *const conn)
{
xmpp_send_raw_string(conn,
"<?xml version=\"1.0\"?>" \
"<stream:stream to=\"%s\" " \
"xml:lang=\"%s\" " \
"version=\"1.0\" " \
"xmlns=\"%s\" " \
"<?xml version=\"1.0\"?>"
"<stream:stream to=\"%s\" "
"xml:lang=\"%s\" "
"version=\"1.0\" "
"xmlns=\"%s\" "
"xmlns:stream=\"%s\">",
conn->domain,
conn->lang,
conn->type == XMPP_CLIENT ? XMPP_NS_CLIENT :
XMPP_NS_COMPONENT,
conn->domain, conn->lang,
conn->type == XMPP_CLIENT ? XMPP_NS_CLIENT
: XMPP_NS_COMPONENT,
XMPP_NS_STREAMS);
}
int conn_tls_start(xmpp_conn_t * const conn)
int conn_tls_start(xmpp_conn_t *const conn)
{
int rc;
@@ -906,8 +926,10 @@ int conn_tls_start(xmpp_conn_t * const conn)
}
}
if (rc != 0) {
xmpp_debug(conn->ctx, "conn", "Couldn't start TLS! "
"error %d tls_error %d", rc, conn->error);
xmpp_debug(conn->ctx, "conn",
"Couldn't start TLS! "
"error %d tls_error %d",
rc, conn->error);
}
return rc;
}
@@ -920,7 +942,7 @@ int conn_tls_start(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
long xmpp_conn_get_flags(const xmpp_conn_t * const conn)
long xmpp_conn_get_flags(const xmpp_conn_t *const conn)
{
long flags;
@@ -928,7 +950,8 @@ long xmpp_conn_get_flags(const xmpp_conn_t * const conn)
XMPP_CONN_FLAG_MANDATORY_TLS * conn->tls_mandatory |
XMPP_CONN_FLAG_LEGACY_SSL * conn->tls_legacy_ssl |
XMPP_CONN_FLAG_TRUST_TLS * conn->tls_trust |
XMPP_CONN_FLAG_LEGACY_AUTH * conn->auth_legacy_enabled;;
XMPP_CONN_FLAG_LEGACY_AUTH * conn->auth_legacy_enabled;
;
return flags;
}
@@ -955,11 +978,12 @@ long xmpp_conn_get_flags(const xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
int xmpp_conn_set_flags(xmpp_conn_t * const conn, long flags)
int xmpp_conn_set_flags(xmpp_conn_t *const conn, long flags)
{
if (conn->state != XMPP_STATE_DISCONNECTED) {
xmpp_error(conn->ctx, "conn", "Flags can be set only "
"for disconnected connection");
xmpp_error(conn->ctx, "conn",
"Flags can be set only "
"for disconnected connection");
return XMPP_EINVOP;
}
if (flags & XMPP_CONN_FLAG_DISABLE_TLS &&
@@ -989,7 +1013,7 @@ int xmpp_conn_set_flags(xmpp_conn_t * const conn, long flags)
*
* @ingroup Connections
*/
void xmpp_conn_disable_tls(xmpp_conn_t * const conn)
void xmpp_conn_disable_tls(xmpp_conn_t *const conn)
{
long flags = xmpp_conn_get_flags(conn);
@@ -1003,7 +1027,7 @@ void xmpp_conn_disable_tls(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
int xmpp_conn_is_secured(xmpp_conn_t * const conn)
int xmpp_conn_is_secured(xmpp_conn_t *const conn)
{
return conn->secured && !conn->tls_failed && conn->tls != NULL ? 1 : 0;
}
@@ -1013,7 +1037,7 @@ int xmpp_conn_is_secured(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
int xmpp_conn_is_connecting(xmpp_conn_t * const conn)
int xmpp_conn_is_connecting(xmpp_conn_t *const conn)
{
return conn->state == XMPP_STATE_CONNECTING ? 1 : 0;
}
@@ -1023,7 +1047,7 @@ int xmpp_conn_is_connecting(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
int xmpp_conn_is_connected(xmpp_conn_t * const conn)
int xmpp_conn_is_connected(xmpp_conn_t *const conn)
{
return conn->state == XMPP_STATE_CONNECTED ? 1 : 0;
}
@@ -1033,26 +1057,24 @@ int xmpp_conn_is_connected(xmpp_conn_t * const conn)
*
* @ingroup Connections
*/
int xmpp_conn_is_disconnected(xmpp_conn_t * const conn)
int xmpp_conn_is_disconnected(xmpp_conn_t *const conn)
{
return conn->state == XMPP_STATE_DISCONNECTED ? 1 : 0;
}
/* timed handler for cleanup if normal disconnect procedure takes too long */
static int _disconnect_cleanup(xmpp_conn_t * const conn,
void * const userdata)
static int _disconnect_cleanup(xmpp_conn_t *const conn, void *const userdata)
{
xmpp_debug(conn->ctx, "xmpp",
"disconnection forced by cleanup timeout");
xmpp_debug(conn->ctx, "xmpp", "disconnection forced by cleanup timeout");
conn_disconnect(conn);
return 0;
}
static char *_conn_build_stream_tag(xmpp_conn_t * const conn,
char **attributes, size_t attributes_len)
static char *_conn_build_stream_tag(xmpp_conn_t *const conn,
char **attributes,
size_t attributes_len)
{
char *tag;
size_t len;
@@ -1068,7 +1090,8 @@ static char *_conn_build_stream_tag(xmpp_conn_t * const conn,
for (i = 0; i < attributes_len; ++i)
len += strlen(attributes[i]) + 2;
tag = xmpp_alloc(conn->ctx, len + 1);
if (!tag) return NULL;
if (!tag)
return NULL;
strcpy(tag, tag_head);
for (i = 0; i < attributes_len; ++i) {
@@ -1084,8 +1107,9 @@ static char *_conn_build_stream_tag(xmpp_conn_t * const conn,
strcat(tag, tag_tail);
if (strlen(tag) != len) {
xmpp_error(conn->ctx, "xmpp", "Internal error in "
"_conn_build_stream_tag().");
xmpp_error(conn->ctx, "xmpp",
"Internal error in "
"_conn_build_stream_tag().");
xmpp_free(conn->ctx, tag);
tag = NULL;
}
@@ -1093,20 +1117,24 @@ static char *_conn_build_stream_tag(xmpp_conn_t * const conn,
return tag;
}
static void _conn_attributes_new(xmpp_conn_t *conn, char **attrs,
char ***attributes, size_t *attributes_len)
static void _conn_attributes_new(xmpp_conn_t *conn,
char **attrs,
char ***attributes,
size_t *attributes_len)
{
char **array = NULL;
size_t nr = 0;
size_t i;
if (attrs) {
for (; attrs[nr]; ++nr);
for (; attrs[nr]; ++nr)
;
array = xmpp_alloc(conn->ctx, sizeof(*array) * nr);
for (i = 0; array && i < nr; ++i) {
array[i] = (i & 1) == 0 ? parser_attr_name(conn->ctx, attrs[i])
: xmpp_strdup(conn->ctx, attrs[i]);
if (array[i] == NULL) break;
if (array[i] == NULL)
break;
}
if (!array || i < nr) {
xmpp_error(conn->ctx, "xmpp", "Memory allocation error.");
@@ -1119,7 +1147,8 @@ static void _conn_attributes_new(xmpp_conn_t *conn, char **attrs,
*attributes_len = nr;
}
static void _conn_attributes_destroy(xmpp_conn_t *conn, char **attributes,
static void _conn_attributes_destroy(xmpp_conn_t *conn,
char **attributes,
size_t attributes_len)
{
size_t i;
@@ -1150,23 +1179,24 @@ static char *_get_stream_attribute(char **attrs, char *name)
{
int i;
if (!attrs) return NULL;
if (!attrs)
return NULL;
for (i = 0; attrs[i]; i += 2)
if (strcmp(name, attrs[i]) == 0)
return attrs[i+1];
return attrs[i + 1];
return NULL;
}
static void _handle_stream_start(char *name, char **attrs,
void * const userdata)
static void _handle_stream_start(char *name, char **attrs, void *const userdata)
{
xmpp_conn_t *conn = (xmpp_conn_t *)userdata;
char *id;
int failed = 0;
if (conn->stream_id) xmpp_free(conn->ctx, conn->stream_id);
if (conn->stream_id)
xmpp_free(conn->ctx, conn->stream_id);
conn->stream_id = NULL;
if (strcmp(name, "stream") == 0) {
@@ -1180,8 +1210,10 @@ static void _handle_stream_start(char *name, char **attrs,
failed = 1;
}
} else {
xmpp_error(conn->ctx, "conn", "Server did not open valid stream."
" name = %s.", name);
xmpp_error(conn->ctx, "conn",
"Server did not open valid stream."
" name = %s.",
name);
failed = 1;
}
@@ -1193,8 +1225,7 @@ static void _handle_stream_start(char *name, char **attrs,
}
}
static void _handle_stream_end(char *name,
void * const userdata)
static void _handle_stream_end(char *name, void *const userdata)
{
xmpp_conn_t *conn = (xmpp_conn_t *)userdata;
@@ -1203,8 +1234,7 @@ static void _handle_stream_end(char *name,
conn_disconnect_clean(conn);
}
static void _handle_stream_stanza(xmpp_stanza_t *stanza,
void * const userdata)
static void _handle_stream_stanza(xmpp_stanza_t *stanza, void *const userdata)
{
xmpp_conn_t *conn = (xmpp_conn_t *)userdata;
char *buf;
@@ -1218,13 +1248,13 @@ static void _handle_stream_stanza(xmpp_stanza_t *stanza,
handler_fire_stanza(conn, stanza);
}
static unsigned short _conn_default_port(xmpp_conn_t * const conn,
static unsigned short _conn_default_port(xmpp_conn_t *const conn,
xmpp_conn_type_t type)
{
switch (type) {
case XMPP_CLIENT:
return conn->tls_legacy_ssl ? XMPP_PORT_CLIENT_LEGACY_SSL :
XMPP_PORT_CLIENT;
return conn->tls_legacy_ssl ? XMPP_PORT_CLIENT_LEGACY_SSL
: XMPP_PORT_CLIENT;
case XMPP_COMPONENT:
return XMPP_PORT_COMPONENT;
default:
@@ -1232,7 +1262,7 @@ static unsigned short _conn_default_port(xmpp_conn_t * const conn,
};
}
static void _conn_reset(xmpp_conn_t * const conn)
static void _conn_reset(xmpp_conn_t *const conn)
{
xmpp_ctx_t *ctx = conn->ctx;
xmpp_send_queue_t *sq, *tsq;
@@ -1262,9 +1292,12 @@ static void _conn_reset(xmpp_conn_t * const conn)
conn->stream_error = NULL;
}
if (conn->domain) xmpp_free(ctx, conn->domain);
if (conn->bound_jid) xmpp_free(ctx, conn->bound_jid);
if (conn->stream_id) xmpp_free(ctx, conn->stream_id);
if (conn->domain)
xmpp_free(ctx, conn->domain);
if (conn->bound_jid)
xmpp_free(ctx, conn->bound_jid);
if (conn->stream_id)
xmpp_free(ctx, conn->stream_id);
conn->domain = NULL;
conn->bound_jid = NULL;
conn->stream_id = NULL;
@@ -1280,30 +1313,35 @@ static void _conn_reset(xmpp_conn_t * const conn)
handler_system_delete_all(conn);
}
static int _conn_connect(xmpp_conn_t * const conn,
const char * const domain,
const char * const host,
static int _conn_connect(xmpp_conn_t *const conn,
const char *const domain,
const char *const host,
unsigned short port,
xmpp_conn_type_t type,
xmpp_conn_handler callback,
void * const userdata)
void *const userdata)
{
xmpp_open_handler open_handler;
if (conn->state != XMPP_STATE_DISCONNECTED) return XMPP_EINVOP;
if (type != XMPP_CLIENT && type != XMPP_COMPONENT) return XMPP_EINVOP;
if (host == NULL || port == 0) return XMPP_EINT;
if (conn->state != XMPP_STATE_DISCONNECTED)
return XMPP_EINVOP;
if (type != XMPP_CLIENT && type != XMPP_COMPONENT)
return XMPP_EINVOP;
if (host == NULL || port == 0)
return XMPP_EINT;
_conn_reset(conn);
conn->type = type;
conn->domain = xmpp_strdup(conn->ctx, domain);
if (!conn->domain) return XMPP_EMEM;
if (!conn->domain)
return XMPP_EMEM;
conn->sock = sock_connect(host, port);
xmpp_debug(conn->ctx, "xmpp", "sock_connect() to %s:%u returned %d",
host, port, conn->sock);
if (conn->sock == -1) return XMPP_EINT;
xmpp_debug(conn->ctx, "xmpp", "sock_connect() to %s:%u returned %d", host,
port, conn->sock);
if (conn->sock == -1)
return XMPP_EINT;
if (conn->ka_timeout || conn->ka_interval)
sock_set_keepalive(conn->sock, conn->ka_timeout, conn->ka_interval);
@@ -1311,9 +1349,10 @@ static int _conn_connect(xmpp_conn_t * const conn,
conn->conn_handler = callback;
conn->userdata = userdata;
open_handler = conn->is_raw ? auth_handle_open_stub :
type == XMPP_CLIENT ? auth_handle_open :
auth_handle_component_open;
open_handler = conn->is_raw
? auth_handle_open_stub
: type == XMPP_CLIENT ? auth_handle_open
: auth_handle_component_open;
conn_prepare_reset(conn, open_handler);
/* FIXME: it could happen that the connect returns immediately as