Compare commits
5 Commits
generic-se
...
0.14.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c2e78937d | ||
|
|
b7bae1502f | ||
|
|
e3d734c5ff | ||
|
|
e7b08faaa7 | ||
|
|
f3fa9f557b |
11
.github/workflows/main.yml
vendored
11
.github/workflows/main.yml
vendored
@@ -14,8 +14,8 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
valgrind:
|
||||
- { configure: '' , make: 'check' }
|
||||
- { configure: '--enable-valgrind' , make: 'check-valgrind' }
|
||||
- { configure: '' , cflags: '', make: 'check' }
|
||||
- { configure: '--enable-valgrind' , cflags: '-O2', make: 'check-valgrind' }
|
||||
options:
|
||||
- { configure: '' }
|
||||
- { configure: '--without-libxml2' }
|
||||
@@ -35,7 +35,7 @@ jobs:
|
||||
- name: Build the library
|
||||
run: |
|
||||
./bootstrap.sh
|
||||
./configure ${{ matrix.options.configure }} ${{ matrix.valgrind.configure }} CFLAGS="-Werror -g3"
|
||||
./configure ${{ matrix.options.configure }} ${{ matrix.valgrind.configure }} CFLAGS="-Werror -g3 ${{ matrix.valgrind.cflags }}"
|
||||
make -j$(nproc)
|
||||
- name: Run tests
|
||||
run: |
|
||||
@@ -135,6 +135,11 @@ jobs:
|
||||
if: ${{ !failure() }}
|
||||
run: |
|
||||
cat testbuild.log
|
||||
- name: Error logs
|
||||
if: ${{ failure() }}
|
||||
run: |
|
||||
cat testbuild.log || true
|
||||
cat testerr.log || true
|
||||
|
||||
code-style:
|
||||
runs-on: ubuntu-20.04
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -46,6 +46,7 @@ examples/roster
|
||||
examples/uuid
|
||||
examples/vcard
|
||||
testbuild*.log
|
||||
testerr*.log
|
||||
test-release/
|
||||
test_stamp
|
||||
test-suite*.log
|
||||
@@ -65,6 +66,7 @@ tests/test_resolver
|
||||
tests/test_sasl
|
||||
tests/test_scram
|
||||
tests/test_send_queue
|
||||
tests/test_serialize_sm
|
||||
tests/test_sha1
|
||||
tests/test_sha256
|
||||
tests/test_sha512
|
||||
|
||||
10
ChangeLog
10
ChangeLog
@@ -1,3 +1,13 @@
|
||||
0.14.0
|
||||
- Add SM-state serialization mechanism (#239)
|
||||
- Add support for `SCRAM-SHA-512-PLUS` (da122981)
|
||||
- Add support for `SSLKEYLOGFILE` with OpenSSL (d82df127)
|
||||
- Fix some issues related to Stream Management (03d43132, 4359536a)
|
||||
- Fix a potential segfault (9fef4b7d)
|
||||
- New API:
|
||||
- xmpp_conn_set_sm_callback()
|
||||
- xmpp_conn_restore_sm_state()
|
||||
|
||||
0.13.1
|
||||
- Fix SCRAM-*-PLUS SASL mechanisms with OpenSSL and TLS < v1.3 (40f2452)
|
||||
- Only signal "stream negotiation success" once (1cf09b1)
|
||||
|
||||
2
Doxyfile
2
Doxyfile
@@ -38,7 +38,7 @@ PROJECT_NAME = Strophe
|
||||
# could be handy for archiving the generated documentation or if some version
|
||||
# control system is used.
|
||||
|
||||
PROJECT_NUMBER = 0.13
|
||||
PROJECT_NUMBER = 0.14
|
||||
|
||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
|
||||
# for a project that appears at the top of each page and should give viewer a
|
||||
|
||||
@@ -190,6 +190,7 @@ STATIC_TESTS = \
|
||||
tests/test_jid \
|
||||
tests/test_ctx \
|
||||
tests/test_send_queue \
|
||||
tests/test_serialize_sm \
|
||||
tests/test_string \
|
||||
tests/test_resolver
|
||||
|
||||
@@ -285,6 +286,11 @@ tests_test_send_queue_CFLAGS = -I$(top_srcdir)/src
|
||||
tests_test_send_queue_LDADD = $(STROPHE_LIBS)
|
||||
tests_test_send_queue_LDFLAGS = -static
|
||||
|
||||
tests_test_serialize_sm_SOURCES = tests/test_serialize_sm.c tests/test.c tests/test.h
|
||||
tests_test_serialize_sm_CFLAGS = -I$(top_srcdir)/src
|
||||
tests_test_serialize_sm_LDADD = $(STROPHE_LIBS)
|
||||
tests_test_serialize_sm_LDFLAGS = -static
|
||||
|
||||
tests_test_snprintf_SOURCES = tests/test_snprintf.c
|
||||
tests_test_snprintf_CFLAGS = -I$(top_srcdir)/src
|
||||
|
||||
@@ -327,6 +333,7 @@ dist-archives:
|
||||
|
||||
test-release: dist
|
||||
@touch testbuild-$(PACKAGE_VERSION).log && ln -sf testbuild-$(PACKAGE_VERSION).log testbuild.log
|
||||
@touch testerr-$(PACKAGE_VERSION).log && ln -sf testerr-$(PACKAGE_VERSION).log testerr.log
|
||||
@mkdir -p test-release && cp $(PACKAGE_TARNAME)-$(PACKAGE_VERSION).tar.* test-release && pushd test-release && \
|
||||
tar xzf $(PACKAGE_TARNAME)-$(PACKAGE_VERSION).tar.gz && pushd $(PACKAGE_TARNAME)-$(PACKAGE_VERSION) && ./testbuild.sh && popd && rm -rf $(PACKAGE_TARNAME)-$(PACKAGE_VERSION) && \
|
||||
echo "Success" && popd
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
m4_define([v_maj], [0])
|
||||
m4_define([v_min], [13])
|
||||
m4_define([v_patch], [1])
|
||||
m4_define([v_min], [14])
|
||||
m4_define([v_patch], [0])
|
||||
m4_define([project_version], [v_maj.v_min.v_patch])
|
||||
|
||||
m4_define([lt_cur], m4_eval(v_maj + v_min))
|
||||
|
||||
@@ -1232,6 +1232,7 @@ static void _sm_enable(xmpp_conn_t *conn)
|
||||
send_stanza(conn, enable, XMPP_QUEUE_SM_STROPHE);
|
||||
conn->sm_state->sm_sent_nr = 0;
|
||||
conn->sm_state->sm_enabled = 1;
|
||||
trigger_sm_callback(conn);
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -1486,6 +1487,8 @@ static int _handle_sm(xmpp_conn_t *const conn,
|
||||
name = NULL;
|
||||
}
|
||||
|
||||
trigger_sm_callback(conn);
|
||||
|
||||
err_sm:
|
||||
if (!name) {
|
||||
char *err = "Couldn't convert stanza to text!";
|
||||
|
||||
@@ -323,6 +323,8 @@ struct _xmpp_conn_t {
|
||||
hash_t *id_handlers;
|
||||
xmpp_handlist_t *handlers;
|
||||
xmpp_sockopt_callback sockopt_cb;
|
||||
xmpp_sm_callback sm_callback;
|
||||
void *sm_callback_ctx;
|
||||
};
|
||||
|
||||
void conn_disconnect(xmpp_conn_t *conn);
|
||||
@@ -376,6 +378,7 @@ void handler_add(xmpp_conn_t *conn,
|
||||
void handler_system_delete_all(xmpp_conn_t *conn);
|
||||
|
||||
/* utility functions */
|
||||
void trigger_sm_callback(xmpp_conn_t *conn);
|
||||
void reset_sm_state(xmpp_sm_state_t *sm_state);
|
||||
void disconnect_mem_error(xmpp_conn_t *conn);
|
||||
|
||||
|
||||
374
src/conn.c
374
src/conn.c
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
@@ -1254,36 +1255,336 @@ int xmpp_conn_is_disconnected(xmpp_conn_t *conn)
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the Stream Management state of a connection object after
|
||||
* it has been disconnected.
|
||||
* One can then initialise a fresh connection object and set this Stream
|
||||
* Management state by calling \ref xmpp_conn_set_sm_state
|
||||
* This sets the Stream Management callback function
|
||||
*
|
||||
* In case one wants to dispose of the state w/o setting it into a fresh
|
||||
* connection object, one can call \ref xmpp_free_sm_state
|
||||
* After setting this API, the library will call the given callback function
|
||||
* each time when the internal SM state is updated.
|
||||
*
|
||||
* After calling this function to retrieve the state, only call one of the
|
||||
* other two.
|
||||
* This can be used in conjunction with \ref xmpp_conn_restore_sm_state to
|
||||
* e.g. implement a mechanism that retains an SM state over potential
|
||||
* application terminations.
|
||||
*
|
||||
* @param conn a Strophe connection object
|
||||
* @return The Stream Management state of the connection or NULL on error
|
||||
* @param cb a callback function or NULL to disable
|
||||
* @param ctx a context that will be passed on invocation of the callback
|
||||
* function
|
||||
*
|
||||
* @ingroup Connections
|
||||
*/
|
||||
xmpp_sm_state_t *xmpp_conn_get_sm_state(xmpp_conn_t *conn)
|
||||
void xmpp_conn_set_sm_callback(xmpp_conn_t *conn,
|
||||
xmpp_sm_callback cb,
|
||||
void *ctx)
|
||||
{
|
||||
xmpp_sm_state_t *ret;
|
||||
conn->sm_callback = cb;
|
||||
conn->sm_callback_ctx = ctx;
|
||||
}
|
||||
|
||||
/* We can only return the SM state when we're disconnected */
|
||||
if (conn->state != XMPP_STATE_DISCONNECTED)
|
||||
return NULL;
|
||||
struct sm_restore {
|
||||
xmpp_conn_t *conn;
|
||||
const unsigned char *state;
|
||||
const unsigned char *const state_end, *const orig;
|
||||
};
|
||||
|
||||
ret = conn->sm_state;
|
||||
conn->sm_state = NULL;
|
||||
static int sm_load_u32(struct sm_restore *sm, uint8_t type, uint32_t *val)
|
||||
{
|
||||
if (*sm->state != type) {
|
||||
strophe_error(
|
||||
sm->conn->ctx, "conn",
|
||||
"Invalid CBOR type at position %u: 0x%02x, expected: 0x%02x",
|
||||
sm->state - sm->orig, *sm->state, type);
|
||||
return XMPP_EINVOP;
|
||||
}
|
||||
sm->state++;
|
||||
if ((sm->state + 4) > sm->state_end) {
|
||||
strophe_error(sm->conn->ctx, "conn",
|
||||
"Provided sm_state data is too short");
|
||||
return XMPP_EINVOP;
|
||||
}
|
||||
uint32_t v;
|
||||
memcpy(&v, sm->state, 4);
|
||||
sm->state += 4;
|
||||
*val = ntohl(v);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sm_load_string(struct sm_restore *sm, char **val, size_t *len)
|
||||
{
|
||||
uint32_t l;
|
||||
int ret = sm_load_u32(sm, 0x7a, &l);
|
||||
if (ret)
|
||||
return ret;
|
||||
if ((sm->state + l) > sm->state_end) {
|
||||
strophe_error(sm->conn->ctx, "conn",
|
||||
"Provided sm_state data is too short");
|
||||
return XMPP_EINVOP;
|
||||
}
|
||||
*val = strophe_alloc(sm->conn->ctx, l + 1);
|
||||
if (!*val)
|
||||
return XMPP_EMEM;
|
||||
memcpy(*val, sm->state, l);
|
||||
(*val)[l] = '\0';
|
||||
sm->state += l;
|
||||
*len = l;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* This restores the serialized Stream Management state
|
||||
*
|
||||
* After setting this API, the library will call the given callback function
|
||||
* each time when the internal SM state is updated.
|
||||
*
|
||||
* This can be used in conjunction with \ref xmpp_conn_restore_sm_state to
|
||||
* e.g. implement a mechanism that retains an SM state over potential
|
||||
* application terminations.
|
||||
*
|
||||
* @param conn a Strophe connection object
|
||||
* @param sm_state a buffer as passed to the SM callback
|
||||
* @param sm_state_len the length of `sm_state`
|
||||
*
|
||||
* @ingroup Connections
|
||||
*/
|
||||
int xmpp_conn_restore_sm_state(xmpp_conn_t *conn,
|
||||
const unsigned char *sm_state,
|
||||
size_t sm_state_len)
|
||||
{
|
||||
/* We can only set the SM state when we're disconnected */
|
||||
if (conn->state != XMPP_STATE_DISCONNECTED) {
|
||||
strophe_error(conn->ctx, "conn",
|
||||
"SM state can only be set the when we're disconnected");
|
||||
return XMPP_EINVOP;
|
||||
}
|
||||
|
||||
if (conn->sm_state) {
|
||||
strophe_error(conn->ctx, "conn", "SM state is already set!");
|
||||
return XMPP_EINVOP;
|
||||
}
|
||||
|
||||
if (sm_state_len < 5 * 6) {
|
||||
strophe_error(conn->ctx, "conn", "Provided sm_state data is too short");
|
||||
return XMPP_EINVOP;
|
||||
}
|
||||
struct sm_restore sm = {.conn = conn,
|
||||
.state = sm_state,
|
||||
.state_end = sm_state + sm_state_len,
|
||||
.orig = sm_state};
|
||||
/* Check for pointer wrap-around, which should never happen */
|
||||
if (sm.state_end < sm.state) {
|
||||
strophe_error(conn->ctx, "conn",
|
||||
"Internal error, pointer wrapped around");
|
||||
return XMPP_EINVOP;
|
||||
}
|
||||
|
||||
if (memcmp(sm.state, "\x1a\x00\x00\x00\x00", 5) != 0) {
|
||||
strophe_error(conn->ctx, "conn", "Unknown sm_state version");
|
||||
return XMPP_EINVOP;
|
||||
}
|
||||
sm.state += 5;
|
||||
|
||||
conn->sm_state = strophe_alloc(conn->ctx, sizeof(*conn->sm_state));
|
||||
if (!conn->sm_state)
|
||||
return XMPP_EMEM;
|
||||
|
||||
memset(conn->sm_state, 0, sizeof(*conn->sm_state));
|
||||
conn->sm_state->ctx = conn->ctx;
|
||||
|
||||
conn->sm_state->sm_support = 1;
|
||||
conn->sm_state->sm_enabled = 1;
|
||||
conn->sm_state->can_resume = 1;
|
||||
conn->sm_state->resume = 1;
|
||||
|
||||
int ret;
|
||||
ret = sm_load_u32(&sm, 0x1a, &conn->sm_state->sm_sent_nr);
|
||||
if (ret)
|
||||
goto err_reload;
|
||||
|
||||
ret = sm_load_u32(&sm, 0x1a, &conn->sm_state->sm_handled_nr);
|
||||
if (ret)
|
||||
goto err_reload;
|
||||
|
||||
size_t id_len;
|
||||
ret = sm_load_string(&sm, &conn->sm_state->id, &id_len);
|
||||
if (ret)
|
||||
goto err_reload;
|
||||
|
||||
uint32_t len, i;
|
||||
ret = sm_load_u32(&sm, 0x9a, &len);
|
||||
if (ret)
|
||||
goto err_reload;
|
||||
conn->send_queue_user_len = conn->send_queue_len = len;
|
||||
for (i = 0; i < len; i++) {
|
||||
xmpp_send_queue_t *item = strophe_alloc(conn->ctx, sizeof(*item));
|
||||
if (!item) {
|
||||
ret = XMPP_EMEM;
|
||||
goto err_reload;
|
||||
}
|
||||
memset(item, 0, sizeof(*item));
|
||||
|
||||
if (!conn->send_queue_tail) {
|
||||
conn->send_queue_head = item;
|
||||
conn->send_queue_tail = item;
|
||||
} else {
|
||||
conn->send_queue_tail->next = item;
|
||||
conn->send_queue_tail = item;
|
||||
}
|
||||
|
||||
ret = sm_load_string(&sm, &item->data, &item->len);
|
||||
if (ret)
|
||||
goto err_reload;
|
||||
|
||||
item->owner = XMPP_QUEUE_USER;
|
||||
}
|
||||
|
||||
ret = sm_load_u32(&sm, 0xba, &len);
|
||||
if (ret)
|
||||
goto err_reload;
|
||||
for (i = 0; i < len; i++) {
|
||||
xmpp_send_queue_t *item = strophe_alloc(conn->ctx, sizeof(*item));
|
||||
if (!item) {
|
||||
ret = XMPP_EMEM;
|
||||
goto err_reload;
|
||||
}
|
||||
memset(item, 0, sizeof(*item));
|
||||
|
||||
add_queue_back(&conn->sm_state->sm_queue, item);
|
||||
|
||||
ret = sm_load_u32(&sm, 0x1a, &item->sm_h);
|
||||
if (ret)
|
||||
goto err_reload;
|
||||
ret = sm_load_string(&sm, &item->data, &item->len);
|
||||
if (ret)
|
||||
goto err_reload;
|
||||
|
||||
item->owner = XMPP_QUEUE_USER;
|
||||
}
|
||||
|
||||
return XMPP_EOK;
|
||||
|
||||
err_reload:
|
||||
xmpp_free_sm_state(conn->sm_state);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int sm_store_u32(unsigned char **next_,
|
||||
const unsigned char *const end,
|
||||
uint8_t type,
|
||||
uint32_t val)
|
||||
{
|
||||
unsigned char *next = *next_;
|
||||
if (next + 5 > end)
|
||||
return 1;
|
||||
*next++ = type;
|
||||
uint32_t v = htonl(val);
|
||||
memcpy(next, &v, 4);
|
||||
next += 4;
|
||||
*next_ = next;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t sm_state_serialize(xmpp_conn_t *conn, unsigned char **buf)
|
||||
{
|
||||
if (!conn->sm_state->sm_support || !conn->sm_state->sm_enabled ||
|
||||
!conn->sm_state->can_resume) {
|
||||
*buf = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t id_len = strlen(conn->sm_state->id);
|
||||
xmpp_send_queue_t *peek = conn->sm_state->sm_queue.head;
|
||||
size_t sm_queue_len = 0;
|
||||
size_t sm_queue_size = 0;
|
||||
while (peek) {
|
||||
sm_queue_len++;
|
||||
sm_queue_size += 10 + peek->len;
|
||||
peek = peek->next;
|
||||
}
|
||||
|
||||
uint32_t send_queue_len = 0;
|
||||
size_t send_queue_size = 0;
|
||||
peek = conn->send_queue_head;
|
||||
while (peek) {
|
||||
send_queue_len++;
|
||||
send_queue_size += 5 + peek->len;
|
||||
peek = peek->next;
|
||||
}
|
||||
|
||||
size_t buf_size =
|
||||
5 + 5 + 5 + 5 + id_len + 5 + send_queue_size + 5 + sm_queue_size;
|
||||
*buf = strophe_alloc(conn->ctx, buf_size);
|
||||
if (*buf == NULL)
|
||||
return 0;
|
||||
unsigned char *next = *buf;
|
||||
const unsigned char *const end = next + buf_size;
|
||||
/* Check for pointer wrap-around, which should never happen */
|
||||
if (end < next) {
|
||||
strophe_error(conn->ctx, "conn",
|
||||
"Internal error, pointer wrapped around");
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(next, "\x1a\x00\x00\x00\x00", 5); // Version
|
||||
next += 5;
|
||||
|
||||
if (sm_store_u32(&next, end, 0x1a, conn->sm_state->sm_sent_nr))
|
||||
goto err_serialize;
|
||||
if (sm_store_u32(&next, end, 0x1a, conn->sm_state->sm_handled_nr))
|
||||
goto err_serialize;
|
||||
|
||||
if (sm_store_u32(&next, end, 0x7a, id_len))
|
||||
goto err_serialize;
|
||||
memcpy(next, conn->sm_state->id, id_len);
|
||||
next += id_len;
|
||||
|
||||
if (sm_store_u32(&next, end, 0x9a, send_queue_len))
|
||||
goto err_serialize;
|
||||
|
||||
peek = conn->send_queue_head;
|
||||
while (peek) {
|
||||
if (sm_store_u32(&next, end, 0x7a, (uint32_t)peek->len))
|
||||
goto err_serialize;
|
||||
if (next + peek->len > end)
|
||||
goto err_serialize;
|
||||
memcpy(next, peek->data, peek->len);
|
||||
next += peek->len;
|
||||
peek = peek->next;
|
||||
}
|
||||
|
||||
if (sm_store_u32(&next, end, 0xba, sm_queue_len))
|
||||
goto err_serialize;
|
||||
|
||||
peek = conn->sm_state->sm_queue.head;
|
||||
while (peek) {
|
||||
if (sm_store_u32(&next, end, 0x1a, peek->sm_h))
|
||||
goto err_serialize;
|
||||
|
||||
if (sm_store_u32(&next, end, 0x7a, (uint32_t)peek->len))
|
||||
goto err_serialize;
|
||||
if (next + peek->len > end)
|
||||
goto err_serialize;
|
||||
memcpy(next, peek->data, peek->len);
|
||||
next += peek->len;
|
||||
peek = peek->next;
|
||||
}
|
||||
|
||||
return buf_size;
|
||||
|
||||
err_serialize:
|
||||
strophe_error(conn->ctx, "conn", "Can't serialize more data, buffer full");
|
||||
strophe_free(conn->ctx, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void trigger_sm_callback(xmpp_conn_t *conn)
|
||||
{
|
||||
if (!conn || !conn->sm_callback)
|
||||
return;
|
||||
|
||||
unsigned char *buf;
|
||||
size_t size = sm_state_serialize(conn, &buf);
|
||||
conn->sm_callback(conn, conn->sm_callback_ctx, buf, size);
|
||||
strophe_free(conn->ctx, buf);
|
||||
}
|
||||
|
||||
static void _reset_sm_state_for_reconnect(xmpp_conn_t *conn)
|
||||
{
|
||||
xmpp_sm_state_t *s = conn->sm_state;
|
||||
@@ -1312,6 +1613,37 @@ static void _reset_sm_state_for_reconnect(xmpp_conn_t *conn)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the Stream Management state of a connection object after
|
||||
* it has been disconnected.
|
||||
* One can then initialise a fresh connection object and set this Stream
|
||||
* Management state by calling \ref xmpp_conn_set_sm_state
|
||||
*
|
||||
* In case one wants to dispose of the state w/o setting it into a fresh
|
||||
* connection object, one can call \ref xmpp_free_sm_state
|
||||
*
|
||||
* After calling this function to retrieve the state, only call one of the
|
||||
* other two.
|
||||
*
|
||||
* @param conn a Strophe connection object
|
||||
* @return The Stream Management state of the connection or NULL on error
|
||||
*
|
||||
* @ingroup Connections
|
||||
*/
|
||||
xmpp_sm_state_t *xmpp_conn_get_sm_state(xmpp_conn_t *conn)
|
||||
{
|
||||
xmpp_sm_state_t *ret;
|
||||
|
||||
/* We can only return the SM state when we're disconnected */
|
||||
if (conn->state != XMPP_STATE_DISCONNECTED)
|
||||
return NULL;
|
||||
|
||||
ret = conn->sm_state;
|
||||
conn->sm_state = NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param conn a Strophe connection object
|
||||
* @param sm_state A Stream Management state returned from a call to
|
||||
@@ -1490,7 +1822,9 @@ char *xmpp_conn_send_queue_drop_element(xmpp_conn_t *conn,
|
||||
conn->sm_state->r_sent = 0;
|
||||
}
|
||||
/* Finally drop the element */
|
||||
return _drop_send_queue_element(conn, t);
|
||||
char *r = _drop_send_queue_element(conn, t);
|
||||
trigger_sm_callback(conn);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* timed handler for cleanup if normal disconnect procedure takes too long */
|
||||
@@ -1684,6 +2018,7 @@ static void _handle_stream_end(char *name, void *userdata)
|
||||
strophe_debug(conn->ctx, "xmpp", "RECV: </stream:stream>");
|
||||
/* the session has been terminated properly, i.e. it can't be resumed */
|
||||
conn->sm_state->can_resume = 0;
|
||||
trigger_sm_callback(conn);
|
||||
conn_disconnect_clean(conn);
|
||||
}
|
||||
|
||||
@@ -1759,6 +2094,7 @@ static void _conn_sm_handle_stanza(xmpp_conn_t *const conn,
|
||||
conn->sm_state->r_sent = 0;
|
||||
}
|
||||
}
|
||||
trigger_sm_callback(conn);
|
||||
}
|
||||
|
||||
static unsigned short _conn_default_port(xmpp_conn_t *conn,
|
||||
@@ -2045,8 +2381,10 @@ static int _send_raw(xmpp_conn_t *conn,
|
||||
strophe_debug_verbose(1, conn->ctx, "conn", "Q_ADD: %p", item);
|
||||
if (!(owner & XMPP_QUEUE_SM) && conn->sm_state->sm_enabled &&
|
||||
!conn->sm_state->r_sent) {
|
||||
send_raw(conn, req_ack, strlen(req_ack), XMPP_QUEUE_SM_STROPHE, item);
|
||||
conn->sm_state->r_sent = 1;
|
||||
send_raw(conn, req_ack, strlen(req_ack), XMPP_QUEUE_SM_STROPHE, item);
|
||||
} else {
|
||||
trigger_sm_callback(conn);
|
||||
}
|
||||
return XMPP_EOK;
|
||||
}
|
||||
|
||||
@@ -174,6 +174,7 @@ void xmpp_run_once(xmpp_ctx_t *ctx, unsigned long timeout)
|
||||
/* if we've sent everything update the tail */
|
||||
if (!sq)
|
||||
conn->send_queue_tail = NULL;
|
||||
trigger_sm_callback(conn);
|
||||
}
|
||||
intf->flush(intf);
|
||||
|
||||
|
||||
33
strophe.h
33
strophe.h
@@ -18,6 +18,7 @@
|
||||
#define __LIBSTROPHE_STROPHE_H__
|
||||
|
||||
#include <stddef.h> /* size_t */
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -414,8 +415,38 @@ typedef enum {
|
||||
char *xmpp_conn_send_queue_drop_element(xmpp_conn_t *conn,
|
||||
xmpp_queue_element_t which);
|
||||
|
||||
xmpp_sm_state_t *xmpp_conn_get_sm_state(xmpp_conn_t *conn);
|
||||
int xmpp_conn_set_sm_state(xmpp_conn_t *conn, xmpp_sm_state_t *sm_state);
|
||||
xmpp_sm_state_t *xmpp_conn_get_sm_state(xmpp_conn_t *conn);
|
||||
|
||||
/** The function which will be called when Strophe updates its internal SM
|
||||
* state.
|
||||
*
|
||||
* Please note that you have to create a copy of the buffer, since the library
|
||||
* will free the buffer right after return of the callback function.
|
||||
*
|
||||
*
|
||||
* @param conn The Strophe connection object this callback
|
||||
* originates from.
|
||||
* @param ctx The `ctx` pointer as passed to
|
||||
* \ref xmpp_conn_set_sm_callback
|
||||
* @param sm_state A pointer to a buffer containing the serialized SM
|
||||
* state.
|
||||
* @param sm_state_len The length of `sm_state`.
|
||||
*
|
||||
* @return 0 on success, -1 on error
|
||||
*
|
||||
* @ingroup Connections
|
||||
*/
|
||||
typedef void (*xmpp_sm_callback)(xmpp_conn_t *conn,
|
||||
void *ctx,
|
||||
const unsigned char *sm_state,
|
||||
size_t sm_state_len);
|
||||
void xmpp_conn_set_sm_callback(xmpp_conn_t *conn,
|
||||
xmpp_sm_callback cb,
|
||||
void *ctx);
|
||||
int xmpp_conn_restore_sm_state(xmpp_conn_t *conn,
|
||||
const unsigned char *sm_state,
|
||||
size_t sm_state_len);
|
||||
|
||||
void xmpp_free_sm_state(xmpp_sm_state_t *sm_state);
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
logfile="../../testbuild.log"
|
||||
errfile="../../testerr.log"
|
||||
|
||||
err_out() {
|
||||
tail $logfile
|
||||
@@ -8,6 +9,6 @@ err_out() {
|
||||
}
|
||||
|
||||
./bootstrap.sh
|
||||
./configure >> $logfile || err_out
|
||||
make -j$(( `nproc` * 2 + 1 )) >> $logfile || err_out
|
||||
make check >> $logfile || err_out
|
||||
./configure >> $logfile 2>> $errfile || err_out
|
||||
make -j$(( `nproc` * 2 + 1 )) >> $logfile 2>> $errfile || err_out
|
||||
make check >> $logfile 2>> $errfile || err_out
|
||||
|
||||
166
tests/test_serialize_sm.c
Normal file
166
tests/test_serialize_sm.c
Normal file
@@ -0,0 +1,166 @@
|
||||
/* SPDX-License-Identifier: MIT OR GPL-3.0-only */
|
||||
/* test_serialize_sm.c
|
||||
** libstrophe XMPP client library -- test routines for the send queue
|
||||
**
|
||||
** Copyright (C) 2024 Stephen Paul Weber
|
||||
**
|
||||
** This software is provided AS-IS with no warranty, either express
|
||||
** or implied.
|
||||
**
|
||||
** This program is dual licensed under the MIT or GPLv3 licenses.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "strophe.h"
|
||||
#include "common.h"
|
||||
|
||||
#include "test.h"
|
||||
|
||||
void callback(xmpp_conn_t *conn,
|
||||
void *ctx,
|
||||
const unsigned char *sm_state,
|
||||
size_t sm_state_len)
|
||||
{
|
||||
int *callback_count = ctx;
|
||||
(*callback_count)++;
|
||||
COMPARE_BUF("\x1a\x00\x00\x00\x00", 5, sm_state, 5);
|
||||
COMPARE_BUF("\x1a\x00\x00\x00\x00", 5, sm_state + 10, 5);
|
||||
COMPARE_BUF("\x7a\x00\x00\x00\x04SMID", 9, sm_state + 15, 9);
|
||||
if (*callback_count == 1) {
|
||||
COMPARE_BUF("\x1a\x00\x00\x00\x00", 5, sm_state + 5, 5);
|
||||
COMPARE_BUF("\x9a\x00\x00\x00\x02", 5, sm_state + 24, 5);
|
||||
COMPARE_BUF("\x7a\x00\x00\x00\x03"
|
||||
"foo",
|
||||
8, sm_state + 29, 8);
|
||||
COMPARE_BUF("\x7a\x00\x00\x00\x1a<r xmlns='urn:xmpp:sm:3'/>", 31,
|
||||
sm_state + 37, 31);
|
||||
COMPARE_BUF("\xba\x00\x00\x00\x00", 5, sm_state + 68, 5);
|
||||
ENSURE_EQ(sm_state_len, 73);
|
||||
|
||||
xmpp_conn_t *newconn = xmpp_conn_new(conn->ctx);
|
||||
xmpp_conn_restore_sm_state(newconn, sm_state, sm_state_len);
|
||||
ENSURE_EQ(newconn->sm_state->sm_sent_nr, 0);
|
||||
ENSURE_EQ(newconn->sm_state->sm_handled_nr, 0);
|
||||
COMPARE(newconn->sm_state->id, "SMID");
|
||||
ENSURE_EQ(newconn->send_queue_len, 2);
|
||||
ENSURE_EQ(newconn->send_queue_user_len, 2);
|
||||
ENSURE_EQ((size_t)(newconn->sm_state->sm_queue.head), 0);
|
||||
xmpp_conn_release(newconn);
|
||||
}
|
||||
if (*callback_count == 2) {
|
||||
COMPARE_BUF("\x1a\x00\x00\x00\x01", 5, sm_state + 5, 5);
|
||||
COMPARE_BUF("\x9a\x00\x00\x00\x01", 5, sm_state + 24, 5);
|
||||
COMPARE_BUF("\x7a\x00\x00\x00\x1a<r xmlns='urn:xmpp:sm:3'/>", 31,
|
||||
sm_state + 29, 31);
|
||||
COMPARE_BUF("\xba\x00\x00\x00\x01", 5, sm_state + 60, 5);
|
||||
COMPARE_BUF("\x1a\x00\x00\x00\x00", 5, sm_state + 65, 5);
|
||||
COMPARE_BUF("\x7a\x00\x00\x00\x03"
|
||||
"foo",
|
||||
8, sm_state + 70, 8);
|
||||
ENSURE_EQ(sm_state_len, 78);
|
||||
|
||||
xmpp_conn_t *newconn = xmpp_conn_new(conn->ctx);
|
||||
xmpp_conn_restore_sm_state(newconn, sm_state, sm_state_len);
|
||||
ENSURE_EQ(newconn->sm_state->sm_sent_nr, 1);
|
||||
ENSURE_EQ(newconn->sm_state->sm_handled_nr, 0);
|
||||
COMPARE(newconn->sm_state->id, "SMID");
|
||||
ENSURE_EQ(newconn->send_queue_len, 1);
|
||||
ENSURE_EQ(newconn->send_queue_user_len, 1);
|
||||
ENSURE_EQ(newconn->sm_state->sm_queue.head->sm_h, 0);
|
||||
xmpp_conn_release(newconn);
|
||||
}
|
||||
if (*callback_count == 3) {
|
||||
COMPARE_BUF("\x1a\x00\x00\x00\x01", 5, sm_state + 5, 5);
|
||||
COMPARE_BUF("\x9a\x00\x00\x00\x00", 5, sm_state + 24, 5);
|
||||
COMPARE_BUF("\xba\x00\x00\x00\x01", 5, sm_state + 29, 5);
|
||||
COMPARE_BUF("\x1a\x00\x00\x00\x00", 5, sm_state + 34, 5);
|
||||
COMPARE_BUF("\x7a\x00\x00\x00\x03"
|
||||
"foo",
|
||||
8, sm_state + 39, 8);
|
||||
ENSURE_EQ(sm_state_len, 47);
|
||||
|
||||
xmpp_conn_t *newconn = xmpp_conn_new(conn->ctx);
|
||||
xmpp_conn_restore_sm_state(newconn, sm_state, sm_state_len);
|
||||
ENSURE_EQ(newconn->sm_state->sm_sent_nr, 1);
|
||||
ENSURE_EQ(newconn->sm_state->sm_handled_nr, 0);
|
||||
COMPARE(newconn->sm_state->id, "SMID");
|
||||
ENSURE_EQ(newconn->send_queue_len, 0);
|
||||
ENSURE_EQ(newconn->send_queue_user_len, 0);
|
||||
ENSURE_EQ(newconn->sm_state->sm_queue.head->sm_h, 0);
|
||||
xmpp_conn_release(newconn);
|
||||
}
|
||||
}
|
||||
|
||||
int fake_read(struct conn_interface *intf, void *buff, size_t len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fake_write(struct conn_interface *intf, const void *buff, size_t len)
|
||||
{
|
||||
return len;
|
||||
}
|
||||
|
||||
int fake_flush(struct conn_interface *intf)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fake_error_is_recoverable(struct conn_interface *intf, int err)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
xmpp_ctx_t *ctx;
|
||||
xmpp_conn_t *conn;
|
||||
xmpp_log_t *log;
|
||||
xmpp_conn_state_t state;
|
||||
xmpp_sm_state_t *sm_state;
|
||||
char *ret;
|
||||
unsigned int n;
|
||||
int callback_count = 0;
|
||||
|
||||
xmpp_initialize();
|
||||
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
|
||||
ctx = xmpp_ctx_new(NULL, log);
|
||||
conn = xmpp_conn_new(ctx);
|
||||
sm_state = strophe_alloc(ctx, sizeof(*sm_state));
|
||||
memset(sm_state, 0, sizeof(*sm_state));
|
||||
sm_state->ctx = ctx;
|
||||
sm_state->sm_support = sm_state->sm_enabled = sm_state->can_resume = 1;
|
||||
sm_state->id = strophe_strdup(ctx, "SMID");
|
||||
|
||||
xmpp_conn_set_sm_state(conn, sm_state);
|
||||
xmpp_conn_set_sm_callback(conn, callback, &callback_count);
|
||||
|
||||
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 0);
|
||||
|
||||
struct conn_interface intf = {fake_read, fake_write,
|
||||
fake_flush, fake_flush,
|
||||
fake_flush, fake_error_is_recoverable,
|
||||
conn};
|
||||
conn->intf = intf;
|
||||
state = conn->state;
|
||||
conn->state = XMPP_STATE_CONNECTED;
|
||||
conn->sock = 123;
|
||||
|
||||
xmpp_send_raw(conn, "foo", 3);
|
||||
ENSURE_EQ(xmpp_conn_send_queue_len(conn), 1);
|
||||
ENSURE_EQ(callback_count, 1);
|
||||
|
||||
xmpp_run_once(ctx, 0);
|
||||
ENSURE_EQ(callback_count, 3);
|
||||
|
||||
conn->state = state;
|
||||
|
||||
xmpp_conn_release(conn);
|
||||
xmpp_ctx_free(ctx);
|
||||
xmpp_shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user