Skip to content

Commit

Permalink
Re-factor into separate compression module
Browse files Browse the repository at this point in the history
Introduce a `conn_interface` to simplify the decision logic which API
we must call.

This also fixes some bugs of the previous commit.

Signed-off-by: Steffen Jaeckel <[email protected]>
  • Loading branch information
sjaeckel committed Jan 31, 2024
1 parent c7d410f commit 3f03de0
Show file tree
Hide file tree
Showing 19 changed files with 555 additions and 359 deletions.
6 changes: 6 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ if NEED_SNPRINTF
libstrophe_la_SOURCES += src/snprintf.c
endif

if DISABLE_COMPRESSION
libstrophe_la_SOURCES += src/compression_dummy.c
else
libstrophe_la_SOURCES += src/compression.c
endif

if DISABLE_TLS
libstrophe_la_SOURCES += src/tls_dummy.c
else
Expand Down
3 changes: 2 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fi
if test "x$enable_zlib" != xno; then
PKG_CHECK_MODULES([zlib], [zlib >= 1.2.0],
[
PC_REQUIRES="libzlib ${PC_REQUIRES}"
PC_REQUIRES="zlib ${PC_REQUIRES}"
ZLIB_CFLAGS=$zlib_CFLAGS
ZLIB_LIBS=$zlib_LIBS
AC_DEFINE([HAVE_ZLIB])
Expand All @@ -269,6 +269,7 @@ m4_ifdef([PKG_INSTALLDIR], [PKG_INSTALLDIR],
AC_SUBST([pkgconfigdir], [${with_pkgconfigdir}])])

AM_CONDITIONAL([PARSER_EXPAT], [test x$with_parser != xlibxml2])
AM_CONDITIONAL([DISABLE_COMPRESSION], [test x$enable_zlib = xno])
AM_CONDITIONAL([DISABLE_TLS], [test x$enable_tls = xno])
AM_CONDITIONAL([DISABLE_STATIC], [test x$enable_static = xno])
AM_CONDITIONAL([NEED_SNPRINTF], [test x$have_snprintf = xno])
Expand Down
23 changes: 13 additions & 10 deletions examples/bot.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ int version_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
return 1;
}

static int _quit_handler(xmpp_conn_t *conn, void *userdata)
{
(void)userdata;
xmpp_disconnect(conn);
return 0;
}

int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
{
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
xmpp_stanza_t *body, *reply;
const char *type;
char *intext, *replytext;
int quit = 0;

body = xmpp_stanza_get_child_by_name(stanza, "body");
if (body == NULL)
Expand All @@ -103,11 +109,11 @@ int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)

if (strcmp(intext, "quit") == 0) {
replytext = strdup("bye!");
quit = 1;
xmpp_timed_handler_add(conn, _quit_handler, 500, NULL);
} else if (strcmp(intext, "reconnect") == 0) {
replytext = strdup("alright, let's see what happens!");
reconnect = 1;
quit = 1;
xmpp_timed_handler_add(conn, _quit_handler, 500, NULL);
} else {
replytext = (char *)malloc(strlen(" to you too!") + strlen(intext) + 1);
strcpy(replytext, intext);
Expand All @@ -120,9 +126,6 @@ int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
xmpp_stanza_release(reply);
free(replytext);

if (quit)
xmpp_disconnect(conn);

return 1;
}

Expand Down Expand Up @@ -216,8 +219,8 @@ static void usage(int exit_code)
"Note: --disable-tls conflicts with --mandatory-tls or "
"--legacy-ssl\n"
" --zlib Enable compression via zlib.\n"
" --dont-flush When using zlib, don't flush after "
"compression.\n");
" --dont-reset When using zlib, don't do a full-flush "
"after compression.\n");

exit(exit_code);
}
Expand Down Expand Up @@ -249,8 +252,8 @@ int main(int argc, char **argv)
flags |= XMPP_CONN_FLAG_LEGACY_AUTH;
else if (strcmp(argv[i], "--zlib") == 0)
flags |= XMPP_CONN_FLAG_ENABLE_COMPRESSION;
else if (strcmp(argv[i], "--dont-flush") == 0)
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_FLUSH;
else if (strcmp(argv[i], "--dont-reset") == 0)
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_RESET;
else if ((strcmp(argv[i], "--jid") == 0) && (++i < argc))
jid = argv[i];
else if ((strcmp(argv[i], "--pass") == 0) && (++i < argc))
Expand Down
8 changes: 4 additions & 4 deletions examples/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ static void usage(int exit_code)
" --legacy-ssl Use old style SSL.\n"
" --legacy-auth Allow legacy authentication.\n"
" --zlib Enable compression via zlib.\n"
" --dont-flush When using zlib, don't flush after "
"compression.\n"
" --dont-reset When using zlib, don't do a full-flush "
"after compression.\n"
" --verbose Increase the verbosity level.\n"
" --tcp-keepalive Configure TCP keepalive.\n\n"
"Note: --disable-tls conflicts with --mandatory-tls or "
Expand Down Expand Up @@ -278,8 +278,8 @@ int main(int argc, char **argv)
flags |= XMPP_CONN_FLAG_LEGACY_AUTH;
else if (strcmp(argv[i], "--zlib") == 0)
flags |= XMPP_CONN_FLAG_ENABLE_COMPRESSION;
else if (strcmp(argv[i], "--dont-flush") == 0)
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_FLUSH;
else if (strcmp(argv[i], "--dont-reset") == 0)
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_RESET;
else if (strcmp(argv[i], "--verbose") == 0)
verbosity++;
else if (strcmp(argv[i], "--tcp-keepalive") == 0)
Expand Down
45 changes: 21 additions & 24 deletions src/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,10 @@ static void _handle_sasl_children(xmpp_conn_t *conn, const char *text)
}
}

static void _handle_compression_children(xmpp_conn_t *conn, const char *text)
{
if (strcasecmp(text, "zlib") == 0) {
conn->compression_supported = 1;
}
}

static int
_handle_features(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
{
xmpp_stanza_t *child, *children;
const char *ns;
char *text;
xmpp_stanza_t *child;

UNUSED(userdata);

Expand Down Expand Up @@ -297,13 +288,6 @@ _handle_features(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
if (conn->sasl_support & ~(SASL_MASK_PLAIN | SASL_MASK_ANONYMOUS))
conn->sasl_support &= ~SASL_MASK_PLAIN;

/* check for compression */
child = xmpp_stanza_get_child_by_name_and_ns(stanza, "compression",
XMPP_NS_COMPRESSION);
if (conn->compression_allowed && child) {
_foreach_child(conn, child, "method", _handle_compression_children);
}

_auth(conn);

return 0;
Expand Down Expand Up @@ -371,7 +355,7 @@ _handle_sasl_result(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
(char *)userdata);

/* reset parser */
conn_prepare_reset(conn, conn->compression_allowed
conn_prepare_reset(conn, conn->compression.allowed
? _handle_open_compress
: _handle_open_sasl);

Expand Down Expand Up @@ -1058,6 +1042,8 @@ static int _handle_compress_result(xmpp_conn_t *const conn,
{
const char *name = xmpp_stanza_get_name(stanza);

UNUSED(userdata);

if (!name)
return 0;
if (strcmp(name, "compressed") == 0) {
Expand All @@ -1068,7 +1054,7 @@ static int _handle_compress_result(xmpp_conn_t *const conn,
conn_prepare_reset(conn, _handle_open_sasl);

/* make compression effective */
conn->compress = 1;
compression_init(conn);

/* send stream tag */
conn_open_stream(conn);
Expand All @@ -1082,15 +1068,26 @@ static int _handle_features_compress(xmpp_conn_t *conn,
{
const char *compress = "<compress xmlns='" XMPP_NS_COMPRESSION
"'><method>zlib</method></compress>";

UNUSED(userdata);
xmpp_stanza_t *child;

/* remove missing features handler */
xmpp_timed_handler_delete(conn, _handle_missing_features);

send_raw(conn, compress, strlen(compress), XMPP_QUEUE_STROPHE, NULL);
handler_add(conn, _handle_compress_result, XMPP_NS_COMPRESSION, NULL, NULL,
NULL);
/* check for compression */
child = xmpp_stanza_get_child_by_name_and_ns(stanza, "compression",
XMPP_NS_FEATURE_COMPRESSION);
if (conn->compression.allowed && child) {
_foreach_child(conn, child, "method",
compression_handle_feature_children);
}

if (conn->compression.supported) {
send_raw(conn, compress, strlen(compress), XMPP_QUEUE_STROPHE, NULL);
handler_add(conn, _handle_compress_result, XMPP_NS_COMPRESSION, NULL,
NULL, NULL);
} else {
return _handle_features_sasl(conn, stanza, userdata);
}

return 0;
}
Expand Down
32 changes: 25 additions & 7 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include <stdio.h>
#include <stdarg.h>
#include <zlib.h>

#include "strophe.h"
#include "ostypes.h"
Expand Down Expand Up @@ -208,7 +207,28 @@ struct _xmpp_sm_t {
xmpp_stanza_t *bind;
};

struct conn_interface {
int (*read)(struct conn_interface *intf, void *buff, size_t len);
int (*write)(struct conn_interface *intf, const void *buff, size_t len);
int (*flush)(struct conn_interface *intf);
int (*pending)(struct conn_interface *intf);
int (*get_error)(struct conn_interface *intf);
int (*error_is_recoverable)(struct conn_interface *intf, int err);
xmpp_conn_t *conn;
};

int conn_interface_write(struct conn_interface *intf,
const void *buff,
size_t len);
int conn_int_nop(struct conn_interface *intf);

int compression_init(xmpp_conn_t *conn);
void compression_free(xmpp_conn_t *conn);
void compression_handle_feature_children(xmpp_conn_t *conn, const char *text);

struct _xmpp_conn_t {
struct conn_interface intf;

unsigned int ref;
xmpp_ctx_t *ctx;
xmpp_conn_type_t type;
Expand Down Expand Up @@ -256,12 +276,10 @@ struct _xmpp_conn_t {
int sm_disable;
xmpp_sm_state_t *sm_state;

int compression_allowed, compression_supported;
int compress, compression_dont_flush;
struct zlib_compression {
void *buffer, *buffer_end;
z_stream stream;
} compression, decompression;
struct {
struct xmpp_compression *state;
int allowed, supported, dont_reset;
} compression;

char *lang;
char *domain;
Expand Down
Loading

0 comments on commit 3f03de0

Please sign in to comment.