Merge pull request #12 from vladmihaisima/fix_ssl
Bug when using SSL communication
This commit is contained in:
12
src/event.c
12
src/event.c
@@ -85,6 +85,7 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
|
|||||||
char buf[4096];
|
char buf[4096];
|
||||||
uint64_t next;
|
uint64_t next;
|
||||||
long usec;
|
long usec;
|
||||||
|
int tls_read_bytes = 0;
|
||||||
|
|
||||||
if (ctx->loop_status == XMPP_LOOP_QUIT) return;
|
if (ctx->loop_status == XMPP_LOOP_QUIT) return;
|
||||||
ctx->loop_status = XMPP_LOOP_RUNNING;
|
ctx->loop_status = XMPP_LOOP_RUNNING;
|
||||||
@@ -215,6 +216,11 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Check if there is something in the SSL buffer. */
|
||||||
|
if (conn->tls) {
|
||||||
|
tls_read_bytes += tls_pending(conn->tls);
|
||||||
|
}
|
||||||
|
|
||||||
if (conn->sock > max) max = conn->sock;
|
if (conn->sock > max) max = conn->sock;
|
||||||
|
|
||||||
connitem = connitem->next;
|
connitem = connitem->next;
|
||||||
@@ -232,8 +238,8 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* no events happened */
|
/* no events happened */
|
||||||
if (ret == 0) return;
|
if (ret == 0 && tls_read_bytes == 0) return;
|
||||||
|
|
||||||
/* process events */
|
/* process events */
|
||||||
connitem = ctx->connlist;
|
connitem = ctx->connlist;
|
||||||
while (connitem) {
|
while (connitem) {
|
||||||
@@ -262,7 +268,7 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case XMPP_STATE_CONNECTED:
|
case XMPP_STATE_CONNECTED:
|
||||||
if (FD_ISSET(conn->sock, &rfds)) {
|
if (FD_ISSET(conn->sock, &rfds) || (conn->tls && tls_pending(conn->tls))) {
|
||||||
if (conn->tls) {
|
if (conn->tls) {
|
||||||
ret = tls_read(conn->tls, buf, 4096);
|
ret = tls_read(conn->tls, buf, 4096);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ int tls_stop(tls_t *tls);
|
|||||||
|
|
||||||
int tls_error(tls_t *tls);
|
int tls_error(tls_t *tls);
|
||||||
|
|
||||||
|
int tls_pending(tls_t *tls);
|
||||||
int tls_read(tls_t *tls, void * const buff, const size_t len);
|
int tls_read(tls_t *tls, void * const buff, const size_t len);
|
||||||
int tls_write(tls_t *tls, const void * const buff, const size_t len);
|
int tls_write(tls_t *tls, const void * const buff, const size_t len);
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,11 @@ int tls_error(tls_t *tls)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int tls_pending(tls_t *tls)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int tls_read(tls_t *tls, void * const buff, const size_t len)
|
int tls_read(tls_t *tls, void * const buff, const size_t len)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -107,6 +107,11 @@ int tls_error(tls_t *tls)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int tls_pending(tls_t *tls)
|
||||||
|
{
|
||||||
|
return gnutls_record_check_pending (tls->session);
|
||||||
|
}
|
||||||
|
|
||||||
int tls_read(tls_t *tls, void * const buff, const size_t len)
|
int tls_read(tls_t *tls, void * const buff, const size_t len)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|||||||
@@ -152,6 +152,11 @@ int tls_is_recoverable(int error)
|
|||||||
|| error == SSL_ERROR_WANT_ACCEPT);
|
|| error == SSL_ERROR_WANT_ACCEPT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int tls_pending(tls_t *tls)
|
||||||
|
{
|
||||||
|
return SSL_pending(tls->ssl);
|
||||||
|
}
|
||||||
|
|
||||||
int tls_read(tls_t *tls, void * const buff, const size_t len)
|
int tls_read(tls_t *tls, void * const buff, const size_t len)
|
||||||
{
|
{
|
||||||
int ret = SSL_read(tls->ssl, buff, len);
|
int ret = SSL_read(tls->ssl, buff, len);
|
||||||
|
|||||||
@@ -398,6 +398,22 @@ int tls_is_recoverable(int error)
|
|||||||
|| error == WSAEINPROGRESS);
|
|| error == WSAEINPROGRESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int tls_pending(tls_t *tls) {
|
||||||
|
// There are 3 cases:
|
||||||
|
// - there is data in ready buffer, so it is by default pending
|
||||||
|
// - there is data in recv buffer. If it is not decrypted yet, means it
|
||||||
|
// was incomplete. This should be processed again only if there is data
|
||||||
|
// on the physical connection
|
||||||
|
// - there is data on the physical connection. This case is treated
|
||||||
|
// outside the tls (in event.c)
|
||||||
|
|
||||||
|
if (tls->readybufferpos < tls->readybufferlen) {
|
||||||
|
return tls->readybufferlen - tls->readybufferpos;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int tls_read(tls_t *tls, void * const buff, const size_t len)
|
int tls_read(tls_t *tls, void * const buff, const size_t len)
|
||||||
{
|
{
|
||||||
int bytes;
|
int bytes;
|
||||||
|
|||||||
Reference in New Issue
Block a user