-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreeRTOS_DNS.c
1657 lines (1438 loc) · 68.3 KB
/
FreeRTOS_DNS.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+TCP <DEVELOPMENT BRANCH>
* Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* 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 FreeRTOS_DNS.c
* @brief Implements the Domain Name System for the FreeRTOS+TCP network stack.
*/
/* Standard includes. */
#include <stdint.h>
#include <stdio.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_IP.h"
#include "FreeRTOS_IP_Timers.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_DNS.h"
#include "FreeRTOS_DHCP.h"
#include "NetworkBufferManagement.h"
#include "FreeRTOS_Routing.h"
#include "NetworkInterface.h"
#include "FreeRTOS_DNS_Globals.h"
#include "FreeRTOS_DNS_Cache.h"
#include "FreeRTOS_DNS_Parser.h"
#include "FreeRTOS_DNS_Networking.h"
#include "FreeRTOS_DNS_Callback.h"
/* Exclude the entire file if DNS is not enabled. */
#if ( ipconfigUSE_DNS != 0 )
/*
* Create the DNS message in the zero copy buffer passed in the first parameter.
*/
static size_t prvCreateDNSMessage( uint8_t * pucUDPPayloadBuffer,
const char * pcHostName,
TickType_t uxIdentifier,
UBaseType_t uxHostType );
/*
* Check if hostname is already known. If not, call prvGetHostByName() to send a DNS request.
*/
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
static uint32_t prvPrepareLookup( const char * pcHostName,
struct freertos_addrinfo ** ppxAddressInfo,
BaseType_t xFamily, /* FREERTOS_AF_INET4 / 6. */
FOnDNSEvent pCallbackFunction,
void * pvSearchID,
TickType_t uxTimeout );
#else
static uint32_t prvPrepareLookup( const char * pcHostName,
struct freertos_addrinfo ** ppxAddressInfo,
BaseType_t xFamily ); /* FREERTOS_AF_INET4 / 6. */
#endif
/*
* Prepare and send a message to a DNS server. 'uxReadTimeOut_ticks' will be passed as
* zero, in case the user has supplied a call-back function.
*/
static uint32_t prvGetHostByName( const char * pcHostName,
TickType_t uxIdentifier,
TickType_t uxReadTimeOut_ticks,
struct freertos_addrinfo ** ppxAddressInfo,
BaseType_t xFamily );
#if ( ipconfigUSE_LLMNR == 1 )
/** @brief The MAC address used for LLMNR. */
const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };
#endif /* ipconfigUSE_LLMNR == 1 */
/*-----------------------------------------------------------*/
#if ( ipconfigUSE_LLMNR == 1 ) && ( ipconfigUSE_IPv6 != 0 )
/**
* @brief The IPv6 link-scope multicast address
*/
const IPv6_Address_t ipLLMNR_IP_ADDR_IPv6 =
{
{ /* ff02::1:3 */
0xff, 0x02,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x01,
0x00, 0x03,
}
};
/**
* @brief The IPv6 link-scope multicast MAC address
*/
const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } };
#endif /* ipconfigUSE_LLMNR && ipconfigUSE_IPv6 */
#if ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_IPv6 != 0 )
/**
* @brief multicast DNS IPv6 address
*/
const IPv6_Address_t ipMDNS_IP_ADDR_IPv6 =
{
{ /* ff02::fb */
0xff, 0x02,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0xfb,
}
};
/**
* @brief The IPv6 multicast DNS MAC address.
* The MAC-addresses are provided here in case a network
* interface needs it.
*/
const MACAddress_t xMDNS_MACAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } };
#endif /* ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_IPv6 != 0 ) */
#if ( ipconfigUSE_MDNS == 1 )
/** @brief The MAC address used for MDNS. */
const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } };
#endif /* ipconfigUSE_MDNS == 1 */
/** @brief This global variable is being used to indicate to the driver which IP type
* is preferred for name service lookup, either IPv6 or IPv4. */
/* TODO: Fix IPv6 DNS query in Windows Simulator. */
IPPreference_t xDNS_IP_Preference = xPreferenceIPv4;
/*-----------------------------------------------------------*/
/**
* @brief A DNS query consists of a header, as described in 'struct xDNSMessage'
* It is followed by 1 or more queries, each one consisting of a name and a tail,
* with two fields: type and class
*/
#include "pack_struct_start.h"
struct xDNSTail
{
uint16_t usType; /**< Type of DNS message. */
uint16_t usClass; /**< Class of DNS message. */
}
#include "pack_struct_end.h"
typedef struct xDNSTail DNSTail_t;
#if ( ipconfigUSE_IPv4 != 0 )
/** @brief Increment the field 'ucDNSIndex', which is an index in the array */
static void prvIncreaseDNS4Index( NetworkEndPoint_t * pxEndPoint );
#endif
#if ( ipconfigUSE_IPv6 != 0 )
/** @brief Increment the field 'ucDNSIndex', which is an index in the array */
static void prvIncreaseDNS6Index( NetworkEndPoint_t * pxEndPoint );
#endif
/*-----------------------------------------------------------*/
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
/**
* @brief Define FreeRTOS_gethostbyname() as a normal blocking call.
* @param[in] pcHostName The hostname whose IP address is being searched for.
* @return The IP-address of the hostname.
*/
uint32_t FreeRTOS_gethostbyname( const char * pcHostName )
{
return FreeRTOS_gethostbyname_a( pcHostName, NULL, ( void * ) NULL, 0U );
}
#endif /* ipconfigDNS_USE_CALLBACKS == 1 */
/*-----------------------------------------------------------*/
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
/** @brief Initialise the list of call-back structures.
*/
void vDNSInitialise( void )
{
vDNSCallbackInitialise();
}
#endif /* ipconfigDNS_USE_CALLBACKS == 1 */
/*-----------------------------------------------------------*/
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
/**
* @brief Remove the entry defined by the search ID to cancel a DNS request.
* @param[in] pvSearchID The search ID of the callback function associated with
* the DNS request being cancelled. Note that the value of
* the pointer matters, not the pointee.
*/
void FreeRTOS_gethostbyname_cancel( void * pvSearchID )
{
vDNSCheckCallBack( pvSearchID );
}
#endif /* ipconfigDNS_USE_CALLBACKS == 1 */
/*-----------------------------------------------------------*/
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
/**
* @brief Look-up the IP-address of a host.
*
* @param[in] pcName The name of the node or device
* @param[in] pcService Ignored for now.
* @param[in] pxHints If not NULL preferences. Can be used to indicate the preferred type if IP ( v4 or v6 ).
* @param[out] ppxResult An allocated struct, containing the results.
*
* @return Zero when the operation was successful, otherwise a negative errno value.
*/
BaseType_t FreeRTOS_getaddrinfo( const char * pcName, /* The name of the node or device */
const char * pcService, /* Ignored for now. */
const struct freertos_addrinfo * pxHints, /* If not NULL: preferences. */
struct freertos_addrinfo ** ppxResult ) /* An allocated struct, containing the results. */
{
/* Call the asynchronous version with NULL parameters. */
return FreeRTOS_getaddrinfo_a( pcName, pcService, pxHints, ppxResult, NULL, NULL, 0U );
}
#endif /* ( ipconfigDNS_USE_CALLBACKS == 1 ) */
/*-----------------------------------------------------------*/
/**
* @brief Internal function: allocate and initialise a new struct of type freertos_addrinfo.
*
* @param[in] pcName the name of the host.
* @param[in] xFamily the type of IP-address: FREERTOS_AF_INET4 or FREERTOS_AF_INET6.
* @param[in] pucAddress The IP-address of the host.
*
* @return A pointer to the newly allocated struct, or NULL in case malloc failed..
*/
struct freertos_addrinfo * pxNew_AddrInfo( const char * pcName,
BaseType_t xFamily,
const uint8_t * pucAddress )
{
struct freertos_addrinfo * pxAddrInfo = NULL;
void * pvBuffer;
/* 'xFamily' might not be used when IPv6 is disabled. */
( void ) xFamily;
pvBuffer = pvPortMalloc( sizeof( *pxAddrInfo ) );
if( pvBuffer != NULL )
{
pxAddrInfo = ( struct freertos_addrinfo * ) pvBuffer;
( void ) memset( pxAddrInfo, 0, sizeof( *pxAddrInfo ) );
pxAddrInfo->ai_canonname = pxAddrInfo->xPrivateStorage.ucName;
( void ) strncpy( pxAddrInfo->xPrivateStorage.ucName, pcName, sizeof( pxAddrInfo->xPrivateStorage.ucName ) - 1U );
pxAddrInfo->xPrivateStorage.ucName[ sizeof( pxAddrInfo->xPrivateStorage.ucName ) - 1U ] = '\0';
pxAddrInfo->ai_addr = ( ( struct freertos_sockaddr * ) &( pxAddrInfo->xPrivateStorage.sockaddr ) );
switch( xFamily )
{
#if ( ipconfigUSE_IPv4 != 0 )
case FREERTOS_AF_INET4:
{
/* ulChar2u32 reads from big-endian to host-endian. */
uint32_t ulIPAddress = ulChar2u32( pucAddress );
/* Translate to network-endian. */
pxAddrInfo->ai_addr->sin_address.ulIP_IPv4 = FreeRTOS_htonl( ulIPAddress );
pxAddrInfo->ai_family = FREERTOS_AF_INET4;
pxAddrInfo->ai_addrlen = ipSIZE_OF_IPv4_ADDRESS;
}
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
#if ( ipconfigUSE_IPv6 != 0 )
case FREERTOS_AF_INET6:
pxAddrInfo->ai_family = FREERTOS_AF_INET6;
pxAddrInfo->ai_addrlen = ipSIZE_OF_IPv6_ADDRESS;
( void ) memcpy( pxAddrInfo->xPrivateStorage.sockaddr.sin_address.xIP_IPv6.ucBytes, pucAddress, ipSIZE_OF_IPv6_ADDRESS );
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
default:
/* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "pxNew_AddrInfo: Undefined xFamily Type \n" ) );
vPortFree( pvBuffer );
pxAddrInfo = NULL;
break;
}
}
return pxAddrInfo;
}
/*-----------------------------------------------------------*/
/**
* @brief Free a chain of structs of type 'freertos_addrinfo'.
* @param[in] pxInfo The first find result.
*/
void FreeRTOS_freeaddrinfo( struct freertos_addrinfo * pxInfo )
{
struct freertos_addrinfo * pxNext;
struct freertos_addrinfo * pxIterator = pxInfo;
if( pxInfo != NULL )
{
while( pxIterator != NULL )
{
pxNext = pxIterator->ai_next;
vPortFree( pxIterator );
pxIterator = pxNext;
}
}
}
/*-----------------------------------------------------------*/
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
/**
* @brief Asynchronous version of getaddrinfo().
*
* @param[in] pcName The name of the node or device
* @param[in] pcService Ignored for now.
* @param[in] pxHints If not NULL preferences. Can be used to indicate the preferred type if IP ( v4 or v6 ).
* @param[out] ppxResult An allocated struct, containing the results.
* @param[in] pCallback A user-defined function which will be called on completion, either when found or after a time-out.
* @param[in] pvSearchID A user provided void pointer that will be communicated on completion.
* @param[in] uxTimeout The maximum number of clock ticks that must be waited for a reply.
*
* @return Zero when the operation was successful, otherwise a negative errno value.
*/
BaseType_t FreeRTOS_getaddrinfo_a( const char * pcName, /* The name of the node or device */
const char * pcService, /* Ignored for now. */
const struct freertos_addrinfo * pxHints, /* If not NULL: preferences. */
struct freertos_addrinfo ** ppxResult, /* An allocated struct, containing the results. */
FOnDNSEvent pCallback,
void * pvSearchID,
TickType_t uxTimeout )
#else
/**
* @brief Look-up the IP-address of a host.
* @param[in] pcName The name of the node or device
* @param[in] pcService Ignored for now.
* @param[in] pxHints If not NULL preferences. Can be used to indicate the preferred type if IP ( v4 or v6 ).
* @param[out] ppxResult An allocated struct, containing the results.
* @return Zero when the operation was successful, otherwise a negative errno value.
*/
BaseType_t FreeRTOS_getaddrinfo( const char * pcName, /* The name of the node or device */
const char * pcService, /* Ignored for now. */
const struct freertos_addrinfo * pxHints, /* If not NULL: preferences. */
struct freertos_addrinfo ** ppxResult ) /* An allocated struct, containing the results. */
#endif /* ipconfigDNS_USE_CALLBACKS == 1 */
{
BaseType_t xReturn = 0;
uint32_t ulResult;
BaseType_t xFamily = FREERTOS_AF_INET4;
( void ) pcService;
( void ) pxHints;
if( ppxResult != NULL )
{
*( ppxResult ) = NULL;
#if ( ipconfigUSE_IPv6 != 0 )
if( pxHints != NULL )
{
if( pxHints->ai_family == FREERTOS_AF_INET6 )
{
xFamily = FREERTOS_AF_INET6;
}
else if( pxHints->ai_family != FREERTOS_AF_INET4 )
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
else
{
/* This is FREERTOS_AF_INET4, carry on. */
}
}
#endif /* ( ipconfigUSE_IPv6 == 0 ) */
#if ( ipconfigUSE_IPv6 != 0 )
if( xReturn == 0 )
#endif
{
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
{
ulResult = prvPrepareLookup( pcName, ppxResult, xFamily, pCallback, pvSearchID, uxTimeout );
}
#else
{
ulResult = prvPrepareLookup( pcName, ppxResult, xFamily );
}
#endif /* ( ipconfigDNS_USE_CALLBACKS == 1 ) */
if( ulResult != 0U )
{
if( *( ppxResult ) != NULL )
{
xReturn = 0;
}
else
{
xReturn = -pdFREERTOS_ERRNO_ENOMEM;
}
}
else
{
xReturn = -pdFREERTOS_ERRNO_ENOENT;
}
}
}
else
{
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}
return xReturn;
}
/*-----------------------------------------------------------*/
#if ( ipconfigDNS_USE_CALLBACKS == 0 )
/**
* @brief Get the IP-address corresponding to the given hostname.
* @param[in] pcHostName The hostname whose IP address is being queried.
* @return The IP-address corresponding to the hostname. 0 is returned in
* case of failure.
*/
uint32_t FreeRTOS_gethostbyname( const char * pcHostName )
{
return prvPrepareLookup( pcHostName, NULL, FREERTOS_AF_INET4 );
}
#else
/**
* @brief Get the IP-address corresponding to the given hostname.
* @param[in] pcHostName The hostname whose IP address is being queried.
* @param[in] pCallback The callback function which will be called upon DNS response. It will be called
* with pcHostName, pvSearchID and pxAddressInfo which points to address info.
* The pxAddressInfo should be freed by the application once the callback
* has been called by the FreeRTOS_freeaddrinfo().
* In case of timeouts pxAddressInfo can be NULL.
* @param[in] pvSearchID Search ID for the callback function.
* @param[in] uxTimeout Timeout for the callback function.
* @return The IP-address corresponding to the hostname. 0 is returned in case of
* failure.
*/
uint32_t FreeRTOS_gethostbyname_a( const char * pcHostName,
FOnDNSEvent pCallback,
void * pvSearchID,
TickType_t uxTimeout )
{
uint32_t ulResult;
struct freertos_addrinfo * pxAddressInfo = NULL;
ulResult = prvPrepareLookup( pcHostName, &( pxAddressInfo ), FREERTOS_AF_INET4, pCallback, pvSearchID, uxTimeout );
if( pxAddressInfo != NULL )
{
FreeRTOS_freeaddrinfo( pxAddressInfo );
}
return ulResult;
}
#endif /* if ( ipconfigDNS_USE_CALLBACKS == 0 ) */
#if ( ipconfigINCLUDE_FULL_INET_ADDR == 1 )
/**
* @brief See if pcHostName contains a valid IPv4 or IPv6 IP-address.
* @param[in] pcHostName The name to be looked up
* @param[in] xFamily the IP-type, either FREERTOS_AF_INET4 or FREERTOS_AF_INET6.
* @param[in] ppxAddressInfo A pointer to a pointer where the find results will
* be stored.
* @return Either 0 or an IP=address.
*/
static uint32_t prvPrepare_ReadIPAddress( const char * pcHostName,
BaseType_t xFamily,
struct freertos_addrinfo ** ppxAddressInfo )
{
uint32_t ulIPAddress = 0U;
( void ) xFamily;
/* Check if the hostname given is actually an IP-address. */
switch( xFamily ) /* LCOV_EXCL_BR_LINE - Family is always either FREERTOS_AF_INET or FREERTOS_AF_INET6. */
{
#if ( ipconfigUSE_IPv4 != 0 )
case FREERTOS_AF_INET:
ulIPAddress = FreeRTOS_inet_addr( pcHostName );
if( ( ulIPAddress != 0U ) && ( ppxAddressInfo != NULL ) )
{
const uint8_t * ucBytes = ( uint8_t * ) &( ulIPAddress );
*( ppxAddressInfo ) = pxNew_AddrInfo( pcHostName, FREERTOS_AF_INET4, ucBytes );
}
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
#if ( ipconfigUSE_IPv6 != 0 )
case FREERTOS_AF_INET6:
{
IPv6_Address_t xAddress_IPv6;
BaseType_t xResult;
/* ulIPAddress does not represent an IPv4 address here. It becomes non-zero when the look-up succeeds. */
xResult = FreeRTOS_inet_pton6( pcHostName, xAddress_IPv6.ucBytes );
if( xResult == 1 )
{
/* This function returns either a valid IPv4 address, or
* in case of an IPv6 lookup, it will return a non-zero */
ulIPAddress = 1U;
/* ppxAddressInfo is always non-NULL in IPv6 case. */
*( ppxAddressInfo ) = pxNew_AddrInfo( pcHostName, FREERTOS_AF_INET6, xAddress_IPv6.ucBytes );
}
}
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
default: /* LCOV_EXCL_LINE - Family is always either FREERTOS_AF_INET or FREERTOS_AF_INET6. */
/* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "prvPrepare_ReadIPAddress: Undefined xFamily Type \n" ) );
break; /* LCOV_EXCL_LINE - Family is always either FREERTOS_AF_INET or FREERTOS_AF_INET6. */
}
return ulIPAddress;
}
#endif /* ( ipconfigINCLUDE_FULL_INET_ADDR == 1 ) */
/*-----------------------------------------------------------*/
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
/**
* @brief Check if hostname is already known. If not, call prvGetHostByName() to send a DNS request.
*
* @param[in] pcHostName The hostname whose IP address is being queried.
* @param[in,out] ppxAddressInfo A pointer to a pointer where the find results
* will be stored.
* @param [in] xFamily indicate what type of record is needed:
* FREERTOS_AF_INET4 or FREERTOS_AF_INET6.
* @param[in] pCallbackFunction The callback function which will be called upon DNS response.
* @param[in] pvSearchID Search ID for the callback function.
* @param[in] uxTimeout Timeout for the callback function.
* @return The IP-address corresponding to the hostname.
*/
static uint32_t prvPrepareLookup( const char * pcHostName,
struct freertos_addrinfo ** ppxAddressInfo,
BaseType_t xFamily,
FOnDNSEvent pCallbackFunction,
void * pvSearchID,
TickType_t uxTimeout )
#else
/**
* @brief Check if hostname is already known. If not, call prvGetHostByName() to send a DNS request.
* @param[in] pcHostName The hostname whose IP address is being queried.
* @return The IP-address corresponding to the hostname.
*/
static uint32_t prvPrepareLookup( const char * pcHostName,
struct freertos_addrinfo ** ppxAddressInfo,
BaseType_t xFamily )
#endif /* if ( ipconfigDNS_USE_CALLBACKS == 1 ) */
{
uint32_t ulIPAddress = 0U;
TickType_t uxReadTimeOut_ticks = ipconfigDNS_RECEIVE_BLOCK_TIME_TICKS;
/* Generate a unique identifier for this query. Keep it in a local variable
* as gethostbyname() may be called from different threads */
BaseType_t xHasRandom = pdFALSE;
TickType_t uxIdentifier = 0U;
#if ( ipconfigUSE_DNS_CACHE != 0 )
BaseType_t xLengthOk = pdFALSE;
#endif
#if ( ipconfigUSE_DNS_CACHE != 0 )
{
if( pcHostName != NULL )
{
size_t uxLength = strlen( pcHostName ) + 1U;
if( uxLength <= ipconfigDNS_CACHE_NAME_LENGTH )
{
/* The name is not too long. */
xLengthOk = pdTRUE;
}
else
{
FreeRTOS_printf( ( "prvPrepareLookup: name is too long ( %u > %u )\n",
( unsigned ) uxLength,
( unsigned ) ipconfigDNS_CACHE_NAME_LENGTH ) );
}
}
}
if( ( pcHostName != NULL ) && ( xLengthOk != pdFALSE ) )
#else /* if ( ipconfigUSE_DNS_CACHE != 0 ) */
if( pcHostName != NULL )
#endif /* ( ipconfigUSE_DNS_CACHE != 0 ) */
{
/* If the supplied hostname is an IP address, put it in ppxAddressInfo
* and return. */
#if ( ipconfigINCLUDE_FULL_INET_ADDR == 1 )
{
ulIPAddress = prvPrepare_ReadIPAddress( pcHostName, xFamily, ppxAddressInfo );
}
#endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */
/* If a DNS cache is used then check the cache before issuing another DNS
* request. */
#if ( ipconfigUSE_DNS_CACHE == 1 )
/* Check the cache before issuing another DNS request. */
if( ulIPAddress == 0U )
{
ulIPAddress = Prepare_CacheLookup( pcHostName, xFamily, ppxAddressInfo );
if( ulIPAddress != 0UL )
{
#if ( ipconfigUSE_IPv6 != 0 )
if( ( ppxAddressInfo != NULL ) && ( ( *ppxAddressInfo )->ai_family == FREERTOS_AF_INET6 ) )
{
FreeRTOS_printf( ( "prvPrepareLookup: found '%s' in cache: %pip\n",
pcHostName, ( void * ) ( *ppxAddressInfo )->xPrivateStorage.sockaddr.sin_address.xIP_IPv6.ucBytes ) );
}
else
#endif
{
FreeRTOS_printf( ( "prvPrepareLookup: found '%s' in cache: %xip\n", pcHostName, ( unsigned ) ulIPAddress ) );
}
}
}
#endif /* ipconfigUSE_DNS_CACHE == 1 */
/* Generate a unique identifier. */
if( ulIPAddress == 0U )
{
uint32_t ulNumber;
xHasRandom = xApplicationGetRandomNumber( &( ulNumber ) );
/* DNS identifiers are 16-bit. */
uxIdentifier = ( TickType_t ) ( ulNumber & 0xffffU );
}
#if ( ipconfigDNS_USE_CALLBACKS == 1 )
{
if( pCallbackFunction != NULL )
{
if( ulIPAddress == 0U )
{
/* The user has provided a callback function, so do not block on recvfrom() */
if( xHasRandom != pdFALSE )
{
uxReadTimeOut_ticks = 0U;
vDNSSetCallBack( pcHostName,
pvSearchID,
pCallbackFunction,
uxTimeout,
( TickType_t ) uxIdentifier,
( xFamily == FREERTOS_AF_INET6 ) ? pdTRUE : pdFALSE );
}
}
else /* When ipconfigDNS_USE_CALLBACKS enabled, ppxAddressInfo is always non null. */
{
/* The IP address is known, do the call-back now. */
pCallbackFunction( pcHostName, pvSearchID, *( ppxAddressInfo ) );
}
}
}
#endif /* if ( ipconfigDNS_USE_CALLBACKS == 1 ) */
if( ( ulIPAddress == 0U ) && ( xHasRandom != pdFALSE ) )
{
ulIPAddress = prvGetHostByName( pcHostName,
uxIdentifier,
uxReadTimeOut_ticks,
ppxAddressInfo,
xFamily );
}
}
return ulIPAddress;
}
/*-----------------------------------------------------------*/
#if ( ipconfigUSE_IPv6 != 0 )
/**
* @brief Increment the field 'ucDNSIndex', which is an index in the array
* of DNS addresses.
* @param[in] pxEndPoint The end-point of which the DNS index should be
* incremented.
*/
static void prvIncreaseDNS6Index( NetworkEndPoint_t * pxEndPoint )
{
uint8_t ucIndex = pxEndPoint->ipv6_settings.ucDNSIndex;
uint8_t ucInitialIndex = ucIndex;
for( ; ; )
{
ucIndex++;
if( ucIndex >= ( uint8_t ) ipconfigENDPOINT_DNS_ADDRESS_COUNT )
{
ucIndex = 0U;
}
if( ( pxEndPoint->ipv6_settings.xDNSServerAddresses[ ucIndex ].ucBytes[ 0 ] != 0U ) ||
( ucInitialIndex == ucIndex ) )
{
break;
}
}
FreeRTOS_printf( ( "prvIncreaseDNS6Index: from %d to %d\n", ( int ) ucInitialIndex, ( int ) ucIndex ) );
pxEndPoint->ipv6_settings.ucDNSIndex = ucIndex;
}
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
/*-----------------------------------------------------------*/
#if ( ipconfigUSE_IPv4 != 0 )
/**
* @brief Increment the field 'ucDNSIndex', which is an index in the array
* of DNS addresses.
* @param[in] pxEndPoint The end-point of which the DNS index should be
* incremented.
*/
static void prvIncreaseDNS4Index( NetworkEndPoint_t * pxEndPoint )
{
uint8_t ucIndex = pxEndPoint->ipv4_settings.ucDNSIndex;
uint8_t ucInitialIndex = ucIndex;
for( ; ; )
{
ucIndex++;
if( ucIndex >= ( uint8_t ) ipconfigENDPOINT_DNS_ADDRESS_COUNT )
{
ucIndex = 0U;
}
if( ( pxEndPoint->ipv4_settings.ulDNSServerAddresses[ ucIndex ] != 0U ) ||
( ucInitialIndex == ucIndex ) )
{
break;
}
}
FreeRTOS_printf( ( "prvIncreaseDNS4Index: from %d to %d\n", ( int ) ucInitialIndex, ( int ) ucIndex ) );
pxEndPoint->ipv4_settings.ucDNSIndex = ucIndex;
}
/*-----------------------------------------------------------*/
#endif /* #if ( ipconfigUSE_IPv4 != 0 ) */
/*!
* @brief create a payload buffer and return it through the parameter
* @param [out] ppxNetworkBuffer network buffer to create
* @param [in] pcHostName hostname to get its length
* @param [in] uxHeaderBytes Size of the header (IPv4/IPv6)
* @returns pointer address to the payload buffer
*
*/
static uint8_t * prvGetPayloadBuffer( NetworkBufferDescriptor_t ** ppxNetworkBuffer,
const char * pcHostName,
size_t uxHeaderBytes )
{
size_t uxExpectedPayloadLength;
uint8_t * pucUDPPayloadBuffer = NULL;
uxExpectedPayloadLength = sizeof( DNSMessage_t ) +
strlen( pcHostName ) +
sizeof( uint16_t ) +
sizeof( uint16_t ) + 2U;
/* Get a buffer. This uses a maximum delay, but the delay will be
* capped to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value
* still needs to be tested. */
*ppxNetworkBuffer = pxGetNetworkBufferWithDescriptor( uxExpectedPayloadLength +
uxHeaderBytes,
0U );
if( *ppxNetworkBuffer != NULL )
{
pucUDPPayloadBuffer = &( ( *ppxNetworkBuffer )->pucEthernetBuffer[ uxHeaderBytes ] );
}
return pucUDPPayloadBuffer;
}
/*!
* @brief fill pxAddress from pucUDPPayloadBuffer
* @param [out] pxAddress ip address and port ... structure
* @param [in] pcHostName hostname to get its length
* @return The end-point that holds the DNS address.
*/
static NetworkEndPoint_t * prvFillSockAddress( struct freertos_sockaddr * pxAddress,
const char * pcHostName )
{
NetworkEndPoint_t * pxEndPoint = NULL;
/* If LLMNR is being used then determine if the host name includes a '.' -
* if not then LLMNR can be used as the lookup method. */
/* For local resolution, mDNS uses names ending with the string ".local" */
BaseType_t bHasDot = pdFALSE;
BaseType_t bHasLocal = pdFALSE;
const char * pcDot = ( const char * ) strchr( pcHostName, ( int32_t ) '.' );
#if ( ipconfigUSE_LLMNR != 1 )
( void ) pcHostName;
#endif
/* Make sure all fields of the 'sockaddr' are cleared. */
( void ) memset( ( void * ) pxAddress, 0, sizeof( *pxAddress ) );
/* And set the address type to IPv4.
* It may change to IPv6 in case an IPv6 DNS server will be used. */
pxAddress->sin_family = FREERTOS_AF_INET;
/* 'sin_len' doesn't really matter, 'sockaddr' and 'sockaddr6'
* have the same size. */
pxAddress->sin_len = ( uint8_t ) sizeof( struct freertos_sockaddr );
/* Use the DNS port by default, this may be changed later. */
pxAddress->sin_port = dnsDNS_PORT;
if( pcDot != NULL )
{
bHasDot = pdTRUE;
if( strcmp( pcDot, ".local" ) == 0 )
{
bHasLocal = pdTRUE;
}
else
{
/* a DNS look-up of a public URL with at least one dot. */
}
}
/* Is this a local lookup? */
if( ( bHasDot == pdFALSE ) || ( bHasLocal == pdTRUE ) )
{
/* Looking for e.g. "mydevice" or "mydevice.local",
* while using either mDNS or LLMNR. */
#if ( ipconfigUSE_MDNS == 1 )
{
if( bHasLocal )
{
/* Looking up a name like "mydevice.local".
* Use mDNS addresses. */
pxAddress->sin_port = ipMDNS_PORT;
pxAddress->sin_port = FreeRTOS_ntohs( pxAddress->sin_port );
switch( xDNS_IP_Preference ) /* LCOV_EXCL_BR_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
{
#if ( ipconfigUSE_IPv4 != 0 )
case xPreferenceIPv4:
pxAddress->sin_address.ulIP_IPv4 = ipMDNS_IP_ADDRESS; /* Is in network byte order. */
/* sin_family is default set to FREERTOS_AF_INET */
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
#if ( ipconfigUSE_IPv6 != 0 )
case xPreferenceIPv6:
memcpy( pxAddress->sin_address.xIP_IPv6.ucBytes,
ipMDNS_IP_ADDR_IPv6.ucBytes,
ipSIZE_OF_IPv6_ADDRESS );
pxAddress->sin_family = FREERTOS_AF_INET6;
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
default: /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
/* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "prvFillSockAddress: Undefined xDNS_IP_Preference \n" ) );
break; /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
}
}
}
#endif /* if ( ipconfigUSE_MDNS == 1 ) */
#if ( ipconfigUSE_LLMNR == 1 )
{
/* The hostname doesn't have a dot. */
if( bHasDot == pdFALSE )
{
/* Use LLMNR addressing. */
pxAddress->sin_port = ipLLMNR_PORT;
pxAddress->sin_port = FreeRTOS_ntohs( pxAddress->sin_port );
switch( xDNS_IP_Preference ) /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
{
#if ( ipconfigUSE_IPv4 != 0 )
case xPreferenceIPv4:
pxAddress->sin_address.ulIP_IPv4 = ipLLMNR_IP_ADDR; /* Is in network byte order. */
pxAddress->sin_family = FREERTOS_AF_INET;
break;
#endif /* ( ipconfigUSE_IPv4 != 0 ) */
#if ( ipconfigUSE_IPv6 != 0 )
case xPreferenceIPv6:
memcpy( pxAddress->sin_address.xIP_IPv6.ucBytes,
ipLLMNR_IP_ADDR_IPv6.ucBytes,
ipSIZE_OF_IPv6_ADDRESS );
pxAddress->sin_family = FREERTOS_AF_INET6;
break;
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
default: /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
/* MISRA 16.4 Compliance */
FreeRTOS_debug_printf( ( "prvFillSockAddress: Undefined xDNS_IP_Preference (LLMNR) \n" ) );
break; /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
}
}
}
#endif /* if ( ipconfigUSE_LLMNR == 1 ) */
#if ( ipconfigUSE_MDNS == 1 ) || ( ipconfigUSE_LLMNR == 1 )
for( pxEndPoint = FreeRTOS_FirstEndPoint( NULL );
pxEndPoint != NULL;
pxEndPoint = FreeRTOS_NextEndPoint( NULL, pxEndPoint ) )
{
#if ( ipconfigUSE_IPv6 != 0 )
if( xDNS_IP_Preference == xPreferenceIPv6 )
{
if( pxEndPoint->bits.bIPv6 != 0U )
{
break;
}
}
else
{
#if ( ipconfigUSE_IPv4 != 0 )
if( pxEndPoint->bits.bIPv6 == 0U )
{
break;
}
#endif /* if ( ipconfigUSE_IPv4 != 0 ) */
}
#else /* if ( ipconfigUSE_IPv6 != 0 ) */
/* IPv6 is not included, so all end-points are IPv4. */
break;
#endif /* if ( ipconfigUSE_IPv6 != 0 ) */
}
#endif /* if ( ipconfigUSE_MDNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) */
}
else
{
BaseType_t xBreakLoop = pdFALSE;
/* Look for an end-point that has defined a DNS server address. */
for( pxEndPoint = FreeRTOS_FirstEndPoint( NULL );
pxEndPoint != NULL;
pxEndPoint = FreeRTOS_NextEndPoint( NULL, pxEndPoint ) )
{
switch( xDNS_IP_Preference ) /* LCOV_EXCL_LINE - xDNS_IP_Preference can be either xPreferenceIPv4 or xPreferenceIPv6 */
{
#if ( ipconfigUSE_IPv4 != 0 )
case xPreferenceIPv4:
if( pxEndPoint->bits.bIPv6 == 0U )
{
uint32_t ulIPAddress;
uint8_t ucIndex = pxEndPoint->ipv4_settings.ucDNSIndex;