Skip to content

Commit a98deac

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 c8bb33f commit a98deac

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

@@ -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: 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"
@@ -202,7 +201,28 @@ 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)(struct conn_interface *intf, 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+
219+
int compression_init(xmpp_conn_t *conn);
220+
void compression_free(xmpp_conn_t *conn);
221+
void compression_handle_feature_children(xmpp_conn_t *conn, const char *text);
222+
205223
struct _xmpp_conn_t {
224+
struct conn_interface intf;
225+
206226
unsigned int ref;
207227
xmpp_ctx_t *ctx;
208228
xmpp_conn_type_t type;
@@ -250,12 +270,10 @@ struct _xmpp_conn_t {
250270
int sm_disable;
251271
xmpp_sm_state_t *sm_state;
252272

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;
273+
struct {
274+
struct xmpp_compression *state;
275+
int allowed, supported, dont_reset;
276+
} compression;
259277

260278
char *lang;
261279
char *domain;

0 commit comments

Comments
 (0)