AzureMXChip/MXCHIPAZ3166/core/lib/netxduo/addons/dhcp/nxd_dhcpv6_server.c
VOID  _nx_dhcpv6_server_thread_entry(ULONG info)
{

UINT                    i;
NX_DHCPV6_SERVER        *dhcpv6_server_ptr;
NX_DHCPV6_CLIENT        *dhcpv6_client_ptr;
ULONG                   dhcpv6_events;
NXD_ADDRESS             client_address;


    /* Setup the DHCPV6 server pointer.  */
    NX_THREAD_EXTENSION_PTR_GET(dhcpv6_server_ptr, NX_DHCPV6_SERVER, info)

    /* Continue processing periodic tasks till this internal flag is cleared. */
    while (1)
    {


        /* Pick up IP event flags.  */
        tx_event_flags_get(&dhcpv6_server_ptr -> nx_dhcpv6_server_timer_events, NX_DHCPV6_ALL_EVENTS, 
                           TX_OR_CLEAR, &dhcpv6_events, TX_WAIT_FOREVER);

        if (dhcpv6_events & NX_DHCPV6_SERVER_RECEIVE_EVENT)
        {

            /* Listen for new Client DHCP service request e.g. DISCOVERY messages. */
            _nx_dhcpv6_listen_for_messages(dhcpv6_server_ptr);
        }

        if (dhcpv6_events & NX_DHCPV6_CHECK_SESSION_PERIODIC_EVENT)
        {
            /* Check the active clients whose sessions that have timed out. */
            for (i = 0; i < NX_DHCPV6_MAX_CLIENTS; i++)
            {

                /* Set a local pointer for convenience. */
                dhcpv6_client_ptr = &dhcpv6_server_ptr -> nx_dhcpv6_clients[i];

                tx_mutex_get(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex, NX_WAIT_FOREVER);

                /* Is this client in session with the server? */
                if (dhcpv6_client_ptr -> nx_dhcpv6_client_session_time)
                {
                
                    /* Update the Client session time.  */
                    dhcpv6_client_ptr -> nx_dhcpv6_client_session_time += NX_DHCPV6_SESSION_TIMER_INTERVAL;
    
                    if (dhcpv6_client_ptr -> nx_dhcpv6_client_session_time >= NX_DHCPV6_SESSION_TIMEOUT)
                    {

                        /* The session has timed out without a response from the Client. Invalidate
                           the client entry. */

                        /* First get the assigned address. */
                        COPY_IPV6_ADDRESS(&dhcpv6_client_ptr -> nx_dhcpv6_ia.nx_global_address.nxd_ip_address.v6[0], 
                                          &client_address.nxd_ip_address.v6[0]);

                        /* Clear the client record. */
                        _nx_dhcpv6_clear_client_record(dhcpv6_server_ptr, dhcpv6_client_ptr);
                    }
                }

                tx_mutex_put(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex);
            }
        }

        /* Check for a periodic DHCP server task.  */
        if (dhcpv6_events & NX_DHCPV6_IP_LEASE_CHECK_PERIODIC_EVENT)
        {

            /* Check the active clients for the time remaining in their leased IP address. */
            for (i = 0; i < NX_DHCPV6_MAX_CLIENTS; i++)
            {

                /* Set a local pointer for convenience. */
                dhcpv6_client_ptr = &dhcpv6_server_ptr -> nx_dhcpv6_clients[i];

                tx_mutex_get(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex, NX_WAIT_FOREVER);

                /* Skip those clients who do not have a lease assigned yet. */
                if (dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued == 0)
                {

                    tx_mutex_put(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex);

                    continue;
                }

                /* Update the time accrued on the client's current IP address lease. */
                dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued += NX_DHCPV6_IP_LEASE_TIMER_INTERVAL;

                /* Has this address become obsolete? */
                if (dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued >= dhcpv6_client_ptr -> nx_dhcpv6_ia.nx_valid_lifetime)
                {

                    tx_mutex_put(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex);

                    /* Clear the client record. */
                    _nx_dhcpv6_clear_client_record(dhcpv6_server_ptr, dhcpv6_client_ptr);
                }
                else if (dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued >= 
                         dhcpv6_client_ptr -> nx_dhcpv6_ia.nx_preferred_lifetime)
                {

                    /* Address is deprecated! Client *should* not use it but no action taken by the server yet. */
                }

                tx_mutex_put(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex);
            }
        }

        /* Relinquish control before another cycle.*/
        tx_thread_sleep(1);
    }
}
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_dhcpv6_server_thread_entry                      PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the background processing thread for the DHCPV6    */
/*    Client.  It waits to be notified by ThreadX event flags when to     */
/*    perform certain actions.  These include updating the time remaining */
/*    on the Client IP address lease time, and maintaining the duration of*/
/*    the current Client Server session (if Client has made a request).   */
/*    It will terminate if a host application handler is called and       */
/*    indicates the Client should abort.                                  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    info                               Pointer to DHCPV6 Client instance*/
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_event_flags_get                Receive notice of event flags set */
/*    tx_mutex_get                      Obtain lock on Client resource    */
/*    tx_mutex_put                      Release lock on Client resource   */
/*    nx_dhcpv6_deprecated_IP_address_handler                             */
/*                                      Callback function for handling an */
/*                                         IP address at the end of its   */
/*                                         preferred life time            */
/*    nx_dhcpv6_expired_IP_address_handler                                */
/*                                      Callback function for handling an */
/*                                         IP address at the end of its   */
/*                                         valid life time                */
/*    _nx_dhcpv6_request_renew          Initiate the RENEW request        */
/*    _nx_dhcpv6_request_rebind         Initiate the REBIND request       */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    ThreadX                                                             */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
/*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
/*                                            resulting in version 6.1    */
/*                                                                        */
/**************************************************************************/
FUNCTION                                               RELEASE

_nx_dhcpv6_server_thread_entry                      PORTABLE C
6.1
AUTHOR

Yuxin Zhou, Microsoft Corporation

DESCRIPTION

This function is the background processing thread for the DHCPV6
Client.  It waits to be notified by ThreadX event flags when to
perform certain actions.  These include updating the time remaining
on the Client IP address lease time, and maintaining the duration of
the current Client Server session (if Client has made a request).
It will terminate if a host application handler is called and
indicates the Client should abort.

INPUT

info                               Pointer to DHCPV6 Client instance

OUTPUT

None

CALLS

tx_event_flags_get                Receive notice of event flags set
tx_mutex_get                      Obtain lock on Client resource
tx_mutex_put                      Release lock on Client resource
nx_dhcpv6_deprecated_IP_address_handler
Callback function for handling an
IP address at the end of its
preferred life time
nx_dhcpv6_expired_IP_address_handler
Callback function for handling an
IP address at the end of its
valid life time
_nx_dhcpv6_request_renew          Initiate the RENEW request
_nx_dhcpv6_request_rebind         Initiate the REBIND request

CALLED BY



RELEASE HISTORY

DATE              NAME                      DESCRIPTION

05-19-2020     Yuxin Zhou               Initial Version 6.0
09-30-2020     Yuxin Zhou               Modified comment(s),
resulting in version 6.1
FUNCTION RELEASE _nx_dhcpv6_server_thread_entry PORTABLE C 6 . 1 AUTHOR Yuxin Zhou Microsoft Corporation DESCRIPTION This function is the background processing thread for the DHCPV6 Client . It waits to be notified by ThreadX event flags when to perform certain actions . These include updating the time remaining on the Client IP address lease time and maintaining the duration of the current Client Server session ( if Client has made a request ) . It will terminate if a host application handler is called and indicates the Client should abort . INPUT info Pointer to DHCPV6 Client instance OUTPUT None CALLS tx_event_flags_get Receive notice of event flags set tx_mutex_get Obtain lock on Client resource tx_mutex_put Release lock on Client resource nx_dhcpv6_deprecated_IP_address_handler Callback function for handling an IP address at the end of its preferred life time nx_dhcpv6_expired_IP_address_handler Callback function for handling an IP address at the end of its valid life time _nx_dhcpv6_request_renew Initiate the RENEW request _nx_dhcpv6_request_rebind Initiate the REBIND request CALLED BY RELEASE HISTORY DATE NAME DESCRIPTION 05 - 19 - 2020 Yuxin Zhou Initial Version 6 . 0 09 - 30 - 2020 Yuxin Zhou Modified comment ( s ) resulting in version 6 . 1
VOID  _nx_dhcpv6_server_thread_entry(ULONG info)
{
UINT                    i;
NX_DHCPV6_SERVER        *dhcpv6_server_ptr;
NX_DHCPV6_CLIENT        *dhcpv6_client_ptr;
ULONG                   dhcpv6_events;
NXD_ADDRESS             client_address;
    NX_THREAD_EXTENSION_PTR_GET(dhcpv6_server_ptr, NX_DHCPV6_SERVER, info)
    while (1)
    {
        tx_event_flags_get(&dhcpv6_server_ptr -> nx_dhcpv6_server_timer_events, NX_DHCPV6_ALL_EVENTS, 
                           TX_OR_CLEAR, &dhcpv6_events, TX_WAIT_FOREVER);
        if (dhcpv6_events & NX_DHCPV6_SERVER_RECEIVE_EVENT)
        {
            _nx_dhcpv6_listen_for_messages(dhcpv6_server_ptr);
        }
        if (dhcpv6_events & NX_DHCPV6_CHECK_SESSION_PERIODIC_EVENT)
        {
            for (i = 0; i < NX_DHCPV6_MAX_CLIENTS; i++)
            {
                dhcpv6_client_ptr = &dhcpv6_server_ptr -> nx_dhcpv6_clients[i];
                tx_mutex_get(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex, NX_WAIT_FOREVER);
                if (dhcpv6_client_ptr -> nx_dhcpv6_client_session_time)
                {
                    dhcpv6_client_ptr -> nx_dhcpv6_client_session_time += NX_DHCPV6_SESSION_TIMER_INTERVAL;
                    if (dhcpv6_client_ptr -> nx_dhcpv6_client_session_time >= NX_DHCPV6_SESSION_TIMEOUT)
                    {
                        COPY_IPV6_ADDRESS(&dhcpv6_client_ptr -> nx_dhcpv6_ia.nx_global_address.nxd_ip_address.v6[0], 
                                          &client_address.nxd_ip_address.v6[0]);
                        _nx_dhcpv6_clear_client_record(dhcpv6_server_ptr, dhcpv6_client_ptr);
                    }
                }
                tx_mutex_put(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex);
            }
        }
        if (dhcpv6_events & NX_DHCPV6_IP_LEASE_CHECK_PERIODIC_EVENT)
        {
            for (i = 0; i < NX_DHCPV6_MAX_CLIENTS; i++)
            {
                dhcpv6_client_ptr = &dhcpv6_server_ptr -> nx_dhcpv6_clients[i];
                tx_mutex_get(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex, NX_WAIT_FOREVER);
                if (dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued == 0)
                {
                    tx_mutex_put(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex);
                    continue;
                }
                dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued += NX_DHCPV6_IP_LEASE_TIMER_INTERVAL;
                if (dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued >= dhcpv6_client_ptr -> nx_dhcpv6_ia.nx_valid_lifetime)
                {
                    tx_mutex_put(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex);
                    _nx_dhcpv6_clear_client_record(dhcpv6_server_ptr, dhcpv6_client_ptr);
                }
                else if (dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued >= 
                         dhcpv6_client_ptr -> nx_dhcpv6_ia.nx_preferred_lifetime)
                {
                }
                tx_mutex_put(&dhcpv6_server_ptr -> nx_dhcpv6_server_mutex);
            }
        }
        tx_thread_sleep(1);
    }
}
VOID _nx_dhcpv6_server_thread_entry ( ULONG info ) { UINT i ; NX_DHCPV6_SERVER * dhcpv6_server_ptr ; NX_DHCPV6_CLIENT * dhcpv6_client_ptr ; ULONG dhcpv6_events ; NXD_ADDRESS client_address ; NX_THREAD_EXTENSION_PTR_GET ( dhcpv6_server_ptr , NX_DHCPV6_SERVER , info ) while ( 1 ) { tx_event_flags_get ( & dhcpv6_server_ptr -> nx_dhcpv6_server_timer_events , NX_DHCPV6_ALL_EVENTS , TX_OR_CLEAR , & dhcpv6_events , TX_WAIT_FOREVER ) ; if ( dhcpv6_events & NX_DHCPV6_SERVER_RECEIVE_EVENT ) { _nx_dhcpv6_listen_for_messages ( dhcpv6_server_ptr ) ; } if ( dhcpv6_events & NX_DHCPV6_CHECK_SESSION_PERIODIC_EVENT ) { for ( i = 0 ; i < NX_DHCPV6_MAX_CLIENTS ; i ++ ) { dhcpv6_client_ptr = & dhcpv6_server_ptr -> nx_dhcpv6_clients [ i ] ; tx_mutex_get ( & dhcpv6_server_ptr -> nx_dhcpv6_server_mutex , NX_WAIT_FOREVER ) ; if ( dhcpv6_client_ptr -> nx_dhcpv6_client_session_time ) { dhcpv6_client_ptr -> nx_dhcpv6_client_session_time += NX_DHCPV6_SESSION_TIMER_INTERVAL ; if ( dhcpv6_client_ptr -> nx_dhcpv6_client_session_time >= NX_DHCPV6_SESSION_TIMEOUT ) { COPY_IPV6_ADDRESS ( & dhcpv6_client_ptr -> nx_dhcpv6_ia . nx_global_address . nxd_ip_address . v6 [ 0 ] , & client_address . nxd_ip_address . v6 [ 0 ] ) ; _nx_dhcpv6_clear_client_record ( dhcpv6_server_ptr , dhcpv6_client_ptr ) ; } } tx_mutex_put ( & dhcpv6_server_ptr -> nx_dhcpv6_server_mutex ) ; } } if ( dhcpv6_events & NX_DHCPV6_IP_LEASE_CHECK_PERIODIC_EVENT ) { for ( i = 0 ; i < NX_DHCPV6_MAX_CLIENTS ; i ++ ) { dhcpv6_client_ptr = & dhcpv6_server_ptr -> nx_dhcpv6_clients [ i ] ; tx_mutex_get ( & dhcpv6_server_ptr -> nx_dhcpv6_server_mutex , NX_WAIT_FOREVER ) ; if ( dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued == 0 ) { tx_mutex_put ( & dhcpv6_server_ptr -> nx_dhcpv6_server_mutex ) ; continue ; } dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued += NX_DHCPV6_IP_LEASE_TIMER_INTERVAL ; if ( dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued >= dhcpv6_client_ptr -> nx_dhcpv6_ia . nx_valid_lifetime ) { tx_mutex_put ( & dhcpv6_server_ptr -> nx_dhcpv6_server_mutex ) ; _nx_dhcpv6_clear_client_record ( dhcpv6_server_ptr , dhcpv6_client_ptr ) ; } else if ( dhcpv6_client_ptr -> nx_dhcpv6_IP_lease_time_accrued >= dhcpv6_client_ptr -> nx_dhcpv6_ia . nx_preferred_lifetime ) { } tx_mutex_put ( & dhcpv6_server_ptr -> nx_dhcpv6_server_mutex ) ; } } tx_thread_sleep ( 1 ) ; } }
FUNCTION                                               RELEASE
FUNCTION RELEASE
/* Setup the DHCPV6 server pointer.  */ /* Continue processing periodic tasks till this internal flag is cleared. */ /* Pick up IP event flags.  */ /* Listen for new Client DHCP service request e.g. DISCOVERY messages. */ /* Check the active clients whose sessions that have timed out. */ /* Set a local pointer for convenience. */ /* Is this client in session with the server? */ /* Update the Client session time.  */ /* The session has timed out without a response from the Client. Invalidate
                           the client entry. */ /* First get the assigned address. */ /* Clear the client record. */ /* Check for a periodic DHCP server task.  */ /* Check the active clients for the time remaining in their leased IP address. */ /* Set a local pointer for convenience. */ /* Skip those clients who do not have a lease assigned yet. */ /* Update the time accrued on the client's current IP address lease. */ /* Has this address become obsolete? */ /* Clear the client record. */ /* Address is deprecated! Client *should* not use it but no action taken by the server yet. */ /* Relinquish control before another cycle.*/