Ticket #369: memory-fix.diff

File memory-fix.diff, 4.6 KB (added by AopicieR, at 2011-12-27T12:56:30Z)

This patch moves the allocation of the GnuTLS credentials to the inital ssl_init. The credentials should then be shared among the different connections and even among forked processes.

  • lib/ssl_bogus.c

    === modified file 'lib/ssl_bogus.c'
     
    3131{
    3232}
    3333
     34void ssl_deinit( void )
     35{
     36}
     37
    3438void *ssl_connect( char *host, int port, gboolean verify, ssl_input_function func, gpointer data )
    3539{
    3640        return( NULL );
  • lib/ssl_client.h

    === modified file 'lib/ssl_client.h'
     
    5757
    5858/* Perform any global initialization the SSL library might need. */
    5959G_MODULE_EXPORT void ssl_init( void );
     60G_MODULE_EXPORT void ssl_deinit( void );
    6061
    6162/* Connect to host:port, call the given function when the connection is
    6263   ready to be used for SSL traffic. This is all done asynchronously, no
  • lib/ssl_gnutls.c

    === modified file 'lib/ssl_gnutls.c'
     
    3737int ssl_errno = 0;
    3838
    3939static gboolean initialized = FALSE;
     40gnutls_certificate_credentials xcred;
    4041
    4142#include <limits.h>
    4243
     
    5960        gboolean verify;
    6061       
    6162        gnutls_session session;
    62         gnutls_certificate_credentials xcred;
    6363};
    6464
    6565static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond );
    6666static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond );
    6767static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond );
    6868
    69 
    7069void ssl_init( void )
    7170{
    7271        if( initialized )
    7372                return;
    7473       
    7574        gnutls_global_init();
     75        gnutls_certificate_allocate_credentials( &xcred );
     76        if( global.conf->cafile )
     77        {
     78                gnutls_certificate_set_x509_trust_file( xcred, global.conf->cafile, GNUTLS_X509_FMT_PEM );
     79                /* TODO: Do we want/need this? */
     80                gnutls_certificate_set_verify_flags( xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT );
     81        }
    7682        initialized = TRUE;
    77         atexit( gnutls_global_deinit );
     83}
     84
     85void ssl_deinit (void)
     86{
     87        gnutls_global_deinit();
     88        gnutls_certificate_free_credentials( xcred );
    7889}
    7990
    8091void *ssl_connect( char *host, int port, gboolean verify, ssl_input_function func, gpointer data )
     
    244255       
    245256        ssl_init();
    246257       
    247         gnutls_certificate_allocate_credentials( &conn->xcred );
    248         if( conn->verify && global.conf->cafile )
    249         {
    250                 gnutls_certificate_set_x509_trust_file( conn->xcred, global.conf->cafile, GNUTLS_X509_FMT_PEM );
    251                 gnutls_certificate_set_verify_flags( conn->xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT );
    252         }
    253 
    254258        gnutls_init( &conn->session, GNUTLS_CLIENT );
    255259        if( conn->verify )
    256260                gnutls_session_set_ptr( conn->session, (void *) conn->hostname );
     
    258262        gnutls_transport_set_lowat( conn->session, 0 );
    259263#endif
    260264        gnutls_set_default_priority( conn->session );
    261         gnutls_credentials_set( conn->session, GNUTLS_CRD_CERTIFICATE, conn->xcred );
     265        gnutls_credentials_set( conn->session, GNUTLS_CRD_CERTIFICATE, xcred );
    262266       
    263267        sock_make_nonblocking( conn->fd );
    264268        gnutls_transport_set_ptr( conn->session, (gnutls_transport_ptr) GNUTLS_STUPID_CAST conn->fd );
     
    283287                        conn->func( conn->data, 0, NULL, cond );
    284288                       
    285289                        gnutls_deinit( conn->session );
    286                         gnutls_certificate_free_credentials( conn->xcred );
    287290                        closesocket( conn->fd );
    288291                       
    289292                        g_free( conn );
     
    296299                        conn->func( conn->data, stver, NULL, cond );
    297300
    298301                        gnutls_deinit( conn->session );
    299                         gnutls_certificate_free_credentials( conn->xcred );
    300302                        closesocket( conn->fd );
    301303
    302304                        g_free( conn );
     
    384386       
    385387        if( conn->session )
    386388                gnutls_deinit( conn->session );
    387         if( conn->xcred )
    388                 gnutls_certificate_free_credentials( conn->xcred );
    389389        g_free( conn );
    390390}
    391391
  • lib/ssl_nss.c

    === modified file 'lib/ssl_nss.c'
     
    102102        initialized = TRUE;
    103103}
    104104
     105void ssl_deinit( void )
     106{
     107}
     108
    105109void *ssl_connect( char *host, int port, gboolean verify, ssl_input_function func, gpointer data )
    106110{
    107111        struct scd *conn = g_new0( struct scd, 1 );
  • lib/ssl_openssl.c

    === modified file 'lib/ssl_openssl.c'
     
    2929#include <openssl/pem.h>
    3030#include <openssl/ssl.h>
    3131#include <openssl/err.h>
     32#include "bitlbee.h"
    3233
    3334#include "proxy.h"
    3435#include "ssl_client.h"
     
    6465        // SSLeay_add_ssl_algorithms();
    6566}
    6667
     68void ssl_deinit( void )
     69{
     70}
     71
    6772void *ssl_connect( char *host, int port, gboolean verify, ssl_input_function func, gpointer data )
    6873{
    6974        struct scd *conn = g_new0( struct scd, 1 );
  • unix.c

    === modified file 'unix.c'
     
    184184       
    185185        /* Mainly good for restarting, to make sure we close the help.txt fd. */
    186186        help_free( &global.help );
     187
     188        ssl_deinit();
    187189       
    188190        if( global.restart )
    189191        {