Commit b82797a7 authored by Ian Craggs's avatar Ian Craggs

Merge branch 'skip-openssl-init' of https://github.com/loigu/paho.mqtt.c into fixes #136

Conflicts:
	src/MQTTAsync.c
	src/MQTTClient.c
	src/MQTTClient.h
	src/SSLSocket.c
	src/SSLSocket.h
parents 9fb30dec b8b562c7
......@@ -66,6 +66,13 @@
const char *client_timestamp_eye = "MQTTAsyncV3_Timestamp " BUILD_TIMESTAMP;
const char *client_version_eye = "MQTTAsyncV3_Version " CLIENT_VERSION;
void MQTTAsync_global_init(int handle_openssl_init)
{
#if defined(OPENSSL)
SSLSocket_handleOpensslInit(handle_openssl_init);
#endif
}
#if !defined(min)
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
......@@ -1395,8 +1402,9 @@ static void MQTTAsync_checkTimeouts(void)
/* check disconnect timeout */
if (m->c->connect_state == -2)
MQTTAsync_checkDisconnect(m, &m->disconnect);
/* check connect timeout */
else if (m->c->connect_state != 0 && MQTTAsync_elapsed(m->connect.start_time) > (m->connectTimeout * 1000))
if (m->c->connect_state != 0 && MQTTAsync_elapsed(m->connect.start_time) > (m->connectTimeout * 1000))
{
nextOrClose(m, MQTTASYNC_FAILURE, "TCP connect timeout");
continue;
......
......@@ -184,6 +184,13 @@
*/
#define MQTT_BAD_SUBSCRIBE 0x80
/**
* Global init of mqtt library. Call once on program start to set global behaviour.
* handle_openssl_init - if mqtt library should handle openssl init (1) or rely on the caller to init it before using mqtt (0)
*/
void MQTTAsync_global_init(int handle_openssl_init);
/**
* A handle representing an MQTT client. A valid client handle is available
* following a successful call to MQTTAsync_create().
......
......@@ -69,9 +69,17 @@
#include "VersionInfo.h"
const char *client_timestamp_eye = "MQTTClientV3_Timestamp " BUILD_TIMESTAMP;
const char *client_version_eye = "MQTTClientV3_Version " CLIENT_VERSION;
void MQTTClient_global_init(int handle_openssl_init)
{
#if defined(OPENSSL)
SSLSocket_handleOpensslInit(handle_openssl_init);
#endif
}
static ClientStates ClientState =
{
CLIENT_VERSION, /* version */
......
......@@ -189,6 +189,12 @@
*/
#define MQTT_BAD_SUBSCRIBE 0x80
/**
* Global init of mqtt library. Call once on program start to set global behaviour.
* handle_openssl_init - if mqtt library should handle openssl init (1) or rely on the caller to init it before using mqtt (0)
*/
void MQTTClient_global_init(int handle_openssl_init);
/**
* A handle representing an MQTT client. A valid client handle is available
* following a successful call to MQTTClient_create().
......
......@@ -68,6 +68,8 @@ int SSLSocket_createContext(networkHandles* net, MQTTClient_SSLOptions* opts);
void SSLSocket_destroyContext(networkHandles* net);
void SSLSocket_addPendingRead(int sock);
/// 1 ~ we are responsible for initializing openssl; 0 ~ openssl init is done externally
static int handle_openssl_init = 1;
static ssl_mutex_type* sslLocks = NULL;
static ssl_mutex_type sslCoreMutex;
......@@ -417,6 +419,13 @@ extern void SSLLocks_callback(int mode, int n, const char *file, int line)
}
}
void SSLSocket_handleOpensslInit(int bool_value)
{
handle_openssl_init = bool_value;
}
int SSLSocket_initialize(void)
{
int rc = 0;
......@@ -426,6 +435,8 @@ int SSLSocket_initialize(void)
FUNC_ENTRY;
if (handle_openssl_init)
{
if ((rc = SSL_library_init()) != 1)
rc = -1;
......@@ -461,6 +472,8 @@ int SSLSocket_initialize(void)
#endif
CRYPTO_set_locking_callback(SSLLocks_callback);
}
SSL_create_mutex(&sslCoreMutex);
exit:
......@@ -471,6 +484,9 @@ exit:
void SSLSocket_terminate(void)
{
FUNC_ENTRY;
if (handle_openssl_init)
{
EVP_cleanup();
ERR_free_strings();
CRYPTO_set_locking_callback(NULL);
......@@ -484,6 +500,10 @@ void SSLSocket_terminate(void)
}
free(sslLocks);
}
}
SSL_destroy_mutex(&sslCoreMutex);
FUNC_EXIT;
}
......
......@@ -31,9 +31,13 @@
#define URI_SSL "ssl://"
/** if we should handle openssl initialization (bool_value == 1) or depend on it to be initalized externally (bool_value == 0) */
void SSLSocket_handleOpensslInit(int bool_value);
int SSLSocket_initialize(void);
void SSLSocket_terminate(void);
int SSLSocket_setSocketForSSL(networkHandles* net, MQTTClient_SSLOptions* opts, char* hostname);
int SSLSocket_getch(SSL* ssl, int socket, char* c);
char *SSLSocket_getdata(SSL* ssl, int socket, size_t bytes, size_t* actual_len);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment