NOEXPORT void session_cache_save(CLI *c, SSL_SESSION *sess) {
    SSL_SESSION *old;

#if OPENSSL_VERSION_NUMBER>=0x10100000L
    SSL_SESSION_up_ref(sess);
#else
    sess=SSL_get1_session(c->ssl);
#endif

    CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_SESSION]);
    if(c->opt->option.delayed_lookup) {
        old=c->opt->session;
        c->opt->session=sess;
    } else { /* per-destination client cache */
        if(c->opt->connect_session) {
            old=c->opt->connect_session[c->idx];
            c->opt->connect_session[c->idx]=sess;
        } else {
            s_log(LOG_ERR, "INTERNAL ERROR: Uninitialized client session cache");
            old=NULL;
        }
    }
    CRYPTO_THREAD_unlock(stunnel_locks[LOCK_SESSION]);

    if(old)
        SSL_SESSION_free(old);
}
NOEXPORT void session_cache_save(CLI *c, SSL_SESSION *sess) {
    SSL_SESSION *old;

#if OPENSSL_VERSION_NUMBER>=0x10100000L
    SSL_SESSION_up_ref(sess);
#else
    sess=SSL_get1_session(c->ssl);
#endif

    CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_SESSION]);
    if(c->opt->option.delayed_lookup) {
        old=c->opt->session;
        c->opt->session=sess;
    } else {  
        if(c->opt->connect_session) {
            old=c->opt->connect_session[c->idx];
            c->opt->connect_session[c->idx]=sess;
        } else {
            s_log(LOG_ERR, "INTERNAL ERROR: Uninitialized client session cache");
            old=NULL;
        }
    }
    CRYPTO_THREAD_unlock(stunnel_locks[LOCK_SESSION]);

    if(old)
        SSL_SESSION_free(old);
}