77 Commits

Author SHA1 Message Date
Dmitry Podgorny
d20957a98d WIP XEP-0198: implement stream management
Stream management is enabled when server supports it. There is no option
to disable it.
2020-01-25 23:45:42 +02:00
Dmitry Podgorny
8cfb2d2a03 XEP-0077: add examples/register
XEP-0077 will not be implemented as part of libstrophe's API, but an
example is provided. The example shows how to use the "raw" connection
to register a user.

Closes #54.
2020-01-25 22:59:19 +02:00
Michael Vetter
1b10e8f987 resolver: include headers for select(2)
C-ares implementation uses select(2) but doesn't include required
headers.

Fixes #149.
2020-01-25 22:06:07 +02:00
Dmitry Podgorny
178f566ae2 Unify include of strophe.h 2020-01-10 01:28:47 +02:00
Dmitry Podgorny
0326e5c5e2 handler: avoid unnamed union
Unnamed unions appeared only in C11 standard. Don't use them to
increase portability.
2020-01-10 01:22:53 +02:00
Dmitry Podgorny
212df6d036 tests: add test for stanza functions
Check reference counting in xmpp_stanza_add_child() amd
xmpp_stanza_add_child_ex().
2020-01-10 00:50:22 +02:00
Dmitry Podgorny
4b04db6b2d stanza: add xmpp_stanza_add_child_ex()
This function is introduced by NetXMS project.
2020-01-10 00:49:45 +02:00
Dmitry Podgorny
fb327d4bd1 parser/expat: provide memory handling suite for 1 ctx
Libstrophe doesn't override memory allocation functions in XML libraries
and default allocators are used as result.

Current patch solves this issue partially for expat module. The module
caches 1st strophe context and overrides expat's allocator for parsers
within this context. For all other contexts, fallback to default
allocator.

This is modified version of solution from UnrealEngine project.
2020-01-07 22:45:15 +02:00
Dmitry Podgorny
a811c868ff util: add time_stamp() implementation for XBOX
Backported from UnrealEngine project.
2020-01-07 22:24:10 +02:00
Dmitry Podgorny
3f05bafffb event: move buffer size to a macro
Move buffer size for incoming messages to a macro. So it can be changed
in one place.
2020-01-07 22:20:12 +02:00
Dmitry Podgorny
133abd096d conn: disconnect if user destroys an active connection object
Backported from UnrealEngine project.
2020-01-07 22:16:11 +02:00
Dmitry Podgorny
cecd6b20e8 stanza: add xmpp_stanza_get_context()
This function is backported from UnrealEngine project.
2020-01-07 22:13:39 +02:00
Dmitry Podgorny
2272681ea2 conn: add state checking API
New boolean functions:
  xmpp_conn_is_connecting()
  xmpp_conn_is_connected()
  xmpp_conn_is_disconnected()

These functions are backported from UnrealEngine project.
2020-01-07 22:12:11 +02:00
Dmitry Podgorny
17f79cbbb8 auth: replace stricmp() with _stricmp()
Newer visual studios deprecate stricmp(). Replace it to fix compilation.
2020-01-07 00:24:57 +02:00
Dmitry Podgorny
2570084cae event: don't redefine error codes
Error codes can be defined in visual studio. Unconditional redefine
causes warnings.
2020-01-07 00:14:59 +02:00
Dmitry Podgorny
b2e6458736 autotools: check for snprintf and vsnprintf
src/snprinf.[ch] rely on HAVE_SNPRINTF macro. The check is missed in
configure script and libstrophe always use internal implementation
of snprinf() which may be inefficient. Add check to configure.ac.
2020-01-05 01:14:22 +02:00
Dmitry Podgorny
c192a2b181 md5: remove unused parameter 2020-01-05 00:30:11 +02:00
Dmitry Podgorny
2e325e5e68 tests/sha1: fix type mismatch warning 2020-01-05 00:28:01 +02:00
Manuel Kasper
f0436490b0 Enable SNI with OpenSSL 0.9.8f as well 2019-12-24 09:07:51 +01:00
Manuel Kasper
243664926f Enable TLS SNI 2019-12-23 14:34:19 +01:00
Dmitry Podgorny
466fa2adbf sha1: don't clean local variable i on exit
In this place value of variable i is constant and predictable. No point
to clean it. Furthermore, since the value is not used after assignment,
compiler will optimize and remove the assignment.

This patch fixes a warning from the clang static analyzer.
2019-11-10 16:04:17 +02:00
Dmitry Podgorny
284e8f4421 tls/openssl: check return code
Check return code of SSL_CTX_set_default_verify_paths() and fail TLS on
an error. However, ignore the error when XMPP_CONN_FLAG_TRUST_TLS is
set.
2019-11-10 15:57:23 +02:00
Dmitry Podgorny
6852da97ae ChangeLog: xmpp_stanza_get_child_by_name_and_ns() 2019-11-30 20:39:17 +02:00
Dmitry Podgorny
35f37e8c23 autotools: link against libm with manually detected libxml2
Fixes build on QNX.
2019-11-30 20:36:34 +02:00
Michael Vetter
cbe6701c14 Add xmpp_stanza_get_child_by_name_and_ns()
I think it's fairly common in the XMPP world to actually want a stanza
by it's NS but also want a certain name.

For example this was needed in Profanity:
68af0aad65
2019-11-25 13:58:05 +00:00
Dmitry Podgorny
296df2fca9 tls/openssl: don't use deprecated function in 1.1.0+
SSLv23_client_method() was deprecated in OpenSSL 1.1.0. It is left as
macro to TLS_client_method.
2019-11-25 13:45:51 +00:00
Dmitry Podgorny
7caf88a6a8 rand: avoid VLA in xmpp_rand_nonce()
Don't use additional buffer. Instead, use half of the output buffer.
2019-11-19 01:46:35 +02:00
Dmitry Podgorny
ebaad72a35 ChangeLog: mention recent changes 2019-11-19 01:11:56 +02:00
François Revol
962bf4de5e Haiku: skip -Wextra -Wno-unused-parameter for GCC 2.95
This is needed to compile for the primary arch.
2019-11-17 15:34:27 +01:00
François Revol
884dace400 Haiku: link with libnetwork 2019-11-17 14:29:24 +01:00
François Revol
234bef4025 Haiku: C89 2019-11-17 14:28:26 +01:00
François Revol
45cbd51633 Haiku: fake va_copy for now 2019-11-17 14:21:37 +01:00
Dmitry Podgorny
484e971323 travis-ci: check --enable-cares 2019-10-14 20:37:41 +03:00
Dmitry Podgorny
038404cb99 sock: reduce copy-paste
Move setting (non)block mode to a single static function.
2019-10-14 20:13:04 +03:00
Dmitry Podgorny
6f075bfc71 resolver: fix gcc 9.2.0 warning
message_name_get() adds "." to the tail and it leads to:
    strncpy(&dest, ".", 1);
gcc 9.2.0 generates a warning, because string is truncated without
copying trailing '\0'. Replace strncpy() with memcpy() to fix the
warning.
2019-10-14 17:36:11 +03:00
Dmitry Podgorny
7aed036896 sha1: fixed libstrophe build issues on Windows
Backported from NetXMS.
2019-10-14 09:54:54 +03:00
Dmitry Podgorny
0e8c88a5e4 resolver: backport patch from netxms
explicitely use non-UNICODE structures in libstrophe DNS resolver on Windows
2019-10-14 09:44:11 +03:00
Dmitry Podgorny
a13847d994 resolver: add c-ares support
This patch based on the patch from @gbour (see #7). With
--enable-cares old implementation will be replaced with c-ares API.
2019-10-14 06:51:08 +03:00
Dmitry Podgorny
18b67d6eaf tls/openssl: add LibreSSL support
OpenSSL and LibreSSL versions are incompatible. Moreover, LibreSSL
always define OPENSSL_VERSION_NUMBER as 0x20000000L. Instead of checking
for LibreSSL everywhere explicitly, redefine OPENSSL_VERSION_NUMBER.

See similar issues with nginx project: https://trac.nginx.org/nginx/ticket/1605
2019-10-11 01:59:34 +03:00
Viktor Ivanov
886f45584c Fix bug with xmpp chat not connecting
The facebook suddenly stopped connecting on my dev machine. I still cannot
explain what caused this change in behaviour, but I was finally able to
diagnose and fix the problem.

Chat was not connecting due to an error in tls_start. The windows function
InitializeSecurityContext was returning a SEC_E_INCOMPLETE_MESSAGE status
which was being treated as an error. However, the documentation states
that this is not an error. This status simply indicates that more data
needs to be read. This commit does precisely that -- when
SEC_E_INCOMPLETE_MESSAGE we read more data from the socket and call
InitializeSecurityContext again.
2019-10-11 00:54:37 +03:00
Dmitry Podgorny
4f915d7515 Release libstrophe-0.9.3 2019-10-10 21:47:08 +03:00
Dmitry Podgorny
e25ca7719f conn: don't hardcode buffer size
Use sizeof instead of hardcoded size, so the buffer can be changed
easily.
2019-10-02 03:26:36 +03:00
Dmitry Podgorny
2bf5cc0e41 auth: disable PLAIN when other mechanisms are supported
PLAIN mechanism sends password in BASE64 encoding which everyone can
read. As result, debug logs expose password and users often don't remove
it when post the logs in the Internet.

Usually, both a secure mechanism and PLAIN are used in the scenario when
username or password is incorrect. PLAIN fails in this scenario anyway.
2019-10-01 23:38:50 +03:00
Dmitry Podgorny
30f3cc9263 auth: disable legacy auth by default
Legacy authentication can expose password in plaintext. Since this is
not widely used mechanism, disable it by default. It can be enabled back
with connection option XMPP_CONN_FLAG_LEGACY_AUTH.
2019-10-01 22:34:31 +03:00
Dmitry Podgorny
e120109644 Android: update makefiles
This patch makes libstrophe build with NDK r20 and new libexpat.
2019-09-25 11:00:52 +00:00
Dmitry Podgorny
b4f13205d8 ChangeLog: notes for 0.9.3 2019-09-25 10:58:09 +00:00
Dmitry Podgorny
4797522d88 ChangeLog: mention missed notes for 0.9.2 2019-09-25 10:57:03 +00:00
Hoenig Mark (TT/EIS3-Lol)
5ee06776ee tls/openssl: don't call SSL_shutdown() after a fatal error
According to SSL_shutdown(3), the function must not be called
if previous fatal error occurred.
2019-07-03 15:14:01 +02:00
Dmitry Podgorny
c40b4c12c1 parser/expat: don't cast name to char*
name already has type char*, remove redundant code.
2019-03-04 18:01:29 +02:00
Dmitry Podgorny
d8329960c5 rand: don't pass NULL to memcpy()
Fixes a compiler's warning.
2019-03-04 18:00:01 +02:00
Andrey Starodubtsev
94ece33f16 attempt to free already freed memory fixed 2018-12-26 15:52:02 +03:00
Andrey Starodubtsev
b179c2d277 some memory leaks fixed 2018-12-26 15:44:55 +03:00
Andrey Starodubtsev
ab568a2666 memory leak in hash values fixed 2018-12-13 14:28:41 +03:00
Nikolay Lineckiy
c10c930e8f Fix incomplete send queue cleanup
Due to incomplete reset xmpp_conn_t object could not be safely reused
for reconnection
2018-12-04 18:58:36 +03:00
Dmitry Podgorny
6ddf424192 travis-ci: add -Werror flag
This flag has been removed from Makefile.am.
2018-11-06 11:45:39 +02:00
Dmitry Podgorny
6aff44184e autotools: remove AM_CFLAGS
AM_CFLAGS is ignored when target specific target_CFLAGS are present.
2018-11-06 11:35:52 +02:00
Dmitry Podgorny
9cc9ea86bb tls/openssl: log some info about certificate
Log subject name and issuer name from certificate after TLS connection
is established or fails to connect.
2018-11-06 10:53:46 +02:00
Dmitry Podgorny
13a0bc463c rand: fix comment 2018-10-24 22:28:54 +03:00
jngd
8ff510a3af Fix annoying comment about how disconnect can be performed 2018-09-26 19:15:58 +02:00
Dmitry Podgorny
633d1e9b52 travis-ci: remove check package from dependecies 2018-07-30 20:45:08 +03:00
Dmitry Podgorny
a13ba65cfb Don't pass NULL to strcmp()
There are places where code relies on well-formed stanzas and expects
that ns attribute is always present. Check whether ns NULL or not before
passing it to strcmp().

Fixes #121.
2018-07-30 20:38:43 +03:00
Stu Tomlinson
0e6b0ef84d Only require session if it is not optional
RFC6121 removes the precondition of establishing a session before a client
may exchange XML stanzas on an XMPP network.

This PR allows libstrophe to not request session where it is indicated
as optional by the server, saving a round trip.

https://tools.ietf.org/html/rfc6121#section-1.4
2018-06-08 14:27:29 +00:00
Xavi
9a86efabe5 Remove unused variable 2018-02-23 23:21:37 +01:00
Dmitry Podgorny
a4b05cf156 Release libstrophe-0.9.2 2018-02-23 14:19:15 +02:00
Dmitry Podgorny
06da6d5125 autotools: remove -Werror
We need it only in development versions. Let releases build with
warnings.
2018-02-23 14:19:15 +02:00
Dmitry Podgorny
1bcf77608d crypto: fix typos in comments 2018-02-23 14:05:56 +02:00
Dmitry Podgorny
aab0cc3e3e parser/libxml2: clear parser->stanza properly
- Release stanza in parser_free()
 - Clear parser->stanza even if xmlCreatePushParserCtxt() fails
2018-02-18 14:51:16 +02:00
Dmitry Podgorny
822f95071a sasl: fix memory leak in DIGEST-MD5 implementation
Don't add "qop" two times to hash table, otherwise, an entry is lost
without freeing memory.
2018-02-18 14:02:17 +02:00
Dmitry Podgorny
27f613bc84 stanza: small fixes in xmpp_error_new()
- Receive const text
 - Move variable declaration to the beginning of the block
2018-02-18 13:18:43 +02:00
Dmitry Podgorny
af1ef3855b resolver: suppress warning in windows code 2018-02-18 13:08:16 +02:00
Dmitry Podgorny
7ede9c6d03 tls/openssl: suppress error in special case in tls_stop()
When peer closes connection instead of proper shutdown SSL_shutdown()
fails in bidirectional mode. Handle this case and suppress the error.
2018-02-18 12:03:11 +02:00
Dmitry Podgorny
20c6f225b9 ChangeLog: add missed changes 2018-02-18 10:42:21 +02:00
Scott M Anderson
5b204b0938 fix strtok_r test on MSW 2018-02-18 09:55:33 +02:00
Dmitry Podgorny
6e6093ee7b stanza: fix xmpp_error_new() in #115
* Don't set text to a tag stanza. It returns XMPP_INVOP.
 * Don't use stanza after releasing own reference.
2017-09-12 17:45:11 +03:00
Dmitry Podgorny
f12d43989f stanza: removed tabs and trailing spaces 2017-09-12 17:36:28 +03:00
Ilya Orlov
032e8ec89d Added new stanza for error responce, libstrophe now will be responce <stream:error> if parser failed 2017-09-04 11:46:09 +03:00
Dmitry Podgorny
d0644c5e95 tls/openssl: print errno on unrecoverable error 2017-08-23 09:23:51 +03:00
36 changed files with 1859 additions and 890 deletions

5
.gitignore vendored
View File

@@ -34,7 +34,7 @@ examples/active
examples/basic
examples/bot
examples/component
examples/ext_auth
examples/register
examples/roster
examples/uuid
examples/vcard
@@ -55,8 +55,9 @@ tests/test_sasl
tests/test_scram
tests/test_sha1
tests/test_snprintf
tests/test_string
tests/test_sock
tests/test_stanza
tests/test_string
m4/
libstrophe.project
libs/

View File

@@ -1,15 +1,15 @@
language: c
install:
- sudo apt-get update
- sudo apt-get -y install libtool pkg-config libexpat1-dev libxml2-dev libssl-dev check
- sudo apt-get -y install libtool pkg-config libexpat1-dev libxml2-dev libssl-dev libc-ares-dev
before_script:
- ./bootstrap.sh
script:
- ./configure ${CONFIGURE_OPT} && make && make check-TESTS
- ./configure ${CONFIGURE_OPT} CFLAGS="-Werror" && make && make check
env:
- CONFIGURE_OPT="--without-libxml2"
- CONFIGURE_OPT="--with-libxml2"
- CONFIGURE_OPT="--disable-tls --without-libxml2"
- CONFIGURE_OPT="--disable-tls --with-libxml2"
- CONFIGURE_OPT="--disable-tls"
- CONFIGURE_OPT="--enable-cares"
matrix:
fast_finish: true

View File

@@ -1,3 +1,24 @@
0.10.0
- c-ares support
- LibreSSL support
- examples/register implements XEP-0077
- New API:
- xmpp_stanza_get_child_by_name_and_ns()
- xmpp_conn_is_connecting()
- xmpp_conn_is_connected()
- xmpp_conn_is_disconnected()
- xmpp_stanza_add_child_ex()
- xmpp_stanza_get_context()
0.9.3
- PLAIN mechanism is used only when no other mechanisms are supported
- Legacy authentication is disabled by default, can be enabled with
connection flag XMPP_CONN_FLAG_LEGACY_AUTH
- Session is not established if it is optional
- Fixed a bug causing a reused connection not to cleanup properly
- Improved debug logging in OpenSSL module
- Few memory leaks fixed
0.9.2
- OpenSSL tls module verifies certificate by default. Set flag
XMPP_CONN_FLAG_TRUST_TLS to ignore result of the verification
@@ -8,7 +29,12 @@
userdata
- System handlers are deleted on xmpp_conn_t reconnection. Old system
handlers could cause problems
- Default timeout for xmpp_run() is increased from 1 millisecond to 1
second in order to reduce CPU consumption
- Reduced memory usage in expat module
- New functions:
- xmpp_error_new()
- xmpp_send_error()
- xmpp_ctx_set_timeout()
- xmpp_sha1_digest()

View File

@@ -1,7 +1,6 @@
AUTOMAKE_OPTIONS = subdir-objects
ACLOCAL_AMFLAGS = -I m4
AM_CFLAGS = -g -Wall
PARSER_CFLAGS=@PARSER_CFLAGS@
PARSER_LIBS=@PARSER_LIBS@
@@ -9,15 +8,17 @@ PARSER_LIBS=@PARSER_LIBS@
SSL_CFLAGS = @openssl_CFLAGS@
SSL_LIBS = @openssl_LIBS@
RESOLV_CFLAGS = @RESOLV_CFLAGS@
RESOLV_LIBS = @RESOLV_LIBS@
STROPHE_FLAGS = -I$(top_srcdir) -Wall -Wextra -Werror -Wno-unused-parameter
WARNING_FLAGS = @WARNING_FLAGS@
STROPHE_FLAGS = -I$(top_srcdir) $(WARNING_FLAGS)
STROPHE_LIBS = libstrophe.la
## Main build targets
lib_LTLIBRARIES = libstrophe.la
libstrophe_la_CFLAGS = $(SSL_CFLAGS) $(STROPHE_FLAGS) $(PARSER_CFLAGS)
libstrophe_la_CFLAGS = $(SSL_CFLAGS) $(STROPHE_FLAGS) $(PARSER_CFLAGS) $(RESOLV_CFLAGS)
libstrophe_la_LDFLAGS = $(SSL_LIBS) $(PARSER_LIBS) $(RESOLV_LIBS) -no-undefined
# Export only public API
libstrophe_la_LDFLAGS += -export-symbols-regex '^xmpp_'
@@ -98,10 +99,10 @@ noinst_PROGRAMS = \
examples/basic \
examples/bot \
examples/component \
examples/ext_auth \
examples/roster \
examples/uuid \
examples/vcard
examples/vcard \
examples/register
examples_active_SOURCES = examples/active.c
examples_active_CFLAGS = $(STROPHE_FLAGS)
@@ -115,9 +116,6 @@ examples_bot_LDADD = $(STROPHE_LIBS)
examples_component_SOURCES = examples/component.c
examples_component_CFLAGS = $(STROPHE_FLAGS)
examples_component_LDADD = $(STROPHE_LIBS)
examples_ext_auth_SOURCES = examples/ext_auth.c
examples_ext_auth_CFLAGS = $(STROPHE_FLAGS)
examples_ext_auth_LDADD = $(STROPHE_LIBS)
examples_roster_SOURCES = examples/roster.c
examples_roster_CFLAGS = $(STROPHE_FLAGS)
examples_roster_LDADD = $(STROPHE_LIBS)
@@ -127,6 +125,9 @@ examples_uuid_LDADD = $(STROPHE_LIBS)
examples_vcard_SOURCES = examples/vcard.c
examples_vcard_CFLAGS = $(STROPHE_FLAGS)
examples_vcard_LDADD = $(STROPHE_LIBS)
examples_register_SOURCES = examples/register.c
examples_register_CFLAGS = $(STROPHE_FLAGS)
examples_register_LDADD = $(STROPHE_LIBS)
## Tests
@@ -142,6 +143,7 @@ TESTS = \
tests/test_jid \
tests/test_snprintf \
tests/test_string \
tests/test_stanza \
tests/test_resolver
check_PROGRAMS = $(TESTS)
@@ -196,3 +198,8 @@ tests_test_string_SOURCES = tests/test_string.c tests/test.h
tests_test_string_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src
tests_test_string_LDADD = $(STROPHE_LIBS)
tests_test_string_LDFLAGS = -static
tests_test_stanza_SOURCES = tests/test_stanza.c
tests_test_stanza_CFLAGS = $(STROPHE_FLAGS)
tests_test_stanza_LDADD = $(STROPHE_LIBS)
tests_test_stanza_LDFLAGS = -static

View File

@@ -1,4 +1,4 @@
AC_INIT([libstrophe], [0.9.1], [jack@metajack.im])
AC_INIT([libstrophe], [0.9.3], [jack@metajack.im])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([foreign])
LT_INIT([dlopen])
@@ -15,12 +15,24 @@ AS_CASE([$host_os],
[*nto*|*qnx*], [PLATFORM="qnx"],
[*solaris*], [PLATFORM="solaris"],
[*android*], [PLATFORM="android"],
[*haiku*], [PLATFORM="haiku"],
[PLATFORM="nix"])
WARNING_FLAGS="-Wall"
AS_CASE([$PLATFORM],
[haiku], [],
[WARNING_FLAGS="$WARNING_FLAGS -Wextra -Wno-unused-parameter"])
AC_ARG_WITH([libxml2],
[AS_HELP_STRING([--with-libxml2], [use libxml2 for XML parsing, expat is the default])])
AC_ARG_ENABLE([tls],
[AS_HELP_STRING([--disable-tls], [disable TLS support])])
AC_ARG_ENABLE([cares],
[AS_HELP_STRING([--enable-cares], [use c-ares for DNS resolution])])
AC_SEARCH_LIBS([socket], [network socket])
#AC_CHECK_FUNCS([snprintf vsnprintf])
if test "x$enable_tls" != xno; then
PKG_CHECK_MODULES([openssl], [openssl],
@@ -62,7 +74,7 @@ if test "x$with_libxml2" != xno -a "x$with_parser" = x; then
[
with_parser="libxml2"
libxml2_CFLAGS="-I${includedir}/libxml2"
libxml2_LIBS="-lxml2"
libxml2_LIBS="-lxml2 -lm"
PC_LIBS="${libxml2_LIBS} ${PC_LIBS}"
PC_CFLAGS="${libxml2_CFLAGS} ${PC_CFLAGS}"
])
@@ -82,13 +94,24 @@ fi
AC_MSG_NOTICE([libstrophe will use the $with_parser XML parser])
AC_SEARCH_LIBS([socket], [socket])
if test "x$enable_cares" = xyes; then
PKG_CHECK_MODULES([libcares], [libcares >= 1.7.0],
[
PC_REQUIRES="libcares ${PC_REQUIRES}"
RESOLV_CFLAGS=$libcares_CFLAGS
RESOLV_LIBS=$libcares_LIBS
AC_DEFINE([HAVE_CARES])
],
[AC_MSG_ERROR([libcares not found])])
# TODO: if pkg-config doesn't find, check the library manually
else
AS_CASE([$PLATFORM],
[bsd], [RESOLV_LIBS=""],
[qnx], [RESOLV_LIBS="-lsocket"],
[solaris], [RESOLV_LIBS="-lresolv -lsocket -lnsl"],
[android], [RESOLV_LIBS=""],
[haiku], [RESOLV_LIBS="-lnetwork"],
[RESOLV_LIBS="-lresolv"])
LIBS_TMP="${LIBS}"
@@ -110,6 +133,8 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([
LIBS="${LIBS_TMP}"
PC_LIBS="${RESOLV_LIBS} ${PC_LIBS}"
fi
m4_ifdef([PKG_INSTALLDIR], [PKG_INSTALLDIR],
[AC_ARG_WITH([pkgconfigdir],
[AS_HELP_STRING([--with-pkgconfigdir],
@@ -126,6 +151,8 @@ AC_SUBST([PC_LIBS], [${PC_LIBS}])
AC_SUBST(PARSER_CFLAGS)
AC_SUBST(PARSER_LIBS)
AC_SUBST(RESOLV_CFLAGS)
AC_SUBST(RESOLV_LIBS)
AC_SUBST(WARNING_FLAGS)
AC_CONFIG_FILES([Makefile libstrophe.pc])
AC_OUTPUT

View File

@@ -1,290 +0,0 @@
/* ext_auth.c
* strophe XMPP client library -- PLAIN mechanism example with a raw connection
*
* Copyright (C) 2016 Dmitry Podgorny <pasis.ua@gmail.com>
*
* This software is provided AS-IS with no warranty, either express
* or implied.
*
* This program is dual licensed under the MIT and GPLv3 licenses.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strophe.h>
typedef struct {
xmpp_ctx_t *ctx;
const char *jid;
const char *pass;
char *domain;
int authenticated;
} xmpp_auth_t;
#define FEATURES_TIMEOUT 5000 /* 5 seconds */
static int _handle_error(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
fprintf(stderr, "DEBUG: received stream error\n");
xmpp_disconnect(conn);
return 0;
}
static int _handle_proceedtls_default(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_auth_t *auth = (xmpp_auth_t *)userdata;
const char *name = xmpp_stanza_get_name(stanza);
(void)auth;
if (strcmp(name, "proceed") == 0) {
if (xmpp_conn_tls_start(conn) == 0) {
xmpp_handler_delete(conn, _handle_error);
xmpp_conn_open_stream_default(conn);
} else {
/* failed tls spoils the connection, so disconnect */
xmpp_disconnect(conn);
}
} else
xmpp_disconnect(conn);
return 0;
}
static int _plain_mechanim_exists(xmpp_ctx_t *ctx, xmpp_stanza_t *features)
{
xmpp_stanza_t *mechanisms;
xmpp_stanza_t *child;
char *text;
int found = 0;
mechanisms = xmpp_stanza_get_child_by_name(features, "mechanisms");
child = mechanisms ? xmpp_stanza_get_children(mechanisms) : NULL;
while (!found && mechanisms && child) {
/* TODO xmpp_stanza_get_name(child) is "mechanism" */
text = xmpp_stanza_get_text(child);
found = text && strcmp(text, "PLAIN") == 0;
xmpp_free(ctx, text);
child = xmpp_stanza_get_next(child);
}
return found;
}
static char *_plain_mechanism_authdata(xmpp_auth_t *auth)
{
char *node, *b64;
unsigned char *msg;
size_t node_len, pass_len;
node = xmpp_jid_node(auth->ctx, auth->jid);
node_len = strlen(node);
pass_len = strlen(auth->pass);
msg = malloc(node_len + pass_len + 2);
msg[0] = '\0';
memcpy(msg + 1, node, node_len);
msg[node_len + 1] = '\0';
memcpy(msg + node_len + 2, auth->pass, pass_len);
b64 = xmpp_base64_encode(auth->ctx, msg, node_len + pass_len + 2);
free(msg);
xmpp_free(auth->ctx, node);
return b64;
}
static int _plain_mechanism_handle_result(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_auth_t *auth = (xmpp_auth_t *)userdata;
const char *name;
name = xmpp_stanza_get_name(stanza);
if (strcmp(name, "success") == 0) {
/* SASL PLAIN auth successful, we need to restart the stream */
fprintf(stderr, "DEBUG: SASL PLAIN auth successful\n");
auth->authenticated = 1;
xmpp_handler_delete(conn, _handle_error);
xmpp_conn_open_stream_default(conn);
} else {
fprintf(stderr, "DEBUG: SASL authentication failed: %s\n", name);
xmpp_disconnect(conn);
}
return 0;
}
static int _handle_missing_features(xmpp_conn_t * const conn,
void * const userdata)
{
fprintf(stderr, "DEBUG: timeout\n");
xmpp_disconnect(conn);
return 0;
}
static int _handle_features(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_auth_t *auth = (xmpp_auth_t *)userdata;
xmpp_ctx_t *ctx = auth->ctx;
xmpp_stanza_t *plain, *child;
char *str;
xmpp_timed_handler_delete(conn, _handle_missing_features);
if (auth->authenticated) {
/* after successful SASL authentication */
/* TODO bind, session and we'are done */
xmpp_disconnect(conn); /* XXX */
return 0;
}
/* secure connection if possible */
child = xmpp_stanza_get_child_by_name(stanza, "starttls");
if (child && (strcmp(xmpp_stanza_get_ns(child), XMPP_NS_TLS) == 0)) {
child = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(child, "starttls");
xmpp_stanza_set_ns(child, XMPP_NS_TLS);
xmpp_handler_add(conn, _handle_proceedtls_default,
XMPP_NS_TLS, NULL, NULL, userdata);
xmpp_send(conn, child);
xmpp_stanza_release(child);
return 0;
}
/* check if PLAIN mechanism is supported */
if (!_plain_mechanim_exists(ctx, stanza)) {
fprintf(stderr, "DEBUG: PLAIN mechanism is NOT supported\n");
xmpp_disconnect(conn);
return 0;
}
/* perform authentication */
plain = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(plain, "auth");
xmpp_stanza_set_ns(plain, XMPP_NS_SASL);
xmpp_stanza_set_attribute(plain, "mechanism", "PLAIN");
str = _plain_mechanism_authdata(auth);
child = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(child, str);
xmpp_free(ctx, str);
xmpp_stanza_add_child(plain, child);
xmpp_stanza_release(child);
xmpp_handler_add(conn, _plain_mechanism_handle_result,
XMPP_NS_SASL, NULL, NULL, userdata);
xmpp_send(conn, plain);
xmpp_stanza_release(plain);
return 0;
}
static void conn_handler(xmpp_conn_t * const conn,
const xmpp_conn_event_t status,
const int error,
xmpp_stream_error_t * const stream_error,
void * const userdata)
{
xmpp_auth_t *auth = (xmpp_auth_t *)userdata;
xmpp_ctx_t *ctx = auth->ctx;
int secured;
if (status == XMPP_CONN_RAW_CONNECT) {
fprintf(stderr, "DEBUG: raw connection established\n");
xmpp_conn_open_stream_default(conn);
} else if (status == XMPP_CONN_CONNECT) {
fprintf(stderr, "DEBUG: stream opened\n");
secured = xmpp_conn_is_secured(conn);
fprintf(stderr, "DEBUG: connection is %s.\n",
secured ? "secured" : "NOT secured");
/* setup handler for stream:error */
xmpp_handler_add(conn, _handle_error, XMPP_NS_STREAMS,
"error", NULL, NULL);
/* setup handlers for incoming <stream:features> */
xmpp_handler_add(conn, _handle_features, XMPP_NS_STREAMS,
"features", NULL, auth);
xmpp_timed_handler_add(conn, _handle_missing_features,
FEATURES_TIMEOUT, NULL);
} else {
fprintf(stderr, "DEBUG: disconnected\n");
xmpp_stop(ctx);
}
}
static xmpp_auth_t *xmpp_auth_new(void)
{
xmpp_auth_t *auth;
auth = malloc(sizeof(*auth));
if (auth != NULL) {
memset(auth, 0, sizeof(*auth));
}
return auth;
}
static void xmpp_auth_release(xmpp_auth_t *auth)
{
free(auth);
}
int main(int argc, char **argv)
{
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
xmpp_auth_t *auth;
const char *jid;
const char *pass;
const char *host = NULL;
if (argc < 3 || argc > 4) {
fprintf(stderr, "Usage: %s <jid> <pass> [<host>]\n", argv[0]);
return 1;
}
jid = argv[1];
pass = argv[2];
if (argc > 3)
host = argv[3];
/*
* Note, this example doesn't handle errors. Applications should check
* return values of non-void functions.
*/
xmpp_initialize();
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
ctx = xmpp_ctx_new(NULL, log);
conn = xmpp_conn_new(ctx);
xmpp_conn_set_jid(conn, jid);
/* private data */
auth = xmpp_auth_new();
auth->ctx = ctx;
auth->jid = jid;
auth->pass = pass;
auth->domain = xmpp_jid_domain(ctx, jid);
xmpp_connect_raw(conn, host, 0, conn_handler, auth);
xmpp_run(ctx);
/* release private data */
xmpp_auth_release(auth);
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
xmpp_shutdown();
return 0;
}

357
examples/register.c Normal file
View File

@@ -0,0 +1,357 @@
/* register.c
* strophe XMPP client library -- In-band registration (XEP-0077)
*
* Copyright (C) 2020 Dmitry Podgorny <pasis.ua@gmail.com>
*
* This software is provided AS-IS with no warranty, either express
* or implied.
*
* This program is dual licensed under the MIT and GPLv3 licenses.
*/
/*
* This example uses a "raw" connection to establish connection to a server
* without account. Then it queries server to register new account according
* to XEP-0077.
*
* How to use it. After the application connects and receives instructions
* from the server, user will be prompted to type information such as
* username, password, etc. Press enter without typing if you want to skip
* a field and not to send it to the server.
*
* Notice, the example doesn't implement forms. Therefore, it won't work
* in complicated scenarios.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strophe.h>
typedef struct {
xmpp_ctx_t *ctx;
const char *jid;
} xmpp_reg_t;
#define FEATURES_TIMEOUT 5000 /* 5 seconds */
static void iq_reg_send_form(xmpp_reg_t *reg,
xmpp_conn_t *conn,
xmpp_stanza_t *stanza)
{
xmpp_ctx_t *ctx = reg->ctx;
xmpp_stanza_t *query;
xmpp_stanza_t *next;
xmpp_stanza_t *elem;
xmpp_stanza_t *text;
xmpp_stanza_t *iq;
const char *name;
ssize_t rc;
size_t size;
char *s;
query = xmpp_stanza_get_child_by_name(stanza, "query");
if (!query) {
xmpp_disconnect(conn);
return;
}
next = xmpp_stanza_get_children(query);
query = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(query, "query");
xmpp_stanza_set_ns(query, XMPP_NS_REGISTER);
while (next) {
name = xmpp_stanza_get_name(next);
if (name && strcmp(name, "instructions") == 0) {
s = xmpp_stanza_get_text(next);
printf("instructions: %s\n", s);
xmpp_free(ctx, s);
} else {
printf("%s: ", name);
s = NULL;
size = 0;
rc = getline(&s, &size, stdin);
if (rc > 0 && s[rc - 1] == '\n')
s[rc - 1] = '\0';
if (rc > 0 && strlen(s) > 0) {
elem = xmpp_stanza_new(ctx);
text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(text, s);
xmpp_stanza_set_name(elem, name);
xmpp_stanza_add_child(elem, text);
xmpp_stanza_add_child(query, elem);
xmpp_stanza_release(text);
xmpp_stanza_release(elem);
}
free(s);
}
next = xmpp_stanza_get_next(next);
}
if (xmpp_stanza_get_children(query) == NULL) {
fprintf(stderr, "DEBUG: nothing to send\n");
xmpp_disconnect(conn);
} else {
iq = xmpp_iq_new(ctx, "set", "reg2");
xmpp_stanza_add_child(iq, query);
xmpp_stanza_release(query);
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
}
}
static int iq_reg2_cb(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
const char *type;
type = xmpp_stanza_get_type(stanza);
if (!type || strcmp(type, "error") == 0) {
fprintf(stderr, "DEBUG: error during registration\n");
goto quit;
}
if (strcmp(type, "result") != 0) {
fprintf(stderr, "DEBUG: expected type 'result', but got %s\n", type);
goto quit;
}
fprintf(stderr, "DEBUG: successful registration\n");
quit:
xmpp_disconnect(conn);
return 0;
}
static int iq_reg_cb(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_reg_t *reg = (xmpp_reg_t *)userdata;
xmpp_stanza_t *registered = NULL;
xmpp_stanza_t *query;
const char *type;
type = xmpp_stanza_get_type(stanza);
if (!type || strcmp(type, "error") == 0) {
fprintf(stderr, "DEBUG: error during registration\n");
xmpp_disconnect(conn);
goto quit;
}
if (strcmp(type, "result") != 0) {
fprintf(stderr, "DEBUG: expected type 'result', but got %s\n", type);
xmpp_disconnect(conn);
goto quit;
}
query = xmpp_stanza_get_child_by_name(stanza, "query");
if (query)
registered = xmpp_stanza_get_child_by_name(query, "registered");
if (registered != NULL) {
fprintf(stderr, "DEBUG: already registered\n");
xmpp_disconnect(conn);
goto quit;
}
xmpp_id_handler_add(conn, iq_reg2_cb, "reg2", reg);
iq_reg_send_form(reg, conn, stanza);
quit:
return 0;
}
static int _handle_error(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
fprintf(stderr, "DEBUG: received stream error\n");
xmpp_disconnect(conn);
return 0;
}
static int _handle_proceedtls_default(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
const char *name = xmpp_stanza_get_name(stanza);
if (strcmp(name, "proceed") == 0) {
fprintf(stderr, "DEBUG: proceeding with TLS\n");
if (xmpp_conn_tls_start(conn) == 0) {
xmpp_handler_delete(conn, _handle_error);
xmpp_conn_open_stream_default(conn);
} else {
fprintf(stderr, "DEBUG: TLS failed\n");
/* failed tls spoils the connection, so disconnect */
xmpp_disconnect(conn);
}
}
return 0;
}
static int _handle_missing_features(xmpp_conn_t * const conn,
void * const userdata)
{
fprintf(stderr, "DEBUG: timeout\n");
xmpp_disconnect(conn);
return 0;
}
static int _handle_features(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_reg_t *reg = (xmpp_reg_t *)userdata;
xmpp_ctx_t *ctx = reg->ctx;
xmpp_stanza_t *child;
xmpp_stanza_t *iq;
char *domain;
xmpp_timed_handler_delete(conn, _handle_missing_features);
/* secure connection if possible */
child = xmpp_stanza_get_child_by_name(stanza, "starttls");
if (child && (strcmp(xmpp_stanza_get_ns(child), XMPP_NS_TLS) == 0)) {
fprintf(stderr, "DEBUG: server supports TLS, try to establish\n");
child = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(child, "starttls");
xmpp_stanza_set_ns(child, XMPP_NS_TLS);
xmpp_handler_add(conn, _handle_proceedtls_default,
XMPP_NS_TLS, NULL, NULL, NULL);
xmpp_send(conn, child);
xmpp_stanza_release(child);
return 0;
}
/* check whether server supports in-band registration */
child = xmpp_stanza_get_child_by_name(stanza, "register");
if (child && strcmp(xmpp_stanza_get_ns(child), XMPP_NS_REGISTER) == 0) {
fprintf(stderr, "DEBUG: server doesn't support in-band registration\n");
xmpp_disconnect(conn);
return 0;
}
fprintf(stderr, "DEBUG: server supports in-band registration\n");
domain = xmpp_jid_domain(ctx, reg->jid);
iq = xmpp_iq_new(ctx, "get", "reg1");
xmpp_stanza_set_to(iq, domain);
child = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(child, "query");
xmpp_stanza_set_ns(child, XMPP_NS_REGISTER);
xmpp_stanza_add_child(iq, child);
xmpp_handler_add(conn, iq_reg_cb, XMPP_NS_REGISTER, "iq", NULL, reg);
xmpp_send(conn, iq);
xmpp_free(ctx, domain);
xmpp_stanza_release(child);
xmpp_stanza_release(iq);
return 0;
}
static void conn_handler(xmpp_conn_t * const conn,
const xmpp_conn_event_t status,
const int error,
xmpp_stream_error_t * const stream_error,
void * const userdata)
{
xmpp_reg_t *reg = (xmpp_reg_t *)userdata;
int secured;
if (status == XMPP_CONN_RAW_CONNECT) {
fprintf(stderr, "DEBUG: raw connection established\n");
xmpp_conn_open_stream_default(conn);
} else if (status == XMPP_CONN_CONNECT) {
fprintf(stderr, "DEBUG: stream opened\n");
secured = xmpp_conn_is_secured(conn);
fprintf(stderr, "DEBUG: connection is %s.\n",
secured ? "secured" : "NOT secured");
/* setup handler for stream:error */
xmpp_handler_add(conn, _handle_error, XMPP_NS_STREAMS,
"error", NULL, NULL);
/* setup handlers for incoming <stream:features> */
xmpp_handler_add(conn, _handle_features, XMPP_NS_STREAMS,
"features", NULL, reg);
xmpp_timed_handler_add(conn, _handle_missing_features,
FEATURES_TIMEOUT, NULL);
} else {
fprintf(stderr, "DEBUG: disconnected\n");
xmpp_stop(reg->ctx);
}
}
xmpp_reg_t *xmpp_reg_new(void)
{
xmpp_reg_t *reg;
reg = malloc(sizeof(*reg));
if (reg != NULL) {
memset(reg, 0, sizeof(*reg));
}
return reg;
}
void xmpp_reg_release(xmpp_reg_t *reg)
{
free(reg);
}
int main(int argc, char **argv)
{
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
xmpp_reg_t *reg;
const char *jid;
const char *host = NULL;
char *domain;
if (argc < 2 || argc > 3) {
fprintf(stderr, "Usage: %s <jid> [<host>]\n", argv[0]);
return 1;
}
jid = argv[1];
if (argc > 2)
host = argv[2];
/*
* Note, this example doesn't handle errors. Applications should check
* return values of non-void functions.
*/
xmpp_initialize();
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
ctx = xmpp_ctx_new(NULL, log);
conn = xmpp_conn_new(ctx);
/* jid can be a jid or domain for "raw" connection */
domain = xmpp_jid_domain(ctx, jid);
xmpp_conn_set_jid(conn, domain);
xmpp_free(ctx, domain);
/* private data */
reg = xmpp_reg_new();
reg->ctx = ctx;
reg->jid = jid;
xmpp_connect_raw(conn, host, 0, conn_handler, reg);
xmpp_run(ctx);
/* release private data */
xmpp_reg_release(reg);
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
xmpp_shutdown();
return 0;
}

View File

@@ -62,7 +62,7 @@ include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libexpat
LOCAL_CFLAGS := -DHAVE_MEMMOVE
LOCAL_CFLAGS := -DHAVE_MEMMOVE -DXML_DEV_URANDOM
#LOCAL_C_INCLUDES := \
# $(LOCAL_PATH)/expat

View File

@@ -1,2 +1,2 @@
APP_ABI := armeabi armeabi-v7a mips x86
APP_ABI := armeabi-v7a arm64-v8a
APP_PLATFORM := android-19

View File

@@ -24,7 +24,7 @@
#include "rand.h"
#ifdef _MSC_VER
#define strcasecmp stricmp
#define strcasecmp _stricmp
#endif
/* TODO: these should configurable at runtime on a per connection basis */
@@ -61,6 +61,7 @@
#endif
static void _auth(xmpp_conn_t * const conn);
static void _auth_legacy(xmpp_conn_t *conn);
static void _handle_open_sasl(xmpp_conn_t * const conn);
static void _handle_open_tls(xmpp_conn_t * const conn);
@@ -69,11 +70,6 @@ static int _handle_component_hs_response(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata);
static int _handle_missing_legacy(xmpp_conn_t * const conn,
void * const userdata);
static int _handle_legacy(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata);
static int _handle_features_sasl(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata);
@@ -103,6 +99,9 @@ static int _handle_session(xmpp_conn_t * const conn,
void * const userdata);
static int _handle_missing_session(xmpp_conn_t * const conn,
void * const userdata);
static int _handle_sm(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata);
static int _handle_missing_handshake(xmpp_conn_t * const conn,
void * const userdata);
@@ -219,6 +218,7 @@ static int _handle_features(xmpp_conn_t * const conn,
void * const userdata)
{
xmpp_stanza_t *child, *mech;
const char *ns;
char *text;
/* remove the handler that detects missing stream:features */
@@ -228,8 +228,10 @@ static int _handle_features(xmpp_conn_t * const conn,
if (!conn->secured) {
if (!conn->tls_disabled) {
child = xmpp_stanza_get_child_by_name(stanza, "starttls");
if (child && (strcmp(xmpp_stanza_get_ns(child), XMPP_NS_TLS) == 0))
conn->tls_support = 1;
if (child) {
ns = xmpp_stanza_get_ns(child);
conn->tls_support = ns != NULL && strcmp(ns, XMPP_NS_TLS) == 0;
}
} else {
conn->tls_support = 0;
}
@@ -237,11 +239,15 @@ static int _handle_features(xmpp_conn_t * const conn,
/* check for SASL */
child = xmpp_stanza_get_child_by_name(stanza, "mechanisms");
if (child && (strcmp(xmpp_stanza_get_ns(child), XMPP_NS_SASL) == 0)) {
ns = child ? xmpp_stanza_get_ns(child) : NULL;
if (child && ns && strcmp(ns, XMPP_NS_SASL) == 0) {
for (mech = xmpp_stanza_get_children(child); mech;
mech = xmpp_stanza_get_next(mech)) {
if (xmpp_stanza_get_name(mech) && strcmp(xmpp_stanza_get_name(mech), "mechanism") == 0) {
text = xmpp_stanza_get_text(mech);
if (text == NULL)
continue;
if (strcasecmp(text, "PLAIN") == 0)
conn->sasl_support |= SASL_MASK_PLAIN;
else if (strcasecmp(text, "DIGEST-MD5") == 0)
@@ -256,6 +262,10 @@ static int _handle_features(xmpp_conn_t * const conn,
}
}
/* Disable PLAIN when other secure mechanisms are supported */
if (conn->sasl_support & ~(SASL_MASK_PLAIN | SASL_MASK_ANONYMOUS))
conn->sasl_support &= ~SASL_MASK_PLAIN;
_auth(conn);
return 0;
@@ -550,9 +560,11 @@ static xmpp_stanza_t *_make_sasl_auth(xmpp_conn_t * const conn,
*/
static void _auth(xmpp_conn_t * const conn)
{
xmpp_stanza_t *auth, *authdata, *query, *child, *iq;
char *str, *authid;
xmpp_stanza_t *auth;
xmpp_stanza_t *authdata;
char *authid;
char *scram_init;
char *str;
int anonjid;
/* if there is no node in conn->jid, we assume anonymous connect */
@@ -719,105 +731,12 @@ static void _auth(xmpp_conn_t * const conn)
/* SASL PLAIN was tried */
conn->sasl_support &= ~SASL_MASK_PLAIN;
} else if (conn->type == XMPP_CLIENT) {
/* legacy client authentication */
iq = xmpp_iq_new(conn->ctx, "set", "_xmpp_auth1");
if (!iq) {
disconnect_mem_error(conn);
return;
}
query = xmpp_stanza_new(conn->ctx);
if (!query) {
xmpp_stanza_release(iq);
disconnect_mem_error(conn);
return;
}
xmpp_stanza_set_name(query, "query");
xmpp_stanza_set_ns(query, XMPP_NS_AUTH);
xmpp_stanza_add_child(iq, query);
xmpp_stanza_release(query);
child = xmpp_stanza_new(conn->ctx);
if (!child) {
xmpp_stanza_release(iq);
disconnect_mem_error(conn);
return;
}
xmpp_stanza_set_name(child, "username");
xmpp_stanza_add_child(query, child);
xmpp_stanza_release(child);
authdata = xmpp_stanza_new(conn->ctx);
if (!authdata) {
xmpp_stanza_release(iq);
disconnect_mem_error(conn);
return;
}
str = xmpp_jid_node(conn->ctx, conn->jid);
xmpp_stanza_set_text(authdata, str);
xmpp_free(conn->ctx, str);
xmpp_stanza_add_child(child, authdata);
xmpp_stanza_release(authdata);
child = xmpp_stanza_new(conn->ctx);
if (!child) {
xmpp_stanza_release(iq);
disconnect_mem_error(conn);
return;
}
xmpp_stanza_set_name(child, "password");
xmpp_stanza_add_child(query, child);
xmpp_stanza_release(child);
authdata = xmpp_stanza_new(conn->ctx);
if (!authdata) {
xmpp_stanza_release(iq);
disconnect_mem_error(conn);
return;
}
xmpp_stanza_set_text(authdata, conn->pass);
xmpp_stanza_add_child(child, authdata);
xmpp_stanza_release(authdata);
child = xmpp_stanza_new(conn->ctx);
if (!child) {
xmpp_stanza_release(iq);
disconnect_mem_error(conn);
return;
}
xmpp_stanza_set_name(child, "resource");
xmpp_stanza_add_child(query, child);
xmpp_stanza_release(child);
authdata = xmpp_stanza_new(conn->ctx);
if (!authdata) {
xmpp_stanza_release(iq);
disconnect_mem_error(conn);
return;
}
str = xmpp_jid_resource(conn->ctx, conn->jid);
if (str) {
xmpp_stanza_set_text(authdata, str);
xmpp_free(conn->ctx, str);
} else {
xmpp_stanza_release(authdata);
xmpp_stanza_release(iq);
xmpp_error(conn->ctx, "auth",
"Cannot authenticate without resource");
xmpp_disconnect(conn);
return;
}
xmpp_stanza_add_child(child, authdata);
xmpp_stanza_release(authdata);
handler_add_id(conn, _handle_legacy, "_xmpp_auth1", NULL);
handler_add_timed(conn, _handle_missing_legacy,
LEGACY_TIMEOUT, NULL);
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
} else if (conn->type == XMPP_CLIENT && conn->auth_legacy_enabled) {
/* legacy client authentication */
_auth_legacy(conn);
} else {
xmpp_error(conn->ctx, "auth", "Cannot authenticate with known methods");
xmpp_disconnect(conn);
}
}
@@ -871,7 +790,8 @@ static int _handle_features_sasl(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_stanza_t *bind, *session, *iq, *res, *text;
xmpp_stanza_t *bind, *session, *iq, *res, *text, *opt;
const char *ns;
char *resource;
/* remove missing features handler */
@@ -880,16 +800,27 @@ static int _handle_features_sasl(xmpp_conn_t * const conn,
/* we are expecting <bind/> and <session/> since this is a
XMPP style connection */
/* check whether resource binding is required */
bind = xmpp_stanza_get_child_by_name(stanza, "bind");
if (bind && strcmp(xmpp_stanza_get_ns(bind), XMPP_NS_BIND) == 0) {
/* resource binding is required */
conn->bind_required = 1;
if (bind) {
ns = xmpp_stanza_get_ns(bind);
conn->bind_required = ns != NULL && strcmp(ns, XMPP_NS_BIND) == 0;
}
/* check whether session establishment is required */
session = xmpp_stanza_get_child_by_name(stanza, "session");
if (session && strcmp(xmpp_stanza_get_ns(session), XMPP_NS_SESSION) == 0) {
/* session establishment required */
conn->session_required = 1;
if (session) {
ns = xmpp_stanza_get_ns(session);
opt = xmpp_stanza_get_child_by_name(session, "optional");
if (!opt)
conn->session_required = ns != NULL &&
strcmp(ns, XMPP_NS_SESSION) == 0;
}
opt = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_SM);
if (opt && strcmp(xmpp_stanza_get_name(opt), "sm") == 0) {
/* stream management supported */
conn->sm_support = 1;
}
/* if bind is required, go ahead and start it */
@@ -980,7 +911,7 @@ static int _handle_bind(xmpp_conn_t * const conn,
void * const userdata)
{
const char *type;
xmpp_stanza_t *iq, *session;
xmpp_stanza_t *iq, *enable, *session, *binding, *jid_stanza;
/* delete missing bind handler */
xmpp_timed_handler_delete(conn, _handle_missing_bind);
@@ -991,12 +922,11 @@ static int _handle_bind(xmpp_conn_t * const conn,
xmpp_error(conn->ctx, "xmpp", "Binding failed.");
xmpp_disconnect(conn);
} else if (type && strcmp(type, "result") == 0) {
xmpp_stanza_t *binding = xmpp_stanza_get_child_by_name(stanza, "bind");
binding = xmpp_stanza_get_child_by_name(stanza, "bind");
xmpp_debug(conn->ctx, "xmpp", "Bind successful.");
if (binding) {
xmpp_stanza_t *jid_stanza = xmpp_stanza_get_child_by_name(binding,
"jid");
jid_stanza = xmpp_stanza_get_child_by_name(binding, "jid");
if (jid_stanza) {
conn->bound_jid = xmpp_stanza_get_text(jid_stanza);
}
@@ -1032,7 +962,23 @@ static int _handle_bind(xmpp_conn_t * const conn,
/* send session establishment request */
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
} else {
}
if (conn->sm_support) {
enable = xmpp_stanza_new(conn->ctx);
if (!enable) {
disconnect_mem_error(conn);
return 0;
}
xmpp_stanza_set_name(enable, "enable");
xmpp_stanza_set_ns(enable, XMPP_NS_SM);
handler_add(conn, _handle_sm, XMPP_NS_SM, NULL, NULL, NULL);
xmpp_send(conn, enable);
xmpp_stanza_release(enable);
conn->sm_sent_nr = 0;
}
if (!conn->session_required) {
conn->authenticated = 1;
/* call connection handler */
@@ -1092,6 +1038,32 @@ static int _handle_missing_session(xmpp_conn_t * const conn,
return 0;
}
static int _handle_missing_legacy(xmpp_conn_t * const conn,
void * const userdata)
{
xmpp_error(conn->ctx, "xmpp", "Server did not reply to legacy "\
"authentication request.");
xmpp_disconnect(conn);
return 0;
}
static int _handle_sm(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
{
const char *name;
name = xmpp_stanza_get_name(stanza);
if (name && strcmp(name, "enabled") == 0) {
conn->sm_enabled = 1;
conn->sm_handled_nr = 0;
} else {
conn->sm_enabled = 0;
xmpp_warn(conn->ctx, "auth", "Stream management failed.");
}
return 0;
}
static int _handle_legacy(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata)
@@ -1128,13 +1100,97 @@ static int _handle_legacy(xmpp_conn_t * const conn,
return 0;
}
static int _handle_missing_legacy(xmpp_conn_t * const conn,
void * const userdata)
static void _auth_legacy(xmpp_conn_t *conn)
{
xmpp_error(conn->ctx, "xmpp", "Server did not reply to legacy "\
"authentication request.");
xmpp_disconnect(conn);
return 0;
xmpp_stanza_t *iq;
xmpp_stanza_t *authdata;
xmpp_stanza_t *query;
xmpp_stanza_t *child;
char *str;
xmpp_debug(conn->ctx, "auth", "Legacy authentication request");
iq = xmpp_iq_new(conn->ctx, "set", "_xmpp_auth1");
if (!iq)
goto err;
query = xmpp_stanza_new(conn->ctx);
if (!query)
goto err_free;
xmpp_stanza_set_name(query, "query");
xmpp_stanza_set_ns(query, XMPP_NS_AUTH);
xmpp_stanza_add_child(iq, query);
xmpp_stanza_release(query);
child = xmpp_stanza_new(conn->ctx);
if (!child)
goto err_free;
xmpp_stanza_set_name(child, "username");
xmpp_stanza_add_child(query, child);
xmpp_stanza_release(child);
authdata = xmpp_stanza_new(conn->ctx);
if (!authdata)
goto err_free;
str = xmpp_jid_node(conn->ctx, conn->jid);
if (!str) {
xmpp_stanza_release(authdata);
goto err_free;
}
xmpp_stanza_set_text(authdata, str);
xmpp_free(conn->ctx, str);
xmpp_stanza_add_child(child, authdata);
xmpp_stanza_release(authdata);
child = xmpp_stanza_new(conn->ctx);
if (!child)
goto err_free;
xmpp_stanza_set_name(child, "password");
xmpp_stanza_add_child(query, child);
xmpp_stanza_release(child);
authdata = xmpp_stanza_new(conn->ctx);
if (!authdata)
goto err_free;
xmpp_stanza_set_text(authdata, conn->pass);
xmpp_stanza_add_child(child, authdata);
xmpp_stanza_release(authdata);
child = xmpp_stanza_new(conn->ctx);
if (!child)
goto err_free;
xmpp_stanza_set_name(child, "resource");
xmpp_stanza_add_child(query, child);
xmpp_stanza_release(child);
authdata = xmpp_stanza_new(conn->ctx);
if (!authdata)
goto err_free;
str = xmpp_jid_resource(conn->ctx, conn->jid);
if (str) {
xmpp_stanza_set_text(authdata, str);
xmpp_free(conn->ctx, str);
} else {
xmpp_stanza_release(authdata);
xmpp_stanza_release(iq);
xmpp_error(conn->ctx, "auth", "Cannot authenticate without resource");
xmpp_disconnect(conn);
return;
}
xmpp_stanza_add_child(child, authdata);
xmpp_stanza_release(authdata);
handler_add_id(conn, _handle_legacy, "_xmpp_auth1", NULL);
handler_add_timed(conn, _handle_missing_legacy, LEGACY_TIMEOUT, NULL);
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
return;
err_free:
xmpp_stanza_release(iq);
err:
disconnect_mem_error(conn);
}
void auth_handle_component_open(xmpp_conn_t * const conn)

View File

@@ -130,7 +130,7 @@ struct _xmpp_handlist_t {
char *name;
char *type;
};
};
} u;
};
#define MAX_DOMAIN_LEN 256
@@ -172,11 +172,16 @@ struct _xmpp_conn_t {
int tls_failed; /* set when tls fails, so we don't try again */
int sasl_support; /* if true, field is a bitfield of supported
mechanisms */
int auth_legacy_enabled;
int secured; /* set when stream is secured with TLS */
/* if server returns <bind/> or <session/> we must do them */
int bind_required;
int session_required;
int sm_support;
int sm_enabled;
uint32_t sm_handled_nr;
uint32_t sm_sent_nr;
char *lang;
char *domain;

View File

@@ -62,6 +62,8 @@ static void _handle_stream_end(char *name,
void * const userdata);
static void _handle_stream_stanza(xmpp_stanza_t *stanza,
void * const userdata);
static void _conn_sm_handle_stanza(xmpp_conn_t * const conn,
xmpp_stanza_t *stanza);
static unsigned short _conn_default_port(xmpp_conn_t * const conn,
xmpp_conn_type_t type);
static void _conn_reset(xmpp_conn_t * const conn);
@@ -73,6 +75,15 @@ static int _conn_connect(xmpp_conn_t * const conn,
xmpp_conn_handler callback,
void * const userdata);
void xmpp_send_error(xmpp_conn_t * const conn, xmpp_error_type_t const type, char * const text)
{
xmpp_stanza_t *error = xmpp_error_new(conn->ctx, type, text);
xmpp_send(conn, error);
xmpp_stanza_release(error);
}
/** Create a new Strophe connection object.
*
* @param ctx a Strophe context object
@@ -131,10 +142,15 @@ xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx)
conn->tls_trust = 0;
conn->tls_failed = 0;
conn->sasl_support = 0;
conn->auth_legacy_enabled = 0;
conn->secured = 0;
conn->bind_required = 0;
conn->session_required = 0;
conn->sm_support = 0;
conn->sm_enabled = 0;
conn->sm_handled_nr = 0;
conn->sm_sent_nr = 0;
conn->parser = parser_new(conn->ctx,
_handle_stream_start,
@@ -244,6 +260,12 @@ int xmpp_conn_release(xmpp_conn_t * const conn)
else {
ctx = conn->ctx;
if (conn->state == XMPP_STATE_CONNECTING ||
conn->state == XMPP_STATE_CONNECTED)
{
conn_disconnect(conn);
}
/* remove connection from context's connlist */
if (ctx->connlist->conn == conn) {
item = ctx->connlist;
@@ -289,7 +311,7 @@ int xmpp_conn_release(xmpp_conn_t * const conn)
while (hlitem) {
thli = hlitem;
hlitem = hlitem->next;
xmpp_free(conn->ctx, thli->id);
xmpp_free(conn->ctx, thli->u.id);
xmpp_free(conn->ctx, thli);
}
}
@@ -301,9 +323,9 @@ int xmpp_conn_release(xmpp_conn_t * const conn)
thli = hlitem;
hlitem = hlitem->next;
if (thli->ns) xmpp_free(ctx, thli->ns);
if (thli->name) xmpp_free(ctx, thli->name);
if (thli->type) xmpp_free(ctx, thli->type);
if (thli->u.ns) xmpp_free(ctx, thli->u.ns);
if (thli->u.name) xmpp_free(ctx, thli->u.name);
if (thli->u.type) xmpp_free(ctx, thli->u.type);
xmpp_free(ctx, thli);
}
@@ -704,7 +726,7 @@ void conn_parser_reset(xmpp_conn_t * const conn)
/** Initiate termination of the connection to the XMPP server.
* This function starts the disconnection sequence by sending
* </stream:stream> to the XMPP server. This function will do nothing
* if the connection state is CONNECTING or CONNECTED.
* if the connection state is different from CONNECTING or CONNECTED.
*
* @param conn a Strophe connection object
*
@@ -746,10 +768,10 @@ void xmpp_send_raw_string(xmpp_conn_t * const conn,
char *bigbuf;
va_start(ap, fmt);
len = xmpp_vsnprintf(buf, 1024, fmt, ap);
len = xmpp_vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (len >= 1024) {
if (len >= sizeof(buf)) {
/* we need more space for this data, so we allocate a big
* enough buffer and print to that */
len++; /* account for trailing \0 */
@@ -770,7 +792,6 @@ void xmpp_send_raw_string(xmpp_conn_t * const conn,
xmpp_free(conn->ctx, bigbuf);
} else {
xmpp_debug(conn->ctx, "conn", "SENT: %s", buf);
xmpp_send_raw(conn, buf, len);
}
}
@@ -835,10 +856,9 @@ void xmpp_send(xmpp_conn_t * const conn,
{
char *buf;
size_t len;
int ret;
if (conn->state == XMPP_STATE_CONNECTED) {
if ((ret = xmpp_stanza_to_text(stanza, &buf, &len)) == 0) {
if (xmpp_stanza_to_text(stanza, &buf, &len) == 0) {
xmpp_send_raw(conn, buf, len);
xmpp_debug(conn->ctx, "conn", "SENT: %s", buf);
xmpp_free(conn->ctx, buf);
@@ -913,7 +933,8 @@ long xmpp_conn_get_flags(const xmpp_conn_t * const conn)
flags = XMPP_CONN_FLAG_DISABLE_TLS * conn->tls_disabled |
XMPP_CONN_FLAG_MANDATORY_TLS * conn->tls_mandatory |
XMPP_CONN_FLAG_LEGACY_SSL * conn->tls_legacy_ssl |
XMPP_CONN_FLAG_TRUST_TLS * conn->tls_trust;
XMPP_CONN_FLAG_TRUST_TLS * conn->tls_trust |
XMPP_CONN_FLAG_LEGACY_AUTH * conn->auth_legacy_enabled;;
return flags;
}
@@ -931,6 +952,7 @@ long xmpp_conn_get_flags(const xmpp_conn_t * const conn)
* - XMPP_CONN_FLAG_MANDATORY_TLS
* - XMPP_CONN_FLAG_LEGACY_SSL
* - XMPP_CONN_FLAG_TRUST_TLS
* - XMPP_CONN_FLAG_LEGACY_AUTH
*
* @param conn a Strophe connection object
* @param flags ORed connection flags
@@ -957,6 +979,7 @@ int xmpp_conn_set_flags(xmpp_conn_t * const conn, long flags)
conn->tls_mandatory = (flags & XMPP_CONN_FLAG_MANDATORY_TLS) ? 1 : 0;
conn->tls_legacy_ssl = (flags & XMPP_CONN_FLAG_LEGACY_SSL) ? 1 : 0;
conn->tls_trust = (flags & XMPP_CONN_FLAG_TRUST_TLS) ? 1 : 0;
conn->auth_legacy_enabled = (flags & XMPP_CONN_FLAG_LEGACY_AUTH) ? 1 : 0;
return 0;
}
@@ -991,6 +1014,37 @@ int xmpp_conn_is_secured(xmpp_conn_t * const conn)
return conn->secured && !conn->tls_failed && conn->tls != NULL ? 1 : 0;
}
/**
* @return TRUE if connection is in connecting state and FALSE otherwise
*
* @ingroup Connections
*/
int xmpp_conn_is_connecting(xmpp_conn_t * const conn)
{
return conn->state == XMPP_STATE_CONNECTING ? 1 : 0;
}
/**
* @return TRUE if connection is in connected state and FALSE otherwise
*
* @ingroup Connections
*/
int xmpp_conn_is_connected(xmpp_conn_t * const conn)
{
return conn->state == XMPP_STATE_CONNECTED ? 1 : 0;
}
/**
* @return TRUE if connection is in disconnected state and FALSE otherwise
*
* @ingroup Connections
*/
int xmpp_conn_is_disconnected(xmpp_conn_t * const conn)
{
return conn->state == XMPP_STATE_DISCONNECTED ? 1 : 0;
}
/* timed handler for cleanup if normal disconnect procedure takes too long */
static int _disconnect_cleanup(xmpp_conn_t * const conn,
void * const userdata)
@@ -1168,6 +1222,36 @@ static void _handle_stream_stanza(xmpp_stanza_t *stanza,
}
handler_fire_stanza(conn, stanza);
if (conn->sm_enabled)
_conn_sm_handle_stanza(conn, stanza);
}
/* XEP-0198 stream management */
static void _conn_sm_handle_stanza(xmpp_conn_t * const conn,
xmpp_stanza_t *stanza)
{
xmpp_stanza_t *a;
const char *name;
const char *ns;
char h[11];
ns = xmpp_stanza_get_ns(stanza);
if (ns && strcmp(ns, XMPP_NS_SM) != 0)
++conn->sm_handled_nr;
else {
name = xmpp_stanza_get_name(stanza);
if (name && strcmp(name, "r") == 0) {
a = xmpp_stanza_new(conn->ctx);
if (!a)
return; /* XXX */
xmpp_stanza_set_name(a, "a");
xmpp_stanza_set_ns(a, XMPP_NS_SM);
xmpp_snprintf(h, sizeof(h), "%u", conn->sm_handled_nr);
xmpp_stanza_set_attribute(a, "h", h);
xmpp_send(conn, a);
xmpp_stanza_release(a);
}
}
}
static unsigned short _conn_default_port(xmpp_conn_t * const conn,
@@ -1202,6 +1286,9 @@ static void _conn_reset(xmpp_conn_t * const conn)
xmpp_free(ctx, tsq->data);
xmpp_free(ctx, tsq);
}
conn->send_queue_head = NULL;
conn->send_queue_tail = NULL;
conn->send_queue_len = 0;
if (conn->stream_error) {
xmpp_stanza_release(conn->stream_error->stanza);
@@ -1223,6 +1310,8 @@ static void _conn_reset(xmpp_conn_t * const conn)
conn->error = 0;
conn->tls_support = 0;
conn->sm_support = 0;
conn->sm_enabled = 0;
conn->bind_required = 0;
conn->session_required = 0;

View File

@@ -61,7 +61,7 @@ static char *digest_to_string_alloc(xmpp_ctx_t *ctx, const uint8_t *digest)
return s;
}
/** Compute SHA1 message digest
/** Compute SHA1 message digest.
* Returns an allocated string which represents SHA1 message digest in
* hexadecimal notation. The string must be freed with xmpp_free().
*
@@ -81,7 +81,7 @@ char *xmpp_sha1(xmpp_ctx_t *ctx, const unsigned char *data, size_t len)
return digest_to_string_alloc(ctx, digest);
}
/** Compute SHA1 message digest
/** Compute SHA1 message digest.
* Stores digest in user's buffer which must be at least XMPP_SHA1_DIGEST_SIZE
* bytes long.
*
@@ -97,7 +97,7 @@ void xmpp_sha1_digest(const unsigned char *data, size_t len,
crypto_SHA1((const uint8_t *)data, len, digest);
}
/** Create new SHA1 object
/** Create new SHA1 object.
* SHA1 object is used to compute SHA1 digest of a buffer that is split
* in multiple chunks or provided in stream mode. A single buffer can be
* processed by short functions xmpp_sha1() and xmpp_sha1_digest().
@@ -111,7 +111,7 @@ void xmpp_sha1_digest(const unsigned char *data, size_t len,
* xmpp_sha1_free(sha1);
* @endcode
*
* @param ctx a Strophe context onject
* @param ctx a Strophe context object
*
* @return new SHA1 object
*
@@ -130,7 +130,7 @@ xmpp_sha1_t *xmpp_sha1_new(xmpp_ctx_t *ctx)
return sha1;
}
/** Destroy SHA1 object
/** Destroy SHA1 object.
*
* @param sha1 a SHA1 object
*
@@ -141,7 +141,7 @@ void xmpp_sha1_free(xmpp_sha1_t *sha1)
xmpp_free(sha1->xmpp_ctx, sha1);
}
/** Update SHA1 context with the next portion of data
/** Update SHA1 context with the next portion of data.
* Can be called repeatedly.
*
* @param sha1 a SHA1 object
@@ -155,7 +155,7 @@ void xmpp_sha1_update(xmpp_sha1_t *sha1, const unsigned char *data, size_t len)
crypto_SHA1_Update(&sha1->ctx, data, len);
}
/** Finish SHA1 computation
/** Finish SHA1 computation.
* Don't call xmpp_sha1_update() after this function. Retrieve resulting
* message digest with xmpp_sha1_to_string() or xmpp_sha1_to_digest().
*
@@ -168,7 +168,7 @@ void xmpp_sha1_final(xmpp_sha1_t *sha1)
crypto_SHA1_Final(&sha1->ctx, sha1->digest);
}
/** Return message digest rendered as a string
/** Return message digest rendered as a string.
* Stores the string to a user's buffer and returns the buffer. Call this
* function after xmpp_sha1_final().
*
@@ -185,7 +185,7 @@ char *xmpp_sha1_to_string(xmpp_sha1_t *sha1, char *s, size_t slen)
return digest_to_string(sha1->digest, s, slen);
}
/** Return message digest rendered as a string
/** Return message digest rendered as a string.
* Returns an allocated string. Free the string using the Strophe context
* which is passed to xmpp_sha1_new(). Call this function after
* xmpp_sha1_final().
@@ -201,7 +201,7 @@ char *xmpp_sha1_to_string_alloc(xmpp_sha1_t *sha1)
return digest_to_string_alloc(sha1->xmpp_ctx, sha1->digest);
}
/** Stores message digest to a user's buffer
/** Stores message digest to a user's buffer.
*
* @param sha1 a SHA1 object
* @param digest output buffer of XMPP_SHA1_DIGEST_SIZE bytes
@@ -419,7 +419,7 @@ _base64_error:
*outlen = 0;
}
/** Base64 encoding routine
/** Base64 encoding routine.
* Returns an allocated string which must be freed with xmpp_free().
*
* @param ctx a Strophe context
@@ -435,7 +435,7 @@ char *xmpp_base64_encode(xmpp_ctx_t *ctx, const unsigned char *data, size_t len)
return base64_encode(ctx, data, len);
}
/** Base64 decoding routine
/** Base64 decoding routine.
* Returns an allocated string which must be freed with xmpp_free(). User
* calls this function when the result must be a string. When decoded buffer
* contains '\0' NULL is returned.
@@ -471,7 +471,7 @@ char *xmpp_base64_decode_str(xmpp_ctx_t *ctx, const char *base64, size_t len)
return (char *)buf;
}
/** Base64 decoding routine
/** Base64 decoding routine.
* Returns an allocated buffer which must be freed with xmpp_free().
*
* @param ctx a Strophe context

View File

@@ -47,10 +47,11 @@
#include "strophe.h"
#include "common.h"
#include "resolver.h"
#include "util.h"
/* Workaround for visual studio without va_copy support. */
#if defined(_MSC_VER) && _MSC_VER < 1800
#if defined(_MSC_VER) && _MSC_VER < 1800 || defined(__HAIKU__)
#define va_copy(d,s) ((d) = (s))
#endif
@@ -63,6 +64,7 @@
void xmpp_initialize(void)
{
sock_initialize();
resolver_initialize();
tls_initialize();
}
@@ -73,6 +75,7 @@
void xmpp_shutdown(void)
{
tls_shutdown();
resolver_shutdown();
sock_shutdown();
}

View File

@@ -41,16 +41,25 @@
#define _sleep(x) usleep((x) * 1000)
#else
#include <winsock2.h>
#ifndef ETIMEDOUT
#define ETIMEDOUT WSAETIMEDOUT
#endif
#ifndef ECONNRESET
#define ECONNRESET WSAECONNRESET
#endif
#ifndef ECONNABORTED
#define ECONNABORTED WSAECONNABORTED
#endif
#define _sleep(x) Sleep(x)
#endif
#include <strophe.h>
#include "strophe.h"
#include "common.h"
#include "parser.h"
/** Max buffer size for receiving messages. */
#define STROPE_MESSAGE_BUFFER_SIZE 4096
/** Run the event loop once.
* This function will run send any data that has been queued by
* xmpp_send and related functions and run through the Strophe even
@@ -74,7 +83,7 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
struct timeval tv;
xmpp_send_queue_t *sq, *tsq;
int towrite;
char buf[4096];
char buf[STROPE_MESSAGE_BUFFER_SIZE];
uint64_t next;
uint64_t usec;
int tls_read_bytes = 0;
@@ -254,18 +263,16 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
case XMPP_STATE_CONNECTED:
if (FD_ISSET(conn->sock, &rfds) || (conn->tls && tls_pending(conn->tls))) {
if (conn->tls) {
ret = tls_read(conn->tls, buf, 4096);
ret = tls_read(conn->tls, buf, STROPE_MESSAGE_BUFFER_SIZE);
} else {
ret = sock_read(conn->sock, buf, 4096);
ret = sock_read(conn->sock, buf, STROPE_MESSAGE_BUFFER_SIZE);
}
if (ret > 0) {
ret = parser_feed(conn->parser, buf, ret);
if (!ret) {
/* parse error, we need to shut down */
/* FIXME */
xmpp_debug(ctx, "xmpp", "parse error, disconnecting");
conn_disconnect(conn);
xmpp_debug(ctx, "xmpp", "parse error [%s]", buf);
xmpp_send_error(conn, XMPP_SE_INVALID_XML, "parse error");
}
} else {
if (conn->tls) {

View File

@@ -90,7 +90,7 @@ void handler_fire_stanza(xmpp_conn_t * const conn,
/* replace old value */
hash_add(conn->id_handlers, id, head);
}
xmpp_free(conn->ctx, item->id);
xmpp_free(conn->ctx, item->u.id);
xmpp_free(conn->ctx, item);
}
item = next;
@@ -116,10 +116,10 @@ void handler_fire_stanza(xmpp_conn_t * const conn,
}
next = item->next;
if ((!item->ns || (ns && strcmp(ns, item->ns) == 0) ||
xmpp_stanza_get_child_by_ns(stanza, item->ns)) &&
(!item->name || (name && strcmp(name, item->name) == 0)) &&
(!item->type || (type && strcmp(type, item->type) == 0))) {
if ((!item->u.ns || (ns && strcmp(ns, item->u.ns) == 0) ||
xmpp_stanza_get_child_by_ns(stanza, item->u.ns)) &&
(!item->u.name || (name && strcmp(name, item->u.name) == 0)) &&
(!item->u.type || (type && strcmp(type, item->u.type) == 0))) {
ret = ((xmpp_handler)(item->handler))(conn, stanza, item->userdata);
/* list may be changed during execution of a handler */
@@ -127,9 +127,9 @@ void handler_fire_stanza(xmpp_conn_t * const conn,
if (!ret) {
/* handler is one-shot, so delete it */
_handler_item_remove(&conn->handlers, item);
if (item->ns) xmpp_free(conn->ctx, item->ns);
if (item->name) xmpp_free(conn->ctx, item->name);
if (item->type) xmpp_free(conn->ctx, item->type);
if (item->u.ns) xmpp_free(conn->ctx, item->u.ns);
if (item->u.name) xmpp_free(conn->ctx, item->u.name);
if (item->u.type) xmpp_free(conn->ctx, item->u.type);
xmpp_free(conn->ctx, item);
}
}
@@ -178,10 +178,10 @@ uint64_t handler_fire_timed(xmpp_ctx_t * const ctx)
next = item->next;
timestamp = time_stamp();
elapsed = time_elapsed(item->last_stamp, timestamp);
if (elapsed >= item->period) {
elapsed = time_elapsed(item->u.last_stamp, timestamp);
if (elapsed >= item->u.period) {
/* fire! */
item->last_stamp = timestamp;
item->u.last_stamp = timestamp;
ret = ((xmpp_timed_handler)item->handler)(conn, item->userdata);
/* list may be changed during execution of a handler */
next = item->next;
@@ -190,8 +190,8 @@ uint64_t handler_fire_timed(xmpp_ctx_t * const ctx)
_handler_item_remove(&conn->timed_handlers, item);
xmpp_free(conn->ctx, item);
}
} else if (min > (item->period - elapsed))
min = item->period - elapsed;
} else if (min > (item->u.period - elapsed))
min = item->u.period - elapsed;
item = next;
}
@@ -215,7 +215,7 @@ void handler_reset_timed(xmpp_conn_t *conn, int user_only)
handitem = conn->timed_handlers;
while (handitem) {
if ((user_only && handitem->user_handler) || !user_only)
handitem->last_stamp = time_stamp();
handitem->u.last_stamp = time_stamp();
handitem = handitem->next;
}
@@ -248,8 +248,8 @@ static void _timed_handler_add(xmpp_conn_t * const conn,
item->enabled = 0;
item->next = NULL;
item->period = period;
item->last_stamp = time_stamp();
item->u.period = period;
item->u.last_stamp = time_stamp();
/* append item to list */
if (!conn->timed_handlers)
@@ -322,8 +322,8 @@ static void _id_handler_add(xmpp_conn_t * const conn,
item->enabled = 0;
item->next = NULL;
item->id = xmpp_strdup(conn->ctx, id);
if (!item->id) {
item->u.id = xmpp_strdup(conn->ctx, id);
if (!item->u.id) {
xmpp_free(conn->ctx, item);
return;
}
@@ -368,7 +368,7 @@ void xmpp_id_handler_delete(xmpp_conn_t * const conn,
hash_add(conn->id_handlers, id, next);
}
xmpp_free(conn->ctx, item->id);
xmpp_free(conn->ctx, item->u.id);
xmpp_free(conn->ctx, item);
item = next;
} else {
@@ -410,31 +410,31 @@ static void _handler_add(xmpp_conn_t * const conn,
item->next = NULL;
if (ns) {
item->ns = xmpp_strdup(conn->ctx, ns);
if (!item->ns) {
item->u.ns = xmpp_strdup(conn->ctx, ns);
if (!item->u.ns) {
xmpp_free(conn->ctx, item);
return;
}
} else
item->ns = NULL;
item->u.ns = NULL;
if (name) {
item->name = xmpp_strdup(conn->ctx, name);
if (!item->name) {
if (item->ns) xmpp_free(conn->ctx, item->ns);
item->u.name = xmpp_strdup(conn->ctx, name);
if (!item->u.name) {
if (item->u.ns) xmpp_free(conn->ctx, item->u.ns);
xmpp_free(conn->ctx, item);
return;
}
} else
item->name = NULL;
item->u.name = NULL;
if (type) {
item->type = xmpp_strdup(conn->ctx, type);
if (!item->type) {
if (item->ns) xmpp_free(conn->ctx, item->ns);
if (item->name) xmpp_free(conn->ctx, item->name);
item->u.type = xmpp_strdup(conn->ctx, type);
if (!item->u.type) {
if (item->u.ns) xmpp_free(conn->ctx, item->u.ns);
if (item->u.name) xmpp_free(conn->ctx, item->u.name);
xmpp_free(conn->ctx, item);
}
} else
item->type = NULL;
item->u.type = NULL;
/* append to list */
if (!conn->handlers)
@@ -470,9 +470,9 @@ void xmpp_handler_delete(xmpp_conn_t * const conn,
else
conn->handlers = item->next;
if (item->ns) xmpp_free(conn->ctx, item->ns);
if (item->name) xmpp_free(conn->ctx, item->name);
if (item->type) xmpp_free(conn->ctx, item->type);
if (item->u.ns) xmpp_free(conn->ctx, item->u.ns);
if (item->u.name) xmpp_free(conn->ctx, item->u.name);
if (item->u.type) xmpp_free(conn->ctx, item->u.type);
xmpp_free(conn->ctx, item);
item = prev ? prev->next : conn->handlers;
} else {
@@ -636,9 +636,9 @@ void handler_system_delete_all(xmpp_conn_t *conn)
if (!item->user_handler) {
next = item->next;
_handler_item_remove(&conn->handlers, item);
if (item->ns) xmpp_free(conn->ctx, item->ns);
if (item->name) xmpp_free(conn->ctx, item->name);
if (item->type) xmpp_free(conn->ctx, item->type);
if (item->u.ns) xmpp_free(conn->ctx, item->u.ns);
if (item->u.name) xmpp_free(conn->ctx, item->u.name);
if (item->u.type) xmpp_free(conn->ctx, item->u.type);
xmpp_free(conn->ctx, item);
item = next;
} else
@@ -665,7 +665,7 @@ void handler_system_delete_all(xmpp_conn_t *conn)
if (!item->user_handler) {
next = item->next;
_handler_item_remove(&head, item);
xmpp_free(conn->ctx, item->id);
xmpp_free(conn->ctx, item->u.id);
xmpp_free(conn->ctx, item);
item = next;
} else

View File

@@ -163,6 +163,8 @@ int hash_add(hash_t *table, const char * const key, void *data)
entry->next = table->entries[table_index];
table->entries[table_index] = entry;
table->num_keys++;
} else {
if (table->free) table->free(ctx, entry->value);
}
entry->value = data;
@@ -241,7 +243,7 @@ void hash_iter_release(hash_iterator_t *iter)
iter->ref--;
if (iter->ref <= 0) {
if (iter->ref == 0) { // ref is unsigned!!!
hash_release(iter->table);
xmpp_free(ctx, iter);
}

View File

@@ -43,8 +43,7 @@
(cp)[3] = ((value) >> 24) & 0xFF; \
} while(0)
static void MD5Transform(uint32_t buf[4], const unsigned char inext[64],
struct MD5Context *ctx);
static void MD5Transform(uint32_t buf[4], const unsigned char inext[64]);
/*
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
@@ -91,7 +90,7 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, uint32_t len)
return;
}
memcpy(p, buf, t);
MD5Transform(ctx->buf, ctx->in, ctx);
MD5Transform(ctx->buf, ctx->in);
buf += t;
len -= t;
}
@@ -99,7 +98,7 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, uint32_t len)
while (len >= 64) {
memcpy(ctx->in, buf, 64);
MD5Transform(ctx->buf, ctx->in, ctx);
MD5Transform(ctx->buf, ctx->in);
buf += 64;
len -= 64;
}
@@ -133,7 +132,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
if (count < 8) {
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
MD5Transform(ctx->buf, ctx->in, ctx);
MD5Transform(ctx->buf, ctx->in);
/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
@@ -146,7 +145,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
PUT_32BIT_LSB_FIRST(ctx->in + 56, ctx->bits[0]);
PUT_32BIT_LSB_FIRST(ctx->in + 60, ctx->bits[1]);
MD5Transform(ctx->buf, ctx->in, ctx);
MD5Transform(ctx->buf, ctx->in);
PUT_32BIT_LSB_FIRST(digest, ctx->buf[0]);
PUT_32BIT_LSB_FIRST(digest + 4, ctx->buf[1]);
PUT_32BIT_LSB_FIRST(digest + 8, ctx->buf[2]);
@@ -180,8 +179,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
static void MD5Transform(uint32_t buf[4], const unsigned char inext[64],
struct MD5Context *ctx)
static void MD5Transform(uint32_t buf[4], const unsigned char inext[64])
{
register uint32_t a, b, c, d, i;
uint32_t in[16];

View File

@@ -19,13 +19,10 @@
#include <expat.h>
#include <strophe.h>
#include "strophe.h"
#include "common.h"
#include "parser.h"
/* Use the Unit Separator to delimit namespace and name in our XML*/
#define NAMESPACE_SEP ('\x1F')
/* Allocate inner text by this number bytes more. Expat splits string
* "new\nline" into 3 strings: "new" "\n" "line". Expecting this pattern,
* we can leave few bytes in the inner_text for "\n". It should reduce
@@ -48,6 +45,46 @@ struct _parser_t {
int inner_text_used;
};
/* Use the Unit Separator to delimit namespace and name in our XML */
const XML_Char namespace_sep = '\x1F';
/*
* Cached strophe ctx. It is used for memory suite.
* Note, expat doesn't support userdata in memory suite, therefore,
* we can support only one strophe context. If user creates more than one
* context, this module will fallback to default library allocator for all
* contexts other than mem_ctx.
*/
static xmpp_ctx_t *mem_ctx = NULL;
static void *parser_mem_malloc(size_t size)
{
if (mem_ctx != NULL)
return xmpp_alloc(mem_ctx, size);
else
return NULL;
}
static void *parser_mem_realloc(void *ptr, size_t size)
{
if (mem_ctx != NULL)
return xmpp_realloc(mem_ctx, ptr, size);
else
return NULL;
}
static void parser_mem_free(void *ptr)
{
if (mem_ctx != NULL)
xmpp_free(mem_ctx, ptr);
}
static const XML_Memory_Handling_Suite parser_mem_suite = {
.malloc_fcn = &parser_mem_malloc,
.realloc_fcn = &parser_mem_realloc,
.free_fcn = &parser_mem_free,
};
/* return allocated string with the name from a delimited
* namespace/name string */
static char *_xml_name(xmpp_ctx_t *ctx, const char *nsname)
@@ -56,7 +93,7 @@ static char *_xml_name(xmpp_ctx_t *ctx, const char *nsname)
const char *c;
size_t len;
c = strchr(nsname, NAMESPACE_SEP);
c = strchr(nsname, namespace_sep);
if (c == NULL) return xmpp_strdup(ctx, nsname);
c++;
@@ -76,7 +113,7 @@ static char *_xml_namespace(xmpp_ctx_t *ctx, const char *nsname)
char *result = NULL;
const char *c;
c = strchr(nsname, NAMESPACE_SEP);
c = strchr(nsname, namespace_sep);
if (c != NULL) {
result = xmpp_alloc(ctx, (c-nsname) + 1);
if (result != NULL) {
@@ -137,7 +174,7 @@ static void _start_element(void *userdata,
if (parser->depth == 0) {
/* notify the owner */
if (parser->startcb)
parser->startcb((char *)name, (char **)attrs,
parser->startcb(name, (char **)attrs,
parser->userdata);
} else {
/* build stanzas at depth 1 */
@@ -273,23 +310,38 @@ void parser_free(parser_t *parser)
/* shuts down and restarts XML parser. true on success */
int parser_reset(parser_t *parser)
{
if (parser->expat)
XML_ParserFree(parser->expat);
XML_Bool ret;
const XML_Memory_Handling_Suite *mem = NULL;
if (parser->stanza)
xmpp_stanza_release(parser->stanza);
if (parser->expat) {
ret = XML_ParserReset(parser->expat, NULL);
if (ret != XML_TRUE) {
XML_ParserFree(parser->expat);
parser->expat = NULL;
}
} else {
if (mem_ctx == NULL)
mem_ctx = parser->ctx;
if (parser->ctx == mem_ctx)
mem = &parser_mem_suite;
parser->expat = XML_ParserCreate_MM(NULL, mem, &namespace_sep);
}
parser->expat = XML_ParserCreateNS(NULL, NAMESPACE_SEP);
if (!parser->expat) return 0;
parser->depth = 0;
parser->stanza = NULL;
if (parser->stanza) {
xmpp_stanza_release(parser->stanza);
parser->stanza = NULL;
}
if (parser->inner_text) {
xmpp_free (parser->ctx, parser->inner_text);
xmpp_free(parser->ctx, parser->inner_text);
parser->inner_text = NULL;
}
if (!parser->expat)
return 0;
parser->depth = 0;
XML_SetUserData(parser->expat, parser);
XML_SetElementHandler(parser->expat, _start_element, _end_element);
XML_SetCharacterDataHandler(parser->expat, _characters);

View File

@@ -20,7 +20,7 @@
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <strophe.h>
#include "strophe.h"
#include "common.h"
#include "parser.h"
@@ -249,6 +249,8 @@ void parser_free(parser_t *parser)
{
if (parser->xmlctx)
xmlFreeParserCtxt(parser->xmlctx);
if (parser->stanza)
xmpp_stanza_release(parser->stanza);
xmpp_free(parser->ctx, parser);
}
@@ -257,18 +259,16 @@ int parser_reset(parser_t *parser)
{
if (parser->xmlctx)
xmlFreeParserCtxt(parser->xmlctx);
if (parser->stanza)
xmpp_stanza_release(parser->stanza);
parser->stanza = NULL;
parser->depth = 0;
parser->xmlctx = xmlCreatePushParserCtxt(&parser->handlers,
parser, NULL, 0, NULL);
if (!parser->xmlctx) return 0;
parser->depth = 0;
parser->stanza = NULL;
return 1;
return parser->xmlctx ? 1 : 0;
}
/* feed a chunk of data to the parser */

View File

@@ -127,9 +127,11 @@ static void Hash_DRBG_Instantiate(Hash_DRBG_CTX *ctx,
assert(entropy_input_len <= ENTROPY_MAX);
assert(nonce_len <= NONCE_MAX);
assert(nonce != NULL || nonce_len == 0);
memcpy(seed_material, entropy_input, entropy_input_len);
memcpy(seed_material + entropy_input_len, nonce, nonce_len);
if (nonce != NULL)
memcpy(seed_material + entropy_input_len, nonce, nonce_len);
Hash_df(seed_material, entropy_input_len + nonce_len, seed, seedlen);
seed0[0] = 0;
@@ -287,25 +289,32 @@ int xmpp_rand(xmpp_rand_t *rand)
return result;
}
static void rand_byte2hex(unsigned char byte, char *hex)
{
static const char hex_tbl[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
hex[0] = hex_tbl[(byte >> 4) & 0x0f];
hex[1] = hex_tbl[byte & 0x0f];
}
void xmpp_rand_nonce(xmpp_rand_t *rand, char *output, size_t len)
{
size_t i;
size_t rand_len = len / 2;
#ifndef _MSC_VER
unsigned char rand_buf[rand_len];
#else
unsigned char *rand_buf = (unsigned char *)_alloca(rand_len);
#endif
const size_t rand_len = len / 2;
/* current implementation returns printable HEX representation of
* a random buffer, however base64 encoding can be used instead;
* the only problem is that base64_encode() allocates memory and
* as result can fail.
/*
* We don't want to use any allocation here, because this function
* can't fail. Also we want to avoid VLA.
* Current implementation uses half of the output buffer for random buffer
* generation and then converts it to HEX representation.
*/
xmpp_rand_bytes(rand, rand_buf, rand_len);
for (i = 0; i < rand_len; ++i) {
xmpp_snprintf(output + i * 2, len, "%02x", rand_buf[i]);
len -= 2;
if (rand_len > 0) {
xmpp_rand_bytes(rand, (unsigned char *)output, rand_len);
for (i = rand_len; i > 0; --i)
rand_byte2hex(output[i - 1], &output[(i - 1) * 2]);
}
if (len > 0)
output[len - 1] = '\0';
}

View File

@@ -36,7 +36,7 @@ xmpp_rand_t *xmpp_rand_new(xmpp_ctx_t *ctx);
*/
void xmpp_rand_free(xmpp_ctx_t *ctx, xmpp_rand_t *rand);
/** Generate random integer
/** Generate random integer.
* Analogue of rand(3).
*
* @ingroup Random

View File

@@ -1,4 +1,4 @@
/* resolver.h
/* resolver.c
* strophe XMPP client library -- DNS resolver
*
* Copyright (C) 2015 Dmitry Podgorny <pasis.ua@gmail.com>
@@ -13,11 +13,23 @@
* DNS resolver.
*/
#ifndef _WIN32
#if !defined(_WIN32) && !defined(HAVE_CARES)
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h> /* res_query */
#endif /* _WIN32 */
#endif /* _WIN32 && HAVE_CARES */
#ifdef HAVE_CARES
#include <ares.h>
/* for select(2) */
#ifdef _WIN32
#include <winsock2.h>
#else /* _WIN32 */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#endif /* !_WIN32 */
#endif /* HAVE_CARES */
#include <string.h> /* strncpy */
@@ -31,6 +43,189 @@
#define MESSAGE_T_SRV 33
#define MESSAGE_C_IN 1
/*******************************************************************************
* Forward declarations.
******************************************************************************/
#ifdef HAVE_CARES
static int resolver_ares_srv_lookup_buf(xmpp_ctx_t *ctx,
const unsigned char *buf,
size_t len,
resolver_srv_rr_t **srv_rr_list);
static int resolver_ares_srv_lookup(xmpp_ctx_t *ctx, const char *fulldomain,
resolver_srv_rr_t **srv_rr_list);
#endif /* HAVE_CARES */
#ifndef HAVE_CARES
static int resolver_raw_srv_lookup_buf(xmpp_ctx_t *ctx,
const unsigned char *buf,
size_t len,
resolver_srv_rr_t **srv_rr_list);
#endif /* !HAVE_CARES */
#ifdef _WIN32
static int resolver_win32_srv_lookup(xmpp_ctx_t *ctx, const char *fulldomain,
resolver_srv_rr_t **srv_rr_list);
static int resolver_win32_srv_query(const char *fulldomain,
unsigned char *buf, size_t len);
#endif /* _WIN32 */
/*******************************************************************************
* Implementation.
******************************************************************************/
void resolver_initialize(void)
{
#ifdef HAVE_CARES
ares_library_init(ARES_LIB_INIT_ALL);
#endif
}
void resolver_shutdown(void)
{
#ifdef HAVE_CARES
ares_library_cleanup();
#endif
}
static void resolver_srv_list_sort(resolver_srv_rr_t **srv_rr_list)
{
resolver_srv_rr_t *rr_head;
resolver_srv_rr_t *rr_current;
resolver_srv_rr_t *rr_next;
resolver_srv_rr_t *rr_prev;
int swap;
rr_head = *srv_rr_list;
if ((rr_head == NULL) || (rr_head->next == NULL)) {
/* Empty or single record list */
return;
}
do {
rr_prev = NULL;
rr_current = rr_head;
rr_next = rr_head->next;
swap = 0;
while (rr_next != NULL) {
/*
* RFC2052: A client MUST attempt to contact the target host
* with the lowest-numbered priority it can reach.
* RFC2052: When selecting a target host among the
* those that have the same priority, the chance of trying
* this one first SHOULD be proportional to its weight.
*/
if ((rr_current->priority > rr_next->priority) ||
(rr_current->priority == rr_next->priority &&
rr_current->weight < rr_next->weight))
{
/* Swap node */
swap = 1;
if (rr_prev != NULL) {
rr_prev->next = rr_next;
} else {
/* Swap head node */
rr_head = rr_next;
}
rr_current->next = rr_next->next;
rr_next->next = rr_current;
rr_prev = rr_next;
rr_next = rr_current->next;
} else {
/* Next node */
rr_prev = rr_current;
rr_current = rr_next;
rr_next = rr_next->next;
}
}
} while (swap != 0);
*srv_rr_list = rr_head;
}
int resolver_srv_lookup_buf(xmpp_ctx_t *ctx, const unsigned char *buf,
size_t len, resolver_srv_rr_t **srv_rr_list)
{
int set;
#ifdef HAVE_CARES
set = resolver_ares_srv_lookup_buf(ctx, buf, len, srv_rr_list);
#else
set = resolver_raw_srv_lookup_buf(ctx, buf, len, srv_rr_list);
#endif
resolver_srv_list_sort(srv_rr_list);
return set;
}
int resolver_srv_lookup(xmpp_ctx_t *ctx, const char *service, const char *proto,
const char *domain, resolver_srv_rr_t **srv_rr_list)
{
#define RESOLVER_BUF_MAX 65536
unsigned char *buf;
char fulldomain[2048];
int len;
int set = XMPP_DOMAIN_NOT_FOUND;
(void)buf;
(void)len;
xmpp_snprintf(fulldomain, sizeof(fulldomain),
"_%s._%s.%s", service, proto, domain);
*srv_rr_list = NULL;
#ifdef HAVE_CARES
set = resolver_ares_srv_lookup(ctx, fulldomain, srv_rr_list);
#else /* HAVE_CARES */
#ifdef _WIN32
set = resolver_win32_srv_lookup(ctx, fulldomain, srv_rr_list);
if (set == XMPP_DOMAIN_FOUND)
return set;
#endif /* _WIN32 */
buf = xmpp_alloc(ctx, RESOLVER_BUF_MAX);
if (buf == NULL)
return XMPP_DOMAIN_NOT_FOUND;
#ifdef _WIN32
len = resolver_win32_srv_query(fulldomain, buf, RESOLVER_BUF_MAX);
#else /* _WIN32 */
len = res_query(fulldomain, MESSAGE_C_IN, MESSAGE_T_SRV, buf,
RESOLVER_BUF_MAX);
#endif /* _WIN32 */
if (len > 0)
set = resolver_srv_lookup_buf(ctx, buf, (size_t)len, srv_rr_list);
xmpp_free(ctx, buf);
#endif /* HAVE_CARES */
return set;
}
void resolver_srv_free(xmpp_ctx_t *ctx, resolver_srv_rr_t *srv_rr_list)
{
resolver_srv_rr_t *rr;
while (srv_rr_list != NULL) {
rr = srv_rr_list->next;
xmpp_free(ctx, srv_rr_list);
srv_rr_list = rr;
}
}
#ifndef HAVE_CARES
/*******************************************************************************
* Resolver raw implementation.
*
* This code is common for both unix and win32.
******************************************************************************/
struct message_header {
uint16_t id;
uint8_t octet2;
@@ -41,13 +236,6 @@ struct message_header {
uint16_t arcount;
};
#ifdef _WIN32
static int resolver_win32_srv_lookup(xmpp_ctx_t *ctx, const char *fulldomain,
resolver_srv_rr_t **srv_rr_list);
static int resolver_win32_srv_query(const char *fulldomain,
unsigned char *buf, size_t len);
#endif /* _WIN32 */
/* the same as ntohs(), but receives pointer to the value */
static uint16_t xmpp_ntohs_ptr(const void *ptr)
{
@@ -80,7 +268,7 @@ static size_t message_name_append_safe(char *name, size_t name_len,
copy_len = name_max > name_len ? name_max - name_len : 0;
copy_len = xmpp_min(tail_len, copy_len);
if (copy_len > 0)
strncpy(&name[name_len], tail, copy_len);
memcpy(&name[name_len], tail, copy_len);
return name_len + tail_len;
}
@@ -160,63 +348,6 @@ static unsigned message_name_len(const unsigned char *buf, size_t buf_len,
return message_name_get(buf, buf_len, buf_offset, NULL, SIZE_MAX);
}
static void resolver_srv_list_sort(resolver_srv_rr_t **srv_rr_list)
{
resolver_srv_rr_t * rr_head;
resolver_srv_rr_t * rr_current;
resolver_srv_rr_t * rr_next;
resolver_srv_rr_t * rr_prev;
int swap;
rr_head = *srv_rr_list;
if ((rr_head == NULL) || (rr_head->next == NULL)) {
/* Empty or single record list */
return;
}
do {
rr_prev = NULL;
rr_current = rr_head;
rr_next = rr_head->next;
swap = 0;
while (rr_next != NULL) {
/*
* RFC2052: A client MUST attempt to contact the target host
* with the lowest-numbered priority it can reach.
* RFC2052: When selecting a target host among the
* those that have the same priority, the chance of trying
* this one first SHOULD be proportional to its weight.
*/
if ((rr_current->priority > rr_next->priority) ||
(rr_current->priority == rr_next->priority &&
rr_current->weight < rr_next->weight))
{
/* Swap node */
swap = 1;
if (rr_prev != NULL) {
rr_prev->next = rr_next;
} else {
/* Swap head node */
rr_head = rr_next;
}
rr_current->next = rr_next->next;
rr_next->next = rr_current;
rr_prev = rr_next;
rr_next = rr_current->next;
} else {
/* Next node */
rr_prev = rr_current;
rr_current = rr_next;
rr_next = rr_next->next;
}
}
} while (swap != 0);
*srv_rr_list = rr_head;
}
#define BUF_OVERFLOW_CHECK(ptr, len) do { \
if ((ptr) >= (len)) { \
if (*srv_rr_list != NULL) \
@@ -226,8 +357,10 @@ static void resolver_srv_list_sort(resolver_srv_rr_t **srv_rr_list)
} \
} while (0)
int resolver_srv_lookup_buf(xmpp_ctx_t *ctx, const unsigned char *buf,
size_t len, resolver_srv_rr_t **srv_rr_list)
static int resolver_raw_srv_lookup_buf(xmpp_ctx_t *ctx,
const unsigned char *buf,
size_t len,
resolver_srv_rr_t **srv_rr_list)
{
unsigned i;
unsigned j;
@@ -292,52 +425,110 @@ int resolver_srv_lookup_buf(xmpp_ctx_t *ctx, const unsigned char *buf,
}
j += rdlength;
}
resolver_srv_list_sort(srv_rr_list);
return *srv_rr_list != NULL ? XMPP_DOMAIN_FOUND : XMPP_DOMAIN_NOT_FOUND;
}
int resolver_srv_lookup(xmpp_ctx_t *ctx, const char *service, const char *proto,
const char *domain, resolver_srv_rr_t **srv_rr_list)
{
char fulldomain[2048];
unsigned char buf[65535];
int len;
int set = XMPP_DOMAIN_NOT_FOUND;
#endif /* !HAVE_CARES */
xmpp_snprintf(fulldomain, sizeof(fulldomain),
"_%s._%s.%s", service, proto, domain);
#ifdef HAVE_CARES
/*******************************************************************************
* Resolver implementation using c-ares library.
******************************************************************************/
struct resolver_ares_ctx {
xmpp_ctx_t *ctx;
int result;
resolver_srv_rr_t *srv_rr_list;
};
static int resolver_ares_srv_lookup_buf(xmpp_ctx_t *ctx,
const unsigned char *buf,
size_t len,
resolver_srv_rr_t **srv_rr_list)
{
struct ares_srv_reply *srv;
struct ares_srv_reply *item;
resolver_srv_rr_t *rr;
int rc;
*srv_rr_list = NULL;
#ifdef _WIN32
set = resolver_win32_srv_lookup(ctx, fulldomain, srv_rr_list);
if (set == XMPP_DOMAIN_FOUND)
return set;
len = resolver_win32_srv_query(fulldomain, buf, sizeof(buf));
#else /* _WIN32 */
len = res_query(fulldomain, MESSAGE_C_IN, MESSAGE_T_SRV, buf, sizeof(buf));
#endif /* _WIN32 */
rc = ares_parse_srv_reply(buf, len, &srv);
if (rc != ARES_SUCCESS)
return XMPP_DOMAIN_NOT_FOUND;
if (len > 0)
set = resolver_srv_lookup_buf(ctx, buf, (size_t)len, srv_rr_list);
return set;
}
void resolver_srv_free(xmpp_ctx_t *ctx, resolver_srv_rr_t *srv_rr_list)
{
resolver_srv_rr_t *rr;
while (srv_rr_list != NULL) {
rr = srv_rr_list->next;
xmpp_free(ctx, srv_rr_list);
srv_rr_list = rr;
item = srv;
while (item != NULL) {
rr = xmpp_alloc(ctx, sizeof(*rr));
if (rr == NULL)
break;
rr->next = *srv_rr_list;
rr->priority = item->priority;
rr->weight = item->weight;
rr->port = item->port;
strncpy(rr->target, item->host, sizeof(rr->target) - 1);
rr->target[sizeof(rr->target) - 1] = '\0';
*srv_rr_list = rr;
item = item->next;
}
ares_free_data(srv);
return *srv_rr_list == NULL ? XMPP_DOMAIN_NOT_FOUND : XMPP_DOMAIN_FOUND;
}
#ifdef _WIN32
static void ares_srv_lookup_callback(void *arg, int status, int timeouts,
unsigned char *buf, int len)
{
struct resolver_ares_ctx *actx = arg;
if (status != ARES_SUCCESS)
actx->result = XMPP_DOMAIN_NOT_FOUND;
else
actx->result = resolver_ares_srv_lookup_buf(actx->ctx, buf, len,
&actx->srv_rr_list);
}
static int resolver_ares_srv_lookup(xmpp_ctx_t *ctx, const char *fulldomain,
resolver_srv_rr_t **srv_rr_list)
{
struct resolver_ares_ctx actx;
ares_channel chan;
struct timeval tv;
struct timeval *tvp;
fd_set rfds;
fd_set wfds;
int nfds;
int rc;
actx.ctx = ctx;
actx.result = XMPP_DOMAIN_NOT_FOUND;
actx.srv_rr_list = NULL;
rc = ares_init(&chan);
if (rc == ARES_SUCCESS) {
ares_query(chan, fulldomain, MESSAGE_C_IN, MESSAGE_T_SRV,
ares_srv_lookup_callback, &actx);
while (1) {
FD_ZERO(&rfds);
FD_ZERO(&wfds);
nfds = ares_fds(chan, &rfds, &wfds);
if (nfds == 0)
break;
tvp = ares_timeout(chan, NULL, &tv);
select(nfds, &rfds, &wfds, NULL, tvp);
ares_process(chan, &rfds, &wfds);
}
ares_destroy(chan);
}
*srv_rr_list = actx.srv_rr_list;
return actx.result;
}
#endif /* HAVE_CARES */
#ifdef _WIN32
/*******************************************************************************
* Next part was copied from sock.c and contains old win32 code.
*
@@ -470,21 +661,21 @@ static int resolver_win32_srv_lookup(xmpp_ctx_t *ctx, const char *fulldomain,
resolver_srv_rr_t *rr;
HINSTANCE hdnsapi = NULL;
DNS_STATUS (WINAPI * pDnsQuery_A)(PCSTR, WORD, DWORD, PIP4_ARRAY, PDNS_RECORD*, PVOID*);
void (WINAPI * pDnsRecordListFree)(PDNS_RECORD, DNS_FREE_TYPE);
DNS_STATUS (WINAPI * pDnsQuery_A)(PCSTR, WORD, DWORD, PIP4_ARRAY, DNS_RECORDA**, PVOID*);
void (WINAPI * pDnsRecordListFree)(DNS_RECORDA*, DNS_FREE_TYPE);
if (hdnsapi = LoadLibrary("dnsapi.dll")) {
pDnsQuery_A = (void *)GetProcAddress(hdnsapi, "DnsQuery_A");
pDnsRecordListFree = (void *)GetProcAddress(hdnsapi, "DnsRecordListFree");
if (pDnsQuery_A && pDnsRecordListFree) {
PDNS_RECORD dnsrecords = NULL;
DNS_RECORDA *dnsrecords = NULL;
DNS_STATUS error;
error = pDnsQuery_A(fulldomain, DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &dnsrecords, NULL);
if (error == 0) {
PDNS_RECORD current = dnsrecords;
DNS_RECORDA *current = dnsrecords;
while (current) {
if (current->wType == DNS_TYPE_SRV) {
@@ -517,7 +708,7 @@ static int resolver_win32_srv_query(const char *fulldomain,
unsigned char *buf, size_t len)
{
int set = 0;
int insize;
int insize = 0;
/* if dnsapi didn't work/isn't there, try querying the dns server manually */
if (!set)
@@ -550,7 +741,7 @@ static int resolver_win32_srv_query(const char *fulldomain,
char buffer[65535];
len = 65535;
fi = buffer;
fi = (FIXED_INFO *)buffer;
if ((error = pGetNetworkParams(fi, &len)) == ERROR_SUCCESS)
{

View File

@@ -33,6 +33,9 @@ typedef struct resolver_srv_rr_struc {
struct resolver_srv_rr_struc *next;
} resolver_srv_rr_t;
void resolver_initialize(void);
void resolver_shutdown(void);
/** Perform lookup for RFC1035 message format.
* This function allocates all elements.
*

View File

@@ -264,7 +264,8 @@ char *sasl_digest_md5(xmpp_ctx_t *ctx, const char *challenge,
xmpp_rand_nonce(ctx->rand, cnonce, sizeof(cnonce));
hash_add(table, "cnonce", xmpp_strdup(ctx, cnonce));
hash_add(table, "nc", xmpp_strdup(ctx, "00000001"));
hash_add(table, "qop", xmpp_strdup(ctx, "auth"));
if (hash_get(table, "qop") == NULL)
hash_add(table, "qop", xmpp_strdup(ctx, "auth"));
value = xmpp_alloc(ctx, 5 + strlen(domain) + 1);
memcpy(value, "xmpp/", 5);
memcpy(value+5, domain, strlen(domain));

View File

@@ -197,8 +197,9 @@ void crypto_SHA1_Update(SHA1_CTX* context, const uint8_t* data,
size_t i, j;
j = (context->count[0] >> 3) & 63;
if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
context->count[1] += (len >> 29);
if ((context->count[0] += (uint32_t)len << 3) < ((uint32_t)len << 3))
context->count[1]++;
context->count[1] += (uint32_t)(len >> 29);
if ((j + len) > 63) {
memcpy(&context->buffer[j], data, (i = 64-j));
SHA1_Transform(context->state, context->buffer);
@@ -233,7 +234,6 @@ void crypto_SHA1_Final(SHA1_CTX* context, uint8_t* digest)
}
/* Wipe variables */
i = 0;
memset(context->buffer, 0, 64);
memset(context->state, 0, 20);
memset(context->count, 0, 8);

View File

@@ -158,38 +158,33 @@ int sock_close(const sock_t sock)
#endif
}
int sock_set_blocking(const sock_t sock)
static int _sock_set_blocking_mode(sock_t sock, int blocking)
{
#ifdef _WIN32
u_long block = 0;
return ioctlsocket(sock, FIONBIO, &block);
#else
int rc;
rc = fcntl(sock, F_GETFL, NULL);
if (rc >= 0) {
rc = fcntl(sock, F_SETFL, rc & (~O_NONBLOCK));
}
return rc;
#endif
}
int sock_set_nonblocking(const sock_t sock)
{
#ifdef _WIN32
u_long nonblock = 1;
u_long nonblock = blocking ? 0 : 1;
return ioctlsocket(sock, FIONBIO, &nonblock);
#else
int rc;
rc = fcntl(sock, F_GETFL, NULL);
if (rc >= 0) {
rc = fcntl(sock, F_SETFL, rc | O_NONBLOCK);
rc = blocking ? rc & (~O_NONBLOCK) : rc | O_NONBLOCK;
rc = fcntl(sock, F_SETFL, rc);
}
return rc;
#endif
}
int sock_set_blocking(const sock_t sock)
{
return _sock_set_blocking_mode(sock, 1);
}
int sock_set_nonblocking(const sock_t sock)
{
return _sock_set_blocking_mode(sock, 0);
}
int sock_read(const sock_t sock, void * const buff, const size_t len)
{
return recv(sock, buff, len, 0);

View File

@@ -1,7 +1,7 @@
/* stanza.c
** strophe XMPP client library -- XMPP stanza object and utilities
**
** Copyright (C) 2005-2009 Collecta, Inc.
** Copyright (C) 2005-2009 Collecta, Inc.
**
** This software is provided AS-IS with no warranty, either express
** or implied.
@@ -40,15 +40,15 @@ xmpp_stanza_t *xmpp_stanza_new(xmpp_ctx_t *ctx)
stanza = xmpp_alloc(ctx, sizeof(xmpp_stanza_t));
if (stanza != NULL) {
stanza->ref = 1;
stanza->ctx = ctx;
stanza->type = XMPP_STANZA_UNKNOWN;
stanza->prev = NULL;
stanza->next = NULL;
stanza->children = NULL;
stanza->parent = NULL;
stanza->data = NULL;
stanza->attributes = NULL;
stanza->ref = 1;
stanza->ctx = ctx;
stanza->type = XMPP_STANZA_UNKNOWN;
stanza->prev = NULL;
stanza->next = NULL;
stanza->children = NULL;
stanza->parent = NULL;
stanza->data = NULL;
stanza->attributes = NULL;
}
return stanza;
@@ -121,27 +121,27 @@ xmpp_stanza_t *xmpp_stanza_copy(const xmpp_stanza_t * const stanza)
copy->type = stanza->type;
if (stanza->data) {
copy->data = xmpp_strdup(stanza->ctx, stanza->data);
if (!copy->data) goto copy_error;
copy->data = xmpp_strdup(stanza->ctx, stanza->data);
if (!copy->data) goto copy_error;
}
if (stanza->attributes) {
if (_stanza_copy_attributes(copy, stanza) == -1)
if (_stanza_copy_attributes(copy, stanza) == -1)
goto copy_error;
}
tail = copy->children;
for (child = stanza->children; child; child = child->next) {
copychild = xmpp_stanza_copy(child);
if (!copychild) goto copy_error;
copychild->parent = copy;
copychild = xmpp_stanza_copy(child);
if (!copychild) goto copy_error;
copychild->parent = copy;
if (tail) {
copychild->prev = tail;
tail->next = copychild;
} else
copy->children = copychild;
tail = copychild;
if (tail) {
copychild->prev = tail;
tail->next = copychild;
} else
copy->children = copychild;
tail = copychild;
}
return copy;
@@ -153,7 +153,7 @@ copy_error:
}
/** Release a stanza object and all of its children.
* This function releases a stanza object and potentially all of its
* This function releases a stanza object and potentially all of its
* children, which may cause the object(s) to be freed.
*
* @param stanza a Strophe stanza object
@@ -169,25 +169,38 @@ int xmpp_stanza_release(xmpp_stanza_t * const stanza)
/* release stanza */
if (stanza->ref > 1)
stanza->ref--;
stanza->ref--;
else {
/* release all children */
child = stanza->children;
while (child) {
tchild = child;
child = child->next;
xmpp_stanza_release(tchild);
}
/* release all children */
child = stanza->children;
while (child) {
tchild = child;
child = child->next;
xmpp_stanza_release(tchild);
}
if (stanza->attributes) hash_release(stanza->attributes);
if (stanza->data) xmpp_free(stanza->ctx, stanza->data);
xmpp_free(stanza->ctx, stanza);
released = 1;
if (stanza->attributes) hash_release(stanza->attributes);
if (stanza->data) xmpp_free(stanza->ctx, stanza->data);
xmpp_free(stanza->ctx, stanza);
released = 1;
}
return released;
}
/** Get the strophe context that the stanza is associated with.
*
* @param stanza a Strophe stanza object
*
* @return a Strophe context
*
* @ingroup Stanza
*/
xmpp_ctx_t *xmpp_stanza_get_context(const xmpp_stanza_t * const stanza)
{
return stanza->ctx;
}
/** Determine if a stanza is a text node.
*
* @param stanza a Strophe stanza object
@@ -274,17 +287,17 @@ static char *_escape_xml(xmpp_ctx_t * const ctx, char *text)
/* small helper function */
static void _render_update(int *written, const int length,
const int lastwrite,
size_t *left, char **ptr)
const int lastwrite,
size_t *left, char **ptr)
{
*written += lastwrite;
if (*written >= length) {
*left = 0;
*ptr = NULL;
*left = 0;
*ptr = NULL;
} else {
*left -= lastwrite;
*ptr = &(*ptr)[lastwrite];
*left -= lastwrite;
*ptr = &(*ptr)[lastwrite];
}
}
@@ -294,7 +307,7 @@ static void _render_update(int *written, const int length,
* and return values > buflen indicate buffer was not large enough
*/
static int _render_stanza_recursive(xmpp_stanza_t *stanza,
char * const buf, size_t const buflen)
char * const buf, size_t const buflen)
{
char *ptr = buf;
size_t left = buflen;
@@ -309,80 +322,86 @@ static int _render_stanza_recursive(xmpp_stanza_t *stanza,
if (stanza->type == XMPP_STANZA_UNKNOWN) return XMPP_EINVOP;
if (stanza->type == XMPP_STANZA_TEXT) {
if (!stanza->data) return XMPP_EINVOP;
if (!stanza->data) return XMPP_EINVOP;
tmp = _escape_xml(stanza->ctx, stanza->data);
if (tmp == NULL) return XMPP_EMEM;
ret = xmpp_snprintf(ptr, left, "%s", tmp);
xmpp_free(stanza->ctx, tmp);
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
tmp = _escape_xml(stanza->ctx, stanza->data);
if (tmp == NULL) return XMPP_EMEM;
ret = xmpp_snprintf(ptr, left, "%s", tmp);
xmpp_free(stanza->ctx, tmp);
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
} else { /* stanza->type == XMPP_STANZA_TAG */
if (!stanza->data) return XMPP_EINVOP;
if (!stanza->data) return XMPP_EINVOP;
/* write beginning of tag and attributes */
ret = xmpp_snprintf(ptr, left, "<%s", stanza->data);
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
/* write beginning of tag and attributes */
ret = xmpp_snprintf(ptr, left, "<%s", stanza->data);
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
if (stanza->attributes && hash_num_keys(stanza->attributes) > 0) {
iter = hash_iter_new(stanza->attributes);
while ((key = hash_iter_next(iter))) {
if (!strcmp(key, "xmlns")) {
/* don't output namespace if parent stanza is the same */
if (stanza->parent &&
stanza->parent->attributes &&
hash_get(stanza->parent->attributes, key) &&
!strcmp((char*)hash_get(stanza->attributes, key),
(char*)hash_get(stanza->parent->attributes, key)))
continue;
/* or if this is the stream namespace */
if (!stanza->parent &&
!strcmp((char*)hash_get(stanza->attributes, key),
XMPP_NS_CLIENT))
continue;
}
tmp = _escape_xml(stanza->ctx,
(char *)hash_get(stanza->attributes, key));
if (tmp == NULL) return XMPP_EMEM;
ret = xmpp_snprintf(ptr, left, " %s=\"%s\"", key, tmp);
xmpp_free(stanza->ctx, tmp);
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
}
hash_iter_release(iter);
}
if (stanza->attributes && hash_num_keys(stanza->attributes) > 0) {
iter = hash_iter_new(stanza->attributes);
while ((key = hash_iter_next(iter))) {
if (!strcmp(key, "xmlns")) {
/* don't output namespace if parent stanza is the same */
if (stanza->parent &&
stanza->parent->attributes &&
hash_get(stanza->parent->attributes, key) &&
!strcmp((char*)hash_get(stanza->attributes, key),
(char*)hash_get(stanza->parent->attributes, key)))
continue;
/* or if this is the stream namespace */
if (!stanza->parent &&
!strcmp((char*)hash_get(stanza->attributes, key),
XMPP_NS_CLIENT))
continue;
}
tmp = _escape_xml(stanza->ctx,
(char *)hash_get(stanza->attributes, key));
if (tmp == NULL) {
hash_iter_release(iter);
return XMPP_EMEM;
}
ret = xmpp_snprintf(ptr, left, " %s=\"%s\"", key, tmp);
xmpp_free(stanza->ctx, tmp);
if (ret < 0) {
hash_iter_release(iter);
return XMPP_EMEM;
}
_render_update(&written, buflen, ret, &left, &ptr);
}
hash_iter_release(iter);
}
if (!stanza->children) {
/* write end if singleton tag */
ret = xmpp_snprintf(ptr, left, "/>");
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
} else {
/* this stanza has child stanzas */
if (!stanza->children) {
/* write end if singleton tag */
ret = xmpp_snprintf(ptr, left, "/>");
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
} else {
/* this stanza has child stanzas */
/* write end of start tag */
ret = xmpp_snprintf(ptr, left, ">");
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
/* iterate and recurse over child stanzas */
child = stanza->children;
while (child) {
ret = _render_stanza_recursive(child, ptr, left);
if (ret < 0) return ret;
/* write end of start tag */
ret = xmpp_snprintf(ptr, left, ">");
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
_render_update(&written, buflen, ret, &left, &ptr);
/* iterate and recurse over child stanzas */
child = stanza->children;
while (child) {
ret = _render_stanza_recursive(child, ptr, left);
if (ret < 0) return ret;
child = child->next;
}
_render_update(&written, buflen, ret, &left, &ptr);
/* write end tag */
ret = xmpp_snprintf(ptr, left, "</%s>", stanza->data);
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
}
child = child->next;
}
/* write end tag */
ret = xmpp_snprintf(ptr, left, "</%s>", stanza->data);
if (ret < 0) return XMPP_EMEM;
_render_update(&written, buflen, ret, &left, &ptr);
}
}
return written;
@@ -415,27 +434,37 @@ int xmpp_stanza_to_text(xmpp_stanza_t *stanza,
length = 1024;
buffer = xmpp_alloc(stanza->ctx, length);
if (!buffer) {
*buf = NULL;
*buflen = 0;
return XMPP_EMEM;
*buf = NULL;
*buflen = 0;
return XMPP_EMEM;
}
ret = _render_stanza_recursive(stanza, buffer, length);
if (ret < 0) return ret;
if (ret < 0) {
xmpp_free(stanza->ctx, buffer);
*buf = NULL;
*buflen = 0;
return ret;
}
if ((size_t)ret > length - 1) {
tmp = xmpp_realloc(stanza->ctx, buffer, ret + 1);
if (!tmp) {
xmpp_free(stanza->ctx, buffer);
*buf = NULL;
*buflen = 0;
return XMPP_EMEM;
}
length = ret + 1;
buffer = tmp;
tmp = xmpp_realloc(stanza->ctx, buffer, ret + 1);
if (!tmp) {
xmpp_free(stanza->ctx, buffer);
*buf = NULL;
*buflen = 0;
return XMPP_EMEM;
}
length = ret + 1;
buffer = tmp;
ret = _render_stanza_recursive(stanza, buffer, length);
if ((size_t)ret > length - 1) return XMPP_EMEM;
ret = _render_stanza_recursive(stanza, buffer, length);
if ((size_t)ret > length - 1) {
xmpp_free(stanza->ctx, buffer);
*buf = NULL;
*buflen = 0;
return XMPP_EMEM;
}
}
buffer[length - 1] = 0;
@@ -457,7 +486,7 @@ int xmpp_stanza_to_text(xmpp_stanza_t *stanza,
* @ingroup Stanza
*/
int xmpp_stanza_set_name(xmpp_stanza_t *stanza,
const char * const name)
const char * const name)
{
if (stanza->type == XMPP_STANZA_TEXT) return XMPP_EINVOP;
@@ -496,7 +525,7 @@ const char *xmpp_stanza_get_name(xmpp_stanza_t * const stanza)
int xmpp_stanza_get_attribute_count(xmpp_stanza_t * const stanza)
{
if (stanza->attributes == NULL) {
return 0;
return 0;
}
return hash_num_keys(stanza->attributes);
@@ -517,30 +546,30 @@ int xmpp_stanza_get_attribute_count(xmpp_stanza_t * const stanza)
* @ingroup Stanza
*/
int xmpp_stanza_get_attributes(xmpp_stanza_t * const stanza,
const char **attr, int attrlen)
const char **attr, int attrlen)
{
hash_iterator_t *iter;
const char *key;
int num = 0;
if (stanza->attributes == NULL) {
return 0;
return 0;
}
iter = hash_iter_new(stanza->attributes);
while ((key = hash_iter_next(iter)) != NULL && attrlen) {
attr[num++] = key;
attrlen--;
if (attrlen == 0) {
hash_iter_release(iter);
return num;
}
attr[num++] = hash_get(stanza->attributes, key);
attrlen--;
if (attrlen == 0) {
hash_iter_release(iter);
return num;
}
attr[num++] = key;
attrlen--;
if (attrlen == 0) {
hash_iter_release(iter);
return num;
}
attr[num++] = hash_get(stanza->attributes, key);
attrlen--;
if (attrlen == 0) {
hash_iter_release(iter);
return num;
}
}
hash_iter_release(iter);
@@ -558,8 +587,8 @@ int xmpp_stanza_get_attributes(xmpp_stanza_t * const stanza,
* @ingroup Stanza
*/
int xmpp_stanza_set_attribute(xmpp_stanza_t * const stanza,
const char * const key,
const char * const value)
const char * const key,
const char * const value)
{
char *val;
int rc;
@@ -567,13 +596,12 @@ int xmpp_stanza_set_attribute(xmpp_stanza_t * const stanza,
if (stanza->type != XMPP_STANZA_TAG) return XMPP_EINVOP;
if (!stanza->attributes) {
stanza->attributes = hash_new(stanza->ctx, 8, xmpp_free);
if (!stanza->attributes) return XMPP_EMEM;
stanza->attributes = hash_new(stanza->ctx, 8, xmpp_free);
if (!stanza->attributes) return XMPP_EMEM;
}
val = xmpp_strdup(stanza->ctx, value);
if (!val) {
hash_release(stanza->attributes);
return XMPP_EMEM;
}
@@ -598,11 +626,50 @@ int xmpp_stanza_set_attribute(xmpp_stanza_t * const stanza,
* @ingroup Stanza
*/
int xmpp_stanza_set_ns(xmpp_stanza_t * const stanza,
const char * const ns)
const char * const ns)
{
return xmpp_stanza_set_attribute(stanza, "xmlns", ns);
}
/** Add a child stanza to a stanza object.
* If do_clone is TRUE, user keeps reference to the child stanza and must call
* xmpp_stanza_release() to release the reference. If do_clone is FALSE, user
* transfers ownership and must not neither call xmpp_stanza_release() for
* the child stanza nor use it.
*
* @param stanza a Strophe stanza object
* @param child the child stanza object
* @param do_clone TRUE to increase ref count of child (default for
* xmpp_stanza_add_child())
*
* @return XMPP_EOK (0) on success or a number less than 0 on failure
*
* @ingroup Stanza
*/
int xmpp_stanza_add_child_ex(xmpp_stanza_t *stanza, xmpp_stanza_t *child,
int do_clone)
{
xmpp_stanza_t *s;
if (do_clone) {
/* get a reference to the child */
xmpp_stanza_clone(child);
}
child->parent = stanza;
if (!stanza->children)
stanza->children = child;
else {
s = stanza->children;
while (s->next) s = s->next;
s->next = child;
child->prev = s;
}
return XMPP_EOK;
}
/** Add a child stanza to a stanza object.
* This function clones the child and appends it to the stanza object's
* children.
@@ -616,23 +683,7 @@ int xmpp_stanza_set_ns(xmpp_stanza_t * const stanza,
*/
int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child)
{
xmpp_stanza_t *s;
/* get a reference to the child */
xmpp_stanza_clone(child);
child->parent = stanza;
if (!stanza->children)
stanza->children = child;
else {
s = stanza->children;
while (s->next) s = s->next;
s->next = child;
child->prev = s;
}
return XMPP_EOK;
return xmpp_stanza_add_child_ex(stanza, child, 1);
}
/** Set the text data for a text stanza.
@@ -649,7 +700,7 @@ int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child)
* @ingroup Stanza
*/
int xmpp_stanza_set_text(xmpp_stanza_t *stanza,
const char * const text)
const char * const text)
{
if (stanza->type == XMPP_STANZA_TAG) return XMPP_EINVOP;
@@ -676,8 +727,8 @@ int xmpp_stanza_set_text(xmpp_stanza_t *stanza,
* @ingroup Stanza
*/
int xmpp_stanza_set_text_with_size(xmpp_stanza_t *stanza,
const char * const text,
const size_t size)
const char * const text,
const size_t size)
{
if (stanza->type == XMPP_STANZA_TAG) return XMPP_EINVOP;
@@ -780,14 +831,14 @@ const char *xmpp_stanza_get_from(xmpp_stanza_t * const stanza)
* @ingroup Stanza
*/
xmpp_stanza_t *xmpp_stanza_get_child_by_name(xmpp_stanza_t * const stanza,
const char * const name)
const char * const name)
{
xmpp_stanza_t *child;
for (child = stanza->children; child; child = child->next) {
if (child->type == XMPP_STANZA_TAG &&
(strcmp(name, xmpp_stanza_get_name(child)) == 0))
break;
if (child->type == XMPP_STANZA_TAG &&
(strcmp(name, xmpp_stanza_get_name(child)) == 0))
break;
}
return child;
@@ -806,19 +857,53 @@ xmpp_stanza_t *xmpp_stanza_get_child_by_name(xmpp_stanza_t * const stanza,
* @ingroup Stanza
*/
xmpp_stanza_t *xmpp_stanza_get_child_by_ns(xmpp_stanza_t * const stanza,
const char * const ns)
const char * const ns)
{
xmpp_stanza_t *child;
const char *child_ns;
for (child = stanza->children; child; child = child->next) {
if (xmpp_stanza_get_ns(child) &&
strcmp(ns, xmpp_stanza_get_ns(child)) == 0)
break;
child_ns = xmpp_stanza_get_ns(child);
if (child_ns && strcmp(ns, child_ns) == 0)
break;
}
return child;
}
/** Get the first child of stanza with name and a given namespace.
* This function searches all the immediate children of stanza for a child
* stanza that matches the name and namespace provided.
* The first matching child is returned.
*
* @param stanza a Strophe stanza object
* @param name a string with the name to match
* @param ns a string with the namespace to match
*
* @return the matching child stanza object or NULL if no match was found
*
* @ingroup Stanza
*/
xmpp_stanza_t *xmpp_stanza_get_child_by_name_and_ns(xmpp_stanza_t * const stanza,
const char * const name,
const char * const ns)
{
xmpp_stanza_t *child;
const char *child_ns;
for (child = stanza->children; child; child = child->next) {
if (child->type == XMPP_STANZA_TAG &&
(strcmp(name, xmpp_stanza_get_name(child)) == 0)) {
child_ns = xmpp_stanza_get_ns(child);
if (child_ns && strcmp(ns, child_ns) == 0) {
break;
}
}
}
return child;
}
/** Get the list of children.
* This function returns the first child of the stanza object. The rest
* of the children can be obtained by calling xmpp_stanza_get_next() to
@@ -866,16 +951,16 @@ char *xmpp_stanza_get_text(xmpp_stanza_t * const stanza)
char *text;
if (stanza->type == XMPP_STANZA_TEXT) {
if (stanza->data)
return xmpp_strdup(stanza->ctx, stanza->data);
else
return NULL;
if (stanza->data)
return xmpp_strdup(stanza->ctx, stanza->data);
else
return NULL;
}
len = 0;
for (child = stanza->children; child; child = child->next)
if (child->type == XMPP_STANZA_TEXT)
len += strlen(child->data);
if (child->type == XMPP_STANZA_TEXT)
len += strlen(child->data);
if (len == 0) return NULL;
@@ -884,11 +969,11 @@ char *xmpp_stanza_get_text(xmpp_stanza_t * const stanza)
len = 0;
for (child = stanza->children; child; child = child->next)
if (child->type == XMPP_STANZA_TEXT) {
clen = strlen(child->data);
memcpy(&text[len], child->data, clen);
len += clen;
}
if (child->type == XMPP_STANZA_TEXT) {
clen = strlen(child->data);
memcpy(&text[len], child->data, clen);
len += clen;
}
text[len] = 0;
@@ -911,7 +996,7 @@ char *xmpp_stanza_get_text(xmpp_stanza_t * const stanza)
const char *xmpp_stanza_get_text_ptr(xmpp_stanza_t * const stanza)
{
if (stanza->type == XMPP_STANZA_TEXT)
return stanza->data;
return stanza->data;
return NULL;
}
@@ -928,7 +1013,7 @@ const char *xmpp_stanza_get_text_ptr(xmpp_stanza_t * const stanza)
* @ingroup Stanza
*/
int xmpp_stanza_set_id(xmpp_stanza_t * const stanza,
const char * const id)
const char * const id)
{
return xmpp_stanza_set_attribute(stanza, "id", id);
}
@@ -945,7 +1030,7 @@ int xmpp_stanza_set_id(xmpp_stanza_t * const stanza,
* @ingroup Stanza
*/
int xmpp_stanza_set_type(xmpp_stanza_t * const stanza,
const char * const type)
const char * const type)
{
return xmpp_stanza_set_attribute(stanza, "type", type);
}
@@ -1001,10 +1086,10 @@ const char *xmpp_stanza_get_attribute(xmpp_stanza_t * const stanza,
const char * const name)
{
if (stanza->type != XMPP_STANZA_TAG)
return NULL;
return NULL;
if (!stanza->attributes)
return NULL;
return NULL;
return hash_get(stanza->attributes, name);
}
@@ -1218,3 +1303,122 @@ xmpp_stanza_t *xmpp_presence_new(xmpp_ctx_t *ctx)
{
return _stanza_new_with_attrs(ctx, "presence", NULL, NULL, NULL);
}
/** Create an <stream:error/> stanza object with given type and error text.
* The error text is optional and may be NULL.
*
* @param ctx a Strophe context object
* @param type enum of xmpp_error_type_t
* @param text content of a 'text'
*
* @return a new Strophe stanza object
*
* @todo Handle errors in this function
*
* @ingroup Stanza
*/
xmpp_stanza_t *xmpp_error_new(xmpp_ctx_t *ctx, xmpp_error_type_t const type,
const char * const text)
{
xmpp_stanza_t *error = _stanza_new_with_attrs(ctx, "stream:error", NULL, NULL, NULL);
xmpp_stanza_t *error_type = xmpp_stanza_new(ctx);
switch(type) {
case XMPP_SE_BAD_FORMAT:
xmpp_stanza_set_name(error_type, "bad-format");
break;
case XMPP_SE_BAD_NS_PREFIX:
xmpp_stanza_set_name(error_type, "bad-namespace-prefix");
break;
case XMPP_SE_CONFLICT:
xmpp_stanza_set_name(error_type, "conflict");
break;
case XMPP_SE_CONN_TIMEOUT:
xmpp_stanza_set_name(error_type, "connection-timeout");
break;
case XMPP_SE_HOST_GONE:
xmpp_stanza_set_name(error_type, "host-gone");
break;
case XMPP_SE_HOST_UNKNOWN:
xmpp_stanza_set_name(error_type, "host-unknown");
break;
case XMPP_SE_IMPROPER_ADDR:
xmpp_stanza_set_name(error_type, "improper-addressing");
break;
case XMPP_SE_INTERNAL_SERVER_ERROR:
xmpp_stanza_set_name(error_type, "internal-server-error");
break;
case XMPP_SE_INVALID_FROM:
xmpp_stanza_set_name(error_type, "invalid-from");
break;
case XMPP_SE_INVALID_ID:
xmpp_stanza_set_name(error_type, "invalid-id");
break;
case XMPP_SE_INVALID_NS:
xmpp_stanza_set_name(error_type, "invalid-namespace");
break;
case XMPP_SE_INVALID_XML:
xmpp_stanza_set_name(error_type, "invalid-xml");
break;
case XMPP_SE_NOT_AUTHORIZED:
xmpp_stanza_set_name(error_type, "not-authorized");
break;
case XMPP_SE_POLICY_VIOLATION:
xmpp_stanza_set_name(error_type, "policy-violation");
break;
case XMPP_SE_REMOTE_CONN_FAILED:
xmpp_stanza_set_name(error_type, "remote-connection-failed");
break;
case XMPP_SE_RESOURCE_CONSTRAINT:
xmpp_stanza_set_name(error_type, "resource-constraint");
break;
case XMPP_SE_RESTRICTED_XML:
xmpp_stanza_set_name(error_type, "restricted-xml");
break;
case XMPP_SE_SEE_OTHER_HOST:
xmpp_stanza_set_name(error_type, "see-other-host");
break;
case XMPP_SE_SYSTEM_SHUTDOWN:
xmpp_stanza_set_name(error_type, "system-shutdown");
break;
case XMPP_SE_UNDEFINED_CONDITION:
xmpp_stanza_set_name(error_type, "undefined-condition");
break;
case XMPP_SE_UNSUPPORTED_ENCODING:
xmpp_stanza_set_name(error_type, "unsupported-encoding");
break;
case XMPP_SE_UNSUPPORTED_STANZA_TYPE:
xmpp_stanza_set_name(error_type, "unsupported-stanza-type");
break;
case XMPP_SE_UNSUPPORTED_VERSION:
xmpp_stanza_set_name(error_type, "unsupported-version");
break;
case XMPP_SE_XML_NOT_WELL_FORMED:
xmpp_stanza_set_name(error_type, "xml-not-well-formed");
break;
default:
xmpp_stanza_set_name(error_type, "internal-server-error");
break;
}
xmpp_stanza_set_ns(error_type, XMPP_NS_STREAMS_IETF);
xmpp_stanza_add_child(error, error_type);
xmpp_stanza_release(error_type);
if (text) {
xmpp_stanza_t *error_text = xmpp_stanza_new(ctx);
xmpp_stanza_t *content = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(error_text, "text");
xmpp_stanza_set_ns(error_text, XMPP_NS_STREAMS_IETF);
xmpp_stanza_set_text(content, text);
xmpp_stanza_add_child(error_text, content);
xmpp_stanza_release(content);
xmpp_stanza_add_child(error, error_text);
xmpp_stanza_release(error_text);
}
return error;
}

View File

@@ -31,6 +31,22 @@
#include "tls.h"
#include "sock.h"
/*
* Redefine OPENSSL_VERSION_NUMBER for LibreSSL.
* LibreSSL and OpenSSL use different and incompatible version schemes. Solve
* this issue in the way how nginx project did.
*/
#if (defined LIBRESSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER == 0x20000000L)
#undef OPENSSL_VERSION_NUMBER
#if (LIBRESSL_VERSION_NUMBER >= 0x2080000fL)
#define OPENSSL_VERSION_NUMBER 0x1010000fL
#elif (LIBRESSL_VERSION_NUMBER >= 0x2070000fL)
#define OPENSSL_VERSION_NUMBER 0x1000200fL
#else
#define OPENSSL_VERSION_NUMBER 0x1000107fL
#endif
#endif
struct _tls {
xmpp_ctx_t *ctx;
sock_t sock;
@@ -48,6 +64,7 @@ enum {
static void _tls_sock_wait(tls_t *tls, int error);
static void _tls_set_error(tls_t *tls, int error);
static void _tls_log_error(xmpp_ctx_t *ctx);
static void _tls_dump_cert_info(tls_t *tls);
void tls_initialize(void)
{
@@ -93,11 +110,19 @@ tls_t *tls_new(xmpp_conn_t *conn)
if (tls) {
int ret;
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
/* Hostname verification is supported in OpenSSL 1.0.2 and newer. */
X509_VERIFY_PARAM *param;
#endif
memset(tls, 0, sizeof(*tls));
tls->ctx = conn->ctx;
tls->sock = conn->sock;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
tls->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
#else
tls->ssl_ctx = SSL_CTX_new(TLS_client_method());
#endif
if (tls->ssl_ctx == NULL)
goto err;
@@ -111,18 +136,34 @@ tls_t *tls_new(xmpp_conn_t *conn)
SSL_CTX_set_client_cert_cb(tls->ssl_ctx, NULL);
SSL_CTX_set_mode(tls->ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
SSL_CTX_set_default_verify_paths(tls->ssl_ctx);
ret = SSL_CTX_set_default_verify_paths(tls->ssl_ctx);
if (ret == 0 && !conn->tls_trust) {
/*
* Returns 1 on success and 0 on failure. A missing default
* location is still treated as a success.
* Ignore errors when XMPP_CONN_FLAG_TRUST_TLS is set.
*/
xmpp_error(tls->ctx, "tls",
"SSL_CTX_set_default_verify_paths() failed");
goto err_free_ctx;
}
tls->ssl = SSL_new(tls->ssl_ctx);
if (tls->ssl == NULL)
goto err_free_ctx;
#if OPENSSL_VERSION_NUMBER >= 0x0908060L && !defined(OPENSSL_NO_TLSEXT)
/* Enable SNI. */
SSL_set_tlsext_host_name(tls->ssl, conn->domain);
#endif
/* Trust server's certificate when user sets the flag explicitly. */
mode = conn->tls_trust ? SSL_VERIFY_NONE : SSL_VERIFY_PEER;
SSL_set_verify(tls->ssl, mode, 0);
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
/* Hostname verification is supported in OpenSSL 1.0.2 and newer. */
X509_VERIFY_PARAM *param = SSL_get0_param(tls->ssl);
param = SSL_get0_param(tls->ssl);
/*
* Allow only complete wildcards. RFC 6125 discourages wildcard usage
@@ -188,6 +229,7 @@ int tls_start(tls_t *tls)
x509_res = SSL_get_verify_result(tls->ssl);
xmpp_debug(tls->ctx, "tls", "Certificate verification %s",
x509_res == X509_V_OK ? "passed" : "FAILED");
_tls_dump_cert_info(tls);
_tls_set_error(tls, error);
return ret <= 0 ? 0 : 1;
@@ -199,6 +241,11 @@ int tls_stop(tls_t *tls)
int error;
int ret;
/* According to OpenSSL.org, we must not call SSL_shutdown(3)
if a previous fatal error has occurred on a connection. */
if (tls->lasterror == SSL_ERROR_SYSCALL || tls->lasterror == SSL_ERROR_SSL)
return 1;
while (1) {
++retries;
ret = SSL_shutdown(tls->ssl);
@@ -209,6 +256,14 @@ int tls_stop(tls_t *tls)
}
_tls_sock_wait(tls, error);
}
if (error == SSL_ERROR_SYSCALL && errno == 0) {
/*
* Handle special case when peer closes connection instead of
* proper shutdown.
*/
error = 0;
ret = 1;
}
_tls_set_error(tls, error);
return ret <= 0 ? 0 : 1;
@@ -260,6 +315,8 @@ static void _tls_sock_wait(tls_t *tls, int error)
int nfds;
int ret;
if (error == SSL_ERROR_NONE) return;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
if (error == SSL_ERROR_WANT_READ)
@@ -278,6 +335,7 @@ static void _tls_sock_wait(tls_t *tls, int error)
static void _tls_set_error(tls_t *tls, int error)
{
if (error != 0 && !tls_is_recoverable(error)) {
xmpp_debug(tls->ctx, "tls", "error=%d errno=%d", error, errno);
_tls_log_error(tls->ctx);
}
tls->lasterror = error;
@@ -296,3 +354,26 @@ static void _tls_log_error(xmpp_ctx_t *ctx)
}
} while (e != 0);
}
static void _tls_dump_cert_info(tls_t *tls)
{
X509 *cert;
char *name;
cert = SSL_get_peer_certificate(tls->ssl);
if (cert == NULL)
xmpp_debug(tls->ctx, "tls", "Certificate was not presented by peer");
else {
name = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
if (name != NULL) {
xmpp_debug(tls->ctx, "tls", "Subject=%s", name);
OPENSSL_free(name);
}
name = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
if (name != NULL) {
xmpp_debug(tls->ctx, "tls", "Issuer=%s", name);
OPENSSL_free(name);
}
X509_free(cert);
}
}

View File

@@ -255,10 +255,18 @@ int tls_start(tls_t *tls)
NULL, 0, &(tls->hctxt), &sbdout,
&ctxtattr, NULL);
unsigned char *p = sbin[0].pvBuffer;
int len = 0;
while (ret == SEC_I_CONTINUE_NEEDED
|| ret == SEC_I_INCOMPLETE_CREDENTIALS) {
unsigned char *p = sbin[0].pvBuffer;
int len = 0, inbytes = 0;
|| ret == SEC_I_INCOMPLETE_CREDENTIALS
|| ret == SEC_E_INCOMPLETE_MESSAGE) {
int inbytes = 0;
if (ret != SEC_E_INCOMPLETE_MESSAGE) {
len = 0;
p = sbin[0].pvBuffer;
}
if (sbdout.pBuffers[0].cbBuffer) {
unsigned char *writebuff = sbdout.pBuffers[0].pvBuffer;

View File

@@ -86,7 +86,12 @@ char *xmpp_strtok_r(char *s, const char *delim, char **saveptr)
*/
uint64_t time_stamp(void)
{
#ifdef _WIN32
#if defined(_XBOX_ONE)
uint64_t SystemTime;
GetSystemTimeAsFileTime((FILETIME*)&SystemTime);
/* Convert 100 nanosec ticks to milliseconds */
return (SystemTime / 10000);
#elif defined(_WIN32)
return timeGetTime();
#else
struct timeval tv;

View File

@@ -71,6 +71,14 @@ extern "C" {
* Namespace definition for 'jabber:iq:roster'.
*/
#define XMPP_NS_ROSTER "jabber:iq:roster"
/** @def XMPP_NS_REGISTER
* Namespace definition for 'jabber:iq:register'.
*/
#define XMPP_NS_REGISTER "jabber:iq:register"
/** @def XMPP_NS_SM
* Namespace definition for Stream Management.
*/
#define XMPP_NS_SM "urn:xmpp:sm:3"
/* error defines */
/** @def XMPP_EOK
@@ -169,6 +177,10 @@ typedef struct _xmpp_stanza_t xmpp_stanza_t;
* Trust server's certificate even if it is invalid.
*/
#define XMPP_CONN_FLAG_TRUST_TLS (1UL << 3)
/** @def XMPP_CONN_FLAG_LEGACY_AUTH
* Enable legacy authentication support.
*/
#define XMPP_CONN_FLAG_LEGACY_AUTH (1UL << 4)
/* connect callback */
typedef enum {
@@ -217,6 +229,7 @@ typedef void (*xmpp_conn_handler)(xmpp_conn_t * const conn,
xmpp_stream_error_t * const stream_error,
void * const userdata);
void xmpp_send_error(xmpp_conn_t * const conn, xmpp_error_type_t const type, char * const text);
xmpp_conn_t *xmpp_conn_new(xmpp_ctx_t * const ctx);
xmpp_conn_t *xmpp_conn_clone(xmpp_conn_t * const conn);
int xmpp_conn_release(xmpp_conn_t * const conn);
@@ -232,6 +245,9 @@ xmpp_ctx_t *xmpp_conn_get_context(xmpp_conn_t * const conn);
void xmpp_conn_disable_tls(xmpp_conn_t * const conn);
int xmpp_conn_is_secured(xmpp_conn_t * const conn);
void xmpp_conn_set_keepalive(xmpp_conn_t * const conn, int timeout, int interval);
int xmpp_conn_is_connecting(xmpp_conn_t * const conn);
int xmpp_conn_is_connected(xmpp_conn_t * const conn);
int xmpp_conn_is_disconnected(xmpp_conn_t * const conn);
int xmpp_connect_client(xmpp_conn_t * const conn,
const char * const altdomain,
@@ -318,6 +334,8 @@ xmpp_stanza_t *xmpp_stanza_copy(const xmpp_stanza_t * const stanza);
/* free a stanza object and it's contents */
int xmpp_stanza_release(xmpp_stanza_t * const stanza);
xmpp_ctx_t *xmpp_stanza_get_context(const xmpp_stanza_t * const stanza);
int xmpp_stanza_is_text(xmpp_stanza_t * const stanza);
int xmpp_stanza_is_tag(xmpp_stanza_t * const stanza);
@@ -330,8 +348,13 @@ xmpp_stanza_t *xmpp_stanza_get_child_by_name(xmpp_stanza_t * const stanza,
const char * const name);
xmpp_stanza_t *xmpp_stanza_get_child_by_ns(xmpp_stanza_t * const stanza,
const char * const ns);
xmpp_stanza_t *xmpp_stanza_get_child_by_name_and_ns(xmpp_stanza_t * const stanza,
const char * const name,
const char * const ns);
xmpp_stanza_t *xmpp_stanza_get_next(xmpp_stanza_t * const stanza);
int xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child);
int xmpp_stanza_add_child_ex(xmpp_stanza_t *stanza, xmpp_stanza_t *child,
int do_clone);
const char *xmpp_stanza_get_attribute(xmpp_stanza_t * const stanza,
const char * const name);
@@ -381,6 +404,8 @@ int xmpp_message_set_body(xmpp_stanza_t *msg, const char * const text);
xmpp_stanza_t *xmpp_iq_new(xmpp_ctx_t *ctx, const char * const type,
const char * const id);
xmpp_stanza_t *xmpp_presence_new(xmpp_ctx_t *ctx);
xmpp_stanza_t *xmpp_error_new(xmpp_ctx_t *ctx, xmpp_error_type_t const type,
const char * const text);
/* jid */

View File

@@ -16,18 +16,19 @@
#include "strophe.h"
#include "common.h"
#include "hash.h"
#include "test.h"
#define TABLESIZE 100
#define TESTSIZE 500
/* static test data */
const int nkeys = 5;
const char *keys[] = {
"foo", "bar", "baz", "quux", "xyzzy"
};
const char *values[] = {
"wuzzle", "mug", "canonical", "rosebud", "lottery"
};
const int nkeys = ARRAY_SIZE(keys);
int main(int argc, char **argv)
{
@@ -58,7 +59,7 @@ int main(int argc, char **argv)
}
/* allocate a hash table */
table = hash_new(ctx, TABLESIZE, NULL);
table = hash_new(ctx, TABLESIZE, xmpp_free);
if (table == NULL) {
/* table allocation failed! */
return 1;
@@ -66,7 +67,7 @@ int main(int argc, char **argv)
/* test insertion */
for (i = 0; i < nkeys; i++) {
err = hash_add(table, keys[i], (void*)values[i]);
err = hash_add(table, keys[i], xmpp_strdup(ctx, values[i]));
if (err) return err;
}
@@ -78,7 +79,7 @@ int main(int argc, char **argv)
/* test replacing old values */
for (i = 0; i < nkeys; i++) {
err = hash_add(table, keys[0], (void*)values[i]);
err = hash_add(table, keys[0], xmpp_strdup(ctx, values[i]));
if (err) return err;
if (hash_num_keys(table) != nkeys) return 1;
result = hash_get(table, keys[0]);
@@ -86,7 +87,7 @@ int main(int argc, char **argv)
if (strcmp(result, values[i]) != 0) return 1;
}
/* restore value for the 1st key */
hash_add(table, keys[0], (void*)values[0]);
hash_add(table, keys[0], xmpp_strdup(ctx, values[0]));
/* test cloning */
clone = hash_clone(table);

View File

@@ -41,7 +41,7 @@ static void digest_to_hex(const uint8_t *digest, char *output)
int main(int argc, char** argv)
{
int k;
size_t k;
SHA1_CTX context;
uint8_t digest[20];
char output[80];

96
tests/test_stanza.c Normal file
View File

@@ -0,0 +1,96 @@
/* test_stanza.c
* libstrophe XMPP client library -- test routines for stanza functions
*
* Copyright (C) 2020 Dmitry Podgorny <pasis.ua@gmail.com>
*
* This software is provided AS-IS with no warranty, either express
* or implied.
*
* This program is dual licensed under the MIT and GPLv3 licenses.
*/
/* gcc -o test_stanza -I./src tests/test_stanza.c -lstrophe */
#include <strophe.h>
#include <assert.h>
#include <stdlib.h>
#define MAGICPTR ((void *)0xfeedbeef)
static unsigned long used_blocks = 0;
static void *stanza_alloc(size_t size, void *userdata)
{
assert(userdata == MAGICPTR);
++used_blocks;
return malloc(size);
}
static void stanza_free(void *ptr, void *userdata)
{
assert(userdata == MAGICPTR);
--used_blocks;
free(ptr);
}
static void *stanza_realloc(void *ptr, size_t size, void *userdata)
{
assert(userdata == MAGICPTR);
return realloc(ptr, size);
}
static const xmpp_mem_t stanza_mem = {
.alloc = &stanza_alloc,
.free = &stanza_free,
.realloc = &stanza_realloc,
.userdata = MAGICPTR,
};
static void test_stanza_add_child(xmpp_ctx_t *ctx)
{
xmpp_stanza_t *stanza;
xmpp_stanza_t *child;
unsigned long baseline = used_blocks;
/* xmpp_stanza_add_child */
stanza = xmpp_stanza_new(ctx);
child = xmpp_stanza_new(ctx);
assert(stanza != NULL);
assert(child != NULL);
xmpp_stanza_add_child(stanza, child);
xmpp_stanza_release(stanza);
assert(used_blocks > baseline);
xmpp_stanza_release(child);
assert(used_blocks == baseline);
/* xmpp_stanza_add_child_ex */
stanza = xmpp_stanza_new(ctx);
child = xmpp_stanza_new(ctx);
assert(stanza != NULL);
assert(child != NULL);
xmpp_stanza_add_child_ex(stanza, child, 0);
xmpp_stanza_release(stanza);
assert(used_blocks == baseline);
}
int main()
{
xmpp_ctx_t *ctx;
xmpp_initialize();
ctx = xmpp_ctx_new(&stanza_mem, NULL);
assert(ctx != NULL);
test_stanza_add_child(ctx);
xmpp_ctx_free(ctx);
xmpp_shutdown();
/* All allocated blocks must be freed. */
assert(used_blocks == 0);
}

View File

@@ -20,6 +20,16 @@
#include "test.h" /* ARRAY_SIZE */
/* strtok_s() has appeared in visual studio 2005.
Use own implementation for older versions. */
#ifdef _MSC_VER
# if (_MSC_VER >= 1400)
# define strtok_r strtok_s
# else
# define strtok_r xmpp_strtok_r
# endif
#endif /* _MSC_VER */
static int test_strtok_r(void)
{
const char *test = "-abc-=-def--";