This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
iot_secure_sockets.c
1063 lines (827 loc) · 27.8 KB
/
iot_secure_sockets.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
/*
* FreeRTOS Secure Sockets V1.3.1
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/**
* @file iot_secure_sockets.c
* @brief Secure Socket interface implementation.
*/
/* Secure Socket interface includes. */
#include "iot_secure_sockets.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "lwip/err.h"
#include "iot_tls.h"
#include "iot_atomic.h"
#include "FreeRTOSConfig.h"
#include "task.h"
#include "event_groups.h"
#include <stdbool.h>
/*
* The loop delay used while waiting for DNS resolution
* to complete.
*/
#define lwip_dns_resolver_LOOP_DELAY_MS ( 250 )
#define lwip_dns_resolver_LOOP_DELAY_TICKS ( ( TickType_t ) lwip_dns_resolver_LOOP_DELAY_MS / portTICK_PERIOD_MS )
/*
* The maximum time to wait for DNS resolution
* to complete.
*/
#define lwip_dns_resolver_MAX_WAIT_SECONDS ( 20 )
/*
* The maximum number of loop iterations to wait for DNS
* resolution to complete.
*/
#define lwip_dns_resolver_MAX_WAIT_CYCLES \
( ( ( lwip_dns_resolver_MAX_WAIT_SECONDS ) * 1000 ) / \
( lwip_dns_resolver_LOOP_DELAY_MS ) )
/*-----------------------------------------------------------*/
#define SS_STATUS_CONNECTED ( 1 )
#define SS_STATUS_SECURED ( 2 )
#define SECURE_SOCKETS_SELECT_WAIT_SEC ( 10 )
#define SOCKETS_START_DELETION ( 0x01 )
#define SOCKETS_COMPLETE_DELETION ( 0x02 )
/*
* secure socket context.
*/
typedef enum E_AWS_SOCkET_RX_STATE
{
SST_RX_IDLE,
SST_RX_READY,
SST_RX_CLOSING,
SST_RX_CLOSED,
} T_AWS_SOCKET_RX_STATE;
typedef struct _ss_ctx_t
{
int ip_socket;
int state;
unsigned int status;
int send_flag;
int recv_flag;
TaskHandle_t rx_handle;
void ( * rx_callback )( Socket_t pxSocket );
EventGroupHandle_t rx_EventGroup;
bool enforce_tls;
void * tls_ctx;
char * destination;
char * server_cert;
int server_cert_len;
char ** ppcAlpnProtocols;
uint32_t ulAlpnProtocolsCount;
uint32_t ulRefcount;
} ss_ctx_t;
/*-----------------------------------------------------------*/
/*#define SUPPORTED_DESCRIPTORS (2) */
/*-----------------------------------------------------------*/
/*static int8_t sockets_allocated = SUPPORTED_DESCRIPTORS; */
static int8_t sockets_allocated = socketsconfigDEFAULT_MAX_NUM_SECURE_SOCKETS;
/*-----------------------------------------------------------*/
/*
* convert from system ticks to seconds.
*/
#define TICK_TO_S( _t_ ) ( ( _t_ ) / configTICK_RATE_HZ )
/*
* convert from system ticks to micro seconds.
*/
#define TICK_TO_US( _t_ ) ( ( _t_ ) * 1000 / configTICK_RATE_HZ * 1000 )
/*-----------------------------------------------------------*/
/*
* @brief Sockets close
*/
static void prvSocketsClose( ss_ctx_t * ctx )
{
uint32_t ulProtocol;
sockets_allocated++;
/* Clean-up application protocol array. */
if( NULL != ctx->ppcAlpnProtocols )
{
for( ulProtocol = 0;
ulProtocol < ctx->ulAlpnProtocolsCount;
ulProtocol++ )
{
if( NULL != ctx->ppcAlpnProtocols[ ulProtocol ] )
{
vPortFree( ctx->ppcAlpnProtocols[ ulProtocol ] );
}
}
vPortFree( ctx->ppcAlpnProtocols );
}
if( true == ctx->enforce_tls )
{
TLS_Cleanup( ctx->tls_ctx );
}
if( ctx->server_cert )
{
vPortFree( ctx->server_cert );
}
if( ctx->destination )
{
vPortFree( ctx->destination );
}
vPortFree( ctx );
}
/*
* @brief Decrement ctx refcount and call release function if the count is 1 (
* last user of the ctx)
*/
static void prvDecrementRefCount( ss_ctx_t * ctx )
{
if( Atomic_Decrement_u32( &ctx->ulRefcount ) == 1 )
{
prvSocketsClose( ctx );
}
}
/*
* @brief Increment ctx refcount
*/
static void prvIncrementRefCount( ss_ctx_t * ctx )
{
Atomic_Increment_u32( &ctx->ulRefcount );
}
/*
* @brief Network send callback.
*/
static BaseType_t prvNetworkSend( void * pvContext,
const unsigned char * pucData,
size_t xDataLength )
{
ss_ctx_t * ctx = ( ss_ctx_t * ) pvContext;
int ret = lwip_send( ctx->ip_socket,
pucData,
xDataLength,
ctx->send_flag );
return ( BaseType_t ) ret;
}
/*-----------------------------------------------------------*/
/*
* @brief Network receive callback.
*/
static BaseType_t prvNetworkRecv( void * pvContext,
unsigned char * pucReceiveBuffer,
size_t xReceiveLength )
{
ss_ctx_t * ctx;
ctx = ( ss_ctx_t * ) pvContext;
configASSERT( ctx->ip_socket >= 0 );
int ret = lwip_recv( ctx->ip_socket,
pucReceiveBuffer,
xReceiveLength,
ctx->recv_flag );
if( -1 == ret )
{
/*
* 1. EWOULDBLOCK if the socket is NON-blocking, but there is no data
* when recv is called.
* 2. EAGAIN if the socket would block and have waited long enough but
* packet is not received.
*/
if( ( errno == EWOULDBLOCK ) || ( errno == EAGAIN ) )
{
return SOCKETS_ERROR_NONE; /* timeout or would block */
}
/*
* socket is not connected.
*/
if( errno == EBADF )
{
return SOCKETS_ECLOSED;
}
}
if( ( 0 == ret ) && ( errno == ENOTCONN ) )
{
ret = SOCKETS_ECLOSED;
}
return ( BaseType_t ) ret;
}
/*-----------------------------------------------------------*/
static void vTaskRxSelect( void * param )
{
ss_ctx_t * ctx = ( ss_ctx_t * ) param;
int s = ctx->ip_socket;
struct timeval tv;
fd_set read_fds;
fd_set write_fds;
fd_set err_fds;
FD_ZERO( &read_fds );
FD_ZERO( &write_fds );
FD_ZERO( &err_fds );
FD_SET( s, &read_fds );
FD_SET( s, &err_fds );
ctx->state = SST_RX_READY;
tv.tv_sec = SECURE_SOCKETS_SELECT_WAIT_SEC;
tv.tv_usec = 0;
while( 1 )
{
if( ctx->state == SST_RX_CLOSING )
{
ctx->state = SST_RX_CLOSED;
break;
}
if( lwip_select( s + 1, &read_fds, &write_fds, &err_fds, &tv ) == -1 )
{
break;
}
if( FD_ISSET( s, &read_fds ) )
{
ctx->rx_callback( ( Socket_t ) ctx );
}
if( xEventGroupWaitBits( ctx->rx_EventGroup, SOCKETS_START_DELETION, pdTRUE, pdTRUE, 0 ) == SOCKETS_START_DELETION )
{
/* Inform the main task that the event has been received. */
( void ) xEventGroupSetBits( ctx->rx_EventGroup, SOCKETS_COMPLETE_DELETION );
break;
}
}
prvDecrementRefCount( ctx );
vTaskDelete( NULL );
}
/*-----------------------------------------------------------*/
static void prvRxSelectSet( ss_ctx_t * ctx,
const void * pvOptionValue )
{
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
configSTACK_DEPTH_TYPE xStackDepth = socketsconfigRECEIVE_CALLBACK_TASK_STACK_DEPTH;
ctx->rx_callback = ( void ( * )( Socket_t ) )pvOptionValue;
prvIncrementRefCount( ctx );
ctx->rx_EventGroup = xEventGroupCreate();
configASSERT( ctx->rx_EventGroup != NULL );
xReturned = xTaskCreate( vTaskRxSelect, /* pvTaskCode */
"rxs", /* pcName */
xStackDepth, /* usStackDepth */
ctx, /* pvParameters */
1, /* uxPriority */
&xHandle ); /* pxCreatedTask */
configASSERT( xReturned == pdPASS );
configASSERT( xHandle != NULL );
ctx->rx_handle = xHandle;
}
/*-----------------------------------------------------------*/
static void prvRxSelectClear( ss_ctx_t * ctx )
{
/* Inform the vTaskRxSelect to delete itself. */
xEventGroupSetBits( ctx->rx_EventGroup, SOCKETS_START_DELETION );
/* Wait for the task to delete itself. */
while( xEventGroupWaitBits( ctx->rx_EventGroup,
SOCKETS_COMPLETE_DELETION,
pdTRUE,
pdTRUE,
pdMS_TO_TICKS( SECURE_SOCKETS_SELECT_WAIT_SEC * 1000 ) ) !=
SOCKETS_COMPLETE_DELETION )
{
/* Continue waiting for the task to delete itself. */
}
/* Reset the handle of the task to NULL. */
ctx->rx_handle = NULL;
/* Delete the event group. */
vEventGroupDelete( ctx->rx_EventGroup );
/* Remove the reference to the callback. */
ctx->rx_callback = NULL;
}
/*-----------------------------------------------------------*/
Socket_t SOCKETS_Socket( int32_t lDomain,
int32_t lType,
int32_t lProtocol )
{
ss_ctx_t * ctx;
configASSERT( lDomain == SOCKETS_AF_INET );
configASSERT( lType == SOCKETS_SOCK_STREAM );
configASSERT( lProtocol == SOCKETS_IPPROTO_TCP );
if( ( lDomain != SOCKETS_AF_INET ) ||
( lType != SOCKETS_SOCK_STREAM ) ||
( lProtocol != SOCKETS_IPPROTO_TCP ) ||
( sockets_allocated <= 0 )
)
{
return SOCKETS_INVALID_SOCKET;
}
ctx = ( ss_ctx_t * ) pvPortMalloc( sizeof( *ctx ) );
if( ctx )
{
memset( ctx, 0, sizeof( *ctx ) );
ctx->ip_socket = lwip_socket( lDomain, lType, lProtocol );
if( ctx->ip_socket >= 0 )
{
ctx->ulRefcount = 1;
sockets_allocated--;
return ( Socket_t ) ctx;
}
vPortFree( ctx );
}
return ( Socket_t ) SOCKETS_INVALID_SOCKET;
}
/*-----------------------------------------------------------*/
int32_t SOCKETS_Bind( Socket_t xSocket,
SocketsSockaddr_t * pxAddress,
Socklen_t xAddressLength )
{
ss_ctx_t * ctx;
int32_t ret;
struct sockaddr_in sa_addr = { 0 };
if( SOCKETS_INVALID_SOCKET == xSocket )
{
configPRINTF( ( "TCP socket Invalid\n" ) );
return SOCKETS_EINVAL;
}
if( pxAddress == NULL )
{
configPRINTF( ( "TCP socket Invalid Address\n" ) );
return SOCKETS_EINVAL;
}
ctx = ( ss_ctx_t * ) xSocket;
if( NULL == ctx )
{
configPRINTF( ( "Invalid secure socket passed: Socket=%p\n", ctx ) );
return SOCKETS_EINVAL;
}
if( 0 > ctx->ip_socket )
{
configPRINTF( ( "TCP socket Invalid index\n" ) );
return SOCKETS_EINVAL;
}
/*
* Setting SO_REUSEADDR socket option in order to be able to bind to the same ip:port again
* without netconn_bind failing.
*/
#if SO_REUSE
ret = lwip_setsockopt( ctx->ip_socket, SOL_SOCKET, SO_REUSEADDR, &( uint32_t ) { 1 }, sizeof( uint32_t ) );
if( 0 > ret )
{
return SOCKETS_SOCKET_ERROR;
}
#endif /* SO_REUSE */
sa_addr.sin_family = pxAddress->ucSocketDomain ? pxAddress->ucSocketDomain : AF_INET;
sa_addr.sin_addr.s_addr = pxAddress->ulAddress;
sa_addr.sin_port = pxAddress->usPort;
ret = lwip_bind( ctx->ip_socket, ( struct sockaddr * ) &sa_addr, sizeof( sa_addr ) );
if( 0 > ret )
{
configPRINTF( ( "lwip_bind fail :%d\n", ret ) );
return SOCKETS_SOCKET_ERROR;
}
return SOCKETS_ERROR_NONE;
}
/*-----------------------------------------------------------*/
int32_t SOCKETS_Connect( Socket_t xSocket,
SocketsSockaddr_t * pxAddress,
Socklen_t xAddressLength )
{
ss_ctx_t * ctx;
if( SOCKETS_INVALID_SOCKET == xSocket )
{
return SOCKETS_EINVAL;
}
/* removed because qualification program wants invalid length to go through */
#if 0
if( ( NULL == pxAddress ) || ( 0 == xAddressLength ) )
{
return SOCKETS_EINVAL;
}
#endif
if( pxAddress == NULL )
{
return SOCKETS_EINVAL;
}
/* support only SOCKETS_AF_INET for now */
pxAddress->ucSocketDomain = SOCKETS_AF_INET;
ctx = ( ss_ctx_t * ) xSocket;
configASSERT( ctx->ip_socket >= 0 );
struct sockaddr_in sa_addr = { 0 };
int ret;
sa_addr.sin_family = pxAddress->ucSocketDomain;
sa_addr.sin_addr.s_addr = pxAddress->ulAddress;
sa_addr.sin_port = pxAddress->usPort;
ret = lwip_connect( ctx->ip_socket,
( struct sockaddr * ) &sa_addr,
sizeof( sa_addr ) );
if( 0 == ret )
{
TLSParams_t tls_params = { 0 };
BaseType_t status;
ctx->status |= SS_STATUS_CONNECTED;
if( !ctx->enforce_tls )
{
return SOCKETS_ERROR_NONE;
}
tls_params.ulSize = sizeof( tls_params );
tls_params.pcDestination = ctx->destination;
tls_params.pcServerCertificate = ctx->server_cert;
tls_params.ulServerCertificateLength = ctx->server_cert_len;
tls_params.pvCallerContext = ctx;
tls_params.pxNetworkRecv = prvNetworkRecv;
tls_params.pxNetworkSend = prvNetworkSend;
tls_params.ppcAlpnProtocols = ( const char ** ) ctx->ppcAlpnProtocols;
tls_params.ulAlpnProtocolsCount = ctx->ulAlpnProtocolsCount;
status = TLS_Init( &ctx->tls_ctx, &tls_params );
if( pdFREERTOS_ERRNO_NONE != status )
{
configPRINTF( ( "TLS_Init fail\n" ) );
return SOCKETS_SOCKET_ERROR;
}
status = TLS_Connect( ctx->tls_ctx );
if( pdFREERTOS_ERRNO_NONE == status )
{
ctx->status |= SS_STATUS_SECURED;
return SOCKETS_ERROR_NONE;
}
else
{
configPRINTF( ( "TLS_Connect fail (0x%x, %s)\n",
( unsigned int ) -status,
ctx->destination ? ctx->destination : "NULL" ) );
}
}
else
{
configPRINTF( ( "LwIP connect fail %d %d\n", ret, errno ) );
}
return SOCKETS_SOCKET_ERROR;
}
/*-----------------------------------------------------------*/
int32_t SOCKETS_Recv( Socket_t xSocket,
void * pvBuffer,
size_t xBufferLength,
uint32_t ulFlags )
{
ss_ctx_t * ctx = ( ss_ctx_t * ) xSocket;
if( SOCKETS_INVALID_SOCKET == xSocket )
{
return SOCKETS_SOCKET_ERROR;
}
if( ( NULL == pvBuffer ) || ( 0 == xBufferLength ) )
{
return SOCKETS_EINVAL;
}
if( ( ctx->status & SS_STATUS_CONNECTED ) != SS_STATUS_CONNECTED )
{
return SOCKETS_ENOTCONN;
}
ctx->recv_flag = ulFlags;
configASSERT( ctx->ip_socket >= 0 );
if( ctx->enforce_tls )
{
/* Receive through TLS pipe, if negotiated. */
return TLS_Recv( ctx->tls_ctx, pvBuffer, xBufferLength );
}
else
{
return prvNetworkRecv( ( void * ) ctx, pvBuffer, xBufferLength );
}
}
/*-----------------------------------------------------------*/
int32_t SOCKETS_Send( Socket_t xSocket,
const void * pvBuffer,
size_t xDataLength,
uint32_t ulFlags )
{
ss_ctx_t * ctx;
if( SOCKETS_INVALID_SOCKET == xSocket )
{
return SOCKETS_SOCKET_ERROR;
}
if( ( NULL == pvBuffer ) || ( 0 == xDataLength ) )
{
return SOCKETS_EINVAL;
}
ctx = ( ss_ctx_t * ) xSocket;
if( ( ctx->status & SS_STATUS_CONNECTED ) != SS_STATUS_CONNECTED )
{
return SOCKETS_ENOTCONN;
}
configASSERT( ctx->ip_socket >= 0 );
ctx->send_flag = ulFlags;
if( ctx->enforce_tls )
{
/* Send through TLS pipe, if negotiated. */
return TLS_Send( ctx->tls_ctx, pvBuffer, xDataLength );
}
else
{
return prvNetworkSend( ( void * ) ctx, pvBuffer, xDataLength );
}
}
/*-----------------------------------------------------------*/
int32_t SOCKETS_Shutdown( Socket_t xSocket,
uint32_t ulHow )
{
ss_ctx_t * ctx;
int ret;
if( SOCKETS_INVALID_SOCKET == xSocket )
{
return SOCKETS_EINVAL;
}
ctx = ( ss_ctx_t * ) xSocket;
configASSERT( ctx->ip_socket >= 0 );
ret = lwip_shutdown( ctx->ip_socket, ( int ) ulHow );
if( 0 > ret )
{
return SOCKETS_SOCKET_ERROR;
}
return SOCKETS_ERROR_NONE;
}
/*-----------------------------------------------------------*/
int32_t SOCKETS_Close( Socket_t xSocket )
{
ss_ctx_t * ctx;
if( SOCKETS_INVALID_SOCKET == xSocket )
{
return SOCKETS_EINVAL;
}
ctx = ( ss_ctx_t * ) xSocket;
ctx->state = SST_RX_CLOSING;
lwip_close( ctx->ip_socket );
prvDecrementRefCount( ctx );
return SOCKETS_ERROR_NONE;
}
/*-----------------------------------------------------------*/
int32_t SOCKETS_SetSockOpt( Socket_t xSocket,
int32_t lLevel,
int32_t lOptionName,
const void * pvOptionValue,
size_t xOptionLength )
{
ss_ctx_t * ctx;
int ret = 0;
char ** ppcAlpnIn = ( char ** ) pvOptionValue;
size_t xLength = 0;
uint32_t ulProtocol;
if( SOCKETS_INVALID_SOCKET == xSocket )
{
return SOCKETS_EINVAL;
}
ctx = ( ss_ctx_t * ) xSocket;
configASSERT( ctx->ip_socket >= 0 );
switch( lOptionName )
{
case SOCKETS_SO_RCVTIMEO:
case SOCKETS_SO_SNDTIMEO:
{
TickType_t ticks;
struct timeval tv;
ticks = *( ( const TickType_t * ) pvOptionValue );
tv.tv_sec = TICK_TO_S( ticks );
tv.tv_usec = TICK_TO_US( ticks % configTICK_RATE_HZ );
ret = lwip_setsockopt( ctx->ip_socket,
SOL_SOCKET,
lOptionName == SOCKETS_SO_RCVTIMEO ?
SO_RCVTIMEO : SO_SNDTIMEO,
( struct timeval * ) &tv,
sizeof( tv ) );
if( 0 != ret )
{
return SOCKETS_EINVAL;
}
break;
}
case SOCKETS_SO_NONBLOCK:
{
int opt;
if( ( ctx->status & SS_STATUS_CONNECTED ) != SS_STATUS_CONNECTED )
{
return SOCKETS_ENOTCONN;
}
opt = 1;
ret = lwip_ioctl( ctx->ip_socket, FIONBIO, &opt );
if( 0 != ret )
{
return SOCKETS_EINVAL;
}
break;
}
case SOCKETS_SO_REQUIRE_TLS:
if( ctx->status & SS_STATUS_CONNECTED )
{
return SOCKETS_EISCONN;
}
ctx->enforce_tls = true;
break;
case SOCKETS_SO_TRUSTED_SERVER_CERTIFICATE:
if( ctx->status & SS_STATUS_CONNECTED )
{
return SOCKETS_EISCONN;
}
if( ( NULL == pvOptionValue ) || ( 0 == xOptionLength ) )
{
return SOCKETS_EINVAL;
}
if( ctx->server_cert )
{
vPortFree( ctx->server_cert );
}
ctx->server_cert = pvPortMalloc( xOptionLength + 1 );
if( NULL == ctx->server_cert )
{
return SOCKETS_ENOMEM;
}
memset( ctx->server_cert, 0, xOptionLength + 1 );
memcpy( ctx->server_cert, pvOptionValue, xOptionLength );
ctx->server_cert_len = xOptionLength;
break;
case SOCKETS_SO_SERVER_NAME_INDICATION:
if( ctx->status & SS_STATUS_CONNECTED )
{
return SOCKETS_EISCONN;
}
if( ( NULL == pvOptionValue ) || ( 0 == xOptionLength ) )
{
return SOCKETS_EINVAL;
}
if( ctx->destination )
{
vPortFree( ctx->destination );
}
ctx->destination = pvPortMalloc( xOptionLength + 1 );
if( NULL == ctx->destination )
{
return SOCKETS_ENOMEM;
}
memcpy( ctx->destination, pvOptionValue, xOptionLength );
ctx->destination[ xOptionLength ] = '\0';
break;
case SOCKETS_SO_WAKEUP_CALLBACK:
if( ( xOptionLength == sizeof( void * ) ) &&
( pvOptionValue != NULL ) )
{
prvRxSelectSet( ctx, pvOptionValue );
}
else
{
prvRxSelectClear( ctx );
}
break;
case SOCKETS_SO_ALPN_PROTOCOLS:
/* Do not set the ALPN option if the socket is already connected. */
if( ctx->status & SS_STATUS_CONNECTED )
{
return SOCKETS_EISCONN;
}
/* Allocate a sufficiently long array of pointers. */
ctx->ulAlpnProtocolsCount = 1 + xOptionLength;
if( NULL == ( ctx->ppcAlpnProtocols =
( char ** ) pvPortMalloc( ctx->ulAlpnProtocolsCount *
sizeof( char * ) ) ) )
{
return SOCKETS_ENOMEM;
}
else
{
memset( ctx->ppcAlpnProtocols,
0x00,
ctx->ulAlpnProtocolsCount * sizeof( char * ) );
}
/* Copy each protocol string. */
for( ulProtocol = 0; ( ulProtocol < ctx->ulAlpnProtocolsCount - 1 );
ulProtocol++ )
{
xLength = strlen( ppcAlpnIn[ ulProtocol ] );
if( NULL == ( ctx->ppcAlpnProtocols[ ulProtocol ] =
( char * ) pvPortMalloc( 1 + xLength ) ) )
{
ctx->ppcAlpnProtocols[ ulProtocol ] = NULL;
return SOCKETS_ENOMEM;
}
else
{
memcpy( ctx->ppcAlpnProtocols[ ulProtocol ],
ppcAlpnIn[ ulProtocol ],
xLength );
ctx->ppcAlpnProtocols[ ulProtocol ][ xLength ] = '\0';
}
}
break;
case SOCKETS_SO_TCPKEEPALIVE:
ret = lwip_setsockopt( ctx->ip_socket,
SOL_SOCKET,
SO_KEEPALIVE,
pvOptionValue,
sizeof( int ) );
break;
#if LWIP_TCP_KEEPALIVE
case SOCKETS_SO_TCPKEEPALIVE_INTERVAL:
ret = lwip_setsockopt( ctx->ip_socket,
IPPROTO_TCP,
TCP_KEEPINTVL,
pvOptionValue,
sizeof( int ) );
break;
case SOCKETS_SO_TCPKEEPALIVE_COUNT:
ret = lwip_setsockopt( ctx->ip_socket,
IPPROTO_TCP,
TCP_KEEPCNT,
pvOptionValue,
sizeof( int ) );
break;
case SOCKETS_SO_TCPKEEPALIVE_IDLE_TIME:
ret = lwip_setsockopt( ctx->ip_socket,
IPPROTO_TCP,
TCP_KEEPIDLE,
pvOptionValue,
sizeof( int ) );
break;
#endif /* if LWIP_TCP_KEEPALIVE */
default:
return SOCKETS_ENOPROTOOPT;
}
if( 0 > ret )
{
return SOCKETS_SOCKET_ERROR;
}
return ret;
}
/*-----------------------------------------------------------*/
/*
* Lwip DNS Found callback, compatible with type "dns_found_callback"
* declared in lwip/dns.h.
*
* NOTE: this resolves only ipv4 addresses; calls to dns_gethostbyname_addrtype()
* must specify dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4.
*/
static void lwip_dns_found_callback( const char * name,
const ip_addr_t * ipaddr,
void * callback_arg )
{
uint32_t * addr = ( uint32_t * ) callback_arg;
if( ipaddr != NULL )
{
*addr = *( ( uint32_t * ) ipaddr ); /* NOTE: IPv4 addresses only */
}
else
{
*addr = 0;
}
}
/*-----------------------------------------------------------*/
uint32_t SOCKETS_GetHostByName( const char * pcHostName )
{
uint32_t addr = 0; /* 0 indicates failure to caller */