Merge pull request #12 from vladmihaisima/fix_ssl

Bug when using SSL communication
This commit is contained in:
Jack Moffitt
2012-02-07 20:33:56 -08:00
6 changed files with 41 additions and 3 deletions

View File

@@ -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,7 +238,7 @@ 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;
@@ -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 {

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;