Skip to content

Commit 898ab72

Browse files
authored
Update mqtt lib, add ssl params. TODO ssl linking. Ref #51 (#52)
1 parent d34b3b6 commit 898ab72

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MQTT is used commonly for constrained devices with low-bandwidth, high-latency o
2323
### Requirements
2424

2525
* kdb+ >= 3.5 64-bit(Linux/MacOS/Windows) and 32-bit ARM
26-
* [paho.mqtt.c](https://github.com/eclipse/paho.mqtt.c) >= 1.3.2
26+
* [paho.mqtt.c](https://github.com/eclipse/paho.mqtt.c) >= 1.3.11
2727
* CMake >= 3.1 [^1]
2828

2929
[^1]: Required when building from source

docker_linux/Dockerfile.centos7

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ENV QHOME /q
1818
ENV PATH /q/l64:$PATH
1919
ENV LD_LIBRARY_PATH /usr/local/lib:$LD_LIBRARY_PATH
2020

21-
RUN cd /source && wget https://github.com/eclipse/paho.mqtt.c/releases/download/v1.3.2/Eclipse-Paho-MQTT-C-1.3.2-Linux.tar.gz && tar xvf Eclipse-Paho-MQTT-C-1.3.2-Linux.tar.gz -C ./paho.mqtt.c --strip-components=1
21+
RUN cd /source && wget https://github.com/eclipse/paho.mqtt.c/releases/download/v1.3.11/Eclipse-Paho-MQTT-C-1.3.11-Linux.tar.gz && tar xvf Eclipse-Paho-MQTT-C-1.3.11-Linux.tar.gz -C ./paho.mqtt.c --strip-components=1
2222
ENV BUILD_HOME /source/paho.mqtt.c
2323

2424
COPY mqtt_build.sh /source

src/mqtt.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ static char* getIntFromList(K propValues,int row, int* value, char* errStr)
7373
return 0;
7474
}
7575

76+
static int ssl_error_cb(const char *str, size_t len, void *u) {
77+
fprintf(stderr,"mqtt ssl error: %.*s\n",(int)len,str);
78+
return 0;
79+
}
80+
81+
static void freeOpts(MQTTClient_connectOptions* conn_opts){
82+
free((void*)conn_opts->username);
83+
free((void*)conn_opts->password);
84+
free((void*)conn_opts->ssl->trustStore);
85+
free((void*)conn_opts->ssl->keyStore);
86+
free((void*)conn_opts->ssl->privateKey);
87+
free((void*)conn_opts->ssl->privateKeyPassword);
88+
free((void*)conn_opts->ssl->enabledCipherSuites);
89+
free((void*)conn_opts->ssl->CApath);
90+
}
91+
7692
/* Establish a tcp connection from a q process to mqtt client
7793
* tcpconn = tcp connection being connected to (symbol)
7894
* pname = name to be associated with the connecting process (symbol)
@@ -90,6 +106,7 @@ EXP K connX(K tcpconn,K pname, K opt){
90106

91107
MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer;
92108
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
109+
MQTTClient_SSLOptions ssl_opts = MQTTClient_SSLOptions_initializer;
93110

94111
K propNames = (kK(opt)[0]);
95112
K propValues = (kK(opt)[1]);
@@ -106,7 +123,6 @@ EXP K connX(K tcpconn,K pname, K opt){
106123
{
107124
if ((kS(propNames)[row])[0] == '\0')
108125
continue;
109-
110126
if (strcmp(kS(propNames)[row],"username")==0)
111127
errStr = getStringFromList(propValues,row,&conn_opts.username,"username type incorrect");
112128
else if (strcmp(kS(propNames)[row],"password")==0)
@@ -136,28 +152,40 @@ EXP K connX(K tcpconn,K pname, K opt){
136152
errStr = getCharArrayAsStringFromList(propValues,row,&will_opts.message,"lastWillMessage type incorrect");
137153
else if (strcmp(kS(propNames)[row],"lastWillRetain")==0)
138154
errStr = getIntFromList(propValues,row,&will_opts.retained,"lastWillRetain type incorrect");
155+
else if (strcmp(kS(propNames)[row],"trustStore")==0)
156+
errStr = getStringFromList(propValues,row,&ssl_opts.trustStore,"trustStore type incorrect");
157+
else if (strcmp(kS(propNames)[row],"keyStore")==0)
158+
errStr = getStringFromList(propValues,row,&ssl_opts.keyStore,"keyStore type incorrect");
159+
else if (strcmp(kS(propNames)[row],"privateKey")==0)
160+
errStr = getStringFromList(propValues,row,&ssl_opts.privateKey,"privateKey type incorrect");
161+
else if (strcmp(kS(propNames)[row],"privateKeyPassword")==0)
162+
errStr = getStringFromList(propValues,row,&ssl_opts.privateKeyPassword,"privateKeyPassword type incorrect");
163+
else if (strcmp(kS(propNames)[row],"enabledCipherSuites")==0)
164+
errStr = getStringFromList(propValues,row,&ssl_opts.enabledCipherSuites,"enabledCipherSuites type incorrect");
165+
else if (strcmp(kS(propNames)[row],"enableServerCertAuth")==0)
166+
errStr = getIntFromList(propValues,row,&ssl_opts.enableServerCertAuth,"enableServerCertAuth type incorrect");
167+
else if (strcmp(kS(propNames)[row],"sslVersion")==0)
168+
errStr = getIntFromList(propValues,row,&ssl_opts.sslVersion,"sslVersion type incorrect");
169+
else if (strcmp(kS(propNames)[row],"verify")==0)
170+
errStr = getIntFromList(propValues,row,&ssl_opts.verify,"verify type incorrect");
171+
else if (strcmp(kS(propNames)[row],"CApath")==0)
172+
errStr = getStringFromList(propValues,row,&ssl_opts.CApath,"CApath type incorrect");
139173
else
140174
errStr = "Unsupported conn opt name in dictionary";
141175
}
142176

143-
if (errStr != 0)
144-
{
145-
free((void*)conn_opts.username);
146-
free((void*)conn_opts.password);
147-
return krr(errStr);
148-
}
177+
ssl_opts.ssl_error_cb = *ssl_error_cb;
178+
conn_opts.ssl = &ssl_opts;
179+
180+
if(errStr)
181+
return freeOpts(&conn_opts),krr(errStr);
149182

150183
if(MQTTCLIENT_SUCCESS != (err = MQTTClient_create(&client, tcpconn->s, pname->s, MQTTCLIENT_PERSISTENCE_NONE, NULL)))
151-
{
152-
free((void*)conn_opts.username);
153-
free((void*)conn_opts.password);
154-
return krr((S)MQTTClient_strerror(err));
155-
}
184+
return freeOpts(&conn_opts),krr((S)MQTTClient_strerror(err));
156185

157186
MQTTClient_setCallbacks(client, NULL, disconn, msgrcvd, msgsent);
158187
err = MQTTClient_connect(client, &conn_opts);
159-
free((void*)conn_opts.username);
160-
free((void*)conn_opts.password);
188+
freeOpts(&conn_opts);
161189

162190
if(MQTTCLIENT_SUCCESS != err)
163191
return krr((S)MQTTClient_strerror(err));

travis_setup.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
mkdir cbuild
44

55
if [ "$TRAVIS_OS_NAME" == "osx" ]; then
6-
wget https://github.com/eclipse/paho.mqtt.c/releases/download/v1.3.2/Eclipse-Paho-MQTT-C-1.3.2-Darwin.tar.gz
7-
tar xvf Eclipse-Paho-MQTT-C-1.3.2-Darwin.tar.gz -C ./cbuild --strip-components=1
6+
wget https://github.com/eclipse/paho.mqtt.c/releases/download/v1.3.11/Eclipse-Paho-MQTT-C-1.3.11-Darwin.tar.gz
7+
tar xvf Eclipse-Paho-MQTT-C-1.3.11-Darwin.tar.gz -C ./cbuild --strip-components=1
88
elif [ "$TRAVIS_OS_NAME" == "linux" ]; then
9-
wget https://github.com/eclipse/paho.mqtt.c/releases/download/v1.3.2/Eclipse-Paho-MQTT-C-1.3.2-Linux.tar.gz
10-
tar xvf Eclipse-Paho-MQTT-C-1.3.2-Linux.tar.gz -C ./cbuild --strip-components=1
9+
wget https://github.com/eclipse/paho.mqtt.c/releases/download/v1.3.11/Eclipse-Paho-MQTT-C-1.3.11-Linux.tar.gz
10+
tar xvf Eclipse-Paho-MQTT-C-1.3.11-Linux.tar.gz -C ./cbuild --strip-components=1
1111
elif [ "$TRAVIS_OS_NAME" == "windows" ]; then
12-
wget https://github.com/eclipse/paho.mqtt.c/releases/download/v1.3.2/eclipse-paho-mqtt-c-win64-1.3.2.zip
13-
7z x -ocbuild eclipse-paho-mqtt-c-win64-1.3.2.zip
12+
wget https://github.com/eclipse/paho.mqtt.c/releases/download/v1.3.11/eclipse-paho-mqtt-c-win64-1.3.11.zip
13+
7z x -ocbuild eclipse-paho-mqtt-c-win64-1.3.11.zip
1414
else
1515
echo "$TRAVIS_OS_NAME is currently not supported"
1616
fi

0 commit comments

Comments
 (0)