From ba897c9e948043821d034e4ab37003dceeb64dd9 Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Mon, 1 Aug 2016 19:12:58 -0700 Subject: [PATCH 1/7] Include getaddrinfo() in the connection timeout (only works with GNU) Signed-off-by: Chris Watts --- Makefile | 14 +++++++------- src/MQTTAsync.c | 9 ++++++++- src/MQTTClient.c | 9 ++++++++- src/MQTTProtocolOut.c | 16 ++++++++++++++++ src/MQTTProtocolOut.h | 12 ++++++++++-- src/Socket.c | 21 ++++++++++++++++++++- src/Socket.h | 6 ++++++ 7 files changed, 75 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index b4668ff4..13de3814 100755 --- a/Makefile +++ b/Makefile @@ -127,15 +127,15 @@ MQTTLIB_A_TARGET = ${blddir}/lib${MQTTLIB_A}.so.${VERSION} MQTTLIB_AS_TARGET = ${blddir}/lib${MQTTLIB_AS}.so.${VERSION} MQTTVERSION_TARGET = ${blddir}/MQTTVersion -CCFLAGS_SO = -g -fPIC $(CFLAGS) -Os -Wall -fvisibility=hidden -I$(blddir_work) -FLAGS_EXE = $(LDFLAGS) -I ${srcdir} -lpthread -L ${blddir} -FLAGS_EXES = $(LDFLAGS) -I ${srcdir} ${START_GROUP} -lpthread -lssl -lcrypto ${END_GROUP} -L ${blddir} +CCFLAGS_SO = -g -fPIC $(CFLAGS) -D_GNU_SOURCE -Os -Wall -fvisibility=hidden -I$(blddir_work) +FLAGS_EXE = $(LDFLAGS) -I ${srcdir} ${START_GROUP} -lpthread -lanl ${END_GROUP} -L ${blddir} +FLAGS_EXES = $(LDFLAGS) -I ${srcdir} ${START_GROUP} -lpthread -lanl -lssl -lcrypto ${END_GROUP} -L ${blddir} LDCONFIG ?= /sbin/ldconfig -LDFLAGS_C = $(LDFLAGS) -shared -Wl,-init,$(MQTTCLIENT_INIT) -lpthread -LDFLAGS_CS = $(LDFLAGS) -shared $(START_GROUP) -lpthread $(EXTRA_LIB) -lssl -lcrypto $(END_GROUP) -Wl,-init,$(MQTTCLIENT_INIT) -LDFLAGS_A = $(LDFLAGS) -shared -Wl,-init,$(MQTTASYNC_INIT) -lpthread -LDFLAGS_AS = $(LDFLAGS) -shared $(START_GROUP) -lpthread $(EXTRA_LIB) -lssl -lcrypto $(END_GROUP) -Wl,-init,$(MQTTASYNC_INIT) +LDFLAGS_C = $(LDFLAGS) -shared -Wl,-init,$(MQTTCLIENT_INIT) $(START_GROUP) -lpthread -lanl $(END_GROUP) +LDFLAGS_CS = $(LDFLAGS) -shared $(START_GROUP) -lpthread -lanl $(EXTRA_LIB) -lssl -lcrypto $(END_GROUP) -Wl,-init,$(MQTTCLIENT_INIT) +LDFLAGS_A = $(LDFLAGS) -shared -Wl,-init,$(MQTTASYNC_INIT) $(START_GROUP) -lpthread -lanl $(END_GROUP) +LDFLAGS_AS = $(LDFLAGS) -shared $(START_GROUP) -lpthread -lanl $(EXTRA_LIB) -lssl -lcrypto $(END_GROUP) -Wl,-init,$(MQTTASYNC_INIT) SED_COMMAND = sed \ -e "s/@CLIENT_VERSION@/${release.version}/g" \ diff --git a/src/MQTTAsync.c b/src/MQTTAsync.c index f475c27b..68a76030 100755 --- a/src/MQTTAsync.c +++ b/src/MQTTAsync.c @@ -38,7 +38,6 @@ * */ -#define _GNU_SOURCE /* for pthread_mutexattr_settype */ #include #if !defined(WIN32) && !defined(WIN64) #include @@ -1154,9 +1153,17 @@ int MQTTAsync_processCommand() Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, command->command.details.conn.MQTTVersion); #if defined(OPENSSL) +#if defined(__GNUC__) + rc = MQTTProtocol_connect(serverURI, command->client->c, command->client->ssl, command->command.details.conn.MQTTVersion, 0); +#else rc = MQTTProtocol_connect(serverURI, command->client->c, command->client->ssl, command->command.details.conn.MQTTVersion); +#endif +#else +#if defined(__GNUC__) + rc = MQTTProtocol_connect(serverURI, command->client->c, command->command.details.conn.MQTTVersion, 0); #else rc = MQTTProtocol_connect(serverURI, command->client->c, command->command.details.conn.MQTTVersion); +#endif #endif if (command->client->c->connect_state == 0) rc = SOCKET_ERROR; diff --git a/src/MQTTClient.c b/src/MQTTClient.c index b26bd06c..6a76a66c 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -37,7 +37,6 @@ * */ -#define _GNU_SOURCE /* for pthread_mutexattr_settype */ #include #if !defined(WIN32) && !defined(WIN64) #include @@ -807,9 +806,17 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, MQTTVersion); #if defined(OPENSSL) +#if defined(__GNUC__) + rc = MQTTProtocol_connect(serverURI, m->c, m->ssl, MQTTVersion, millisecsTimeout - MQTTClient_elapsed(start)); +#else rc = MQTTProtocol_connect(serverURI, m->c, m->ssl, MQTTVersion); +#endif +#else +#if defined(__GNUC__) + rc = MQTTProtocol_connect(serverURI, m->c, MQTTVersion, millisecsTimeout - MQTTClient_elapsed(start)); #else rc = MQTTProtocol_connect(serverURI, m->c, MQTTVersion); +#endif #endif if (rc == SOCKET_ERROR) goto exit; diff --git a/src/MQTTProtocolOut.c b/src/MQTTProtocolOut.c index b754c79d..e4ca5865 100755 --- a/src/MQTTProtocolOut.c +++ b/src/MQTTProtocolOut.c @@ -80,13 +80,22 @@ char* MQTTProtocol_addressPort(const char* uri, int* port) * @param aClient a structure with all MQTT data needed * @param int ssl * @param int MQTTVersion the MQTT version to connect with (3 or 4) + * @param long timeout how long to wait for a new socket to be created * @return return code */ #if defined(OPENSSL) +#if defined (__GNUC__) +int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int ssl, int MQTTVersion, long timeout) +#else int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int ssl, int MQTTVersion) +#endif +#else +#if defined (__GNUC__) +int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersion, long timeout) #else int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersion) #endif +#endif { int rc, port; char* addr; @@ -95,7 +104,14 @@ int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersi aClient->good = 1; addr = MQTTProtocol_addressPort(ip_address, &port); +#if defined(__GNUC__) + if (timeout < 0) + rc = -1; + else + rc = Socket_new(addr, port, &(aClient->net.socket), timeout); +#else rc = Socket_new(addr, port, &(aClient->net.socket)); +#endif if (rc == EINPROGRESS || rc == EWOULDBLOCK) aClient->connect_state = 1; /* TCP connect called - wait for connect completion */ else if (rc == 0) diff --git a/src/MQTTProtocolOut.h b/src/MQTTProtocolOut.h index 7cc9a3a6..72784f9d 100644 --- a/src/MQTTProtocolOut.h +++ b/src/MQTTProtocolOut.h @@ -31,9 +31,17 @@ void MQTTProtocol_reconnect(const char* ip_address, Clients* client); #if defined(OPENSSL) -int MQTTProtocol_connect(const char* ip_address, Clients* acClients, int ssl, int MQTTVersion); +#if defined (__GNUC__) +int MQTTProtocol_connect(const char* ip_address, Clients* aClients, int ssl, int MQTTVersion, long timeout); #else -int MQTTProtocol_connect(const char* ip_address, Clients* acClients, int MQTTVersion); +int MQTTProtocol_connect(const char* ip_address, Clients* aClients, int ssl, int MQTTVersion); +#endif +#else +#if defined (__GNUC__) +int MQTTProtocol_connect(const char* ip_address, Clients* aClients, int MQTTVersion, long timeout); +#else +int MQTTProtocol_connect(const char* ip_address, Clients* aClients, int MQTTVersion); +#endif #endif int MQTTProtocol_handlePingresps(void* pack, int sock); int MQTTProtocol_subscribe(Clients* client, List* topics, List* qoss, int msgID); diff --git a/src/Socket.c b/src/Socket.c index 536fb054..f6f9174f 100755 --- a/src/Socket.c +++ b/src/Socket.c @@ -582,9 +582,14 @@ void Socket_close(int socket) * @param addr the address string * @param port the TCP port * @param sock returns the new socket + * @param timeout the timeout in milliseconds * @return completion code */ +#if defined(__GNUC__) +int Socket_new(char* addr, int port, int* sock, long timeout) +#else int Socket_new(char* addr, int port, int* sock) +#endif { int type = SOCK_STREAM; struct sockaddr_in address; @@ -607,7 +612,21 @@ int Socket_new(char* addr, int port, int* sock) if (addr[0] == '[') ++addr; - if ((rc = getaddrinfo(addr, NULL, &hints, &result)) == 0) +#if defined(__GNUC__) + struct gaicb ar = {addr, NULL, &hints, result}; + struct gaicb *reqs[] = {&ar}; + + __time_t seconds = timeout / 1000L; + __syscall_slong_t nanos = (timeout - (seconds * 1000L)) * 1000000L; + struct timespec timeoutspec = {seconds, nanos}; + + rc = getaddrinfo_a(GAI_NOWAIT, reqs, 1, NULL); + gai_suspend((const struct gaicb* const *) reqs, 1, &timeoutspec); +#else + rc = getaddrinfo(addr, NULL, &hints, &result); +#endif + + if (rc == 0) { struct addrinfo* res = result; diff --git a/src/Socket.h b/src/Socket.h index e1ab2912..173244f7 100755 --- a/src/Socket.h +++ b/src/Socket.h @@ -118,7 +118,13 @@ int Socket_getch(int socket, char* c); char *Socket_getdata(int socket, size_t bytes, size_t* actual_len); int Socket_putdatas(int socket, char* buf0, size_t buf0len, int count, char** buffers, size_t* buflens, int* frees); void Socket_close(int socket); + +#if defined(__GNUC__) +/* able to use GNU's getaddrinfo_a to make timeouts possible */ +int Socket_new(char* addr, int port, int* socket, long timeout); +#else int Socket_new(char* addr, int port, int* socket); +#endif int Socket_noPendingWrites(int socket); char* Socket_getpeer(int sock); From 634b13718e06c37d918a6fc9a1a1098a9a3c66e3 Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Mon, 1 Aug 2016 19:45:53 -0700 Subject: [PATCH 2/7] Fix reliance on system types Signed-off-by: Chris Watts --- src/Socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Socket.c b/src/Socket.c index f6f9174f..b595bb9f 100755 --- a/src/Socket.c +++ b/src/Socket.c @@ -616,8 +616,8 @@ int Socket_new(char* addr, int port, int* sock) struct gaicb ar = {addr, NULL, &hints, result}; struct gaicb *reqs[] = {&ar}; - __time_t seconds = timeout / 1000L; - __syscall_slong_t nanos = (timeout - (seconds * 1000L)) * 1000000L; + unsigned long int seconds = timeout / 1000L; + unsigned long int nanos = (timeout - (seconds * 1000L)) * 1000000L; struct timespec timeoutspec = {seconds, nanos}; rc = getaddrinfo_a(GAI_NOWAIT, reqs, 1, NULL); From aff34749248fc531aabbe97e8ce3a480e2153d3e Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Mon, 1 Aug 2016 19:55:02 -0700 Subject: [PATCH 3/7] Prevent use of getaddrinfo_a() on OSX Signed-off-by: Chris Watts --- src/MQTTAsync.c | 4 ++-- src/MQTTClient.c | 4 ++-- src/MQTTProtocolOut.c | 2 +- src/Socket.c | 4 ++-- src/Socket.h | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/MQTTAsync.c b/src/MQTTAsync.c index 856388b1..1b6eacba 100755 --- a/src/MQTTAsync.c +++ b/src/MQTTAsync.c @@ -1153,13 +1153,13 @@ int MQTTAsync_processCommand() Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, command->command.details.conn.MQTTVersion); #if defined(OPENSSL) -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) rc = MQTTProtocol_connect(serverURI, command->client->c, command->client->ssl, command->command.details.conn.MQTTVersion, 0); #else rc = MQTTProtocol_connect(serverURI, command->client->c, command->client->ssl, command->command.details.conn.MQTTVersion); #endif #else -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) rc = MQTTProtocol_connect(serverURI, command->client->c, command->command.details.conn.MQTTVersion, 0); #else rc = MQTTProtocol_connect(serverURI, command->client->c, command->command.details.conn.MQTTVersion); diff --git a/src/MQTTClient.c b/src/MQTTClient.c index 6a76a66c..5f6bd412 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -806,13 +806,13 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, MQTTVersion); #if defined(OPENSSL) -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) rc = MQTTProtocol_connect(serverURI, m->c, m->ssl, MQTTVersion, millisecsTimeout - MQTTClient_elapsed(start)); #else rc = MQTTProtocol_connect(serverURI, m->c, m->ssl, MQTTVersion); #endif #else -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) rc = MQTTProtocol_connect(serverURI, m->c, MQTTVersion, millisecsTimeout - MQTTClient_elapsed(start)); #else rc = MQTTProtocol_connect(serverURI, m->c, MQTTVersion); diff --git a/src/MQTTProtocolOut.c b/src/MQTTProtocolOut.c index e4ca5865..f37c8211 100755 --- a/src/MQTTProtocolOut.c +++ b/src/MQTTProtocolOut.c @@ -104,7 +104,7 @@ int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersi aClient->good = 1; addr = MQTTProtocol_addressPort(ip_address, &port); -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) if (timeout < 0) rc = -1; else diff --git a/src/Socket.c b/src/Socket.c index b595bb9f..8e5072f3 100755 --- a/src/Socket.c +++ b/src/Socket.c @@ -585,7 +585,7 @@ void Socket_close(int socket) * @param timeout the timeout in milliseconds * @return completion code */ -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) int Socket_new(char* addr, int port, int* sock, long timeout) #else int Socket_new(char* addr, int port, int* sock) @@ -612,7 +612,7 @@ int Socket_new(char* addr, int port, int* sock) if (addr[0] == '[') ++addr; -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) struct gaicb ar = {addr, NULL, &hints, result}; struct gaicb *reqs[] = {&ar}; diff --git a/src/Socket.h b/src/Socket.h index 173244f7..27e495b7 100755 --- a/src/Socket.h +++ b/src/Socket.h @@ -119,7 +119,7 @@ char *Socket_getdata(int socket, size_t bytes, size_t* actual_len); int Socket_putdatas(int socket, char* buf0, size_t buf0len, int count, char** buffers, size_t* buflens, int* frees); void Socket_close(int socket); -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) /* able to use GNU's getaddrinfo_a to make timeouts possible */ int Socket_new(char* addr, int port, int* socket, long timeout); #else From 187a1b81bf3631095ec1cf96d1c49717271e71cc Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Mon, 1 Aug 2016 19:55:02 -0700 Subject: [PATCH 4/7] Prevent use of getaddrinfo_a() on OSX Signed-off-by: Chris Watts --- src/MQTTAsync.c | 4 ++-- src/MQTTClient.c | 4 ++-- src/MQTTProtocolOut.c | 6 +++--- src/Socket.c | 4 ++-- src/Socket.h | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/MQTTAsync.c b/src/MQTTAsync.c index 856388b1..1b6eacba 100755 --- a/src/MQTTAsync.c +++ b/src/MQTTAsync.c @@ -1153,13 +1153,13 @@ int MQTTAsync_processCommand() Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, command->command.details.conn.MQTTVersion); #if defined(OPENSSL) -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) rc = MQTTProtocol_connect(serverURI, command->client->c, command->client->ssl, command->command.details.conn.MQTTVersion, 0); #else rc = MQTTProtocol_connect(serverURI, command->client->c, command->client->ssl, command->command.details.conn.MQTTVersion); #endif #else -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) rc = MQTTProtocol_connect(serverURI, command->client->c, command->command.details.conn.MQTTVersion, 0); #else rc = MQTTProtocol_connect(serverURI, command->client->c, command->command.details.conn.MQTTVersion); diff --git a/src/MQTTClient.c b/src/MQTTClient.c index 6a76a66c..5f6bd412 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -806,13 +806,13 @@ int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* o Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, MQTTVersion); #if defined(OPENSSL) -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) rc = MQTTProtocol_connect(serverURI, m->c, m->ssl, MQTTVersion, millisecsTimeout - MQTTClient_elapsed(start)); #else rc = MQTTProtocol_connect(serverURI, m->c, m->ssl, MQTTVersion); #endif #else -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) rc = MQTTProtocol_connect(serverURI, m->c, MQTTVersion, millisecsTimeout - MQTTClient_elapsed(start)); #else rc = MQTTProtocol_connect(serverURI, m->c, MQTTVersion); diff --git a/src/MQTTProtocolOut.c b/src/MQTTProtocolOut.c index e4ca5865..f5370875 100755 --- a/src/MQTTProtocolOut.c +++ b/src/MQTTProtocolOut.c @@ -84,13 +84,13 @@ char* MQTTProtocol_addressPort(const char* uri, int* port) * @return return code */ #if defined(OPENSSL) -#if defined (__GNUC__) +#if defined(__GNUC__) && defined(__linux__) int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int ssl, int MQTTVersion, long timeout) #else int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int ssl, int MQTTVersion) #endif #else -#if defined (__GNUC__) +#if defined(__GNUC__) && defined(__linux__) int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersion, long timeout) #else int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersion) @@ -104,7 +104,7 @@ int MQTTProtocol_connect(const char* ip_address, Clients* aClient, int MQTTVersi aClient->good = 1; addr = MQTTProtocol_addressPort(ip_address, &port); -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) if (timeout < 0) rc = -1; else diff --git a/src/Socket.c b/src/Socket.c index b595bb9f..8e5072f3 100755 --- a/src/Socket.c +++ b/src/Socket.c @@ -585,7 +585,7 @@ void Socket_close(int socket) * @param timeout the timeout in milliseconds * @return completion code */ -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) int Socket_new(char* addr, int port, int* sock, long timeout) #else int Socket_new(char* addr, int port, int* sock) @@ -612,7 +612,7 @@ int Socket_new(char* addr, int port, int* sock) if (addr[0] == '[') ++addr; -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) struct gaicb ar = {addr, NULL, &hints, result}; struct gaicb *reqs[] = {&ar}; diff --git a/src/Socket.h b/src/Socket.h index 173244f7..27e495b7 100755 --- a/src/Socket.h +++ b/src/Socket.h @@ -119,7 +119,7 @@ char *Socket_getdata(int socket, size_t bytes, size_t* actual_len); int Socket_putdatas(int socket, char* buf0, size_t buf0len, int count, char** buffers, size_t* buflens, int* frees); void Socket_close(int socket); -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__linux__) /* able to use GNU's getaddrinfo_a to make timeouts possible */ int Socket_new(char* addr, int port, int* socket, long timeout); #else From f3ba738a976f729d59e0012ebc21fa2158f755eb Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Mon, 1 Aug 2016 20:10:32 -0700 Subject: [PATCH 5/7] Only add -lanl on Linux Signed-off-by: Chris Watts --- Makefile | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 13de3814..18883dc7 100755 --- a/Makefile +++ b/Makefile @@ -128,14 +128,14 @@ MQTTLIB_AS_TARGET = ${blddir}/lib${MQTTLIB_AS}.so.${VERSION} MQTTVERSION_TARGET = ${blddir}/MQTTVersion CCFLAGS_SO = -g -fPIC $(CFLAGS) -D_GNU_SOURCE -Os -Wall -fvisibility=hidden -I$(blddir_work) -FLAGS_EXE = $(LDFLAGS) -I ${srcdir} ${START_GROUP} -lpthread -lanl ${END_GROUP} -L ${blddir} -FLAGS_EXES = $(LDFLAGS) -I ${srcdir} ${START_GROUP} -lpthread -lanl -lssl -lcrypto ${END_GROUP} -L ${blddir} +FLAGS_EXE = $(LDFLAGS) -I ${srcdir} ${START_GROUP} -lpthread ${GAI_LIB} ${END_GROUP} -L ${blddir} +FLAGS_EXES = $(LDFLAGS) -I ${srcdir} ${START_GROUP} -lpthread ${GAI_LIB} -lssl -lcrypto ${END_GROUP} -L ${blddir} LDCONFIG ?= /sbin/ldconfig -LDFLAGS_C = $(LDFLAGS) -shared -Wl,-init,$(MQTTCLIENT_INIT) $(START_GROUP) -lpthread -lanl $(END_GROUP) -LDFLAGS_CS = $(LDFLAGS) -shared $(START_GROUP) -lpthread -lanl $(EXTRA_LIB) -lssl -lcrypto $(END_GROUP) -Wl,-init,$(MQTTCLIENT_INIT) -LDFLAGS_A = $(LDFLAGS) -shared -Wl,-init,$(MQTTASYNC_INIT) $(START_GROUP) -lpthread -lanl $(END_GROUP) -LDFLAGS_AS = $(LDFLAGS) -shared $(START_GROUP) -lpthread -lanl $(EXTRA_LIB) -lssl -lcrypto $(END_GROUP) -Wl,-init,$(MQTTASYNC_INIT) +LDFLAGS_C = $(LDFLAGS) -shared -Wl,-init,$(MQTTCLIENT_INIT) $(START_GROUP) -lpthread $(GAI_LIB) $(END_GROUP) +LDFLAGS_CS = $(LDFLAGS) -shared $(START_GROUP) -lpthread $(GAI_LIB) $(EXTRA_LIB) -lssl -lcrypto $(END_GROUP) -Wl,-init,$(MQTTCLIENT_INIT) +LDFLAGS_A = $(LDFLAGS) -shared -Wl,-init,$(MQTTASYNC_INIT) $(START_GROUP) -lpthread $(GAI_LIB) $(END_GROUP) +LDFLAGS_AS = $(LDFLAGS) -shared $(START_GROUP) -lpthread $(GAI_LIB) $(EXTRA_LIB) -lssl -lcrypto $(END_GROUP) -Wl,-init,$(MQTTASYNC_INIT) SED_COMMAND = sed \ -e "s/@CLIENT_VERSION@/${release.version}/g" \ @@ -148,6 +148,7 @@ MQTTASYNC_INIT = MQTTAsync_init START_GROUP = -Wl,--start-group END_GROUP = -Wl,--end-group +GAI_LIB = -lanl EXTRA_LIB = -ldl LDFLAGS_C += -Wl,-soname,lib$(MQTTLIB_C).so.${MAJOR_VERSION} @@ -162,6 +163,7 @@ MQTTASYNC_INIT = _MQTTAsync_init START_GROUP = END_GROUP = +GAI_LIB = EXTRA_LIB = -ldl CCFLAGS_SO += -Wno-deprecated-declarations -DUSE_NAMED_SEMAPHORES From 79739ea13c7dee583835610e6874ebb3c7d21466 Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Tue, 2 Aug 2016 11:52:13 -0700 Subject: [PATCH 6/7] Fix reading async DNS result from the wrong pointer Signed-off-by: Chris Watts --- src/Socket.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Socket.c b/src/Socket.c index 8e5072f3..fe4c0cd6 100755 --- a/src/Socket.c +++ b/src/Socket.c @@ -613,7 +613,7 @@ int Socket_new(char* addr, int port, int* sock) ++addr; #if defined(__GNUC__) && defined(__linux__) - struct gaicb ar = {addr, NULL, &hints, result}; + struct gaicb ar = {addr, NULL, &hints, NULL}; struct gaicb *reqs[] = {&ar}; unsigned long int seconds = timeout / 1000L; @@ -621,7 +621,9 @@ int Socket_new(char* addr, int port, int* sock) struct timespec timeoutspec = {seconds, nanos}; rc = getaddrinfo_a(GAI_NOWAIT, reqs, 1, NULL); - gai_suspend((const struct gaicb* const *) reqs, 1, &timeoutspec); + if (rc == 0) + gai_suspend((const struct gaicb* const *) reqs, 1, &timeoutspec); + result = ar.ar_result; #else rc = getaddrinfo(addr, NULL, &hints, &result); #endif From b726275116ceb728797f071815d5478cc33ed4d8 Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Tue, 2 Aug 2016 11:56:21 -0700 Subject: [PATCH 7/7] Retrieve more useful RC number from getaddrinfo_a() Signed-off-by: Chris Watts --- src/Socket.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Socket.c b/src/Socket.c index fe4c0cd6..b71df449 100755 --- a/src/Socket.c +++ b/src/Socket.c @@ -622,8 +622,13 @@ int Socket_new(char* addr, int port, int* sock) rc = getaddrinfo_a(GAI_NOWAIT, reqs, 1, NULL); if (rc == 0) - gai_suspend((const struct gaicb* const *) reqs, 1, &timeoutspec); - result = ar.ar_result; + rc = gai_suspend((const struct gaicb* const *) reqs, 1, &timeoutspec); + + if (rc == 0) + { + rc = gai_error(reqs[0]); + result = ar.ar_result; + } #else rc = getaddrinfo(addr, NULL, &hints, &result); #endif