AzureMXChip/MXCHIPAZ3166/core/lib/netxduo/addons/dhcp/nxd_dhcpv6_server.c
UINT  _nx_dhcpv6_server_start(NX_DHCPV6_SERVER *dhcpv6_server_ptr)
{

UINT        status;
#ifdef NETXDUO_MULTI_HOME
UINT        if_index;
#endif


    /* Determine if the DHCPV6 server has already started.  */
    if (dhcpv6_server_ptr -> nx_dhcpv6_server_running)
    {

        /* Yes, it has.  */
        return(NX_DHCPV6_ALREADY_STARTED);
    }

    /* Check if the server has assignable addresses. */
    if (dhcpv6_server_ptr -> nx_dhcpv6_assignable_addresses == 0)
    {
        return  NX_DHCPV6_NO_ASSIGNABLE_ADDRESSES;
    }

    /* Check that the global address is valid. */
    if (dhcpv6_server_ptr -> nx_dhcpv6_server_ga_address_index >= NX_MAX_IPV6_ADDRESSES)
    {
        return NX_DHCPV6_INVALID_GLOBAL_INDEX;
    }

    /* Create the Socket and check the status */
    status = nx_udp_socket_create(dhcpv6_server_ptr -> nx_dhcpv6_ip_ptr, 
                                  &(dhcpv6_server_ptr -> nx_dhcpv6_server_socket), (CHAR *)"NetX DHCPV6 Server",
                                  NX_DHCPV6_TYPE_OF_SERVICE, NX_DHCPV6_FRAGMENT_OPTION, 
                                  NX_DHCPV6_TIME_TO_LIVE, NX_DHCPV6_QUEUE_DEPTH);

    /* Was the socket creation successful?  */
    if (status != NX_SUCCESS)
    {

        /* No, return error status.  */
        return status;
    }

    /* Save the DHCP instance pointer in the socket. */
    dhcpv6_server_ptr -> nx_dhcpv6_server_socket.nx_udp_socket_reserved_ptr =  (void *) dhcpv6_server_ptr;

    /* Set the DHCPv6 request handler for packets received on the DHCPv6 server socket. */
    status = nx_udp_socket_receive_notify(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket), _nx_dhcpv6_server_socket_receive_notify);

    /* Was the socket receive_notify set successful?  */
    if (status != NX_SUCCESS)
    {

        /* No, return error status.  */
        return status;
    }

    /* Check if the host application has created or retrieved a previously created server DUID. */
    if (dhcpv6_server_ptr -> nx_dhcpv6_server_duid.nx_duid_type == 0)
    {

        /* No valid server DUID detected. Server cannot run yet!*/
        return NX_DHCPV6_NO_SERVER_DUID;
    }

    /* Bind the UDP socket to the DHCPV6 Server port.  */
    status =  nx_udp_socket_bind(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket), NX_DHCPV6_SERVER_UDP_PORT, TX_WAIT_FOREVER);

    /* Check status.  */
    if (status != NX_SUCCESS)
    {

        /* Delete the UDP socket.  */
        nx_udp_socket_delete(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));

        return status;
    }

    /* Activate the DHCPv6 timers.  */
    status = tx_timer_activate(&dhcpv6_server_ptr -> nx_dhcpv6_lease_timer);

    /* Determine if the timer activate call was successful.  */
    if ((status != TX_SUCCESS) && (status != TX_ACTIVATE_ERROR))
    {

        /* No it was not; unbind the DHCPV6 socket.  */
        nx_udp_socket_unbind(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));

        /* Delete the UDP socket.  */
        nx_udp_socket_delete(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));

        /* Return the error status. */
        return status;
    }

    /* Start the session timer. */
    status = tx_timer_activate(&(dhcpv6_server_ptr -> nx_dhcpv6_session_timer));

    /* Check for invalid activate status. */
    if ((status != TX_SUCCESS) && (status != TX_ACTIVATE_ERROR))
    {

        /* No it was not; unbind the DHCPV6 socket.  */
        nx_udp_socket_unbind(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));

        /* Delete the UDP socket.  */
        nx_udp_socket_delete(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));

        /* Return error status. */
        return status;
    }

    /* Ready to resume the DHCPV6 server thread.  */
    status =  tx_thread_resume(&(dhcpv6_server_ptr -> nx_dhcpv6_server_thread));

    /* Determine if the resume was successful.  */
    if (status != NX_SUCCESS)
    {

        /* Deactivate the timer. */
        tx_timer_deactivate(&dhcpv6_server_ptr -> nx_dhcpv6_lease_timer);

        /* Unbind the DHCPV6 socket.  */
        nx_udp_socket_unbind(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));

        /* Delete the UDP socket.  */
        nx_udp_socket_delete(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));

        return status;
    }

    /* Set the DHCPV6 started flag to indicate the DHCPV6 server is now running.  */
    dhcpv6_server_ptr -> nx_dhcpv6_server_running =  NX_TRUE;

    /* Return completion status.  */
    return(status);  
}
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_dhcpv6_server_start                             PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function starts the DHCPV6 processing thread and prepares the  */
/*    DHCPv6 Server to receive DHCPv6 client requests.                    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    dhcpv6_server_ptr                     Pointer to DHCPV6 Server      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                              Completion status               */
/*    NX_DHCPV6_ALREADY_STARTED           DHCPv6 server already started   */
/*    NX_DHCPV6_MISSING_REQUIRED_OPTIONS  Server missing required options */
/*    NX_DHCPV6_NO_ASSIGNABLE_ADDRESSES   No addresses available to assign*/
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    nxd_ipv6_linklocal_address_set        Obtain MAC address for DUID   */
/*    nx_udp_socket_create                  Create the Server UDP socket  */
/*    nx_udp_socket_bind                    Bind the Server socket        */
/*    nx_udp_socket_unbind                  Unbind the Server socket      */
/*    tx_timer_activate                     Activate Server lease timer   */
/*    tx_timer_deactivate                   Deactivate Server lease timer */
/*    tx_thread_resume                      Resume Server thread task     */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/*  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_start                             PORTABLE C
6.1
AUTHOR

Yuxin Zhou, Microsoft Corporation

DESCRIPTION

This function starts the DHCPV6 processing thread and prepares the
DHCPv6 Server to receive DHCPv6 client requests.

INPUT

dhcpv6_server_ptr                     Pointer to DHCPV6 Server

OUTPUT

status                              Completion status
NX_DHCPV6_ALREADY_STARTED           DHCPv6 server already started
NX_DHCPV6_MISSING_REQUIRED_OPTIONS  Server missing required options
NX_DHCPV6_NO_ASSIGNABLE_ADDRESSES   No addresses available to assign

CALLS

nxd_ipv6_linklocal_address_set        Obtain MAC address for DUID
nx_udp_socket_create                  Create the Server UDP socket
nx_udp_socket_bind                    Bind the Server socket
nx_udp_socket_unbind                  Unbind the Server socket
tx_timer_activate                     Activate Server lease timer
tx_timer_deactivate                   Deactivate Server lease timer
tx_thread_resume                      Resume Server thread task

CALLED BY

Application Code

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_start PORTABLE C 6 . 1 AUTHOR Yuxin Zhou Microsoft Corporation DESCRIPTION This function starts the DHCPV6 processing thread and prepares the DHCPv6 Server to receive DHCPv6 client requests . INPUT dhcpv6_server_ptr Pointer to DHCPV6 Server OUTPUT status Completion status NX_DHCPV6_ALREADY_STARTED DHCPv6 server already started NX_DHCPV6_MISSING_REQUIRED_OPTIONS Server missing required options NX_DHCPV6_NO_ASSIGNABLE_ADDRESSES No addresses available to assign CALLS nxd_ipv6_linklocal_address_set Obtain MAC address for DUID nx_udp_socket_create Create the Server UDP socket nx_udp_socket_bind Bind the Server socket nx_udp_socket_unbind Unbind the Server socket tx_timer_activate Activate Server lease timer tx_timer_deactivate Deactivate Server lease timer tx_thread_resume Resume Server thread task CALLED BY Application Code 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
UINT  _nx_dhcpv6_server_start(NX_DHCPV6_SERVER *dhcpv6_server_ptr)
{
UINT        status;
#ifdef NETXDUO_MULTI_HOME
UINT        if_index;
#endif
    if (dhcpv6_server_ptr -> nx_dhcpv6_server_running)
    {
        return(NX_DHCPV6_ALREADY_STARTED);
    }
    if (dhcpv6_server_ptr -> nx_dhcpv6_assignable_addresses == 0)
    {
        return  NX_DHCPV6_NO_ASSIGNABLE_ADDRESSES;
    }
    if (dhcpv6_server_ptr -> nx_dhcpv6_server_ga_address_index >= NX_MAX_IPV6_ADDRESSES)
    {
        return NX_DHCPV6_INVALID_GLOBAL_INDEX;
    }
    status = nx_udp_socket_create(dhcpv6_server_ptr -> nx_dhcpv6_ip_ptr, 
                                  &(dhcpv6_server_ptr -> nx_dhcpv6_server_socket), (CHAR *)"NetX DHCPV6 Server",
                                  NX_DHCPV6_TYPE_OF_SERVICE, NX_DHCPV6_FRAGMENT_OPTION, 
                                  NX_DHCPV6_TIME_TO_LIVE, NX_DHCPV6_QUEUE_DEPTH);
    if (status != NX_SUCCESS)
    {
        return status;
    }
    dhcpv6_server_ptr -> nx_dhcpv6_server_socket.nx_udp_socket_reserved_ptr =  (void *) dhcpv6_server_ptr;
    status = nx_udp_socket_receive_notify(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket), _nx_dhcpv6_server_socket_receive_notify);
    if (status != NX_SUCCESS)
    {
        return status;
    }
    if (dhcpv6_server_ptr -> nx_dhcpv6_server_duid.nx_duid_type == 0)
    {
        return NX_DHCPV6_NO_SERVER_DUID;
    }
    status =  nx_udp_socket_bind(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket), NX_DHCPV6_SERVER_UDP_PORT, TX_WAIT_FOREVER);
    if (status != NX_SUCCESS)
    {
        nx_udp_socket_delete(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));
        return status;
    }
    status = tx_timer_activate(&dhcpv6_server_ptr -> nx_dhcpv6_lease_timer);
    if ((status != TX_SUCCESS) && (status != TX_ACTIVATE_ERROR))
    {
        nx_udp_socket_unbind(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));
        nx_udp_socket_delete(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));
        return status;
    }
    status = tx_timer_activate(&(dhcpv6_server_ptr -> nx_dhcpv6_session_timer));
    if ((status != TX_SUCCESS) && (status != TX_ACTIVATE_ERROR))
    {
        nx_udp_socket_unbind(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));
        nx_udp_socket_delete(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));
        return status;
    }
    status =  tx_thread_resume(&(dhcpv6_server_ptr -> nx_dhcpv6_server_thread));
    if (status != NX_SUCCESS)
    {
        tx_timer_deactivate(&dhcpv6_server_ptr -> nx_dhcpv6_lease_timer);
        nx_udp_socket_unbind(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));
        nx_udp_socket_delete(&(dhcpv6_server_ptr -> nx_dhcpv6_server_socket));
        return status;
    }
    dhcpv6_server_ptr -> nx_dhcpv6_server_running =  NX_TRUE;
    return(status);  
}
UINT _nx_dhcpv6_server_start ( NX_DHCPV6_SERVER * dhcpv6_server_ptr ) { UINT status ; #ifdef NETXDUO_MULTI_HOME UINT if_index ; #endif if ( dhcpv6_server_ptr -> nx_dhcpv6_server_running ) { return ( NX_DHCPV6_ALREADY_STARTED ) ; } if ( dhcpv6_server_ptr -> nx_dhcpv6_assignable_addresses == 0 ) { return NX_DHCPV6_NO_ASSIGNABLE_ADDRESSES ; } if ( dhcpv6_server_ptr -> nx_dhcpv6_server_ga_address_index >= NX_MAX_IPV6_ADDRESSES ) { return NX_DHCPV6_INVALID_GLOBAL_INDEX ; } status = nx_udp_socket_create ( dhcpv6_server_ptr -> nx_dhcpv6_ip_ptr , & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) , ( CHAR * ) " " , NX_DHCPV6_TYPE_OF_SERVICE , NX_DHCPV6_FRAGMENT_OPTION , NX_DHCPV6_TIME_TO_LIVE , NX_DHCPV6_QUEUE_DEPTH ) ; if ( status != NX_SUCCESS ) { return status ; } dhcpv6_server_ptr -> nx_dhcpv6_server_socket . nx_udp_socket_reserved_ptr = ( void * ) dhcpv6_server_ptr ; status = nx_udp_socket_receive_notify ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) , _nx_dhcpv6_server_socket_receive_notify ) ; if ( status != NX_SUCCESS ) { return status ; } if ( dhcpv6_server_ptr -> nx_dhcpv6_server_duid . nx_duid_type == 0 ) { return NX_DHCPV6_NO_SERVER_DUID ; } status = nx_udp_socket_bind ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) , NX_DHCPV6_SERVER_UDP_PORT , TX_WAIT_FOREVER ) ; if ( status != NX_SUCCESS ) { nx_udp_socket_delete ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) ) ; return status ; } status = tx_timer_activate ( & dhcpv6_server_ptr -> nx_dhcpv6_lease_timer ) ; if ( ( status != TX_SUCCESS ) && ( status != TX_ACTIVATE_ERROR ) ) { nx_udp_socket_unbind ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) ) ; nx_udp_socket_delete ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) ) ; return status ; } status = tx_timer_activate ( & ( dhcpv6_server_ptr -> nx_dhcpv6_session_timer ) ) ; if ( ( status != TX_SUCCESS ) && ( status != TX_ACTIVATE_ERROR ) ) { nx_udp_socket_unbind ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) ) ; nx_udp_socket_delete ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) ) ; return status ; } status = tx_thread_resume ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_thread ) ) ; if ( status != NX_SUCCESS ) { tx_timer_deactivate ( & dhcpv6_server_ptr -> nx_dhcpv6_lease_timer ) ; nx_udp_socket_unbind ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) ) ; nx_udp_socket_delete ( & ( dhcpv6_server_ptr -> nx_dhcpv6_server_socket ) ) ; return status ; } dhcpv6_server_ptr -> nx_dhcpv6_server_running = NX_TRUE ; return ( status ) ; }
FUNCTION                                               RELEASE
FUNCTION RELEASE
/* Determine if the DHCPV6 server has already started.  */ /* Yes, it has.  */ /* Check if the server has assignable addresses. */ /* Check that the global address is valid. */ /* Create the Socket and check the status */ /* Was the socket creation successful?  */ /* No, return error status.  */ /* Save the DHCP instance pointer in the socket. */ /* Set the DHCPv6 request handler for packets received on the DHCPv6 server socket. */ /* Was the socket receive_notify set successful?  */ /* No, return error status.  */ /* Check if the host application has created or retrieved a previously created server DUID. */ /* No valid server DUID detected. Server cannot run yet!*/ /* Bind the UDP socket to the DHCPV6 Server port.  */ /* Check status.  */ /* Delete the UDP socket.  */ /* Activate the DHCPv6 timers.  */ /* Determine if the timer activate call was successful.  */ /* No it was not; unbind the DHCPV6 socket.  */ /* Delete the UDP socket.  */ /* Return the error status. */ /* Start the session timer. */ /* Check for invalid activate status. */ /* No it was not; unbind the DHCPV6 socket.  */ /* Delete the UDP socket.  */ /* Return error status. */ /* Ready to resume the DHCPV6 server thread.  */ /* Determine if the resume was successful.  */ /* Deactivate the timer. */ /* Unbind the DHCPV6 socket.  */ /* Delete the UDP socket.  */ /* Set the DHCPV6 started flag to indicate the DHCPV6 server is now running.  */ /* Return completion status.  */