1
0
mirror of https://github.com/FreeRTOS/FreeRTOS-Plus-TCP synced 2025-10-21 23:30:39 +08:00
Files
FreeRTOS-Plus-TCP/source/FreeRTOS_DNS_Callback.c
Tony Josi 0e9628796c Fixing doxygen comments (#728)
* updating doxygen config

* fixing doxygen comments

* adding IPv6 files and fixing comments

* fix doxygen cfg and file names in comments

* wip doxygen v6 docs

* adding doxygen comments

* include RA src file to doxgendocs generation

* fix spell check issues

* Uncrustify: triggered by comment.

* fix minor build issue

* fix spell check issues

* Uncrustify: triggered by comment

* fix trailing white space

* Dev integration hein.v8 (#738)

* Updating tcp utilities

* Some more change in dev_integration_hein.v8

* In FreeRTOS_DNS_Parser.c : use 'ipUDP_PAYLOAD_OFFSET_IPv4' in stead of 'ipIP_PAYLOAD_OFFSET'

* And a few more corrections

* Changes to WinPCap network interface, removed debugging code

* After applying uncrustify

* Oops, I forgot the push changes in include files.

* Now removing it, hopefully

---------

Co-authored-by: Nikhil Kamath <110539926+amazonKamath@users.noreply.github.com>
Co-authored-by: Monika Singh <108652024+moninom1@users.noreply.github.com>

* Fix CBMC proofs for DNS (#718)

* Use CBMC XML output to enable VSCode debugger (#673)

Prior to this commit, CBMC would emit logging information in plain text
format, which does not contain information required for the CBMC VSCode
debugger. This commit makes CBMC use XML instead of plain text.

Co-authored-by: Mark Tuttle <tuttle@acm.org>

* wip

* wip DNSgetHostByName

* wip DNSgetHostByName

* fixed cbmc proof for DNS_ReadNameField

* wip DNSgetHostByName_a_harness

* Fix CBMC prooff for DNSgetHostByName

* wip fix DNSgetHostByName_a CBMC proof

* fixed cbmc target func not called issue in DNSclear

* fixed cbmc target func not called issue in DNSlookup

* fix DNSgetHostByName_a CBMC proof

* update comments

* more asserts

* fixing formatting

* updating as per review comments

* fix dns after review comments

* adding more asserts

* adds more asserts

* minor fix

* fixing comments

* fixing comments

* fixing minor issue

* fixing DNS_ReadReply() signature

* making code more consistant

* adding more  asserts

* making code more consistent

---------

Co-authored-by: Kareem Khazem <karkhaz@amazon.com>
Co-authored-by: Mark Tuttle <tuttle@acm.org>

* Uncrustify: triggered by comment

* fixing formatting

---------

Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: Hein Tibosch <hein_tibosch@yahoo.es>
Co-authored-by: Nikhil Kamath <110539926+amazonKamath@users.noreply.github.com>
Co-authored-by: Monika Singh <108652024+moninom1@users.noreply.github.com>
Co-authored-by: Kareem Khazem <karkhaz@amazon.com>
Co-authored-by: Mark Tuttle <tuttle@acm.org>
2023-02-24 13:58:53 +05:30

277 lines
11 KiB
C

/*
* FreeRTOS+TCP <DEVELOPMENT BRANCH>
* Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/**
* @file FreeRTOS_DNS_Callback.c
* @brief File that handles the DNS Callback option
*/
#include "FreeRTOS_DNS_Callback.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_IP_Private.h"
#include "FreeRTOS_DNS_Globals.h"
#include "FreeRTOS_IP_Timers.h"
#if ( ( ipconfigDNS_USE_CALLBACKS == 1 ) && ( ipconfigUSE_DNS != 0 ) )
/**
* @brief list of callbacks to send
*/
static List_t xCallbackList;
/**
* @brief A DNS reply was received, see if there is any matching entry and
* call the handler.
*
* @param[in,out] pxSet: a set of variables that are shared among the helper functions.
* @param[in] pxAddress: Pointer to address info ( IPv4/IPv6 ) obtained from the DNS server.
*
* @return Returns pdTRUE if uxIdentifier was recognized.
*/
BaseType_t xDNSDoCallback( ParseSet_t * pxSet,
struct freertos_addrinfo * pxAddress )
{
BaseType_t xResult = pdFALSE;
const ListItem_t * pxIterator;
const ListItem_t * xEnd = listGET_END_MARKER( &xCallbackList );
TickType_t uxIdentifier = ( TickType_t ) pxSet->pxDNSMessageHeader->usIdentifier;
/* While iterating through the list, the scheduler is suspended.
* Remember which function shall be called once the scheduler is
* running again. */
FOnDNSEvent pCallbackFunction = NULL;
void * pvSearchID = NULL;
vTaskSuspendAll();
{
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
pxIterator != ( const ListItem_t * ) xEnd;
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
{
BaseType_t xMatching;
DNSCallback_t * pxCallback = ( ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator ) );
#if ( ipconfigUSE_MDNS == 1 )
/* mDNS port 5353. */
if( pxSet->usPortNumber == FreeRTOS_htons( ipMDNS_PORT ) )
{
/* In mDNS, the query ID field is ignored and the
* hostname will be compared with outstanding requests. */
xMatching = ( strcasecmp( pxCallback->pcName, pxSet->pcName ) == 0 ) ? pdTRUE : pdFALSE;
}
else
#endif /* if ( ipconfigUSE_MDNS == 1 ) */
{
xMatching = ( listGET_LIST_ITEM_VALUE( pxIterator ) == uxIdentifier ) ? pdTRUE : pdFALSE;
}
if( xMatching == pdTRUE )
{
pvSearchID = pxCallback->pvSearchID;
pCallbackFunction = pxCallback->pCallbackFunction;
( void ) uxListRemove( &pxCallback->xListItem );
vPortFree( pxCallback );
if( listLIST_IS_EMPTY( &xCallbackList ) != pdFALSE )
{
/* The list of outstanding requests is empty. No need for periodic polling. */
vIPSetDNSTimerEnableState( pdFALSE );
}
xResult = pdTRUE;
break;
}
}
}
( void ) xTaskResumeAll();
if( pCallbackFunction != NULL )
{
pCallbackFunction( pxSet->pcName, pvSearchID, pxAddress );
}
return xResult;
}
/**
* @brief FreeRTOS_gethostbyname_a() was called along with callback parameters.
* Store them in a list for later reference.
*
* @param[in] pcHostName: The hostname whose IP address is being searched for.
* @param[in] pvSearchID: The search ID of the DNS callback function to set.
* @param[in] pCallbackFunction: The callback function pointer.
* @param[in] uxTimeout: Timeout of the callback function.
* @param[in] uxIdentifier: Random number used as ID in the DNS message.
* @param[in] xIsIPv6: pdTRUE if the address type should be IPv6.
*/
void vDNSSetCallBack( const char * pcHostName,
void * pvSearchID,
FOnDNSEvent pCallbackFunction,
TickType_t uxTimeout,
TickType_t uxIdentifier,
BaseType_t xIsIPv6 )
{
size_t lLength = strlen( pcHostName );
DNSCallback_t * pxCallback = ( ( DNSCallback_t * ) pvPortMalloc( sizeof( *pxCallback ) + lLength ) );
/* Translate from ms to number of clock ticks. */
uxTimeout /= portTICK_PERIOD_MS;
if( pxCallback != NULL )
{
if( listLIST_IS_EMPTY( &xCallbackList ) != pdFALSE )
{
/* This is the first one, start the DNS timer to check for timeouts */
vDNSTimerReload( FreeRTOS_min_uint32( 1000U, uxTimeout ) );
}
( void ) strcpy( pxCallback->pcName, pcHostName );
pxCallback->pCallbackFunction = pCallbackFunction;
pxCallback->pvSearchID = pvSearchID;
pxCallback->uxRemainingTime = uxTimeout;
pxCallback->xIsIPv6 = xIsIPv6;
vTaskSetTimeOutState( &( pxCallback->uxTimeoutState ) );
listSET_LIST_ITEM_OWNER( &( pxCallback->xListItem ), ( void * ) pxCallback );
listSET_LIST_ITEM_VALUE( &( pxCallback->xListItem ), uxIdentifier );
vTaskSuspendAll();
{
vListInsertEnd( &xCallbackList, &pxCallback->xListItem );
}
( void ) xTaskResumeAll();
}
else
{
FreeRTOS_debug_printf( ( " vDNSSetCallBack : Could not allocate memory: %u bytes",
( unsigned ) ( sizeof( *pxCallback ) + lLength ) ) );
}
}
/*-----------------------------------------------------------*/
/**
* @brief Iterate through the list of call-back structures and remove
* old entries which have reached a timeout.
* As soon as the list has become empty, the DNS timer will be stopped.
* In case pvSearchID is supplied, the user wants to cancel a DNS request.
*
* @param[in] pvSearchID: The search ID of callback function whose associated
* DNS request is being cancelled. If non-ID specific checking of
* all requests is required, then this field should be kept as NULL.
*/
void vDNSCheckCallBack( void * pvSearchID )
{
const ListItem_t * pxIterator;
const ListItem_t * xEnd = listGET_END_MARKER( &xCallbackList );
/* When a DNS-search times out, the call-back function shall
* be called. Store theses item in a temporary list.
* Only when the scheduler is running, user functions
* shall be called. */
List_t xTempList;
vListInitialise( &xTempList );
vTaskSuspendAll();
{
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
pxIterator != xEnd; )
{
DNSCallback_t * pxCallback = ( ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator ) );
/* Move to the next item because we might remove this item */
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator );
if( ( pvSearchID != NULL ) && ( pvSearchID == pxCallback->pvSearchID ) )
{
( void ) uxListRemove( &( pxCallback->xListItem ) );
vPortFree( pxCallback );
}
else if( xTaskCheckForTimeOut( &pxCallback->uxTimeoutState, &( pxCallback->uxRemainingTime ) ) != pdFALSE )
{
/* A time-out occurred in the asynchronous search.
* Remove it from xCallbackList. */
( void ) uxListRemove( &( pxCallback->xListItem ) );
/* Insert it in a temporary list. The function will be called
* once the scheduler is resumed. */
vListInsertEnd( &( xTempList ), &pxCallback->xListItem );
}
else
{
/* This call-back is still waiting for a reply or a time-out. */
}
}
}
( void ) xTaskResumeAll();
if( listLIST_IS_EMPTY( &xTempList ) == pdFALSE )
{
/* There is at least one item in xTempList which must be removed and deleted. */
xEnd = listGET_END_MARKER( &xTempList );
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( xEnd );
pxIterator != xEnd;
)
{
DNSCallback_t * pxCallback = ( ( DNSCallback_t * ) listGET_LIST_ITEM_OWNER( pxIterator ) );
/* Move to the next item because we might remove this item */
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator );
/* A time-out occurred in the asynchronous search.
* Call the application hook with the proper information. */
if( pxCallback->xIsIPv6 != 0 )
{
pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, NULL );
}
else
{
pxCallback->pCallbackFunction( pxCallback->pcName, pxCallback->pvSearchID, 0U );
}
/* Remove it from 'xTempList' and free the memory. */
( void ) uxListRemove( &( pxCallback->xListItem ) );
vPortFree( pxCallback );
}
}
if( listLIST_IS_EMPTY( &xCallbackList ) != pdFALSE )
{
vIPSetDNSTimerEnableState( pdFALSE );
}
}
/*-----------------------------------------------------------*/
/**
* @brief initialize the cache
* @post will modify global list xCallbackList
*/
void vDNSCallbackInitialise()
{
vListInitialise( &xCallbackList );
}
#endif /* if ( ipconfigDNS_USE_CALLBACKS == 1 ) */