Skip to content

Commit 9f592a2

Browse files
committed
Re-factor into separate compression module
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]>
1 parent 6e80fcd commit 9f592a2

File tree

19 files changed

+537
-340
lines changed

19 files changed

+537
-340
lines changed

Makefile.am

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ if NEED_SNPRINTF
8585
libstrophe_la_SOURCES += src/snprintf.c
8686
endif
8787

88+
if DISABLE_COMPRESSION
89+
libstrophe_la_SOURCES += src/compression_dummy.c
90+
else
91+
libstrophe_la_SOURCES += src/compression.c
92+
endif
93+
8894
if DISABLE_TLS
8995
libstrophe_la_SOURCES += src/tls_dummy.c
9096
else

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ m4_ifdef([PKG_INSTALLDIR], [PKG_INSTALLDIR],
269269
AC_SUBST([pkgconfigdir], [${with_pkgconfigdir}])])
270270

271271
AM_CONDITIONAL([PARSER_EXPAT], [test x$with_parser != xlibxml2])
272+
AM_CONDITIONAL([DISABLE_COMPRESSION], [test x$enable_zlib = xno])
272273
AM_CONDITIONAL([DISABLE_TLS], [test x$enable_tls = xno])
273274
AM_CONDITIONAL([DISABLE_STATIC], [test x$enable_static = xno])
274275
AM_CONDITIONAL([NEED_SNPRINTF], [test x$have_snprintf = xno])

examples/bot.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ int version_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
7777
return 1;
7878
}
7979

80+
static int _quit_handler(xmpp_conn_t *conn, void *userdata)
81+
{
82+
(void)userdata;
83+
xmpp_disconnect(conn);
84+
return 0;
85+
}
86+
8087
int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
8188
{
8289
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
@@ -103,7 +110,7 @@ int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
103110

104111
if (strcmp(intext, "quit") == 0) {
105112
replytext = strdup("bye!");
106-
quit = 1;
113+
xmpp_timed_handler_add(conn, _quit_handler, 500, NULL);
107114
} else if (strcmp(intext, "reconnect") == 0) {
108115
replytext = strdup("alright, let's see what happens!");
109116
reconnect = 1;
@@ -216,8 +223,8 @@ static void usage(int exit_code)
216223
"Note: --disable-tls conflicts with --mandatory-tls or "
217224
"--legacy-ssl\n"
218225
" --zlib Enable compression via zlib.\n"
219-
" --dont-flush When using zlib, don't flush after "
220-
"compression.\n");
226+
" --dont-reset When using zlib, don't do a full-flush "
227+
"after compression.\n");
221228

222229
exit(exit_code);
223230
}
@@ -249,8 +256,8 @@ int main(int argc, char **argv)
249256
flags |= XMPP_CONN_FLAG_LEGACY_AUTH;
250257
else if (strcmp(argv[i], "--zlib") == 0)
251258
flags |= XMPP_CONN_FLAG_ENABLE_COMPRESSION;
252-
else if (strcmp(argv[i], "--dont-flush") == 0)
253-
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_FLUSH;
259+
else if (strcmp(argv[i], "--dont-reset") == 0)
260+
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_RESET;
254261
else if ((strcmp(argv[i], "--jid") == 0) && (++i < argc))
255262
jid = argv[i];
256263
else if ((strcmp(argv[i], "--pass") == 0) && (++i < argc))

examples/complex.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ static void usage(int exit_code)
240240
" --legacy-ssl Use old style SSL.\n"
241241
" --legacy-auth Allow legacy authentication.\n"
242242
" --zlib Enable compression via zlib.\n"
243-
" --dont-flush When using zlib, don't flush after "
244-
"compression.\n"
243+
" --dont-reset When using zlib, don't do a full-flush "
244+
"after compression.\n"
245245
" --verbose Increase the verbosity level.\n"
246246
" --tcp-keepalive Configure TCP keepalive.\n\n"
247247
"Note: --disable-tls conflicts with --mandatory-tls or "
@@ -278,8 +278,8 @@ int main(int argc, char **argv)
278278
flags |= XMPP_CONN_FLAG_LEGACY_AUTH;
279279
else if (strcmp(argv[i], "--zlib") == 0)
280280
flags |= XMPP_CONN_FLAG_ENABLE_COMPRESSION;
281-
else if (strcmp(argv[i], "--dont-flush") == 0)
282-
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_FLUSH;
281+
else if (strcmp(argv[i], "--dont-reset") == 0)
282+
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_RESET;
283283
else if (strcmp(argv[i], "--verbose") == 0)
284284
verbosity++;
285285
else if (strcmp(argv[i], "--tcp-keepalive") == 0)

src/auth.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,10 @@ static void _handle_sasl_children(xmpp_conn_t *conn, const char *text)
255255
}
256256
}
257257

258-
static void _handle_compression_children(xmpp_conn_t *conn, const char *text)
259-
{
260-
if (strcasecmp(text, "zlib") == 0) {
261-
conn->compression_supported = 1;
262-
}
263-
}
264-
265258
static int
266259
_handle_features(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
267260
{
268-
xmpp_stanza_t *child, *children;
269-
const char *ns;
270-
char *text;
261+
xmpp_stanza_t *child;
271262

272263
UNUSED(userdata);
273264

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

300-
/* check for compression */
301-
child = xmpp_stanza_get_child_by_name_and_ns(stanza, "compression",
302-
XMPP_NS_COMPRESSION);
303-
if (conn->compression_allowed && child) {
304-
_foreach_child(conn, child, "method", _handle_compression_children);
305-
}
306-
307291
_auth(conn);
308292

309293
return 0;
@@ -371,7 +355,7 @@ _handle_sasl_result(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
371355
(char *)userdata);
372356

373357
/* reset parser */
374-
conn_prepare_reset(conn, conn->compression_allowed
358+
conn_prepare_reset(conn, conn->compression.allowed
375359
? _handle_open_compress
376360
: _handle_open_sasl);
377361

@@ -1057,6 +1041,8 @@ static int _handle_compress_result(xmpp_conn_t *const conn,
10571041
{
10581042
const char *name = xmpp_stanza_get_name(stanza);
10591043

1044+
UNUSED(userdata);
1045+
10601046
if (!name)
10611047
return 0;
10621048
if (strcmp(name, "compressed") == 0) {
@@ -1067,7 +1053,7 @@ static int _handle_compress_result(xmpp_conn_t *const conn,
10671053
conn_prepare_reset(conn, _handle_open_sasl);
10681054

10691055
/* make compression effective */
1070-
conn->compress = 1;
1056+
compression_init(conn);
10711057

10721058
/* send stream tag */
10731059
conn_open_stream(conn);
@@ -1081,15 +1067,26 @@ static int _handle_features_compress(xmpp_conn_t *conn,
10811067
{
10821068
const char *compress = "<compress xmlns='" XMPP_NS_COMPRESSION
10831069
"'><method>zlib</method></compress>";
1084-
1085-
UNUSED(userdata);
1070+
xmpp_stanza_t *child;
10861071

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

1090-
send_raw(conn, compress, strlen(compress), XMPP_QUEUE_STROPHE, NULL);
1091-
handler_add(conn, _handle_compress_result, XMPP_NS_COMPRESSION, NULL, NULL,
1092-
NULL);
1075+
/* check for compression */
1076+
child = xmpp_stanza_get_child_by_name_and_ns(stanza, "compression",
1077+
XMPP_NS_FEATURE_COMPRESSION);
1078+
if (conn->compression.allowed && child) {
1079+
_foreach_child(conn, child, "method",
1080+
compression_handle_feature_children);
1081+
}
1082+
1083+
if (conn->compression.supported) {
1084+
send_raw(conn, compress, strlen(compress), XMPP_QUEUE_STROPHE, NULL);
1085+
handler_add(conn, _handle_compress_result, XMPP_NS_COMPRESSION, NULL,
1086+
NULL, NULL);
1087+
} else {
1088+
return _handle_features_sasl(conn, stanza, userdata);
1089+
}
10931090

10941091
return 0;
10951092
}

src/common.h

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include <stdio.h>
2121
#include <stdarg.h>
22-
#include <zlib.h>
2322

2423
#include "strophe.h"
2524
#include "ostypes.h"
@@ -202,7 +201,29 @@ struct _xmpp_sm_t {
202201
xmpp_stanza_t *bind;
203202
};
204203

204+
struct conn_interface {
205+
int (*read)(struct conn_interface *intf, void *buff, size_t len);
206+
int (*write)(struct conn_interface *intf, const void *buff, size_t len);
207+
int (*flush)(struct conn_interface *intf);
208+
int (*pending)(struct conn_interface *intf);
209+
int (*get_error)(struct conn_interface *intf);
210+
int (*error_is_recoverable)(int err);
211+
xmpp_conn_t *conn;
212+
};
213+
214+
int conn_interface_write(struct conn_interface *intf,
215+
const void *buff,
216+
size_t len);
217+
int conn_int_nop(struct conn_interface *intf);
218+
int conn_recoverable_nop(int err);
219+
220+
int compression_init(xmpp_conn_t *conn);
221+
void compression_free(xmpp_conn_t *conn);
222+
void compression_handle_feature_children(xmpp_conn_t *conn, const char *text);
223+
205224
struct _xmpp_conn_t {
225+
struct conn_interface intf;
226+
206227
unsigned int ref;
207228
xmpp_ctx_t *ctx;
208229
xmpp_conn_type_t type;
@@ -250,12 +271,10 @@ struct _xmpp_conn_t {
250271
int sm_disable;
251272
xmpp_sm_state_t *sm_state;
252273

253-
int compression_allowed, compression_supported;
254-
int compress, compression_dont_flush;
255-
struct zlib_compression {
256-
void *buffer, *buffer_end;
257-
z_stream stream;
258-
} compression, decompression;
274+
struct {
275+
struct xmpp_compression *state;
276+
int allowed, supported, dont_reset;
277+
} compression;
259278

260279
char *lang;
261280
char *domain;

0 commit comments

Comments
 (0)