-
Notifications
You must be signed in to change notification settings - Fork 158
/
mqttnet.c
1162 lines (1002 loc) · 30.4 KB
/
mqttnet.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* mqttnet.c
*
* Copyright (C) 2006-2022 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
#include "wolfmqtt/mqtt_client.h"
#include "examples/mqttnet.h"
#include "examples/mqttexample.h"
/* FreeRTOS TCP */
#ifdef FREERTOS_TCP
#include "FreeRTOS.h"
#include "task.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_DNS.h"
#include "FreeRTOS_Sockets.h"
#define SOCKET_T Socket_t
#define SOCK_ADDR_IN struct freertos_sockaddr
/* FreeRTOS and LWIP */
#elif defined(FREERTOS) && defined(WOLFSSL_LWIP)
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* lwIP includes. */
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
/* Windows */
#elif defined(USE_WINDOWS_API)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#define SOCKET_T SOCKET
#ifdef _WIN32
#define SOERROR_T int
#else
#define SOERROR_T char
#endif
#define SELECT_FD(fd) (fd)
#ifndef SOCKET_INVALID /* Do not redefine from wolfssl */
#define SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET)
#endif
#define SOCK_CLOSE closesocket
#define SOCK_SEND(s,b,l,f) send((s), (const char*)(b), (size_t)(l), (f))
#define SOCK_RECV(s,b,l,f) recv((s), (char*)(b), (size_t)(l), (f))
#define GET_SOCK_ERROR(f,s,o,e) (e) = WSAGetLastError()
#define SOCK_EQ_ERROR(e) (((e) == WSAEWOULDBLOCK) || ((e) == WSAEINPROGRESS))
/* Freescale MQX / RTCS */
#elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
#if defined(FREESCALE_MQX)
#include <posix.h>
#endif
#include <rtcs.h>
/* Note: Use "RTCS_geterror(sock->fd);" to get error number */
#define SOCKET_INVALID RTCS_SOCKET_ERROR
#define SOCKET_T uint32_t
#define SOCK_CLOSE closesocket
#define SOCK_OPEN RTCS_socket
/* Microchip MPLABX Harmony, TCP/IP */
#elif defined(MICROCHIP_MPLAB_HARMONY)
#include "app.h"
#include "system_config.h"
#include "tcpip/tcpip.h"
#include <sys/errno.h>
#include <errno.h>
#define SOCKET_INVALID (-1)
#define SOCK_CLOSE closesocket
#ifndef WOLFMQTT_NONBLOCK
#error wolfMQTT must be built with WOLFMQTT_NONBLOCK defined for Harmony
#endif
/* Linux */
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#endif
/* Setup defaults */
#ifndef SOCK_OPEN
#define SOCK_OPEN socket
#endif
#ifndef SOCKET_T
#define SOCKET_T int
#endif
#ifndef SOERROR_T
#define SOERROR_T int
#endif
#ifndef SELECT_FD
#define SELECT_FD(fd) ((fd) + 1)
#endif
#ifndef SOCKET_INVALID
#define SOCKET_INVALID ((SOCKET_T)0)
#endif
#ifndef SOCK_CONNECT
#define SOCK_CONNECT connect
#endif
#ifndef SOCK_SEND
#define SOCK_SEND(s,b,l,f) send((s), (b), (size_t)(l), (f))
#endif
#ifndef SOCK_RECV
#define SOCK_RECV(s,b,l,f) recv((s), (b), (size_t)(l), (f))
#endif
#ifndef SOCK_CLOSE
#define SOCK_CLOSE close
#endif
#ifndef SOCK_ADDR_IN
#define SOCK_ADDR_IN struct sockaddr_in
#endif
#ifdef SOCK_ADDRINFO
#define SOCK_ADDRINFO struct addrinfo
#endif
#ifndef GET_SOCK_ERROR
#define GET_SOCK_ERROR(f,s,o,e) \
socklen_t len = sizeof(so_error); \
getsockopt((f), (s), (o), &(e), &len)
#endif
#ifndef SOCK_EQ_ERROR
#define SOCK_EQ_ERROR(e) (((e) == EWOULDBLOCK) || ((e) == EAGAIN))
#endif
/* Local context for Net callbacks */
typedef enum {
SOCK_BEGIN = 0,
SOCK_CONN,
} NB_Stat;
#if 0 /* TODO: add multicast support */
typedef struct MulticastCtx {
} MulticastCtx;
#endif
typedef struct _SocketContext {
SOCKET_T fd;
NB_Stat stat;
SOCK_ADDR_IN addr;
#ifdef MICROCHIP_MPLAB_HARMONY
word32 bytes;
#endif
#if defined(WOLFMQTT_MULTITHREAD) && defined(WOLFMQTT_ENABLE_STDIN_CAP)
/* "self pipe" -> signal wake sleep() */
SOCKET_T pfd[2];
#endif
MQTTCtx* mqttCtx;
} SocketContext;
/* Private functions */
/* -------------------------------------------------------------------------- */
/* FREERTOS TCP NETWORK CALLBACK EXAMPLE */
/* -------------------------------------------------------------------------- */
#ifdef FREERTOS_TCP
#ifndef WOLFMQTT_NO_TIMEOUT
static SocketSet_t gxFDSet = NULL;
#endif
static int NetConnect(void *context, const char* host, word16 port,
int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
uint32_t hostIp = 0;
int rc = -1;
MQTTCtx* mqttCtx = sock->mqttCtx;
switch (sock->stat) {
case SOCK_BEGIN:
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
host, port, timeout_ms, mqttCtx->use_tls);
hostIp = FreeRTOS_gethostbyname_a(host, NULL, 0, 0);
if (hostIp == 0)
break;
sock->addr.sin_family = FREERTOS_AF_INET;
sock->addr.sin_port = FreeRTOS_htons(port);
sock->addr.sin_addr = hostIp;
/* Create socket */
sock->fd = FreeRTOS_socket(sock->addr.sin_family, FREERTOS_SOCK_STREAM,
FREERTOS_IPPROTO_TCP);
if (sock->fd == FREERTOS_INVALID_SOCKET)
break;
#ifndef WOLFMQTT_NO_TIMEOUT
/* Set timeouts for socket */
timeout_ms = pdMS_TO_TICKS(timeout_ms);
FreeRTOS_setsockopt(sock->fd, 0, FREERTOS_SO_SNDTIMEO,
(void*)&timeout_ms, sizeof(timeout_ms));
FreeRTOS_setsockopt(sock->fd, 0, FREERTOS_SO_RCVTIMEO,
(void*)&timeout_ms, sizeof(timeout_ms));
#else
(void)timeout_ms;
#endif
sock->stat = SOCK_CONN;
FALL_THROUGH;
case SOCK_CONN:
/* Start connect */
rc = FreeRTOS_connect(sock->fd, (SOCK_ADDR_IN*)&sock->addr,
sizeof(sock->addr));
break;
}
return rc;
}
static int NetRead(void *context, byte* buf, int buf_len,
int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
int rc = -1, timeout = 0;
word32 bytes = 0;
if (context == NULL || buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
#ifndef WOLFMQTT_NO_TIMEOUT
/* Create the set of sockets that will be passed into FreeRTOS_select(). */
if (gxFDSet == NULL)
gxFDSet = FreeRTOS_CreateSocketSet();
if (gxFDSet == NULL)
return MQTT_CODE_ERROR_OUT_OF_BUFFER;
timeout_ms = pdMS_TO_TICKS(timeout_ms); /* convert ms to ticks */
#else
(void)timeout_ms;
#endif
/* Loop until buf_len has been read, error or timeout */
while ((bytes < buf_len) && (timeout == 0)) {
#ifndef WOLFMQTT_NO_TIMEOUT
/* set the socket to do used */
FreeRTOS_FD_SET(sock->fd, gxFDSet, eSELECT_READ | eSELECT_EXCEPT);
/* Wait for any event within the socket set. */
rc = FreeRTOS_select(gxFDSet, timeout_ms);
if (rc != 0) {
if (FreeRTOS_FD_ISSET(sock->fd, gxFDSet))
#endif
{
/* Try and read number of buf_len provided,
minus what's already been read */
rc = (int)FreeRTOS_recv(sock->fd, &buf[bytes],
buf_len - bytes, 0);
if (rc <= 0) {
break; /* Error */
}
else {
bytes += rc; /* Data */
}
}
#ifndef WOLFMQTT_NO_TIMEOUT
}
else {
timeout = 1;
}
#endif
}
if (rc == 0 || timeout) {
rc = MQTT_CODE_ERROR_TIMEOUT;
}
else if (rc < 0) {
#ifdef WOLFMQTT_NONBLOCK
if (rc == -pdFREERTOS_ERRNO_EWOULDBLOCK) {
return MQTT_CODE_CONTINUE;
}
#endif
PRINTF("NetRead: Error %d", rc);
rc = MQTT_CODE_ERROR_NETWORK;
}
else {
rc = bytes;
}
return rc;
}
static int NetWrite(void *context, const byte* buf, int buf_len, int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
int rc = -1;
(void)timeout_ms;
if (context == NULL || buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
rc = (int)FreeRTOS_send(sock->fd, buf, buf_len, 0);
if (rc < 0) {
#ifdef WOLFMQTT_NONBLOCK
if (rc == -pdFREERTOS_ERRNO_EWOULDBLOCK) {
return MQTT_CODE_CONTINUE;
}
#endif
PRINTF("NetWrite: Error %d", rc);
rc = MQTT_CODE_ERROR_NETWORK;
}
return rc;
}
static int NetDisconnect(void *context)
{
SocketContext *sock = (SocketContext*)context;
if (sock) {
FreeRTOS_closesocket(sock->fd);
sock->stat = SOCK_BEGIN;
}
#ifndef WOLFMQTT_NO_TIMEOUT
if (gxFDSet != NULL) {
FreeRTOS_DeleteSocketSet(gxFDSet);
gxFDSet = NULL;
}
#endif
return 0;
}
/* -------------------------------------------------------------------------- */
/* MICROCHIP HARMONY TCP NETWORK CALLBACK EXAMPLE */
/* -------------------------------------------------------------------------- */
#elif defined(MICROCHIP_MPLAB_HARMONY)
static int NetConnect(void *context, const char* host, word16 port,
int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
int type = SOCK_STREAM;
int rc = MQTT_CODE_ERROR_NETWORK;
struct addrinfo hints;
struct hostent *hostInfo;
MQTTCtx* mqttCtx = sock->mqttCtx;
/* Get address information for host and locate IPv4 */
switch(sock->stat) {
case SOCK_BEGIN:
{
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
host, port, timeout_ms, mqttCtx->use_tls);
XMEMSET(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
XMEMSET(&sock->addr, 0, sizeof(sock->addr));
sock->addr.sin_family = AF_INET;
hostInfo = gethostbyname((char *)host);
if (hostInfo != NULL) {
sock->addr.sin_port = port; /* htons(port); */
sock->addr.sin_family = AF_INET;
XMEMCPY(&sock->addr.sin_addr.S_un,
*(hostInfo->h_addr_list), sizeof(IPV4_ADDR));
}
else {
return MQTT_CODE_CONTINUE;
}
/* Create socket */
sock->fd = SOCK_OPEN(sock->addr.sin_family, type, 0);
if (sock->fd == SOCKET_INVALID)
goto exit;
sock->stat = SOCK_CONN;
}
FALL_THROUGH;
case SOCK_CONN:
{
/* Start connect */
rc = SOCK_CONNECT(sock->fd, (struct sockaddr*)&sock->addr,
sizeof(sock->addr));
break;
}
default:
rc = MQTT_CODE_ERROR_BAD_ARG;
break;
} /* switch */
(void)timeout_ms;
exit:
/* check for error */
if (rc != 0) {
if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
return MQTT_CODE_CONTINUE;
}
/* Show error */
PRINTF("NetConnect: Rc=%d, ErrNo=%d", rc, errno);
}
return rc;
}
static int NetWrite(void *context, const byte* buf, int buf_len,
int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
int rc;
if (context == NULL || buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
rc = (int)send(sock->fd, buf, (size_t)buf_len, 0);
if (rc <= 0) {
/* Check for in progress */
if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
return MQTT_CODE_CONTINUE;
}
PRINTF("NetWrite Error: Rc %d, BufLen %d, ErrNo %d", rc, buf_len, errno);
rc = MQTT_CODE_ERROR_NETWORK;
}
(void)timeout_ms;
return rc;
}
static int NetRead(void *context, byte* buf, int buf_len,
int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
int rc = MQTT_CODE_ERROR_NETWORK;
if (context == NULL || buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
rc = (int)recv(sock->fd,
&buf[sock->bytes],
(size_t)(buf_len - sock->bytes),
0);
if (rc < 0) {
if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
return MQTT_CODE_CONTINUE;
}
PRINTF("NetRead Error: Rc %d, BufLen %d, ErrNo %d", rc, buf_len, errno);
rc = MQTT_CODE_ERROR_NETWORK;
}
else {
/* Try and build entire recv buffer before returning success */
sock->bytes += rc;
if (sock->bytes < buf_len) {
return MQTT_CODE_CONTINUE;
}
rc = sock->bytes;
sock->bytes = 0;
}
(void)timeout_ms;
return rc;
}
static int NetDisconnect(void *context)
{
SocketContext *sock = (SocketContext*)context;
if (sock) {
if (sock->fd != SOCKET_INVALID) {
closesocket(sock->fd);
sock->fd = SOCKET_INVALID;
}
sock->stat = SOCK_BEGIN;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* GENERIC BSD SOCKET TCP NETWORK CALLBACK EXAMPLE */
/* -------------------------------------------------------------------------- */
#else
#ifndef WOLFMQTT_NO_TIMEOUT
static void setup_timeout(struct timeval* tv, int timeout_ms)
{
tv->tv_sec = timeout_ms / 1000;
tv->tv_usec = (timeout_ms % 1000) * 1000;
/* Make sure there is a minimum value specified */
if (tv->tv_sec < 0 || (tv->tv_sec == 0 && tv->tv_usec <= 0)) {
tv->tv_sec = 0;
tv->tv_usec = 100;
}
}
#ifdef WOLFMQTT_NONBLOCK
static void tcp_set_nonblocking(SOCKET_T* sockfd)
{
#ifdef USE_WINDOWS_API
unsigned long blocking = 1;
int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
if (ret == SOCKET_ERROR)
PRINTF("ioctlsocket failed!");
#else
int flags = fcntl(*sockfd, F_GETFL, 0);
if (flags < 0)
PRINTF("fcntl get failed!");
flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK);
if (flags < 0)
PRINTF("fcntl set failed!");
#endif
}
#endif /* WOLFMQTT_NONBLOCK */
#endif /* !WOLFMQTT_NO_TIMEOUT */
static int NetConnect(void *context, const char* host, word16 port,
int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
int type = SOCK_STREAM;
int rc = -1;
SOERROR_T so_error = 0;
struct addrinfo *result = NULL;
struct addrinfo hints;
MQTTCtx* mqttCtx = sock->mqttCtx;
/* Get address information for host and locate IPv4 */
switch(sock->stat) {
case SOCK_BEGIN:
{
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
host, port, timeout_ms, mqttCtx->use_tls);
XMEMSET(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
XMEMSET(&sock->addr, 0, sizeof(sock->addr));
sock->addr.sin_family = AF_INET;
rc = getaddrinfo(host, NULL, &hints, &result);
if (rc == 0) {
struct addrinfo* result_i = result;
if (! result) {
rc = -1;
goto exit;
}
/* prefer ip4 addresses */
while (result_i) {
if (result_i->ai_family == AF_INET)
break;
result_i = result_i->ai_next;
}
if (result_i) {
sock->addr.sin_port = htons(port);
sock->addr.sin_family = AF_INET;
sock->addr.sin_addr =
((SOCK_ADDR_IN*)(result_i->ai_addr))->sin_addr;
}
else {
rc = -1;
}
freeaddrinfo(result);
}
if (rc != 0)
goto exit;
/* Default to error */
rc = -1;
/* Create socket */
sock->fd = SOCK_OPEN(sock->addr.sin_family, type, 0);
if (sock->fd == SOCKET_INVALID)
goto exit;
sock->stat = SOCK_CONN;
}
FALL_THROUGH;
case SOCK_CONN:
{
#ifndef WOLFMQTT_NO_TIMEOUT
fd_set fdset;
struct timeval tv;
/* Setup timeout and FD's */
setup_timeout(&tv, timeout_ms);
FD_ZERO(&fdset);
FD_SET(sock->fd, &fdset);
#endif /* !WOLFMQTT_NO_TIMEOUT */
#if !defined(WOLFMQTT_NO_TIMEOUT) && defined(WOLFMQTT_NONBLOCK)
if (mqttCtx->useNonBlockMode) {
/* Set socket as non-blocking */
tcp_set_nonblocking(&sock->fd);
}
#endif
/* Start connect */
rc = SOCK_CONNECT(sock->fd, (struct sockaddr*)&sock->addr,
sizeof(sock->addr));
if (rc < 0) {
/* set default error case */
rc = MQTT_CODE_ERROR_NETWORK;
#ifdef WOLFMQTT_NONBLOCK
/* Check for error */
GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR, so_error);
if (
#ifndef _WIN32
(errno == EINPROGRESS) ||
#endif
SOCK_EQ_ERROR(so_error))
{
#ifndef WOLFMQTT_NO_TIMEOUT
/* Wait for connect */
if (select((int)SELECT_FD(sock->fd), NULL, &fdset,
NULL, &tv) > 0) {
rc = MQTT_CODE_SUCCESS;
}
#else
rc = MQTT_CODE_CONTINUE;
#endif
}
#endif
}
break;
}
default:
rc = -1;
} /* switch */
(void)timeout_ms;
exit:
/* Show error */
if (rc != 0) {
PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error);
}
return rc;
}
#ifdef WOLFMQTT_SN
static int SN_NetConnect(void *context, const char* host, word16 port,
int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
int type = SOCK_DGRAM;
int rc;
SOERROR_T so_error = 0;
struct addrinfo *result = NULL;
struct addrinfo hints;
MQTTCtx* mqttCtx = sock->mqttCtx;
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
host, port, timeout_ms, mqttCtx->use_tls);
/* Get address information for host and locate IPv4 */
XMEMSET(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
XMEMSET(&sock->addr, 0, sizeof(sock->addr));
sock->addr.sin_family = AF_INET;
rc = getaddrinfo(host, NULL, &hints, &result);
if (rc == 0) {
struct addrinfo* result_i = result;
if (! result) {
rc = -1;
goto exit;
}
/* prefer ip4 addresses */
while (result_i) {
if (result_i->ai_family == AF_INET)
break;
result_i = result_i->ai_next;
}
if (result_i) {
sock->addr.sin_port = htons(port);
sock->addr.sin_family = AF_INET;
sock->addr.sin_addr =
((SOCK_ADDR_IN*)(result_i->ai_addr))->sin_addr;
}
else {
rc = -1;
}
freeaddrinfo(result);
}
if (rc != 0)
goto exit;
if (rc == 0) {
/* Create the socket */
sock->fd = SOCK_OPEN(sock->addr.sin_family, type, 0);
if (sock->fd == SOCKET_INVALID) {
rc = -1;
}
}
if (rc == 0)
{
#ifndef WOLFMQTT_NO_TIMEOUT
fd_set fdset;
struct timeval tv;
/* Setup timeout and FD's */
setup_timeout(&tv, timeout_ms);
FD_ZERO(&fdset);
FD_SET(sock->fd, &fdset);
#else
(void)timeout_ms;
#endif /* !WOLFMQTT_NO_TIMEOUT */
/* Start connect */
rc = SOCK_CONNECT(sock->fd, (struct sockaddr*)&sock->addr,
sizeof(sock->addr));
}
exit:
/* Show error */
if (rc != 0) {
SOCK_CLOSE(sock->fd);
PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error);
}
return rc;
}
#endif
static int NetWrite(void *context, const byte* buf, int buf_len,
int timeout_ms)
{
SocketContext *sock = (SocketContext*)context;
int rc;
SOERROR_T so_error = 0;
#ifndef WOLFMQTT_NO_TIMEOUT
struct timeval tv;
#endif
if (context == NULL || buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
if (sock->fd == SOCKET_INVALID)
return MQTT_CODE_ERROR_BAD_ARG;
#ifndef WOLFMQTT_NO_TIMEOUT
/* Setup timeout */
setup_timeout(&tv, timeout_ms);
setsockopt(sock->fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
#endif
rc = (int)SOCK_SEND(sock->fd, buf, buf_len, 0);
if (rc == -1) {
/* Get error */
GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR, so_error);
if (so_error == 0) {
#if defined(USE_WINDOWS_API) && defined(WOLFMQTT_NONBLOCK)
/* assume non-blocking case */
rc = MQTT_CODE_CONTINUE;
#else
rc = 0; /* Handle signal */
#endif
}
else {
#ifdef WOLFMQTT_NONBLOCK
if (SOCK_EQ_ERROR(so_error)) {
return MQTT_CODE_CONTINUE;
}
#endif
rc = MQTT_CODE_ERROR_NETWORK;
PRINTF("NetWrite: Error %d", so_error);
}
}
(void)timeout_ms;
return rc;
}
static int NetRead_ex(void *context, byte* buf, int buf_len,
int timeout_ms, byte peek)
{
SocketContext *sock = (SocketContext*)context;
MQTTCtx* mqttCtx = sock->mqttCtx;
int rc = -1, timeout = 0;
SOERROR_T so_error = 0;
int bytes = 0;
int flags = 0;
#ifndef WOLFMQTT_NO_TIMEOUT
fd_set recvfds;
fd_set errfds;
struct timeval tv;
#endif
(void)mqttCtx;
if (context == NULL || buf == NULL || buf_len <= 0) {
return MQTT_CODE_ERROR_BAD_ARG;
}
if (sock->fd == SOCKET_INVALID)
return MQTT_CODE_ERROR_BAD_ARG;
if (peek == 1) {
flags |= MSG_PEEK;
}
#ifndef WOLFMQTT_NO_TIMEOUT
/* Setup timeout */
setup_timeout(&tv, timeout_ms);
/* Setup select file descriptors to watch */
FD_ZERO(&errfds);
FD_SET(sock->fd, &errfds);
FD_ZERO(&recvfds);
FD_SET(sock->fd, &recvfds);
#ifdef WOLFMQTT_ENABLE_STDIN_CAP
#ifdef WOLFMQTT_MULTITHREAD
FD_SET(sock->pfd[0], &recvfds);
#endif
if (!mqttCtx->test_mode) {
FD_SET(STDIN, &recvfds);
}
#endif /* WOLFMQTT_ENABLE_STDIN_CAP */
#else
(void)timeout_ms;
#endif /* !WOLFMQTT_NO_TIMEOUT */
/* Loop until buf_len has been read, error or timeout */
while (bytes < buf_len) {
int do_read = 0;
#ifndef WOLFMQTT_NO_TIMEOUT
#ifdef WOLFMQTT_NONBLOCK
if (mqttCtx->useNonBlockMode) {
do_read = 1;
}
else
#endif
{
/* Wait for rx data to be available */
rc = select((int)SELECT_FD(sock->fd), &recvfds, NULL, &errfds, &tv);
if (rc > 0)
{
if (FD_ISSET(sock->fd, &recvfds)) {
do_read = 1;
}
/* Check if rx or error */
#ifdef WOLFMQTT_ENABLE_STDIN_CAP
else if ((!mqttCtx->test_mode && FD_ISSET(STDIN, &recvfds))
#ifdef WOLFMQTT_MULTITHREAD
|| FD_ISSET(sock->pfd[0], &recvfds)
#endif
) {
return MQTT_CODE_STDIN_WAKE;
}
#endif
if (FD_ISSET(sock->fd, &errfds)) {
rc = -1;
break;
}
}
else {
timeout = 1;
break; /* timeout or signal */
}
}
#else
do_read = 1;
#endif /* !WOLFMQTT_NO_TIMEOUT */
if (do_read) {
/* Try and read number of buf_len provided,
minus what's already been read */
rc = (int)SOCK_RECV(sock->fd,
&buf[bytes],
buf_len - bytes,
flags);
if (rc <= 0) {
rc = -1;
goto exit; /* Error */
}
else {
bytes += rc; /* Data */
}
}
/* no timeout and non-block should always exit loop */
#ifdef WOLFMQTT_NONBLOCK
if (mqttCtx->useNonBlockMode) {
break;
}
#endif
#ifdef WOLFMQTT_NO_TIMEOUT
break;
#endif
} /* while */
exit:
if (rc == 0 && timeout) {
rc = MQTT_CODE_ERROR_TIMEOUT;
}
else if (rc < 0) {
/* Get error */
GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR, so_error);
if (so_error == 0) {
rc = 0; /* Handle signal */
}
else {
#ifdef WOLFMQTT_NONBLOCK
if (SOCK_EQ_ERROR(so_error)) {
return MQTT_CODE_CONTINUE;
}
#endif
rc = MQTT_CODE_ERROR_NETWORK;
PRINTF("NetRead: Error %d", so_error);
}
}
else {
rc = bytes;
}
return rc;
}
static int NetRead(void *context, byte* buf, int buf_len, int timeout_ms)
{
return NetRead_ex(context, buf, buf_len, timeout_ms, 0);
}
#ifdef WOLFMQTT_SN
static int NetPeek(void *context, byte* buf, int buf_len, int timeout_ms)
{
return NetRead_ex(context, buf, buf_len, timeout_ms, 1);
}
#endif