-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathmqtt_socket.c
565 lines (488 loc) · 16.2 KB
/
mqtt_socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* mqtt_socket.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfMQTT.
*
* wolfMQTT is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfMQTT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Include the autoconf generated config.h */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef WOLFMQTT_NONBLOCK
/* need EWOULDBLOCK and EAGAIN */
#if defined(MICROCHIP_MPLAB_HARMONY) && \
((__XC32_VERSION < 4000) || (__XC32_VERSION == 243739000))
/* xc32 versions >= v4.0 no longer have sys/errno.h */
#include <sys/errno.h>
#endif
#include <errno.h>
#endif
#ifdef ENABLE_MQTT_CURL
#include <curl/curl.h>
#endif
#include "wolfmqtt/mqtt_client.h"
#include "wolfmqtt/mqtt_socket.h"
/* Options */
#ifdef WOLFMQTT_NO_STDIO
#undef WOLFMQTT_DEBUG_SOCKET
#endif
/* lwip */
#ifdef WOLFSSL_LWIP
#undef read
#undef write
#undef connect
#endif
/* Public Functions */
#if defined(ENABLE_MQTT_TLS) && !defined(ENABLE_MQTT_CURL)
int MqttSocket_TlsSocketReceive(WOLFSSL* ssl, char *buf, int sz,
void *ptr)
{
int rc;
MqttClient *client = (MqttClient*)ptr;
(void)ssl; /* Not used */
rc = client->net->read(client->net->context, (byte*)buf, sz,
client->tls.timeout_ms_read);
/* save network read response */
client->tls.sockRcRead = rc;
if (rc == 0 || rc == MQTT_CODE_ERROR_TIMEOUT || rc == MQTT_CODE_STDIN_WAKE
|| rc == MQTT_CODE_CONTINUE) {
rc = WOLFSSL_CBIO_ERR_WANT_READ;
}
else if (rc < 0) {
rc = WOLFSSL_CBIO_ERR_GENERAL;
}
return rc;
}
int MqttSocket_TlsSocketSend(WOLFSSL* ssl, char *buf, int sz,
void *ptr)
{
int rc;
MqttClient *client = (MqttClient*)ptr;
(void)ssl; /* Not used */
rc = client->net->write(client->net->context, (byte*)buf, sz,
client->tls.timeout_ms_write);
/* save network write response */
client->tls.sockRcWrite = rc;
if (rc == 0 || rc == MQTT_CODE_ERROR_TIMEOUT || rc == MQTT_CODE_CONTINUE) {
rc = WOLFSSL_CBIO_ERR_WANT_WRITE;
}
else if (rc < 0) {
rc = WOLFSSL_CBIO_ERR_GENERAL;
}
return rc;
}
#endif /* ENABLE_MQTT_TLS && !ENABLE_MQTT_CURL*/
int MqttSocket_Init(MqttClient *client, MqttNet *net)
{
int rc = MQTT_CODE_ERROR_BAD_ARG;
if (client) {
#ifdef ENABLE_MQTT_CURL
curl_global_init(CURL_GLOBAL_DEFAULT);
#endif
client->net = net;
MqttClient_Flags(client, (MQTT_CLIENT_FLAG_IS_CONNECTED |
MQTT_CLIENT_FLAG_IS_TLS), 0);;
#if defined(ENABLE_MQTT_TLS) && !defined(ENABLE_MQTT_CURL)
client->tls.ctx = NULL;
client->tls.ssl = NULL;
client->tls.timeout_ms_read = client->cmd_timeout_ms;
client->tls.timeout_ms_write = client->cmd_timeout_ms;
#endif
/* Validate callbacks are not null! */
if (net && net->connect && net->read && net->write && net->disconnect) {
rc = MQTT_CODE_SUCCESS;
}
}
return rc;
}
static int MqttSocket_WriteDo(MqttClient *client, const byte* buf, int buf_len,
int timeout_ms)
{
int rc;
#if defined(ENABLE_MQTT_TLS) && !defined(ENABLE_MQTT_CURL)
if (MqttClient_Flags(client,0,0) & MQTT_CLIENT_FLAG_IS_TLS) {
client->tls.timeout_ms_write = timeout_ms;
client->tls.sockRcWrite = 0; /* init value */
rc = wolfSSL_write(client->tls.ssl, (char*)buf, buf_len);
if (rc < 0) {
#if defined(WOLFMQTT_DEBUG_SOCKET) || defined(WOLFSSL_ASYNC_CRYPT)
int error = wolfSSL_get_error(client->tls.ssl, 0);
#endif
#ifdef WOLFMQTT_DEBUG_SOCKET
if (error != WOLFSSL_ERROR_WANT_WRITE
#ifdef WOLFSSL_ASYNC_CRYPT
&& error != WC_PENDING_E
#endif
) {
PRINTF("MqttSocket_WriteDo: SSL Error=%d (rc %d, sockrc %d)",
error, rc, client->tls.sockRcWrite);
}
#endif
/* return code from net callback */
rc = client->tls.sockRcWrite;
#ifdef WOLFSSL_ASYNC_CRYPT
if (error == WC_PENDING_E) {
rc = MQTT_CODE_CONTINUE;
}
#endif
}
}
else
#endif /* ENABLE_MQTT_TLS && !ENABLE_MQTT_CURL*/
{
rc = client->net->write(client->net->context, buf, buf_len,
timeout_ms);
}
#ifdef WOLFMQTT_DEBUG_SOCKET
if (rc != 0 && rc != MQTT_CODE_CONTINUE) { /* hide in non-blocking case */
PRINTF("MqttSocket_Write: Len=%d, Rc=%d", buf_len, rc);
}
#endif
return rc;
}
int MqttSocket_Write(MqttClient *client, const byte* buf, int buf_len,
int timeout_ms)
{
int rc;
/* Validate arguments */
if (client == NULL || client->net == NULL || client->net->write == NULL ||
buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
/* check for buffer position overflow */
if (client->write.pos >= buf_len) {
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_OUT_OF_BUFFER);
}
#ifdef WOLFMQTT_NONBLOCK
rc = MqttSocket_WriteDo(client, &buf[client->write.pos],
buf_len - client->write.pos, timeout_ms);
if (rc >= 0) {
client->write.pos += rc;
client->write.total += rc;
if (client->write.pos < buf_len) {
rc = MQTT_CODE_CONTINUE;
}
}
else if (rc == EWOULDBLOCK || rc == EAGAIN) {
rc = MQTT_CODE_CONTINUE;
}
#else
do {
rc = MqttSocket_WriteDo(client, &buf[client->write.pos],
buf_len - client->write.pos, timeout_ms);
if (rc <= 0) {
break;
}
client->write.pos += rc;
client->write.total += rc;
} while (client->write.pos < buf_len);
#endif /* WOLFMQTT_NONBLOCK */
/* handle return code */
if (rc > 0) {
/* return length write and reset position */
rc = client->write.pos;
client->write.pos = 0;
}
return rc;
}
static int MqttSocket_ReadDo(MqttClient *client, byte* buf, int buf_len,
int timeout_ms)
{
int rc;
#if defined(ENABLE_MQTT_TLS) && !defined(ENABLE_MQTT_CURL)
if (MqttClient_Flags(client,0,0) & MQTT_CLIENT_FLAG_IS_TLS) {
client->tls.timeout_ms_read = timeout_ms;
client->tls.sockRcRead = 0; /* init value */
rc = wolfSSL_read(client->tls.ssl, (char*)buf, buf_len);
if (rc < 0) {
int error = wolfSSL_get_error(client->tls.ssl, 0);
#ifdef WOLFMQTT_DEBUG_SOCKET
if (error != WOLFSSL_ERROR_WANT_READ
#ifdef WOLFSSL_ASYNC_CRYPT
&& error != WC_PENDING_E
#endif
) {
PRINTF("MqttSocket_ReadDo: SSL Error=%d (rc %d, sockrc %d)",
error, rc, client->tls.sockRcRead);
}
#endif
/* return code from net callback */
rc = client->tls.sockRcRead;
#ifdef WOLFSSL_ASYNC_CRYPT
if (error == WC_PENDING_E) {
rc = MQTT_CODE_CONTINUE;
}
else
#endif
/* used with compatibility layer to communicate peer close */
if (error == WOLFSSL_ERROR_ZERO_RETURN) {
rc = MQTT_CODE_ERROR_NETWORK;
}
}
}
else
#endif /* ENABLE_MQTT_TLS && !ENABLE_MQTT_CURL*/
{
rc = client->net->read(client->net->context, buf, buf_len, timeout_ms);
}
#ifdef WOLFMQTT_DEBUG_SOCKET
if (rc != 0 && rc != MQTT_CODE_CONTINUE) { /* hide in non-blocking case */
PRINTF("MqttSocket_ReadDo: Len=%d, Rc=%d", buf_len, rc);
}
#endif
return rc;
}
int MqttSocket_Read(MqttClient *client, byte* buf, int buf_len, int timeout_ms)
{
int rc;
/* Validate arguments */
if (client == NULL || client->net == NULL || client->net->read == NULL ||
buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
/* check for buffer position overflow */
if (client->read.pos >= buf_len) {
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_OUT_OF_BUFFER);
}
#ifdef WOLFMQTT_NONBLOCK
rc = MqttSocket_ReadDo(client, &buf[client->read.pos],
buf_len - client->read.pos, timeout_ms);
if (rc >= 0) {
client->read.pos += rc;
client->read.total += rc;
if (client->read.pos < buf_len) {
rc = MQTT_CODE_CONTINUE;
}
}
else if (rc == EWOULDBLOCK || rc == EAGAIN) {
rc = MQTT_CODE_CONTINUE;
}
#else
do {
rc = MqttSocket_ReadDo(client, &buf[client->read.pos],
buf_len - client->read.pos, timeout_ms);
if (rc <= 0) {
break;
}
client->read.pos += rc;
client->read.total += rc;
} while (client->read.pos < buf_len);
#endif /* WOLFMQTT_NONBLOCK */
/* handle return code */
if (rc > 0) {
/* return length read and reset position */
rc = client->read.pos;
client->read.pos = 0;
}
return rc;
}
#ifdef WOLFMQTT_SN
int MqttSocket_Peek(MqttClient *client, byte* buf, int buf_len, int timeout_ms)
{
int rc;
/* Validate arguments */
if (client == NULL || client->net == NULL || client->net->peek == NULL ||
buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
/* check for buffer position overflow */
if (client->read.pos >= buf_len) {
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_OUT_OF_BUFFER);
}
rc = client->net->peek(client->net->context, buf, buf_len, timeout_ms);
if (rc > 0) {
#ifdef WOLFMQTT_DEBUG_SOCKET
PRINTF("MqttSocket_Peek: Len=%d, Rc=%d", buf_len, rc);
#endif
/* return length read and reset position */
client->read.pos = 0;
}
return rc;
}
#endif /* WOLFMQTT_SN */
int MqttSocket_Connect(MqttClient *client, const char* host, word16 port,
int timeout_ms, int use_tls, MqttTlsCb cb)
{
int rc = MQTT_CODE_SUCCESS;
/* Validate arguments */
if (client == NULL || client->net == NULL ||
client->net->connect == NULL) {
return MQTT_CODE_ERROR_BAD_ARG;
}
#ifndef ENABLE_MQTT_TLS
/* cannot use TLS unless ENABLE_MQTT_TLS is defined */
if (use_tls) {
return MQTT_CODE_ERROR_BAD_ARG;
}
#endif
if ((MqttClient_Flags(client, 0, 0) & MQTT_CLIENT_FLAG_IS_CONNECTED) == 0) {
/* Validate port */
if (port == 0) {
port = (use_tls) ? MQTT_SECURE_PORT : MQTT_DEFAULT_PORT;
}
/* Connect to host */
rc = client->net->connect(client->net->context, host, port, timeout_ms);
if (rc != MQTT_CODE_SUCCESS) {
return rc;
}
MqttClient_Flags(client, 0, MQTT_CLIENT_FLAG_IS_CONNECTED);
}
#if defined(ENABLE_MQTT_TLS) && !defined(ENABLE_MQTT_CURL)
if (use_tls) {
if (client->tls.ctx == NULL) {
#ifdef DEBUG_WOLFSSL
wolfSSL_Debugging_ON();
#endif
/* Setup the WolfSSL library */
rc = wolfSSL_Init();
/* Issue callback to allow setup of the wolfSSL_CTX and cert
verification settings */
if ((rc == WOLFSSL_SUCCESS) && (cb != NULL)) {
rc = cb(client);
}
if (rc != WOLFSSL_SUCCESS) {
rc = MQTT_CODE_ERROR_TLS_CONNECT;
goto exit;
}
}
/* Create and initialize the WOLFSSL_CTX structure */
if (client->tls.ctx == NULL) {
/* Use defaults */
/* Use highest available and allow downgrade. If wolfSSL is built
* with old TLS support, it is possible for a server to force a
* downgrade to an insecure version. */
if (!(MqttClient_Flags(client,0,0) & MQTT_CLIENT_FLAG_IS_DTLS)) {
client->tls.ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
}
#ifdef WOLFSSL_DTLS
else {
client->tls.ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
}
#endif
if (client->tls.ctx == NULL) {
rc = MQTT_CODE_ERROR_TLS_CONNECT;
goto exit;
}
wolfSSL_CTX_set_verify(client->tls.ctx, WOLFSSL_VERIFY_NONE, 0);
}
#ifndef NO_DH
wolfSSL_CTX_SetMinDhKey_Sz(client->tls.ctx, WOLF_TLS_DHKEY_BITS_MIN);
#endif
/* Setup the IO callbacks */
wolfSSL_CTX_SetIORecv(client->tls.ctx, MqttSocket_TlsSocketReceive);
wolfSSL_CTX_SetIOSend(client->tls.ctx, MqttSocket_TlsSocketSend);
if (client->tls.ssl == NULL) {
client->tls.ssl = wolfSSL_new(client->tls.ctx);
if (client->tls.ssl == NULL) {
rc = MQTT_CODE_ERROR_TLS_CONNECT;
goto exit;
}
} else {
/* Since the user setup client->tls.ssl the IO callbacks didn't get
* associated with this wolfSSL struct */
wolfSSL_SSLSetIORecv(client->tls.ssl, MqttSocket_TlsSocketReceive);
wolfSSL_SSLSetIOSend(client->tls.ssl, MqttSocket_TlsSocketSend);
}
/* Set the IO callback context */
wolfSSL_SetIOReadCtx(client->tls.ssl, (void *)client);
wolfSSL_SetIOWriteCtx(client->tls.ssl, (void *)client);
if (client->ctx != NULL) {
/* Store any app data for use by the tls verify callback*/
wolfSSL_SetCertCbCtx(client->tls.ssl, client->ctx);
}
MqttClient_Flags(client, 0, MQTT_CLIENT_FLAG_IS_TLS);
rc = wolfSSL_connect(client->tls.ssl);
if (rc != WOLFSSL_SUCCESS) {
rc = MQTT_CODE_ERROR_TLS_CONNECT;
MqttClient_Flags(client, MQTT_CLIENT_FLAG_IS_TLS, 0);
goto exit;
}
rc = MQTT_CODE_SUCCESS;
}
exit:
/* Handle error case */
if (rc != MQTT_CODE_SUCCESS) {
#ifdef WOLFMQTT_DEBUG_SOCKET
const char* errstr = "";
#endif
int errnum = 0;
if (client->tls.ssl) {
errnum = wolfSSL_get_error(client->tls.ssl, 0);
if ( errnum == WOLFSSL_ERROR_WANT_READ
|| errnum == WOLFSSL_ERROR_WANT_WRITE
#ifdef WOLFSSL_ASYNC_CRYPT
|| errnum == WC_PENDING_E
#endif
) {
return MQTT_CODE_CONTINUE;
}
#ifdef WOLFMQTT_DEBUG_SOCKET
errstr = wolfSSL_ERR_reason_error_string(errnum);
#endif
}
#ifdef WOLFMQTT_DEBUG_SOCKET
PRINTF("MqttSocket_TlsConnect Error %d: Num %d, %s",
rc, errnum, errstr);
#endif /* WOLFMQTT_DEBUG_SOCKET */
/* Make sure we cleanup on error */
MqttSocket_Disconnect(client);
}
#else
(void)cb;
#endif /* ENABLE_MQTT_TLS && !ENABLE_MQTT_CURL*/
#ifdef WOLFMQTT_DEBUG_SOCKET
PRINTF("MqttSocket_Connect: Rc=%d", rc);
#endif
return rc;
}
int MqttSocket_Disconnect(MqttClient *client)
{
int rc = MQTT_CODE_SUCCESS;
if (client) {
#if defined(ENABLE_MQTT_TLS)
#if !defined(ENABLE_MQTT_CURL)
if (client->tls.ssl) {
wolfSSL_free(client->tls.ssl);
client->tls.ssl = NULL;
}
if (client->tls.ctx) {
wolfSSL_CTX_free(client->tls.ctx);
client->tls.ctx = NULL;
}
wolfSSL_Cleanup();
#endif
MqttClient_Flags(client,
(MQTT_CLIENT_FLAG_IS_TLS | MQTT_CLIENT_FLAG_IS_DTLS), 0);
#endif
/* Make sure socket is closed */
if (client->net && client->net->disconnect) {
rc = client->net->disconnect(client->net->context);
}
MqttClient_Flags(client, MQTT_CLIENT_FLAG_IS_CONNECTED, 0);
#ifdef ENABLE_MQTT_CURL
curl_global_cleanup();
#endif
}
#ifdef WOLFMQTT_DEBUG_SOCKET
PRINTF("MqttSocket_Disconnect: Rc=%d", rc);
#endif
/* Check for error */
if (rc < 0) {
rc = MQTT_CODE_ERROR_NETWORK;
}
return rc;
}