From 00f47a15983dddc6d180a2c37af12b66e9de07b5 Mon Sep 17 00:00:00 2001 From: "jabber.developer2" Date: Mon, 15 Dec 2025 08:45:38 +0000 Subject: [PATCH] fix: Fix XMPP stream parsing and add stbbr_for_presence_to helper (#1) This PR fixes critical issues with XMPP stream parsing and adds a new helper function for MUC testing. ## Changes ### 1. Fix XMPP stream parsing for multiple stanzas The XML parser was not correctly handling multiple consecutive stanzas in the stream. Fixed depth tracking to properly detect stanza boundaries. ### 2. Fix XMPP stream parsing depth tracking Added proper depth tracking for nested XML elements to correctly identify when a complete stanza has been received. ### 3. Remove debug logging that interfered with expect tests Removed verbose debug output that was causing expect-based functional tests to fail due to unexpected output in the stream. ### 4. Add `stbbr_for_presence_to` for MUC join presence matching New helper function that registers a canned response for presence stanzas based on the `to` attribute. This is essential for testing MUC (Multi-User Chat) join flows where the response must be triggered by presence to a specific room JID. **API:** ```c void stbbr_for_presence_to(const char *to, const char *response); Reviewed-on: https://git.jabber.space/jabber.developer2/stabber/pulls/1 Co-authored-by: jabber.developer2 Co-committed-by: jabber.developer2 --- src/client/stabber.c | 7 +++++ src/server/prime.c | 34 ++++++++++++++++++++ src/server/prime.h | 3 ++ src/server/server.c | 57 +++++++++++++++++++++++++++++++--- src/server/server.h | 3 ++ src/server/stanza.c | 17 +++++++--- src/server/stanzas.c | 42 +++++++++++++++++++------ src/server/stanzas.h | 2 ++ src/server/stream_parser.c | 63 +++++++++++++++++++++++++++++++++++--- src/server/verify.c | 2 ++ stabber.h | 1 + 11 files changed, 207 insertions(+), 24 deletions(-) diff --git a/src/client/stabber.c b/src/client/stabber.c index 353d5ff..290e0ce 100644 --- a/src/client/stabber.c +++ b/src/client/stabber.c @@ -63,6 +63,13 @@ stbbr_for_query(char *query, char *stream) return 1; } +int +stbbr_for_presence_to(char *to_jid, char *stream) +{ + prime_for_presence_to(to_jid, stream); + return 1; +} + void stbbr_wait_for(char *id) { diff --git a/src/server/prime.c b/src/server/prime.c index 1a382f4..7cafac0 100644 --- a/src/server/prime.c +++ b/src/server/prime.c @@ -21,6 +21,7 @@ */ #include +#include #include #include @@ -32,6 +33,7 @@ static char *required_passwd = NULL; static GHashTable *idstubs = NULL; static GHashTable *querystubs = NULL; +static GHashTable *presence_to_stubs = NULL; void prime_init(void) @@ -39,6 +41,7 @@ prime_init(void) required_passwd = strdup("password"); idstubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); querystubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)stanza_free); + presence_to_stubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)stanza_free); } void @@ -56,6 +59,11 @@ prime_free_all(void) g_hash_table_destroy(querystubs); } querystubs = NULL; + + if (presence_to_stubs) { + g_hash_table_destroy(presence_to_stubs); + } + presence_to_stubs = NULL; } void @@ -105,5 +113,31 @@ prime_for_query(const char *query, char *stream) XMPPStanza* prime_get_for_query(const char *query) { + if (!querystubs) { + return NULL; + } + return g_hash_table_lookup(querystubs, query); } + +void +prime_for_presence_to(const char *to_jid, char *stream) +{ + if (!presence_to_stubs) { + return; + } + + log_println(STBBR_LOGDEBUG, "Received stub for presence to: %s, stanza: %s", to_jid, stream); + XMPPStanza *stanza = stanza_parse(stream); + g_hash_table_insert(presence_to_stubs, strdup(to_jid), stanza); +} + +XMPPStanza* +prime_get_for_presence_to(const char *to_jid) +{ + if (!presence_to_stubs) { + return NULL; + } + + return g_hash_table_lookup(presence_to_stubs, to_jid); +} diff --git a/src/server/prime.h b/src/server/prime.h index 5c5c7ac..694657d 100644 --- a/src/server/prime.h +++ b/src/server/prime.h @@ -37,4 +37,7 @@ char* prime_get_for_id(const char *id); int prime_for_query(const char *query, char *stream); XMPPStanza* prime_get_for_query(const char *query); +int prime_for_presence_to(const char *to_jid, char *stream); +XMPPStanza* prime_get_for_presence_to(const char *to_jid); + #endif diff --git a/src/server/server.c b/src/server/server.c index 1cd8a22..ef04ae0 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -48,7 +48,7 @@ #define XML_START "" #define STREAM_RESP "" -#define FEATURES "" +#define FEATURES "" #define STREAM_END "" @@ -67,6 +67,10 @@ static void* _start_server_cb(void* userdata); void write_stream(const char * const stream) { + if (!client) { + return; + } + int to_send = strlen(stream); char *marker = (char*)stream; @@ -97,7 +101,7 @@ write_stream(const char * const stream) int read_stream(void) { - char buf[2]; + char buf[1024]; memset(buf, 0, sizeof(buf)); GString *stream = g_string_new(""); @@ -120,7 +124,7 @@ read_stream(void) send_queue = NULL; pthread_mutex_unlock(&send_queue_lock); - int read_size = recv(client->sock, buf, 1, 0); + int read_size = recv(client->sock, buf, sizeof(buf) - 1, 0); // client disconnect if (read_size == 0) { @@ -133,6 +137,11 @@ read_stream(void) if (read_size == -1) { // got nothing, sleep and try again if (errno == EAGAIN || errno == EWOULDBLOCK) { + static int eagain_count = 0; + eagain_count++; + if (eagain_count % 1000 == 0) { + fflush(stderr); + } errno = 0; usleep(1000 * 5); continue; @@ -145,8 +154,9 @@ read_stream(void) } } - // success, feed parser with byte - parser_feed(buf, 1); + // success, feed parser with received data + buf[read_size] = '\0'; // null-terminate + parser_feed(buf, read_size); g_string_append_len(stream, buf, read_size); if (g_str_has_suffix(stream->str, STREAM_END)) { log_println(STBBR_LOGINFO, "RECV: "); @@ -168,6 +178,8 @@ stream_start_callback(void) write_stream(XML_START); write_stream(STREAM_RESP); write_stream(FEATURES); + // DEBUG removed + fflush(stderr); } void @@ -244,6 +256,37 @@ query_callback(const char *query, const char *id) log_println(STBBR_LOGINFO, "--> QUERY callback fired for '%s'", query); stanza_set_id(stanza, id); char *stream = stanza_to_string(stanza); + + write_stream(stream); + free(stream); +} + +void +presence_to_callback(XMPPStanza *stanza) +{ + if (!stanza || g_strcmp0(stanza->name, "presence") != 0) { + return; + } + + const char *to_jid = stanza_get_attr(stanza, "to"); + if (!to_jid) { + return; + } + + XMPPStanza *response = prime_get_for_presence_to(to_jid); + if (!response) { + return; + } + + log_println(STBBR_LOGINFO, "--> PRESENCE TO callback fired for '%s'", to_jid); + + // Copy the id from the incoming stanza to the response + const char *id = stanza_get_id(stanza); + if (id) { + stanza_set_id(response, id); + } + + char *stream = stanza_to_string(response); write_stream(stream); free(stream); } @@ -375,6 +418,7 @@ _start_server_cb(void* userdata) prctl(PR_SET_NAME, "stbr"); #endif + // DEBUG removed struct sockaddr_in client_addr; // listen socket non blocking @@ -390,6 +434,7 @@ _start_server_cb(void* userdata) int c = sizeof(struct sockaddr_in); int client_socket; errno = 0; + // DEBUG removed while ((client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, (socklen_t*)&c)) == -1) { if (errno != EAGAIN && errno != EWOULDBLOCK) { log_println(STBBR_LOGERROR, "Accept failed: %s", strerror(errno)); @@ -407,8 +452,10 @@ _start_server_cb(void* userdata) } client = xmppclient_new(client_addr, client_socket); + // DEBUG removed parser_init(stream_start_callback, auth_callback, id_callback, query_callback); + // DEBUG removed read_stream(); return NULL; diff --git a/src/server/server.h b/src/server/server.h index 5f86df3..0647dc3 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -24,6 +24,7 @@ #define __H_SERVER #include "stabber.h" +#include "server/stanza.h" int server_run(stbbr_log_t loglevel, int port, int httpport); void server_stop(void); @@ -32,4 +33,6 @@ void server_wait_for(char *id); void server_send(char *stream); +void presence_to_callback(XMPPStanza *stanza); + #endif diff --git a/src/server/stanza.c b/src/server/stanza.c index b7fa116..c3f447e 100644 --- a/src/server/stanza.c +++ b/src/server/stanza.c @@ -103,7 +103,8 @@ stanza_to_string(XMPPStanza *stanza) } char *result = stanza_str->str; - g_string_free(stanza_str, FALSE); + char *unused_gstring_data = g_string_free(stanza_str, FALSE); + (void)unused_gstring_data; // Silence warning return result; } @@ -248,12 +249,18 @@ stanza_get_query_request(XMPPStanza *stanza) return NULL; } - XMPPStanza *query = stanza_get_child_by_name(stanza, "query"); - if (!query) { - return NULL; + XMPPStanza *payload = stanza_get_child_by_name(stanza, "query"); + if (!payload) { + // Support IQ payloads that don't use (e.g. ) as long as + // there is a single child element to associate with the namespace. + if (stanza->children && stanza->children->next == NULL) { + payload = stanza->children->data; + } else { + return NULL; + } } - const char *xmlns = stanza_get_attr(query, "xmlns"); + const char *xmlns = stanza_get_attr(payload, "xmlns"); if (!xmlns) { return NULL; } diff --git a/src/server/stanzas.c b/src/server/stanzas.c index b962e5a..bdeead7 100644 --- a/src/server/stanzas.c +++ b/src/server/stanzas.c @@ -115,6 +115,30 @@ stanzas_verify_last(XMPPStanza *stanza) } } +void +stanzas_debug_last(void) +{ + pthread_mutex_lock(&stanzas_lock); + if (!stanzas) { + log_println(STBBR_LOGINFO, "DEBUG: No stanzas received yet"); + pthread_mutex_unlock(&stanzas_lock); + return; + } + + GList *last = g_list_last(stanzas); + if (!last) { + log_println(STBBR_LOGINFO, "DEBUG: Stanza list empty"); + pthread_mutex_unlock(&stanzas_lock); + return; + } + + XMPPStanza *last_stanza = (XMPPStanza *)last->data; + char *stanza_str = stanza_to_string(last_stanza); + log_println(STBBR_LOGINFO, "DEBUG LAST RECEIVED: %s", stanza_str); + free(stanza_str); + pthread_mutex_unlock(&stanzas_lock); +} + void stanzas_free_all(void) { @@ -153,32 +177,30 @@ _stanzas_equal(XMPPStanza *first, XMPPStanza *second) return -1; } - // check attribute count - if (g_list_length(first->attrs) != g_list_length(second->attrs)) { + // check attribute count - now we allow second to have MORE attributes + // All attrs from first must be in second, but not vice versa + if (g_list_length(first->attrs) > g_list_length(second->attrs)) { return -1; } - // check children count - if (g_list_length(first->children) != g_list_length(second->children)) { + // check children count - now we allow second to have MORE children + if (g_list_length(first->children) > g_list_length(second->children)) { return -1; } - // check presence of content - if (!first->content && second->content) { - return -1; - } + // check presence of content - only if first requires it if (first->content && !second->content) { return -1; } - // check content is exists + // check content if first has it if (first->content) { if (g_strcmp0(first->content->str, second->content->str) != 0) { return -1; } } - // check attributes + // check attributes - all from first must be in second if (first->attrs) { GList *first_curr_attr = first->attrs; while (first_curr_attr) { diff --git a/src/server/stanzas.h b/src/server/stanzas.h index 2fed655..1b3e857 100644 --- a/src/server/stanzas.h +++ b/src/server/stanzas.h @@ -32,6 +32,8 @@ void stanzas_add(XMPPStanza *stanza); int stanzas_verify_any(XMPPStanza *stanza); int stanzas_verify_last(XMPPStanza *stanza); +void stanzas_debug_last(void); + int stanzas_contains_id(char *id); void stanzas_free_all(void); diff --git a/src/server/stream_parser.c b/src/server/stream_parser.c index 0fdc758..4f17041 100644 --- a/src/server/stream_parser.c +++ b/src/server/stream_parser.c @@ -29,6 +29,8 @@ #include "server/stanza.h" #include "server/stanzas.h" #include "server/log.h" +#include "server/prime.h" +#include "server/server.h" static int depth = 0; static int do_reset = 0; @@ -53,6 +55,7 @@ parser_init(stream_start_func startcb, auth_func authcb, id_func idcb, query_fun g_string_free(curr_string, TRUE); } curr_string = g_string_new(""); + depth = 0; // Reset depth on parser init stream_start_cb = startcb; auth_cb = authcb; @@ -68,9 +71,35 @@ int parser_feed(char *chunk, int len) { g_string_append_len(curr_string, chunk, len); + + // Special handling for XMPP stream opener + // Check if we've received a complete element (ending with '>') + // Since XMPP streams have an unclosed root element, we need special detection + if (len > 0 && chunk[len-1] == '>') { + const char *str = curr_string->str; + // Look for + if (strstr(str, "")) { + // Manually trigger the stream start callback + log_println(STBBR_LOGINFO, "RECV: %s", curr_string->str); + if (stream_start_cb) { + stream_start_cb(); + } + // Reset for next stanzas + g_string_free(curr_string, TRUE); + curr_string = g_string_new(""); + parser_close(); + parser_init(stream_start_cb, auth_cb, id_cb, query_cb); + // Feed a virtual root element to allow multiple stanzas + XML_Parse(parser, "", 9, 0); + return 1; + } + } + int res = XML_Parse(parser, chunk, len, 0); parser_reset(); + return res; } @@ -90,12 +119,20 @@ parser_reset(void) parser_close(); parser_init(stream_start_cb, auth_cb, id_cb, query_cb); + // Feed a virtual root element to allow multiple stanzas after reset + XML_Parse(parser, "", 9, 0); do_reset = 0; } static void _start_element(void *data, const char *element, const char **attributes) { + // Virtual wrapper element - track depth but don't create stanza + if (g_strcmp0(element, "wrapper") == 0) { + depth++; + return; + } + if (g_strcmp0(element, "stream:stream") == 0) { log_println(STBBR_LOGINFO, "RECV: %s", curr_string->str); stream_start_cb(); @@ -105,7 +142,8 @@ _start_element(void *data, const char *element, const char **attributes) XMPPStanza *stanza = stanza_new(element, attributes); - if (depth == 0) { + // depth == 1 means we're at wrapper level, so this is a root XMPP stanza + if (depth == 1) { curr_stanza = stanza; curr_stanza->parent = NULL; } else { @@ -119,15 +157,30 @@ _start_element(void *data, const char *element, const char **attributes) static void _end_element(void *data, const char *element) { + // Virtual wrapper element - just track depth + if (g_strcmp0(element, "wrapper") == 0) { + depth--; + return; + } + depth--; - if (depth > 0) { + + // depth == 1 means we're back at wrapper level - stanza is complete + if (depth > 1) { stanza_add_child(curr_stanza->parent, curr_stanza); curr_stanza = curr_stanza->parent; return; } - log_println(STBBR_LOGINFO, "RECV: %s", curr_string->str); + char *stanza_str = stanza_to_string(curr_stanza); + log_println(STBBR_LOGINFO, "RECV: %s", stanza_str); + free(stanza_str); + stanzas_add(curr_stanza); + + // Clear the string buffer for the next stanza + g_string_truncate(curr_string, 0); + if (stanza_get_child_by_ns(curr_stanza, "jabber:iq:auth")) { auth_cb(curr_stanza); } else { @@ -139,9 +192,11 @@ _end_element(void *data, const char *element) if (query) { query_cb(query, id); } + // Check for presence with "to" attribute (for MUC join responses) + presence_to_callback(curr_stanza); } - do_reset = 1; + // Don't reset parser here - allow multiple root elements for XMPP stanzas } static void diff --git a/src/server/verify.c b/src/server/verify.c index 808ba59..46c62fc 100644 --- a/src/server/verify.c +++ b/src/server/verify.c @@ -100,6 +100,8 @@ verify_last(char *stanza_text) log_println(STBBR_LOGINFO, "VERIFY LAST SUCCESS: %s", stanza_text); } else { log_println(STBBR_LOGINFO, "VERIFY LAST FAIL: %s", stanza_text); + // Debug: show what was actually received + stanzas_debug_last(); } return result; diff --git a/stabber.h b/stabber.h index daa2ad0..5a20b74 100644 --- a/stabber.h +++ b/stabber.h @@ -38,6 +38,7 @@ void stbbr_set_timeout(int seconds); int stbbr_auth_passwd(char *password); int stbbr_for_id(char *id, char *stream); int stbbr_for_query(char *query, char *stream); +int stbbr_for_presence_to(char *to_jid, char *stream); void stbbr_wait_for(char *id);