aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordequis <dx@dxzone.com.ar>2015-08-26 02:08:03 -0300
committerdequis <dx@dxzone.com.ar>2015-08-26 02:41:10 -0300
commit06054980118d193c902ae60b0ae7f33271159396 (patch)
tree05019f99d57986dfc4bc5b525e2bae9785082fc7
parent3e6cdb6697d21b2b55af2239738be42ddbbf85c8 (diff)
jabber/io: Split input buffer parsing to a jabber_feed_input() function
To simplify testing. Also allow passing a -1 as size to use strlen() Minor behavior change: The jabber_init_iq_auth() branch can no longer return immediately, which means it will continue through the ssl_pending() check in jabber_read_callback(). Other than that, the size -1 change, and one indentation level less, the function body is the same as before.
-rw-r--r--protocols/jabber/io.c122
1 files changed, 71 insertions, 51 deletions
diff --git a/protocols/jabber/io.c b/protocols/jabber/io.c
index 48ba6b18..2a4b0815 100644
--- a/protocols/jabber/io.c
+++ b/protocols/jabber/io.c
@@ -146,6 +146,76 @@ static gboolean jabber_write_queue(struct im_connection *ic)
}
}
+static gboolean jabber_feed_input(struct im_connection *ic, char *buf, int size)
+{
+ struct jabber_data *jd = ic->proto_data;
+
+ /* Allow not passing a size for debugging purposes.
+ * This never happens when reading from the socket */
+ if (size == -1) {
+ size = strlen(buf);
+ }
+
+ /* Parse. */
+ if (xt_feed(jd->xt, buf, size) < 0) {
+ imcb_error(ic, "XML stream error");
+ imc_logout(ic, TRUE);
+ return FALSE;
+ }
+
+ /* Execute all handlers. */
+ if (!xt_handle(jd->xt, NULL, 1)) {
+ /* Don't do anything, the handlers should have
+ aborted the connection already. */
+ return FALSE;
+ }
+
+ if (jd->flags & JFLAG_STREAM_RESTART) {
+ jd->flags &= ~JFLAG_STREAM_RESTART;
+ jabber_start_stream(ic);
+ }
+
+ /* Garbage collection. */
+ xt_cleanup(jd->xt, NULL, 1);
+
+ /* This is a bit hackish, unfortunately. Although xmltree
+ has nifty event handler stuff, it only calls handlers
+ when nodes are complete. Since the server should only
+ send an opening <stream:stream> tag, we have to check
+ this by hand. :-( */
+ if (!(jd->flags & JFLAG_STREAM_STARTED) && jd->xt && jd->xt->root) {
+ if (g_strcasecmp(jd->xt->root->name, "stream:stream") == 0) {
+ jd->flags |= JFLAG_STREAM_STARTED;
+
+ /* If there's no version attribute, assume
+ this is an old server that can't do SASL
+ authentication. */
+ if (!set_getbool(&ic->acc->set, "sasl") || !sasl_supported(ic)) {
+ /* If there's no version= tag, we suppose
+ this server does NOT implement: XMPP 1.0,
+ SASL and TLS. */
+ if (set_getbool(&ic->acc->set, "tls")) {
+ imcb_error(ic, "TLS is turned on for this "
+ "account, but is not supported by this server");
+ imc_logout(ic, FALSE);
+ return FALSE;
+ } else {
+ if (!jabber_init_iq_auth(ic)) {
+ return FALSE;
+ }
+ }
+ }
+ } else {
+ imcb_error(ic, "XML stream error");
+ imc_logout(ic, TRUE);
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+
static gboolean jabber_read_callback(gpointer data, gint fd, b_input_condition cond)
{
struct im_connection *ic = data;
@@ -164,59 +234,9 @@ static gboolean jabber_read_callback(gpointer data, gint fd, b_input_condition c
}
if (st > 0) {
- /* Parse. */
- if (xt_feed(jd->xt, buf, st) < 0) {
- imcb_error(ic, "XML stream error");
- imc_logout(ic, TRUE);
- return FALSE;
- }
-
- /* Execute all handlers. */
- if (!xt_handle(jd->xt, NULL, 1)) {
- /* Don't do anything, the handlers should have
- aborted the connection already. */
+ if (!jabber_feed_input(ic, buf, st)) {
return FALSE;
}
-
- if (jd->flags & JFLAG_STREAM_RESTART) {
- jd->flags &= ~JFLAG_STREAM_RESTART;
- jabber_start_stream(ic);
- }
-
- /* Garbage collection. */
- xt_cleanup(jd->xt, NULL, 1);
-
- /* This is a bit hackish, unfortunately. Although xmltree
- has nifty event handler stuff, it only calls handlers
- when nodes are complete. Since the server should only
- send an opening <stream:stream> tag, we have to check
- this by hand. :-( */
- if (!(jd->flags & JFLAG_STREAM_STARTED) && jd->xt && jd->xt->root) {
- if (g_strcasecmp(jd->xt->root->name, "stream:stream") == 0) {
- jd->flags |= JFLAG_STREAM_STARTED;
-
- /* If there's no version attribute, assume
- this is an old server that can't do SASL
- authentication. */
- if (!set_getbool(&ic->acc->set, "sasl") || !sasl_supported(ic)) {
- /* If there's no version= tag, we suppose
- this server does NOT implement: XMPP 1.0,
- SASL and TLS. */
- if (set_getbool(&ic->acc->set, "tls")) {
- imcb_error(ic, "TLS is turned on for this "
- "account, but is not supported by this server");
- imc_logout(ic, FALSE);
- return FALSE;
- } else {
- return jabber_init_iq_auth(ic);
- }
- }
- } else {
- imcb_error(ic, "XML stream error");
- imc_logout(ic, TRUE);
- return FALSE;
- }
- }
} else if (st == 0 || (st < 0 && !ssl_sockerr_again(jd->ssl))) {
closesocket(jd->fd);
jd->fd = -1;