Skip to content

Commit

Permalink
ssl session caching: fix compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
yangtse committed Jan 18, 2012
1 parent d1becc3 commit d56b4c3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 53 deletions.
16 changes: 6 additions & 10 deletions lib/share.c
Expand Up @@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -88,8 +88,8 @@ curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
case CURL_LOCK_DATA_SSL_SESSION:
#ifdef USE_SSL
if(!share->sslsession) {
share->nsslsession = 8;
share->sslsession = calloc(share->nsslsession,
share->max_ssl_sessions = 8;
share->sslsession = calloc(share->max_ssl_sessions,
sizeof(struct curl_ssl_session));
share->sessionage = 0;
if(!share->sslsession)
Expand Down Expand Up @@ -132,11 +132,7 @@ curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)

case CURL_LOCK_DATA_SSL_SESSION:
#ifdef USE_SSL
if(share->sslsession) {
free(share->sslsession);
share->sslsession = NULL;
share->nsslsession = 0;
}
Curl_safefree(share->sslsession);
break;
#else
return CURLSHE_NOT_BUILT_IN;
Expand Down Expand Up @@ -202,8 +198,8 @@ curl_share_cleanup(CURLSH *sh)

#ifdef USE_SSL
if(share->sslsession) {
unsigned int i;
for(i = 0; i < share->nsslsession; ++i)
size_t i;
for(i = 0; i < share->max_ssl_sessions; i++)
Curl_ssl_kill_session(&(share->sslsession[i]));
free(share->sslsession);
}
Expand Down
11 changes: 5 additions & 6 deletions lib/share.h
@@ -1,14 +1,13 @@
#ifndef __CURL_SHARE_H
#define __CURL_SHARE_H

#ifndef HEADER_CURL_SHARE_H
#define HEADER_CURL_SHARE_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -51,12 +50,12 @@ struct Curl_share {
#endif

struct curl_ssl_session *sslsession;
unsigned int nsslsession;
size_t max_ssl_sessions;
long sessionage;
};

CURLSHcode Curl_share_lock (struct SessionHandle *, curl_lock_data,
curl_lock_access);
CURLSHcode Curl_share_unlock (struct SessionHandle *, curl_lock_data);

#endif /* __CURL_SHARE_H */
#endif /* HEADER_CURL_SHARE_H */
39 changes: 16 additions & 23 deletions lib/sslgen.c
Expand Up @@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -235,7 +235,7 @@ int Curl_ssl_getsessionid(struct connectdata *conn,
{
struct curl_ssl_session *check;
struct SessionHandle *data = conn->data;
long i;
size_t i;
long *general_age;
bool no_match = TRUE;

Expand All @@ -253,7 +253,7 @@ int Curl_ssl_getsessionid(struct connectdata *conn,
else
general_age = &data->state.sessionage;

for(i=0; i< data->set.ssl.numsessions; i++) {
for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
check = &data->state.session[i];
if(!check->sessionid)
/* not session ID means blank entry */
Expand Down Expand Up @@ -282,41 +282,35 @@ int Curl_ssl_getsessionid(struct connectdata *conn,
/*
* Kill a single session ID entry in the cache.
*/
int Curl_ssl_kill_session(struct curl_ssl_session *session)
void Curl_ssl_kill_session(struct curl_ssl_session *session)
{
if(session->sessionid) {
/* defensive check */

/* free the ID the SSL-layer specific way */
curlssl_session_free(session->sessionid);

session->sessionid=NULL;
session->sessionid = NULL;
session->age = 0; /* fresh */

Curl_free_ssl_config(&session->ssl_config);

Curl_safefree(session->name);
session->name = NULL; /* no name */

return 0; /* ok */
}
else
return 1;
}

/*
* Delete the given session ID from the cache.
*/
void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid)
{
int i;
size_t i;
struct SessionHandle *data=conn->data;

if(SSLSESSION_SHARED(data))
Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION,
CURL_LOCK_ACCESS_SINGLE);
Curl_share_lock(data, CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE);

for(i=0; i< data->set.ssl.numsessions; i++) {
for(i = 0; i < data->set.ssl.max_ssl_sessions; i++) {
struct curl_ssl_session *check = &data->state.session[i];

if(check->sessionid == ssl_sessionid) {
Expand All @@ -339,7 +333,7 @@ CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
void *ssl_sessionid,
size_t idsize)
{
long i;
size_t i;
struct SessionHandle *data=conn->data; /* the mother of all structs */
struct curl_ssl_session *store = &data->state.session[0];
long oldest_age=data->state.session[0].age; /* zero if unused */
Expand Down Expand Up @@ -367,14 +361,14 @@ CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
}

/* find an empty slot for us, or find the oldest */
for(i=1; (i<data->set.ssl.numsessions) &&
for(i = 1; (i < data->set.ssl.max_ssl_sessions) &&
data->state.session[i].sessionid; i++) {
if(data->state.session[i].age < oldest_age) {
oldest_age = data->state.session[i].age;
store = &data->state.session[i];
}
}
if(i == data->set.ssl.numsessions)
if(i == data->set.ssl.max_ssl_sessions)
/* cache is full, we must "kill" the oldest entry! */
Curl_ssl_kill_session(store);
else
Expand Down Expand Up @@ -407,16 +401,15 @@ CURLcode Curl_ssl_addsessionid(struct connectdata *conn,

void Curl_ssl_close_all(struct SessionHandle *data)
{
long i;
size_t i;
/* kill the session ID cache if not shared */
if(data->state.session && !SSLSESSION_SHARED(data)) {
for(i=0; i< data->set.ssl.numsessions; i++)
for(i = 0; i < data->set.ssl.max_ssl_sessions; i++)
/* the single-killer function handles empty table slots */
Curl_ssl_kill_session(&data->state.session[i]);

/* free the cache data */
free(data->state.session);
data->state.session = NULL;
Curl_safefree(data->state.session);
}

curlssl_close_all(data);
Expand Down Expand Up @@ -466,7 +459,7 @@ struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data)
* This sets up a session ID cache to the specified size. Make sure this code
* is agnostic to what underlying SSL technology we use.
*/
CURLcode Curl_ssl_initsessions(struct SessionHandle *data, long amount)
CURLcode Curl_ssl_initsessions(struct SessionHandle *data, size_t amount)
{
struct curl_ssl_session *session;

Expand All @@ -479,7 +472,7 @@ CURLcode Curl_ssl_initsessions(struct SessionHandle *data, long amount)
return CURLE_OUT_OF_MEMORY;

/* store the info in the SSL section */
data->set.ssl.numsessions = amount;
data->set.ssl.max_ssl_sessions = amount;
data->state.session = session;
data->state.sessionage = 1; /* this is brand new */
return CURLE_OK;
Expand Down
8 changes: 4 additions & 4 deletions lib/sslgen.h
Expand Up @@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -47,7 +47,7 @@ CURLcode Curl_ssl_set_engine_default(struct SessionHandle *data);
struct curl_slist *Curl_ssl_engines_list(struct SessionHandle *data);

/* init the SSL session ID cache */
CURLcode Curl_ssl_initsessions(struct SessionHandle *, long);
CURLcode Curl_ssl_initsessions(struct SessionHandle *, size_t);
size_t Curl_ssl_version(char *buffer, size_t size);
bool Curl_ssl_data_pending(const struct connectdata *conn,
int connindex);
Expand All @@ -65,7 +65,7 @@ CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
void *ssl_sessionid,
size_t idsize);
/* Kill a single session ID entry in the cache */
int Curl_ssl_kill_session(struct curl_ssl_session *session);
void Curl_ssl_kill_session(struct curl_ssl_session *session);
/* delete a session from the cache */
void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid);

Expand All @@ -90,7 +90,7 @@ void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid);
#define Curl_ssl_check_cxn(x) 0
#define Curl_ssl_free_certinfo(x) Curl_nop_stmt
#define Curl_ssl_connect_nonblocking(x,y,z) CURLE_NOT_BUILT_IN
#define Curl_ssl_kill_session(x) 0
#define Curl_ssl_kill_session(x) Curl_nop_stmt
#endif

#endif /* HEADER_CURL_SSLGEN_H */
4 changes: 2 additions & 2 deletions lib/transfer.c
Expand Up @@ -1420,9 +1420,9 @@ CURLcode Curl_pretransfer(struct SessionHandle *data)
}

/* Init the SSL session ID cache here. We do it here since we want to do it
after the *_setopt() calls (that could change the size of the cache) but
after the *_setopt() calls (that could specify the size of the cache) but
before any transfer takes place. */
res = Curl_ssl_initsessions(data, data->set.ssl.numsessions);
res = Curl_ssl_initsessions(data, data->set.ssl.max_ssl_sessions);
if(res)
return res;

Expand Down
8 changes: 3 additions & 5 deletions lib/url.c
Expand Up @@ -683,7 +683,7 @@ CURLcode Curl_init_userdefined(struct UserDefined *set)
set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */

/* Set the default size of the SSL session ID cache */
set->ssl.numsessions = 5;
set->ssl.max_ssl_sessions = 5;

set->proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
Expand Down Expand Up @@ -2106,10 +2106,8 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
data->cookies = NULL;
#endif

if(data->share->sslsession == data->state.session) {
if(data->share->sslsession == data->state.session)
data->state.session = NULL;
data->set.ssl.numsessions = 0;
}

data->share->dirty--;

Expand Down Expand Up @@ -2143,7 +2141,7 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
}
#endif /* CURL_DISABLE_HTTP */
if(data->share->sslsession) {
data->set.ssl.numsessions = data->share->nsslsession;
data->set.ssl.max_ssl_sessions = data->share->max_ssl_sessions;
data->state.session = data->share->sslsession;
}
Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
Expand Down
6 changes: 3 additions & 3 deletions lib/urldata.h
Expand Up @@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -299,7 +299,7 @@ struct ssl_config_data {
char *random_file; /* path to file containing "random" data */
char *egdsocket; /* path to file containing the EGD daemon socket */
char *cipher_list; /* list of ciphers to use */
long numsessions; /* SSL session id cache size */
size_t max_ssl_sessions; /* SSL session id cache size */
curl_ssl_ctx_callback fsslctx; /* function to initialize ssl ctx */
void *fsslctxp; /* parameter for call back */
bool sessionid; /* cache session IDs or not */
Expand Down Expand Up @@ -1140,7 +1140,7 @@ struct UrlState {
following not keep sending user+password... This is
strdup() data.
*/
struct curl_ssl_session *session; /* array of 'numsessions' size */
struct curl_ssl_session *session; /* array of 'max_ssl_sessions' size */
long sessionage; /* number of the most recent session */
char *tempwrite; /* allocated buffer to keep data in when a write
callback returns to make the connection paused */
Expand Down

0 comments on commit d56b4c3

Please sign in to comment.