Changeset a03a9f3


Ignore:
Timestamp:
2005-12-18T01:02:29Z (18 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
43f205b
Parents:
7c0a497
Message:

Non-blocking I/O for OpenSSL, plus some extra (important) cleanup code for GnuTLS.

Location:
protocols
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • protocols/ssl_gnutls.c

    r7c0a497 ra03a9f3  
    111111       
    112112        if( conn->inpa != -1 )
     113        {
    113114                gaim_input_remove( conn->inpa );
     115                conn->inpa = -1;
     116        }
    114117       
    115118        if( ( st = gnutls_handshake( conn->session ) ) < 0 )
     
    183186        struct scd *conn = conn_;
    184187       
     188        if( conn->inpa != -1 )
     189                gaim_input_remove( conn->inpa );
     190       
    185191        if( conn->established )
    186192                gnutls_bye( conn->session, GNUTLS_SHUT_WR );
  • protocols/ssl_openssl.c

    r7c0a497 ra03a9f3  
    4646        gboolean established;
    4747       
     48        int inpa;
     49        int lasterr;            /* Necessary for SSL_get_error */
    4850        SSL *ssl;
    4951        SSL_CTX *ssl_ctx;
     
    9395}
    9496
     97static void ssl_handshake( gpointer data, gint source, GaimInputCondition cond );
     98
    9599static void ssl_connected( gpointer data, gint source, GaimInputCondition cond )
    96100{
     
    98102       
    99103        if( source == -1 )
    100                 goto ssl_connected_failure;
    101        
     104                return ssl_handshake( data, -1, cond );
     105       
     106        /* Make it non-blocking at least during the handshake... */
     107        sock_make_nonblocking( conn->fd );
    102108        SSL_set_fd( conn->ssl, conn->fd );
    103109       
    104         if( SSL_connect( conn->ssl ) < 0 )
    105                 goto ssl_connected_failure;
     110        return ssl_handshake( data, source, cond );
     111}       
     112
     113static void ssl_handshake( gpointer data, gint source, GaimInputCondition cond )
     114{
     115        struct scd *conn = data;
     116        int st;
     117       
     118        if( conn->inpa != -1 )
     119        {
     120                gaim_input_remove( conn->inpa );
     121                conn->inpa = -1;
     122        }
     123       
     124        if( ( st = SSL_connect( conn->ssl ) ) < 0 )
     125        {
     126                conn->lasterr = SSL_get_error( conn->ssl, st );
     127                if( conn->lasterr != SSL_ERROR_WANT_READ && conn->lasterr != SSL_ERROR_WANT_WRITE )
     128                        goto ssl_connected_failure;
     129               
     130                conn->inpa = gaim_input_add( conn->fd, ssl_getdirection( conn ), ssl_handshake, data );
     131                return;
     132        }
    106133       
    107134        conn->established = TRUE;
     135        sock_make_blocking( conn->fd );         /* For now... */
    108136        conn->func( conn->data, conn, cond );
    109137        return;
     
    127155int ssl_read( void *conn, char *buf, int len )
    128156{
     157        int st;
     158       
    129159        if( !((struct scd*)conn)->established )
    130                 return( 0 );
    131        
    132         return( SSL_read( ((struct scd*)conn)->ssl, buf, len ) );
     160        {
     161                ssl_errno = SSL_NOHANDSHAKE;
     162                return -1;
     163        }
     164       
     165        st = SSL_read( ((struct scd*)conn)->ssl, buf, len );
     166       
     167        ssl_errno = SSL_OK;
     168        if( st <= 0 )
     169        {
     170                ((struct scd*)conn)->lasterr = SSL_get_error( ((struct scd*)conn)->ssl, st );
     171                if( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_READ || ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE )
     172                        ssl_errno = SSL_AGAIN;
     173        }
     174       
     175        return st;
    133176}
    134177
    135178int ssl_write( void *conn, const char *buf, int len )
    136179{
     180        int st;
     181       
    137182        if( !((struct scd*)conn)->established )
    138                 return( 0 );
    139        
    140         return( SSL_write( ((struct scd*)conn)->ssl, buf, len ) );
     183        {
     184                ssl_errno = SSL_NOHANDSHAKE;
     185                return -1;
     186        }
     187       
     188        st = SSL_write( ((struct scd*)conn)->ssl, buf, len );
     189       
     190        ssl_errno = SSL_OK;
     191        if( st <= 0 )
     192        {
     193                ((struct scd*)conn)->lasterr = SSL_get_error( ((struct scd*)conn)->ssl, st );
     194                if( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_READ || ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE )
     195                        ssl_errno = SSL_AGAIN;
     196        }
     197       
     198        return st;
    141199}
    142200
     
    144202{
    145203        struct scd *conn = conn_;
     204       
     205        if( conn->inpa != -1 )
     206                gaim_input_remove( conn->inpa );
    146207       
    147208        if( conn->established )
     
    159220        return( ((struct scd*)conn)->fd );
    160221}
     222
     223GaimInputCondition ssl_getdirection( void *conn )
     224{
     225        return( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ? GAIM_INPUT_WRITE : GAIM_INPUT_READ );
     226}
Note: See TracChangeset for help on using the changeset viewer.