2 * FreeRTOS+TCP V2.0.3
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://aws.amazon.com/freertos
\r
23 * http://www.FreeRTOS.org
\r
26 /* Standard includes. */
\r
29 /* FreeRTOS includes. */
\r
30 #include "FreeRTOS.h"
\r
36 /* FreeRTOS+TCP includes. */
\r
37 #include "FreeRTOS_IP.h"
\r
38 #include "FreeRTOS_Sockets.h"
\r
39 #include "FreeRTOS_IP_Private.h"
\r
40 #include "FreeRTOS_UDP_IP.h"
\r
41 #include "FreeRTOS_DNS.h"
\r
42 #include "NetworkBufferManagement.h"
\r
43 #include "NetworkInterface.h"
\r
44 #include "IPTraceMacroDefaults.h"
\r
46 /* Exclude the entire file if DNS is not enabled. */
\r
47 #if( ipconfigUSE_DNS != 0 )
\r
49 #if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )
\r
50 #define dnsDNS_PORT 0x3500u
\r
51 #define dnsONE_QUESTION 0x0100u
\r
52 #define dnsOUTGOING_FLAGS 0x0001u /* Standard query. */
\r
53 #define dnsRX_FLAGS_MASK 0x0f80u /* The bits of interest in the flags field of incoming DNS messages. */
\r
54 #define dnsEXPECTED_RX_FLAGS 0x0080u /* Should be a response, without any errors. */
\r
56 #define dnsDNS_PORT 0x0035u
\r
57 #define dnsONE_QUESTION 0x0001u
\r
58 #define dnsOUTGOING_FLAGS 0x0100u /* Standard query. */
\r
59 #define dnsRX_FLAGS_MASK 0x800fu /* The bits of interest in the flags field of incoming DNS messages. */
\r
60 #define dnsEXPECTED_RX_FLAGS 0x8000u /* Should be a response, without any errors. */
\r
62 #endif /* ipconfigBYTE_ORDER */
\r
64 /* The maximum number of times a DNS request should be sent out if a response
\r
65 is not received, before giving up. */
\r
66 #ifndef ipconfigDNS_REQUEST_ATTEMPTS
\r
67 #define ipconfigDNS_REQUEST_ATTEMPTS 5
\r
70 /* If the top two bits in the first character of a name field are set then the
\r
71 name field is an offset to the string, rather than the string itself. */
\r
72 #define dnsNAME_IS_OFFSET ( ( uint8_t ) 0xc0 )
\r
75 #define dnsNBNS_FLAGS_RESPONSE 0x8000u
\r
76 #define dnsNBNS_FLAGS_OPCODE_MASK 0x7800u
\r
77 #define dnsNBNS_FLAGS_OPCODE_QUERY 0x0000u
\r
78 #define dnsNBNS_FLAGS_OPCODE_REGISTRATION 0x2800u
\r
81 #define dnsTYPE_A_HOST 0x0001u
\r
82 #define dnsCLASS_IN 0x0001u
\r
84 /* LLMNR constants. */
\r
85 #define dnsLLMNR_TTL_VALUE 300000u
\r
86 #define dnsLLMNR_FLAGS_IS_REPONSE 0x8000u
\r
88 /* NBNS constants. */
\r
89 #define dnsNBNS_TTL_VALUE 3600u /* 1 hour valid */
\r
90 #define dnsNBNS_TYPE_NET_BIOS 0x0020u
\r
91 #define dnsNBNS_CLASS_IN 0x0001u
\r
92 #define dnsNBNS_NAME_FLAGS 0x6000u
\r
93 #define dnsNBNS_ENCODED_NAME_LENGTH 32
\r
95 /* If the queried NBNS name matches with the device's name,
\r
96 the query will be responded to with these flags: */
\r
97 #define dnsNBNS_QUERY_RESPONSE_FLAGS 0x8500u
\r
100 * Create a socket and bind it to the standard DNS port number. Return the
\r
101 * the created socket - or NULL if the socket could not be created or bound.
\r
103 static Socket_t prvCreateDNSSocket( void );
\r
106 * Create the DNS message in the zero copy buffer passed in the first parameter.
\r
108 static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier );
\r
111 * Simple routine that jumps over the NAME field of a resource record.
\r
113 static uint8_t *prvSkipNameField( uint8_t *pucByte );
\r
116 * Process a response packet from a DNS server.
\r
118 static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, TickType_t xIdentifier );
\r
121 * Prepare and send a message to a DNS server. 'xReadTimeOut_ms' will be passed as
\r
122 * zero, in case the user has supplied a call-back function.
\r
124 static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms );
\r
127 * The NBNS and the LLMNR protocol share this reply function.
\r
129 #if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
\r
130 static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength );
\r
133 #if( ipconfigUSE_NBNS == 1 )
\r
134 static portINLINE void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, uint32_t ulIPAddress );
\r
135 #endif /* ipconfigUSE_NBNS */
\r
137 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
138 static uint8_t *prvReadNameField( uint8_t *pucByte, char *pcName, BaseType_t xLen );
\r
139 static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, BaseType_t xLookUp );
\r
141 typedef struct xDNS_CACHE_TABLE_ROW
\r
143 uint32_t ulIPAddress; /* The IP address of an ARP cache entry. */
\r
144 char pcName[ipconfigDNS_CACHE_NAME_LENGTH]; /* The name of the host */
\r
145 uint8_t ucAge; /* A value that is periodically decremented but can also be refreshed by active communication. The ARP cache entry is removed if the value reaches zero. */
\r
148 static DNSCacheRow_t xDNSCache[ ipconfigDNS_CACHE_ENTRIES ];
\r
149 #endif /* ipconfigUSE_DNS_CACHE == 1 */
\r
151 #if( ipconfigUSE_LLMNR == 1 )
\r
152 const MACAddress_t xLLMNR_MacAdress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };
\r
153 #endif /* ipconfigUSE_LLMNR == 1 */
\r
155 /*-----------------------------------------------------------*/
\r
157 #include "pack_struct_start.h"
\r
160 uint16_t usIdentifier;
\r
162 uint16_t usQuestions;
\r
163 uint16_t usAnswers;
\r
164 uint16_t usAuthorityRRs;
\r
165 uint16_t usAdditionalRRs;
\r
167 #include "pack_struct_end.h"
\r
168 typedef struct xDNSMessage DNSMessage_t;
\r
170 /* A DNS query consists of a header, as described in 'struct xDNSMessage'
\r
171 It is followed by 1 or more queries, each one consisting of a name and a tail,
\r
172 with two fields: type and class
\r
174 #include "pack_struct_start.h"
\r
180 #include "pack_struct_end.h"
\r
181 typedef struct xDNSTail DNSTail_t;
\r
183 #if( ipconfigUSE_LLMNR == 1 )
\r
185 #include "pack_struct_start.h"
\r
186 struct xLLMNRAnswer
\r
188 uint8_t ucNameCode;
\r
189 uint8_t ucNameOffset; /* The name is not repeated in the answer, only the offset is given with "0xc0 <offs>" */
\r
193 uint16_t usDataLength;
\r
194 uint32_t ulIPAddress;
\r
196 #include "pack_struct_end.h"
\r
197 typedef struct xLLMNRAnswer LLMNRAnswer_t;
\r
199 #endif /* ipconfigUSE_LLMNR == 1 */
\r
201 #if( ipconfigUSE_NBNS == 1 )
\r
203 #include "pack_struct_start.h"
\r
204 struct xNBNSRequest
\r
206 uint16_t usRequestId;
\r
208 uint16_t ulRequestCount;
\r
209 uint16_t usAnswerRSS;
\r
210 uint16_t usAuthRSS;
\r
211 uint16_t usAdditionalRSS;
\r
212 uint8_t ucNameSpace;
\r
213 uint8_t ucName[ dnsNBNS_ENCODED_NAME_LENGTH ];
\r
214 uint8_t ucNameZero;
\r
218 #include "pack_struct_end.h"
\r
219 typedef struct xNBNSRequest NBNSRequest_t;
\r
221 #include "pack_struct_start.h"
\r
227 uint16_t usDataLength;
\r
228 uint16_t usNbFlags; /* NetBIOS flags 0x6000 : IP-address, big-endian */
\r
229 uint32_t ulIPAddress;
\r
231 #include "pack_struct_end.h"
\r
232 typedef struct xNBNSAnswer NBNSAnswer_t;
\r
234 #endif /* ipconfigUSE_NBNS == 1 */
\r
236 /*-----------------------------------------------------------*/
\r
238 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
239 uint32_t FreeRTOS_dnslookup( const char *pcHostName )
\r
241 uint32_t ulIPAddress = 0UL;
\r
242 prvProcessDNSCache( pcHostName, &ulIPAddress, pdTRUE );
\r
243 return ulIPAddress;
\r
245 #endif /* ipconfigUSE_DNS_CACHE == 1 */
\r
246 /*-----------------------------------------------------------*/
\r
248 #if( ipconfigDNS_USE_CALLBACKS != 0 )
\r
250 typedef struct xDNS_Callback {
\r
251 TickType_t xRemaningTime; /* Timeout in ms */
\r
252 FOnDNSEvent pCallbackFunction; /* Function to be called when the address has been found or when a timeout has beeen reached */
\r
253 TimeOut_t xTimeoutState;
\r
255 struct xLIST_ITEM xListItem;
\r
259 static List_t xCallbackList;
\r
261 /* Define FreeRTOS_gethostbyname() as a normal blocking call. */
\r
262 uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
\r
264 return FreeRTOS_gethostbyname_a( pcHostName, ( FOnDNSEvent ) NULL, ( void* )NULL, 0 );
\r
266 /*-----------------------------------------------------------*/
\r
268 /* Initialise the list of call-back structures. */
\r
269 void vDNSInitialise( void );
\r
270 void vDNSInitialise( void )
\r
272 vListInitialise( &xCallbackList );
\r
274 /*-----------------------------------------------------------*/
\r
276 /* Iterate through the list of call-back structures and remove
\r
277 old entries which have reached a timeout.
\r
278 As soon as the list hase become empty, the DNS timer will be stopped
\r
279 In case pvSearchID is supplied, the user wants to cancel a DNS request
\r
281 void vDNSCheckCallBack( void *pvSearchID );
\r
282 void vDNSCheckCallBack( void *pvSearchID )
\r
284 const ListItem_t *pxIterator;
\r
285 const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );
\r
289 for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
\r
290 pxIterator != ( const ListItem_t * ) xEnd;
\r
293 DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
\r
294 /* Move to the next item because we might remove this item */
\r
295 pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator );
\r
296 if( ( pvSearchID != NULL ) && ( pvSearchID == pxCallback->pvSearchID ) )
\r
298 uxListRemove( &pxCallback->xListItem );
\r
299 vPortFree( pxCallback );
\r
301 else if( xTaskCheckForTimeOut( &pxCallback->xTimeoutState, &pxCallback->xRemaningTime ) != pdFALSE )
\r
303 pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0 );
\r
304 uxListRemove( &pxCallback->xListItem );
\r
305 vPortFree( ( void * ) pxCallback );
\r
311 if( listLIST_IS_EMPTY( &xCallbackList ) )
\r
313 vIPSetDnsTimerEnableState( pdFALSE );
\r
316 /*-----------------------------------------------------------*/
\r
318 void FreeRTOS_gethostbyname_cancel( void *pvSearchID )
\r
320 /* _HT_ Should better become a new API call to have the IP-task remove the callback */
\r
321 vDNSCheckCallBack( pvSearchID );
\r
323 /*-----------------------------------------------------------*/
\r
325 /* FreeRTOS_gethostbyname_a() was called along with callback parameters.
\r
326 Store them in a list for later reference. */
\r
327 static void vDNSSetCallBack( const char *pcHostName, void *pvSearchID, FOnDNSEvent pCallbackFunction, TickType_t xTimeout, TickType_t xIdentifier );
\r
328 static void vDNSSetCallBack( const char *pcHostName, void *pvSearchID, FOnDNSEvent pCallbackFunction, TickType_t xTimeout, TickType_t xIdentifier )
\r
330 size_t lLength = strlen( pcHostName );
\r
331 DNSCallback_t *pxCallback = ( DNSCallback_t * )pvPortMalloc( sizeof( *pxCallback ) + lLength );
\r
333 /* Translate from ms to number of clock ticks. */
\r
334 xTimeout /= portTICK_PERIOD_MS;
\r
335 if( pxCallback != NULL )
\r
337 if( listLIST_IS_EMPTY( &xCallbackList ) )
\r
339 /* This is the first one, start the DNS timer to check for timeouts */
\r
340 vIPReloadDNSTimer( FreeRTOS_min_uint32( 1000U, xTimeout ) );
\r
342 strcpy( pxCallback->pcName, pcHostName );
\r
343 pxCallback->pCallbackFunction = pCallbackFunction;
\r
344 pxCallback->pvSearchID = pvSearchID;
\r
345 pxCallback->xRemaningTime = xTimeout;
\r
346 vTaskSetTimeOutState( &pxCallback->xTimeoutState );
\r
347 listSET_LIST_ITEM_OWNER( &( pxCallback->xListItem ), ( void* ) pxCallback );
\r
348 listSET_LIST_ITEM_VALUE( &( pxCallback->xListItem ), xIdentifier );
\r
351 vListInsertEnd( &xCallbackList, &pxCallback->xListItem );
\r
356 /*-----------------------------------------------------------*/
\r
358 /* A DNS reply was received, see if there is any matching entry and
\r
359 call the handler. */
\r
360 static void vDNSDoCallback( TickType_t xIdentifier, const char *pcName, uint32_t ulIPAddress );
\r
361 static void vDNSDoCallback( TickType_t xIdentifier, const char *pcName, uint32_t ulIPAddress )
\r
363 const ListItem_t *pxIterator;
\r
364 const MiniListItem_t* xEnd = ( const MiniListItem_t* )listGET_END_MARKER( &xCallbackList );
\r
368 for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
\r
369 pxIterator != ( const ListItem_t * ) xEnd;
\r
370 pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
\r
372 if( listGET_LIST_ITEM_VALUE( pxIterator ) == xIdentifier )
\r
374 DNSCallback_t *pxCallback = ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
\r
375 pxCallback->pCallbackFunction( pcName, pxCallback->pvSearchID, ulIPAddress );
\r
376 uxListRemove( &pxCallback->xListItem );
\r
377 vPortFree( pxCallback );
\r
378 if( listLIST_IS_EMPTY( &xCallbackList ) )
\r
380 vIPSetDnsTimerEnableState( pdFALSE );
\r
389 #endif /* ipconfigDNS_USE_CALLBACKS != 0 */
\r
390 /*-----------------------------------------------------------*/
\r
392 #if( ipconfigDNS_USE_CALLBACKS == 0 )
\r
393 uint32_t FreeRTOS_gethostbyname( const char *pcHostName )
\r
395 uint32_t FreeRTOS_gethostbyname_a( const char *pcHostName, FOnDNSEvent pCallback, void *pvSearchID, TickType_t xTimeout )
\r
398 uint32_t ulIPAddress = 0UL;
\r
399 static uint16_t usIdentifier = 0u;
\r
400 TickType_t xReadTimeOut_ms = 1200U;
\r
401 /* Generate a unique identifier for this query. Keep it in a local variable
\r
402 as gethostbyname() may be called from different threads */
\r
403 TickType_t xIdentifier = ( TickType_t )usIdentifier++;
\r
405 /* If the supplied hostname is IP address, convert it to uint32_t
\r
407 #if( ipconfigINCLUDE_FULL_INET_ADDR == 1 )
\r
409 ulIPAddress = FreeRTOS_inet_addr( pcHostName );
\r
411 #endif /* ipconfigINCLUDE_FULL_INET_ADDR == 1 */
\r
413 /* If a DNS cache is used then check the cache before issuing another DNS
\r
415 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
417 if( ulIPAddress == 0UL )
\r
419 ulIPAddress = FreeRTOS_dnslookup( pcHostName );
\r
420 if( ulIPAddress != 0 )
\r
422 FreeRTOS_debug_printf( ( "FreeRTOS_gethostbyname: found '%s' in cache: %lxip\n", pcHostName, ulIPAddress ) );
\r
426 /* prvGetHostByName will be called to start a DNS lookup */
\r
430 #endif /* ipconfigUSE_DNS_CACHE == 1 */
\r
432 #if( ipconfigDNS_USE_CALLBACKS != 0 )
\r
434 if( pCallback != NULL )
\r
436 if( ulIPAddress == 0UL )
\r
438 /* The user has provided a callback function, so do not block on recvfrom() */
\r
439 xReadTimeOut_ms = 0;
\r
440 vDNSSetCallBack( pcHostName, pvSearchID, pCallback, xTimeout, ( TickType_t ) xIdentifier );
\r
444 /* The IP address is known, do the call-back now. */
\r
445 pCallback( pcHostName, pvSearchID, ulIPAddress );
\r
451 if( ulIPAddress == 0UL)
\r
453 ulIPAddress = prvGetHostByName( pcHostName, xIdentifier, xReadTimeOut_ms );
\r
456 return ulIPAddress;
\r
458 /*-----------------------------------------------------------*/
\r
460 static uint32_t prvGetHostByName( const char *pcHostName, TickType_t xIdentifier, TickType_t xReadTimeOut_ms )
\r
462 struct freertos_sockaddr xAddress;
\r
463 Socket_t xDNSSocket;
\r
464 uint32_t ulIPAddress = 0UL;
\r
465 uint8_t *pucUDPPayloadBuffer;
\r
466 uint32_t ulAddressLength = sizeof( struct freertos_sockaddr );
\r
467 BaseType_t xAttempt;
\r
469 size_t xPayloadLength, xExpectedPayloadLength;
\r
470 TickType_t xWriteTimeOut_ms = 100U;
\r
472 #if( ipconfigUSE_LLMNR == 1 )
\r
473 BaseType_t bHasDot = pdFALSE;
\r
474 #endif /* ipconfigUSE_LLMNR == 1 */
\r
476 /* If LLMNR is being used then determine if the host name includes a '.' -
\r
477 if not then LLMNR can be used as the lookup method. */
\r
478 #if( ipconfigUSE_LLMNR == 1 )
\r
480 const char *pucPtr;
\r
481 for( pucPtr = pcHostName; *pucPtr; pucPtr++ )
\r
483 if( *pucPtr == '.' )
\r
490 #endif /* ipconfigUSE_LLMNR == 1 */
\r
492 /* Two is added at the end for the count of characters in the first
\r
493 subdomain part and the string end byte. */
\r
494 xExpectedPayloadLength = sizeof( DNSMessage_t ) + strlen( pcHostName ) + sizeof( uint16_t ) + sizeof( uint16_t ) + 2u;
\r
496 xDNSSocket = prvCreateDNSSocket();
\r
498 if( xDNSSocket != NULL )
\r
500 FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xWriteTimeOut_ms, sizeof( TickType_t ) );
\r
501 FreeRTOS_setsockopt( xDNSSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xReadTimeOut_ms, sizeof( TickType_t ) );
\r
503 for( xAttempt = 0; xAttempt < ipconfigDNS_REQUEST_ATTEMPTS; xAttempt++ )
\r
505 /* Get a buffer. This uses a maximum delay, but the delay will be
\r
506 capped to ipconfigUDP_MAX_SEND_BLOCK_TIME_TICKS so the return value
\r
507 still needs to be tested. */
\r
508 pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xExpectedPayloadLength, portMAX_DELAY );
\r
510 if( pucUDPPayloadBuffer != NULL )
\r
512 /* Create the message in the obtained buffer. */
\r
513 xPayloadLength = prvCreateDNSMessage( pucUDPPayloadBuffer, pcHostName, xIdentifier );
\r
515 iptraceSENDING_DNS_REQUEST();
\r
517 /* Obtain the DNS server address. */
\r
518 FreeRTOS_GetAddressConfiguration( NULL, NULL, NULL, &ulIPAddress );
\r
520 /* Send the DNS message. */
\r
521 #if( ipconfigUSE_LLMNR == 1 )
\r
522 if( bHasDot == pdFALSE )
\r
524 /* Use LLMNR addressing. */
\r
525 ( ( DNSMessage_t * ) pucUDPPayloadBuffer) -> usFlags = 0;
\r
526 xAddress.sin_addr = ipLLMNR_IP_ADDR; /* Is in network byte order. */
\r
527 xAddress.sin_port = FreeRTOS_ntohs( ipLLMNR_PORT );
\r
532 /* Use DNS server. */
\r
533 xAddress.sin_addr = ulIPAddress;
\r
534 xAddress.sin_port = dnsDNS_PORT;
\r
539 if( FreeRTOS_sendto( xDNSSocket, pucUDPPayloadBuffer, xPayloadLength, FREERTOS_ZERO_COPY, &xAddress, sizeof( xAddress ) ) != 0 )
\r
541 /* Wait for the reply. */
\r
542 lBytes = FreeRTOS_recvfrom( xDNSSocket, &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xAddress, &ulAddressLength );
\r
546 /* The reply was received. Process it. */
\r
547 ulIPAddress = prvParseDNSReply( pucUDPPayloadBuffer, xIdentifier );
\r
549 /* Finished with the buffer. The zero copy interface
\r
550 is being used, so the buffer must be freed by the
\r
552 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
554 if( ulIPAddress != 0UL )
\r
563 /* The message was not sent so the stack will not be
\r
564 releasing the zero copy - it must be released here. */
\r
565 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
570 /* Finished with the socket. */
\r
571 FreeRTOS_closesocket( xDNSSocket );
\r
574 return ulIPAddress;
\r
576 /*-----------------------------------------------------------*/
\r
578 static size_t prvCreateDNSMessage( uint8_t *pucUDPPayloadBuffer, const char *pcHostName, TickType_t xIdentifier )
\r
580 DNSMessage_t *pxDNSMessageHeader;
\r
581 uint8_t *pucStart, *pucByte;
\r
583 static const DNSMessage_t xDefaultPartDNSHeader =
\r
585 0, /* The identifier will be overwritten. */
\r
586 dnsOUTGOING_FLAGS, /* Flags set for standard query. */
\r
587 dnsONE_QUESTION, /* One question is being asked. */
\r
588 0, /* No replies are included. */
\r
589 0, /* No authorities. */
\r
590 0 /* No additional authorities. */
\r
593 /* Copy in the const part of the header. */
\r
594 memcpy( ( void * ) pucUDPPayloadBuffer, ( void * ) &xDefaultPartDNSHeader, sizeof( xDefaultPartDNSHeader ) );
\r
596 /* Write in a unique identifier. */
\r
597 pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
\r
598 pxDNSMessageHeader->usIdentifier = ( uint16_t ) xIdentifier;
\r
600 /* Create the resource record at the end of the header. First
\r
601 find the end of the header. */
\r
602 pucStart = pucUDPPayloadBuffer + sizeof( xDefaultPartDNSHeader );
\r
604 /* Leave a gap for the first length bytes. */
\r
605 pucByte = pucStart + 1;
\r
607 /* Copy in the host name. */
\r
608 strcpy( ( char * ) pucByte, pcHostName );
\r
610 /* Mark the end of the string. */
\r
611 pucByte += strlen( pcHostName );
\r
614 /* Walk the string to replace the '.' characters with byte counts.
\r
615 pucStart holds the address of the byte count. Walking the string
\r
616 starts after the byte count position. */
\r
617 pucByte = pucStart;
\r
623 while( ( *pucByte != 0x00 ) && ( *pucByte != '.' ) )
\r
628 /* Fill in the byte count, then move the pucStart pointer up to
\r
629 the found byte position. */
\r
630 *pucStart = ( uint8_t ) ( ( uint32_t ) pucByte - ( uint32_t ) pucStart );
\r
633 pucStart = pucByte;
\r
635 } while( *pucByte != 0x00 );
\r
637 /* Finish off the record. */
\r
639 pxTail = (DNSTail_t *)( pucByte + 1 );
\r
641 vSetField16( pxTail, DNSTail_t, usType, dnsTYPE_A_HOST ); /* Type A: host */
\r
642 vSetField16( pxTail, DNSTail_t, usClass, dnsCLASS_IN ); /* 1: Class IN */
\r
644 /* Return the total size of the generated message, which is the space from
\r
645 the last written byte to the beginning of the buffer. */
\r
646 return ( ( uint32_t ) pucByte - ( uint32_t ) pucUDPPayloadBuffer + 1 ) + sizeof( *pxTail );
\r
648 /*-----------------------------------------------------------*/
\r
650 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
652 static uint8_t *prvReadNameField( uint8_t *pucByte, char *pcName, BaseType_t xLen )
\r
654 BaseType_t xNameLen = 0;
\r
655 /* Determine if the name is the fully coded name, or an offset to the name
\r
656 elsewhere in the message. */
\r
657 if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
\r
659 /* Jump over the two byte offset. */
\r
660 pucByte += sizeof( uint16_t );
\r
665 /* pucByte points to the full name. Walk over the string. */
\r
666 while( *pucByte != 0x00 )
\r
669 if( xNameLen && xNameLen < xLen - 1 )
\r
670 pcName[xNameLen++] = '.';
\r
671 for( xCount = *(pucByte++); xCount--; pucByte++ )
\r
673 if( xNameLen < xLen - 1 )
\r
674 pcName[xNameLen++] = *( ( char * ) pucByte );
\r
683 #endif /* ipconfigUSE_DNS_CACHE == 1 */
\r
684 /*-----------------------------------------------------------*/
\r
686 static uint8_t *prvSkipNameField( uint8_t *pucByte )
\r
688 /* Determine if the name is the fully coded name, or an offset to the name
\r
689 elsewhere in the message. */
\r
690 if( ( *pucByte & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
\r
692 /* Jump over the two byte offset. */
\r
693 pucByte += sizeof( uint16_t );
\r
698 /* pucByte points to the full name. Walk over the string. */
\r
699 while( *pucByte != 0x00 )
\r
701 /* The number of bytes to jump for each name section is stored in the byte
\r
702 before the name section. */
\r
703 pucByte += ( *pucByte + 1 );
\r
711 /*-----------------------------------------------------------*/
\r
713 uint32_t ulDNSHandlePacket( NetworkBufferDescriptor_t *pxNetworkBuffer )
\r
715 uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
\r
716 DNSMessage_t *pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
\r
718 prvParseDNSReply( pucUDPPayloadBuffer, ( uint32_t ) pxDNSMessageHeader->usIdentifier );
\r
720 /* The packet was not consumed. */
\r
723 /*-----------------------------------------------------------*/
\r
725 #if( ipconfigUSE_NBNS == 1 )
\r
727 uint32_t ulNBNSHandlePacket (NetworkBufferDescriptor_t *pxNetworkBuffer )
\r
729 UDPPacket_t *pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
\r
730 uint8_t *pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + sizeof( *pxUDPPacket );
\r
732 prvTreatNBNS( pucUDPPayloadBuffer, pxUDPPacket->xIPHeader.ulSourceIPAddress );
\r
734 /* The packet was not consumed. */
\r
738 #endif /* ipconfigUSE_NBNS */
\r
739 /*-----------------------------------------------------------*/
\r
741 static uint32_t prvParseDNSReply( uint8_t *pucUDPPayloadBuffer, TickType_t xIdentifier )
\r
743 DNSMessage_t *pxDNSMessageHeader;
\r
744 uint32_t ulIPAddress = 0UL;
\r
745 #if( ipconfigUSE_LLMNR == 1 )
\r
746 char *pcRequestedName = NULL;
\r
749 uint16_t x, usDataLength, usQuestions;
\r
750 #if( ipconfigUSE_LLMNR == 1 )
\r
751 uint16_t usType = 0, usClass = 0;
\r
753 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
754 char pcName[128] = ""; /*_RB_ What is the significance of 128? Probably too big to go on the stack for a small MCU but don't know how else it could be made re-entrant. Might be necessary. */
\r
757 pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
\r
759 if( pxDNSMessageHeader->usIdentifier == ( uint16_t ) xIdentifier )
\r
761 /* Start at the first byte after the header. */
\r
762 pucByte = pucUDPPayloadBuffer + sizeof( DNSMessage_t );
\r
764 /* Skip any question records. */
\r
765 usQuestions = FreeRTOS_ntohs( pxDNSMessageHeader->usQuestions );
\r
766 for( x = 0; x < usQuestions; x++ )
\r
768 #if( ipconfigUSE_LLMNR == 1 )
\r
772 pcRequestedName = ( char * ) pucByte;
\r
777 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
780 pucByte = prvReadNameField( pucByte, pcName, sizeof( pcName ) );
\r
783 #endif /* ipconfigUSE_DNS_CACHE */
\r
785 /* Skip the variable length pcName field. */
\r
786 pucByte = prvSkipNameField( pucByte );
\r
789 #if( ipconfigUSE_LLMNR == 1 )
\r
791 /* usChar2u16 returns value in host endianness. */
\r
792 usType = usChar2u16( pucByte );
\r
793 usClass = usChar2u16( pucByte + 2 );
\r
795 #endif /* ipconfigUSE_LLMNR */
\r
797 /* Skip the type and class fields. */
\r
798 pucByte += sizeof( uint32_t );
\r
801 /* Search through the answers records. */
\r
802 pxDNSMessageHeader->usAnswers = FreeRTOS_ntohs( pxDNSMessageHeader->usAnswers );
\r
804 if( ( pxDNSMessageHeader->usFlags & dnsRX_FLAGS_MASK ) == dnsEXPECTED_RX_FLAGS )
\r
806 for( x = 0; x < pxDNSMessageHeader->usAnswers; x++ )
\r
808 pucByte = prvSkipNameField( pucByte );
\r
810 /* Is the type field that of an A record? */
\r
811 if( usChar2u16( pucByte ) == dnsTYPE_A_HOST )
\r
813 /* This is the required record. Skip the type, class, and
\r
814 time to live fields, plus the first byte of the data
\r
816 pucByte += ( sizeof( uint32_t ) + sizeof( uint32_t ) + sizeof( uint8_t ) );
\r
818 /* Sanity check the data length. */
\r
819 if( ( size_t ) *pucByte == sizeof( uint32_t ) )
\r
821 /* Skip the second byte of the length. */
\r
824 /* Copy the IP address out of the record. */
\r
825 memcpy( ( void * ) &ulIPAddress, ( void * ) pucByte, sizeof( uint32_t ) );
\r
827 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
829 prvProcessDNSCache( pcName, &ulIPAddress, pdFALSE );
\r
831 #endif /* ipconfigUSE_DNS_CACHE */
\r
832 #if( ipconfigDNS_USE_CALLBACKS != 0 )
\r
834 /* See if any asynchronous call was made to FreeRTOS_gethostbyname_a() */
\r
835 vDNSDoCallback( ( TickType_t ) pxDNSMessageHeader->usIdentifier, pcName, ulIPAddress );
\r
837 #endif /* ipconfigDNS_USE_CALLBACKS != 0 */
\r
844 /* Skip the type, class and time to live fields. */
\r
845 pucByte += ( sizeof( uint32_t ) + sizeof( uint32_t ) );
\r
847 /* Determine the length of the data in the field. */
\r
848 memcpy( ( void * ) &usDataLength, ( void * ) pucByte, sizeof( uint16_t ) );
\r
849 usDataLength = FreeRTOS_ntohs( usDataLength );
\r
851 /* Jump over the data length bytes, and the data itself. */
\r
852 pucByte += usDataLength + sizeof( uint16_t );
\r
856 #if( ipconfigUSE_LLMNR == 1 )
\r
857 else if( ( usQuestions != ( uint16_t )0u ) && ( usType == ( uint16_t )dnsTYPE_A_HOST ) && ( usClass == ( uint16_t )dnsCLASS_IN ) )
\r
859 /* If this is not a reply to our DNS request, it might an LLMNR
\r
861 if( xApplicationDNSQueryHook ( ( pcRequestedName + 1 ) ) )
\r
864 NetworkBufferDescriptor_t *pxNewBuffer = NULL;
\r
865 NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
\r
866 LLMNRAnswer_t *pxAnswer;
\r
868 if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
\r
870 BaseType_t xDataLength = pxNetworkBuffer->xDataLength + sizeof( UDPHeader_t ) +
\r
871 sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
\r
873 /* The field xDataLength was set to the length of the UDP payload.
\r
874 The answer (reply) will be longer than the request, so the packet
\r
875 must be duplicaed into a bigger buffer */
\r
876 pxNetworkBuffer->xDataLength = xDataLength;
\r
877 pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + 16 );
\r
878 if( pxNewBuffer != NULL )
\r
880 BaseType_t xOffset1, xOffset2;
\r
882 xOffset1 = ( BaseType_t ) ( pucByte - pucUDPPayloadBuffer );
\r
883 xOffset2 = ( BaseType_t ) ( ( ( uint8_t * ) pcRequestedName ) - pucUDPPayloadBuffer );
\r
885 pxNetworkBuffer = pxNewBuffer;
\r
886 pucUDPPayloadBuffer = pxNetworkBuffer->pucEthernetBuffer + ipUDP_PAYLOAD_OFFSET_IPv4;
\r
888 pucByte = pucUDPPayloadBuffer + xOffset1;
\r
889 pcRequestedName = ( char * ) ( pucUDPPayloadBuffer + xOffset2 );
\r
890 pxDNSMessageHeader = ( DNSMessage_t * ) pucUDPPayloadBuffer;
\r
895 /* Just to indicate that the message may not be answered. */
\r
896 pxNetworkBuffer = NULL;
\r
899 if( pxNetworkBuffer != NULL )
\r
901 pxAnswer = (LLMNRAnswer_t *)pucByte;
\r
903 /* Leave 'usIdentifier' and 'usQuestions' untouched. */
\r
904 vSetField16( pxDNSMessageHeader, DNSMessage_t, usFlags, dnsLLMNR_FLAGS_IS_REPONSE ); /* Set the response flag */
\r
905 vSetField16( pxDNSMessageHeader, DNSMessage_t, usAnswers, 1 ); /* Provide a single answer */
\r
906 vSetField16( pxDNSMessageHeader, DNSMessage_t, usAuthorityRRs, 0 ); /* No authority */
\r
907 vSetField16( pxDNSMessageHeader, DNSMessage_t, usAdditionalRRs, 0 ); /* No additional info */
\r
909 pxAnswer->ucNameCode = dnsNAME_IS_OFFSET;
\r
910 pxAnswer->ucNameOffset = ( uint8_t )( pcRequestedName - ( char * ) pucUDPPayloadBuffer );
\r
912 vSetField16( pxAnswer, LLMNRAnswer_t, usType, dnsTYPE_A_HOST ); /* Type A: host */
\r
913 vSetField16( pxAnswer, LLMNRAnswer_t, usClass, dnsCLASS_IN ); /* 1: Class IN */
\r
914 vSetField32( pxAnswer, LLMNRAnswer_t, ulTTL, dnsLLMNR_TTL_VALUE );
\r
915 vSetField16( pxAnswer, LLMNRAnswer_t, usDataLength, 4 );
\r
916 vSetField32( pxAnswer, LLMNRAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );
\r
918 usLength = ( int16_t ) ( sizeof( *pxAnswer ) + ( size_t ) ( pucByte - pucUDPPayloadBuffer ) );
\r
920 prvReplyDNSMessage( pxNetworkBuffer, usLength );
\r
922 if( pxNewBuffer != NULL )
\r
924 vReleaseNetworkBufferAndDescriptor( pxNewBuffer );
\r
929 #endif /* ipconfigUSE_LLMNR == 1 */
\r
932 return ulIPAddress;
\r
934 /*-----------------------------------------------------------*/
\r
936 #if( ipconfigUSE_NBNS == 1 )
\r
938 static void prvTreatNBNS( uint8_t *pucUDPPayloadBuffer, uint32_t ulIPAddress )
\r
940 uint16_t usFlags, usType, usClass;
\r
941 uint8_t *pucSource, *pucTarget;
\r
943 uint8_t ucNBNSName[ 17 ];
\r
945 usFlags = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usFlags ) );
\r
947 if( ( usFlags & dnsNBNS_FLAGS_OPCODE_MASK ) == dnsNBNS_FLAGS_OPCODE_QUERY )
\r
949 usType = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
\r
950 usClass = usChar2u16( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usClass ) );
\r
952 /* Not used for now */
\r
954 /* For NBNS a name is 16 bytes long, written with capitals only.
\r
955 Make sure that the copy is terminated with a zero. */
\r
956 pucTarget = ucNBNSName + sizeof(ucNBNSName ) - 2;
\r
957 pucTarget[ 1 ] = '\0';
\r
959 /* Start with decoding the last 2 bytes. */
\r
960 pucSource = pucUDPPayloadBuffer + ( offsetof( NBNSRequest_t, ucName ) + ( dnsNBNS_ENCODED_NAME_LENGTH - 2 ) );
\r
964 ucByte = ( uint8_t ) ( ( ( pucSource[ 0 ] - 0x41 ) << 4 ) | ( pucSource[ 1 ] - 0x41 ) );
\r
966 /* Make sure there are no trailing spaces in the name. */
\r
967 if( ( ucByte == ' ' ) && ( pucTarget[ 1 ] == '\0' ) )
\r
972 *pucTarget = ucByte;
\r
974 if( pucTarget == ucNBNSName )
\r
983 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
985 if( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) != 0 )
\r
987 /* If this is a response from another device,
\r
988 add the name to the DNS cache */
\r
989 prvProcessDNSCache( ( char * ) ucNBNSName, &ulIPAddress, pdFALSE );
\r
994 /* Avoid compiler warnings. */
\r
995 ( void ) ulIPAddress;
\r
997 #endif /* ipconfigUSE_DNS_CACHE */
\r
999 if( ( ( usFlags & dnsNBNS_FLAGS_RESPONSE ) == 0 ) &&
\r
1000 ( usType == dnsNBNS_TYPE_NET_BIOS ) &&
\r
1001 ( xApplicationDNSQueryHook( ( const char * ) ucNBNSName ) != pdFALSE ) )
\r
1003 uint16_t usLength;
\r
1004 DNSMessage_t *pxMessage;
\r
1005 NBNSAnswer_t *pxAnswer;
\r
1007 /* Someone is looking for a device with ucNBNSName,
\r
1008 prepare a positive reply. */
\r
1009 NetworkBufferDescriptor_t *pxNetworkBuffer = pxUDPPayloadBuffer_to_NetworkBuffer( pucUDPPayloadBuffer );
\r
1011 if( ( xBufferAllocFixedSize == pdFALSE ) && ( pxNetworkBuffer != NULL ) )
\r
1013 NetworkBufferDescriptor_t *pxNewBuffer;
\r
1014 BaseType_t xDataLength = pxNetworkBuffer->xDataLength + sizeof( UDPHeader_t ) +
\r
1016 sizeof( EthernetHeader_t ) + sizeof( IPHeader_t );
\r
1018 /* The field xDataLength was set to the length of the UDP payload.
\r
1019 The answer (reply) will be longer than the request, so the packet
\r
1020 must be duplicated into a bigger buffer */
\r
1021 pxNetworkBuffer->xDataLength = xDataLength;
\r
1022 pxNewBuffer = pxDuplicateNetworkBufferWithDescriptor( pxNetworkBuffer, xDataLength + 16 );
\r
1023 if( pxNewBuffer != NULL )
\r
1025 pucUDPPayloadBuffer = pxNewBuffer->pucEthernetBuffer + sizeof( UDPPacket_t );
\r
1026 pxNetworkBuffer = pxNewBuffer;
\r
1030 /* Just prevent that a reply will be sent */
\r
1031 pxNetworkBuffer = NULL;
\r
1035 /* Should not occur: pucUDPPayloadBuffer is part of a xNetworkBufferDescriptor */
\r
1036 if( pxNetworkBuffer != NULL )
\r
1038 pxMessage = (DNSMessage_t *)pucUDPPayloadBuffer;
\r
1040 /* As the fields in the structures are not word-aligned, we have to
\r
1041 copy the values byte-by-byte using macro's vSetField16() and vSetField32() */
\r
1042 vSetField16( pxMessage, DNSMessage_t, usFlags, dnsNBNS_QUERY_RESPONSE_FLAGS ); /* 0x8500 */
\r
1043 vSetField16( pxMessage, DNSMessage_t, usQuestions, 0 );
\r
1044 vSetField16( pxMessage, DNSMessage_t, usAnswers, 1 );
\r
1045 vSetField16( pxMessage, DNSMessage_t, usAuthorityRRs, 0 );
\r
1046 vSetField16( pxMessage, DNSMessage_t, usAdditionalRRs, 0 );
\r
1048 pxAnswer = (NBNSAnswer_t *)( pucUDPPayloadBuffer + offsetof( NBNSRequest_t, usType ) );
\r
1050 vSetField16( pxAnswer, NBNSAnswer_t, usType, usType ); /* Type */
\r
1051 vSetField16( pxAnswer, NBNSAnswer_t, usClass, dnsNBNS_CLASS_IN ); /* Class */
\r
1052 vSetField32( pxAnswer, NBNSAnswer_t, ulTTL, dnsNBNS_TTL_VALUE );
\r
1053 vSetField16( pxAnswer, NBNSAnswer_t, usDataLength, 6 ); /* 6 bytes including the length field */
\r
1054 vSetField16( pxAnswer, NBNSAnswer_t, usNbFlags, dnsNBNS_NAME_FLAGS );
\r
1055 vSetField32( pxAnswer, NBNSAnswer_t, ulIPAddress, FreeRTOS_ntohl( *ipLOCAL_IP_ADDRESS_POINTER ) );
\r
1057 usLength = ( uint16_t ) ( offsetof( NBNSRequest_t, usType ) + sizeof( NBNSAnswer_t ) );
\r
1059 prvReplyDNSMessage( pxNetworkBuffer, usLength );
\r
1065 #endif /* ipconfigUSE_NBNS */
\r
1066 /*-----------------------------------------------------------*/
\r
1068 static Socket_t prvCreateDNSSocket( void )
\r
1070 Socket_t xSocket = NULL;
\r
1071 struct freertos_sockaddr xAddress;
\r
1072 BaseType_t xReturn;
\r
1073 TickType_t xTimeoutTime = pdMS_TO_TICKS( 200 );
\r
1075 /* This must be the first time this function has been called. Create
\r
1077 xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
1079 /* Auto bind the port. */
\r
1080 xAddress.sin_port = 0u;
\r
1081 xReturn = FreeRTOS_bind( xSocket, &xAddress, sizeof( xAddress ) );
\r
1083 /* Check the bind was successful, and clean up if not. */
\r
1084 if( xReturn != 0 )
\r
1086 FreeRTOS_closesocket( xSocket );
\r
1091 /* Set the send and receive timeouts. */
\r
1092 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );
\r
1093 FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xTimeoutTime, sizeof( TickType_t ) );
\r
1098 /*-----------------------------------------------------------*/
\r
1100 #if( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) )
\r
1102 static void prvReplyDNSMessage( NetworkBufferDescriptor_t *pxNetworkBuffer, BaseType_t lNetLength )
\r
1104 UDPPacket_t *pxUDPPacket;
\r
1105 IPHeader_t *pxIPHeader;
\r
1106 UDPHeader_t *pxUDPHeader;
\r
1108 pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;
\r
1109 pxIPHeader = &pxUDPPacket->xIPHeader;
\r
1110 pxUDPHeader = &pxUDPPacket->xUDPHeader;
\r
1111 /* HT: started using defines like 'ipSIZE_OF_xxx' */
\r
1112 pxIPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER );
\r
1113 /* HT:endian: should not be translated, copying from packet to packet */
\r
1114 pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;
\r
1115 pxIPHeader->ulSourceIPAddress = *ipLOCAL_IP_ADDRESS_POINTER;
\r
1116 pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE;
\r
1117 pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier );
\r
1118 usPacketIdentifier++;
\r
1119 pxUDPHeader->usLength = FreeRTOS_htons( lNetLength + ipSIZE_OF_UDP_HEADER );
\r
1120 vFlip_16( pxUDPPacket->xUDPHeader.usSourcePort, pxUDPPacket->xUDPHeader.usDestinationPort );
\r
1122 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
\r
1124 /* calculate the IP header checksum */
\r
1125 pxIPHeader->usHeaderChecksum = 0x00;
\r
1126 pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
\r
1127 pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
\r
1129 /* calculate the UDP checksum for outgoing package */
\r
1130 usGenerateProtocolChecksum( ( uint8_t* ) pxUDPPacket, pdTRUE );
\r
1134 /* Important: tell NIC driver how many bytes must be sent */
\r
1135 pxNetworkBuffer->xDataLength = ( size_t ) ( lNetLength + ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER );
\r
1137 /* This function will fill in the eth addresses and send the packet */
\r
1138 vReturnEthernetFrame( pxNetworkBuffer, pdFALSE );
\r
1141 #endif /* ipconfigUSE_NBNS == 1 || ipconfigUSE_LLMNR == 1 */
\r
1142 /*-----------------------------------------------------------*/
\r
1144 #if( ipconfigUSE_DNS_CACHE == 1 )
\r
1146 static void prvProcessDNSCache( const char *pcName, uint32_t *pulIP, BaseType_t xLookUp )
\r
1149 BaseType_t xFound = pdFALSE;
\r
1150 static BaseType_t xFreeEntry = 0;
\r
1152 /* For each entry in the DNS cache table. */
\r
1153 for( x = 0; x < ipconfigDNS_CACHE_ENTRIES; x++ )
\r
1155 if( xDNSCache[ x ].pcName[ 0 ] == 0 )
\r
1160 if( strncmp( xDNSCache[ x ].pcName, pcName, sizeof( xDNSCache[ x ].pcName ) ) == 0 )
\r
1162 /* Is this function called for a lookup or to add/update an IP address? */
\r
1163 if( xLookUp != pdFALSE )
\r
1165 *pulIP = xDNSCache[ x ].ulIPAddress;
\r
1169 xDNSCache[ x ].ulIPAddress = *pulIP;
\r
1177 if( xFound == pdFALSE )
\r
1179 if( xLookUp != pdFALSE )
\r
1185 /* Called to add or update an item */
\r
1186 strncpy( xDNSCache[ xFreeEntry ].pcName, pcName, sizeof( xDNSCache[ xFreeEntry ].pcName ) );
\r
1187 xDNSCache[ xFreeEntry ].ulIPAddress = *pulIP;
\r
1190 if( xFreeEntry == ipconfigDNS_CACHE_ENTRIES )
\r
1197 if( ( xLookUp == 0 ) || ( *pulIP != 0 ) )
\r
1199 FreeRTOS_debug_printf( ( "prvProcessDNSCache: %s: '%s' @ %lxip\n", xLookUp ? "look-up" : "add", pcName, FreeRTOS_ntohl( *pulIP ) ) );
\r
1203 #endif /* ipconfigUSE_DNS_CACHE */
\r
1205 #endif /* ipconfigUSE_DNS != 0 */
\r