Skip to content

Commit 0d573b4

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 c7d410f commit 0d573b4

File tree

19 files changed

+556
-359
lines changed

19 files changed

+556
-359
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fi
252252
if test "x$enable_zlib" != xno; then
253253
PKG_CHECK_MODULES([zlib], [zlib >= 1.2.0],
254254
[
255-
PC_REQUIRES="libzlib ${PC_REQUIRES}"
255+
PC_REQUIRES="zlib ${PC_REQUIRES}"
256256
ZLIB_CFLAGS=$zlib_CFLAGS
257257
ZLIB_LIBS=$zlib_LIBS
258258
AC_DEFINE([HAVE_ZLIB])
@@ -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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,19 @@ 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;
8390
xmpp_stanza_t *body, *reply;
8491
const char *type;
8592
char *intext, *replytext;
86-
int quit = 0;
8793

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

104110
if (strcmp(intext, "quit") == 0) {
105111
replytext = strdup("bye!");
106-
quit = 1;
112+
xmpp_timed_handler_add(conn, _quit_handler, 500, NULL);
107113
} else if (strcmp(intext, "reconnect") == 0) {
108114
replytext = strdup("alright, let's see what happens!");
109115
reconnect = 1;
110-
quit = 1;
116+
xmpp_timed_handler_add(conn, _quit_handler, 500, NULL);
111117
} else {
112118
replytext = (char *)malloc(strlen(" to you too!") + strlen(intext) + 1);
113119
strcpy(replytext, intext);
@@ -120,9 +126,6 @@ int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
120126
xmpp_stanza_release(reply);
121127
free(replytext);
122128

123-
if (quit)
124-
xmpp_disconnect(conn);
125-
126129
return 1;
127130
}
128131

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

222225
exit(exit_code);
223226
}
@@ -249,8 +252,8 @@ int main(int argc, char **argv)
249252
flags |= XMPP_CONN_FLAG_LEGACY_AUTH;
250253
else if (strcmp(argv[i], "--zlib") == 0)
251254
flags |= XMPP_CONN_FLAG_ENABLE_COMPRESSION;
252-
else if (strcmp(argv[i], "--dont-flush") == 0)
253-
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_FLUSH;
255+
else if (strcmp(argv[i], "--dont-reset") == 0)
256+
flags |= XMPP_CONN_FLAG_COMPRESSION_DONT_RESET;
254257
else if ((strcmp(argv[i], "--jid") == 0) && (++i < argc))
255258
jid = argv[i];
256259
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

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

1045+
UNUSED(userdata);
1046+
10611047
if (!name)
10621048
return 0;
10631049
if (strcmp(name, "compressed") == 0) {
@@ -1068,7 +1054,7 @@ static int _handle_compress_result(xmpp_conn_t *const conn,
10681054
conn_prepare_reset(conn, _handle_open_sasl);
10691055

10701056
/* make compression effective */
1071-
conn->compress = 1;
1057+
compression_init(conn);
10721058

10731059
/* send stream tag */
10741060
conn_open_stream(conn);
@@ -1082,15 +1068,26 @@ static int _handle_features_compress(xmpp_conn_t *conn,
10821068
{
10831069
const char *compress = "<compress xmlns='" XMPP_NS_COMPRESSION
10841070
"'><method>zlib</method></compress>";
1085-
1086-
UNUSED(userdata);
1071+
xmpp_stanza_t *child;
10871072

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

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

10951092
return 0;
10961093
}

src/common.h

Lines changed: 25 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"
@@ -208,7 +207,28 @@ struct _xmpp_sm_t {
208207
xmpp_stanza_t *bind;
209208
};
210209

210+
struct conn_interface {
211+
int (*read)(struct conn_interface *intf, void *buff, size_t len);
212+
int (*write)(struct conn_interface *intf, const void *buff, size_t len);
213+
int (*flush)(struct conn_interface *intf);
214+
int (*pending)(struct conn_interface *intf);
215+
int (*get_error)(struct conn_interface *intf);
216+
int (*error_is_recoverable)(struct conn_interface *intf, int err);
217+
xmpp_conn_t *conn;
218+
};
219+
220+
int conn_interface_write(struct conn_interface *intf,
221+
const void *buff,
222+
size_t len);
223+
int conn_int_nop(struct conn_interface *intf);
224+
225+
int compression_init(xmpp_conn_t *conn);
226+
void compression_free(xmpp_conn_t *conn);
227+
void compression_handle_feature_children(xmpp_conn_t *conn, const char *text);
228+
211229
struct _xmpp_conn_t {
230+
struct conn_interface intf;
231+
212232
unsigned int ref;
213233
xmpp_ctx_t *ctx;
214234
xmpp_conn_type_t type;
@@ -256,12 +276,10 @@ struct _xmpp_conn_t {
256276
int sm_disable;
257277
xmpp_sm_state_t *sm_state;
258278

259-
int compression_allowed, compression_supported;
260-
int compress, compression_dont_flush;
261-
struct zlib_compression {
262-
void *buffer, *buffer_end;
263-
z_stream stream;
264-
} compression, decompression;
279+
struct {
280+
struct xmpp_compression *state;
281+
int allowed, supported, dont_reset;
282+
} compression;
265283

266284
char *lang;
267285
char *domain;

0 commit comments

Comments
 (0)