FreeRTOS_DNS.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  1. /*
  2. * FreeRTOS+TCP V2.3.2 LTS Patch 1
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. /**
  26. * @file FreeRTOS_DNS.c
  27. * @brief Implements the Domain Name System for the FreeRTOS+TCP network stack.
  28. */
  29. /* Standard includes. */
  30. #include <stdint.h>
  31. #include <stdio.h>
  32. /* FreeRTOS includes. */
  33. #include "FreeRTOS.h"
  34. #include "task.h"
  35. #include "semphr.h"
  36. /* FreeRTOS+TCP includes. */
  37. #include "FreeRTOS_IP.h"
  38. #include "FreeRTOS_Sockets.h"
  39. #include "FreeRTOS_IP_Private.h"
  40. #include "FreeRTOS_UDP_IP.h"
  41. #include "FreeRTOS_DNS.h"
  42. #include "FreeRTOS_DHCP.h"
  43. #include "NetworkBufferManagement.h"
  44. #include "NetworkInterface.h"
  45. /* Exclude the entire file if DNS is not enabled. */
  46. #if ( ipconfigUSE_DNS != 0 )
  47. #if ( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
  48. #define dnsDNS_PORT 0x3500U /**< Little endian: Port used for DNS. */
  49. #define dnsONE_QUESTION 0x0100U /**< Little endian representation of a DNS question.*/
  50. #define dnsOUTGOING_FLAGS 0x0001U /**< Little endian representation of standard query. */
  51. #define dnsRX_FLAGS_MASK 0x0f80U /**< Little endian: The bits of interest in the flags field of incoming DNS messages. */
  52. #define dnsEXPECTED_RX_FLAGS 0x0080U /**< Little Endian: Should be a response, without any errors. */
  53. #else
  54. #define dnsDNS_PORT 0x0035U /**< Big endian: Port used for DNS. */
  55. #define dnsONE_QUESTION 0x0001U /**< Big endian representation of a DNS question.*/
  56. #define dnsOUTGOING_FLAGS 0x0100U /**< Big endian representation of standard query. */
  57. #define dnsRX_FLAGS_MASK 0x800fU /**< Big endian: The bits of interest in the flags field of incoming DNS messages. */
  58. #define dnsEXPECTED_RX_FLAGS 0x8000U /**< Big endian: Should be a response, without any errors. */
  59. #endif /* ipconfigBYTE_ORDER */
  60. /** @brief The maximum number of times a DNS request should be sent out if a response
  61. * is not received, before giving up. */
  62. #ifndef ipconfigDNS_REQUEST_ATTEMPTS
  63. #define ipconfigDNS_REQUEST_ATTEMPTS 5
  64. #endif
  65. /** @brief If the top two bits in the first character of a name field are set then the
  66. * name field is an offset to the string, rather than the string itself. */
  67. #define dnsNAME_IS_OFFSET ( ( uint8_t ) 0xc0 )
  68. /* NBNS flags. */
  69. #if ( ipconfigUSE_NBNS == 1 )
  70. #define dnsNBNS_FLAGS_RESPONSE 0x8000U /**< NBNS response flag. */
  71. #define dnsNBNS_FLAGS_OPCODE_MASK 0x7800U /**< NBNS opcode bitmask. */
  72. #define dnsNBNS_FLAGS_OPCODE_QUERY 0x0000U /**< NBNS opcode query. */
  73. #endif /* ( ipconfigUSE_NBNS == 1 ) */
  74. /* Host types. */
  75. #define dnsTYPE_A_HOST 0x01U /**< DNS type A host. */
  76. #define dnsCLASS_IN 0x01U /**< DNS class IN (Internet). */
  77. #ifndef _lint
  78. /* LLMNR constants. */
  79. #define dnsLLMNR_TTL_VALUE 300000UL /**< LLMNR time to live value. */
  80. #define dnsLLMNR_FLAGS_IS_REPONSE 0x8000U /**< LLMNR flag value for response. */
  81. #endif /* _lint */
  82. /* NBNS constants. */
  83. #if ( ipconfigUSE_NBNS != 0 )
  84. #define dnsNBNS_TTL_VALUE 3600UL /**< NBNS TTL: 1 hour valid. */
  85. #define dnsNBNS_TYPE_NET_BIOS 0x0020U /**< NBNS Type: NetBIOS. */
  86. #define dnsNBNS_CLASS_IN 0x01U /**< NBNS Class: IN (Internet). */
  87. #define dnsNBNS_NAME_FLAGS 0x6000U /**< NBNS name flags. */
  88. #define dnsNBNS_ENCODED_NAME_LENGTH 32 /**< NBNS encoded name length. */
  89. /** @brief If the queried NBNS name matches with the device's name,
  90. * the query will be responded to with these flags: */
  91. #define dnsNBNS_QUERY_RESPONSE_FLAGS ( 0x8500U )
  92. #endif /* ( ipconfigUSE_NBNS != 0 ) */
  93. /** @brief Flag DNS parsing errors in situations where an IPv4 address is the return
  94. * type. */
  95. #define dnsPARSE_ERROR 0UL
  96. #ifndef _lint
  97. #if ( ipconfigUSE_DNS_CACHE == 0 )
  98. #if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY != 1 )
  99. #error When DNS caching is disabled, please make ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY equal to 1.
  100. #endif
  101. #endif
  102. #endif
  103. /** @brief Define the ASCII value of '.' (Period/Full-stop). */
  104. #define ASCII_BASELINE_DOT 46U
  105. /*
  106. * Create a socket and bind it to the standard DNS port number. Return the
  107. * the created socket - or NULL if the socket could not be created or bound.
  108. */
  109. static Socket_t prvCreateDNSSocket( void );
  110. /*
  111. * Create the DNS message in the zero copy buffer passed in the first parameter.
  112. */
  113. _static size_t prvCreateDNSMessage( uint8_t * pucUDPPayloadBuffer,
  114. const char * pcHostName,
  115. TickType_t uxIdentifier );
  116. /*
  117. * Simple routine that jumps over the NAME field of a resource record.
  118. * It returns the number of bytes read.
  119. */
  120. _static size_t prvSkipNameField( const uint8_t * pucByte,
  121. size_t uxLength );
  122. /*
  123. * Process a response packet from a DNS server.
  124. * The parameter 'xExpected' indicates whether the identifier in the reply
  125. * was expected, and thus if the DNS cache may be updated with the reply.
  126. */
  127. _static uint32_t prvParseDNSReply( uint8_t * pucUDPPayloadBuffer,
  128. size_t uxBufferLength,
  129. BaseType_t xExpected );
  130. /*
  131. * Check if hostname is already known. If not, call prvGetHostByName() to send a DNS request.
  132. */
  133. #if ( ipconfigDNS_USE_CALLBACKS == 1 )
  134. static uint32_t prvPrepareLookup( const char * pcHostName,
  135. FOnDNSEvent pCallback,
  136. void * pvSearchID,
  137. TickType_t uxTimeout );
  138. #else
  139. static uint32_t prvPrepareLookup( const char * pcHostName );
  140. #endif
  141. /*
  142. * Prepare and send a message to a DNS server. 'uxReadTimeOut_ticks' will be passed as
  143. * zero, in case the user has supplied a call-back function.
  144. */
  145. static uint32_t prvGetHostByName( const char * pcHostName,
  146. TickType_t uxIdentifier,
  147. TickType_t uxReadTimeOut_ticks );
  148. #if ( ipconfigDNS_USE_CALLBACKS != 0 )
  149. static void vDNSSetCallBack( const char * pcHostName,
  150. void * pvSearchID,
  151. FOnDNSEvent pCallbackFunction,
  152. TickType_t uxTimeout,
  153. TickType_t uxIdentifier );
  154. #endif /* ipconfigDNS_USE_CALLBACKS */
  155. #if ( ipconfigDNS_USE_CALLBACKS != 0 )
  156. static BaseType_t xDNSDoCallback( TickType_t uxIdentifier,
  157. const char * pcName,
  158. uint32_t ulIPAddress );
  159. #endif /* ipconfigDNS_USE_CALLBACKS */
  160. /*
  161. * The NBNS and the LLMNR protocol share this reply function.
  162. */
  163. #if ( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
  164. static void prvReplyDNSMessage( NetworkBufferDescriptor_t * pxNetworkBuffer,
  165. BaseType_t lNetLength );
  166. #endif
  167. #if ( ipconfigUSE_NBNS == 1 )
  168. static portINLINE void prvTreatNBNS( uint8_t * pucPayload,
  169. size_t uxBufferLength,
  170. uint32_t ulIPAddress );
  171. #endif /* ipconfigUSE_NBNS */
  172. #if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
  173. _static size_t prvReadNameField( const uint8_t * pucByte,
  174. size_t uxRemainingBytes,
  175. char * pcName,
  176. size_t uxDestLen );
  177. #endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
  178. #if ( ipconfigUSE_DNS_CACHE == 1 )
  179. static BaseType_t prvProcessDNSCache( const char * pcName,
  180. uint32_t * pulIP,
  181. uint32_t ulTTL,
  182. BaseType_t xLookUp );
  183. typedef struct xDNS_CACHE_TABLE_ROW
  184. {
  185. uint32_t ulIPAddresses[ ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY ]; /* The IP address(es) of an ARP cache entry. */
  186. char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ]; /* The name of the host */
  187. uint32_t ulTTL; /* Time-to-Live (in seconds) from the DNS server. */
  188. uint32_t ulTimeWhenAddedInSeconds;
  189. #if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 )
  190. uint8_t ucNumIPAddresses;
  191. uint8_t ucCurrentIPAddress;
  192. #endif
  193. } DNSCacheRow_t;
  194. static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ];
  195. /* Utility function: Clear DNS cache by calling this function. */
  196. void FreeRTOS_dnsclear( void )
  197. {
  198. ( void ) memset( xDNSCache, 0x0, sizeof( xDNSCache ) );
  199. }
  200. #endif /* ipconfigUSE_DNS_CACHE == 1 */
  201. #if ( ipconfigUSE_LLMNR == 1 )
  202. /** @brief The MAC address used for LLMNR. */
  203. const MACAddress_t xLLMNR_MacAdress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };
  204. #endif /* ipconfigUSE_LLMNR == 1 */
  205. /*-----------------------------------------------------------*/
  206. /* Below #include just tells the compiler to pack the structure.
  207. * It is included in to make the code more readable */
  208. #include "pack_struct_start.h"
  209. struct xDNSMessage
  210. {
  211. uint16_t usIdentifier; /**< Query identifier. Used to match up replies to outstanding queries. */
  212. uint16_t usFlags; /**< Flags. */
  213. uint16_t usQuestions; /**< Number of questions asked in this query. */
  214. uint16_t usAnswers; /**< Number of answers being provided in this query. */
  215. uint16_t usAuthorityRRs; /**< Authoritative name server resource records. */
  216. uint16_t usAdditionalRRs; /**< Additional resource records.*/
  217. };
  218. #include "pack_struct_end.h"
  219. typedef struct xDNSMessage DNSMessage_t;
  220. /**
  221. * @brief Utility function to cast pointer of a type to pointer of type DNSMessage_t.
  222. *
  223. * @return The casted pointer.
  224. */
  225. static portINLINE ipDECL_CAST_PTR_FUNC_FOR_TYPE( DNSMessage_t )
  226. {
  227. return ( DNSMessage_t * ) pvArgument;
  228. }
  229. /**
  230. * @brief Utility function to cast a const pointer of a type to a const pointer of type DNSMessage_t.
  231. *
  232. * @return The casted pointer.
  233. */
  234. static portINLINE ipDECL_CAST_CONST_PTR_FUNC_FOR_TYPE( DNSMessage_t )
  235. {
  236. return ( const DNSMessage_t * ) pvArgument;
  237. }
  238. /* A DNS query consists of a header, as described in 'struct xDNSMessage'
  239. * It is followed by 1 or more queries, each one consisting of a name and a tail,
  240. * with two fields: type and class
  241. */
  242. #include "pack_struct_start.h"
  243. struct xDNSTail
  244. {
  245. uint16_t usType; /**< Type of DNS message. */
  246. uint16_t usClass; /**< Class of DNS message. */
  247. };
  248. #include "pack_struct_end.h"
  249. typedef struct xDNSTail DNSTail_t;
  250. /**
  251. * @brief Utility function to cast pointer of a type to pointer of type DNSTail_t.
  252. *
  253. * @return The casted pointer.
  254. */
  255. static portINLINE ipDECL_CAST_PTR_FUNC_FOR_TYPE( DNSTail_t )
  256. {
  257. return ( DNSTail_t * ) pvArgument;
  258. }
  259. /* DNS answer record header. */
  260. #include "pack_struct_start.h"
  261. struct xDNSAnswerRecord
  262. {
  263. uint16_t usType; /**< Type of DNS answer record. */
  264. uint16_t usClass; /**< Class of DNS answer record. */
  265. uint32_t ulTTL; /**< Number of seconds the result can be cached. */
  266. uint16_t usDataLength; /**< Length of the data field. */
  267. };
  268. #include "pack_struct_end.h"
  269. typedef struct xDNSAnswerRecord DNSAnswerRecord_t;
  270. /**
  271. * @brief Utility function to cast pointer of a type to pointer of type DNSAnswerRecord_t.
  272. *
  273. * @return The casted pointer.
  274. */
  275. static portINLINE ipDECL_CAST_PTR_FUNC_FOR_TYPE( DNSAnswerRecord_t )
  276. {
  277. return ( DNSAnswerRecord_t * ) pvArgument;
  278. }
  279. #if ( ipconfigUSE_LLMNR == 1 )
  280. #include "pack_struct_start.h"
  281. struct xLLMNRAnswer
  282. {
  283. uint8_t ucNameCode; /**< Name type. */
  284. uint8_t ucNameOffset; /**< The name is not repeated in the answer, only the offset is given with "0xc0 <offs>" */
  285. uint16_t usType; /**< Type of the Resource record. */
  286. uint16_t usClass; /**< Class of the Resource record. */
  287. uint32_t ulTTL; /**< Seconds till this entry can be cached. */
  288. uint16_t usDataLength; /**< Length of the address in this record. */
  289. uint32_t ulIPAddress; /**< The IP-address. */
  290. };
  291. #include "pack_struct_end.h"
  292. typedef struct xLLMNRAnswer LLMNRAnswer_t;
  293. /**
  294. * @brief Utility function to cast pointer of a type to pointer of type LLMNRAnswer_t.
  295. *
  296. * @return The casted pointer.
  297. */
  298. static portINLINE ipDECL_CAST_PTR_FUNC_FOR_TYPE( LLMNRAnswer_t )
  299. {
  300. return ( LLMNRAnswer_t * ) pvArgument;
  301. }
  302. #endif /* ipconfigUSE_LLMNR == 1 */
  303. #if ( ipconfigUSE_NBNS == 1 )
  304. #include "pack_struct_start.h"
  305. struct xNBNSRequest
  306. {
  307. uint16_t usRequestId; /**< NBNS request ID. */
  308. uint16_t usFlags; /**< Flags of the DNS message. */
  309. uint16_t ulRequestCount; /**< The number of requests/questions in this query. */
  310. uint16_t usAnswerRSS; /**< The number of answers in this query. */
  311. uint16_t usAuthRSS; /**< Number of authoritative resource records. */
  312. uint16_t usAdditionalRSS; /**< Number of additional resource records. */
  313. uint8_t ucNameSpace; /**< Length of name. */
  314. uint8_t ucName[ dnsNBNS_ENCODED_NAME_LENGTH ]; /**< The domain name. */
  315. uint8_t ucNameZero; /**< Terminator of the name. */
  316. uint16_t usType; /**< Type of NBNS record. */
  317. uint16_t usClass; /**< Class of NBNS request. */
  318. };
  319. #include "pack_struct_end.h"
  320. typedef struct xNBNSRequest NBNSRequest_t;
  321. #include "pack_struct_start.h"
  322. struct xNBNSAnswer
  323. {
  324. uint16_t usType; /**< Type of NBNS answer. */
  325. uint16_t usClass; /**< Class of NBNS answer. */
  326. uint32_t ulTTL; /**< Time in seconds for which the answer can be cached. */
  327. uint16_t usDataLength; /**< Data length. */
  328. uint16_t usNbFlags; /**< NetBIOS flags 0x6000 : IP-address, big-endian. */
  329. uint32_t ulIPAddress; /**< The IPv4 address. */
  330. };
  331. #include "pack_struct_end.h"
  332. typedef struct xNBNSAnswer NBNSAnswer_t;
  333. /**
  334. * @brief Utility function to cast pointer of a type to pointer of type NBNSAnswer_t.
  335. *
  336. * @return The casted pointer.
  337. */
  338. static portINLINE ipDECL_CAST_PTR_FUNC_FOR_TYPE( NBNSAnswer_t )
  339. {
  340. return ( NBNSAnswer_t * ) pvArgument;
  341. }
  342. #endif /* ipconfigUSE_NBNS == 1 */
  343. /*-----------------------------------------------------------*/
  344. #if ( ipconfigUSE_DNS_CACHE == 1 )
  345. uint32_t FreeRTOS_dnslookup( const char * pcHostName )
  346. {
  347. uint32_t ulIPAddress = 0UL;
  348. ( void ) prvProcessDNSCache( pcHostName, &ulIPAddress, 0, pdTRUE );
  349. return ulIPAddress;
  350. }
  351. #endif /* ipconfigUSE_DNS_CACHE == 1 */
  352. /*-----------------------------------------------------------*/
  353. #if ( ipconfigDNS_USE_CALLBACKS == 1 )
  354. /** @brief The structure to hold information for a DNS callback. */
  355. typedef struct xDNS_Callback
  356. {
  357. TickType_t uxRemaningTime; /**< Timeout in ms */
  358. FOnDNSEvent pCallbackFunction; /**< Function to be called when the address has been found or when a timeout has been reached */
  359. TimeOut_t uxTimeoutState; /**< Timeout state. */
  360. void * pvSearchID; /**< Search ID of the callback function. */
  361. struct xLIST_ITEM xListItem; /**< List struct. */
  362. char pcName[ 1 ]; /**< 1 character name. */
  363. } DNSCallback_t;
  364. /**
  365. * @brief Utility function to cast pointer of a type to pointer of type DNSCallback_t.
  366. *
  367. * @return The casted pointer.
  368. */
  369. static portINLINE ipDECL_CAST_PTR_FUNC_FOR_TYPE( DNSCallback_t )
  370. {
  371. return ( DNSCallback_t * ) pvArgument;
  372. }
  373. /** @brief The list of all callback structures. */
  374. static List_t xCallbackList;
  375. /**
  376. * @brief Define FreeRTOS_gethostbyname() as a normal blocking call.
  377. *
  378. * @param[in] pcHostName: The hostname whose IP address is being searched for.
  379. *
  380. * @return The IP-address of the hostname.
  381. */
  382. uint32_t FreeRTOS_gethostbyname( const char * pcHostName )
  383. {
  384. return FreeRTOS_gethostbyname_a( pcHostName, NULL, ( void * ) NULL, 0U );
  385. }
  386. /*-----------------------------------------------------------*/
  387. /** @brief Initialise the list of call-back structures.
  388. */
  389. void vDNSInitialise( void )
  390. {
  391. vListInitialise( &xCallbackList );
  392. }
  393. /*-----------------------------------------------------------*/
  394. /**
  395. * @brief Iterate through the list of call-back structures and remove
  396. * old entries which have reached a timeout.
  397. * As soon as the list has become empty, the DNS timer will be stopped.
  398. * In case pvSearchID is supplied, the user wants to cancel a DNS request.
  399. *
  400. * @param[in] pvSearchID: The search ID of callback function whose associated
  401. * DNS request is being cancelled. If non-ID specific checking of
  402. * all requests is required, then this field should be kept as NULL.
  403. */
  404. void vDNSCheckCallBack( void * pvSearchID )
  405. {
  406. const ListItem_t * pxIterator;
  407. const ListItem_t * xEnd = listGET_END_MARKER( &xCallbackList );
  408. vTaskSuspendAll();
  409. {
  410. for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
  411. pxIterator != xEnd;
  412. )
  413. {
  414. DNSCallback_t * pxCallback = ipCAST_PTR_TO_TYPE_PTR( DNSCallback_t, listGET_LIST_ITEM_OWNER( pxIterator ) );
  415. /* Move to the next item because we might remove this item */
  416. pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator );
  417. if( ( pvSearchID != NULL ) && ( pvSearchID == pxCallback->pvSearchID ) )
  418. {
  419. ( void ) uxListRemove( &( pxCallback->xListItem ) );
  420. vPortFree( pxCallback );
  421. }
  422. else if( xTaskCheckForTimeOut( &pxCallback->uxTimeoutState, &pxCallback->uxRemaningTime ) != pdFALSE )
  423. {
  424. pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0 );
  425. ( void ) uxListRemove( &( pxCallback->xListItem ) );
  426. vPortFree( pxCallback );
  427. }
  428. else
  429. {
  430. /* This call-back is still waiting for a reply or a time-out. */
  431. }
  432. }
  433. }
  434. ( void ) xTaskResumeAll();
  435. if( listLIST_IS_EMPTY( &xCallbackList ) != pdFALSE )
  436. {
  437. vIPSetDnsTimerEnableState( pdFALSE );
  438. }
  439. }
  440. /*-----------------------------------------------------------*/
  441. /**
  442. * @brief Remove the entry defined by the search ID to cancel a DNS request.
  443. *
  444. * @param[in] pvSearchID: The search ID of the callback function associated with
  445. * the DNS request being cancelled. Note that the value of
  446. * the pointer matters, not the pointee.
  447. */
  448. void FreeRTOS_gethostbyname_cancel( void * pvSearchID )
  449. {
  450. /* _HT_ Should better become a new API call to have the IP-task remove the callback */
  451. vDNSCheckCallBack( pvSearchID );
  452. }
  453. /*-----------------------------------------------------------*/
  454. /**
  455. * @brief FreeRTOS_gethostbyname_a() was called along with callback parameters.
  456. * Store them in a list for later reference.
  457. *
  458. * @param[in] pcHostName: The hostname whose IP address is being searched for.
  459. * @param[in] pvSearchID: The search ID of the DNS callback function to set.
  460. * @param[in] pCallbackFunction: The callback function pointer.
  461. * @param[in] uxTimeout: Timeout of the callback function.
  462. * @param[in] uxIdentifier: Random number used as ID in the DNS message.
  463. */
  464. static void vDNSSetCallBack( const char * pcHostName,
  465. void * pvSearchID,
  466. FOnDNSEvent pCallbackFunction,
  467. TickType_t uxTimeout,
  468. TickType_t uxIdentifier )
  469. {
  470. size_t lLength = strlen( pcHostName );
  471. DNSCallback_t * pxCallback = ipCAST_PTR_TO_TYPE_PTR( DNSCallback_t, pvPortMalloc( sizeof( *pxCallback ) + lLength ) );
  472. /* Translate from ms to number of clock ticks. */
  473. uxTimeout /= portTICK_PERIOD_MS;
  474. if( pxCallback != NULL )
  475. {
  476. if( listLIST_IS_EMPTY( &xCallbackList ) != pdFALSE )
  477. {
  478. /* This is the first one, start the DNS timer to check for timeouts */
  479. vIPReloadDNSTimer( FreeRTOS_min_uint32( 1000U, uxTimeout ) );
  480. }
  481. ( void ) strcpy( pxCallback->pcName, pcHostName );
  482. pxCallback->pCallbackFunction = pCallbackFunction;
  483. pxCallback->pvSearchID = pvSearchID;
  484. pxCallback->uxRemaningTime = uxTimeout;
  485. vTaskSetTimeOutState( &pxCallback->uxTimeoutState );
  486. listSET_LIST_ITEM_OWNER( &( pxCallback->xListItem ), ( void * ) pxCallback );
  487. listSET_LIST_ITEM_VALUE( &( pxCallback->xListItem ), uxIdentifier );
  488. vTaskSuspendAll();
  489. {
  490. vListInsertEnd( &xCallbackList, &pxCallback->xListItem );
  491. }
  492. ( void ) xTaskResumeAll();
  493. }
  494. }
  495. /*-----------------------------------------------------------*/
  496. /**
  497. * @brief A DNS reply was received, see if there is any matching entry and
  498. * call the handler.
  499. *
  500. * @param[in] uxIdentifier: Identifier associated with the callback function.
  501. * @param[in] pcName: The name associated with the callback function.
  502. * @param[in] ulIPAddress: IP-address obtained from the DNS server.
  503. *
  504. * @return Returns pdTRUE if uxIdentifier was recognized.
  505. */
  506. static BaseType_t xDNSDoCallback( TickType_t uxIdentifier,
  507. const char * pcName,
  508. uint32_t ulIPAddress )
  509. {
  510. BaseType_t xResult = pdFALSE;
  511. const ListItem_t * pxIterator;
  512. const ListItem_t * xEnd = listGET_END_MARKER( &xCallbackList );
  513. vTaskSuspendAll();
  514. {
  515. for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
  516. pxIterator != ( const ListItem_t * ) xEnd;
  517. pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
  518. {
  519. if( listGET_LIST_ITEM_VALUE( pxIterator ) == uxIdentifier )
  520. {
  521. DNSCallback_t * pxCallback = ipCAST_PTR_TO_TYPE_PTR( DNSCallback_t, listGET_LIST_ITEM_OWNER( pxIterator ) );
  522. pxCallback->pCallbackFunction( pcName, pxCallback->pvSearchID, ulIPAddress );
  523. ( void ) uxListRemove( &pxCallback->xListItem );
  524. vPortFree( pxCallback );
  525. if( listLIST_IS_EMPTY( &xCallbackList ) != pdFALSE )
  526. {
  527. /* The list of outstanding requests is empty. No need for periodic polling. */
  528. vIPSetDnsTimerEnableState( pdFALSE );
  529. }
  530. xResult = pdTRUE;
  531. break;
  532. }
  533. }
  534. }
  535. ( void ) xTaskResumeAll();
  536. return xResult;
  537. }
  538. #endif /* ipconfigDNS_USE_CALLBACKS == 1 */
  539. /*-----------------------------------------------------------*/
  540. #if ( ipconfigDNS_USE_CALLBACKS == 0 )
  541. /**
  542. * @brief Get the IP-address corresponding to the given hostname.
  543. *
  544. * @param[in] pcHostName: The hostname whose IP address is being queried.
  545. *
  546. * @return The IP-address corresponding to the hostname.
  547. */
  548. uint32_t FreeRTOS_gethostbyname( const char * pcHostName )
  549. {
  550. return prvPrepareLookup( pcHostName );
  551. }
  552. #else
  553. /**
  554. * @brief Get the IP-address corresponding to the given hostname.
  555. *
  556. * @param[in] pcHostName: The hostname whose IP address is being queried.
  557. * @param[in] pCallback: The callback function which will be called upon DNS response.
  558. * @param[in] pvSearchID: Search ID for the callback function.
  559. * @param[in] uxTimeout: Timeout for the callback function.
  560. *
  561. * @return The IP-address corresponding to the hostname.
  562. */
  563. uint32_t FreeRTOS_gethostbyname_a( const char * pcHostName,
  564. FOnDNSEvent pCallback,
  565. void * pvSearchID,
  566. TickType_t uxTimeout )
  567. {
  568. return prvPrepareLookup( pcHostName, pCallback, pvSearchID, uxTimeout );
  569. }
  570. #endif /* if ( ipconfigDNS_USE_CALLBACKS == 0 ) */
  571. #if ( ipconfigDNS_USE_CALLBACKS == 1 )
  572. /**
  573. * @brief Check if hostname is already known. If not, call prvGetHostByName() to send a DNS request.
  574. *
  575. * @param[in] pcHostName: The hostname whose IP address is being queried.
  576. * @param[in] pCallback: The callback function which will be called upon DNS response.
  577. * @param[in] pvSearchID: Search ID for the callback function.
  578. * @param[in] uxTimeout: Timeout for the callback function.
  579. *
  580. * @return The IP-address corresponding to the hostname.
  581. */
  582. static uint32_t prvPrepareLookup( const char * pcHostName,
  583. FOnDNSEvent pCallback,
  584. void * pvSearchID,
  585. TickType_t uxTimeout )
  586. #else
  587. /**
  588. * @brief Check if hostname is already known. If not, call prvGetHostByName() to send a DNS request.
  589. *
  590. * @param[in] pcHostName: The hostname whose IP address is being queried.
  591. *
  592. * @return The IP-address corresponding to the hostname.
  593. */
  594. static uint32_t prvPrepareLookup( const char * pcHostName )
  595. #endif
  596. {
  597. uint32_t ulIPAddress = 0UL;
  598. TickType_t uxReadTimeOut_ticks = ipconfigDNS_RECEIVE_BLOCK_TIME_TICKS;
  599. /* Generate a unique identifier for this query. Keep it in a local variable
  600. * as gethostbyname() may be called from different threads */
  601. BaseType_t xHasRandom = pdFALSE;
  602. TickType_t uxIdentifier = 0U;
  603. #if ( ipconfigUSE_DNS_CACHE != 0 )
  604. BaseType_t xLengthOk = pdFALSE;
  605. #endif
  606. #if ( ipconfigUSE_DNS_CACHE != 0 )
  607. {
  608. if( pcHostName != NULL )
  609. {
  610. size_t xLength = strlen( pcHostName ) + 1U;
  611. if( xLength <= ipconfigDNS_CACHE_NAME_LENGTH )
  612. {
  613. /* The name is not too long. */
  614. xLengthOk = pdTRUE;
  615. }
  616. else
  617. {
  618. FreeRTOS_printf( ( "prvPrepareLookup: name is too long ( %lu > %lu )\n",
  619. ( uint32_t ) xLength,
  620. ( uint32_t ) ipconfigDNS_CACHE_NAME_LENGTH ) );
  621. }
  622. }
  623. }
  624. if( ( pcHostName != NULL ) && ( xLengthOk != pdFALSE ) )
  625. #else /* if ( ipconfigUSE_DNS_CACHE != 0 ) */
  626. if( pcHostName != NULL )
  627. #endif /* ( ipconfigUSE_DNS_CACHE != 0 ) */
  628. {
  629. /* If the supplied hostname is IP address, convert it to uint32_t
  630. * and return. */
  631. #if ( ipconfigINCLUDE_FULL_INET_ADDR == 1 )
  632. {
  633. ulIPAddress = FreeRTOS_inet_addr( pcHostName );
  634. }
  635. #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */
  636. /* If a DNS cache is used then check the cache before issuing another DNS
  637. * request. */
  638. #if ( ipconfigUSE_DNS_CACHE == 1 )
  639. {
  640. if( ulIPAddress == 0UL )
  641. {
  642. ulIPAddress = FreeRTOS_dnslookup( pcHostName );
  643. if( ulIPAddress != 0UL )
  644. {
  645. FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );
  646. }
  647. else
  648. {
  649. /* prvGetHostByName will be called to start a DNS lookup. */
  650. }
  651. }
  652. }
  653. #endif /* ipconfigUSE_DNS_CACHE == 1 */
  654. /* Generate a unique identifier. */
  655. if( ulIPAddress == 0UL )
  656. {
  657. uint32_t ulNumber;
  658. xHasRandom = xApplicationGetRandomNumber( &( ulNumber ) );
  659. /* DNS identifiers are 16-bit. */
  660. uxIdentifier = ( TickType_t ) ( ulNumber & 0xffffU );
  661. }
  662. #if ( ipconfigDNS_USE_CALLBACKS == 1 )
  663. {
  664. if( pCallback != NULL )
  665. {
  666. if( ulIPAddress == 0UL )
  667. {
  668. /* The user has provided a callback function, so do not block on recvfrom() */
  669. if( xHasRandom != pdFALSE )
  670. {
  671. uxReadTimeOut_ticks = 0U;
  672. vDNSSetCallBack( pcHostName, pvSearchID, pCallback, uxTimeout, uxIdentifier );
  673. }
  674. }
  675. else
  676. {
  677. /* The IP address is known, do the call-back now. */
  678. pCallback( pcHostName, pvSearchID, ulIPAddress );
  679. }
  680. }
  681. }
  682. #endif /* if ( ipconfigDNS_USE_CALLBACKS == 1 ) */
  683. if( ( ulIPAddress == 0UL ) && ( xHasRandom != pdFALSE ) )
  684. {
  685. ulIPAddress = prvGetHostByName( pcHostName, uxIdentifier, uxReadTimeOut_ticks );
  686. }
  687. }
  688. return ulIPAddress;
  689. }
  690. /*-----------------------------------------------------------*/
  691. /**
  692. * @brief Prepare and send a message to a DNS server. 'uxReadTimeOut_ticks' will be passed as
  693. * zero, in case the user has supplied a call-back function.
  694. *
  695. * @param[in] pcHostName: The hostname for which an IP address is required.
  696. * @param[in] uxIdentifier: Identifier to send in the DNS message.
  697. * @param[in] uxReadTimeOut_ticks: The timeout in ticks for waiting. In case the user has supplied
  698. * a call-back function, this value should be zero.
  699. *
  700. * @return The IPv4 IP address for the hostname being queried. It will be zero if there is no reply.
  701. */
  702. static uint32_t prvGetHostByName( const char * pcHostName,
  703. TickType_t uxIdentifier,
  704. TickType_t uxReadTimeOut_ticks )
  705. {
  706. struct freertos_sockaddr xAddress;
  707. Socket_t xDNSSocket;
  708. uint32_t ulIPAddress = 0UL;
  709. uint32_t ulAddressLength = sizeof( struct freertos_sockaddr );
  710. BaseType_t xAttempt;
  711. int32_t lBytes;
  712. size_t uxPayloadLength, uxExpectedPayloadLength;
  713. TickType_t uxWriteTimeOut_ticks = ipconfigDNS_SEND_BLOCK_TIME_TICKS;
  714. #if ( ipconfigUSE_LLMNR == 1 )
  715. BaseType_t bHasDot = pdFALSE;
  716. #endif /* ipconfigUSE_LLMNR == 1 */
  717. /* If LLMNR is being used then determine if the host name includes a '.' -
  718. * if not then LLMNR can be used as the lookup method. */
  719. #if ( ipconfigUSE_LLMNR == 1 )
  720. {
  721. const char * pucPtr;
  722. for( pucPtr = pcHostName; *pucPtr != ( char ) 0; pucPtr++ )
  723. {
  724. if( *pucPtr == '.' )
  725. {
  726. bHasDot = pdTRUE;
  727. break;
  728. }
  729. }
  730. }
  731. #endif /* ipconfigUSE_LLMNR == 1 */
  732. /* Two is added at the end for the count of characters in the first
  733. * subdomain part and the string end byte. */
  734. uxExpectedPayloadLength = sizeof( DNSMessage_t ) + strlen( pcHostName ) + sizeof( uint16_t ) + sizeof( uint16_t ) + 2U;
  735. xDNSSocket = prvCreateDNSSocket();
  736. if( xDNSSocket != NULL )
  737. {
  738. /* Ideally we should check for the return value. But since we are passing
  739. * correct parameters, and xDNSSocket is != NULL, the return value is
  740. * going to be '0' i.e. success. Thus, return value is discarded */
  741. ( void ) FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_SNDTIMEO, &( uxWriteTimeOut_ticks ), sizeof( TickType_t ) );
  742. ( void ) FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_RCVTIMEO, &( uxReadTimeOut_ticks ), sizeof( TickType_t ) );
  743. for( xAttempt = 0; xAttempt < ipconfigDNS_REQUEST_ATTEMPTS; xAttempt++ )
  744. {
  745. size_t uxHeaderBytes;
  746. NetworkBufferDescriptor_t * pxNetworkBuffer;
  747. uint8_t * pucUDPPayloadBuffer = NULL, * pucReceiveBuffer;
  748. /* Get a buffer. This uses a maximum delay, but the delay will be
  749. * capped to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value
  750. * still needs to be tested. */
  751. uxHeaderBytes = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER;
  752. pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( uxHeaderBytes + uxExpectedPayloadLength, 0UL );
  753. if( pxNetworkBuffer != NULL )
  754. {
  755. pucUDPPayloadBuffer = &( pxNetworkBuffer->pucEthernetBuffer[ uxHeaderBytes ] );
  756. }
  757. if( pucUDPPayloadBuffer != NULL )
  758. {
  759. /* Create the message in the obtained buffer. */
  760. uxPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, uxIdentifier );
  761. iptraceSENDING_DNS_REQUEST();
  762. /* Obtain the DNS server address. */
  763. FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulIPAddress );
  764. /* Send the DNS message. */
  765. #if ( ipconfigUSE_LLMNR == 1 )
  766. if( bHasDot == pdFALSE )
  767. {
  768. /* Use LLMNR addressing. */
  769. ( ipCAST_PTR_TO_TYPE_PTR( DNSMessage_t, pucUDPPayloadBuffer ) )->usFlags = 0;
  770. xAddress.sin_addr = ipLLMNR_IP_ADDR; /* Is in network byte order. */
  771. xAddress.sin_port = ipLLMNR_PORT;
  772. xAddress.sin_port = FreeRTOS_ntohs( xAddress.sin_port );
  773. }
  774. else
  775. #endif
  776. {
  777. /* Use DNS server. */
  778. xAddress.sin_addr = ulIPAddress;
  779. xAddress.sin_port = dnsDNS_PORT;
  780. }
  781. ulIPAddress = 0UL;
  782. if( FreeRTOS_sendto( xDNSSocket, pucUDPPayloadBuffer, uxPayloadLength, FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) != 0 )
  783. {
  784. /* Wait for the reply. */
  785. lBytes = FreeRTOS_recvfrom( xDNSSocket, &pucReceiveBuffer, 0, FREERTOS_ZERO_COPY, &xAddress, &ulAddressLength );
  786. if( lBytes > 0 )
  787. {
  788. BaseType_t xExpected;
  789. const DNSMessage_t * pxDNSMessageHeader = ipCAST_CONST_PTR_TO_CONST_TYPE_PTR( DNSMessage_t, pucReceiveBuffer );
  790. /* See if the identifiers match. */
  791. if( uxIdentifier == ( TickType_t ) pxDNSMessageHeader->usIdentifier )
  792. {
  793. xExpected = pdTRUE;
  794. }
  795. else
  796. {
  797. /* The reply was not expected. */
  798. xExpected = pdFALSE;
  799. }
  800. /* The reply was received. Process it. */
  801. #if ( ipconfigDNS_USE_CALLBACKS == 0 )
  802. /* It is useless to analyse the unexpected reply
  803. * unless asynchronous look-ups are enabled. */
  804. if( xExpected != pdFALSE )
  805. #endif /* ipconfigDNS_USE_CALLBACKS == 0 */
  806. {
  807. ulIPAddress = prvParseDNSReply( pucReceiveBuffer, ( size_t ) lBytes, xExpected );
  808. }
  809. /* Finished with the buffer. The zero copy interface
  810. * is being used, so the buffer must be freed by the
  811. * task. */
  812. FreeRTOS_ReleaseUDPPayloadBuffer( pucReceiveBuffer );
  813. if( ulIPAddress != 0UL )
  814. {
  815. /* All done. */
  816. /* coverity[break_stmt] : Break statement terminating the loop */
  817. break;
  818. }
  819. }
  820. }
  821. else
  822. {
  823. /* The message was not sent so the stack will not be
  824. * releasing the zero copy - it must be released here. */
  825. vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
  826. }
  827. }
  828. if( uxReadTimeOut_ticks == 0U )
  829. {
  830. /* This DNS lookup is asynchronous, using a call-back:
  831. * send the request only once. */
  832. break;
  833. }
  834. }
  835. /* Finished with the socket. */
  836. ( void ) FreeRTOS_closesocket( xDNSSocket );
  837. }
  838. return ulIPAddress;
  839. }
  840. /*-----------------------------------------------------------*/
  841. /**
  842. * @brief Create the DNS message in the zero copy buffer passed in the first parameter.
  843. *
  844. * @param[in,out] pucUDPPayloadBuffer: The zero copy buffer where the DNS message will be created.
  845. * @param[in] pcHostName: Hostname to be looked up.
  846. * @param[in] uxIdentifier: The identifier to be added to the DNS message.
  847. *
  848. * @return Total size of the generated message, which is the space from the last written byte
  849. * to the beginning of the buffer.
  850. */
  851. _static size_t prvCreateDNSMessage( uint8_t * pucUDPPayloadBuffer,
  852. const char * pcHostName,
  853. TickType_t uxIdentifier )
  854. {
  855. DNSMessage_t * pxDNSMessageHeader;
  856. size_t uxStart, uxIndex;
  857. DNSTail_t const * pxTail;
  858. static const DNSMessage_t xDefaultPartDNSHeader =
  859. {
  860. 0, /* The identifier will be overwritten. */
  861. dnsOUTGOING_FLAGS, /* Flags set for standard query. */
  862. dnsONE_QUESTION, /* One question is being asked. */
  863. 0, /* No replies are included. */
  864. 0, /* No authorities. */
  865. 0 /* No additional authorities. */
  866. };
  867. /* memcpy() helper variables for MISRA Rule 21.15 compliance*/
  868. const void * pvCopySource;
  869. void * pvCopyDest;
  870. /* Copy in the const part of the header. Intentionally using different
  871. * pointers with memcpy() to put the information in to correct place. */
  872. /*
  873. * Use helper variables for memcpy() to remain
  874. * compliant with MISRA Rule 21.15. These should be
  875. * optimized away.
  876. */
  877. pvCopySource = &xDefaultPartDNSHeader;
  878. pvCopyDest = pucUDPPayloadBuffer;
  879. ( void ) memcpy( pvCopyDest, pvCopySource, sizeof( xDefaultPartDNSHeader ) );
  880. /* Write in a unique identifier. Cast the Payload Buffer to DNSMessage_t
  881. * to easily access fields of the DNS Message. */
  882. pxDNSMessageHeader = ipCAST_PTR_TO_TYPE_PTR( DNSMessage_t, pucUDPPayloadBuffer );
  883. pxDNSMessageHeader->usIdentifier = ( uint16_t ) uxIdentifier;
  884. /* Create the resource record at the end of the header. First
  885. * find the end of the header. */
  886. uxStart = sizeof( xDefaultPartDNSHeader );
  887. /* Leave a gap for the first length byte. */
  888. uxIndex = uxStart + 1U;
  889. /* Copy in the host name. */
  890. ( void ) strcpy( ( char * ) &( pucUDPPayloadBuffer[ uxIndex ] ), pcHostName );
  891. /* Walk through the string to replace the '.' characters with byte
  892. * counts. pucStart holds the address of the byte count. Walking the
  893. * string starts after the byte count position. */
  894. uxIndex = uxStart;
  895. do
  896. {
  897. size_t uxLength;
  898. /* Skip the length byte. */
  899. uxIndex++;
  900. while( ( pucUDPPayloadBuffer[ uxIndex ] != ( uint8_t ) 0U ) &&
  901. ( pucUDPPayloadBuffer[ uxIndex ] != ( uint8_t ) ASCII_BASELINE_DOT ) )
  902. {
  903. uxIndex++;
  904. }
  905. /* Fill in the byte count, then move the pucStart pointer up to
  906. * the found byte position. */
  907. uxLength = uxIndex - ( uxStart + 1U );
  908. pucUDPPayloadBuffer[ uxStart ] = ( uint8_t ) uxLength;
  909. uxStart = uxIndex;
  910. } while( pucUDPPayloadBuffer[ uxIndex ] != ( uint8_t ) 0U );
  911. /* Finish off the record. Cast the record onto DNSTail_t structure to easily
  912. * access the fields of the DNS Message. */
  913. pxTail = ipCAST_PTR_TO_TYPE_PTR( DNSTail_t, &( pucUDPPayloadBuffer[ uxStart + 1U ] ) );
  914. #if defined( _lint ) || defined( __COVERITY__ )
  915. ( void ) pxTail;
  916. #else
  917. vSetField16( pxTail, DNSTail_t, usType, dnsTYPE_A_HOST );
  918. vSetField16( pxTail, DNSTail_t, usClass, dnsCLASS_IN );
  919. #endif
  920. /* Return the total size of the generated message, which is the space from
  921. * the last written byte to the beginning of the buffer. */
  922. return uxIndex + sizeof( DNSTail_t ) + 1U;
  923. }
  924. /*-----------------------------------------------------------*/
  925. #if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
  926. /**
  927. * @brief Read the Name field out of a DNS response packet.
  928. *
  929. * @param[in] pucByte: Pointer to the DNS response.
  930. * @param[in] uxRemainingBytes: Length of the DNS response.
  931. * @param[out] pcName: The pointer in which the name in the DNS response will be returned.
  932. * @param[in] uxDestLen: Size of the pcName array.
  933. *
  934. * @return If a fully formed name was found, then return the number of bytes processed in pucByte.
  935. */
  936. _static size_t prvReadNameField( const uint8_t * pucByte,
  937. size_t uxRemainingBytes,
  938. char * pcName,
  939. size_t uxDestLen )
  940. {
  941. size_t uxNameLen = 0U;
  942. size_t uxIndex = 0U;
  943. size_t uxSourceLen = uxRemainingBytes;
  944. /* uxCount gets the values from pucByte and counts down to 0.
  945. * No need to have a different type than that of pucByte */
  946. size_t uxCount;
  947. if( uxSourceLen == ( size_t ) 0U )
  948. {
  949. /* Return 0 value in case of error. */
  950. uxIndex = 0U;
  951. }
  952. /* Determine if the name is the fully coded name, or an offset to the name
  953. * elsewhere in the message. */
  954. else if( ( pucByte[ uxIndex ] & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
  955. {
  956. /* Jump over the two byte offset. */
  957. if( uxSourceLen > sizeof( uint16_t ) )
  958. {
  959. uxIndex += sizeof( uint16_t );
  960. }
  961. else
  962. {
  963. uxIndex = 0U;
  964. }
  965. }
  966. else
  967. {
  968. /* 'uxIndex' points to the full name. Walk over the string. */
  969. while( ( uxIndex < uxSourceLen ) && ( pucByte[ uxIndex ] != ( uint8_t ) 0x00U ) )
  970. {
  971. /* If this is not the first time through the loop, then add a
  972. * separator in the output. */
  973. if( ( uxNameLen > 0U ) )
  974. {
  975. if( uxNameLen >= uxDestLen )
  976. {
  977. uxIndex = 0U;
  978. /* coverity[break_stmt] : Break statement terminating the loop */
  979. break;
  980. }
  981. pcName[ uxNameLen ] = '.';
  982. uxNameLen++;
  983. }
  984. /* Process the first/next sub-string. */
  985. uxCount = ( size_t ) pucByte[ uxIndex ];
  986. uxIndex++;
  987. if( ( uxIndex + uxCount ) > uxSourceLen )
  988. {
  989. uxIndex = 0U;
  990. break;
  991. }
  992. while( ( uxCount-- != 0U ) && ( uxIndex < uxSourceLen ) )
  993. {
  994. if( uxNameLen >= uxDestLen )
  995. {
  996. uxIndex = 0U;
  997. break;
  998. /* break out of inner loop here
  999. * break out of outer loop at the test uxNameLen >= uxDestLen. */
  1000. }
  1001. pcName[ uxNameLen ] = ( char ) pucByte[ uxIndex ];
  1002. uxNameLen++;
  1003. uxIndex++;
  1004. }
  1005. }
  1006. /* Confirm that a fully formed name was found. */
  1007. if( uxIndex > 0U )
  1008. {
  1009. if( ( uxNameLen < uxDestLen ) && ( uxIndex < uxSourceLen ) && ( pucByte[ uxIndex ] == 0U ) )
  1010. {
  1011. pcName[ uxNameLen ] = '\0';
  1012. uxIndex++;
  1013. }
  1014. else
  1015. {
  1016. uxIndex = 0U;
  1017. }
  1018. }
  1019. }
  1020. return uxIndex;
  1021. }
  1022. #endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
  1023. /*-----------------------------------------------------------*/
  1024. /**
  1025. * @brief Simple routine that jumps over the NAME field of a resource record.
  1026. *
  1027. * @param[in] pucByte: The pointer to the resource record.
  1028. * @param[in] uxLength: Length of the resource record.
  1029. *
  1030. * @return It returns the number of bytes read, or zero when an error has occurred.
  1031. */
  1032. _static size_t prvSkipNameField( const uint8_t * pucByte,
  1033. size_t uxLength )
  1034. {
  1035. size_t uxChunkLength;
  1036. size_t uxSourceLenCpy = uxLength;
  1037. size_t uxIndex = 0U;
  1038. if( uxSourceLenCpy == 0U )
  1039. {
  1040. uxIndex = 0U;
  1041. }
  1042. /* Determine if the name is the fully coded name, or an offset to the name
  1043. * elsewhere in the message. */
  1044. else if( ( pucByte[ uxIndex ] & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
  1045. {
  1046. /* Jump over the two byte offset. */
  1047. if( uxSourceLenCpy > sizeof( uint16_t ) )
  1048. {
  1049. uxIndex += sizeof( uint16_t );
  1050. }
  1051. else
  1052. {
  1053. uxIndex = 0U;
  1054. }
  1055. }
  1056. else
  1057. {
  1058. /* pucByte points to the full name. Walk over the string. */
  1059. while( ( pucByte[ uxIndex ] != 0U ) && ( uxSourceLenCpy > 1U ) )
  1060. {
  1061. /* Conversion to size_t causes addition to be done
  1062. * in size_t */
  1063. uxChunkLength = ( ( size_t ) pucByte[ uxIndex ] ) + 1U;
  1064. if( uxSourceLenCpy > uxChunkLength )
  1065. {
  1066. uxSourceLenCpy -= uxChunkLength;
  1067. uxIndex += uxChunkLength;
  1068. }
  1069. else
  1070. {
  1071. uxIndex = 0U;
  1072. break;
  1073. }
  1074. }
  1075. /* Confirm that a fully formed name was found. */
  1076. if( uxIndex > 0U )
  1077. {
  1078. if( pucByte[ uxIndex ] == 0U )
  1079. {
  1080. uxIndex++;
  1081. }
  1082. else
  1083. {
  1084. uxIndex = 0U;
  1085. }
  1086. }
  1087. }
  1088. return uxIndex;
  1089. }
  1090. /*-----------------------------------------------------------*/
  1091. /* The function below will only be called :
  1092. * when ipconfigDNS_USE_CALLBACKS == 1
  1093. * when ipconfigUSE_LLMNR == 1
  1094. * for testing purposes, by the module test_freertos_tcp.c
  1095. */
  1096. /**
  1097. * @brief Perform some preliminary checks and then parse the DNS packet.
  1098. *
  1099. * @param[in] pxNetworkBuffer: The network buffer to be parsed.
  1100. *
  1101. * @return Always pdFAIL to indicate that the packet was not consumed and must
  1102. * be released by the caller.
  1103. */
  1104. uint32_t ulDNSHandlePacket( const NetworkBufferDescriptor_t * pxNetworkBuffer )
  1105. {
  1106. uint8_t * pucPayLoadBuffer;
  1107. size_t uxPayloadSize;
  1108. /* Only proceed if the payload length indicated in the header
  1109. * appears to be valid. */
  1110. if( pxNetworkBuffer->xDataLength >= sizeof( UDPPacket_t ) )
  1111. {
  1112. uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );
  1113. if( uxPayloadSize >= sizeof( DNSMessage_t ) )
  1114. {
  1115. pucPayLoadBuffer = &( pxNetworkBuffer->pucEthernetBuffer[ sizeof( UDPPacket_t ) ] );
  1116. /* The parameter pdFALSE indicates that the reply was not expected. */
  1117. ( void ) prvParseDNSReply( pucPayLoadBuffer,
  1118. uxPayloadSize,
  1119. pdFALSE );
  1120. }
  1121. }
  1122. /* The packet was not consumed. */
  1123. return pdFAIL;
  1124. }
  1125. /*-----------------------------------------------------------*/
  1126. #if ( ipconfigUSE_NBNS == 1 )
  1127. /**
  1128. * @brief Handle an NBNS packet.
  1129. *
  1130. * @param[in] pxNetworkBuffer: The network buffer holding the NBNS packet.
  1131. *
  1132. * @return pdFAIL to show that the packet was not consumed.
  1133. */
  1134. uint32_t ulNBNSHandlePacket( NetworkBufferDescriptor_t * pxNetworkBuffer )
  1135. {
  1136. UDPPacket_t * pxUDPPacket = ipCAST_PTR_TO_TYPE_PTR( UDPPacket_t, pxNetworkBuffer->pucEthernetBuffer );
  1137. uint8_t * pucUDPPayloadBuffer = &( pxNetworkBuffer->pucEthernetBuffer[ sizeof( *pxUDPPacket ) ] );
  1138. prvTreatNBNS( pucUDPPayloadBuffer,
  1139. pxNetworkBuffer->xDataLength,
  1140. pxUDPPacket->xIPHeader.ulSourceIPAddress );
  1141. /* The packet was not consumed. */
  1142. return pdFAIL;
  1143. }
  1144. #endif /* ipconfigUSE_NBNS */
  1145. /*-----------------------------------------------------------*/
  1146. /**
  1147. * @brief Process a response packet from a DNS server, or an LLMNR reply.
  1148. *
  1149. * @param[in] pucUDPPayloadBuffer: The DNS response received as a UDP
  1150. * payload.
  1151. * @param[in] uxBufferLength: Length of the UDP payload buffer.
  1152. * @param[in] xExpected: indicates whether the identifier in the reply
  1153. * was expected, and thus if the DNS cache may be
  1154. * updated with the reply.
  1155. *
  1156. * @return The IP address in the DNS response if present and if xExpected is set to pdTRUE.
  1157. * An error code (dnsPARSE_ERROR) if there was an error in the DNS response.
  1158. * 0 if xExpected set to pdFALSE.
  1159. */
  1160. _static uint32_t prvParseDNSReply( uint8_t * pucUDPPayloadBuffer,
  1161. size_t uxBufferLength,
  1162. BaseType_t xExpected )
  1163. {
  1164. DNSMessage_t * pxDNSMessageHeader;
  1165. /* This pointer is not used to modify anything */
  1166. const DNSAnswerRecord_t * pxDNSAnswerRecord;
  1167. uint32_t ulIPAddress = 0UL;
  1168. #if ( ipconfigUSE_LLMNR == 1 )
  1169. char * pcRequestedName = NULL;
  1170. #endif
  1171. uint8_t * pucByte;
  1172. size_t uxSourceBytesRemaining;
  1173. uint16_t x, usDataLength, usQuestions;
  1174. uint16_t usType = 0U;
  1175. BaseType_t xReturn = pdTRUE;
  1176. /* memcpy() helper variables for MISRA Rule 21.15 compliance*/
  1177. const void * pvCopySource;
  1178. void * pvCopyDest;
  1179. #if ( ipconfigUSE_LLMNR == 1 )
  1180. uint16_t usClass = 0U;
  1181. #endif
  1182. #if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
  1183. BaseType_t xDoStore = xExpected;
  1184. char pcName[ ipconfigDNS_CACHE_NAME_LENGTH ] = "";
  1185. #endif
  1186. const size_t uxAddressLength = ipSIZE_OF_IPv4_ADDRESS;
  1187. /* Ensure that the buffer is of at least minimal DNS message length. */
  1188. if( uxBufferLength < sizeof( DNSMessage_t ) )
  1189. {
  1190. xReturn = pdFALSE;
  1191. }
  1192. else
  1193. {
  1194. uxSourceBytesRemaining = uxBufferLength;
  1195. /* Parse the DNS message header. Map the byte stream onto a structure
  1196. * for easier access. */
  1197. pxDNSMessageHeader = ipCAST_PTR_TO_TYPE_PTR( DNSMessage_t, pucUDPPayloadBuffer );
  1198. /* Introduce a do {} while (0) to allow the use of breaks. */
  1199. do
  1200. {
  1201. size_t uxBytesRead = 0U;
  1202. size_t uxResult;
  1203. /* Start at the first byte after the header. */
  1204. pucByte = &( pucUDPPayloadBuffer[ sizeof( DNSMessage_t ) ] );
  1205. uxSourceBytesRemaining -= sizeof( DNSMessage_t );
  1206. /* Skip any question records. */
  1207. usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions );
  1208. for( x = 0U; x < usQuestions; x++ )
  1209. {
  1210. #if ( ipconfigUSE_LLMNR == 1 )
  1211. {
  1212. if( x == 0U )
  1213. {
  1214. pcRequestedName = ( char * ) pucByte;
  1215. }
  1216. }
  1217. #endif
  1218. #if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
  1219. if( x == 0U )
  1220. {
  1221. uxResult = prvReadNameField( pucByte,
  1222. uxSourceBytesRemaining,
  1223. pcName,
  1224. sizeof( pcName ) );
  1225. /* Check for a malformed response. */
  1226. if( uxResult == 0U )
  1227. {
  1228. xReturn = pdFALSE;
  1229. break;
  1230. }
  1231. uxBytesRead += uxResult;
  1232. pucByte = &( pucByte[ uxResult ] );
  1233. uxSourceBytesRemaining -= uxResult;
  1234. }
  1235. else
  1236. #endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
  1237. {
  1238. /* Skip the variable length pcName field. */
  1239. uxResult = prvSkipNameField( pucByte,
  1240. uxSourceBytesRemaining );
  1241. /* Check for a malformed response. */
  1242. if( uxResult == 0U )
  1243. {
  1244. xReturn = pdFALSE;
  1245. break;
  1246. }
  1247. uxBytesRead += uxResult;
  1248. pucByte = &( pucByte[ uxResult ] );
  1249. uxSourceBytesRemaining -= uxResult;
  1250. }
  1251. /* Check the remaining buffer size. */
  1252. if( uxSourceBytesRemaining >= sizeof( uint32_t ) )
  1253. {
  1254. #if ( ipconfigUSE_LLMNR == 1 )
  1255. {
  1256. /* usChar2u16 returns value in host endianness. */
  1257. usType = usChar2u16( pucByte );
  1258. usClass = usChar2u16( &( pucByte[ 2 ] ) );
  1259. }
  1260. #endif /* ipconfigUSE_LLMNR */
  1261. /* Skip the type and class fields. */
  1262. pucByte = &( pucByte[ sizeof( uint32_t ) ] );
  1263. uxSourceBytesRemaining -= sizeof( uint32_t );
  1264. }
  1265. else
  1266. {
  1267. xReturn = pdFALSE;
  1268. break;
  1269. }
  1270. }
  1271. if( xReturn == pdFALSE )
  1272. {
  1273. /* No need to proceed. Break out of the do-while loop. */
  1274. break;
  1275. }
  1276. /* Search through the answer records. */
  1277. pxDNSMessageHeader->usAnswers = FreeRTOS_ntohs( pxDNSMessageHeader->usAnswers );
  1278. if( ( pxDNSMessageHeader->usFlags & dnsRX_FLAGS_MASK ) == dnsEXPECTED_RX_FLAGS )
  1279. {
  1280. const uint16_t usCount = ( uint16_t ) ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY;
  1281. uint16_t usNumARecordsStored = 0;
  1282. for( x = 0U; x < pxDNSMessageHeader->usAnswers; x++ )
  1283. {
  1284. BaseType_t xDoAccept;
  1285. if( usNumARecordsStored >= usCount )
  1286. {
  1287. /* Only count ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY number of records. */
  1288. break;
  1289. }
  1290. uxResult = prvSkipNameField( pucByte,
  1291. uxSourceBytesRemaining );
  1292. /* Check for a malformed response. */
  1293. if( uxResult == 0U )
  1294. {
  1295. xReturn = pdFALSE;
  1296. break;
  1297. }
  1298. uxBytesRead += uxResult;
  1299. pucByte = &( pucByte[ uxResult ] );
  1300. uxSourceBytesRemaining -= uxResult;
  1301. /* Is there enough data for an IPv4 A record answer and, if so,
  1302. * is this an A record? */
  1303. if( uxSourceBytesRemaining < sizeof( uint16_t ) )
  1304. {
  1305. xReturn = pdFALSE;
  1306. break;
  1307. }
  1308. usType = usChar2u16( pucByte );
  1309. if( usType == ( uint16_t ) dnsTYPE_A_HOST )
  1310. {
  1311. if( uxSourceBytesRemaining >= ( sizeof( DNSAnswerRecord_t ) + uxAddressLength ) )
  1312. {
  1313. xDoAccept = pdTRUE;
  1314. }
  1315. else
  1316. {
  1317. xDoAccept = pdFALSE;
  1318. }
  1319. }
  1320. else
  1321. {
  1322. /* Unknown host type. */
  1323. xDoAccept = pdFALSE;
  1324. }
  1325. if( xDoAccept != pdFALSE )
  1326. {
  1327. /* This is the required record type and is of sufficient size. */
  1328. /* Mapping pucByte to a DNSAnswerRecord allows easy access of the
  1329. * fields of the structure. */
  1330. pxDNSAnswerRecord = ipCAST_PTR_TO_TYPE_PTR( DNSAnswerRecord_t, pucByte );
  1331. /* Sanity check the data length of an IPv4 answer. */
  1332. if( FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength ) == ( uint16_t ) uxAddressLength )
  1333. {
  1334. /* Copy the IP address out of the record. Using different pointers
  1335. * to copy only the portion we want is intentional here. */
  1336. /*
  1337. * Use helper variables for memcpy() to remain
  1338. * compliant with MISRA Rule 21.15. These should be
  1339. * optimized away.
  1340. */
  1341. pvCopySource = &pucByte[ sizeof( DNSAnswerRecord_t ) ];
  1342. pvCopyDest = &ulIPAddress;
  1343. ( void ) memcpy( pvCopyDest, pvCopySource, uxAddressLength );
  1344. #if ( ipconfigDNS_USE_CALLBACKS == 1 )
  1345. {
  1346. /* See if any asynchronous call was made to FreeRTOS_gethostbyname_a() */
  1347. if( xDNSDoCallback( ( TickType_t ) pxDNSMessageHeader->usIdentifier, pcName, ulIPAddress ) != pdFALSE )
  1348. {
  1349. /* This device has requested this DNS look-up.
  1350. * The result may be stored in the DNS cache. */
  1351. xDoStore = pdTRUE;
  1352. }
  1353. }
  1354. #endif /* ipconfigDNS_USE_CALLBACKS == 1 */
  1355. #if ( ipconfigUSE_DNS_CACHE == 1 )
  1356. {
  1357. char cBuffer[ 16 ];
  1358. /* The reply will only be stored in the DNS cache when the
  1359. * request was issued by this device. */
  1360. if( xDoStore != pdFALSE )
  1361. {
  1362. ( void ) prvProcessDNSCache( pcName, &ulIPAddress, pxDNSAnswerRecord->ulTTL, pdFALSE );
  1363. usNumARecordsStored++; /* Track # of A records stored */
  1364. }
  1365. ( void ) FreeRTOS_inet_ntop( FREERTOS_AF_INET, ( const void * ) &( ulIPAddress ), cBuffer, sizeof( cBuffer ) );
  1366. /* Show what has happened. */
  1367. FreeRTOS_printf( ( "DNS[0x%04lX]: The answer to '%s' (%s) will%s be stored\n",
  1368. ( UBaseType_t ) pxDNSMessageHeader->usIdentifier,
  1369. pcName,
  1370. cBuffer,
  1371. ( xDoStore != 0 ) ? "" : " NOT" ) );
  1372. }
  1373. #endif /* ipconfigUSE_DNS_CACHE */
  1374. }
  1375. pucByte = &( pucByte[ sizeof( DNSAnswerRecord_t ) + uxAddressLength ] );
  1376. uxSourceBytesRemaining -= ( sizeof( DNSAnswerRecord_t ) + uxAddressLength );
  1377. }
  1378. else if( uxSourceBytesRemaining >= sizeof( DNSAnswerRecord_t ) )
  1379. {
  1380. /* It's not an A record, so skip it. Get the header location
  1381. * and then jump over the header. */
  1382. /* Cast the response to DNSAnswerRecord for easy access to fields of the DNS response. */
  1383. pxDNSAnswerRecord = ipCAST_PTR_TO_TYPE_PTR( DNSAnswerRecord_t, pucByte );
  1384. pucByte = &( pucByte[ sizeof( DNSAnswerRecord_t ) ] );
  1385. uxSourceBytesRemaining -= sizeof( DNSAnswerRecord_t );
  1386. /* Determine the length of the answer data from the header. */
  1387. usDataLength = FreeRTOS_ntohs( pxDNSAnswerRecord->usDataLength );
  1388. /* Jump over the answer. */
  1389. if( uxSourceBytesRemaining >= usDataLength )
  1390. {
  1391. pucByte = &( pucByte[ usDataLength ] );
  1392. uxSourceBytesRemaining -= usDataLength;
  1393. }
  1394. else
  1395. {
  1396. /* Malformed response. */
  1397. xReturn = pdFALSE;
  1398. break;
  1399. }
  1400. }
  1401. else
  1402. {
  1403. /* Do nothing */
  1404. }
  1405. }
  1406. }
  1407. #if ( ipconfigUSE_LLMNR == 1 )
  1408. else if( ( usQuestions != ( uint16_t ) 0U ) && ( usType == dnsTYPE_A_HOST ) && ( usClass == dnsCLASS_IN ) && ( pcRequestedName != NULL ) )
  1409. {
  1410. /* If this is not a reply to our DNS request, it might an LLMNR
  1411. * request. */
  1412. if( xApplicationDNSQueryHook( &( pcRequestedName[ 1 ] ) ) != pdFALSE )
  1413. {
  1414. int16_t usLength;
  1415. NetworkBufferDescriptor_t * pxNewBuffer = NULL;
  1416. NetworkBufferDescriptor_t * pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
  1417. LLMNRAnswer_t * pxAnswer;
  1418. uint8_t * pucNewBuffer = NULL;
  1419. if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
  1420. {
  1421. size_t uxDataLength = uxBufferLength + sizeof( UDPHeader_t ) + sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
  1422. /* Set the size of the outgoing packet. */
  1423. pxNetworkBuffer->xDataLength = uxDataLength;
  1424. pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, uxDataLength + sizeof( LLMNRAnswer_t ) );
  1425. if( pxNewBuffer != NULL )
  1426. {
  1427. BaseType_t xOffset1, xOffset2;
  1428. xOffset1 = ( BaseType_t ) ( pucByte - pucUDPPayloadBuffer );
  1429. xOffset2 = ( BaseType_t ) ( ( ( uint8_t * ) pcRequestedName ) - pucUDPPayloadBuffer );
  1430. pxNetworkBuffer = pxNewBuffer;
  1431. pucNewBuffer = &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] );
  1432. pucByte = &( pucNewBuffer[ xOffset1 ] );
  1433. pcRequestedName = ( char * ) &( pucNewBuffer[ xOffset2 ] );
  1434. pxDNSMessageHeader = ipCAST_PTR_TO_TYPE_PTR( DNSMessage_t, pucNewBuffer );
  1435. }
  1436. else
  1437. {
  1438. /* Just to indicate that the message may not be answered. */
  1439. pxNetworkBuffer = NULL;
  1440. }
  1441. }
  1442. /* The test on 'pucNewBuffer' is only to satisfy lint. */
  1443. if( ( pxNetworkBuffer != NULL ) && ( pucNewBuffer != NULL ) )
  1444. {
  1445. pxAnswer = ipCAST_PTR_TO_TYPE_PTR( LLMNRAnswer_t, pucByte );
  1446. /* We leave 'usIdentifier' and 'usQuestions' untouched */
  1447. #ifndef _lint
  1448. vSetField16( pxDNSMessageHeader, DNSMessage_t, usFlags, dnsLLMNR_FLAGS_IS_REPONSE ); /* Set the response flag */
  1449. vSetField16( pxDNSMessageHeader, DNSMessage_t, usAnswers, 1 ); /* Provide a single answer */
  1450. vSetField16( pxDNSMessageHeader, DNSMessage_t, usAuthorityRRs, 0 ); /* No authority */
  1451. vSetField16( pxDNSMessageHeader, DNSMessage_t, usAdditionalRRs, 0 ); /* No additional info */
  1452. #endif /* lint */
  1453. pxAnswer->ucNameCode = dnsNAME_IS_OFFSET;
  1454. pxAnswer->ucNameOffset = ( uint8_t ) ( pcRequestedName - ( char * ) pucNewBuffer );
  1455. #ifndef _lint
  1456. vSetField16( pxAnswer, LLMNRAnswer_t, usType, dnsTYPE_A_HOST ); /* Type A: host */
  1457. vSetField16( pxAnswer, LLMNRAnswer_t, usClass, dnsCLASS_IN ); /* 1: Class IN */
  1458. vSetField32( pxAnswer, LLMNRAnswer_t, ulTTL, dnsLLMNR_TTL_VALUE );
  1459. vSetField16( pxAnswer, LLMNRAnswer_t, usDataLength, 4 );
  1460. vSetField32( pxAnswer, LLMNRAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );
  1461. #endif /* lint */
  1462. usLength = ( int16_t ) ( sizeof( *pxAnswer ) + ( size_t ) ( pucByte - pucNewBuffer ) );
  1463. prvReplyDNSMessage( pxNetworkBuffer, usLength );
  1464. if( pxNewBuffer != NULL )
  1465. {
  1466. vReleaseNetworkBufferAndDescriptor( pxNewBuffer );
  1467. }
  1468. }
  1469. }
  1470. }
  1471. else
  1472. {
  1473. /* Not an expected reply. */
  1474. }
  1475. #endif /* ipconfigUSE_LLMNR == 1 */
  1476. ( void ) uxBytesRead;
  1477. } while( ipFALSE_BOOL );
  1478. }
  1479. if( xReturn == pdFALSE )
  1480. {
  1481. /* There was an error while parsing the DNS response. Return error code. */
  1482. ulIPAddress = dnsPARSE_ERROR;
  1483. }
  1484. else if( xExpected == pdFALSE )
  1485. {
  1486. /* Do not return a valid IP-address in case the reply was not expected. */
  1487. ulIPAddress = 0UL;
  1488. }
  1489. else
  1490. {
  1491. /* The IP-address found will be returned. */
  1492. }
  1493. #if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
  1494. ( void ) xDoStore;
  1495. #endif
  1496. return ulIPAddress;
  1497. }
  1498. /*-----------------------------------------------------------*/
  1499. #if ( ipconfigUSE_NBNS == 1 )
  1500. /**
  1501. * @brief Respond to an NBNS query or an NBNS reply.
  1502. *
  1503. * @param[in] pucPayload: the UDP payload of the NBNS message.
  1504. * @param[in] uxBufferLength: Length of the Buffer.
  1505. * @param[in] ulIPAddress: IP address of the sender.
  1506. */
  1507. static void prvTreatNBNS( uint8_t * pucPayload,
  1508. size_t uxBufferLength,
  1509. uint32_t ulIPAddress )
  1510. {
  1511. uint16_t usFlags, usType, usClass;
  1512. uint8_t * pucSource, * pucTarget;
  1513. uint8_t ucByte;
  1514. uint8_t ucNBNSName[ 17 ];
  1515. uint8_t * pucUDPPayloadBuffer = pucPayload;
  1516. NetworkBufferDescriptor_t * pxNetworkBuffer;
  1517. size_t uxBytesNeeded = sizeof( UDPPacket_t ) + sizeof( NBNSRequest_t );
  1518. /* Check for minimum buffer size. */
  1519. if( uxBufferLength < uxBytesNeeded )
  1520. {
  1521. return;
  1522. }
  1523. /* Read the request flags in host endianness. */
  1524. usFlags = usChar2u16( &( pucUDPPayloadBuffer[ offsetof( NBNSRequest_t, usFlags ) ] ) );
  1525. if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY )
  1526. {
  1527. usType = usChar2u16( &( pucUDPPayloadBuffer[ offsetof( NBNSRequest_t, usType ) ] ) );
  1528. usClass = usChar2u16( &( pucUDPPayloadBuffer[ offsetof( NBNSRequest_t, usClass ) ] ) );
  1529. /* Not used for now */
  1530. ( void ) usClass;
  1531. /* For NBNS a name is 16 bytes long, written with capitals only.
  1532. * Make sure that the copy is terminated with a zero. */
  1533. pucTarget = &( ucNBNSName[ sizeof( ucNBNSName ) - 2U ] );
  1534. pucTarget[ 1 ] = ( uint8_t ) 0U;
  1535. /* Start with decoding the last 2 bytes. */
  1536. pucSource = &( pucUDPPayloadBuffer[ ( dnsNBNS_ENCODED_NAME_LENGTH - 2 ) + offsetof( NBNSRequest_t, ucName ) ] );
  1537. for( ; ; )
  1538. {
  1539. const uint8_t ucCharA = ( uint8_t ) 0x41U;
  1540. ucByte = ( ( uint8_t ) ( ( pucSource[ 0 ] - ucCharA ) << 4 ) ) | ( pucSource[ 1 ] - ucCharA );
  1541. /* Make sure there are no trailing spaces in the name. */
  1542. if( ( ucByte == ( uint8_t ) ' ' ) && ( pucTarget[ 1 ] == 0U ) )
  1543. {
  1544. ucByte = 0U;
  1545. }
  1546. *pucTarget = ucByte;
  1547. if( pucTarget == ucNBNSName )
  1548. {
  1549. break;
  1550. }
  1551. pucTarget -= 1;
  1552. pucSource -= 2;
  1553. }
  1554. #if ( ipconfigUSE_DNS_CACHE == 1 )
  1555. {
  1556. if( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) != 0U )
  1557. {
  1558. /* If this is a response from another device,
  1559. * add the name to the DNS cache */
  1560. ( void ) prvProcessDNSCache( ( char * ) ucNBNSName, &( ulIPAddress ), 0, pdFALSE );
  1561. }
  1562. }
  1563. #else
  1564. {
  1565. /* Avoid compiler warnings. */
  1566. ( void ) ulIPAddress;
  1567. }
  1568. #endif /* ipconfigUSE_DNS_CACHE */
  1569. if( ( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) == 0U ) &&
  1570. ( usType == dnsNBNS_TYPE_NET_BIOS ) &&
  1571. ( xApplicationDNSQueryHook( ( const char * ) ucNBNSName ) != pdFALSE ) )
  1572. {
  1573. uint16_t usLength;
  1574. DNSMessage_t * pxMessage;
  1575. NBNSAnswer_t * pxAnswer;
  1576. /* Someone is looking for a device with ucNBNSName,
  1577. * prepare a positive reply. */
  1578. pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
  1579. if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
  1580. {
  1581. NetworkBufferDescriptor_t * pxNewBuffer;
  1582. /* The field xDataLength was set to the total length of the UDP packet,
  1583. * i.e. the payload size plus sizeof( UDPPacket_t ). */
  1584. pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, pxNetworkBuffer->xDataLength + sizeof( NBNSAnswer_t ) );
  1585. if( pxNewBuffer != NULL )
  1586. {
  1587. pucUDPPayloadBuffer = &( pxNewBuffer->pucEthernetBuffer[ sizeof( UDPPacket_t ) ] );
  1588. pxNetworkBuffer = pxNewBuffer;
  1589. }
  1590. else
  1591. {
  1592. /* Just prevent that a reply will be sent */
  1593. pxNetworkBuffer = NULL;
  1594. }
  1595. }
  1596. /* Should not occur: pucUDPPayloadBuffer is part of a xNetworkBufferDescriptor */
  1597. if( pxNetworkBuffer != NULL )
  1598. {
  1599. pxMessage = ipCAST_PTR_TO_TYPE_PTR( DNSMessage_t, pucUDPPayloadBuffer );
  1600. /* As the fields in the structures are not word-aligned, we have to
  1601. * copy the values byte-by-byte using macro's vSetField16() and vSetField32() */
  1602. #ifndef _lint
  1603. vSetField16( pxMessage, DNSMessage_t, usFlags, dnsNBNS_QUERY_RESPONSE_FLAGS ); /* 0x8500 */
  1604. vSetField16( pxMessage, DNSMessage_t, usQuestions, 0 );
  1605. vSetField16( pxMessage, DNSMessage_t, usAnswers, 1 );
  1606. vSetField16( pxMessage, DNSMessage_t, usAuthorityRRs, 0 );
  1607. vSetField16( pxMessage, DNSMessage_t, usAdditionalRRs, 0 );
  1608. #else
  1609. ( void ) pxMessage;
  1610. #endif
  1611. pxAnswer = ipCAST_PTR_TO_TYPE_PTR( NBNSAnswer_t, &( pucUDPPayloadBuffer[ offsetof( NBNSRequest_t, usType ) ] ) );
  1612. #ifndef _lint
  1613. vSetField16( pxAnswer, NBNSAnswer_t, usType, usType ); /* Type */
  1614. vSetField16( pxAnswer, NBNSAnswer_t, usClass, dnsNBNS_CLASS_IN ); /* Class */
  1615. vSetField32( pxAnswer, NBNSAnswer_t, ulTTL, dnsNBNS_TTL_VALUE );
  1616. vSetField16( pxAnswer, NBNSAnswer_t, usDataLength, 6 ); /* 6 bytes including the length field */
  1617. vSetField16( pxAnswer, NBNSAnswer_t, usNbFlags, dnsNBNS_NAME_FLAGS );
  1618. vSetField32( pxAnswer, NBNSAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );
  1619. #else
  1620. ( void ) pxAnswer;
  1621. #endif
  1622. usLength = ( uint16_t ) ( sizeof( NBNSAnswer_t ) + ( size_t ) offsetof( NBNSRequest_t, usType ) );
  1623. prvReplyDNSMessage( pxNetworkBuffer, ( BaseType_t ) usLength );
  1624. }
  1625. }
  1626. }
  1627. }
  1628. #endif /* ipconfigUSE_NBNS */
  1629. /*-----------------------------------------------------------*/
  1630. /**
  1631. * @brief Create a socket and bind it to the standard DNS port number.
  1632. *
  1633. * @return The created socket - or NULL if the socket could not be created or could not be bound.
  1634. */
  1635. static Socket_t prvCreateDNSSocket( void )
  1636. {
  1637. Socket_t xSocket;
  1638. struct freertos_sockaddr xAddress;
  1639. BaseType_t xReturn;
  1640. /* This must be the first time this function has been called. Create
  1641. * the socket. */
  1642. xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
  1643. if( xSocketValid( xSocket ) == pdFALSE )
  1644. {
  1645. /* There was an error, return NULL. */
  1646. xSocket = NULL;
  1647. }
  1648. else
  1649. {
  1650. /* Auto bind the port. */
  1651. xAddress.sin_port = 0U;
  1652. xReturn = FreeRTOS_bind( xSocket, &xAddress, sizeof( xAddress ) );
  1653. /* Check the bind was successful, and clean up if not. */
  1654. if( xReturn != 0 )
  1655. {
  1656. ( void ) FreeRTOS_closesocket( xSocket );
  1657. xSocket = NULL;
  1658. }
  1659. else
  1660. {
  1661. /* The send and receive timeouts will be set later on. */
  1662. }
  1663. }
  1664. return xSocket;
  1665. }
  1666. /*-----------------------------------------------------------*/
  1667. #if ( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
  1668. /**
  1669. * @brief Send a DNS message to be used in NBNS or LLMNR
  1670. *
  1671. * @param[in] pxNetworkBuffer: The network buffer descriptor with the DNS message.
  1672. * @param[in] lNetLength: The length of the DNS message.
  1673. */
  1674. static void prvReplyDNSMessage( NetworkBufferDescriptor_t * pxNetworkBuffer,
  1675. BaseType_t lNetLength )
  1676. {
  1677. UDPPacket_t * pxUDPPacket;
  1678. IPHeader_t * pxIPHeader;
  1679. UDPHeader_t * pxUDPHeader;
  1680. size_t uxDataLength;
  1681. pxUDPPacket = ipCAST_PTR_TO_TYPE_PTR( UDPPacket_t, pxNetworkBuffer->pucEthernetBuffer );
  1682. pxIPHeader = &pxUDPPacket->xIPHeader;
  1683. pxUDPHeader = &pxUDPPacket->xUDPHeader;
  1684. /* HT: started using defines like 'ipSIZE_OF_xxx' */
  1685. pxIPHeader->usLength = FreeRTOS_htons( ( uint16_t ) lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );
  1686. /* HT:endian: should not be translated, copying from packet to packet */
  1687. pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;
  1688. pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
  1689. pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE;
  1690. pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );
  1691. usPacketIdentifier++;
  1692. pxUDPHeader->usLength = FreeRTOS_htons( ( uint32_t ) lNetLength + ipSIZE_OF_UDP_HEADER );
  1693. vFlip_16( pxUDPHeader->usSourcePort, pxUDPHeader->usDestinationPort );
  1694. /* Important: tell NIC driver how many bytes must be sent */
  1695. uxDataLength = ( ( size_t ) lNetLength ) + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER;
  1696. #if ( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
  1697. {
  1698. /* Calculate the IP header checksum. */
  1699. pxIPHeader->usHeaderChecksum = 0U;
  1700. pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0U, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
  1701. pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
  1702. /* calculate the UDP checksum for outgoing package */
  1703. ( void ) usGenerateProtocolChecksum( ( uint8_t * ) pxUDPPacket, uxDataLength, pdTRUE );
  1704. }
  1705. #endif
  1706. /* Important: tell NIC driver how many bytes must be sent */
  1707. pxNetworkBuffer->xDataLength = uxDataLength;
  1708. /* This function will fill in the eth addresses and send the packet */
  1709. vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );
  1710. }
  1711. #endif /* ipconfigUSE_NBNS == 1 || ipconfigUSE_LLMNR == 1 */
  1712. /*-----------------------------------------------------------*/
  1713. #if ( ipconfigUSE_DNS_CACHE == 1 )
  1714. /**
  1715. * @brief Send a DNS message to be used in NBNS or LLMNR
  1716. *
  1717. * @param[in] pcName: the name of the host
  1718. * @param[in,out] pulIP: when doing a lookup, will be set, when doing an update,
  1719. * will be read.
  1720. * @param[in] ulTTL: Time To Live
  1721. * @param[in] xLookUp: pdTRUE if a look-up is expected, pdFALSE, when the DNS cache must
  1722. * be updated.
  1723. *
  1724. * @return
  1725. */
  1726. static BaseType_t prvProcessDNSCache( const char * pcName,
  1727. uint32_t * pulIP,
  1728. uint32_t ulTTL,
  1729. BaseType_t xLookUp )
  1730. {
  1731. BaseType_t x;
  1732. BaseType_t xFound = pdFALSE;
  1733. uint32_t ulCurrentTimeSeconds = ( xTaskGetTickCount() / portTICK_PERIOD_MS ) / 1000UL;
  1734. uint32_t ulIPAddressIndex = 0;
  1735. static BaseType_t xFreeEntry = 0;
  1736. configASSERT( ( pcName != NULL ) );
  1737. /* For each entry in the DNS cache table. */
  1738. for( x = 0; x < ipconfigDNS_CACHE_ENTRIES; x++ )
  1739. {
  1740. if( xDNSCache[ x ].pcName[ 0 ] == ( char ) 0 )
  1741. {
  1742. continue;
  1743. }
  1744. if( strcmp( xDNSCache[ x ].pcName, pcName ) == 0 )
  1745. {
  1746. /* Is this function called for a lookup or to add/update an IP address? */
  1747. if( xLookUp != pdFALSE )
  1748. {
  1749. /* Confirm that the record is still fresh. */
  1750. if( ulCurrentTimeSeconds < ( xDNSCache[ x ].ulTimeWhenAddedInSeconds + FreeRTOS_ntohl( xDNSCache[ x ].ulTTL ) ) )
  1751. {
  1752. #if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 )
  1753. uint8_t ucIndex;
  1754. /* The ucCurrentIPAddress value increments without bound and will rollover, */
  1755. /* modulo it by the number of IP addresses to keep it in range. */
  1756. /* Also perform a final modulo by the max number of IP addresses */
  1757. /* per DNS cache entry to prevent out-of-bounds access in the event */
  1758. /* that ucNumIPAddresses has been corrupted. */
  1759. ucIndex = xDNSCache[ x ].ucCurrentIPAddress % xDNSCache[ x ].ucNumIPAddresses;
  1760. ucIndex = ucIndex % ( uint8_t ) ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY;
  1761. ulIPAddressIndex = ucIndex;
  1762. xDNSCache[ x ].ucCurrentIPAddress++;
  1763. #endif /* if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 ) */
  1764. *pulIP = xDNSCache[ x ].ulIPAddresses[ ulIPAddressIndex ];
  1765. }
  1766. else
  1767. {
  1768. /* Age out the old cached record. */
  1769. xDNSCache[ x ].pcName[ 0 ] = ( char ) 0;
  1770. }
  1771. }
  1772. else
  1773. {
  1774. #if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 )
  1775. if( xDNSCache[ x ].ucNumIPAddresses < ( uint8_t ) ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY )
  1776. {
  1777. /* If more answers exist than there are IP address storage slots */
  1778. /* they will overwrite entry 0 */
  1779. ulIPAddressIndex = xDNSCache[ x ].ucNumIPAddresses;
  1780. xDNSCache[ x ].ucNumIPAddresses++;
  1781. }
  1782. #endif
  1783. xDNSCache[ x ].ulIPAddresses[ ulIPAddressIndex ] = *pulIP;
  1784. xDNSCache[ x ].ulTTL = ulTTL;
  1785. xDNSCache[ x ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;
  1786. }
  1787. xFound = pdTRUE;
  1788. break;
  1789. }
  1790. }
  1791. if( xFound == pdFALSE )
  1792. {
  1793. if( xLookUp != pdFALSE )
  1794. {
  1795. *pulIP = 0UL;
  1796. }
  1797. else
  1798. {
  1799. /* Add or update the item. */
  1800. if( strlen( pcName ) < ( size_t ) ipconfigDNS_CACHE_NAME_LENGTH )
  1801. {
  1802. ( void ) strcpy( xDNSCache[ xFreeEntry ].pcName, pcName );
  1803. xDNSCache[ xFreeEntry ].ulIPAddresses[ 0 ] = *pulIP;
  1804. xDNSCache[ xFreeEntry ].ulTTL = ulTTL;
  1805. xDNSCache[ xFreeEntry ].ulTimeWhenAddedInSeconds = ulCurrentTimeSeconds;
  1806. #if ( ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY > 1 )
  1807. xDNSCache[ xFreeEntry ].ucNumIPAddresses = 1;
  1808. xDNSCache[ xFreeEntry ].ucCurrentIPAddress = 0;
  1809. /* Initialize all remaining IP addresses in this entry to 0 */
  1810. ( void ) memset( &xDNSCache[ xFreeEntry ].ulIPAddresses[ 1 ],
  1811. 0,
  1812. sizeof( xDNSCache[ xFreeEntry ].ulIPAddresses[ 1 ] ) *
  1813. ( ( uint32_t ) ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY - 1U ) );
  1814. #endif
  1815. xFreeEntry++;
  1816. if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )
  1817. {
  1818. xFreeEntry = 0;
  1819. }
  1820. }
  1821. }
  1822. }
  1823. if( ( xLookUp == 0 ) || ( *pulIP != 0UL ) )
  1824. {
  1825. FreeRTOS_debug_printf( ( "prvProcessDNSCache: %s: '%s' @ %lxip\n", ( xLookUp != 0 ) ? "look-up" : "add", pcName, FreeRTOS_ntohl( *pulIP ) ) );
  1826. }
  1827. return xFound;
  1828. }
  1829. #endif /* ipconfigUSE_DNS_CACHE */
  1830. #endif /* ipconfigUSE_DNS != 0 */
  1831. /*-----------------------------------------------------------*/
  1832. /* Provide access to private members for testing. */
  1833. #ifdef FREERTOS_ENABLE_UNIT_TESTS
  1834. #include "freertos_tcp_test_access_dns_define.h"
  1835. #endif